• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

hvirtual/quicktime/ffmpeg/libavcodec/h264.c

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 library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  */
00020  
00027 #include "common.h"
00028 #include "dsputil.h"
00029 #include "avcodec.h"
00030 #include "mpegvideo.h"
00031 #include "h264data.h"
00032 #include "golomb.h"
00033 
00034 #include "cabac.h"
00035 
00036 #undef NDEBUG
00037 #include <assert.h>
00038 
00039 #define interlaced_dct interlaced_dct_is_a_bad_name
00040 #define mb_intra mb_intra_isnt_initalized_see_mb_type
00041 
00042 #define LUMA_DC_BLOCK_INDEX   25
00043 #define CHROMA_DC_BLOCK_INDEX 26
00044 
00045 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
00046 #define COEFF_TOKEN_VLC_BITS           8
00047 #define TOTAL_ZEROS_VLC_BITS           9
00048 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
00049 #define RUN_VLC_BITS                   3
00050 #define RUN7_VLC_BITS                  6
00051 
00052 #define MAX_SPS_COUNT 32
00053 #define MAX_PPS_COUNT 256
00054 
00055 #define MAX_MMCO_COUNT 66
00056 
00060 typedef struct SPS{
00061     
00062     int profile_idc;
00063     int level_idc;
00064     int transform_bypass;              
00065     int log2_max_frame_num;            
00066     int poc_type;                      
00067     int log2_max_poc_lsb;              
00068     int delta_pic_order_always_zero_flag;
00069     int offset_for_non_ref_pic;
00070     int offset_for_top_to_bottom_field;
00071     int poc_cycle_length;              
00072     int ref_frame_count;               
00073     int gaps_in_frame_num_allowed_flag;
00074     int mb_width;                      
00075     int mb_height;                     
00076     int frame_mbs_only_flag;
00077     int mb_aff;                        
00078     int direct_8x8_inference_flag;
00079     int crop;                   
00080     int crop_left;              
00081     int crop_right;             
00082     int crop_top;               
00083     int crop_bottom;            
00084     int vui_parameters_present_flag;
00085     AVRational sar;
00086     int timing_info_present_flag;
00087     uint32_t num_units_in_tick;
00088     uint32_t time_scale;
00089     int fixed_frame_rate_flag;
00090     short offset_for_ref_frame[256]; //FIXME dyn aloc?
00091     int bitstream_restriction_flag;
00092     int num_reorder_frames;
00093 }SPS;
00094 
00098 typedef struct PPS{
00099     int sps_id;
00100     int cabac;                  
00101     int pic_order_present;      
00102     int slice_group_count;      
00103     int mb_slice_group_map_type;
00104     int ref_count[2];           
00105     int weighted_pred;          
00106     int weighted_bipred_idc;
00107     int init_qp;                
00108     int init_qs;                
00109     int chroma_qp_index_offset;
00110     int deblocking_filter_parameters_present; 
00111     int constrained_intra_pred; 
00112     int redundant_pic_cnt_present; 
00113     int transform_8x8_mode;     
00114 }PPS;
00115 
00119 typedef enum MMCOOpcode{
00120     MMCO_END=0,
00121     MMCO_SHORT2UNUSED,
00122     MMCO_LONG2UNUSED,
00123     MMCO_SHORT2LONG,
00124     MMCO_SET_MAX_LONG,
00125     MMCO_RESET, 
00126     MMCO_LONG,
00127 } MMCOOpcode;
00128 
00132 typedef struct MMCO{
00133     MMCOOpcode opcode;
00134     int short_frame_num;
00135     int long_index;
00136 } MMCO;
00137 
00141 typedef struct H264Context{
00142     MpegEncContext s;
00143     int nal_ref_idc;    
00144     int nal_unit_type;
00145 #define NAL_SLICE               1
00146 #define NAL_DPA                 2
00147 #define NAL_DPB                 3
00148 #define NAL_DPC                 4
00149 #define NAL_IDR_SLICE           5
00150 #define NAL_SEI                 6
00151 #define NAL_SPS                 7
00152 #define NAL_PPS                 8
00153 #define NAL_PICTURE_DELIMITER   9
00154 #define NAL_FILTER_DATA         10
00155     uint8_t *rbsp_buffer;
00156     int rbsp_buffer_size;
00157 
00161     int is_avc; 
00162     int got_avcC; 
00163     int nal_length_size; 
00164 
00165     int chroma_qp; //QPc
00166 
00167     int prev_mb_skipped; //FIXME remove (IMHO not used)
00168 
00169     //prediction stuff
00170     int chroma_pred_mode;
00171     int intra16x16_pred_mode;
00172 
00173     int top_mb_xy;
00174     int left_mb_xy[2];
00175     
00176     int8_t intra4x4_pred_mode_cache[5*8];
00177     int8_t (*intra4x4_pred_mode)[8];
00178     void (*pred4x4  [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
00179     void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
00180     void (*pred8x8  [4+3])(uint8_t *src, int stride);
00181     void (*pred16x16[4+3])(uint8_t *src, int stride);
00182     unsigned int topleft_samples_available;
00183     unsigned int top_samples_available;
00184     unsigned int topright_samples_available;
00185     unsigned int left_samples_available;
00186     uint8_t (*top_borders[2])[16+2*8];
00187     uint8_t left_border[2*(17+2*9)];
00188 
00193     uint8_t non_zero_count_cache[6*8] __align8;
00194     uint8_t (*non_zero_count)[16];
00195 
00199     int16_t mv_cache[2][5*8][2] __align8;
00200     int8_t ref_cache[2][5*8] __align8;
00201 #define LIST_NOT_USED -1 //FIXME rename?
00202 #define PART_NOT_AVAILABLE -2
00203     
00207     int mv_cache_clean[2];
00208 
00212     int neighbor_transform_size;
00213 
00218     int block_offset[2*(16+8)];
00219     
00220     uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
00221     uint32_t *mb2b8_xy;
00222     int b_stride; //FIXME use s->b4_stride
00223     int b8_stride;
00224 
00225     int halfpel_flag;
00226     int thirdpel_flag;
00227 
00228     int unknown_svq3_flag;
00229     int next_slice_index;
00230 
00231     SPS sps_buffer[MAX_SPS_COUNT];
00232     SPS sps; 
00233     
00234     PPS pps_buffer[MAX_PPS_COUNT];
00238     PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
00239 
00240     uint16_t (*dequant4_coeff)[16]; // FIXME quant matrices should be per SPS or PPS
00241     uint16_t (*dequant8_coeff)[64];
00242 
00243     int slice_num;
00244     uint8_t *slice_table_base;
00245     uint8_t *slice_table;      
00246     int slice_type;
00247     int slice_type_fixed;
00248     
00249     //interlacing specific flags
00250     int mb_aff_frame;
00251     int mb_field_decoding_flag;
00252     
00253     int sub_mb_type[4];
00254     
00255     //POC stuff
00256     int poc_lsb;
00257     int poc_msb;
00258     int delta_poc_bottom;
00259     int delta_poc[2];
00260     int frame_num;
00261     int prev_poc_msb;             
00262     int prev_poc_lsb;             
00263     int frame_num_offset;         
00264     int prev_frame_num_offset;    
00265     int prev_frame_num;           
00266 
00270     int curr_pic_num;
00271     
00275     int max_pic_num;
00276 
00277     //Weighted pred stuff
00278     int use_weight;
00279     int use_weight_chroma;
00280     int luma_log2_weight_denom;
00281     int chroma_log2_weight_denom;
00282     int luma_weight[2][16];
00283     int luma_offset[2][16];
00284     int chroma_weight[2][16][2];
00285     int chroma_offset[2][16][2];
00286     int implicit_weight[16][16];
00287    
00288     //deblock
00289     int deblocking_filter;         
00290     int slice_alpha_c0_offset;
00291     int slice_beta_offset;
00292      
00293     int redundant_pic_count;
00294     
00295     int direct_spatial_mv_pred;
00296     int dist_scale_factor[16];
00297     int map_col_to_list0[2][16];
00298 
00302     int ref_count[2];// FIXME split for AFF
00303     Picture *short_ref[32];
00304     Picture *long_ref[32];
00305     Picture default_ref_list[2][32];
00306     Picture ref_list[2][32]; //FIXME size?
00307     Picture field_ref_list[2][32]; //FIXME size?
00308     Picture *delayed_pic[16]; //FIXME size?
00309     Picture *delayed_output_pic;
00310     
00314     MMCO mmco[MAX_MMCO_COUNT];
00315     int mmco_index;
00316     
00317     int long_ref_count;  
00318     int short_ref_count; 
00319     
00320     //data partitioning
00321     GetBitContext intra_gb;
00322     GetBitContext inter_gb;
00323     GetBitContext *intra_gb_ptr;
00324     GetBitContext *inter_gb_ptr;
00325     
00326     DCTELEM mb[16*24] __align8;
00327 
00331     CABACContext cabac;
00332     uint8_t      cabac_state[460];
00333     int          cabac_init_idc;
00334 
00335     /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
00336     uint16_t     *cbp_table;
00337     int top_cbp;
00338     int left_cbp;
00339     /* chroma_pred_mode for i4x4 or i16x16, else 0 */
00340     uint8_t     *chroma_pred_mode_table;
00341     int         last_qscale_diff;
00342     int16_t     (*mvd_table[2])[2];
00343     int16_t     mvd_cache[2][5*8][2] __align8;
00344     uint8_t     *direct_table;
00345     uint8_t     direct_cache[5*8];
00346 
00347     uint8_t zigzag_scan[16];
00348     uint8_t field_scan[16];
00349     const uint8_t *zigzag_scan_q0;
00350     const uint8_t *field_scan_q0;
00351 }H264Context;
00352 
00353 static VLC coeff_token_vlc[4];
00354 static VLC chroma_dc_coeff_token_vlc;
00355 
00356 static VLC total_zeros_vlc[15];
00357 static VLC chroma_dc_total_zeros_vlc[3];
00358 
00359 static VLC run_vlc[6];
00360 static VLC run7_vlc;
00361 
00362 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
00363 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
00364 static void 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);
00365 
00366 static inline uint32_t pack16to32(int a, int b){
00367 #ifdef WORDS_BIGENDIAN
00368    return (b&0xFFFF) + (a<<16);
00369 #else
00370    return (a&0xFFFF) + (b<<16);
00371 #endif
00372 }
00373 
00380 static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
00381     uint8_t *p= (uint8_t*)vp;
00382     assert(size==1 || size==4);
00383     
00384     w      *= size;
00385     stride *= size;
00386     
00387     assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
00388     assert((stride&(w-1))==0);
00389 //FIXME check what gcc generates for 64 bit on x86 and possibly write a 32 bit ver of it
00390     if(w==2 && h==2){
00391         *(uint16_t*)(p + 0)=
00392         *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
00393     }else if(w==2 && h==4){
00394         *(uint16_t*)(p + 0*stride)=
00395         *(uint16_t*)(p + 1*stride)=
00396         *(uint16_t*)(p + 2*stride)=
00397         *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
00398     }else if(w==4 && h==1){
00399         *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
00400     }else if(w==4 && h==2){
00401         *(uint32_t*)(p + 0*stride)=
00402         *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
00403     }else if(w==4 && h==4){
00404         *(uint32_t*)(p + 0*stride)=
00405         *(uint32_t*)(p + 1*stride)=
00406         *(uint32_t*)(p + 2*stride)=
00407         *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
00408     }else if(w==8 && h==1){
00409         *(uint32_t*)(p + 0)=
00410         *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
00411     }else if(w==8 && h==2){
00412         *(uint32_t*)(p + 0 + 0*stride)=
00413         *(uint32_t*)(p + 4 + 0*stride)=
00414         *(uint32_t*)(p + 0 + 1*stride)=
00415         *(uint32_t*)(p + 4 + 1*stride)=  size==4 ? val : val*0x01010101;
00416     }else if(w==8 && h==4){
00417         *(uint64_t*)(p + 0*stride)=
00418         *(uint64_t*)(p + 1*stride)=
00419         *(uint64_t*)(p + 2*stride)=
00420         *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
00421     }else if(w==16 && h==2){
00422         *(uint64_t*)(p + 0+0*stride)=
00423         *(uint64_t*)(p + 8+0*stride)=
00424         *(uint64_t*)(p + 0+1*stride)=
00425         *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
00426     }else if(w==16 && h==4){
00427         *(uint64_t*)(p + 0+0*stride)=
00428         *(uint64_t*)(p + 8+0*stride)=
00429         *(uint64_t*)(p + 0+1*stride)=
00430         *(uint64_t*)(p + 8+1*stride)=
00431         *(uint64_t*)(p + 0+2*stride)=
00432         *(uint64_t*)(p + 8+2*stride)=
00433         *(uint64_t*)(p + 0+3*stride)=
00434         *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
00435     }else
00436         assert(0);
00437 }
00438 
00439 static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){
00440     MpegEncContext * const s = &h->s;
00441     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00442     int topleft_xy, top_xy, topright_xy, left_xy[2];
00443     int topleft_type, top_type, topright_type, left_type[2];
00444     int left_block[8];
00445     int i;
00446 
00447     //FIXME deblocking can skip fill_caches much of the time with multiple slices too.
00448     // the actual condition is whether we're on the edge of a slice,
00449     // and even then the intra and nnz parts are unnecessary.
00450     if(for_deblock && h->slice_num == 1)
00451         return;
00452 
00453     //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it 
00454     
00455     top_xy     = mb_xy  - s->mb_stride;
00456     topleft_xy = top_xy - 1;
00457     topright_xy= top_xy + 1;
00458     left_xy[1] = left_xy[0] = mb_xy-1;
00459     left_block[0]= 0;
00460     left_block[1]= 1;
00461     left_block[2]= 2;
00462     left_block[3]= 3;
00463     left_block[4]= 7;
00464     left_block[5]= 10;
00465     left_block[6]= 8;
00466     left_block[7]= 11;
00467     if(h->mb_aff_frame){
00468         const int pair_xy          = s->mb_x     + (s->mb_y & ~1)*s->mb_stride;
00469         const int top_pair_xy      = pair_xy     - s->mb_stride;
00470         const int topleft_pair_xy  = top_pair_xy - 1;
00471         const int topright_pair_xy = top_pair_xy + 1;
00472         const int topleft_mb_frame_flag  = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
00473         const int top_mb_frame_flag      = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
00474         const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
00475         const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
00476         const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
00477         const int bottom = (s->mb_y & 1);
00478         tprintf("fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
00479         if (bottom
00480                 ? !curr_mb_frame_flag // bottom macroblock
00481                 : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
00482                 ) {
00483             top_xy -= s->mb_stride;
00484         }
00485         if (bottom
00486                 ? !curr_mb_frame_flag // bottom macroblock
00487                 : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
00488                 ) {
00489             topleft_xy -= s->mb_stride;
00490         }
00491         if (bottom
00492                 ? !curr_mb_frame_flag // bottom macroblock
00493                 : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
00494                 ) {
00495             topright_xy -= s->mb_stride;
00496         }
00497         if (left_mb_frame_flag != curr_mb_frame_flag) {
00498             left_xy[1] = left_xy[0] = pair_xy - 1;
00499             if (curr_mb_frame_flag) {
00500                 if (bottom) {
00501                     left_block[0]= 2;
00502                     left_block[1]= 2;
00503                     left_block[2]= 3;
00504                     left_block[3]= 3;
00505                     left_block[4]= 8;
00506                     left_block[5]= 11;
00507                     left_block[6]= 8;
00508                     left_block[7]= 11;
00509                 } else {
00510                     left_block[0]= 0;
00511                     left_block[1]= 0;
00512                     left_block[2]= 1;
00513                     left_block[3]= 1;
00514                     left_block[4]= 7;
00515                     left_block[5]= 10;
00516                     left_block[6]= 7;
00517                     left_block[7]= 10;
00518                 }
00519             } else {
00520                 left_xy[1] += s->mb_stride;
00521                 //left_block[0]= 0;
00522                 left_block[1]= 2;
00523                 left_block[2]= 0;
00524                 left_block[3]= 2;
00525                 //left_block[4]= 7;
00526                 left_block[5]= 10;
00527                 left_block[6]= 7;
00528                 left_block[7]= 10;
00529             }
00530         }
00531     }
00532 
00533     h->top_mb_xy = top_xy;
00534     h->left_mb_xy[0] = left_xy[0];
00535     h->left_mb_xy[1] = left_xy[1];
00536     if(for_deblock){
00537         topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
00538         top_type     = h->slice_table[top_xy     ] < 255 ? s->current_picture.mb_type[top_xy]     : 0;
00539         topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
00540         left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
00541         left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
00542     }else{
00543         topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
00544         top_type     = h->slice_table[top_xy     ] == h->slice_num ? s->current_picture.mb_type[top_xy]     : 0;
00545         topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
00546         left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
00547         left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
00548     }
00549 
00550     if(IS_INTRA(mb_type)){
00551         h->topleft_samples_available= 
00552         h->top_samples_available= 
00553         h->left_samples_available= 0xFFFF;
00554         h->topright_samples_available= 0xEEEA;
00555 
00556         if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
00557             h->topleft_samples_available= 0xB3FF;
00558             h->top_samples_available= 0x33FF;
00559             h->topright_samples_available= 0x26EA;
00560         }
00561         for(i=0; i<2; i++){
00562             if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
00563                 h->topleft_samples_available&= 0xDF5F;
00564                 h->left_samples_available&= 0x5F5F;
00565             }
00566         }
00567         
00568         if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
00569             h->topleft_samples_available&= 0x7FFF;
00570         
00571         if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
00572             h->topright_samples_available&= 0xFBFF;
00573     
00574         if(IS_INTRA4x4(mb_type)){
00575             if(IS_INTRA4x4(top_type)){
00576                 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
00577                 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
00578                 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
00579                 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
00580             }else{
00581                 int pred;
00582                 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
00583                     pred= -1;
00584                 else{
00585                     pred= 2;
00586                 }
00587                 h->intra4x4_pred_mode_cache[4+8*0]=
00588                 h->intra4x4_pred_mode_cache[5+8*0]=
00589                 h->intra4x4_pred_mode_cache[6+8*0]=
00590                 h->intra4x4_pred_mode_cache[7+8*0]= pred;
00591             }
00592             for(i=0; i<2; i++){
00593                 if(IS_INTRA4x4(left_type[i])){
00594                     h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
00595                     h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
00596                 }else{
00597                     int pred;
00598                     if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
00599                         pred= -1;
00600                     else{
00601                         pred= 2;
00602                     }
00603                     h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
00604                     h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
00605                 }
00606             }
00607         }
00608     }
00609     
00610     
00611 /*
00612 0 . T T. T T T T 
00613 1 L . .L . . . . 
00614 2 L . .L . . . . 
00615 3 . T TL . . . . 
00616 4 L . .L . . . . 
00617 5 L . .. . . . . 
00618 */
00619 //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
00620     if(top_type){
00621         h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
00622         h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
00623         h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
00624         h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
00625     
00626         h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
00627         h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
00628     
00629         h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
00630         h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
00631         
00632     }else{
00633         h->non_zero_count_cache[4+8*0]=      
00634         h->non_zero_count_cache[5+8*0]=
00635         h->non_zero_count_cache[6+8*0]=
00636         h->non_zero_count_cache[7+8*0]=
00637     
00638         h->non_zero_count_cache[1+8*0]=
00639         h->non_zero_count_cache[2+8*0]=
00640     
00641         h->non_zero_count_cache[1+8*3]=
00642         h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
00643         
00644     }
00645 
00646     for (i=0; i<2; i++) {
00647         if(left_type[i]){
00648             h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
00649             h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
00650             h->non_zero_count_cache[0+8*1 +   8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
00651             h->non_zero_count_cache[0+8*4 +   8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
00652         }else{
00653             h->non_zero_count_cache[3+8*1 + 2*8*i]= 
00654             h->non_zero_count_cache[3+8*2 + 2*8*i]= 
00655             h->non_zero_count_cache[0+8*1 +   8*i]= 
00656             h->non_zero_count_cache[0+8*4 +   8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
00657         }
00658     }
00659 
00660     if( h->pps.cabac ) {
00661         // top_cbp
00662         if(top_type) {
00663             h->top_cbp = h->cbp_table[top_xy];
00664         } else if(IS_INTRA(mb_type)) {
00665             h->top_cbp = 0x1C0;
00666         } else {
00667             h->top_cbp = 0;
00668         }
00669         // left_cbp
00670         if (left_type[0]) {
00671             h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
00672         } else if(IS_INTRA(mb_type)) {
00673             h->left_cbp = 0x1C0;
00674         } else {
00675             h->left_cbp = 0;
00676         }
00677         if (left_type[0]) {
00678             h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
00679         }
00680         if (left_type[1]) {
00681             h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
00682         }
00683     }
00684 
00685 #if 1
00686     //FIXME direct mb can skip much of this
00687     if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
00688         int list;
00689         for(list=0; list<1+(h->slice_type==B_TYPE); list++){
00690             if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
00691                 /*if(!h->mv_cache_clean[list]){
00692                     memset(h->mv_cache [list],  0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
00693                     memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
00694                     h->mv_cache_clean[list]= 1;
00695                 }*/
00696                 continue;
00697             }
00698             h->mv_cache_clean[list]= 0;
00699             
00700             if(IS_INTER(top_type)){
00701                 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
00702                 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
00703                 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
00704                 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
00705                 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
00706                 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
00707                 h->ref_cache[list][scan8[0] + 0 - 1*8]=
00708                 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
00709                 h->ref_cache[list][scan8[0] + 2 - 1*8]=
00710                 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
00711             }else{
00712                 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]= 
00713                 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]= 
00714                 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]= 
00715                 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
00716                 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
00717             }
00718 
00719             //FIXME unify cleanup or sth
00720             if(IS_INTER(left_type[0])){
00721                 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
00722                 const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
00723                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]];
00724                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1]];
00725                 h->ref_cache[list][scan8[0] - 1 + 0*8]= 
00726                 h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
00727             }else{
00728                 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
00729                 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
00730                 h->ref_cache[list][scan8[0] - 1 + 0*8]=
00731                 h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00732             }
00733             
00734             if(IS_INTER(left_type[1])){
00735                 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
00736                 const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
00737                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[2]];
00738                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[3]];
00739                 h->ref_cache[list][scan8[0] - 1 + 2*8]= 
00740                 h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
00741             }else{
00742                 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
00743                 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
00744                 h->ref_cache[list][scan8[0] - 1 + 2*8]=
00745                 h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00746                 assert((!left_type[0]) == (!left_type[1]));
00747             }
00748 
00749             if(for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred))
00750                 continue;
00751 
00752             if(IS_INTER(topleft_type)){
00753                 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
00754                 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
00755                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
00756                 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
00757             }else{
00758                 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
00759                 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00760             }
00761             
00762             if(IS_INTER(topright_type)){
00763                 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
00764                 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
00765                 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
00766                 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
00767             }else{
00768                 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
00769                 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00770             }
00771             
00772 
00773             h->ref_cache[list][scan8[5 ]+1] = 
00774             h->ref_cache[list][scan8[7 ]+1] = 
00775             h->ref_cache[list][scan8[13]+1] =  //FIXME remove past 3 (init somewhere else)
00776             h->ref_cache[list][scan8[4 ]] = 
00777             h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
00778             *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
00779             *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
00780             *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
00781             *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
00782             *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
00783 
00784             if( h->pps.cabac ) {
00785                 /* XXX beurk, Load mvd */
00786                 if(IS_INTER(topleft_type)){
00787                     const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
00788                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
00789                 }else{
00790                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
00791                 }
00792 
00793                 if(IS_INTER(top_type)){
00794                     const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
00795                     *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
00796                     *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
00797                     *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
00798                     *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
00799                 }else{
00800                     *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]= 
00801                     *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]= 
00802                     *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]= 
00803                     *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
00804                 }
00805                 if(IS_INTER(left_type[0])){
00806                     const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
00807                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
00808                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
00809                 }else{
00810                     *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
00811                     *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
00812                 }
00813                 if(IS_INTER(left_type[1])){
00814                     const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
00815                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
00816                     *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
00817                 }else{
00818                     *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
00819                     *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
00820                 }
00821                 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
00822                 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
00823                 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
00824                 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
00825                 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
00826 
00827                 if(h->slice_type == B_TYPE){
00828                     fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
00829 
00830                     if(IS_DIRECT(top_type)){
00831                         *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
00832                     }else if(IS_8X8(top_type)){
00833                         int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
00834                         h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
00835                         h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
00836                     }else{
00837                         *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
00838                     }
00839                     
00840                     //FIXME interlacing
00841                     if(IS_DIRECT(left_type[0])){
00842                         h->direct_cache[scan8[0] - 1 + 0*8]=
00843                         h->direct_cache[scan8[0] - 1 + 2*8]= 1;
00844                     }else if(IS_8X8(left_type[0])){
00845                         int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
00846                         h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
00847                         h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
00848                     }else{
00849                         h->direct_cache[scan8[0] - 1 + 0*8]=
00850                         h->direct_cache[scan8[0] - 1 + 2*8]= 0;
00851                     }
00852                 }
00853             }
00854         }
00855     }
00856 #endif
00857 
00858     h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
00859 }
00860 
00861 static inline void write_back_intra_pred_mode(H264Context *h){
00862     MpegEncContext * const s = &h->s;
00863     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00864 
00865     h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
00866     h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
00867     h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
00868     h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
00869     h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
00870     h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
00871     h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
00872 }
00873 
00877 static inline int check_intra4x4_pred_mode(H264Context *h){
00878     MpegEncContext * const s = &h->s;
00879     static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
00880     static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
00881     int i;
00882     
00883     if(!(h->top_samples_available&0x8000)){
00884         for(i=0; i<4; i++){
00885             int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
00886             if(status<0){
00887                 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
00888                 return -1;
00889             } else if(status){
00890                 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
00891             }
00892         }
00893     }
00894     
00895     if(!(h->left_samples_available&0x8000)){
00896         for(i=0; i<4; i++){
00897             int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
00898             if(status<0){
00899                 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
00900                 return -1;
00901             } else if(status){
00902                 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
00903             }
00904         }
00905     }
00906 
00907     return 0;
00908 } //FIXME cleanup like next
00909 
00913 static inline int check_intra_pred_mode(H264Context *h, int mode){
00914     MpegEncContext * const s = &h->s;
00915     static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
00916     static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
00917     
00918     if(mode < 0 || mode > 6) {
00919         av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
00920         return -1;
00921     }
00922     
00923     if(!(h->top_samples_available&0x8000)){
00924         mode= top[ mode ];
00925         if(mode<0){
00926             av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
00927             return -1;
00928         }
00929     }
00930     
00931     if(!(h->left_samples_available&0x8000)){
00932         mode= left[ mode ];
00933         if(mode<0){
00934             av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
00935             return -1;
00936         } 
00937     }
00938 
00939     return mode;
00940 }
00941 
00945 static inline int pred_intra_mode(H264Context *h, int n){
00946     const int index8= scan8[n];
00947     const int left= h->intra4x4_pred_mode_cache[index8 - 1];
00948     const int top = h->intra4x4_pred_mode_cache[index8 - 8];
00949     const int min= FFMIN(left, top);
00950 
00951     tprintf("mode:%d %d min:%d\n", left ,top, min);
00952 
00953     if(min<0) return DC_PRED;
00954     else      return min;
00955 }
00956 
00957 static inline void write_back_non_zero_count(H264Context *h){
00958     MpegEncContext * const s = &h->s;
00959     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00960 
00961     h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
00962     h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
00963     h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
00964     h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
00965     h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
00966     h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
00967     h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
00968     
00969     h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
00970     h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
00971     h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
00972 
00973     h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
00974     h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
00975     h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
00976 }
00977 
00982 static inline int pred_non_zero_count(H264Context *h, int n){
00983     const int index8= scan8[n];
00984     const int left= h->non_zero_count_cache[index8 - 1];
00985     const int top = h->non_zero_count_cache[index8 - 8];
00986     int i= left + top;
00987     
00988     if(i<64) i= (i+1)>>1;
00989 
00990     tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
00991 
00992     return i&31;
00993 }
00994 
00995 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
00996     const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
00997 
00998     if(topright_ref != PART_NOT_AVAILABLE){
00999         *C= h->mv_cache[list][ i - 8 + part_width ];
01000         return topright_ref;
01001     }else{
01002         tprintf("topright MV not available\n");
01003 
01004         *C= h->mv_cache[list][ i - 8 - 1 ];
01005         return h->ref_cache[list][ i - 8 - 1 ];
01006     }
01007 }
01008 
01016 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
01017     const int index8= scan8[n];
01018     const int top_ref=      h->ref_cache[list][ index8 - 8 ];
01019     const int left_ref=     h->ref_cache[list][ index8 - 1 ];
01020     const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
01021     const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
01022     const int16_t * C;
01023     int diagonal_ref, match_count;
01024 
01025     assert(part_width==1 || part_width==2 || part_width==4);
01026 
01027 /* mv_cache
01028   B . . A T T T T 
01029   U . . L . . , .
01030   U . . L . . . .
01031   U . . L . . , .
01032   . . . L . . . .
01033 */
01034 
01035     diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
01036     match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
01037     tprintf("pred_motion match_count=%d\n", match_count);
01038     if(match_count > 1){ //most common
01039         *mx= mid_pred(A[0], B[0], C[0]);
01040         *my= mid_pred(A[1], B[1], C[1]);
01041     }else if(match_count==1){
01042         if(left_ref==ref){
01043             *mx= A[0];
01044             *my= A[1];        
01045         }else if(top_ref==ref){
01046             *mx= B[0];
01047             *my= B[1];        
01048         }else{
01049             *mx= C[0];
01050             *my= C[1];        
01051         }
01052     }else{
01053         if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
01054             *mx= A[0];
01055             *my= A[1];        
01056         }else{
01057             *mx= mid_pred(A[0], B[0], C[0]);
01058             *my= mid_pred(A[1], B[1], C[1]);
01059         }
01060     }
01061         
01062     tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1],                    diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
01063 }
01064 
01071 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
01072     if(n==0){
01073         const int top_ref=      h->ref_cache[list][ scan8[0] - 8 ];
01074         const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
01075 
01076         tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
01077         
01078         if(top_ref == ref){
01079             *mx= B[0];
01080             *my= B[1];
01081             return;
01082         }
01083     }else{
01084         const int left_ref=     h->ref_cache[list][ scan8[8] - 1 ];
01085         const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
01086         
01087         tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
01088 
01089         if(left_ref == ref){
01090             *mx= A[0];
01091             *my= A[1];
01092             return;
01093         }
01094     }
01095 
01096     //RARE
01097     pred_motion(h, n, 4, list, ref, mx, my);
01098 }
01099 
01106 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
01107     if(n==0){
01108         const int left_ref=      h->ref_cache[list][ scan8[0] - 1 ];
01109         const int16_t * const A=  h->mv_cache[list][ scan8[0] - 1 ];
01110         
01111         tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
01112 
01113         if(left_ref == ref){
01114             *mx= A[0];
01115             *my= A[1];
01116             return;
01117         }
01118     }else{
01119         const int16_t * C;
01120         int diagonal_ref;
01121 
01122         diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
01123         
01124         tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
01125 
01126         if(diagonal_ref == ref){ 
01127             *mx= C[0];
01128             *my= C[1];
01129             return;
01130         }
01131     }
01132 
01133     //RARE
01134     pred_motion(h, n, 2, list, ref, mx, my);
01135 }
01136 
01137 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
01138     const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
01139     const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
01140 
01141     tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
01142 
01143     if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
01144        || (top_ref == 0  && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
01145        || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
01146        
01147         *mx = *my = 0;
01148         return;
01149     }
01150         
01151     pred_motion(h, 0, 4, 0, 0, mx, my);
01152 
01153     return;
01154 }
01155 
01156 static inline void direct_dist_scale_factor(H264Context * const h){
01157     const int poc = h->s.current_picture_ptr->poc;
01158     const int poc1 = h->ref_list[1][0].poc;
01159     int i;
01160     for(i=0; i<h->ref_count[0]; i++){
01161         int poc0 = h->ref_list[0][i].poc;
01162         int td = clip(poc1 - poc0, -128, 127);
01163         if(td == 0 /* FIXME || pic0 is a long-term ref */){
01164             h->dist_scale_factor[i] = 256;
01165         }else{
01166             int tb = clip(poc - poc0, -128, 127);
01167             int tx = (16384 + (ABS(td) >> 1)) / td;
01168             h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
01169         }
01170     }
01171 }
01172 static inline void direct_ref_list_init(H264Context * const h){
01173     MpegEncContext * const s = &h->s;
01174     Picture * const ref1 = &h->ref_list[1][0];
01175     Picture * const cur = s->current_picture_ptr;
01176     int list, i, j;
01177     if(cur->pict_type == I_TYPE)
01178         cur->ref_count[0] = 0;
01179     if(cur->pict_type != B_TYPE)
01180         cur->ref_count[1] = 0;
01181     for(list=0; list<2; list++){
01182         cur->ref_count[list] = h->ref_count[list];
01183         for(j=0; j<h->ref_count[list]; j++)
01184             cur->ref_poc[list][j] = h->ref_list[list][j].poc;
01185     }
01186     if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
01187         return;
01188     for(list=0; list<2; list++){
01189         for(i=0; i<ref1->ref_count[list]; i++){
01190             const int poc = ref1->ref_poc[list][i];
01191             h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE;
01192             for(j=0; j<h->ref_count[list]; j++)
01193                 if(h->ref_list[list][j].poc == poc){
01194                     h->map_col_to_list0[list][i] = j;
01195                     break;
01196                 }
01197         }
01198     }
01199 }
01200 
01201 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
01202     MpegEncContext * const s = &h->s;
01203     const int mb_xy =   s->mb_x +   s->mb_y*s->mb_stride;
01204     const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
01205     const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
01206     const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
01207     const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
01208     const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
01209     const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
01210     const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
01211     const int is_b8x8 = IS_8X8(*mb_type);
01212     int sub_mb_type;
01213     int i8, i4;
01214 
01215     if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
01216         /* FIXME save sub mb types from previous frames (or derive from MVs)
01217          * so we know exactly what block size to use */
01218         sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
01219         *mb_type =    MB_TYPE_8x8|MB_TYPE_L0L1;
01220     }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
01221         sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
01222         *mb_type =    MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
01223     }else{
01224         sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
01225         *mb_type =    MB_TYPE_8x8|MB_TYPE_L0L1;
01226     }
01227     if(!is_b8x8)
01228         *mb_type |= MB_TYPE_DIRECT2;
01229 
01230     tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
01231     
01232     if(h->direct_spatial_mv_pred){
01233         int ref[2];
01234         int mv[2][2];
01235         int list;
01236 
01237         /* ref = min(neighbors) */
01238         for(list=0; list<2; list++){
01239             int refa = h->ref_cache[list][scan8[0] - 1];
01240             int refb = h->ref_cache[list][scan8[0] - 8];
01241             int refc = h->ref_cache[list][scan8[0] - 8 + 4];
01242             if(refc == -2)
01243                 refc = h->ref_cache[list][scan8[0] - 8 - 1];
01244             ref[list] = refa;
01245             if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
01246                 ref[list] = refb;
01247             if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
01248                 ref[list] = refc;
01249             if(ref[list] < 0)
01250                 ref[list] = -1;
01251         }
01252 
01253         if(ref[0] < 0 && ref[1] < 0){
01254             ref[0] = ref[1] = 0;
01255             mv[0][0] = mv[0][1] =
01256             mv[1][0] = mv[1][1] = 0;
01257         }else{
01258             for(list=0; list<2; list++){
01259                 if(ref[list] >= 0)
01260                     pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
01261                 else
01262                     mv[list][0] = mv[list][1] = 0;
01263             }
01264         }
01265 
01266         if(ref[1] < 0){
01267             *mb_type &= ~MB_TYPE_P0L1;
01268             sub_mb_type &= ~MB_TYPE_P0L1;
01269         }else if(ref[0] < 0){
01270             *mb_type &= ~MB_TYPE_P0L0;
01271             sub_mb_type &= ~MB_TYPE_P0L0;
01272         }
01273 
01274         if(IS_16X16(*mb_type)){
01275             fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1);
01276             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1);
01277             if(!IS_INTRA(mb_type_col) 
01278                && (   l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1
01279                    || l1ref0[0]  < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1)){
01280                 if(ref[0] > 0)
01281                     fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
01282                 else
01283                     fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
01284                 if(ref[1] > 0)
01285                     fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
01286                 else
01287                     fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
01288             }else{
01289                 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
01290                 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
01291             }
01292         }else{
01293             for(i8=0; i8<4; i8++){
01294                 const int x8 = i8&1;
01295                 const int y8 = i8>>1;
01296     
01297                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01298                     continue;
01299                 h->sub_mb_type[i8] = sub_mb_type;
01300     
01301                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
01302                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
01303                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1);
01304                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1);
01305     
01306                 /* col_zero_flag */
01307                 if(!IS_INTRA(mb_type_col) && (   l1ref0[x8 + y8*h->b8_stride] == 0 
01308                                               || l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0)){
01309                     const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
01310                     for(i4=0; i4<4; i4++){
01311                         const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
01312                         if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
01313                             if(ref[0] == 0)
01314                                 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
01315                             if(ref[1] == 0)
01316                                 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
01317                         }
01318                     }
01319                 }
01320             }
01321         }
01322     }else{ /* direct temporal mv pred */
01323         if(IS_16X16(*mb_type)){
01324             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
01325             if(IS_INTRA(mb_type_col)){
01326                 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
01327                 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
01328                 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
01329             }else{
01330                 const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
01331                                                 : h->map_col_to_list0[1][l1ref1[0]];
01332                 const int dist_scale_factor = h->dist_scale_factor[ref0];
01333                 const int16_t *mv_col = l1mv0[0];
01334                 int mv_l0[2];
01335                 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
01336                 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
01337                 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
01338                 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
01339                 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4);
01340             }
01341         }else{
01342             for(i8=0; i8<4; i8++){
01343                 const int x8 = i8&1;
01344                 const int y8 = i8>>1;
01345                 int ref0, dist_scale_factor;
01346     
01347                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01348                     continue;
01349                 h->sub_mb_type[i8] = sub_mb_type;
01350                 if(IS_INTRA(mb_type_col)){
01351                     fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
01352                     fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
01353                     fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
01354                     fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
01355                     continue;
01356                 }
01357     
01358                 ref0 = l1ref0[x8 + y8*h->b8_stride];
01359                 if(ref0 >= 0)
01360                     ref0 = h->map_col_to_list0[0][ref0];
01361                 else
01362                     ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
01363                 dist_scale_factor = h->dist_scale_factor[ref0];
01364     
01365                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
01366                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
01367                 for(i4=0; i4<4; i4++){
01368                     const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
01369                     int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
01370                     mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
01371                     mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
01372                     *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
01373                         pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
01374                 }
01375             }
01376         }
01377     }
01378 }
01379 
01380 static inline void write_back_motion(H264Context *h, int mb_type){
01381     MpegEncContext * const s = &h->s;
01382     const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
01383     const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
01384     int list;
01385 
01386     for(list=0; list<2; list++){
01387         int y;
01388         if(!USES_LIST(mb_type, list)){
01389             if(1){ //FIXME skip or never read if mb_type doesn't use it
01390                 for(y=0; y<4; y++){
01391                     *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
01392                     *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
01393                 }
01394                 if( h->pps.cabac ) {
01395                     /* FIXME needed ? */
01396                     for(y=0; y<4; y++){
01397                         *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=
01398                         *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;
01399                     }
01400                 }
01401                 for(y=0; y<2; y++){
01402                     s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]=
01403                     s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= LIST_NOT_USED;
01404                 }
01405             }
01406             continue;
01407         }
01408         
01409         for(y=0; y<4; y++){
01410             *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
01411             *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
01412         }
01413         if( h->pps.cabac ) {
01414             for(y=0; y<4; y++){
01415                 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
01416                 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
01417             }
01418         }
01419         for(y=0; y<2; y++){
01420             s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
01421             s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
01422         }
01423     }
01424     
01425     if(h->slice_type == B_TYPE && h->pps.cabac){
01426         if(IS_8X8(mb_type)){
01427             h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
01428             h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
01429             h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
01430         }
01431     }
01432 }
01433 
01441 static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
01442     int i, si, di;
01443     uint8_t *dst;
01444 
01445 //    src[0]&0x80;              //forbidden bit
01446     h->nal_ref_idc= src[0]>>5;
01447     h->nal_unit_type= src[0]&0x1F;
01448 
01449     src++; length--;
01450 #if 0    
01451     for(i=0; i<length; i++)
01452         printf("%2X ", src[i]);
01453 #endif
01454     for(i=0; i+1<length; i+=2){
01455         if(src[i]) continue;
01456         if(i>0 && src[i-1]==0) i--;
01457         if(i+2<length && src[i+1]==0 && src[i+2]<=3){
01458             if(src[i+2]!=3){
01459                 /* startcode, so we must be past the end */
01460                 length=i;
01461             }
01462             break;
01463         }
01464     }
01465 
01466     if(i>=length-1){ //no escaped 0
01467         *dst_length= length;
01468         *consumed= length+1; //+1 for the header
01469         return src; 
01470     }
01471 
01472     h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
01473     dst= h->rbsp_buffer;
01474 
01475 //printf("decoding esc\n");
01476     si=di=0;
01477     while(si<length){ 
01478         //remove escapes (very rare 1:2^22)
01479         if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
01480             if(src[si+2]==3){ //escape
01481                 dst[di++]= 0;
01482                 dst[di++]= 0;
01483                 si+=3;
01484                 continue;
01485             }else //next start code
01486                 break;
01487         }
01488 
01489         dst[di++]= src[si++];
01490     }
01491 
01492     *dst_length= di;
01493     *consumed= si + 1;//+1 for the header
01494 //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
01495     return dst;
01496 }
01497 
01498 #if 0
01499 
01506 static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
01507     int i, escape_count, si, di;
01508     uint8_t *temp;
01509     
01510     assert(length>=0);
01511     assert(dst_length>0);
01512     
01513     dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
01514 
01515     if(length==0) return 1;
01516 
01517     escape_count= 0;
01518     for(i=0; i<length; i+=2){
01519         if(src[i]) continue;
01520         if(i>0 && src[i-1]==0) 
01521             i--;
01522         if(i+2<length && src[i+1]==0 && src[i+2]<=3){
01523             escape_count++;
01524             i+=2;
01525         }
01526     }
01527     
01528     if(escape_count==0){ 
01529         if(dst+1 != src)
01530             memcpy(dst+1, src, length);
01531         return length + 1;
01532     }
01533     
01534     if(length + escape_count + 1> dst_length)
01535         return -1;
01536 
01537     //this should be damn rare (hopefully)
01538 
01539     h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
01540     temp= h->rbsp_buffer;
01541 //printf("encoding esc\n");
01542     
01543     si= 0;
01544     di= 0;
01545     while(si < length){
01546         if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
01547             temp[di++]= 0; si++;
01548             temp[di++]= 0; si++;
01549             temp[di++]= 3; 
01550             temp[di++]= src[si++];
01551         }
01552         else
01553             temp[di++]= src[si++];
01554     }
01555     memcpy(dst+1, temp, length+escape_count);
01556     
01557     assert(di == length+escape_count);
01558     
01559     return di + 1;
01560 }
01561 
01565 static void encode_rbsp_trailing(PutBitContext *pb){
01566     int length;
01567     put_bits(pb, 1, 1);
01568     length= (-put_bits_count(pb))&7;
01569     if(length) put_bits(pb, length, 0);
01570 }
01571 #endif
01572 
01577 static int decode_rbsp_trailing(uint8_t *src){
01578     int v= *src;
01579     int r;
01580 
01581     tprintf("rbsp trailing %X\n", v);
01582 
01583     for(r=1; r<9; r++){
01584         if(v&1) return r;
01585         v>>=1;
01586     }
01587     return 0;
01588 }
01589 
01594 static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
01595     const int qmul= dequant_coeff[qp][0];
01596 #define stride 16
01597     int i;
01598     int temp[16]; //FIXME check if this is a good idea
01599     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
01600     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
01601 
01602 //memset(block, 64, 2*256);
01603 //return;
01604     for(i=0; i<4; i++){
01605         const int offset= y_offset[i];
01606         const int z0= block[offset+stride*0] + block[offset+stride*4];
01607         const int z1= block[offset+stride*0] - block[offset+stride*4];
01608         const int z2= block[offset+stride*1] - block[offset+stride*5];
01609         const int z3= block[offset+stride*1] + block[offset+stride*5];
01610 
01611         temp[4*i+0]= z0+z3;
01612         temp[4*i+1]= z1+z2;
01613         temp[4*i+2]= z1-z2;
01614         temp[4*i+3]= z0-z3;
01615     }
01616 
01617     for(i=0; i<4; i++){
01618         const int offset= x_offset[i];
01619         const int z0= temp[4*0+i] + temp[4*2+i];
01620         const int z1= temp[4*0+i] - temp[4*2+i];
01621         const int z2= temp[4*1+i] - temp[4*3+i];
01622         const int z3= temp[4*1+i] + temp[4*3+i];
01623 
01624         block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
01625         block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
01626         block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
01627         block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
01628     }
01629 }
01630 
01631 #if 0
01632 
01636 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
01637 //    const int qmul= dequant_coeff[qp][0];
01638     int i;
01639     int temp[16]; //FIXME check if this is a good idea
01640     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
01641     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
01642 
01643     for(i=0; i<4; i++){
01644         const int offset= y_offset[i];
01645         const int z0= block[offset+stride*0] + block[offset+stride*4];
01646         const int z1= block[offset+stride*0] - block[offset+stride*4];
01647         const int z2= block[offset+stride*1] - block[offset+stride*5];
01648         const int z3= block[offset+stride*1] + block[offset+stride*5];
01649 
01650         temp[4*i+0]= z0+z3;
01651         temp[4*i+1]= z1+z2;
01652         temp[4*i+2]= z1-z2;
01653         temp[4*i+3]= z0-z3;
01654     }
01655 
01656     for(i=0; i<4; i++){
01657         const int offset= x_offset[i];
01658         const int z0= temp[4*0+i] + temp[4*2+i];
01659         const int z1= temp[4*0+i] - temp[4*2+i];
01660         const int z2= temp[4*1+i] - temp[4*3+i];
01661         const int z3= temp[4*1+i] + temp[4*3+i];
01662 
01663         block[stride*0 +offset]= (z0 + z3)>>1;
01664         block[stride*2 +offset]= (z1 + z2)>>1;
01665         block[stride*8 +offset]= (z1 - z2)>>1;
01666         block[stride*10+offset]= (z0 - z3)>>1;
01667     }
01668 }
01669 #endif
01670 
01671 #undef xStride
01672 #undef stride
01673 
01674 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
01675     const int qmul= dequant_coeff[qp][0];
01676     const int stride= 16*2;
01677     const int xStride= 16;
01678     int a,b,c,d,e;
01679 
01680     a= block[stride*0 + xStride*0];
01681     b= block[stride*0 + xStride*1];
01682     c= block[stride*1 + xStride*0];
01683     d= block[stride*1 + xStride*1];
01684 
01685     e= a-b;
01686     a= a+b;
01687     b= c-d;
01688     c= c+d;
01689 
01690     block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
01691     block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
01692     block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
01693     block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
01694 }
01695 
01696 #if 0
01697 static void chroma_dc_dct_c(DCTELEM *block){
01698     const int stride= 16*2;
01699     const int xStride= 16;
01700     int a,b,c,d,e;
01701 
01702     a= block[stride*0 + xStride*0];
01703     b= block[stride*0 + xStride*1];
01704     c= block[stride*1 + xStride*0];
01705     d= block[stride*1 + xStride*1];
01706 
01707     e= a-b;
01708     a= a+b;
01709     b= c-d;
01710     c= c+d;
01711 
01712     block[stride*0 + xStride*0]= (a+c);
01713     block[stride*0 + xStride*1]= (e+b);
01714     block[stride*1 + xStride*0]= (a-c);
01715     block[stride*1 + xStride*1]= (e-b);
01716 }
01717 #endif
01718 
01722 static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
01723     
01724     return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
01725 }
01726 
01727 
01728 #if 0
01729 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
01730     int i;
01731     //FIXME try int temp instead of block
01732     
01733     for(i=0; i<4; i++){
01734         const int d0= src1[0 + i*stride] - src2[0 + i*stride];
01735         const int d1= src1[1 + i*stride] - src2[1 + i*stride];
01736         const int d2= src1[2 + i*stride] - src2[2 + i*stride];
01737         const int d3= src1[3 + i*stride] - src2[3 + i*stride];
01738         const int z0= d0 + d3;
01739         const int z3= d0 - d3;
01740         const int z1= d1 + d2;
01741         const int z2= d1 - d2;
01742         
01743         block[0 + 4*i]=   z0 +   z1;
01744         block[1 + 4*i]= 2*z3 +   z2;
01745         block[2 + 4*i]=   z0 -   z1;
01746         block[3 + 4*i]=   z3 - 2*z2;
01747     }    
01748 
01749     for(i=0; i<4; i++){
01750         const int z0= block[0*4 + i] + block[3*4 + i];
01751         const int z3= block[0*4 + i] - block[3*4 + i];
01752         const int z1= block[1*4 + i] + block[2*4 + i];
01753         const int z2= block[1*4 + i] - block[2*4 + i];
01754         
01755         block[0*4 + i]=   z0 +   z1;
01756         block[1*4 + i]= 2*z3 +   z2;
01757         block[2*4 + i]=   z0 -   z1;
01758         block[3*4 + i]=   z3 - 2*z2;
01759     }
01760 }
01761 #endif
01762 
01763 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
01764 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
01765 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
01766     int i;
01767     const int * const quant_table= quant_coeff[qscale];
01768     const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
01769     const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
01770     const unsigned int threshold2= (threshold1<<1);
01771     int last_non_zero;
01772 
01773     if(seperate_dc){
01774         if(qscale<=18){
01775             //avoid overflows
01776             const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
01777             const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
01778             const unsigned int dc_threshold2= (dc_threshold1<<1);
01779 
01780             int level= block[0]*quant_coeff[qscale+18][0];
01781             if(((unsigned)(level+dc_threshold1))>dc_threshold2){
01782                 if(level>0){
01783                     level= (dc_bias + level)>>(QUANT_SHIFT-2);
01784                     block[0]= level;
01785                 }else{
01786                     level= (dc_bias - level)>>(QUANT_SHIFT-2);
01787                     block[0]= -level;
01788                 }
01789 //                last_non_zero = i;
01790             }else{
01791                 block[0]=0;
01792             }
01793         }else{
01794             const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
01795             const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
01796             const unsigned int dc_threshold2= (dc_threshold1<<1);
01797 
01798             int level= block[0]*quant_table[0];
01799             if(((unsigned)(level+dc_threshold1))>dc_threshold2){
01800                 if(level>0){
01801                     level= (dc_bias + level)>>(QUANT_SHIFT+1);
01802                     block[0]= level;
01803                 }else{
01804                     level= (dc_bias - level)>>(QUANT_SHIFT+1);
01805                     block[0]= -level;
01806                 }
01807 //                last_non_zero = i;
01808             }else{
01809                 block[0]=0;
01810             }
01811         }
01812         last_non_zero= 0;
01813         i=1;
01814     }else{
01815         last_non_zero= -1;
01816         i=0;
01817     }
01818 
01819     for(; i<16; i++){
01820         const int j= scantable[i];
01821         int level= block[j]*quant_table[j];
01822 
01823 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
01824 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
01825         if(((unsigned)(level+threshold1))>threshold2){
01826             if(level>0){
01827                 level= (bias + level)>>QUANT_SHIFT;
01828                 block[j]= level;
01829             }else{
01830                 level= (bias - level)>>QUANT_SHIFT;
01831                 block[j]= -level;
01832             }
01833             last_non_zero = i;
01834         }else{
01835             block[j]=0;
01836         }
01837     }
01838 
01839     return last_non_zero;
01840 }
01841 
01842 static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
01843     const uint32_t a= ((uint32_t*)(src-stride))[0];
01844     ((uint32_t*)(src+0*stride))[0]= a;
01845     ((uint32_t*)(src+1*stride))[0]= a;
01846     ((uint32_t*)(src+2*stride))[0]= a;
01847     ((uint32_t*)(src+3*stride))[0]= a;
01848 }
01849 
01850 static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
01851     ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
01852     ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
01853     ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
01854     ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
01855 }
01856 
01857 static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
01858     const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
01859                    + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
01860     
01861     ((uint32_t*)(src+0*stride))[0]= 
01862     ((uint32_t*)(src+1*stride))[0]= 
01863     ((uint32_t*)(src+2*stride))[0]= 
01864     ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; 
01865 }
01866 
01867 static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
01868     const int dc= (  src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
01869     
01870     ((uint32_t*)(src+0*stride))[0]= 
01871     ((uint32_t*)(src+1*stride))[0]= 
01872     ((uint32_t*)(src+2*stride))[0]= 
01873     ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; 
01874 }
01875 
01876 static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
01877     const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
01878     
01879     ((uint32_t*)(src+0*stride))[0]= 
01880     ((uint32_t*)(src+1*stride))[0]= 
01881     ((uint32_t*)(src+2*stride))[0]= 
01882     ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; 
01883 }
01884 
01885 static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
01886     ((uint32_t*)(src+0*stride))[0]= 
01887     ((uint32_t*)(src+1*stride))[0]= 
01888     ((uint32_t*)(src+2*stride))[0]= 
01889     ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
01890 }
01891 
01892 
01893 #define LOAD_TOP_RIGHT_EDGE\
01894     const int t4= topright[0];\
01895     const int t5= topright[1];\
01896     const int t6= topright[2];\
01897     const int t7= topright[3];\
01898 
01899 #define LOAD_LEFT_EDGE\
01900     const int l0= src[-1+0*stride];\
01901     const int l1= src[-1+1*stride];\
01902     const int l2= src[-1+2*stride];\
01903     const int l3= src[-1+3*stride];\
01904 
01905 #define LOAD_TOP_EDGE\
01906     const int t0= src[ 0-1*stride];\
01907     const int t1= src[ 1-1*stride];\
01908     const int t2= src[ 2-1*stride];\
01909     const int t3= src[ 3-1*stride];\
01910 
01911 static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
01912     const int lt= src[-1-1*stride];
01913     LOAD_TOP_EDGE
01914     LOAD_LEFT_EDGE
01915 
01916     src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2; 
01917     src[0+2*stride]=
01918     src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2; 
01919     src[0+1*stride]=
01920     src[1+2*stride]=
01921     src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2; 
01922     src[0+0*stride]=
01923     src[1+1*stride]=
01924     src[2+2*stride]=
01925     src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2; 
01926     src[1+0*stride]=
01927     src[2+1*stride]=
01928     src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
01929     src[2+0*stride]=
01930     src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
01931     src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
01932 }
01933 
01934 static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
01935     LOAD_TOP_EDGE    
01936     LOAD_TOP_RIGHT_EDGE    
01937 //    LOAD_LEFT_EDGE    
01938 
01939     src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
01940     src[1+0*stride]=
01941     src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
01942     src[2+0*stride]=
01943     src[1+1*stride]=
01944     src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
01945     src[3+0*stride]=
01946     src[2+1*stride]=
01947     src[1+2*stride]=
01948     src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
01949     src[3+1*stride]=
01950     src[2+2*stride]=
01951     src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
01952     src[3+2*stride]=
01953     src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
01954     src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
01955 }
01956 
01957 static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
01958     const int lt= src[-1-1*stride];
01959     LOAD_TOP_EDGE    
01960     LOAD_LEFT_EDGE    
01961     const __attribute__((unused)) int unu= l3;
01962 
01963     src[0+0*stride]=
01964     src[1+2*stride]=(lt + t0 + 1)>>1;
01965     src[1+0*stride]=
01966     src[2+2*stride]=(t0 + t1 + 1)>>1;
01967     src[2+0*stride]=
01968     src[3+2*stride]=(t1 + t2 + 1)>>1;
01969     src[3+0*stride]=(t2 + t3 + 1)>>1;
01970     src[0+1*stride]=
01971     src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
01972     src[1+1*stride]=
01973     src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
01974     src[2+1*stride]=
01975     src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
01976     src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
01977     src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
01978     src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
01979 }
01980 
01981 static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
01982     LOAD_TOP_EDGE    
01983     LOAD_TOP_RIGHT_EDGE    
01984     const __attribute__((unused)) int unu= t7;
01985 
01986     src[0+0*stride]=(t0 + t1 + 1)>>1;
01987     src[1+0*stride]=
01988     src[0+2*stride]=(t1 + t2 + 1)>>1;
01989     src[2+0*stride]=
01990     src[1+2*stride]=(t2 + t3 + 1)>>1;
01991     src[3+0*stride]=
01992     src[2+2*stride]=(t3 + t4+ 1)>>1;
01993     src[3+2*stride]=(t4 + t5+ 1)>>1;
01994     src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
01995     src[1+1*stride]=
01996     src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
01997     src[2+1*stride]=
01998     src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
01999     src[3+1*stride]=
02000     src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
02001     src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
02002 }
02003 
02004 static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
02005     LOAD_LEFT_EDGE    
02006 
02007     src[0+0*stride]=(l0 + l1 + 1)>>1;
02008     src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
02009     src[2+0*stride]=
02010     src[0+1*stride]=(l1 + l2 + 1)>>1;
02011     src[3+0*stride]=
02012     src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
02013     src[2+1*stride]=
02014     src[0+2*stride]=(l2 + l3 + 1)>>1;
02015     src[3+1*stride]=
02016     src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
02017     src[3+2*stride]=
02018     src[1+3*stride]=
02019     src[0+3*stride]=
02020     src[2+2*stride]=
02021     src[2+3*stride]=
02022     src[3+3*stride]=l3;
02023 }
02024     
02025 static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
02026     const int lt= src[-1-1*stride];
02027     LOAD_TOP_EDGE    
02028     LOAD_LEFT_EDGE    
02029     const __attribute__((unused)) int unu= t3;
02030 
02031     src[0+0*stride]=
02032     src[2+1*stride]=(lt + l0 + 1)>>1;
02033     src[1+0*stride]=
02034     src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
02035     src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
02036     src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
02037     src[0+1*stride]=
02038     src[2+2*stride]=(l0 + l1 + 1)>>1;
02039     src[1+1*stride]=
02040     src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
02041     src[0+2*stride]=
02042     src[2+3*stride]=(l1 + l2+ 1)>>1;
02043     src[1+2*stride]=
02044     src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
02045     src[0+3*stride]=(l2 + l3 + 1)>>1;
02046     src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
02047 }
02048 
02049 static void pred16x16_vertical_c(uint8_t *src, int stride){
02050     int i;
02051     const uint32_t a= ((uint32_t*)(src-stride))[0];
02052     const uint32_t b= ((uint32_t*)(src-stride))[1];
02053     const uint32_t c= ((uint32_t*)(src-stride))[2];
02054     const uint32_t d= ((uint32_t*)(src-stride))[3];
02055     
02056     for(i=0; i<16; i++){
02057         ((uint32_t*)(src+i*stride))[0]= a;
02058         ((uint32_t*)(src+i*stride))[1]= b;
02059         ((uint32_t*)(src+i*stride))[2]= c;
02060         ((uint32_t*)(src+i*stride))[3]= d;
02061     }
02062 }
02063 
02064 static void pred16x16_horizontal_c(uint8_t *src, int stride){
02065     int i;
02066 
02067     for(i=0; i<16; i++){
02068         ((uint32_t*)(src+i*stride))[0]=
02069         ((uint32_t*)(src+i*stride))[1]=
02070         ((uint32_t*)(src+i*stride))[2]=
02071         ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
02072     }
02073 }
02074 
02075 static void pred16x16_dc_c(uint8_t *src, int stride){
02076     int i, dc=0;
02077 
02078     for(i=0;i<16; i++){
02079         dc+= src[-1+i*stride];
02080     }
02081     
02082     for(i=0;i<16; i++){
02083         dc+= src[i-stride];
02084     }
02085 
02086     dc= 0x01010101*((dc + 16)>>5);
02087 
02088     for(i=0; i<16; i++){
02089         ((uint32_t*)(src+i*stride))[0]=
02090         ((uint32_t*)(src+i*stride))[1]=
02091         ((uint32_t*)(src+i*stride))[2]=
02092         ((uint32_t*)(src+i*stride))[3]= dc;
02093     }
02094 }
02095 
02096 static void pred16x16_left_dc_c(uint8_t *src, int stride){
02097     int i, dc=0;
02098 
02099     for(i=0;i<16; i++){
02100         dc+= src[-1+i*stride];
02101     }
02102     
02103     dc= 0x01010101*((dc + 8)>>4);
02104 
02105     for(i=0; i<16; i++){
02106         ((uint32_t*)(src+i*stride))[0]=
02107         ((uint32_t*)(src+i*stride))[1]=
02108         ((uint32_t*)(src+i*stride))[2]=
02109         ((uint32_t*)(src+i*stride))[3]= dc;
02110     }
02111 }
02112 
02113 static void pred16x16_top_dc_c(uint8_t *src, int stride){
02114     int i, dc=0;
02115 
02116     for(i=0;i<16; i++){
02117         dc+= src[i-stride];
02118     }
02119     dc= 0x01010101*((dc + 8)>>4);
02120 
02121     for(i=0; i<16; i++){
02122         ((uint32_t*)(src+i*stride))[0]=
02123         ((uint32_t*)(src+i*stride))[1]=
02124         ((uint32_t*)(src+i*stride))[2]=
02125         ((uint32_t*)(src+i*stride))[3]= dc;
02126     }
02127 }
02128 
02129 static void pred16x16_128_dc_c(uint8_t *src, int stride){
02130     int i;
02131 
02132     for(i=0; i<16; i++){
02133         ((uint32_t*)(src+i*stride))[0]=
02134         ((uint32_t*)(src+i*stride))[1]=
02135         ((uint32_t*)(src+i*stride))[2]=
02136         ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
02137     }
02138 }
02139 
02140 static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
02141   int i, j, k;
02142   int a;
02143   uint8_t *cm = cropTbl + MAX_NEG_CROP;
02144   const uint8_t * const src0 = src+7-stride;
02145   const uint8_t *src1 = src+8*stride-1;
02146   const uint8_t *src2 = src1-2*stride;      // == src+6*stride-1;
02147   int H = src0[1] - src0[-1];
02148   int V = src1[0] - src2[ 0];
02149   for(k=2; k<=8; ++k) {
02150     src1 += stride; src2 -= stride;
02151     H += k*(src0[k] - src0[-k]);
02152     V += k*(src1[0] - src2[ 0]);
02153   }
02154   if(svq3){
02155     H = ( 5*(H/4) ) / 16;
02156     V = ( 5*(V/4) ) / 16;
02157 
02158     /* required for 100% accuracy */
02159     i = H; H = V; V = i;
02160   }else{
02161     H = ( 5*H+32 ) >> 6;
02162     V = ( 5*V+32 ) >> 6;
02163   }
02164 
02165   a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
02166   for(j=16; j>0; --j) {
02167     int b = a;
02168     a += V;
02169     for(i=-16; i<0; i+=4) {
02170       src[16+i] = cm[ (b    ) >> 5 ];
02171       src[17+i] = cm[ (b+  H) >> 5 ];
02172       src[18+i] = cm[ (b+2*H) >> 5 ];
02173       src[19+i] = cm[ (b+3*H) >> 5 ];
02174       b += 4*H;
02175     }
02176     src += stride;
02177   }
02178 }
02179 
02180 static void pred16x16_plane_c(uint8_t *src, int stride){
02181     pred16x16_plane_compat_c(src, stride, 0);
02182 }
02183 
02184 static void pred8x8_vertical_c(uint8_t *src, int stride){
02185     int i;
02186     const uint32_t a= ((uint32_t*)(src-stride))[0];
02187     const uint32_t b= ((uint32_t*)(src-stride))[1];
02188     
02189     for(i=0; i<8; i++){
02190         ((uint32_t*)(src+i*stride))[0]= a;
02191         ((uint32_t*)(src+i*stride))[1]= b;
02192     }
02193 }
02194 
02195 static void pred8x8_horizontal_c(uint8_t *src, int stride){
02196     int i;
02197 
02198     for(i=0; i<8; i++){
02199         ((uint32_t*)(src+i*stride))[0]=
02200         ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
02201     }
02202 }
02203 
02204 static void pred8x8_128_dc_c(uint8_t *src, int stride){
02205     int i;
02206 
02207     for(i=0; i<8; i++){
02208         ((uint32_t*)(src+i*stride))[0]= 
02209         ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
02210     }
02211 }
02212 
02213 static void pred8x8_left_dc_c(uint8_t *src, int stride){
02214     int i;
02215     int dc0, dc2;
02216 
02217     dc0=dc2=0;
02218     for(i=0;i<4; i++){
02219         dc0+= src[-1+i*stride];
02220         dc2+= src[-1+(i+4)*stride];
02221     }
02222     dc0= 0x01010101*((dc0 + 2)>>2);
02223     dc2= 0x01010101*((dc2 + 2)>>2);
02224 
02225     for(i=0; i<4; i++){
02226         ((uint32_t*)(src+i*stride))[0]=
02227         ((uint32_t*)(src+i*stride))[1]= dc0;
02228     }
02229     for(i=4; i<8; i++){
02230         ((uint32_t*)(src+i*stride))[0]=
02231         ((uint32_t*)(src+i*stride))[1]= dc2;
02232     }
02233 }
02234 
02235 static void pred8x8_top_dc_c(uint8_t *src, int stride){
02236     int i;
02237     int dc0, dc1;
02238 
02239     dc0=dc1=0;
02240     for(i=0;i<4; i++){
02241         dc0+= src[i-stride];
02242         dc1+= src[4+i-stride];
02243     }
02244     dc0= 0x01010101*((dc0 + 2)>>2);
02245     dc1= 0x01010101*((dc1 + 2)>>2);
02246 
02247     for(i=0; i<4; i++){
02248         ((uint32_t*)(src+i*stride))[0]= dc0;
02249         ((uint32_t*)(src+i*stride))[1]= dc1;
02250     }
02251     for(i=4; i<8; i++){
02252         ((uint32_t*)(src+i*stride))[0]= dc0;
02253         ((uint32_t*)(src+i*stride))[1]= dc1;
02254     }
02255 }
02256 
02257 
02258 static void pred8x8_dc_c(uint8_t *src, int stride){
02259     int i;
02260     int dc0, dc1, dc2, dc3;
02261 
02262     dc0=dc1=dc2=0;
02263     for(i=0;i<4; i++){
02264         dc0+= src[-1+i*stride] + src[i-stride];
02265         dc1+= src[4+i-stride];
02266         dc2+= src[-1+(i+4)*stride];
02267     }
02268     dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
02269     dc0= 0x01010101*((dc0 + 4)>>3);
02270     dc1= 0x01010101*((dc1 + 2)>>2);
02271     dc2= 0x01010101*((dc2 + 2)>>2);
02272 
02273     for(i=0; i<4; i++){
02274         ((uint32_t*)(src+i*stride))[0]= dc0;
02275         ((uint32_t*)(src+i*stride))[1]= dc1;
02276     }
02277     for(i=4; i<8; i++){
02278         ((uint32_t*)(src+i*stride))[0]= dc2;
02279         ((uint32_t*)(src+i*stride))[1]= dc3;
02280     }
02281 }
02282 
02283 static void pred8x8_plane_c(uint8_t *src, int stride){
02284   int j, k;
02285   int a;
02286   uint8_t *cm = cropTbl + MAX_NEG_CROP;
02287   const uint8_t * const src0 = src+3-stride;
02288   const uint8_t *src1 = src+4*stride-1;
02289   const uint8_t *src2 = src1-2*stride;      // == src+2*stride-1;
02290   int H = src0[1] - src0[-1];
02291   int V = src1[0] - src2[ 0];
02292   for(k=2; k<=4; ++k) {
02293     src1 += stride; src2 -= stride;
02294     H += k*(src0[k] - src0[-k]);
02295     V += k*(src1[0] - src2[ 0]);
02296   }
02297   H = ( 17*H+16 ) >> 5;
02298   V = ( 17*V+16 ) >> 5;
02299 
02300   a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
02301   for(j=8; j>0; --j) {
02302     int b = a;
02303     a += V;
02304     src[0] = cm[ (b    ) >> 5 ];
02305     src[1] = cm[ (b+  H) >> 5 ];
02306     src[2] = cm[ (b+2*H) >> 5 ];
02307     src[3] = cm[ (b+3*H) >> 5 ];
02308     src[4] = cm[ (b+4*H) >> 5 ];
02309     src[5] = cm[ (b+5*H) >> 5 ];
02310     src[6] = cm[ (b+6*H) >> 5 ];
02311     src[7] = cm[ (b+7*H) >> 5 ];
02312     src += stride;
02313   }
02314 }
02315 
02316 #define SRC(x,y) src[(x)+(y)*stride]
02317 #define PL(y) \
02318     const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
02319 #define PREDICT_8x8_LOAD_LEFT \
02320     const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
02321                      + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
02322     PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
02323     const int l7 = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
02324 
02325 #define PT(x) \
02326     const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
02327 #define PREDICT_8x8_LOAD_TOP \
02328     const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
02329                      + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
02330     PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
02331     const int t7 = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
02332                      + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
02333 
02334 #define PTR(x) \
02335     t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
02336 #define PREDICT_8x8_LOAD_TOPRIGHT \
02337     int t8, t9, t10, t11, t12, t13, t14, t15; \
02338     if(has_topright) { \
02339         PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
02340         t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
02341     } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
02342 
02343 #define PREDICT_8x8_LOAD_TOPLEFT \
02344     const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
02345 
02346 #define PREDICT_8x8_DC(v) \
02347     int y; \
02348     for( y = 0; y < 8; y++ ) { \
02349         ((uint32_t*)src)[0] = \
02350         ((uint32_t*)src)[1] = v; \
02351         src += stride; \
02352     }
02353 
02354 static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02355 {
02356     PREDICT_8x8_DC(0x80808080);
02357 }
02358 static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02359 {
02360     PREDICT_8x8_LOAD_LEFT;
02361     const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
02362     PREDICT_8x8_DC(dc);
02363 }
02364 static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02365 {
02366     PREDICT_8x8_LOAD_TOP;
02367     const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
02368     PREDICT_8x8_DC(dc);
02369 }
02370 static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02371 {
02372     PREDICT_8x8_LOAD_LEFT;
02373     PREDICT_8x8_LOAD_TOP;
02374     const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
02375                          +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
02376     PREDICT_8x8_DC(dc);
02377 }
02378 static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02379 {
02380     PREDICT_8x8_LOAD_LEFT;
02381 #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
02382                ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
02383     ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
02384 #undef ROW
02385 }
02386 static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02387 {
02388     int y;
02389     PREDICT_8x8_LOAD_TOP;
02390     src[0] = t0;
02391     src[1] = t1;
02392     src[2] = t2;
02393     src[3] = t3;
02394     src[4] = t4;
02395     src[5] = t5;
02396     src[6] = t6;
02397     src[7] = t7;
02398     for( y = 1; y < 8; y++ )
02399         *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
02400 }
02401 static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02402 {
02403     PREDICT_8x8_LOAD_TOP;
02404     PREDICT_8x8_LOAD_TOPRIGHT;
02405     SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
02406     SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
02407     SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
02408     SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
02409     SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
02410     SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
02411     SRC(0,6)=SRC(1,5)=SRC(2,4)=SRC(3,3)=SRC(4,2)=SRC(5,1)=SRC(6,0)= (t6 + 2*t7 + t8 + 2) >> 2;
02412     SRC(0,7)=SRC(1,6)=SRC(2,5)=SRC(3,4)=SRC(4,3)=SRC(5,2)=SRC(6,1)=SRC(7,0)= (t7 + 2*t8 + t9 + 2) >> 2;
02413     SRC(1,7)=SRC(2,6)=SRC(3,5)=SRC(4,4)=SRC(5,3)=SRC(6,2)=SRC(7,1)= (t8 + 2*t9 + t10 + 2) >> 2;
02414     SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
02415     SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
02416     SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
02417     SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
02418     SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
02419     SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
02420 }
02421 static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02422 {
02423     PREDICT_8x8_LOAD_TOP;
02424     PREDICT_8x8_LOAD_LEFT;
02425     PREDICT_8x8_LOAD_TOPLEFT;
02426     SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
02427     SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
02428     SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
02429     SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
02430     SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
02431     SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
02432     SRC(0,1)=SRC(1,2)=SRC(2,3)=SRC(3,4)=SRC(4,5)=SRC(5,6)=SRC(6,7)= (l1 + 2*l0 + lt + 2) >> 2;
02433     SRC(0,0)=SRC(1,1)=SRC(2,2)=SRC(3,3)=SRC(4,4)=SRC(5,5)=SRC(6,6)=SRC(7,7)= (l0 + 2*lt + t0 + 2) >> 2;
02434     SRC(1,0)=SRC(2,1)=SRC(3,2)=SRC(4,3)=SRC(5,4)=SRC(6,5)=SRC(7,6)= (lt + 2*t0 + t1 + 2) >> 2;
02435     SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
02436     SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
02437     SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
02438     SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
02439     SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
02440     SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
02441   
02442 }
02443 static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02444 {
02445     PREDICT_8x8_LOAD_TOP;
02446     PREDICT_8x8_LOAD_LEFT;
02447     PREDICT_8x8_LOAD_TOPLEFT;
02448     SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
02449     SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
02450     SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
02451     SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
02452     SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
02453     SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
02454     SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
02455     SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
02456     SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
02457     SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
02458     SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
02459     SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
02460     SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
02461     SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
02462     SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
02463     SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
02464     SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
02465     SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
02466     SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
02467     SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
02468     SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
02469     SRC(7,0)= (t6 + t7 + 1) >> 1;
02470 }
02471 static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02472 {
02473     PREDICT_8x8_LOAD_TOP;
02474     PREDICT_8x8_LOAD_LEFT;
02475     PREDICT_8x8_LOAD_TOPLEFT;
02476     SRC(0,7)= (l6 + l7 + 1) >> 1;
02477     SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
02478     SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
02479     SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
02480     SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
02481     SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
02482     SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
02483     SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
02484     SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
02485     SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
02486     SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
02487     SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
02488     SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
02489     SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
02490     SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
02491     SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
02492     SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
02493     SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
02494     SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
02495     SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
02496     SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
02497     SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
02498 }
02499 static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02500 {
02501     PREDICT_8x8_LOAD_TOP;
02502     PREDICT_8x8_LOAD_TOPRIGHT;
02503     SRC(0,0)= (t0 + t1 + 1) >> 1;
02504     SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
02505     SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
02506     SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
02507     SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
02508     SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
02509     SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
02510     SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
02511     SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
02512     SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
02513     SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
02514     SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
02515     SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
02516     SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
02517     SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
02518     SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
02519     SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
02520     SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
02521     SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
02522     SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
02523     SRC(7,6)= (t10 + t11 + 1) >> 1;
02524     SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
02525 }
02526 static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
02527 {
02528     PREDICT_8x8_LOAD_LEFT;
02529     SRC(0,0)= (l0 + l1 + 1) >> 1;
02530     SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
02531     SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
02532     SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
02533     SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
02534     SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
02535     SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
02536     SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
02537     SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
02538     SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
02539     SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
02540     SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
02541     SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
02542     SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
02543     SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
02544     SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
02545     SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
02546     SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
02547 }
02548 #undef PREDICT_8x8_LOAD_LEFT
02549 #undef PREDICT_8x8_LOAD_TOP
02550 #undef PREDICT_8x8_LOAD_TOPLEFT
02551 #undef PREDICT_8x8_LOAD_TOPRIGHT
02552 #undef PREDICT_8x8_DC
02553 #undef PTR
02554 #undef PT
02555 #undef PL
02556 #undef SRC
02557 
02558 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
02559                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02560                            int src_x_offset, int src_y_offset,
02561                            qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
02562     MpegEncContext * const s = &h->s;
02563     const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
02564     const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
02565     const int luma_xy= (mx&3) + ((my&3)<<2);
02566     uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
02567     uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
02568     uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
02569     int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
02570     int extra_height= extra_width;
02571     int emu=0;
02572     const int full_mx= mx>>2;
02573     const int full_my= my>>2;
02574     
02575     assert(pic->data[0]);
02576     
02577     if(mx&7) extra_width -= 3;
02578     if(my&7) extra_height -= 3;
02579     
02580     if(   full_mx < 0-extra_width 
02581        || full_my < 0-extra_height 
02582        || full_mx + 16/*FIXME*/ > s->width + extra_width 
02583        || full_my + 16/*FIXME*/ > s->height + extra_height){
02584         ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
02585             src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
02586         emu=1;
02587     }
02588     
02589     qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
02590     if(!square){
02591         qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
02592     }
02593     
02594     if(s->flags&CODEC_FLAG_GRAY) return;
02595     
02596     if(emu){
02597         ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
02598             src_cb= s->edge_emu_buffer;
02599     }
02600     chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
02601 
02602     if(emu){
02603         ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
02604             src_cr= s->edge_emu_buffer;
02605     }
02606     chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
02607 }
02608 
02609 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
02610                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02611                            int x_offset, int y_offset,
02612                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
02613                            qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
02614                            int list0, int list1){
02615     MpegEncContext * const s = &h->s;
02616     qpel_mc_func *qpix_op=  qpix_put;
02617     h264_chroma_mc_func chroma_op= chroma_put;
02618     
02619     dest_y  += 2*x_offset + 2*y_offset*s->  linesize;
02620     dest_cb +=   x_offset +   y_offset*s->uvlinesize;
02621     dest_cr +=   x_offset +   y_offset*s->uvlinesize;
02622     x_offset += 8*s->mb_x;
02623     y_offset += 8*s->mb_y;
02624     
02625     if(list0){
02626         Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
02627         mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
02628                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
02629                            qpix_op, chroma_op);
02630 
02631         qpix_op=  qpix_avg;
02632         chroma_op= chroma_avg;
02633     }
02634 
02635     if(list1){
02636         Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
02637         mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
02638                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
02639                            qpix_op, chroma_op);
02640     }
02641 }
02642 
02643 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
02644                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02645                            int x_offset, int y_offset,
02646                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
02647                            h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
02648                            h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
02649                            int list0, int list1){
02650     MpegEncContext * const s = &h->s;
02651 
02652     dest_y  += 2*x_offset + 2*y_offset*s->  linesize;
02653     dest_cb +=   x_offset +   y_offset*s->uvlinesize;
02654     dest_cr +=   x_offset +   y_offset*s->uvlinesize;
02655     x_offset += 8*s->mb_x;
02656     y_offset += 8*s->mb_y;
02657     
02658     if(list0 && list1){
02659         /* don't optimize for luma-only case, since B-frames usually
02660          * use implicit weights => chroma too. */
02661         uint8_t *tmp_cb = s->obmc_scratchpad;
02662         uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
02663         uint8_t *tmp_y  = tmp_cr + 8*s->uvlinesize;
02664         int refn0 = h->ref_cache[0][ scan8[n] ];
02665         int refn1 = h->ref_cache[1][ scan8[n] ];
02666 
02667         mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
02668                     dest_y, dest_cb, dest_cr,
02669                     x_offset, y_offset, qpix_put, chroma_put);
02670         mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
02671                     tmp_y, tmp_cb, tmp_cr,
02672                     x_offset, y_offset, qpix_put, chroma_put);
02673 
02674         if(h->use_weight == 2){
02675             int weight0 = h->implicit_weight[refn0][refn1];
02676             int weight1 = 64 - weight0;
02677             luma_weight_avg(  dest_y,  tmp_y,  s->  linesize, 5, weight0, weight1, 0, 0);
02678             chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0);
02679             chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0);
02680         }else{
02681             luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
02682                             h->luma_weight[0][refn0], h->luma_weight[1][refn1], 
02683                             h->luma_offset[0][refn0], h->luma_offset[1][refn1]);
02684             chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
02685                             h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0], 
02686                             h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]);
02687             chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
02688                             h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1], 
02689                             h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]);
02690         }
02691     }else{
02692         int list = list1 ? 1 : 0;
02693         int refn = h->ref_cache[list][ scan8[n] ];
02694         Picture *ref= &h->ref_list[list][refn];
02695         mc_dir_part(h, ref, n, square, chroma_height, delta, list,
02696                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
02697                     qpix_put, chroma_put);
02698 
02699         luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
02700                        h->luma_weight[list][refn], h->luma_offset[list][refn]);
02701         if(h->use_weight_chroma){
02702             chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
02703                              h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
02704             chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
02705                              h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
02706         }
02707     }
02708 }
02709 
02710 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
02711                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02712                            int x_offset, int y_offset,
02713                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
02714                            qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
02715                            h264_weight_func *weight_op, h264_biweight_func *weight_avg, 
02716                            int list0, int list1){
02717     if((h->use_weight==2 && list0 && list1
02718         && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
02719        || h->use_weight==1)
02720         mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
02721                          x_offset, y_offset, qpix_put, chroma_put,
02722                          weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
02723     else
02724         mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
02725                     x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
02726 }
02727 
02728 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02729                       qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
02730                       qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
02731                       h264_weight_func *weight_op, h264_biweight_func *weight_avg){
02732     MpegEncContext * const s = &h->s;
02733     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
02734     const int mb_type= s->current_picture.mb_type[mb_xy];
02735     
02736     assert(IS_INTER(mb_type));
02737     
02738     if(IS_16X16(mb_type)){
02739         mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
02740                 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
02741                 &weight_op[0], &weight_avg[0],
02742                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
02743     }else if(IS_16X8(mb_type)){
02744         mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
02745                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
02746                 &weight_op[1], &weight_avg[1],
02747                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
02748         mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
02749                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
02750                 &weight_op[1], &weight_avg[1],
02751                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
02752     }else if(IS_8X16(mb_type)){
02753         mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
02754                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
02755                 &weight_op[2], &weight_avg[2],
02756                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
02757         mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
02758                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
02759                 &weight_op[2], &weight_avg[2],
02760                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
02761     }else{
02762         int i;
02763         
02764         assert(IS_8X8(mb_type));
02765 
02766         for(i=0; i<4; i++){
02767             const int sub_mb_type= h->sub_mb_type[i];
02768             const int n= 4*i;
02769             int x_offset= (i&1)<<2;
02770             int y_offset= (i&2)<<1;
02771 
02772             if(IS_SUB_8X8(sub_mb_type)){
02773                 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
02774                     qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
02775                     &weight_op[3], &weight_avg[3],
02776                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02777             }else if(IS_SUB_8X4(sub_mb_type)){
02778                 mc_part(h, n  , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
02779                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
02780                     &weight_op[4], &weight_avg[4],
02781                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02782                 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
02783                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
02784                     &weight_op[4], &weight_avg[4],
02785                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02786             }else if(IS_SUB_4X8(sub_mb_type)){
02787                 mc_part(h, n  , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
02788                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
02789                     &weight_op[5], &weight_avg[5],
02790                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02791                 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
02792                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
02793                     &weight_op[5], &weight_avg[5],
02794                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02795             }else{
02796                 int j;
02797                 assert(IS_SUB_4X4(sub_mb_type));
02798                 for(j=0; j<4; j++){
02799                     int sub_x_offset= x_offset + 2*(j&1);
02800                     int sub_y_offset= y_offset +   (j&2);
02801                     mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
02802                         qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
02803                         &weight_op[6], &weight_avg[6],
02804                         IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
02805                 }
02806             }
02807         }
02808     }
02809 }
02810 
02811 static void decode_init_vlc(H264Context *h){
02812     static int done = 0;
02813 
02814     if (!done) {
02815         int i;
02816         done = 1;
02817 
02818         init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5, 
02819                  &chroma_dc_coeff_token_len [0], 1, 1,
02820                  &chroma_dc_coeff_token_bits[0], 1, 1, 1);
02821 
02822         for(i=0; i<4; i++){
02823             init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17, 
02824                      &coeff_token_len [i][0], 1, 1,
02825                      &coeff_token_bits[i][0], 1, 1, 1);
02826         }
02827 
02828         for(i=0; i<3; i++){
02829             init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
02830                      &chroma_dc_total_zeros_len [i][0], 1, 1,
02831                      &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
02832         }
02833         for(i=0; i<15; i++){
02834             init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16, 
02835                      &total_zeros_len [i][0], 1, 1,
02836                      &total_zeros_bits[i][0], 1, 1, 1);
02837         }
02838 
02839         for(i=0; i<6; i++){
02840             init_vlc(&run_vlc[i], RUN_VLC_BITS, 7, 
02841                      &run_len [i][0], 1, 1,
02842                      &run_bits[i][0], 1, 1, 1);
02843         }
02844         init_vlc(&run7_vlc, RUN7_VLC_BITS, 16, 
02845                  &run_len [6][0], 1, 1,
02846                  &run_bits[6][0], 1, 1, 1);
02847     }
02848 }
02849 
02853 static void init_pred_ptrs(H264Context *h){
02854 //    MpegEncContext * const s = &h->s;
02855 
02856     h->pred4x4[VERT_PRED           ]= pred4x4_vertical_c;
02857     h->pred4x4[HOR_PRED            ]= pred4x4_horizontal_c;
02858     h->pred4x4[DC_PRED             ]= pred4x4_dc_c;
02859     h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
02860     h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
02861     h->pred4x4[VERT_RIGHT_PRED     ]= pred4x4_vertical_right_c;
02862     h->pred4x4[HOR_DOWN_PRED       ]= pred4x4_horizontal_down_c;
02863     h->pred4x4[VERT_LEFT_PRED      ]= pred4x4_vertical_left_c;
02864     h->pred4x4[HOR_UP_PRED         ]= pred4x4_horizontal_up_c;
02865     h->pred4x4[LEFT_DC_PRED        ]= pred4x4_left_dc_c;
02866     h->pred4x4[TOP_DC_PRED         ]= pred4x4_top_dc_c;
02867     h->pred4x4[DC_128_PRED         ]= pred4x4_128_dc_c;
02868 
02869     h->pred8x8l[VERT_PRED           ]= pred8x8l_vertical_c;
02870     h->pred8x8l[HOR_PRED            ]= pred8x8l_horizontal_c;
02871     h->pred8x8l[DC_PRED             ]= pred8x8l_dc_c;
02872     h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
02873     h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
02874     h->pred8x8l[VERT_RIGHT_PRED     ]= pred8x8l_vertical_right_c;
02875     h->pred8x8l[HOR_DOWN_PRED       ]= pred8x8l_horizontal_down_c;
02876     h->pred8x8l[VERT_LEFT_PRED      ]= pred8x8l_vertical_left_c;
02877     h->pred8x8l[HOR_UP_PRED         ]= pred8x8l_horizontal_up_c;
02878     h->pred8x8l[LEFT_DC_PRED        ]= pred8x8l_left_dc_c;
02879     h->pred8x8l[TOP_DC_PRED         ]= pred8x8l_top_dc_c;
02880     h->pred8x8l[DC_128_PRED         ]= pred8x8l_128_dc_c;
02881 
02882     h->pred8x8[DC_PRED8x8     ]= pred8x8_dc_c;
02883     h->pred8x8[VERT_PRED8x8   ]= pred8x8_vertical_c;
02884     h->pred8x8[HOR_PRED8x8    ]= pred8x8_horizontal_c;
02885     h->pred8x8[PLANE_PRED8x8  ]= pred8x8_plane_c;
02886     h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
02887     h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
02888     h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
02889 
02890     h->pred16x16[DC_PRED8x8     ]= pred16x16_dc_c;
02891     h->pred16x16[VERT_PRED8x8   ]= pred16x16_vertical_c;
02892     h->pred16x16[HOR_PRED8x8    ]= pred16x16_horizontal_c;
02893     h->pred16x16[PLANE_PRED8x8  ]= pred16x16_plane_c;
02894     h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
02895     h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
02896     h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
02897 }
02898 
02899 static void free_tables(H264Context *h){
02900     av_freep(&h->intra4x4_pred_mode);
02901     av_freep(&h->chroma_pred_mode_table);
02902     av_freep(&h->cbp_table);
02903     av_freep(&h->mvd_table[0]);
02904     av_freep(&h->mvd_table[1]);
02905     av_freep(&h->direct_table);
02906     av_freep(&h->non_zero_count);
02907     av_freep(&h->slice_table_base);
02908     av_freep(&h->top_borders[1]);
02909     av_freep(&h->top_borders[0]);
02910     h->slice_table= NULL;
02911 
02912     av_freep(&h->mb2b_xy);
02913     av_freep(&h->mb2b8_xy);
02914 
02915     av_freep(&h->dequant4_coeff);
02916     av_freep(&h->dequant8_coeff);
02917 
02918     av_freep(&h->s.obmc_scratchpad);
02919 }
02920 
02925 static int alloc_tables(H264Context *h){
02926     MpegEncContext * const s = &h->s;
02927     const int big_mb_num= s->mb_stride * (s->mb_height+1);
02928     int x,y,q;
02929 
02930     CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8  * sizeof(uint8_t))
02931 
02932     CHECKED_ALLOCZ(h->non_zero_count    , big_mb_num * 16 * sizeof(uint8_t))
02933     CHECKED_ALLOCZ(h->slice_table_base  , big_mb_num * sizeof(uint8_t))
02934     CHECKED_ALLOCZ(h->top_borders[0]    , s->mb_width * (16+8+8) * sizeof(uint8_t))
02935     CHECKED_ALLOCZ(h->top_borders[1]    , s->mb_width * (16+8+8) * sizeof(uint8_t))
02936     CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
02937 
02938     if( h->pps.cabac ) {
02939         CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
02940         CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
02941         CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
02942         CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
02943     }
02944 
02945     memset(h->slice_table_base, -1, big_mb_num  * sizeof(uint8_t));
02946     h->slice_table= h->slice_table_base + s->mb_stride + 1;
02947 
02948     CHECKED_ALLOCZ(h->mb2b_xy  , big_mb_num * sizeof(uint32_t));
02949     CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
02950     for(y=0; y<s->mb_height; y++){
02951         for(x=0; x<s->mb_width; x++){
02952             const int mb_xy= x + y*s->mb_stride;
02953             const int b_xy = 4*x + 4*y*h->b_stride;
02954             const int b8_xy= 2*x + 2*y*h->b8_stride;
02955         
02956             h->mb2b_xy [mb_xy]= b_xy;
02957             h->mb2b8_xy[mb_xy]= b8_xy;
02958         }
02959     }
02960 
02961     CHECKED_ALLOCZ(h->dequant4_coeff, 52*16 * sizeof(uint16_t));
02962     CHECKED_ALLOCZ(h->dequant8_coeff, 52*64 * sizeof(uint16_t));
02963     memcpy(h->dequant4_coeff, dequant_coeff, 52*16 * sizeof(uint16_t));
02964     for(q=0; q<52; q++){
02965         int shift = div6[q];
02966         int idx = rem6[q];
02967         if(shift >= 2) // qp<12 are shifted during dequant
02968             shift -= 2;
02969         for(x=0; x<64; x++)
02970             h->dequant8_coeff[q][x] = dequant8_coeff_init[idx][
02971                 dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] << shift;
02972     }
02973     if(h->sps.transform_bypass){
02974         for(x=0; x<16; x++)
02975             h->dequant4_coeff[0][x] = 1;
02976         for(x=0; x<64; x++)
02977             h->dequant8_coeff[0][x] = 1<<2;
02978     }
02979 
02980     s->obmc_scratchpad = NULL;
02981 
02982     return 0;
02983 fail:
02984     free_tables(h);
02985     return -1;
02986 }
02987 
02988 static void common_init(H264Context *h){
02989     MpegEncContext * const s = &h->s;
02990 
02991     s->width = s->avctx->width;
02992     s->height = s->avctx->height;
02993     s->codec_id= s->avctx->codec->id;
02994     
02995     init_pred_ptrs(h);
02996 
02997     s->unrestricted_mv=1;
02998     s->decode=1; //FIXME
02999 }
03000 
03001 static int decode_init(AVCodecContext *avctx){
03002     H264Context *h= avctx->priv_data;
03003     MpegEncContext * const s = &h->s;
03004 
03005     MPV_decode_defaults(s);
03006     
03007     s->avctx = avctx;
03008     common_init(h);
03009 
03010     s->out_format = FMT_H264;
03011     s->workaround_bugs= avctx->workaround_bugs;
03012 
03013     // set defaults
03014 //    s->decode_mb= ff_h263_decode_mb;
03015     s->low_delay= 1;
03016     avctx->pix_fmt= PIX_FMT_YUV420P;
03017 
03018     decode_init_vlc(h);
03019     
03020     if(avctx->extradata_size > 0 && avctx->extradata &&
03021        *(char *)avctx->extradata == 1){
03022         h->is_avc = 1;
03023         h->got_avcC = 0;
03024     } else {
03025         h->is_avc = 0;
03026     }
03027 
03028     return 0;
03029 }
03030 
03031 static void frame_start(H264Context *h){
03032     MpegEncContext * const s = &h->s;
03033     int i;
03034 
03035     MPV_frame_start(s, s->avctx);
03036     ff_er_frame_start(s);
03037 
03038     assert(s->linesize && s->uvlinesize);
03039 
03040     for(i=0; i<16; i++){
03041         h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
03042         h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
03043     }
03044     for(i=0; i<4; i++){
03045         h->block_offset[16+i]=
03046         h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
03047         h->block_offset[24+16+i]=
03048         h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
03049     }
03050 
03051     /* can't be in alloc_tables because linesize isn't known there.
03052      * FIXME: redo bipred weight to not require extra buffer? */
03053     if(!s->obmc_scratchpad)
03054         s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
03055 
03056 //    s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
03057 }
03058 
03059 static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
03060     MpegEncContext * const s = &h->s;
03061     int i;
03062     
03063     src_y  -=   linesize;
03064     src_cb -= uvlinesize;
03065     src_cr -= uvlinesize;
03066 
03067     // There are two lines saved, the line above the the top macroblock of a pair,
03068     // and the line above the bottom macroblock
03069     h->left_border[0]= h->top_borders[0][s->mb_x][15];
03070     for(i=1; i<17; i++){
03071         h->left_border[i]= src_y[15+i*  linesize];
03072     }
03073     
03074     *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y +  16*linesize);
03075     *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
03076 
03077     if(!(s->flags&CODEC_FLAG_GRAY)){
03078         h->left_border[17  ]= h->top_borders[0][s->mb_x][16+7];
03079         h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
03080         for(i=1; i<9; i++){
03081             h->left_border[i+17  ]= src_cb[7+i*uvlinesize];
03082             h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
03083         }
03084         *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
03085         *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
03086     }
03087 }
03088 
03089 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
03090     MpegEncContext * const s = &h->s;
03091     int temp8, i;
03092     uint64_t temp64;
03093     int deblock_left = (s->mb_x > 0);
03094     int deblock_top  = (s->mb_y > 0);
03095 
03096     src_y  -=   linesize + 1;
03097     src_cb -= uvlinesize + 1;
03098     src_cr -= uvlinesize + 1;
03099 
03100 #define XCHG(a,b,t,xchg)\
03101 t= a;\
03102 if(xchg)\
03103     a= b;\
03104 b= t;
03105 
03106     if(deblock_left){
03107         for(i = !deblock_top; i<17; i++){
03108             XCHG(h->left_border[i     ], src_y [i*  linesize], temp8, xchg);
03109         }
03110     }
03111 
03112     if(deblock_top){
03113         XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
03114         XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
03115         if(s->mb_x+1 < s->mb_width){
03116             XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
03117         }
03118     }
03119 
03120     if(!(s->flags&CODEC_FLAG_GRAY)){
03121         if(deblock_left){
03122             for(i = !deblock_top; i<9; i++){
03123                 XCHG(h->left_border[i+17  ], src_cb[i*uvlinesize], temp8, xchg);
03124                 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
03125             }
03126         }
03127         if(deblock_top){
03128             XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
03129             XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
03130         }
03131     }
03132 }
03133 
03134 static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
03135     MpegEncContext * const s = &h->s;
03136     int i;
03137     
03138     src_y  -= 2 *   linesize;
03139     src_cb -= 2 * uvlinesize;
03140     src_cr -= 2 * uvlinesize;
03141 
03142     // There are two lines saved, the line above the the top macroblock of a pair,
03143     // and the line above the bottom macroblock
03144     h->left_border[0]= h->top_borders[0][s->mb_x][15];
03145     h->left_border[1]= h->top_borders[1][s->mb_x][15];
03146     for(i=2; i<34; i++){
03147         h->left_border[i]= src_y[15+i*  linesize];
03148     }
03149     
03150     *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y +  32*linesize);
03151     *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
03152     *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y +  33*linesize);
03153     *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
03154 
03155     if(!(s->flags&CODEC_FLAG_GRAY)){
03156         h->left_border[34     ]= h->top_borders[0][s->mb_x][16+7];
03157         h->left_border[34+   1]= h->top_borders[1][s->mb_x][16+7];
03158         h->left_border[34+18  ]= h->top_borders[0][s->mb_x][24+7];
03159         h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
03160         for(i=2; i<18; i++){
03161             h->left_border[i+34   ]= src_cb[7+i*uvlinesize];
03162             h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
03163         }
03164         *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
03165         *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
03166         *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
03167         *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
03168     }
03169 }
03170 
03171 static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
03172     MpegEncContext * const s = &h->s;
03173     int temp8, i;
03174     uint64_t temp64;
03175     int deblock_left = (s->mb_x > 0);
03176     int deblock_top  = (s->mb_y > 0);
03177 
03178     tprintf("xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
03179 
03180     src_y  -= 2 *   linesize + 1;
03181     src_cb -= 2 * uvlinesize + 1;
03182     src_cr -= 2 * uvlinesize + 1;
03183 
03184 #define XCHG(a,b,t,xchg)\
03185 t= a;\
03186 if(xchg)\
03187     a= b;\
03188 b= t;
03189 
03190     if(deblock_left){
03191         for(i = (!deblock_top)<<1; i<34; i++){
03192             XCHG(h->left_border[i     ], src_y [i*  linesize], temp8, xchg);
03193         }
03194     }
03195 
03196     if(deblock_top){
03197         XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
03198         XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
03199         XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
03200         XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
03201     }
03202 
03203     if(!(s->flags&CODEC_FLAG_GRAY)){
03204         if(deblock_left){
03205             for(i = (!deblock_top) << 1; i<18; i++){
03206                 XCHG(h->left_border[i+34   ], src_cb[i*uvlinesize], temp8, xchg);
03207                 XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
03208             }
03209         }
03210         if(deblock_top){
03211             XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
03212             XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
03213             XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
03214             XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
03215         }
03216     }
03217 }
03218 
03219 static void hl_decode_mb(H264Context *h){
03220     MpegEncContext * const s = &h->s;
03221     const int mb_x= s->mb_x;
03222     const int mb_y= s->mb_y;
03223     const int mb_xy= mb_x + mb_y*s->mb_stride;
03224     const int mb_type= s->current_picture.mb_type[mb_xy];
03225     uint8_t  *dest_y, *dest_cb, *dest_cr;
03226     int linesize, uvlinesize /*dct_offset*/;
03227     int i;
03228     int *block_offset = &h->block_offset[0];
03229     const unsigned int bottom = mb_y & 1;
03230     const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
03231     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
03232 
03233     if(!s->decode)
03234         return;
03235 
03236     dest_y  = s->current_picture.data[0] + (mb_y * 16* s->linesize  ) + mb_x * 16;
03237     dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
03238     dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
03239 
03240     if (h->mb_field_decoding_flag) {
03241         linesize = s->linesize * 2;
03242         uvlinesize = s->uvlinesize * 2;
03243         block_offset = &h->block_offset[24];
03244         if(mb_y&1){ //FIXME move out of this func?
03245             dest_y -= s->linesize*15;
03246             dest_cb-= s->uvlinesize*7;
03247             dest_cr-= s->uvlinesize*7;
03248         }
03249     } else {
03250         linesize = s->linesize;
03251         uvlinesize = s->uvlinesize;
03252 //        dct_offset = s->linesize * 16;
03253     }
03254     
03255     idct_add = transform_bypass
03256              ? IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4
03257              : IS_8x8DCT(mb_type) ? s->dsp.h264_idct8_add : s->dsp.h264_idct_add;
03258 
03259     if (IS_INTRA_PCM(mb_type)) {
03260         unsigned int x, y;
03261 
03262         // The pixels are stored in h->mb array in the same order as levels,
03263         // copy them in output in the correct order.
03264         for(i=0; i<16; i++) {
03265             for (y=0; y<4; y++) {
03266                 for (x=0; x<4; x++) {
03267                     *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
03268                 }
03269             }
03270         }
03271         for(i=16; i<16+4; i++) {
03272             for (y=0; y<4; y++) {
03273                 for (x=0; x<4; x++) {
03274                     *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
03275                 }
03276             }
03277         }
03278         for(i=20; i<20+4; i++) {
03279             for (y=0; y<4; y++) {
03280                 for (x=0; x<4; x++) {
03281                     *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
03282                 }
03283             }
03284         }
03285     } else {
03286         if(IS_INTRA(mb_type)){
03287             if(h->deblocking_filter) {
03288                 if (h->mb_aff_frame) {
03289                     if (!bottom)
03290                         xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1);
03291                 } else {
03292                     xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
03293                 }
03294             }
03295 
03296             if(!(s->flags&CODEC_FLAG_GRAY)){
03297                 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
03298                 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
03299             }
03300 
03301             if(IS_INTRA4x4(mb_type)){
03302                 if(!s->encoding){
03303                     if(IS_8x8DCT(mb_type)){
03304                         for(i=0; i<16; i+=4){
03305                             uint8_t * const ptr= dest_y + block_offset[i];
03306                             const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
03307                             h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
03308                                                    (h->topright_samples_available<<(i+1))&0x8000, linesize);
03309                             if(h->non_zero_count_cache[ scan8[i] ])
03310                                 idct_add(ptr, h->mb + i*16, linesize);
03311                         }
03312                     }else
03313                     for(i=0; i<16; i++){
03314                         uint8_t * const ptr= dest_y + block_offset[i];
03315                         uint8_t *topright;
03316                         const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
03317                         int tr;
03318 
03319                         if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
03320                             const int topright_avail= (h->topright_samples_available<<i)&0x8000;
03321                             assert(mb_y || linesize <= block_offset[i]);
03322                             if(!topright_avail){
03323                                 tr= ptr[3 - linesize]*0x01010101;
03324                                 topright= (uint8_t*) &tr;
03325                             }else 
03326                                 topright= ptr + 4 - linesize;
03327                         }else
03328                             topright= NULL;
03329 
03330                         h->pred4x4[ dir ](ptr, topright, linesize);
03331                         if(h->non_zero_count_cache[ scan8[i] ]){
03332                             if(s->codec_id == CODEC_ID_H264)
03333                                 idct_add(ptr, h->mb + i*16, linesize);
03334                             else
03335                                 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
03336                         }
03337                     }
03338                 }
03339             }else{
03340                 h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
03341                 if(s->codec_id == CODEC_ID_H264){
03342                     if(!transform_bypass)
03343                         h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
03344                 }else
03345                     svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
03346             }
03347             if(h->deblocking_filter) {
03348                 if (h->mb_aff_frame) {
03349                     if (bottom) {
03350                         uint8_t *pair_dest_y  = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize  ) + mb_x * 16;
03351                         uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
03352                         uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
03353                         s->mb_y--;
03354                         xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
03355                         s->mb_y++;
03356                     }
03357                 } else {
03358                     xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
03359                 }
03360             }
03361         }else if(s->codec_id == CODEC_ID_H264){
03362             hl_motion(h, dest_y, dest_cb, dest_cr,
03363                       s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, 
03364                       s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
03365                       s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
03366         }
03367 
03368 
03369         if(!IS_INTRA4x4(mb_type)){
03370             if(s->codec_id == CODEC_ID_H264){
03371                 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
03372                 for(i=0; i<16; i+=di){
03373                     if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
03374                         uint8_t * const ptr= dest_y + block_offset[i];
03375                         idct_add(ptr, h->mb + i*16, linesize);
03376                     }
03377                 }
03378             }else{
03379                 for(i=0; i<16; i++){
03380                     if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
03381                         uint8_t * const ptr= dest_y + block_offset[i];
03382                         svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
03383                     }
03384                 }
<