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.

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