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.

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