import ext/codecparsers instead of git submodule
[profile/ivi/gstreamer-vaapi.git] / ext / codecparsers / gst-libs / gst / codecparsers / gstvc1parser.h
1 /* Gstreamer
2  * Copyright (C) <2011> Intel
3  * Copyright (C) <2011> Collabora Ltd.
4  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifndef __GST_VC1_PARSER_H__
23 #define __GST_VC1_PARSER_H__
24
25 #ifndef GST_USE_UNSTABLE_API
26 #warning "The VC1 parsing library is unstable API and may change in future."
27 #warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
28 #endif
29
30 #include <gst/gst.h>
31
32 G_BEGIN_DECLS
33
34 #define MAX_HRD_NUM_LEAKY_BUCKETS 31
35
36 /**
37  * @GST_VC1_BFRACTION_BASIS: The @bfraction variable should be divided
38  * by this constant to have the actual value.
39  */
40 #define GST_VC1_BFRACTION_BASIS 840
41
42 #define GST_VC1_BFRACTION_RESERVED (GST_VC1_BFRACTION_BASIS + 1)
43 #define GST_VC1_BFRACTION_PTYPE_BI (GST_VC1_BFRACTION_BASIS + 2)
44
45 typedef enum {
46   GST_VC1_END_OF_SEQ       = 0x0A,
47   GST_VC1_SLICE            = 0x0B,
48   GST_VC1_FIELD            = 0x0C,
49   GST_VC1_FRAME            = 0x0D,
50   GST_VC1_ENTRYPOINT       = 0x0E,
51   GST_VC1_SEQUENCE         = 0x0F,
52   GST_VC1_SLICE_USER       = 0x1B,
53   GST_VC1_FIELD_USER       = 0x1C,
54   GST_VC1_FRAME_USER       = 0x1D,
55   GST_VC1_ENTRY_POINT_USER = 0x1E,
56   GST_VC1_SEQUENCE_USER    = 0x1F
57 } GstVC1StartCode;
58
59 typedef enum {
60   GST_VC1_PROFILE_SIMPLE,
61   GST_VC1_PROFILE_MAIN,
62   GST_VC1_PROFILE_RESERVED,
63   GST_VC1_PROFILE_ADVANCED
64 } GstVC1Profile;
65
66 typedef enum {
67   GST_VC1_PARSER_OK,
68   GST_VC1_PARSER_BROKEN_DATA,
69   GST_VC1_PARSER_NO_BDU,
70   GST_VC1_PARSER_NO_BDU_END,
71   GST_VC1_PARSER_ERROR,
72 } GstVC1ParserResult;
73
74 typedef enum
75 {
76   GST_VC1_PICTURE_TYPE_P,
77   GST_VC1_PICTURE_TYPE_B,
78   GST_VC1_PICTURE_TYPE_I,
79   GST_VC1_PICTURE_TYPE_BI,
80   GST_VC1_PICTURE_TYPE_SKIPPED
81 } GstVC1PictureType;
82
83 typedef enum
84 {
85     GST_VC1_LEVEL_LOW    = 0,    /* Simple/Main profile low level */
86     GST_VC1_LEVEL_MEDIUM = 1,    /* Simple/Main profile medium level */
87     GST_VC1_LEVEL_HIGH   = 2,   /* Main profile high level */
88
89     GST_VC1_LEVEL_L0    = 0,    /* Advanced profile level 0 */
90     GST_VC1_LEVEL_L1    = 1,    /* Advanced profile level 1 */
91     GST_VC1_LEVEL_L2    = 2,    /* Advanced profile level 2 */
92     GST_VC1_LEVEL_L3    = 3,    /* Advanced profile level 3 */
93     GST_VC1_LEVEL_L4    = 4,    /* Advanced profile level 4 */
94
95     /* 5 to 7 reserved */
96     GST_VC1_LEVEL_UNKNOWN = 255  /* Unknown profile */
97 } GstVC1Level;
98
99 typedef enum
100 {
101   GST_VC1_QUANTIZER_IMPLICITLY,
102   GST_VC1_QUANTIZER_EXPLICITLY,
103   GST_VC1_QUANTIZER_NON_UNIFORM,
104   GST_VC1_QUANTIZER_UNIFORM
105 } GstVC1QuantizerSpec;
106
107 typedef enum {
108   GST_VC1_DQPROFILE_FOUR_EDGES,
109   GST_VC1_DQPROFILE_DOUBLE_EDGES,
110   GST_VC1_DQPROFILE_SINGLE_EDGE,
111   GST_VC1_DQPROFILE_ALL_MBS
112 } GstVC1DQProfile;
113
114 typedef enum {
115   GST_VC1_CONDOVER_NONE,
116   GST_VC1_CONDOVER_ALL,
117   GST_VC1_CONDOVER_SELECT
118 } GstVC1Condover;
119
120 /**
121  * GstVC1MvMode:
122  *
123  */
124 typedef enum
125 {
126   GST_VC1_MVMODE_1MV_HPEL_BILINEAR,
127   GST_VC1_MVMODE_1MV,
128   GST_VC1_MVMODE_1MV_HPEL,
129   GST_VC1_MVMODE_MIXED_MV,
130   GST_VC1_MVMODE_INTENSITY_COMP
131 } GstVC1MvMode;
132
133 typedef enum
134 {
135   GST_VC1_FRAME_PROGRESSIVE = 0x0,
136   GST_VC1_FRAME_INTERLACE   = 0x10,
137   GST_VC1_FIELD_INTERLACE   = 0x11
138 } GstVC1FrameCodingMode;
139
140 typedef struct _GstVC1SeqHdr            GstVC1SeqHdr;
141 typedef struct _GstVC1AdvancedSeqHdr    GstVC1AdvancedSeqHdr;
142 typedef struct _GstVC1HrdParam          GstVC1HrdParam;
143 typedef struct _GstVC1EntryPointHdr     GstVC1EntryPointHdr;
144
145 typedef struct _GstVC1SeqLayer     GstVC1SeqLayer;
146
147 typedef struct _GstVC1SeqStructA   GstVC1SeqStructA;
148 typedef struct _GstVC1SeqStructB   GstVC1SeqStructB;
149 typedef struct _GstVC1SeqStructC   GstVC1SeqStructC;
150
151 /* Pictures Structures */
152 typedef struct _GstVC1FrameLayer        GstVC1FrameLayer;
153 typedef struct _GstVC1FrameHdr          GstVC1FrameHdr;
154 typedef struct _GstVC1PicAdvanced       GstVC1PicAdvanced;
155 typedef struct _GstVC1PicSimpleMain     GstVC1PicSimpleMain;
156 typedef struct _GstVC1Picture           GstVC1Picture;
157
158 typedef struct _GstVC1VopDquant         GstVC1VopDquant;
159
160 typedef struct _GstVC1BitPlanes         GstVC1BitPlanes;
161
162 typedef struct _GstVC1BDU               GstVC1BDU;
163
164 struct _GstVC1HrdParam
165 {
166   guint8 hrd_num_leaky_buckets;
167   guint8 bit_rate_exponent;
168   guint8 buffer_size_exponent;
169   guint16 hrd_rate[MAX_HRD_NUM_LEAKY_BUCKETS];
170   guint16 hrd_buffer[MAX_HRD_NUM_LEAKY_BUCKETS];
171 };
172
173 /**
174  * GstVC1EntryPointHdr:
175  *
176  * Structure for entrypoint header, this will be used only in advanced profiles
177  */
178 struct _GstVC1EntryPointHdr
179 {
180   guint8 broken_link;
181   guint8 closed_entry;
182   guint8 panscan_flag;
183   guint8 refdist_flag;
184   guint8 loopfilter;
185   guint8 fastuvmc;
186   guint8 extended_mv;
187   guint8 dquant;
188   guint8 vstransform;
189   guint8 overlap;
190   guint8 quantizer;
191   guint8 coded_size_flag;
192   guint16 coded_width;
193   guint16 coded_height;
194   guint8 extended_dmv;
195   guint8 range_mapy_flag;
196   guint8 range_mapy;
197   guint8 range_mapuv_flag;
198   guint8 range_mapuv;
199
200   guint8 hrd_full[MAX_HRD_NUM_LEAKY_BUCKETS];
201 };
202
203 /**
204  * GstVC1AdvancedSeqHdr:
205  *
206  * Structure for the advanced profile sequence headers specific parameters.
207  */
208 struct _GstVC1AdvancedSeqHdr
209 {
210   GstVC1Level  level;
211
212   guint8  frmrtq_postproc;
213   guint8  bitrtq_postproc;
214   guint8  postprocflag;
215   guint16 max_coded_width;
216   guint16 max_coded_height;
217   guint8  pulldown;
218   guint8  interlace;
219   guint8  tfcntrflag;
220   guint8  finterpflag;
221   guint8  psf;
222   guint8  display_ext;
223   guint16 disp_horiz_size;
224   guint16 disp_vert_size;
225   guint8  aspect_ratio_flag;
226   guint8  aspect_ratio;
227   guint8  aspect_horiz_size;
228   guint8  aspect_vert_size;
229   guint8  framerate_flag;
230   guint8  framerateind;
231   guint8  frameratenr;
232   guint8  frameratedr;
233   guint16 framerateexp;
234   guint8  color_format_flag;
235   guint8  color_prim;
236   guint8  transfer_char;
237   guint8  matrix_coef;
238   guint8  hrd_param_flag;
239   guint8  colordiff_format;
240
241   GstVC1HrdParam hrd_param;
242
243   /* computed */
244   guint framerate; /* Around in fps, 0 if unknown*/
245   guint bitrate;   /* Around in kpbs, 0 if unknown*/
246   guint par_n;
247   guint par_d;
248   guint fps_n;
249   guint fps_d;
250
251   /* The last parsed entry point */
252   GstVC1EntryPointHdr entrypoint;
253 };
254
255 struct _GstVC1SeqStructA
256 {
257   guint32 vert_size;
258   guint32 horiz_size;
259 };
260
261 struct _GstVC1SeqStructB
262 {
263   GstVC1Level  level;
264
265   guint8 cbr;
266   guint32 framerate;
267
268   /* In simple and main profiles only */
269   guint32 hrd_buffer;
270   guint32 hrd_rate;
271 };
272
273 struct _GstVC1SeqStructC
274 {
275   GstVC1Profile profile;
276
277   /* Only in simple and main profiles */
278   guint8 frmrtq_postproc;
279   guint8 bitrtq_postproc;
280   guint8 res_sprite;
281   guint8 loop_filter;
282   guint8 multires;
283   guint8 fastuvmc;
284   guint8 extended_mv;
285   guint8 dquant;
286   guint8 vstransform;
287   guint8 overlap;
288   guint8 syncmarker;
289   guint8 rangered;
290   guint8 maxbframes;
291   guint8 quantizer;
292   guint8 finterpflag;
293
294   /* Computed */
295   guint framerate; /* Around in fps, 0 if unknown*/
296   guint bitrate;   /* Around in kpbs, 0 if unknown*/
297
298   /* This should be filled by user if previously known */
299   guint16 coded_width;
300   /* This should be filled by user if previously known */
301   guint16 coded_height;
302
303   /* Wmvp specific */
304   guint8 wmvp;          /* Specify if the stream is wmp or not */
305   /* In the wmvp case, the framerate is not computed but in the bistream */
306   guint8 slice_code;
307 };
308
309 struct _GstVC1SeqLayer
310 {
311   guint32 numframes;
312
313   GstVC1SeqStructA struct_a;
314   GstVC1SeqStructB struct_b;
315   GstVC1SeqStructC struct_c;
316 };
317
318 /**
319  * GstVC1SeqHdr:
320  *
321  * Structure for sequence headers in any profile.
322  */
323 struct _GstVC1SeqHdr
324 {
325   GstVC1Profile profile;
326
327   GstVC1SeqStructC struct_c;
328
329   /*  calculated */
330   guint mb_height;
331   guint mb_width;
332   guint mb_stride;
333
334   GstVC1AdvancedSeqHdr   advanced;
335
336 };
337
338 /**
339  * GstVC1PicSimpleMain:
340  * @bfaction: Should be divided by #GST_VC1_BFRACTION_BASIS
341  * to get the real value.
342  */
343 struct _GstVC1PicSimpleMain
344 {
345   guint8 frmcnt;
346   guint8 mvrange;
347   guint8 rangeredfrm;
348
349   /* I and P pic simple and main profiles only */
350   guint8 respic;
351
352   /* I and BI pic simple and main profiles only */
353   guint8 transacfrm2;
354   guint8 bf;
355
356   /* B and P pic simple and main profiles only */
357   guint8 mvmode;
358   guint8 mvtab;
359   guint8 ttmbf;
360
361   /* P pic simple and main profiles only */
362   guint8 mvmode2;
363   guint8 lumscale;
364   guint8 lumshift;
365
366   guint8 cbptab;
367   guint8 ttfrm;
368
369   /* B and BI picture only
370    * Should be divided by #GST_VC1_BFRACTION_BASIS
371    * to get the real value. */
372   guint16 bfraction;
373
374   /* Biplane value, those fields only mention the fact
375    * that the bitplane is in raw mode or not */
376   guint8 mvtypemb;
377   guint8 skipmb;
378   guint8 directmb; /* B pic main profile only */
379 };
380
381 /**
382  * GstVC1PicAdvanced:
383  * @bfaction: Should be divided by #GST_VC1_BFRACTION_BASIS
384  * to get the real value.
385  */
386 struct _GstVC1PicAdvanced
387 {
388   GstVC1FrameCodingMode fcm;
389   guint8  tfcntr;
390
391   guint8  rptfrm;
392   guint8  tff;
393   guint8  rff;
394   guint8  ps_present;
395   guint32 ps_hoffset;
396   guint32 ps_voffset;
397   guint16 ps_width;
398   guint16 ps_height;
399   guint8  rndctrl;
400   guint8  uvsamp;
401   guint8  postproc;
402
403   /*  B and P picture specific */
404   guint8  mvrange;
405   guint8  mvmode;
406   guint8  mvtab;
407   guint8  cbptab;
408   guint8  ttmbf;
409   guint8  ttfrm;
410
411   /* B and BI picture only
412    * Should be divided by #GST_VC1_BFRACTION_BASIS
413    * to get the real value. */
414   guint16 bfraction;
415
416   /* ppic */
417   guint8  mvmode2;
418   guint8  lumscale;
419   guint8  lumshift;
420
421   /* bipic */
422   guint8  bf;
423   guint8  condover;
424   guint8  transacfrm2;
425
426   /* Biplane value, those fields only mention the fact
427    * that the bitplane is in raw mode or not */
428   guint8  acpred;
429   guint8  overflags;
430   guint8  mvtypemb;
431   guint8  skipmb;
432   guint8  directmb;
433   guint8  forwardmb; /* B pic interlace field only */
434
435   /* For interlaced pictures only */
436   guint8  fieldtx;
437
438   /* P and B pictures */
439   guint8  intcomp;
440   guint8  dmvrange;
441   guint8  mbmodetab;
442   guint8  imvtab;
443   guint8  icbptab;
444   guint8  mvbptab2;
445   guint8  mvbptab4; /* If 4mvswitch in ppic */
446
447   /*  P picture */
448   guint8  mvswitch4;
449
450   /* For interlaced fields only */
451   guint16 refdist;
452   guint8 fptype; /* Raw value */
453
454   /* P pic */
455   guint8  numref;
456   guint8  reffield;
457   guint8  lumscale2;
458   guint8  lumshift2;
459   guint8  intcompfield;
460
461 };
462
463 struct _GstVC1BitPlanes
464 {
465   guint8  *acpred;
466   guint8  *fieldtx;
467   guint8  *overflags;
468   guint8  *mvtypemb;
469   guint8  *skipmb;
470   guint8  *directmb;
471   guint8  *forwardmb;
472
473   guint size; /* Size of the arrays */
474 };
475
476 struct _GstVC1VopDquant
477 {
478   guint8 pqdiff;
479   guint8 abspq;
480
481   /* Computed */
482   guint8 altpquant;
483
484   /*  if dqant != 2*/
485   guint8 dquantfrm;
486   guint8 dqprofile;
487
488   /* if dqprofile == GST_VC1_DQPROFILE_SINGLE_EDGE
489    * or GST_VC1_DQPROFILE_DOUBLE_EDGE:*/
490   guint8 dqsbedge;
491
492   /* if dqprofile == GST_VC1_DQPROFILE_SINGLE_EDGE
493    * or GST_VC1_DQPROFILE_DOUBLE_EDGE:*/
494   guint8 dqbedge;
495
496   /* if dqprofile == GST_VC1_DQPROFILE_ALL_MBS */
497   guint8 dqbilevel;
498
499 };
500
501 struct _GstVC1FrameLayer
502 {
503   guint8 key;
504   guint32 framesize;
505
506   guint32 timestamp;
507
508   /* calculated */
509   guint32 next_framelayer_offset;
510   guint8 skiped_p_frame;
511 };
512
513 /**
514  * GstVC1FrameHdr:
515  *
516  * Structure that represent picture in any profile or mode.
517  * You should look at @ptype and @profile to know what is currently
518  * in use.
519  */
520 struct _GstVC1FrameHdr
521 {
522   /* common fields */
523   GstVC1PictureType ptype;
524   guint8 interpfrm;
525   guint8 halfqp;
526   guint8 transacfrm;
527   guint8 transdctab;
528   guint8 pqindex;
529   guint8 pquantizer;
530
531   /* Computed */
532   guint8 pquant;
533
534   /* Convenience fields */
535   guint8 profile;
536   guint8 dquant;
537
538   /*  If dquant */
539   GstVC1VopDquant vopdquant;
540
541   union {
542     GstVC1PicSimpleMain simple;
543     GstVC1PicAdvanced advanced;
544   } pic;
545
546   /* Size of the picture layer in bits */
547   guint header_size;
548 };
549
550 /**
551  * GstVC1BDU:
552  *
553  * Structure that represents a Bitstream Data Unit.
554  */
555 struct _GstVC1BDU
556 {
557   GstVC1StartCode type;
558   guint size;
559   guint sc_offset;
560   guint offset;
561   guint8 * data;
562 };
563
564 GstVC1ParserResult gst_vc1_identify_next_bdu           (const guint8 *data,
565                                                         gsize size,
566                                                         GstVC1BDU *bdu);
567
568
569 GstVC1ParserResult gst_vc1_parse_sequence_header       (const guint8 *data,
570                                                         gsize size,
571                                                         GstVC1SeqHdr * seqhdr);
572
573 GstVC1ParserResult gst_vc1_parse_entry_point_header    (const  guint8 *data,
574                                                         gsize size,
575                                                         GstVC1EntryPointHdr * entrypoint,
576                                                         GstVC1SeqHdr *seqhdr);
577
578 GstVC1ParserResult gst_vc1_parse_sequence_layer        (const guint8 *data,
579                                                         gsize size,
580                                                         GstVC1SeqLayer * seqlayer);
581
582 GstVC1ParserResult
583 gst_vc1_parse_sequence_header_struct_a                 (const guint8 *data,
584                                                         gsize size,
585                                                         GstVC1SeqStructA *structa);
586 GstVC1ParserResult
587 gst_vc1_parse_sequence_header_struct_b                 (const guint8 *data,
588                                                         gsize size,
589                                                         GstVC1SeqStructB *structb);
590
591 GstVC1ParserResult
592 gst_vc1_parse_sequence_header_struct_c                 (const guint8 *data,
593                                                         gsize size,
594                                                         GstVC1SeqStructC *structc);
595
596 GstVC1ParserResult gst_vc1_parse_frame_layer           (const guint8 *data,
597                                                         gsize size,
598                                                         GstVC1FrameLayer * framelayer);
599
600 GstVC1ParserResult gst_vc1_parse_frame_header          (const guint8 *data,
601                                                         gsize size,
602                                                         GstVC1FrameHdr * framehdr,
603                                                         GstVC1SeqHdr *seqhdr,
604                                                         GstVC1BitPlanes *bitplanes);
605
606 GstVC1ParserResult gst_vc1_parse_field_header          (const guint8 *data,
607                                                         gsize size,
608                                                         GstVC1FrameHdr * fieldhdr,
609                                                         GstVC1SeqHdr *seqhdr,
610                                                         GstVC1BitPlanes *bitplanes);
611
612 GstVC1BitPlanes *  gst_vc1_bitplanes_new               (void);
613
614 void               gst_vc1_bitplanes_free              (GstVC1BitPlanes *bitplanes);
615
616 void               gst_vc1_bitplanes_free_1            (GstVC1BitPlanes *bitplanes);
617
618 gboolean           gst_vc1_bitplanes_ensure_size       (GstVC1BitPlanes *bitplanes,
619                                                         GstVC1SeqHdr *seqhdr);
620
621 G_END_DECLS
622 #endif