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.

314 lines
10KB

  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. * add sane pulse detection
  29. ***********************************/
  30. #include "avcodec.h"
  31. #include "bitstream.h"
  32. #include "dsputil.h"
  33. #include "mpeg4audio.h"
  34. #include "aacpsy.h"
  35. #include "aac.h"
  36. #include "aactab.h"
  37. static const uint8_t swb_size_1024_96[] = {
  38. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  39. 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
  40. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  41. };
  42. static const uint8_t swb_size_1024_64[] = {
  43. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
  44. 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
  45. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
  46. };
  47. static const uint8_t swb_size_1024_48[] = {
  48. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  49. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  50. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  51. 96
  52. };
  53. static const uint8_t swb_size_1024_32[] = {
  54. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  55. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  56. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  57. };
  58. static const uint8_t swb_size_1024_24[] = {
  59. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  60. 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
  61. 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  62. };
  63. static const uint8_t swb_size_1024_16[] = {
  64. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  65. 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
  66. 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  67. };
  68. static const uint8_t swb_size_1024_8[] = {
  69. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  70. 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
  71. 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  72. };
  73. static const uint8_t *swb_size_1024[] = {
  74. swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
  75. swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
  76. swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
  77. swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
  78. };
  79. static const uint8_t swb_size_128_96[] = {
  80. 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  81. };
  82. static const uint8_t swb_size_128_48[] = {
  83. 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
  84. };
  85. static const uint8_t swb_size_128_24[] = {
  86. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
  87. };
  88. static const uint8_t swb_size_128_16[] = {
  89. 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  90. };
  91. static const uint8_t swb_size_128_8[] = {
  92. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  93. };
  94. static const uint8_t *swb_size_128[] = {
  95. /* the last entry on the following row is swb_size_128_64 but is a
  96. duplicate of swb_size_128_96 */
  97. swb_size_128_96, swb_size_128_96, swb_size_128_96,
  98. swb_size_128_48, swb_size_128_48, swb_size_128_48,
  99. swb_size_128_24, swb_size_128_24, swb_size_128_16,
  100. swb_size_128_16, swb_size_128_16, swb_size_128_8
  101. };
  102. /** default channel configurations */
  103. static const uint8_t aac_chan_configs[6][5] = {
  104. {1, TYPE_SCE}, // 1 channel - single channel element
  105. {1, TYPE_CPE}, // 2 channels - channel pair
  106. {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
  107. {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
  108. {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
  109. {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  110. };
  111. /**
  112. * AAC encoder context
  113. */
  114. typedef struct {
  115. PutBitContext pb;
  116. MDCTContext mdct1024; ///< long (1024 samples) frame transform context
  117. MDCTContext mdct128; ///< short (128 samples) frame transform context
  118. DSPContext dsp;
  119. } AACEncContext;
  120. /**
  121. * Make AAC audio config object.
  122. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  123. */
  124. static void put_audio_specific_config(AVCodecContext *avctx)
  125. {
  126. PutBitContext pb;
  127. AACEncContext *s = avctx->priv_data;
  128. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  129. put_bits(&pb, 5, 2); //object type - AAC-LC
  130. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  131. put_bits(&pb, 4, avctx->channels);
  132. //GASpecificConfig
  133. put_bits(&pb, 1, 0); //frame length - 1024 samples
  134. put_bits(&pb, 1, 0); //does not depend on core coder
  135. put_bits(&pb, 1, 0); //is not extension
  136. flush_put_bits(&pb);
  137. }
  138. static av_cold int aac_encode_init(AVCodecContext *avctx)
  139. {
  140. AACEncContext *s = avctx->priv_data;
  141. int i;
  142. avctx->frame_size = 1024;
  143. for(i = 0; i < 16; i++)
  144. if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  145. break;
  146. if(i == 16){
  147. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
  148. return -1;
  149. }
  150. if(avctx->channels > 6){
  151. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
  152. return -1;
  153. }
  154. s->samplerate_index = i;
  155. s->swb_sizes1024 = swb_size_1024[i];
  156. s->swb_num1024 = ff_aac_num_swb_1024[i];
  157. s->swb_sizes128 = swb_size_128[i];
  158. s->swb_num128 = ff_aac_num_swb_128[i];
  159. dsputil_init(&s->dsp, avctx);
  160. ff_mdct_init(&s->mdct1024, 11, 0);
  161. ff_mdct_init(&s->mdct128, 8, 0);
  162. // window init
  163. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  164. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  165. ff_sine_window_init(ff_sine_1024, 1024);
  166. ff_sine_window_init(ff_sine_128, 128);
  167. s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
  168. s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
  169. 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){
  170. av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
  171. return -1;
  172. }
  173. avctx->extradata = av_malloc(2);
  174. avctx->extradata_size = 2;
  175. put_audio_specific_config(avctx);
  176. return 0;
  177. }
  178. /**
  179. * Encode ics_info element.
  180. * @see Table 4.6 (syntax of ics_info)
  181. */
  182. static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
  183. {
  184. AACEncContext *s = avctx->priv_data;
  185. int i;
  186. put_bits(&s->pb, 1, 0); // ics_reserved bit
  187. put_bits(&s->pb, 2, info->window_sequence[0]);
  188. put_bits(&s->pb, 1, info->use_kb_window[0]);
  189. if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
  190. put_bits(&s->pb, 6, info->max_sfb);
  191. put_bits(&s->pb, 1, 0); // no prediction
  192. }else{
  193. put_bits(&s->pb, 4, info->max_sfb);
  194. for(i = 1; i < info->num_windows; i++)
  195. put_bits(&s->pb, 1, info->group_len[i]);
  196. }
  197. }
  198. /**
  199. * Encode pulse data.
  200. */
  201. static void encode_pulses(AVCodecContext *avctx, AACEncContext *s, Pulse *pulse, int channel)
  202. {
  203. int i;
  204. put_bits(&s->pb, 1, !!pulse->num_pulse);
  205. if(!pulse->num_pulse) return;
  206. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  207. put_bits(&s->pb, 6, pulse->start);
  208. for(i = 0; i < pulse->num_pulse; i++){
  209. put_bits(&s->pb, 5, pulse->pos[i]);
  210. put_bits(&s->pb, 4, pulse->amp[i]);
  211. }
  212. }
  213. /**
  214. * Encode spectral coefficients processed by psychoacoustic model.
  215. */
  216. static void encode_spectral_coeffs(AVCodecContext *avctx, AACEncContext *s, ChannelElement *cpe, int channel)
  217. {
  218. int start, i, w, w2, wg;
  219. w = 0;
  220. for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
  221. start = 0;
  222. for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
  223. if(cpe->ch[channel].zeroes[w][i]){
  224. start += cpe->ch[channel].ics.swb_sizes[i];
  225. continue;
  226. }
  227. for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
  228. encode_band_coeffs(s, cpe, channel, start + w2*128, cpe->ch[channel].ics.swb_sizes[i], cpe->ch[channel].band_type[w][i]);
  229. }
  230. start += cpe->ch[channel].ics.swb_sizes[i];
  231. }
  232. w += cpe->ch[channel].ics.group_len[wg];
  233. }
  234. }
  235. /**
  236. * Write some auxiliary information about the created AAC file.
  237. */
  238. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
  239. {
  240. int i, namelen, padbits;
  241. namelen = strlen(name) + 2;
  242. put_bits(&s->pb, 3, TYPE_FIL);
  243. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  244. if(namelen >= 15)
  245. put_bits(&s->pb, 8, namelen - 16);
  246. put_bits(&s->pb, 4, 0); //extension type - filler
  247. padbits = 8 - (put_bits_count(&s->pb) & 7);
  248. align_put_bits(&s->pb);
  249. for(i = 0; i < namelen - 2; i++)
  250. put_bits(&s->pb, 8, name[i]);
  251. put_bits(&s->pb, 12 - padbits, 0);
  252. }
  253. static av_cold int aac_encode_end(AVCodecContext *avctx)
  254. {
  255. AACEncContext *s = avctx->priv_data;
  256. ff_mdct_end(&s->mdct1024);
  257. ff_mdct_end(&s->mdct128);
  258. ff_aac_psy_end(&s->psy);
  259. av_freep(&s->samples);
  260. av_freep(&s->cpe);
  261. return 0;
  262. }
  263. AVCodec aac_encoder = {
  264. "aac",
  265. CODEC_TYPE_AUDIO,
  266. CODEC_ID_AAC,
  267. sizeof(AACEncContext),
  268. aac_encode_init,
  269. aac_encode_frame,
  270. aac_encode_end,
  271. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  272. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  273. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  274. };