• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/h264.h

Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #ifndef AVCODEC_H264_H
00029 #define AVCODEC_H264_H
00030 
00031 #include "libavutil/intreadwrite.h"
00032 #include "dsputil.h"
00033 #include "cabac.h"
00034 #include "mpegvideo.h"
00035 #include "h264dsp.h"
00036 #include "h264pred.h"
00037 #include "rectangle.h"
00038 
00039 #define interlaced_dct interlaced_dct_is_a_bad_name
00040 #define mb_intra mb_intra_is_not_initialized_see_mb_type
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 #define MAX_MBPAIR_SIZE (256*1024) // a tighter bound could be calculated if someone cares about a few bytes
00057 
00058 /* Compiling in interlaced support reduces the speed
00059  * of progressive decoding by about 2%. */
00060 #define ALLOW_INTERLACE
00061 
00062 #define FMO 0
00063 
00068 #define MAX_SLICES 16
00069 
00070 #ifdef ALLOW_INTERLACE
00071 #define MB_MBAFF h->mb_mbaff
00072 #define MB_FIELD h->mb_field_decoding_flag
00073 #define FRAME_MBAFF h->mb_aff_frame
00074 #define FIELD_PICTURE (s->picture_structure != PICT_FRAME)
00075 #else
00076 #define MB_MBAFF 0
00077 #define MB_FIELD 0
00078 #define FRAME_MBAFF 0
00079 #define FIELD_PICTURE 0
00080 #undef  IS_INTERLACED
00081 #define IS_INTERLACED(mb_type) 0
00082 #endif
00083 #define FIELD_OR_MBAFF_PICTURE (FRAME_MBAFF || FIELD_PICTURE)
00084 
00085 #ifndef CABAC
00086 #define CABAC h->pps.cabac
00087 #endif
00088 
00089 #define CHROMA444 (h->sps.chroma_format_idc == 3)
00090 
00091 #define EXTENDED_SAR          255
00092 
00093 #define MB_TYPE_REF0       MB_TYPE_ACPRED //dirty but it fits in 16 bit
00094 #define MB_TYPE_8x8DCT     0x01000000
00095 #define IS_REF0(a)         ((a) & MB_TYPE_REF0)
00096 #define IS_8x8DCT(a)       ((a) & MB_TYPE_8x8DCT)
00097 
00102 #define DELAYED_PIC_REF 4
00103 
00104 #define QP_MAX_NUM (51 + 4*6)           // The maximum supported qp
00105 
00106 /* NAL unit types */
00107 enum {
00108     NAL_SLICE=1,
00109     NAL_DPA,
00110     NAL_DPB,
00111     NAL_DPC,
00112     NAL_IDR_SLICE,
00113     NAL_SEI,
00114     NAL_SPS,
00115     NAL_PPS,
00116     NAL_AUD,
00117     NAL_END_SEQUENCE,
00118     NAL_END_STREAM,
00119     NAL_FILLER_DATA,
00120     NAL_SPS_EXT,
00121     NAL_AUXILIARY_SLICE=19
00122 };
00123 
00127 typedef enum {
00128     SEI_BUFFERING_PERIOD             =  0, 
00129     SEI_TYPE_PIC_TIMING              =  1, 
00130     SEI_TYPE_USER_DATA_UNREGISTERED  =  5, 
00131     SEI_TYPE_RECOVERY_POINT          =  6  
00132 } SEI_Type;
00133 
00137 typedef enum {
00138     SEI_PIC_STRUCT_FRAME             = 0, 
00139     SEI_PIC_STRUCT_TOP_FIELD         = 1, 
00140     SEI_PIC_STRUCT_BOTTOM_FIELD      = 2, 
00141     SEI_PIC_STRUCT_TOP_BOTTOM        = 3, 
00142     SEI_PIC_STRUCT_BOTTOM_TOP        = 4, 
00143     SEI_PIC_STRUCT_TOP_BOTTOM_TOP    = 5, 
00144     SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM = 6, 
00145     SEI_PIC_STRUCT_FRAME_DOUBLING    = 7, 
00146     SEI_PIC_STRUCT_FRAME_TRIPLING    = 8  
00147 } SEI_PicStructType;
00148 
00152 typedef struct SPS{
00153 
00154     int profile_idc;
00155     int level_idc;
00156     int chroma_format_idc;
00157     int transform_bypass;              
00158     int log2_max_frame_num;            
00159     int poc_type;                      
00160     int log2_max_poc_lsb;              
00161     int delta_pic_order_always_zero_flag;
00162     int offset_for_non_ref_pic;
00163     int offset_for_top_to_bottom_field;
00164     int poc_cycle_length;              
00165     int ref_frame_count;               
00166     int gaps_in_frame_num_allowed_flag;
00167     int mb_width;                      
00168     int mb_height;                     
00169     int frame_mbs_only_flag;
00170     int mb_aff;                        
00171     int direct_8x8_inference_flag;
00172     int crop;                   
00173     unsigned int crop_left;            
00174     unsigned int crop_right;           
00175     unsigned int crop_top;             
00176     unsigned int crop_bottom;          
00177     int vui_parameters_present_flag;
00178     AVRational sar;
00179     int video_signal_type_present_flag;
00180     int full_range;
00181     int colour_description_present_flag;
00182     enum AVColorPrimaries color_primaries;
00183     enum AVColorTransferCharacteristic color_trc;
00184     enum AVColorSpace colorspace;
00185     int timing_info_present_flag;
00186     uint32_t num_units_in_tick;
00187     uint32_t time_scale;
00188     int fixed_frame_rate_flag;
00189     short offset_for_ref_frame[256]; //FIXME dyn aloc?
00190     int bitstream_restriction_flag;
00191     int num_reorder_frames;
00192     int scaling_matrix_present;
00193     uint8_t scaling_matrix4[6][16];
00194     uint8_t scaling_matrix8[6][64];
00195     int nal_hrd_parameters_present_flag;
00196     int vcl_hrd_parameters_present_flag;
00197     int pic_struct_present_flag;
00198     int time_offset_length;
00199     int cpb_cnt;                       
00200     int initial_cpb_removal_delay_length; 
00201     int cpb_removal_delay_length;      
00202     int dpb_output_delay_length;       
00203     int bit_depth_luma;                
00204     int bit_depth_chroma;              
00205     int residual_color_transform_flag; 
00206     int constraint_set_flags;          
00207 }SPS;
00208 
00212 typedef struct PPS{
00213     unsigned int sps_id;
00214     int cabac;                  
00215     int pic_order_present;      
00216     int slice_group_count;      
00217     int mb_slice_group_map_type;
00218     unsigned int ref_count[2];  
00219     int weighted_pred;          
00220     int weighted_bipred_idc;
00221     int init_qp;                
00222     int init_qs;                
00223     int chroma_qp_index_offset[2];
00224     int deblocking_filter_parameters_present; 
00225     int constrained_intra_pred; 
00226     int redundant_pic_cnt_present; 
00227     int transform_8x8_mode;     
00228     uint8_t scaling_matrix4[6][16];
00229     uint8_t scaling_matrix8[6][64];
00230     uint8_t chroma_qp_table[2][QP_MAX_NUM+1];  
00231     int chroma_qp_diff;
00232 }PPS;
00233 
00237 typedef enum MMCOOpcode{
00238     MMCO_END=0,
00239     MMCO_SHORT2UNUSED,
00240     MMCO_LONG2UNUSED,
00241     MMCO_SHORT2LONG,
00242     MMCO_SET_MAX_LONG,
00243     MMCO_RESET,
00244     MMCO_LONG,
00245 } MMCOOpcode;
00246 
00250 typedef struct MMCO{
00251     MMCOOpcode opcode;
00252     int short_pic_num;  
00253     int long_arg;       
00254 } MMCO;
00255 
00259 typedef struct H264Context{
00260     MpegEncContext s;
00261     H264DSPContext h264dsp;
00262     int pixel_shift;    
00263     int chroma_qp[2]; //QPc
00264 
00265     int qp_thresh;      
00266 
00267     int prev_mb_skipped;
00268     int next_mb_skipped;
00269 
00270     //prediction stuff
00271     int chroma_pred_mode;
00272     int intra16x16_pred_mode;
00273 
00274     int topleft_mb_xy;
00275     int top_mb_xy;
00276     int topright_mb_xy;
00277     int left_mb_xy[2];
00278 
00279     int topleft_type;
00280     int top_type;
00281     int topright_type;
00282     int left_type[2];
00283 
00284     const uint8_t * left_block;
00285     int topleft_partition;
00286 
00287     int8_t intra4x4_pred_mode_cache[5*8];
00288     int8_t (*intra4x4_pred_mode);
00289     H264PredContext hpc;
00290     unsigned int topleft_samples_available;
00291     unsigned int top_samples_available;
00292     unsigned int topright_samples_available;
00293     unsigned int left_samples_available;
00294     uint8_t (*top_borders[2])[(16*3)*2];
00295 
00300     DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15*8];
00301 
00302     uint8_t (*non_zero_count)[48];
00303 
00307     DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5*8][2];
00308     DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5*8];
00309 #define LIST_NOT_USED -1 //FIXME rename?
00310 #define PART_NOT_AVAILABLE -2
00311 
00315     int mv_cache_clean[2];
00316 
00320     int neighbor_transform_size;
00321 
00326     int block_offset[2*(16*3)];
00327 
00328     uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
00329     uint32_t *mb2br_xy;
00330     int b_stride; //FIXME use s->b4_stride
00331 
00332     int mb_linesize;   
00333     int mb_uvlinesize;
00334 
00335     int emu_edge_width;
00336     int emu_edge_height;
00337 
00338     SPS sps; 
00339 
00343     PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
00344 
00345     uint32_t dequant4_buffer[6][QP_MAX_NUM+1][16]; //FIXME should these be moved down?
00346     uint32_t dequant8_buffer[6][QP_MAX_NUM+1][64];
00347     uint32_t (*dequant4_coeff[6])[16];
00348     uint32_t (*dequant8_coeff[6])[64];
00349 
00350     int slice_num;
00351     uint16_t *slice_table;     
00352     int slice_type;
00353     int slice_type_nos;        
00354     int slice_type_fixed;
00355 
00356     //interlacing specific flags
00357     int mb_aff_frame;
00358     int mb_field_decoding_flag;
00359     int mb_mbaff;              
00360 
00361     DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
00362 
00363     //Weighted pred stuff
00364     int use_weight;
00365     int use_weight_chroma;
00366     int luma_log2_weight_denom;
00367     int chroma_log2_weight_denom;
00368     //The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
00369     int luma_weight[48][2][2];
00370     int chroma_weight[48][2][2][2];
00371     int implicit_weight[48][48][2];
00372 
00373     int direct_spatial_mv_pred;
00374     int col_parity;
00375     int col_fieldoff;
00376     int dist_scale_factor[16];
00377     int dist_scale_factor_field[2][32];
00378     int map_col_to_list0[2][16+32];
00379     int map_col_to_list0_field[2][2][16+32];
00380 
00384     uint8_t *list_counts;            
00385     unsigned int ref_count[2];   
00386     unsigned int list_count;
00387     Picture ref_list[2][48];         
00390     int ref2frm[MAX_SLICES][2][64];  
00391 
00392     //data partitioning
00393     GetBitContext intra_gb;
00394     GetBitContext inter_gb;
00395     GetBitContext *intra_gb_ptr;
00396     GetBitContext *inter_gb_ptr;
00397 
00398     DECLARE_ALIGNED(16, DCTELEM, mb)[16*48*2]; 
00399     DECLARE_ALIGNED(16, DCTELEM, mb_luma_dc)[3][16*2];
00400     DCTELEM mb_padding[256*2];        
00401 
00405     CABACContext cabac;
00406     uint8_t      cabac_state[1024];
00407 
00408     /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
00409     uint16_t     *cbp_table;
00410     int cbp;
00411     int top_cbp;
00412     int left_cbp;
00413     /* chroma_pred_mode for i4x4 or i16x16, else 0 */
00414     uint8_t     *chroma_pred_mode_table;
00415     int         last_qscale_diff;
00416     uint8_t     (*mvd_table[2])[2];
00417     DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5*8][2];
00418     uint8_t     *direct_table;
00419     uint8_t     direct_cache[5*8];
00420 
00421     uint8_t zigzag_scan[16];
00422     uint8_t zigzag_scan8x8[64];
00423     uint8_t zigzag_scan8x8_cavlc[64];
00424     uint8_t field_scan[16];
00425     uint8_t field_scan8x8[64];
00426     uint8_t field_scan8x8_cavlc[64];
00427     const uint8_t *zigzag_scan_q0;
00428     const uint8_t *zigzag_scan8x8_q0;
00429     const uint8_t *zigzag_scan8x8_cavlc_q0;
00430     const uint8_t *field_scan_q0;
00431     const uint8_t *field_scan8x8_q0;
00432     const uint8_t *field_scan8x8_cavlc_q0;
00433 
00434     int x264_build;
00435 
00436     int mb_xy;
00437 
00438     int is_complex;
00439 
00440     //deblock
00441     int deblocking_filter;         
00442     int slice_alpha_c0_offset;
00443     int slice_beta_offset;
00444 
00445 //=============================================================
00446     //Things below are not used in the MB or more inner code
00447 
00448     int nal_ref_idc;
00449     int nal_unit_type;
00450     uint8_t *rbsp_buffer[2];
00451     unsigned int rbsp_buffer_size[2];
00452 
00456     int is_avc; 
00457     int nal_length_size; 
00458     int got_first; 
00459 
00460     SPS *sps_buffers[MAX_SPS_COUNT];
00461     PPS *pps_buffers[MAX_PPS_COUNT];
00462 
00463     int dequant_coeff_pps;     
00464 
00465     uint16_t *slice_table_base;
00466 
00467 
00468     //POC stuff
00469     int poc_lsb;
00470     int poc_msb;
00471     int delta_poc_bottom;
00472     int delta_poc[2];
00473     int frame_num;
00474     int prev_poc_msb;             
00475     int prev_poc_lsb;             
00476     int frame_num_offset;         
00477     int prev_frame_num_offset;    
00478     int prev_frame_num;           
00479 
00483     int curr_pic_num;
00484 
00488     int max_pic_num;
00489 
00490     int redundant_pic_count;
00491 
00492     Picture *short_ref[32];
00493     Picture *long_ref[32];
00494     Picture default_ref_list[2][32]; 
00495     Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2]; //FIXME size?
00496     Picture *next_output_pic;
00497     int outputed_poc;
00498     int next_outputed_poc;
00499 
00503     MMCO mmco[MAX_MMCO_COUNT];
00504     int mmco_index;
00505 
00506     int long_ref_count;  
00507     int short_ref_count; 
00508 
00509     int          cabac_init_idc;
00510 
00515     struct H264Context *thread_context[MAX_THREADS];
00516 
00520     int current_slice;
00521 
00528     int max_contexts;
00529 
00534     int single_decode_warning;
00535 
00536     int last_slice_type;
00542     SEI_PicStructType sei_pic_struct;
00543 
00550     int prev_interlaced_frame;
00551 
00557     int sei_ct_type;
00558 
00562     int sei_dpb_output_delay;
00563 
00567     int sei_cpb_removal_delay;
00568 
00576     int sei_recovery_frame_cnt;
00577 
00578     int luma_weight_flag[2];   
00579     int chroma_weight_flag[2]; 
00580 
00581     // Timestamp stuff
00582     int sei_buffering_period_present;  
00583     int initial_cpb_removal_delay[32]; 
00584 }H264Context;
00585 
00586 
00587 extern const uint8_t ff_h264_chroma_qp[5][QP_MAX_NUM+1]; 
00588 
00592 int ff_h264_decode_sei(H264Context *h);
00593 
00597 int ff_h264_decode_seq_parameter_set(H264Context *h);
00598 
00602 int ff_h264_get_profile(SPS *sps);
00603 
00607 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
00608 
00616 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
00617 
00621 av_cold void ff_h264_free_context(H264Context *h);
00622 
00626 int ff_h264_get_slice_type(const H264Context *h);
00627 
00632 int ff_h264_alloc_tables(H264Context *h);
00633 
00637 int ff_h264_fill_default_ref_list(H264Context *h);
00638 
00639 int ff_h264_decode_ref_pic_list_reordering(H264Context *h);
00640 void ff_h264_fill_mbaff_ref_list(H264Context *h);
00641 void ff_h264_remove_all_refs(H264Context *h);
00642 
00646 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
00647 
00648 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb);
00649 
00650 void ff_generate_sliding_window_mmcos(H264Context *h);
00651 
00652 
00656 int ff_h264_check_intra4x4_pred_mode(H264Context *h);
00657 
00661 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
00662 
00663 void ff_h264_write_back_intra_pred_mode(H264Context *h);
00664 void ff_h264_hl_decode_mb(H264Context *h);
00665 int ff_h264_frame_start(H264Context *h);
00666 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size);
00667 av_cold int ff_h264_decode_init(AVCodecContext *avctx);
00668 av_cold int ff_h264_decode_end(AVCodecContext *avctx);
00669 av_cold void ff_h264_decode_init_vlc(void);
00670 
00675 int ff_h264_decode_mb_cavlc(H264Context *h);
00676 
00681 int ff_h264_decode_mb_cabac(H264Context *h);
00682 
00683 void ff_h264_init_cabac_states(H264Context *h);
00684 
00685 void ff_h264_direct_dist_scale_factor(H264Context * const h);
00686 void ff_h264_direct_ref_list_init(H264Context * const h);
00687 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type);
00688 
00689 void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
00690 void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
00691 
00697 void ff_h264_reset_sei(H264Context *h);
00698 
00699 
00700 /*
00701 o-o o-o
00702  / / /
00703 o-o o-o
00704  ,---'
00705 o-o o-o
00706  / / /
00707 o-o o-o
00708 */
00709 
00710 /* Scan8 organization:
00711  *    0 1 2 3 4 5 6 7
00712  * 0  DY    y y y y y
00713  * 1        y Y Y Y Y
00714  * 2        y Y Y Y Y
00715  * 3        y Y Y Y Y
00716  * 4        y Y Y Y Y
00717  * 5  DU    u u u u u
00718  * 6        u U U U U
00719  * 7        u U U U U
00720  * 8        u U U U U
00721  * 9        u U U U U
00722  * 10 DV    v v v v v
00723  * 11       v V V V V
00724  * 12       v V V V V
00725  * 13       v V V V V
00726  * 14       v V V V V
00727  * DY/DU/DV are for luma/chroma DC.
00728  */
00729 
00730 #define LUMA_DC_BLOCK_INDEX   48
00731 #define CHROMA_DC_BLOCK_INDEX 49
00732 
00733 //This table must be here because scan8[constant] must be known at compiletime
00734 static const uint8_t scan8[16*3 + 3]={
00735  4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
00736  6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
00737  4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
00738  6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
00739  4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
00740  6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
00741  4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
00742  6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
00743  4+11*8, 5+11*8, 4+12*8, 5+12*8,
00744  6+11*8, 7+11*8, 6+12*8, 7+12*8,
00745  4+13*8, 5+13*8, 4+14*8, 5+14*8,
00746  6+13*8, 7+13*8, 6+14*8, 7+14*8,
00747  0+ 0*8, 0+ 5*8, 0+10*8
00748 };
00749 
00750 static av_always_inline uint32_t pack16to32(int a, int b){
00751 #if HAVE_BIGENDIAN
00752    return (b&0xFFFF) + (a<<16);
00753 #else
00754    return (a&0xFFFF) + (b<<16);
00755 #endif
00756 }
00757 
00758 static av_always_inline uint16_t pack8to16(int a, int b){
00759 #if HAVE_BIGENDIAN
00760    return (b&0xFF) + (a<<8);
00761 #else
00762    return (a&0xFF) + (b<<8);
00763 #endif
00764 }
00765 
00769 static inline int get_chroma_qp(H264Context *h, int t, int qscale){
00770     return h->pps.chroma_qp_table[t][qscale];
00771 }
00772 
00773 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my);
00774 
00775 static void fill_decode_neighbors(H264Context *h, int mb_type){
00776     MpegEncContext * const s = &h->s;
00777     const int mb_xy= h->mb_xy;
00778     int topleft_xy, top_xy, topright_xy, left_xy[2];
00779     static const uint8_t left_block_options[4][32]={
00780         {0,1,2,3,7,10,8,11,3+0*4, 3+1*4, 3+2*4, 3+3*4, 1+4*4, 1+8*4, 1+5*4, 1+9*4},
00781         {2,2,3,3,8,11,8,11,3+2*4, 3+2*4, 3+3*4, 3+3*4, 1+5*4, 1+9*4, 1+5*4, 1+9*4},
00782         {0,0,1,1,7,10,7,10,3+0*4, 3+0*4, 3+1*4, 3+1*4, 1+4*4, 1+8*4, 1+4*4, 1+8*4},
00783         {0,2,0,2,7,10,7,10,3+0*4, 3+2*4, 3+0*4, 3+2*4, 1+4*4, 1+8*4, 1+4*4, 1+8*4}
00784     };
00785 
00786     h->topleft_partition= -1;
00787 
00788     top_xy     = mb_xy  - (s->mb_stride << MB_FIELD);
00789 
00790     /* Wow, what a mess, why didn't they simplify the interlacing & intra
00791      * stuff, I can't imagine that these complex rules are worth it. */
00792 
00793     topleft_xy = top_xy - 1;
00794     topright_xy= top_xy + 1;
00795     left_xy[1] = left_xy[0] = mb_xy-1;
00796     h->left_block = left_block_options[0];
00797     if(FRAME_MBAFF){
00798         const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
00799         const int curr_mb_field_flag     = IS_INTERLACED(mb_type);
00800         if(s->mb_y&1){
00801             if (left_mb_field_flag != curr_mb_field_flag) {
00802                 left_xy[1] = left_xy[0] = mb_xy - s->mb_stride - 1;
00803                 if (curr_mb_field_flag) {
00804                     left_xy[1] += s->mb_stride;
00805                     h->left_block = left_block_options[3];
00806                 } else {
00807                     topleft_xy += s->mb_stride;
00808                     // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
00809                     h->topleft_partition = 0;
00810                     h->left_block = left_block_options[1];
00811                 }
00812             }
00813         }else{
00814             if(curr_mb_field_flag){
00815                 topleft_xy  += s->mb_stride & (((s->current_picture.mb_type[top_xy - 1]>>7)&1)-1);
00816                 topright_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy + 1]>>7)&1)-1);
00817                 top_xy      += s->mb_stride & (((s->current_picture.mb_type[top_xy    ]>>7)&1)-1);
00818             }
00819             if (left_mb_field_flag != curr_mb_field_flag) {
00820                 if (curr_mb_field_flag) {
00821                     left_xy[1] += s->mb_stride;
00822                     h->left_block = left_block_options[3];
00823                 } else {
00824                     h->left_block = left_block_options[2];
00825                 }
00826             }
00827         }
00828     }
00829 
00830     h->topleft_mb_xy = topleft_xy;
00831     h->top_mb_xy     = top_xy;
00832     h->topright_mb_xy= topright_xy;
00833     h->left_mb_xy[0] = left_xy[0];
00834     h->left_mb_xy[1] = left_xy[1];
00835     //FIXME do we need all in the context?
00836 
00837     h->topleft_type = s->current_picture.mb_type[topleft_xy] ;
00838     h->top_type     = s->current_picture.mb_type[top_xy]     ;
00839     h->topright_type= s->current_picture.mb_type[topright_xy];
00840     h->left_type[0] = s->current_picture.mb_type[left_xy[0]] ;
00841     h->left_type[1] = s->current_picture.mb_type[left_xy[1]] ;
00842 
00843     if(FMO){
00844     if(h->slice_table[topleft_xy ] != h->slice_num) h->topleft_type = 0;
00845     if(h->slice_table[top_xy     ] != h->slice_num) h->top_type     = 0;
00846     if(h->slice_table[left_xy[0] ] != h->slice_num) h->left_type[0] = h->left_type[1] = 0;
00847     }else{
00848         if(h->slice_table[topleft_xy ] != h->slice_num){
00849             h->topleft_type = 0;
00850             if(h->slice_table[top_xy     ] != h->slice_num) h->top_type     = 0;
00851             if(h->slice_table[left_xy[0] ] != h->slice_num) h->left_type[0] = h->left_type[1] = 0;
00852         }
00853     }
00854     if(h->slice_table[topright_xy] != h->slice_num) h->topright_type= 0;
00855 }
00856 
00857 static void fill_decode_caches(H264Context *h, int mb_type){
00858     MpegEncContext * const s = &h->s;
00859     int topleft_xy, top_xy, topright_xy, left_xy[2];
00860     int topleft_type, top_type, topright_type, left_type[2];
00861     const uint8_t * left_block= h->left_block;
00862     int i;
00863 
00864     topleft_xy   = h->topleft_mb_xy ;
00865     top_xy       = h->top_mb_xy     ;
00866     topright_xy  = h->topright_mb_xy;
00867     left_xy[0]   = h->left_mb_xy[0] ;
00868     left_xy[1]   = h->left_mb_xy[1] ;
00869     topleft_type = h->topleft_type  ;
00870     top_type     = h->top_type      ;
00871     topright_type= h->topright_type ;
00872     left_type[0] = h->left_type[0]  ;
00873     left_type[1] = h->left_type[1]  ;
00874 
00875     if(!IS_SKIP(mb_type)){
00876         if(IS_INTRA(mb_type)){
00877             int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
00878             h->topleft_samples_available=
00879             h->top_samples_available=
00880             h->left_samples_available= 0xFFFF;
00881             h->topright_samples_available= 0xEEEA;
00882 
00883             if(!(top_type & type_mask)){
00884                 h->topleft_samples_available= 0xB3FF;
00885                 h->top_samples_available= 0x33FF;
00886                 h->topright_samples_available= 0x26EA;
00887             }
00888             if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
00889                 if(IS_INTERLACED(mb_type)){
00890                     if(!(left_type[0] & type_mask)){
00891                         h->topleft_samples_available&= 0xDFFF;
00892                         h->left_samples_available&= 0x5FFF;
00893                     }
00894                     if(!(left_type[1] & type_mask)){
00895                         h->topleft_samples_available&= 0xFF5F;
00896                         h->left_samples_available&= 0xFF5F;
00897                     }
00898                 }else{
00899                     int left_typei = s->current_picture.mb_type[left_xy[0] + s->mb_stride];
00900 
00901                     assert(left_xy[0] == left_xy[1]);
00902                     if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
00903                         h->topleft_samples_available&= 0xDF5F;
00904                         h->left_samples_available&= 0x5F5F;
00905                     }
00906                 }
00907             }else{
00908                 if(!(left_type[0] & type_mask)){
00909                     h->topleft_samples_available&= 0xDF5F;
00910                     h->left_samples_available&= 0x5F5F;
00911                 }
00912             }
00913 
00914             if(!(topleft_type & type_mask))
00915                 h->topleft_samples_available&= 0x7FFF;
00916 
00917             if(!(topright_type & type_mask))
00918                 h->topright_samples_available&= 0xFBFF;
00919 
00920             if(IS_INTRA4x4(mb_type)){
00921                 if(IS_INTRA4x4(top_type)){
00922                     AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
00923                 }else{
00924                     h->intra4x4_pred_mode_cache[4+8*0]=
00925                     h->intra4x4_pred_mode_cache[5+8*0]=
00926                     h->intra4x4_pred_mode_cache[6+8*0]=
00927                     h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask);
00928                 }
00929                 for(i=0; i<2; i++){
00930                     if(IS_INTRA4x4(left_type[i])){
00931                         int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[i]];
00932                         h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]];
00933                         h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]];
00934                     }else{
00935                         h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
00936                         h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[i] & type_mask);
00937                     }
00938                 }
00939             }
00940         }
00941 
00942 
00943 /*
00944 0 . T T. T T T T
00945 1 L . .L . . . .
00946 2 L . .L . . . .
00947 3 . T TL . . . .
00948 4 L . .L . . . .
00949 5 L . .. . . . .
00950 */
00951 //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
00952     if(top_type){
00953         AV_COPY32(&h->non_zero_count_cache[4+8* 0], &h->non_zero_count[top_xy][4*3]);
00954         if(CHROMA444){
00955             AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 7]);
00956             AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4*11]);
00957         }else{
00958             AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 5]);
00959             AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4* 9]);
00960         }
00961     }else{
00962         uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
00963         AV_WN32A(&h->non_zero_count_cache[4+8* 0], top_empty);
00964         AV_WN32A(&h->non_zero_count_cache[4+8* 5], top_empty);
00965         AV_WN32A(&h->non_zero_count_cache[4+8*10], top_empty);
00966     }
00967 
00968     for (i=0; i<2; i++) {
00969         if(left_type[i]){
00970             h->non_zero_count_cache[3+8* 1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]];
00971             h->non_zero_count_cache[3+8* 2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]];
00972             if(CHROMA444){
00973                 h->non_zero_count_cache[3+8* 6 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+4*4];
00974                 h->non_zero_count_cache[3+8* 7 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+4*4];
00975                 h->non_zero_count_cache[3+8*11 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+8*4];
00976                 h->non_zero_count_cache[3+8*12 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+8*4];
00977             }else{
00978                 h->non_zero_count_cache[3+8* 6 +   8*i]= h->non_zero_count[left_xy[i]][left_block[8+4+2*i]];
00979                 h->non_zero_count_cache[3+8*11 +   8*i]= h->non_zero_count[left_xy[i]][left_block[8+5+2*i]];
00980             }
00981         }else{
00982             h->non_zero_count_cache[3+8* 1 + 2*8*i]=
00983             h->non_zero_count_cache[3+8* 2 + 2*8*i]=
00984             h->non_zero_count_cache[3+8* 6 + 2*8*i]=
00985             h->non_zero_count_cache[3+8* 7 + 2*8*i]=
00986             h->non_zero_count_cache[3+8*11 + 2*8*i]=
00987             h->non_zero_count_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;
00988         }
00989     }
00990 
00991     if( CABAC ) {
00992         // top_cbp
00993         if(top_type) {
00994             h->top_cbp = h->cbp_table[top_xy];
00995         } else {
00996             h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
00997         }
00998         // left_cbp
00999         if (left_type[0]) {
01000             h->left_cbp =   (h->cbp_table[left_xy[0]] & 0x7F0)
01001                         |  ((h->cbp_table[left_xy[0]]>>(left_block[0]&(~1)))&2)
01002                         | (((h->cbp_table[left_xy[1]]>>(left_block[2]&(~1)))&2) << 2);
01003         } else {
01004             h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
01005         }
01006     }
01007     }
01008 
01009     if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){
01010         int list;
01011         for(list=0; list<h->list_count; list++){
01012             if(!USES_LIST(mb_type, list)){
01013                 /*if(!h->mv_cache_clean[list]){
01014                     memset(h->mv_cache [list],  0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
01015                     memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
01016                     h->mv_cache_clean[list]= 1;
01017                 }*/
01018                 continue;
01019             }
01020             assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
01021 
01022             h->mv_cache_clean[list]= 0;
01023 
01024             if(USES_LIST(top_type, list)){
01025                 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
01026                 AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
01027                     h->ref_cache[list][scan8[0] + 0 - 1*8]=
01028                     h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 2];
01029                     h->ref_cache[list][scan8[0] + 2 - 1*8]=
01030                     h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 3];
01031             }else{
01032                 AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
01033                 AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);
01034             }
01035 
01036             if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){
01037             for(i=0; i<2; i++){
01038                 int cache_idx = scan8[0] - 1 + i*2*8;
01039                 if(USES_LIST(left_type[i], list)){
01040                     const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
01041                     const int b8_xy= 4*left_xy[i] + 1;
01042                     AV_COPY32(h->mv_cache[list][cache_idx  ], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]]);
01043                     AV_COPY32(h->mv_cache[list][cache_idx+8], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]]);
01044                         h->ref_cache[list][cache_idx  ]= s->current_picture.ref_index[list][b8_xy + (left_block[0+i*2]&~1)];
01045                         h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + (left_block[1+i*2]&~1)];
01046                 }else{
01047                     AV_ZERO32(h->mv_cache [list][cache_idx  ]);
01048                     AV_ZERO32(h->mv_cache [list][cache_idx+8]);
01049                     h->ref_cache[list][cache_idx  ]=
01050                     h->ref_cache[list][cache_idx+8]= (left_type[i]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;
01051                 }
01052             }
01053             }else{
01054                 if(USES_LIST(left_type[0], list)){
01055                     const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
01056                     const int b8_xy= 4*left_xy[0] + 1;
01057                     AV_COPY32(h->mv_cache[list][scan8[0] - 1], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]]);
01058                     h->ref_cache[list][scan8[0] - 1]= s->current_picture.ref_index[list][b8_xy + (left_block[0]&~1)];
01059                 }else{
01060                     AV_ZERO32(h->mv_cache [list][scan8[0] - 1]);
01061                     h->ref_cache[list][scan8[0] - 1]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
01062                 }
01063             }
01064 
01065             if(USES_LIST(topright_type, list)){
01066                 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
01067                 AV_COPY32(h->mv_cache[list][scan8[0] + 4 - 1*8], s->current_picture.motion_val[list][b_xy]);
01068                 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][4*topright_xy + 2];
01069             }else{
01070                 AV_ZERO32(h->mv_cache [list][scan8[0] + 4 - 1*8]);
01071                 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
01072             }
01073             if(h->ref_cache[list][scan8[0] + 2 - 1*8] < 0 || h->ref_cache[list][scan8[0] + 4 - 1*8] < 0){
01074                 if(USES_LIST(topleft_type, list)){
01075                     const int b_xy = h->mb2b_xy [topleft_xy] + 3 + h->b_stride + (h->topleft_partition & 2*h->b_stride);
01076                     const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2);
01077                     AV_COPY32(h->mv_cache[list][scan8[0] - 1 - 1*8], s->current_picture.motion_val[list][b_xy]);
01078                     h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
01079                 }else{
01080                     AV_ZERO32(h->mv_cache[list][scan8[0] - 1 - 1*8]);
01081                     h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
01082                 }
01083             }
01084 
01085             if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
01086                 continue;
01087 
01088             if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))) {
01089             h->ref_cache[list][scan8[4 ]] =
01090             h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
01091             AV_ZERO32(h->mv_cache [list][scan8[4 ]]);
01092             AV_ZERO32(h->mv_cache [list][scan8[12]]);
01093 
01094             if( CABAC ) {
01095                 /* XXX beurk, Load mvd */
01096                 if(USES_LIST(top_type, list)){
01097                     const int b_xy= h->mb2br_xy[top_xy];
01098                     AV_COPY64(h->mvd_cache[list][scan8[0] + 0 - 1*8], h->mvd_table[list][b_xy + 0]);
01099                 }else{
01100                     AV_ZERO64(h->mvd_cache[list][scan8[0] + 0 - 1*8]);
01101                 }
01102                 if(USES_LIST(left_type[0], list)){
01103                     const int b_xy= h->mb2br_xy[left_xy[0]] + 6;
01104                     AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 0*8], h->mvd_table[list][b_xy - left_block[0]]);
01105                     AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 1*8], h->mvd_table[list][b_xy - left_block[1]]);
01106                 }else{
01107                     AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 0*8]);
01108                     AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 1*8]);
01109                 }
01110                 if(USES_LIST(left_type[1], list)){
01111                     const int b_xy= h->mb2br_xy[left_xy[1]] + 6;
01112                     AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 2*8], h->mvd_table[list][b_xy - left_block[2]]);
01113                     AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 3*8], h->mvd_table[list][b_xy - left_block[3]]);
01114                 }else{
01115                     AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 2*8]);
01116                     AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 3*8]);
01117                 }
01118                 AV_ZERO16(h->mvd_cache [list][scan8[4 ]]);
01119                 AV_ZERO16(h->mvd_cache [list][scan8[12]]);
01120                 if(h->slice_type_nos == AV_PICTURE_TYPE_B){
01121                     fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, MB_TYPE_16x16>>1, 1);
01122 
01123                     if(IS_DIRECT(top_type)){
01124                         AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));
01125                     }else if(IS_8X8(top_type)){
01126                         int b8_xy = 4*top_xy;
01127                         h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy + 2];
01128                         h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 3];
01129                     }else{
01130                         AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101*(MB_TYPE_16x16>>1));
01131                     }
01132 
01133                     if(IS_DIRECT(left_type[0]))
01134                         h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_DIRECT2>>1;
01135                     else if(IS_8X8(left_type[0]))
01136                         h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[4*left_xy[0] + 1 + (left_block[0]&~1)];
01137                     else
01138                         h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_16x16>>1;
01139 
01140                     if(IS_DIRECT(left_type[1]))
01141                         h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_DIRECT2>>1;
01142                     else if(IS_8X8(left_type[1]))
01143                         h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[4*left_xy[1] + 1 + (left_block[2]&~1)];
01144                     else
01145                         h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_16x16>>1;
01146                 }
01147             }
01148             }
01149             if(FRAME_MBAFF){
01150 #define MAP_MVS\
01151                     MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
01152                     MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
01153                     MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
01154                     MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
01155                     MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
01156                     MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
01157                     MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
01158                     MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
01159                     MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
01160                     MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
01161                 if(MB_FIELD){
01162 #define MAP_F2F(idx, mb_type)\
01163                     if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
01164                         h->ref_cache[list][idx] <<= 1;\
01165                         h->mv_cache[list][idx][1] /= 2;\
01166                         h->mvd_cache[list][idx][1] >>=1;\
01167                     }
01168                     MAP_MVS
01169 #undef MAP_F2F
01170                 }else{
01171 #define MAP_F2F(idx, mb_type)\
01172                     if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
01173                         h->ref_cache[list][idx] >>= 1;\
01174                         h->mv_cache[list][idx][1] <<= 1;\
01175                         h->mvd_cache[list][idx][1] <<= 1;\
01176                     }
01177                     MAP_MVS
01178 #undef MAP_F2F
01179                 }
01180             }
01181         }
01182     }
01183 
01184         h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
01185 }
01186 
01190 static inline int pred_intra_mode(H264Context *h, int n){
01191     const int index8= scan8[n];
01192     const int left= h->intra4x4_pred_mode_cache[index8 - 1];
01193     const int top = h->intra4x4_pred_mode_cache[index8 - 8];
01194     const int min= FFMIN(left, top);
01195 
01196     tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
01197 
01198     if(min<0) return DC_PRED;
01199     else      return min;
01200 }
01201 
01202 static inline void write_back_non_zero_count(H264Context *h){
01203     const int mb_xy= h->mb_xy;
01204 
01205     AV_COPY32(&h->non_zero_count[mb_xy][ 0], &h->non_zero_count_cache[4+8* 1]);
01206     AV_COPY32(&h->non_zero_count[mb_xy][ 4], &h->non_zero_count_cache[4+8* 2]);
01207     AV_COPY32(&h->non_zero_count[mb_xy][ 8], &h->non_zero_count_cache[4+8* 3]);
01208     AV_COPY32(&h->non_zero_count[mb_xy][12], &h->non_zero_count_cache[4+8* 4]);
01209     AV_COPY32(&h->non_zero_count[mb_xy][16], &h->non_zero_count_cache[4+8* 6]);
01210     AV_COPY32(&h->non_zero_count[mb_xy][20], &h->non_zero_count_cache[4+8* 7]);
01211     AV_COPY32(&h->non_zero_count[mb_xy][32], &h->non_zero_count_cache[4+8*11]);
01212     AV_COPY32(&h->non_zero_count[mb_xy][36], &h->non_zero_count_cache[4+8*12]);
01213 
01214     if(CHROMA444){
01215         AV_COPY32(&h->non_zero_count[mb_xy][24], &h->non_zero_count_cache[4+8* 8]);
01216         AV_COPY32(&h->non_zero_count[mb_xy][28], &h->non_zero_count_cache[4+8* 9]);
01217         AV_COPY32(&h->non_zero_count[mb_xy][40], &h->non_zero_count_cache[4+8*13]);
01218         AV_COPY32(&h->non_zero_count[mb_xy][44], &h->non_zero_count_cache[4+8*14]);
01219     }
01220 }
01221 
01222 static inline void write_back_motion(H264Context *h, int mb_type){
01223     MpegEncContext * const s = &h->s;
01224     const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; //try mb2b(8)_xy
01225     const int b8_xy= 4*h->mb_xy;
01226     int list;
01227 
01228     if(!USES_LIST(mb_type, 0))
01229         fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
01230 
01231     for(list=0; list<h->list_count; list++){
01232         int y, b_stride;
01233         int16_t (*mv_dst)[2];
01234         int16_t (*mv_src)[2];
01235 
01236         if(!USES_LIST(mb_type, list))
01237             continue;
01238 
01239         b_stride = h->b_stride;
01240         mv_dst   = &s->current_picture.motion_val[list][b_xy];
01241         mv_src   = &h->mv_cache[list][scan8[0]];
01242         for(y=0; y<4; y++){
01243             AV_COPY128(mv_dst + y*b_stride, mv_src + 8*y);
01244         }
01245         if( CABAC ) {
01246             uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8*h->mb_xy : h->mb2br_xy[h->mb_xy]];
01247             uint8_t (*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
01248             if(IS_SKIP(mb_type))
01249                 AV_ZERO128(mvd_dst);
01250             else{
01251             AV_COPY64(mvd_dst, mvd_src + 8*3);
01252                 AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8*0);
01253                 AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8*1);
01254                 AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8*2);
01255             }
01256         }
01257 
01258         {
01259             int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
01260             ref_index[0+0*2]= h->ref_cache[list][scan8[0]];
01261             ref_index[1+0*2]= h->ref_cache[list][scan8[4]];
01262             ref_index[0+1*2]= h->ref_cache[list][scan8[8]];
01263             ref_index[1+1*2]= h->ref_cache[list][scan8[12]];
01264         }
01265     }
01266 
01267     if(h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC){
01268         if(IS_8X8(mb_type)){
01269             uint8_t *direct_table = &h->direct_table[4*h->mb_xy];
01270             direct_table[1] = h->sub_mb_type[1]>>1;
01271             direct_table[2] = h->sub_mb_type[2]>>1;
01272             direct_table[3] = h->sub_mb_type[3]>>1;
01273         }
01274     }
01275 }
01276 
01277 static inline int get_dct8x8_allowed(H264Context *h){
01278     if(h->sps.direct_8x8_inference_flag)
01279         return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8                )*0x0001000100010001ULL));
01280     else
01281         return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
01282 }
01283 
01287 static void av_unused decode_mb_skip(H264Context *h){
01288     MpegEncContext * const s = &h->s;
01289     const int mb_xy= h->mb_xy;
01290     int mb_type=0;
01291 
01292     memset(h->non_zero_count[mb_xy], 0, 48);
01293 
01294     if(MB_FIELD)
01295         mb_type|= MB_TYPE_INTERLACED;
01296 
01297     if( h->slice_type_nos == AV_PICTURE_TYPE_B )
01298     {
01299         // just for fill_caches. pred_direct_motion will set the real mb_type
01300         mb_type|= MB_TYPE_L0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
01301         if(h->direct_spatial_mv_pred){
01302             fill_decode_neighbors(h, mb_type);
01303         fill_decode_caches(h, mb_type); //FIXME check what is needed and what not ...
01304         }
01305         ff_h264_pred_direct_motion(h, &mb_type);
01306         mb_type|= MB_TYPE_SKIP;
01307     }
01308     else
01309     {
01310         int mx, my;
01311         mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
01312 
01313         fill_decode_neighbors(h, mb_type);
01314         fill_decode_caches(h, mb_type); //FIXME check what is needed and what not ...
01315         pred_pskip_motion(h, &mx, &my);
01316         fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
01317         fill_rectangle(  h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
01318     }
01319 
01320     write_back_motion(h, mb_type);
01321     s->current_picture.mb_type[mb_xy]= mb_type;
01322     s->current_picture.qscale_table[mb_xy]= s->qscale;
01323     h->slice_table[ mb_xy ]= h->slice_num;
01324     h->prev_mb_skipped= 1;
01325 }
01326 
01327 #include "h264_mvpred.h" //For pred_pskip_motion()
01328 
01329 #endif /* AVCODEC_H264_H */

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1