00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #ifndef AVCODEC_H264_H
00029 #define AVCODEC_H264_H
00030
00031 #include "dsputil.h"
00032 #include "cabac.h"
00033 #include "mpegvideo.h"
00034 #include "h264pred.h"
00035
00036 #define interlaced_dct interlaced_dct_is_a_bad_name
00037 #define mb_intra mb_intra_is_not_initialized_see_mb_type
00038
00039 #define LUMA_DC_BLOCK_INDEX 25
00040 #define CHROMA_DC_BLOCK_INDEX 26
00041
00042 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
00043 #define COEFF_TOKEN_VLC_BITS 8
00044 #define TOTAL_ZEROS_VLC_BITS 9
00045 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
00046 #define RUN_VLC_BITS 3
00047 #define RUN7_VLC_BITS 6
00048
00049 #define MAX_SPS_COUNT 32
00050 #define MAX_PPS_COUNT 256
00051
00052 #define MAX_MMCO_COUNT 66
00053
00054 #define MAX_DELAYED_PIC_COUNT 16
00055
00056
00057
00058 #define ALLOW_INTERLACE
00059
00060 #define ALLOW_NOCHROMA
00061
00066 #define MAX_SLICES 16
00067
00068 #ifdef ALLOW_INTERLACE
00069 #define MB_MBAFF h->mb_mbaff
00070 #define MB_FIELD h->mb_field_decoding_flag
00071 #define FRAME_MBAFF h->mb_aff_frame
00072 #define FIELD_PICTURE (s->picture_structure != PICT_FRAME)
00073 #else
00074 #define MB_MBAFF 0
00075 #define MB_FIELD 0
00076 #define FRAME_MBAFF 0
00077 #define FIELD_PICTURE 0
00078 #undef IS_INTERLACED
00079 #define IS_INTERLACED(mb_type) 0
00080 #endif
00081 #define FIELD_OR_MBAFF_PICTURE (FRAME_MBAFF || FIELD_PICTURE)
00082
00083 #ifdef ALLOW_NOCHROMA
00084 #define CHROMA h->sps.chroma_format_idc
00085 #else
00086 #define CHROMA 1
00087 #endif
00088
00089 #define EXTENDED_SAR 255
00090
00091 #define MB_TYPE_REF0 MB_TYPE_ACPRED //dirty but it fits in 16 bit
00092 #define MB_TYPE_8x8DCT 0x01000000
00093 #define IS_REF0(a) ((a) & MB_TYPE_REF0)
00094 #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
00095
00096
00097 enum {
00098 NAL_SLICE=1,
00099 NAL_DPA,
00100 NAL_DPB,
00101 NAL_DPC,
00102 NAL_IDR_SLICE,
00103 NAL_SEI,
00104 NAL_SPS,
00105 NAL_PPS,
00106 NAL_AUD,
00107 NAL_END_SEQUENCE,
00108 NAL_END_STREAM,
00109 NAL_FILLER_DATA,
00110 NAL_SPS_EXT,
00111 NAL_AUXILIARY_SLICE=19
00112 };
00113
00117 typedef enum {
00118 SEI_BUFFERING_PERIOD = 0,
00119 SEI_TYPE_PIC_TIMING = 1,
00120 SEI_TYPE_USER_DATA_UNREGISTERED = 5,
00121 SEI_TYPE_RECOVERY_POINT = 6
00122 } SEI_Type;
00123
00127 typedef enum {
00128 SEI_PIC_STRUCT_FRAME = 0,
00129 SEI_PIC_STRUCT_TOP_FIELD = 1,
00130 SEI_PIC_STRUCT_BOTTOM_FIELD = 2,
00131 SEI_PIC_STRUCT_TOP_BOTTOM = 3,
00132 SEI_PIC_STRUCT_BOTTOM_TOP = 4,
00133 SEI_PIC_STRUCT_TOP_BOTTOM_TOP = 5,
00134 SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM = 6,
00135 SEI_PIC_STRUCT_FRAME_DOUBLING = 7,
00136 SEI_PIC_STRUCT_FRAME_TRIPLING = 8
00137 } SEI_PicStructType;
00138
00142 typedef struct SPS{
00143
00144 int profile_idc;
00145 int level_idc;
00146 int chroma_format_idc;
00147 int transform_bypass;
00148 int log2_max_frame_num;
00149 int poc_type;
00150 int log2_max_poc_lsb;
00151 int delta_pic_order_always_zero_flag;
00152 int offset_for_non_ref_pic;
00153 int offset_for_top_to_bottom_field;
00154 int poc_cycle_length;
00155 int ref_frame_count;
00156 int gaps_in_frame_num_allowed_flag;
00157 int mb_width;
00158 int mb_height;
00159 int frame_mbs_only_flag;
00160 int mb_aff;
00161 int direct_8x8_inference_flag;
00162 int crop;
00163 unsigned int crop_left;
00164 unsigned int crop_right;
00165 unsigned int crop_top;
00166 unsigned int crop_bottom;
00167 int vui_parameters_present_flag;
00168 AVRational sar;
00169 int timing_info_present_flag;
00170 uint32_t num_units_in_tick;
00171 uint32_t time_scale;
00172 int fixed_frame_rate_flag;
00173 short offset_for_ref_frame[256];
00174 int bitstream_restriction_flag;
00175 int num_reorder_frames;
00176 int scaling_matrix_present;
00177 uint8_t scaling_matrix4[6][16];
00178 uint8_t scaling_matrix8[2][64];
00179 int nal_hrd_parameters_present_flag;
00180 int vcl_hrd_parameters_present_flag;
00181 int pic_struct_present_flag;
00182 int time_offset_length;
00183 int cpb_cnt;
00184 int initial_cpb_removal_delay_length;
00185 int cpb_removal_delay_length;
00186 int dpb_output_delay_length;
00187 int bit_depth_luma;
00188 int bit_depth_chroma;
00189 int residual_color_transform_flag;
00190 }SPS;
00191
00195 typedef struct PPS{
00196 unsigned int sps_id;
00197 int cabac;
00198 int pic_order_present;
00199 int slice_group_count;
00200 int mb_slice_group_map_type;
00201 unsigned int ref_count[2];
00202 int weighted_pred;
00203 int weighted_bipred_idc;
00204 int init_qp;
00205 int init_qs;
00206 int chroma_qp_index_offset[2];
00207 int deblocking_filter_parameters_present;
00208 int constrained_intra_pred;
00209 int redundant_pic_cnt_present;
00210 int transform_8x8_mode;
00211 uint8_t scaling_matrix4[6][16];
00212 uint8_t scaling_matrix8[2][64];
00213 uint8_t chroma_qp_table[2][64];
00214 int chroma_qp_diff;
00215 }PPS;
00216
00220 typedef enum MMCOOpcode{
00221 MMCO_END=0,
00222 MMCO_SHORT2UNUSED,
00223 MMCO_LONG2UNUSED,
00224 MMCO_SHORT2LONG,
00225 MMCO_SET_MAX_LONG,
00226 MMCO_RESET,
00227 MMCO_LONG,
00228 } MMCOOpcode;
00229
00233 typedef struct MMCO{
00234 MMCOOpcode opcode;
00235 int short_pic_num;
00236 int long_arg;
00237 } MMCO;
00238
00242 typedef struct H264Context{
00243 MpegEncContext s;
00244 int nal_ref_idc;
00245 int nal_unit_type;
00246 uint8_t *rbsp_buffer[2];
00247 unsigned int rbsp_buffer_size[2];
00248
00252 int is_avc;
00253 int got_avcC;
00254 int nal_length_size;
00255
00256 int chroma_qp[2];
00257
00258 int prev_mb_skipped;
00259 int next_mb_skipped;
00260
00261
00262 int chroma_pred_mode;
00263 int intra16x16_pred_mode;
00264
00265 int top_mb_xy;
00266 int left_mb_xy[2];
00267
00268 int8_t intra4x4_pred_mode_cache[5*8];
00269 int8_t (*intra4x4_pred_mode)[8];
00270 H264PredContext hpc;
00271 unsigned int topleft_samples_available;
00272 unsigned int top_samples_available;
00273 unsigned int topright_samples_available;
00274 unsigned int left_samples_available;
00275 uint8_t (*top_borders[2])[16+2*8];
00276 uint8_t left_border[2*(17+2*9)];
00277
00282 DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
00283 uint8_t (*non_zero_count)[16];
00284
00288 DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
00289 DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
00290 #define LIST_NOT_USED -1 //FIXME rename?
00291 #define PART_NOT_AVAILABLE -2
00292
00296 int mv_cache_clean[2];
00297
00301 int neighbor_transform_size;
00302
00307 int block_offset[2*(16+8)];
00308
00309 uint32_t *mb2b_xy;
00310 uint32_t *mb2b8_xy;
00311 int b_stride;
00312 int b8_stride;
00313
00314 int mb_linesize;
00315 int mb_uvlinesize;
00316
00317 int emu_edge_width;
00318 int emu_edge_height;
00319
00320 int halfpel_flag;
00321 int thirdpel_flag;
00322
00323 int unknown_svq3_flag;
00324 int next_slice_index;
00325
00326 SPS *sps_buffers[MAX_SPS_COUNT];
00327 SPS sps;
00328
00329 PPS *pps_buffers[MAX_PPS_COUNT];
00333 PPS pps;
00334
00335 uint32_t dequant4_buffer[6][52][16];
00336 uint32_t dequant8_buffer[2][52][64];
00337 uint32_t (*dequant4_coeff[6])[16];
00338 uint32_t (*dequant8_coeff[2])[64];
00339 int dequant_coeff_pps;
00340
00341 int slice_num;
00342 uint16_t *slice_table_base;
00343 uint16_t *slice_table;
00344 int slice_type;
00345 int slice_type_nos;
00346 int slice_type_fixed;
00347
00348
00349 int mb_aff_frame;
00350 int mb_field_decoding_flag;
00351 int mb_mbaff;
00352
00353 DECLARE_ALIGNED_8(uint16_t, sub_mb_type[4]);
00354
00355
00356 int poc_lsb;
00357 int poc_msb;
00358 int delta_poc_bottom;
00359 int delta_poc[2];
00360 int frame_num;
00361 int prev_poc_msb;
00362 int prev_poc_lsb;
00363 int frame_num_offset;
00364 int prev_frame_num_offset;
00365 int prev_frame_num;
00366
00370 int curr_pic_num;
00371
00375 int max_pic_num;
00376
00377
00378 int use_weight;
00379 int use_weight_chroma;
00380 int luma_log2_weight_denom;
00381 int chroma_log2_weight_denom;
00382 int luma_weight[2][48];
00383 int luma_offset[2][48];
00384 int chroma_weight[2][48][2];
00385 int chroma_offset[2][48][2];
00386 int implicit_weight[48][48];
00387
00388
00389 int deblocking_filter;
00390 int slice_alpha_c0_offset;
00391 int slice_beta_offset;
00392
00393 int redundant_pic_count;
00394
00395 int direct_spatial_mv_pred;
00396 int dist_scale_factor[16];
00397 int dist_scale_factor_field[2][32];
00398 int map_col_to_list0[2][16+32];
00399 int map_col_to_list0_field[2][2][16+32];
00400
00404 unsigned int ref_count[2];
00405 unsigned int list_count;
00406 Picture *short_ref[32];
00407 Picture *long_ref[32];
00408 Picture default_ref_list[2][32];
00409 Picture ref_list[2][48];
00412 int ref2frm[MAX_SLICES][2][64];
00413 Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2];
00414 int outputed_poc;
00415
00419 MMCO mmco[MAX_MMCO_COUNT];
00420 int mmco_index;
00421
00422 int long_ref_count;
00423 int short_ref_count;
00424
00425
00426 GetBitContext intra_gb;
00427 GetBitContext inter_gb;
00428 GetBitContext *intra_gb_ptr;
00429 GetBitContext *inter_gb_ptr;
00430
00431 DECLARE_ALIGNED_16(DCTELEM, mb[16*24]);
00432 DCTELEM mb_padding[256];
00433
00437 CABACContext cabac;
00438 uint8_t cabac_state[460];
00439 int cabac_init_idc;
00440
00441
00442 uint16_t *cbp_table;
00443 int cbp;
00444 int top_cbp;
00445 int left_cbp;
00446
00447 uint8_t *chroma_pred_mode_table;
00448 int last_qscale_diff;
00449 int16_t (*mvd_table[2])[2];
00450 DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
00451 uint8_t *direct_table;
00452 uint8_t direct_cache[5*8];
00453
00454 uint8_t zigzag_scan[16];
00455 uint8_t zigzag_scan8x8[64];
00456 uint8_t zigzag_scan8x8_cavlc[64];
00457 uint8_t field_scan[16];
00458 uint8_t field_scan8x8[64];
00459 uint8_t field_scan8x8_cavlc[64];
00460 const uint8_t *zigzag_scan_q0;
00461 const uint8_t *zigzag_scan8x8_q0;
00462 const uint8_t *zigzag_scan8x8_cavlc_q0;
00463 const uint8_t *field_scan_q0;
00464 const uint8_t *field_scan8x8_q0;
00465 const uint8_t *field_scan8x8_cavlc_q0;
00466
00467 int x264_build;
00468
00473 struct H264Context *thread_context[MAX_THREADS];
00474
00478 int current_slice;
00479
00486 int max_contexts;
00487
00492 int single_decode_warning;
00493
00494 int last_slice_type;
00497 int mb_xy;
00498
00499 uint32_t svq3_watermark_key;
00500
00504 SEI_PicStructType sei_pic_struct;
00505
00509 int sei_dpb_output_delay;
00510
00514 int sei_cpb_removal_delay;
00515
00523 int sei_recovery_frame_cnt;
00524
00525 int is_complex;
00526
00527 int luma_weight_flag[2];
00528 int chroma_weight_flag[2];
00529
00530
00531 int sei_buffering_period_present;
00532 int initial_cpb_removal_delay[32];
00533 }H264Context;
00534
00538 int ff_h264_decode_sei(H264Context *h);
00539
00543 int ff_h264_decode_seq_parameter_set(H264Context *h);
00544
00548 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
00549
00557 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
00558
00563 int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src);
00564
00565 #endif