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.

642 lines
22KB

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