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.

716 lines
25KB

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