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 @@
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "bitstream.h"
#include "internal.h"
#include "sinewin.h"
#include "wma.h"
......@@ -382,30 +383,30 @@ int ff_wma_end(AVCodecContext *avctx)
/**
* Decode an uncompressed coefficient.
* @param gb GetBitContext
* @param bc BitstreamContext
* @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 */
int n_bits = 8;
/** decode length */
if (get_bits1(gb)) {
if (bitstream_read_bit(bc)) {
n_bits += 8;
if (get_bits1(gb)) {
if (bitstream_read_bit(bc)) {
n_bits += 8;
if (get_bits1(gb))
if (bitstream_read_bit(bc))
n_bits += 7;
}
}
return get_bits_long(gb, n_bits);
return bitstream_read(bc, n_bits);
}
/**
* Decode run level compressed coefficients.
* @param avctx codec context
* @param gb bitstream reader context
* @param vlc vlc table for get_vlc2
* @param bc bitstream reader context
* @param vlc VLC table for bitstream_read_vlc
* @param level_table level codes
* @param run_table run codes
* @param version 0 for wma1,2 1 for wmapro
......@@ -417,7 +418,7 @@ unsigned int ff_wma_get_large_val(GetBitContext *gb)
* @param coef_nb_bits number of bits for escaped level codes
* @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,
const uint16_t *run_table, int version,
WMACoef *ptr, int offset, int num_coefs,
......@@ -429,11 +430,11 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
uint32_t *iptr = (uint32_t *) ptr;
const unsigned int coef_mask = block_len - 1;
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) {
/** normal code */
offset += run_table[code];
sign = get_bits1(gb) - 1;
sign = bitstream_read_bit(bc) - 1;
iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
} else if (code == 1) {
/** EOB */
......@@ -441,26 +442,26 @@ int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
} else {
/** escape */
if (!version) {
level = get_bits(gb, coef_nb_bits);
level = bitstream_read(bc, coef_nb_bits);
/** NOTE: this is rather suboptimal. reading
* block_len_bits would be better */
offset += get_bits(gb, frame_len_bits);
offset += bitstream_read(bc, frame_len_bits);
} else {
level = ff_wma_get_large_val(gb);
level = ff_wma_get_large_val(bc);
/** escape decode */
if (get_bits1(gb)) {
if (get_bits1(gb)) {
if (get_bits1(gb)) {
if (bitstream_read_bit(bc)) {
if (bitstream_read_bit(bc)) {
if (bitstream_read_bit(bc)) {
av_log(avctx, AV_LOG_ERROR,
"broken escape sequence\n");
return -1;
} else
offset += get_bits(gb, frame_len_bits) + 4;
offset += bitstream_read(bc, frame_len_bits) + 4;
} 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;
}
}
......
......@@ -25,8 +25,8 @@
#include "libavutil/float_dsp.h"
#include "avcodec.h"
#include "bitstream.h"
#include "fft.h"
#include "get_bits.h"
#include "put_bits.h"
/* size of blocks */
......@@ -66,7 +66,7 @@ typedef struct CoefVLCTable {
typedef struct WMACodecContext {
AVCodecContext *avctx;
GetBitContext gb;
BitstreamContext bc;
PutBitContext pb;
int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
int use_bit_reservoir;
......@@ -147,8 +147,8 @@ extern const uint8_t ff_aac_scalefactor_bits[121];
int ff_wma_init(AVCodecContext *avctx, int flags2);
int ff_wma_total_gain_to_bits(int total_gain);
int ff_wma_end(AVCodecContext *avctx);
unsigned int ff_wma_get_large_val(GetBitContext *gb);
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
unsigned int ff_wma_get_large_val(BitstreamContext *bc);
int ff_wma_run_level_decode(AVCodecContext *avctx, BitstreamContext *bc,
VLC *vlc, const float *level_table,
const uint16_t *run_table, int version,
WMACoef *ptr, int offset, int num_coefs,
......
......@@ -36,6 +36,7 @@
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "bitstream.h"
#include "internal.h"
#include "wma.h"
......@@ -209,9 +210,9 @@ static void decode_exp_lsp(WMACodecContext *s, int ch)
for (i = 0; i < NB_LSP_COEFS; i++) {
if (i == 0 || i >= 8)
val = get_bits(&s->gb, 3);
val = bitstream_read(&s->bc, 3);
else
val = get_bits(&s->gb, 4);
val = bitstream_read(&s->bc, 4);
lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
}
......@@ -318,7 +319,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
q_end = q + s->block_len;
max_scale = 0;
if (s->version == 1) {
last_exp = get_bits(&s->gb, 5) + 10;
last_exp = bitstream_read(&s->bc, 5) + 10;
v = ptab[last_exp];
iv = iptab[last_exp];
max_scale = v;
......@@ -333,7 +334,7 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
last_exp = 36;
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) {
av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
return -1;
......@@ -436,7 +437,7 @@ static int wma_decode_block(WMACodecContext *s)
if (s->reset_block_lengths) {
s->reset_block_lengths = 0;
v = get_bits(&s->gb, n);
v = bitstream_read(&s->bc, n);
if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR,
"prev_block_len_bits %d out of range\n",
......@@ -444,7 +445,7 @@ static int wma_decode_block(WMACodecContext *s)
return -1;
}
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) {
av_log(s->avctx, AV_LOG_ERROR,
"block_len_bits %d out of range\n",
......@@ -457,7 +458,7 @@ static int wma_decode_block(WMACodecContext *s)
s->prev_block_len_bits = s->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) {
av_log(s->avctx, AV_LOG_ERROR,
"next_block_len_bits %d out of range\n",
......@@ -480,10 +481,10 @@ static int wma_decode_block(WMACodecContext *s)
}
if (s->avctx->channels == 2)
s->ms_stereo = get_bits1(&s->gb);
s->ms_stereo = bitstream_read_bit(&s->bc);
v = 0;
for (ch = 0; ch < s->avctx->channels; ch++) {
a = get_bits1(&s->gb);
a = bitstream_read_bit(&s->bc);
s->channel_coded[ch] = a;
v |= a;
}
......@@ -499,7 +500,7 @@ static int wma_decode_block(WMACodecContext *s)
* coef escape coding */
total_gain = 1;
for (;;) {
a = get_bits(&s->gb, 7);
a = bitstream_read(&s->bc, 7);
total_gain += a;
if (a != 127)
break;
......@@ -519,7 +520,7 @@ static int wma_decode_block(WMACodecContext *s)
int i, n, a;
n = s->exponent_high_sizes[bsize];
for (i = 0; i < n; i++) {
a = get_bits1(&s->gb);
a = bitstream_read_bit(&s->bc);
s->high_band_coded[ch][i] = a;
/* if noise coding, the coefficients are not transmitted */
if (a)
......@@ -536,10 +537,11 @@ static int wma_decode_block(WMACodecContext *s)
for (i = 0; i < n; i++) {
if (s->high_band_coded[ch][i]) {
if (val == (int) 0x80000000) {
val = get_bits(&s->gb, 7) - 19;
val = bitstream_read(&s->bc, 7) - 19;
} else {
code = get_vlc2(&s->gb, s->hgain_vlc.table,
HGAINVLCBITS, HGAINMAX);
code = bitstream_read_vlc(&s->bc,
s->hgain_vlc.table,
HGAINVLCBITS, HGAINMAX);
if (code < 0) {
av_log(s->avctx, AV_LOG_ERROR,
"hgain vlc invalid\n");
......@@ -555,7 +557,7 @@ static int wma_decode_block(WMACodecContext *s)
}
/* 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++) {
if (s->channel_coded[ch]) {
if (s->use_exp_vlc) {
......@@ -579,13 +581,13 @@ static int wma_decode_block(WMACodecContext *s)
* there is potentially less energy there */
tindex = (ch == 1 && s->ms_stereo);
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],
0, ptr, 0, nb_coefs[ch],
s->block_len, s->frame_len_bits, coef_nb_bits);
}
if (s->version == 1 && s->avctx->channels >= 2)
align_get_bits(&s->gb);
bitstream_align(&s->bc);
}
/* normalize */
......@@ -810,12 +812,12 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
}
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) {
/* read super frame header */
skip_bits(&s->gb, 4); /* super frame index */
nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
bitstream_skip(&s->bc, 4); /* super frame index */
nb_frames = bitstream_read(&s->bc, 4) - (s->last_superframe_len <= 0);
} else
nb_frames = 1;
......@@ -829,11 +831,11 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
samples_offset = 0;
if (s->use_bit_reservoir) {
bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
if (bit_offset > get_bits_left(&s->gb)) {
bit_offset = bitstream_read(&s->bc, s->byte_offset_bits + 3);
if (bit_offset > bitstream_bits_left(&s->bc)) {
av_log(avctx, AV_LOG_ERROR,
"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;
}
......@@ -845,19 +847,19 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
q = s->last_superframe + s->last_superframe_len;
len = bit_offset;
while (len > 7) {
*q++ = (get_bits) (&s->gb, 8);
*q++ = bitstream_read(&s->bc, 8);
len -= 8;
}
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);
/* XXX: bit_offset bits into last frame */
init_get_bits(&s->gb, s->last_superframe,
s->last_superframe_len * 8 + bit_offset);
bitstream_init(&s->bc, s->last_superframe,
s->last_superframe_len * 8 + bit_offset);
/* skip unused bits */
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
* current one */
if (wma_decode_frame(s, samples, samples_offset) < 0)
......@@ -870,10 +872,10 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
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;
if (len > 0)
skip_bits(&s->gb, len);
bitstream_skip(&s->bc, len);
s->reset_block_lengths = 1;
for (i = 0; i < nb_frames; i++) {
......@@ -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 */
pos = get_bits_count(&s->gb) +
pos = bitstream_tell(&s->bc) +
((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
s->last_bitoffset = pos & 7;
pos >>= 3;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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