Commit 4d49a4c5 authored by Alexandra Hájková's avatar Alexandra Hájková Committed by Diego Biurrun

apedec: Convert to the new bitstream reader

parent b4a911c1
...@@ -25,13 +25,14 @@ ...@@ -25,13 +25,14 @@
#include "libavutil/avassert.h" #include "libavutil/avassert.h"
#include "libavutil/channel_layout.h" #include "libavutil/channel_layout.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "apedsp.h" #include "apedsp.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "bswapdsp.h" #include "bswapdsp.h"
#include "bytestream.h" #include "bytestream.h"
#include "internal.h" #include "internal.h"
#include "get_bits.h" #include "unary.h"
#include "unary_legacy.h"
/** /**
* @file * @file
...@@ -162,7 +163,7 @@ typedef struct APEContext { ...@@ -162,7 +163,7 @@ typedef struct APEContext {
APERice riceX; ///< rice code parameters for the second channel APERice riceX; ///< rice code parameters for the second channel
APERice riceY; ///< rice code parameters for the first channel APERice riceY; ///< rice code parameters for the first channel
APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
GetBitContext gb; BitstreamContext bc;
uint8_t *data; ///< current frame data uint8_t *data; ///< current frame data
uint8_t *data_end; ///< frame data end uint8_t *data_end; ///< frame data end
...@@ -484,24 +485,24 @@ static inline void update_rice(APERice *rice, unsigned int x) ...@@ -484,24 +485,24 @@ static inline void update_rice(APERice *rice, unsigned int x)
rice->k++; rice->k++;
} }
static inline int get_rice_ook(GetBitContext *gb, int k) static inline int get_rice_ook(BitstreamContext *bc, int k)
{ {
unsigned int x; unsigned int x;
x = get_unary(gb, 1, get_bits_left(gb)); x = get_unary(bc, 1, bitstream_bits_left(bc));
if (k) if (k)
x = (x << k) | get_bits(gb, k); x = (x << k) | bitstream_read(bc, k);
return x; return x;
} }
static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, static inline int ape_decode_value_3860(APEContext *ctx, BitstreamContext *bc,
APERice *rice) APERice *rice)
{ {
unsigned int x, overflow; unsigned int x, overflow;
overflow = get_unary(gb, 1, get_bits_left(gb)); overflow = get_unary(bc, 1, bitstream_bits_left(bc));
if (ctx->fileversion > 3880) { if (ctx->fileversion > 3880) {
while (overflow >= 16) { while (overflow >= 16) {
...@@ -513,7 +514,7 @@ static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, ...@@ -513,7 +514,7 @@ static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
if (!rice->k) if (!rice->k)
x = overflow; x = overflow;
else else
x = (overflow << rice->k) + get_bits(gb, rice->k); x = (overflow << rice->k) + bitstream_read(bc, rice->k);
rice->ksum += x - (rice->ksum + 8 >> 4); rice->ksum += x - (rice->ksum + 8 >> 4);
if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0)) if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
...@@ -607,7 +608,7 @@ static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice) ...@@ -607,7 +608,7 @@ static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
return -(x >> 1); return -(x >> 1);
} }
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, static void decode_array_0000(APEContext *ctx, BitstreamContext *bc,
int32_t *out, APERice *rice, int blockstodecode) int32_t *out, APERice *rice, int blockstodecode)
{ {
int i; int i;
...@@ -615,19 +616,19 @@ static void decode_array_0000(APEContext *ctx, GetBitContext *gb, ...@@ -615,19 +616,19 @@ static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
rice->ksum = 0; rice->ksum = 0;
for (i = 0; i < FFMIN(blockstodecode, 5); i++) { for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
out[i] = get_rice_ook(&ctx->gb, 10); out[i] = get_rice_ook(&ctx->bc, 10);
rice->ksum += out[i]; rice->ksum += out[i];
} }
rice->k = av_log2(rice->ksum / 10) + 1; rice->k = av_log2(rice->ksum / 10) + 1;
for (; i < FFMIN(blockstodecode, 64); i++) { for (; i < FFMIN(blockstodecode, 64); i++) {
out[i] = get_rice_ook(&ctx->gb, rice->k); out[i] = get_rice_ook(&ctx->bc, rice->k);
rice->ksum += out[i]; rice->ksum += out[i];
rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1; rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
} }
ksummax = 1 << rice->k + 7; ksummax = 1 << rice->k + 7;
ksummin = rice->k ? (1 << rice->k + 6) : 0; ksummin = rice->k ? (1 << rice->k + 6) : 0;
for (; i < blockstodecode; i++) { for (; i < blockstodecode; i++) {
out[i] = get_rice_ook(&ctx->gb, rice->k); out[i] = get_rice_ook(&ctx->bc, rice->k);
rice->ksum += out[i] - out[i - 64]; rice->ksum += out[i] - out[i - 64];
while (rice->ksum < ksummin) { while (rice->ksum < ksummin) {
rice->k--; rice->k--;
...@@ -653,15 +654,15 @@ static void decode_array_0000(APEContext *ctx, GetBitContext *gb, ...@@ -653,15 +654,15 @@ static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode) static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
{ {
decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY, decode_array_0000(ctx, &ctx->bc, ctx->decoded[0], &ctx->riceY,
blockstodecode); blockstodecode);
} }
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode) static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
{ {
decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY, decode_array_0000(ctx, &ctx->bc, ctx->decoded[0], &ctx->riceY,
blockstodecode); blockstodecode);
decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX, decode_array_0000(ctx, &ctx->bc, ctx->decoded[1], &ctx->riceX,
blockstodecode); blockstodecode);
} }
...@@ -670,7 +671,7 @@ static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode) ...@@ -670,7 +671,7 @@ static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
int32_t *decoded0 = ctx->decoded[0]; int32_t *decoded0 = ctx->decoded[0];
while (blockstodecode--) while (blockstodecode--)
*decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY); *decoded0++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceY);
} }
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode) static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
...@@ -680,9 +681,9 @@ static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode) ...@@ -680,9 +681,9 @@ static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
int blocks = blockstodecode; int blocks = blockstodecode;
while (blockstodecode--) while (blockstodecode--)
*decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY); *decoded0++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceY);
while (blocks--) while (blocks--)
*decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX); *decoded1++ = ape_decode_value_3860(ctx, &ctx->bc, &ctx->riceX);
} }
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode) static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
...@@ -747,7 +748,7 @@ static int init_entropy_decoder(APEContext *ctx) ...@@ -747,7 +748,7 @@ static int init_entropy_decoder(APEContext *ctx)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
ctx->CRC = bytestream_get_be32(&ctx->ptr); ctx->CRC = bytestream_get_be32(&ctx->ptr);
} else { } else {
ctx->CRC = get_bits_long(&ctx->gb, 32); ctx->CRC = bitstream_read(&ctx->bc, 32);
} }
/* Read the frame flags if they exist */ /* Read the frame flags if they exist */
...@@ -1480,11 +1481,11 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data, ...@@ -1480,11 +1481,11 @@ static int ape_decode_frame(AVCodecContext *avctx, void *data,
} }
s->ptr += offset; s->ptr += offset;
} else { } else {
init_get_bits(&s->gb, s->ptr, (s->data_end - s->ptr) * 8); bitstream_init8(&s->bc, s->ptr, s->data_end - s->ptr);
if (s->fileversion > 3800) if (s->fileversion > 3800)
skip_bits_long(&s->gb, offset * 8); bitstream_skip(&s->bc, offset * 8);
else else
skip_bits_long(&s->gb, offset); bitstream_skip(&s->bc, offset);
} }
if (!nblocks || nblocks > INT_MAX) { if (!nblocks || nblocks > INT_MAX) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment