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.

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