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.

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