3 * Copyright (c) 2002-2007 The FFmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32/* XXX: use same run/length optimization as mpeg decoders */
33// FIXME maybe split decode / encode or pass flag
35 float **plevel_table, uint16_t **pint_table,
39 const uint8_t *table_bits = vlc_table->
huffbits;
40 const uint32_t *table_codes = vlc_table->
huffcodes;
41 const uint16_t *levels_table = vlc_table->
levels;
42 uint16_t *run_table, *int_table;
44 int i, l, j, k,
level, ret;
46 ret =
vlc_init(vlc,
VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
53 if (!run_table || !flevel_table || !int_table) {
64 l = levels_table[k++];
65 for (j = 0; j < l; j++) {
72 *prun_table = run_table;
73 *plevel_table = flevel_table;
74 *pint_table = int_table;
84 float bps1, high_freq;
100 /* compute MDCT block size */
103 s->next_block_len_bits =
s->frame_len_bits;
104 s->prev_block_len_bits =
s->frame_len_bits;
105 s->block_len_bits =
s->frame_len_bits;
107 s->frame_len = 1 <<
s->frame_len_bits;
108 if (
s->use_variable_block_len) {
110 nb = ((flags2 >> 3) & 3) + 1;
116 s->nb_block_sizes = nb + 1;
118 s->nb_block_sizes = 1;
120 /* init rate dependent parameters */
121 s->use_noise_coding = 1;
124 /* if version 2, then the rates are normalized */
126 if (
s->version == 2) {
127 if (sample_rate1 >= 44100)
128 sample_rate1 = 44100;
129 else if (sample_rate1 >= 22050)
130 sample_rate1 = 22050;
131 else if (sample_rate1 >= 16000)
132 sample_rate1 = 16000;
133 else if (sample_rate1 >= 11025)
134 sample_rate1 = 11025;
135 else if (sample_rate1 >= 8000)
141 s->byte_offset_bits =
av_log2((
int) (
bps *
s->frame_len / 8.0 + 0.5)) + 2;
147 /* compute high frequency value and choose if noise coding should
152 if (sample_rate1 == 44100) {
154 s->use_noise_coding = 0;
156 high_freq = high_freq * 0.4;
157 }
else if (sample_rate1 == 22050) {
159 s->use_noise_coding = 0;
160 else if (bps1 >= 0.72)
161 high_freq = high_freq * 0.7;
163 high_freq = high_freq * 0.6;
164 }
else if (sample_rate1 == 16000) {
166 high_freq = high_freq * 0.5;
168 high_freq = high_freq * 0.3;
169 }
else if (sample_rate1 == 11025)
170 high_freq = high_freq * 0.7;
171 else if (sample_rate1 == 8000) {
173 high_freq = high_freq * 0.5;
175 s->use_noise_coding = 0;
177 high_freq = high_freq * 0.65;
180 high_freq = high_freq * 0.75;
182 high_freq = high_freq * 0.6;
184 high_freq = high_freq * 0.5;
186 ff_dlog(
s->avctx,
"flags2=0x%x\n", flags2);
187 ff_dlog(
s->avctx,
"version=%d channels=%d sample_rate=%d bitrate=%"PRId64
" block_align=%d\n",
190 ff_dlog(
s->avctx,
"bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
191 bps, bps1, high_freq,
s->byte_offset_bits);
192 ff_dlog(
s->avctx,
"use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
193 s->use_noise_coding,
s->use_exp_vlc,
s->nb_block_sizes);
195 /* compute the scale factor band sizes for each MDCT block size */
197 int a,
b,
pos, lpos, k, block_len,
i, j, n;
198 const uint8_t *
table;
204 for (k = 0; k <
s->nb_block_sizes; k++) {
205 block_len =
s->frame_len >> k;
207 if (
s->version == 1) {
209 for (
i = 0;
i < 25;
i++) {
212 pos = ((block_len * 2 *
a) + (
b >> 1)) /
b;
215 s->exponent_bands[0][
i] =
pos - lpos;
216 if (
pos >= block_len) {
222 s->exponent_sizes[0] =
i;
224 /* hardcoded tables */
237 for (
i = 0;
i < n;
i++)
239 s->exponent_sizes[k] = n;
243 for (
i = 0;
i < 25;
i++) {
246 pos = ((block_len * 2 *
a) + (
b << 1)) / (4 *
b);
251 s->exponent_bands[k][j++] =
pos - lpos;
252 if (
pos >= block_len)
256 s->exponent_sizes[k] = j;
260 /* max number of coefs */
261 s->coefs_end[k] = (
s->frame_len - ((
s->frame_len * 9) / 100)) >> k;
262 /* high freq computation */
263 s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
265 n =
s->exponent_sizes[k];
268 for (
i = 0;
i < n;
i++) {
271 pos +=
s->exponent_bands[k][
i];
273 if (start < s->high_band_start[k])
274 start =
s->high_band_start[k];
275 if (end >
s->coefs_end[k])
276 end =
s->coefs_end[k];
278 s->exponent_high_bands[k][j++] = end - start;
280 s->exponent_high_sizes[k] = j;
287 for (
i = 0;
i <
s->nb_block_sizes;
i++) {
290 s->exponent_sizes[
i]);
291 for (j = 0; j <
s->exponent_sizes[
i]; j++)
292 ff_tlog(
s->avctx,
" %d",
s->exponent_bands[
i][j]);
298 /* init MDCT windows : simple sine window */
299 for (
i = 0;
i <
s->nb_block_sizes;
i++) {
304 s->reset_block_lengths = 1;
306 if (
s->use_noise_coding) {
307 /* init the noise generator */
309 s->noise_mult = 0.02;
311 s->noise_mult = 0.04;
315 s->noise_table[
i] = 1.0 *
s->noise_mult;
321 norm = (1.0 / (
float) (1LL << 31)) * sqrt(3) *
s->noise_mult;
334 /* choose the VLC tables for the coefficients */
339 else if (bps1 < 1.16)
342 s->coef_vlcs[0] = &
coef_vlcs[coef_vlc_table * 2];
343 s->coef_vlcs[1] = &
coef_vlcs[coef_vlc_table * 2 + 1];
345 &
s->int_table[0],
s->coef_vlcs[0]);
350 &
s->int_table[1],
s->coef_vlcs[1]);
357 else if (total_gain < 32)
359 else if (total_gain < 40)
361 else if (total_gain < 45)
372 for (
i = 0;
i <
s->nb_block_sizes;
i++)
377 if (
s->use_noise_coding)
379 for (
i = 0;
i < 2;
i++) {
391 * Decode an uncompressed coefficient.
392 * @param gb GetBitContext
393 * @return the decoded coefficient
397 /** consumes up to 34 bits */
412 * Decode run level compressed coefficients.
413 * @param avctx codec context
414 * @param gb bitstream reader context
415 * @param vlc vlc table for get_vlc2
416 * @param level_table level codes
417 * @param run_table run codes
418 * @param version 0 for wma1,2 1 for wmapro
419 * @param ptr output buffer
420 * @param offset offset in the output buffer
421 * @param num_coefs number of input coefficients
422 * @param block_len input buffer length (2^n)
423 * @param frame_len_bits number of bits for escaped run codes
424 * @param coef_nb_bits number of bits for escaped level codes
425 * @return 0 on success, -1 otherwise
428 const VLCElem *vlc,
const float *level_table,
429 const uint16_t *run_table,
int version,
431 int block_len,
int frame_len_bits,
435 const uint32_t *ilvl = (
const uint32_t *) level_table;
436 uint32_t *iptr = (uint32_t *) ptr;
437 const unsigned int coef_mask = block_len - 1;
444 iptr[
offset & coef_mask] = ilvl[
code] ^ (sign & 0x80000000);
445 }
else if (
code == 1) {
452 /** NOTE: this is rather suboptimal. reading
453 * block_len_bits would be better */
462 "broken escape sequence\n");
474 /** NOTE: EOB can be omitted */
477 "overflow (%d > %d) in spectral RLE, ignoring\n",
Libavcodec external API header.
#define i(width, name, range_min, range_max)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static unsigned int get_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Macro definitions for various function/variable attributes.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Memory handling functions.
static const uint16_t table[]
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
SINETABLE_CONST float *const ff_sine_windows[]
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
const uint32_t * huffcodes
VLC bit values.
const uint8_t * huffbits
VLC bit size.
int n
total number of codes
const uint16_t * levels
table to build run/level tables
#define av_malloc_array(a, b)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
void ff_vlc_free(VLC *vlc)
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
int ff_wma_total_gain_to_bits(int total_gain)
av_cold int ff_wma_end(AVCodecContext *avctx)
static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table, float **plevel_table, uint16_t **pint_table, const CoefVLCTable *vlc_table)
av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
unsigned int ff_wma_get_large_val(GetBitContext *gb)
Decode an uncompressed coefficient.
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, const VLCElem *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits)
Decode run level compressed coefficients.
float WMACoef
type for decoded coefficients, int16_t would be enough for wma 1/2
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
const uint16_t ff_wma_critical_freqs[25]
static const uint8_t exponent_band_32000[3][25]
static const uint8_t exponent_band_22050[3][25]
static const CoefVLCTable coef_vlcs[6]
static const uint8_t exponent_band_44100[3][25]