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