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.

698 lines
25KB

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