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.

352 lines
12KB

  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. /** bits needed to code codebook run value for long windows */
  103. static const uint8_t run_value_bits_long[64] = {
  104. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  105. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
  106. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  107. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
  108. };
  109. /** bits needed to code codebook run value for short windows */
  110. static const uint8_t run_value_bits_short[16] = {
  111. 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
  112. };
  113. static const uint8_t* run_value_bits[2] = {
  114. run_value_bits_long, run_value_bits_short
  115. };
  116. /** default channel configurations */
  117. static const uint8_t aac_chan_configs[6][5] = {
  118. {1, TYPE_SCE}, // 1 channel - single channel element
  119. {1, TYPE_CPE}, // 2 channels - channel pair
  120. {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
  121. {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
  122. {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
  123. {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  124. };
  125. /**
  126. * structure used in optimal codebook search
  127. */
  128. typedef struct BandCodingPath {
  129. int prev_idx; ///< pointer to the previous path point
  130. int codebook; ///< codebook for coding band run
  131. int bits; ///< number of bit needed to code given number of bands
  132. } BandCodingPath;
  133. /**
  134. * AAC encoder context
  135. */
  136. typedef struct {
  137. PutBitContext pb;
  138. MDCTContext mdct1024; ///< long (1024 samples) frame transform context
  139. MDCTContext mdct128; ///< short (128 samples) frame transform context
  140. DSPContext dsp;
  141. DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients
  142. int16_t* samples; ///< saved preprocessed input
  143. int samplerate_index; ///< MPEG-4 samplerate index
  144. ChannelElement *cpe; ///< channel elements
  145. AACPsyContext psy; ///< psychoacoustic model context
  146. int last_frame;
  147. } AACEncContext;
  148. /**
  149. * Make AAC audio config object.
  150. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  151. */
  152. static void put_audio_specific_config(AVCodecContext *avctx)
  153. {
  154. PutBitContext pb;
  155. AACEncContext *s = avctx->priv_data;
  156. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  157. put_bits(&pb, 5, 2); //object type - AAC-LC
  158. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  159. put_bits(&pb, 4, avctx->channels);
  160. //GASpecificConfig
  161. put_bits(&pb, 1, 0); //frame length - 1024 samples
  162. put_bits(&pb, 1, 0); //does not depend on core coder
  163. put_bits(&pb, 1, 0); //is not extension
  164. flush_put_bits(&pb);
  165. }
  166. static av_cold int aac_encode_init(AVCodecContext *avctx)
  167. {
  168. AACEncContext *s = avctx->priv_data;
  169. int i;
  170. avctx->frame_size = 1024;
  171. for(i = 0; i < 16; i++)
  172. if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  173. break;
  174. if(i == 16){
  175. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
  176. return -1;
  177. }
  178. if(avctx->channels > 6){
  179. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
  180. return -1;
  181. }
  182. s->samplerate_index = i;
  183. s->swb_sizes1024 = swb_size_1024[i];
  184. s->swb_num1024 = ff_aac_num_swb_1024[i];
  185. s->swb_sizes128 = swb_size_128[i];
  186. s->swb_num128 = ff_aac_num_swb_128[i];
  187. dsputil_init(&s->dsp, avctx);
  188. ff_mdct_init(&s->mdct1024, 11, 0);
  189. ff_mdct_init(&s->mdct128, 8, 0);
  190. // window init
  191. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  192. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  193. ff_sine_window_init(ff_sine_1024, 1024);
  194. ff_sine_window_init(ff_sine_128, 128);
  195. s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
  196. s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
  197. if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP,
  198. aac_chan_configs[avctx->channels-1][0], 0,
  199. s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
  200. av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
  201. return -1;
  202. }
  203. avctx->extradata = av_malloc(2);
  204. avctx->extradata_size = 2;
  205. put_audio_specific_config(avctx);
  206. return 0;
  207. }
  208. /**
  209. * Encode ics_info element.
  210. * @see Table 4.6 (syntax of ics_info)
  211. */
  212. static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
  213. {
  214. AACEncContext *s = avctx->priv_data;
  215. int i;
  216. put_bits(&s->pb, 1, 0); // ics_reserved bit
  217. put_bits(&s->pb, 2, info->window_sequence[0]);
  218. put_bits(&s->pb, 1, info->use_kb_window[0]);
  219. if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
  220. put_bits(&s->pb, 6, info->max_sfb);
  221. put_bits(&s->pb, 1, 0); // no prediction
  222. }else{
  223. put_bits(&s->pb, 4, info->max_sfb);
  224. for(i = 1; i < info->num_windows; i++)
  225. put_bits(&s->pb, 1, info->group_len[i]);
  226. }
  227. }
  228. /**
  229. * Encode pulse data.
  230. */
  231. static void encode_pulses(AACEncContext *s, Pulse *pulse, int channel)
  232. {
  233. int i;
  234. put_bits(&s->pb, 1, !!pulse->num_pulse);
  235. if(!pulse->num_pulse) return;
  236. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  237. put_bits(&s->pb, 6, pulse->start);
  238. for(i = 0; i < pulse->num_pulse; i++){
  239. put_bits(&s->pb, 5, pulse->pos[i]);
  240. put_bits(&s->pb, 4, pulse->amp[i]);
  241. }
  242. }
  243. /**
  244. * Encode spectral coefficients processed by psychoacoustic model.
  245. */
  246. static void encode_spectral_coeffs(AACEncContext *s, ChannelElement *cpe, int channel)
  247. {
  248. int start, i, w, w2, wg;
  249. w = 0;
  250. for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
  251. start = 0;
  252. for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
  253. if(cpe->ch[channel].zeroes[w*16 + i]){
  254. start += cpe->ch[channel].ics.swb_sizes[i];
  255. continue;
  256. }
  257. for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
  258. encode_band_coeffs(s, cpe, channel, start + w2*128,
  259. cpe->ch[channel].ics.swb_sizes[i],
  260. cpe->ch[channel].band_type[w*16 + i]);
  261. }
  262. start += cpe->ch[channel].ics.swb_sizes[i];
  263. }
  264. w += cpe->ch[channel].ics.group_len[wg];
  265. }
  266. }
  267. /**
  268. * Write some auxiliary information about the created AAC file.
  269. */
  270. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
  271. {
  272. int i, namelen, padbits;
  273. namelen = strlen(name) + 2;
  274. put_bits(&s->pb, 3, TYPE_FIL);
  275. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  276. if(namelen >= 15)
  277. put_bits(&s->pb, 8, namelen - 16);
  278. put_bits(&s->pb, 4, 0); //extension type - filler
  279. padbits = 8 - (put_bits_count(&s->pb) & 7);
  280. align_put_bits(&s->pb);
  281. for(i = 0; i < namelen - 2; i++)
  282. put_bits(&s->pb, 8, name[i]);
  283. put_bits(&s->pb, 12 - padbits, 0);
  284. }
  285. static av_cold int aac_encode_end(AVCodecContext *avctx)
  286. {
  287. AACEncContext *s = avctx->priv_data;
  288. ff_mdct_end(&s->mdct1024);
  289. ff_mdct_end(&s->mdct128);
  290. ff_aac_psy_end(&s->psy);
  291. av_freep(&s->samples);
  292. av_freep(&s->cpe);
  293. return 0;
  294. }
  295. AVCodec aac_encoder = {
  296. "aac",
  297. CODEC_TYPE_AUDIO,
  298. CODEC_ID_AAC,
  299. sizeof(AACEncContext),
  300. aac_encode_init,
  301. aac_encode_frame,
  302. aac_encode_end,
  303. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  304. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  305. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  306. };