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.

258 lines
8.4KB

  1. /*
  2. * AAC encoder
  3. * Copyright (C) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file aacenc.c
  23. * AAC encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * psy model selection with some option
  28. * change greedy codebook search into something more optimal, like Viterbi algorithm
  29. * determine run lengths along with codebook
  30. ***********************************/
  31. #include "avcodec.h"
  32. #include "bitstream.h"
  33. #include "dsputil.h"
  34. #include "mpeg4audio.h"
  35. #include "aacpsy.h"
  36. #include "aac.h"
  37. #include "aactab.h"
  38. static const uint8_t swb_size_1024_96[] = {
  39. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  40. 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
  41. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  42. };
  43. static const uint8_t swb_size_1024_64[] = {
  44. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
  45. 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
  46. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
  47. };
  48. static const uint8_t swb_size_1024_48[] = {
  49. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  50. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  51. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  52. 96
  53. };
  54. static const uint8_t swb_size_1024_32[] = {
  55. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  56. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  57. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  58. };
  59. static const uint8_t swb_size_1024_24[] = {
  60. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  61. 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
  62. 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  63. };
  64. static const uint8_t swb_size_1024_16[] = {
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
  67. 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  68. };
  69. static const uint8_t swb_size_1024_8[] = {
  70. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  71. 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
  72. 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  73. };
  74. static const uint8_t *swb_size_1024[] = {
  75. swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
  76. swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
  77. swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
  78. swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
  79. };
  80. static const uint8_t swb_size_128_96[] = {
  81. 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  82. };
  83. static const uint8_t swb_size_128_48[] = {
  84. 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
  85. };
  86. static const uint8_t swb_size_128_24[] = {
  87. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
  88. };
  89. static const uint8_t swb_size_128_16[] = {
  90. 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  91. };
  92. static const uint8_t swb_size_128_8[] = {
  93. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  94. };
  95. static const uint8_t *swb_size_128[] = {
  96. /* the last entry on the following row is swb_size_128_64 but is a
  97. duplicate of swb_size_128_96 */
  98. swb_size_128_96, swb_size_128_96, swb_size_128_96,
  99. swb_size_128_48, swb_size_128_48, swb_size_128_48,
  100. swb_size_128_24, swb_size_128_24, swb_size_128_16,
  101. swb_size_128_16, swb_size_128_16, swb_size_128_8
  102. };
  103. /** default channel configurations */
  104. static const uint8_t aac_chan_configs[6][5] = {
  105. {1, ID_SCE}, // 1 channel - single channel element
  106. {1, ID_CPE}, // 2 channels - channel pair
  107. {2, ID_SCE, ID_CPE}, // 3 channels - center + stereo
  108. {3, ID_SCE, ID_CPE, ID_SCE}, // 4 channels - front center + stereo + back center
  109. {3, ID_SCE, ID_CPE, ID_CPE}, // 5 channels - front center + stereo + back stereo
  110. {4, ID_SCE, ID_CPE, ID_CPE, ID_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  111. };
  112. /**
  113. * Make AAC audio config object.
  114. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  115. */
  116. static void put_audio_specific_config(AVCodecContext *avctx)
  117. {
  118. PutBitContext pb;
  119. AACEncContext *s = avctx->priv_data;
  120. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  121. put_bits(&pb, 5, 2); //object type - AAC-LC
  122. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  123. put_bits(&pb, 4, avctx->channels);
  124. //GASpecificConfig
  125. put_bits(&pb, 1, 0); //frame length - 1024 samples
  126. put_bits(&pb, 1, 0); //does not depend on core coder
  127. put_bits(&pb, 1, 0); //is not extension
  128. flush_put_bits(&pb);
  129. }
  130. static av_cold int aac_encode_init(AVCodecContext *avctx)
  131. {
  132. AACEncContext *s = avctx->priv_data;
  133. int i;
  134. avctx->frame_size = 1024;
  135. for(i = 0; i < 16; i++)
  136. if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  137. break;
  138. if(i == 16){
  139. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
  140. return -1;
  141. }
  142. if(avctx->channels > 6){
  143. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
  144. return -1;
  145. }
  146. s->samplerate_index = i;
  147. s->swb_sizes1024 = swb_size_1024[i];
  148. s->swb_num1024 = ff_aac_num_swb_1024[i];
  149. s->swb_sizes128 = swb_size_128[i];
  150. s->swb_num128 = ff_aac_num_swb_128[i];
  151. dsputil_init(&s->dsp, avctx);
  152. ff_mdct_init(&s->mdct1024, 11, 0);
  153. ff_mdct_init(&s->mdct128, 8, 0);
  154. s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
  155. s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
  156. if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP, aac_chan_configs[avctx->channels-1][0], 0, s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
  157. av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
  158. return -1;
  159. }
  160. avctx->extradata = av_malloc(2);
  161. avctx->extradata_size = 2;
  162. put_audio_specific_config(avctx);
  163. return 0;
  164. }
  165. /**
  166. * Encode ics_info element.
  167. * @see Table 4.6 (syntax of ics_info)
  168. */
  169. static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
  170. {
  171. AACEncContext *s = avctx->priv_data;
  172. int i;
  173. put_bits(&s->pb, 1, 0); // ics_reserved bit
  174. put_bits(&s->pb, 2, info->window_sequence[0]);
  175. put_bits(&s->pb, 1, info->use_kb_window[0]);
  176. if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
  177. put_bits(&s->pb, 6, info->max_sfb);
  178. put_bits(&s->pb, 1, 0); // no prediction
  179. }else{
  180. put_bits(&s->pb, 4, info->max_sfb);
  181. for(i = 1; i < info->num_windows; i++)
  182. put_bits(&s->pb, 1, info->group_len[i]);
  183. }
  184. }
  185. /**
  186. * Write some auxiliary information about the created AAC file.
  187. */
  188. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
  189. {
  190. int i, namelen, padbits;
  191. namelen = strlen(name) + 2;
  192. put_bits(&s->pb, 3, ID_FIL);
  193. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  194. if(namelen >= 15)
  195. put_bits(&s->pb, 8, namelen - 16);
  196. put_bits(&s->pb, 4, 0); //extension type - filler
  197. padbits = 8 - (put_bits_count(&s->pb) & 7);
  198. align_put_bits(&s->pb);
  199. for(i = 0; i < namelen - 2; i++)
  200. put_bits(&s->pb, 8, name[i]);
  201. put_bits(&s->pb, 12 - padbits, 0);
  202. }
  203. static av_cold int aac_encode_end(AVCodecContext *avctx)
  204. {
  205. AACEncContext *s = avctx->priv_data;
  206. ff_mdct_end(&s->mdct1024);
  207. ff_mdct_end(&s->mdct128);
  208. ff_aac_psy_end(&s->psy);
  209. av_freep(&s->samples);
  210. av_freep(&s->cpe);
  211. return 0;
  212. }
  213. AVCodec aac_encoder = {
  214. "aac",
  215. CODEC_TYPE_AUDIO,
  216. CODEC_ID_AAC,
  217. sizeof(AACEncContext),
  218. aac_encode_init,
  219. aac_encode_frame,
  220. aac_encode_end,
  221. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  222. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  223. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  224. };