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.

647 lines
23KB

  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
  23. * AAC encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * add sane pulse detection
  28. * add temporal noise shaping
  29. ***********************************/
  30. #include "avcodec.h"
  31. #include "put_bits.h"
  32. #include "dsputil.h"
  33. #include "mpeg4audio.h"
  34. #include "aac.h"
  35. #include "aactab.h"
  36. #include "aacenc.h"
  37. #include "psymodel.h"
  38. #define AAC_MAX_CHANNELS 6
  39. static const uint8_t swb_size_1024_96[] = {
  40. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  41. 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
  42. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  43. };
  44. static const uint8_t swb_size_1024_64[] = {
  45. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
  46. 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
  47. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
  48. };
  49. static const uint8_t swb_size_1024_48[] = {
  50. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  51. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  52. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  53. 96
  54. };
  55. static const uint8_t swb_size_1024_32[] = {
  56. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  57. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  58. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  59. };
  60. static const uint8_t swb_size_1024_24[] = {
  61. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  62. 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
  63. 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  64. };
  65. static const uint8_t swb_size_1024_16[] = {
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
  68. 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  69. };
  70. static const uint8_t swb_size_1024_8[] = {
  71. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  72. 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
  73. 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  74. };
  75. static const uint8_t *swb_size_1024[] = {
  76. swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
  77. swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
  78. swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
  79. swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
  80. };
  81. static const uint8_t swb_size_128_96[] = {
  82. 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  83. };
  84. static const uint8_t swb_size_128_48[] = {
  85. 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
  86. };
  87. static const uint8_t swb_size_128_24[] = {
  88. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
  89. };
  90. static const uint8_t swb_size_128_16[] = {
  91. 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  92. };
  93. static const uint8_t swb_size_128_8[] = {
  94. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  95. };
  96. static const uint8_t *swb_size_128[] = {
  97. /* the last entry on the following row is swb_size_128_64 but is a
  98. duplicate of swb_size_128_96 */
  99. swb_size_128_96, swb_size_128_96, swb_size_128_96,
  100. swb_size_128_48, swb_size_128_48, swb_size_128_48,
  101. swb_size_128_24, swb_size_128_24, swb_size_128_16,
  102. swb_size_128_16, swb_size_128_16, swb_size_128_8
  103. };
  104. /** default channel configurations */
  105. static const uint8_t aac_chan_configs[6][5] = {
  106. {1, TYPE_SCE}, // 1 channel - single channel element
  107. {1, TYPE_CPE}, // 2 channels - channel pair
  108. {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
  109. {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
  110. {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
  111. {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  112. };
  113. /**
  114. * Make AAC audio config object.
  115. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  116. */
  117. static void put_audio_specific_config(AVCodecContext *avctx)
  118. {
  119. PutBitContext pb;
  120. AACEncContext *s = avctx->priv_data;
  121. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  122. put_bits(&pb, 5, 2); //object type - AAC-LC
  123. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  124. put_bits(&pb, 4, avctx->channels);
  125. //GASpecificConfig
  126. put_bits(&pb, 1, 0); //frame length - 1024 samples
  127. put_bits(&pb, 1, 0); //does not depend on core coder
  128. put_bits(&pb, 1, 0); //is not extension
  129. flush_put_bits(&pb);
  130. }
  131. static av_cold int aac_encode_init(AVCodecContext *avctx)
  132. {
  133. AACEncContext *s = avctx->priv_data;
  134. int i;
  135. const uint8_t *sizes[2];
  136. int lengths[2];
  137. avctx->frame_size = 1024;
  138. for (i = 0; i < 16; i++)
  139. if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
  140. break;
  141. if (i == 16) {
  142. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
  143. return -1;
  144. }
  145. if (avctx->channels > AAC_MAX_CHANNELS) {
  146. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
  147. return -1;
  148. }
  149. if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
  150. av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
  151. return -1;
  152. }
  153. if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
  154. av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
  155. return -1;
  156. }
  157. s->samplerate_index = i;
  158. dsputil_init(&s->dsp, avctx);
  159. ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
  160. ff_mdct_init(&s->mdct128, 8, 0, 1.0);
  161. // window init
  162. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  163. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  164. ff_init_ff_sine_windows(10);
  165. ff_init_ff_sine_windows(7);
  166. s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
  167. s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
  168. avctx->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
  169. avctx->extradata_size = 2;
  170. put_audio_specific_config(avctx);
  171. sizes[0] = swb_size_1024[i];
  172. sizes[1] = swb_size_128[i];
  173. lengths[0] = ff_aac_num_swb_1024[i];
  174. lengths[1] = ff_aac_num_swb_128[i];
  175. ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
  176. s->psypp = ff_psy_preprocess_init(avctx);
  177. s->coder = &ff_aac_coders[2];
  178. s->lambda = avctx->global_quality ? avctx->global_quality : 120;
  179. ff_aac_tableinit();
  180. if (avctx->channels > 5)
  181. av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. "
  182. "The output will most likely be an illegal bitstream.\n");
  183. return 0;
  184. }
  185. static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
  186. SingleChannelElement *sce, short *audio, int channel)
  187. {
  188. int i, j, k;
  189. const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  190. const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  191. const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  192. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  193. memcpy(s->output, sce->saved, sizeof(float)*1024);
  194. if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
  195. memset(s->output, 0, sizeof(s->output[0]) * 448);
  196. for (i = 448; i < 576; i++)
  197. s->output[i] = sce->saved[i] * pwindow[i - 448];
  198. for (i = 576; i < 704; i++)
  199. s->output[i] = sce->saved[i];
  200. }
  201. if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
  202. for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) {
  203. s->output[i+1024] = audio[j] * lwindow[1024 - i - 1];
  204. sce->saved[i] = audio[j] * lwindow[i];
  205. }
  206. } else {
  207. for (i = 0, j = channel; i < 448; i++, j += avctx->channels)
  208. s->output[i+1024] = audio[j];
  209. for (; i < 576; i++, j += avctx->channels)
  210. s->output[i+1024] = audio[j] * swindow[576 - i - 1];
  211. memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
  212. for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
  213. sce->saved[i] = audio[j];
  214. }
  215. ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
  216. } else {
  217. for (k = 0; k < 1024; k += 128) {
  218. for (i = 448 + k; i < 448 + k + 256; i++)
  219. s->output[i - 448 - k] = (i < 1024)
  220. ? sce->saved[i]
  221. : audio[channel + (i-1024)*avctx->channels];
  222. s->dsp.vector_fmul (s->output, k ? swindow : pwindow, 128);
  223. s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
  224. ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
  225. }
  226. for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
  227. sce->saved[i] = audio[j];
  228. }
  229. }
  230. /**
  231. * Encode ics_info element.
  232. * @see Table 4.6 (syntax of ics_info)
  233. */
  234. static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
  235. {
  236. int w;
  237. put_bits(&s->pb, 1, 0); // ics_reserved bit
  238. put_bits(&s->pb, 2, info->window_sequence[0]);
  239. put_bits(&s->pb, 1, info->use_kb_window[0]);
  240. if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  241. put_bits(&s->pb, 6, info->max_sfb);
  242. put_bits(&s->pb, 1, 0); // no prediction
  243. } else {
  244. put_bits(&s->pb, 4, info->max_sfb);
  245. for (w = 1; w < 8; w++)
  246. put_bits(&s->pb, 1, !info->group_len[w]);
  247. }
  248. }
  249. /**
  250. * Encode MS data.
  251. * @see 4.6.8.1 "Joint Coding - M/S Stereo"
  252. */
  253. static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
  254. {
  255. int i, w;
  256. put_bits(pb, 2, cpe->ms_mode);
  257. if (cpe->ms_mode == 1)
  258. for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
  259. for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
  260. put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
  261. }
  262. /**
  263. * Produce integer coefficients from scalefactors provided by the model.
  264. */
  265. static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
  266. {
  267. int i, w, w2, g, ch;
  268. int start, sum, maxsfb, cmaxsfb;
  269. for (ch = 0; ch < chans; ch++) {
  270. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  271. start = 0;
  272. maxsfb = 0;
  273. cpe->ch[ch].pulse.num_pulse = 0;
  274. for (w = 0; w < ics->num_windows*16; w += 16) {
  275. for (g = 0; g < ics->num_swb; g++) {
  276. sum = 0;
  277. //apply M/S
  278. if (!ch && cpe->ms_mask[w + g]) {
  279. for (i = 0; i < ics->swb_sizes[g]; i++) {
  280. cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
  281. cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
  282. }
  283. }
  284. start += ics->swb_sizes[g];
  285. }
  286. for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
  287. ;
  288. maxsfb = FFMAX(maxsfb, cmaxsfb);
  289. }
  290. ics->max_sfb = maxsfb;
  291. //adjust zero bands for window groups
  292. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  293. for (g = 0; g < ics->max_sfb; g++) {
  294. i = 1;
  295. for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
  296. if (!cpe->ch[ch].zeroes[w2*16 + g]) {
  297. i = 0;
  298. break;
  299. }
  300. }
  301. cpe->ch[ch].zeroes[w*16 + g] = i;
  302. }
  303. }
  304. }
  305. if (chans > 1 && cpe->common_window) {
  306. IndividualChannelStream *ics0 = &cpe->ch[0].ics;
  307. IndividualChannelStream *ics1 = &cpe->ch[1].ics;
  308. int msc = 0;
  309. ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
  310. ics1->max_sfb = ics0->max_sfb;
  311. for (w = 0; w < ics0->num_windows*16; w += 16)
  312. for (i = 0; i < ics0->max_sfb; i++)
  313. if (cpe->ms_mask[w+i])
  314. msc++;
  315. if (msc == 0 || ics0->max_sfb == 0)
  316. cpe->ms_mode = 0;
  317. else
  318. cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
  319. }
  320. }
  321. /**
  322. * Encode scalefactor band coding type.
  323. */
  324. static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
  325. {
  326. int w;
  327. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  328. s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
  329. }
  330. /**
  331. * Encode scalefactors.
  332. */
  333. static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
  334. SingleChannelElement *sce)
  335. {
  336. int off = sce->sf_idx[0], diff;
  337. int i, w;
  338. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  339. for (i = 0; i < sce->ics.max_sfb; i++) {
  340. if (!sce->zeroes[w*16 + i]) {
  341. diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
  342. if (diff < 0 || diff > 120)
  343. av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
  344. off = sce->sf_idx[w*16 + i];
  345. put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
  346. }
  347. }
  348. }
  349. }
  350. /**
  351. * Encode pulse data.
  352. */
  353. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  354. {
  355. int i;
  356. put_bits(&s->pb, 1, !!pulse->num_pulse);
  357. if (!pulse->num_pulse)
  358. return;
  359. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  360. put_bits(&s->pb, 6, pulse->start);
  361. for (i = 0; i < pulse->num_pulse; i++) {
  362. put_bits(&s->pb, 5, pulse->pos[i]);
  363. put_bits(&s->pb, 4, pulse->amp[i]);
  364. }
  365. }
  366. /**
  367. * Encode spectral coefficients processed by psychoacoustic model.
  368. */
  369. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  370. {
  371. int start, i, w, w2;
  372. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  373. start = 0;
  374. for (i = 0; i < sce->ics.max_sfb; i++) {
  375. if (sce->zeroes[w*16 + i]) {
  376. start += sce->ics.swb_sizes[i];
  377. continue;
  378. }
  379. for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
  380. s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
  381. sce->ics.swb_sizes[i],
  382. sce->sf_idx[w*16 + i],
  383. sce->band_type[w*16 + i],
  384. s->lambda);
  385. start += sce->ics.swb_sizes[i];
  386. }
  387. }
  388. }
  389. /**
  390. * Encode one channel of audio data.
  391. */
  392. static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
  393. SingleChannelElement *sce,
  394. int common_window)
  395. {
  396. put_bits(&s->pb, 8, sce->sf_idx[0]);
  397. if (!common_window)
  398. put_ics_info(s, &sce->ics);
  399. encode_band_info(s, sce);
  400. encode_scale_factors(avctx, s, sce);
  401. encode_pulses(s, &sce->pulse);
  402. put_bits(&s->pb, 1, 0); //tns
  403. put_bits(&s->pb, 1, 0); //ssr
  404. encode_spectral_coeffs(s, sce);
  405. return 0;
  406. }
  407. /**
  408. * Write some auxiliary information about the created AAC file.
  409. */
  410. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
  411. const char *name)
  412. {
  413. int i, namelen, padbits;
  414. namelen = strlen(name) + 2;
  415. put_bits(&s->pb, 3, TYPE_FIL);
  416. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  417. if (namelen >= 15)
  418. put_bits(&s->pb, 8, namelen - 16);
  419. put_bits(&s->pb, 4, 0); //extension type - filler
  420. padbits = 8 - (put_bits_count(&s->pb) & 7);
  421. align_put_bits(&s->pb);
  422. for (i = 0; i < namelen - 2; i++)
  423. put_bits(&s->pb, 8, name[i]);
  424. put_bits(&s->pb, 12 - padbits, 0);
  425. }
  426. static int aac_encode_frame(AVCodecContext *avctx,
  427. uint8_t *frame, int buf_size, void *data)
  428. {
  429. AACEncContext *s = avctx->priv_data;
  430. int16_t *samples = s->samples, *samples2, *la;
  431. ChannelElement *cpe;
  432. int i, j, chans, tag, start_ch;
  433. const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
  434. int chan_el_counter[4];
  435. FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
  436. if (s->last_frame)
  437. return 0;
  438. if (data) {
  439. if (!s->psypp) {
  440. memcpy(s->samples + 1024 * avctx->channels, data,
  441. 1024 * avctx->channels * sizeof(s->samples[0]));
  442. } else {
  443. start_ch = 0;
  444. samples2 = s->samples + 1024 * avctx->channels;
  445. for (i = 0; i < chan_map[0]; i++) {
  446. tag = chan_map[i+1];
  447. chans = tag == TYPE_CPE ? 2 : 1;
  448. ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
  449. samples2 + start_ch, start_ch, chans);
  450. start_ch += chans;
  451. }
  452. }
  453. }
  454. if (!avctx->frame_number) {
  455. memcpy(s->samples, s->samples + 1024 * avctx->channels,
  456. 1024 * avctx->channels * sizeof(s->samples[0]));
  457. return 0;
  458. }
  459. start_ch = 0;
  460. for (i = 0; i < chan_map[0]; i++) {
  461. FFPsyWindowInfo* wi = windows + start_ch;
  462. tag = chan_map[i+1];
  463. chans = tag == TYPE_CPE ? 2 : 1;
  464. cpe = &s->cpe[i];
  465. samples2 = samples + start_ch;
  466. la = samples2 + (448+64) * avctx->channels + start_ch;
  467. if (!data)
  468. la = NULL;
  469. for (j = 0; j < chans; j++) {
  470. IndividualChannelStream *ics = &cpe->ch[j].ics;
  471. int k;
  472. wi[j] = ff_psy_suggest_window(&s->psy, samples2, la, start_ch + j, ics->window_sequence[0]);
  473. ics->window_sequence[1] = ics->window_sequence[0];
  474. ics->window_sequence[0] = wi[j].window_type[0];
  475. ics->use_kb_window[1] = ics->use_kb_window[0];
  476. ics->use_kb_window[0] = wi[j].window_shape;
  477. ics->num_windows = wi[j].num_windows;
  478. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  479. ics->num_swb = s->psy.num_bands[ics->num_windows == 8];
  480. for (k = 0; k < ics->num_windows; k++)
  481. ics->group_len[k] = wi[j].grouping[k];
  482. s->cur_channel = start_ch + j;
  483. apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2, j);
  484. }
  485. start_ch += chans;
  486. }
  487. do {
  488. int frame_bits;
  489. init_put_bits(&s->pb, frame, buf_size*8);
  490. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
  491. put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
  492. start_ch = 0;
  493. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  494. for (i = 0; i < chan_map[0]; i++) {
  495. FFPsyWindowInfo* wi = windows + start_ch;
  496. tag = chan_map[i+1];
  497. chans = tag == TYPE_CPE ? 2 : 1;
  498. cpe = &s->cpe[i];
  499. for (j = 0; j < chans; j++) {
  500. s->cur_channel = start_ch + j;
  501. ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
  502. s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
  503. }
  504. cpe->common_window = 0;
  505. if (chans > 1
  506. && wi[0].window_type[0] == wi[1].window_type[0]
  507. && wi[0].window_shape == wi[1].window_shape) {
  508. cpe->common_window = 1;
  509. for (j = 0; j < wi[0].num_windows; j++) {
  510. if (wi[0].grouping[j] != wi[1].grouping[j]) {
  511. cpe->common_window = 0;
  512. break;
  513. }
  514. }
  515. }
  516. s->cur_channel = start_ch;
  517. if (cpe->common_window && s->coder->search_for_ms)
  518. s->coder->search_for_ms(s, cpe, s->lambda);
  519. adjust_frame_information(s, cpe, chans);
  520. put_bits(&s->pb, 3, tag);
  521. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  522. if (chans == 2) {
  523. put_bits(&s->pb, 1, cpe->common_window);
  524. if (cpe->common_window) {
  525. put_ics_info(s, &cpe->ch[0].ics);
  526. encode_ms_info(&s->pb, cpe);
  527. }
  528. }
  529. for (j = 0; j < chans; j++) {
  530. s->cur_channel = start_ch + j;
  531. encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
  532. }
  533. start_ch += chans;
  534. }
  535. frame_bits = put_bits_count(&s->pb);
  536. if (frame_bits <= 6144 * avctx->channels - 3)
  537. break;
  538. s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
  539. } while (1);
  540. put_bits(&s->pb, 3, TYPE_END);
  541. flush_put_bits(&s->pb);
  542. avctx->frame_bits = put_bits_count(&s->pb);
  543. // rate control stuff
  544. if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
  545. float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
  546. s->lambda *= ratio;
  547. s->lambda = FFMIN(s->lambda, 65536.f);
  548. }
  549. if (!data)
  550. s->last_frame = 1;
  551. memcpy(s->samples, s->samples + 1024 * avctx->channels,
  552. 1024 * avctx->channels * sizeof(s->samples[0]));
  553. return put_bits_count(&s->pb)>>3;
  554. }
  555. static av_cold int aac_encode_end(AVCodecContext *avctx)
  556. {
  557. AACEncContext *s = avctx->priv_data;
  558. ff_mdct_end(&s->mdct1024);
  559. ff_mdct_end(&s->mdct128);
  560. ff_psy_end(&s->psy);
  561. ff_psy_preprocess_end(s->psypp);
  562. av_freep(&s->samples);
  563. av_freep(&s->cpe);
  564. return 0;
  565. }
  566. AVCodec aac_encoder = {
  567. "aac",
  568. AVMEDIA_TYPE_AUDIO,
  569. CODEC_ID_AAC,
  570. sizeof(AACEncContext),
  571. aac_encode_init,
  572. aac_encode_frame,
  573. aac_encode_end,
  574. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
  575. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  576. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  577. };