1 /*!
2  ************************************************************************
3  *  \file
4  *     global.h
5  *
6  *  \brief
7  *     global definitions for H.264 encoder.
8  *
9  *  \author
10  *    Main contributors (see contributors.h for copyright, address and affiliation details)
11  *
12  ************************************************************************
13  */
14 #ifndef _GLOBAL_H_
15 #define _GLOBAL_H_
16 
17 #include "defines.h"
18 #include "win32.h"
19 #include "typedefs.h"
20 #include "parsetcommon.h"
21 #include "ifunctions.h"
22 #include "frame.h"
23 #include "io_video.h"
24 #include "io_image.h"
25 #include "nalucommon.h"
26 #include "params.h"
27 #include "distortion.h"
28 #include "lagrangian.h"
29 #include "quant_params.h"
30 #include "rc_types.h"
31 
32 typedef struct bit_stream_enc Bitstream;
33 
34 #include "pred_struct_types.h"
35 
36 
37 /***********************************************************************
38  * D a t a    t y p e s   f o r  C A B A C
39  ***********************************************************************
40  */
41 typedef struct inp_par_enc InputParameters;
42 
43 // structures that will be declared somewhere else
44 struct storable_picture;
45 struct coding_state;
46 struct pic_motion_params_old;
47 struct pic_motion_params;
48 
49 typedef struct image_structure ImageStructure;
50 typedef struct info_8x8 Info8x8;
51 typedef struct search_window SearchWindow;
52 typedef struct block_pos BlockPos;
53 typedef struct bit_counter BitCounter;
54 typedef struct encoding_environment EncodingEnvironment;
55 typedef EncodingEnvironment *EncodingEnvironmentPtr;
56 typedef struct bi_context_type BiContextType;
57 typedef BiContextType *BiContextTypePtr;
58 
59 struct image_structure
60 {
61   FrameFormat format;      //!< ImageStructure format Information
62   imgpel **data[3];        //!< ImageStructure pixel data
63 };
64 
65 //! block 8x8 temporary RD info
66 struct info_8x8
67 {
68    char   mode;
69    char   pdir;
70    char   ref[2];
71    char   bipred;
72 };
73 
74 struct search_window {
75   int    min_x;
76   int    max_x;
77   int    min_y;
78   int    max_y;
79 };
80 
81 struct block_pos
82 {
83   short x;
84   short y;
85 };
86 
87 //! bit counter for a macroblock. Note that it seems safe to change all to unsigned short for 16x16 MBs. May be an issue if MB > 32x32
88 struct bit_counter
89 {
90   int mb_total;
91   unsigned short mb_mode;
92   unsigned short mb_inter;
93   unsigned short mb_cbp;
94   unsigned short mb_delta_quant;
95   int mb_y_coeff;
96   int mb_uv_coeff;
97   int mb_cb_coeff;
98   int mb_cr_coeff;
99   int mb_stuffing;
100 };
101 
102 //! struct to characterize the state of the arithmetic coding engine
103 struct encoding_environment
104 {
105   struct video_par *p_Vid;
106   unsigned int  Elow, Erange;
107   unsigned int  Ebuffer;
108   unsigned int  Ebits_to_go;
109   unsigned int  Echunks_outstanding;
110   int           Epbuf;
111   byte          *Ecodestrm;
112   int           *Ecodestrm_len;
113   int           C;
114   int           E;
115 };
116 
117 //! struct for context management
118 struct bi_context_type
119 {
120   unsigned long  count;
121   byte state; //uint16 state;         // index into state-table CP
122   unsigned char  MPS;           // Least Probable Symbol 0/1 CP
123 };
124 
125 
126 
127 /**********************************************************************
128  * 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
129  **********************************************************************
130  */
131 
132 typedef struct
133 {
134   BiContextType mb_type_contexts [3][NUM_MB_TYPE_CTX];
135   BiContextType b8_type_contexts [2][NUM_B8_TYPE_CTX];
136   BiContextType mv_res_contexts  [2][NUM_MV_RES_CTX];
137   BiContextType ref_no_contexts  [2][NUM_REF_NO_CTX];
138 } MotionInfoContexts;
139 
140 typedef struct
141 {
142   BiContextType  transform_size_contexts   [NUM_TRANSFORM_SIZE_CTX];
143   BiContextType  ipr_contexts [NUM_IPR_CTX];
144   BiContextType  cipr_contexts[NUM_CIPR_CTX];
145   BiContextType  cbp_contexts [3][NUM_CBP_CTX];
146   BiContextType  bcbp_contexts[NUM_BLOCK_TYPES][NUM_BCBP_CTX];
147   BiContextType  delta_qp_contexts   [NUM_DELTA_QP_CTX];
148   BiContextType  one_contexts [NUM_BLOCK_TYPES][NUM_ONE_CTX];
149   BiContextType  abs_contexts [NUM_BLOCK_TYPES][NUM_ABS_CTX];
150 #if ENABLE_FIELD_CTX
151   BiContextType  mb_aff_contexts [NUM_MB_AFF_CTX];
152   BiContextType  map_contexts [2][NUM_BLOCK_TYPES][NUM_MAP_CTX];
153   BiContextType  last_contexts[2][NUM_BLOCK_TYPES][NUM_LAST_CTX];
154 #else
155   BiContextType  map_contexts [1][NUM_BLOCK_TYPES][NUM_MAP_CTX];
156   BiContextType  last_contexts[1][NUM_BLOCK_TYPES][NUM_LAST_CTX];
157 #endif
158 } TextureInfoContexts;
159 
160 //*********************** end of data type definition for CABAC *******************
161 
162 //! Pixel position for checking neighbors
163 typedef struct pix_pos
164 {
165   int   available;
166   int   mb_addr;
167   short x;
168   short y;
169   short pos_x;
170   short pos_y;
171 } PixelPos;
172 
173 //! Motion Vector structure
174 typedef struct
175 {
176   short mv_x;
177   short mv_y;
178 } MotionVector;
179 
180 //! Bipred Motion Vector structure
181 typedef struct
182 {
183   short refIdx[2];
184   MotionVector mv[2];
185   distblk dist;
186   distblk sad;
187 } BiPredMVInfo;
188 
189 static const MotionVector zero_mv = {0, 0};
190 
191 typedef struct rd_8x8_data
192 {
193   distblk  mb_p8x8_cost;
194   distblk  smb_p8x8_cost[4];
195   distblk  smb_p8x8_rdcost[4];
196   Info8x8 part[4];
197 
198   int cbp8x8;
199   int cbp_blk8x8;
200   int cnt_nonz_8x8;
201 
202   int **lrec; // transform and quantized coefficients will be stored here for SP frames
203   imgpel **mpr8x8;
204   imgpel ***mpr8x8CbCr;
205   imgpel **rec_mbY8x8;
206   imgpel ***rec_mb8x8_cr;
207 } RD_8x8DATA;
208 
209 typedef struct rd_params
210 {
211   double lambda_md;        //!< Mode decision Lambda
212 //#if JCOST_CALC_SCALEUP
213   int    lambda_mdfp;       //!< Fixed point mode decision lambda;
214 //#endif
215   double lambda_me[3];     //!< Motion Estimation Lambda
216   int    lambda_mf[3];     //!< Integer formatted Motion Estimation Lambda
217   int    best_mcost[2];
218 
219   short  valid[MAXMODE];
220   short  curr_mb_field;
221 } RD_PARAMS;
222 
223 
224 //! Set Explicit GOP Parameters.
225 //! Currently only supports Enhancement GOP but could be easily extended
226 typedef struct
227 {
228   int slice_type;         //! Slice type
229   int display_no;         //! GOP Display order
230   int reference_idc;      //! Is reference?
231   int slice_qp_off;       //! Assigned QP offset
232   int hierarchy_layer;    //! Hierarchy layer (used with GOP Hierarchy option 2)
233   int hierarchyPocDelta;  //! Currently unused
234   int temporal_layer;     //! Temporal layer;
235   // This information is used for hierarchical coding with temporal scalability feature
236   // If ReferenceReorder = 2, then
237   //    reference reordering is applied based on POC and then frames of higher temporal layer are prevented from being referenced
238   // If PocMemoryManagement = 2, then
239   //    after encoding a reference frame with temporal layer T, a list of MMCO commands to delete higher temporal layer frames is created
240 
241 } GOP_DATA;
242 
243 //! Buffer structure for decoded reference picture marking commands
244 typedef struct DecRefPicMarking_s
245 {
246   int memory_management_control_operation;
247   int difference_of_pic_nums_minus1;
248   int long_term_pic_num;
249   int long_term_frame_idx;
250   int max_long_term_frame_idx_plus1;
251   struct DecRefPicMarking_s *Next;
252 } DecRefPicMarking_t;
253 
254 typedef struct me_block
255 {
256   struct video_par *p_Vid;
257 
258   short            pos_x_padded;    //!< position x in image
259   short            pos_y_padded;    //!< position y in image
260 
261   short            blocktype;
262   short            blocksize_x;     //!< block size x dimension
263   short            blocksize_y;     //!< block size y dimension
264   short            blocksize_cr_x;  //!< block size x dimension
265   short            blocksize_cr_y;  //!< block size y dimension
266 
267   short            block_x;         //!< position x in MB (subpartitions)
268   short            block_y;         //!< position y in MB
269 
270   short            pos_x;           //!< position x in image
271   short            pos_y;           //!< position y in image
272   short            pos_x2;          //!< position x >> 2 in image
273   short            pos_y2;          //!< position y >> 2 in image
274 
275   short            pos_cr_x;        //!< position x in image
276   short            pos_cr_y;        //!< position y in image
277 
278   short            hme_level;        //!< level for HME search;
279   int              hme_ref_size_y_pad;
280   int              hme_ref_size_x_pad;
281   int              hme_ref_size_y_max;
282   int              hme_ref_size_x_max;
283   char             list;             //!< current list (list 0 or list 1). This is needed for bipredictive ME
284   char             ref_idx;          //!< current reference index
285   MotionVector     mv[2];            //!< motion vectors (L0/L1)
286   PixelPos         block[4];
287   struct slice    *p_Slice;
288   struct search_window searchRange;
289   int              cost;           //!< Rate Distortion cost
290   imgpel         **orig_pic;      //!< Block Data
291   int              ChromaMEEnable;
292   int              ChromaMEWeight;
293   // use weighted prediction based ME
294   int              apply_weights;
295   int              apply_bi_weights;
296   // 8x8 transform
297   int              test8x8;
298   // Single List Prediction weights
299   short            weight_luma;
300   short            weight_cr[2];
301   short            offset_luma;
302   short            offset_cr[2];
303 
304   // Biprediction weights
305   short            weight1;
306   short            weight2;
307   short            offsetBi;
308   short            weight1_cr[2];
309   short            weight2_cr[2];
310   short            offsetBi_cr[2];
311   int              search_pos2;   // <--  search positions for    half-pel search  (default: 9)
312   int              search_pos4;   // <--  search positions for quarter-pel search  (default: 9)
313   int              iteration_no;  // <--  bi pred iteration number
314 
315   // functions
316   distblk (*computePredFPel)    (struct storable_picture *, struct me_block *, distblk , MotionVector * );
317   distblk (*computePredHPel)    (struct storable_picture *, struct me_block *, distblk , MotionVector * );
318   distblk (*computePredQPel)    (struct storable_picture *, struct me_block *, distblk , MotionVector * );
319   distblk (*computeBiPredFPel)  (struct storable_picture *, struct storable_picture *, struct me_block *, distblk , MotionVector *, MotionVector *);
320   distblk (*computeBiPredHPel)  (struct storable_picture *, struct storable_picture *, struct me_block *, distblk , MotionVector *, MotionVector *);
321   distblk (*computeBiPredQPel)  (struct storable_picture *, struct storable_picture *, struct me_block *, distblk , MotionVector *, MotionVector *);
322 } MEBlock;
323 
324 //! Syntax Element
325 typedef struct syntaxelement_enc
326 {
327   int                 type;           //!< type of syntax element for data part.
328   int                 value1;         //!< numerical value of syntax element
329   int                 value2;         //!< for blocked symbols, e.g. run/level
330   int                 len;            //!< length of code
331   int                 inf;            //!< info part of UVLC code
332   unsigned int        bitpattern;     //!< UVLC bitpattern
333   int                 context;        //!< CABAC context
334 
335 #if TRACE
336   #define             TRACESTRING_SIZE 100            //!< size of trace string
337   char                tracestring[TRACESTRING_SIZE];  //!< trace string
338 #endif
339 
340   //!< for mapping of syntaxElement to UVLC
341   void    (*mapping)(int value1, int value2, int* len_ptr, int* info_ptr);
342 
343 } SyntaxElement;
344 
345 typedef struct wp_params
346 {
347   short weight[3];
348   short offset[3];
349 } WPParams;
350 
351 //! Macroblock
352 typedef struct macroblock_enc
353 {
354   struct slice       *p_Slice;                    //!< pointer to the current slice
355   struct video_par   *p_Vid;                      //!< pointer to VideoParameters
356   InputParameters    *p_Inp;
357   int                 mbAddrX;                    //!< current MB address
358   short               mb_type;                    //!< current MB mode type
359   short               slice_nr;                   //!< current MB slice id
360   short               mb_x;                       //!< current MB horizontal
361   short               mb_y;                       //!< current MB vertical
362   short               block_x;                    //!< current block horizontal
363   short               block_y;                    //!< current block vertical
364 
365   short               pix_x;                      //!< current pixel horizontal
366   short               pix_y;                      //!< current pixel vertical
367   short               pix_c_x;                    //!< current pixel chroma horizontal
368   short               pix_c_y;                    //!< current pixel chroma vertical
369 
370   short               opix_y;                     //!< current original picture pixel vertical
371   short               opix_c_y;                   //!< current original picture pixel chroma vertical
372 
373   short               subblock_x;                 //!< current subblock horizontal
374   short               subblock_y;                 //!< current subblock vertical
375 
376   short               list_offset;
377   Boolean             prev_recode_mb;
378   int                 DeblockCall;
379 
380   int                 mbAddrA, mbAddrB, mbAddrC, mbAddrD;
381   byte                mbAvailA, mbAvailB, mbAvailC, mbAvailD;
382 
383   short               qp;                         //!< QP luma
384   short               qpc[2];                     //!< QP chroma
385   short               qp_scaled[MAX_PLANE];       //!< QP scaled for all comps.
386   short               qpsp;
387   int                 cbp;
388   short               prev_qp;
389   short               prev_dqp;
390   int                 prev_cbp;
391   int                 cr_cbp[3];        // 444. Should be added in an external structure
392   int                 c_nzCbCr[3];
393 
394   short               i16offset;
395 
396   BitCounter          bits;
397 
398   short               ar_mode; //!< mb type to store adaptive rounding parameters
399   short               mvd[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE][2];          //!< indices correspond to [list][block_y][block_x][x,y]
400   char                c_ipred_mode;      //!< chroma intra prediction mode
401   char                i16mode;
402 
403   Info8x8             b8x8[4];
404   //imgpel              intra4x4_pred[16][17]; //!< 4x4 Intra prediction samples
405   imgpel              **intra4x4_pred; //[3][17]; //!< 4x4 Intra prediction samples
406   imgpel              **intra4x4_pred_buf[2]; //[3][17]; //!< 4x4 Intra prediction samples
407   imgpel              **intra8x8_pred; //[3][25]; //!< 8x8 Intra prediction samples
408   imgpel              **intra8x8_pred_buf[2]; //[3][25]; //!< 8x8 Intra prediction samples
409   imgpel              **intra16x16_pred; //[3][33]; //!< 8x8 Intra prediction samples
410   imgpel              **intra16x16_pred_buf[2]; //[3][33]; //!< 8x8 Intra prediction samples
411 
412   char                IntraChromaPredModeFlag;
413   byte                mb_field;
414   byte                is_field_mode;
415   byte                luma_transform_size_8x8_flag;
416   byte                temp_transform_size_8x8_flag;
417   byte                NoMbPartLessThan8x8Flag;
418   byte                valid_8x8;
419   byte                valid_4x4;
420   byte                write_mb;
421   byte                is_intra_block;
422 
423   char                DFDisableIdc;
424   char                DFAlphaC0Offset;
425   char                DFBetaOffset;
426 
427   int                 skip_flag;
428 
429   char                intra_pred_modes   [MB_BLOCK_PARTITIONS];
430   char                intra_pred_modes8x8[MB_BLOCK_PARTITIONS];           //!< four 8x8 blocks in a macroblock
431 
432   int64               cbp_blk ;    //!< 1 bit set for every 4x4 block with coefs (not implemented for INTRA)
433   int64               cbp_bits[3];
434   int64               cbp_bits_8x8[3];
435 
436   distblk              min_rdcost;
437   distblk              min_dcost;
438   distblk              min_rate;
439   int                  min_bits;
440 
441   short               best_mode;
442   short               best_i16offset;
443   char                best_i16mode;
444   char                best_c_imode;
445   int                 best_cbp;
446 
447   //For residual DPCM
448   short               ipmode_DPCM;
449 
450 
451 
452   struct macroblock_enc   *mb_up;   //!< pointer to neighboring MB (CABAC)
453   struct macroblock_enc   *mb_left; //!< pointer to neighboring MB (CABAC)
454   struct macroblock_enc   *PrevMB;
455 
456   // Functions
457   void (*GetMVPredictor) (struct macroblock_enc *currMB, PixelPos *block, MotionVector *pmv, short ref_frame,
458     struct pic_motion_params **mv_info, int list, int mb_x, int mb_y, int blockshape_x, int blockshape_y);
459   distblk (*IntPelME)       (struct macroblock_enc *currMB, MotionVector *, struct me_block *mv_block, distblk, int);
460   distblk (*SubPelBiPredME) (struct macroblock_enc *currMB, struct me_block *, int list,
461     MotionVector *pred_mv1, MotionVector *pred_mv2, MotionVector *mv1, MotionVector *mv2, distblk min_mcost, int* lambda_factor);
462   distblk (*SubPelME)       (struct macroblock_enc *currMB,
463     MotionVector *pred_mv, struct me_block *mv_block, distblk min_mcost, int* lambda_factor);
464   distblk (*BiPredME)       (struct macroblock_enc *currMB, int, MotionVector *, MotionVector *, MotionVector *, MotionVector *, struct me_block *, int, distblk, int);
465   int  (*residual_transform_quant_luma_4x4)      (struct macroblock_enc *currMB, ColorPlane pl, int block_x, int block_y, int *coeff_cost, int intra);
466   int  (*residual_transform_quant_luma_16x16)    (struct macroblock_enc *currMB, ColorPlane pl);
467   int  (*residual_transform_quant_luma_8x8)      (struct macroblock_enc *currMB, ColorPlane pl, int b8, int *coeff_cost, int intra);
468   int  (*residual_transform_quant_chroma_4x4[2])(struct macroblock_enc *currMB, int uv,int i11);
469   void (*p_SetupFastFullPelSearch) (struct macroblock_enc *, MEBlock *, int ) ;
470 
471   void (*cbp_linfo_intra)(int cbp, int dummy, int *len,int *info);
472   void (*cbp_linfo_inter)(int cbp, int dummy, int *len,int *info);
473 } Macroblock;
474 
475 //! Bitstream
476 struct bit_stream_enc
477 {
478   int     buffer_size;        //!< Buffer size
479   int     byte_pos;           //!< current position in bitstream;
480   int     bits_to_go;         //!< current bitcounter
481 
482   int     stored_byte_pos;    //!< storage for position in bitstream;
483   int     stored_bits_to_go;  //!< storage for bitcounter
484   int     byte_pos_skip;      //!< storage for position in bitstream;
485   int     bits_to_go_skip;    //!< storage for bitcounter
486   int     write_flag;         //!< Bitstream contains data and needs to be written
487 
488   byte    byte_buf;           //!< current buffer for last written byte
489   byte    stored_byte_buf;    //!< storage for buffer of last written byte
490   byte    byte_buf_skip;      //!< current buffer for last written byte
491   byte    *streamBuffer;      //!< actual buffer for written bytes
492 
493 #if TRACE
494   Boolean trace_enabled;
495 #endif
496 };
497 
498 
499 //! DataPartition
500 typedef struct datapartition_enc
501 {
502   struct slice        *p_Slice;
503   struct video_par      *p_Vid;
504   InputParameters     *p_Inp;   //!< pointer to the input parameters
505   Bitstream           *bitstream;
506   NALU_t              *nal_unit;
507   EncodingEnvironment ee_cabac;
508   EncodingEnvironment ee_recode;
509 } DataPartition;
510 
511 /*! For MB level field/frame coding tools
512     temporary structure to store MB data for field/frame coding */
513 typedef struct rd_data
514 {
515   double  min_rdcost;
516   double  min_dcost;
517   double  min_rate;
518 
519   imgpel  ***rec_mb;            //!< hold the components of reconstructed MB
520   int     ****cofAC;
521   int     ***cofDC;
522   int     *****cofAC_new;
523   short   mb_type;
524   int64   cbp_blk;
525   int     cbp;
526   int     prev_cbp;
527   int     mode;
528   short   i16offset;
529   char    i16mode;
530   Boolean NoMbPartLessThan8x8Flag;
531 
532   char    c_ipred_mode;
533   short   qp;
534   short   prev_qp;
535   short   prev_dqp;
536   byte    luma_transform_size_8x8_flag;
537   Info8x8 block;
538   Info8x8 b8x8[4];
539 
540   MotionVector   *****all_mv;         //!< all modes motion vectors
541   MotionVector ******bipred_mv;       //!< Bipredictive motion vectors
542 
543   char    intra_pred_modes[16];
544   char    intra_pred_modes8x8[16];
545   char    **ipredmode;
546   char    ***refar;                   //!< reference frame array [list][y][x]
547 } RD_DATA;
548 
549  //! Slice
550 typedef struct slice
551 {
552   struct video_par    *p_Vid;   // pointer to the original video structure
553   InputParameters     *p_Inp;   // pointer to the input parameters
554   pic_parameter_set_rbsp_t *active_pps;
555   seq_parameter_set_rbsp_t *active_sps;
556 
557   struct decoded_picture_buffer *p_Dpb;
558 
559   int                 layer_id;
560   int                 picture_id;
561   int                 qp;
562   int                 qs;
563   short               slice_type;   //!< picture type
564   unsigned int        frame_num;
565   unsigned int        max_frame_num;
566   signed int          framepoc;     //!< min (toppoc, bottompoc)
567   signed int          ThisPOC;      //!< current picture POC
568   short               slice_nr;
569   int                 model_number;
570   int                 colour_plane_id;   //!< colour plane id for 4:4:4 profile
571   int                 lossless_qpprime_flag;
572   int                 P444_joined;
573   int                 disthres;
574   int                 Transform8x8Mode;
575   int                 rdoq_motion_copy;
576   // RDOQ at the slice level (note this allows us to reduce passed parameters,
577   // but also could enable RDOQ control at the slice level.
578   int                 UseRDOQuant;
579   int                 RDOQ_QP_Num;
580 
581   char                num_ref_idx_active[2];
582   int                 ref_pic_list_reordering_flag[2];
583   int                 *modification_of_pic_nums_idc[2];
584   int                 *abs_diff_pic_num_minus1[2];
585   int                 *long_term_pic_idx[2];
586 #if (MVC_EXTENSION_ENABLE)
587   int                 *abs_diff_view_idx_minus1[2];
588 #endif
589   int                 view_id;
590   int                 width_blk;               //!< Number of columns in blocks
591   int                 height_blk;              //!< Number of lines in blocks
592 
593   PictureStructure    structure;
594   Boolean             mb_aff_frame_flag;
595 
596   int                 start_mb_nr;
597   int                 max_part_nr;  //!< number of different partitions
598   int                 num_mb;       //!< number of MBs in the slice
599 
600   int                 cmp_cbp[3];
601   int                 curr_cbp[2];
602   char                symbol_mode;
603   short               NoResidueDirect;
604   short               partition_mode;
605   short               idr_flag;
606   int                 frame_no;
607   unsigned int        PicSizeInMbs;
608   int                 num_blk8x8_uv;
609   int                 nal_reference_idc;                       //!< nal_reference_idc from NAL unit
610 
611   // Bit depth related information
612   short               bitdepth_luma;
613   short               bitdepth_chroma;
614   int                 bitdepth_scale[2];
615   int                 bitdepth_luma_qp_scale;
616   int                 bitdepth_chroma_qp_scale;
617   int                 bitdepth_lambda_scale;
618 
619   DataPartition       *partArr;     //!< array of partitions
620   MotionInfoContexts  *mot_ctx;     //!< pointer to struct of context models for use in CABAC
621   TextureInfoContexts *tex_ctx;     //!< pointer to struct of context models for use in CABAC
622 
623   int                 mvscale[6][MAX_REFERENCE_PICTURES];
624   char                direct_spatial_mv_pred_flag;              //!< Direct Mode type to be used (0: Temporal, 1: Spatial)
625   // Deblocking filter parameters
626   char                DFDisableIdc;                             //!< Deblocking Filter Disable indicator
627   char                DFAlphaC0Offset;                          //!< Deblocking Filter Alpha offset
628   char                DFBetaOffset;                             //!< Deblocking Filter Beta offset
629 
630   byte                weighted_prediction;                       //!< Use of weighted prediction
631   byte                weighted_bipred_idc;                      //!< Use of weighted biprediction (note that weighted_pred_flag is probably not needed here)
632 
633   short               luma_log_weight_denom;
634   short               chroma_log_weight_denom;
635   short               wp_luma_round;
636   short               wp_chroma_round;
637 
638   short  max_num_references;      //!< maximum number of reference pictures that may occur
639   // Motion vectors for a macroblock
640   // These need to be changed to MotionVector parameters
641   MotionVector *****all_mv;         //!< replaces local all_mv
642   MotionVector ******bipred_mv;     //!< Biprediction MVs
643   //Weighted prediction
644   short ***wp_weight;         //!< weight in [list][index][component] order
645   short ***wp_offset;         //!< offset in [list][index][component] order
646   short ****wbp_weight;       //!< weight in [list][fwd_index][bwd_idx][component] order
647 
648   int *****cofAC_new;          //!< AC coefficients [comp][8x8block][4x4block][level/run][scan_pos]
649 
650   int ****cofAC;               //!< AC coefficients [8x8block][4x4block][level/run][scan_pos]
651   int *** cofDC;               //!< DC coefficients [yuv][level/run][scan_pos]
652 
653   // For rate control
654   int diffy[16][16];
655 
656   int64 cur_cbp_blk[MAX_PLANE];
657   int coeff_cost_cr[MAX_PLANE];
658 
659 
660   int **tblk16x16;   //!< Transform related array
661   int **tblk4x4;     //!< Transform related array
662   int ****i16blk4x4;
663 
664   RD_DATA *rddata;
665   // RD_DATA data. Moved here to enable parallelization at the slice level
666   // of RDOQ
667   RD_DATA rddata_trellis_best;
668   RD_DATA rddata_trellis_curr;
669   //!< For MB level field/frame coding tools
670   RD_DATA rddata_top_frame_mb;
671   RD_DATA rddata_bot_frame_mb;
672   RD_DATA rddata_top_field_mb;
673   RD_DATA rddata_bot_field_mb;
674 
675   Boolean si_frame_indicator;
676   Boolean sp2_frame_indicator;
677 
678   char    ***direct_ref_idx;           //!< direct mode reference index buffer
679   char    **direct_pdir;               //!< direct mode direction buffer
680 
681   MotionVector ****tmp_mv8;
682   MotionVector ****tmp_mv4;
683 
684   distblk    ***motion_cost8;
685   distblk    ***motion_cost4;
686   int     deltaQPTable[9];
687 
688   // RDOQ
689   struct est_bits_cabac *estBitsCabac; // [NUM_BLOCK_TYPES]
690   double norm_factor_4x4;
691   double norm_factor_8x8;
692   int    norm_shift_4x4;
693   int    norm_shift_8x8;
694 
695   imgpel ****mpr_4x4;           //!< prediction samples for   4x4 intra prediction modes
696   imgpel ****mpr_8x8;           //!< prediction samples for   8x8 intra prediction modes
697   imgpel ****mpr_16x16;         //!< prediction samples for 16x16 intra prediction modes (and chroma)
698   imgpel ***mb_pred;            //!< current best prediction mode
699   int ***mb_rres;               //!< the diff pixel values between the original macroblock/block and its prediction (reconstructed)
700   int ***mb_ores;               //!< the diff pixel values between the original macroblock/block and its prediction (original)
701 
702   // Residue Color Transform
703   char b8_ipredmode8x8[4][4];
704   char b8_intra_pred_modes8x8[16];
705   char b4_ipredmode[16];
706   char b4_intra_pred_modes[16];
707 
708   struct rdo_structure    *p_RDO;
709   struct epzs_params      *p_EPZS;
710 
711   // This should be the right location for this
712   struct storable_picture **listX[6];
713   char listXsize[6];
714 
715   // Some Cabac related parameters (could be put in a different structure so we can dynamically allocate them when needed)
716   int  coeff[64];
717   int  coeff_ctr;
718   int  pos;
719 
720   int default_pic_num[2][MAX_REFERENCE_PICTURES];
721   int default_view_id[2][MAX_REFERENCE_PICTURES];
722 
723   // Function pointers
724   int     (*mode_decision_for_I16x16_MB)        (Macroblock *currMB, int lambda);
725   int     (*mode_decision_for_I4x4_blocks)      (Macroblock *currMB, int  b8,  int  b4,  int  lambda, distblk*  min_cost);
726   int     (*mode_decision_for_I8x8_blocks)      (Macroblock *currMB, int b8, int lambda, distblk *min_cost);
727   distblk (*rdcost_for_4x4_intra_blocks)        (Macroblock *currMB, int *c_nz, int b8, int b4, int ipmode, int lambda, int mostProbableMode, distblk min_rdcost);
728   distblk (*rdcost_for_8x8_intra_blocks)        (Macroblock *currMB, int *c_nz, int b8, int ipmode, int lambda, distblk min_rdcost, int mostProbableMode);
729   void    (*submacroblock_mode_decision)        (Macroblock *currMB, RD_PARAMS *, RD_8x8DATA *, int ****, int, distblk *);
730   void    (*intra_chroma_RD_decision)           (Macroblock *currMB, RD_PARAMS *);
731 
732   void    (*set_intrapred_4x4)                  (Macroblock *currMB, ColorPlane pl, int img_x,int img_y, int *left_available, int *up_available, int *all_available);
733   void    (*set_intrapred_8x8)                  (Macroblock *currMB, ColorPlane pl, int img_x,int img_y, int *left_available, int *up_available, int *all_available);
734   void    (*set_intrapred_16x16)                (Macroblock *currMB, ColorPlane pl, int *left_available, int *up_available, int *all_available);
735   void    (*intra_chroma_prediction)            (Macroblock *currMB, int*, int*, int*);
736   void    (*rdo_low_intra_chroma_decision)      (Macroblock *currMB, int mb_available_up, int mb_available_left[2], int mb_available_up_left);
737 
738   void    (*Get_Direct_Motion_Vectors)          (Macroblock *currMB);
739   Boolean (*slice_too_big)                      (int bits_slice); //!< for use of callback functions
740   void    (*set_motion_vectors_mb)              (Macroblock *currMB);
741   void    (*encode_one_macroblock)              (Macroblock *currMB);
742   void    (*set_stored_mb_parameters)           (Macroblock *currMB);
743   void    (*set_ref_and_motion_vectors)         (Macroblock *currMB, struct pic_motion_params **motion, Info8x8 *part, int block);
744 
745   void    (*luma_residual_coding)               (Macroblock *currMB);
746   int     (*luma_residual_coding_8x8)           (Macroblock* currMB, int*, int64*, int, short, int[2], char *);
747   void    (*chroma_residual_coding)             (Macroblock *currMB);
748 
749   distblk (*compute_cost8x8)                    (struct video_par *p_Vid, imgpel **cur_img, imgpel **prd_img, int pic_opix_x, distblk min_cost);
750   distblk (*compute_cost4x4)                    (struct video_par *p_Vid, imgpel **cur_img, imgpel **prd_img, int pic_opix_x, distblk min_cost);
751   distblk (*find_sad_16x16)                     (Macroblock *currMB);
752   distblk (*distI16x16)                         (Macroblock *currMB, imgpel **img_org, imgpel **pred_img, distblk min_cost);
753 
754   void    (*set_lagrangian_multipliers)         (struct slice *currSlice);
755 
756   void (*set_modes_and_refs_for_blocks)         (Macroblock *currMB, short mode);
757   void (*set_coeff_and_recon_8x8      )         (Macroblock *currMB);
758   // Write MB info function pointers
759   void (*writeMB_typeInfo)      (Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
760   void (*writeMB_Skip)          (Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
761   void (*writeIntraPredMode)    (SyntaxElement *se, DataPartition *dP);
762   void (*writeB8_typeInfo)      (SyntaxElement *se, DataPartition *dP);
763   void (*writeRefFrame[6])      (Macroblock* currMB, SyntaxElement *se, DataPartition *dP);
764   int  (*writeMotionInfo2NAL)   (Macroblock* currMB);
765   int  (*write_MB_layer)        (Macroblock *currMB, int rdopt, int *coeff_rate);
766   void (*writeMVD)              (Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
767   void (*writeCBP)              (Macroblock* currMB, SyntaxElement *se, DataPartition *dP);
768   void (*writeDquant)           (Macroblock* currMB, SyntaxElement *se, DataPartition *dP);
769   void (*writeCIPredMode)       (Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
770   void (*writeFieldModeInfo)    (Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
771   void (*writeMB_transform_size)(Macroblock *currMB, SyntaxElement *se, DataPartition *dP);
772   int  (*writeCoeff16x16)       (Macroblock* currMB, ColorPlane);
773   int  (*writeCoeff4x4_CAVLC)   (Macroblock* currMB, int block_type, int b8, int b4, int param);
774   void (*write_and_store_CBP_block_bit) (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int cbp_bit, TextureInfoContexts*  tex_ctx);
775 
776   // Coding state
777   void (*reset_coding_state) (Macroblock *currMB, struct coding_state *);
778   void (*store_coding_state) (Macroblock *currMB, struct coding_state *);
779 
780   // List initialization / reordering
781   void (*init_lists               ) (struct slice *currSlice);
782   void (*reorder_lists            ) (struct slice *currSlice);
783   void (*poc_ref_pic_reorder_frame) (struct slice *currSlice, unsigned num_ref_idx_lX_active, int list_no );
784 
785 
786   // Quantization
787   int (*quant_4x4)     (Macroblock *currMB, int **tblock, struct quant_methods *q_method);
788   int (*quant_ac4x4cr) (Macroblock *currMB, int **tblock, struct quant_methods *q_method);
789   int (*quant_dc4x4)   (Macroblock *currMB, int **tblock, int qp, int*  DCLevel, int*  DCRun, LevelQuantParams *q_params_4x4, const byte (*pos_scan)[2]);
790   int (*quant_ac4x4)   (Macroblock *currMB, int **tblock, struct quant_methods *q_method);
791   int (*quant_8x8)     (Macroblock *currMB, int **tblock, struct quant_methods *q_method);
792   int (*quant_8x8cavlc)(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int***  cofAC);
793 
794   int (*quant_dc_cr)     (Macroblock *currMB, int **tblock, int qp, int* DCLevel, int* DCRun,
795     LevelQuantParams *q_params_4x4, int **fadjust, const byte (*pos_scan)[2]);
796   void (*rdoq_4x4)       (Macroblock *currMB, int **tblock, struct quant_methods *q_method, int levelTrellis[16]);
797 
798   void (*rdoq_dc)        (Macroblock *currMB, int **tblock, int qp_per, int qp_rem, LevelQuantParams *q_params_4x4,
799     const byte (*pos_scan)[2], int levelTrellis[16], int type);
800 
801   void (*rdoq_ac4x4)     (Macroblock *currMB, int **tblock , struct quant_methods *q_method, int levelTrellis[16]);
802 
803   void (*rdoq_dc_cr)     (Macroblock *currMB, int **tblock, int qp_per, int qp_rem, LevelQuantParams *q_params_4x4,
804     const byte (*pos_scan)[2], int levelTrellis[16], int type);
805 
806   void (*set_modes_and_reframe)    (Macroblock *currMB, int b8, short* p_dir, int list_mode[2], char *list_ref_idx);
807   void (*store_8x8_motion_vectors) (struct slice *currSlice, int dir, int block8x8, Info8x8 *B8x8Info);
808   distblk (*getDistortion)         ( Macroblock *currMB );
809 
810 } Slice;
811 
812 #if (MVC_EXTENSION_ENABLE)
813 typedef struct prev_coding_stats
814 {
815   int frm_no_in_file;
816   char pic_type[256];
817   int view_id;
818   int cur_bits;
819   int wp_method;
820   int AverageFrameQP;
821   int lambda;
822   float psnr_value[3];
823   float ssim_value[3];
824   int tmp_time;
825   int me_time;
826   int fld_flag;
827   int intras;
828   int direct_mode;
829   int num_ref_idx_l0_active;
830   int num_ref_idx_l1_active;
831   int rd_pass;
832   int nal_reference_idc;
833 
834 } PrevCodingStats;
835 #endif
836 
837 //! DistortionParams
838 typedef struct distortion_metric
839 {
840   float value[3];                    //!< current frame distortion
841   float average[3];                  //!< average frame distortion
842   float avslice[NUM_SLICE_TYPES][3]; //!< average frame type distortion
843 } DistMetric;
844 
845 typedef struct distortion_params
846 {
847   int        frame_ctr;                     //!< number of coded frames
848   DistMetric metric[TOTAL_DIST_TYPES];      //!< Distortion metrics
849 #if (MVC_EXTENSION_ENABLE)
850   int        frame_ctr_v[2];                //!< number of coded frames for each view
851   DistMetric metric_v[2][TOTAL_DIST_TYPES]; //!< Distortion metrics for each view
852 #endif
853 } DistortionParams;
854 
855 typedef struct picture
856 {
857   int   no_slices;
858   int   bits_per_picture;
859   struct slice *slices[MAXSLICEPERPICTURE];
860 
861   DistMetric distortion;
862   byte  idr_flag;
863 } Picture;
864 
865 //! block 8x8 temporary RD info
866 typedef struct block_8x8_info
867 {
868   Info8x8 best[MAXMODE][4];
869 } Block8x8Info;
870 
871 typedef struct coding_par
872 {
873   int layer_id;
874   ColorFormat yuv_format;
875   int P444_joined;
876 
877   short bitdepth_luma;
878   short bitdepth_chroma;
879   int bitdepth_scale[2];
880   int bitdepth_luma_qp_scale;
881   int bitdepth_chroma_qp_scale;
882   int bitdepth_lambda_scale;
883 
884   uint32 dc_pred_value_comp[MAX_PLANE]; //!< component value for DC prediction (depends on component pel bit depth)
885   int max_pel_value_comp      [MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
886   int max_imgpel_value_comp_sq   [MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
887 
888   int num_blk8x8_uv;
889   int num_cdc_coeff;
890 
891   short mb_cr_size_x;
892   short mb_cr_size_y;
893   int mb_size[MAX_PLANE][2];
894 
895   int max_bitCount;
896   int width;                   //!< Number of pels
897   int width_padded;            //!< Width in pels of padded picture
898   int width_blk;               //!< Number of columns in blocks
899   int width_cr;                //!< Number of pels chroma
900   int height;                  //!< Number of lines
901   int height_padded;           //!< Number in lines of padded picture
902   int height_blk;              //!< Number of lines in blocks
903   int height_cr;               //!< Number of lines  chroma
904   int height_cr_frame;         //!< Number of lines  chroma frame
905   int size;                    //!< Luma Picture size in pels
906   int size_cr;                 //!< Chroma Picture size in pels
907 
908   int padded_size_x;
909   int padded_size_x_m8x8;
910   int padded_size_x_m4x4;
911   int cr_padded_size_x;
912   int cr_padded_size_x_m8;
913   int cr_padded_size_x2;
914   int cr_padded_size_x4;
915 
916   int pad_size_uv_x;
917   int pad_size_uv_y;
918   unsigned char chroma_mask_mv_y;
919   unsigned char chroma_mask_mv_x;
920   int chroma_shift_x;
921   int chroma_shift_y;
922   int shift_cr_x;
923   int shift_cr_y;
924 
925   unsigned int PicWidthInMbs;
926   unsigned int PicHeightInMapUnits;
927   unsigned int FrameHeightInMbs;
928   unsigned int FrameSizeInMbs;
929 
930   unsigned int frame_num;
931   unsigned int prevFrameNum;
932   unsigned int FrameNumOffset;
933   unsigned int prevFrameNumOffset;
934   int last_ref_idc;
935 
936   //uint32 dc_pred_value;                 //!< DC prediction value for current component
937   //short max_imgpel_value;              //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
938   char md5String[2][33];
939 }CodingParameters;
940 
941 //! VideoParameters
942 typedef struct video_par
943 {
944   InputParameters          *p_Inp;
945   pic_parameter_set_rbsp_t *active_pps;
946   seq_parameter_set_rbsp_t *active_sps;
947   struct sei_params        *p_SEI;
948   struct decoders          *p_decs;
949   CodingParameters         *p_CurrEncodePar;
950   CodingParameters         *p_EncodePar[MAX_NUM_DPB_LAYERS];
951 
952   int number;                  //!< current image number to be encoded (in first layer)
953   int LevelIndex;              //!< mapped level idc
954   int MaxVmvR[6];              //!< maximum vertical motion vector
955   int MaxHmvR[6];              //!< maximum horizontal motion vector
956   int current_mb_nr;
957   short current_slice_nr;
958   short type;
959   PictureStructure structure;  //!< picture structure
960   int base_dist;
961   int num_ref_frames;          //!< number of reference frames to be used
962   int max_num_references;      //!< maximum number of reference pictures that may occur
963   int masterQP;                //!< Master quantization parameter
964   int qp;                      //!< quant for the current frame
965   int qpsp;                    //!< quant for the prediction frame of SP-frame
966 
967   int prev_frame_no; // POC200301
968   int consecutive_non_reference_pictures; // POC200301
969 
970   // prediction structure
971   int frm_struct_buffer;      //!< length of the frame struct buffer (this may also be a define)
972   int last_idr_code_order;
973   int last_idr_disp_order;
974   int last_mmco_5_code_order; //!< it is a good idea to re-initialize POCs after such a frame: while seemingly not required it is critical for good implicit WP performance (I *THINK*) since POCs are set internally to zero after an MMCO=5 command.
975   int last_mmco_5_disp_order;
976   int curr_frm_idx;           //!< frame we wish to code in coding order (points also to p_frm)
977   int FrameNumOffset;         //!< POC type 1
978   int prevFrameNumOffset;     //!< POC type 1
979   unsigned int prevFrameNum;  //!< POC type 1
980   SeqStructure    *p_pred;
981   FrameUnitStruct *p_curr_frm_struct;
982   PicStructure    *p_curr_pic;
983   SliceStructure  *p_curr_slice;
984   int pic_struct;
985   struct search_window searchRange;
986 
987   //Hierarchical Motion Estimation structure
988   struct hme_info *pHMEInfo;  //!< HME information
989   int    is_hme;
990 
991   ImageData imgData;           //!< Image data to be encoded
992   ImageData imgData0;          //!< Input Image Data
993   ImageData imgData1;
994   ImageData imgData2;
995   ImageData imgData3;
996   // Data needed for 3:2 pulldown or temporal interleaving
997   ImageData imgData32;           //!< Image data to be encoded
998   ImageData imgData4;
999   ImageData imgData5;
1000   ImageData imgData6;
1001   ImageData imgRefData;
1002   ImageData origData0;
1003   ImageData origData1;
1004 
1005   ImageData imgDataOpt1;
1006   ImageData imgDataOpt2;
1007 
1008 
1009   int   num_slices_wp;
1010   int   wp_parameters_set;
1011   short ****wp_weights;   // explicit WP weights
1012   short ****wp_offsets;   // explicit WP offsets
1013   short *****wbp_weight;  // implicit WP weights
1014 
1015   struct image_structure imgSRC;
1016   struct image_structure imgREF;
1017   struct image_structure imgREF0;
1018   struct image_structure imgREF1;
1019   struct image_structure imgRGB_src;
1020   struct image_structure imgRGB_ref;
1021   int       **imgY_sub_tmp;           //!< Y picture temporary component (Quarter pel)
1022   imgpel    **imgY_com;               //!< Encoded luma images
1023   imgpel   **imgUV_com[2];              //!< Encoded croma images
1024 
1025   imgpel    **imgY_com_buf[2];               //!< Encoded luma images
1026   imgpel   **imgUV_com_buf[2][2];              //!< Encoded croma images
1027 
1028   imgpel    **pCurImg;                //!< Reference image. Luma for other profiles, can be any component for 4:4:4
1029   // global picture format dependend buffers, mem allocation in image.c
1030   imgpel    **pImgOrg[MAX_PLANE];
1031 
1032   imgpel    **pCurImgRef;
1033   imgpel    **pImgOrgRef[MAX_PLANE];
1034 
1035   Picture *p_frame_pic;
1036   Picture **frame_pic;
1037 #if (MVC_EXTENSION_ENABLE)
1038   Picture **field_pic_ptr;
1039   Picture **field_pic1;
1040   Picture **field_pic2;
1041 #else
1042   Picture **field_pic;
1043 #endif
1044 
1045   Picture *frame_pic_si;
1046 
1047   byte *MapUnitToSliceGroupMap;
1048 
1049   byte *buf;
1050   byte *ibuf;
1051 
1052 #ifdef _LEAKYBUCKET_
1053   long *Bit_Buffer;
1054   unsigned long total_frame_buffer;
1055 #endif
1056 
1057   unsigned int log2_max_frame_num_minus4;
1058   unsigned int log2_max_pic_order_cnt_lsb_minus4;
1059   unsigned int max_frame_num;
1060   unsigned int max_pic_order_cnt_lsb;
1061 
1062   int64  me_tot_time;
1063   int64  tot_time;
1064   int64  me_time;
1065 
1066   byte mixedModeEdgeFlag;
1067 
1068   int *RefreshPattern;
1069   int *IntraMBs;
1070   int WalkAround;
1071   int NumberOfMBs;
1072   int NumberIntraPerPicture;
1073 
1074   short start_me_refinement_hp; //!< if set then recheck the center position when doing half-pel motion refinement
1075   short start_me_refinement_qp; //!< if set then recheck the center position when doing quarter-pel motion refinement
1076 
1077   // Motion Estimation
1078   struct umhex_struct *p_UMHex;
1079   struct umhex_smp_struct *p_UMHexSMP;
1080   struct me_full_fast *p_ffast_me;
1081 
1082   struct search_window *p_search_window;
1083 
1084   // EPZS
1085   struct epzs_struct *sdiamond;
1086   struct epzs_struct *square;
1087   struct epzs_struct *ediamond;
1088   struct epzs_struct *ldiamond;
1089   struct epzs_struct *sbdiamond;
1090   struct epzs_struct *pmvfast;
1091   struct epzs_struct *search_rpu_square;
1092   struct epzs_struct *SP_M2DRPU;
1093   struct epzs_struct *SP_M2DRPU4MM;
1094 
1095   // RDOQ
1096   int precalcUnaryLevelTab[128][MAX_PREC_COEFF];
1097   int AdaptRndWeight;
1098   int AdaptRndCrWeight;
1099 
1100 
1101   //////////////////////////////////////////////////////////////////////////
1102   // B pictures
1103   // motion vector : forward, backward, direct
1104   byte  MBPairIsField;     //!< For MB level field/frame coding tools
1105 
1106   // Buffers for rd optimization with packet losses, Dim. Kontopodis
1107   byte **pixel_map;   //!< Shows the latest reference frame that is reliable for each pixel
1108   byte **refresh_map; //!< Stores the new values for pixel_map
1109   int intras;         //!< Counts the intra updates in each frame.
1110   int iInterViewMBs;
1111   int RCMinQP;
1112   int RCMaxQP;
1113 
1114   float framerate;
1115 
1116   int is_v_block;
1117   int mb_y_upd;
1118   int mb_y_intra;              //!< which GOB to intra code
1119   char **ipredmode;            //!< intra prediction mode
1120   char **ipredmode8x8;         //!< help storage for 8x8 modes, inserted by YV
1121   //fast intra prediction;
1122   char **ipredmode4x4_line;     //!< intra prediction mode
1123   char **ipredmode8x8_line;     //!< help storage for 8x8 modes, inserted by YV
1124 
1125   int cod_counter;             //!< Current count of number of skipped macroblocks in a row
1126   int ***nz_coeff;             //!< number of coefficients per block (CAVLC)
1127   int ***nz_coeff_buf[2];
1128   int pix_x;                   //!< current pixel horizontal
1129   int pix_y;                   //!< current pixel vertical
1130 
1131 
1132   imgpel min_IPCM_value;
1133   // Cabac related parameters (we can put these in a structure)
1134   int pic_bin_count;
1135 
1136   // Adaptive rounding
1137   int ****ARCofAdj4x4;         //!< Transform coefficients for 4x4 luma/chroma.
1138   int ****ARCofAdj8x8;         //!< Transform coefficients for 4x4 luma/chroma.
1139 
1140   ImageData tempData1, tempData2, tempData3, tempData4;
1141   imgpel **tempImg;
1142 
1143   Picture       *currentPicture; //!< The coded picture currently in the works (typically p_frame_pic, p_Vid->field_pic[0], or p_Vid->field_pic[1])
1144   struct slice  *currentSlice;                                //!< pointer to current Slice data struct
1145   Macroblock    *mb_data;                                   //!< array containing all MBs of a whole frame
1146   Block8x8Info  *b8x8info;                                  //!< block 8x8 information for RDopt
1147 
1148   //FAST_REFPIC_DECISION
1149   int           mb_refpic_used; //<! [2][16] for fast reference decision;
1150   int           parent_part_refpic_used; //<! [2][16] for fast reference decision;
1151   int           submb_parent_part_refpic_used;//<! [2][16] for fast reference decision;
1152   //end;
1153   short   *intra_block;
1154 
1155   int frame_no;
1156   int fld_type;                        //!< top or bottom field
1157   byte fld_flag;
1158   unsigned int rd_pass;
1159 
1160   int  redundant_coding;
1161   int  key_frame;
1162   int  redundant_ref_idx;
1163   int  frm_no_in_file;
1164 
1165   char DFDisableIdc;
1166   char DFAlphaC0Offset;
1167   char DFBetaOffset;
1168 
1169   char direct_spatial_mv_pred_flag;              //!< Direct Mode type to be used (0: Temporal, 1: Spatial)
1170 
1171   int shift_cr_x2;
1172 
1173   int num_ref_idx_l0_active;
1174   int num_ref_idx_l1_active;
1175 
1176   Boolean field_mode;     //!< For MB level field/frame -- field mode on flag
1177   Boolean top_field;      //!< For MB level field/frame -- top field flag
1178 
1179   int layer;              //!< which layer this picture belonged to
1180 
1181   int AdaptiveRounding;   //!< Adaptive Rounding parameter based on JVT-N011
1182 
1183   int redundant_pic_cnt;
1184 
1185   Boolean mb_aff_frame_flag;    //!< indicates frame with mb aff coding
1186 
1187   //the following should probably go in sequence parameters
1188   unsigned int pic_order_cnt_type;
1189 
1190   // for poc mode 1
1191   Boolean      delta_pic_order_always_zero_flag;
1192   int          offset_for_non_ref_pic;
1193   int          offset_for_top_to_bottom_field;
1194   unsigned int num_ref_frames_in_pic_order_cnt_cycle;
1195   int          offset_for_ref_frame[1];
1196 
1197   //the following is for slice header syntax elements of poc
1198   // for poc mode 0.
1199   unsigned int pic_order_cnt_lsb;
1200   int          delta_pic_order_cnt_bottom;
1201   // for poc mode 1.
1202   int          delta_pic_order_cnt[2];
1203 
1204   int          frm_iter;   //!< frame variations to create (useful for multiple coding passes)
1205 
1206   unsigned int field_picture;
1207   signed int toppoc;       //!< poc for this frame or field
1208   signed int bottompoc;    //!< for completeness - poc of bottom field of a frame (always = poc+1)
1209   signed int framepoc;     //!< min (toppoc, bottompoc)
1210   signed int ThisPOC;      //!< current picture POC
1211   unsigned int frame_num;    //!< frame_num for this frame
1212 
1213   unsigned int PicSizeInMbs;
1214 
1215   //the following should probably go in picture parameters
1216   Boolean bottom_field_pic_order_in_frame_present_flag; // ????????
1217 
1218   //the following are sent in the slice header
1219   //  int delta_pic_order_cnt[2];
1220   int nal_reference_idc;
1221 
1222   int     adaptive_ref_pic_buffering_flag;
1223   int     no_output_of_prior_pics_flag;
1224   Boolean long_term_reference_flag;
1225 
1226   DecRefPicMarking_t *dec_ref_pic_marking_buffer;
1227   int* mvbits;
1228 
1229   int     max_mvd;  //for MVD overflow checking;
1230   int*    refbits;
1231 
1232   // rate control variables
1233   int NumberofCodedMacroBlocks;
1234   int BasicUnitQP;
1235   int NumberofMBTextureBits;
1236   int NumberofMBHeaderBits;
1237   unsigned int BasicUnit;
1238   byte write_macroblock;
1239   byte bot_MB;
1240   int write_mbaff_frame;
1241 
1242   int last_pic_bottom_field;
1243   int last_has_mmco_5;
1244   int pre_frame_num;
1245 
1246   int slice_group_change_cycle;
1247 
1248   int max_qp_delta;
1249   int min_qp_delta;
1250 
1251   // Lagrangian Parameters
1252   LambdaParams **lambda;
1253   double  **lambda_md;     //!< Mode decision Lambda
1254   double ***lambda_me;     //!< Motion Estimation Lambda
1255   int    ***lambda_mf;     //!< Integer formatted Motion Estimation Lambda
1256   double  **lambda_rdoq;   //!< RDOQ Lambda
1257   double **lambda_mf_factor; //!< Motion Estimation Lamda Scale Factor
1258 
1259   LambdaParams **lambda_buf[2];
1260   double  **lambda_md_buf[2];     //!< Mode decision Lambda
1261   double ***lambda_me_buf[2];     //!< Motion Estimation Lambda
1262   int    ***lambda_mf_buf[2];     //!< Integer formatted Motion Estimation Lambda
1263   double  **lambda_rdoq_buf[2];   //!< RDOQ Lambda
1264   double **lambda_mf_factor_buf[2]; //!< Motion Estimation Lamda Scale Factor
1265 
1266   uint32 dc_pred_value;                 //!< DC prediction value for current component
1267   short max_imgpel_value;              //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
1268 
1269   struct edge_info *p_edge;
1270 
1271   int chroma_qp_offset[2];      //!< offset for qp for chroma [0-Cb, 1-Cr]
1272 
1273   int auto_crop_right;
1274   int auto_crop_bottom;
1275 
1276   short checkref;
1277   int last_valid_reference;
1278   int bytes_in_picture;
1279 
1280   int64 last_bit_ctr_n;
1281 
1282   int AverageFrameQP;
1283   int SumFrameQP;
1284 
1285   int ChromaArrayType;
1286   Macroblock    *mb_data_JV[MAX_PLANE];  //!< mb_data to be used during 4:4:4 independent mode encoding
1287   char colour_plane_id;    //!< colour_plane_id of the current coded slice (valid only when separate_colour_plane_flag is 1)
1288 
1289   int lastIntraNumber;
1290   int lastINTRA;
1291   int last_ref_idc;
1292   int idr_refresh;
1293 
1294   int p_dec;                      //!< decoded image file handle
1295 #if (MVC_EXTENSION_ENABLE)
1296   int p_dec2;                     //!< decoded image file handle for view 1
1297 #endif
1298   int frame_statistic_start;
1299   int initial_Bframes;
1300   int cabac_encoding;
1301 
1302   unsigned int primary_pic_type;
1303 
1304   int frameOffsetTotal[2][MAX_REFERENCE_PICTURES];
1305   int frameOffsetCount[2][MAX_REFERENCE_PICTURES];
1306   short frameOffset[2][MAX_REFERENCE_PICTURES];
1307   int frameOffsetAvail;
1308   int view_id;
1309 #if (MVC_EXTENSION_ENABLE)
1310   int sec_view_force_fld;    //!< if view 0 is coded as FLD then force also view to be coded as FLD as well
1311   int prev_view_is_anchor;
1312 
1313   int temp0_nal_reference_idc;
1314   int temp0_non_idr_flag[2];
1315   int temp0_priority_id;
1316   int temp0_view_id;
1317   int temp0_temporal_id;
1318   int temp0_anchor_pic_flag[2];
1319   int temp0_inter_view_flag[2];
1320 
1321   int non_idr_flag[2];
1322   int priority_id;
1323   int temporal_id;
1324   int anchor_pic_flag[2];
1325   int inter_view_flag[2];
1326 
1327   PrevCodingStats prev_cs;
1328   int MVCInterViewReorder;
1329   struct storable_picture *proc_picture;
1330 #endif
1331 
1332   double *mb16x16_cost_frame;
1333   double mb16x16_cost;
1334 
1335   int **lrec;
1336   int ***lrec_uv;
1337   Boolean sp2_frame_indicator;
1338   int number_sp2_frames;
1339 
1340   Boolean giRDOpt_B8OnlyFlag;
1341 
1342   int  frameNuminGOP;
1343   // Redundant picture
1344   imgpel **imgY_tmp;
1345   imgpel **imgUV_tmp[2];
1346   imgpel **imgY_tmp_buf[2];
1347   imgpel **imgUV_tmp_buf[2][2];
1348 
1349   // RDOQ related parameters
1350   //int Intra_Selected;
1351 
1352   int CbCr_predmode_8x8[4];
1353   distblk ****motion_cost;
1354   int*** initialized;
1355   int*** modelNumber;
1356   int    bipred_enabled[MAXMODE];
1357 
1358 
1359   int num_mb_per_slice;
1360   int number_of_slices;
1361 
1362   int  imgpel_abs_range;
1363 #if (JM_MEM_DISTORTION)
1364   int* imgpel_abs;
1365   int* imgpel_quad;
1366 #endif
1367 
1368   GOP_DATA *gop_structure;
1369   byte *MBAmap;
1370   unsigned int PicSizeInMapUnits;
1371   int FirstMBInSlice[MAXSLICEGROUPIDS];
1372 
1373   MotionVector* spiral_search;
1374   MotionVector* spiral_hpel_search;
1375   MotionVector* spiral_qpel_search;
1376 
1377   // files
1378   FILE *p_log;                     //!< SNR file
1379 
1380   // generic output file
1381   FILE **f_out;
1382   // ANNEX B output file
1383   FILE *f_annexb;
1384   // RTP output file
1385   FILE *f_rtp;
1386   int CurrentRTPTimestamp;             //!< The RTP timestamp of the current packet,
1387   //! incremented with all P and I frames
1388   uint16 CurrentRTPSequenceNumber;     //!< The RTP sequence number of the current packet
1389   //!< incremented by one for each sent packet
1390 
1391   // This should be the right location for this
1392   //struct storable_picture **listX[6];
1393   //char listXsize[6];
1394 
1395   DistortionParams *p_Dist;
1396   struct stat_parameters  *p_Stats;
1397   pic_parameter_set_rbsp_t *PicParSet[MAXPPS];
1398   //struct decoded_picture_buffer *p_Dpb;
1399   struct decoded_picture_buffer *p_Dpb_layer[MAX_NUM_DPB_LAYERS];
1400   struct frame_store            *out_buffer;
1401   struct storable_picture       *enc_picture;
1402   struct storable_picture       **enc_frame_picture;
1403   struct storable_picture       **enc_field_picture;
1404   struct storable_picture       *enc_frame_picture_JV[MAX_PLANE];  //!< enc_frame to be used during 4:4:4 independent mode encoding
1405   struct quant_params           *p_Quant;
1406   struct scaling_list           *p_QScale;
1407 
1408   // rate control
1409   RCGeneric   *p_rc_gen;
1410   RCGeneric   *p_rc_gen_init, *p_rc_gen_best;
1411   RCQuadratic *p_rc_quad;
1412   RCQuadratic *p_rc_quad_init, *p_rc_quad_best;
1413 
1414   double entropy[128];
1415   double enorm  [128];
1416   double probability[128];
1417 
1418 
1419   FILE       *expSFile;
1420   struct exp_seq_info *expSeq;
1421   // Weighted prediction
1422   struct wpx_object   *pWPX;
1423 
1424   // Wavelet PSNR
1425   int ***wavREF[NUM_WAVELET_LEVEL+1];
1426   int ***wavSRC[NUM_WAVELET_LEVEL+1];
1427   double JND[NUM_WAVELET_LEVEL][4];
1428   int *temp_low, *temp_high;
1429 
1430 #ifdef BEST_NZ_COEFF
1431   int gaaiMBAFF_NZCoeff[4][12];
1432 #endif
1433 
1434 
1435   int offset_y, offset_cr;
1436   int wka0, wka1, wka2, wka3, wka4;
1437   int EvaluateDBOff;
1438   int TurnDBOff;
1439   // Note that these function pointers definitely affect now parallelization since they are only
1440   // allocated once. We need to add such info at maybe within picture information or at a lower level
1441   // RC
1442   int  (*updateQP)                (struct video_par *p_Vid, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield);
1443   void (*rc_update_pict_frame_ptr)(struct video_par *p_Vid, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int nbits);
1444   void (*rc_update_picture_ptr)   (struct video_par *p_Vid, InputParameters *p_Inp, int bits);
1445   void (*rc_init_pict_ptr)        (struct video_par *p_Vid, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int fieldpic, int topfield, int targetcomputation, float mult);
1446 
1447   //Various
1448   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);
1449   void (*getNeighbour)         (Macroblock *currMB, int xN, int yN, int mb_size[2], PixelPos *pix);
1450   void (*get_mb_block_pos)     (BlockPos *PicPos, int mb_addr, short *x, short *y);
1451   int  (*WriteNALU)            (struct video_par *p_Vid, NALU_t *n, FILE **f_out);     //! Hides the write function in Annex B or RTP
1452   void (*error_conceal_picture)(struct video_par *p_Vid, struct storable_picture *enc_pic, int decoder);
1453   distblk (*estimate_distortion)(Macroblock *currMB, int block, int block_size, short mode, short pdir, distblk min_rdcost);
1454   // function pointer for different ways of obtaining chroma interpolation
1455   void (*OneComponentChromaPrediction4x4)   (Macroblock *currMB, imgpel* , int , int , MotionVector ** , struct storable_picture *listX, int );
1456 
1457   // deblocking
1458   void (*GetStrengthVer)    (byte Strength[16], Macroblock *MbQ, int edge, int mvlimit);
1459   void (*GetStrengthHor)    (byte Strength[16], Macroblock *MbQ, int edge, int mvlimit);
1460   void (*EdgeLoopLumaHor)   (ColorPlane pl, imgpel** Img, byte Strength[16], Macroblock *MbQ, int edge, int width);
1461   void (*EdgeLoopLumaVer)   (ColorPlane pl, imgpel** Img, byte Strength[16], Macroblock *MbQ, int edge);
1462   void (*EdgeLoopChromaVer)(imgpel** Img, byte Strength[16], Macroblock *MbQ, int edge, int uv);
1463   void (*EdgeLoopChromaHor)(imgpel** Img, byte Strength[16], Macroblock *MbQ, int edge, int width, int uv);
1464 
1465   // We should move these at the slice level at some point.
1466   void (*EstimateWPBSlice) (struct slice *currSlice);
1467   void (*EstimateWPPSlice) (struct slice *currSlice, int offset);
1468   int  (*TestWPPSlice)     (struct slice *currSlice, int offset);
1469   int  (*TestWPBSlice)     (struct slice *currSlice, int method);
1470   distblk  (*distortion4x4)(short*, distblk);
1471   distblk  (*distortion8x8)(short*, distblk);
1472 
1473   // ME distortion Function pointers. We need to move this to the MB or slice level
1474   distblk (*computeUniPred[6])   (struct storable_picture *ref1, struct me_block *, distblk , MotionVector * );
1475   distblk (*computeBiPred1[3])   (struct storable_picture *ref1, struct storable_picture *ref2, struct me_block*, distblk , MotionVector *, MotionVector *);
1476   distblk (*computeBiPred2[3])   (struct storable_picture *ref1, struct storable_picture *ref2, struct me_block*, distblk , MotionVector *, MotionVector *);
1477 
1478   seq_parameter_set_rbsp_t *sps[MAX_NUM_DPB_LAYERS];
1479   FrameFormat output_format[MAX_NUM_DPB_LAYERS];
1480   int num_of_layers;
1481   int dpb_layer_id;
1482 
1483   BlockPos *PicPos;
1484 
1485   //deprecative varialbes, they will be removed in future, so donot add varaibles here;
1486   ColorFormat yuv_format;
1487   int P444_joined;
1488   short bitdepth_luma;
1489   short bitdepth_chroma;
1490   int bitdepth_scale[2];
1491   int bitdepth_luma_qp_scale;
1492   int bitdepth_chroma_qp_scale;
1493   int bitdepth_lambda_scale;
1494   uint32 dc_pred_value_comp[MAX_PLANE]; //!< component value for DC prediction (depends on component pel bit depth)
1495   int max_pel_value_comp      [MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
1496   int max_imgpel_value_comp_sq   [MAX_PLANE];       //!< max value that one picture element (pixel) can take (depends on pic_unit_bitdepth)
1497   int num_blk8x8_uv;
1498   int num_cdc_coeff;
1499   short mb_cr_size_x;
1500   short mb_cr_size_y;
1501   int mb_size[MAX_PLANE][2];
1502   int max_bitCount;
1503   int width;                   //!< Number of pels
1504   int width_padded;            //!< Width in pels of padded picture
1505   int width_blk;               //!< Number of columns in blocks
1506   int width_cr;                //!< Number of pels chroma
1507   int height;                  //!< Number of lines
1508   int height_padded;           //!< Number in lines of padded picture
1509   int height_blk;              //!< Number of lines in blocks
1510   int height_cr;               //!< Number of lines  chroma
1511   int height_cr_frame;         //!< Number of lines  chroma frame
1512   int size;                    //!< Luma Picture size in pels
1513   int size_cr;                 //!< Chroma Picture size in pels
1514   int padded_size_x;
1515   int padded_size_x_m8x8;
1516   int padded_size_x_m4x4;
1517   int cr_padded_size_x;
1518   int cr_padded_size_x_m8;
1519   int cr_padded_size_x2;
1520   int cr_padded_size_x4;
1521   int pad_size_uv_x;
1522   int pad_size_uv_y;
1523   unsigned char chroma_mask_mv_y;
1524   unsigned char chroma_mask_mv_x;
1525   int chroma_shift_y, chroma_shift_x;
1526   int shift_cr_x, shift_cr_y;
1527 
1528   unsigned int PicWidthInMbs;
1529   unsigned int PicHeightInMapUnits;
1530   unsigned int FrameHeightInMbs;
1531   unsigned int FrameSizeInMbs;
1532   //end;
1533 } VideoParameters;
1534 
1535 
1536 typedef struct scaling_list      ScaleParameters;
1537 typedef struct rdo_structure     RDOPTStructure;
1538 
1539 typedef struct encoder_params
1540 {
1541   InputParameters   *p_Inp;          //!< Input Parameters
1542   VideoParameters   *p_Vid;          //!< Image Parameters
1543   FILE              *p_trace;        //!< Trace file
1544   int64              bufferSize;     //!< buffer size for tiff reads (not currently supported)
1545 } EncoderParams;
1546 
1547 extern EncoderParams  *p_Enc;
1548 
1549 /***********************************************************************
1550  * P r o t o t y p e s   f o r    T M L
1551  ***********************************************************************
1552  */
1553 // Transform function pointers
1554 
1555 extern void copyblock_sp    (Macroblock *currMB, ColorPlane pl, int pos_mb1,int pos_mb2);
1556 extern int  residual_transform_quant_chroma_4x4      (Macroblock *currMB, int uv, int cr_cbp);
1557 extern int  residual_transform_quant_chroma_4x4_sp   (Macroblock *currMB, int uv,int i11);
1558 extern int  residual_transform_quant_chroma_4x4_sp2  (Macroblock *currMB, int uv,int i11);
1559 
1560 
1561 extern distblk   GetDirectCostMB           (Macroblock *currMB);
1562 extern distblk   GetDirectCost8x8          (Macroblock *currMB, int, distblk*);
1563 
1564 extern int   picture_coding_decision   (VideoParameters *p_Vid, Picture *picture1, Picture *picture2, int qp);
1565 
1566 extern void no_mem_exit  (char *where);
1567 extern int  get_mem_ACcoeff_new  (int****** cofAC, int chroma);
1568 extern int  get_mem_ACcoeff      (VideoParameters *p_Vid, int*****);
1569 extern int  get_mem_DCcoeff      (int****);
1570 extern void free_mem_ACcoeff     (int****);
1571 extern void free_mem_ACcoeff_new (int***** cofAC);
1572 extern void free_mem_DCcoeff     (int***);
1573 
1574 #if TRACE
1575 extern void  trace2out(SyntaxElement *se);
1576 extern void  trace2out_cabac(SyntaxElement *se);
1577 #endif
1578 
1579 extern void error(char *text, int code);
1580 extern void set_mbaff_parameters(Macroblock  *currMB);
1581 
1582 //============= restriction of reference frames based on the latest intra-refreshes==========
1583 extern int  is_bipred_enabled          (VideoParameters *p_Vid, int mode);
1584 extern void update_qp                  (Macroblock *currMB);
1585 extern void select_plane               (VideoParameters *p_Vid, ColorPlane color_plane);
1586 extern void select_transform           (Macroblock *currMB);
1587 extern void set_slice_type             (VideoParameters *p_Vid, InputParameters *p_Inp, int slice_type);
1588 extern void free_encoder_memory        (VideoParameters *p_Vid, InputParameters *p_Inp);
1589 extern void output_SP_coefficients     (VideoParameters *p_Vid, InputParameters *p_Inp);
1590 extern void read_SP_coefficients       (VideoParameters *p_Vid, InputParameters *p_Inp);
1591 extern void init_redundant_frame       (VideoParameters *p_Vid, InputParameters *p_Inp);
1592 extern void set_redundant_frame        (VideoParameters *p_Vid, InputParameters *p_Inp);
1593 extern void encode_one_redundant_frame (VideoParameters *p_Vid, InputParameters *p_Inp);
1594 
1595 
1596 // struct with pointers to the sub-images
1597 typedef struct
1598 {
1599   imgpel ****luma;    //!< component 0 (usually Y, X, or R)
1600   imgpel ****crcb[2]; //!< component 2 (usually U/V, Y/Z, or G/B)
1601 } SubImageContainer;
1602 
1603 // For 4:4:4 independent mode
1604 extern void change_plane_JV      ( VideoParameters *p_Vid, int nplane );
1605 extern void make_frame_picture_JV( VideoParameters *p_Vid );
1606 
1607 
1608 
1609 extern char errortext[ET_SIZE]; //!< buffer for error message for exit with error()
1610 extern void setup_coding_layer(VideoParameters *p_Vid);
1611 
is_FREXT_profile(unsigned int profile_idc)1612 static inline int is_FREXT_profile(unsigned int profile_idc)
1613 {
1614   // we allow all FRExt tools, when no profile is active
1615   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 );
1616 }
1617 
1618 
is_MVC_profile(unsigned int profile_idc)1619 static inline int is_MVC_profile(unsigned int profile_idc)
1620 {
1621   return ( (0)
1622 #if (MVC_EXTENSION_ENABLE)
1623   || (profile_idc == MULTIVIEW_HIGH) || (profile_idc == STEREO_HIGH)
1624 #endif
1625     );
1626 }
1627 
is_intra(Macroblock * curr_MB)1628 static inline int is_intra(Macroblock *curr_MB)
1629 {
1630   return ((curr_MB)->mb_type==SI4MB || (curr_MB)->mb_type==I4MB || (curr_MB)->mb_type==I16MB || (curr_MB)->mb_type==I8MB || (curr_MB)->mb_type==IPCM);
1631 }
1632 
1633 
1634 #endif
1635 
1636