codecparsers: mpeg: Add an mpeg video bitstream parsing library
[platform/upstream/gstreamer.git] / gst-libs / gst / codecparsers / gstmpegvideoparser.h
1 /* Gstreamer
2  * Copyright (C) <2011> Intel Corporation
3  * Copyright (C) <2011> Collabora Ltd.
4  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
5  *
6  * From bad/sys/vdpau/mpeg/mpegutil.c:
7  *   Copyright (C) <2007> Jan Schmidt <thaytan@mad.scientist.com>
8  *   Copyright (C) <2009> Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #ifndef __GST_MPEG_VIDEO_UTILS_H__
27 #define __GST_MPEG_VIDEO_UTILS_H__
28
29 #include <gst/gst.h>
30
31 G_BEGIN_DECLS
32
33 /**
34  * GstMpegVideoPacketTypeCode:
35  * @GST_MPEG_VIDEO_PACKET_PICTURE: Picture packet starting code
36  * @GST_MPEG_VIDEO_PACKET_SLICE_MIN: Picture packet starting code
37  * @GST_MPEG_VIDEO_PACKET_SLICE_MAX: Slice max packet starting code
38  * @GST_MPEG_VIDEO_PACKET_SEQUENCE : Sequence packet starting code
39  * @GST_MPEG_VIDEO_PACKET_EXTENSION: Extension packet starting code
40  * @GST_MPEG_VIDEO_PACKET_SEQUENCE_END: Sequence end packet code
41  * @GST_MPEG_VIDEO_PACKET_GOP: Group of Picture packet starting code
42  * @GST_MPEG_VIDEO_PACKET_NONE: None packet code
43  *
44  * Indicates the type of MPEG packet
45  */
46 typedef enum {
47   GST_MPEG_VIDEO_PACKET_PICTURE      = 0x00,
48   GST_MPEG_VIDEO_PACKET_SLICE_MIN    = 0x01,
49   GST_MPEG_VIDEO_PACKET_SLICE_MAX    = 0xaf,
50   GST_MPEG_VIDEO_PACKET_SEQUENCE     = 0xb3,
51   GST_MPEG_VIDEO_PACKET_EXTENSION    = 0xb5,
52   GST_MPEG_VIDEO_PACKET_SEQUENCE_END = 0xb7,
53   GST_MPEG_VIDEO_PACKET_GOP          = 0xb8,
54   GST_MPEG_VIDEO_PACKET_NONE         = 0xff
55 } GstMpegVideoPacketTypeCode;
56
57 /**
58  * GstMpegVideoPacketExtensionCode:
59  * @GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE: Sequence extension code
60  * @GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY: Display extension code
61  * @GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX: Quantizer extension code
62  * @GST_MPEG_VIDEO_PACKET_EXT_GOP: Group Of Picture extension code
63  *
64  * Indicates what type of packets are in this
65  * block, some are mutually * exclusive though - ie, sequence packs are
66  * accumulated separately. GOP & Picture may occur together or separately
67  */
68 typedef enum {
69   GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE         = 0x01,
70   GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY = 0x02,
71   GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX     = 0x03,
72   GST_MPEG_VIDEO_PACKET_EXT_GOP              = 0x04
73 } GstMpegVideoPacketExtensionCode;
74
75 /**
76  * GstMpegVideoLevel:
77  * @GST_MPEG_VIDEO_LEVEL_LOW
78  * @GST_MPEG_VIDEO_LEVEL_MAIN
79  * @GST_MPEG_VIDEO_LEVEL_HIGH_1440
80  * @GST_MPEG_VIDEO_LEVEL_HIGH
81  *
82  * Indicates the level in use
83  **/
84 typedef enum {
85  GST_MPEG_VIDEO_LEVEL_HIGH      = 0x04,
86  GST_MPEG_VIDEO_LEVEL_HIGH_1440 = 0x06,
87  GST_MPEG_VIDEO_LEVEL_MAIN      = 0x08,
88  GST_MPEG_VIDEO_LEVEL_LOW       = 0x0a
89 } GstMpegVideoLevel;
90
91 /**
92  * GstMpegVideoProfile:
93  * @GST_MPEG_VIDEO_PROFILE_422,
94  * @GST_MPEG_VIDEO_PROFILE_HIGH,
95  * @GST_MPEG_VIDEO_PROFILE_SPATIALLY_SCALABLE,
96  * @GST_MPEG_VIDEO_PROFILE_SNR_SCALABLE,
97  * @GST_MPEG_VIDEO_PROFILE_MAIN,
98  * @GST_MPEG_VIDEO_PROFILE_SIMPLE,
99  *
100  * Indicates the profile type in use
101  **/
102 typedef enum {
103   GST_MPEG_VIDEO_PROFILE_422                 = 0x00,
104   GST_MPEG_VIDEO_PROFILE_HIGH                = 0x01,
105   GST_MPEG_VIDEO_PROFILE_SPATIALLY_SCALABLE  = 0x02,
106   GST_MPEG_VIDEO_PROFILE_SNR_SCALABLE        = 0x03,
107   GST_MPEG_VIDEO_PROFILE_MAIN                = 0x04,
108   GST_MPEG_VIDEO_PROFILE_SIMPLE              = 0x05
109 } GstMpegVideoProfile;
110
111 /**
112  * GstMpegVideoPictureType:
113  * @GST_MPEG_VIDEO_PICTURE_TYPE_I: Type I
114  * @GST_MPEG_VIDEO_PICTURE_TYPE_P: Type P
115  * @GST_MPEG_VIDEO_PICTURE_TYPE_B: Type B
116  * @GST_MPEG_VIDEO_PICTURE_TYPE_D: Type D
117  *
118  * Indicates the type of picture
119  */
120 typedef enum {
121   GST_MPEG_VIDEO_PICTURE_TYPE_I = 0x01,
122   GST_MPEG_VIDEO_PICTURE_TYPE_P = 0x02,
123   GST_MPEG_VIDEO_PICTURE_TYPE_B = 0x03,
124   GST_MPEG_VIDEO_PICTURE_TYPE_D = 0x04
125 } GstMpegVideoPictureType;
126
127 /**
128  * GstMpegVideoPictureStructure:
129  * @GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD: Top field
130  * @GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD: Bottom field
131  * @GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME: Frame
132  *
133  * Indicates the structure of picture
134  */
135 typedef enum {
136     GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD    = 0x01,
137     GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD = 0x02,
138     GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME        = 0x03
139 } GstMpegVideoPictureStructure;
140
141 typedef struct _GstMpegVideoSequenceHdr     GstMpegVideoSequenceHdr;
142 typedef struct _GstMpegVideoSequenceExt     GstMpegVideoSequenceExt;
143 typedef struct _GstMpegVideoPictureHdr      GstMpegVideoPictureHdr;
144 typedef struct _GstMpegVideoGop             GstMpegVideoGop;
145 typedef struct _GstMpegVideoPictureExt      GstMpegVideoPictureExt;
146 typedef struct _GstMpegVideoQuantMatrixExt  GstMpegVideoQuantMatrixExt;
147 typedef struct _GstMpegVideoTypeOffsetSize  GstMpegVideoTypeOffsetSize;
148
149 /**
150  * GstMpegVideoSequenceHdr:
151  * @width: Width of each frame
152  * @height: Height of each frame
153  * @par_w: Calculated Pixel Aspect Ratio width
154  * @par_h: Pixel Aspect Ratio height
155  * @fps_n: Calculated Framrate nominator
156  * @fps_d: Calculated Framerate denominator
157  * @bitrate_value: Value of the bitrate as is in the stream (400bps unit)
158  * @bitrate: the real bitrate of the Mpeg video stream in bits per second, 0 if VBR stream
159  * @constrained_parameters_flag: %TRUE if this stream uses contrained parameters.
160  * @intra_quantizer_matrix: intra-quantization table
161  * @non_intra_quantizer_matrix: non-intra quantization table
162  *
163  * The Mpeg2 Video Sequence Header structure.
164  */
165 struct _GstMpegVideoSequenceHdr
166 {
167   guint16 width, height;
168   guint8 aspect_ratio_info;
169   guint8 frame_rate_code;
170   guint32 bitrate_value;
171   guint16 vbv_buffer_size_value;
172
173   guint8 constrained_parameters_flag;
174
175   guint8 intra_quantizer_matrix[64];
176   guint8 non_intra_quantizer_matrix[64];
177
178   /* Calculated values */
179   guint par_w, par_h;
180   guint fps_n, fps_d;
181   guint bitrate;
182 };
183
184 /**
185  * GstMpegVideoSequenceExt:
186  * @profile: mpeg2 decoder profil
187  * @level: mpeg2 decoder level
188  * @progressive: %TRUE if the frames are progressive %FALSE otherwize
189  * @chroma_format: indicates the chrominance format
190  * @horiz_size_ext: Horizontal size
191  * @vert_size_ext: Vertical size
192  * @bitrate_ext: The bitrate
193  * @vbv_buffer_size_extension: Vbv vuffer size
194  * @low_delay: %TRUE if the sequence doesn't contain any B-pitcture, %FALSE
195  * otherwize
196  * @fps_n_ext: Framerate nominator code
197  * @fps_d_ext: Framerate denominator code
198  *
199  * The Mpeg2 Video Sequence Extension structure.
200  **/
201 struct _GstMpegVideoSequenceExt
202 {
203   /* mpeg2 decoder profile */
204   guint8 profile;
205   /* mpeg2 decoder level */
206   guint8 level;
207
208   guint8 progressive;
209   guint8 chroma_format;
210
211   guint8 horiz_size_ext, vert_size_ext;
212
213   guint16 bitrate_ext;
214   guint8 vbv_buffer_size_extension;
215   guint8 low_delay;
216   guint8 fps_n_ext, fps_d_ext;
217
218 };
219
220 /**
221  * GstMpegVideoQuantMatrixExt:
222  * @load_intra_quantiser_matrix
223  * @intra_quantiser_matrix
224  * @load_non_intra_quantiser_matrix
225  * @non_intra_quantiser_matrix:
226  * @load_chroma_intra_quantiser_matrix
227  * @chroma_intra_quantiser_matrix
228  * @load_chroma_non_intra_quantiser_matrix
229  * @chroma_non_intra_quantiser_matrix
230  *
231  * The Quant Matrix Extension structure
232  */
233 struct _GstMpegVideoQuantMatrixExt
234 {
235  guint8 load_intra_quantiser_matrix;
236  guint8 intra_quantiser_matrix[64];
237  guint8 load_non_intra_quantiser_matrix;
238  guint8 non_intra_quantiser_matrix[64];
239  guint8 load_chroma_intra_quantiser_matrix;
240  guint8 chroma_intra_quantiser_matrix[64];
241  guint8 load_chroma_non_intra_quantiser_matrix;
242  guint8 chroma_non_intra_quantiser_matrix[64];
243 };
244
245 /**
246  * GstMpegVideoPictureHdr:
247  * @tsn: Temporal Sequence Number
248  * @pic_type: Type of the frame
249  * @full_pel_forward_vector: the full pel forward flag of
250  *  the frame: 0 or 1.
251  * @full_pel_backward_vector: the full pel backward flag
252  *  of the frame: 0 or 1.
253  * @f_code: F code
254  *
255  * The Mpeg2 Video Picture Header structure.
256  */
257 struct _GstMpegVideoPictureHdr
258 {
259   guint16 tsn;
260   guint8 pic_type;
261
262   guint8 full_pel_forward_vector, full_pel_backward_vector;
263
264   guint8 f_code[2][2];
265 };
266
267 /**
268  * GstMpegVideoPictureExt:
269  * @intra_dc_precision: Intra DC precision
270  * @picture_structure: Structure of the picture
271  * @top_field_first: Top field first
272  * @frame_pred_frame_dct: Frame
273  * @concealment_motion_vectors: Concealment Motion Vectors
274  * @q_scale_type: Q Scale Type
275  * @intra_vlc_format: Intra Vlc Format
276  * @alternate_scan: Alternate Scan
277  * @repeat_first_field: Repeat First Field
278  * @chroma_420_type: Chroma 420 Type
279  * @progressive_frame: %TRUE if the frame is progressive %FALSE otherwize
280  *
281  * The Mpeg2 Video Picture Extension structure.
282  */
283 struct _GstMpegVideoPictureExt
284 {
285   guint8 f_code[2][2];
286
287   guint8 intra_dc_precision;
288   guint8 picture_structure;
289   guint8 top_field_first;
290   guint8 frame_pred_frame_dct;
291   guint8 concealment_motion_vectors;
292   guint8 q_scale_type;
293   guint8 intra_vlc_format;
294   guint8 alternate_scan;
295   guint8 repeat_first_field;
296   guint8 chroma_420_type;
297   guint8 progressive_frame;
298   guint8 composite_display;
299   guint8 v_axis;
300   guint8 field_sequence;
301   guint8 sub_carrier;
302   guint8 burst_amplitude;
303   guint8 sub_carrier_phase;
304 };
305
306 /**
307  * GstMpegVideoGop:
308  * @drop_frame_flag: Drop Frame Flag
309  * @hour: Hour (0-23)
310  * @minute: Minute (O-59)
311  * @second: Second (0-59)
312  * @frame: Frame (0-59)
313  * @closed_gop: Closed Gop
314  * @broken_gop: Broken Gop
315  *
316  * The Mpeg Video Group of Picture structure.
317  */
318 struct _GstMpegVideoGop
319 {
320   guint8 drop_frame_flag;
321
322   guint8 hour, minute, second, frame;
323
324   guint8 closed_gop;
325   guint8 broken_gop;
326 };
327
328 /**
329  * GstMpegVideoTypeOffsetSize:
330  * @type: the type of the packet that start at @offset
331  * @offset: the offset of the packet start in bytes, it is the exact, start of the packet, no sync code included
332  * @size: The size in bytes of the packet or -1 if the end wasn't found. It is the exact size of the packet, no sync code included
333  *
334  * A structure that contains the type of a packet, its offset and its size
335  */
336 struct _GstMpegVideoTypeOffsetSize
337 {
338   guint8 type;
339   guint offset;
340   gint size;
341 };
342
343 GList * gst_mpeg_video_parse                          (guint8 * data, gsize size, guint offset);
344
345 gboolean gst_mpeg_video_parse_sequence_header         (GstMpegVideoSequenceHdr * params,
346                                                        guint8 * data, gsize size, guint offset);
347
348 gboolean gst_mpeg_video_parse_picture_header          (GstMpegVideoPictureHdr* hdr,
349                                                        guint8 * data, gsize size, guint offset);
350
351 gboolean gst_mpeg_video_parse_picture_extension       (GstMpegVideoPictureExt *ext,
352                                                        guint8 * data, gsize size, guint offset);
353
354 gboolean gst_mpeg_video_parse_gop                     (GstMpegVideoGop * gop,
355                                                        guint8 * data, gsize size, guint offset);
356
357 gboolean gst_mpeg_video_parse_sequence_extension      (GstMpegVideoSequenceExt * seqext,
358                                                        guint8 * data, gsize size, guint offset);
359
360 gboolean gst_mpeg_video_parse_quant_matrix_extension  (GstMpegVideoQuantMatrixExt * quant,
361                                                        guint8 * data, gsize size, guint offset);
362
363 G_END_DECLS
364
365 #endif