Home | History | Annotate | Download | only in include
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /**
     19 This file contains common code shared between AVC decoder and AVC encoder for
     20 internal use only.
     21 @publishedAll
     22 */
     23 
     24 #ifndef AVCINT_COMMON_H_INCLUDED
     25 #define AVCINT_COMMON_H_INCLUDED
     26 
     27 #ifndef AVCAPI_COMMON_H_INCLUDED
     28 #include "avcapi_common.h"
     29 #endif
     30 
     31 
     32 #ifndef TRUE
     33 #define TRUE  1
     34 #define FALSE 0
     35 #endif
     36 
     37 
     38 
     39 /**
     40 Mathematic functions defined in subclause 5.7.
     41 Can be replaced with assembly instructions for speedup.
     42 @publishedAll
     43 */
     44 #define AVC_ABS(x)   (((x)<0)? -(x) : (x))
     45 #define AVC_SIGN(x)  (((x)<0)? -1 : 1)
     46 #define AVC_SIGN0(x) (((x)<0)? -1 : (((x)>0) ? 1 : 0))
     47 #define AVC_MAX(x,y) ((x)>(y)? (x):(y))
     48 #define AVC_MIN(x,y) ((x)<(y)? (x):(y))
     49 #define AVC_MEDIAN(A,B,C) ((A) > (B) ? ((A) < (C) ? (A) : (B) > (C) ? (B) : (C)): (B) < (C) ? (B) : (C) > (A) ? (C) : (A))
     50 #define AVC_CLIP3(a,b,x) (AVC_MAX(a,AVC_MIN(x,b)))  /* clip x between a and b */
     51 #define AVC_CLIP(x)  AVC_CLIP3(0,255,x)
     52 #define AVC_FLOOR(x) ((int)(x))
     53 #define AVC_RASTER_SCAN(x,y,n)  ((x)+(y)*(n))
     54 #define AVC_ROUND(x) (AVC_SIGN(x)*AVC_FLOOR(AVC_ABS(x)+0.5))
     55 #define AVC_INVERSE_RASTER_SCAN(a,b,c,d,e) (((e)==0)? (((a)%((d)/(b)))*(b)): (((a)/((d)/(b)))*(c)))
     56 /* a:block address, b:block width, c:block height, d:total_width, e:x or y coordinate */
     57 
     58 #define DEFAULT_ATTR  0  /* default memory attribute  */
     59 #define FAST_MEM_ATTR 1  /* fast memory attribute */
     60 
     61 
     62 /* This section is for definition of constants. */
     63 #define MB_SIZE 16
     64 #define BLOCK_SIZE 4
     65 #define EMULATION_PREVENTION_THREE_BYTE 0x3
     66 #define NUM_PIXELS_IN_MB  (24*16)
     67 #define NUM_BLKS_IN_MB 24
     68 
     69 #define AVCNumI4PredMode  9
     70 #define AVCNumI16PredMode  4
     71 #define AVCNumIChromaMode  4
     72 
     73 /* constants used in the structures below */
     74 #define MAXIMUMVALUEOFcpb_cnt   32  /* used in HRDParams */
     75 #define MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE 255   /* used in SeqParamSet */
     76 #define MAX_NUM_SLICE_GROUP  8      /* used in PicParamSet */
     77 #define MAX_REF_PIC_LIST_REORDERING 32  /* 32 is maximum according to Annex A, SliceHeader */
     78 #define MAX_DEC_REF_PIC_MARKING 64   /* 64 is the maximum possible given the max num ref pictures to 31. */
     79 #define MAX_FS (16+1)  /* pre-defined size of frame store array */
     80 #define MAX_LEVEL_IDX  15  /* only 15 levels defined for now */
     81 #define MAX_REF_PIC_LIST 33 /* max size of the RefPicList0 and RefPicList1 */
     82 
     83 
     84 /**
     85 Architectural related macros.
     86 @publishedAll
     87 */
     88 #ifdef USE_PRED_BLOCK
     89 #define MB_BASED_DEBLOCK
     90 #endif
     91 
     92 /**
     93 Picture type, PV created.
     94 @publishedAll
     95 */
     96 typedef enum
     97 {
     98     AVC_FRAME = 3
     99 } AVCPictureType;
    100 
    101 /**
    102 This slice type follows Table 7-3. The bottom 5 items may not needed.
    103 @publishedAll
    104 */
    105 typedef enum
    106 {
    107     AVC_P_SLICE = 0,
    108     AVC_B_SLICE = 1,
    109     AVC_I_SLICE = 2,
    110     AVC_SP_SLICE = 3,
    111     AVC_SI_SLICE = 4,
    112     AVC_P_ALL_SLICE = 5,
    113     AVC_B_ALL_SLICE = 6,
    114     AVC_I_ALL_SLICE = 7,
    115     AVC_SP_ALL_SLICE = 8,
    116     AVC_SI_ALL_SLICE = 9
    117 } AVCSliceType;
    118 
    119 /**
    120 Types of the macroblock and partition. PV Created.
    121 @publishedAll
    122 */
    123 typedef enum
    124 {
    125     /* intra */
    126     AVC_I4,
    127     AVC_I16,
    128     AVC_I_PCM,
    129     AVC_SI4,
    130 
    131     /* inter for both P and B*/
    132     AVC_BDirect16,
    133     AVC_P16,
    134     AVC_P16x8,
    135     AVC_P8x16,
    136     AVC_P8,
    137     AVC_P8ref0,
    138     AVC_SKIP
    139 } AVCMBMode;
    140 
    141 /**
    142 Enumeration for sub-macroblock mode, interpreted from sub_mb_type.
    143 @publishedAll
    144 */
    145 typedef enum
    146 {
    147     /* for sub-partition mode */
    148     AVC_BDirect8,
    149     AVC_8x8,
    150     AVC_8x4,
    151     AVC_4x8,
    152     AVC_4x4
    153 } AVCSubMBMode;
    154 
    155 /**
    156 Mode of prediction of partition or sub-partition. PV Created.
    157 Do not change the order!!! Used in table look-up mode prediction in
    158 vlc.c.
    159 @publishedAll
    160 */
    161 typedef enum
    162 {
    163     AVC_Pred_L0 = 0,
    164     AVC_Pred_L1,
    165     AVC_BiPred,
    166     AVC_Direct
    167 } AVCPredMode;
    168 
    169 
    170 /**
    171 Mode of intra 4x4 prediction. Table 8-2
    172 @publishedAll
    173 */
    174 typedef enum
    175 {
    176     AVC_I4_Vertical = 0,
    177     AVC_I4_Horizontal,
    178     AVC_I4_DC,
    179     AVC_I4_Diagonal_Down_Left,
    180     AVC_I4_Diagonal_Down_Right,
    181     AVC_I4_Vertical_Right,
    182     AVC_I4_Horizontal_Down,
    183     AVC_I4_Vertical_Left,
    184     AVC_I4_Horizontal_Up
    185 } AVCIntra4x4PredMode;
    186 
    187 /**
    188 Mode of intra 16x16 prediction. Table 8-3
    189 @publishedAll
    190 */
    191 typedef enum
    192 {
    193     AVC_I16_Vertical = 0,
    194     AVC_I16_Horizontal,
    195     AVC_I16_DC,
    196     AVC_I16_Plane
    197 } AVCIntra16x16PredMode;
    198 
    199 
    200 /**
    201 Mode of intra chroma prediction. Table 8-4
    202 @publishedAll
    203 */
    204 typedef enum
    205 {
    206     AVC_IC_DC = 0,
    207     AVC_IC_Horizontal,
    208     AVC_IC_Vertical,
    209     AVC_IC_Plane
    210 } AVCIntraChromaPredMode;
    211 
    212 /**
    213 Type of residual going to residual_block_cavlc function, PV created.
    214 @publishedAll
    215 */
    216 typedef enum
    217 {
    218     AVC_Luma,
    219     AVC_Intra16DC,
    220     AVC_Intra16AC,
    221     AVC_ChromaDC,
    222     AVC_ChromaAC
    223 } AVCResidualType;
    224 
    225 
    226 /**
    227 This structure contains VUI parameters as specified in Annex E.
    228 Some variables may be removed from the structure if they are found to be useless to store.
    229 @publishedAll
    230 */
    231 typedef struct tagHRDParams
    232 {
    233     uint  cpb_cnt_minus1;                                   /* ue(v), range 0..31 */
    234     uint  bit_rate_scale;                          /* u(4) */
    235     uint  cpb_size_scale;                          /* u(4) */
    236     uint32  bit_rate_value_minus1[MAXIMUMVALUEOFcpb_cnt];/* ue(v), range 0..2^32-2 */
    237     uint32  cpb_size_value_minus1[MAXIMUMVALUEOFcpb_cnt]; /* ue(v), range 0..2^32-2 */
    238     uint  cbr_flag[MAXIMUMVALUEOFcpb_cnt];         /* u(1) */
    239     uint  initial_cpb_removal_delay_length_minus1;   /* u(5), default 23 */
    240     uint  cpb_removal_delay_length_minus1;           /* u(5), default 23 */
    241     uint  dpb_output_delay_length_minus1;            /* u(5), default 23 */
    242     uint  time_offset_length;                        /* u(5), default 24 */
    243 } AVCHRDParams;
    244 
    245 /**
    246 This structure contains VUI parameters as specified in Annex E.
    247 Some variables may be removed from the structure if they are found to be useless to store.
    248 @publishedAll
    249 */
    250 typedef struct tagVUIParam
    251 {
    252     uint      aspect_ratio_info_present_flag;     /* u(1) */
    253     uint  aspect_ratio_idc;                     /* u(8), table E-1 */
    254     uint  sar_width;                          /* u(16) */
    255     uint  sar_height;                         /* u(16) */
    256     uint      overscan_info_present_flag;         /* u(1) */
    257     uint      overscan_appropriate_flag;        /* u(1) */
    258     uint      video_signal_type_present_flag;     /* u(1) */
    259     uint  video_format;                         /* u(3), Table E-2, default 5, unspecified */
    260     uint      video_full_range_flag;            /* u(1) */
    261     uint      colour_description_present_flag;  /* u(1) */
    262     uint  colour_primaries;                   /* u(8), Table E-3, default 2, unspecified */
    263     uint  transfer_characteristics;           /* u(8), Table E-4, default 2, unspecified */
    264     uint  matrix_coefficients;                /* u(8), Table E-5, default 2, unspecified */
    265     uint      chroma_location_info_present_flag;  /* u(1) */
    266     uint  chroma_sample_loc_type_top_field;                /* ue(v), Fig. E-1range 0..5, default 0 */
    267     uint  chroma_sample_loc_type_bottom_field;                /* ue(v) */
    268     uint      timing_info_present_flag;           /* u(1) */
    269     uint  num_units_in_tick;                    /* u(32), must be > 0 */
    270     uint  time_scale;                           /* u(32), must be > 0 */
    271     uint      fixed_frame_rate_flag;            /* u(1), Eq. C-13 */
    272     uint      nal_hrd_parameters_present_flag;    /* u(1) */
    273     AVCHRDParams nal_hrd_parameters;               /* hrd_paramters */
    274     uint      vcl_hrd_parameters_present_flag;    /* u(1) */
    275     AVCHRDParams vcl_hrd_parameters;               /* hrd_paramters */
    276     /* if ((nal_hrd_parameters_present_flag || (vcl_hrd_parameters_present_flag)) */
    277     uint      low_delay_hrd_flag;               /* u(1) */
    278     uint    pic_struct_present_flag;
    279     uint      bitstream_restriction_flag;         /* u(1) */
    280     uint      motion_vectors_over_pic_boundaries_flag;    /* u(1) */
    281     uint  max_bytes_per_pic_denom;              /* ue(v), default 2 */
    282     uint  max_bits_per_mb_denom;                /* ue(v), range 0..16, default 1 */
    283     uint  log2_max_mv_length_vertical;          /* ue(v), range 0..16, default 16 */
    284     uint  log2_max_mv_length_horizontal;        /* ue(v), range 0..16, default 16 */
    285     uint  max_dec_frame_reordering;             /* ue(v) */
    286     uint  max_dec_frame_buffering;              /* ue(v) */
    287 } AVCVUIParams;
    288 
    289 
    290 /**
    291 This structure contains information in a sequence parameter set NAL.
    292 Some variables may be removed from the structure if they are found to be useless to store.
    293 @publishedAll
    294 */
    295 typedef struct tagSeqParamSet
    296 {
    297     uint   Valid;            /* indicates the parameter set is valid */
    298 
    299     uint  profile_idc;              /* u(8) */
    300     uint   constrained_set0_flag;  /* u(1) */
    301     uint   constrained_set1_flag;  /* u(1) */
    302     uint   constrained_set2_flag;  /* u(1) */
    303     uint   constrained_set3_flag;  /* u(1) */
    304     uint  level_idc;               /* u(8) */
    305     uint  seq_parameter_set_id;    /* ue(v), range 0..31 */
    306     uint  log2_max_frame_num_minus4; /* ue(v), range 0..12 */
    307     uint pic_order_cnt_type;        /* ue(v), range 0..2 */
    308     /* if( pic_order_cnt_type == 0 )  */
    309     uint log2_max_pic_order_cnt_lsb_minus4; /* ue(v), range 0..12 */
    310     /* else if( pic_order_cnt_type == 1 ) */
    311     uint delta_pic_order_always_zero_flag;  /* u(1) */
    312     int32  offset_for_non_ref_pic;       /* se(v) */
    313     int32  offset_for_top_to_bottom_field;  /* se(v) */
    314     uint  num_ref_frames_in_pic_order_cnt_cycle;   /* ue(v) , range 0..255 */
    315     /* for( i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ ) */
    316     int32   offset_for_ref_frame[MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE];        /* se(v) */
    317     uint  num_ref_frames;                           /* ue(v), range 0..16 */
    318     uint   gaps_in_frame_num_value_allowed_flag;    /* u(1) */
    319     uint  pic_width_in_mbs_minus1;                  /* ue(v) */
    320     uint  pic_height_in_map_units_minus1;           /* ue(v) */
    321     uint   frame_mbs_only_flag;                     /* u(1) */
    322     /* if( !frame_mbs_only_flag ) */
    323     uint   mb_adaptive_frame_field_flag;          /* u(1) */
    324     uint   direct_8x8_inference_flag;    /* u(1), must be 1 when frame_mbs_only_flag is 0 */
    325     uint   frame_cropping_flag;                     /* u(1) */
    326     /* if( frmae_cropping_flag) */
    327     uint  frame_crop_left_offset;                /* ue(v) */
    328     uint  frame_crop_right_offset;               /* ue(v) */
    329     uint  frame_crop_top_offset;                 /* ue(v) */
    330     uint  frame_crop_bottom_offset;              /* ue(v) */
    331     uint   vui_parameters_present_flag;                      /* u(1) */
    332 //  uint nal_hrd_parameters_present_flag;
    333 //  uint vcl_hrd_parameters_present_flag;
    334 //  AVCHRDParams *nal_hrd_parameters;
    335 //  AVCHRDParams *vcl_hrd_parameters;
    336     AVCVUIParams vui_parameters;                  /* AVCVUIParam */
    337 } AVCSeqParamSet;
    338 
    339 /**
    340 This structure contains information in a picture parameter set NAL.
    341 Some variables may be removed from the structure if they are found to be useless to store.
    342 @publishedAll
    343 */
    344 typedef struct tagPicParamSet
    345 {
    346     uint  pic_parameter_set_id;              /* ue(v), range 0..255 */
    347     uint  seq_parameter_set_id;              /* ue(v), range 0..31 */
    348     uint  entropy_coding_mode_flag;         /* u(1) */
    349     uint  pic_order_present_flag;        /* u(1) */
    350     uint  num_slice_groups_minus1;           /* ue(v), range in Annex A */
    351     /* if( num_slice_groups_minus1 > 0) */
    352     uint  slice_group_map_type;           /* ue(v), range 0..6 */
    353     /* if( slice_group_map_type = = 0 ) */
    354     /* for(0:1:num_slice_groups_minus1) */
    355     uint  run_length_minus1[MAX_NUM_SLICE_GROUP]; /* ue(v) */
    356     /* else if( slice_group_map_type = = 2 ) */
    357     /* for(0:1:num_slice_groups_minus1-1) */
    358     uint  top_left[MAX_NUM_SLICE_GROUP-1];      /* ue(v) */
    359     uint  bottom_right[MAX_NUM_SLICE_GROUP-1];  /* ue(v) */
    360     /* else if( slice_group_map_type = = 3 || 4 || 5 */
    361     uint  slice_group_change_direction_flag;        /* u(1) */
    362     uint  slice_group_change_rate_minus1;            /* ue(v) */
    363     /* else if( slice_group_map_type = = 6 ) */
    364     uint  pic_size_in_map_units_minus1;          /* ue(v) */
    365     /* for(0:1:pic_size_in_map_units_minus1) */
    366     uint  *slice_group_id;                           /* complete MBAmap u(v) */
    367     uint  num_ref_idx_l0_active_minus1;                  /* ue(v), range 0..31 */
    368     uint  num_ref_idx_l1_active_minus1;                  /* ue(v), range 0..31 */
    369     uint  weighted_pred_flag;                           /* u(1) */
    370     uint  weighted_bipred_idc;                          /* u(2), range 0..2 */
    371     int   pic_init_qp_minus26;                       /* se(v), range -26..25 */
    372     int   pic_init_qs_minus26;                       /* se(v), range -26..25 */
    373     int   chroma_qp_index_offset;                    /* se(v), range -12..12 */
    374     uint  deblocking_filter_control_present_flag;       /* u(1) */
    375     uint  constrained_intra_pred_flag;                  /* u(1) */
    376     uint  redundant_pic_cnt_present_flag;               /* u(1) */
    377 } AVCPicParamSet;
    378 
    379 
    380 /**
    381 This structure contains slice header information.
    382 Some variables may be removed from the structure if they are found to be useless to store.
    383 @publishedAll
    384 */
    385 typedef struct tagSliceHeader
    386 {
    387     uint    first_mb_in_slice;      /* ue(v) */
    388     AVCSliceType slice_type;                /* ue(v), Table 7-3, range 0..9 */
    389     uint    pic_parameter_set_id;   /* ue(v), range 0..255 */
    390     uint    frame_num;              /* u(v), see log2max_frame_num_minus4 */
    391     /* if( !frame_mbs_only_flag) */
    392     uint    field_pic_flag;         /* u(1) */
    393     /* if(field_pic_flag) */
    394     uint bottom_field_flag; /* u(1) */
    395     /* if(nal_unit_type == 5) */
    396     uint    idr_pic_id;         /* ue(v), range 0..65535 */
    397     /* if(pic_order_cnt_type==0) */
    398     uint    pic_order_cnt_lsb;  /* u(v), range 0..MaxPicOrderCntLsb-1 */
    399     /* if(pic_order_present_flag && !field_pic_flag) */
    400     int32 delta_pic_order_cnt_bottom;   /* se(v) */
    401     /* if(pic_order_cnt_type==1 && !delta_pic_order_always_zero_flag) */
    402     /* if(pic_order_present_flag && !field_pic_flag) */
    403     int32 delta_pic_order_cnt[2];
    404     /* if(redundant_pic_cnt_present_flag) */
    405     uint redundant_pic_cnt; /* ue(v), range 0..127 */
    406     /* if(slice_type == B) */
    407     uint direct_spatial_mv_pred_flag; /* u(1) */
    408     /* if(slice_type == P || slice_type==SP || slice_type==B) */
    409     uint num_ref_idx_active_override_flag;  /* u(1) */
    410     /* if(num_ref_idx_active_override_flag) */
    411     uint num_ref_idx_l0_active_minus1;  /* ue(v) */
    412     /* if(slie_type == B) */
    413     uint num_ref_idx_l1_active_minus1;  /* ue(v) */
    414 
    415     /* ref_pic_list_reordering() */
    416     uint ref_pic_list_reordering_flag_l0;   /* u(1) */
    417     uint reordering_of_pic_nums_idc_l0[MAX_REF_PIC_LIST_REORDERING];   /* ue(v), range 0..3 */
    418     uint abs_diff_pic_num_minus1_l0[MAX_REF_PIC_LIST_REORDERING];   /* ue(v) */
    419     uint long_term_pic_num_l0[MAX_REF_PIC_LIST_REORDERING];     /* ue(v) */
    420     uint ref_pic_list_reordering_flag_l1;   /* u(1) */
    421     uint reordering_of_pic_nums_idc_l1[MAX_REF_PIC_LIST_REORDERING];   /* ue(v), range 0..3 */
    422     uint abs_diff_pic_num_minus1_l1[MAX_REF_PIC_LIST_REORDERING];   /* ue(v) */
    423     uint long_term_pic_num_l1[MAX_REF_PIC_LIST_REORDERING];     /* ue(v) */
    424 
    425     /* end ref_pic_list_reordering() */
    426     /* if(nal_ref_idc!=0) */
    427     /* dec_ref_pic_marking() */
    428     uint    no_output_of_prior_pics_flag;   /* u(1) */
    429     uint long_term_reference_flag;      /* u(1) */
    430     uint    adaptive_ref_pic_marking_mode_flag; /* u(1) */
    431     uint    memory_management_control_operation[MAX_DEC_REF_PIC_MARKING];   /* ue(v), range 0..6 */
    432     uint difference_of_pic_nums_minus1[MAX_DEC_REF_PIC_MARKING];    /* ue(v) */
    433     uint    long_term_pic_num[MAX_DEC_REF_PIC_MARKING];             /* ue(v) */
    434     uint    long_term_frame_idx[MAX_DEC_REF_PIC_MARKING];           /* ue(v) */
    435     uint    max_long_term_frame_idx_plus1[MAX_DEC_REF_PIC_MARKING]; /* ue(v) */
    436     /* end dec_ref_pic_marking() */
    437     /* if(entropy_coding_mode_flag && slice_type!=I && slice_type!=SI) */
    438     uint cabac_init_idc;        /* ue(v), range 0..2 */
    439     int slice_qp_delta;     /* se(v), range 0..51 */
    440     /* if(slice_type==SP || slice_type==SI) */
    441     /* if(slice_type==SP) */
    442     uint    sp_for_switch_flag; /* u(1) */
    443     int slice_qs_delta;     /* se(v) */
    444 
    445     /* if(deblocking_filter_control_present_flag)*/
    446     uint disable_deblocking_filter_idc; /* ue(v), range 0..2 */
    447     /* if(disable_deblocking_filter_idc!=1) */
    448     int slice_alpha_c0_offset_div2; /* se(v), range -6..6, default 0 */
    449     int slice_beta_offset_div_2; /* se(v), range -6..6, default 0 */
    450     /* if(num_slice_groups_minus1>0 && slice_group_map_type>=3 && slice_group_map_type<=5)*/
    451     uint    slice_group_change_cycle;   /* u(v), use ceil(log2(PicSizeInMapUnits/SliceGroupChangeRate + 1)) bits*/
    452 
    453 } AVCSliceHeader;
    454 
    455 /**
    456 This struct contains information about the neighboring pixel.
    457 @publishedAll
    458 */
    459 typedef struct tagPixPos
    460 {
    461     int available;
    462     int mb_addr;    /* macroblock address of the current pixel, see below */
    463     int x;      /* x,y positions of current pixel relative to the macroblock mb_addr */
    464     int y;
    465     int pos_x;  /* x,y positions of current pixel relative to the picture. */
    466     int pos_y;
    467 } AVCPixelPos;
    468 
    469 typedef struct tagNeighborAvailability
    470 {
    471     int left;
    472     int top;    /* macroblock address of the current pixel, see below */
    473     int top_right;      /* x,y positions of current pixel relative to the macroblock mb_addr */
    474 } AVCNeighborAvailability;
    475 
    476 
    477 /**
    478 This structure contains picture data and related information necessary to be used as
    479 reference frame.
    480 @publishedAll
    481 */
    482 typedef struct tagPictureData
    483 {
    484     uint16 RefIdx;  /* index used for reference frame */
    485     uint8 *Sl;   /* derived from base_dpb in AVCFrameStore */
    486     uint8 *Scb;  /* for complementary fields, YUV are interlaced */
    487     uint8 *Scr;  /* Sl of top_field and bottom_fields will be one line apart and the
    488                     stride will be 2 times the width. */
    489     /* For non-complementary field, the above still applies. A special
    490        output formatting is required. */
    491 
    492     /* Then, necessary variables that need to be stored */
    493     AVCPictureType  picType; /* frame, top-field or bot-field */
    494     /*bool*/
    495     uint    isReference;
    496     /*bool*/
    497     uint    isLongTerm;
    498     int     PicOrderCnt;
    499     int     PicNum;
    500     int     LongTermPicNum;
    501 
    502     int     width; /* how many pixel per line */
    503     int     height;/* how many line */
    504     int     pitch; /* how many pixel between the line */
    505 
    506     uint    padded; /* flag for being padded */
    507 
    508 } AVCPictureData;
    509 
    510 /**
    511 This structure contains information for frame storage.
    512 @publishedAll
    513 */
    514 typedef struct tagFrameStore
    515 {
    516     uint8 *base_dpb;    /* base pointer for the YCbCr */
    517 
    518     int     IsReference; /*  0=not used for ref; 1=top used; 2=bottom used; 3=both fields (or frame) used */
    519     int     IsLongTerm;  /*  0=not used for ref; 1=top used; 2=bottom used; 3=both fields (or frame) used */
    520     /* if IsLongTerm is true, IsReference can be ignored. */
    521     /* if IsReference is true, IsLongterm will be checked for short-term or long-term. */
    522     /* IsUsed must be true to enable the validity of IsReference and IsLongTerm */
    523 
    524     int     IsOutputted;  /* has it been outputted via AVCDecGetOutput API, then don't output it again,
    525                             wait until it is returned. */
    526     AVCPictureData frame;
    527 
    528     int     FrameNum;
    529     int     FrameNumWrap;
    530     int     LongTermFrameIdx;
    531     int     PicOrderCnt; /* of the frame, smaller of the 2 fields */
    532 
    533 } AVCFrameStore;
    534 
    535 /**
    536 This structure maintains the actual memory for the decoded picture buffer (DPB) which is
    537 allocated at the beginning according to profile/level.
    538 Once decoded_picture_buffer is allocated, Sl,Scb,Scr in
    539 AVCPictureData structure just point to the address in decoded_picture_buffer.
    540 used_size maintains the used space.
    541 NOTE:: In order to maintain contiguous memory space, memory equal to a single frame is
    542 assigned at a time. Two opposite fields reside in the same frame memory.
    543 
    544   |-------|---|---|---|xxx|-------|xxx|---|-------|   decoded_picture_buffer
    545     frame  top bot top      frame      bot  frame
    546       0     1   1   2         3         4     5
    547 
    548   bot 2 and top 4 do not exist, the memory is not used.
    549 
    550 @publishedAll
    551 */
    552 typedef struct tagDecPicBuffer
    553 {
    554     uint8 *decoded_picture_buffer;  /* actual memory */
    555     uint32  dpb_size;       /* size of dpb in bytes */
    556     uint32  used_size;  /* used size */
    557     struct tagFrameStore    *fs[MAX_FS]; /* list of frame stored, actual buffer */
    558     int     num_fs;  /* size of fs */
    559 
    560 } AVCDecPicBuffer;
    561 
    562 
    563 /**
    564 This structure contains macroblock related variables.
    565 @publishedAll
    566 */
    567 typedef struct tagMacroblock
    568 {
    569     AVCIntraChromaPredMode  intra_chroma_pred_mode;  /* ue(v) */
    570 
    571     int32 mvL0[16];  /* motion vectors, 16 bit packed (x,y) per element  */
    572     int32 mvL1[16];
    573     int16 ref_idx_L0[4];
    574     int16 ref_idx_L1[4];
    575     uint16 RefIdx[4]; /* ref index, has value of AVCPictureData->RefIdx */
    576     /* stored data */
    577     /*bool*/
    578     uint    mb_intra; /* intra flag */
    579     /*bool*/
    580     uint    mb_bottom_field;
    581 
    582     AVCMBMode mbMode;   /* type of MB prediction */
    583     AVCSubMBMode subMbMode[4]; /* for each 8x8 partition */
    584 
    585     uint    CBP; /* CodeBlockPattern */
    586     AVCIntra16x16PredMode i16Mode; /* Intra16x16PredMode */
    587     AVCIntra4x4PredMode i4Mode[16]; /* Intra4x4PredMode, in raster scan order */
    588     int NumMbPart; /* number of partition */
    589     AVCPredMode MBPartPredMode[4][4]; /* prediction mode [MBPartIndx][subMBPartIndx] */
    590     int MbPartWidth;
    591     int MbPartHeight;
    592     int NumSubMbPart[4];  /* for each 8x8 partition */
    593     int SubMbPartWidth[4];  /* for each 8x8 partition */
    594     int SubMbPartHeight[4]; /* for each 8x8 partition */
    595 
    596     uint8 nz_coeff[NUM_BLKS_IN_MB];  /* [blk_y][blk_x], Chroma is [4..5][0...3], see predict_nnz() function */
    597 
    598     int QPy; /* Luma QP */
    599     int QPc; /* Chroma QP */
    600     int QSc; /* Chroma QP S-picture */
    601 
    602     int slice_id;           // MC slice
    603 } AVCMacroblock;
    604 
    605 
    606 /**
    607 This structure contains common internal variables between the encoder and decoder
    608 such that some functions can be shared among them.
    609 @publishedAll
    610 */
    611 typedef struct tagCommonObj
    612 {
    613     /* put these 2 up here to make sure they are word-aligned */
    614     int16   block[NUM_PIXELS_IN_MB]; /* for transformed residue coefficient */
    615     uint8   *pred_block;    /* pointer to prediction block, could point to a frame */
    616 #ifdef USE_PRED_BLOCK
    617     uint8   pred[688];  /* for prediction */
    618     /* Luma [0-399], Cb [400-543], Cr[544-687] */
    619 #endif
    620     int     pred_pitch; /* either equal to 20 or to frame pitch */
    621 
    622     /* temporary buffers for intra prediction */
    623     /* these variables should remain inside fast RAM */
    624 #ifdef MB_BASED_DEBLOCK
    625     uint8   *intra_pred_top; /* a row of pixel for intra prediction */
    626     uint8   intra_pred_left[17]; /* a column of pixel for intra prediction */
    627     uint8   *intra_pred_top_cb;
    628     uint8   intra_pred_left_cb[9];
    629     uint8   *intra_pred_top_cr;
    630     uint8   intra_pred_left_cr[9];
    631 #endif
    632     /* pointer to the prediction area for intra prediction */
    633     uint8   *pintra_pred_top;   /* pointer to the top intra prediction value */
    634     uint8   *pintra_pred_left;  /* pointer to the left intra prediction value */
    635     uint8   intra_pred_topleft; /* the [-1,-1] neighboring pixel */
    636     uint8   *pintra_pred_top_cb;
    637     uint8   *pintra_pred_left_cb;
    638     uint8   intra_pred_topleft_cb;
    639     uint8   *pintra_pred_top_cr;
    640     uint8   *pintra_pred_left_cr;
    641     uint8   intra_pred_topleft_cr;
    642 
    643     int QPy;
    644     int QPc;
    645     int QPy_div_6;
    646     int QPy_mod_6;
    647     int QPc_div_6;
    648     int QPc_mod_6;
    649     /**** nal_unit ******/
    650     /* previously in AVCNALUnit format */
    651     uint    NumBytesInRBSP;
    652     int     forbidden_bit;
    653     int     nal_ref_idc;
    654     AVCNalUnitType  nal_unit_type;
    655     AVCNalUnitType  prev_nal_unit_type;
    656     /*bool*/
    657     uint    slice_data_partitioning; /* flag when nal_unit_type is between 2 and 4 */
    658     /**** ******** ******/
    659     AVCSliceType slice_type;
    660     AVCDecPicBuffer     *decPicBuf; /* decoded picture buffer */
    661 
    662     AVCSeqParamSet *currSeqParams; /*  the currently used one */
    663 
    664     AVCPicParamSet  *currPicParams; /* the currently used one */
    665     uint        seq_parameter_set_id;
    666     /* slice header */
    667     AVCSliceHeader *sliceHdr;   /* slice header param syntax variables */
    668 
    669     AVCPictureData  *currPic; /* pointer to current picture */
    670     AVCFrameStore   *currFS;  /* pointer to current frame store */
    671     AVCPictureType  currPicType; /* frame, top-field or bot-field */
    672     /*bool*/
    673     uint    newPic; /* flag for new picture */
    674     uint            newSlice; /* flag for new slice */
    675     AVCPictureData  *prevRefPic; /* pointer to previous picture */
    676 
    677     AVCMacroblock   *mblock; /* array of macroblocks covering entire picture */
    678     AVCMacroblock   *currMB; /* pointer to current macroblock */
    679     uint                    mbNum; /* number of current MB */
    680     int                 mb_x;  /* x-coordinate of the current mbNum */
    681     int                 mb_y;  /* y-coordinate of the current mbNum */
    682 
    683     /* For internal operation, scratch memory for MV, prediction, transform, etc.*/
    684     uint32 cbp4x4; /* each bit represent nonzero 4x4 block in reverse raster scan order */
    685     /* starting from luma, Cb and Cr, lsb toward msb */
    686     int mvd_l0[4][4][2]; /* [mbPartIdx][subMbPartIdx][compIdx], se(v) */
    687     int mvd_l1[4][4][2]; /* [mbPartIdx][subMbPartIdx][compIdx], se(v) */
    688 
    689     int mbAddrA, mbAddrB, mbAddrC, mbAddrD; /* address of neighboring MBs */
    690     /*bool*/
    691     uint    mbAvailA, mbAvailB, mbAvailC, mbAvailD; /* availability */
    692     /*bool*/
    693     uint    intraAvailA, intraAvailB, intraAvailC, intraAvailD; /* for intra mode */
    694     /***********************************************/
    695     /* The following variables are defined in the draft. */
    696     /* They may need to be stored in PictureData structure and used for reference. */
    697     /* In that case, just move or copy it to AVCDecPictureData structure. */
    698 
    699     int     padded_size;    /* size of extra padding to a frame */
    700 
    701     uint    MaxFrameNum;    /*2^(log2_max_frame_num_minus4+4), range 0.. 2^16-1 */
    702     uint    MaxPicOrderCntLsb; /*2^(log2_max_pic_order_cnt_lsb_minus4+4), 0..2^16-1 */
    703     uint    PicWidthInMbs;  /*pic_width_in_mbs_minus1+1 */
    704     uint    PicWidthInSamplesL; /* PicWidthInMbs*16 */
    705     uint    PicWidthInSamplesC; /* PicWIdthInMbs*8 */
    706     uint    PicHeightInMapUnits; /* pic_height_in_map_units_minus1+1 */
    707     uint    PicSizeInMapUnits;  /* PicWidthInMbs*PicHeightInMapUnits */
    708     uint    FrameHeightInMbs;   /*(2-frame_mbs_only_flag)*PicHeightInMapUnits */
    709 
    710     uint    SliceGroupChangeRate; /* slice_group_change_rate_minus1 + 1 */
    711 
    712     /* access unit */
    713     uint    primary_pic_type;   /* u(3), Table 7-2, kinda informative only */
    714 
    715     /* slice data partition */
    716     uint    slice_id;           /* ue(v) */
    717 
    718     uint    UnusedShortTermFrameNum;
    719     uint    PrevRefFrameNum;
    720     uint    MbaffFrameFlag; /* (mb_adaptive_frame_field_flag && !field_pic_flag) */
    721     uint    PicHeightInMbs; /* FrameHeightInMbs/(1+field_pic_flag) */
    722     int     PicHeightInSamplesL; /* PicHeightInMbs*16 */
    723     int     PicHeightInSamplesC; /* PicHeightInMbs*8 */
    724     uint    PicSizeInMbs;   /* PicWidthInMbs*PicHeightInMbs */
    725     uint    level_idc;
    726     int     numMBs;
    727     uint    MaxPicNum;
    728     uint    CurrPicNum;
    729     int     QSy;    /* 26+pic_init_qp_minus26+slice_qs_delta */
    730     int     FilterOffsetA;
    731     int     FilterOffsetB;
    732     uint    MapUnitsInSliceGroup0;  /* Min(slie_group_change_cycle*SliceGroupChangeRate,PicSizeInMapUnits) */
    733     /* dec_ref_pic_marking */
    734     int     MaxLongTermFrameIdx;
    735     int     LongTermFrameIdx;
    736 
    737     /* POC related variables */
    738     /*bool*/
    739     uint    mem_mgr_ctrl_eq_5;  /* if memory_management_control_operation equal to 5 flag */
    740     int     PicOrderCnt;
    741     int     BottomFieldOrderCnt, TopFieldOrderCnt;
    742     /* POC mode 0 */
    743     int     prevPicOrderCntMsb;
    744     uint    prevPicOrderCntLsb;
    745     int     PicOrderCntMsb;
    746     /* POC mode 1 */
    747     int     prevFrameNumOffset, FrameNumOffset;
    748     uint    prevFrameNum;
    749     int     absFrameNum;
    750     int     picOrderCntCycleCnt, frameNumInPicOrderCntCycle;
    751     int     expectedDeltaPerPicOrderCntCycle;
    752     int     expectedPicOrderCnt;
    753 
    754     /* FMO */
    755     int *MbToSliceGroupMap;  /* to be re-calculate at the beginning */
    756 
    757     /* ref pic list */
    758     AVCPictureData  *RefPicList0[MAX_REF_PIC_LIST]; /* list 0 */
    759     AVCPictureData  *RefPicList1[MAX_REF_PIC_LIST]; /* list 1 */
    760     AVCFrameStore   *refFrameList0ShortTerm[32];
    761     AVCFrameStore   *refFrameList1ShortTerm[32];
    762     AVCFrameStore   *refFrameListLongTerm[32];
    763     int     refList0Size;
    764     int     refList1Size;
    765 
    766     /* slice data semantics*/
    767     int mb_skip_run;    /* ue(v) */
    768     /*uint  mb_skip_flag;*/ /* ae(v) */
    769     /* uint end_of_slice_flag;*//* ae(v) */
    770     /***********************************************/
    771 
    772     /* function pointers */
    773     int (*is_short_ref)(AVCPictureData *s);
    774     int (*is_long_ref)(AVCPictureData *s);
    775 
    776 } AVCCommonObj;
    777 
    778 /**
    779 Commonly used constant arrays.
    780 @publishedAll
    781 */
    782 /**
    783 Zigzag scan from 1-D to 2-D. */
    784 const static uint8 ZZ_SCAN[16] = {0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15};
    785 /* Zigzag scan from 1-D to 2-D output to block[24][16]. */
    786 const static uint8 ZZ_SCAN_BLOCK[16] = {0, 1, 16, 32, 17, 2, 3, 18, 33, 48, 49, 34, 19, 35, 50, 51};
    787 
    788 /**
    789 From zigzag to raster for luma DC value */
    790 const static uint8 ZIGZAG2RASTERDC[16] = {0, 4, 64, 128, 68, 8, 12, 72, 132, 192, 196, 136, 76, 140, 200, 204};
    791 
    792 
    793 /**
    794 Mapping from coding scan block indx to raster scan block index */
    795 const static int blkIdx2blkX[16] = {0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3};
    796 const static int blkIdx2blkY[16] = {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3};
    797 /** from [blk8indx][blk4indx] to raster scan index */
    798 const static int blkIdx2blkXY[4][4] = {{0, 1, 4, 5}, {2, 3, 6, 7}, {8, 9, 12, 13}, {10, 11, 14, 15}};
    799 
    800 /*
    801 Availability of the neighboring top-right block relative to the current block. */
    802 const static int BlkTopRight[16] = {2, 2, 2, 3, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0};
    803 
    804 /**
    805 Table 8-13 Specification of QPc as a function of qPI. */
    806 const static uint8 mapQPi2QPc[52] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    807                                      21, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36,
    808                                      37, 37, 37, 38, 38, 38, 39, 39, 39, 39
    809                                     };
    810 
    811 /**
    812 See 8.5.5 equation (8-252 and 8-253) the definition of v matrix. */
    813 /* in zigzag scan */
    814 const static int dequant_coefres[6][16] =
    815 {
    816     {10, 13, 13, 10, 16, 10, 13, 13, 13, 13, 16, 10, 16, 13, 13, 16},
    817     {11, 14, 14, 11, 18, 11, 14, 14, 14, 14, 18, 11, 18, 14, 14, 18},
    818     {13, 16, 16, 13, 20, 13, 16, 16, 16, 16, 20, 13, 20, 16, 16, 20},
    819     {14, 18, 18, 14, 23, 14, 18, 18, 18, 18, 23, 14, 23, 18, 18, 23},
    820     {16, 20, 20, 16, 25, 16, 20, 20, 20, 20, 25, 16, 25, 20, 20, 25},
    821     {18, 23, 23, 18, 29, 18, 23, 23, 23, 23, 29, 18, 29, 23, 23, 29}
    822 };
    823 
    824 /**
    825 From jm7.6 block.c. (in zigzag scan) */
    826 const static int quant_coef[6][16] =
    827 {
    828     {13107, 8066,   8066,   13107,  5243,   13107,  8066,   8066,   8066,   8066,   5243,   13107,  5243,   8066,   8066,   5243},
    829     {11916, 7490,   7490,   11916,  4660,   11916,  7490,   7490,   7490,   7490,   4660,   11916,  4660,   7490,   7490,   4660},
    830     {10082, 6554,   6554,   10082,  4194,   10082,  6554,   6554,   6554,   6554,   4194,   10082,  4194,   6554,   6554,   4194},
    831     {9362,  5825,   5825,   9362,   3647,   9362,   5825,   5825,   5825,   5825,   3647,   9362,   3647,   5825,   5825,   3647},
    832     {8192,  5243,   5243,   8192,   3355,   8192,   5243,   5243,   5243,   5243,   3355,   8192,   3355,   5243,   5243,   3355},
    833     {7282,  4559,   4559,   7282,   2893,   7282,   4559,   4559,   4559,   4559,   2893,   7282,   2893,   4559,   4559,   2893}
    834 };
    835 
    836 /**
    837 Convert scan from raster scan order to block decoding order and
    838 from block decoding order to raster scan order. Same table!!!
    839 */
    840 const static uint8 ras2dec[16] = {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};
    841 
    842 /* mapping from level_idc to index map */
    843 const static uint8 mapLev2Idx[61] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 1,
    844                                      0, 1, 2, 3, 255, 255, 255, 255, 255, 255,
    845                                      4, 5, 6, 255, 255, 255, 255, 255, 255, 255,
    846                                      7, 8, 9, 255, 255, 255, 255, 255, 255, 255,
    847                                      10, 11, 12, 255, 255, 255, 255, 255, 255, 255,
    848                                      13, 14, 255, 255, 255, 255, 255, 255, 255, 255
    849                                     };
    850 /* map back from index to Level IDC */
    851 const static uint8 mapIdx2Lev[MAX_LEVEL_IDX] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41, 42, 50, 51};
    852 
    853 /**
    854 from the index map to the MaxDPB value times 2 */
    855 const static int32 MaxDPBX2[MAX_LEVEL_IDX] = {297, 675, 1782, 1782, 1782, 3564, 6075, 6075,
    856         13500, 15360, 24576, 24576, 24576, 82620, 138240
    857                                              };
    858 
    859 /* map index to the max frame size */
    860 const static int MaxFS[MAX_LEVEL_IDX] = {99, 396, 396, 396, 396, 792, 1620, 1620, 3600, 5120,
    861                                         8192, 8192, 8192, 22080, 36864
    862                                         };
    863 
    864 /* map index to max MB processing rate */
    865 const static int32 MaxMBPS[MAX_LEVEL_IDX] = {1485, 3000, 6000, 11880, 11880, 19800, 20250, 40500,
    866         108000, 216000, 245760, 245760, 491520, 589824, 983040
    867                                             };
    868 
    869 /* map index to max video bit rate */
    870 const static uint32 MaxBR[MAX_LEVEL_IDX] = {64, 192, 384, 768, 2000, 4000, 4000, 10000, 14000, 20000,
    871         20000, 50000, 50000, 135000, 240000
    872                                            };
    873 
    874 /* map index to max CPB size */
    875 const static uint32 MaxCPB[MAX_LEVEL_IDX] = {175, 500, 1000, 2000, 2000, 4000, 4000, 10000, 14000,
    876         20000, 25000, 62500, 62500, 135000, 240000
    877                                             };
    878 
    879 /* map index to max vertical MV range */
    880 const static int MaxVmvR[MAX_LEVEL_IDX] = {64, 128, 128, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512, 512};
    881 
    882 #endif /*  _AVCINT_COMMON_H_ */
    883