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.

833 lines
28KB

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