comparison h264.c @ 6783:df0893f4fd86 libavcodec

Store mb_xy in H264Context and only calculate it once per MB.
author astrange
date Sun, 11 May 2008 19:16:21 +0000
parents 73de16350531
children 2e43999e8ee6
comparison
equal deleted inserted replaced
6782:781bfad6e40e 6783:df0893f4fd86
79 }; 79 };
80 80
81 81
82 static void fill_caches(H264Context *h, int mb_type, int for_deblock){ 82 static void fill_caches(H264Context *h, int mb_type, int for_deblock){
83 MpegEncContext * const s = &h->s; 83 MpegEncContext * const s = &h->s;
84 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 84 const int mb_xy= h->mb_xy;
85 int topleft_xy, top_xy, topright_xy, left_xy[2]; 85 int topleft_xy, top_xy, topright_xy, left_xy[2];
86 int topleft_type, top_type, topright_type, left_type[2]; 86 int topleft_type, top_type, topright_type, left_type[2];
87 int left_block[8]; 87 int left_block[8];
88 int topleft_partition= -1; 88 int topleft_partition= -1;
89 int i; 89 int i;
549 h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]); 549 h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
550 } 550 }
551 551
552 static inline void write_back_intra_pred_mode(H264Context *h){ 552 static inline void write_back_intra_pred_mode(H264Context *h){
553 MpegEncContext * const s = &h->s; 553 MpegEncContext * const s = &h->s;
554 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 554 const int mb_xy= h->mb_xy;
555 555
556 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1]; 556 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
557 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2]; 557 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
558 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3]; 558 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
559 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4]; 559 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
645 else return min; 645 else return min;
646 } 646 }
647 647
648 static inline void write_back_non_zero_count(H264Context *h){ 648 static inline void write_back_non_zero_count(H264Context *h){
649 MpegEncContext * const s = &h->s; 649 MpegEncContext * const s = &h->s;
650 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 650 const int mb_xy= h->mb_xy;
651 651
652 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1]; 652 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
653 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2]; 653 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
654 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3]; 654 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
655 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4]; 655 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
955 } 955 }
956 } 956 }
957 957
958 static inline void pred_direct_motion(H264Context * const h, int *mb_type){ 958 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
959 MpegEncContext * const s = &h->s; 959 MpegEncContext * const s = &h->s;
960 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; 960 const int mb_xy = h->mb_xy;
961 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride; 961 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
962 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; 962 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
963 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy]; 963 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
964 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy]; 964 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
965 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy]; 965 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
1871 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 1871 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1872 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put), 1872 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
1873 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg), 1873 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
1874 h264_weight_func *weight_op, h264_biweight_func *weight_avg){ 1874 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
1875 MpegEncContext * const s = &h->s; 1875 MpegEncContext * const s = &h->s;
1876 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 1876 const int mb_xy= h->mb_xy;
1877 const int mb_type= s->current_picture.mb_type[mb_xy]; 1877 const int mb_type= s->current_picture.mb_type[mb_xy];
1878 1878
1879 assert(IS_INTER(mb_type)); 1879 assert(IS_INTER(mb_type));
1880 1880
1881 prefetch_motion(h, 0); 1881 prefetch_motion(h, 0);
2297 int deblock_left; 2297 int deblock_left;
2298 int deblock_top; 2298 int deblock_top;
2299 int mb_xy; 2299 int mb_xy;
2300 2300
2301 if(h->deblocking_filter == 2) { 2301 if(h->deblocking_filter == 2) {
2302 mb_xy = s->mb_x + s->mb_y*s->mb_stride; 2302 mb_xy = h->mb_xy;
2303 deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1]; 2303 deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
2304 deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy]; 2304 deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
2305 } else { 2305 } else {
2306 deblock_left = (s->mb_x > 0); 2306 deblock_left = (s->mb_x > 0);
2307 deblock_top = (s->mb_y > 0); 2307 deblock_top = (s->mb_y > 0);
2436 2436
2437 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){ 2437 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
2438 MpegEncContext * const s = &h->s; 2438 MpegEncContext * const s = &h->s;
2439 const int mb_x= s->mb_x; 2439 const int mb_x= s->mb_x;
2440 const int mb_y= s->mb_y; 2440 const int mb_y= s->mb_y;
2441 const int mb_xy= mb_x + mb_y*s->mb_stride; 2441 const int mb_xy= h->mb_xy;
2442 const int mb_type= s->current_picture.mb_type[mb_xy]; 2442 const int mb_type= s->current_picture.mb_type[mb_xy];
2443 uint8_t *dest_y, *dest_cb, *dest_cr; 2443 uint8_t *dest_y, *dest_cb, *dest_cr;
2444 int linesize, uvlinesize /*dct_offset*/; 2444 int linesize, uvlinesize /*dct_offset*/;
2445 int i; 2445 int i;
2446 int *block_offset = &h->block_offset[0]; 2446 int *block_offset = &h->block_offset[0];
2686 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0); 2686 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
2687 2687
2688 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize); 2688 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
2689 // deblock a pair 2689 // deblock a pair
2690 // top 2690 // top
2691 s->mb_y--; 2691 s->mb_y--; h->mb_xy -= s->mb_stride;
2692 tprintf(h->s.avctx, "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); 2692 tprintf(h->s.avctx, "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);
2693 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb 2693 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
2694 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]); 2694 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
2695 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]); 2695 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
2696 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize); 2696 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
2697 // bottom 2697 // bottom
2698 s->mb_y++; 2698 s->mb_y++; h->mb_xy += s->mb_stride;
2699 tprintf(h->s.avctx, "call mbaff filter_mb\n"); 2699 tprintf(h->s.avctx, "call mbaff filter_mb\n");
2700 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb 2700 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
2701 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]); 2701 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
2702 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]); 2702 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
2703 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize); 2703 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2726 2726
2727 static void hl_decode_mb(H264Context *h){ 2727 static void hl_decode_mb(H264Context *h){
2728 MpegEncContext * const s = &h->s; 2728 MpegEncContext * const s = &h->s;
2729 const int mb_x= s->mb_x; 2729 const int mb_x= s->mb_x;
2730 const int mb_y= s->mb_y; 2730 const int mb_y= s->mb_y;
2731 const int mb_xy= mb_x + mb_y*s->mb_stride; 2731 const int mb_xy= h->mb_xy;
2732 const int mb_type= s->current_picture.mb_type[mb_xy]; 2732 const int mb_type= s->current_picture.mb_type[mb_xy];
2733 int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding; 2733 int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding;
2734 2734
2735 if(!s->decode) 2735 if(!s->decode)
2736 return; 2736 return;
4441 return 0; 4441 return 0;
4442 } 4442 }
4443 4443
4444 static void predict_field_decoding_flag(H264Context *h){ 4444 static void predict_field_decoding_flag(H264Context *h){
4445 MpegEncContext * const s = &h->s; 4445 MpegEncContext * const s = &h->s;
4446 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 4446 const int mb_xy= h->mb_xy;
4447 int mb_type = (h->slice_table[mb_xy-1] == h->slice_num) 4447 int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
4448 ? s->current_picture.mb_type[mb_xy-1] 4448 ? s->current_picture.mb_type[mb_xy-1]
4449 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num) 4449 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
4450 ? s->current_picture.mb_type[mb_xy-s->mb_stride] 4450 ? s->current_picture.mb_type[mb_xy-s->mb_stride]
4451 : 0; 4451 : 0;
4455 /** 4455 /**
4456 * decodes a P_SKIP or B_SKIP macroblock 4456 * decodes a P_SKIP or B_SKIP macroblock
4457 */ 4457 */
4458 static void decode_mb_skip(H264Context *h){ 4458 static void decode_mb_skip(H264Context *h){
4459 MpegEncContext * const s = &h->s; 4459 MpegEncContext * const s = &h->s;
4460 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 4460 const int mb_xy= h->mb_xy;
4461 int mb_type=0; 4461 int mb_type=0;
4462 4462
4463 memset(h->non_zero_count[mb_xy], 0, 16); 4463 memset(h->non_zero_count[mb_xy], 0, 16);
4464 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui 4464 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
4465 4465
4497 * decodes a macroblock 4497 * decodes a macroblock
4498 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed 4498 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
4499 */ 4499 */
4500 static int decode_mb_cavlc(H264Context *h){ 4500 static int decode_mb_cavlc(H264Context *h){
4501 MpegEncContext * const s = &h->s; 4501 MpegEncContext * const s = &h->s;
4502 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 4502 int mb_xy;
4503 int partition_count; 4503 int partition_count;
4504 unsigned int mb_type, cbp; 4504 unsigned int mb_type, cbp;
4505 int dct8x8_allowed= h->pps.transform_8x8_mode; 4505 int dct8x8_allowed= h->pps.transform_8x8_mode;
4506
4507 mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
4506 4508
4507 s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong? 4509 s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
4508 4510
4509 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); 4511 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
4510 cbp = 0; /* avoid warning. FIXME: find a solution without slowing 4512 cbp = 0; /* avoid warning. FIXME: find a solution without slowing
5119 && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) 5121 && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
5120 mbb_xy -= s->mb_stride; 5122 mbb_xy -= s->mb_stride;
5121 }else 5123 }else
5122 mbb_xy = mb_x + (mb_y-1)*s->mb_stride; 5124 mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
5123 }else{ 5125 }else{
5124 int mb_xy = mb_x + mb_y*s->mb_stride; 5126 int mb_xy = h->mb_xy;
5125 mba_xy = mb_xy - 1; 5127 mba_xy = mb_xy - 1;
5126 mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE); 5128 mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
5127 } 5129 }
5128 5130
5129 if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )) 5131 if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
5370 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5372 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5371 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8 5373 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
5372 }; 5374 };
5373 5375
5374 static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) { 5376 static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
5375 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride; 5377 const int mb_xy = h->mb_xy;
5376 static const int significant_coeff_flag_offset[2][6] = { 5378 static const int significant_coeff_flag_offset[2][6] = {
5377 { 105+0, 105+15, 105+29, 105+44, 105+47, 402 }, 5379 { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
5378 { 277+0, 277+15, 277+29, 277+44, 277+47, 436 } 5380 { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
5379 }; 5381 };
5380 static const int last_coeff_flag_offset[2][6] = { 5382 static const int last_coeff_flag_offset[2][6] = {
5556 } 5558 }
5557 5559
5558 static inline void compute_mb_neighbors(H264Context *h) 5560 static inline void compute_mb_neighbors(H264Context *h)
5559 { 5561 {
5560 MpegEncContext * const s = &h->s; 5562 MpegEncContext * const s = &h->s;
5561 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; 5563 const int mb_xy = h->mb_xy;
5562 h->top_mb_xy = mb_xy - s->mb_stride; 5564 h->top_mb_xy = mb_xy - s->mb_stride;
5563 h->left_mb_xy[0] = mb_xy - 1; 5565 h->left_mb_xy[0] = mb_xy - 1;
5564 if(FRAME_MBAFF){ 5566 if(FRAME_MBAFF){
5565 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride; 5567 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
5566 const int top_pair_xy = pair_xy - s->mb_stride; 5568 const int top_pair_xy = pair_xy - s->mb_stride;
5587 * decodes a macroblock 5589 * decodes a macroblock
5588 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed 5590 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
5589 */ 5591 */
5590 static int decode_mb_cabac(H264Context *h) { 5592 static int decode_mb_cabac(H264Context *h) {
5591 MpegEncContext * const s = &h->s; 5593 MpegEncContext * const s = &h->s;
5592 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 5594 int mb_xy;
5593 int mb_type, partition_count, cbp = 0; 5595 int mb_type, partition_count, cbp = 0;
5594 int dct8x8_allowed= h->pps.transform_8x8_mode; 5596 int dct8x8_allowed= h->pps.transform_8x8_mode;
5597
5598 mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
5595 5599
5596 s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?) 5600 s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
5597 5601
5598 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); 5602 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
5599 if( h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE ) { 5603 if( h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE ) {
6387 MpegEncContext * const s = &h->s; 6391 MpegEncContext * const s = &h->s;
6388 int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD; 6392 int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
6389 int mb_xy, mb_type; 6393 int mb_xy, mb_type;
6390 int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh; 6394 int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
6391 6395
6392 mb_xy = mb_x + mb_y*s->mb_stride; 6396 mb_xy = h->mb_xy;
6393 6397
6394 if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff || 6398 if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
6395 (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] || 6399 (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
6396 h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) { 6400 h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
6397 filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize); 6401 filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);