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.

242 lines
7.9KB

  1. /*
  2. * AAC decoder
  3. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  4. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  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 aac.c
  24. * AAC decoder
  25. * @author Oded Shimon ( ods15 ods15 dyndns org )
  26. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  27. */
  28. /*
  29. * supported tools
  30. *
  31. * Support? Name
  32. * N (code in SoC repo) gain control
  33. * Y block switching
  34. * Y window shapes - standard
  35. * N window shapes - Low Delay
  36. * Y filterbank - standard
  37. * N (code in SoC repo) filterbank - Scalable Sample Rate
  38. * Y Temporal Noise Shaping
  39. * N (code in SoC repo) Long Term Prediction
  40. * Y intensity stereo
  41. * Y channel coupling
  42. * N frequency domain prediction
  43. * Y Perceptual Noise Substitution
  44. * Y Mid/Side stereo
  45. * N Scalable Inverse AAC Quantization
  46. * N Frequency Selective Switch
  47. * N upsampling filter
  48. * Y quantization & coding - AAC
  49. * N quantization & coding - TwinVQ
  50. * N quantization & coding - BSAC
  51. * N AAC Error Resilience tools
  52. * N Error Resilience payload syntax
  53. * N Error Protection tool
  54. * N CELP
  55. * N Silence Compression
  56. * N HVXC
  57. * N HVXC 4kbits/s VR
  58. * N Structured Audio tools
  59. * N Structured Audio Sample Bank Format
  60. * N MIDI
  61. * N Harmonic and Individual Lines plus Noise
  62. * N Text-To-Speech Interface
  63. * N (in progress) Spectral Band Replication
  64. * Y (not in this code) Layer-1
  65. * Y (not in this code) Layer-2
  66. * Y (not in this code) Layer-3
  67. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  68. * N (planned) Parametric Stereo
  69. * N Direct Stream Transfer
  70. *
  71. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  72. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  73. Parametric Stereo.
  74. */
  75. #include "avcodec.h"
  76. #include "bitstream.h"
  77. #include "dsputil.h"
  78. #include "aac.h"
  79. #include "aactab.h"
  80. #include "mpeg4audio.h"
  81. #include <assert.h>
  82. #include <errno.h>
  83. #include <math.h>
  84. #include <string.h>
  85. #ifndef CONFIG_HARDCODED_TABLES
  86. static float ff_aac_ivquant_tab[IVQUANT_SIZE];
  87. #endif /* CONFIG_HARDCODED_TABLES */
  88. static VLC vlc_scalefactors;
  89. static VLC vlc_spectral[11];
  90. num_front = get_bits(gb, 4);
  91. num_side = get_bits(gb, 4);
  92. num_back = get_bits(gb, 4);
  93. num_lfe = get_bits(gb, 2);
  94. num_assoc_data = get_bits(gb, 3);
  95. num_cc = get_bits(gb, 4);
  96. newpcs->mono_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1;
  97. newpcs->stereo_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1;
  98. if (get_bits1(gb)) {
  99. newpcs->mixdown_coeff_index = get_bits(gb, 2);
  100. newpcs->pseudo_surround = get_bits1(gb);
  101. }
  102. program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  103. program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  104. program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_BACK, gb, num_back );
  105. program_config_element_parse_tags(NULL, newpcs->che_type[ID_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
  106. skip_bits_long(gb, 4 * num_assoc_data);
  107. program_config_element_parse_tags(newpcs->che_type[ID_CCE], newpcs->che_type[ID_CCE], AAC_CHANNEL_CC, gb, num_cc );
  108. align_get_bits(gb);
  109. /* comment field, first byte is length */
  110. skip_bits_long(gb, 8 * get_bits(gb, 8));
  111. static av_cold int aac_decode_init(AVCodecContext * avccontext) {
  112. AACContext * ac = avccontext->priv_data;
  113. int i;
  114. ac->avccontext = avccontext;
  115. avccontext->sample_rate = ac->m4ac.sample_rate;
  116. avccontext->frame_size = 1024;
  117. AAC_INIT_VLC_STATIC( 0, 144);
  118. AAC_INIT_VLC_STATIC( 1, 114);
  119. AAC_INIT_VLC_STATIC( 2, 188);
  120. AAC_INIT_VLC_STATIC( 3, 180);
  121. AAC_INIT_VLC_STATIC( 4, 172);
  122. AAC_INIT_VLC_STATIC( 5, 140);
  123. AAC_INIT_VLC_STATIC( 6, 168);
  124. AAC_INIT_VLC_STATIC( 7, 114);
  125. AAC_INIT_VLC_STATIC( 8, 262);
  126. AAC_INIT_VLC_STATIC( 9, 248);
  127. AAC_INIT_VLC_STATIC(10, 384);
  128. dsputil_init(&ac->dsp, avccontext);
  129. // -1024 - Compensate wrong IMDCT method.
  130. // 32768 - Required to scale values to the correct range for the bias method
  131. // for float to int16 conversion.
  132. if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
  133. ac->add_bias = 385.0f;
  134. ac->sf_scale = 1. / (-1024. * 32768.);
  135. ac->sf_offset = 0;
  136. } else {
  137. ac->add_bias = 0.0f;
  138. ac->sf_scale = 1. / -1024.;
  139. ac->sf_offset = 60;
  140. }
  141. #ifndef CONFIG_HARDCODED_TABLES
  142. for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++)
  143. ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i;
  144. #endif /* CONFIG_HARDCODED_TABLES */
  145. INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]),
  146. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  147. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  148. 352);
  149. ff_mdct_init(&ac->mdct, 11, 1);
  150. ff_mdct_init(&ac->mdct_small, 8, 1);
  151. return 0;
  152. }
  153. int byte_align = get_bits1(gb);
  154. int count = get_bits(gb, 8);
  155. if (count == 255)
  156. count += get_bits(gb, 8);
  157. if (byte_align)
  158. align_get_bits(gb);
  159. skip_bits_long(gb, 8 * count);
  160. }
  161. /**
  162. * inverse quantization
  163. *
  164. * @param a quantized value to be dequantized
  165. * @return Returns dequantized value.
  166. */
  167. static inline float ivquant(int a) {
  168. if (a + (unsigned int)IVQUANT_SIZE/2 - 1 < (unsigned int)IVQUANT_SIZE - 1)
  169. return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1];
  170. else
  171. return cbrtf(fabsf(a)) * a;
  172. }
  173. * @param pulse pointer to pulse data struct
  174. * @param icoef array of quantized spectral data
  175. */
  176. static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) {
  177. int i, off = ics->swb_offset[pulse->start];
  178. for (i = 0; i < pulse->num_pulse; i++) {
  179. int ic;
  180. off += pulse->offset[i];
  181. ic = (icoef[off] - 1)>>31;
  182. icoef[off] += (pulse->amp[i]^ic) - ic;
  183. }
  184. }
  185. static av_cold int aac_decode_close(AVCodecContext * avccontext) {
  186. AACContext * ac = avccontext->priv_data;
  187. int i, j;
  188. for (i = 0; i < MAX_TAGID; i++) {
  189. for(j = 0; j < 4; j++)
  190. av_freep(&ac->che[j][i]);
  191. }
  192. ff_mdct_end(&ac->mdct);
  193. ff_mdct_end(&ac->mdct_small);
  194. av_freep(&ac->interleaved_output);
  195. return 0 ;
  196. }
  197. AVCodec aac_decoder = {
  198. "aac",
  199. CODEC_TYPE_AUDIO,
  200. CODEC_ID_AAC,
  201. sizeof(AACContext),
  202. aac_decode_init,
  203. NULL,
  204. aac_decode_close,
  205. aac_decode_frame,
  206. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  207. };