Mercurial > libavcodec.hg
annotate wmaprodec.c @ 10097:9b608d9428ae libavcodec
add decode_coeffs()
| author | faust3 |
|---|---|
| date | Wed, 26 Aug 2009 21:23:21 +0000 |
| parents | 071d6d272a77 |
| children | 254f8c7d5e4d |
| rev | line source |
|---|---|
| 10073 | 1 /* |
| 2 * Wmapro compatible decoder | |
| 3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion | |
| 4 * Copyright (c) 2008 - 2009 Sascha Sommer, Benjamin Larsson | |
| 5 * | |
| 6 * This file is part of FFmpeg. | |
| 7 * | |
| 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. | |
| 12 * | |
| 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. | |
| 17 * | |
| 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 | |
| 21 */ | |
| 22 | |
| 23 /** | |
| 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 | |
| 31 * - IMDCT | |
| 32 * - windowing and overlapp-add | |
| 33 * | |
| 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 | |
| 39 * a frame. | |
| 40 * The number of samples and a few other decode flags are stored | |
| 41 * as extradata that has to be passed to the decoder. | |
| 42 * | |
| 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. | |
| 46 * | |
| 47 * Example wmapro bitstream (in samples): | |
| 48 * | |
| 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 * --------------------------------------------------- | |
| 57 * | |
| 58 * The frame layouts for the individual channels of a wma frame does not need | |
| 59 * to be the same. | |
| 60 * | |
| 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. | |
| 71 * | |
| 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 | |
| 79 * codes. | |
| 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 | |
| 82 * modifier. | |
| 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. | |
| 87 */ | |
| 88 | |
| 10005 | 89 /** |
| 90 *@brief Uninitialize the decoder and free all resources. | |
| 91 *@param avctx codec context | |
| 92 *@return 0 on success, < 0 otherwise | |
| 93 */ | |
| 94 static av_cold int decode_end(AVCodecContext *avctx) | |
| 95 { | |
| 96 WMA3DecodeContext *s = avctx->priv_data; | |
| 97 int i; | |
| 98 | |
| 10084 | 99 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) |
| 10005 | 100 ff_mdct_end(&s->mdct_ctx[i]); |
| 101 | |
| 102 return 0; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 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 | |
| 109 */ | |
| 10006 | 110 static void decode_decorrelation_matrix(WMA3DecodeContext *s, |
| 111 WMA3ChannelGroup *chgroup) | |
| 10005 | 112 { |
| 113 int i; | |
| 114 int offset = 0; | |
| 115 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS]; | |
| 10084 | 116 memset(chgroup->decorrelation_matrix, 0, |
| 10005 | 117 sizeof(float) *s->num_channels * s->num_channels); |
| 118 | |
| 10006 | 119 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++) |
| 10084 | 120 rotation_offset[i] = get_bits(&s->gb, 6); |
| 10005 | 121 |
| 10006 | 122 for (i = 0; i < chgroup->num_channels; i++) |
| 10005 | 123 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] = |
| 124 get_bits1(&s->gb) ? 1.0 : -1.0; | |
| 125 | |
| 10006 | 126 for (i = 1; i < chgroup->num_channels; i++) { |
| 10005 | 127 int x; |
| 10006 | 128 for (x = 0; x < i; x++) { |
| 10005 | 129 int y; |
| 10084 | 130 for (y = 0; y < i + 1; y++) { |
| 10005 | 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]; | |
| 134 float sinv; | |
| 135 float cosv; | |
| 136 | |
| 10006 | 137 if (n < 32) { |
| 10005 | 138 sinv = sin64[n]; |
| 139 cosv = sin64[32-n]; | |
| 140 } else { | |
| 141 sinv = sin64[64-n]; | |
| 142 cosv = -sin64[n-32]; | |
| 143 } | |
| 144 | |
| 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); | |
| 149 } | |
| 150 } | |
| 151 offset += i; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 /** | |
| 10097 | 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 | |
| 160 */ | |
| 161 static int decode_coeffs(WMA3DecodeContext *s, int c) | |
| 162 { | |
| 163 int vlctable; | |
| 164 VLC* vlc; | |
| 165 WMA3ChannelCtx* ci = &s->channel[c]; | |
| 166 int rl_mode = 0; | |
| 167 int cur_coeff = 0; | |
| 168 int num_zeros = 0; | |
| 169 const uint16_t* run; | |
| 170 const uint16_t* level; | |
| 171 | |
| 172 dprintf(s->avctx, "decode coefficients for channel %i\n", c); | |
| 173 | |
| 174 vlctable = get_bits1(&s->gb); | |
| 175 vlc = &coef_vlc[vlctable]; | |
| 176 | |
| 177 if (vlctable) { | |
| 178 run = coef1_run; | |
| 179 level = coef1_level; | |
| 180 } else { | |
| 181 run = coef0_run; | |
| 182 level = coef0_level; | |
| 183 } | |
| 184 | |
| 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) { | |
| 188 int vals[4]; | |
| 189 int i; | |
| 190 unsigned int idx; | |
| 191 | |
| 192 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH); | |
| 193 | |
| 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); | |
| 204 } else { | |
| 205 vals[i] = symbol_to_vec2[idx] >> 4; | |
| 206 vals[i+1] = symbol_to_vec2[idx] & 0xF; | |
| 207 } | |
| 208 } | |
| 209 } else { | |
| 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; | |
| 214 } | |
| 215 | |
| 216 /** decode sign */ | |
| 217 for (i = 0; i < 4; i++) { | |
| 218 if (vals[i]) { | |
| 219 int sign = get_bits1(&s->gb) - 1; | |
| 220 ci->coeffs[cur_coeff] = (vals[i]^sign) - sign; | |
| 221 num_zeros = 0; | |
| 222 } else { | |
| 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); | |
| 226 } | |
| 227 ++cur_coeff; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 /** decode run level coded coefficients */ | |
| 232 if (rl_mode) { | |
| 233 if(ff_wma_run_level_decode(s->avctx, &s->gb, vlc, | |
| 234 level, run, 1, ci->coeffs, | |
| 235 cur_coeff, s->subframe_len, s->subframe_len, | |
| 236 s->esc_len, 0)) | |
| 237 return AVERROR_INVALIDDATA; | |
| 238 } | |
| 239 | |
| 240 return 0; | |
| 241 } | |
| 242 | |
| 243 /** | |
| 10005 | 244 *@brief Reconstruct the individual channel data. |
| 245 *@param s codec context | |
| 246 */ | |
| 247 static void inverse_channel_transform(WMA3DecodeContext *s) | |
| 248 { | |
| 249 int i; | |
| 250 | |
| 10006 | 251 for (i = 0; i < s->num_chgroups; i++) { |
|
10096
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
252 if (s->chgroup[i].transform) { |
| 10005 | 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; | |
| 258 int16_t* sfb; | |
| 259 | |
| 260 /** multichannel decorrelation */ | |
| 10084 | 261 for (sfb = s->cur_sfb_offsets; |
| 10005 | 262 sfb < s->cur_sfb_offsets + s->num_bands;sfb++) { |
|
10096
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
263 int y; |
| 10005 | 264 if (*tb++ == 1) { |
| 265 /** multiply values with the decorrelation_matrix */ | |
| 10006 | 266 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) { |
| 10005 | 267 const float* mat = s->chgroup[i].decorrelation_matrix; |
| 10006 | 268 const float* data_end = data + num_channels; |
| 269 float* data_ptr = data; | |
| 10005 | 270 float** ch; |
| 271 | |
| 10084 | 272 for (ch = ch_data; ch < ch_end; ch++) |
| 10005 | 273 *data_ptr++ = (*ch)[y]; |
| 274 | |
| 275 for (ch = ch_data; ch < ch_end; ch++) { | |
| 276 float sum = 0; | |
| 277 data_ptr = data; | |
| 278 while (data_ptr < data_end) | |
| 279 sum += *data_ptr++ * *mat++; | |
| 280 | |
| 281 (*ch)[y] = sum; | |
| 282 } | |
| 283 } | |
|
10096
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
284 } else if (s->num_channels == 2) { |
|
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
285 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) { |
|
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
286 ch_data[0][y] *= 181.0 / 128; |
|
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
287 ch_data[1][y] *= 181.0 / 128; |
|
071d6d272a77
merge 2-channel M/S stereo decoding code with the multichannel version
faust3
parents:
10084
diff
changeset
|
288 } |
| 10005 | 289 } |
| 290 } | |
| 291 } | |
| 292 } | |
| 293 } | |
| 294 |
