X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fh263.c;h=3d4ba83863ffd68c525cd3588c4ebafed5b0ad69;hb=55bf7fb6130a2a13c30498a787d6dfead39ecceb;hp=6a934b5397879421372037921c42486a63eb0b35;hpb=176629551635fc74e0cba50e0fbe6ebb9c6a2565;p=ffmpeg diff --git a/libavcodec/h263.c b/libavcodec/h263.c index 6a934b53978..3d4ba83863f 100644 --- a/libavcodec/h263.c +++ b/libavcodec/h263.c @@ -5,6 +5,10 @@ * Copyright (c) 2001 Juan J. Sierralta P. * Copyright (c) 2002-2004 Michael Niedermayer * + * ac prediction encoding, B-frame support, error resilience, optimizations, + * qpel decoding, gmc decoding, interlaced decoding + * by Michael Niedermayer + * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or @@ -20,10 +24,6 @@ * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * ac prediction encoding, b-frame support, error resilience, optimizations, - * qpel decoding, gmc decoding, interlaced decoding, - * by Michael Niedermayer */ /** @@ -34,7 +34,6 @@ //#define DEBUG #include -#include "common.h" #include "dsputil.h" #include "avcodec.h" #include "mpegvideo.h" @@ -87,7 +86,7 @@ static uint8_t uni_DCtab_chrom_len[512]; static uint16_t uni_DCtab_lum_bits[512]; static uint16_t uni_DCtab_chrom_bits[512]; -static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; +static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; static uint8_t fcode_tab[MAX_MV*2+1]; static uint8_t umv_fcode_tab[MAX_MV*2+1]; @@ -112,6 +111,8 @@ max run: 29/41 */ #endif +static uint8_t static_rl_table_store[5][2][2*MAX_RUN + MAX_LEVEL + 3]; + #if 0 //3IV1 is quite rare and it slows things down a tiny bit #define IS_3IV1 s->codec_tag == ff_get_fourcc("3IV1") #else @@ -137,6 +138,23 @@ int h263_get_picture_format(int width, int height) return format; } +static void show_pict_info(MpegEncContext *s){ + av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n", + s->qscale, av_get_pict_type_char(s->pict_type), + s->gb.size_in_bits, 1-s->no_rounding, + s->obmc ? " AP" : "", + s->umvplus ? " UMV" : "", + s->h263_long_vectors ? " LONG" : "", + s->h263_plus ? " +" : "", + s->h263_aic ? " AIC" : "", + s->alt_inter_vlc ? " AIV" : "", + s->modified_quant ? " MQ" : "", + s->loop_filter ? " LOOP" : "", + s->h263_slice_structured ? " SS" : "", + s->avctx->time_base.den, s->avctx->time_base.num + ); +} + #ifdef CONFIG_ENCODERS static void aspect_to_info(MpegEncContext * s, AVRational aspect){ @@ -211,7 +229,7 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number) for(i=0; i<2; i++){ int div, error; div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den); - div= clip(1, div, 127); + div= av_clip(div, 1, 127); error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div); if(error < best_error){ best_error= error; @@ -486,6 +504,20 @@ static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], i } } +/** + * init s->current_picture.qscale_table from s->lambda_table + */ +static void ff_init_qscale_tab(MpegEncContext *s){ + int8_t * const qscale_table= s->current_picture.qscale_table; + int i; + + for(i=0; imb_num; i++){ + unsigned int lam= s->lambda_table[ s->mb_index2xy[i] ]; + int qp= (lam*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7); + qscale_table[ s->mb_index2xy[i] ]= av_clip(qp, s->avctx->qmin, s->avctx->qmax); + } +} + /** * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2) */ @@ -493,6 +525,8 @@ void ff_clean_h263_qscales(MpegEncContext *s){ int i; int8_t * const qscale_table= s->current_picture.qscale_table; + ff_init_qscale_tab(s); + for(i=1; imb_num; i++){ if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2) qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2; @@ -507,7 +541,6 @@ void ff_clean_h263_qscales(MpegEncContext *s){ int mb_xy= s->mb_index2xy[i]; if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){ - s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V; s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER; } } @@ -546,7 +579,6 @@ void ff_clean_mpeg4_qscales(MpegEncContext *s){ for(i=1; imb_num; i++){ int mb_xy= s->mb_index2xy[i]; if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){ - s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT; s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR; } } @@ -878,7 +910,7 @@ void mpeg4_encode_mb(MpegEncContext * s, int i, cbp; if(s->pict_type==B_TYPE){ - static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */ + static const int mb_type_table[8]= {-1, 3, 2, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */ int mb_type= mb_type_table[s->mv_dir]; if(s->mb_x==0){ @@ -1783,9 +1815,6 @@ static void init_mv_penalty_and_fcode(MpegEncContext *s) int f_code; int mv; - if(mv_penalty==NULL) - mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); - for(f_code=1; f_code<=MAX_FCODE; f_code++){ for(mv=-MAX_MV; mv<=MAX_MV; mv++){ int len; @@ -2019,9 +2048,9 @@ void h263_encode_init(MpegEncContext *s) init_uni_dc_tab(); - init_rl(&rl_inter, 1); - init_rl(&rl_intra, 1); - init_rl(&rl_intra_aic, 1); + init_rl(&rl_inter, static_rl_table_store[0]); + init_rl(&rl_intra, static_rl_table_store[1]); + init_rl(&rl_intra_aic, static_rl_table_store[2]); init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len); init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len); @@ -2247,25 +2276,12 @@ void ff_mpeg4_stuffing(PutBitContext * pbc) } /* must be called before writing the header */ -void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){ - int time_div, time_mod; - - assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE); - s->time= s->current_picture_ptr->pts*s->avctx->time_base.num; - - time_div= s->time/s->avctx->time_base.den; - time_mod= s->time%s->avctx->time_base.den; - +void ff_set_mpeg4_time(MpegEncContext * s){ if(s->pict_type==B_TYPE){ - s->pb_time= s->pp_time - (s->last_non_b_time - s->time); - assert(s->pb_time > 0 && s->pb_time < s->pp_time); ff_mpeg4_init_direct_mv(s); }else{ s->last_time_base= s->time_base; - s->time_base= time_div; - s->pp_time= s->time - s->last_non_b_time; - s->last_non_b_time= s->time; - assert(picture_number==0 || s->pp_time > 0); + s->time_base= s->time/s->avctx->time_base.den; } } @@ -2348,6 +2364,8 @@ static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_n { int vo_ver_id; + if (!ENABLE_MPEG4_ENCODER) return; + if(s->max_b_frames || s->quarter_sample){ vo_ver_id= 5; s->vo_type= ADV_SIMPLE_VO_TYPE; @@ -2504,23 +2522,6 @@ void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number) #endif //CONFIG_ENCODERS -/** - * set qscale and update qscale dependant variables. - */ -void ff_set_qscale(MpegEncContext * s, int qscale) -{ - if (qscale < 1) - qscale = 1; - else if (qscale > 31) - qscale = 31; - - s->qscale = qscale; - s->chroma_qscale= s->chroma_qscale_table[qscale]; - - s->y_dc_scale= s->y_dc_scale_table[ qscale ]; - s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; -} - /** * predicts the dc. * encoding quantized level -> quantized diff @@ -2905,59 +2906,6 @@ static VLC mb_type_b_vlc; static VLC h263_mbtype_b_vlc; static VLC cbpc_b_vlc; -void init_vlc_rl(RLTable *rl, int use_static) -{ - int i, q; - - /* Return if static table is already initialized */ - if(use_static && rl->rl_vlc[0]) - return; - - init_vlc(&rl->vlc, 9, rl->n + 1, - &rl->table_vlc[0][1], 4, 2, - &rl->table_vlc[0][0], 4, 2, use_static); - - - for(q=0; q<32; q++){ - int qmul= q*2; - int qadd= (q-1)|1; - - if(q==0){ - qmul=1; - qadd=0; - } - if(use_static) - rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); - else - rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); - for(i=0; ivlc.table_size; i++){ - int code= rl->vlc.table[i][0]; - int len = rl->vlc.table[i][1]; - int level, run; - - if(len==0){ // illegal code - run= 66; - level= MAX_LEVEL; - }else if(len<0){ //more bits needed - run= 0; - level= code; - }else{ - if(code==rl->n){ //esc - run= 66; - level= 0; - }else{ - run= rl->table_run [code] + 1; - level= rl->table_level[code] * qmul + qadd; - if(code >= rl->last) run+=192; - } - } - rl->rl_vlc[q][i].len= len; - rl->rl_vlc[q][i].level= level; - rl->rl_vlc[q][i].run= run; - } - } -} - /* init vlcs */ /* XXX: find a better solution to handle static init */ @@ -2980,11 +2928,11 @@ void h263_decode_init_vlc(MpegEncContext *s) init_vlc(&mv_vlc, MV_VLC_BITS, 33, &mvtab[0][1], 2, 1, &mvtab[0][0], 2, 1, 1); - init_rl(&rl_inter, 1); - init_rl(&rl_intra, 1); - init_rl(&rvlc_rl_inter, 1); - init_rl(&rvlc_rl_intra, 1); - init_rl(&rl_intra_aic, 1); + init_rl(&rl_inter, static_rl_table_store[0]); + init_rl(&rl_intra, static_rl_table_store[1]); + init_rl(&rvlc_rl_inter, static_rl_table_store[3]); + init_rl(&rvlc_rl_intra, static_rl_table_store[4]); + init_rl(&rl_intra_aic, static_rl_table_store[2]); init_vlc_rl(&rl_inter, 1); init_vlc_rl(&rl_intra, 1); init_vlc_rl(&rvlc_rl_inter, 1); @@ -3188,20 +3136,29 @@ void ff_mpeg4_encode_video_packet_header(MpegEncContext *s) * @return 0 if not */ static inline int mpeg4_is_resync(MpegEncContext *s){ - const int bits_count= get_bits_count(&s->gb); + int bits_count= get_bits_count(&s->gb); + int v= show_bits(&s->gb, 16); if(s->workaround_bugs&FF_BUG_NO_PADDING){ return 0; } + while(v<=0xFF){ + if(s->pict_type==B_TYPE || (v>>(8-s->pict_type)!=1) || s->partitioned_frame) + break; + skip_bits(&s->gb, 8+s->pict_type); + bits_count+= 8+s->pict_type; + v= show_bits(&s->gb, 16); + } + if(bits_count + 8 >= s->gb.size_in_bits){ - int v= show_bits(&s->gb, 8); + v>>=8; v|= 0x7F >> (7-(bits_count&7)); if(v==0x7F) return 1; }else{ - if(show_bits(&s->gb, 16) == ff_mpeg4_resync_prefix[bits_count&7]){ + if(v == ff_mpeg4_resync_prefix[bits_count&7]){ int len; GetBitContext gb= s->gb; @@ -3365,7 +3322,7 @@ int ff_h263_resync(MpegEncContext *s){ if(ret>=0) return 0; } - //ok, it's not where its supposed to be ... + //OK, it's not where it is supposed to be ... s->gb= s->last_resync_gb; align_get_bits(&s->gb); left= s->gb.size_in_bits - get_bits_count(&s->gb); @@ -4520,12 +4477,6 @@ end: /* per-MB end of slice check */ if(s->codec_id==CODEC_ID_MPEG4){ -#if 0 //http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_IEC_14496-4_2004_Conformance_Testing/video_conformance/version_1/simple/ERROR.ZIP/mit025.m4v needs this but its unclear if the mpeg4 standard allows this at all (MN) - if(s->pict_type != B_TYPE){ - while(show_bits(&s->gb, 9 + (s->pict_type == P_TYPE)) == 1) - skip_bits(&s->gb, 9 + (s->pict_type == P_TYPE)); - } -#endif if(mpeg4_is_resync(s)){ const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1; if(s->pict_type==B_TYPE && s->next_picture.mbskip_table[xy + delta]) @@ -4705,7 +4656,7 @@ retry: i += run; if (i >= 64){ if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){ - //looks like a hack but no, it's the way its supposed to work ... + //Looks like a hack but no, it's the way it is supposed to work ... rl = &rl_intra_aic; i = 0; s->gb= gb; @@ -5124,9 +5075,9 @@ int h263_decode_picture_header(MpegEncContext *s) if (ufep == 1) { /* OPPTYPE */ format = get_bits(&s->gb, 3); - dprintf("ufep=1, format: %d\n", format); + dprintf(s->avctx, "ufep=1, format: %d\n", format); s->custom_pcf= get_bits1(&s->gb); - s->umvplus = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */ + s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */ if (get_bits1(&s->gb) != 0) { av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n"); } @@ -5174,7 +5125,7 @@ int h263_decode_picture_header(MpegEncContext *s) if (format == 6) { /* Custom Picture Format (CPFMT) */ s->aspect_ratio_info = get_bits(&s->gb, 4); - dprintf("aspect: %d\n", s->aspect_ratio_info); + dprintf(s->avctx, "aspect: %d\n", s->aspect_ratio_info); /* aspect ratios: 0 - forbidden 1 - 1:1 @@ -5187,7 +5138,7 @@ int h263_decode_picture_header(MpegEncContext *s) width = (get_bits(&s->gb, 9) + 1) * 4; skip_bits1(&s->gb); height = get_bits(&s->gb, 9) * 4; - dprintf("\nH.263+ Custom picture: %dx%d\n",width,height); + dprintf(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height); if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) { /* aspected dimensions */ s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8); @@ -5278,20 +5229,7 @@ int h263_decode_picture_header(MpegEncContext *s) } if(s->avctx->debug&FF_DEBUG_PICT_INFO){ - av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n", - s->qscale, av_get_pict_type_char(s->pict_type), - s->gb.size_in_bits, 1-s->no_rounding, - s->obmc ? " AP" : "", - s->umvplus ? " UMV" : "", - s->h263_long_vectors ? " LONG" : "", - s->h263_plus ? " +" : "", - s->h263_aic ? " AIC" : "", - s->alt_inter_vlc ? " AIV" : "", - s->modified_quant ? " MQ" : "", - s->loop_filter ? " LOOP" : "", - s->h263_slice_structured ? " SS" : "", - s->avctx->time_base.den, s->avctx->time_base.num - ); + show_pict_info(s); } #if 1 if (s->pict_type == I_TYPE && s->codec_tag == ff_get_fourcc("ZYGO")){ @@ -5649,6 +5587,11 @@ static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){ skip_bits1(gb); /* marker */ } s->num_sprite_warping_points= get_bits(gb, 6); + if(s->num_sprite_warping_points > 3){ + av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points); + s->num_sprite_warping_points= 0; + return -1; + } s->sprite_warping_accuracy = get_bits(gb, 2); s->sprite_brightness_change= get_bits1(gb); if(s->vol_sprite_usage==STATIC_SPRITE) @@ -5911,6 +5854,10 @@ static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){ - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2; s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame) - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2; + if(!s->progressive_sequence){ + if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) + return FRAME_SKIPPED; + } } //av_log(s->avctx, AV_LOG_DEBUG, "last nonb %"PRId64" last_base %d time %"PRId64" pp %d pb %d t %d ppf %d pbf %d\n", s->last_non_b_time, s->last_time_base, s->time, s->pp_time, s->pb_time, s->t_frame, s->pp_field_time, s->pb_field_time); @@ -6177,11 +6124,7 @@ int intel_h263_decode_picture_header(MpegEncContext *s) av_log(s->avctx, AV_LOG_ERROR, "SAC not supported\n"); return -1; /* SAC: off */ } - if (get_bits1(&s->gb) != 0) { - s->obmc= 1; - av_log(s->avctx, AV_LOG_ERROR, "Advanced Prediction Mode not supported\n"); -// return -1; /* advanced prediction mode: off */ - } + s->obmc= get_bits1(&s->gb); if (get_bits1(&s->gb) != 0) { av_log(s->avctx, AV_LOG_ERROR, "PB frame mode no supported\n"); return -1; /* PB frame mode */ @@ -6202,6 +6145,9 @@ int intel_h263_decode_picture_header(MpegEncContext *s) s->y_dc_scale_table= s->c_dc_scale_table= ff_mpeg1_dc_scale_table; + if(s->avctx->debug&FF_DEBUG_PICT_INFO) + show_pict_info(s); + return 0; }