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

wma: Convert to the new bitstream reader

parent 58d87e0f
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "libavutil/attributes.h" #include "libavutil/attributes.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "internal.h" #include "internal.h"
#include "sinewin.h" #include "sinewin.h"
#include "wma.h" #include "wma.h"
...@@ -382,30 +383,30 @@ int ff_wma_end(AVCodecContext *avctx) ...@@ -382,30 +383,30 @@ int ff_wma_end(AVCodecContext *avctx)
/** /**
* Decode an uncompressed coefficient. * Decode an uncompressed coefficient.
* @param gb GetBitContext * @param bc BitstreamContext
* @return the decoded coefficient * @return the decoded coefficient
*/ */
unsigned int ff_wma_get_large_val(GetBitContext *gb) unsigned int ff_wma_get_large_val(BitstreamContext *bc)
{ {
/** consumes up to 34 bits */ /** consumes up to 34 bits */
int n_bits = 8; int n_bits = 8;
/** decode length */ /** decode length */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
n_bits += 8; n_bits += 8;
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
n_bits += 8; n_bits += 8;
if (get_bits1(gb)) if (bitstream_read_bit(bc))
n_bits += 7; n_bits += 7;
} }
} }
return get_bits_long(gb, n_bits); return bitstream_read(bc, n_bits);
} }
/** /**
* Decode run level compressed coefficients. * Decode run level compressed coefficients.
* @param avctx codec context * @param avctx codec context
* @param gb bitstream reader context * @param bc bitstream reader context
* @param vlc vlc table for get_vlc2 * @param vlc VLC table for bitstream_read_vlc
* @param level_table level codes * @param level_table level codes
* @param run_table run codes * @param run_table run codes
* @param version 0 for wma1,2 1 for wmapro * @param version 0 for wma1,2 1 for wmapro
...@@ -417,7 +418,7 @@ unsigned int ff_wma_get_large_val(GetBitContext *gb) ...@@ -417,7 +418,7 @@ unsigned int ff_wma_get_large_val(GetBitContext *gb)
* @param coef_nb_bits number of bits for escaped level codes * @param coef_nb_bits number of bits for escaped level codes
* @return 0 on success, -1 otherwise * @return 0 on success, -1 otherwise
*/ */
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, int ff_wma_run_level_decode(AVCodecContext *avctx, BitstreamContext *bc,
VLC *vlc, const float *level_table, VLC *vlc, const float *level_table,
const uint16_t *run_table, int version, const uint16_t *run_table, int version,
WMACoef *ptr, int offset, int num_coefs, WMACoef *ptr, int offset, int num_coefs,
...@@ -429,11 +430,11 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, ...@@ -429,11 +430,11 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
uint32_t *iptr = (uint32_t *) ptr; uint32_t *iptr = (uint32_t *) ptr;
const unsigned int coef_mask = block_len - 1; const unsigned int coef_mask = block_len - 1;
for (; offset < num_coefs; offset++) { for (; offset < num_coefs; offset++) {
code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX); code = bitstream_read_vlc(bc, vlc->table, VLCBITS, VLCMAX);
if (code > 1) { if (code > 1) {
/** normal code */ /** normal code */
offset += run_table[code]; offset += run_table[code];
sign = get_bits1(gb) - 1; sign = bitstream_read_bit(bc) - 1;
iptr[offset & coef_mask] = ilvl[code] ^ sign << 31; iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
} else if (code == 1) { } else if (code == 1) {
/** EOB */ /** EOB */
...@@ -441,26 +442,26 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, ...@@ -441,26 +442,26 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
} else { } else {
/** escape */ /** escape */
if (!version) { if (!version) {
level = get_bits(gb, coef_nb_bits); level = bitstream_read(bc, coef_nb_bits);
/** NOTE: this is rather suboptimal. reading /** NOTE: this is rather suboptimal. reading
* block_len_bits would be better */ * block_len_bits would be better */
offset += get_bits(gb, frame_len_bits); offset += bitstream_read(bc, frame_len_bits);
} else { } else {
level = ff_wma_get_large_val(gb); level = ff_wma_get_large_val(bc);
/** escape decode */ /** escape decode */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
av_log(avctx, AV_LOG_ERROR, av_log(avctx, AV_LOG_ERROR,
"broken escape sequence\n"); "broken escape sequence\n");
return -1; return -1;
} else } else
offset += get_bits(gb, frame_len_bits) + 4; offset += bitstream_read(bc, frame_len_bits) + 4;
} else } else
offset += get_bits(gb, 2) + 1; offset += bitstream_read(bc, 2) + 1;
} }
} }
sign = get_bits1(gb) - 1; sign = bitstream_read_bit(bc) - 1;
ptr[offset & coef_mask] = (level ^ sign) - sign; ptr[offset & coef_mask] = (level ^ sign) - sign;
} }
} }
......
...@@ -25,8 +25,8 @@ ...@@ -25,8 +25,8 @@
#include "libavutil/float_dsp.h" #include "libavutil/float_dsp.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "fft.h" #include "fft.h"
#include "get_bits.h"
#include "put_bits.h" #include "put_bits.h"
/* size of blocks */ /* size of blocks */
...@@ -66,7 +66,7 @@ typedef struct CoefVLCTable { ...@@ -66,7 +66,7 @@ typedef struct CoefVLCTable {
typedef struct WMACodecContext { typedef struct WMACodecContext {
AVCodecContext *avctx; AVCodecContext *avctx;
GetBitContext gb; BitstreamContext bc;
PutBitContext pb; PutBitContext pb;
int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
int use_bit_reservoir; int use_bit_reservoir;
...@@ -147,8 +147,8 @@ extern const uint8_t ff_aac_scalefactor_bits[121]; ...@@ -147,8 +147,8 @@ extern const uint8_t ff_aac_scalefactor_bits[121];
int ff_wma_init(AVCodecContext *avctx, int flags2); int ff_wma_init(AVCodecContext *avctx, int flags2);
int ff_wma_total_gain_to_bits(int total_gain); int ff_wma_total_gain_to_bits(int total_gain);
int ff_wma_end(AVCodecContext *avctx); int ff_wma_end(AVCodecContext *avctx);
unsigned int ff_wma_get_large_val(GetBitContext *gb); unsigned int ff_wma_get_large_val(BitstreamContext *bc);
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, int ff_wma_run_level_decode(AVCodecContext *avctx, BitstreamContext *bc,
VLC *vlc, const float *level_table, VLC *vlc, const float *level_table,
const uint16_t *run_table, int version, const uint16_t *run_table, int version,
WMACoef *ptr, int offset, int num_coefs, WMACoef *ptr, int offset, int num_coefs,
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "libavutil/attributes.h" #include "libavutil/attributes.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "internal.h" #include "internal.h"
#include "wma.h" #include "wma.h"
...@@ -209,9 +210,9 @@ static void decode_exp_lsp(WMACodecContext *s, int ch) ...@@ -209,9 +210,9 @@ static void decode_exp_lsp(WMACodecContext *s, int ch)
for (i = 0; i < NB_LSP_COEFS; i++) { for (i = 0; i < NB_LSP_COEFS; i++) {
if (i == 0 || i >= 8) if (i == 0 || i >= 8)
val = get_bits(&s->gb, 3); val = bitstream_read(&s->bc, 3);
else else
val = get_bits(&s->gb, 4); val = bitstream_read(&s->bc, 4);
lsp_coefs[i] = ff_wma_lsp_codebook[i][val]; lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
} }
...@@ -318,7 +319,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch) ...@@ -318,7 +319,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
q_end = q + s->block_len; q_end = q + s->block_len;
max_scale = 0; max_scale = 0;
if (s->version == 1) { if (s->version == 1) {
last_exp = get_bits(&s->gb, 5) + 10; last_exp = bitstream_read(&s->bc, 5) + 10;
v = ptab[last_exp]; v = ptab[last_exp];
iv = iptab[last_exp]; iv = iptab[last_exp];
max_scale = v; max_scale = v;
...@@ -333,7 +334,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch) ...@@ -333,7 +334,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
last_exp = 36; last_exp = 36;
while (q < q_end) { while (q < q_end) {
code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX); code = bitstream_read_vlc(&s->bc, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
if (code < 0) { if (code < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n"); av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
return -1; return -1;
...@@ -436,7 +437,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -436,7 +437,7 @@ static int wma_decode_block(WMACodecContext *s)
if (s->reset_block_lengths) { if (s->reset_block_lengths) {
s->reset_block_lengths = 0; s->reset_block_lengths = 0;
v = get_bits(&s->gb, n); v = bitstream_read(&s->bc, n);
if (v >= s->nb_block_sizes) { if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"prev_block_len_bits %d out of range\n", "prev_block_len_bits %d out of range\n",
...@@ -444,7 +445,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -444,7 +445,7 @@ static int wma_decode_block(WMACodecContext *s)
return -1; return -1;
} }
s->prev_block_len_bits = s->frame_len_bits - v; s->prev_block_len_bits = s->frame_len_bits - v;
v = get_bits(&s->gb, n); v = bitstream_read(&s->bc, n);
if (v >= s->nb_block_sizes) { if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"block_len_bits %d out of range\n", "block_len_bits %d out of range\n",
...@@ -457,7 +458,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -457,7 +458,7 @@ static int wma_decode_block(WMACodecContext *s)
s->prev_block_len_bits = s->block_len_bits; s->prev_block_len_bits = s->block_len_bits;
s->block_len_bits = s->next_block_len_bits; s->block_len_bits = s->next_block_len_bits;
} }
v = get_bits(&s->gb, n); v = bitstream_read(&s->bc, n);
if (v >= s->nb_block_sizes) { if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"next_block_len_bits %d out of range\n", "next_block_len_bits %d out of range\n",
...@@ -480,10 +481,10 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -480,10 +481,10 @@ static int wma_decode_block(WMACodecContext *s)
} }
if (s->avctx->channels == 2) if (s->avctx->channels == 2)
s->ms_stereo = get_bits1(&s->gb); s->ms_stereo = bitstream_read_bit(&s->bc);
v = 0; v = 0;
for (ch = 0; ch < s->avctx->channels; ch++) { for (ch = 0; ch < s->avctx->channels; ch++) {
a = get_bits1(&s->gb); a = bitstream_read_bit(&s->bc);
s->channel_coded[ch] = a; s->channel_coded[ch] = a;
v |= a; v |= a;
} }
...@@ -499,7 +500,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -499,7 +500,7 @@ static int wma_decode_block(WMACodecContext *s)
* coef escape coding */ * coef escape coding */
total_gain = 1; total_gain = 1;
for (;;) { for (;;) {
a = get_bits(&s->gb, 7); a = bitstream_read(&s->bc, 7);
total_gain += a; total_gain += a;
if (a != 127) if (a != 127)
break; break;
...@@ -519,7 +520,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -519,7 +520,7 @@ static int wma_decode_block(WMACodecContext *s)
int i, n, a; int i, n, a;
n = s->exponent_high_sizes[bsize]; n = s->exponent_high_sizes[bsize];
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
a = get_bits1(&s->gb); a = bitstream_read_bit(&s->bc);
s->high_band_coded[ch][i] = a; s->high_band_coded[ch][i] = a;
/* if noise coding, the coefficients are not transmitted */ /* if noise coding, the coefficients are not transmitted */
if (a) if (a)
...@@ -536,9 +537,10 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -536,9 +537,10 @@ static int wma_decode_block(WMACodecContext *s)
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (s->high_band_coded[ch][i]) { if (s->high_band_coded[ch][i]) {
if (val == (int) 0x80000000) { if (val == (int) 0x80000000) {
val = get_bits(&s->gb, 7) - 19; val = bitstream_read(&s->bc, 7) - 19;
} else { } else {
code = get_vlc2(&s->gb, s->hgain_vlc.table, code = bitstream_read_vlc(&s->bc,
s->hgain_vlc.table,
HGAINVLCBITS, HGAINMAX); HGAINVLCBITS, HGAINMAX);
if (code < 0) { if (code < 0) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
...@@ -555,7 +557,7 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -555,7 +557,7 @@ static int wma_decode_block(WMACodecContext *s)
} }
/* exponents can be reused in short blocks. */ /* exponents can be reused in short blocks. */
if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb)) { if ((s->block_len_bits == s->frame_len_bits) || bitstream_read_bit(&s->bc)) {
for (ch = 0; ch < s->avctx->channels; ch++) { for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) { if (s->channel_coded[ch]) {
if (s->use_exp_vlc) { if (s->use_exp_vlc) {
...@@ -579,13 +581,13 @@ static int wma_decode_block(WMACodecContext *s) ...@@ -579,13 +581,13 @@ static int wma_decode_block(WMACodecContext *s)
* there is potentially less energy there */ * there is potentially less energy there */
tindex = (ch == 1 && s->ms_stereo); tindex = (ch == 1 && s->ms_stereo);
memset(ptr, 0, s->block_len * sizeof(WMACoef)); memset(ptr, 0, s->block_len * sizeof(WMACoef));
ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex], ff_wma_run_level_decode(s->avctx, &s->bc, &s->coef_vlc[tindex],
s->level_table[tindex], s->run_table[tindex], s->level_table[tindex], s->run_table[tindex],
0, ptr, 0, nb_coefs[ch], 0, ptr, 0, nb_coefs[ch],
s->block_len, s->frame_len_bits, coef_nb_bits); s->block_len, s->frame_len_bits, coef_nb_bits);
} }
if (s->version == 1 && s->avctx->channels >= 2) if (s->version == 1 && s->avctx->channels >= 2)
align_get_bits(&s->gb); bitstream_align(&s->bc);
} }
/* normalize */ /* normalize */
...@@ -810,12 +812,12 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data, ...@@ -810,12 +812,12 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
} }
buf_size = avctx->block_align; buf_size = avctx->block_align;
init_get_bits(&s->gb, buf, buf_size * 8); bitstream_init8(&s->bc, buf, buf_size);
if (s->use_bit_reservoir) { if (s->use_bit_reservoir) {
/* read super frame header */ /* read super frame header */
skip_bits(&s->gb, 4); /* super frame index */ bitstream_skip(&s->bc, 4); /* super frame index */
nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0); nb_frames = bitstream_read(&s->bc, 4) - (s->last_superframe_len <= 0);
} else } else
nb_frames = 1; nb_frames = 1;
...@@ -829,11 +831,11 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data, ...@@ -829,11 +831,11 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
samples_offset = 0; samples_offset = 0;
if (s->use_bit_reservoir) { if (s->use_bit_reservoir) {
bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); bit_offset = bitstream_read(&s->bc, s->byte_offset_bits + 3);
if (bit_offset > get_bits_left(&s->gb)) { if (bit_offset > bitstream_bits_left(&s->bc)) {
av_log(avctx, AV_LOG_ERROR, av_log(avctx, AV_LOG_ERROR,
"Invalid last frame bit offset %d > buf size %d (%d)\n", "Invalid last frame bit offset %d > buf size %d (%d)\n",
bit_offset, get_bits_left(&s->gb), buf_size); bit_offset, bitstream_bits_left(&s->bc), buf_size);
goto fail; goto fail;
} }
...@@ -845,19 +847,19 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data, ...@@ -845,19 +847,19 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
q = s->last_superframe + s->last_superframe_len; q = s->last_superframe + s->last_superframe_len;
len = bit_offset; len = bit_offset;
while (len > 7) { while (len > 7) {
*q++ = (get_bits) (&s->gb, 8); *q++ = bitstream_read(&s->bc, 8);
len -= 8; len -= 8;
} }
if (len > 0) if (len > 0)
*q++ = (get_bits) (&s->gb, len) << (8 - len); *q++ = bitstream_read(&s->bc, len) << (8 - len);
memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE); memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE);
/* XXX: bit_offset bits into last frame */ /* XXX: bit_offset bits into last frame */
init_get_bits(&s->gb, s->last_superframe, bitstream_init(&s->bc, s->last_superframe,
s->last_superframe_len * 8 + bit_offset); s->last_superframe_len * 8 + bit_offset);
/* skip unused bits */ /* skip unused bits */
if (s->last_bitoffset > 0) if (s->last_bitoffset > 0)
skip_bits(&s->gb, s->last_bitoffset); bitstream_skip(&s->bc, s->last_bitoffset);
/* this frame is stored in the last superframe and in the /* this frame is stored in the last superframe and in the
* current one */ * current one */
if (wma_decode_frame(s, samples, samples_offset) < 0) if (wma_decode_frame(s, samples, samples_offset) < 0)
...@@ -870,10 +872,10 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data, ...@@ -870,10 +872,10 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8) if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3)) * 8); bitstream_init8(&s->bc, buf + (pos >> 3), buf_size - (pos >> 3));
len = pos & 7; len = pos & 7;
if (len > 0) if (len > 0)
skip_bits(&s->gb, len); bitstream_skip(&s->bc, len);
s->reset_block_lengths = 1; s->reset_block_lengths = 1;
for (i = 0; i < nb_frames; i++) { for (i = 0; i < nb_frames; i++) {
...@@ -883,7 +885,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data, ...@@ -883,7 +885,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
} }
/* we copy the end of the frame in the last frame buffer */ /* we copy the end of the frame in the last frame buffer */
pos = get_bits_count(&s->gb) + pos = bitstream_tell(&s->bc) +
((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
s->last_bitoffset = pos & 7; s->last_bitoffset = pos & 7;
pos >>= 3; pos >>= 3;
......
...@@ -28,8 +28,8 @@ ...@@ -28,8 +28,8 @@
#include "libavutil/avassert.h" #include "libavutil/avassert.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "internal.h" #include "internal.h"
#include "get_bits.h"
#include "put_bits.h" #include "put_bits.h"
#include "wma.h" #include "wma.h"
#include "wma_common.h" #include "wma_common.h"
...@@ -87,7 +87,7 @@ typedef struct WmallDecodeCtx { ...@@ -87,7 +87,7 @@ typedef struct WmallDecodeCtx {
uint16_t min_samples_per_subframe; uint16_t min_samples_per_subframe;
/* packet decode state */ /* packet decode state */
GetBitContext pgb; ///< bitstream reader context for the packet BitstreamContext pbc; ///< bitstream reader context for the packet
int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet
uint8_t packet_offset; ///< offset to the frame in the packet uint8_t packet_offset; ///< offset to the frame in the packet
uint8_t packet_sequence_number; ///< current packet number uint8_t packet_sequence_number; ///< current packet number
...@@ -99,7 +99,7 @@ typedef struct WmallDecodeCtx { ...@@ -99,7 +99,7 @@ typedef struct WmallDecodeCtx {
/* frame decode state */ /* frame decode state */
uint32_t frame_num; ///< current frame number (not used for decoding) uint32_t frame_num; ///< current frame number (not used for decoding)
GetBitContext gb; ///< bitstream reader context BitstreamContext bc; ///< bitstream reader context
int buf_bit_size; ///< buffer size in bits int buf_bit_size; ///< buffer size in bits
int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (16-bit) int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (16-bit)
int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (24-bit) int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (24-bit)
...@@ -286,7 +286,7 @@ static int decode_subframe_length(WmallDecodeCtx *s, int offset) ...@@ -286,7 +286,7 @@ static int decode_subframe_length(WmallDecodeCtx *s, int offset)
return s->min_samples_per_subframe; return s->min_samples_per_subframe;
len = av_log2(s->max_num_subframes - 1) + 1; len = av_log2(s->max_num_subframes - 1) + 1;
frame_len_ratio = get_bits(&s->gb, len); frame_len_ratio = bitstream_read(&s->bc, len);
subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1); subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
/* sanity check the length */ /* sanity check the length */
...@@ -332,7 +332,7 @@ static int decode_tilehdr(WmallDecodeCtx *s) ...@@ -332,7 +332,7 @@ static int decode_tilehdr(WmallDecodeCtx *s)
for (c = 0; c < s->num_channels; c++) for (c = 0; c < s->num_channels; c++)
s->channel[c].num_subframes = 0; s->channel[c].num_subframes = 0;
tile_aligned = get_bits1(&s->gb); tile_aligned = bitstream_read_bit(&s->bc);
if (s->max_num_subframes == 1 || tile_aligned) if (s->max_num_subframes == 1 || tile_aligned)
fixed_channel_layout = 1; fixed_channel_layout = 1;
...@@ -347,7 +347,7 @@ static int decode_tilehdr(WmallDecodeCtx *s) ...@@ -347,7 +347,7 @@ static int decode_tilehdr(WmallDecodeCtx *s)
(min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) { (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
contains_subframe[c] = in_use = 1; contains_subframe[c] = in_use = 1;
} else { } else {
if (get_bits1(&s->gb)) if (bitstream_read_bit(&s->bc))
contains_subframe[c] = in_use = 1; contains_subframe[c] = in_use = 1;
} }
} else } else
...@@ -407,32 +407,32 @@ static int decode_tilehdr(WmallDecodeCtx *s) ...@@ -407,32 +407,32 @@ static int decode_tilehdr(WmallDecodeCtx *s)
static void decode_ac_filter(WmallDecodeCtx *s) static void decode_ac_filter(WmallDecodeCtx *s)
{ {
int i; int i;
s->acfilter_order = get_bits(&s->gb, 4) + 1; s->acfilter_order = bitstream_read(&s->bc, 4) + 1;
s->acfilter_scaling = get_bits(&s->gb, 4); s->acfilter_scaling = bitstream_read(&s->bc, 4);
for (i = 0; i < s->acfilter_order; i++) for (i = 0; i < s->acfilter_order; i++)
s->acfilter_coeffs[i] = get_bitsz(&s->gb, s->acfilter_scaling) + 1; s->acfilter_coeffs[i] = bitstream_read(&s->bc, s->acfilter_scaling) + 1;
} }
static void decode_mclms(WmallDecodeCtx *s) static void decode_mclms(WmallDecodeCtx *s)
{ {
s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2; s->mclms_order = (bitstream_read(&s->bc, 4) + 1) * 2;
s->mclms_scaling = get_bits(&s->gb, 4); s->mclms_scaling = bitstream_read(&s->bc, 4);
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
int i, send_coef_bits; int i, send_coef_bits;
int cbits = av_log2(s->mclms_scaling + 1); int cbits = av_log2(s->mclms_scaling + 1);
if (1 << cbits < s->mclms_scaling + 1) if (1 << cbits < s->mclms_scaling + 1)
cbits++; cbits++;
send_coef_bits = get_bitsz(&s->gb, cbits) + 2; send_coef_bits = bitstream_read(&s->bc, cbits) + 2;
for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits); s->mclms_coeffs[i] = bitstream_read(&s->bc, send_coef_bits);
for (i = 0; i < s->num_channels; i++) { for (i = 0; i < s->num_channels; i++) {
int c; int c;
for (c = 0; c < i; c++) for (c = 0; c < i; c++)
s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits); s->mclms_coeffs_cur[i * s->num_channels + c] = bitstream_read(&s->bc, send_coef_bits);
} }
} }
} }
...@@ -440,12 +440,12 @@ static void decode_mclms(WmallDecodeCtx *s) ...@@ -440,12 +440,12 @@ static void decode_mclms(WmallDecodeCtx *s)
static int decode_cdlms(WmallDecodeCtx *s) static int decode_cdlms(WmallDecodeCtx *s)
{ {
int c, i; int c, i;
int cdlms_send_coef = get_bits1(&s->gb); int cdlms_send_coef = bitstream_read_bit(&s->bc);
for (c = 0; c < s->num_channels; c++) { for (c = 0; c < s->num_channels; c++) {
s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1; s->cdlms_ttl[c] = bitstream_read(&s->bc, 3) + 1;
for (i = 0; i < s->cdlms_ttl[c]; i++) { for (i = 0; i < s->cdlms_ttl[c]; i++) {
s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8; s->cdlms[c][i].order = (bitstream_read(&s->bc, 7) + 1) * 8;
if (s->cdlms[c][i].order > MAX_ORDER) { if (s->cdlms[c][i].order > MAX_ORDER) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"Order[%d][%d] %d > max (%d), not supported\n", "Order[%d][%d] %d > max (%d), not supported\n",
...@@ -456,7 +456,7 @@ static int decode_cdlms(WmallDecodeCtx *s) ...@@ -456,7 +456,7 @@ static int decode_cdlms(WmallDecodeCtx *s)
} }
for (i = 0; i < s->cdlms_ttl[c]; i++) for (i = 0; i < s->cdlms_ttl[c]; i++)
s->cdlms[c][i].scaling = get_bits(&s->gb, 4); s->cdlms[c][i].scaling = bitstream_read(&s->bc, 4);
if (cdlms_send_coef) { if (cdlms_send_coef) {
for (i = 0; i < s->cdlms_ttl[c]; i++) { for (i = 0; i < s->cdlms_ttl[c]; i++) {
...@@ -464,18 +464,18 @@ static int decode_cdlms(WmallDecodeCtx *s) ...@@ -464,18 +464,18 @@ static int decode_cdlms(WmallDecodeCtx *s)
cbits = av_log2(s->cdlms[c][i].order); cbits = av_log2(s->cdlms[c][i].order);
if ((1 << cbits) < s->cdlms[c][i].order) if ((1 << cbits) < s->cdlms[c][i].order)
cbits++; cbits++;
s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1; s->cdlms[c][i].coefsend = bitstream_read(&s->bc, cbits) + 1;
cbits = av_log2(s->cdlms[c][i].scaling + 1); cbits = av_log2(s->cdlms[c][i].scaling + 1);
if ((1 << cbits) < s->cdlms[c][i].scaling + 1) if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
cbits++; cbits++;
s->cdlms[c][i].bitsend = get_bitsz(&s->gb, cbits) + 2; s->cdlms[c][i].bitsend = bitstream_read(&s->bc, cbits) + 2;
shift_l = 32 - s->cdlms[c][i].bitsend; shift_l = 32 - s->cdlms[c][i].bitsend;
shift_r = 32 - s->cdlms[c][i].scaling - 2; shift_r = 32 - s->cdlms[c][i].scaling - 2;
for (j = 0; j < s->cdlms[c][i].coefsend; j++) for (j = 0; j < s->cdlms[c][i].coefsend; j++)
s->cdlms[c][i].coefs[j] = s->cdlms[c][i].coefs[j] =
(get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r; (bitstream_read(&s->bc, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
} }
} }
} }
...@@ -487,9 +487,9 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size) ...@@ -487,9 +487,9 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
{ {
int i = 0; int i = 0;
unsigned int ave_mean; unsigned int ave_mean;
s->transient[ch] = get_bits1(&s->gb); s->transient[ch] = bitstream_read_bit(&s->bc);
if (s->transient[ch]) { if (s->transient[ch]) {
s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size)); s->transient_pos[ch] = bitstream_read(&s->bc, av_log2(tile_size));
if (s->transient_pos[ch]) if (s->transient_pos[ch])
s->transient[ch] = 0; s->transient[ch] = 0;
s->channel[ch].transient_counter = s->channel[ch].transient_counter =
...@@ -498,33 +498,33 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size) ...@@ -498,33 +498,33 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
s->transient[ch] = 1; s->transient[ch] = 1;
if (s->seekable_tile) { if (s->seekable_tile) {
ave_mean = get_bits(&s->gb, s->bits_per_sample); ave_mean = bitstream_read(&s->bc, s->bits_per_sample);
s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1); s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
} }
if (s->seekable_tile) { if (s->seekable_tile) {
if (s->do_inter_ch_decorr) if (s->do_inter_ch_decorr)
s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1); s->channel_residues[ch][0] = bitstream_read_signed(&s->bc, s->bits_per_sample + 1);
else else
s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample); s->channel_residues[ch][0] = bitstream_read_signed(&s->bc, s->bits_per_sample);
i++; i++;
} }
for (; i < tile_size; i++) { for (; i < tile_size; i++) {
int quo = 0, rem, rem_bits, residue; int quo = 0, rem, rem_bits, residue;
while(get_bits1(&s->gb)) { while (bitstream_read_bit(&s->bc)) {
quo++; quo++;
if (get_bits_left(&s->gb) <= 0) if (bitstream_bits_left(&s->bc) <= 0)
return -1; return -1;
} }
if (quo >= 32) if (quo >= 32)
quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1); quo += bitstream_read(&s->bc, bitstream_read(&s->bc, 5) + 1);
ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1); ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
if (ave_mean <= 1) if (ave_mean <= 1)
residue = quo; residue = quo;
else { else {
rem_bits = av_ceil_log2(ave_mean); rem_bits = av_ceil_log2(ave_mean);
rem = rem_bits ? get_bits_long(&s->gb, rem_bits) : 0; rem = rem_bits ? bitstream_read(&s->bc, rem_bits) : 0;
residue = (quo << rem_bits) + rem; residue = (quo << rem_bits) + rem;
} }
...@@ -545,13 +545,13 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size) ...@@ -545,13 +545,13 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
static void decode_lpc(WmallDecodeCtx *s) static void decode_lpc(WmallDecodeCtx *s)
{ {
int ch, i, cbits; int ch, i, cbits;
s->lpc_order = get_bits(&s->gb, 5) + 1; s->lpc_order = bitstream_read(&s->bc, 5) + 1;
s->lpc_scaling = get_bits(&s->gb, 4); s->lpc_scaling = bitstream_read(&s->bc, 4);
s->lpc_intbits = get_bits(&s->gb, 3) + 1; s->lpc_intbits = bitstream_read(&s->bc, 3) + 1;
cbits = s->lpc_scaling + s->lpc_intbits; cbits = s->lpc_scaling + s->lpc_intbits;
for (ch = 0; ch < s->num_channels; ch++) for (ch = 0; ch < s->num_channels; ch++)
for (i = 0; i < s->lpc_order; i++) for (i = 0; i < s->lpc_order; i++)
s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits); s->lpc_coefs[ch][i] = bitstream_read_signed(&s->bc, cbits);
} }
static void clear_codec_buffers(WmallDecodeCtx *s) static void clear_codec_buffers(WmallDecodeCtx *s)
...@@ -846,7 +846,7 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -846,7 +846,7 @@ static int decode_subframe(WmallDecodeCtx *s)
int total_samples = s->samples_per_frame * s->num_channels; int total_samples = s->samples_per_frame * s->num_channels;
int i, j, rawpcm_tile, padding_zeroes, res; int i, j, rawpcm_tile, padding_zeroes, res;
s->subframe_offset = get_bits_count(&s->gb); s->subframe_offset = bitstream_tell(&s->bc);
/* reset channel context and find the next block offset and size /* reset channel context and find the next block offset and size
== the next block of the channel with the smallest number of == the next block of the channel with the smallest number of
...@@ -883,18 +883,18 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -883,18 +883,18 @@ static int decode_subframe(WmallDecodeCtx *s)
s->parsed_all_subframes = 1; s->parsed_all_subframes = 1;
s->seekable_tile = get_bits1(&s->gb); s->seekable_tile = bitstream_read_bit(&s->bc);
if (s->seekable_tile) { if (s->seekable_tile) {
clear_codec_buffers(s); clear_codec_buffers(s);
s->do_arith_coding = get_bits1(&s->gb); s->do_arith_coding = bitstream_read_bit(&s->bc);
if (s->do_arith_coding) { if (s->do_arith_coding) {
avpriv_request_sample(s->avctx, "Arithmetic coding"); avpriv_request_sample(s->avctx, "Arithmetic coding");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
s->do_ac_filter = get_bits1(&s->gb); s->do_ac_filter = bitstream_read_bit(&s->bc);
s->do_inter_ch_decorr = get_bits1(&s->gb); s->do_inter_ch_decorr = bitstream_read_bit(&s->bc);
s->do_mclms = get_bits1(&s->gb); s->do_mclms = bitstream_read_bit(&s->bc);
if (s->do_ac_filter) if (s->do_ac_filter)
decode_ac_filter(s); decode_ac_filter(s);
...@@ -904,8 +904,8 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -904,8 +904,8 @@ static int decode_subframe(WmallDecodeCtx *s)
if ((res = decode_cdlms(s)) < 0) if ((res = decode_cdlms(s)) < 0)
return res; return res;
s->movave_scaling = get_bits(&s->gb, 3); s->movave_scaling = bitstream_read(&s->bc, 3);
s->quant_stepsize = get_bits(&s->gb, 8) + 1; s->quant_stepsize = bitstream_read(&s->bc, 8) + 1;
reset_codec(s); reset_codec(s);
} else if (!s->cdlms[0][0].order) { } else if (!s->cdlms[0][0].order) {
...@@ -915,18 +915,18 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -915,18 +915,18 @@ static int decode_subframe(WmallDecodeCtx *s)
return -1; return -1;
} }
rawpcm_tile = get_bits1(&s->gb); rawpcm_tile = bitstream_read_bit(&s->bc);
for (i = 0; i < s->num_channels; i++) for (i = 0; i < s->num_channels; i++)
s->is_channel_coded[i] = 1; s->is_channel_coded[i] = 1;
if (!rawpcm_tile) { if (!rawpcm_tile) {
for (i = 0; i < s->num_channels; i++) for (i = 0; i < s->num_channels; i++)
s->is_channel_coded[i] = get_bits1(&s->gb); s->is_channel_coded[i] = bitstream_read_bit(&s->bc);
if (s->bV3RTM) { if (s->bV3RTM) {
// LPC // LPC
s->do_lpc = get_bits1(&s->gb); s->do_lpc = bitstream_read_bit(&s->bc);
if (s->do_lpc) { if (s->do_lpc) {
decode_lpc(s); decode_lpc(s);
avpriv_request_sample(s->avctx, "Expect wrong output since " avpriv_request_sample(s->avctx, "Expect wrong output since "
...@@ -937,8 +937,8 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -937,8 +937,8 @@ static int decode_subframe(WmallDecodeCtx *s)
} }
if (get_bits1(&s->gb)) if (bitstream_read_bit(&s->bc))
padding_zeroes = get_bits(&s->gb, 5); padding_zeroes = bitstream_read(&s->bc, 5);
else else
padding_zeroes = 0; padding_zeroes = 0;
...@@ -951,10 +951,10 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -951,10 +951,10 @@ static int decode_subframe(WmallDecodeCtx *s)
} }
ff_dlog(s->avctx, "RAWPCM %d bits per sample. " ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
"total %d bits, remain=%d\n", bits, "total %d bits, remain=%d\n", bits,
bits * s->num_channels * subframe_len, get_bits_count(&s->gb)); bits * s->num_channels * subframe_len, bitstream_tell(&s->bc));
for (i = 0; i < s->num_channels; i++) for (i = 0; i < s->num_channels; i++)
for (j = 0; j < subframe_len; j++) for (j = 0; j < subframe_len; j++)
s->channel_coeffs[i][j] = get_sbits(&s->gb, bits); s->channel_coeffs[i][j] = bitstream_read_signed(&s->bc, bits);
} else { } else {
for (i = 0; i < s->num_channels; i++) for (i = 0; i < s->num_channels; i++)
if (s->is_channel_coded[i]) { if (s->is_channel_coded[i]) {
...@@ -1015,7 +1015,7 @@ static int decode_subframe(WmallDecodeCtx *s) ...@@ -1015,7 +1015,7 @@ static int decode_subframe(WmallDecodeCtx *s)
*/ */
static int decode_frame(WmallDecodeCtx *s) static int decode_frame(WmallDecodeCtx *s)
{ {
GetBitContext* gb = &s->gb; BitstreamContext *bc = &s->bc;
int more_frames = 0, len = 0, i, ret; int more_frames = 0, len = 0, i, ret;
s->frame->nb_samples = s->samples_per_frame; s->frame->nb_samples = s->samples_per_frame;
...@@ -1033,7 +1033,7 @@ static int decode_frame(WmallDecodeCtx *s) ...@@ -1033,7 +1033,7 @@ static int decode_frame(WmallDecodeCtx *s)
/* get frame length */ /* get frame length */
if (s->len_prefix) if (s->len_prefix)
len = get_bits(gb, s->log2_frame_size); len = bitstream_read(bc, s->log2_frame_size);
/* decode tile information */ /* decode tile information */
if (decode_tilehdr(s)) { if (decode_tilehdr(s)) {
...@@ -1043,22 +1043,22 @@ static int decode_frame(WmallDecodeCtx *s) ...@@ -1043,22 +1043,22 @@ static int decode_frame(WmallDecodeCtx *s)
/* read drc info */ /* read drc info */
if (s->dynamic_range_compression) if (s->dynamic_range_compression)
s->drc_gain = get_bits(gb, 8); s->drc_gain = bitstream_read(bc, 8);
/* no idea what these are for, might be the number of samples /* no idea what these are for, might be the number of samples
that need to be skipped at the beginning or end of a stream */ that need to be skipped at the beginning or end of a stream */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
int av_unused skip; int av_unused skip;
/* usually true for the first frame */ /* usually true for the first frame */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
ff_dlog(s->avctx, "start skip: %i\n", skip); ff_dlog(s->avctx, "start skip: %i\n", skip);
} }
/* sometimes true for the last frame */ /* sometimes true for the last frame */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
ff_dlog(s->avctx, "end skip: %i\n", skip); ff_dlog(s->avctx, "end skip: %i\n", skip);
} }
...@@ -1085,22 +1085,22 @@ static int decode_frame(WmallDecodeCtx *s) ...@@ -1085,22 +1085,22 @@ static int decode_frame(WmallDecodeCtx *s)
s->skip_frame = 0; s->skip_frame = 0;
if (s->len_prefix) { if (s->len_prefix) {
if (len != (get_bits_count(gb) - s->frame_offset) + 2) { if (len != (bitstream_tell(bc) - s->frame_offset) + 2) {
/* FIXME: not sure if this is always an error */ /* FIXME: not sure if this is always an error */
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"frame[%"PRIu32"] would have to skip %i bits\n", "frame[%"PRIu32"] would have to skip %i bits\n",
s->frame_num, s->frame_num,
len - (get_bits_count(gb) - s->frame_offset) - 1); len - (bitstream_tell(bc) - s->frame_offset) - 1);
s->packet_loss = 1; s->packet_loss = 1;
return 0; return 0;
} }
/* skip the rest of the frame data */ /* skip the rest of the frame data */
skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1); bitstream_skip(bc, len - (bitstream_tell(bc) - s->frame_offset) - 1);
} }
/* decode trailer bit */ /* decode trailer bit */
more_frames = get_bits1(gb); more_frames = bitstream_read_bit(bc);
++s->frame_num; ++s->frame_num;
return more_frames; return more_frames;
} }
...@@ -1108,22 +1108,22 @@ static int decode_frame(WmallDecodeCtx *s) ...@@ -1108,22 +1108,22 @@ static int decode_frame(WmallDecodeCtx *s)
/** /**
* @brief Calculate remaining input buffer length. * @brief Calculate remaining input buffer length.
* @param s codec context * @param s codec context
* @param gb bitstream reader context * @param bc bitstream reader context
* @return remaining size in bits * @return remaining size in bits
*/ */
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb) static int remaining_bits(WmallDecodeCtx *s, BitstreamContext *bc)
{ {
return s->buf_bit_size - get_bits_count(gb); return s->buf_bit_size - bitstream_tell(bc);
} }
/** /**
* @brief Fill the bit reservoir with a (partial) frame. * @brief Fill the bit reservoir with a (partial) frame.
* @param s codec context * @param s codec context
* @param gb bitstream reader context * @param bc bitstream reader context
* @param len length of the partial frame * @param len length of the partial frame
* @param append decides whether to reset the buffer or not * @param append decides whether to reset the buffer or not
*/ */
static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len, static void save_bits(WmallDecodeCtx *s, BitstreamContext *bc, int len,
int append) int append)
{ {
int buflen; int buflen;
...@@ -1134,7 +1134,7 @@ static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len, ...@@ -1134,7 +1134,7 @@ static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
and skipped later so that a fast byte copy is possible */ and skipped later so that a fast byte copy is possible */
if (!append) { if (!append) {
s->frame_offset = get_bits_count(gb) & 7; s->frame_offset = bitstream_tell(bc) & 7;
s->num_saved_bits = s->frame_offset; s->num_saved_bits = s->frame_offset;
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE); init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
} }
...@@ -1149,29 +1149,29 @@ static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len, ...@@ -1149,29 +1149,29 @@ static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
s->num_saved_bits += len; s->num_saved_bits += len;
if (!append) { if (!append) {
avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3),
s->num_saved_bits); s->num_saved_bits);
} else { } else {
int align = 8 - (get_bits_count(gb) & 7); int align = 8 - (bitstream_tell(bc) & 7);
align = FFMIN(align, len); align = FFMIN(align, len);
put_bits(&s->pb, align, get_bits(gb, align)); put_bits(&s->pb, align, bitstream_read(bc, align));
len -= align; len -= align;
avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len); avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3), len);
} }
skip_bits_long(gb, len); bitstream_skip(bc, len);
tmp = s->pb; tmp = s->pb;
flush_put_bits(&tmp); flush_put_bits(&tmp);
init_get_bits(&s->gb, s->frame_data, s->num_saved_bits); bitstream_init(&s->bc, s->frame_data, s->num_saved_bits);
skip_bits(&s->gb, s->frame_offset); bitstream_skip(&s->bc, s->frame_offset);
} }
static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
AVPacket* avpkt) AVPacket* avpkt)
{ {
WmallDecodeCtx *s = avctx->priv_data; WmallDecodeCtx *s = avctx->priv_data;
GetBitContext* gb = &s->pgb; BitstreamContext *bc = &s->pbc;
const uint8_t* buf = avpkt->data; const uint8_t* buf = avpkt->data;
int buf_size = avpkt->size; int buf_size = avpkt->size;
int num_bits_prev_frame, packet_sequence_number, spliced_packet; int num_bits_prev_frame, packet_sequence_number, spliced_packet;
...@@ -1190,15 +1190,15 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, ...@@ -1190,15 +1190,15 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
s->buf_bit_size = buf_size << 3; s->buf_bit_size = buf_size << 3;
/* parse packet header */ /* parse packet header */
init_get_bits(gb, buf, s->buf_bit_size); bitstream_init(bc, buf, s->buf_bit_size);
packet_sequence_number = get_bits(gb, 4); packet_sequence_number = bitstream_read(bc, 4);
skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently unused bitstream_skip(bc, 1); // Skip seekable_frame_in_packet, currently ununused
spliced_packet = get_bits1(gb); spliced_packet = bitstream_read_bit(bc);
if (spliced_packet) if (spliced_packet)
avpriv_request_sample(avctx, "Bitstream splicing"); avpriv_request_sample(avctx, "Bitstream splicing");
/* get number of bits that need to be added to the previous frame */ /* get number of bits that need to be added to the previous frame */
num_bits_prev_frame = get_bits(gb, s->log2_frame_size); num_bits_prev_frame = bitstream_read(bc, s->log2_frame_size);
/* check for packet loss */ /* check for packet loss */
if (!s->packet_loss && if (!s->packet_loss &&
...@@ -1211,7 +1211,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, ...@@ -1211,7 +1211,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
s->packet_sequence_number = packet_sequence_number; s->packet_sequence_number = packet_sequence_number;
if (num_bits_prev_frame > 0) { if (num_bits_prev_frame > 0) {
int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb); int remaining_packet_bits = s->buf_bit_size - bitstream_tell(bc);
if (num_bits_prev_frame >= remaining_packet_bits) { if (num_bits_prev_frame >= remaining_packet_bits) {
num_bits_prev_frame = remaining_packet_bits; num_bits_prev_frame = remaining_packet_bits;
s->packet_done = 1; s->packet_done = 1;
...@@ -1219,7 +1219,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, ...@@ -1219,7 +1219,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
/* Append the previous frame data to the remaining data from the /* Append the previous frame data to the remaining data from the
* previous packet to create a full frame. */ * previous packet to create a full frame. */
save_bits(s, gb, num_bits_prev_frame, 1); save_bits(s, bc, num_bits_prev_frame, 1);
/* decode the cross packet frame if it is valid */ /* decode the cross packet frame if it is valid */
if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss) if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
...@@ -1241,16 +1241,16 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, ...@@ -1241,16 +1241,16 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
int frame_size; int frame_size;
s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3; s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
init_get_bits(gb, avpkt->data, s->buf_bit_size); bitstream_init(bc, avpkt->data, s->buf_bit_size);
skip_bits(gb, s->packet_offset); bitstream_skip(bc, s->packet_offset);
if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size && if (s->len_prefix && remaining_bits(s, bc) > s->log2_frame_size &&
(frame_size = show_bits(gb, s->log2_frame_size)) && (frame_size = bitstream_peek(bc, s->log2_frame_size)) &&
frame_size <= remaining_bits(s, gb)) { frame_size <= remaining_bits(s, bc)) {
save_bits(s, gb, frame_size, 0); save_bits(s, bc, frame_size, 0);
s->packet_done = !decode_frame(s); s->packet_done = !decode_frame(s);
} else if (!s->len_prefix } else if (!s->len_prefix
&& s->num_saved_bits > get_bits_count(&s->gb)) { && s->num_saved_bits > bitstream_tell(&s->bc)) {
/* when the frames do not have a length prefix, we don't know the /* when the frames do not have a length prefix, we don't know the
* compressed length of the individual frames however, we know what * compressed length of the individual frames however, we know what
* part of a new packet belongs to the previous frame therefore we * part of a new packet belongs to the previous frame therefore we
...@@ -1264,18 +1264,18 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, ...@@ -1264,18 +1264,18 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
} }
if (s->packet_done && !s->packet_loss && if (s->packet_done && !s->packet_loss &&
remaining_bits(s, gb) > 0) { remaining_bits(s, bc) > 0) {
/* save the rest of the data so that it can be decoded /* save the rest of the data so that it can be decoded
* with the next packet */ * with the next packet */
save_bits(s, gb, remaining_bits(s, gb), 0); save_bits(s, bc, remaining_bits(s, bc), 0);
} }
*got_frame_ptr = s->frame->nb_samples > 0; *got_frame_ptr = s->frame->nb_samples > 0;
av_frame_move_ref(data, s->frame); av_frame_move_ref(data, s->frame);
s->packet_offset = get_bits_count(gb) & 7; s->packet_offset = bitstream_tell(bc) & 7;
return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3; return (s->packet_loss) ? AVERROR_INVALIDDATA : bitstream_tell(bc) >> 3;
} }
static void flush(AVCodecContext *avctx) static void flush(AVCodecContext *avctx)
......
...@@ -91,9 +91,10 @@ ...@@ -91,9 +91,10 @@
#include "libavutil/float_dsp.h" #include "libavutil/float_dsp.h"
#include "libavutil/intfloat.h" #include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "internal.h" #include "internal.h"
#include "get_bits.h"
#include "put_bits.h" #include "put_bits.h"
#include "wmaprodata.h" #include "wmaprodata.h"
#include "sinewin.h" #include "sinewin.h"
...@@ -197,7 +198,7 @@ typedef struct WMAProDecodeCtx { ...@@ -197,7 +198,7 @@ typedef struct WMAProDecodeCtx {
int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
/* packet decode state */ /* packet decode state */
GetBitContext pgb; ///< bitstream reader context for the packet BitstreamContext pbc; ///< bitstream reader context for the packet
int next_packet_start; ///< start offset of the next wma packet in the demuxer packet int next_packet_start; ///< start offset of the next wma packet in the demuxer packet
uint8_t packet_offset; ///< frame offset in the packet uint8_t packet_offset; ///< frame offset in the packet
uint8_t packet_sequence_number; ///< current packet number uint8_t packet_sequence_number; ///< current packet number
...@@ -209,7 +210,7 @@ typedef struct WMAProDecodeCtx { ...@@ -209,7 +210,7 @@ typedef struct WMAProDecodeCtx {
/* frame decode state */ /* frame decode state */
uint32_t frame_num; ///< current frame number (not used for decoding) uint32_t frame_num; ///< current frame number (not used for decoding)
GetBitContext gb; ///< bitstream reader context BitstreamContext bc; ///< bitstream reader context
int buf_bit_size; ///< buffer size in bits int buf_bit_size; ///< buffer size in bits
uint8_t drc_gain; ///< gain for the DRC tool uint8_t drc_gain; ///< gain for the DRC tool
int8_t skip_frame; ///< skip output step int8_t skip_frame; ///< skip output step
...@@ -495,10 +496,11 @@ static int decode_subframe_length(WMAProDecodeCtx *s, int offset) ...@@ -495,10 +496,11 @@ static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
/** 1 bit indicates if the subframe is of maximum length */ /** 1 bit indicates if the subframe is of maximum length */
if (s->max_subframe_len_bit) { if (s->max_subframe_len_bit) {
if (get_bits1(&s->gb)) if (bitstream_read_bit(&s->bc))
frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1); frame_len_shift = 1 + bitstream_read(&s->bc,
s->subframe_len_bits - 1);
} else } else
frame_len_shift = get_bits(&s->gb, s->subframe_len_bits); frame_len_shift = bitstream_read(&s->bc, s->subframe_len_bits);
subframe_len = s->samples_per_frame >> frame_len_shift; subframe_len = s->samples_per_frame >> frame_len_shift;
...@@ -551,7 +553,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s) ...@@ -551,7 +553,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
for (c = 0; c < s->avctx->channels; c++) for (c = 0; c < s->avctx->channels; c++)
s->channel[c].num_subframes = 0; s->channel[c].num_subframes = 0;
if (s->max_num_subframes == 1 || get_bits1(&s->gb)) if (s->max_num_subframes == 1 || bitstream_read_bit(&s->bc))
fixed_channel_layout = 1; fixed_channel_layout = 1;
/** loop until the frame data is split between the subframes */ /** loop until the frame data is split between the subframes */
...@@ -565,7 +567,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s) ...@@ -565,7 +567,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
(min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
contains_subframe[c] = 1; contains_subframe[c] = 1;
else else
contains_subframe[c] = get_bits1(&s->gb); contains_subframe[c] = bitstream_read_bit(&s->bc);
} else } else
contains_subframe[c] = 0; contains_subframe[c] = 0;
} }
...@@ -633,11 +635,11 @@ static void decode_decorrelation_matrix(WMAProDecodeCtx *s, ...@@ -633,11 +635,11 @@ static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
s->avctx->channels * sizeof(*chgroup->decorrelation_matrix)); s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++) for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
rotation_offset[i] = get_bits(&s->gb, 6); rotation_offset[i] = bitstream_read(&s->bc, 6);
for (i = 0; i < chgroup->num_channels; i++) for (i = 0; i < chgroup->num_channels; i++)
chgroup->decorrelation_matrix[chgroup->num_channels * i + i] = chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
get_bits1(&s->gb) ? 1.0 : -1.0; bitstream_read_bit(&s->bc) ? 1.0 : -1.0;
for (i = 1; i < chgroup->num_channels; i++) { for (i = 1; i < chgroup->num_channels; i++) {
int x; int x;
...@@ -686,7 +688,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s) ...@@ -686,7 +688,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
if (s->avctx->channels > 1) { if (s->avctx->channels > 1) {
int remaining_channels = s->channels_for_cur_subframe; int remaining_channels = s->channels_for_cur_subframe;
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
avpriv_request_sample(s->avctx, avpriv_request_sample(s->avctx,
"Channel transform bit"); "Channel transform bit");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
...@@ -704,7 +706,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s) ...@@ -704,7 +706,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
for (i = 0; i < s->channels_for_cur_subframe; i++) { for (i = 0; i < s->channels_for_cur_subframe; i++) {
int channel_idx = s->channel_indexes_for_cur_subframe[i]; int channel_idx = s->channel_indexes_for_cur_subframe[i];
if (!s->channel[channel_idx].grouped if (!s->channel[channel_idx].grouped
&& get_bits1(&s->gb)) { && bitstream_read_bit(&s->bc)) {
++chgroup->num_channels; ++chgroup->num_channels;
s->channel[channel_idx].grouped = 1; s->channel[channel_idx].grouped = 1;
*channel_data++ = s->channel[channel_idx].coeffs; *channel_data++ = s->channel[channel_idx].coeffs;
...@@ -722,8 +724,8 @@ static int decode_channel_transform(WMAProDecodeCtx* s) ...@@ -722,8 +724,8 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
/** decode transform type */ /** decode transform type */
if (chgroup->num_channels == 2) { if (chgroup->num_channels == 2) {
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
avpriv_request_sample(s->avctx, avpriv_request_sample(s->avctx,
"Unknown channel transform type"); "Unknown channel transform type");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
...@@ -744,9 +746,9 @@ static int decode_channel_transform(WMAProDecodeCtx* s) ...@@ -744,9 +746,9 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
} }
} }
} else if (chgroup->num_channels > 2) { } else if (chgroup->num_channels > 2) {
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
chgroup->transform = 1; chgroup->transform = 1;
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
decode_decorrelation_matrix(s, chgroup); decode_decorrelation_matrix(s, chgroup);
} else { } else {
/** FIXME: more than 6 coupled channels not supported */ /** FIXME: more than 6 coupled channels not supported */
...@@ -765,11 +767,11 @@ static int decode_channel_transform(WMAProDecodeCtx* s) ...@@ -765,11 +767,11 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
/** decode transform on / off */ /** decode transform on / off */
if (chgroup->transform) { if (chgroup->transform) {
if (!get_bits1(&s->gb)) { if (!bitstream_read_bit(&s->bc)) {
int i; int i;
/** transform can be enabled for individual bands */ /** transform can be enabled for individual bands */
for (i = 0; i < s->num_bands; i++) { for (i = 0; i < s->num_bands; i++) {
chgroup->transform_band[i] = get_bits1(&s->gb); chgroup->transform_band[i] = bitstream_read_bit(&s->bc);
} }
} else { } else {
memset(chgroup->transform_band, 1, s->num_bands); memset(chgroup->transform_band, 1, s->num_bands);
...@@ -809,7 +811,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c) ...@@ -809,7 +811,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c)
ff_dlog(s->avctx, "decode coefficients for channel %i\n", c); ff_dlog(s->avctx, "decode coefficients for channel %i\n", c);
vlctable = get_bits1(&s->gb); vlctable = bitstream_read_bit(&s->bc);
vlc = &coef_vlc[vlctable]; vlc = &coef_vlc[vlctable];
if (vlctable) { if (vlctable) {
...@@ -828,19 +830,19 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c) ...@@ -828,19 +830,19 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c)
int i; int i;
unsigned int idx; unsigned int idx;
idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH); idx = bitstream_read_vlc(&s->bc, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
if (idx == HUFF_VEC4_SIZE - 1) { if (idx == HUFF_VEC4_SIZE - 1) {
for (i = 0; i < 4; i += 2) { for (i = 0; i < 4; i += 2) {
idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH); idx = bitstream_read_vlc(&s->bc, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
if (idx == HUFF_VEC2_SIZE - 1) { if (idx == HUFF_VEC2_SIZE - 1) {
uint32_t v0, v1; uint32_t v0, v1;
v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH); v0 = bitstream_read_vlc(&s->bc, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
if (v0 == HUFF_VEC1_SIZE - 1) if (v0 == HUFF_VEC1_SIZE - 1)
v0 += ff_wma_get_large_val(&s->gb); v0 += ff_wma_get_large_val(&s->bc);
v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH); v1 = bitstream_read_vlc(&s->bc, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
if (v1 == HUFF_VEC1_SIZE - 1) if (v1 == HUFF_VEC1_SIZE - 1)
v1 += ff_wma_get_large_val(&s->gb); v1 += ff_wma_get_large_val(&s->bc);
vals[i ] = av_float2int(v0); vals[i ] = av_float2int(v0);
vals[i+1] = av_float2int(v1); vals[i+1] = av_float2int(v1);
} else { } else {
...@@ -858,7 +860,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c) ...@@ -858,7 +860,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c)
/** decode sign */ /** decode sign */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if (vals[i]) { if (vals[i]) {
uint32_t sign = get_bits1(&s->gb) - 1; uint32_t sign = bitstream_read_bit(&s->bc) - 1;
AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31); AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
num_zeros = 0; num_zeros = 0;
} else { } else {
...@@ -875,7 +877,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c) ...@@ -875,7 +877,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c)
if (cur_coeff < s->subframe_len) { if (cur_coeff < s->subframe_len) {
memset(&ci->coeffs[cur_coeff], 0, memset(&ci->coeffs[cur_coeff], 0,
sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff)); sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc, if (ff_wma_run_level_decode(s->avctx, &s->bc, vlc,
level, run, 1, ci->coeffs, level, run, 1, ci->coeffs,
cur_coeff, s->subframe_len, cur_coeff, s->subframe_len,
s->subframe_len, s->esc_len, 0)) s->subframe_len, s->esc_len, 0))
...@@ -918,15 +920,14 @@ static int decode_scale_factors(WMAProDecodeCtx* s) ...@@ -918,15 +920,14 @@ static int decode_scale_factors(WMAProDecodeCtx* s)
s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++]; s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
} }
if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) { if (!s->channel[c].cur_subframe || bitstream_read_bit(&s->bc)) {
if (!s->channel[c].reuse_sf) { if (!s->channel[c].reuse_sf) {
int val; int val;
/** decode DPCM coded scale factors */ /** decode DPCM coded scale factors */
s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1; s->channel[c].scale_factor_step = bitstream_read(&s->bc, 2) + 1;
val = 45 / s->channel[c].scale_factor_step; val = 45 / s->channel[c].scale_factor_step;
for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) { for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60; val += bitstream_read_vlc(&s->bc, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
*sf = val; *sf = val;
} }
} else { } else {
...@@ -938,10 +939,10 @@ static int decode_scale_factors(WMAProDecodeCtx* s) ...@@ -938,10 +939,10 @@ static int decode_scale_factors(WMAProDecodeCtx* s)
int val; int val;
int sign; int sign;
idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH); idx = bitstream_read_vlc(&s->bc, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
if (!idx) { if (!idx) {
uint32_t code = get_bits(&s->gb, 14); uint32_t code = bitstream_read(&s->bc, 14);
val = code >> 6; val = code >> 6;
sign = (code & 1) - 1; sign = (code & 1) - 1;
skip = (code & 0x3f) >> 1; skip = (code & 0x3f) >> 1;
...@@ -950,7 +951,7 @@ static int decode_scale_factors(WMAProDecodeCtx* s) ...@@ -950,7 +951,7 @@ static int decode_scale_factors(WMAProDecodeCtx* s)
} else { } else {
skip = scale_rl_run[idx]; skip = scale_rl_run[idx];
val = scale_rl_level[idx]; val = scale_rl_level[idx];
sign = get_bits1(&s->gb)-1; sign = bitstream_read_bit(&s->bc)-1;
} }
i += skip; i += skip;
...@@ -1077,7 +1078,7 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1077,7 +1078,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
int transmit_coeffs = 0; int transmit_coeffs = 0;
int cur_subwoofer_cutoff; int cur_subwoofer_cutoff;
s->subframe_offset = get_bits_count(&s->gb); s->subframe_offset = bitstream_tell(&s->bc);
/** reset channel context and find the next block offset and size /** reset channel context and find the next block offset and size
== the next block of the channel with the smallest number of == the next block of the channel with the smallest number of
...@@ -1141,25 +1142,25 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1141,25 +1142,25 @@ static int decode_subframe(WMAProDecodeCtx *s)
s->esc_len = av_log2(s->subframe_len - 1) + 1; s->esc_len = av_log2(s->subframe_len - 1) + 1;
/** skip extended header if any */ /** skip extended header if any */
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
int num_fill_bits; int num_fill_bits;
if (!(num_fill_bits = get_bits(&s->gb, 2))) { if (!(num_fill_bits = bitstream_read(&s->bc, 2))) {
int len = get_bits(&s->gb, 4); int len = bitstream_read(&s->bc, 4);
num_fill_bits = get_bitsz(&s->gb, len) + 1; num_fill_bits = bitstream_read(&s->bc, len) + 1;
} }
if (num_fill_bits >= 0) { if (num_fill_bits >= 0) {
if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) { if (bitstream_tell(&s->bc) + num_fill_bits > s->num_saved_bits) {
av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n"); av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
skip_bits_long(&s->gb, num_fill_bits); bitstream_skip(&s->bc, num_fill_bits);
} }
} }
/** no idea for what the following bit is used */ /** no idea for what the following bit is used */
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
avpriv_request_sample(s->avctx, "Reserved bit"); avpriv_request_sample(s->avctx, "Reserved bit");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
...@@ -1171,7 +1172,7 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1171,7 +1172,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
for (i = 0; i < s->channels_for_cur_subframe; i++) { for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i]; int c = s->channel_indexes_for_cur_subframe[i];
if ((s->channel[c].transmit_coefs = get_bits1(&s->gb))) if ((s->channel[c].transmit_coefs = bitstream_read_bit(&s->bc)))
transmit_coeffs = 1; transmit_coeffs = 1;
} }
...@@ -1180,11 +1181,11 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1180,11 +1181,11 @@ static int decode_subframe(WMAProDecodeCtx *s)
int quant_step = 90 * s->bits_per_sample >> 4; int quant_step = 90 * s->bits_per_sample >> 4;
/** decode number of vector coded coefficients */ /** decode number of vector coded coefficients */
if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) { if ((s->transmit_num_vec_coeffs = bitstream_read_bit(&s->bc))) {
int num_bits = av_log2((s->subframe_len + 3)/4) + 1; int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
for (i = 0; i < s->channels_for_cur_subframe; i++) { for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i]; int c = s->channel_indexes_for_cur_subframe[i];
int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2; int num_vec_coeffs = bitstream_read(&s->bc, num_bits) << 2;
if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) { if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) {
av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs); av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
...@@ -1198,13 +1199,13 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1198,13 +1199,13 @@ static int decode_subframe(WMAProDecodeCtx *s)
} }
} }
/** decode quantization step */ /** decode quantization step */
step = get_sbits(&s->gb, 6); step = bitstream_read_signed(&s->bc, 6);
quant_step += step; quant_step += step;
if (step == -32 || step == 31) { if (step == -32 || step == 31) {
const int sign = (step == 31) - 1; const int sign = (step == 31) - 1;
int quant = 0; int quant = 0;
while (get_bits_count(&s->gb) + 5 < s->num_saved_bits && while (bitstream_tell(&s->bc) + 5 < s->num_saved_bits &&
(step = get_bits(&s->gb, 5)) == 31) { (step = bitstream_read(&s->bc, 5)) == 31) {
quant += 31; quant += 31;
} }
quant_step += ((quant + step) ^ sign) - sign; quant_step += ((quant + step) ^ sign) - sign;
...@@ -1218,13 +1219,13 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1218,13 +1219,13 @@ static int decode_subframe(WMAProDecodeCtx *s)
if (s->channels_for_cur_subframe == 1) { if (s->channels_for_cur_subframe == 1) {
s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step; s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
} else { } else {
int modifier_len = get_bits(&s->gb, 3); int modifier_len = bitstream_read(&s->bc, 3);
for (i = 0; i < s->channels_for_cur_subframe; i++) { for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i]; int c = s->channel_indexes_for_cur_subframe[i];
s->channel[c].quant_step = quant_step; s->channel[c].quant_step = quant_step;
if (get_bits1(&s->gb)) { if (bitstream_read_bit(&s->bc)) {
if (modifier_len) { if (modifier_len) {
s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1; s->channel[c].quant_step += bitstream_read(&s->bc, modifier_len) + 1;
} else } else
++s->channel[c].quant_step; ++s->channel[c].quant_step;
} }
...@@ -1237,13 +1238,13 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1237,13 +1238,13 @@ static int decode_subframe(WMAProDecodeCtx *s)
} }
ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n", ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
get_bits_count(&s->gb) - s->subframe_offset); bitstream_tell(&s->bc) - s->subframe_offset);
/** parse coefficients */ /** parse coefficients */
for (i = 0; i < s->channels_for_cur_subframe; i++) { for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i]; int c = s->channel_indexes_for_cur_subframe[i];
if (s->channel[c].transmit_coefs && if (s->channel[c].transmit_coefs &&
get_bits_count(&s->gb) < s->num_saved_bits) { bitstream_tell(&s->bc) < s->num_saved_bits) {
decode_coeffs(s, c); decode_coeffs(s, c);
} else } else
memset(s->channel[c].coeffs, 0, memset(s->channel[c].coeffs, 0,
...@@ -1251,7 +1252,7 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1251,7 +1252,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
} }
ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\n", ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
get_bits_count(&s->gb) - s->subframe_offset); bitstream_tell(&s->bc) - s->subframe_offset);
if (transmit_coeffs) { if (transmit_coeffs) {
FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS]; FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
...@@ -1309,14 +1310,14 @@ static int decode_subframe(WMAProDecodeCtx *s) ...@@ -1309,14 +1310,14 @@ static int decode_subframe(WMAProDecodeCtx *s)
static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr) static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
{ {
AVCodecContext *avctx = s->avctx; AVCodecContext *avctx = s->avctx;
GetBitContext* gb = &s->gb; BitstreamContext *bc = &s->bc;
int more_frames = 0; int more_frames = 0;
int len = 0; int len = 0;
int i, ret; int i, ret;
/** get frame length */ /** get frame length */
if (s->len_prefix) if (s->len_prefix)
len = get_bits(gb, s->log2_frame_size); len = bitstream_read(bc, s->log2_frame_size);
ff_dlog(s->avctx, "decoding frame with length %x\n", len); ff_dlog(s->avctx, "decoding frame with length %x\n", len);
...@@ -1327,40 +1328,40 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr) ...@@ -1327,40 +1328,40 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
} }
/** read postproc transform */ /** read postproc transform */
if (s->avctx->channels > 1 && get_bits1(gb)) { if (s->avctx->channels > 1 && bitstream_read_bit(bc)) {
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
for (i = 0; i < avctx->channels * avctx->channels; i++) for (i = 0; i < avctx->channels * avctx->channels; i++)
skip_bits(gb, 4); bitstream_skip(bc, 4);
} }
} }
/** read drc info */ /** read drc info */
if (s->dynamic_range_compression) { if (s->dynamic_range_compression) {
s->drc_gain = get_bits(gb, 8); s->drc_gain = bitstream_read(bc, 8);
ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain); ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
} }
/** no idea what these are for, might be the number of samples /** no idea what these are for, might be the number of samples
that need to be skipped at the beginning or end of a stream */ that need to be skipped at the beginning or end of a stream */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
int av_unused skip; int av_unused skip;
/** usually true for the first frame */ /** usually true for the first frame */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
ff_dlog(s->avctx, "start skip: %i\n", skip); ff_dlog(s->avctx, "start skip: %i\n", skip);
} }
/** sometimes true for the last frame */ /** sometimes true for the last frame */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
ff_dlog(s->avctx, "end skip: %i\n", skip); ff_dlog(s->avctx, "end skip: %i\n", skip);
} }
} }
ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n", ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
get_bits_count(gb) - s->frame_offset); bitstream_tell(bc) - s->frame_offset);
/** reset subframe states */ /** reset subframe states */
s->parsed_all_subframes = 0; s->parsed_all_subframes = 0;
...@@ -1407,25 +1408,25 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr) ...@@ -1407,25 +1408,25 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
} }
if (s->len_prefix) { if (s->len_prefix) {
if (len != (get_bits_count(gb) - s->frame_offset) + 2) { if (len != (bitstream_tell(bc) - s->frame_offset) + 2) {
/** FIXME: not sure if this is always an error */ /** FIXME: not sure if this is always an error */
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"frame[%"PRIu32"] would have to skip %i bits\n", "frame[%"PRIu32"] would have to skip %i bits\n",
s->frame_num, s->frame_num,
len - (get_bits_count(gb) - s->frame_offset) - 1); len - (bitstream_tell(bc) - s->frame_offset) - 1);
s->packet_loss = 1; s->packet_loss = 1;
return 0; return 0;
} }
/** skip the rest of the frame data */ /** skip the rest of the frame data */
skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1); bitstream_skip(bc, len - (bitstream_tell(bc) - s->frame_offset) - 1);
} else { } else {
while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) { while (bitstream_tell(bc) < s->num_saved_bits && bitstream_read_bit(bc) == 0) {
} }
} }
/** decode trailer bit */ /** decode trailer bit */
more_frames = get_bits1(gb); more_frames = bitstream_read_bit(bc);
++s->frame_num; ++s->frame_num;
return more_frames; return more_frames;
...@@ -1434,22 +1435,22 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr) ...@@ -1434,22 +1435,22 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
/** /**
*@brief Calculate remaining input buffer length. *@brief Calculate remaining input buffer length.
*@param s codec context *@param s codec context
*@param gb bitstream reader context *@param bc bitstream reader context
*@return remaining size in bits *@return remaining size in bits
*/ */
static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb) static int remaining_bits(WMAProDecodeCtx *s, BitstreamContext *bc)
{ {
return s->buf_bit_size - get_bits_count(gb); return s->buf_bit_size - bitstream_tell(bc);
} }
/** /**
*@brief Fill the bit reservoir with a (partial) frame. *@brief Fill the bit reservoir with a (partial) frame.
*@param s codec context *@param s codec context
*@param gb bitstream reader context *@param bc bitstream reader context
*@param len length of the partial frame *@param len length of the partial frame
*@param append decides whether to reset the buffer or not *@param append decides whether to reset the buffer or not
*/ */
static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len, static void save_bits(WMAProDecodeCtx *s, BitstreamContext *bc, int len,
int append) int append)
{ {
int buflen; int buflen;
...@@ -1459,7 +1460,7 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len, ...@@ -1459,7 +1460,7 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
and skipped later so that a fast byte copy is possible */ and skipped later so that a fast byte copy is possible */
if (!append) { if (!append) {
s->frame_offset = get_bits_count(gb) & 7; s->frame_offset = bitstream_tell(bc) & 7;
s->num_saved_bits = s->frame_offset; s->num_saved_bits = s->frame_offset;
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE); init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
} }
...@@ -1482,24 +1483,24 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len, ...@@ -1482,24 +1483,24 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
s->num_saved_bits += len; s->num_saved_bits += len;
if (!append) { if (!append) {
avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3),
s->num_saved_bits); s->num_saved_bits);
} else { } else {
int align = 8 - (get_bits_count(gb) & 7); int align = 8 - (bitstream_tell(bc) & 7);
align = FFMIN(align, len); align = FFMIN(align, len);
put_bits(&s->pb, align, get_bits(gb, align)); put_bits(&s->pb, align, bitstream_read(bc, align));
len -= align; len -= align;
avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len); avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3), len);
} }
skip_bits_long(gb, len); bitstream_skip(bc, len);
{ {
PutBitContext tmp = s->pb; PutBitContext tmp = s->pb;
flush_put_bits(&tmp); flush_put_bits(&tmp);
} }
init_get_bits(&s->gb, s->frame_data, s->num_saved_bits); bitstream_init(&s->bc, s->frame_data, s->num_saved_bits);
skip_bits(&s->gb, s->frame_offset); bitstream_skip(&s->bc, s->frame_offset);
} }
/** /**
...@@ -1513,7 +1514,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1513,7 +1514,7 @@ static int decode_packet(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket* avpkt) int *got_frame_ptr, AVPacket* avpkt)
{ {
WMAProDecodeCtx *s = avctx->priv_data; WMAProDecodeCtx *s = avctx->priv_data;
GetBitContext* gb = &s->pgb; BitstreamContext *bc = &s->pbc;
const uint8_t* buf = avpkt->data; const uint8_t* buf = avpkt->data;
int buf_size = avpkt->size; int buf_size = avpkt->size;
int num_bits_prev_frame; int num_bits_prev_frame;
...@@ -1536,12 +1537,12 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1536,12 +1537,12 @@ static int decode_packet(AVCodecContext *avctx, void *data,
s->buf_bit_size = buf_size << 3; s->buf_bit_size = buf_size << 3;
/** parse packet header */ /** parse packet header */
init_get_bits(gb, buf, s->buf_bit_size); bitstream_init(bc, buf, s->buf_bit_size);
packet_sequence_number = get_bits(gb, 4); packet_sequence_number = bitstream_read(bc, 4);
skip_bits(gb, 2); bitstream_skip(bc, 2);
/** get number of bits that need to be added to the previous frame */ /** get number of bits that need to be added to the previous frame */
num_bits_prev_frame = get_bits(gb, s->log2_frame_size); num_bits_prev_frame = bitstream_read(bc, s->log2_frame_size);
ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number, ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
num_bits_prev_frame); num_bits_prev_frame);
...@@ -1556,7 +1557,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1556,7 +1557,7 @@ static int decode_packet(AVCodecContext *avctx, void *data,
s->packet_sequence_number = packet_sequence_number; s->packet_sequence_number = packet_sequence_number;
if (num_bits_prev_frame > 0) { if (num_bits_prev_frame > 0) {
int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb); int remaining_packet_bits = s->buf_bit_size - bitstream_tell(bc);
if (num_bits_prev_frame >= remaining_packet_bits) { if (num_bits_prev_frame >= remaining_packet_bits) {
num_bits_prev_frame = remaining_packet_bits; num_bits_prev_frame = remaining_packet_bits;
s->packet_done = 1; s->packet_done = 1;
...@@ -1564,7 +1565,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1564,7 +1565,7 @@ static int decode_packet(AVCodecContext *avctx, void *data,
/** append the previous frame data to the remaining data from the /** append the previous frame data to the remaining data from the
previous packet to create a full frame */ previous packet to create a full frame */
save_bits(s, gb, num_bits_prev_frame, 1); save_bits(s, bc, num_bits_prev_frame, 1);
ff_dlog(avctx, "accumulated %x bits of frame data\n", ff_dlog(avctx, "accumulated %x bits of frame data\n",
s->num_saved_bits - s->frame_offset); s->num_saved_bits - s->frame_offset);
...@@ -1587,15 +1588,15 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1587,15 +1588,15 @@ static int decode_packet(AVCodecContext *avctx, void *data,
} else { } else {
int frame_size; int frame_size;
s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3; s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
init_get_bits(gb, avpkt->data, s->buf_bit_size); bitstream_init(bc, avpkt->data, s->buf_bit_size);
skip_bits(gb, s->packet_offset); bitstream_skip(bc, s->packet_offset);
if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size && if (s->len_prefix && remaining_bits(s, bc) > s->log2_frame_size &&
(frame_size = show_bits(gb, s->log2_frame_size)) && (frame_size = bitstream_peek(bc, s->log2_frame_size)) &&
frame_size <= remaining_bits(s, gb)) { frame_size <= remaining_bits(s, bc)) {
save_bits(s, gb, frame_size, 0); save_bits(s, bc, frame_size, 0);
s->packet_done = !decode_frame(s, data, got_frame_ptr); s->packet_done = !decode_frame(s, data, got_frame_ptr);
} else if (!s->len_prefix } else if (!s->len_prefix
&& s->num_saved_bits > get_bits_count(&s->gb)) { && s->num_saved_bits > bitstream_tell(&s->bc)) {
/** when the frames do not have a length prefix, we don't know /** when the frames do not have a length prefix, we don't know
the compressed length of the individual frames the compressed length of the individual frames
however, we know what part of a new packet belongs to the however, we know what part of a new packet belongs to the
...@@ -1609,17 +1610,17 @@ static int decode_packet(AVCodecContext *avctx, void *data, ...@@ -1609,17 +1610,17 @@ static int decode_packet(AVCodecContext *avctx, void *data,
} }
if (s->packet_done && !s->packet_loss && if (s->packet_done && !s->packet_loss &&
remaining_bits(s, gb) > 0) { remaining_bits(s, bc) > 0) {
/** save the rest of the data so that it can be decoded /** save the rest of the data so that it can be decoded
with the next packet */ with the next packet */
save_bits(s, gb, remaining_bits(s, gb), 0); save_bits(s, bc, remaining_bits(s, bc), 0);
} }
s->packet_offset = get_bits_count(gb) & 7; s->packet_offset = bitstream_tell(bc) & 7;
if (s->packet_loss) if (s->packet_loss)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
return get_bits_count(gb) >> 3; return bitstream_tell(bc) >> 3;
} }
/** /**
......
...@@ -25,16 +25,15 @@ ...@@ -25,16 +25,15 @@
* @author Ronald S. Bultje <rsbultje@gmail.com> * @author Ronald S. Bultje <rsbultje@gmail.com>
*/ */
#define UNCHECKED_BITSTREAM_READER 1
#include <math.h> #include <math.h>
#include "libavutil/channel_layout.h" #include "libavutil/channel_layout.h"
#include "libavutil/float_dsp.h" #include "libavutil/float_dsp.h"
#include "libavutil/mem.h" #include "libavutil/mem.h"
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h"
#include "internal.h" #include "internal.h"
#include "get_bits.h"
#include "put_bits.h" #include "put_bits.h"
#include "wmavoice_data.h" #include "wmavoice_data.h"
#include "celp_filters.h" #include "celp_filters.h"
...@@ -136,7 +135,7 @@ typedef struct WMAVoiceContext { ...@@ -136,7 +135,7 @@ typedef struct WMAVoiceContext {
* @name Global values specified in the stream header / extradata or used all over. * @name Global values specified in the stream header / extradata or used all over.
* @{ * @{
*/ */
GetBitContext gb; ///< packet bitreader. During decoder init, BitstreamContext bc; ///< packet bitreader. During decoder init,
///< it contains the extradata from the ///< it contains the extradata from the
///< demuxer. During decoding, it contains ///< demuxer. During decoding, it contains
///< packet data. ///< packet data.
...@@ -296,20 +295,20 @@ typedef struct WMAVoiceContext { ...@@ -296,20 +295,20 @@ typedef struct WMAVoiceContext {
/** /**
* Set up the variable bit mode (VBM) tree from container extradata. * Set up the variable bit mode (VBM) tree from container extradata.
* @param gb bit I/O context. * @param bc bit I/O context.
* The bit context (s->gb) should be loaded with byte 23-46 of the * The bit context (s->bc) should be loaded with byte 23-46 of the
* container extradata (i.e. the ones containing the VBM tree). * container extradata (i.e. the ones containing the VBM tree).
* @param vbm_tree pointer to array to which the decoded VBM tree will be * @param vbm_tree pointer to array to which the decoded VBM tree will be
* written. * written.
* @return 0 on success, <0 on error. * @return 0 on success, <0 on error.
*/ */
static av_cold int decode_vbmtree(GetBitContext *gb, int8_t vbm_tree[25]) static av_cold int decode_vbmtree(BitstreamContext *bc, int8_t vbm_tree[25])
{ {
int cntr[8] = { 0 }, n, res; int cntr[8] = { 0 }, n, res;
memset(vbm_tree, 0xff, sizeof(vbm_tree[0]) * 25); memset(vbm_tree, 0xff, sizeof(vbm_tree[0]) * 25);
for (n = 0; n < 17; n++) { for (n = 0; n < 17; n++) {
res = get_bits(gb, 3); res = bitstream_read(bc, 3);
if (cntr[res] > 3) // should be >= 3 + (res == 7)) if (cntr[res] > 3) // should be >= 3 + (res == 7))
return -1; return -1;
vbm_tree[res * 3 + cntr[res]++] = n; vbm_tree[res * 3 + cntr[res]++] = n;
...@@ -401,8 +400,8 @@ static av_cold int wmavoice_decode_init(AVCodecContext *ctx) ...@@ -401,8 +400,8 @@ static av_cold int wmavoice_decode_init(AVCodecContext *ctx)
for (n = 0; n < s->lsps; n++) for (n = 0; n < s->lsps; n++)
s->prev_lsps[n] = M_PI * (n + 1.0) / (s->lsps + 1.0); s->prev_lsps[n] = M_PI * (n + 1.0) / (s->lsps + 1.0);
init_get_bits(&s->gb, ctx->extradata + 22, (ctx->extradata_size - 22) << 3); bitstream_init8(&s->bc, ctx->extradata + 22, ctx->extradata_size - 22);
if (decode_vbmtree(&s->gb, s->vbm_tree) < 0) { if (decode_vbmtree(&s->bc, s->vbm_tree) < 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid VBM tree; broken extradata?\n"); av_log(ctx, AV_LOG_ERROR, "Invalid VBM tree; broken extradata?\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
...@@ -857,7 +856,7 @@ static void dequant_lsps(double *lsps, int num, ...@@ -857,7 +856,7 @@ static void dequant_lsps(double *lsps, int num,
/** /**
* Parse 10 independently-coded LSPs. * Parse 10 independently-coded LSPs.
*/ */
static void dequant_lsp10i(GetBitContext *gb, double *lsps) static void dequant_lsp10i(BitstreamContext *bc, double *lsps)
{ {
static const uint16_t vec_sizes[4] = { 256, 64, 32, 32 }; static const uint16_t vec_sizes[4] = { 256, 64, 32, 32 };
static const double mul_lsf[4] = { static const double mul_lsf[4] = {
...@@ -870,10 +869,10 @@ static void dequant_lsp10i(GetBitContext *gb, double *lsps) ...@@ -870,10 +869,10 @@ static void dequant_lsp10i(GetBitContext *gb, double *lsps)
}; };
uint16_t v[4]; uint16_t v[4];
v[0] = get_bits(gb, 8); v[0] = bitstream_read(bc, 8);
v[1] = get_bits(gb, 6); v[1] = bitstream_read(bc, 6);
v[2] = get_bits(gb, 5); v[2] = bitstream_read(bc, 5);
v[3] = get_bits(gb, 5); v[3] = bitstream_read(bc, 5);
dequant_lsps(lsps, 10, v, vec_sizes, 4, wmavoice_dq_lsp10i, dequant_lsps(lsps, 10, v, vec_sizes, 4, wmavoice_dq_lsp10i,
mul_lsf, base_lsf); mul_lsf, base_lsf);
...@@ -883,7 +882,7 @@ static void dequant_lsp10i(GetBitContext *gb, double *lsps) ...@@ -883,7 +882,7 @@ static void dequant_lsp10i(GetBitContext *gb, double *lsps)
* Parse 10 independently-coded LSPs, and then derive the tables to * Parse 10 independently-coded LSPs, and then derive the tables to
* generate LSPs for the other frames from them (residual coding). * generate LSPs for the other frames from them (residual coding).
*/ */
static void dequant_lsp10r(GetBitContext *gb, static void dequant_lsp10r(BitstreamContext *bc,
double *i_lsps, const double *old, double *i_lsps, const double *old,
double *a1, double *a2, int q_mode) double *a1, double *a2, int q_mode)
{ {
...@@ -899,12 +898,12 @@ static void dequant_lsp10r(GetBitContext *gb, ...@@ -899,12 +898,12 @@ static void dequant_lsp10r(GetBitContext *gb,
uint16_t interpol, v[3]; uint16_t interpol, v[3];
int n; int n;
dequant_lsp10i(gb, i_lsps); dequant_lsp10i(bc, i_lsps);
interpol = get_bits(gb, 5); interpol = bitstream_read(bc, 5);
v[0] = get_bits(gb, 7); v[0] = bitstream_read(bc, 7);
v[1] = get_bits(gb, 6); v[1] = bitstream_read(bc, 6);
v[2] = get_bits(gb, 6); v[2] = bitstream_read(bc, 6);
for (n = 0; n < 10; n++) { for (n = 0; n < 10; n++) {
double delta = old[n] - i_lsps[n]; double delta = old[n] - i_lsps[n];
...@@ -919,7 +918,7 @@ static void dequant_lsp10r(GetBitContext *gb, ...@@ -919,7 +918,7 @@ static void dequant_lsp10r(GetBitContext *gb,
/** /**
* Parse 16 independently-coded LSPs. * Parse 16 independently-coded LSPs.
*/ */
static void dequant_lsp16i(GetBitContext *gb, double *lsps) static void dequant_lsp16i(BitstreamContext *bc, double *lsps)
{ {
static const uint16_t vec_sizes[5] = { 256, 64, 128, 64, 128 }; static const uint16_t vec_sizes[5] = { 256, 64, 128, 64, 128 };
static const double mul_lsf[5] = { static const double mul_lsf[5] = {
...@@ -934,11 +933,11 @@ static void dequant_lsp16i(GetBitContext *gb, double *lsps) ...@@ -934,11 +933,11 @@ static void dequant_lsp16i(GetBitContext *gb, double *lsps)
}; };
uint16_t v[5]; uint16_t v[5];
v[0] = get_bits(gb, 8); v[0] = bitstream_read(bc, 8);
v[1] = get_bits(gb, 6); v[1] = bitstream_read(bc, 6);
v[2] = get_bits(gb, 7); v[2] = bitstream_read(bc, 7);
v[3] = get_bits(gb, 6); v[3] = bitstream_read(bc, 6);
v[4] = get_bits(gb, 7); v[4] = bitstream_read(bc, 7);
dequant_lsps( lsps, 5, v, vec_sizes, 2, dequant_lsps( lsps, 5, v, vec_sizes, 2,
wmavoice_dq_lsp16i1, mul_lsf, base_lsf); wmavoice_dq_lsp16i1, mul_lsf, base_lsf);
...@@ -952,7 +951,7 @@ static void dequant_lsp16i(GetBitContext *gb, double *lsps) ...@@ -952,7 +951,7 @@ static void dequant_lsp16i(GetBitContext *gb, double *lsps)
* Parse 16 independently-coded LSPs, and then derive the tables to * Parse 16 independently-coded LSPs, and then derive the tables to
* generate LSPs for the other frames from them (residual coding). * generate LSPs for the other frames from them (residual coding).
*/ */
static void dequant_lsp16r(GetBitContext *gb, static void dequant_lsp16r(BitstreamContext *bc,
double *i_lsps, const double *old, double *i_lsps, const double *old,
double *a1, double *a2, int q_mode) double *a1, double *a2, int q_mode)
{ {
...@@ -968,12 +967,12 @@ static void dequant_lsp16r(GetBitContext *gb, ...@@ -968,12 +967,12 @@ static void dequant_lsp16r(GetBitContext *gb,
uint16_t interpol, v[3]; uint16_t interpol, v[3];
int n; int n;
dequant_lsp16i(gb, i_lsps); dequant_lsp16i(bc, i_lsps);
interpol = get_bits(gb, 5); interpol = bitstream_read(bc, 5);
v[0] = get_bits(gb, 7); v[0] = bitstream_read(bc, 7);
v[1] = get_bits(gb, 7); v[1] = bitstream_read(bc, 7);
v[2] = get_bits(gb, 7); v[2] = bitstream_read(bc, 7);
for (n = 0; n < 16; n++) { for (n = 0; n < 16; n++) {
double delta = old[n] - i_lsps[n]; double delta = old[n] - i_lsps[n];
...@@ -999,10 +998,10 @@ static void dequant_lsp16r(GetBitContext *gb, ...@@ -999,10 +998,10 @@ static void dequant_lsp16r(GetBitContext *gb,
* Parse the offset of the first pitch-adaptive window pulses, and * Parse the offset of the first pitch-adaptive window pulses, and
* the distribution of pulses between the two blocks in this frame. * the distribution of pulses between the two blocks in this frame.
* @param s WMA Voice decoding context private data * @param s WMA Voice decoding context private data
* @param gb bit I/O context * @param bc bit I/O context
* @param pitch pitch for each block in this frame * @param pitch pitch for each block in this frame
*/ */
static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb, static void aw_parse_coords(WMAVoiceContext *s, BitstreamContext *bc,
const int *pitch) const int *pitch)
{ {
static const int16_t start_offset[94] = { static const int16_t start_offset[94] = {
...@@ -1019,9 +1018,9 @@ static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1019,9 +1018,9 @@ static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb,
/* position of pulse */ /* position of pulse */
s->aw_idx_is_ext = 0; s->aw_idx_is_ext = 0;
if ((bits = get_bits(gb, 6)) >= 54) { if ((bits = bitstream_read(bc, 6)) >= 54) {
s->aw_idx_is_ext = 1; s->aw_idx_is_ext = 1;
bits += (bits - 54) * 3 + get_bits(gb, 2); bits += (bits - 54) * 3 + bitstream_read(bc, 2);
} }
/* for a repeated pulse at pulse_off with a pitch_lag of pitch[], count /* for a repeated pulse at pulse_off with a pitch_lag of pitch[], count
...@@ -1049,12 +1048,12 @@ static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1049,12 +1048,12 @@ static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb,
/** /**
* Apply second set of pitch-adaptive window pulses. * Apply second set of pitch-adaptive window pulses.
* @param s WMA Voice decoding context private data * @param s WMA Voice decoding context private data
* @param gb bit I/O context * @param bc bit I/O context
* @param block_idx block index in frame [0, 1] * @param block_idx block index in frame [0, 1]
* @param fcb structure containing fixed codebook vector info * @param fcb structure containing fixed codebook vector info
* @return -1 on error, 0 otherwise * @return -1 on error, 0 otherwise
*/ */
static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb, static int aw_pulse_set2(WMAVoiceContext *s, BitstreamContext *bc,
int block_idx, AMRFixed *fcb) int block_idx, AMRFixed *fcb)
{ {
uint16_t use_mask_mem[9]; // only 5 are used, rest is padding uint16_t use_mask_mem[9]; // only 5 are used, rest is padding
...@@ -1108,7 +1107,7 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1108,7 +1107,7 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb,
} }
/* find the 'aidx'th offset that is not excluded */ /* find the 'aidx'th offset that is not excluded */
aidx = get_bits(gb, s->aw_n_pulses[0] > 0 ? 5 - 2 * block_idx : 4); aidx = bitstream_read(bc, s->aw_n_pulses[0] > 0 ? 5 - 2 * block_idx : 4);
for (n = 0; n <= aidx; pulse_start++) { for (n = 0; n <= aidx; pulse_start++) {
for (idx = pulse_start; idx < 0; idx += fcb->pitch_lag) ; for (idx = pulse_start; idx < 0; idx += fcb->pitch_lag) ;
if (idx >= MAX_FRAMESIZE / 2) { // find from zero if (idx >= MAX_FRAMESIZE / 2) { // find from zero
...@@ -1128,7 +1127,7 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1128,7 +1127,7 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb,
} }
fcb->x[fcb->n] = start_off; fcb->x[fcb->n] = start_off;
fcb->y[fcb->n] = get_bits1(gb) ? -1.0 : 1.0; fcb->y[fcb->n] = bitstream_read_bit(bc) ? -1.0 : 1.0;
fcb->n++; fcb->n++;
/* set offset for next block, relative to start of that block */ /* set offset for next block, relative to start of that block */
...@@ -1140,14 +1139,14 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1140,14 +1139,14 @@ static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb,
/** /**
* Apply first set of pitch-adaptive window pulses. * Apply first set of pitch-adaptive window pulses.
* @param s WMA Voice decoding context private data * @param s WMA Voice decoding context private data
* @param gb bit I/O context * @param bc bit I/O context
* @param block_idx block index in frame [0, 1] * @param block_idx block index in frame [0, 1]
* @param fcb storage location for fixed codebook pulse info * @param fcb storage location for fixed codebook pulse info
*/ */
static void aw_pulse_set1(WMAVoiceContext *s, GetBitContext *gb, static void aw_pulse_set1(WMAVoiceContext *s, BitstreamContext *bc,
int block_idx, AMRFixed *fcb) int block_idx, AMRFixed *fcb)
{ {
int val = get_bits(gb, 12 - 2 * (s->aw_idx_is_ext && !block_idx)); int val = bitstream_read(bc, 12 - 2 * (s->aw_idx_is_ext && !block_idx));
float v; float v;
if (s->aw_n_pulses[block_idx] > 0) { if (s->aw_n_pulses[block_idx] > 0) {
...@@ -1241,7 +1240,7 @@ static int pRNG(int frame_cntr, int block_num, int block_size) ...@@ -1241,7 +1240,7 @@ static int pRNG(int frame_cntr, int block_num, int block_size)
* Parse hardcoded signal for a single block. * Parse hardcoded signal for a single block.
* @note see #synth_block(). * @note see #synth_block().
*/ */
static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb, static void synth_block_hardcoded(WMAVoiceContext *s, BitstreamContext *bc,
int block_idx, int size, int block_idx, int size,
const struct frame_type_desc *frame_desc, const struct frame_type_desc *frame_desc,
float *excitation) float *excitation)
...@@ -1256,8 +1255,8 @@ static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1256,8 +1255,8 @@ static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb,
r_idx = pRNG(s->frame_cntr, block_idx, size); r_idx = pRNG(s->frame_cntr, block_idx, size);
gain = s->silence_gain; gain = s->silence_gain;
} else /* FCB_TYPE_HARDCODED */ { } else /* FCB_TYPE_HARDCODED */ {
r_idx = get_bits(gb, 8); r_idx = bitstream_read(bc, 8);
gain = wmavoice_gain_universal[get_bits(gb, 6)]; gain = wmavoice_gain_universal[bitstream_read(bc, 6)];
} }
/* Clear gain prediction parameters */ /* Clear gain prediction parameters */
...@@ -1272,7 +1271,7 @@ static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1272,7 +1271,7 @@ static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb,
* Parse FCB/ACB signal for a single block. * Parse FCB/ACB signal for a single block.
* @note see #synth_block(). * @note see #synth_block().
*/ */
static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, static void synth_block_fcb_acb(WMAVoiceContext *s, BitstreamContext *bc,
int block_idx, int size, int block_idx, int size,
int block_pitch_sh2, int block_pitch_sh2,
const struct frame_type_desc *frame_desc, const struct frame_type_desc *frame_desc,
...@@ -1296,8 +1295,8 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1296,8 +1295,8 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
/* For the other frame types, this is where we apply the innovation /* For the other frame types, this is where we apply the innovation
* (fixed) codebook pulses of the speech signal. */ * (fixed) codebook pulses of the speech signal. */
if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {
aw_pulse_set1(s, gb, block_idx, &fcb); aw_pulse_set1(s, bc, block_idx, &fcb);
if (aw_pulse_set2(s, gb, block_idx, &fcb)) { if (aw_pulse_set2(s, bc, block_idx, &fcb)) {
/* Conceal the block with silence and return. /* Conceal the block with silence and return.
* Skip the correct amount of bits to read the next * Skip the correct amount of bits to read the next
* block from the correct offset. */ * block from the correct offset. */
...@@ -1306,7 +1305,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1306,7 +1305,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
for (n = 0; n < size; n++) for (n = 0; n < size; n++)
excitation[n] = excitation[n] =
wmavoice_std_codebook[r_idx + n] * s->silence_gain; wmavoice_std_codebook[r_idx + n] * s->silence_gain;
skip_bits(gb, 7 + 1); bitstream_skip(bc, 7 + 1);
return; return;
} }
} else /* FCB_TYPE_EXC_PULSES */ { } else /* FCB_TYPE_EXC_PULSES */ {
...@@ -1319,12 +1318,12 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1319,12 +1318,12 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
float sign; float sign;
int pos1, pos2; int pos1, pos2;
sign = get_bits1(gb) ? 1.0 : -1.0; sign = bitstream_read_bit(bc) ? 1.0 : -1.0;
pos1 = get_bits(gb, offset_nbits); pos1 = bitstream_read(bc, offset_nbits);
fcb.x[fcb.n] = n + 5 * pos1; fcb.x[fcb.n] = n + 5 * pos1;
fcb.y[fcb.n++] = sign; fcb.y[fcb.n++] = sign;
if (n < frame_desc->dbl_pulses) { if (n < frame_desc->dbl_pulses) {
pos2 = get_bits(gb, offset_nbits); pos2 = bitstream_read(bc, offset_nbits);
fcb.x[fcb.n] = n + 5 * pos2; fcb.x[fcb.n] = n + 5 * pos2;
fcb.y[fcb.n++] = (pos1 < pos2) ? -sign : sign; fcb.y[fcb.n++] = (pos1 < pos2) ? -sign : sign;
} }
...@@ -1334,7 +1333,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1334,7 +1333,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
/* Calculate gain for adaptive & fixed codebook signal. /* Calculate gain for adaptive & fixed codebook signal.
* see ff_amr_set_fixed_gain(). */ * see ff_amr_set_fixed_gain(). */
idx = get_bits(gb, 7); idx = bitstream_read(bc, 7);
fcb_gain = expf(avpriv_scalarproduct_float_c(s->gain_pred_err, fcb_gain = expf(avpriv_scalarproduct_float_c(s->gain_pred_err,
gain_coeff, 6) - gain_coeff, 6) -
5.2409161640 + wmavoice_gain_codebook_fcb[idx]); 5.2409161640 + wmavoice_gain_codebook_fcb[idx]);
...@@ -1396,7 +1395,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1396,7 +1395,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
* @note we assume enough bits are available, caller should check. * @note we assume enough bits are available, caller should check.
* *
* @param s WMA Voice decoding context private data * @param s WMA Voice decoding context private data
* @param gb bit I/O context * @param bc bit I/O context
* @param block_idx index of the to-be-read block * @param block_idx index of the to-be-read block
* @param size amount of samples to be read in this block * @param size amount of samples to be read in this block
* @param block_pitch_sh2 pitch for this block << 2 * @param block_pitch_sh2 pitch for this block << 2
...@@ -1407,7 +1406,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1407,7 +1406,7 @@ static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb,
* @param synth target memory for the speech synthesis filter output * @param synth target memory for the speech synthesis filter output
* @return 0 on success, <0 on error. * @return 0 on success, <0 on error.
*/ */
static void synth_block(WMAVoiceContext *s, GetBitContext *gb, static void synth_block(WMAVoiceContext *s, BitstreamContext *bc,
int block_idx, int size, int block_idx, int size,
int block_pitch_sh2, int block_pitch_sh2,
const double *lsps, const double *prev_lsps, const double *lsps, const double *prev_lsps,
...@@ -1420,9 +1419,9 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1420,9 +1419,9 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb,
int n; int n;
if (frame_desc->acb_type == ACB_TYPE_NONE) if (frame_desc->acb_type == ACB_TYPE_NONE)
synth_block_hardcoded(s, gb, block_idx, size, frame_desc, excitation); synth_block_hardcoded(s, bc, block_idx, size, frame_desc, excitation);
else else
synth_block_fcb_acb(s, gb, block_idx, size, block_pitch_sh2, synth_block_fcb_acb(s, bc, block_idx, size, block_pitch_sh2,
frame_desc, excitation); frame_desc, excitation);
/* convert interpolated LSPs to LPCs */ /* convert interpolated LSPs to LPCs */
...@@ -1440,7 +1439,7 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1440,7 +1439,7 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb,
* @note we assume enough bits are available, caller should check. * @note we assume enough bits are available, caller should check.
* *
* @param ctx WMA Voice decoder context * @param ctx WMA Voice decoder context
* @param gb bit I/O context (s->gb or one for cross-packet superframes) * @param bc bit I/O context (s->bc or one for cross-packet superframes)
* @param frame_idx Frame number within superframe [0-2] * @param frame_idx Frame number within superframe [0-2]
* @param samples pointer to output sample buffer, has space for at least 160 * @param samples pointer to output sample buffer, has space for at least 160
* samples * samples
...@@ -1450,8 +1449,8 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb, ...@@ -1450,8 +1449,8 @@ static void synth_block(WMAVoiceContext *s, GetBitContext *gb,
* @param synth target buffer for synthesized speech data * @param synth target buffer for synthesized speech data
* @return 0 on success, <0 on error. * @return 0 on success, <0 on error.
*/ */
static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, static int synth_frame(AVCodecContext *ctx, BitstreamContext *bc,
float *samples, int frame_idx, float *samples,
const double *lsps, const double *prev_lsps, const double *lsps, const double *prev_lsps,
float *excitation, float *synth) float *excitation, float *synth)
{ {
...@@ -1460,7 +1459,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, ...@@ -1460,7 +1459,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx,
int pitch[MAX_BLOCKS], last_block_pitch; int pitch[MAX_BLOCKS], last_block_pitch;
/* Parse frame type ("frame header"), see frame_descs */ /* Parse frame type ("frame header"), see frame_descs */
int bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)], block_nsamples; int bd_idx = s->vbm_tree[bitstream_read_vlc(bc, frame_type_vlc.table, 6, 3)], block_nsamples;
if (bd_idx < 0) { if (bd_idx < 0) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
...@@ -1478,7 +1477,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, ...@@ -1478,7 +1477,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx,
* incrementing/decrementing prev_frame_pitch to cur_pitch_val. */ * incrementing/decrementing prev_frame_pitch to cur_pitch_val. */
n_blocks_x2 = frame_descs[bd_idx].n_blocks << 1; n_blocks_x2 = frame_descs[bd_idx].n_blocks << 1;
log_n_blocks_x2 = frame_descs[bd_idx].log_n_blocks + 1; log_n_blocks_x2 = frame_descs[bd_idx].log_n_blocks + 1;
cur_pitch_val = s->min_pitch_val + get_bits(gb, s->pitch_nbits); cur_pitch_val = s->min_pitch_val + bitstream_read(bc, s->pitch_nbits);
cur_pitch_val = FFMIN(cur_pitch_val, s->max_pitch_val - 1); cur_pitch_val = FFMIN(cur_pitch_val, s->max_pitch_val - 1);
if (s->last_acb_type == ACB_TYPE_NONE || if (s->last_acb_type == ACB_TYPE_NONE ||
20 * abs(cur_pitch_val - s->last_pitch_val) > 20 * abs(cur_pitch_val - s->last_pitch_val) >
...@@ -1502,10 +1501,10 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, ...@@ -1502,10 +1501,10 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx,
/* Global gain (if silence) and pitch-adaptive window coordinates */ /* Global gain (if silence) and pitch-adaptive window coordinates */
switch (frame_descs[bd_idx].fcb_type) { switch (frame_descs[bd_idx].fcb_type) {
case FCB_TYPE_SILENCE: case FCB_TYPE_SILENCE:
s->silence_gain = wmavoice_gain_silence[get_bits(gb, 8)]; s->silence_gain = wmavoice_gain_silence[bitstream_read(bc, 8)];
break; break;
case FCB_TYPE_AW_PULSES: case FCB_TYPE_AW_PULSES:
aw_parse_coords(s, gb, pitch); aw_parse_coords(s, bc, pitch);
break; break;
} }
...@@ -1526,10 +1525,10 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, ...@@ -1526,10 +1525,10 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx,
t3 = s->block_conv_table[3] - s->block_conv_table[2] + 1; t3 = s->block_conv_table[3] - s->block_conv_table[2] + 1;
if (n == 0) { if (n == 0) {
block_pitch = get_bits(gb, s->block_pitch_nbits); block_pitch = bitstream_read(bc, s->block_pitch_nbits);
} else } else
block_pitch = last_block_pitch - s->block_delta_pitch_hrange + block_pitch = last_block_pitch - s->block_delta_pitch_hrange +
get_bits(gb, s->block_delta_pitch_nbits); bitstream_read(bc, s->block_delta_pitch_nbits);
/* Convert last_ so that any next delta is within _range */ /* Convert last_ so that any next delta is within _range */
last_block_pitch = av_clip(block_pitch, last_block_pitch = av_clip(block_pitch,
s->block_delta_pitch_hrange, s->block_delta_pitch_hrange,
...@@ -1567,7 +1566,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, ...@@ -1567,7 +1566,7 @@ static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx,
break; break;
} }
synth_block(s, gb, n, block_nsamples, bl_pitch_sh2, synth_block(s, bc, n, block_nsamples, bl_pitch_sh2,
lsps, prev_lsps, &frame_descs[bd_idx], lsps, prev_lsps, &frame_descs[bd_idx],
&excitation[n * block_nsamples], &excitation[n * block_nsamples],
&synth[n * block_nsamples]); &synth[n * block_nsamples]);
...@@ -1658,32 +1657,32 @@ static void stabilize_lsps(double *lsps, int num) ...@@ -1658,32 +1657,32 @@ static void stabilize_lsps(double *lsps, int num)
/** /**
* Test if there's enough bits to read 1 superframe. * Test if there's enough bits to read 1 superframe.
* *
* @param orig_gb bit I/O context used for reading. This function * @param orig_bc bit I/O context used for reading. This function
* does not modify the state of the bitreader; it * does not modify the state of the bitreader; it
* only uses it to copy the current stream position * only uses it to copy the current stream position
* @param s WMA Voice decoding context private data * @param s WMA Voice decoding context private data
* @return < 0 on error, 1 on not enough bits or 0 if OK. * @return < 0 on error, 1 on not enough bits or 0 if OK.
*/ */
static int check_bits_for_superframe(GetBitContext *orig_gb, static int check_bits_for_superframe(BitstreamContext *orig_bc,
WMAVoiceContext *s) WMAVoiceContext *s)
{ {
GetBitContext s_gb, *gb = &s_gb; BitstreamContext s_bc, *bc = &s_bc;
int n, need_bits, bd_idx; int n, need_bits, bd_idx;
const struct frame_type_desc *frame_desc; const struct frame_type_desc *frame_desc;
/* initialize a copy */ /* initialize a copy */
*gb = *orig_gb; *bc = *orig_bc;
/* superframe header */ /* superframe header */
if (get_bits_left(gb) < 14) if (bitstream_bits_left(bc) < 14)
return 1; return 1;
if (!get_bits1(gb)) if (!bitstream_read_bit(bc))
return AVERROR(ENOSYS); // WMAPro-in-WMAVoice superframe return AVERROR(ENOSYS); // WMAPro-in-WMAVoice superframe
if (get_bits1(gb)) skip_bits(gb, 12); // number of samples in superframe if (bitstream_read_bit(bc)) bitstream_skip(bc, 12); // number of samples in superframe
if (s->has_residual_lsps) { // residual LSPs (for all frames) if (s->has_residual_lsps) { // residual LSPs (for all frames)
if (get_bits_left(gb) < s->sframe_lsp_bitsize) if (bitstream_bits_left(bc) < s->sframe_lsp_bitsize)
return 1; return 1;
skip_bits_long(gb, s->sframe_lsp_bitsize); bitstream_skip(bc, s->sframe_lsp_bitsize);
} }
/* frames */ /* frames */
...@@ -1691,24 +1690,25 @@ static int check_bits_for_superframe(GetBitContext *orig_gb, ...@@ -1691,24 +1690,25 @@ static int check_bits_for_superframe(GetBitContext *orig_gb,
int aw_idx_is_ext = 0; int aw_idx_is_ext = 0;
if (!s->has_residual_lsps) { // independent LSPs (per-frame) if (!s->has_residual_lsps) { // independent LSPs (per-frame)
if (get_bits_left(gb) < s->frame_lsp_bitsize) return 1; if (bitstream_bits_left(bc) < s->frame_lsp_bitsize)
skip_bits_long(gb, s->frame_lsp_bitsize); return 1;
bitstream_skip(bc, s->frame_lsp_bitsize);
} }
bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)]; bd_idx = s->vbm_tree[bitstream_read_vlc(bc, frame_type_vlc.table, 6, 3)];
if (bd_idx < 0) if (bd_idx < 0)
return AVERROR_INVALIDDATA; // invalid frame type VLC code return AVERROR_INVALIDDATA; // invalid frame type VLC code
frame_desc = &frame_descs[bd_idx]; frame_desc = &frame_descs[bd_idx];
if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) { if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {
if (get_bits_left(gb) < s->pitch_nbits) if (bitstream_bits_left(bc) < s->pitch_nbits)
return 1; return 1;
skip_bits_long(gb, s->pitch_nbits); bitstream_skip(bc, s->pitch_nbits);
} }
if (frame_desc->fcb_type == FCB_TYPE_SILENCE) { if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {
skip_bits(gb, 8); bitstream_skip(bc, 8);
} else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {
int tmp = get_bits(gb, 6); int tmp = bitstream_read(bc, 6);
if (tmp >= 0x36) { if (tmp >= 0x36) {
skip_bits(gb, 2); bitstream_skip(bc, 2);
aw_idx_is_ext = 1; aw_idx_is_ext = 1;
} }
} }
...@@ -1722,9 +1722,9 @@ static int check_bits_for_superframe(GetBitContext *orig_gb, ...@@ -1722,9 +1722,9 @@ static int check_bits_for_superframe(GetBitContext *orig_gb,
} else } else
need_bits = 0; need_bits = 0;
need_bits += frame_desc->frame_size; need_bits += frame_desc->frame_size;
if (get_bits_left(gb) < need_bits) if (bitstream_bits_left(bc) < need_bits)
return 1; return 1;
skip_bits_long(gb, need_bits); bitstream_skip(bc, need_bits);
} }
return 0; return 0;
...@@ -1733,7 +1733,7 @@ static int check_bits_for_superframe(GetBitContext *orig_gb, ...@@ -1733,7 +1733,7 @@ static int check_bits_for_superframe(GetBitContext *orig_gb,
/** /**
* Synthesize output samples for a single superframe. If we have any data * Synthesize output samples for a single superframe. If we have any data
* cached in s->sframe_cache, that will be used instead of whatever is loaded * cached in s->sframe_cache, that will be used instead of whatever is loaded
* in s->gb. * in s->bc.
* *
* WMA Voice superframes contain 3 frames, each containing 160 audio samples, * WMA Voice superframes contain 3 frames, each containing 160 audio samples,
* to give a total of 480 samples per frame. See #synth_frame() for frame * to give a total of 480 samples per frame. See #synth_frame() for frame
...@@ -1751,7 +1751,7 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1751,7 +1751,7 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
int *got_frame_ptr) int *got_frame_ptr)
{ {
WMAVoiceContext *s = ctx->priv_data; WMAVoiceContext *s = ctx->priv_data;
GetBitContext *gb = &s->gb, s_gb; BitstreamContext *bc = &s->bc, s_bc;
int n, res, n_samples = 480; int n, res, n_samples = 480;
double lsps[MAX_FRAMES][MAX_LSPS]; double lsps[MAX_FRAMES][MAX_LSPS];
const double *mean_lsf = s->lsps == 16 ? const double *mean_lsf = s->lsps == 16 ?
...@@ -1766,12 +1766,12 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1766,12 +1766,12 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
s->history_nsamples * sizeof(*excitation)); s->history_nsamples * sizeof(*excitation));
if (s->sframe_cache_size > 0) { if (s->sframe_cache_size > 0) {
gb = &s_gb; bc = &s_bc;
init_get_bits(gb, s->sframe_cache, s->sframe_cache_size); bitstream_init(bc, s->sframe_cache, s->sframe_cache_size);
s->sframe_cache_size = 0; s->sframe_cache_size = 0;
} }
if ((res = check_bits_for_superframe(gb, s)) == 1) { if ((res = check_bits_for_superframe(bc, s)) == 1) {
*got_frame_ptr = 0; *got_frame_ptr = 0;
return 1; return 1;
} else if (res < 0) } else if (res < 0)
...@@ -1781,14 +1781,14 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1781,14 +1781,14 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
* speech samples (the actual codec) and WMAVoice music samples, which * speech samples (the actual codec) and WMAVoice music samples, which
* are really WMAPro-in-WMAVoice-superframes. I've never seen those in * are really WMAPro-in-WMAVoice-superframes. I've never seen those in
* the wild yet. */ * the wild yet. */
if (!get_bits1(gb)) { if (!bitstream_read_bit(bc)) {
avpriv_request_sample(ctx, "WMAPro-in-WMAVoice"); avpriv_request_sample(ctx, "WMAPro-in-WMAVoice");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
/* (optional) nr. of samples in superframe; always <= 480 and >= 0 */ /* (optional) nr. of samples in superframe; always <= 480 and >= 0 */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
if ((n_samples = get_bits(gb, 12)) > 480) { if ((n_samples = bitstream_read(bc, 12)) > 480) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"Superframe encodes >480 samples (%d), not allowed\n", "Superframe encodes >480 samples (%d), not allowed\n",
n_samples); n_samples);
...@@ -1803,9 +1803,9 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1803,9 +1803,9 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
prev_lsps[n] = s->prev_lsps[n] - mean_lsf[n]; prev_lsps[n] = s->prev_lsps[n] - mean_lsf[n];
if (s->lsps == 10) { if (s->lsps == 10) {
dequant_lsp10r(gb, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode); dequant_lsp10r(bc, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode);
} else /* s->lsps == 16 */ } else /* s->lsps == 16 */
dequant_lsp16r(gb, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode); dequant_lsp16r(bc, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode);
for (n = 0; n < s->lsps; n++) { for (n = 0; n < s->lsps; n++) {
lsps[0][n] = mean_lsf[n] + (a1[n] - a2[n * 2]); lsps[0][n] = mean_lsf[n] + (a1[n] - a2[n * 2]);
...@@ -1831,16 +1831,16 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1831,16 +1831,16 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
int m; int m;
if (s->lsps == 10) { if (s->lsps == 10) {
dequant_lsp10i(gb, lsps[n]); dequant_lsp10i(bc, lsps[n]);
} else /* s->lsps == 16 */ } else /* s->lsps == 16 */
dequant_lsp16i(gb, lsps[n]); dequant_lsp16i(bc, lsps[n]);
for (m = 0; m < s->lsps; m++) for (m = 0; m < s->lsps; m++)
lsps[n][m] += mean_lsf[m]; lsps[n][m] += mean_lsf[m];
stabilize_lsps(lsps[n], s->lsps); stabilize_lsps(lsps[n], s->lsps);
} }
if ((res = synth_frame(ctx, gb, n, if ((res = synth_frame(ctx, bc, n,
&samples[n * MAX_FRAMESIZE], &samples[n * MAX_FRAMESIZE],
lsps[n], n == 0 ? s->prev_lsps : lsps[n - 1], lsps[n], n == 0 ? s->prev_lsps : lsps[n - 1],
&excitation[s->history_nsamples + n * MAX_FRAMESIZE], &excitation[s->history_nsamples + n * MAX_FRAMESIZE],
...@@ -1853,9 +1853,9 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1853,9 +1853,9 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
/* Statistics? FIXME - we don't check for length, a slight overrun /* Statistics? FIXME - we don't check for length, a slight overrun
* will be caught by internal buffer padding, and anything else * will be caught by internal buffer padding, and anything else
* will be skipped, not read. */ * will be skipped, not read. */
if (get_bits1(gb)) { if (bitstream_read_bit(bc)) {
res = get_bits(gb, 4); res = bitstream_read(bc, 4);
skip_bits(gb, 10 * (res + 1)); bitstream_skip(bc, 10 * (res + 1));
} }
*got_frame_ptr = 1; *got_frame_ptr = 1;
...@@ -1883,31 +1883,31 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, ...@@ -1883,31 +1883,31 @@ static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
*/ */
static int parse_packet_header(WMAVoiceContext *s) static int parse_packet_header(WMAVoiceContext *s)
{ {
GetBitContext *gb = &s->gb; BitstreamContext *bc = &s->bc;
unsigned int res; unsigned int res;
if (get_bits_left(gb) < 11) if (bitstream_bits_left(bc) < 11)
return 1; return 1;
skip_bits(gb, 4); // packet sequence number bitstream_skip(bc, 4); // packet sequence number
s->has_residual_lsps = get_bits1(gb); s->has_residual_lsps = bitstream_read_bit(bc);
do { do {
res = get_bits(gb, 6); // number of superframes per packet res = bitstream_read(bc, 6); // number of superframes per packet
// (minus first one if there is spillover) // (minus first one if there is spillover)
if (get_bits_left(gb) < 6 * (res == 0x3F) + s->spillover_bitsize) if (bitstream_bits_left(bc) < 6 * (res == 0x3F) + s->spillover_bitsize)
return 1; return 1;
} while (res == 0x3F); } while (res == 0x3F);
s->spillover_nbits = get_bits(gb, s->spillover_bitsize); s->spillover_nbits = bitstream_read(bc, s->spillover_bitsize);
return 0; return 0;
} }
/** /**
* Copy (unaligned) bits from gb/data/size to pb. * Copy (unaligned) bits from bc/data/size to pb.
* *
* @param pb target buffer to copy bits into * @param pb target buffer to copy bits into
* @param data source buffer to copy bits from * @param data source buffer to copy bits from
* @param size size of the source data, in bytes * @param size size of the source data, in bytes
* @param gb bit I/O context specifying the current position in the source. * @param bc bit I/O context specifying the current position in the source.
* data. This function might use this to align the bit position to * data. This function might use this to align the bit position to
* a whole-byte boundary before calling #avpriv_copy_bits() on aligned * a whole-byte boundary before calling #avpriv_copy_bits() on aligned
* source data * source data
...@@ -1918,18 +1918,18 @@ static int parse_packet_header(WMAVoiceContext *s) ...@@ -1918,18 +1918,18 @@ static int parse_packet_header(WMAVoiceContext *s)
*/ */
static void copy_bits(PutBitContext *pb, static void copy_bits(PutBitContext *pb,
const uint8_t *data, int size, const uint8_t *data, int size,
GetBitContext *gb, int nbits) BitstreamContext *bc, int nbits)
{ {
int rmn_bytes, rmn_bits; int rmn_bytes, rmn_bits;
rmn_bits = rmn_bytes = get_bits_left(gb); rmn_bits = rmn_bytes = bitstream_bits_left(bc);
if (rmn_bits < nbits) if (rmn_bits < nbits)
return; return;
if (nbits > pb->size_in_bits - put_bits_count(pb)) if (nbits > pb->size_in_bits - put_bits_count(pb))
return; return;
rmn_bits &= 7; rmn_bytes >>= 3; rmn_bits &= 7; rmn_bytes >>= 3;
if ((rmn_bits = FFMIN(rmn_bits, nbits)) > 0) if ((rmn_bits = FFMIN(rmn_bits, nbits)) > 0)
put_bits(pb, rmn_bits, get_bits(gb, rmn_bits)); put_bits(pb, rmn_bits, bitstream_read(bc, rmn_bits));
avpriv_copy_bits(pb, data + size - rmn_bytes, avpriv_copy_bits(pb, data + size - rmn_bytes,
FFMIN(nbits - rmn_bits, rmn_bytes << 3)); FFMIN(nbits - rmn_bits, rmn_bytes << 3));
} }
...@@ -1949,7 +1949,7 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data, ...@@ -1949,7 +1949,7 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
int *got_frame_ptr, AVPacket *avpkt) int *got_frame_ptr, AVPacket *avpkt)
{ {
WMAVoiceContext *s = ctx->priv_data; WMAVoiceContext *s = ctx->priv_data;
GetBitContext *gb = &s->gb; BitstreamContext *bc = &s->bc;
int size, res, pos; int size, res, pos;
/* Packets are sometimes a multiple of ctx->block_align, with a packet /* Packets are sometimes a multiple of ctx->block_align, with a packet
...@@ -1962,7 +1962,7 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data, ...@@ -1962,7 +1962,7 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
*got_frame_ptr = 0; *got_frame_ptr = 0;
return 0; return 0;
} }
init_get_bits(&s->gb, avpkt->data, size << 3); bitstream_init8(&s->bc, avpkt->data, size);
/* size == ctx->block_align is used to indicate whether we are dealing with /* size == ctx->block_align is used to indicate whether we are dealing with
* a new packet or a packet of which we already read the packet header * a new packet or a packet of which we already read the packet header
...@@ -1976,8 +1976,8 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data, ...@@ -1976,8 +1976,8 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
* continuing to parse new superframes in the current packet. */ * continuing to parse new superframes in the current packet. */
if (s->spillover_nbits > 0) { if (s->spillover_nbits > 0) {
if (s->sframe_cache_size > 0) { if (s->sframe_cache_size > 0) {
int cnt = get_bits_count(gb); int cnt = bitstream_tell(bc);
copy_bits(&s->pb, avpkt->data, size, gb, s->spillover_nbits); copy_bits(&s->pb, avpkt->data, size, bc, s->spillover_nbits);
flush_put_bits(&s->pb); flush_put_bits(&s->pb);
s->sframe_cache_size += s->spillover_nbits; s->sframe_cache_size += s->spillover_nbits;
if ((res = synth_superframe(ctx, data, got_frame_ptr)) == 0 && if ((res = synth_superframe(ctx, data, got_frame_ptr)) == 0 &&
...@@ -1986,33 +1986,33 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data, ...@@ -1986,33 +1986,33 @@ static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
s->skip_bits_next = cnt & 7; s->skip_bits_next = cnt & 7;
return cnt >> 3; return cnt >> 3;
} else } else
skip_bits_long (gb, s->spillover_nbits - cnt + bitstream_skip (bc, s->spillover_nbits - cnt +
get_bits_count(gb)); // resync bitstream_tell(bc)); // resync
} else } else
skip_bits_long(gb, s->spillover_nbits); // resync bitstream_skip(bc, s->spillover_nbits); // resync
} }
} else if (s->skip_bits_next) } else if (s->skip_bits_next)
skip_bits(gb, s->skip_bits_next); bitstream_skip(bc, s->skip_bits_next);
/* Try parsing superframes in current packet */ /* Try parsing superframes in current packet */
s->sframe_cache_size = 0; s->sframe_cache_size = 0;
s->skip_bits_next = 0; s->skip_bits_next = 0;
pos = get_bits_left(gb); pos = bitstream_bits_left(bc);
if ((res = synth_superframe(ctx, data, got_frame_ptr)) < 0) { if ((res = synth_superframe(ctx, data, got_frame_ptr)) < 0) {
return res; return res;
} else if (*got_frame_ptr) { } else if (*got_frame_ptr) {
int cnt = get_bits_count(gb); int cnt = bitstream_tell(bc);
s->skip_bits_next = cnt & 7; s->skip_bits_next = cnt & 7;
return cnt >> 3; return cnt >> 3;
} else if ((s->sframe_cache_size = pos) > 0) { } else if ((s->sframe_cache_size = pos) > 0) {
/* rewind bit reader to start of last (incomplete) superframe... */ /* rewind bit reader to start of last (incomplete) superframe... */
init_get_bits(gb, avpkt->data, size << 3); bitstream_init8(bc, avpkt->data, size);
skip_bits_long(gb, (size << 3) - pos); bitstream_skip(bc, (size << 3) - pos);
assert(get_bits_left(gb) == pos); assert(bitstream_bits_left(bc) == pos);
/* ...and cache it for spillover in next packet */ /* ...and cache it for spillover in next packet */
init_put_bits(&s->pb, s->sframe_cache, SFRAME_CACHE_MAXSIZE); init_put_bits(&s->pb, s->sframe_cache, SFRAME_CACHE_MAXSIZE);
copy_bits(&s->pb, avpkt->data, size, gb, s->sframe_cache_size); copy_bits(&s->pb, avpkt->data, size, bc, s->sframe_cache_size);
// FIXME bad - just copy bytes as whole and add use the // FIXME bad - just copy bytes as whole and add use the
// skip_bits_next field // skip_bits_next field
} }
......
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