2 * Wmapro compatible decoder
3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4 * Copyright (c) 2008 - 2009 Sascha Sommer, Benjamin Larsson
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/wmaprodec.c
25 * @brief wmapro decoder implementation
26 * Wmapro is an MDCT based codec comparable to wma standard or AAC.
27 * The decoding therefore consists of the following steps:
28 * - bitstream decoding
29 * - reconstruction of per-channel data
30 * - rescaling and inverse quantization
32 * - windowing and overlapp-add
34 * The compressed wmapro bitstream is split into individual packets.
35 * Every such packet contains one or more wma frames.
36 * The compressed frames may have a variable length and frames may
37 * cross packet boundaries.
38 * Common to all wmapro frames is the number of samples that are stored in
40 * The number of samples and a few other decode flags are stored
41 * as extradata that has to be passed to the decoder.
43 * The wmapro frames themselves are again split into a variable number of
44 * subframes. Every subframe contains the data for 2^N time domain samples
45 * where N varies between 7 and 12.
47 * Example wmapro bitstream (in samples):
49 * || packet 0 || packet 1 || packet 2 packets
50 * ---------------------------------------------------
51 * || frame 0 || frame 1 || frame 2 || frames
52 * ---------------------------------------------------
53 * || | | || | | | || || subframes of channel 0
54 * ---------------------------------------------------
55 * || | | || | | | || || subframes of channel 1
56 * ---------------------------------------------------
58 * The frame layouts for the individual channels of a wma frame does not need
61 * However, if the offsets and lengths of several subframes of a frame are the
62 * same, the subframes of the channels can be grouped.
63 * Every group may then use special coding techniques like M/S stereo coding
64 * to improve the compression ratio. These channel transformations do not
65 * need to be applied to a whole subframe. Instead, they can also work on
66 * individual scale factor bands (see below).
67 * The coefficients that carry the audio signal in the frequency domain
68 * are transmitted as huffman-coded vectors with 4, 2 and 1 elements.
69 * In addition to that, the encoder can switch to a runlevel coding scheme
70 * by transmitting subframe_length / 128 zero coefficients.
72 * Before the audio signal can be converted to the time domain, the
73 * coefficients have to be rescaled and inverse quantized.
74 * A subframe is therefore split into several scale factor bands that get
75 * scaled individually.
76 * Scale factors are submitted for every frame but they might be shared
77 * between the subframes of a channel. Scale factors are initially DPCM-coded.
78 * Once scale factors are shared, the differences are transmitted as runlevel
80 * Every subframe length and offset combination in the frame layout shares a
81 * common quantization factor that can be adjusted for every channel by a
83 * After the inverse quantization, the coefficients get processed by an IMDCT.
84 * The resulting values are then windowed with a sine window and the first half
85 * of the values are added to the second half of the output from the previous
86 * subframe in order to reconstruct the output samples.
90 *@brief Uninitialize the decoder and free all resources.
91 *@param avctx codec context
92 *@return 0 on success, < 0 otherwise
94 static av_cold int decode_end(AVCodecContext *avctx)
96 WMA3DecodeContext *s = avctx->priv_data;
99 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
100 ff_mdct_end(&s->mdct_ctx[i]);
106 *@brief Calculate a decorrelation matrix from the bitstream parameters.
107 *@param s codec context
108 *@param chgroup channel group for which the matrix needs to be calculated
110 static void decode_decorrelation_matrix(WMA3DecodeContext *s,
111 WMA3ChannelGroup *chgroup)
115 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
116 memset(chgroup->decorrelation_matrix, 0,
117 sizeof(float) *s->num_channels * s->num_channels);
119 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
120 rotation_offset[i] = get_bits(&s->gb, 6);
122 for (i = 0; i < chgroup->num_channels; i++)
123 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
124 get_bits1(&s->gb) ? 1.0 : -1.0;
126 for (i = 1; i < chgroup->num_channels; i++) {
128 for (x = 0; x < i; x++) {
130 for (y = 0; y < i + 1; y++) {
131 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
132 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
133 int n = rotation_offset[offset + x];
145 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
146 (v1 * sinv) - (v2 * cosv);
147 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
148 (v1 * cosv) + (v2 * sinv);
156 *@brief Extract the coefficients from the bitstream.
157 *@param s codec context
158 *@param c current channel number
159 *@return 0 on success, < 0 in case of bitstream errors
161 static int decode_coeffs(WMA3DecodeContext *s, int c)
165 WMA3ChannelCtx* ci = &s->channel[c];
170 const uint16_t* level;
172 dprintf(s->avctx, "decode coefficients for channel %i\n", c);
174 vlctable = get_bits1(&s->gb);
175 vlc = &coef_vlc[vlctable];
185 /** decode vector coefficients (consumes up to 167 bits per iteration for
186 4 vector coded large values) */
187 while (!rl_mode && cur_coeff + 3 < s->subframe_len) {
192 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
194 if (idx == HUFF_VEC4_SIZE - 1) {
195 for (i = 0; i < 4; i += 2) {
196 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
197 if (idx == HUFF_VEC2_SIZE - 1) {
198 vals[i] = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
199 if (vals[i] == HUFF_VEC1_SIZE - 1)
200 vals[i] += ff_wma_get_large_val(&s->gb);
201 vals[i+1] = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
202 if (vals[i+1] == HUFF_VEC1_SIZE - 1)
203 vals[i+1] += ff_wma_get_large_val(&s->gb);
205 vals[i] = symbol_to_vec2[idx] >> 4;
206 vals[i+1] = symbol_to_vec2[idx] & 0xF;
210 vals[0] = symbol_to_vec4[idx] >> 12;
211 vals[1] = (symbol_to_vec4[idx] >> 8) & 0xF;
212 vals[2] = (symbol_to_vec4[idx] >> 4) & 0xF;
213 vals[3] = symbol_to_vec4[idx] & 0xF;
217 for (i = 0; i < 4; i++) {
219 int sign = get_bits1(&s->gb) - 1;
220 ci->coeffs[cur_coeff] = (vals[i]^sign) - sign;
223 /** switch to run level mode when subframe_len / 128 zeros
224 were found in a row */
225 rl_mode |= (++num_zeros > s->subframe_len>>8);
231 /** decode run level coded coefficients */
233 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
234 level, run, 1, ci->coeffs,
235 cur_coeff, s->subframe_len,
236 s->subframe_len, s->esc_len, 0))
237 return AVERROR_INVALIDDATA;
244 *@brief Reconstruct the individual channel data.
245 *@param s codec context
247 static void inverse_channel_transform(WMA3DecodeContext *s)
251 for (i = 0; i < s->num_chgroups; i++) {
252 if (s->chgroup[i].transform) {
253 float data[WMAPRO_MAX_CHANNELS];
254 const int num_channels = s->chgroup[i].num_channels;
255 float** ch_data = s->chgroup[i].channel_data;
256 float** ch_end = ch_data + num_channels;
257 const int8_t* tb = s->chgroup[i].transform_band;
260 /** multichannel decorrelation */
261 for (sfb = s->cur_sfb_offsets;
262 sfb < s->cur_sfb_offsets + s->num_bands;sfb++) {
265 /** multiply values with the decorrelation_matrix */
266 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
267 const float* mat = s->chgroup[i].decorrelation_matrix;
268 const float* data_end = data + num_channels;
269 float* data_ptr = data;
272 for (ch = ch_data; ch < ch_end; ch++)
273 *data_ptr++ = (*ch)[y];
275 for (ch = ch_data; ch < ch_end; ch++) {
278 while (data_ptr < data_end)
279 sum += *data_ptr++ * *mat++;
284 } else if (s->num_channels == 2) {
285 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
286 ch_data[0][y] *= 181.0 / 128;
287 ch_data[1][y] *= 181.0 / 128;