You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
11KB

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