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