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;
......
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