*
* Copyright (c) 2002 The FFmpeg Project
*
- * Licensed under the GNU Lesser General Public License.
- * For licencing details see COPYING.LIB.
+ * Licensed under the GNU Lesser General Public License, see file COPYING.LIB.
*/
/** \file wmadec_filter.c paraslash's WMA decoder. */
* This decoder handles Microsoft Windows Media Audio data version 2.
*/
-#define _XOPEN_SOURCE 600
-
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <math.h>
-#include <string.h>
#include <regex.h>
#include <sys/select.h>
#include "para.h"
#include "error.h"
#include "list.h"
-#include "ggo.h"
#include "string.h"
#include "sched.h"
#include "buffer_tree.h"
#include "filter.h"
+#include "portable_io.h"
#include "bitstream.h"
#include "imdct.h"
#include "wma.h"
/** Information contained in the audio file header. */
struct asf_header_info ahi;
struct getbit_context gb;
- /** Whether to use the bit reservoir. */
- int use_bit_reservoir;
- /** Whether to use variable block length. */
- int use_variable_block_len;
- /** Whether to use exponent coding. */
- int use_exp_vlc;
/** Whether perceptual noise is added. */
int use_noise_coding;
/** Depends on number of the bits per second and the frame length. */
int byte_offset_bits;
- /** Only used if use_exp_vlc is true. */
+ /** Only used if ahi->use_exp_vlc is true. */
struct vlc exp_vlc;
uint16_t exponent_bands[BLOCK_NB_SIZES][25];
/** The index of the first coef in high band. */
struct vlc coef_vlc[2];
uint16_t *run_table[2];
uint16_t *level_table[2];
- const struct coef_vlc_table *coef_vlcs[2];
/** Frame length in samples. */
int frame_len;
/** log2 of frame_len. */
int frame_len_bits;
- /** Number of block sizes. */
+ /** Number of block sizes, one if !ahi->use_variable_block_len. */
int nb_block_sizes;
- /* block info */
- int reset_block_lengths;
+ /* Whether to update block lengths from getbit context. */
+ bool reset_block_lengths;
/** log2 of current block length. */
int block_len_bits;
/** log2 of next block length. */
int block_len;
/** Current position in frame. */
int block_pos;
- /** True if mid/side stereo mode. */
- uint8_t ms_stereo;
/** True if channel is coded. */
uint8_t channel_coded[MAX_CHANNELS];
/** log2 ratio frame/exp. length. */
};
#define EXPVLCBITS 8
-#define EXPMAX DIV_ROUND_UP(19, EXPVLCBITS)
-
#define HGAINVLCBITS 9
-#define HGAINMAX DIV_ROUND_UP(13, HGAINVLCBITS)
-
#define VLCBITS 9
-#define VLCMAX DIV_ROUND_UP(22, VLCBITS)
+
+/** \cond sine_winows */
#define SINE_WINDOW(x) static float sine_ ## x[x] __a_aligned(16)
static float *sine_windows[6] = {
sine_128, sine_256, sine_512, sine_1024, sine_2048, sine_4096
};
+/** \endcond sine_windows */
/* Generate a sine window. */
static void sine_window_init(float *window, int n)
window[i] = sinf((i + 0.5) * (M_PI / (2.0 * n)));
}
-static void wmadec_cleanup(struct private_wmadec_data *pwd)
-{
- int i;
-
- for (i = 0; i < pwd->nb_block_sizes; i++)
- imdct_end(pwd->mdct_ctx[i]);
- if (pwd->use_exp_vlc)
- free_vlc(&pwd->exp_vlc);
- if (pwd->use_noise_coding)
- free_vlc(&pwd->hgain_vlc);
- for (i = 0; i < 2; i++) {
- free_vlc(&pwd->coef_vlc[i]);
- free(pwd->run_table[i]);
- free(pwd->level_table[i]);
- }
-}
-
-static void init_coef_vlc(struct vlc *vlc, uint16_t **prun_table,
- uint16_t **plevel_table, const struct coef_vlc_table *vlc_table)
+static void init_coef_vlc(struct private_wmadec_data *pwd, int sidx, int didx)
{
- int n = vlc_table->n;
- const uint8_t *table_bits = vlc_table->huffbits;
- const uint32_t *table_codes = vlc_table->huffcodes;
- const uint16_t *levels_table = vlc_table->levels;
- uint16_t *run_table, *level_table;
- int i, l, j, k, level;
-
- init_vlc(vlc, VLCBITS, n, table_bits, table_codes, 4);
+ const struct coef_vlc_table *src = coef_vlcs + sidx;
+ struct vlc *dst = pwd->coef_vlc + didx;
+ int i, l, j, k, level, n = src->n;
- run_table = para_malloc(n * sizeof(uint16_t));
- level_table = para_malloc(n * sizeof(uint16_t));
+ init_vlc(dst, VLCBITS, n, src->huffbits, src->huffcodes, 4);
+ pwd->run_table[didx] = para_malloc(n * sizeof(uint16_t));
+ pwd->level_table[didx] = para_malloc(n * sizeof(uint16_t));
i = 2;
level = 1;
k = 0;
while (i < n) {
- l = levels_table[k++];
+ l = src->levels[k++];
for (j = 0; j < l; j++) {
- run_table[i] = j;
- level_table[i] = level;
+ pwd->run_table[didx][i] = j;
+ pwd->level_table[didx][i] = level;
i++;
}
level++;
}
- *prun_table = run_table;
- *plevel_table = level_table;
}
/* compute the scale factor band sizes for each MDCT block size */
else
pwd->frame_len_bits = 11;
pwd->frame_len = 1 << pwd->frame_len_bits;
- if (pwd->use_variable_block_len) {
+ if (pwd->ahi.use_variable_block_len) {
int nb_max, nb;
nb = ((flags2 >> 3) & 3) + 1;
if ((ahi->bit_rate / ahi->channels) >= 32000)
else
high_freq = high_freq * 0.5;
}
- PARA_INFO_LOG("channels=%d sample_rate=%d "
- "bitrate=%d block_align=%d\n",
+ PARA_INFO_LOG("channels=%u sample_rate=%u "
+ "bitrate=%u block_align=%d\n",
ahi->channels, ahi->sample_rate,
ahi->bit_rate, ahi->block_align);
PARA_INFO_LOG("frame_len=%d, bps=%f bps1=%f "
pwd->frame_len, bps, bps1,
high_freq, pwd->byte_offset_bits);
PARA_INFO_LOG("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
- pwd->use_noise_coding, pwd->use_exp_vlc, pwd->nb_block_sizes);
+ pwd->use_noise_coding, pwd->ahi.use_exp_vlc, pwd->nb_block_sizes);
compute_scale_factor_band_sizes(pwd, high_freq);
/* init MDCT windows : simple sinus window */
pwd->windows[i] = sine_windows[pwd->frame_len_bits - i - 7];
}
- pwd->reset_block_lengths = 1;
+ pwd->reset_block_lengths = true;
if (pwd->use_noise_coding) {
/* init the noise generator */
- if (pwd->use_exp_vlc)
+ if (pwd->ahi.use_exp_vlc)
pwd->noise_mult = 0.02;
else
pwd->noise_mult = 0.04;
}
/* choose the VLC tables for the coefficients */
- coef_vlc_table = 2;
+ coef_vlc_table = 4;
if (ahi->sample_rate >= 32000) {
if (bps1 < 0.72)
coef_vlc_table = 0;
else if (bps1 < 1.16)
- coef_vlc_table = 1;
+ coef_vlc_table = 2;
}
- pwd->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
- pwd->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
- init_coef_vlc(&pwd->coef_vlc[0], &pwd->run_table[0], &pwd->level_table[0],
- pwd->coef_vlcs[0]);
- init_coef_vlc(&pwd->coef_vlc[1], &pwd->run_table[1], &pwd->level_table[1],
- pwd->coef_vlcs[1]);
+ init_coef_vlc(pwd, coef_vlc_table, 0);
+ init_coef_vlc(pwd, coef_vlc_table + 1, 1);
return 0;
}
-static void wma_lsp_to_curve_init(struct private_wmadec_data *pwd, int frame_len)
+static void wma_lsp_to_curve_init(struct private_wmadec_data *pwd)
{
float wdel, a, b;
int i, e, m;
- wdel = M_PI / frame_len;
- for (i = 0; i < frame_len; i++)
+ wdel = M_PI / pwd->frame_len;
+ for (i = 0; i < pwd->frame_len; i++)
pwd->lsp_cos_table[i] = 2.0f * cos(wdel * i);
/* tables for x^-0.25 computation */
return ret;
}
- pwd->use_exp_vlc = pwd->ahi.flags2 & 0x0001;
- pwd->use_bit_reservoir = pwd->ahi.flags2 & 0x0002;
- pwd->use_variable_block_len = pwd->ahi.flags2 & 0x0004;
-
ret = wma_init(pwd);
if (ret < 0)
return ret;
wma_hgain_huffcodes, 2);
}
- if (pwd->use_exp_vlc) {
+ if (pwd->ahi.use_exp_vlc) {
PARA_INFO_LOG("using exp_vlc\n");
init_vlc(&pwd->exp_vlc, EXPVLCBITS, sizeof(wma_scale_huffbits),
wma_scale_huffbits, wma_scale_huffcodes, 4);
} else {
PARA_INFO_LOG("using curve\n");
- wma_lsp_to_curve_init(pwd, pwd->frame_len);
+ wma_lsp_to_curve_init(pwd);
}
*result = pwd;
return pwd->ahi.header_len;
last_exp = 36;
while (q < q_end) {
- code = get_vlc(&pwd->gb, pwd->exp_vlc.table, EXPVLCBITS, EXPMAX);
+ code = get_vlc(&pwd->gb, &pwd->exp_vlc);
if (code < 0)
return code;
/* NOTE: this offset is the same as MPEG4 AAC ! */
if (val == (int)0x80000000)
val = get_bits(&pwd->gb, 7) - 19;
else {
- int code = get_vlc(&pwd->gb,
- pwd->hgain_vlc.table, HGAINVLCBITS,
- HGAINMAX);
+ int code = get_vlc(&pwd->gb, &pwd->hgain_vlc);
if (code < 0)
return code;
val += code - 18;
}
/* very high freqs: noise */
n = pwd->block_len - pwd->coefs_end[bsize];
- mult1 = mult * exponents[((-1 << bsize)) >> esize];
+ mult1 = mult * exponents[(-(1 << bsize)) >> esize];
for (i = 0; i < n; i++) {
*coefs++ = pwd->noise_table[pwd->noise_index] * mult1;
pwd->noise_index = (pwd->noise_index + 1)
int ret, n, v, ch, code, bsize;
int coef_nb_bits, total_gain;
int nb_coefs[MAX_CHANNELS];
+ bool ms_stereo = false; /* mid/side stereo mode */
/* compute current block length */
- if (pwd->use_variable_block_len) {
+ if (pwd->ahi.use_variable_block_len) {
n = wma_log2(pwd->nb_block_sizes - 1) + 1;
if (pwd->reset_block_lengths) {
- pwd->reset_block_lengths = 0;
+ pwd->reset_block_lengths = false;
v = get_bits(&pwd->gb, n);
if (v >= pwd->nb_block_sizes)
return -E_WMA_BLOCK_SIZE;
return -E_INCOHERENT_BLOCK_LEN;
if (pwd->ahi.channels == 2)
- pwd->ms_stereo = get_bit(&pwd->gb);
+ ms_stereo = get_bit(&pwd->gb);
v = 0;
for (ch = 0; ch < pwd->ahi.channels; ch++) {
int a = get_bit(&pwd->gb);
if ((pwd->block_len_bits == pwd->frame_len_bits) || get_bit(&pwd->gb)) {
for (ch = 0; ch < pwd->ahi.channels; ch++) {
if (pwd->channel_coded[ch]) {
- if (pwd->use_exp_vlc) {
+ if (pwd->ahi.use_exp_vlc) {
ret = decode_exp_vlc(pwd, ch);
if (ret < 0)
return ret;
* special VLC tables are used for ms stereo because there is
* potentially less energy there
*/
- tindex = (ch == 1 && pwd->ms_stereo);
+ tindex = ch == 1 && ms_stereo;
coef_vlc = &pwd->coef_vlc[tindex];
run_table = pwd->run_table[tindex];
level_table = pwd->level_table[tindex];
eptr = ptr + nb_coefs[ch];
memset(ptr, 0, pwd->block_len * sizeof(int16_t));
for (;;) {
- code = get_vlc(&pwd->gb, coef_vlc->table,
- VLCBITS, VLCMAX);
+ code = get_vlc(&pwd->gb, coef_vlc);
if (code < 0)
return code;
if (code == 1) /* EOB */
}
}
compute_mdct_coefficients(pwd, bsize, total_gain, nb_coefs);
- if (pwd->ms_stereo && pwd->channel_coded[1]) {
+ if (ms_stereo && pwd->channel_coded[1]) {
float a, b;
int i;
/*
n4 = pwd->block_len / 2;
if (pwd->channel_coded[ch])
imdct(pwd->mdct_ctx[bsize], pwd->output, pwd->coefs[ch]);
- else if (!(pwd->ms_stereo && ch == 1))
+ else if (!(ms_stereo && ch == 1))
memset(pwd->output, 0, sizeof(pwd->output));
/* multiply by the window and add in the frame */
/* Decode a frame of frame_len samples. */
static int wma_decode_frame(struct private_wmadec_data *pwd, int16_t *samples)
{
- int ret, i, n, ch, incr;
+ int ret, i, ch;
int16_t *ptr;
float *iptr;
}
/* convert frame to integer */
- n = pwd->frame_len;
- incr = pwd->ahi.channels;
for (ch = 0; ch < pwd->ahi.channels; ch++) {
ptr = samples + ch;
iptr = pwd->frame_out[ch];
- for (i = 0; i < n; i++) {
+ for (i = 0; i < pwd->frame_len; i++) {
*ptr = av_clip_int16(lrintf(*iptr++));
- ptr += incr;
+ ptr += pwd->ahi.channels;
}
/* prepare for next block */
memmove(&pwd->frame_out[ch][0], &pwd->frame_out[ch][pwd->frame_len],
return 0;
}
-static int wma_decode_superframe(struct private_wmadec_data *pwd, void *data,
- int *data_size, const uint8_t *buf, int buf_size)
+static int wma_decode_superframe(struct private_wmadec_data *pwd, void *out,
+ int *out_size, const uint8_t *in)
{
- int ret;
- int16_t *samples;
+ int ret, in_size = pwd->ahi.packet_size - WMA_FRAME_SKIP;
+ int16_t *samples = out;
- if (buf_size == 0) {
- pwd->last_superframe_len = 0;
- return 0;
- }
- if (buf_size < pwd->ahi.block_align)
- return 0;
- buf_size = pwd->ahi.block_align;
- samples = data;
- init_get_bits(&pwd->gb, buf, buf_size);
- if (pwd->use_bit_reservoir) {
+ init_get_bits(&pwd->gb, in, in_size);
+ if (pwd->ahi.use_bit_reservoir) {
int i, nb_frames, bit_offset, pos, len;
uint8_t *q;
// PARA_DEBUG_LOG("have %d frames\n", nb_frames);
ret = -E_WMA_OUTPUT_SPACE;
if ((nb_frames + 1) * pwd->ahi.channels * pwd->frame_len
- * sizeof(int16_t) > *data_size)
+ * sizeof(int16_t) > *out_size)
goto fail;
bit_offset = get_bits(&pwd->gb, pwd->byte_offset_bits + 3);
/* read each frame starting from bit_offset */
pos = bit_offset + 4 + 4 + pwd->byte_offset_bits + 3;
- init_get_bits(&pwd->gb, buf + (pos >> 3),
+ init_get_bits(&pwd->gb, in + (pos >> 3),
(MAX_CODED_SUPERFRAME_SIZE - (pos >> 3)));
len = pos & 7;
if (len > 0)
skip_bits(&pwd->gb, len);
- pwd->reset_block_lengths = 1;
+ pwd->reset_block_lengths = true;
for (i = 0; i < nb_frames; i++) {
ret = wma_decode_frame(pwd, samples);
if (ret < 0)
((bit_offset + 4 + 4 + pwd->byte_offset_bits + 3) & ~7);
pwd->last_bitoffset = pos & 7;
pos >>= 3;
- len = buf_size - pos;
+ len = in_size - pos;
ret = -E_WMA_BAD_SUPERFRAME;
if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
goto fail;
pwd->last_superframe_len = len;
- memcpy(pwd->last_superframe, buf + pos, len);
+ memcpy(pwd->last_superframe, in + pos, len);
} else {
PARA_DEBUG_LOG("not using bit reservoir\n");
ret = -E_WMA_OUTPUT_SPACE;
- if (pwd->ahi.channels * pwd->frame_len * sizeof(int16_t) > *data_size)
+ if (pwd->ahi.channels * pwd->frame_len * sizeof(int16_t) > *out_size)
goto fail;
/* single frame decode */
ret = wma_decode_frame(pwd, samples);
}
PARA_DEBUG_LOG("frame_len: %d, block_len: %d, outbytes: %d, eaten: %d\n",
pwd->frame_len, pwd->block_len,
- (int)((int8_t *)samples - (int8_t *)data), pwd->ahi.block_align);
- *data_size = (int8_t *)samples - (int8_t *)data;
+ (int)((int8_t *)samples - (int8_t *)out), pwd->ahi.block_align);
+ *out_size = (int8_t *)samples - (int8_t *)out;
return pwd->ahi.block_align;
fail:
/* reset the bit reservoir on errors */
static void wmadec_close(struct filter_node *fn)
{
struct private_wmadec_data *pwd = fn->private_data;
+ int i;
if (!pwd)
return;
- wmadec_cleanup(pwd);
+ for (i = 0; i < pwd->nb_block_sizes; i++)
+ imdct_end(pwd->mdct_ctx[i]);
+ if (pwd->ahi.use_exp_vlc)
+ free_vlc(&pwd->exp_vlc);
+ if (pwd->use_noise_coding)
+ free_vlc(&pwd->hgain_vlc);
+ for (i = 0; i < 2; i++) {
+ free_vlc(&pwd->coef_vlc[i]);
+ free(pwd->run_table[i]);
+ free(pwd->level_table[i]);
+ }
free(fn->private_data);
fn->private_data = NULL;
}
#define WMA_OUTPUT_BUFFER_SIZE (128 * 1024)
-static int wmadec_post_select(__a_unused struct sched *s, struct task *t)
+static int wmadec_post_select(__a_unused struct sched *s, void *context)
{
- struct filter_node *fn = container_of(t, struct filter_node, task);
+ struct filter_node *fn = context;
int ret, converted, out_size;
struct private_wmadec_data *pwd = fn->private_data;
struct btr_node *btrn = fn->btrn;
if (ret == 0)
return 0;
btr_merge(btrn, fn->min_iqs);
- len = btr_next_buffer(btrn, (char **)&in);
+ len = btr_next_buffer(btrn, &in);
ret = -E_WMADEC_EOF;
if (len < fn->min_iqs)
goto err;
fn->min_iqs += 4096;
goto next_buffer;
}
- fn->min_iqs = 2 * (WMA_FRAME_SKIP + pwd->ahi.block_align);
+ fn->min_iqs = 2 * pwd->ahi.packet_size;
fn->private_data = pwd;
converted = pwd->ahi.header_len;
goto success;
}
- fn->min_iqs = WMA_FRAME_SKIP + pwd->ahi.block_align;
+ fn->min_iqs = pwd->ahi.packet_size;
if (fn->min_iqs > len)
goto success;
out_size = WMA_OUTPUT_BUFFER_SIZE;
out = para_malloc(out_size);
ret = wma_decode_superframe(pwd, out, &out_size,
- (uint8_t *)in + WMA_FRAME_SKIP, len - WMA_FRAME_SKIP);
+ (uint8_t *)in + WMA_FRAME_SKIP);
if (ret < 0) {
free(out);
goto err;
}
- out = para_realloc(out, out_size);
- if (out_size > 0)
+ if (out_size > 0) {
+ out = para_realloc(out, out_size);
btr_add_output(out, out_size, btrn);
- converted += ret + WMA_FRAME_SKIP;
+ } else
+ free(out);
+ converted += pwd->ahi.packet_size;
success:
btr_consume(btrn, converted);
return 0;
fn->min_iqs = 4096;
}
-/**
- * The init function of the wma decoder.
- *
- * \param f Its fields are filled in by the function.
- */
-void wmadec_filter_init(struct filter *f)
-{
- f->open = wmadec_open;
- f->close = wmadec_close;
- f->execute = wmadec_execute;
- f->pre_select = generic_filter_pre_select;
- f->post_select = wmadec_post_select;
-}
+const struct filter lsg_filter_cmd_com_wmadec_user_data = {
+ .open = wmadec_open,
+ .close = wmadec_close,
+ .execute = wmadec_execute,
+ .pre_select = generic_filter_pre_select,
+ .post_select = wmadec_post_select,
+};