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