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_ES_H
21 #define _MPEG_ES_H
22 
23 /**
24  * @file mpeg_es.h
25  * @ingroup dvb_table
26  * @brief Provides the table parser for the MPEG-TS 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-2
32  *
33  * @see
34  * http://dvd.sourceforge.net/dvdinfo/mpeghdrs.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  * @def DVB_MPEG_ES_PIC_START
45  *	@brief Picture Start
46  *	@ingroup dvb_table
47  * @def DVB_MPEG_ES_USER_DATA
48  *	@brief User Data
49  *	@ingroup dvb_table
50  * @def DVB_MPEG_ES_SEQ_START
51  *	@brief Sequence Start
52  *	@ingroup dvb_table
53  * @def DVB_MPEG_ES_SEQ_EXT
54  *	@brief Extension
55  *	@ingroup dvb_table
56  * @def DVB_MPEG_ES_GOP
57  *	@brief Group Of Pictures
58  *	@ingroup dvb_table
59  * @def DVB_MPEG_ES_SLICES
60  *	@brief Slices
61  *	@ingroup dvb_table
62  */
63 #define DVB_MPEG_ES_PIC_START  0x00
64 #define DVB_MPEG_ES_USER_DATA  0xb2
65 #define DVB_MPEG_ES_SEQ_START  0xb3
66 #define DVB_MPEG_ES_SEQ_EXT    0xb5
67 #define DVB_MPEG_ES_GOP        0xb8
68 #define DVB_MPEG_ES_SLICES     0x01 ... 0xaf
69 
70 /**
71  * @struct dvb_mpeg_es_seq_start
72  * @brief MPEG ES Sequence header
73  * @ingroup dvb_table
74  *
75  * @param type		DVB_MPEG_ES_SEQ_START
76  * @param sync		Sync bytes
77  * @param framerate	Framerate
78  * @param aspect	Aspect ratio
79  * @param height	Height
80  * @param width		Width
81  * @param qm_nonintra	Load non-intra quantizer matrix
82  * @param qm_intra	Load intra quantizer matrix
83  * @param constrained	Constrained parameters flag
84  * @param vbv		VBV buffer size
85  * @param one		Should be 1
86  * @param bitrate	Bitrate
87  */
88 struct dvb_mpeg_es_seq_start {
89 	union {
90 		uint32_t bitfield;
91 		struct {
92 			uint32_t  type:8;
93 			uint32_t  sync:24;
94 		} __attribute__((packed));
95 	} __attribute__((packed));
96 	union {
97 		uint32_t bitfield2;
98 		struct {
99 			uint32_t framerate:4;
100 			uint32_t aspect:4;
101 			uint32_t height:12;
102 			uint32_t width:12;
103 		} __attribute__((packed));
104 	} __attribute__((packed));
105 	union {
106 		uint32_t bitfield3;
107 		struct {
108 			uint32_t qm_nonintra:1;
109 			uint32_t qm_intra:1;
110 			uint32_t constrained:1;
111 			uint32_t vbv:10; // Size of video buffer verifier = 16*1024*vbv buf size
112 			uint32_t one:1;
113 			uint32_t bitrate:18;
114 		} __attribute__((packed));
115 	} __attribute__((packed));
116 } __attribute__((packed));
117 
118 /**
119  * @struct dvb_mpeg_es_pic_start
120  * @brief MPEG ES Picture start header
121  * @ingroup dvb_table
122  *
123  * @param type		DVB_MPEG_ES_PIC_START
124  * @param sync		Sync bytes
125  * @param dummy		Unused
126  * @param vbv_delay	VBV delay
127  * @param coding_type	Frame type (enum dvb_mpeg_es_frame_t)
128  * @param temporal_ref	Temporal sequence number
129  */
130 struct dvb_mpeg_es_pic_start {
131 	union {
132 		uint32_t bitfield;
133 		struct {
134 			uint32_t  type:8;
135 			uint32_t  sync:24;
136 		} __attribute__((packed));
137 	} __attribute__((packed));
138 	union {
139 		uint32_t bitfield2;
140 		struct {
141 			uint32_t dummy:3;
142 			uint32_t vbv_delay:16;
143 			uint32_t coding_type:3;
144 			uint32_t temporal_ref:10;
145 		} __attribute__((packed));
146 	} __attribute__((packed));
147 } __attribute__((packed));
148 
149 /**
150  * @enum dvb_mpeg_es_frame_t
151  * @brief MPEG frame types
152  * @ingroup dvb_table
153  *
154  * @var DVB_MPEG_ES_FRAME_UNKNOWN
155  *	@brief	Unknown frame
156  * @var DVB_MPEG_ES_FRAME_I
157  *	@brief	I frame
158  * @var DVB_MPEG_ES_FRAME_P
159  *	@brief	P frame
160  * @var DVB_MPEG_ES_FRAME_B
161  *	@brief	B frame
162  * @var DVB_MPEG_ES_FRAME_D
163  *	@brief	D frame
164  */
165 enum dvb_mpeg_es_frame_t
166 {
167 	DVB_MPEG_ES_FRAME_UNKNOWN,
168 	DVB_MPEG_ES_FRAME_I,
169 	DVB_MPEG_ES_FRAME_P,
170 	DVB_MPEG_ES_FRAME_B,
171 	DVB_MPEG_ES_FRAME_D
172 };
173 
174 /**
175  * @brief Vector that translates from enum dvb_mpeg_es_frame_t to string.
176  * @ingroup dvb_table
177  */
178 extern const char *dvb_mpeg_es_frame_names[5];
179 
180 struct dvb_v5_fe_parms;
181 
182 #ifdef __cplusplus
183 extern "C" {
184 #endif
185 
186 /**
187  * @brief Initialize a struct dvb_mpeg_es_seq_start from buffer
188  * @ingroup dvb_table
189  *
190  * @param buf		Buffer
191  * @param buflen	Length of buffer
192  * @param seq_start	Pointer to allocated struct dvb_mpeg_es_seq_start
193  *
194  * @return		If buflen too small, return -1, 0 otherwise.
195  *
196  * This function copies the length of struct dvb_mpeg_es_seq_start
197  * to seq_start and fixes endianness. seq_start has to be allocated
198  * with malloc.
199  */
200 int  dvb_mpeg_es_seq_start_init (const uint8_t *buf, ssize_t buflen,
201 		struct dvb_mpeg_es_seq_start *seq_start);
202 
203 /**
204  * @brief Print details of struct dvb_mpeg_es_seq_start
205  * @ingroup dvb_table
206  *
207  * @param parms		struct dvb_v5_fe_parms for log functions
208  * @param seq_start	Pointer to struct dvb_mpeg_es_seq_start to print
209  *
210  * This function prints the fields of struct dvb_mpeg_es_seq_start
211  */
212 void dvb_mpeg_es_seq_start_print(struct dvb_v5_fe_parms *parms,
213 		struct dvb_mpeg_es_seq_start *seq_start);
214 
215 /**
216  * @brief Initialize a struct dvb_mpeg_es_pic_start from buffer
217  * @ingroup dvb_table
218  *
219  * @param buf		Buffer
220  * @param buflen	Length of buffer
221  * @param pic_start	Pointer to allocated structdvb_mpeg_es_pic_start
222  *
223  * @return		If buflen too small, return -1, 0 otherwise.
224  *
225  * This function copies the length of struct dvb_mpeg_es_pic_start
226  * to pic_start	and fixes endianness. seq_start has to be allocated
227  * with malloc.
228  */
229 int  dvb_mpeg_es_pic_start_init (const uint8_t *buf, ssize_t buflen,
230 		struct dvb_mpeg_es_pic_start *pic_start);
231 
232 /**
233  * @brief Print details of struct dvb_mpeg_es_pic_start
234  * @ingroup dvb_table
235  *
236  * @param parms		struct dvb_v5_fe_parms for log functions
237  * @param pic_start	Pointer to struct dvb_mpeg_es_pic_start to print
238  *
239  * This function prints the fields of struct dvb_mpeg_es_pic_start
240  */
241 void dvb_mpeg_es_pic_start_print(struct dvb_v5_fe_parms *parms,
242 		struct dvb_mpeg_es_pic_start *pic_start);
243 
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif
249