diff options
Diffstat (limited to 'libavcodec/lagarith.c')
-rw-r--r-- | libavcodec/lagarith.c | 279 |
1 files changed, 152 insertions, 127 deletions
diff --git a/libavcodec/lagarith.c b/libavcodec/lagarith.c index 95e6aba..59169be 100644 --- a/libavcodec/lagarith.c +++ b/libavcodec/lagarith.c @@ -2,20 +2,20 @@ * Lagarith lossless decoder * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -28,10 +28,10 @@ #include <inttypes.h> #include "avcodec.h" -#include "bitstream.h" +#include "get_bits.h" #include "mathops.h" -#include "huffyuvdsp.h" #include "lagarithrac.h" +#include "lossless_videodsp.h" #include "thread.h" enum LagarithFrameType { @@ -50,12 +50,9 @@ enum LagarithFrameType { typedef struct LagarithContext { AVCodecContext *avctx; - HuffYUVDSPContext hdsp; + LLVidDSPContext llviddsp; int zeros; /**< number of consecutive zero bytes encountered */ int zeros_rem; /**< number of zero bytes remaining to output */ - uint8_t *rgb_planes; - int rgb_planes_allocated; - int rgb_stride; } LagarithContext; /** @@ -91,17 +88,17 @@ static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa) uint64_t h = x * (mantissa >> 32); h += l >> 32; l &= 0xffffffff; - l += 1 << av_log2(h >> 21); + l += 1LL << av_log2(h >> 21); h += l >> 32; return h >> 20; } static uint8_t lag_calc_zero_run(int8_t x) { - return (x << 1) ^ (x >> 7); + return (x * 2) ^ (x >> 7); } -static int lag_decode_prob(BitstreamContext *bc, uint32_t *value) +static int lag_decode_prob(GetBitContext *gb, uint32_t *value) { static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 }; int i; @@ -114,7 +111,7 @@ static int lag_decode_prob(BitstreamContext *bc, uint32_t *value) if (prevbit && bit) break; prevbit = bit; - bit = bitstream_read_bit(bc); + bit = get_bits1(gb); if (bit && !prevbit) bits += series[i]; } @@ -127,26 +124,27 @@ static int lag_decode_prob(BitstreamContext *bc, uint32_t *value) return 0; } - val = bitstream_read(bc, bits); - val |= 1 << bits; + val = get_bits_long(gb, bits); + val |= 1U << bits; *value = val - 1; return 0; } -static int lag_read_prob_header(lag_rac *rac, BitstreamContext *bc) +static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb) { int i, j, scale_factor; unsigned prob, cumulative_target; unsigned cumul_prob = 0; unsigned scaled_cumul_prob = 0; + int nnz = 0; rac->prob[0] = 0; rac->prob[257] = UINT_MAX; /* Read probabilities from bitstream */ for (i = 1; i < 257; i++) { - if (lag_decode_prob(bc, &rac->prob[i]) < 0) { + if (lag_decode_prob(gb, &rac->prob[i]) < 0) { av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n"); return -1; } @@ -156,14 +154,16 @@ static int lag_read_prob_header(lag_rac *rac, BitstreamContext *bc) } cumul_prob += rac->prob[i]; if (!rac->prob[i]) { - if (lag_decode_prob(bc, &prob)) { + if (lag_decode_prob(gb, &prob)) { av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n"); return -1; } - if (prob > 257 - i) - prob = 257 - i; + if (prob > 256 - i) + prob = 256 - i; for (j = 0; j < prob; j++) rac->prob[++i] = 0; + }else { + nnz++; } } @@ -172,18 +172,32 @@ static int lag_read_prob_header(lag_rac *rac, BitstreamContext *bc) return -1; } + if (nnz == 1 && (show_bits_long(gb, 32) & 0xFFFFFF)) { + return AVERROR_INVALIDDATA; + } + /* Scale probabilities so cumulative probability is an even power of 2. */ scale_factor = av_log2(cumul_prob); if (cumul_prob & (cumul_prob - 1)) { uint64_t mul = softfloat_reciprocal(cumul_prob); - for (i = 1; i < 257; i++) { + for (i = 1; i <= 128; i++) { + rac->prob[i] = softfloat_mul(rac->prob[i], mul); + scaled_cumul_prob += rac->prob[i]; + } + if (scaled_cumul_prob <= 0) { + av_log(rac->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n"); + return AVERROR_INVALIDDATA; + } + for (; i < 257; i++) { rac->prob[i] = softfloat_mul(rac->prob[i], mul); scaled_cumul_prob += rac->prob[i]; } scale_factor++; - cumulative_target = 1 << scale_factor; + if (scale_factor >= 32U) + return AVERROR_INVALIDDATA; + cumulative_target = 1U << scale_factor; if (scaled_cumul_prob > cumulative_target) { av_log(rac->avctx, AV_LOG_ERROR, @@ -251,11 +265,8 @@ static void lag_pred_line(LagarithContext *l, uint8_t *buf, int L, TL; if (!line) { - int i, align_width = (width - 1) & ~31; /* Left prediction only for first line */ - L = l->hdsp.add_hfyu_left_pred(buf + 1, buf + 1, align_width, buf[0]); - for (i = align_width + 1; i < width; i++) - buf[i] += buf[i - 1]; + L = l->llviddsp.add_left_pred(buf, buf, width, 0); } else { /* Left pixel is actually prev_row[width] */ L = buf[width - stride - 1]; @@ -281,18 +292,12 @@ static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf, int L, TL; if (!line) { - int i, align_width; - if (is_luma) { - buf++; - width--; - } - - align_width = (width - 1) & ~31; - l->hdsp.add_hfyu_left_pred(buf + 1, buf + 1, align_width, buf[0]); - - for (i = align_width + 1; i < width; i++) - buf[i] += buf[i - 1]; - + L= buf[0]; + if (is_luma) + buf[0] = 0; + l->llviddsp.add_left_pred(buf, buf, width, 0); + if (is_luma) + buf[0] = L; return; } if (line == 1) { @@ -313,7 +318,7 @@ static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf, } else { TL = buf[width - (2 * stride) - 1]; L = buf[width - stride - 1]; - l->hdsp.add_hfyu_median_pred(buf, buf - stride, buf, width, &L, &TL); + l->llviddsp.add_median_pred(buf, buf - stride, buf, width, &L, &TL); } } @@ -371,6 +376,10 @@ static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst, uint8_t mask2 = -(esc_count < 3); uint8_t *end = dst + (width - 2); + avpriv_request_sample(l->avctx, "zero_run_line"); + + memset(dst, 0, width); + output_zeros: if (l->zeros_rem) { count = FFMIN(l->zeros_rem, width - i); @@ -388,7 +397,7 @@ output_zeros: i = 0; while (!zero_run && dst + i < end) { i++; - if (src + i >= src_end) + if (i+2 >= src_end - src) return AVERROR_INVALIDDATA; zero_run = !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2)); @@ -408,7 +417,7 @@ output_zeros: dst += i; } } - return src_start - src; + return src - src_start; } @@ -421,31 +430,41 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst, int read = 0; uint32_t length; uint32_t offset = 1; - int esc_count = src[0]; - BitstreamContext bc; + int esc_count; + GetBitContext gb; lag_rac rac; const uint8_t *src_end = src + src_size; + int ret; rac.avctx = l->avctx; l->zeros = 0; + if(src_size < 2) + return AVERROR_INVALIDDATA; + + esc_count = src[0]; if (esc_count < 4) { length = width * height; + if(src_size < 5) + return AVERROR_INVALIDDATA; if (esc_count && AV_RL32(src + 1) < length) { length = AV_RL32(src + 1); offset += 4; } - bitstream_init8(&bc, src + offset, src_size); + if ((ret = init_get_bits8(&gb, src + offset, src_size - offset)) < 0) + return ret; - if (lag_read_prob_header(&rac, &bc) < 0) + if (lag_read_prob_header(&rac, &gb) < 0) return -1; - ff_lag_rac_init(&rac, &bc, length - stride); - - for (i = 0; i < height; i++) + ff_lag_rac_init(&rac, &gb, length - stride); + for (i = 0; i < height; i++) { + if (rac.overread > MAX_OVERREAD) + return AVERROR_INVALIDDATA; read += lag_decode_line(l, &rac, dst + (i * stride), width, stride, esc_count); + } if (read > length) av_log(l->avctx, AV_LOG_WARNING, @@ -453,6 +472,8 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst, length); } else if (esc_count < 8) { esc_count -= 4; + src ++; + src_size --; if (esc_count > 0) { /* Zero run coding only, no range coding. */ for (i = 0; i < height; i++) { @@ -513,17 +534,19 @@ static int lag_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; + unsigned int buf_size = avpkt->size; LagarithContext *l = avctx->priv_data; ThreadFrame frame = { .f = data }; AVFrame *const p = data; - uint8_t frametype = 0; + uint8_t frametype; uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9; uint32_t offs[4]; - uint8_t *srcs[4], *dst; + uint8_t *srcs[4]; int i, j, planes = 3; + int ret; p->key_frame = 1; + p->pict_type = AV_PICTURE_TYPE_I; frametype = buf[0]; @@ -532,93 +555,96 @@ static int lag_decode_frame(AVCodecContext *avctx, switch (frametype) { case FRAME_SOLID_RGBA: - avctx->pix_fmt = AV_PIX_FMT_RGB32; + avctx->pix_fmt = AV_PIX_FMT_GBRAP; + case FRAME_SOLID_GRAY: + if (frametype == FRAME_SOLID_GRAY) + if (avctx->bits_per_coded_sample == 24) { + avctx->pix_fmt = AV_PIX_FMT_GBRP; + } else { + avctx->pix_fmt = AV_PIX_FMT_GBRAP; + planes = 4; + } - if (ff_thread_get_buffer(avctx, &frame, 0) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; + + if (frametype == FRAME_SOLID_RGBA) { + for (i = 0; i < avctx->height; i++) { + memset(p->data[0] + i * p->linesize[0], buf[2], avctx->width); + memset(p->data[1] + i * p->linesize[1], buf[1], avctx->width); + memset(p->data[2] + i * p->linesize[2], buf[3], avctx->width); + memset(p->data[3] + i * p->linesize[3], buf[4], avctx->width); + } + } else { + for (i = 0; i < avctx->height; i++) { + for (j = 0; j < planes; j++) + memset(p->data[j] + i * p->linesize[j], buf[1], avctx->width); + } + } + break; + case FRAME_SOLID_COLOR: + if (avctx->bits_per_coded_sample == 24) { + avctx->pix_fmt = AV_PIX_FMT_GBRP; + } else { + avctx->pix_fmt = AV_PIX_FMT_GBRAP; } - dst = p->data[0]; - for (j = 0; j < avctx->height; j++) { - for (i = 0; i < avctx->width; i++) - AV_WN32(dst + i * 4, offset_gu); - dst += p->linesize[0]; + if ((ret = ff_thread_get_buffer(avctx, &frame,0)) < 0) + return ret; + + for (i = 0; i < avctx->height; i++) { + memset(p->data[0] + i * p->linesize[0], buf[2], avctx->width); + memset(p->data[1] + i * p->linesize[1], buf[1], avctx->width); + memset(p->data[2] + i * p->linesize[2], buf[3], avctx->width); + if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) + memset(p->data[3] + i * p->linesize[3], 0xFFu, avctx->width); } break; case FRAME_ARITH_RGBA: - avctx->pix_fmt = AV_PIX_FMT_RGB32; + avctx->pix_fmt = AV_PIX_FMT_GBRAP; planes = 4; offset_ry += 4; offs[3] = AV_RL32(buf + 9); case FRAME_ARITH_RGB24: case FRAME_U_RGB24: if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24) - avctx->pix_fmt = AV_PIX_FMT_RGB24; + avctx->pix_fmt = AV_PIX_FMT_GBRP; - if (ff_thread_get_buffer(avctx, &frame, 0) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; - } + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; offs[0] = offset_bv; offs[1] = offset_gu; offs[2] = offset_ry; - l->rgb_stride = FFALIGN(avctx->width, 16); - av_fast_malloc(&l->rgb_planes, &l->rgb_planes_allocated, - l->rgb_stride * avctx->height * planes + 1); - if (!l->rgb_planes) { - av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n"); - return AVERROR(ENOMEM); - } for (i = 0; i < planes; i++) - srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride; - if (offset_ry >= buf_size || - offset_gu >= buf_size || - offset_bv >= buf_size || - (planes == 4 && offs[3] >= buf_size)) { - av_log(avctx, AV_LOG_ERROR, - "Invalid frame offsets\n"); - return AVERROR_INVALIDDATA; - } + srcs[i] = p->data[i] + (avctx->height - 1) * p->linesize[i]; + for (i = 0; i < planes; i++) + if (buf_size <= offs[i]) { + av_log(avctx, AV_LOG_ERROR, + "Invalid frame offsets\n"); + return AVERROR_INVALIDDATA; + } + for (i = 0; i < planes; i++) lag_decode_arith_plane(l, srcs[i], avctx->width, avctx->height, - -l->rgb_stride, buf + offs[i], + -p->linesize[i], buf + offs[i], buf_size - offs[i]); - dst = p->data[0]; - for (i = 0; i < planes; i++) - srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height; - for (j = 0; j < avctx->height; j++) { - for (i = 0; i < avctx->width; i++) { - uint8_t r, g, b, a; - r = srcs[0][i]; - g = srcs[1][i]; - b = srcs[2][i]; - r += g; - b += g; - if (frametype == FRAME_ARITH_RGBA) { - a = srcs[3][i]; - AV_WN32(dst + i * 4, MKBETAG(a, r, g, b)); - } else { - dst[i * 3 + 0] = r; - dst[i * 3 + 1] = g; - dst[i * 3 + 2] = b; - } - } - dst += p->linesize[0]; - for (i = 0; i < planes; i++) - srcs[i] += l->rgb_stride; + for (i = 0; i < avctx->height; i++) { + l->llviddsp.add_bytes(p->data[0] + i * p->linesize[0], p->data[1] + i * p->linesize[1], avctx->width); + l->llviddsp.add_bytes(p->data[2] + i * p->linesize[2], p->data[1] + i * p->linesize[1], avctx->width); } + FFSWAP(uint8_t*, p->data[0], p->data[1]); + FFSWAP(int, p->linesize[0], p->linesize[1]); + FFSWAP(uint8_t*, p->data[2], p->data[1]); + FFSWAP(int, p->linesize[2], p->linesize[1]); break; case FRAME_ARITH_YUY2: avctx->pix_fmt = AV_PIX_FMT_YUV422P; - if (ff_thread_get_buffer(avctx, &frame, 0) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; - } + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; if (offset_ry >= buf_size || offset_gu >= buf_size || @@ -631,20 +657,18 @@ static int lag_decode_frame(AVCodecContext *avctx, lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height, p->linesize[0], buf + offset_ry, buf_size - offset_ry); - lag_decode_arith_plane(l, p->data[1], avctx->width / 2, + lag_decode_arith_plane(l, p->data[1], (avctx->width + 1) / 2, avctx->height, p->linesize[1], buf + offset_gu, buf_size - offset_gu); - lag_decode_arith_plane(l, p->data[2], avctx->width / 2, + lag_decode_arith_plane(l, p->data[2], (avctx->width + 1) / 2, avctx->height, p->linesize[2], buf + offset_bv, buf_size - offset_bv); break; case FRAME_ARITH_YV12: avctx->pix_fmt = AV_PIX_FMT_YUV420P; - if (ff_thread_get_buffer(avctx, &frame, 0) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; - } + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; if (offset_ry >= buf_size || offset_gu >= buf_size || @@ -657,17 +681,17 @@ static int lag_decode_frame(AVCodecContext *avctx, lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height, p->linesize[0], buf + offset_ry, buf_size - offset_ry); - lag_decode_arith_plane(l, p->data[2], avctx->width / 2, - avctx->height / 2, p->linesize[2], + lag_decode_arith_plane(l, p->data[2], (avctx->width + 1) / 2, + (avctx->height + 1) / 2, p->linesize[2], buf + offset_gu, buf_size - offset_gu); - lag_decode_arith_plane(l, p->data[1], avctx->width / 2, - avctx->height / 2, p->linesize[1], + lag_decode_arith_plane(l, p->data[1], (avctx->width + 1) / 2, + (avctx->height + 1) / 2, p->linesize[1], buf + offset_bv, buf_size - offset_bv); break; default: av_log(avctx, AV_LOG_ERROR, "Unsupported Lagarith frame type: %#"PRIx8"\n", frametype); - return -1; + return AVERROR_PATCHWELCOME; } *got_frame = 1; @@ -680,19 +704,20 @@ static av_cold int lag_decode_init(AVCodecContext *avctx) LagarithContext *l = avctx->priv_data; l->avctx = avctx; - ff_huffyuvdsp_init(&l->hdsp); + ff_llviddsp_init(&l->llviddsp); return 0; } -static av_cold int lag_decode_end(AVCodecContext *avctx) +#if HAVE_THREADS +static av_cold int lag_decode_init_thread_copy(AVCodecContext *avctx) { LagarithContext *l = avctx->priv_data; - - av_freep(&l->rgb_planes); + l->avctx = avctx; return 0; } +#endif AVCodec ff_lagarith_decoder = { .name = "lagarith", @@ -701,7 +726,7 @@ AVCodec ff_lagarith_decoder = { .id = AV_CODEC_ID_LAGARITH, .priv_data_size = sizeof(LagarithContext), .init = lag_decode_init, - .close = lag_decode_end, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(lag_decode_init_thread_copy), .decode = lag_decode_frame, .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, }; |