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.

827 lines
28KB

  1. /*
  2. * AAC encoder
  3. * Copyright (C) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 * const 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 * const 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 Libav'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. if (diff < 0 || diff > 120)
  336. av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
  337. off = sce->sf_idx[w*16 + i];
  338. put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
  339. }
  340. }
  341. }
  342. }
  343. /**
  344. * Encode pulse data.
  345. */
  346. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  347. {
  348. int i;
  349. put_bits(&s->pb, 1, !!pulse->num_pulse);
  350. if (!pulse->num_pulse)
  351. return;
  352. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  353. put_bits(&s->pb, 6, pulse->start);
  354. for (i = 0; i < pulse->num_pulse; i++) {
  355. put_bits(&s->pb, 5, pulse->pos[i]);
  356. put_bits(&s->pb, 4, pulse->amp[i]);
  357. }
  358. }
  359. /**
  360. * Encode spectral coefficients processed by psychoacoustic model.
  361. */
  362. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  363. {
  364. int start, i, w, w2;
  365. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  366. start = 0;
  367. for (i = 0; i < sce->ics.max_sfb; i++) {
  368. if (sce->zeroes[w*16 + i]) {
  369. start += sce->ics.swb_sizes[i];
  370. continue;
  371. }
  372. for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
  373. s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
  374. sce->ics.swb_sizes[i],
  375. sce->sf_idx[w*16 + i],
  376. sce->band_type[w*16 + i],
  377. s->lambda);
  378. start += sce->ics.swb_sizes[i];
  379. }
  380. }
  381. }
  382. /**
  383. * Encode one channel of audio data.
  384. */
  385. static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
  386. SingleChannelElement *sce,
  387. int common_window)
  388. {
  389. put_bits(&s->pb, 8, sce->sf_idx[0]);
  390. if (!common_window)
  391. put_ics_info(s, &sce->ics);
  392. encode_band_info(s, sce);
  393. encode_scale_factors(avctx, s, sce);
  394. encode_pulses(s, &sce->pulse);
  395. put_bits(&s->pb, 1, 0); //tns
  396. put_bits(&s->pb, 1, 0); //ssr
  397. encode_spectral_coeffs(s, sce);
  398. return 0;
  399. }
  400. /**
  401. * Write some auxiliary information about the created AAC file.
  402. */
  403. static void put_bitstream_info(AACEncContext *s, const char *name)
  404. {
  405. int i, namelen, padbits;
  406. namelen = strlen(name) + 2;
  407. put_bits(&s->pb, 3, TYPE_FIL);
  408. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  409. if (namelen >= 15)
  410. put_bits(&s->pb, 8, namelen - 14);
  411. put_bits(&s->pb, 4, 0); //extension type - filler
  412. padbits = -put_bits_count(&s->pb) & 7;
  413. avpriv_align_put_bits(&s->pb);
  414. for (i = 0; i < namelen - 2; i++)
  415. put_bits(&s->pb, 8, name[i]);
  416. put_bits(&s->pb, 12 - padbits, 0);
  417. }
  418. /*
  419. * Copy input samples.
  420. * Channels are reordered from Libav's default order to AAC order.
  421. */
  422. static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
  423. {
  424. int ch;
  425. int end = 2048 + (frame ? frame->nb_samples : 0);
  426. const uint8_t *channel_map = aac_chan_maps[s->channels - 1];
  427. /* copy and remap input samples */
  428. for (ch = 0; ch < s->channels; ch++) {
  429. /* copy last 1024 samples of previous frame to the start of the current frame */
  430. memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
  431. /* copy new samples and zero any remaining samples */
  432. if (frame) {
  433. memcpy(&s->planar_samples[ch][2048],
  434. frame->extended_data[channel_map[ch]],
  435. frame->nb_samples * sizeof(s->planar_samples[0][0]));
  436. }
  437. memset(&s->planar_samples[ch][end], 0,
  438. (3072 - end) * 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. int frame_bits;
  450. FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
  451. if (s->last_frame == 2)
  452. return 0;
  453. /* add current frame to queue */
  454. if (frame) {
  455. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  456. return ret;
  457. }
  458. copy_input_samples(s, frame);
  459. if (s->psypp)
  460. ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
  461. if (!avctx->frame_number)
  462. return 0;
  463. start_ch = 0;
  464. for (i = 0; i < s->chan_map[0]; i++) {
  465. FFPsyWindowInfo* wi = windows + start_ch;
  466. tag = s->chan_map[i+1];
  467. chans = tag == TYPE_CPE ? 2 : 1;
  468. cpe = &s->cpe[i];
  469. for (ch = 0; ch < chans; ch++) {
  470. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  471. int cur_channel = start_ch + ch;
  472. overlap = &samples[cur_channel][0];
  473. samples2 = overlap + 1024;
  474. la = samples2 + (448+64);
  475. if (!frame)
  476. la = NULL;
  477. if (tag == TYPE_LFE) {
  478. wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
  479. wi[ch].window_shape = 0;
  480. wi[ch].num_windows = 1;
  481. wi[ch].grouping[0] = 1;
  482. /* Only the lowest 12 coefficients are used in a LFE channel.
  483. * The expression below results in only the bottom 8 coefficients
  484. * being used for 11.025kHz to 16kHz sample rates.
  485. */
  486. ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
  487. } else {
  488. wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
  489. ics->window_sequence[0]);
  490. }
  491. ics->window_sequence[1] = ics->window_sequence[0];
  492. ics->window_sequence[0] = wi[ch].window_type[0];
  493. ics->use_kb_window[1] = ics->use_kb_window[0];
  494. ics->use_kb_window[0] = wi[ch].window_shape;
  495. ics->num_windows = wi[ch].num_windows;
  496. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  497. ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
  498. for (w = 0; w < ics->num_windows; w++)
  499. ics->group_len[w] = wi[ch].grouping[w];
  500. apply_window_and_mdct(s, &cpe->ch[ch], overlap);
  501. }
  502. start_ch += chans;
  503. }
  504. if ((ret = ff_alloc_packet(avpkt, 768 * s->channels))) {
  505. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  506. return ret;
  507. }
  508. do {
  509. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  510. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  511. put_bitstream_info(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 + 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;
  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(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. frame_bits = put_bits_count(&s->pb);
  576. #if FF_API_STAT_BITS
  577. FF_DISABLE_DEPRECATION_WARNINGS
  578. avctx->frame_bits = frame_bits;
  579. FF_ENABLE_DEPRECATION_WARNINGS
  580. #endif
  581. // rate control stuff
  582. if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  583. float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / 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. return 0;
  607. }
  608. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  609. {
  610. int ret = 0;
  611. avpriv_float_dsp_init(&s->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
  612. // window init
  613. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  614. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  615. ff_init_ff_sine_windows(10);
  616. ff_init_ff_sine_windows(7);
  617. if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
  618. return ret;
  619. if (ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0))
  620. return ret;
  621. return 0;
  622. }
  623. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  624. {
  625. int ch;
  626. FF_ALLOCZ_OR_GOTO(avctx, s->buffer.samples, 3 * 1024 * s->channels * sizeof(s->buffer.samples[0]), alloc_fail);
  627. FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
  628. FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + AV_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
  629. for(ch = 0; ch < s->channels; ch++)
  630. s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
  631. return 0;
  632. alloc_fail:
  633. return AVERROR(ENOMEM);
  634. }
  635. static av_cold int aac_encode_init(AVCodecContext *avctx)
  636. {
  637. AACEncContext *s = avctx->priv_data;
  638. int i, ret = 0;
  639. const uint8_t *sizes[2];
  640. uint8_t grouping[AAC_MAX_CHANNELS];
  641. int lengths[2];
  642. avctx->frame_size = 1024;
  643. for (i = 0; i < 16; i++)
  644. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
  645. break;
  646. s->channels = avctx->channels;
  647. ERROR_IF(i == 16,
  648. "Unsupported sample rate %d\n", avctx->sample_rate);
  649. ERROR_IF(s->channels > AAC_MAX_CHANNELS,
  650. "Unsupported number of channels: %d\n", s->channels);
  651. ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
  652. "Unsupported profile %d\n", avctx->profile);
  653. ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  654. "Too many bits %f > %d per frame requested\n",
  655. 1024.0 * avctx->bit_rate / avctx->sample_rate,
  656. 6144 * s->channels);
  657. s->samplerate_index = i;
  658. s->chan_map = aac_chan_configs[s->channels-1];
  659. if ((ret = dsp_init(avctx, s)) < 0)
  660. goto fail;
  661. if ((ret = alloc_buffers(avctx, s)) < 0)
  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,
  672. s->chan_map[0], grouping)) < 0)
  673. goto fail;
  674. s->psypp = ff_psy_preprocess_init(avctx);
  675. s->coder = &ff_aac_coders[2];
  676. s->lambda = avctx->global_quality ? avctx->global_quality : 120;
  677. ff_aac_tableinit();
  678. for (i = 0; i < 428; i++)
  679. ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
  680. avctx->initial_padding = 1024;
  681. ff_af_queue_init(avctx, &s->afq);
  682. return 0;
  683. fail:
  684. aac_encode_end(avctx);
  685. return ret;
  686. }
  687. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  688. static const AVOption aacenc_options[] = {
  689. {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.i64 = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
  690. {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
  691. {"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
  692. {"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"},
  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. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  704. .type = AVMEDIA_TYPE_AUDIO,
  705. .id = AV_CODEC_ID_AAC,
  706. .priv_data_size = sizeof(AACEncContext),
  707. .init = aac_encode_init,
  708. .encode2 = aac_encode_frame,
  709. .close = aac_encode_end,
  710. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY |
  711. AV_CODEC_CAP_EXPERIMENTAL,
  712. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  713. AV_SAMPLE_FMT_NONE },
  714. .priv_class = &aacenc_class,
  715. };