X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fdiracdec.c;h=fd2313906219c3852b115530e37913026da55c13;hb=e0686318ddd7a5138d1847d97fe967fb2e813802;hp=753adeff61ce8362c31159d7cbb9714931e5663c;hpb=4339c94364f8ff143d051fcace3e5801625db607;p=ffmpeg diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c index 753adeff61c..fd231390621 100644 --- a/libavcodec/diracdec.c +++ b/libavcodec/diracdec.c @@ -141,7 +141,7 @@ typedef struct DiracContext { GetBitContext gb; AVDiracSeqHeader seq; int seen_sequence_header; - int frame_number; /* number of the next frame to display */ + int64_t frame_number; /* number of the next frame to display */ Plane plane[3]; int chroma_x_shift; int chroma_y_shift; @@ -488,7 +488,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 +505,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 +516,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 +524,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]; @@ -548,6 +548,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 +561,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, buf += b->stride; } } + return 0; } /** @@ -593,7 +596,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 +604,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 +620,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 +635,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,6 +676,11 @@ 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)); @@ -677,11 +690,20 @@ static void decode_component(DiracContext *s, int comp) /* 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) \ @@ -986,6 +1008,10 @@ static int decode_lowdelay(DiracContext *s) for (slice_x = 0; bufsize > 0 && slice_x < s->num_x; slice_x++) { bytes = (slice_num+1) * (int64_t)s->lowdelay.bytes.num / s->lowdelay.bytes.den - slice_num * (int64_t)s->lowdelay.bytes.num / s->lowdelay.bytes.den; + if (bytes >= INT_MAX || bytes*8 > bufsize) { + av_log(s->avctx, AV_LOG_ERROR, "too many bytes\n"); + return AVERROR_INVALIDDATA; + } slices[slice_num].bytes = bytes; slices[slice_num].slice_x = slice_x; slices[slice_num].slice_y = slice_y; @@ -1243,7 +1269,10 @@ static int dirac_unpack_idwt_params(DiracContext *s) else { s->num_x = get_interleaved_ue_golomb(gb); 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) { + 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 + ) { av_log(s->avctx,AV_LOG_ERROR,"Invalid numx/y\n"); s->num_x = s->num_y = 0; return AVERROR_INVALIDDATA; @@ -1399,8 +1428,8 @@ static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref) 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); @@ -1860,7 +1889,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); @@ -2310,7 +2341,7 @@ static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, } if (*got_frame) - s->frame_number = picture->display_picture_number + 1; + s->frame_number = picture->display_picture_number + 1LL; return buf_idx; }