summaryrefslogtreecommitdiff
path: root/libavcodec/apedec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/apedec.c')
-rw-r--r--libavcodec/apedec.c172
1 files changed, 75 insertions, 97 deletions
diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index da45e85e51..15eb416ba4 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -3,20 +3,20 @@
* Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
* based upon libdemac from Dave Chapman.
*
- * 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
*/
@@ -25,13 +25,12 @@
#include "libavutil/avassert.h"
#include "libavutil/channel_layout.h"
#include "libavutil/opt.h"
-
-#include "apedsp.h"
+#include "lossless_audiodsp.h"
#include "avcodec.h"
-#include "bitstream.h"
#include "bswapdsp.h"
#include "bytestream.h"
#include "internal.h"
+#include "get_bits.h"
#include "unary.h"
/**
@@ -138,7 +137,7 @@ typedef struct APEContext {
AVClass *class; ///< class for AVOptions
AVCodecContext *avctx;
BswapDSPContext bdsp;
- APEDSPContext adsp;
+ LLAudDSPContext adsp;
int channels;
int samples; ///< samples left to decode in current frame
int bps;
@@ -163,7 +162,7 @@ typedef struct APEContext {
APERice riceX; ///< rice code parameters for the second channel
APERice riceY; ///< rice code parameters for the first channel
APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
- BitstreamContext bc;
+ GetBitContext gb;
uint8_t *data; ///< current frame data
uint8_t *data_end; ///< frame data end
@@ -213,19 +212,6 @@ static av_cold int ape_decode_close(AVCodecContext *avctx)
return 0;
}
-static int32_t scalarproduct_and_madd_int16_c(int16_t *v1, const int16_t *v2,
- const int16_t *v3,
- int order, int mul)
-{
- int res = 0;
-
- while (order--) {
- res += *v1 * *v2++;
- *v1++ += mul * *v3++;
- }
- return res;
-}
-
static av_cold int ape_decode_init(AVCodecContext *avctx)
{
APEContext *s = avctx->priv_data;
@@ -261,9 +247,10 @@ static av_cold int ape_decode_init(AVCodecContext *avctx)
s->compression_level = AV_RL16(avctx->extradata + 2);
s->flags = AV_RL16(avctx->extradata + 4);
- av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n",
+ av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
s->compression_level, s->flags);
if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
+ !s->compression_level ||
(s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
s->compression_level);
@@ -306,16 +293,8 @@ static av_cold int ape_decode_init(AVCodecContext *avctx)
s->predictor_decode_stereo = predictor_decode_stereo_3950;
}
- s->adsp.scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_c;
-
- if (ARCH_ARM)
- ff_apedsp_init_arm(&s->adsp);
- if (ARCH_PPC)
- ff_apedsp_init_ppc(&s->adsp);
- if (ARCH_X86)
- ff_apedsp_init_x86(&s->adsp);
-
ff_bswapdsp_init(&s->bdsp);
+ ff_llauddsp_init(&s->adsp);
avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
return 0;
@@ -485,24 +464,24 @@ static inline void update_rice(APERice *rice, unsigned int x)
rice->k++;
}
-static inline int get_rice_ook(BitstreamContext *bc, int k)
+static inline int get_rice_ook(GetBitContext *gb, int k)
{
unsigned int x;
- x = get_unary(bc, 1, bitstream_bits_left(bc));
+ x = get_unary(gb, 1, get_bits_left(gb));
if (k)
- x = (x << k) | bitstream_read(bc, k);
+ x = (x << k) | get_bits(gb, k);
return x;
}
-static inline int ape_decode_value_3860(APEContext *ctx, BitstreamContext *bc,
+static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
APERice *rice)
{
unsigned int x, overflow;
- overflow = get_unary(bc, 1, bitstream_bits_left(bc));
+ overflow = get_unary(gb, 1, get_bits_left(gb));
if (ctx->fileversion > 3880) {
while (overflow >= 16) {
@@ -513,9 +492,12 @@ static inline int ape_decode_value_3860(APEContext *ctx, BitstreamContext *bc,
if (!rice->k)
x = overflow;
- else
- x = (overflow << rice->k) + bitstream_read(bc, rice->k);
-
+ else if(rice->k <= MIN_CACHE_BITS) {
+ x = (overflow << rice->k) + get_bits(gb, rice->k);
+ } else {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %"PRIu32"\n", rice->k);
+ return AVERROR_INVALIDDATA;
+ }
rice->ksum += x - (rice->ksum + 8 >> 4);
if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
rice->k--;
@@ -523,10 +505,7 @@ static inline int ape_decode_value_3860(APEContext *ctx, BitstreamContext *bc,
rice->k++;
/* Convert to signed */
- if (x & 1)
- return (x >> 1) + 1;
- else
- return -(x >> 1);
+ return ((x >> 1) ^ ((x & 1) - 1)) + 1;
}
static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
@@ -542,9 +521,13 @@ static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
} else
tmpk = (rice->k < 1) ? 0 : rice->k - 1;
- if (tmpk <= 16 || ctx->fileversion < 3910)
+ if (tmpk <= 16 || ctx->fileversion < 3910) {
+ if (tmpk > 23) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
+ return AVERROR_INVALIDDATA;
+ }
x = range_decode_bits(ctx, tmpk);
- else if (tmpk <= 32) {
+ } else if (tmpk <= 31) {
x = range_decode_bits(ctx, 16);
x |= (range_decode_bits(ctx, tmpk - 16) << 16);
} else {
@@ -556,10 +539,7 @@ static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
update_rice(rice, x);
/* Convert to signed */
- if (x & 1)
- return (x >> 1) + 1;
- else
- return -(x >> 1);
+ return ((x >> 1) ^ ((x & 1) - 1)) + 1;
}
static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
@@ -602,13 +582,10 @@ static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
update_rice(rice, x);
/* Convert to signed */
- if (x & 1)
- return (x >> 1) + 1;
- else
- return -(x >> 1);
+ return ((x >> 1) ^ ((x & 1) - 1)) + 1;
}
-static void decode_array_0000(APEContext *ctx, BitstreamContext *bc,
+static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
int32_t *out, APERice *rice, int blockstodecode)
{
int i;
@@ -616,19 +593,23 @@ static void decode_array_0000(APEContext *ctx, BitstreamContext *bc,
rice->ksum = 0;
for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
- out[i] = get_rice_ook(&ctx->bc, 10);
+ out[i] = get_rice_ook(&ctx->gb, 10);
rice->ksum += out[i];
}
rice->k = av_log2(rice->ksum / 10) + 1;
+ if (rice->k >= 24)
+ return;
for (; i < FFMIN(blockstodecode, 64); i++) {
- out[i] = get_rice_ook(&ctx->bc, rice->k);
+ out[i] = get_rice_ook(&ctx->gb, rice->k);
rice->ksum += out[i];
rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
+ if (rice->k >= 24)
+ return;
}
ksummax = 1 << rice->k + 7;
ksummin = rice->k ? (1 << rice->k + 6) : 0;
for (; i < blockstodecode; i++) {
- out[i] = get_rice_ook(&ctx->bc, rice->k);
+ out[i] = get_rice_ook(&ctx->gb, rice->k);
rice->ksum += out[i] - out[i - 64];
while (rice->ksum < ksummin) {
rice->k--;
@@ -644,25 +625,21 @@ static void decode_array_0000(APEContext *ctx, BitstreamContext *bc,
}
}
- for (i = 0; i < blockstodecode; i++) {
- if (out[i] & 1)
- out[i] = (out[i] >> 1) + 1;
- else
- out[i] = -(out[i] >> 1);
- }
+ for (i = 0; i < blockstodecode; i++)
+ out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
}
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
{
- decode_array_0000(ctx, &ctx->bc, ctx->decoded[0], &ctx->riceY,
+ decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
blockstodecode);
}
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
{
- decode_array_0000(ctx, &ctx->bc, ctx->decoded[0], &ctx->riceY,
+ decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
blockstodecode);
- decode_array_0000(ctx, &ctx->bc, ctx->decoded[1], &ctx->riceX,
+ decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
blockstodecode);
}
@@ -671,7 +648,7 @@ static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
int32_t *decoded0 = ctx->decoded[0];
while (blockstodecode--)
- *decoded0++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceY);
+ *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
}
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
@@ -681,9 +658,9 @@ static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
int blocks = blockstodecode;
while (blockstodecode--)
- *decoded0++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceY);
+ *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
while (blocks--)
- *decoded1++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceX);
+ *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
}
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
@@ -748,7 +725,7 @@ static int init_entropy_decoder(APEContext *ctx)
return AVERROR_INVALIDDATA;
ctx->CRC = bytestream_get_be32(&ctx->ptr);
} else {
- ctx->CRC = bitstream_read(&ctx->bc, 32);
+ ctx->CRC = get_bits_long(&ctx->gb, 32);
}
/* Read the frame flags if they exist */
@@ -909,11 +886,14 @@ static av_always_inline int filter_3800(APEPredictor *p,
return p->filterA[filter];
}
-static void long_filter_high_3800(int32_t *buffer, int order, int shift,
- int32_t *coeffs, int32_t *delay, int length)
+static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
{
int i, j;
int32_t dotprod, sign;
+ int32_t coeffs[256], delay[256];
+
+ if (order >= length)
+ return;
memset(coeffs, 0, order * sizeof(*coeffs));
for (i = 0; i < order; i++)
@@ -923,7 +903,7 @@ static void long_filter_high_3800(int32_t *buffer, int order, int shift,
sign = APESIGN(buffer[i]);
for (j = 0; j < order; j++) {
dotprod += delay[j] * coeffs[j];
- coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
+ coeffs[j] += ((delay[j] >> 31) | 1) * sign;
}
buffer[i] -= dotprod >> shift;
for (j = 0; j < order - 1; j++)
@@ -943,7 +923,7 @@ static void long_filter_ehigh_3830(int32_t *buffer, int length)
sign = APESIGN(buffer[i]);
for (j = 7; j >= 0; j--) {
dotprod += delay[j] * coeffs[j];
- coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
+ coeffs[j] += ((delay[j] >> 31) | 1) * sign;
}
for (j = 7; j > 0; j--)
delay[j] = delay[j - 1];
@@ -957,13 +937,12 @@ static void predictor_decode_stereo_3800(APEContext *ctx, int count)
APEPredictor *p = &ctx->predictor;
int32_t *decoded0 = ctx->decoded[0];
int32_t *decoded1 = ctx->decoded[1];
- int32_t coeffs[256], delay[256];
int start = 4, shift = 10;
if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
start = 16;
- long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
- long_filter_high_3800(decoded1, 16, 9, coeffs, delay, count);
+ long_filter_high_3800(decoded0, 16, 9, count);
+ long_filter_high_3800(decoded1, 16, 9, count);
} else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
int order = 128, shift2 = 11;
@@ -975,8 +954,8 @@ static void predictor_decode_stereo_3800(APEContext *ctx, int count)
long_filter_ehigh_3830(decoded1 + order, count - order);
}
start = order;
- long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
- long_filter_high_3800(decoded1, order, shift2, coeffs, delay, count);
+ long_filter_high_3800(decoded0, order, shift2, count);
+ long_filter_high_3800(decoded1, order, shift2, count);
}
while (count--) {
@@ -1012,12 +991,11 @@ static void predictor_decode_mono_3800(APEContext *ctx, int count)
{
APEPredictor *p = &ctx->predictor;
int32_t *decoded0 = ctx->decoded[0];
- int32_t coeffs[256], delay[256];
int start = 4, shift = 10;
if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
start = 16;
- long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
+ long_filter_high_3800(decoded0, 16, 9, count);
} else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
int order = 128, shift2 = 11;
@@ -1028,7 +1006,7 @@ static void predictor_decode_mono_3800(APEContext *ctx, int count)
long_filter_ehigh_3830(decoded0 + order, count - order);
}
start = order;
- long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
+ long_filter_high_3800(decoded0, order, shift2, count);
}
while (count--) {
@@ -1402,7 +1380,7 @@ static void ape_unpack_stereo(APEContext *ctx, int count)
int32_t *decoded0 = ctx->decoded[0];
int32_t *decoded1 = ctx->decoded[1];
- if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
+ if ((ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) == APE_FRAMECODE_STEREO_SILENCE) {
/* We are pure silence, so we're done. */
av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
return;
@@ -1434,6 +1412,7 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
int32_t *sample24;
int i, ch, ret;
int blockstodecode;
+ uint64_t decoded_buffer_size;
/* this should never be negative, but bad things will happen if it is, so
check it just to make sure. */
@@ -1458,7 +1437,7 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
}
if (s->fileversion < 3950) // previous versions overread two bytes
buf_size += 2;
- av_fast_malloc(&s->data, &s->data_size, buf_size);
+ av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
if (!s->data)
return AVERROR(ENOMEM);
s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
@@ -1481,26 +1460,26 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
}
s->ptr += offset;
} else {
- bitstream_init8(&s->bc, s->ptr, s->data_end - s->ptr);
+ if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
+ return ret;
if (s->fileversion > 3800)
- bitstream_skip(&s->bc, offset * 8);
+ skip_bits_long(&s->gb, offset * 8);
else
- bitstream_skip(&s->bc, offset);
+ skip_bits_long(&s->gb, offset);
}
- if (!nblocks || nblocks > INT_MAX) {
+ if (!nblocks || nblocks > INT_MAX / 2 / sizeof(*s->decoded_buffer) - 8) {
av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
nblocks);
return AVERROR_INVALIDDATA;
}
- s->samples = nblocks;
/* Initialize the frame decoder */
if (init_frame_decoder(s) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
return AVERROR_INVALIDDATA;
}
-
+ s->samples = nblocks;
}
if (!s->data) {
@@ -1515,8 +1494,9 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
blockstodecode = s->samples;
/* reallocate decoded sample buffer if needed */
- av_fast_malloc(&s->decoded_buffer, &s->decoded_size,
- 2 * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer));
+ decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer);
+ av_assert0(decoded_buffer_size <= INT_MAX);
+ av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size);
if (!s->decoded_buffer)
return AVERROR(ENOMEM);
memset(s->decoded_buffer, 0, s->decoded_size);
@@ -1525,10 +1505,8 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
/* get output buffer */
frame->nb_samples = blockstodecode;
- if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
return ret;
- }
s->error=0;
@@ -1572,7 +1550,7 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
*got_frame_ptr = 1;
- return (s->samples == 0) ? avpkt->size : 0;
+ return !s->samples ? avpkt->size : 0;
}
static void ape_flush(AVCodecContext *avctx)