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.

660 lines
23KB

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