1 /*
2  * Copyright (c) 2013-2014 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 #ifndef _MPEG_TS_H
21 #define _MPEG_TS_H
22 
23 /**
24  * @file mpeg_ts.h
25  * @ingroup dvb_table
26  * @brief Provides the table parser for the MPEG-PES Elementary Stream
27  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
28  * @author Andre Roth
29  *
30  * @par Relevant specs
31  * The table described herein is defined in ISO 13818-1
32  *
33  * @see
34  * http://en.wikipedia.org/wiki/MPEG_transport_stream
35  *
36  * @par Bug Report
37  * Please submit bug reports and patches to linux-media@vger.kernel.org
38  */
39 #include <stdint.h>
40 #include <unistd.h> /* ssize_t */
41 
42 /**
43  * @def DVB_MPEG_TS
44  *	@brief MPEG Transport Stream magic
45  *	@ingroup dvb_table
46  * @def DVB_MPEG_TS_PACKET_SIZE
47  *	@brief Size of an MPEG packet
48  *	@ingroup dvb_table
49  */
50 #define DVB_MPEG_TS  0x47
51 #define DVB_MPEG_TS_PACKET_SIZE  188
52 
53 /**
54  * @struct dvb_mpeg_ts_adaption
55  * @brief MPEG TS header adaption field
56  * @ingroup dvb_table
57  *
58  * @param type			DVB_MPEG_ES_SEQ_START
59  * @param length		1 bit	Adaptation Field Length
60  * @param discontinued		1 bit	Discontinuity indicator
61  * @param random_access		1 bit	Random Access indicator
62  * @param priority		1 bit	Elementary stream priority indicator
63  * @param PCR			1 bit	PCR flag
64  * @param OPCR			1 bit	OPCR flag
65  * @param splicing_point	1 bit	Splicing point flag
66  * @param private_data		1 bit	Transport private data flag
67  * @param extension		1 bit	Adaptation field extension flag
68  * @param data			Pointer to data
69  */
70 struct dvb_mpeg_ts_adaption {
71 	uint8_t length;
72 	struct {
73 		uint8_t extension:1;
74 		uint8_t private_data:1;
75 		uint8_t splicing_point:1;
76 		uint8_t OPCR:1;
77 		uint8_t PCR:1;
78 		uint8_t priority:1;
79 		uint8_t random_access:1;
80 		uint8_t discontinued:1;
81 	} __attribute__((packed));
82 	uint8_t data[];
83 } __attribute__((packed));
84 
85 /**
86  * @struct dvb_mpeg_ts
87  * @brief MPEG TS header
88  * @ingroup dvb_table
89  *
90  * @param sync_byte		DVB_MPEG_TS
91  * @param tei			1 bit	Transport Error Indicator
92  * @param payload_start		1 bit	Payload Unit Start Indicator
93  * @param priority		1 bit	Transport Priority
94  * @param pid			13 bits	Packet Identifier
95  * @param scrambling		2 bits	Scrambling control
96  * @param adaptation_field	1 bit	Adaptation field exist
97  * @param payload		1 bit	Contains payload
98  * @param continuity_counter	4 bits	Continuity counter
99  * @param adaption		Pointer to optional adaption fiels (struct dvb_mpeg_ts_adaption)
100  */
101 struct dvb_mpeg_ts {
102 	uint8_t sync_byte;
103 	union {
104 		uint16_t bitfield;
105 		struct {
106 			uint16_t pid:13;
107 			uint16_t priority:1;
108 			uint16_t payload_start:1;
109 			uint16_t tei:1;
110 		} __attribute__((packed));
111 	} __attribute__((packed));
112 	struct {
113 		uint8_t continuity_counter:4;
114 		uint8_t payload:1;
115 		uint8_t adaptation_field:1;
116 		uint8_t scrambling:2;
117 	} __attribute__((packed));
118 	struct dvb_mpeg_ts_adaption adaption[];
119 } __attribute__((packed));
120 
121 struct dvb_v5_fe_parms;
122 
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126 
127 /**
128  * @brief Initialize a struct dvb_mpeg_ts from buffer
129  * @ingroup dvb_table
130  *
131  * @param parms		struct dvb_v5_fe_parms for log functions
132  * @param buf		Buffer
133  * @param buflen	Length of buffer
134  * @param table		Pointer to allocated struct dvb_mpeg_ts
135  * @param table_length	Pointer to size_t where length will be written to
136  *
137  * @return		Length of data in table
138  *
139  * This function copies the length of struct dvb_mpeg_ts
140  * to table and fixes endianness. The pointer table has to be allocated
141  * on stack or dynamically.
142  */
143 ssize_t dvb_mpeg_ts_init (struct dvb_v5_fe_parms *parms, const uint8_t *buf, ssize_t buflen,
144 		uint8_t *table, ssize_t *table_length);
145 
146 /**
147  * @brief Deallocate memory associated with a struct dvb_mpeg_ts
148  * @ingroup dvb_table
149  *
150  * @param ts	struct dvb_mpeg_ts to be deallocated
151  *
152  * If ts was allocated dynamically, this function
153  * can be used to free the memory.
154  */
155 void dvb_mpeg_ts_free(struct dvb_mpeg_ts *ts);
156 
157 /**
158  * @brief Print details of struct dvb_mpeg_ts
159  * @ingroup dvb_table
160  *
161  * @param parms		struct dvb_v5_fe_parms for log functions
162  * @param ts    	Pointer to struct dvb_mpeg_ts to print
163  *
164  * This function prints the fields of struct dvb_mpeg_ts
165  */
166 void dvb_mpeg_ts_print(struct dvb_v5_fe_parms *parms, struct dvb_mpeg_ts *ts);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif
173