X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Frv10.c;h=29e41dc9b95c3532ba218eb90c34dd147873c3d5;hb=c242bbd8b6939507a1a6fb64101b0553d92d303f;hp=9a1fda113787fec29af993b691e8a2cda7c512e4;hpb=ec6402b7c595c3ceed6d1b8c1b75c6aa8336e052;p=ffmpeg diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c index 9a1fda11378..29e41dc9b95 100644 --- a/libavcodec/rv10.c +++ b/libavcodec/rv10.c @@ -27,15 +27,23 @@ #include "libavutil/imgutils.h" #include "avcodec.h" -#include "dsputil.h" #include "mpegvideo.h" #include "mpeg4video.h" #include "h263.h" //#define DEBUG +#define RV_GET_MAJOR_VER(x) ((x) >> 28) +#define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF) +#define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF) + #define DC_VLC_BITS 14 //FIXME find a better solution +typedef struct RVDecContext { + MpegEncContext m; + int sub_id; +} RVDecContext; + static const uint16_t rv_lum_code[256] = { 0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06, @@ -182,7 +190,7 @@ static const uint8_t rv_chrom_bits[256] = static VLC rv_dc_lum, rv_dc_chrom; -int rv_decode_dc(MpegEncContext *s, int n) +int ff_rv_decode_dc(MpegEncContext *s, int n) { int code; @@ -289,16 +297,11 @@ static int rv10_decode_picture_header(MpegEncContext *s) return mb_count; } -static int rv20_decode_picture_header(MpegEncContext *s) +static int rv20_decode_picture_header(RVDecContext *rv) { + MpegEncContext *s = &rv->m; int seq, mb_pos, i; - - if(s->avctx->sub_id == 0x30202002 || s->avctx->sub_id == 0x30203002){ - if (get_bits(&s->gb, 3)){ - av_log(s->avctx, AV_LOG_ERROR, "unknown triplet set\n"); - return -1; - } - } + int rpr_bits; i= get_bits(&s->gb, 2); switch(i){ @@ -317,7 +320,7 @@ static int rv20_decode_picture_header(MpegEncContext *s) } if (get_bits1(&s->gb)){ - av_log(s->avctx, AV_LOG_ERROR, "unknown bit set\n"); + av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n"); return -1; } @@ -326,23 +329,21 @@ static int rv20_decode_picture_header(MpegEncContext *s) av_log(s->avctx, AV_LOG_ERROR, "error, qscale:0\n"); return -1; } - if(s->avctx->sub_id == 0x30203002){ - if (get_bits1(&s->gb)){ - av_log(s->avctx, AV_LOG_ERROR, "unknown bit2 set\n"); - return -1; - } - } - if(s->avctx->has_b_frames){ - int f, new_w, new_h; - int v= s->avctx->extradata_size >= 4 ? 7&((uint8_t*)s->avctx->extradata)[1] : 0; + if(RV_GET_MINOR_VER(rv->sub_id) >= 2) + s->loop_filter = get_bits1(&s->gb); - if (get_bits1(&s->gb)){ - av_log(s->avctx, AV_LOG_ERROR, "unknown bit3 set\n"); - } - seq= get_bits(&s->gb, 13)<<2; + if(RV_GET_MINOR_VER(rv->sub_id) <= 1) + seq = get_bits(&s->gb, 8) << 7; + else + seq = get_bits(&s->gb, 13) << 2; + + rpr_bits = s->avctx->extradata[1] & 7; + if(rpr_bits){ + int f, new_w, new_h; + rpr_bits = FFMIN((rpr_bits >> 1) + 1, 3); - f= get_bits(&s->gb, av_log2(v)+1); + f = get_bits(&s->gb, rpr_bits); if(f){ new_w= 4*((uint8_t*)s->avctx->extradata)[6+2*f]; @@ -355,29 +356,22 @@ static int rv20_decode_picture_header(MpegEncContext *s) av_log(s->avctx, AV_LOG_DEBUG, "attempting to change resolution to %dx%d\n", new_w, new_h); if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0) return -1; - MPV_common_end(s); + ff_MPV_common_end(s); avcodec_set_dimensions(s->avctx, new_w, new_h); s->width = new_w; s->height = new_h; - if (MPV_common_init(s) < 0) + if (ff_MPV_common_init(s) < 0) return -1; } if(s->avctx->debug & FF_DEBUG_PICT_INFO){ - av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d\n", f, v); + av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d\n", f, rpr_bits); } - }else{ - seq= get_bits(&s->gb, 8)*128; - } + } else if (av_image_check_size(s->width, s->height, 0, s->avctx) < 0) + return AVERROR_INVALIDDATA; + + mb_pos = ff_h263_decode_mba(s); -// if(s->avctx->sub_id <= 0x20201002){ //0x20201002 definitely needs this - mb_pos= ff_h263_decode_mba(s); -/* }else{ - mb_pos= get_bits(&s->gb, av_log2(s->mb_num-1)+1); - s->mb_x= mb_pos % s->mb_width; - s->mb_y= mb_pos / s->mb_width; - }*/ -//av_log(s->avctx, AV_LOG_DEBUG, "%d\n", seq); seq |= s->time &~0x7FFF; if(seq - s->time > 0x4000) seq -= 0x8000; if(seq - s->time < -0x4000) seq += 0x8000; @@ -396,13 +390,12 @@ static int rv20_decode_picture_header(MpegEncContext *s) ff_mpeg4_init_direct_mv(s); } } -// printf("%d %d %d %d %d\n", seq, (int)s->time, (int)s->last_non_b_time, s->pp_time, s->pb_time); -/*for(i=0; i<32; i++){ - av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb)); -} -av_log(s->avctx, AV_LOG_DEBUG, "\n");*/ + s->no_rounding= get_bits1(&s->gb); + if(RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B) + skip_bits(&s->gb, 5); // binary decoder reads 3+2 bits here but they don't seem to be used + s->f_code = 1; s->unrestricted_mv = 1; s->h263_aic= s->pict_type == AV_PICTURE_TYPE_I; @@ -410,8 +403,7 @@ av_log(s->avctx, AV_LOG_DEBUG, "\n");*/ // s->obmc=1; // s->umvplus=1; s->modified_quant=1; - if(!s->avctx->lowres) - s->loop_filter=1; + s->loop_filter=1; if(s->avctx->debug & FF_DEBUG_PICT_INFO){ av_log(s->avctx, AV_LOG_INFO, "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n", @@ -425,63 +417,61 @@ av_log(s->avctx, AV_LOG_DEBUG, "\n");*/ static av_cold int rv10_decode_init(AVCodecContext *avctx) { - MpegEncContext *s = avctx->priv_data; + RVDecContext *rv = avctx->priv_data; + MpegEncContext *s = &rv->m; static int done=0; + int major_ver, minor_ver, micro_ver; if (avctx->extradata_size < 8) { av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n"); return -1; } - MPV_decode_defaults(s); + ff_MPV_decode_defaults(s); s->avctx= avctx; s->out_format = FMT_H263; s->codec_id= avctx->codec_id; + avctx->flags |= CODEC_FLAG_EMU_EDGE; s->orig_width = s->width = avctx->coded_width; s->orig_height= s->height = avctx->coded_height; s->h263_long_vectors= ((uint8_t*)avctx->extradata)[3] & 1; - avctx->sub_id= AV_RB32((uint8_t*)avctx->extradata + 4); - - if (avctx->sub_id == 0x10000000) { - s->rv10_version= 0; - s->low_delay=1; - } else if (avctx->sub_id == 0x10001000) { - s->rv10_version= 3; - s->low_delay=1; - } else if (avctx->sub_id == 0x10002000) { - s->rv10_version= 3; - s->low_delay=1; - s->obmc=1; - } else if (avctx->sub_id == 0x10003000) { - s->rv10_version= 3; - s->low_delay=1; - } else if (avctx->sub_id == 0x10003001) { - s->rv10_version= 3; - s->low_delay=1; - } else if ( avctx->sub_id == 0x20001000 - || (avctx->sub_id >= 0x20100000 && avctx->sub_id < 0x201a0000)) { - s->low_delay=1; - } else if ( avctx->sub_id == 0x30202002 - || avctx->sub_id == 0x30203002 - || (avctx->sub_id >= 0x20200002 && avctx->sub_id < 0x20300000)) { - s->low_delay=0; - s->avctx->has_b_frames=1; - } else - av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", avctx->sub_id); + rv->sub_id = AV_RB32((uint8_t*)avctx->extradata + 4); + + major_ver = RV_GET_MAJOR_VER(rv->sub_id); + minor_ver = RV_GET_MINOR_VER(rv->sub_id); + micro_ver = RV_GET_MICRO_VER(rv->sub_id); + + s->low_delay = 1; + switch (major_ver) { + case 1: + s->rv10_version = micro_ver ? 3 : 1; + s->obmc = micro_ver == 2; + break; + case 2: + if (minor_ver >= 2) { + s->low_delay = 0; + s->avctx->has_b_frames = 1; + } + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id); + av_log_missing_feature(avctx, "RV1/2 version", 1); + return AVERROR_PATCHWELCOME; + } if(avctx->debug & FF_DEBUG_PICT_INFO){ - av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", avctx->sub_id, avctx->extradata_size >= 4 ? ((uint32_t*)avctx->extradata)[0] : -1); + av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", rv->sub_id, avctx->extradata_size >= 4 ? ((uint32_t*)avctx->extradata)[0] : -1); } - avctx->pix_fmt = PIX_FMT_YUV420P; + avctx->pix_fmt = AV_PIX_FMT_YUV420P; - if (MPV_common_init(s) < 0) + if (ff_MPV_common_init(s) < 0) return -1; - h263_decode_init_vlc(s); + ff_h263_decode_init_vlc(); /* init rv vlc */ if (!done) { @@ -501,21 +491,23 @@ static av_cold int rv10_decode_end(AVCodecContext *avctx) { MpegEncContext *s = avctx->priv_data; - MPV_common_end(s); + ff_MPV_common_end(s); return 0; } static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int buf_size2) { - MpegEncContext *s = avctx->priv_data; - int mb_count, mb_pos, left, start_mb_x; + RVDecContext *rv = avctx->priv_data; + MpegEncContext *s = &rv->m; + int mb_count, mb_pos, left, start_mb_x, active_bits_size; - init_get_bits(&s->gb, buf, buf_size*8); - if(s->codec_id ==CODEC_ID_RV10) + active_bits_size = buf_size * 8; + init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8); + if(s->codec_id ==AV_CODEC_ID_RV10) mb_count = rv10_decode_picture_header(s); else - mb_count = rv20_decode_picture_header(s); + mb_count = rv20_decode_picture_header(rv); if (mb_count < 0) { av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n"); return -1; @@ -535,19 +527,24 @@ static int rv10_decode_packet(AVCodecContext *avctx, if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) { if(s->current_picture_ptr){ //FIXME write parser so we always have complete frames? - ff_er_frame_end(s); - MPV_frame_end(s); + ff_er_frame_end(&s->er); + ff_MPV_frame_end(s); s->mb_x= s->mb_y = s->resync_mb_x = s->resync_mb_y= 0; } - if(MPV_frame_start(s, avctx) < 0) + if(ff_MPV_frame_start(s, avctx) < 0) + return -1; + ff_mpeg_er_frame_start(s); + } else { + if (s->current_picture_ptr->f.pict_type != s->pict_type) { + av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n"); return -1; - ff_er_frame_start(s); + } } av_dlog(avctx, "qscale=%d\n", s->qscale); /* default quantization values */ - if(s->codec_id== CODEC_ID_RV10){ + if(s->codec_id== AV_CODEC_ID_RV10){ if(s->mb_y==0) s->first_slice_line=1; }else{ s->first_slice_line=1; @@ -589,19 +586,32 @@ static int rv10_decode_packet(AVCodecContext *avctx, s->mv_type = MV_TYPE_16X16; ret=ff_h263_decode_mb(s, s->block); - if (ret != SLICE_ERROR && s->gb.size_in_bits < get_bits_count(&s->gb) && 8*buf_size2 >= get_bits_count(&s->gb)){ - av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n", s->gb.size_in_bits, 8*buf_size2); - s->gb.size_in_bits= 8*buf_size2; + // Repeat the slice end check from ff_h263_decode_mb with our active + // bitstream size + if (ret != SLICE_ERROR) { + int v = show_bits(&s->gb, 16); + + if (get_bits_count(&s->gb) + 16 > active_bits_size) + v >>= get_bits_count(&s->gb) + 16 - active_bits_size; + + if (!v) + ret = SLICE_END; + } + if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&s->gb) && + 8 * buf_size2 >= get_bits_count(&s->gb)) { + active_bits_size = buf_size2 * 8; + av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n", + 8 * buf_size, active_bits_size); ret= SLICE_OK; } - if (ret == SLICE_ERROR || s->gb.size_in_bits < get_bits_count(&s->gb)) { + if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) { av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x, s->mb_y); return -1; } if(s->pict_type != AV_PICTURE_TYPE_B) ff_h263_update_motion_val(s); - MPV_decode_mb(s, s->block); + ff_MPV_decode_mb(s, s->block); if(s->loop_filter) ff_h263_loop_filter(s); @@ -615,9 +625,9 @@ static int rv10_decode_packet(AVCodecContext *avctx, if(ret == SLICE_END) break; } - ff_er_add_slice(s, start_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); + ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END); - return s->gb.size_in_bits; + return active_bits_size; } static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n) @@ -627,7 +637,7 @@ static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n) } static int rv10_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, + void *data, int *got_frame, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; @@ -647,15 +657,26 @@ static int rv10_decode_frame(AVCodecContext *avctx, if(!avctx->slice_count){ slice_count = (*buf++) + 1; + buf_size--; + + if (!slice_count || buf_size <= 8 * slice_count) { + av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n", slice_count); + return AVERROR_INVALIDDATA; + } + slices_hdr = buf + 4; buf += 8 * slice_count; + buf_size -= 8 * slice_count; }else slice_count = avctx->slice_count; for(i=0; i= buf_size) + return AVERROR_INVALIDDATA; + if(i+1 == slice_count) size= buf_size - offset; else @@ -666,55 +687,57 @@ static int rv10_decode_frame(AVCodecContext *avctx, else size2= get_slice_offset(avctx, slices_hdr, i+2) - offset; + if (size <= 0 || size2 <= 0 || + offset + FFMAX(size, size2) > buf_size) + return AVERROR_INVALIDDATA; + if(rv10_decode_packet(avctx, buf+offset, size, size2) > 8*size) i++; } if(s->current_picture_ptr != NULL && s->mb_y>=s->mb_height){ - ff_er_frame_end(s); - MPV_frame_end(s); + ff_er_frame_end(&s->er); + ff_MPV_frame_end(s); if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { - *pict= *(AVFrame*)s->current_picture_ptr; + *pict = s->current_picture_ptr->f; } else if (s->last_picture_ptr != NULL) { - *pict= *(AVFrame*)s->last_picture_ptr; + *pict = s->last_picture_ptr->f; } if(s->last_picture_ptr || s->low_delay){ - *data_size = sizeof(AVFrame); + *got_frame = 1; ff_print_debug_info(s, pict); } - s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...) + s->current_picture_ptr= NULL; // so we can detect if frame_end was not called (find some nicer solution...) } - return buf_size; + return avpkt->size; } AVCodec ff_rv10_decoder = { .name = "rv10", .type = AVMEDIA_TYPE_VIDEO, - .id = CODEC_ID_RV10, - .priv_data_size = sizeof(MpegEncContext), + .id = AV_CODEC_ID_RV10, + .priv_data_size = sizeof(RVDecContext), .init = rv10_decode_init, .close = rv10_decode_end, .decode = rv10_decode_frame, .capabilities = CODEC_CAP_DR1, - .max_lowres = 3, - .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"), - .pix_fmts= ff_pixfmt_list_420, + .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"), + .pix_fmts = ff_pixfmt_list_420, }; AVCodec ff_rv20_decoder = { .name = "rv20", .type = AVMEDIA_TYPE_VIDEO, - .id = CODEC_ID_RV20, - .priv_data_size = sizeof(MpegEncContext), + .id = AV_CODEC_ID_RV20, + .priv_data_size = sizeof(RVDecContext), .init = rv10_decode_init, .close = rv10_decode_end, .decode = rv10_decode_frame, .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY, - .flush= ff_mpeg_flush, - .max_lowres = 3, - .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"), - .pix_fmts= ff_pixfmt_list_420, + .flush = ff_mpeg_flush, + .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"), + .pix_fmts = ff_pixfmt_list_420, };