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.

229 lines
8.9KB

  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 Reconstruct the individual channel data.
  146. *@param s codec context
  147. */
  148. static void inverse_channel_transform(WMA3DecodeContext *s)
  149. {
  150. int i;
  151. for (i = 0; i < s->num_chgroups; i++) {
  152. if (s->chgroup[i].transform == 1) {
  153. /** M/S stereo decoding */
  154. int16_t* sfb_offsets = s->cur_sfb_offsets;
  155. float* ch0 = *sfb_offsets + s->channel[0].coeffs;
  156. float* ch1 = *sfb_offsets++ + s->channel[1].coeffs;
  157. const char* tb = s->chgroup[i].transform_band;
  158. const char* tb_end = tb + s->num_bands;
  159. while (tb < tb_end) {
  160. const float* ch0_end = s->channel[0].coeffs +
  161. FFMIN(*sfb_offsets,s->subframe_len);
  162. if (*tb++ == 1) {
  163. while (ch0 < ch0_end) {
  164. const float v1 = *ch0;
  165. const float v2 = *ch1;
  166. *ch0++ = v1 - v2;
  167. *ch1++ = v1 + v2;
  168. }
  169. } else {
  170. while (ch0 < ch0_end) {
  171. *ch0++ *= 181.0 / 128;
  172. *ch1++ *= 181.0 / 128;
  173. }
  174. }
  175. ++sfb_offsets;
  176. }
  177. } else if (s->chgroup[i].transform) {
  178. float data[WMAPRO_MAX_CHANNELS];
  179. const int num_channels = s->chgroup[i].num_channels;
  180. float** ch_data = s->chgroup[i].channel_data;
  181. float** ch_end = ch_data + num_channels;
  182. const int8_t* tb = s->chgroup[i].transform_band;
  183. int16_t* sfb;
  184. /** multichannel decorrelation */
  185. for (sfb = s->cur_sfb_offsets ;
  186. sfb < s->cur_sfb_offsets + s->num_bands;sfb++) {
  187. if (*tb++ == 1) {
  188. int y;
  189. /** multiply values with the decorrelation_matrix */
  190. for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
  191. const float* mat = s->chgroup[i].decorrelation_matrix;
  192. const float* data_end = data + num_channels;
  193. float* data_ptr = data;
  194. float** ch;
  195. for (ch = ch_data;ch < ch_end; ch++)
  196. *data_ptr++ = (*ch)[y];
  197. for (ch = ch_data; ch < ch_end; ch++) {
  198. float sum = 0;
  199. data_ptr = data;
  200. while (data_ptr < data_end)
  201. sum += *data_ptr++ * *mat++;
  202. (*ch)[y] = sum;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }