Commit 2b94ed12 authored by Alexandra Hájková's avatar Alexandra Hájková Committed by Diego Biurrun

shorten: Convert to the new bitstream reader

parent 5a6da49d
...@@ -26,10 +26,11 @@ ...@@ -26,10 +26,11 @@
*/ */
#include <limits.h> #include <limits.h>
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "bytestream.h" #include "bytestream.h"
#include "get_bits.h" #include "golomb.h"
#include "golomb_legacy.h"
#include "internal.h" #include "internal.h"
#define MAX_CHANNELS 8 #define MAX_CHANNELS 8
...@@ -79,7 +80,7 @@ static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 }; ...@@ -79,7 +80,7 @@ static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
typedef struct ShortenContext { typedef struct ShortenContext {
AVCodecContext *avctx; AVCodecContext *avctx;
GetBitContext gb; BitstreamContext bc;
int min_framesize, max_framesize; int min_framesize, max_framesize;
unsigned channels; unsigned channels;
...@@ -154,8 +155,8 @@ static int allocate_buffers(ShortenContext *s) ...@@ -154,8 +155,8 @@ static int allocate_buffers(ShortenContext *s)
static inline unsigned int get_uint(ShortenContext *s, int k) static inline unsigned int get_uint(ShortenContext *s, int k)
{ {
if (s->version != 0) if (s->version != 0)
k = get_ur_golomb_shorten(&s->gb, ULONGSIZE); k = get_ur_golomb_shorten(&s->bc, ULONGSIZE);
return get_ur_golomb_shorten(&s->gb, k); return get_ur_golomb_shorten(&s->bc, k);
} }
static void fix_bitshift(ShortenContext *s, int32_t *buffer) static void fix_bitshift(ShortenContext *s, int32_t *buffer)
...@@ -280,7 +281,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, ...@@ -280,7 +281,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
if (command == FN_QLPC) { if (command == FN_QLPC) {
/* read/validate prediction order */ /* read/validate prediction order */
pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE); pred_order = get_ur_golomb_shorten(&s->bc, LPCQSIZE);
if (pred_order > s->nwrap) { if (pred_order > s->nwrap) {
av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
pred_order); pred_order);
...@@ -288,7 +289,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, ...@@ -288,7 +289,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
} }
/* read LPC coefficients */ /* read LPC coefficients */
for (i = 0; i < pred_order; i++) for (i = 0; i < pred_order; i++)
s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT); s->coeffs[i] = get_sr_golomb_shorten(&s->bc, LPCQUANT);
coeffs = s->coeffs; coeffs = s->coeffs;
qshift = LPCQUANT; qshift = LPCQUANT;
...@@ -315,7 +316,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, ...@@ -315,7 +316,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
sum = init_sum; sum = init_sum;
for (j = 0; j < pred_order; j++) for (j = 0; j < pred_order; j++)
sum += coeffs[j] * s->decoded[channel][i - j - 1]; sum += coeffs[j] * s->decoded[channel][i - j - 1];
s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i] = get_sr_golomb_shorten(&s->bc, residual_size) +
(sum >> qshift); (sum >> qshift);
} }
...@@ -332,7 +333,7 @@ static int read_header(ShortenContext *s) ...@@ -332,7 +333,7 @@ static int read_header(ShortenContext *s)
int i, ret; int i, ret;
int maxnlpc = 0; int maxnlpc = 0;
/* shorten signature */ /* shorten signature */
if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) { if (bitstream_read(&s->bc, 32) != AV_RB32("ajkg")) {
av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n"); av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
...@@ -340,7 +341,7 @@ static int read_header(ShortenContext *s) ...@@ -340,7 +341,7 @@ static int read_header(ShortenContext *s)
s->lpcqoffset = 0; s->lpcqoffset = 0;
s->blocksize = DEFAULT_BLOCK_SIZE; s->blocksize = DEFAULT_BLOCK_SIZE;
s->nmean = -1; s->nmean = -1;
s->version = get_bits(&s->gb, 8); s->version = bitstream_read(&s->bc, 8);
s->internal_ftype = get_uint(s, TYPESIZE); s->internal_ftype = get_uint(s, TYPESIZE);
s->channels = get_uint(s, CHANSIZE); s->channels = get_uint(s, CHANSIZE);
...@@ -374,7 +375,7 @@ static int read_header(ShortenContext *s) ...@@ -374,7 +375,7 @@ static int read_header(ShortenContext *s)
skip_bytes = get_uint(s, NSKIPSIZE); skip_bytes = get_uint(s, NSKIPSIZE);
for (i = 0; i < skip_bytes; i++) for (i = 0; i < skip_bytes; i++)
skip_bits(&s->gb, 8); bitstream_skip(&s->bc, 8);
} }
s->nwrap = FFMAX(NWRAP, maxnlpc); s->nwrap = FFMAX(NWRAP, maxnlpc);
...@@ -387,13 +388,13 @@ static int read_header(ShortenContext *s) ...@@ -387,13 +388,13 @@ static int read_header(ShortenContext *s)
if (s->version > 1) if (s->version > 1)
s->lpcqoffset = V2LPCQOFFSET; s->lpcqoffset = V2LPCQOFFSET;
if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) { if (get_ur_golomb_shorten(&s->bc, FNSIZE) != FN_VERBATIM) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"missing verbatim section at beginning of stream\n"); "missing verbatim section at beginning of stream\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); s->header_size = get_ur_golomb_shorten(&s->bc, VERBATIM_CKSIZE_SIZE);
if (s->header_size >= OUT_BUFFER_SIZE || if (s->header_size >= OUT_BUFFER_SIZE ||
s->header_size < CANONICAL_HEADER_SIZE) { s->header_size < CANONICAL_HEADER_SIZE) {
av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
...@@ -402,7 +403,7 @@ static int read_header(ShortenContext *s) ...@@ -402,7 +403,7 @@ static int read_header(ShortenContext *s)
} }
for (i = 0; i < s->header_size; i++) for (i = 0; i < s->header_size; i++)
s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); s->header[i] = (char)get_ur_golomb_shorten(&s->bc, VERBATIM_BYTE_SIZE);
if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0) if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
return ret; return ret;
...@@ -464,8 +465,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, ...@@ -464,8 +465,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data,
} }
} }
/* init and position bitstream reader */ /* init and position bitstream reader */
init_get_bits(&s->gb, buf, buf_size * 8); bitstream_init8(&s->bc, buf, buf_size);
skip_bits(&s->gb, s->bitindex); bitstream_skip(&s->bc, s->bitindex);
/* process header or next subblock */ /* process header or next subblock */
if (!s->got_header) { if (!s->got_header) {
...@@ -486,12 +487,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, ...@@ -486,12 +487,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data,
unsigned cmd; unsigned cmd;
int len; int len;
if (get_bits_left(&s->gb) < 3 + FNSIZE) { if (bitstream_bits_left(&s->bc) < 3 + FNSIZE) {
*got_frame_ptr = 0; *got_frame_ptr = 0;
break; break;
} }
cmd = get_ur_golomb_shorten(&s->gb, FNSIZE); cmd = get_ur_golomb_shorten(&s->bc, FNSIZE);
if (cmd > FN_VERBATIM) { if (cmd > FN_VERBATIM) {
av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd); av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
...@@ -503,12 +504,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, ...@@ -503,12 +504,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data,
/* process non-audio command */ /* process non-audio command */
switch (cmd) { switch (cmd) {
case FN_VERBATIM: case FN_VERBATIM:
len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); len = get_ur_golomb_shorten(&s->bc, VERBATIM_CKSIZE_SIZE);
while (len--) while (len--)
get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); get_ur_golomb_shorten(&s->bc, VERBATIM_BYTE_SIZE);
break; break;
case FN_BITSHIFT: case FN_BITSHIFT:
s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE); s->bitshift = get_ur_golomb_shorten(&s->bc, BITSHIFTSIZE);
if (s->bitshift < 0) if (s->bitshift < 0)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
break; break;
...@@ -543,7 +544,7 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, ...@@ -543,7 +544,7 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data,
/* get Rice code for residual decoding */ /* get Rice code for residual decoding */
if (cmd != FN_ZERO) { if (cmd != FN_ZERO) {
residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE); residual_size = get_ur_golomb_shorten(&s->bc, ENERGYSIZE);
/* This is a hack as version 0 differed in the definition /* This is a hack as version 0 differed in the definition
* of get_sr_golomb_shorten(). */ * of get_sr_golomb_shorten(). */
if (s->version == 0) if (s->version == 0)
...@@ -616,8 +617,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, ...@@ -616,8 +617,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data,
*got_frame_ptr = 0; *got_frame_ptr = 0;
finish_frame: finish_frame:
s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8); s->bitindex = bitstream_tell(&s->bc) - 8 * (bitstream_tell(&s->bc) / 8);
i = get_bits_count(&s->gb) / 8; i = bitstream_tell(&s->bc) / 8;
if (i > buf_size) { if (i > buf_size) {
av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size); av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
s->bitstream_size = 0; s->bitstream_size = 0;
......
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