X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fdiracdec.c;h=b9999cde014cfe32c418f6eea5f35ae813abc764;hb=252500a78fe1a31abc79e6070d16f50382c39343;hp=9a417caec59a27673891991b3a62b26dd16c5cf0;hpb=5c1aafff2d844b177b0b20db4a38d8460992558b;p=ffmpeg diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c index 9a417caec59..b9999cde014 100644 --- a/libavcodec/diracdec.c +++ b/libavcodec/diracdec.c @@ -26,6 +26,7 @@ * @author Marco Gerards , David Conrad, Jordi Ortiz */ +#include "libavutil/mem_internal.h" #include "libavutil/pixdesc.h" #include "libavutil/thread.h" #include "avcodec.h" @@ -136,7 +137,6 @@ typedef struct DiracContext { MpegvideoEncDSPContext mpvencdsp; VideoDSPContext vdsp; DiracDSPContext diracdsp; - DiracGolombLUT *reader_ctx; DiracVersionInfo version; GetBitContext gb; AVDiracSeqHeader seq; @@ -395,7 +395,6 @@ static av_cold int dirac_decode_init(AVCodecContext *avctx) s->threads_num_buf = -1; s->thread_buf_size = -1; - ff_dirac_golomb_reader_init(&s->reader_ctx); ff_diracdsp_init(&s->diracdsp); ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx); ff_videodsp_init(&s->vdsp, 8); @@ -428,8 +427,6 @@ static av_cold int dirac_decode_end(AVCodecContext *avctx) DiracContext *s = avctx->priv_data; int i; - ff_dirac_golomb_reader_end(&s->reader_ctx); - dirac_decode_flush(avctx); for (i = 0; i < MAX_FRAMES; i++) av_frame_free(&s->all_frames[i].avframe); @@ -488,7 +485,7 @@ UNPACK_ARITH(10, int32_t) * Decode the coeffs in the rectangle defined by left, right, top, bottom * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock() */ -static inline void codeblock(DiracContext *s, SubBand *b, +static inline int codeblock(DiracContext *s, SubBand *b, GetBitContext *gb, DiracArith *c, int left, int right, int top, int bottom, int blockcnt_one, int is_arith) @@ -505,7 +502,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, zero_block = get_bits1(gb); if (zero_block) - return; + return 0; } if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) { @@ -516,7 +513,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, quant = dirac_get_se_golomb(gb); if (quant > INT_MAX - b->quant || b->quant + quant < 0) { av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n"); - return; + return AVERROR_INVALIDDATA; } b->quant += quant; } @@ -524,7 +521,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, if (b->quant > (DIRAC_MAX_QUANT_INDEX - 1)) { av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", b->quant); b->quant = 0; - return; + return AVERROR_INVALIDDATA; } qfactor = ff_dirac_qscale_tab[b->quant]; @@ -537,6 +534,8 @@ static inline void codeblock(DiracContext *s, SubBand *b, buf = b->ibuf + top * b->stride; if (is_arith) { for (y = top; y < bottom; y++) { + if (c->error) + return c->error; for (x = left; x < right; x++) { if (b->pshift) { coeff_unpack_arith_10(c, qfactor, qoffset, b, (int32_t*)(buf)+x, x, y); @@ -548,6 +547,8 @@ static inline void codeblock(DiracContext *s, SubBand *b, } } else { for (y = top; y < bottom; y++) { + if (get_bits_left(gb) < 1) + return AVERROR_INVALIDDATA; for (x = left; x < right; x++) { int val = coeff_unpack_golomb(gb, qfactor, qoffset); if (b->pshift) { @@ -559,6 +560,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, buf += b->stride; } } + return 0; } /** @@ -593,7 +595,7 @@ INTRA_DC_PRED(10, uint32_t) * Dirac Specification -> * 13.4.2 Non-skipped subbands. subband_coeffs() */ -static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith) +static av_always_inline int decode_subband_internal(DiracContext *s, SubBand *b, int is_arith) { int cb_x, cb_y, left, right, top, bottom; DiracArith c; @@ -601,9 +603,10 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width; int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height; int blockcnt_one = (cb_width + cb_height) == 2; + int ret; if (!b->length) - return; + return 0; init_get_bits8(&gb, b->coeff_data, b->length); @@ -616,7 +619,9 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b left = 0; for (cb_x = 0; cb_x < cb_width; cb_x++) { right = (b->width * (cb_x+1LL)) / cb_width; - codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith); + ret = codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith); + if (ret < 0) + return ret; left = right; } top = bottom; @@ -629,33 +634,35 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b intra_dc_prediction_8(b); } } + return 0; } static int decode_subband_arith(AVCodecContext *avctx, void *b) { DiracContext *s = avctx->priv_data; - decode_subband_internal(s, b, 1); - return 0; + return decode_subband_internal(s, b, 1); } static int decode_subband_golomb(AVCodecContext *avctx, void *arg) { DiracContext *s = avctx->priv_data; SubBand **b = arg; - decode_subband_internal(s, *b, 0); - return 0; + return decode_subband_internal(s, *b, 0); } /** * Dirac Specification -> * [DIRAC_STD] 13.4.1 core_transform_data() */ -static void decode_component(DiracContext *s, int comp) +static int decode_component(DiracContext *s, int comp) { AVCodecContext *avctx = s->avctx; SubBand *bands[3*MAX_DWT_LEVELS+1]; enum dirac_subband orientation; int level, num_bands = 0; + int ret[3*MAX_DWT_LEVELS+1]; + int i; + int damaged_count = 0; /* Unpack all subbands at all levels. */ for (level = 0; level < s->wavelet_depth; level++) { @@ -668,20 +675,37 @@ static void decode_component(DiracContext *s, int comp) b->length = get_interleaved_ue_golomb(&s->gb); if (b->length) { b->quant = get_interleaved_ue_golomb(&s->gb); + if (b->quant > (DIRAC_MAX_QUANT_INDEX - 1)) { + av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", b->quant); + b->quant = 0; + return AVERROR_INVALIDDATA; + } align_get_bits(&s->gb); b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8; - b->length = FFMIN(b->length, FFMAX(get_bits_left(&s->gb)/8, 0)); + if (b->length > FFMAX(get_bits_left(&s->gb)/8, 0)) { + b->length = FFMAX(get_bits_left(&s->gb)/8, 0); + damaged_count ++; + } skip_bits_long(&s->gb, b->length*8); } } /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */ if (s->is_arith) avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level], - NULL, 4-!!level, sizeof(SubBand)); + ret + 3*level + !!level, 4-!!level, sizeof(SubBand)); } /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */ if (!s->is_arith) - avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*)); + avctx->execute(avctx, decode_subband_golomb, bands, ret, num_bands, sizeof(SubBand*)); + + for (i = 0; i < s->wavelet_depth * 3 + 1; i++) { + if (ret[i] < 0) + damaged_count++; + } + if (damaged_count > (s->wavelet_depth * 3 + 1) /2) + return AVERROR_INVALIDDATA; + + return 0; } #define PARSE_VALUES(type, x, gb, ebits, buf1, buf2) \ @@ -854,11 +878,11 @@ static int decode_hq_slice(DiracContext *s, DiracSlice *slice, uint8_t *tmp_buf) coef_num = subband_coeffs(s, slice->slice_x, slice->slice_y, i, coeffs_num); if (s->pshift) - coef_par = ff_dirac_golomb_read_32bit(s->reader_ctx, addr, - length, tmp_buf, coef_num); + coef_par = ff_dirac_golomb_read_32bit(addr, length, + tmp_buf, coef_num); else - coef_par = ff_dirac_golomb_read_16bit(s->reader_ctx, addr, - length, tmp_buf, coef_num); + coef_par = ff_dirac_golomb_read_16bit(addr, length, + tmp_buf, coef_num); if (coef_num > coef_par) { const int start_b = coef_par * (1 << (s->pshift + 1)); @@ -1249,7 +1273,9 @@ static int dirac_unpack_idwt_params(DiracContext *s) s->num_y = get_interleaved_ue_golomb(gb); if (s->num_x * s->num_y == 0 || s->num_x * (uint64_t)s->num_y > INT_MAX || s->num_x * (uint64_t)s->avctx->width > INT_MAX || - s->num_y * (uint64_t)s->avctx->height > INT_MAX + s->num_y * (uint64_t)s->avctx->height > INT_MAX || + s->num_x > s->avctx->width || + s->num_y > s->avctx->height ) { av_log(s->avctx,AV_LOG_ERROR,"Invalid numx/y\n"); s->num_x = s->num_y = 0; @@ -1405,9 +1431,9 @@ static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref) int *b = s->globalmc[ref].pan_tilt; int *c = s->globalmc[ref].perspective; - int m = (1<u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep); block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep); @@ -1524,6 +1550,11 @@ static int dirac_unpack_block_motion_data(DiracContext *s) } } + for (i = 0; i < 4 + 2*s->num_refs; i++) { + if (arith[i].error) + return arith[i].error; + } + return 0; } @@ -1867,7 +1898,9 @@ static int dirac_decode_frame_internal(DiracContext *s) if (!s->zero_res && !s->low_delay) { memset(p->idwt.buf, 0, p->idwt.stride * p->idwt.height); - decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */ + ret = decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */ + if (ret < 0) + return ret; } ret = ff_spatial_idwt_init(&d, &p->idwt, s->wavelet_idx+2, s->wavelet_depth, s->bit_depth); @@ -2111,7 +2144,7 @@ static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int return ret; } - if (CALC_PADDING((int64_t)dsh->width, MAX_DWT_LEVELS) * CALC_PADDING((int64_t)dsh->height, MAX_DWT_LEVELS) > avctx->max_pixels) + if (CALC_PADDING((int64_t)dsh->width, MAX_DWT_LEVELS) * CALC_PADDING((int64_t)dsh->height, MAX_DWT_LEVELS) * 5LL > avctx->max_pixels) ret = AVERROR(ERANGE); if (ret >= 0) ret = ff_set_dimensions(avctx, dsh->width, dsh->height);