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_PES_H
21 #define _MPEG_PES_H
22 
23 /**
24  * @file mpeg_pes.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://dvd.sourceforge.net/dvdinfo/pes-hdr.html
35  *
36  * @par Bug Report
37  * Please submit bug reports and patches to linux-media@vger.kernel.org
38  */
39 
40 #include <stdint.h>
41 #include <unistd.h> /* ssize_t */
42 
43 
44 /**
45  * @def DVB_MPEG_PES
46  *	@brief MPEG Packetized Elementary Stream magic
47  *	@ingroup dvb_table
48  * @def DVB_MPEG_PES_AUDIO
49  *	@brief PES Audio
50  *	@ingroup dvb_table
51  * @def DVB_MPEG_PES_VIDEO
52  *	@brief PES Video
53  *	@ingroup dvb_table
54  * @def DVB_MPEG_STREAM_MAP
55  *	@brief PES Stream map
56  *	@ingroup dvb_table
57  * @def DVB_MPEG_STREAM_PADDING
58  *	@brief PES padding
59  *	@ingroup dvb_table
60  * @def DVB_MPEG_STREAM_PRIVATE_2
61  *	@brief PES private
62  *	@ingroup dvb_table
63  * @def DVB_MPEG_STREAM_ECM
64  *	@brief PES ECM Stream
65  *	@ingroup dvb_table
66  * @def DVB_MPEG_STREAM_EMM
67  *	@brief PES EMM Stream
68  *	@ingroup dvb_table
69  * @def DVB_MPEG_STREAM_DIRECTORY
70  *	@brief PES Stream directory
71  *	@ingroup dvb_table
72  * @def DVB_MPEG_STREAM_DSMCC
73  *	@brief PES DSMCC
74  *	@ingroup dvb_table
75  * @def DVB_MPEG_STREAM_H222E
76  *	@brief PES H.222.1 type E
77  *	@ingroup dvb_table
78  */
79 
80 #define DVB_MPEG_PES  0x00001
81 
82 #define DVB_MPEG_PES_AUDIO  0xc0 ... 0xcf
83 #define DVB_MPEG_PES_VIDEO  0xe0 ... 0xef
84 
85 #define DVB_MPEG_STREAM_MAP       0xBC
86 #define DVB_MPEG_STREAM_PADDING   0xBE
87 #define DVB_MPEG_STREAM_PRIVATE_2 0x5F
88 #define DVB_MPEG_STREAM_ECM       0x70
89 #define DVB_MPEG_STREAM_EMM       0x71
90 #define DVB_MPEG_STREAM_DIRECTORY 0xFF
91 #define DVB_MPEG_STREAM_DSMCC     0x7A
92 #define DVB_MPEG_STREAM_H222E     0xF8
93 
94 /**
95  * @struct ts_t
96  * @brief MPEG PES timestamp structure, used for dts and pts
97  * @ingroup dvb_table
98  *
99  * @param tag		4 bits  Should be 0010 for PTS and 0011 for DTS
100  * @param bits30	3 bits	Timestamp bits 30-32
101  * @param one		1 bit	Sould be 1
102  * @param bits15	15 bits	Timestamp bits 15-29
103  * @param one1		1 bit	Should be 1
104  * @param bits00	15 Bits	Timestamp bits 0-14
105  * @param one2		1 bit	Should be 1
106  */
107 
108 struct ts_t {
109 	uint8_t  one:1;
110 	uint8_t  bits30:3;
111 	uint8_t  tag:4;
112 
113 	union {
114 		uint16_t bitfield;
115 		struct {
116 			uint16_t  one1:1;
117 			uint16_t  bits15:15;
118 		} __attribute__((packed));
119 	} __attribute__((packed));
120 
121 	union {
122 		uint16_t bitfield2;
123 		struct {
124 			uint16_t  one2:1;
125 			uint16_t  bits00:15;
126 		} __attribute__((packed));
127 	} __attribute__((packed));
128 } __attribute__((packed));
129 
130 /**
131  * @struct dvb_mpeg_pes_optional
132  * @brief MPEG PES optional header
133  * @ingroup dvb_table
134  *
135  * @param two				2 bits	Should be 10
136  * @param PES_scrambling_control	2 bits	PES Scrambling Control (Not Scrambled=00, otherwise scrambled)
137  * @param PES_priority			1 bit	PES Priority
138  * @param data_alignment_indicator	1 bit	PES data alignment
139  * @param copyright			1 bit	PES content protected by copyright
140  * @param original_or_copy		1 bit	PES content is original (=1) or copied (=0)
141  * @param PTS_DTS			2 bit	PES header contains PTS (=10, =11) and/or DTS (=01, =11)
142  * @param ESCR				1 bit	PES header contains ESCR fields
143  * @param ES_rate			1 bit	PES header contains ES_rate field
144  * @param DSM_trick_mode		1 bit	PES header contains DSM_trick_mode field
145  * @param additional_copy_info		1 bit	PES header contains additional_copy_info field
146  * @param PES_CRC			1 bit	PES header contains CRC field
147  * @param PES_extension			1 bit	PES header contains extension field
148  * @param length			8 bit	PES header data length
149  * @param pts				64 bit	PES PTS timestamp
150  * @param dts				64 bit	PES DTS timestamp
151  */
152 struct dvb_mpeg_pes_optional {
153 	union {
154 		uint16_t bitfield;
155 		struct {
156 			uint16_t PES_extension:1;
157 			uint16_t PES_CRC:1;
158 			uint16_t additional_copy_info:1;
159 			uint16_t DSM_trick_mode:1;
160 			uint16_t ES_rate:1;
161 			uint16_t ESCR:1;
162 			uint16_t PTS_DTS:2;
163 			uint16_t original_or_copy:1;
164 			uint16_t copyright:1;
165 			uint16_t data_alignment_indicator:1;
166 			uint16_t PES_priority:1;
167 			uint16_t PES_scrambling_control:2;
168 			uint16_t two:2;
169 		} __attribute__((packed));
170 	} __attribute__((packed));
171 	uint8_t length;
172 	uint64_t pts;
173 	uint64_t dts;
174 } __attribute__((packed));
175 
176 /**
177  * @struct dvb_mpeg_pes
178  * @brief MPEG PES data structure
179  * @ingroup dvb_table
180  *
181  * @param sync		24 bits	DVB_MPEG_PES
182  * @param stream_id	8 bits	PES Stream ID
183  * @param length	16 bits	PES packet length
184  * @param optional	Pointer to optional PES header
185  */
186 struct dvb_mpeg_pes {
187 	union {
188 		uint32_t bitfield;
189 		struct {
190 			uint32_t  stream_id:8;
191 			uint32_t  sync:24;
192 		} __attribute__((packed));
193 	} __attribute__((packed));
194 	uint16_t length;
195 	struct dvb_mpeg_pes_optional optional[];
196 } __attribute__((packed));
197 
198 struct dvb_v5_fe_parms;
199 
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
203 
204 /**
205  * @brief Initialize a struct dvb_mpeg_pes from buffer
206  * @ingroup dvb_table
207  *
208  * @param parms		struct dvb_v5_fe_parms for log functions
209  * @param buf		Buffer
210  * @param buflen	Length of buffer
211  * @param table		Pointer to allocated struct dvb_mpeg_pes
212  *
213  * @return		Length of data in table
214  *
215  * This function copies the length of struct dvb_mpeg_pes
216  * to table and fixes endianness. The pointer table has to be
217  * allocated on stack or dynamically.
218  */
219 ssize_t dvb_mpeg_pes_init(struct dvb_v5_fe_parms *parms, const uint8_t *buf, ssize_t buflen,
220 		uint8_t *table);
221 
222 /**
223  * @brief Deallocate memory associated with a struct dvb_mpeg_pes
224  * @ingroup dvb_table
225  *
226  * @param pes	struct dvb_mpeg_pes to be deallocated
227  *
228  * If the pointer pes was allocated dynamically, this function
229  * can be used to free the memory.
230  */
231 void dvb_mpeg_pes_free(struct dvb_mpeg_pes *pes);
232 
233 /**
234  * @brief Print details of struct dvb_mpeg_pes
235  * @ingroup dvb_table
236  *
237  * @param parms		struct dvb_v5_fe_parms for log functions
238  * @param pes    	Pointer to struct dvb_mpeg_pes to print
239  *
240  * This function prints the fields of struct dvb_mpeg_pes
241  */
242 void dvb_mpeg_pes_print (struct dvb_v5_fe_parms *parms, struct dvb_mpeg_pes *pes);
243 
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif
249