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.

793 lines
27KB

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