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.

686 lines
24KB

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