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