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.

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