00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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];
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;
00166
00167 int prev_mb_skipped;
00168
00169
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);
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;
00221 uint32_t *mb2b8_xy;
00222 int b_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;
00239
00240 uint16_t (*dequant4_coeff)[16];
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
00250 int mb_aff_frame;
00251 int mb_field_decoding_flag;
00252
00253 int sub_mb_type[4];
00254
00255
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
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
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];
00303 Picture *short_ref[32];
00304 Picture *long_ref[32];
00305 Picture default_ref_list[2][32];
00306 Picture ref_list[2][32];
00307 Picture field_ref_list[2][32];
00308 Picture *delayed_pic[16];
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
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
00336 uint16_t *cbp_table;
00337 int top_cbp;
00338 int left_cbp;
00339
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){
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
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
00448
00449
00450 if(for_deblock && h->slice_num == 1)
00451 return;
00452
00453
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
00481 : (!curr_mb_frame_flag && !top_mb_frame_flag)
00482 ) {
00483 top_xy -= s->mb_stride;
00484 }
00485 if (bottom
00486 ? !curr_mb_frame_flag
00487 : (!curr_mb_frame_flag && !topleft_mb_frame_flag)
00488 ) {
00489 topleft_xy -= s->mb_stride;
00490 }
00491 if (bottom
00492 ? !curr_mb_frame_flag
00493 : (!curr_mb_frame_flag && !topright_mb_frame_flag)
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
00522 left_block[1]= 2;
00523 left_block[2]= 0;
00524 left_block[3]= 2;
00525
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
00613
00614
00615
00616
00617
00618
00619
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
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
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
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
00692
00693
00694
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
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] =
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]=
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
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]=
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
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 }
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
01028
01029
01030
01031
01032
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){
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
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
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 ){
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
01217
01218 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
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;
01222 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
01223 }else{
01224 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
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
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
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{
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){
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
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
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
01460 length=i;
01461 }
01462 break;
01463 }
01464 }
01465
01466 if(i>=length-1){
01467 *dst_length= length;
01468 *consumed= length+1;
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
01476 si=di=0;
01477 while(si<length){
01478
01479 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
01480 if(src[si+2]==3){
01481 dst[di++]= 0;
01482 dst[di++]= 0;
01483 si+=3;
01484 continue;
01485 }else
01486 break;
01487 }
01488
01489 dst[di++]= src[si++];
01490 }
01491
01492 *dst_length= di;
01493 *consumed= si + 1;
01494
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
01538
01539 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
01540 temp= h->rbsp_buffer;
01541
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];
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
01603
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;
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){
01637
01638 int i;
01639 int temp[16];
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
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
01764
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
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
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
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
01824
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
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;
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
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;
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;
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 > s->width + extra_width
02583 || full_my + 16 > 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, 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);
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, (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, (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
02660
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
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)
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;
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
03014
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
03052
03053 if(!s->obmc_scratchpad)
03054 s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
03055
03056
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
03068
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
03143
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 ;
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){
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
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
03263
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]){
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]){
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 }
03385 }
03386 }
03387
03388 if(!(s->flags&CODEC_FLAG_GRAY)){
03389 idct_add = transform_bypass ? s->dsp.add_pixels4 : s->dsp.h264_idct_add;
03390 if(!transform_bypass){
03391 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
03392 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
03393 }
03394 if(s->codec_id == CODEC_ID_H264){
03395 for(i=16; i<16+4; i++){
03396 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
03397 uint8_t * const ptr= dest_cb + block_offset[i];
03398 idct_add(ptr, h->mb + i*16, uvlinesize);
03399 }
03400 }
03401 for(i=20; i<20+4; i++){
03402 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
03403 uint8_t * const ptr= dest_cr + block_offset[i];
03404 idct_add(ptr, h->mb + i*16, uvlinesize);
03405 }
03406 }
03407 }else{
03408 for(i=16; i<16+4; i++){
03409 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
03410 uint8_t * const ptr= dest_cb + block_offset[i];
03411 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
03412 }
03413 }
03414 for(i=20; i<20+4; i++){
03415 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
03416 uint8_t * const ptr= dest_cr + block_offset[i];
03417 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
03418 }
03419 }
03420 }
03421 }
03422 }
03423 if(h->deblocking_filter) {
03424 if (h->mb_aff_frame) {
03425 const int mb_y = s->mb_y - 1;
03426 uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
03427 const int mb_xy= mb_x + mb_y*s->mb_stride;
03428 const int mb_type_top = s->current_picture.mb_type[mb_xy];
03429 const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
03430 uint8_t tmp = s->current_picture.data[1][384];
03431 if (!bottom) return;
03432 pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
03433 pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
03434 pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
03435
03436 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
03437
03438
03439 s->mb_y--;
03440 tprintf("call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
03441 fill_caches(h, mb_type_top, 1);
03442 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
03443 if (tmp != s->current_picture.data[1][384]) {
03444 tprintf("modified pixel 8,1 (1)\n");
03445 }
03446
03447 s->mb_y++;
03448 tprintf("call mbaff filter_mb\n");
03449 fill_caches(h, mb_type_bottom, 1);
03450 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
03451 if (tmp != s->current_picture.data[1][384]) {
03452 tprintf("modified pixel 8,1 (2)\n");
03453 }
03454 } else {
03455 tprintf("call filter_mb\n");
03456 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
03457 fill_caches(h, mb_type, 1);
03458 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
03459 }
03460 }
03461 }
03462
03466 static int fill_default_ref_list(H264Context *h){
03467 MpegEncContext * const s = &h->s;
03468 int i;
03469 int smallest_poc_greater_than_current = -1;
03470 Picture sorted_short_ref[32];
03471
03472 if(h->slice_type==B_TYPE){
03473 int out_i;
03474 int limit= INT_MIN;
03475
03476
03477 for(out_i=0; out_i<h->short_ref_count; out_i++){
03478 int best_i=INT_MIN;
03479 int best_poc=INT_MAX;
03480
03481 for(i=0; i<h->short_ref_count; i++){
03482 const int poc= h->short_ref[i]->poc;
03483 if(poc > limit && poc < best_poc){
03484 best_poc= poc;
03485 best_i= i;
03486 }
03487 }
03488
03489 assert(best_i != INT_MIN);
03490
03491 limit= best_poc;
03492 sorted_short_ref[out_i]= *h->short_ref[best_i];
03493 tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
03494 if (-1 == smallest_poc_greater_than_current) {
03495 if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
03496 smallest_poc_greater_than_current = out_i;
03497 }
03498 }
03499 }
03500 }
03501
03502 if(s->picture_structure == PICT_FRAME){
03503 if(h->slice_type==B_TYPE){
03504 int list;
03505 tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
03506
03507
03508 for(list=0; list<2; list++){
03509 int index = 0;
03510 int j= -99;
03511 int step= list ? -1 : 1;
03512
03513 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
03514 while(j<0 || j>= h->short_ref_count){
03515 if(j != -99 && step == (list ? -1 : 1))
03516 return -1;
03517 step = -step;
03518 j= smallest_poc_greater_than_current + (step>>1);
03519 }
03520 if(sorted_short_ref[j].reference != 3) continue;
03521 h->default_ref_list[list][index ]= sorted_short_ref[j];
03522 h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
03523 }
03524
03525 for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
03526 if(h->long_ref[i] == NULL) continue;
03527 if(h->long_ref[i]->reference != 3) continue;
03528
03529 h->default_ref_list[ list ][index ]= *h->long_ref[i];
03530 h->default_ref_list[ list ][index++].pic_id= i;;
03531 }
03532
03533 if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
03534
03535
03536 Picture temp= h->default_ref_list[1][0];
03537 h->default_ref_list[1][0] = h->default_ref_list[1][1];
03538 h->default_ref_list[1][1] = temp;
03539 }
03540
03541 if(index < h->ref_count[ list ])
03542 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
03543 }
03544 }else{
03545 int index=0;
03546 for(i=0; i<h->short_ref_count; i++){
03547 if(h->short_ref[i]->reference != 3) continue;
03548 h->default_ref_list[0][index ]= *h->short_ref[i];
03549 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
03550 }
03551 for(i = 0; i < 16; i++){
03552 if(h->long_ref[i] == NULL) continue;
03553 if(h->long_ref[i]->reference != 3) continue;
03554 h->default_ref_list[0][index ]= *h->long_ref[i];
03555 h->default_ref_list[0][index++].pic_id= i;;
03556 }
03557 if(index < h->ref_count[0])
03558 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
03559 }
03560 }else{
03561 if(h->slice_type==B_TYPE){
03562 }else{
03563
03564 }
03565 }
03566 #ifdef TRACE
03567 for (i=0; i<h->ref_count[0]; i++) {
03568 tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
03569 }
03570 if(h->slice_type==B_TYPE){
03571 for (i=0; i<h->ref_count[1]; i++) {
03572 tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
03573 }
03574 }
03575 #endif
03576 return 0;
03577 }
03578
03579 static void print_short_term(H264Context *h);
03580 static void print_long_term(H264Context *h);
03581
03582 static int decode_ref_pic_list_reordering(H264Context *h){
03583 MpegEncContext * const s = &h->s;
03584 int list, index;
03585
03586 print_short_term(h);
03587 print_long_term(h);
03588 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0;
03589
03590 for(list=0; list<2; list++){
03591 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
03592
03593 if(get_bits1(&s->gb)){
03594 int pred= h->curr_pic_num;
03595
03596 for(index=0; ; index++){
03597 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
03598 int pic_id;
03599 int i;
03600 Picture *ref = NULL;
03601
03602 if(reordering_of_pic_nums_idc==3)
03603 break;
03604
03605 if(index >= h->ref_count[list]){
03606 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
03607 return -1;
03608 }
03609
03610 if(reordering_of_pic_nums_idc<3){
03611 if(reordering_of_pic_nums_idc<2){
03612 const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
03613
03614 if(abs_diff_pic_num >= h->max_pic_num){
03615 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
03616 return -1;
03617 }
03618
03619 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
03620 else pred+= abs_diff_pic_num;
03621 pred &= h->max_pic_num - 1;
03622
03623 for(i= h->short_ref_count-1; i>=0; i--){
03624 ref = h->short_ref[i];
03625 assert(ref->reference == 3);
03626 assert(!ref->long_ref);
03627 if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0)
03628 break;
03629 }
03630 if(i>=0)
03631 ref->pic_id= ref->frame_num;
03632 }else{
03633 pic_id= get_ue_golomb(&s->gb);
03634 ref = h->long_ref[pic_id];
03635 ref->pic_id= pic_id;
03636 assert(ref->reference == 3);
03637 assert(ref->long_ref);
03638 i=0;
03639 }
03640
03641 if (i < 0) {
03642 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
03643 memset(&h->ref_list[list][index], 0, sizeof(Picture));
03644 } else {
03645 for(i=index; i+1<h->ref_count[list]; i++){
03646 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
03647 break;
03648 }
03649 for(; i > index; i--){
03650 h->ref_list[list][i]= h->ref_list[list][i-1];
03651 }
03652 h->ref_list[list][index]= *ref;
03653 }
03654 }else{
03655 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
03656 return -1;
03657 }
03658 }
03659 }
03660
03661 if(h->slice_type!=B_TYPE) break;
03662 }
03663 for(list=0; list<2; list++){
03664 for(index= 0; index < h->ref_count[list]; index++){
03665 if(!h->ref_list[list][index].data[0])
03666 h->ref_list[list][index]= s->current_picture;
03667 }
03668 if(h->slice_type!=B_TYPE) break;
03669 }
03670
03671 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
03672 direct_dist_scale_factor(h);
03673 direct_ref_list_init(h);
03674 return 0;
03675 }
03676
03677 static int pred_weight_table(H264Context *h){
03678 MpegEncContext * const s = &h->s;
03679 int list, i;
03680 int luma_def, chroma_def;
03681
03682 h->use_weight= 0;
03683 h->use_weight_chroma= 0;
03684 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
03685 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
03686 luma_def = 1<<h->luma_log2_weight_denom;
03687 chroma_def = 1<<h->chroma_log2_weight_denom;
03688
03689 for(list=0; list<2; list++){
03690 for(i=0; i<h->ref_count[list]; i++){
03691 int luma_weight_flag, chroma_weight_flag;
03692
03693 luma_weight_flag= get_bits1(&s->gb);
03694 if(luma_weight_flag){
03695 h->luma_weight[list][i]= get_se_golomb(&s->gb);
03696 h->luma_offset[list][i]= get_se_golomb(&s->gb);
03697 if( h->luma_weight[list][i] != luma_def
03698 || h->luma_offset[list][i] != 0)
03699 h->use_weight= 1;
03700 }else{
03701 h->luma_weight[list][i]= luma_def;
03702 h->luma_offset[list][i]= 0;
03703 }
03704
03705 chroma_weight_flag= get_bits1(&s->gb);
03706 if(chroma_weight_flag){
03707 int j;
03708 for(j=0; j<2; j++){
03709 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
03710 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
03711 if( h->chroma_weight[list][i][j] != chroma_def
03712 || h->chroma_offset[list][i][j] != 0)
03713 h->use_weight_chroma= 1;
03714 }
03715 }else{
03716 int j;
03717 for(j=0; j<2; j++){
03718 h->chroma_weight[list][i][j]= chroma_def;
03719 h->chroma_offset[list][i][j]= 0;
03720 }
03721 }
03722 }
03723 if(h->slice_type != B_TYPE) break;
03724 }
03725 h->use_weight= h->use_weight || h->use_weight_chroma;
03726 return 0;
03727 }
03728
03729 static void implicit_weight_table(H264Context *h){
03730 MpegEncContext * const s = &h->s;
03731 int ref0, ref1;
03732 int cur_poc = s->current_picture_ptr->poc;
03733
03734 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
03735 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
03736 h->use_weight= 0;
03737 h->use_weight_chroma= 0;
03738 return;
03739 }
03740
03741 h->use_weight= 2;
03742 h->use_weight_chroma= 2;
03743 h->luma_log2_weight_denom= 5;
03744 h->chroma_log2_weight_denom= 5;
03745
03746
03747 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
03748 int poc0 = h->ref_list[0][ref0].poc;
03749 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
03750 int poc1 = h->ref_list[1][ref1].poc;
03751 int td = clip(poc1 - poc0, -128, 127);
03752 if(td){
03753 int tb = clip(cur_poc - poc0, -128, 127);
03754 int tx = (16384 + (ABS(td) >> 1)) / td;
03755 int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
03756 if(dist_scale_factor < -64 || dist_scale_factor > 128)
03757 h->implicit_weight[ref0][ref1] = 32;
03758 else
03759 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
03760 }else
03761 h->implicit_weight[ref0][ref1] = 32;
03762 }
03763 }
03764 }
03765
03766 static inline void unreference_pic(H264Context *h, Picture *pic){
03767 int i;
03768 pic->reference=0;
03769 if(pic == h->delayed_output_pic)
03770 pic->reference=1;
03771 else{
03772 for(i = 0; h->delayed_pic[i]; i++)
03773 if(pic == h->delayed_pic[i]){
03774 pic->reference=1;
03775 break;
03776 }
03777 }
03778 }
03779
03783 static void idr(H264Context *h){
03784 int i;
03785
03786 for(i=0; i<16; i++){
03787 if (h->long_ref[i] != NULL) {
03788 unreference_pic(h, h->long_ref[i]);
03789 h->long_ref[i]= NULL;
03790 }
03791 }
03792 h->long_ref_count=0;
03793
03794 for(i=0; i<h->short_ref_count; i++){
03795 unreference_pic(h, h->short_ref[i]);
03796 h->short_ref[i]= NULL;
03797 }
03798 h->short_ref_count=0;
03799 }
03800
03801
03802 static void flush_dpb(AVCodecContext *avctx){
03803 H264Context *h= avctx->priv_data;
03804 int i;
03805 for(i=0; i<16; i++)
03806 h->delayed_pic[i]= NULL;
03807 h->delayed_output_pic= NULL;
03808 idr(h);
03809 if(h->s.current_picture_ptr)
03810 h->s.current_picture_ptr->reference= 0;
03811 }
03812
03817 static Picture * remove_short(H264Context *h, int frame_num){
03818 MpegEncContext * const s = &h->s;
03819 int i;
03820
03821 if(s->avctx->debug&FF_DEBUG_MMCO)
03822 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
03823
03824 for(i=0; i<h->short_ref_count; i++){
03825 Picture *pic= h->short_ref[i];
03826 if(s->avctx->debug&FF_DEBUG_MMCO)
03827 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
03828 if(pic->frame_num == frame_num){
03829 h->short_ref[i]= NULL;
03830 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
03831 h->short_ref_count--;
03832 return pic;
03833 }
03834 }
03835 return NULL;
03836 }
03837
03842 static Picture * remove_long(H264Context *h, int i){
03843 Picture *pic;
03844
03845 pic= h->long_ref[i];
03846 h->long_ref[i]= NULL;
03847 if(pic) h->long_ref_count--;
03848
03849 return pic;
03850 }
03851
03855 static void print_short_term(H264Context *h) {
03856 uint32_t i;
03857 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
03858 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
03859 for(i=0; i<h->short_ref_count; i++){
03860 Picture *pic= h->short_ref[i];
03861 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
03862 }
03863 }
03864 }
03865
03869 static void print_long_term(H264Context *h) {
03870 uint32_t i;
03871 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
03872 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
03873 for(i = 0; i < 16; i++){
03874 Picture *pic= h->long_ref[i];
03875 if (pic) {
03876 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
03877 }
03878 }
03879 }
03880 }
03881
03885 static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
03886 MpegEncContext * const s = &h->s;
03887 int i, j;
03888 int current_is_long=0;
03889 Picture *pic;
03890
03891 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
03892 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
03893
03894 for(i=0; i<mmco_count; i++){
03895 if(s->avctx->debug&FF_DEBUG_MMCO)
03896 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
03897
03898 switch(mmco[i].opcode){
03899 case MMCO_SHORT2UNUSED:
03900 pic= remove_short(h, mmco[i].short_frame_num);
03901 if(pic)
03902 unreference_pic(h, pic);
03903 else if(s->avctx->debug&FF_DEBUG_MMCO)
03904 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
03905 break;
03906 case MMCO_SHORT2LONG:
03907 pic= remove_long(h, mmco[i].long_index);
03908 if(pic) unreference_pic(h, pic);
03909
03910 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
03911 h->long_ref[ mmco[i].long_index ]->long_ref=1;
03912 h->long_ref_count++;
03913 break;
03914 case MMCO_LONG2UNUSED:
03915 pic= remove_long(h, mmco[i].long_index);
03916 if(pic)
03917 unreference_pic(h, pic);
03918 else if(s->avctx->debug&FF_DEBUG_MMCO)
03919 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
03920 break;
03921 case MMCO_LONG:
03922 pic= remove_long(h, mmco[i].long_index);
03923 if(pic) unreference_pic(h, pic);
03924
03925 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
03926 h->long_ref[ mmco[i].long_index ]->long_ref=1;
03927 h->long_ref_count++;
03928
03929 current_is_long=1;
03930 break;
03931 case MMCO_SET_MAX_LONG:
03932 assert(mmco[i].long_index <= 16);
03933
03934 for(j = mmco[i].long_index; j<16; j++){
03935 pic = remove_long(h, j);
03936 if (pic) unreference_pic(h, pic);
03937 }
03938 break;
03939 case MMCO_RESET:
03940 while(h->short_ref_count){
03941 pic= remove_short(h, h->short_ref[0]->frame_num);
03942 unreference_pic(h, pic);
03943 }
03944 for(j = 0; j < 16; j++) {
03945 pic= remove_long(h, j);
03946 if(pic) unreference_pic(h, pic);
03947 }
03948 break;
03949 default: assert(0);
03950 }
03951 }
03952
03953 if(!current_is_long){
03954 pic= remove_short(h, s->current_picture_ptr->frame_num);
03955 if(pic){
03956 unreference_pic(h, pic);
03957 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
03958 }
03959
03960 if(h->short_ref_count)
03961 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
03962
03963 h->short_ref[0]= s->current_picture_ptr;
03964 h->short_ref[0]->long_ref=0;
03965 h->short_ref_count++;
03966 }
03967
03968 print_short_term(h);
03969 print_long_term(h);
03970 return 0;
03971 }
03972
03973 static int decode_ref_pic_marking(H264Context *h){
03974 MpegEncContext * const s = &h->s;
03975 int i;
03976
03977 if(h->nal_unit_type == NAL_IDR_SLICE){
03978 s->broken_link= get_bits1(&s->gb) -1;
03979 h->mmco[0].long_index= get_bits1(&s->gb) - 1;
03980 if(h->mmco[0].long_index == -1)
03981 h->mmco_index= 0;
03982 else{
03983 h->mmco[0].opcode= MMCO_LONG;
03984 h->mmco_index= 1;
03985 }
03986 }else{
03987 if(get_bits1(&s->gb)){
03988 for(i= 0; i<MAX_MMCO_COUNT; i++) {
03989 MMCOOpcode opcode= get_ue_golomb(&s->gb);;
03990
03991 h->mmco[i].opcode= opcode;
03992 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
03993 h->mmco[i].short_frame_num= (h->frame_num - get_ue_golomb(&s->gb) - 1) & ((1<<h->sps.log2_max_frame_num)-1);
03994
03995
03996
03997
03998 }
03999 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
04000 h->mmco[i].long_index= get_ue_golomb(&s->gb);
04001 if( h->mmco[i].long_index >= 16){
04002 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
04003 return -1;
04004 }
04005 }
04006
04007 if(opcode > MMCO_LONG){
04008 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
04009 return -1;
04010 }
04011 if(opcode == MMCO_END)
04012 break;
04013 }
04014 h->mmco_index= i;
04015 }else{
04016 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
04017
04018 if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){
04019 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
04020 h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
04021 h->mmco_index= 1;
04022 }else
04023 h->mmco_index= 0;
04024 }
04025 }
04026
04027 return 0;
04028 }
04029
04030 static int init_poc(H264Context *h){
04031 MpegEncContext * const s = &h->s;
04032 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
04033 int field_poc[2];
04034
04035 if(h->nal_unit_type == NAL_IDR_SLICE){
04036 h->frame_num_offset= 0;
04037 }else{
04038 if(h->frame_num < h->prev_frame_num)
04039 h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
04040 else
04041 h->frame_num_offset= h->prev_frame_num_offset;
04042 }
04043
04044 if(h->sps.poc_type==0){
04045 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
04046
04047 if(h->nal_unit_type == NAL_IDR_SLICE){
04048 h->prev_poc_msb=
04049 h->prev_poc_lsb= 0;
04050 }
04051
04052 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
04053 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
04054 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
04055 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
04056 else
04057 h->poc_msb = h->prev_poc_msb;
04058
04059 field_poc[0] =
04060 field_poc[1] = h->poc_msb + h->poc_lsb;
04061 if(s->picture_structure == PICT_FRAME)
04062 field_poc[1] += h->delta_poc_bottom;
04063 }else if(h->sps.poc_type==1){
04064 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
04065 int i;
04066
04067 if(h->sps.poc_cycle_length != 0)
04068 abs_frame_num = h->frame_num_offset + h->frame_num;
04069 else
04070 abs_frame_num = 0;
04071
04072 if(h->nal_ref_idc==0 && abs_frame_num > 0)
04073 abs_frame_num--;
04074
04075 expected_delta_per_poc_cycle = 0;
04076 for(i=0; i < h->sps.poc_cycle_length; i++)
04077 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ];
04078
04079 if(abs_frame_num > 0){
04080 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
04081 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
04082
04083 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
04084 for(i = 0; i <= frame_num_in_poc_cycle; i++)
04085 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
04086 } else
04087 expectedpoc = 0;
04088
04089 if(h->nal_ref_idc == 0)
04090 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
04091
04092 field_poc[0] = expectedpoc + h->delta_poc[0];
04093 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
04094
04095 if(s->picture_structure == PICT_FRAME)
04096 field_poc[1] += h->delta_poc[1];
04097 }else{
04098 int poc;
04099 if(h->nal_unit_type == NAL_IDR_SLICE){
04100 poc= 0;
04101 }else{
04102 if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
04103 else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
04104 }
04105 field_poc[0]= poc;
04106 field_poc[1]= poc;
04107 }
04108
04109 if(s->picture_structure != PICT_BOTTOM_FIELD)
04110 s->current_picture_ptr->field_poc[0]= field_poc[0];
04111 if(s->picture_structure != PICT_TOP_FIELD)
04112 s->current_picture_ptr->field_poc[1]= field_poc[1];
04113 if(s->picture_structure == PICT_FRAME)
04114 s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
04115
04116 return 0;
04117 }
04118
04123 static int decode_slice_header(H264Context *h){
04124 MpegEncContext * const s = &h->s;
04125 int first_mb_in_slice, pps_id;
04126 int num_ref_idx_active_override_flag;
04127 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
04128 int slice_type;
04129 int default_ref_list_done = 0;
04130
04131 s->current_picture.reference= h->nal_ref_idc != 0;
04132 s->dropable= h->nal_ref_idc == 0;
04133
04134 first_mb_in_slice= get_ue_golomb(&s->gb);
04135
04136 slice_type= get_ue_golomb(&s->gb);
04137 if(slice_type > 9){
04138 av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
04139 return -1;
04140 }
04141 if(slice_type > 4){
04142 slice_type -= 5;
04143 h->slice_type_fixed=1;
04144 }else
04145 h->slice_type_fixed=0;
04146
04147 slice_type= slice_type_map[ slice_type ];
04148 if (slice_type == I_TYPE
04149 || (h->slice_num != 0 && slice_type == h->slice_type) ) {
04150 default_ref_list_done = 1;
04151 }
04152 h->slice_type= slice_type;
04153
04154 s->pict_type= h->slice_type;
04155
04156 pps_id= get_ue_golomb(&s->gb);
04157 if(pps_id>255){
04158 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
04159 return -1;
04160 }
04161 h->pps= h->pps_buffer[pps_id];
04162 if(h->pps.slice_group_count == 0){
04163 av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
04164 return -1;
04165 }
04166
04167 h->sps= h->sps_buffer[ h->pps.sps_id ];
04168 if(h->sps.log2_max_frame_num == 0){
04169 av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
04170 return -1;
04171 }
04172
04173 s->mb_width= h->sps.mb_width;
04174 s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
04175
04176 h->b_stride= s->mb_width*4 + 1;
04177 h->b8_stride= s->mb_width*2 + 1;
04178
04179 s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
04180 if(h->sps.frame_mbs_only_flag)
04181 s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
04182 else
04183 s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom);
04184
04185 if (s->context_initialized
04186 && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
04187 free_tables(h);
04188 MPV_common_end(s);
04189 }
04190 if (!s->context_initialized) {
04191 if (MPV_common_init(s) < 0)
04192 return -1;
04193
04194 if(s->dsp.h264_idct_add == ff_h264_idct_add_c){
04195 memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
04196 memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
04197 }else{
04198 int i;
04199 for(i=0; i<16; i++){
04200 #define T(x) (x>>2) | ((x<<2) & 0xF)
04201 h->zigzag_scan[i] = T(zigzag_scan[i]);
04202 h-> field_scan[i] = T( field_scan[i]);
04203 }
04204 }
04205 if(h->sps.transform_bypass){
04206 h->zigzag_scan_q0 = zigzag_scan;
04207 h->field_scan_q0 = field_scan;
04208 }else{
04209 h->zigzag_scan_q0 = h->zigzag_scan;
04210 h->field_scan_q0 = h->field_scan;
04211 }
04212
04213 alloc_tables(h);
04214
04215 s->avctx->width = s->width;
04216 s->avctx->height = s->height;
04217 s->avctx->sample_aspect_ratio= h->sps.sar;
04218 if(!s->avctx->sample_aspect_ratio.den)
04219 s->avctx->sample_aspect_ratio.den = 1;
04220
04221 if(h->sps.timing_info_present_flag){
04222 s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
04223 }
04224 }
04225
04226 if(h->slice_num == 0){
04227 frame_start(h);
04228 }
04229
04230 s->current_picture_ptr->frame_num=
04231 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
04232
04233 h->mb_aff_frame = 0;
04234 if(h->sps.frame_mbs_only_flag){
04235 s->picture_structure= PICT_FRAME;
04236 }else{
04237 if(get_bits1(&s->gb)) {
04238 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb);
04239 } else {
04240 s->picture_structure= PICT_FRAME;
04241 first_mb_in_slice <<= h->sps.mb_aff;
04242 h->mb_aff_frame = h->sps.mb_aff;
04243 }
04244 }
04245
04246 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
04247 s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width;
04248 if(s->mb_y >= s->mb_height){
04249 return -1;
04250 }
04251
04252 if(s->picture_structure==PICT_FRAME){
04253 h->curr_pic_num= h->frame_num;
04254 h->max_pic_num= 1<< h->sps.log2_max_frame_num;
04255 }else{
04256 h->curr_pic_num= 2*h->frame_num;
04257 h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
04258 }
04259
04260 if(h->nal_unit_type == NAL_IDR_SLICE){
04261 get_ue_golomb(&s->gb);
04262 }
04263
04264 if(h->sps.poc_type==0){
04265 h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
04266
04267 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
04268 h->delta_poc_bottom= get_se_golomb(&s->gb);
04269 }
04270 }
04271
04272 if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
04273 h->delta_poc[0]= get_se_golomb(&s->gb);
04274
04275 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
04276 h->delta_poc[1]= get_se_golomb(&s->gb);
04277 }
04278
04279 init_poc(h);
04280
04281 if(h->pps.redundant_pic_cnt_present){
04282 h->redundant_pic_count= get_ue_golomb(&s->gb);
04283 }
04284
04285
04286 h->ref_count[0]= h->pps.ref_count[0];
04287 h->ref_count[1]= h->pps.ref_count[1];
04288
04289 if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
04290 if(h->slice_type == B_TYPE){
04291 h->direct_spatial_mv_pred= get_bits1(&s->gb);
04292 }
04293 num_ref_idx_active_override_flag= get_bits1(&s->gb);
04294
04295 if(num_ref_idx_active_override_flag){
04296 h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
04297 if(h->slice_type==B_TYPE)
04298 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
04299
04300 if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
04301 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
04302 return -1;
04303 }
04304 }
04305 }
04306
04307 if(!default_ref_list_done){
04308 fill_default_ref_list(h);
04309 }
04310
04311 if(decode_ref_pic_list_reordering(h) < 0)
04312 return -1;
04313
04314 if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
04315 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
04316 pred_weight_table(h);
04317 else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
04318 implicit_weight_table(h);
04319 else
04320 h->use_weight = 0;
04321
04322 if(s->current_picture.reference)
04323 decode_ref_pic_marking(h);
04324
04325 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
04326 h->cabac_init_idc = get_ue_golomb(&s->gb);
04327
04328 h->last_qscale_diff = 0;
04329 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
04330 if(s->qscale<0 || s->qscale>51){
04331 av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
04332 return -1;
04333 }
04334 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
04335
04336 if(h->slice_type == SP_TYPE){
04337 get_bits1(&s->gb);
04338 }
04339 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
04340 get_se_golomb(&s->gb);
04341 }
04342
04343 h->deblocking_filter = 1;
04344 h->slice_alpha_c0_offset = 0;
04345 h->slice_beta_offset = 0;
04346 if( h->pps.deblocking_filter_parameters_present ) {
04347 h->deblocking_filter= get_ue_golomb(&s->gb);
04348 if(h->deblocking_filter < 2)
04349 h->deblocking_filter^= 1;
04350
04351 if( h->deblocking_filter ) {
04352 h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
04353 h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
04354 }
04355 }
04356 if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
04357 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
04358 ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
04359 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
04360 h->deblocking_filter= 0;
04361
04362 #if 0 //FMO
04363 if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
04364 slice_group_change_cycle= get_bits(&s->gb, ?);
04365 #endif
04366
04367 h->slice_num++;
04368
04369 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
04370 av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
04371 h->slice_num,
04372 (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
04373 first_mb_in_slice,
04374 av_get_pict_type_char(h->slice_type),
04375 pps_id, h->frame_num,
04376 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
04377 h->ref_count[0], h->ref_count[1],
04378 s->qscale,
04379 h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
04380 h->use_weight,
04381 h->use_weight==1 && h->use_weight_chroma ? "c" : ""
04382 );
04383 }
04384
04385 return 0;
04386 }
04387
04391 static inline int get_level_prefix(GetBitContext *gb){
04392 unsigned int buf;
04393 int log;
04394
04395 OPEN_READER(re, gb);
04396 UPDATE_CACHE(re, gb);
04397 buf=GET_CACHE(re, gb);
04398
04399 log= 32 - av_log2(buf);
04400 #ifdef TRACE
04401 print_bin(buf>>(32-log), log);
04402 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
04403 #endif
04404
04405 LAST_SKIP_BITS(re, gb, log);
04406 CLOSE_READER(re, gb);
04407
04408 return log-1;
04409 }
04410
04411 static inline int get_dct8x8_allowed(H264Context *h){
04412 int i;
04413 for(i=0; i<4; i++){
04414 if(!IS_SUB_8X8(h->sub_mb_type[i])
04415 || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
04416 return 0;
04417 }
04418 return 1;
04419 }
04420
04428 static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint16_t *qmul, int max_coeff){
04429 MpegEncContext * const s = &h->s;
04430 static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
04431 int level[16], run[16];
04432 int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
04433
04434
04435
04436 if(n == CHROMA_DC_BLOCK_INDEX){
04437 coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
04438 total_coeff= coeff_token>>2;
04439 }else{
04440 if(n == LUMA_DC_BLOCK_INDEX){
04441 total_coeff= pred_non_zero_count(h, 0);
04442 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
04443 total_coeff= coeff_token>>2;
04444 }else{
04445 total_coeff= pred_non_zero_count(h, n);
04446 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
04447 total_coeff= coeff_token>>2;
04448 h->non_zero_count_cache[ scan8[n] ]= total_coeff;
04449 }
04450 }
04451
04452
04453
04454 if(total_coeff==0)
04455 return 0;
04456
04457 trailing_ones= coeff_token&3;
04458 tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
04459 assert(total_coeff<=16);
04460
04461 for(i=0; i<trailing_ones; i++){
04462 level[i]= 1 - 2*get_bits1(gb);
04463 }
04464
04465 suffix_length= total_coeff > 10 && trailing_ones < 3;
04466
04467 for(; i<total_coeff; i++){
04468 const int prefix= get_level_prefix(gb);
04469 int level_code, mask;
04470
04471 if(prefix<14){
04472 if(suffix_length)
04473 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);
04474 else
04475 level_code= (prefix<<suffix_length);
04476 }else if(prefix==14){
04477 if(suffix_length)
04478 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);
04479 else
04480 level_code= prefix + get_bits(gb, 4);
04481 }else if(prefix==15){
04482 level_code= (prefix<<suffix_length) + get_bits(gb, 12);
04483 if(suffix_length==0) level_code+=15;
04484 }else{
04485 av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
04486 return -1;
04487 }
04488
04489 if(i==trailing_ones && i<3) level_code+= 2;
04490
04491 mask= -(level_code&1);
04492 level[i]= (((2+level_code)>>1) ^ mask) - mask;
04493
04494 if(suffix_length==0) suffix_length=1;
04495
04496 #if 1
04497 if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
04498 #else
04499 if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
04500
04501 #endif
04502 tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
04503 }
04504
04505 if(total_coeff == max_coeff)
04506 zeros_left=0;
04507 else{
04508 if(n == CHROMA_DC_BLOCK_INDEX)
04509 zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
04510 else
04511 zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
04512 }
04513
04514 for(i=0; i<total_coeff-1; i++){
04515 if(zeros_left <=0)
04516 break;
04517 else if(zeros_left < 7){
04518 run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
04519 }else{
04520 run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
04521 }
04522 zeros_left -= run[i];
04523 }
04524
04525 if(zeros_left<0){
04526 av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
04527 return -1;
04528 }
04529
04530 for(; i<total_coeff-1; i++){
04531 run[i]= 0;
04532 }
04533
04534 run[i]= zeros_left;
04535
04536 coeff_num=-1;
04537 if(n > 24){
04538 for(i=total_coeff-1; i>=0; i--){
04539 int j;
04540
04541 coeff_num += run[i] + 1;
04542 j= scantable[ coeff_num ];
04543
04544 block[j]= level[i];
04545 }
04546 }else{
04547 for(i=total_coeff-1; i>=0; i--){
04548 int j;
04549
04550 coeff_num += run[i] + 1;
04551 j= scantable[ coeff_num ];
04552
04553 block[j]= level[i] * qmul[j];
04554
04555 }
04556 }
04557 return 0;
04558 }
04559
04563 static void decode_mb_skip(H264Context *h){
04564 MpegEncContext * const s = &h->s;
04565 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04566 int mb_type=0;
04567
04568 memset(h->non_zero_count[mb_xy], 0, 16);
04569 memset(h->non_zero_count_cache + 8, 0, 8*5);
04570
04571 if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){
04572 h->mb_field_decoding_flag= get_bits1(&s->gb);
04573 }
04574 if(h->mb_field_decoding_flag)
04575 mb_type|= MB_TYPE_INTERLACED;
04576
04577 if( h->slice_type == B_TYPE )
04578 {
04579
04580 mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
04581
04582 fill_caches(h, mb_type, 0);
04583 pred_direct_motion(h, &mb_type);
04584 if(h->pps.cabac){
04585 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
04586 fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
04587 }
04588 }
04589 else
04590 {
04591 int mx, my;
04592 mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
04593
04594 fill_caches(h, mb_type, 0);
04595 pred_pskip_motion(h, &mx, &my);
04596 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
04597 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
04598 if(h->pps.cabac)
04599 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
04600 }
04601
04602 write_back_motion(h, mb_type);
04603 s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
04604 s->current_picture.qscale_table[mb_xy]= s->qscale;
04605 h->slice_table[ mb_xy ]= h->slice_num;
04606 h->prev_mb_skipped= 1;
04607 }
04608
04613 static int decode_mb_cavlc(H264Context *h){
04614 MpegEncContext * const s = &h->s;
04615 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04616 int mb_type, partition_count, cbp;
04617 int dct8x8_allowed= h->pps.transform_8x8_mode;
04618
04619 s->dsp.clear_blocks(h->mb);
04620
04621 tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
04622 cbp = 0;
04623
04624 if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
04625 if(s->mb_skip_run==-1)
04626 s->mb_skip_run= get_ue_golomb(&s->gb);
04627
04628 if (s->mb_skip_run--) {
04629 decode_mb_skip(h);
04630 return 0;
04631 }
04632 }
04633 if(h->mb_aff_frame){
04634 if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
04635 h->mb_field_decoding_flag = get_bits1(&s->gb);
04636 }else
04637 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
04638
04639 h->prev_mb_skipped= 0;
04640
04641 mb_type= get_ue_golomb(&s->gb);
04642 if(h->slice_type == B_TYPE){
04643 if(mb_type < 23){
04644 partition_count= b_mb_type_info[mb_type].partition_count;
04645 mb_type= b_mb_type_info[mb_type].type;
04646 }else{
04647 mb_type -= 23;
04648 goto decode_intra_mb;
04649 }
04650 }else if(h->slice_type == P_TYPE ){
04651 if(mb_type < 5){
04652 partition_count= p_mb_type_info[mb_type].partition_count;
04653 mb_type= p_mb_type_info[mb_type].type;
04654 }else{
04655 mb_type -= 5;
04656 goto decode_intra_mb;
04657 }
04658 }else{
04659 assert(h->slice_type == I_TYPE);
04660 decode_intra_mb:
04661 if(mb_type > 25){
04662 av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
04663 return -1;
04664 }
04665 partition_count=0;
04666 cbp= i_mb_type_info[mb_type].cbp;
04667 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
04668 mb_type= i_mb_type_info[mb_type].type;
04669 }
04670
04671 if(h->mb_field_decoding_flag)
04672 mb_type |= MB_TYPE_INTERLACED;
04673
04674 h->slice_table[ mb_xy ]= h->slice_num;
04675
04676 if(IS_INTRA_PCM(mb_type)){
04677 unsigned int x, y;
04678
04679
04680 align_get_bits(&s->gb);
04681
04682
04683 for(y=0; y<16; y++){
04684 const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
04685 for(x=0; x<16; x++){
04686 tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04687 h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
04688 }
04689 }
04690 for(y=0; y<8; y++){
04691 const int index= 256 + 4*(y&3) + 32*(y>>2);
04692 for(x=0; x<8; x++){
04693 tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04694 h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
04695 }
04696 }
04697 for(y=0; y<8; y++){
04698 const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
04699 for(x=0; x<8; x++){
04700 tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04701 h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
04702 }
04703 }
04704
04705
04706 s->current_picture.qscale_table[mb_xy]= 0;
04707 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
04708
04709 memset(h->non_zero_count[mb_xy], 16, 16);
04710
04711 s->current_picture.mb_type[mb_xy]= mb_type;
04712 return 0;
04713 }
04714
04715 fill_caches(h, mb_type, 0);
04716
04717
04718 if(IS_INTRA(mb_type)){
04719
04720 if(IS_INTRA4x4(mb_type)){
04721 int i;
04722 int di = 1;
04723 if(dct8x8_allowed && get_bits1(&s->gb)){
04724 mb_type |= MB_TYPE_8x8DCT;
04725 di = 4;
04726 }
04727
04728
04729 for(i=0; i<16; i+=di){
04730 const int mode_coded= !get_bits1(&s->gb);
04731 const int predicted_mode= pred_intra_mode(h, i);
04732 int mode;
04733
04734 if(mode_coded){
04735 const int rem_mode= get_bits(&s->gb, 3);
04736 if(rem_mode<predicted_mode)
04737 mode= rem_mode;
04738 else
04739 mode= rem_mode + 1;
04740 }else{
04741 mode= predicted_mode;
04742 }
04743
04744 if(di==4)
04745 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
04746 else
04747 h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
04748 }
04749 write_back_intra_pred_mode(h);
04750 if( check_intra4x4_pred_mode(h) < 0)
04751 return -1;
04752 }else{
04753 h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
04754 if(h->intra16x16_pred_mode < 0)
04755 return -1;
04756 }
04757 h->chroma_pred_mode= get_ue_golomb(&s->gb);
04758
04759 h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
04760 if(h->chroma_pred_mode < 0)
04761 return -1;
04762 }else if(partition_count==4){
04763 int i, j, sub_partition_count[4], list, ref[2][4];
04764
04765 if(h->slice_type == B_TYPE){
04766 for(i=0; i<4; i++){
04767 h->sub_mb_type[i]= get_ue_golomb(&s->gb);
04768 if(h->sub_mb_type[i] >=13){
04769 av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
04770 return -1;
04771 }
04772 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
04773 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
04774 }
04775 if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
04776 || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3]))
04777 pred_direct_motion(h, &mb_type);
04778 }else{
04779 assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE);
04780 for(i=0; i<4; i++){
04781 h->sub_mb_type[i]= get_ue_golomb(&s->gb);
04782 if(h->sub_mb_type[i] >=4){
04783 av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
04784 return -1;
04785 }
04786 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
04787 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
04788 }
04789 }
04790
04791 for(list=0; list<2; list++){
04792 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
04793 if(ref_count == 0) continue;
04794 if (h->mb_aff_frame && h->mb_field_decoding_flag) {
04795 ref_count <<= 1;
04796 }
04797 for(i=0; i<4; i++){
04798 if(IS_DIRECT(h->sub_mb_type[i])) continue;
04799 if(IS_DIR(h->sub_mb_type[i], 0, list)){
04800 ref[list][i] = get_te0_golomb(&s->gb, ref_count);
04801 }else{
04802
04803 ref[list][i] = -1;
04804 }
04805 }
04806 }
04807
04808 if(dct8x8_allowed)
04809 dct8x8_allowed = get_dct8x8_allowed(h);
04810
04811 for(list=0; list<2; list++){
04812 const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
04813 if(ref_count == 0) continue;
04814
04815 for(i=0; i<4; i++){
04816 if(IS_DIRECT(h->sub_mb_type[i])) continue;
04817 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
04818 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
04819
04820 if(IS_DIR(h->sub_mb_type[i], 0, list)){
04821 const int sub_mb_type= h->sub_mb_type[i];
04822 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
04823 for(j=0; j<sub_partition_count[i]; j++){
04824 int mx, my;
04825 const int index= 4*i + block_width*j;
04826 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
04827 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
04828 mx += get_se_golomb(&s->gb);
04829 my += get_se_golomb(&s->gb);
04830 tprintf("final mv:%d %d\n", mx, my);
04831
04832 if(IS_SUB_8X8(sub_mb_type)){
04833 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
04834 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
04835 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
04836 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
04837 }else if(IS_SUB_8X4(sub_mb_type)){
04838 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
04839 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
04840 }else if(IS_SUB_4X8(sub_mb_type)){
04841 mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
04842 mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
04843 }else{
04844 assert(IS_SUB_4X4(sub_mb_type));
04845 mv_cache[ 0 ][0]= mx;
04846 mv_cache[ 0 ][1]= my;
04847 }
04848 }
04849 }else{
04850 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
04851 p[0] = p[1]=
04852 p[8] = p[9]= 0;
04853 }
04854 }
04855 }
04856 }else if(IS_DIRECT(mb_type)){
04857 pred_direct_motion(h, &mb_type);
04858 dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
04859 }else{
04860 int list, mx, my, i;
04861
04862 if(IS_16X16(mb_type)){
04863 for(list=0; list<2; list++){
04864 if(h->ref_count[list]>0){
04865 if(IS_DIR(mb_type, 0, list)){
04866 const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
04867 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
04868 }else
04869 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
04870 }
04871 }
04872 for(list=0; list<2; list++){
04873 if(IS_DIR(mb_type, 0, list)){
04874 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
04875 mx += get_se_golomb(&s->gb);
04876 my += get_se_golomb(&s->gb);
04877 tprintf("final mv:%d %d\n", mx, my);
04878
04879 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
04880 }else
04881 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
04882 }
04883 }
04884 else if(IS_16X8(mb_type)){
04885 for(list=0; list<2; list++){
04886 if(h->ref_count[list]>0){
04887 for(i=0; i<2; i++){
04888 if(IS_DIR(mb_type, i, list)){
04889 const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
04890 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
04891 }else
04892 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
04893 }
04894 }
04895 }
04896 for(list=0; list<2; list++){
04897 for(i=0; i<2; i++){
04898 if(IS_DIR(mb_type, i, list)){
04899 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
04900 mx += get_se_golomb(&s->gb);
04901 my += get_se_golomb(&s->gb);
04902 tprintf("final mv:%d %d\n", mx, my);
04903
04904 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
04905 }else
04906 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
04907 }
04908 }
04909 }else{
04910 assert(IS_8X16(mb_type));
04911 for(list=0; list<2; list++){
04912 if(h->ref_count[list]>0){
04913 for(i=0; i<2; i++){
04914 if(IS_DIR(mb_type, i, list)){
04915 const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
04916 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
04917 }else
04918 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
04919 }
04920 }
04921 }
04922 for(list=0; list<2; list++){
04923 for(i=0; i<2; i++){
04924 if(IS_DIR(mb_type, i, list)){
04925 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
04926 mx += get_se_golomb(&s->gb);
04927 my += get_se_golomb(&s->gb);
04928 tprintf("final mv:%d %d\n", mx, my);
04929
04930 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
04931 }else
04932 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
04933 }
04934 }
04935 }
04936 }
04937
04938 if(IS_INTER(mb_type))
04939 write_back_motion(h, mb_type);
04940
04941 if(!IS_INTRA16x16(mb_type)){
04942 cbp= get_ue_golomb(&s->gb);
04943 if(cbp > 47){
04944 av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
04945 return -1;
04946 }
04947
04948 if(IS_INTRA4x4(mb_type))
04949 cbp= golomb_to_intra4x4_cbp[cbp];
04950 else
04951 cbp= golomb_to_inter_cbp[cbp];
04952 }
04953
04954 if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
04955 if(get_bits1(&s->gb))
04956 mb_type |= MB_TYPE_8x8DCT;
04957 }
04958 s->current_picture.mb_type[mb_xy]= mb_type;
04959
04960 if(cbp || IS_INTRA16x16(mb_type)){
04961 int i8x8, i4x4, chroma_idx;
04962 int chroma_qp, dquant;
04963 GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
04964 const uint8_t *scan, *dc_scan;
04965
04966
04967
04968 if(IS_INTERLACED(mb_type)){
04969 scan= s->qscale ? h->field_scan : h->field_scan_q0;
04970 dc_scan= luma_dc_field_scan;
04971 }else{
04972 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
04973 dc_scan= luma_dc_zigzag_scan;
04974 }
04975
04976 dquant= get_se_golomb(&s->gb);
04977
04978 if( dquant > 25 || dquant < -26 ){
04979 av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
04980 return -1;
04981 }
04982
04983 s->qscale += dquant;
04984 if(((unsigned)s->qscale) > 51){
04985 if(s->qscale<0) s->qscale+= 52;
04986 else s->qscale-= 52;
04987 }
04988
04989 h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
04990 if(IS_INTRA16x16(mb_type)){
04991 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[s->qscale], 16) < 0){
04992 return -1;
04993 }
04994
04995 assert((cbp&15) == 0 || (cbp&15) == 15);
04996
04997 if(cbp&15){
04998 for(i8x8=0; i8x8<4; i8x8++){
04999 for(i4x4=0; i4x4<4; i4x4++){
05000 const int index= i4x4 + 4*i8x8;
05001 if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[s->qscale], 15) < 0 ){
05002 return -1;
05003 }
05004 }
05005 }
05006 }else{
05007 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
05008 }
05009 }else{
05010 for(i8x8=0; i8x8<4; i8x8++){
05011 if(cbp & (1<<i8x8)){
05012 if(IS_8x8DCT(mb_type)){
05013 DCTELEM *buf = &h->mb[64*i8x8];
05014 uint8_t *nnz;
05015 for(i4x4=0; i4x4<4; i4x4++){
05016 if( decode_residual(h, gb, buf, i4x4+4*i8x8, zigzag_scan8x8_cavlc+16*i4x4,
05017 h->dequant8_coeff[s->qscale], 16) <0 )
05018 return -1;
05019 }
05020 if(s->qscale < 12){
05021 int i;
05022 for(i=0; i<64; i++)
05023 buf[i] = (buf[i] + 2) >> 2;
05024 }
05025 nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
05026 nnz[0] |= nnz[1] | nnz[8] | nnz[9];
05027 }else{
05028 for(i4x4=0; i4x4<4; i4x4++){
05029 const int index= i4x4 + 4*i8x8;
05030
05031 if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[s->qscale], 16) <0 ){
05032 return -1;
05033 }
05034 }
05035 }
05036 }else{
05037 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
05038 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
05039 }
05040 }
05041 }
05042
05043 if(cbp&0x30){
05044 for(chroma_idx=0; chroma_idx<2; chroma_idx++)
05045 if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, h->dequant4_coeff[chroma_qp], 4) < 0){
05046 return -1;
05047 }
05048 }
05049
05050 if(cbp&0x20){
05051 for(chroma_idx=0; chroma_idx<2; chroma_idx++){
05052 for(i4x4=0; i4x4<4; i4x4++){
05053 const int index= 16 + 4*chroma_idx + i4x4;
05054 if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[chroma_qp], 15) < 0){
05055 return -1;
05056 }
05057 }
05058 }
05059 }else{
05060 uint8_t * const nnz= &h->non_zero_count_cache[0];
05061 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
05062 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
05063 }
05064 }else{
05065 uint8_t * const nnz= &h->non_zero_count_cache[0];
05066 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
05067 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
05068 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
05069 }
05070 s->current_picture.qscale_table[mb_xy]= s->qscale;
05071 write_back_non_zero_count(h);
05072
05073 return 0;
05074 }
05075
05076 static int decode_cabac_field_decoding_flag(H264Context *h) {
05077 MpegEncContext * const s = &h->s;
05078 const int mb_x = s->mb_x;
05079 const int mb_y = s->mb_y & ~1;
05080 const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
05081 const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
05082
05083 unsigned int ctx = 0;
05084
05085 if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
05086 ctx += 1;
05087 }
05088 if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
05089 ctx += 1;
05090 }
05091
05092 return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
05093 }
05094
05095 static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
05096 uint8_t *state= &h->cabac_state[ctx_base];
05097 int mb_type;
05098
05099 if(intra_slice){
05100 MpegEncContext * const s = &h->s;
05101 const int mba_xy = h->left_mb_xy[0];
05102 const int mbb_xy = h->top_mb_xy;
05103 int ctx=0;
05104 if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
05105 ctx++;
05106 if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
05107 ctx++;
05108 if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
05109 return 0;
05110 state += 2;
05111 }else{
05112 if( get_cabac( &h->cabac, &state[0] ) == 0 )
05113 return 0;
05114 }
05115
05116 if( get_cabac_terminate( &h->cabac ) )
05117 return 25;
05118
05119 mb_type = 1;
05120 if( get_cabac( &h->cabac, &state[1] ) )
05121 mb_type += 12;
05122
05123 if( get_cabac( &h->cabac, &state[2] ) ) {
05124 if( get_cabac( &h->cabac, &state[2+intra_slice] ) )
05125 mb_type += 4 * 2;
05126 else
05127 mb_type += 4 * 1;
05128 }
05129 if( get_cabac( &h->cabac, &state[3+intra_slice] ) )
05130 mb_type += 2;
05131 if( get_cabac( &h->cabac, &state[3+2*intra_slice] ) )
05132 mb_type += 1;
05133 return mb_type;
05134 }
05135
05136 static int decode_cabac_mb_type( H264Context *h ) {
05137 MpegEncContext * const s = &h->s;
05138
05139 if( h->slice_type == I_TYPE ) {
05140 return decode_cabac_intra_mb_type(h, 3, 1);
05141 } else if( h->slice_type == P_TYPE ) {
05142 if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
05143
05144 if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
05145 if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
05146 return 0;
05147 else
05148 return 3;
05149 } else {
05150 if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
05151 return 2;
05152 else
05153 return 1;
05154 }
05155 } else {
05156 return decode_cabac_intra_mb_type(h, 17, 0) + 5;
05157 }
05158 } else if( h->slice_type == B_TYPE ) {
05159 const int mba_xy = h->left_mb_xy[0];
05160 const int mbb_xy = h->top_mb_xy;
05161 int ctx = 0;
05162 int bits;
05163
05164 if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )
05165 && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
05166 ctx++;
05167 if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] )
05168 && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
05169 ctx++;
05170
05171 if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
05172 return 0;
05173
05174 if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
05175 return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] );
05176 }
05177
05178 bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
05179 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
05180 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
05181 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
05182 if( bits < 8 )
05183 return bits + 3;
05184 else if( bits == 13 ) {
05185 return decode_cabac_intra_mb_type(h, 32, 0) + 23;
05186 } else if( bits == 14 )
05187 return 11;
05188 else if( bits == 15 )
05189 return 22;
05190
05191 bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
05192 return bits - 4;
05193 } else {
05194
05195 return -1;
05196 }
05197 }
05198
05199 static int decode_cabac_mb_skip( H264Context *h) {
05200 MpegEncContext * const s = &h->s;
05201 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
05202 const int mba_xy = mb_xy - 1;
05203 const int mbb_xy = mb_xy - s->mb_stride;
05204 int ctx = 0;
05205
05206 if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
05207 ctx++;
05208 if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
05209 ctx++;
05210
05211 if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
05212 return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
05213 else
05214 return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
05215 }
05216
05217 static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
05218 int mode = 0;
05219
05220 if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
05221 return pred_mode;
05222
05223 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
05224 mode += 1;
05225 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
05226 mode += 2;
05227 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
05228 mode += 4;
05229 if( mode >= pred_mode )
05230 return mode + 1;
05231 else
05232 return mode;
05233 }
05234
05235 static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
05236 const int mba_xy = h->left_mb_xy[0];
05237 const int mbb_xy = h->top_mb_xy;
05238
05239 int ctx = 0;
05240
05241
05242 if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
05243 ctx++;
05244
05245 if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
05246 ctx++;
05247
05248 if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
05249 return 0;
05250
05251 if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
05252 return 1;
05253 if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
05254 return 2;
05255 else
05256 return 3;
05257 }
05258
05259 static const uint8_t block_idx_x[16] = {
05260 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
05261 };
05262 static const uint8_t block_idx_y[16] = {
05263 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
05264 };
05265 static const uint8_t block_idx_xy[4][4] = {
05266 { 0, 2, 8, 10},
05267 { 1, 3, 9, 11},
05268 { 4, 6, 12, 14},
05269 { 5, 7, 13, 15}
05270 };
05271
05272 static int decode_cabac_mb_cbp_luma( H264Context *h) {
05273 MpegEncContext * const s = &h->s;
05274
05275 int cbp = 0;
05276 int i8x8;
05277
05278 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
05279 int cbp_a = -1;
05280 int cbp_b = -1;
05281 int x, y;
05282 int ctx = 0;
05283
05284 x = block_idx_x[4*i8x8];
05285 y = block_idx_y[4*i8x8];
05286
05287 if( x > 0 )
05288 cbp_a = cbp;
05289 else if( s->mb_x > 0 && (h->slice_table[h->left_mb_xy[0]] == h->slice_num)) {
05290 cbp_a = h->left_cbp;
05291 tprintf("cbp_a = left_cbp = %x\n", cbp_a);
05292 }
05293
05294 if( y > 0 )
05295 cbp_b = cbp;
05296 else if( s->mb_y > 0 && (h->slice_table[h->top_mb_xy] == h->slice_num)) {
05297 cbp_b = h->top_cbp;
05298 tprintf("cbp_b = top_cbp = %x\n", cbp_b);
05299 }
05300
05301
05302
05303 if( cbp_a >= 0 ) {
05304 int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
05305 if( ((cbp_a >> i8x8a)&0x01) == 0 )
05306 ctx++;
05307 }
05308
05309 if( cbp_b >= 0 ) {
05310 int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
05311 if( ((cbp_b >> i8x8b)&0x01) == 0 )
05312 ctx += 2;
05313 }
05314
05315 if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
05316 cbp |= 1 << i8x8;
05317 }
05318 }
05319 return cbp;
05320 }
05321 static int decode_cabac_mb_cbp_chroma( H264Context *h) {
05322 int ctx;
05323 int cbp_a, cbp_b;
05324
05325 cbp_a = (h->left_cbp>>4)&0x03;
05326 cbp_b = (h-> top_cbp>>4)&0x03;
05327
05328 ctx = 0;
05329 if( cbp_a > 0 ) ctx++;
05330 if( cbp_b > 0 ) ctx += 2;
05331 if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
05332 return 0;
05333
05334 ctx = 4;
05335 if( cbp_a == 2 ) ctx++;
05336 if( cbp_b == 2 ) ctx += 2;
05337 return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
05338 }
05339 static int decode_cabac_mb_dqp( H264Context *h) {
05340 MpegEncContext * const s = &h->s;
05341 int mbn_xy;
05342 int ctx = 0;
05343 int val = 0;
05344
05345 if( s->mb_x > 0 )
05346 mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
05347 else
05348 mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
05349
05350 if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
05351 ctx++;
05352
05353 while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
05354 if( ctx < 2 )
05355 ctx = 2;
05356 else
05357 ctx = 3;
05358 val++;
05359 }
05360
05361 if( val&0x01 )
05362 return (val + 1)/2;
05363 else
05364 return -(val + 1)/2;
05365 }
05366 static int decode_cabac_p_mb_sub_type( H264Context *h ) {
05367 if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
05368 return 0;
05369 if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
05370 return 1;
05371 if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
05372 return 2;
05373 return 3;
05374 }
05375 static int decode_cabac_b_mb_sub_type( H264Context *h ) {
05376 int type;
05377 if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
05378 return 0;
05379 if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
05380 return 1 + get_cabac( &h->cabac, &h->cabac_state[39] );
05381 type = 3;
05382 if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
05383 if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
05384 return 11 + get_cabac( &h->cabac, &h->cabac_state[39] );
05385 type += 4;
05386 }
05387 type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
05388 type += get_cabac( &h->cabac, &h->cabac_state[39] );
05389 return type;
05390 }
05391
05392 static inline int decode_cabac_mb_transform_size( H264Context *h ) {
05393 return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
05394 }
05395
05396 static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
05397 int refa = h->ref_cache[list][scan8[n] - 1];
05398 int refb = h->ref_cache[list][scan8[n] - 8];
05399 int ref = 0;
05400 int ctx = 0;
05401
05402 if( h->slice_type == B_TYPE) {
05403 if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
05404 ctx++;
05405 if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
05406 ctx += 2;
05407 } else {
05408 if( refa > 0 )
05409 ctx++;
05410 if( refb > 0 )
05411 ctx += 2;
05412 }
05413
05414 while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
05415 ref++;
05416 if( ctx < 4 )
05417 ctx = 4;
05418 else
05419 ctx = 5;
05420 }
05421 return ref;
05422 }
05423
05424 static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
05425 int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
05426 abs( h->mvd_cache[list][scan8[n] - 8][l] );
05427 int ctxbase = (l == 0) ? 40 : 47;
05428 int ctx, mvd;
05429
05430 if( amvd < 3 )
05431 ctx = 0;
05432 else if( amvd > 32 )
05433 ctx = 2;
05434 else
05435 ctx = 1;
05436
05437 if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
05438 return 0;
05439
05440 mvd= 1;
05441 ctx= 3;
05442 while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
05443 mvd++;
05444 if( ctx < 6 )
05445 ctx++;
05446 }
05447
05448 if( mvd >= 9 ) {
05449 int k = 3;
05450 while( get_cabac_bypass( &h->cabac ) ) {
05451 mvd += 1 << k;
05452 k++;
05453 }
05454 while( k-- ) {
05455 if( get_cabac_bypass( &h->cabac ) )
05456 mvd += 1 << k;
05457 }
05458 }
05459 if( get_cabac_bypass( &h->cabac ) ) return -mvd;
05460 else return mvd;
05461 }
05462
05463 static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
05464 int nza, nzb;
05465 int ctx = 0;
05466
05467 if( cat == 0 ) {
05468 nza = h->left_cbp&0x100;
05469 nzb = h-> top_cbp&0x100;
05470 } else if( cat == 1 || cat == 2 ) {
05471 nza = h->non_zero_count_cache[scan8[idx] - 1];
05472 nzb = h->non_zero_count_cache[scan8[idx] - 8];
05473 } else if( cat == 3 ) {
05474 nza = (h->left_cbp>>(6+idx))&0x01;
05475 nzb = (h-> top_cbp>>(6+idx))&0x01;
05476 } else {
05477 assert(cat == 4);
05478 nza = h->non_zero_count_cache[scan8[16+idx] - 1];
05479 nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
05480 }
05481
05482 if( nza > 0 )
05483 ctx++;
05484
05485 if( nzb > 0 )
05486 ctx += 2;
05487
05488 return ctx + 4 * cat;
05489 }
05490
05491 static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint16_t *qmul, int max_coeff) {
05492 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
05493 static const int significant_coeff_flag_field_offset[2] = { 105, 277 };
05494 static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 };
05495 static const int significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 297 };
05496 static const int last_significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 251 };
05497 static const int coeff_abs_level_m1_offset[6] = { 227+0, 227+10, 227+20, 227+30, 227+39, 426 };
05498 static const int identity[15] = {
05499 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
05500 };
05501 static const int significant_coeff_flag_offset_8x8[63] = {
05502 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
05503 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
05504 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
05505 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
05506 };
05507 static const int last_coeff_flag_offset_8x8[63] = {
05508 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
05509 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
05510 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
05511 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
05512 };
05513
05514 int index[64];
05515
05516 int i, last;
05517 int coeff_count = 0;
05518
05519 int abslevel1 = 1;
05520 int abslevelgt1 = 0;
05521
05522 const int* significant_coeff_ctx_offset;
05523 const int* last_coeff_ctx_offset;
05524 const int significant_coeff_ctx_base = significant_coeff_flag_offset[cat]
05525 + significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
05526 const int last_coeff_ctx_base = last_significant_coeff_flag_offset[cat]
05527 + last_significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
05528
05529
05530
05531
05532
05533
05534
05535
05536
05537
05538 if( cat == 5 ) {
05539 significant_coeff_ctx_offset = significant_coeff_flag_offset_8x8;
05540 last_coeff_ctx_offset = last_coeff_flag_offset_8x8;
05541 } else {
05542 if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
05543 if( cat == 1 || cat == 2 )
05544 h->non_zero_count_cache[scan8[n]] = 0;
05545 else if( cat == 4 )
05546 h->non_zero_count_cache[scan8[16+n]] = 0;
05547
05548 return 0;
05549 }
05550
05551 significant_coeff_ctx_offset =
05552 last_coeff_ctx_offset = identity;
05553 }
05554
05555 for(last= 0; last < max_coeff - 1; last++) {
05556 int sig_ctx = significant_coeff_ctx_base + significant_coeff_ctx_offset[last];
05557 if( get_cabac( &h->cabac, &h->cabac_state[sig_ctx] )) {
05558 int last_ctx = last_coeff_ctx_base + last_coeff_ctx_offset[last];
05559 index[coeff_count++] = last;
05560 if( get_cabac( &h->cabac, &h->cabac_state[last_ctx] ) ) {
05561 last= max_coeff;
05562 break;
05563 }
05564 }
05565 }
05566 if( last == max_coeff -1 ) {
05567 index[coeff_count++] = last;
05568 }
05569 assert(coeff_count > 0);
05570
05571 if( cat == 0 )
05572 h->cbp_table[mb_xy] |= 0x100;
05573 else if( cat == 1 || cat == 2 )
05574 h->non_zero_count_cache[scan8[n]] = coeff_count;
05575 else if( cat == 3 )
05576 h->cbp_table[mb_xy] |= 0x40 << n;
05577 else if( cat == 4 )
05578 h->non_zero_count_cache[scan8[16+n]] = coeff_count;
05579 else {
05580 assert( cat == 5 );
05581 fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, 1, 1);
05582 }
05583
05584 for( i = coeff_count - 1; i >= 0; i-- ) {
05585 int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + coeff_abs_level_m1_offset[cat];
05586 int j= scantable[index[i]];
05587
05588 if( get_cabac( &h->cabac, &h->cabac_state[ctx] ) == 0 ) {
05589 if( cat == 0 || cat == 3 ) {
05590 if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
05591 else block[j] = 1;
05592 }else{
05593 if( get_cabac_bypass( &h->cabac ) ) block[j] = -qmul[j];
05594 else block[j] = qmul[j];
05595 }
05596
05597 abslevel1++;
05598 } else {
05599 int coeff_abs = 2;
05600 ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat];
05601 while( coeff_abs < 15 && get_cabac( &h->cabac, &h->cabac_state[ctx] ) ) {
05602 coeff_abs++;
05603 }
05604
05605 if( coeff_abs >= 15 ) {
05606 int j = 0;
05607 while( get_cabac_bypass( &h->cabac ) ) {
05608 coeff_abs += 1 << j;
05609 j++;
05610 }
05611
05612 while( j-- ) {
05613 if( get_cabac_bypass( &h->cabac ) )
05614 coeff_abs += 1 << j ;
05615 }
05616 }
05617
05618 if( cat == 0 || cat == 3 ) {
05619 if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
05620 else block[j] = coeff_abs;
05621 }else{
05622 if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs * qmul[j];
05623 else block[j] = coeff_abs * qmul[j];
05624 }
05625
05626 abslevelgt1++;
05627 }
05628 }
05629 return 0;
05630 }
05631
05632 void inline compute_mb_neighboors(H264Context *h)
05633 {
05634 MpegEncContext * const s = &h->s;
05635 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
05636 h->top_mb_xy = mb_xy - s->mb_stride;
05637 h->left_mb_xy[0] = mb_xy - 1;
05638 if(h->mb_aff_frame){
05639 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
05640 const int top_pair_xy = pair_xy - s->mb_stride;
05641 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
05642 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
05643 const int curr_mb_frame_flag = !h->mb_field_decoding_flag;
05644 const int bottom = (s->mb_y & 1);
05645 if (bottom
05646 ? !curr_mb_frame_flag
05647 : (!curr_mb_frame_flag && !top_mb_frame_flag)
05648 ) {
05649 h->top_mb_xy -= s->mb_stride;
05650 }
05651 if (left_mb_frame_flag != curr_mb_frame_flag) {
05652 h->left_mb_xy[0] = pair_xy - 1;
05653 }
05654 }
05655 return;
05656 }
05657
05662 static int decode_mb_cabac(H264Context *h) {
05663 MpegEncContext * const s = &h->s;
05664 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
05665 int mb_type, partition_count, cbp = 0;
05666 int dct8x8_allowed= h->pps.transform_8x8_mode;
05667
05668 s->dsp.clear_blocks(h->mb);
05669
05670 tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
05671 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
05672
05673 if( decode_cabac_mb_skip( h ) ) {
05674 decode_mb_skip(h);
05675
05676 h->cbp_table[mb_xy] = 0;
05677 h->chroma_pred_mode_table[mb_xy] = 0;
05678 h->last_qscale_diff = 0;
05679
05680 return 0;
05681
05682 }
05683 }
05684 if(h->mb_aff_frame){
05685 if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
05686 h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
05687 }else
05688 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
05689
05690 h->prev_mb_skipped = 0;
05691
05692 compute_mb_neighboors(h);
05693 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
05694 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
05695 return -1;
05696 }
05697
05698 if( h->slice_type == B_TYPE ) {
05699 if( mb_type < 23 ){
05700 partition_count= b_mb_type_info[mb_type].partition_count;
05701 mb_type= b_mb_type_info[mb_type].type;
05702 }else{
05703 mb_type -= 23;
05704 goto decode_intra_mb;
05705 }
05706 } else if( h->slice_type == P_TYPE ) {
05707 if( mb_type < 5) {
05708 partition_count= p_mb_type_info[mb_type].partition_count;
05709 mb_type= p_mb_type_info[mb_type].type;
05710 } else {
05711 mb_type -= 5;
05712 goto decode_intra_mb;
05713 }
05714 } else {
05715 assert(h->slice_type == I_TYPE);
05716 decode_intra_mb:
05717 partition_count = 0;
05718 cbp= i_mb_type_info[mb_type].cbp;
05719 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
05720 mb_type= i_mb_type_info[mb_type].type;
05721 }
05722 if(h->mb_field_decoding_flag)
05723 mb_type |= MB_TYPE_INTERLACED;
05724
05725 h->slice_table[ mb_xy ]= h->slice_num;
05726
05727 if(IS_INTRA_PCM(mb_type)) {
05728 const uint8_t *ptr;
05729 unsigned int x, y;
05730
05731
05732
05733
05734 ptr= h->cabac.bytestream;
05735 if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
05736
05737
05738 for(y=0; y<16; y++){
05739 const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
05740 for(x=0; x<16; x++){
05741 tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
05742 h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
05743 }
05744 }
05745 for(y=0; y<8; y++){
05746 const int index= 256 + 4*(y&3) + 32*(y>>2);
05747 for(x=0; x<8; x++){
05748 tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
05749 h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
05750 }
05751 }
05752 for(y=0; y<8; y++){
05753 const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
05754 for(x=0; x<8; x++){
05755 tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
05756 h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
05757 }
05758 }
05759
05760 ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
05761
05762
05763 h->cbp_table[mb_xy] = 0x1ef;
05764 h->chroma_pred_mode_table[mb_xy] = 0;
05765
05766 s->current_picture.qscale_table[mb_xy]= 0;
05767 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
05768
05769 memset(h->non_zero_count[mb_xy], 16, 16);
05770 s->current_picture.mb_type[mb_xy]= mb_type;
05771 return 0;
05772 }
05773
05774 fill_caches(h, mb_type, 0);
05775
05776 if( IS_INTRA( mb_type ) ) {
05777 int i;
05778 if( IS_INTRA4x4( mb_type ) ) {
05779 if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
05780 mb_type |= MB_TYPE_8x8DCT;
05781 for( i = 0; i < 16; i+=4 ) {
05782 int pred = pred_intra_mode( h, i );
05783 int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
05784 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
05785 }
05786 } else {
05787 for( i = 0; i < 16; i++ ) {
05788 int pred = pred_intra_mode( h, i );
05789 h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
05790
05791
05792 }
05793 }
05794 write_back_intra_pred_mode(h);
05795 if( check_intra4x4_pred_mode(h) < 0 ) return -1;
05796 } else {
05797 h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
05798 if( h->intra16x16_pred_mode < 0 ) return -1;
05799 }
05800 h->chroma_pred_mode_table[mb_xy] =
05801 h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
05802
05803 h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
05804 if( h->chroma_pred_mode < 0 ) return -1;
05805 } else if( partition_count == 4 ) {
05806 int i, j, sub_partition_count[4], list, ref[2][4];
05807
05808 if( h->slice_type == B_TYPE ) {
05809 for( i = 0; i < 4; i++ ) {
05810 h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
05811 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
05812 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
05813 }
05814 if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
05815 || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
05816 pred_direct_motion(h, &mb_type);
05817 if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
05818 for( i = 0; i < 4; i++ )
05819 if( IS_DIRECT(h->sub_mb_type[i]) )
05820 fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
05821 }
05822 }
05823 } else {
05824 for( i = 0; i < 4; i++ ) {
05825 h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
05826 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
05827 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
05828 }
05829 }
05830
05831 for( list = 0; list < 2; list++ ) {
05832 if( h->ref_count[list] > 0 ) {
05833 for( i = 0; i < 4; i++ ) {
05834 if(IS_DIRECT(h->sub_mb_type[i])) continue;
05835 if(IS_DIR(h->sub_mb_type[i], 0, list)){
05836 if( h->ref_count[list] > 1 )
05837 ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
05838 else
05839 ref[list][i] = 0;
05840 } else {
05841 ref[list][i] = -1;
05842 }
05843 h->ref_cache[list][ scan8[4*i]+1 ]=
05844 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
05845 }
05846 }
05847 }
05848
05849 if(dct8x8_allowed)
05850 dct8x8_allowed = get_dct8x8_allowed(h);
05851
05852 for(list=0; list<2; list++){
05853 for(i=0; i<4; i++){
05854 if(IS_DIRECT(h->sub_mb_type[i])){
05855 fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
05856 continue;
05857 }
05858 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
05859
05860 if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
05861 const int sub_mb_type= h->sub_mb_type[i];
05862 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
05863 for(j=0; j<sub_partition_count[i]; j++){
05864 int mpx, mpy;
05865 int mx, my;
05866 const int index= 4*i + block_width*j;
05867 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
05868 int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
05869 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
05870
05871 mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
05872 my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
05873 tprintf("final mv:%d %d\n", mx, my);
05874
05875 if(IS_SUB_8X8(sub_mb_type)){
05876 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
05877 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
05878 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
05879 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
05880
05881 mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
05882 mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
05883 mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
05884 mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
05885 }else if(IS_SUB_8X4(sub_mb_type)){
05886 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
05887 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
05888
05889 mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
05890 mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
05891 }else if(IS_SUB_4X8(sub_mb_type)){
05892 mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
05893 mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
05894
05895 mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
05896 mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
05897 }else{
05898 assert(IS_SUB_4X4(sub_mb_type));
05899 mv_cache[ 0 ][0]= mx;
05900 mv_cache[ 0 ][1]= my;
05901
05902 mvd_cache[ 0 ][0]= mx - mpx;
05903 mvd_cache[ 0 ][1]= my - mpy;
05904 }
05905 }
05906 }else{
05907 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
05908 uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
05909 p[0] = p[1] = p[8] = p[9] = 0;
05910 pd[0]= pd[1]= pd[8]= pd[9]= 0;
05911 }
05912 }
05913 }
05914 } else if( IS_DIRECT(mb_type) ) {
05915 pred_direct_motion(h, &mb_type);
05916 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
05917 fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
05918 dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
05919 } else {
05920 int list, mx, my, i, mpx, mpy;
05921 if(IS_16X16(mb_type)){
05922 for(list=0; list<2; list++){
05923 if(IS_DIR(mb_type, 0, list)){
05924 if(h->ref_count[list] > 0 ){
05925 const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
05926 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
05927 }
05928 }else
05929 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
05930 }
05931 for(list=0; list<2; list++){
05932 if(IS_DIR(mb_type, 0, list)){
05933 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
05934
05935 mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
05936 my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
05937 tprintf("final mv:%d %d\n", mx, my);
05938
05939 fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
05940 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
05941 }else
05942 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
05943 }
05944 }
05945 else if(IS_16X8(mb_type)){
05946 for(list=0; list<2; list++){
05947 if(h->ref_count[list]>0){
05948 for(i=0; i<2; i++){
05949 if(IS_DIR(mb_type, i, list)){
05950 const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
05951 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
05952 }else
05953 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
05954 }
05955 }
05956 }
05957 for(list=0; list<2; list++){
05958 for(i=0; i<2; i++){
05959 if(IS_DIR(mb_type, i, list)){
05960 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
05961 mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
05962 my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
05963 tprintf("final mv:%d %d\n", mx, my);
05964
05965 fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
05966 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
05967 }else{
05968 fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
05969 fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
05970 }
05971 }
05972 }
05973 }else{
05974 assert(IS_8X16(mb_type));
05975 for(list=0; list<2; list++){
05976 if(h->ref_count[list]>0){
05977 for(i=0; i<2; i++){
05978 if(IS_DIR(mb_type, i, list)){
05979 const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
05980 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
05981 }else
05982 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
05983 }
05984 }
05985 }
05986 for(list=0; list<2; list++){
05987 for(i=0; i<2; i++){
05988 if(IS_DIR(mb_type, i, list)){
05989 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
05990 mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
05991 my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
05992
05993 tprintf("final mv:%d %d\n", mx, my);
05994 fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
05995 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
05996 }else{
05997 fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
05998 fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
05999 }
06000 }
06001 }
06002 }
06003 }
06004
06005 if( IS_INTER( mb_type ) ) {
06006 h->chroma_pred_mode_table[mb_xy] = 0;
06007 write_back_motion( h, mb_type );
06008 }
06009
06010 if( !IS_INTRA16x16( mb_type ) ) {
06011 cbp = decode_cabac_mb_cbp_luma( h );
06012 cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
06013 }
06014
06015 h->cbp_table[mb_xy] = cbp;
06016
06017 if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
06018 if( decode_cabac_mb_transform_size( h ) )
06019 mb_type |= MB_TYPE_8x8DCT;
06020 }
06021 s->current_picture.mb_type[mb_xy]= mb_type;
06022
06023 if( cbp || IS_INTRA16x16( mb_type ) ) {
06024 const uint8_t *scan, *dc_scan;
06025 int dqp;
06026
06027 if(IS_INTERLACED(mb_type)){
06028 scan= s->qscale ? h->field_scan : h->field_scan_q0;
06029 dc_scan= luma_dc_field_scan;
06030 }else{
06031 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
06032 dc_scan= luma_dc_zigzag_scan;
06033 }
06034
06035 h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
06036 s->qscale += dqp;
06037 if(((unsigned)s->qscale) > 51){
06038 if(s->qscale<0) s->qscale+= 52;
06039 else s->qscale-= 52;
06040 }
06041 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
06042
06043 if( IS_INTRA16x16( mb_type ) ) {
06044 int i;
06045
06046 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, h->dequant4_coeff[s->qscale], 16) < 0)
06047 return -1;
06048 if( cbp&15 ) {
06049 for( i = 0; i < 16; i++ ) {
06050
06051 if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[s->qscale], 15) < 0 )
06052 return -1;
06053 }
06054 } else {
06055 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
06056 }
06057 } else {
06058 int i8x8, i4x4;
06059 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
06060 if( cbp & (1<<i8x8) ) {
06061 if( IS_8x8DCT(mb_type) ) {
06062 if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
06063 zigzag_scan8x8, h->dequant8_coeff[s->qscale], 64) < 0 )
06064 return -1;
06065 if(s->qscale < 12){
06066 int i;
06067 for(i=0; i<64; i++)
06068 h->mb[64*i8x8+i] = (h->mb[64*i8x8+i] + 2) >> 2;
06069 }
06070 } else
06071 for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
06072 const int index = 4*i8x8 + i4x4;
06073
06074 if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[s->qscale], 16) < 0 )
06075 return -1;
06076 }
06077 } else {
06078 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
06079 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
06080 }
06081 }
06082 }
06083
06084 if( cbp&0x30 ){
06085 int c;
06086 for( c = 0; c < 2; c++ ) {
06087
06088 if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, h->dequant4_coeff[h->chroma_qp], 4) < 0)
06089 return -1;
06090 }
06091 }
06092
06093 if( cbp&0x20 ) {
06094 int c, i;
06095 for( c = 0; c < 2; c++ ) {
06096 for( i = 0; i < 4; i++ ) {
06097 const int index = 16 + 4 * c + i;
06098
06099 if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->dequant4_coeff[h->chroma_qp], 15) < 0)
06100 return -1;
06101 }
06102 }
06103 } else {
06104 uint8_t * const nnz= &h->non_zero_count_cache[0];
06105 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
06106 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
06107 }
06108 } else {
06109 uint8_t * const nnz= &h->non_zero_count_cache[0];
06110 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
06111 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
06112 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
06113 }
06114
06115 s->current_picture.qscale_table[mb_xy]= s->qscale;
06116 write_back_non_zero_count(h);
06117
06118 return 0;
06119 }
06120
06121
06122 static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
06123 int i, d;
06124 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
06125 const int alpha = alpha_table[index_a];
06126 const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
06127
06128 if( bS[0] < 4 ) {
06129 int8_t tc[4];
06130 for(i=0; i<4; i++)
06131 tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
06132 h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
06133 } else {
06134
06135
06136 for( d = 0; d < 16; d++ ) {
06137 const int p0 = pix[-1];
06138 const int p1 = pix[-2];
06139 const int p2 = pix[-3];
06140
06141 const int q0 = pix[0];
06142 const int q1 = pix[1];
06143 const int q2 = pix[2];
06144
06145 if( ABS( p0 - q0 ) < alpha &&
06146 ABS( p1 - p0 ) < beta &&
06147 ABS( q1 - q0 ) < beta ) {
06148
06149 if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06150 if( ABS( p2 - p0 ) < beta)
06151 {
06152 const int p3 = pix[-4];
06153
06154 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06155 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06156 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06157 } else {
06158
06159 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06160 }
06161 if( ABS( q2 - q0 ) < beta)
06162 {
06163 const int q3 = pix[3];
06164
06165 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06166 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06167 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06168 } else {
06169
06170 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06171 }
06172 }else{
06173
06174 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06175 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06176 }
06177 tprintf("filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
06178 }
06179 pix += stride;
06180 }
06181 }
06182 }
06183 static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
06184 int i, d;
06185 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
06186 const int alpha = alpha_table[index_a];
06187 const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
06188
06189 if( bS[0] < 4 ) {
06190 int8_t tc[4];
06191 for(i=0; i<4; i++)
06192 tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
06193 h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
06194 } else {
06195 h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
06196 }
06197 }
06198
06199 static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
06200 int i;
06201 for( i = 0; i < 16; i++, pix += stride) {
06202 int index_a;
06203 int alpha;
06204 int beta;
06205
06206 int qp_index;
06207 int bS_index = (i >> 1);
06208 if (h->mb_field_decoding_flag) {
06209 bS_index &= ~1;
06210 bS_index |= (i & 1);
06211 }
06212
06213 if( bS[bS_index] == 0 ) {
06214 continue;
06215 }
06216
06217 qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
06218 index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
06219 alpha = alpha_table[index_a];
06220 beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
06221
06222
06223 if( bS[bS_index] < 4 ) {
06224 const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
06225
06226 const int p0 = pix[-1];
06227 const int p1 = pix[-2];
06228 const int p2 = pix[-3];
06229 const int q0 = pix[0];
06230 const int q1 = pix[1];
06231 const int q2 = pix[2];
06232
06233 if( ABS( p0 - q0 ) < alpha &&
06234 ABS( p1 - p0 ) < beta &&
06235 ABS( q1 - q0 ) < beta ) {
06236 int tc = tc0;
06237 int i_delta;
06238
06239 if( ABS( p2 - p0 ) < beta ) {
06240 pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
06241 tc++;
06242 }
06243 if( ABS( q2 - q0 ) < beta ) {
06244 pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
06245 tc++;
06246 }
06247
06248 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
06249 pix[-1] = clip_uint8( p0 + i_delta );
06250 pix[0] = clip_uint8( q0 - i_delta );
06251 tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
06252 }
06253 }else{
06254
06255 const int p0 = pix[-1];
06256 const int p1 = pix[-2];
06257 const int p2 = pix[-3];
06258
06259 const int q0 = pix[0];
06260 const int q1 = pix[1];
06261 const int q2 = pix[2];
06262
06263 if( ABS( p0 - q0 ) < alpha &&
06264 ABS( p1 - p0 ) < beta &&
06265 ABS( q1 - q0 ) < beta ) {
06266
06267 if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06268 if( ABS( p2 - p0 ) < beta)
06269 {
06270 const int p3 = pix[-4];
06271
06272 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06273 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06274 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06275 } else {
06276
06277 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06278 }
06279 if( ABS( q2 - q0 ) < beta)
06280 {
06281 const int q3 = pix[3];
06282
06283 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06284 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06285 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06286 } else {
06287
06288 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06289 }
06290 }else{
06291
06292 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06293 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06294 }
06295 tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
06296 }
06297 }
06298 }
06299 }
06300 static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) {
06301 int i;
06302 for( i = 0; i < 8; i++, pix += stride) {
06303 int index_a;
06304 int alpha;
06305 int beta;
06306
06307 int qp_index;
06308 int bS_index = i;
06309
06310 if( bS[bS_index] == 0 ) {
06311 continue;
06312 }
06313
06314 qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
06315 index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
06316 alpha = alpha_table[index_a];
06317 beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
06318 if( bS[bS_index] < 4 ) {
06319 const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
06320
06321 const int p0 = pix[-1];
06322 const int p1 = pix[-2];
06323 const int q0 = pix[0];
06324 const int q1 = pix[1];
06325
06326 if( ABS( p0 - q0 ) < alpha &&
06327 ABS( p1 - p0 ) < beta &&
06328 ABS( q1 - q0 ) < beta ) {
06329 const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
06330
06331 pix[-1] = clip_uint8( p0 + i_delta );
06332 pix[0] = clip_uint8( q0 - i_delta );
06333 tprintf("filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
06334 }
06335 }else{
06336 const int p0 = pix[-1];
06337 const int p1 = pix[-2];
06338 const int q0 = pix[0];
06339 const int q1 = pix[1];
06340
06341 if( ABS( p0 - q0 ) < alpha &&
06342 ABS( p1 - p0 ) < beta &&
06343 ABS( q1 - q0 ) < beta ) {
06344
06345 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06346 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06347 tprintf("filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
06348 }
06349 }
06350 }
06351 }
06352
06353 static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
06354 int i, d;
06355 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
06356 const int alpha = alpha_table[index_a];
06357 const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
06358 const int pix_next = stride;
06359
06360 if( bS[0] < 4 ) {
06361 int8_t tc[4];
06362 for(i=0; i<4; i++)
06363 tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
06364 h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
06365 } else {
06366
06367 for( d = 0; d < 16; d++ ) {
06368 const int p0 = pix[-1*pix_next];
06369 const int p1 = pix[-2*pix_next];
06370 const int p2 = pix[-3*pix_next];
06371 const int q0 = pix[0];
06372 const int q1 = pix[1*pix_next];
06373 const int q2 = pix[2*pix_next];
06374
06375 if( ABS( p0 - q0 ) < alpha &&
06376 ABS( p1 - p0 ) < beta &&
06377 ABS( q1 - q0 ) < beta ) {
06378
06379 const int p3 = pix[-4*pix_next];
06380 const int q3 = pix[ 3*pix_next];
06381
06382 if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06383 if( ABS( p2 - p0 ) < beta) {
06384
06385 pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06386 pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06387 pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06388 } else {
06389
06390 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06391 }
06392 if( ABS( q2 - q0 ) < beta) {
06393
06394 pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06395 pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06396 pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06397 } else {
06398
06399 pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06400 }
06401 }else{
06402
06403 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06404 pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06405 }
06406 tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
06407 }
06408 pix++;
06409 }
06410 }
06411 }
06412
06413 static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
06414 int i, d;
06415 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
06416 const int alpha = alpha_table[index_a];
06417 const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
06418
06419 if( bS[0] < 4 ) {
06420 int8_t tc[4];
06421 for(i=0; i<4; i++)
06422 tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
06423 h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
06424 } else {
06425 h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
06426 }
06427 }
06428
06429 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) {
06430 MpegEncContext * const s = &h->s;
06431 const int mb_xy= mb_x + mb_y*s->mb_stride;
06432 int first_vertical_edge_done = 0;
06433 int dir;
06434
06435
06436
06437 static const int ref2frm[18] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
06438
06439 if (h->mb_aff_frame
06440
06441 && h->slice_table[mb_xy-1] != 255
06442
06443 && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
06444
06445 && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
06446
06447
06448
06449 int bS[8];
06450 int qp[2];
06451 int chroma_qp[2];
06452
06453 int i;
06454 first_vertical_edge_done = 1;
06455 for( i = 0; i < 8; i++ ) {
06456 int y = i>>1;
06457 int b_idx= 8 + 4 + 8*y;
06458 int bn_idx= b_idx - 1;
06459
06460 int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1];
06461
06462 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
06463 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
06464 bS[i] = 4;
06465 } else if( h->non_zero_count_cache[b_idx] != 0 ||
06466
06467 h->non_zero_count_cache[bn_idx] != 0 ) {
06468 bS[i] = 2;
06469 } else {
06470 int l;
06471 bS[i] = 0;
06472 for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
06473 if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
06474 ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
06475 ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
06476 bS[i] = 1;
06477 break;
06478 }
06479 }
06480 }
06481 }
06482 if(bS[0]+bS[1]+bS[2]+bS[3] != 0) {
06483
06484
06485 qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1;
06486 chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
06487 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1;
06488 qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1;
06489 chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
06490 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1;
06491
06492
06493 tprintf("filter mb:%d/%d MBAFF, QPy:%d/%d, QPc:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], chroma_qp[0], chroma_qp[1], linesize, uvlinesize);
06494 { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
06495 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
06496 filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
06497 filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
06498 }
06499 }
06500
06501 for( dir = 0; dir < 2; dir++ )
06502 {
06503 int edge;
06504 const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
06505 int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
06506
06507 if (first_vertical_edge_done) {
06508 start = 1;
06509 first_vertical_edge_done = 0;
06510 }
06511
06512 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
06513 start = 1;
06514
06515
06516 for( edge = start; edge < 4; edge++ ) {
06517
06518 int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
06519 int bS[4];
06520 int qp;
06521
06522 if( (edge&1) && IS_8x8DCT(s->current_picture.mb_type[mb_xy]) )
06523 continue;
06524
06525 if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0)
06526 && !IS_INTERLACED(s->current_picture.mb_type[mb_xy])
06527 && IS_INTERLACED(s->current_picture.mb_type[mbn_xy])
06528 ) {
06529
06530
06531
06532
06533 unsigned int tmp_linesize = 2 * linesize;
06534 unsigned int tmp_uvlinesize = 2 * uvlinesize;
06535 int mbn_xy = mb_xy - 2 * s->mb_stride;
06536 int qp, chroma_qp;
06537
06538
06539 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
06540 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
06541 bS[0] = bS[1] = bS[2] = bS[3] = 3;
06542 } else {
06543
06544 assert(0);
06545 }
06546
06547
06548
06549 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
06550 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
06551 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
06552 filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp );
06553 chroma_qp = ( h->chroma_qp +
06554 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
06555 filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp );
06556 filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp );
06557
06558
06559 mbn_xy += s->mb_stride;
06560 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
06561 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
06562 bS[0] = bS[1] = bS[2] = bS[3] = 3;
06563 } else {
06564
06565 assert(0);
06566 }
06567
06568
06569
06570 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
06571 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
06572 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
06573 filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp );
06574 chroma_qp = ( h->chroma_qp +
06575 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
06576 filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
06577 filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
06578 continue;
06579 }
06580 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
06581 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
06582 int value;
06583 if (edge == 0) {
06584 if ( (!IS_INTERLACED(s->current_picture.mb_type[mb_xy]) && !IS_INTERLACED(s->current_picture.mb_type[mbm_xy]))
06585 || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0))
06586 ) {
06587 value = 4;
06588 } else {
06589 value = 3;
06590 }
06591 } else {
06592 value = 3;
06593 }
06594 bS[0] = bS[1] = bS[2] = bS[3] = value;
06595 } else {
06596 int i;
06597 for( i = 0; i < 4; i++ ) {
06598 int x = dir == 0 ? edge : i;
06599 int y = dir == 0 ? i : edge;
06600 int b_idx= 8 + 4 + x + 8*y;
06601 int bn_idx= b_idx - (dir ? 8:1);
06602
06603 if( h->non_zero_count_cache[b_idx] != 0 ||
06604 h->non_zero_count_cache[bn_idx] != 0 ) {
06605 bS[i] = 2;
06606 }
06607 else
06608 {
06609 int l;
06610 bS[i] = 0;
06611 for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
06612 if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
06613 ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
06614 ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
06615 bS[i] = 1;
06616 break;
06617 }
06618 }
06619 }
06620 }
06621
06622 if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
06623 continue;
06624 }
06625
06626
06627
06628
06629 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
06630
06631 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
06632 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
06633 if( dir == 0 ) {
06634 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
06635 if( (edge&1) == 0 ) {
06636 int chroma_qp = ( h->chroma_qp +
06637 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
06638 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
06639 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
06640 }
06641 } else {
06642 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
06643 if( (edge&1) == 0 ) {
06644 int chroma_qp = ( h->chroma_qp +
06645 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
06646 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
06647 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
06648 }
06649 }
06650 }
06651 }
06652 }
06653
06654 static int decode_slice(H264Context *h){
06655 MpegEncContext * const s = &h->s;
06656 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
06657
06658 s->mb_skip_run= -1;
06659
06660 if( h->pps.cabac ) {
06661 int i;
06662
06663
06664 align_get_bits( &s->gb );
06665
06666
06667 ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
06668 ff_init_cabac_decoder( &h->cabac,
06669 s->gb.buffer + get_bits_count(&s->gb)/8,
06670 ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
06671
06672 for( i= 0; i < 460; i++ ) {
06673 int pre;
06674 if( h->slice_type == I_TYPE )
06675 pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
06676 else
06677 pre = clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
06678
06679 if( pre <= 63 )
06680 h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
06681 else
06682 h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
06683 }
06684
06685 for(;;){
06686 int ret = decode_mb_cabac(h);
06687 int eos;
06688
06689 if(ret>=0) hl_decode_mb(h);
06690
06691
06692 if( ret >= 0 && h->mb_aff_frame ) {
06693 s->mb_y++;
06694
06695 if(ret>=0) ret = decode_mb_cabac(h);
06696
06697 hl_decode_mb(h);
06698 s->mb_y--;
06699 }
06700 eos = get_cabac_terminate( &h->cabac );
06701
06702 if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
06703 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
06704 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06705 return -1;
06706 }
06707
06708 if( ++s->mb_x >= s->mb_width ) {
06709 s->mb_x = 0;
06710 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06711 ++s->mb_y;
06712 if(h->mb_aff_frame) {
06713 ++s->mb_y;
06714 }
06715 }
06716
06717 if( eos || s->mb_y >= s->mb_height ) {
06718 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06719 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06720 return 0;
06721 }
06722 }
06723
06724 } else {
06725 for(;;){
06726 int ret = decode_mb_cavlc(h);
06727
06728 if(ret>=0) hl_decode_mb(h);
06729
06730 if(ret>=0 && h->mb_aff_frame){
06731 s->mb_y++;
06732 ret = decode_mb_cavlc(h);
06733
06734 if(ret>=0) hl_decode_mb(h);
06735 s->mb_y--;
06736 }
06737
06738 if(ret<0){
06739 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
06740 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06741
06742 return -1;
06743 }
06744
06745 if(++s->mb_x >= s->mb_width){
06746 s->mb_x=0;
06747 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06748 ++s->mb_y;
06749 if(h->mb_aff_frame) {
06750 ++s->mb_y;
06751 }
06752 if(s->mb_y >= s->mb_height){
06753 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06754
06755 if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
06756 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06757
06758 return 0;
06759 }else{
06760 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06761
06762 return -1;
06763 }
06764 }
06765 }
06766
06767 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
06768 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06769 if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
06770 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06771
06772 return 0;
06773 }else{
06774 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06775
06776 return -1;
06777 }
06778 }
06779 }
06780 }
06781
06782 #if 0
06783 for(;s->mb_y < s->mb_height; s->mb_y++){
06784 for(;s->mb_x < s->mb_width; s->mb_x++){
06785 int ret= decode_mb(h);
06786
06787 hl_decode_mb(h);
06788
06789 if(ret<0){
06790 fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
06791 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06792
06793 return -1;
06794 }
06795
06796 if(++s->mb_x >= s->mb_width){
06797 s->mb_x=0;
06798 if(++s->mb_y >= s->mb_height){
06799 if(get_bits_count(s->gb) == s->gb.size_in_bits){
06800 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06801
06802 return 0;
06803 }else{
06804 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06805
06806 return -1;
06807 }
06808 }
06809 }
06810
06811 if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
06812 if(get_bits_count(s->gb) == s->gb.size_in_bits){
06813 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06814
06815 return 0;
06816 }else{
06817 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06818
06819 return -1;
06820 }
06821 }
06822 }
06823 s->mb_x=0;
06824 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06825 }
06826 #endif
06827 return -1;
06828 }
06829
06830 static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
06831 MpegEncContext * const s = &h->s;
06832 int cpb_count, i;
06833 cpb_count = get_ue_golomb(&s->gb) + 1;
06834 get_bits(&s->gb, 4);
06835 get_bits(&s->gb, 4);
06836 for(i=0; i<cpb_count; i++){
06837 get_ue_golomb(&s->gb);
06838 get_ue_golomb(&s->gb);
06839 get_bits1(&s->gb);
06840 }
06841 get_bits(&s->gb, 5);
06842 get_bits(&s->gb, 5);
06843 get_bits(&s->gb, 5);
06844 get_bits(&s->gb, 5);
06845 }
06846
06847 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
06848 MpegEncContext * const s = &h->s;
06849 int aspect_ratio_info_present_flag, aspect_ratio_idc;
06850 int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
06851
06852 aspect_ratio_info_present_flag= get_bits1(&s->gb);
06853
06854 if( aspect_ratio_info_present_flag ) {
06855 aspect_ratio_idc= get_bits(&s->gb, 8);
06856 if( aspect_ratio_idc == EXTENDED_SAR ) {
06857 sps->sar.num= get_bits(&s->gb, 16);
06858 sps->sar.den= get_bits(&s->gb, 16);
06859 }else if(aspect_ratio_idc < 16){
06860 sps->sar= pixel_aspect[aspect_ratio_idc];
06861 }else{
06862 av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
06863 return -1;
06864 }
06865 }else{
06866 sps->sar.num=
06867 sps->sar.den= 0;
06868 }
06869
06870
06871 if(get_bits1(&s->gb)){
06872 get_bits1(&s->gb);
06873 }
06874
06875 if(get_bits1(&s->gb)){
06876 get_bits(&s->gb, 3);
06877 get_bits1(&s->gb);
06878 if(get_bits1(&s->gb)){
06879 get_bits(&s->gb, 8);
06880 get_bits(&s->gb, 8);
06881 get_bits(&s->gb, 8);
06882 }
06883 }
06884
06885 if(get_bits1(&s->gb)){
06886 get_ue_golomb(&s->gb);
06887 get_ue_golomb(&s->gb);
06888 }
06889
06890 sps->timing_info_present_flag = get_bits1(&s->gb);
06891 if(sps->timing_info_present_flag){
06892 sps->num_units_in_tick = get_bits_long(&s->gb, 32);
06893 sps->time_scale = get_bits_long(&s->gb, 32);
06894 sps->fixed_frame_rate_flag = get_bits1(&s->gb);
06895 }
06896
06897 nal_hrd_parameters_present_flag = get_bits1(&s->gb);
06898 if(nal_hrd_parameters_present_flag)
06899 decode_hrd_parameters(h, sps);
06900 vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
06901 if(vcl_hrd_parameters_present_flag)
06902 decode_hrd_parameters(h, sps);
06903 if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
06904 get_bits1(&s->gb);
06905 get_bits1(&s->gb);
06906
06907 sps->bitstream_restriction_flag = get_bits1(&s->gb);
06908 if(sps->bitstream_restriction_flag){
06909 get_bits1(&s->gb);
06910 get_ue_golomb(&s->gb);
06911 get_ue_golomb(&s->gb);
06912 get_ue_golomb(&s->gb);
06913 get_ue_golomb(&s->gb);
06914 sps->num_reorder_frames = get_ue_golomb(&s->gb);
06915 get_ue_golomb(&s->gb);
06916 }
06917
06918 return 0;
06919 }
06920
06921 static inline int decode_seq_parameter_set(H264Context *h){
06922 MpegEncContext * const s = &h->s;
06923 int profile_idc, level_idc;
06924 int sps_id, i;
06925 SPS *sps;
06926
06927 profile_idc= get_bits(&s->gb, 8);
06928 get_bits1(&s->gb);
06929 get_bits1(&s->gb);
06930 get_bits1(&s->gb);
06931 get_bits1(&s->gb);
06932 get_bits(&s->gb, 4);
06933 level_idc= get_bits(&s->gb, 8);
06934 sps_id= get_ue_golomb(&s->gb);
06935
06936 sps= &h->sps_buffer[ sps_id ];
06937 sps->profile_idc= profile_idc;
06938 sps->level_idc= level_idc;
06939
06940 if(sps->profile_idc >= 100){
06941 if(get_ue_golomb(&s->gb) == 3)
06942 get_bits1(&s->gb);
06943 get_ue_golomb(&s->gb);
06944 get_ue_golomb(&s->gb);
06945 sps->transform_bypass = get_bits1(&s->gb);
06946 if(get_bits1(&s->gb)){
06947 av_log(h->s.avctx, AV_LOG_ERROR, "custom scaling matrix not implemented\n");
06948 return -1;
06949 }
06950 }
06951
06952 sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
06953 sps->poc_type= get_ue_golomb(&s->gb);
06954
06955 if(sps->poc_type == 0){
06956 sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
06957 } else if(sps->poc_type == 1){
06958 sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
06959 sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
06960 sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
06961 sps->poc_cycle_length= get_ue_golomb(&s->gb);
06962
06963 for(i=0; i<sps->poc_cycle_length; i++)
06964 sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
06965 }
06966 if(sps->poc_type > 2){
06967 av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
06968 return -1;
06969 }
06970
06971 sps->ref_frame_count= get_ue_golomb(&s->gb);
06972 if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
06973 av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
06974 }
06975 sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
06976 sps->mb_width= get_ue_golomb(&s->gb) + 1;
06977 sps->mb_height= get_ue_golomb(&s->gb) + 1;
06978 if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
06979 avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
06980 return -1;
06981
06982 sps->frame_mbs_only_flag= get_bits1(&s->gb);
06983 if(!sps->frame_mbs_only_flag)
06984 sps->mb_aff= get_bits1(&s->gb);
06985 else
06986 sps->mb_aff= 0;
06987
06988 sps->direct_8x8_inference_flag= get_bits1(&s->gb);
06989
06990 sps->crop= get_bits1(&s->gb);
06991 if(sps->crop){
06992 sps->crop_left = get_ue_golomb(&s->gb);
06993 sps->crop_right = get_ue_golomb(&s->gb);
06994 sps->crop_top = get_ue_golomb(&s->gb);
06995 sps->crop_bottom= get_ue_golomb(&s->gb);
06996 if(sps->crop_left || sps->crop_top){
06997 av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
06998 }
06999 }else{
07000 sps->crop_left =
07001 sps->crop_right =
07002 sps->crop_top =
07003 sps->crop_bottom= 0;
07004 }
07005
07006 sps->vui_parameters_present_flag= get_bits1(&s->gb);
07007 if( sps->vui_parameters_present_flag )
07008 decode_vui_parameters(h, sps);
07009
07010 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
07011 av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
07012 sps_id, sps->profile_idc, sps->level_idc,
07013 sps->poc_type,
07014 sps->ref_frame_count,
07015 sps->mb_width, sps->mb_height,
07016 sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
07017 sps->direct_8x8_inference_flag ? "8B8" : "",
07018 sps->crop_left, sps->crop_right,
07019 sps->crop_top, sps->crop_bottom,
07020 sps->vui_parameters_present_flag ? "VUI" : ""
07021 );
07022 }
07023 return 0;
07024 }
07025
07026 static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
07027 MpegEncContext * const s = &h->s;
07028 int pps_id= get_ue_golomb(&s->gb);
07029 PPS *pps= &h->pps_buffer[pps_id];
07030
07031 pps->sps_id= get_ue_golomb(&s->gb);
07032 pps->cabac= get_bits1(&s->gb);
07033 pps->pic_order_present= get_bits1(&s->gb);
07034 pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
07035 if(pps->slice_group_count > 1 ){
07036 pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
07037 av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
07038 switch(pps->mb_slice_group_map_type){
07039 case 0:
07040 #if 0
07041 | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
07042 | run_length[ i ] |1 |ue(v) |
07043 #endif
07044 break;
07045 case 2:
07046 #if 0
07047 | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
07048 |{ | | |
07049 | top_left_mb[ i ] |1 |ue(v) |
07050 | bottom_right_mb[ i ] |1 |ue(v) |
07051 | } | | |
07052 #endif
07053 break;
07054 case 3:
07055 case 4:
07056 case 5:
07057 #if 0
07058 | slice_group_change_direction_flag |1 |u(1) |
07059 | slice_group_change_rate_minus1 |1 |ue(v) |
07060 #endif
07061 break;
07062 case 6:
07063 #if 0
07064 | slice_group_id_cnt_minus1 |1 |ue(v) |
07065 | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
07066 |) | | |
07067 | slice_group_id[ i ] |1 |u(v) |
07068 #endif
07069 break;
07070 }
07071 }
07072 pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
07073 pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
07074 if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
07075 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
07076 return -1;
07077 }
07078
07079 pps->weighted_pred= get_bits1(&s->gb);
07080 pps->weighted_bipred_idc= get_bits(&s->gb, 2);
07081 pps->init_qp= get_se_golomb(&s->gb) + 26;
07082 pps->init_qs= get_se_golomb(&s->gb) + 26;
07083 pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
07084 pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
07085 pps->constrained_intra_pred= get_bits1(&s->gb);
07086 pps->redundant_pic_cnt_present = get_bits1(&s->gb);
07087
07088 if(get_bits_count(&s->gb) < bit_length){
07089 pps->transform_8x8_mode= get_bits1(&s->gb);
07090 if(get_bits1(&s->gb)){
07091 av_log(h->s.avctx, AV_LOG_ERROR, "custom scaling matrix not implemented\n");
07092 return -1;
07093 }
07094 get_se_golomb(&s->gb);
07095 }
07096
07097 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
07098 av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s %s\n",
07099 pps_id, pps->sps_id,
07100 pps->cabac ? "CABAC" : "CAVLC",
07101 pps->slice_group_count,
07102 pps->ref_count[0], pps->ref_count[1],
07103 pps->weighted_pred ? "weighted" : "",
07104 pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
07105 pps->deblocking_filter_parameters_present ? "LPAR" : "",
07106 pps->constrained_intra_pred ? "CONSTR" : "",
07107 pps->redundant_pic_cnt_present ? "REDU" : "",
07108 pps->transform_8x8_mode ? "8x8DCT" : ""
07109 );
07110 }
07111
07112 return 0;
07113 }
07114
07119 static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
07120 int i;
07121 uint32_t state;
07122 ParseContext *pc = &(h->s.parse_context);
07123
07124
07125 state= pc->state;
07126 for(i=0; i<=buf_size; i++){
07127 if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
07128 tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
07129 if(pc->frame_start_found){
07130
07131
07132
07133 if (i >= buf_size) break;
07134 if (buf[i] & 0x80) {
07135
07136
07137 tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
07138 pc->state=-1;
07139 pc->frame_start_found= 0;
07140 return i-4;
07141 }
07142 }
07143 pc->frame_start_found = 1;
07144 }
07145 if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
07146 if(pc->frame_start_found){
07147 pc->state=-1;
07148 pc->frame_start_found= 0;
07149 return i-4;
07150 }
07151 }
07152 if (i<buf_size)
07153 state= (state<<8) | buf[i];
07154 }
07155
07156 pc->state= state;
07157 return END_NOT_FOUND;
07158 }
07159
07160 static int h264_parse(AVCodecParserContext *s,
07161 AVCodecContext *avctx,
07162 uint8_t **poutbuf, int *poutbuf_size,
07163 const uint8_t *buf, int buf_size)
07164 {
07165 H264Context *h = s->priv_data;
07166 ParseContext *pc = &h->s.parse_context;
07167 int next;
07168
07169 next= find_frame_end(h, buf, buf_size);
07170
07171 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
07172 *poutbuf = NULL;
07173 *poutbuf_size = 0;
07174 return buf_size;
07175 }
07176
07177 *poutbuf = (uint8_t *)buf;
07178 *poutbuf_size = buf_size;
07179 return next;
07180 }
07181
07182 static int h264_split(AVCodecContext *avctx,
07183 const uint8_t *buf, int buf_size)
07184 {
07185 int i;
07186 uint32_t state = -1;
07187 int has_sps= 0;
07188
07189 for(i=0; i<=buf_size; i++){
07190 if((state&0xFFFFFF1F) == 0x107)
07191 has_sps=1;
07192
07193
07194 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
07195 if(has_sps){
07196 while(i>4 && buf[i-5]==0) i--;
07197 return i-4;
07198 }
07199 }
07200 if (i<buf_size)
07201 state= (state<<8) | buf[i];
07202 }
07203 return 0;
07204 }
07205
07206
07207 static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
07208 MpegEncContext * const s = &h->s;
07209 AVCodecContext * const avctx= s->avctx;
07210 int buf_index=0;
07211 #if 0
07212 int i;
07213 for(i=0; i<32; i++){
07214 printf("%X ", buf[i]);
07215 }
07216 #endif
07217 h->slice_num = 0;
07218 s->current_picture_ptr= NULL;
07219 for(;;){
07220 int consumed;
07221 int dst_length;
07222 int bit_length;
07223 uint8_t *ptr;
07224 int i, nalsize = 0;
07225
07226 if(h->is_avc) {
07227 if(buf_index >= buf_size) break;
07228 nalsize = 0;
07229 for(i = 0; i < h->nal_length_size; i++)
07230 nalsize = (nalsize << 8) | buf[buf_index++];
07231 } else {
07232
07233 for(; buf_index + 3 < buf_size; buf_index++){
07234
07235 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
07236 break;
07237 }
07238
07239 if(buf_index+3 >= buf_size) break;
07240
07241 buf_index+=3;
07242 }
07243
07244 ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
07245 if(ptr[dst_length - 1] == 0) dst_length--;
07246 bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
07247
07248 if(s->avctx->debug&FF_DEBUG_STARTCODE){
07249 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", h->nal_unit_type, buf_index, buf_size, dst_length);
07250 }
07251
07252 if (h->is_avc && (nalsize != consumed))
07253 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
07254
07255 buf_index += consumed;
07256
07257 if( (s->hurry_up == 1 && h->nal_ref_idc == 0)
07258 ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
07259 continue;
07260
07261 switch(h->nal_unit_type){
07262 case NAL_IDR_SLICE:
07263 idr(h);
07264 case NAL_SLICE:
07265 init_get_bits(&s->gb, ptr, bit_length);
07266 h->intra_gb_ptr=
07267 h->inter_gb_ptr= &s->gb;
07268 s->data_partitioning = 0;
07269
07270 if(decode_slice_header(h) < 0){
07271 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
07272 break;
07273 }
07274 if(h->redundant_pic_count==0 && s->hurry_up < 5
07275 && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
07276 && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
07277 && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
07278 && avctx->skip_frame < AVDISCARD_ALL)
07279 decode_slice(h);
07280 break;
07281 case NAL_DPA:
07282 init_get_bits(&s->gb, ptr, bit_length);
07283 h->intra_gb_ptr=
07284 h->inter_gb_ptr= NULL;
07285 s->data_partitioning = 1;
07286
07287 if(decode_slice_header(h) < 0){
07288 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
07289 }
07290 break;
07291 case NAL_DPB:
07292 init_get_bits(&h->intra_gb, ptr, bit_length);
07293 h->intra_gb_ptr= &h->intra_gb;
07294 break;
07295 case NAL_DPC:
07296 init_get_bits(&h->inter_gb, ptr, bit_length);
07297 h->inter_gb_ptr= &h->inter_gb;
07298
07299 if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
07300 && s->hurry_up < 5
07301 && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
07302 && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
07303 && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
07304 && avctx->skip_frame < AVDISCARD_ALL)
07305 decode_slice(h);
07306 break;
07307 case NAL_SEI:
07308 break;
07309 case NAL_SPS:
07310 init_get_bits(&s->gb, ptr, bit_length);
07311 decode_seq_parameter_set(h);
07312
07313 if(s->flags& CODEC_FLAG_LOW_DELAY)
07314 s->low_delay=1;
07315
07316 if(avctx->has_b_frames < 2)
07317 avctx->has_b_frames= !s->low_delay;
07318 break;
07319 case NAL_PPS:
07320 init_get_bits(&s->gb, ptr, bit_length);
07321
07322 decode_picture_parameter_set(h, bit_length);
07323
07324 break;
07325 case NAL_PICTURE_DELIMITER:
07326 break;
07327 case NAL_FILTER_DATA:
07328 break;
07329 default:
07330 av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
07331 }
07332 }
07333
07334 if(!s->current_picture_ptr) return buf_index;
07335
07336 s->current_picture_ptr->pict_type= s->pict_type;
07337 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
07338
07339 h->prev_frame_num_offset= h->frame_num_offset;
07340 h->prev_frame_num= h->frame_num;
07341 if(s->current_picture_ptr->reference){
07342 h->prev_poc_msb= h->poc_msb;
07343 h->prev_poc_lsb= h->poc_lsb;
07344 }
07345 if(s->current_picture_ptr->reference)
07346 execute_ref_pic_marking(h, h->mmco, h->mmco_index);
07347
07348 ff_er_frame_end(s);
07349
07350 MPV_frame_end(s);
07351
07352 return buf_index;
07353 }
07354
07358 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
07359 if(s->flags&CODEC_FLAG_TRUNCATED){
07360 pos -= s->parse_context.last_index;
07361 if(pos<0) pos=0;
07362
07363 return pos;
07364 }else{
07365 if(pos==0) pos=1;
07366 if(pos+10>buf_size) pos=buf_size;
07367
07368 return pos;
07369 }
07370 }
07371
07372 static int decode_frame(AVCodecContext *avctx,
07373 void *data, int *data_size,
07374 uint8_t *buf, int buf_size)
07375 {
07376 H264Context *h = avctx->priv_data;
07377 MpegEncContext *s = &h->s;
07378 AVFrame *pict = data;
07379 int buf_index;
07380
07381 s->flags= avctx->flags;
07382 s->flags2= avctx->flags2;
07383
07384
07385 if (buf_size == 0) {
07386 return 0;
07387 }
07388
07389 if(s->flags&CODEC_FLAG_TRUNCATED){
07390 int next= find_frame_end(h, buf, buf_size);
07391
07392 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
07393 return buf_size;
07394
07395 }
07396
07397 if(h->is_avc && !h->got_avcC) {
07398 int i, cnt, nalsize;
07399 unsigned char *p = avctx->extradata;
07400 if(avctx->extradata_size < 7) {
07401 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
07402 return -1;
07403 }
07404 if(*p != 1) {
07405 av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
07406 return -1;
07407 }
07408
07409
07410 h->nal_length_size = 2;
07411
07412 cnt = *(p+5) & 0x1f;
07413 p += 6;
07414 for (i = 0; i < cnt; i++) {
07415 nalsize = BE_16(p) + 2;
07416 if(decode_nal_units(h, p, nalsize) != nalsize) {
07417 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
07418 return -1;
07419 }
07420 p += nalsize;
07421 }
07422
07423 cnt = *(p++);
07424 for (i = 0; i < cnt; i++) {
07425 nalsize = BE_16(p) + 2;
07426 if(decode_nal_units(h, p, nalsize) != nalsize) {
07427 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
07428 return -1;
07429 }
07430 p += nalsize;
07431 }
07432
07433 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
07434
07435 h->got_avcC = 1;
07436 }
07437
07438 if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
07439 if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
07440 return -1;
07441 }
07442
07443 buf_index=decode_nal_units(h, buf, buf_size);
07444 if(buf_index < 0)
07445 return -1;
07446
07447
07448
07449
07450 if(!s->current_picture_ptr){
07451 av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
07452 return -1;
07453 }
07454
07455 {
07456 Picture *out = s->current_picture_ptr;
07457 #if 0 //decode order
07458 *data_size = sizeof(AVFrame);
07459 #else
07460
07461 Picture *cur = s->current_picture_ptr;
07462 Picture *prev = h->delayed_output_pic;
07463 int out_idx = 0;
07464 int pics = 0;
07465 int out_of_order;
07466 int cross_idr = 0;
07467 int dropped_frame = 0;
07468 int i;
07469
07470 if(h->sps.bitstream_restriction_flag
07471 && s->avctx->has_b_frames < h->sps.num_reorder_frames){
07472 s->avctx->has_b_frames = h->sps.num_reorder_frames;
07473 s->low_delay = 0;
07474 }
07475
07476 while(h->delayed_pic[pics]) pics++;
07477 h->delayed_pic[pics++] = cur;
07478 if(cur->reference == 0)
07479 cur->reference = 1;
07480
07481 for(i=0; h->delayed_pic[i]; i++)
07482 if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
07483 cross_idr = 1;
07484
07485 out = h->delayed_pic[0];
07486 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
07487 if(h->delayed_pic[i]->poc < out->poc){
07488 out = h->delayed_pic[i];
07489 out_idx = i;
07490 }
07491
07492 out_of_order = !cross_idr && prev && out->poc < prev->poc;
07493 if(prev && pics <= s->avctx->has_b_frames)
07494 out = prev;
07495 else if((out_of_order && pics-1 == s->avctx->has_b_frames)
07496 || (s->low_delay &&
07497 ((!cross_idr && prev && out->poc > prev->poc + 2)
07498 || cur->pict_type == B_TYPE)))
07499 {
07500 s->low_delay = 0;
07501 s->avctx->has_b_frames++;
07502 out = prev;
07503 }
07504 else if(out_of_order)
07505 out = prev;
07506
07507 if(out_of_order || pics > s->avctx->has_b_frames){
07508 dropped_frame = (out != h->delayed_pic[out_idx]);
07509 for(i=out_idx; h->delayed_pic[i]; i++)
07510 h->delayed_pic[i] = h->delayed_pic[i+1];
07511 }
07512
07513 if(prev == out && !dropped_frame)
07514 *data_size = 0;
07515 else
07516 *data_size = sizeof(AVFrame);
07517 if(prev && prev != out && prev->reference == 1)
07518 prev->reference = 0;
07519 h->delayed_output_pic = out;
07520 #endif
07521
07522 *pict= *(AVFrame*)out;
07523 }
07524
07525 assert(pict->data[0]);
07526 ff_print_debug_info(s, pict);
07527
07528 #if 0 //?
07529
07530
07531
07532 avctx->frame_number = s->picture_number - 1;
07533 #endif
07534 return get_consumed_bytes(s, buf_index, buf_size);
07535 }
07536 #if 0
07537 static inline void fill_mb_avail(H264Context *h){
07538 MpegEncContext * const s = &h->s;
07539 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
07540
07541 if(s->mb_y){
07542 h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
07543 h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
07544 h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
07545 }else{
07546 h->mb_avail[0]=
07547 h->mb_avail[1]=
07548 h->mb_avail[2]= 0;
07549 }
07550 h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
07551 h->mb_avail[4]= 1;
07552 h->mb_avail[5]= 0;
07553 }
07554 #endif
07555
07556 #if 0 //selftest
07557 #define COUNT 8000
07558 #define SIZE (COUNT*40)
07559 int main(){
07560 int i;
07561 uint8_t temp[SIZE];
07562 PutBitContext pb;
07563 GetBitContext gb;
07564
07565 DSPContext dsp;
07566 AVCodecContext avctx;
07567
07568 dsputil_init(&dsp, &avctx);
07569
07570 init_put_bits(&pb, temp, SIZE);
07571 printf("testing unsigned exp golomb\n");
07572 for(i=0; i<COUNT; i++){
07573 START_TIMER
07574 set_ue_golomb(&pb, i);
07575 STOP_TIMER("set_ue_golomb");
07576 }
07577 flush_put_bits(&pb);
07578
07579 init_get_bits(&gb, temp, 8*SIZE);
07580 for(i=0; i<COUNT; i++){
07581 int j, s;
07582
07583 s= show_bits(&gb, 24);
07584
07585 START_TIMER
07586 j= get_ue_golomb(&gb);
07587 if(j != i){
07588 printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
07589
07590 }
07591 STOP_TIMER("get_ue_golomb");
07592 }
07593
07594
07595 init_put_bits(&pb, temp, SIZE);
07596 printf("testing signed exp golomb\n");
07597 for(i=0; i<COUNT; i++){
07598 START_TIMER
07599 set_se_golomb(&pb, i - COUNT/2);
07600 STOP_TIMER("set_se_golomb");
07601 }
07602 flush_put_bits(&pb);
07603
07604 init_get_bits(&gb, temp, 8*SIZE);
07605 for(i=0; i<COUNT; i++){
07606 int j, s;
07607
07608 s= show_bits(&gb, 24);
07609
07610 START_TIMER
07611 j= get_se_golomb(&gb);
07612 if(j != i - COUNT/2){
07613 printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
07614
07615 }
07616 STOP_TIMER("get_se_golomb");
07617 }
07618
07619 printf("testing 4x4 (I)DCT\n");
07620
07621 DCTELEM block[16];
07622 uint8_t src[16], ref[16];
07623 uint64_t error= 0, max_error=0;
07624
07625 for(i=0; i<COUNT; i++){
07626 int j;
07627
07628 for(j=0; j<16; j++){
07629 ref[j]= random()%255;
07630 src[j]= random()%255;
07631 }
07632
07633 h264_diff_dct_c(block, src, ref, 4);
07634
07635
07636 for(j=0; j<16; j++){
07637
07638 block[j]= block[j]*4;
07639 if(j&1) block[j]= (block[j]*4 + 2)/5;
07640 if(j&4) block[j]= (block[j]*4 + 2)/5;
07641 }
07642
07643
07644 s->dsp.h264_idct_add(ref, block, 4);
07645
07646
07647
07648
07649
07650 for(j=0; j<16; j++){
07651 int diff= ABS(src[j] - ref[j]);
07652
07653 error+= diff*diff;
07654 max_error= FFMAX(max_error, diff);
07655 }
07656 }
07657 printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
07658 #if 0
07659 printf("testing quantizer\n");
07660 for(qp=0; qp<52; qp++){
07661 for(i=0; i<16; i++)
07662 src1_block[i]= src2_block[i]= random()%255;
07663
07664 }
07665 #endif
07666 printf("Testing NAL layer\n");
07667
07668 uint8_t bitstream[COUNT];
07669 uint8_t nal[COUNT*2];
07670 H264Context h;
07671 memset(&h, 0, sizeof(H264Context));
07672
07673 for(i=0; i<COUNT; i++){
07674 int zeros= i;
07675 int nal_length;
07676 int consumed;
07677 int out_length;
07678 uint8_t *out;
07679 int j;
07680
07681 for(j=0; j<COUNT; j++){
07682 bitstream[j]= (random() % 255) + 1;
07683 }
07684
07685 for(j=0; j<zeros; j++){
07686 int pos= random() % COUNT;
07687 while(bitstream[pos] == 0){
07688 pos++;
07689 pos %= COUNT;
07690 }
07691 bitstream[pos]=0;
07692 }
07693
07694 START_TIMER
07695
07696 nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
07697 if(nal_length<0){
07698 printf("encoding failed\n");
07699 return -1;
07700 }
07701
07702 out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
07703
07704 STOP_TIMER("NAL")
07705
07706 if(out_length != COUNT){
07707 printf("incorrect length %d %d\n", out_length, COUNT);
07708 return -1;
07709 }
07710
07711 if(consumed != nal_length){
07712 printf("incorrect consumed length %d %d\n", nal_length, consumed);
07713 return -1;
07714 }
07715
07716 if(memcmp(bitstream, out, COUNT)){
07717 printf("missmatch\n");
07718 return -1;
07719 }
07720 }
07721
07722 printf("Testing RBSP\n");
07723
07724
07725 return 0;
07726 }
07727 #endif
07728
07729
07730 static int decode_end(AVCodecContext *avctx)
07731 {
07732 H264Context *h = avctx->priv_data;
07733 MpegEncContext *s = &h->s;
07734
07735 free_tables(h);
07736 MPV_common_end(s);
07737
07738
07739
07740 return 0;
07741 }
07742
07743
07744 AVCodec h264_decoder = {
07745 "h264",
07746 CODEC_TYPE_VIDEO,
07747 CODEC_ID_H264,
07748 sizeof(H264Context),
07749 decode_init,
07750 NULL,
07751 decode_end,
07752 decode_frame,
07753 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
07754 .flush= flush_dpb,
07755 };
07756
07757 AVCodecParser h264_parser = {
07758 { CODEC_ID_H264 },
07759 sizeof(H264Context),
07760 NULL,
07761 h264_parse,
07762 ff_parse_close,
07763 h264_split,
07764 };
07765
07766 #include "svq3.c"