1 
2 /*!
3  ************************************************************************
4  *  \file
5  *     global.h
6  *  \brief
7  *     global definitions for H.264 decoder.
8  *  \author
9  *     Copyright (C) 1999  Telenor Satellite Services,Norway
10  *                         Ericsson Radio Systems, Sweden
11  *
12  *     Inge Lille-Langoy               <inge.lille-langoy@telenor.com>
13  *
14  *     Telenor Satellite Services
15  *     Keysers gt.13                       tel.:   +47 23 13 86 98
16  *     N-0130 Oslo,Norway                  fax.:   +47 22 77 79 80
17  *
18  *     Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
19  *
20  *     Ericsson Radio Systems
21  *     KI/ERA/T/VV
22  *     164 80 Stockholm, Sweden
23  *
24  ************************************************************************
25  */
26 #ifndef _GLOBAL_H_
27 #define _GLOBAL_H_
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <time.h>
33 #include <sys/timeb.h>
34 
35 #include "win32.h"
36 #include "defines.h"
37 #include "ifunctions.h"
38 #include "parsetcommon.h"
39 #include "types.h"
40 #include "io_image.h"
41 #include "frame.h"
42 #include "distortion.h"
43 #include "io_video.h"
44 
45 typedef struct bit_stream_dec Bitstream;
46 
47 #define ET_SIZE 300      //!< size of error text buffer
48 extern char errortext[ET_SIZE]; //!< buffer for error message for exit with error()
49 
50 struct pic_motion_params_old;
51 struct pic_motion_params;
52 
53 /***********************************************************************
54  * T y p e    d e f i n i t i o n s    f o r    J M
55  ***********************************************************************
56  */
57 typedef enum
58 {
59    DEC_OPENED = 0,
60    DEC_STOPPED,
61 }DecoderStatus_e;
62 
63 typedef enum
64 {
65   LumaComp = 0,
66   CrComp = 1,
67   CbComp = 2
68 } Color_Component;
69 
70 /***********************************************************************
71  * D a t a    t y p e s   f o r  C A B A C
72  ***********************************************************************
73  */
74 
75 typedef struct pix_pos
76 {
77   int   available;
78   int   mb_addr;
79   short x;
80   short y;
81   short pos_x;
82   short pos_y;
83 } PixelPos;
84 
85 //! struct to characterize the state of the arithmetic coding engine
86 typedef struct
87 {
88   unsigned int    Drange;
89   unsigned int    Dvalue;
90   int             DbitsLeft;
91   byte            *Dcodestrm;
92   int             *Dcodestrm_len;
93 } DecodingEnvironment;
94 
95 typedef DecodingEnvironment *DecodingEnvironmentPtr;
96 
97 // Motion Vector structure
98 typedef struct
99 {
100   short mv_x;
101   short mv_y;
102 } MotionVector;
103 
104 static const MotionVector zero_mv = {0, 0};
105 
106 typedef struct
107 {
108   short x;
109   short y;
110 } BlockPos;
111 
112 //! struct for context management
113 typedef struct
114 {
115   uint16 state;         // index into state-table CP
116   unsigned char  MPS;           // Least Probable Symbol 0/1 CP
117   unsigned char dummy;          // for alignment
118 } BiContextType;
119 
120 typedef BiContextType *BiContextTypePtr;
121 
122 
123 /**********************************************************************
124  * C O N T E X T S   F O R   T M L   S Y N T A X   E L E M E N T S
125  **********************************************************************
126  */
127 
128 #define NUM_MB_TYPE_CTX  11
129 #define NUM_B8_TYPE_CTX  9
130 #define NUM_MV_RES_CTX   10
131 #define NUM_REF_NO_CTX   6
132 #define NUM_DELTA_QP_CTX 4
133 #define NUM_MB_AFF_CTX 4
134 #define NUM_TRANSFORM_SIZE_CTX 3
135 
136 // structures that will be declared somewhere else
137 struct storable_picture;
138 struct datapartition_dec;
139 struct syntaxelement_dec;
140 
141 typedef struct
142 {
143   BiContextType mb_type_contexts [3][NUM_MB_TYPE_CTX];
144   BiContextType b8_type_contexts [2][NUM_B8_TYPE_CTX];
145   BiContextType mv_res_contexts  [2][NUM_MV_RES_CTX];
146   BiContextType ref_no_contexts  [2][NUM_REF_NO_CTX];
147   BiContextType delta_qp_contexts[NUM_DELTA_QP_CTX];
148   BiContextType mb_aff_contexts  [NUM_MB_AFF_CTX];
149 } MotionInfoContexts;
150 
151 #define NUM_IPR_CTX    2
152 #define NUM_CIPR_CTX   4
153 #define NUM_CBP_CTX    4
154 #define NUM_BCBP_CTX   4
155 #define NUM_MAP_CTX   15
156 #define NUM_LAST_CTX  15
157 #define NUM_ONE_CTX    5
158 #define NUM_ABS_CTX    5
159 
160 typedef struct
161 {
162   BiContextType  transform_size_contexts [NUM_TRANSFORM_SIZE_CTX];
163   BiContextType  ipr_contexts [NUM_IPR_CTX];
164   BiContextType  cipr_contexts[NUM_CIPR_CTX];
165   BiContextType  cbp_contexts [3][NUM_CBP_CTX];
166   BiContextType  bcbp_contexts[NUM_BLOCK_TYPES][NUM_BCBP_CTX];
167   BiContextType  map_contexts [2][NUM_BLOCK_TYPES][NUM_MAP_CTX];
168   BiContextType  last_contexts[2][NUM_BLOCK_TYPES][NUM_LAST_CTX];
169   BiContextType  one_contexts [NUM_BLOCK_TYPES][NUM_ONE_CTX];
170   BiContextType  abs_contexts [NUM_BLOCK_TYPES][NUM_ABS_CTX];
171 } TextureInfoContexts;
172 
173 
174 //*********************** end of data type definition for CABAC *******************
175 
176 /***********************************************************************
177  * N e w   D a t a    t y p e s   f o r    T M L
178  ***********************************************************************
179  */
180 
181 /*! Buffer structure for decoded referenc picture marking commands */
182 typedef struct DecRefPicMarking_s
183 {
184   int memory_management_control_operation;
185   int difference_of_pic_nums_minus1;
186   int long_term_pic_num;
187   int long_term_frame_idx;
188   int max_long_term_frame_idx_plus1;
189   struct DecRefPicMarking_s *Next;
190 } DecRefPicMarking_t;
191 
192 //! cbp structure
193 typedef struct cbp_s
194 {
195   int64         blk     ;
196   int64         bits    ;
197   int64         bits_8x8;
198 } CBPStructure;
199 
200 //! Macroblock
201 typedef struct macroblock_dec
202 {
203   struct slice       *p_Slice;                    //!< pointer to the current slice
204   struct video_par   *p_Vid;                      //!< pointer to VideoParameters
205   struct inp_par     *p_Inp;
206   int                 mbAddrX;                    //!< current MB address
207   int mbAddrA, mbAddrB, mbAddrC, mbAddrD;
208   Boolean mbAvailA, mbAvailB, mbAvailC, mbAvailD;
209   BlockPos mb;
210   int block_x;
211   int block_y;
212   int block_y_aff;
213   int pix_x;
214   int pix_y;
215   int pix_c_x;
216   int pix_c_y;
217 
218   int subblock_x;
219   int subblock_y;
220 
221   int           qp;                    //!< QP luma
222   int           qpc[2];                //!< QP chroma
223   int           qp_scaled[MAX_PLANE];  //!< QP scaled for all comps.
224   Boolean       is_lossless;
225   Boolean       is_intra_block;
226   Boolean       is_v_block;
227   int           DeblockCall;
228 
229   short         slice_nr;
230   char          ei_flag;             //!< error indicator flag that enables concealment
231   char          dpl_flag;            //!< error indicator flag that signals a missing data partition
232   short         delta_quant;          //!< for rate control
233   short         list_offset;
234 
235   struct macroblock_dec   *mb_up;   //!< pointer to neighboring MB (CABAC)
236   struct macroblock_dec   *mb_left; //!< pointer to neighboring MB (CABAC)
237 
238   struct macroblock_dec   *mbup;   // neighbors for loopfilter
239   struct macroblock_dec   *mbleft; // neighbors for loopfilter
240 
241   // some storage of macroblock syntax elements for global access
242   short         mb_type;
243   short         mvd[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE][2];      //!< indices correspond to [forw,backw][block_y][block_x][x,y]
244   //short         ****mvd;      //!< indices correspond to [forw,backw][block_y][block_x][x,y]
245   int           cbp;
246   CBPStructure  s_cbp[3];
247 
248   int           i16mode;
249   char          b8mode[4];
250   char          b8pdir[4];
251   char          ipmode_DPCM;
252   char          c_ipred_mode;       //!< chroma intra prediction mode
253   char          skip_flag;
254   short         DFDisableIdc;
255   short         DFAlphaC0Offset;
256   short         DFBetaOffset;
257 
258   Boolean       mb_field;
259   //Flag for MBAFF deblocking;
260   byte          mixedModeEdgeFlag;
261 
262   // deblocking strength indices
263   byte strength_ver[4][4];
264   byte strength_hor[4][16];
265 
266 
267   Boolean       luma_transform_size_8x8_flag;
268   Boolean       NoMbPartLessThan8x8Flag;
269 
270   void (*itrans_4x4)(struct macroblock_dec *currMB, ColorPlane pl, int ioff, int joff);
271   void (*itrans_8x8)(struct macroblock_dec *currMB, ColorPlane pl, int ioff, int joff);
272 
273   void (*GetMVPredictor) (struct macroblock_dec *currMB, PixelPos *block,
274     MotionVector *pmv, short ref_frame, struct pic_motion_params **mv_info, int list, int mb_x, int mb_y, int blockshape_x, int blockshape_y);
275 
276   int  (*read_and_store_CBP_block_bit)  (struct macroblock_dec *currMB, DecodingEnvironmentPtr  dep_dp, int type);
277   char (*readRefPictureIdx)             (struct macroblock_dec *currMB, struct syntaxelement_dec *currSE, struct datapartition_dec *dP, char b8mode, int list);
278 
279   void (*read_comp_coeff_4x4_CABAC)     (struct macroblock_dec *currMB, struct syntaxelement_dec *currSE, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp);
280   void (*read_comp_coeff_8x8_CABAC)     (struct macroblock_dec *currMB, struct syntaxelement_dec *currSE, ColorPlane pl);
281 
282   void (*read_comp_coeff_4x4_CAVLC)     (struct macroblock_dec *currMB, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp, byte **nzcoeff);
283   void (*read_comp_coeff_8x8_CAVLC)     (struct macroblock_dec *currMB, ColorPlane pl, int (*InvLevelScale8x8)[8], int qp_per, int cbp, byte **nzcoeff);
284 } Macroblock;
285 
286 //! Syntaxelement
287 typedef struct syntaxelement_dec
288 {
289   int           type;                  //!< type of syntax element for data part.
290   int           value1;                //!< numerical value of syntax element
291   int           value2;                //!< for blocked symbols, e.g. run/level
292   int           len;                   //!< length of code
293   int           inf;                   //!< info part of CAVLC code
294   unsigned int  bitpattern;            //!< CAVLC bitpattern
295   int           context;               //!< CABAC context
296   int           k;                     //!< CABAC context for coeff_count,uv
297 
298 #if TRACE
299   #define       TRACESTRING_SIZE 100           //!< size of trace string
300   char          tracestring[TRACESTRING_SIZE]; //!< trace string
301 #endif
302 
303   //! for mapping of CAVLC to syntaxElement
304   void  (*mapping)(int len, int info, int *value1, int *value2);
305   //! used for CABAC: refers to actual coding method of each individual syntax element type
306   void  (*reading)(struct macroblock_dec *currMB, struct syntaxelement_dec *, DecodingEnvironmentPtr);
307 } SyntaxElement;
308 
309 
310 //! Bitstream
311 struct bit_stream_dec
312 {
313   // CABAC Decoding
314   int           read_len;           //!< actual position in the codebuffer, CABAC only
315   int           code_len;           //!< overall codebuffer length, CABAC only
316   // CAVLC Decoding
317   int           frame_bitoffset;    //!< actual position in the codebuffer, bit-oriented, CAVLC only
318   int           bitstream_length;   //!< over codebuffer lnegth, byte oriented, CAVLC only
319   // ErrorConcealment
320   byte          *streamBuffer;      //!< actual codebuffer for read bytes
321   int           ei_flag;            //!< error indication, 0: no error, else unspecified error
322 };
323 
324 //! DataPartition
325 typedef struct datapartition_dec
326 {
327 
328   Bitstream           *bitstream;
329   DecodingEnvironment de_cabac;
330 
331   int     (*readSyntaxElement)(struct macroblock_dec *currMB, struct syntaxelement_dec *, struct datapartition_dec *);
332           /*!< virtual function;
333                actual method depends on chosen data partition and
334                entropy coding method  */
335 } DataPartition;
336 
337 typedef struct wp_params
338 {
339   short weight[3];
340   short offset[3];
341 } WPParams;
342 
343 #if (MVC_EXTENSION_ENABLE)
344 typedef struct nalunitheadermvcext_tag
345 {
346    unsigned int non_idr_flag;
347    unsigned int priority_id;
348    unsigned int view_id;
349    unsigned int temporal_id;
350    unsigned int anchor_pic_flag;
351    unsigned int inter_view_flag;
352    unsigned int reserved_one_bit;
353    unsigned int iPrefixNALU;
354 } NALUnitHeaderMVCExt_t;
355 #endif
356 
357 //! Slice
358 typedef struct slice
359 {
360   struct video_par    *p_Vid;
361   struct inp_par      *p_Inp;
362   pic_parameter_set_rbsp_t *active_pps;
363   seq_parameter_set_rbsp_t *active_sps;
364   int svc_extension_flag;
365 
366   // dpb pointer
367   struct decoded_picture_buffer *p_Dpb;
368 
369   //slice property;
370   int idr_flag;
371   int idr_pic_id;
372   int nal_reference_idc;                       //!< nal_reference_idc from NAL unit
373   int Transform8x8Mode;
374   Boolean chroma444_not_separate;              //!< indicates chroma 4:4:4 coding with separate_colour_plane_flag equal to zero
375 
376   int toppoc;      //poc for this top field
377   int bottompoc;   //poc of bottom field of frame
378   int framepoc;    //poc of this frame
379 
380   //the following is for slice header syntax elements of poc
381   // for poc mode 0.
382   unsigned int pic_order_cnt_lsb;
383   int delta_pic_order_cnt_bottom;
384   // for poc mode 1.
385   int delta_pic_order_cnt[2];
386 
387   // ////////////////////////
388   // for POC mode 0:
389   signed   int PicOrderCntMsb;
390 
391   //signed   int PrevPicOrderCntMsb;
392   //unsigned int PrevPicOrderCntLsb;
393 
394   // for POC mode 1:
395   unsigned int AbsFrameNum;
396   int ThisPOC;
397   //signed int ExpectedPicOrderCnt, PicOrderCntCycleCnt, FrameNumInPicOrderCntCycle;
398   //unsigned int PreviousFrameNum, FrameNumOffset;
399   //int ExpectedDeltaPerPicOrderCntCycle;
400   //int PreviousFrameNumOffset;
401   // /////////////////////////
402 
403   //information need to move to slice;
404   unsigned int current_mb_nr; // bitstream order
405   unsigned int num_dec_mb;
406   short        current_slice_nr;
407   //int mb_x;
408   //int mb_y;
409   //int block_x;
410   //int block_y;
411   //int pix_c_x;
412   //int pix_c_y;
413   int cod_counter;                   //!< Current count of number of skipped macroblocks in a row
414   int allrefzero;
415   //end;
416 
417   int                 mb_aff_frame_flag;
418   int                 direct_spatial_mv_pred_flag;       //!< Indicator for direct mode type (1 for Spatial, 0 for Temporal)
419   int                 num_ref_idx_active[2];             //!< number of available list references
420   //int                 num_ref_idx_l0_active;             //!< number of available list 0 references
421   //int                 num_ref_idx_l1_active;             //!< number of available list 1 references
422 
423   int                 ei_flag;       //!< 0 if the partArr[0] contains valid information
424   int                 qp;
425   int                 slice_qp_delta;
426   int                 qs;
427   int                 slice_qs_delta;
428   int                 slice_type;    //!< slice type
429   int                 model_number;  //!< cabac model number
430   unsigned int        frame_num;   //frame_num for this frame
431   unsigned int        field_pic_flag;
432   byte                bottom_field_flag;
433   PictureStructure    structure;     //!< Identify picture structure type
434   int                 start_mb_nr;   //!< MUST be set by NAL even in case of ei_flag == 1
435   int                 end_mb_nr_plus1;
436   int                 max_part_nr;
437   int                 dp_mode;       //!< data partitioning mode
438   int                 current_header;
439   int                 next_header;
440   int                 last_dquant;
441 
442   //slice header information;
443   int colour_plane_id;               //!< colour_plane_id of the current coded slice
444   int redundant_pic_cnt;
445   int sp_switch;                              //!< 1 for switching sp, 0 for normal sp
446   int slice_group_change_cycle;
447   int redundant_slice_ref_idx;     //!< reference index of redundant slice
448   int no_output_of_prior_pics_flag;
449   int long_term_reference_flag;
450   int adaptive_ref_pic_buffering_flag;
451   DecRefPicMarking_t *dec_ref_pic_marking_buffer;                    //!< stores the memory management control operations
452 
453   char listXsize[6];
454   struct storable_picture **listX[6];
455 
456   //  int                 last_mb_nr;    //!< only valid when entropy coding == CABAC
457   DataPartition       *partArr;      //!< array of partitions
458   MotionInfoContexts  *mot_ctx;      //!< pointer to struct of context models for use in CABAC
459   TextureInfoContexts *tex_ctx;      //!< pointer to struct of context models for use in CABAC
460 
461   int mvscale[6][MAX_REFERENCE_PICTURES];
462 
463   int                 ref_pic_list_reordering_flag[2];
464   int                 *modification_of_pic_nums_idc[2];
465   int                 *abs_diff_pic_num_minus1[2];
466   int                 *long_term_pic_idx[2];
467 
468 #if (MVC_EXTENSION_ENABLE)
469   int                 *abs_diff_view_idx_minus1[2];
470 
471   int                 view_id;
472   int                 inter_view_flag;
473   int                 anchor_pic_flag;
474 
475   NALUnitHeaderMVCExt_t NaluHeaderMVCExt;
476 #endif
477   int                 layer_id;
478   short               DFDisableIdc;     //!< Disable deblocking filter on slice
479   short               DFAlphaC0Offset;  //!< Alpha and C0 offset for filtering slice
480   short               DFBetaOffset;     //!< Beta offset for filtering slice
481 
482   int                 pic_parameter_set_id;   //!<the ID of the picture parameter set the slice is reffering to
483 
484   int                 dpB_NotPresent;    //!< non-zero, if data partition B is lost
485   int                 dpC_NotPresent;    //!< non-zero, if data partition C is lost
486 
487   Boolean is_reset_coeff;
488   Boolean is_reset_coeff_cr;
489   imgpel  ***mb_pred;
490   imgpel  ***mb_rec;
491   int     ***mb_rres;
492   int     ***cof;
493   int     ***fcf;
494 
495   int cofu[16];
496 
497   imgpel **tmp_block_l0;
498   imgpel **tmp_block_l1;
499   int    **tmp_res;
500   imgpel **tmp_block_l2;
501   imgpel **tmp_block_l3;
502 
503   // Scaling matrix info
504   int  InvLevelScale4x4_Intra[3][6][4][4];
505   int  InvLevelScale4x4_Inter[3][6][4][4];
506   int  InvLevelScale8x8_Intra[3][6][8][8];
507   int  InvLevelScale8x8_Inter[3][6][8][8];
508 
509   int  *qmatrix[12];
510 
511   // Cabac
512   int  coeff[64]; // one more for EOB
513   int  coeff_ctr;
514   int  pos;
515 
516 
517   //weighted prediction
518   unsigned short weighted_pred_flag;
519   unsigned short weighted_bipred_idc;
520 
521   unsigned short luma_log2_weight_denom;
522   unsigned short chroma_log2_weight_denom;
523 
524   WPParams **wp_params; // wp parameters in [list][index]
525 
526   int ***wp_weight;  // weight in [list][index][component] order
527   int ***wp_offset;  // offset in [list][index][component] order
528   int ****wbp_weight; //weight in [list][fw_index][bw_index][component] order
529   short wp_round_luma;
530   short wp_round_chroma;
531 
532 #if (MVC_EXTENSION_ENABLE)
533   int listinterviewidx0;
534   int listinterviewidx1;
535   struct frame_store **fs_listinterview0;
536   struct frame_store **fs_listinterview1;
537 #endif
538 
539   // for signalling to the neighbour logic that this is a deblocker call
540   //byte mixedModeEdgeFlag;
541   int max_mb_vmv_r;                          //!< maximum vertical motion vector range in luma quarter pixel units for the current level_idc
542   int ref_flag[17];                //!< 0: i-th previous frame is incorrect
543 
544   int erc_mvperMB;
545   Macroblock *mb_data;
546   struct storable_picture *dec_picture;
547   int **siblock;
548   byte **ipredmode;
549   char  *intra_block;
550   char  chroma_vector_adjustment[6][32];
551   void (*read_CBP_and_coeffs_from_NAL) (Macroblock *currMB);
552   int  (*decode_one_component     )    (Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, struct storable_picture *dec_picture);
553   int  (*readSlice                )    (struct video_par *, struct inp_par *);
554   int  (*nal_startcode_follows    )    (struct slice*, int );
555   void (*read_motion_info_from_NAL)    (Macroblock *currMB);
556   void (*read_one_macroblock      )    (Macroblock *currMB);
557   void (*interpret_mb_mode        )    (Macroblock *currMB);
558   void (*init_lists               )    (struct slice *currSlice);
559 
560   void (*intra_pred_chroma        )    (Macroblock *currMB);
561   int  (*intra_pred_4x4)               (Macroblock *currMB, ColorPlane pl, int ioff, int joff,int i4,int j4);
562   int  (*intra_pred_8x8)               (Macroblock *currMB, ColorPlane pl, int ioff, int joff);
563   int  (*intra_pred_16x16)             (Macroblock *currMB, ColorPlane pl, int predmode);
564 
565   void (*linfo_cbp_intra          )    (int len, int info, int *cbp, int *dummy);
566   void (*linfo_cbp_inter          )    (int len, int info, int *cbp, int *dummy);
567   void (*update_direct_mv_info    )    (Macroblock *currMB);
568   void (*read_coeff_4x4_CAVLC     )    (Macroblock *currMB, int block_type, int i, int j, int levarr[16], int runarr[16], int *number_coefficients);
569 
570 } Slice;
571 
572 typedef struct decodedpic_t
573 {
574   int bValid;                 //0: invalid, 1: valid, 3: valid for 3D output;
575   int iViewId;                //-1: single view, >=0 multiview[VIEW1|VIEW0];
576   int iPOC;
577   int iYUVFormat;             //0: 4:0:0, 1: 4:2:0, 2: 4:2:2, 3: 4:4:4
578   int iYUVStorageFormat;      //0: YUV seperate; 1: YUV interleaved; 2: 3D output;
579   int iBitDepth;
580   byte *pY;                   //if iPictureFormat is 1, [0]: top; [1] bottom;
581   byte *pU;
582   byte *pV;
583   int iWidth;                 //frame width;
584   int iHeight;                //frame height;
585   int iYBufStride;            //stride of pY[0/1] buffer in bytes;
586   int iUVBufStride;           //stride of pU[0/1] and pV[0/1] buffer in bytes;
587   int iSkipPicNum;
588   int iBufSize;
589   struct decodedpic_t *pNext;
590 } DecodedPicList;
591 
592 //****************************** ~DM ***********************************
593 typedef struct coding_par
594 {
595   int layer_id;
596   int profile_idc;
597   int width;
598   int height;
599   int width_cr;                               //!< width chroma
600   int height_cr;                              //!< height chroma
601 
602   int pic_unit_bitsize_on_disk;
603   short bitdepth_luma;
604   short bitdepth_chroma;
605   int bitdepth_scale[2];
606   int bitdepth_luma_qp_scale;
607   int bitdepth_chroma_qp_scale;
608   unsigned int dc_pred_value_comp[MAX_PLANE]; //!< component value for DC prediction (depends on component pel bit depth)
609   int max_pel_value_comp[MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
610 
611   int yuv_format;
612   int lossless_qpprime_flag;
613   int num_blk8x8_uv;
614   int num_uv_blocks;
615   int num_cdc_coeff;
616   int mb_cr_size_x;
617   int mb_cr_size_y;
618   int mb_cr_size_x_blk;
619   int mb_cr_size_y_blk;
620   int mb_cr_size;
621   int mb_size[3][2];                         //!< component macroblock dimensions
622   int mb_size_blk[3][2];                     //!< component macroblock dimensions
623   int mb_size_shift[3][2];
624 
625   int max_vmv_r;                             //!< maximum vertical motion vector range in luma quarter frame pixel units for the current level_idc
626   int separate_colour_plane_flag;
627   int ChromaArrayType;
628   int max_frame_num;
629   unsigned int PicWidthInMbs;
630   unsigned int PicHeightInMapUnits;
631   unsigned int FrameHeightInMbs;
632   unsigned int FrameSizeInMbs;
633   int iLumaPadX;
634   int iLumaPadY;
635   int iChromaPadX;
636   int iChromaPadY;
637 
638   int subpel_x;
639   int subpel_y;
640   int shiftpel_x;
641   int shiftpel_y;
642   int total_scale;
643   unsigned int oldFrameSizeInMbs;
644 
645   //padding info;
646   void (*img2buf)          (imgpel** imgX, unsigned char* buf, int size_x, int size_y, int symbol_size_in_bytes, int crop_left, int crop_right, int crop_top, int crop_bottom, int iOutStride);
647   int rgb_output;
648 
649   imgpel **imgY_ref;                              //!< reference frame find snr
650   imgpel ***imgUV_ref;
651   Macroblock *mb_data;               //!< array containing all MBs of a whole frame
652   Macroblock *mb_data_JV[MAX_PLANE]; //!< mb_data to be used for 4:4:4 independent mode
653   char  *intra_block;
654   char  *intra_block_JV[MAX_PLANE];
655   BlockPos *PicPos;
656   byte **ipredmode;                  //!< prediction type [90][74]
657   byte **ipredmode_JV[MAX_PLANE];
658   byte ****nz_coeff;
659   int **siblock;
660   int **siblock_JV[MAX_PLANE];
661   int *qp_per_matrix;
662   int *qp_rem_matrix;
663 }CodingParameters;
664 
665 typedef struct layer_par
666 {
667   int layer_id;
668   struct video_par *p_Vid;
669   CodingParameters *p_Cps;
670   seq_parameter_set_rbsp_t *p_SPS;
671   struct decoded_picture_buffer *p_Dpb;
672 }LayerParameters;
673 
674 // video parameters
675 typedef struct video_par
676 {
677   struct inp_par      *p_Inp;
678   pic_parameter_set_rbsp_t *active_pps;
679   seq_parameter_set_rbsp_t *active_sps;
680   seq_parameter_set_rbsp_t SeqParSet[MAXSPS];
681   pic_parameter_set_rbsp_t PicParSet[MAXPPS];
682   struct decoded_picture_buffer *p_Dpb_layer[MAX_NUM_DPB_LAYERS];
683   CodingParameters *p_EncodePar[MAX_NUM_DPB_LAYERS];
684   LayerParameters *p_LayerPar[MAX_NUM_DPB_LAYERS];
685 
686 #if (MVC_EXTENSION_ENABLE)
687   subset_seq_parameter_set_rbsp_t *active_subset_sps;
688   //int svc_extension_flag;
689   subset_seq_parameter_set_rbsp_t SubsetSeqParSet[MAXSPS];
690   int last_pic_width_in_mbs_minus1;
691   int last_pic_height_in_map_units_minus1;
692   int last_max_dec_frame_buffering;
693   int last_profile_idc;
694 #endif
695 
696   struct sei_params        *p_SEI;
697 
698   struct old_slice_par *old_slice;
699   struct snr_par       *snr;
700   int number;                                 //!< frame number
701 
702   //current picture property;
703   unsigned int num_dec_mb;
704   int iSliceNumOfCurrPic;
705   int iNumOfSlicesAllocated;
706   int iNumOfSlicesDecoded;
707   Slice **ppSliceList;
708   char  *intra_block;
709   char  *intra_block_JV[MAX_PLANE];
710   //int qp;                                     //!< quant for the current frame
711 
712   //int sp_switch;                              //!< 1 for switching sp, 0 for normal sp
713   int type;                                   //!< image type INTER/INTRA
714 
715   byte **ipredmode;                  //!< prediction type [90][74]
716   byte **ipredmode_JV[MAX_PLANE];
717   byte ****nz_coeff;
718   int **siblock;
719   int **siblock_JV[MAX_PLANE];
720   BlockPos *PicPos;
721 
722   int newframe;
723   int structure;                     //!< Identify picture structure type
724 
725   //Slice      *currentSlice;          //!< pointer to current Slice data struct
726   Slice      *pNextSlice;             //!< pointer to first Slice of next picture;
727   Macroblock *mb_data;               //!< array containing all MBs of a whole frame
728   Macroblock *mb_data_JV[MAX_PLANE]; //!< mb_data to be used for 4:4:4 independent mode
729   //int colour_plane_id;               //!< colour_plane_id of the current coded slice
730   int ChromaArrayType;
731 
732   // picture error concealment
733   // concealment_head points to first node in list, concealment_end points to
734   // last node in list. Initialize both to NULL, meaning no nodes in list yet
735   struct concealment_node *concealment_head;
736   struct concealment_node *concealment_end;
737 
738   unsigned int pre_frame_num;           //!< store the frame_num in the last decoded slice. For detecting gap in frame_num.
739   int non_conforming_stream;
740 
741   // ////////////////////////
742   // for POC mode 0:
743   signed   int PrevPicOrderCntMsb;
744   unsigned int PrevPicOrderCntLsb;
745 
746   // for POC mode 1:
747   signed int ExpectedPicOrderCnt, PicOrderCntCycleCnt, FrameNumInPicOrderCntCycle;
748   unsigned int PreviousFrameNum, FrameNumOffset;
749   int ExpectedDeltaPerPicOrderCntCycle;
750   int ThisPOC;
751   int PreviousFrameNumOffset;
752   // /////////////////////////
753 
754   unsigned int PicHeightInMbs;
755   unsigned int PicSizeInMbs;
756 
757   int no_output_of_prior_pics_flag;
758 
759   int last_has_mmco_5;
760   int last_pic_bottom_field;
761 
762   int idr_psnr_number;
763   int psnr_number;
764 
765   // Timing related variables
766   TIME_T start_time;
767   TIME_T end_time;
768 
769   // picture error concealment
770   int last_ref_pic_poc;
771   int ref_poc_gap;
772   int poc_gap;
773   int conceal_mode;
774   int earlier_missing_poc;
775   unsigned int frame_to_conceal;
776   int IDR_concealment_flag;
777   int conceal_slice_type;
778 
779   Boolean first_sps;
780   // random access point decoding
781   int recovery_point;
782   int recovery_point_found;
783   int recovery_frame_cnt;
784   int recovery_frame_num;
785   int recovery_poc;
786 
787   byte *buf;
788   byte *ibuf;
789 
790   ImageData imgData;           //!< Image data to be encoded (dummy variable for now)
791   ImageData imgData0;          //!< base layer input
792   ImageData imgData1;          //!< temp buffer for left de-muxed view
793   ImageData imgData2;          //!< temp buffer for right de-muxed view
794 
795   // Data needed for 3:2 pulldown or temporal interleaving
796   ImageData imgData32;           //!< Image data to be encoded
797   ImageData imgData4;
798   ImageData imgData5;
799   ImageData imgData6;
800 
801 
802   // Redundant slices. Should be moved to another structure and allocated only if extended profile
803   unsigned int previous_frame_num; //!< frame number of previous slice
804   //!< non-zero: i-th previous frame is correct
805   int Is_primary_correct;          //!< if primary frame is correct, 0: incorrect
806   int Is_redundant_correct;        //!< if redundant frame is correct, 0:incorrect
807 
808   // Time
809   int64 tot_time;
810 
811   // files
812   int p_out;                       //!< file descriptor to output YUV file
813 #if (MVC_EXTENSION_ENABLE)
814   int p_out_mvc[MAX_VIEW_NUM];     //!< file descriptor to output YUV file for MVC
815 #endif
816   int p_ref;                       //!< pointer to input original reference YUV file file
817 
818   //FILE *p_log;                     //!< SNR file
819   int LastAccessUnitExists;
820   int NALUCount;
821 
822   // B pictures
823   int  Bframe_ctr;
824   int  frame_no;
825 
826   int  g_nFrame;
827   Boolean global_init_done[2];
828 
829   // global picture format dependent buffers, memory allocation in decod.c
830   imgpel **imgY_ref;                              //!< reference frame find snr
831   imgpel ***imgUV_ref;
832 
833   int *qp_per_matrix;
834   int *qp_rem_matrix;
835 
836   struct frame_store *last_out_fs;
837   int pocs_in_dpb[100];
838 
839   struct storable_picture *dec_picture;
840   struct storable_picture *dec_picture_JV[MAX_PLANE];  //!< dec_picture to be used during 4:4:4 independent mode decoding
841   struct storable_picture *no_reference_picture; //!< dummy storable picture for recovery point
842 
843   // Error parameters
844   struct object_buffer  *erc_object_list;
845   struct ercVariables_s *erc_errorVar;
846 
847   int erc_mvperMB;
848   struct video_par *erc_img;
849   int ec_flag[SE_MAX_ELEMENTS];        //!< array to set errorconcealment
850 
851   struct annex_b_struct *annex_b;
852 
853   struct frame_store *out_buffer;
854 
855   struct storable_picture *pending_output;
856   int    pending_output_state;
857   int    recovery_flag;
858 
859   int BitStreamFile;
860 
861   // report
862   char cslice_type[9];
863   // FMO
864   int *MbToSliceGroupMap;
865   int *MapUnitToSliceGroupMap;
866   int  NumberOfSliceGroups;    // the number of slice groups -1 (0 == scan order, 7 == maximum)
867 
868 #if (ENABLE_OUTPUT_TONEMAPPING)
869   struct tone_mapping_struct_s *seiToneMapping;
870 #endif
871 
872   void (*buf2img)          (imgpel** imgX, unsigned char* buf, int size_x, int size_y, int o_size_x, int o_size_y, int symbol_size_in_bytes, int bitshift);
873   void (*getNeighbour)     (Macroblock *currMB, int xN, int yN, int mb_size[2], PixelPos *pix);
874   void (*get_mb_block_pos) (BlockPos *PicPos, int mb_addr, short *x, short *y);
875   void (*GetStrengthVer)   (Macroblock *MbQ, int edge, int mvlimit, struct storable_picture *p);
876   void (*GetStrengthHor)   (Macroblock *MbQ, int edge, int mvlimit, struct storable_picture *p);
877   void (*EdgeLoopLumaVer)  (ColorPlane pl, imgpel** Img, byte *Strength, Macroblock *MbQ, int edge);
878   void (*EdgeLoopLumaHor)  (ColorPlane pl, imgpel** Img, byte *Strength, Macroblock *MbQ, int edge, struct storable_picture *p);
879   void (*EdgeLoopChromaVer)(imgpel** Img, byte *Strength, Macroblock *MbQ, int edge, int uv, struct storable_picture *p);
880   void (*EdgeLoopChromaHor)(imgpel** Img, byte *Strength, Macroblock *MbQ, int edge, int uv, struct storable_picture *p);
881   void (*img2buf)          (imgpel** imgX, unsigned char* buf, int size_x, int size_y, int symbol_size_in_bytes, int crop_left, int crop_right, int crop_top, int crop_bottom, int iOutStride);
882 
883   ImageData tempData3;
884   DecodedPicList *pDecOuputPic;
885   int iDeblockMode;  //0: deblock in picture, 1: deblock in slice;
886   struct nalu_t *nalu;
887   int iLumaPadX;
888   int iLumaPadY;
889   int iChromaPadX;
890   int iChromaPadY;
891   //control;
892   int bDeblockEnable;
893   int iPostProcess;
894   int bFrameInit;
895 #if _FLTDBG_
896   FILE *fpDbg;
897 #endif
898   pic_parameter_set_rbsp_t *pNextPPS;
899   int last_dec_poc;
900   int last_dec_view_id;
901   int last_dec_layer_id;
902   int dpb_layer_id;
903 
904 /******************* deprecative variables; ***************************************/
905   int width;
906   int height;
907   int width_cr;                               //!< width chroma
908   int height_cr;                              //!< height chroma
909   // Fidelity Range Extensions Stuff
910   int pic_unit_bitsize_on_disk;
911   short bitdepth_luma;
912   short bitdepth_chroma;
913   int bitdepth_scale[2];
914   int bitdepth_luma_qp_scale;
915   int bitdepth_chroma_qp_scale;
916   unsigned int dc_pred_value_comp[MAX_PLANE]; //!< component value for DC prediction (depends on component pel bit depth)
917   int max_pel_value_comp[MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
918 
919   int separate_colour_plane_flag;
920   int pic_unit_size_on_disk;
921 
922   int profile_idc;
923   int yuv_format;
924   int lossless_qpprime_flag;
925   int num_blk8x8_uv;
926   int num_uv_blocks;
927   int num_cdc_coeff;
928   int mb_cr_size_x;
929   int mb_cr_size_y;
930   int mb_cr_size_x_blk;
931   int mb_cr_size_y_blk;
932   int mb_cr_size;
933   int mb_size[3][2];                         //!< component macroblock dimensions
934   int mb_size_blk[3][2];                     //!< component macroblock dimensions
935   int mb_size_shift[3][2];
936   int subpel_x;
937   int subpel_y;
938   int shiftpel_x;
939   int shiftpel_y;
940   int total_scale;
941   int max_frame_num;
942 
943   unsigned int PicWidthInMbs;
944   unsigned int PicHeightInMapUnits;
945   unsigned int FrameHeightInMbs;
946   unsigned int FrameSizeInMbs;
947   unsigned int oldFrameSizeInMbs;
948   int max_vmv_r;                             //!< maximum vertical motion vector range in luma quarter frame pixel units for the current level_idc
949   //int max_mb_vmv_r;                        //!< maximum vertical motion vector range in luma quarter pixel units for the current level_idc
950 /******************* end deprecative variables; ***************************************/
951 
952   struct dec_stat_parameters *dec_stats;
953 } VideoParameters;
954 
955 
956 // signal to noise ratio parameters
957 typedef struct snr_par
958 {
959   int   frame_ctr;
960   float snr[3];                                //!< current SNR (component)
961   float snr1[3];                               //!< SNR (dB) first frame (component)
962   float snra[3];                               //!< Average component SNR (dB) remaining frames
963   float sse[3];                                //!< component SSE
964   float msse[3];                                //!< Average component SSE
965 } SNRParameters;
966 
967 // input parameters from configuration file
968 typedef struct inp_par
969 {
970   char infile[FILE_NAME_SIZE];                       //!< H.264 inputfile
971   char outfile[FILE_NAME_SIZE];                      //!< Decoded YUV 4:2:0 output
972   char reffile[FILE_NAME_SIZE];                      //!< Optional YUV 4:2:0 reference file for SNR measurement
973 
974   int FileFormat;                         //!< File format of the Input file, PAR_OF_ANNEXB or PAR_OF_RTP
975   int ref_offset;
976   int poc_scale;
977   int write_uv;
978   int silent;
979   int intra_profile_deblocking;               //!< Loop filter usage determined by flags and parameters in bitstream
980 
981   // Input/output sequence format related variables
982   FrameFormat source;                   //!< source related information
983   FrameFormat output;                   //!< output related information
984 
985   int  ProcessInput;
986   int  enable_32_pulldown;
987   VideoDataFile input_file1;          //!< Input video file1
988   VideoDataFile input_file2;          //!< Input video file2
989   VideoDataFile input_file3;          //!< Input video file3
990 #if (MVC_EXTENSION_ENABLE)
991   int  DecodeAllLayers;
992 #endif
993 
994 #ifdef _LEAKYBUCKET_
995   unsigned long R_decoder;                //!< Decoder Rate in HRD Model
996   unsigned long B_decoder;                //!< Decoder Buffer size in HRD model
997   unsigned long F_decoder;                //!< Decoder Initial buffer fullness in HRD model
998   char LeakyBucketParamFile[FILE_NAME_SIZE];         //!< LeakyBucketParamFile
999 #endif
1000 
1001   // picture error concealment
1002   int conceal_mode;
1003   int ref_poc_gap;
1004   int poc_gap;
1005 
1006 
1007   // dummy for encoder
1008   int start_frame;
1009 
1010   // Needed to allow compilation for decoder. May be used later for distortion computation operations
1011   int stdRange;                         //!< 1 - standard range, 0 - full range
1012   int videoCode;                        //!< 1 - 709, 3 - 601:  See VideoCode in io_tiff.
1013   int export_views;
1014 
1015   int iDecFrmNum;
1016 
1017   int bDisplayDecParams;
1018   int dpb_plus[2];
1019 } InputParameters;
1020 
1021 typedef struct old_slice_par
1022 {
1023   unsigned field_pic_flag;
1024   unsigned frame_num;
1025   int      nal_ref_idc;
1026   unsigned pic_oder_cnt_lsb;
1027   int      delta_pic_oder_cnt_bottom;
1028   int      delta_pic_order_cnt[2];
1029   byte     bottom_field_flag;
1030   byte     idr_flag;
1031   int      idr_pic_id;
1032   int      pps_id;
1033 #if (MVC_EXTENSION_ENABLE)
1034   int      view_id;
1035   int      inter_view_flag;
1036   int      anchor_pic_flag;
1037 #endif
1038   int      layer_id;
1039 } OldSliceParams;
1040 
1041 typedef struct decoder_params
1042 {
1043   InputParameters   *p_Inp;          //!< Input Parameters
1044   VideoParameters   *p_Vid;          //!< Image Parameters
1045   int64              bufferSize;     //!< buffersize for tiff reads (not currently supported)
1046   int                UsedBits;      // for internal statistics, is adjusted by read_se_v, read_ue_v, read_u_1
1047   FILE              *p_trace;        //!< Trace file
1048   int                bitcounter;
1049 } DecoderParams;
1050 
1051 extern DecoderParams  *p_Dec;
1052 
1053 // prototypes
1054 extern void error(char *text, int code);
1055 
1056 // dynamic mem allocation
1057 extern int  init_global_buffers( VideoParameters *p_Vid, int layer_id );
1058 extern void free_global_buffers( VideoParameters *p_Vid);
1059 extern void free_layer_buffers( VideoParameters *p_Vid, int layer_id );
1060 
1061 extern int RBSPtoSODB(byte *streamBuffer, int last_byte_pos);
1062 extern int EBSPtoRBSP(byte *streamBuffer, int end_bytepos, int begin_bytepos);
1063 
1064 extern void FreePartition (DataPartition *dp, int n);
1065 extern DataPartition *AllocPartition(int n);
1066 
1067 extern void tracebits (const char *trace_str, int len, int info, int value1);
1068 extern void tracebits2(const char *trace_str, int len, int info);
1069 
1070 extern unsigned CeilLog2   ( unsigned uiVal);
1071 extern unsigned CeilLog2_sf( unsigned uiVal);
1072 
1073 // For 4:4:4 independent mode
1074 extern void change_plane_JV      ( VideoParameters *p_Vid, int nplane, Slice *pSlice);
1075 extern void make_frame_picture_JV( VideoParameters *p_Vid );
1076 
1077 #if (MVC_EXTENSION_ENABLE)
1078 extern void nal_unit_header_mvc_extension(NALUnitHeaderMVCExt_t *NaluHeaderMVCExt, struct bit_stream_dec *bitstream);
1079 #endif
1080 
1081 extern void FreeDecPicList ( DecodedPicList *pDecPicList );
1082 extern void ClearDecPicList( VideoParameters *p_Vid );
1083 extern DecodedPicList *get_one_avail_dec_pic_from_list(DecodedPicList *pDecPicList, int b3D, int view_id);
1084 extern Slice *malloc_slice( InputParameters *p_Inp, VideoParameters *p_Vid );
1085 extern void copy_slice_info ( Slice *currSlice, OldSliceParams *p_old_slice );
1086 extern void OpenOutputFiles(VideoParameters *p_Vid, int view0_id, int view1_id);
1087 extern void set_global_coding_par(VideoParameters *p_Vid, CodingParameters *cps);
1088 
is_FREXT_profile(unsigned int profile_idc)1089 static inline int is_FREXT_profile(unsigned int profile_idc)
1090 {
1091   // we allow all FRExt tools, when no profile is active
1092   return ( profile_idc==NO_PROFILE || profile_idc==FREXT_HP || profile_idc==FREXT_Hi10P || profile_idc==FREXT_Hi422 || profile_idc==FREXT_Hi444 || profile_idc == FREXT_CAVLC444 );
1093 }
1094 
is_HI_intra_only_profile(unsigned int profile_idc,Boolean constrained_set3_flag)1095 static inline int is_HI_intra_only_profile(unsigned int profile_idc, Boolean constrained_set3_flag)
1096 {
1097   return ( ( ( (profile_idc == FREXT_Hi10P)||(profile_idc == FREXT_Hi422)|| (profile_idc == FREXT_Hi444)) && constrained_set3_flag) || (profile_idc == FREXT_CAVLC444) );
1098 }
is_BL_profile(unsigned int profile_idc)1099 static inline int is_BL_profile(unsigned int profile_idc)
1100 {
1101   return ( profile_idc == FREXT_CAVLC444 || profile_idc == BASELINE || profile_idc == MAIN || profile_idc == EXTENDED ||
1102            profile_idc == FREXT_HP || profile_idc == FREXT_Hi10P || profile_idc == FREXT_Hi422 || profile_idc == FREXT_Hi444);
1103 }
is_EL_profile(unsigned int profile_idc)1104 static inline int is_EL_profile(unsigned int profile_idc)
1105 {
1106   return ( (profile_idc == MVC_HIGH) || (profile_idc == STEREO_HIGH) );
1107 }
1108 
is_MVC_profile(unsigned int profile_idc)1109 static inline int is_MVC_profile(unsigned int profile_idc)
1110 {
1111   return ( (0)
1112 #if (MVC_EXTENSION_ENABLE)
1113   || (profile_idc == MVC_HIGH) || (profile_idc == STEREO_HIGH)
1114 #endif
1115   );
1116 }
1117 
1118 #endif
1119 
1120