Mercurial > libavcodec.hg
comparison h264.c @ 5226:65bffcc5571a libavcodec
Precompute a chroma_qp table with index offset for each pps,
this saves speed for the upcoming secondqp fix.
Patch by Andreas ªÓman % andreas A olebyn P nu %
Original thread:
Date: Jun 26, 2007 8:48 PM
subject: [FFmpeg-devel] Color corruption and seeking errors with H264 disc sources
author | gpoirier |
---|---|
date | Fri, 06 Jul 2007 07:58:08 +0000 |
parents | 099c495618c1 |
children | 07a97575d0c4 |
comparison
equal
deleted
inserted
replaced
5225:099c495618c1 | 5226:65bffcc5571a |
---|---|
1596 #endif | 1596 #endif |
1597 | 1597 |
1598 /** | 1598 /** |
1599 * gets the chroma qp. | 1599 * gets the chroma qp. |
1600 */ | 1600 */ |
1601 static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){ | 1601 static inline int get_chroma_qp(H264Context *h, int qscale){ |
1602 | 1602 return h->pps.chroma_qp_table[qscale & 0xff]; |
1603 return chroma_qp[av_clip(qscale + chroma_qp_index_offset, 0, 51)]; | |
1604 } | 1603 } |
1605 | 1604 |
1606 //FIXME need to check that this does not overflow signed 32 bit for low qp, i am not sure, it's very close | 1605 //FIXME need to check that this does not overflow signed 32 bit for low qp, i am not sure, it's very close |
1607 //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away) | 1606 //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away) |
1608 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){ | 1607 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){ |
3418 // deblock a pair | 3417 // deblock a pair |
3419 // top | 3418 // top |
3420 s->mb_y--; | 3419 s->mb_y--; |
3421 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); | 3420 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); |
3422 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb | 3421 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb |
3423 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]); | 3422 h->chroma_qp = get_chroma_qp(h, s->current_picture.qscale_table[mb_xy]); |
3424 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize); | 3423 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize); |
3425 // bottom | 3424 // bottom |
3426 s->mb_y++; | 3425 s->mb_y++; |
3427 tprintf(h->s.avctx, "call mbaff filter_mb\n"); | 3426 tprintf(h->s.avctx, "call mbaff filter_mb\n"); |
3428 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb | 3427 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb |
3429 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]); | 3428 h->chroma_qp = get_chroma_qp(h, s->current_picture.qscale_table[mb_xy+s->mb_stride]); |
3430 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize); | 3429 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize); |
3431 } else { | 3430 } else { |
3432 tprintf(h->s.avctx, "call filter_mb\n"); | 3431 tprintf(h->s.avctx, "call filter_mb\n"); |
3433 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple); | 3432 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple); |
3434 fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb | 3433 fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb |
4446 if(tmp>51){ | 4445 if(tmp>51){ |
4447 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp); | 4446 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp); |
4448 return -1; | 4447 return -1; |
4449 } | 4448 } |
4450 s->qscale= tmp; | 4449 s->qscale= tmp; |
4451 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); | 4450 h->chroma_qp = get_chroma_qp(h, s->qscale); |
4452 //FIXME qscale / qp ... stuff | 4451 //FIXME qscale / qp ... stuff |
4453 if(h->slice_type == SP_TYPE){ | 4452 if(h->slice_type == SP_TYPE){ |
4454 get_bits1(&s->gb); /* sp_for_switch_flag */ | 4453 get_bits1(&s->gb); /* sp_for_switch_flag */ |
4455 } | 4454 } |
4456 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){ | 4455 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){ |
4859 } | 4858 } |
4860 } | 4859 } |
4861 | 4860 |
4862 // In deblocking, the quantizer is 0 | 4861 // In deblocking, the quantizer is 0 |
4863 s->current_picture.qscale_table[mb_xy]= 0; | 4862 s->current_picture.qscale_table[mb_xy]= 0; |
4864 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0); | 4863 h->chroma_qp = get_chroma_qp(h, 0); |
4865 // All coeffs are present | 4864 // All coeffs are present |
4866 memset(h->non_zero_count[mb_xy], 16, 16); | 4865 memset(h->non_zero_count[mb_xy], 16, 16); |
4867 | 4866 |
4868 s->current_picture.mb_type[mb_xy]= mb_type; | 4867 s->current_picture.mb_type[mb_xy]= mb_type; |
4869 return 0; | 4868 return 0; |
5162 if(((unsigned)s->qscale) > 51){ | 5161 if(((unsigned)s->qscale) > 51){ |
5163 if(s->qscale<0) s->qscale+= 52; | 5162 if(s->qscale<0) s->qscale+= 52; |
5164 else s->qscale-= 52; | 5163 else s->qscale-= 52; |
5165 } | 5164 } |
5166 | 5165 |
5167 h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); | 5166 h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale); |
5168 if(IS_INTRA16x16(mb_type)){ | 5167 if(IS_INTRA16x16(mb_type)){ |
5169 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){ | 5168 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){ |
5170 return -1; //FIXME continue if partitioned and other return -1 too | 5169 return -1; //FIXME continue if partitioned and other return -1 too |
5171 } | 5170 } |
5172 | 5171 |
6012 // All blocks are present | 6011 // All blocks are present |
6013 h->cbp_table[mb_xy] = 0x1ef; | 6012 h->cbp_table[mb_xy] = 0x1ef; |
6014 h->chroma_pred_mode_table[mb_xy] = 0; | 6013 h->chroma_pred_mode_table[mb_xy] = 0; |
6015 // In deblocking, the quantizer is 0 | 6014 // In deblocking, the quantizer is 0 |
6016 s->current_picture.qscale_table[mb_xy]= 0; | 6015 s->current_picture.qscale_table[mb_xy]= 0; |
6017 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0); | 6016 h->chroma_qp = get_chroma_qp(h, 0); |
6018 // All coeffs are present | 6017 // All coeffs are present |
6019 memset(h->non_zero_count[mb_xy], 16, 16); | 6018 memset(h->non_zero_count[mb_xy], 16, 16); |
6020 s->current_picture.mb_type[mb_xy]= mb_type; | 6019 s->current_picture.mb_type[mb_xy]= mb_type; |
6021 return 0; | 6020 return 0; |
6022 } | 6021 } |
6288 s->qscale += dqp; | 6287 s->qscale += dqp; |
6289 if(((unsigned)s->qscale) > 51){ | 6288 if(((unsigned)s->qscale) > 51){ |
6290 if(s->qscale<0) s->qscale+= 52; | 6289 if(s->qscale<0) s->qscale+= 52; |
6291 else s->qscale-= 52; | 6290 else s->qscale-= 52; |
6292 } | 6291 } |
6293 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); | 6292 h->chroma_qp = get_chroma_qp(h, s->qscale); |
6294 | 6293 |
6295 if( IS_INTRA16x16( mb_type ) ) { | 6294 if( IS_INTRA16x16( mb_type ) ) { |
6296 int i; | 6295 int i; |
6297 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" ); | 6296 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" ); |
6298 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0) | 6297 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0) |
6696 | 6695 |
6697 mb_type = s->current_picture.mb_type[mb_xy]; | 6696 mb_type = s->current_picture.mb_type[mb_xy]; |
6698 qp = s->current_picture.qscale_table[mb_xy]; | 6697 qp = s->current_picture.qscale_table[mb_xy]; |
6699 qp0 = s->current_picture.qscale_table[mb_xy-1]; | 6698 qp0 = s->current_picture.qscale_table[mb_xy-1]; |
6700 qp1 = s->current_picture.qscale_table[h->top_mb_xy]; | 6699 qp1 = s->current_picture.qscale_table[h->top_mb_xy]; |
6701 qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp ); | 6700 qpc = get_chroma_qp( h, qp ); |
6702 qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 ); | 6701 qpc0 = get_chroma_qp( h, qp0 ); |
6703 qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 ); | 6702 qpc1 = get_chroma_qp( h, qp1 ); |
6704 qp0 = (qp + qp0 + 1) >> 1; | 6703 qp0 = (qp + qp0 + 1) >> 1; |
6705 qp1 = (qp + qp1 + 1) >> 1; | 6704 qp1 = (qp + qp1 + 1) >> 1; |
6706 qpc0 = (qpc + qpc0 + 1) >> 1; | 6705 qpc0 = (qpc + qpc0 + 1) >> 1; |
6707 qpc1 = (qpc + qpc1 + 1) >> 1; | 6706 qpc1 = (qpc + qpc1 + 1) >> 1; |
6708 qp_thresh = 15 - h->slice_alpha_c0_offset; | 6707 qp_thresh = 15 - h->slice_alpha_c0_offset; |
6853 | 6852 |
6854 mb_qp = s->current_picture.qscale_table[mb_xy]; | 6853 mb_qp = s->current_picture.qscale_table[mb_xy]; |
6855 mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]]; | 6854 mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]]; |
6856 mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]]; | 6855 mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]]; |
6857 qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1; | 6856 qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1; |
6858 chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) + | 6857 chroma_qp[0] = ( get_chroma_qp( h, mb_qp ) + |
6859 get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1; | 6858 get_chroma_qp( h, mbn0_qp ) + 1 ) >> 1; |
6860 qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1; | 6859 qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1; |
6861 chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) + | 6860 chroma_qp[1] = ( get_chroma_qp( h, mb_qp ) + |
6862 get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1; | 6861 get_chroma_qp( h, mbn1_qp ) + 1 ) >> 1; |
6863 | 6862 |
6864 /* Filter edge */ | 6863 /* Filter edge */ |
6865 tprintf(s->avctx, "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); | 6864 tprintf(s->avctx, "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); |
6866 { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } | 6865 { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } |
6867 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp ); | 6866 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp ); |
6927 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; | 6926 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; |
6928 tprintf(s->avctx, "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); | 6927 tprintf(s->avctx, "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); |
6929 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } | 6928 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } |
6930 filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp ); | 6929 filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp ); |
6931 chroma_qp = ( h->chroma_qp + | 6930 chroma_qp = ( h->chroma_qp + |
6932 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; | 6931 get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; |
6933 filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp ); | 6932 filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp ); |
6934 filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp ); | 6933 filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp ); |
6935 } | 6934 } |
6936 | 6935 |
6937 start = 1; | 6936 start = 1; |
7027 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } | 7026 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } |
7028 if( dir == 0 ) { | 7027 if( dir == 0 ) { |
7029 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp ); | 7028 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp ); |
7030 if( (edge&1) == 0 ) { | 7029 if( (edge&1) == 0 ) { |
7031 int chroma_qp = ( h->chroma_qp + | 7030 int chroma_qp = ( h->chroma_qp + |
7032 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; | 7031 get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; |
7033 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp ); | 7032 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp ); |
7034 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp ); | 7033 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp ); |
7035 } | 7034 } |
7036 } else { | 7035 } else { |
7037 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp ); | 7036 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp ); |
7038 if( (edge&1) == 0 ) { | 7037 if( (edge&1) == 0 ) { |
7039 int chroma_qp = ( h->chroma_qp + | 7038 int chroma_qp = ( h->chroma_qp + |
7040 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; | 7039 get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; |
7041 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); | 7040 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); |
7042 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); | 7041 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); |
7043 } | 7042 } |
7044 } | 7043 } |
7045 } | 7044 } |
7568 sps->crop_top, sps->crop_bottom, | 7567 sps->crop_top, sps->crop_bottom, |
7569 sps->vui_parameters_present_flag ? "VUI" : "" | 7568 sps->vui_parameters_present_flag ? "VUI" : "" |
7570 ); | 7569 ); |
7571 } | 7570 } |
7572 return 0; | 7571 return 0; |
7572 } | |
7573 | |
7574 static void | |
7575 build_qp_table(PPS *pps, int index) | |
7576 { | |
7577 int i; | |
7578 for(i = 0; i < 255; i++) | |
7579 pps->chroma_qp_table[i & 0xff] = chroma_qp[av_clip(i + index, 0, 51)]; | |
7580 pps->chroma_qp_index_offset = index; | |
7573 } | 7581 } |
7574 | 7582 |
7575 static inline int decode_picture_parameter_set(H264Context *h, int bit_length){ | 7583 static inline int decode_picture_parameter_set(H264Context *h, int bit_length){ |
7576 MpegEncContext * const s = &h->s; | 7584 MpegEncContext * const s = &h->s; |
7577 unsigned int tmp, pps_id= get_ue_golomb(&s->gb); | 7585 unsigned int tmp, pps_id= get_ue_golomb(&s->gb); |
7638 | 7646 |
7639 pps->weighted_pred= get_bits1(&s->gb); | 7647 pps->weighted_pred= get_bits1(&s->gb); |
7640 pps->weighted_bipred_idc= get_bits(&s->gb, 2); | 7648 pps->weighted_bipred_idc= get_bits(&s->gb, 2); |
7641 pps->init_qp= get_se_golomb(&s->gb) + 26; | 7649 pps->init_qp= get_se_golomb(&s->gb) + 26; |
7642 pps->init_qs= get_se_golomb(&s->gb) + 26; | 7650 pps->init_qs= get_se_golomb(&s->gb) + 26; |
7643 pps->chroma_qp_index_offset= get_se_golomb(&s->gb); | 7651 build_qp_table(pps, get_se_golomb(&s->gb)); |
7644 pps->deblocking_filter_parameters_present= get_bits1(&s->gb); | 7652 pps->deblocking_filter_parameters_present= get_bits1(&s->gb); |
7645 pps->constrained_intra_pred= get_bits1(&s->gb); | 7653 pps->constrained_intra_pred= get_bits1(&s->gb); |
7646 pps->redundant_pic_cnt_present = get_bits1(&s->gb); | 7654 pps->redundant_pic_cnt_present = get_bits1(&s->gb); |
7647 | 7655 |
7648 pps->transform_8x8_mode= 0; | 7656 pps->transform_8x8_mode= 0; |