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.

1057 lines
40KB

  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. ***********************************/
  29. #include "libavutil/thread.h"
  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 "aacenctab.h"
  42. #include "aacenc_utils.h"
  43. #include "psymodel.h"
  44. static AVOnce aac_table_init = AV_ONCE_INIT;
  45. /**
  46. * Make AAC audio config object.
  47. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  48. */
  49. static void put_audio_specific_config(AVCodecContext *avctx)
  50. {
  51. PutBitContext pb;
  52. AACEncContext *s = avctx->priv_data;
  53. int channels = s->channels - (s->channels == 8 ? 1 : 0);
  54. init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
  55. put_bits(&pb, 5, s->profile+1); //profile
  56. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  57. put_bits(&pb, 4, channels);
  58. //GASpecificConfig
  59. put_bits(&pb, 1, 0); //frame length - 1024 samples
  60. put_bits(&pb, 1, 0); //does not depend on core coder
  61. put_bits(&pb, 1, 0); //is not extension
  62. //Explicitly Mark SBR absent
  63. put_bits(&pb, 11, 0x2b7); //sync extension
  64. put_bits(&pb, 5, AOT_SBR);
  65. put_bits(&pb, 1, 0);
  66. flush_put_bits(&pb);
  67. }
  68. void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
  69. {
  70. int sf, g;
  71. for (sf = 0; sf < 256; sf++) {
  72. for (g = 0; g < 128; g++) {
  73. s->quantize_band_cost_cache[sf][g].bits = -1;
  74. }
  75. }
  76. }
  77. #define WINDOW_FUNC(type) \
  78. static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
  79. SingleChannelElement *sce, \
  80. const float *audio)
  81. WINDOW_FUNC(only_long)
  82. {
  83. const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  84. const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  85. float *out = sce->ret_buf;
  86. fdsp->vector_fmul (out, audio, lwindow, 1024);
  87. fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
  88. }
  89. WINDOW_FUNC(long_start)
  90. {
  91. const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  92. const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  93. float *out = sce->ret_buf;
  94. fdsp->vector_fmul(out, audio, lwindow, 1024);
  95. memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
  96. fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
  97. memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
  98. }
  99. WINDOW_FUNC(long_stop)
  100. {
  101. const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  102. const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  103. float *out = sce->ret_buf;
  104. memset(out, 0, sizeof(out[0]) * 448);
  105. fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
  106. memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
  107. fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
  108. }
  109. WINDOW_FUNC(eight_short)
  110. {
  111. const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  112. const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  113. const float *in = audio + 448;
  114. float *out = sce->ret_buf;
  115. int w;
  116. for (w = 0; w < 8; w++) {
  117. fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
  118. out += 128;
  119. in += 128;
  120. fdsp->vector_fmul_reverse(out, in, swindow, 128);
  121. out += 128;
  122. }
  123. }
  124. static void (*const apply_window[4])(AVFloatDSPContext *fdsp,
  125. SingleChannelElement *sce,
  126. const float *audio) = {
  127. [ONLY_LONG_SEQUENCE] = apply_only_long_window,
  128. [LONG_START_SEQUENCE] = apply_long_start_window,
  129. [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
  130. [LONG_STOP_SEQUENCE] = apply_long_stop_window
  131. };
  132. static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
  133. float *audio)
  134. {
  135. int i;
  136. float *output = sce->ret_buf;
  137. apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);
  138. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)
  139. s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
  140. else
  141. for (i = 0; i < 1024; i += 128)
  142. s->mdct128.mdct_calc(&s->mdct128, &sce->coeffs[i], output + i*2);
  143. memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
  144. memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
  145. }
  146. /**
  147. * Encode ics_info element.
  148. * @see Table 4.6 (syntax of ics_info)
  149. */
  150. static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
  151. {
  152. int w;
  153. put_bits(&s->pb, 1, 0); // ics_reserved bit
  154. put_bits(&s->pb, 2, info->window_sequence[0]);
  155. put_bits(&s->pb, 1, info->use_kb_window[0]);
  156. if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  157. put_bits(&s->pb, 6, info->max_sfb);
  158. put_bits(&s->pb, 1, !!info->predictor_present);
  159. } else {
  160. put_bits(&s->pb, 4, info->max_sfb);
  161. for (w = 1; w < 8; w++)
  162. put_bits(&s->pb, 1, !info->group_len[w]);
  163. }
  164. }
  165. /**
  166. * Encode MS data.
  167. * @see 4.6.8.1 "Joint Coding - M/S Stereo"
  168. */
  169. static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
  170. {
  171. int i, w;
  172. put_bits(pb, 2, cpe->ms_mode);
  173. if (cpe->ms_mode == 1)
  174. for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
  175. for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
  176. put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
  177. }
  178. /**
  179. * Produce integer coefficients from scalefactors provided by the model.
  180. */
  181. static void adjust_frame_information(ChannelElement *cpe, int chans)
  182. {
  183. int i, w, w2, g, ch;
  184. int maxsfb, cmaxsfb;
  185. for (ch = 0; ch < chans; ch++) {
  186. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  187. maxsfb = 0;
  188. cpe->ch[ch].pulse.num_pulse = 0;
  189. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  190. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  191. for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
  192. ;
  193. maxsfb = FFMAX(maxsfb, cmaxsfb);
  194. }
  195. }
  196. ics->max_sfb = maxsfb;
  197. //adjust zero bands for window groups
  198. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  199. for (g = 0; g < ics->max_sfb; g++) {
  200. i = 1;
  201. for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
  202. if (!cpe->ch[ch].zeroes[w2*16 + g]) {
  203. i = 0;
  204. break;
  205. }
  206. }
  207. cpe->ch[ch].zeroes[w*16 + g] = i;
  208. }
  209. }
  210. }
  211. if (chans > 1 && cpe->common_window) {
  212. IndividualChannelStream *ics0 = &cpe->ch[0].ics;
  213. IndividualChannelStream *ics1 = &cpe->ch[1].ics;
  214. int msc = 0;
  215. ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
  216. ics1->max_sfb = ics0->max_sfb;
  217. for (w = 0; w < ics0->num_windows*16; w += 16)
  218. for (i = 0; i < ics0->max_sfb; i++)
  219. if (cpe->ms_mask[w+i])
  220. msc++;
  221. if (msc == 0 || ics0->max_sfb == 0)
  222. cpe->ms_mode = 0;
  223. else
  224. cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
  225. }
  226. }
  227. static void apply_intensity_stereo(ChannelElement *cpe)
  228. {
  229. int w, w2, g, i;
  230. IndividualChannelStream *ics = &cpe->ch[0].ics;
  231. if (!cpe->common_window)
  232. return;
  233. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  234. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  235. int start = (w+w2) * 128;
  236. for (g = 0; g < ics->num_swb; g++) {
  237. int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
  238. float scale = cpe->ch[0].is_ener[w*16+g];
  239. if (!cpe->is_mask[w*16 + g]) {
  240. start += ics->swb_sizes[g];
  241. continue;
  242. }
  243. if (cpe->ms_mask[w*16 + g])
  244. p *= -1;
  245. for (i = 0; i < ics->swb_sizes[g]; i++) {
  246. float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;
  247. cpe->ch[0].coeffs[start+i] = sum;
  248. cpe->ch[1].coeffs[start+i] = 0.0f;
  249. }
  250. start += ics->swb_sizes[g];
  251. }
  252. }
  253. }
  254. }
  255. static void apply_mid_side_stereo(ChannelElement *cpe)
  256. {
  257. int w, w2, g, i;
  258. IndividualChannelStream *ics = &cpe->ch[0].ics;
  259. if (!cpe->common_window)
  260. return;
  261. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  262. for (w2 = 0; w2 < ics->group_len[w]; w2++) {
  263. int start = (w+w2) * 128;
  264. for (g = 0; g < ics->num_swb; g++) {
  265. /* ms_mask can be used for other purposes in PNS and I/S,
  266. * so must not apply M/S if any band uses either, even if
  267. * ms_mask is set.
  268. */
  269. if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g]
  270. || cpe->ch[0].band_type[w*16 + g] == NOISE_BT
  271. || cpe->ch[1].band_type[w*16 + g] == NOISE_BT) {
  272. start += ics->swb_sizes[g];
  273. continue;
  274. }
  275. for (i = 0; i < ics->swb_sizes[g]; i++) {
  276. float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;
  277. float R = L - cpe->ch[1].coeffs[start+i];
  278. cpe->ch[0].coeffs[start+i] = L;
  279. cpe->ch[1].coeffs[start+i] = R;
  280. }
  281. start += ics->swb_sizes[g];
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Encode scalefactor band coding type.
  288. */
  289. static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
  290. {
  291. int w;
  292. if (s->coder->set_special_band_scalefactors)
  293. s->coder->set_special_band_scalefactors(s, sce);
  294. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  295. s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
  296. }
  297. /**
  298. * Encode scalefactors.
  299. */
  300. static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
  301. SingleChannelElement *sce)
  302. {
  303. int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
  304. int off_is = 0, noise_flag = 1;
  305. int i, w;
  306. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  307. for (i = 0; i < sce->ics.max_sfb; i++) {
  308. if (!sce->zeroes[w*16 + i]) {
  309. if (sce->band_type[w*16 + i] == NOISE_BT) {
  310. diff = sce->sf_idx[w*16 + i] - off_pns;
  311. off_pns = sce->sf_idx[w*16 + i];
  312. if (noise_flag-- > 0) {
  313. put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);
  314. continue;
  315. }
  316. } else if (sce->band_type[w*16 + i] == INTENSITY_BT ||
  317. sce->band_type[w*16 + i] == INTENSITY_BT2) {
  318. diff = sce->sf_idx[w*16 + i] - off_is;
  319. off_is = sce->sf_idx[w*16 + i];
  320. } else {
  321. diff = sce->sf_idx[w*16 + i] - off_sf;
  322. off_sf = sce->sf_idx[w*16 + i];
  323. }
  324. diff += SCALE_DIFF_ZERO;
  325. av_assert0(diff >= 0 && diff <= 120);
  326. put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * Encode pulse data.
  333. */
  334. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  335. {
  336. int i;
  337. put_bits(&s->pb, 1, !!pulse->num_pulse);
  338. if (!pulse->num_pulse)
  339. return;
  340. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  341. put_bits(&s->pb, 6, pulse->start);
  342. for (i = 0; i < pulse->num_pulse; i++) {
  343. put_bits(&s->pb, 5, pulse->pos[i]);
  344. put_bits(&s->pb, 4, pulse->amp[i]);
  345. }
  346. }
  347. /**
  348. * Encode spectral coefficients processed by psychoacoustic model.
  349. */
  350. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  351. {
  352. int start, i, w, w2;
  353. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  354. start = 0;
  355. for (i = 0; i < sce->ics.max_sfb; i++) {
  356. if (sce->zeroes[w*16 + i]) {
  357. start += sce->ics.swb_sizes[i];
  358. continue;
  359. }
  360. for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
  361. s->coder->quantize_and_encode_band(s, &s->pb,
  362. &sce->coeffs[start + w2*128],
  363. NULL, sce->ics.swb_sizes[i],
  364. sce->sf_idx[w*16 + i],
  365. sce->band_type[w*16 + i],
  366. s->lambda,
  367. sce->ics.window_clipping[w]);
  368. }
  369. start += sce->ics.swb_sizes[i];
  370. }
  371. }
  372. }
  373. /**
  374. * Downscale spectral coefficients for near-clipping windows to avoid artifacts
  375. */
  376. static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
  377. {
  378. int start, i, j, w;
  379. if (sce->ics.clip_avoidance_factor < 1.0f) {
  380. for (w = 0; w < sce->ics.num_windows; w++) {
  381. start = 0;
  382. for (i = 0; i < sce->ics.max_sfb; i++) {
  383. float *swb_coeffs = &sce->coeffs[start + w*128];
  384. for (j = 0; j < sce->ics.swb_sizes[i]; j++)
  385. swb_coeffs[j] *= sce->ics.clip_avoidance_factor;
  386. start += sce->ics.swb_sizes[i];
  387. }
  388. }
  389. }
  390. }
  391. /**
  392. * Encode one channel of audio data.
  393. */
  394. static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
  395. SingleChannelElement *sce,
  396. int common_window)
  397. {
  398. put_bits(&s->pb, 8, sce->sf_idx[0]);
  399. if (!common_window) {
  400. put_ics_info(s, &sce->ics);
  401. if (s->coder->encode_main_pred)
  402. s->coder->encode_main_pred(s, sce);
  403. if (s->coder->encode_ltp_info)
  404. s->coder->encode_ltp_info(s, sce, 0);
  405. }
  406. encode_band_info(s, sce);
  407. encode_scale_factors(avctx, s, sce);
  408. encode_pulses(s, &sce->pulse);
  409. put_bits(&s->pb, 1, !!sce->tns.present);
  410. if (s->coder->encode_tns_info)
  411. s->coder->encode_tns_info(s, sce);
  412. put_bits(&s->pb, 1, 0); //ssr
  413. encode_spectral_coeffs(s, sce);
  414. return 0;
  415. }
  416. /**
  417. * Write some auxiliary information about the created AAC file.
  418. */
  419. static void put_bitstream_info(AACEncContext *s, const char *name)
  420. {
  421. int i, namelen, padbits;
  422. namelen = strlen(name) + 2;
  423. put_bits(&s->pb, 3, TYPE_FIL);
  424. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  425. if (namelen >= 15)
  426. put_bits(&s->pb, 8, namelen - 14);
  427. put_bits(&s->pb, 4, 0); //extension type - filler
  428. padbits = -put_bits_count(&s->pb) & 7;
  429. avpriv_align_put_bits(&s->pb);
  430. for (i = 0; i < namelen - 2; i++)
  431. put_bits(&s->pb, 8, name[i]);
  432. put_bits(&s->pb, 12 - padbits, 0);
  433. }
  434. /*
  435. * Copy input samples.
  436. * Channels are reordered from libavcodec's default order to AAC order.
  437. */
  438. static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
  439. {
  440. int ch;
  441. int end = 2048 + (frame ? frame->nb_samples : 0);
  442. const uint8_t *channel_map = aac_chan_maps[s->channels - 1];
  443. /* copy and remap input samples */
  444. for (ch = 0; ch < s->channels; ch++) {
  445. /* copy last 1024 samples of previous frame to the start of the current frame */
  446. memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
  447. /* copy new samples and zero any remaining samples */
  448. if (frame) {
  449. memcpy(&s->planar_samples[ch][2048],
  450. frame->extended_data[channel_map[ch]],
  451. frame->nb_samples * sizeof(s->planar_samples[0][0]));
  452. }
  453. memset(&s->planar_samples[ch][end], 0,
  454. (3072 - end) * sizeof(s->planar_samples[0][0]));
  455. }
  456. }
  457. static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  458. const AVFrame *frame, int *got_packet_ptr)
  459. {
  460. AACEncContext *s = avctx->priv_data;
  461. float **samples = s->planar_samples, *samples2, *la, *overlap;
  462. ChannelElement *cpe;
  463. SingleChannelElement *sce;
  464. IndividualChannelStream *ics;
  465. int i, its, ch, w, chans, tag, start_ch, ret, frame_bits;
  466. int target_bits, rate_bits, too_many_bits, too_few_bits;
  467. int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
  468. int chan_el_counter[4];
  469. FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
  470. if (s->last_frame == 2)
  471. return 0;
  472. /* add current frame to queue */
  473. if (frame) {
  474. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  475. return ret;
  476. }
  477. copy_input_samples(s, frame);
  478. if (s->psypp)
  479. ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
  480. if (!avctx->frame_number)
  481. return 0;
  482. start_ch = 0;
  483. for (i = 0; i < s->chan_map[0]; i++) {
  484. FFPsyWindowInfo* wi = windows + start_ch;
  485. tag = s->chan_map[i+1];
  486. chans = tag == TYPE_CPE ? 2 : 1;
  487. cpe = &s->cpe[i];
  488. for (ch = 0; ch < chans; ch++) {
  489. float clip_avoidance_factor;
  490. sce = &cpe->ch[ch];
  491. ics = &sce->ics;
  492. s->cur_channel = start_ch + ch;
  493. overlap = &samples[s->cur_channel][0];
  494. samples2 = overlap + 1024;
  495. la = samples2 + (448+64);
  496. if (!frame)
  497. la = NULL;
  498. if (tag == TYPE_LFE) {
  499. wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
  500. wi[ch].window_shape = 0;
  501. wi[ch].num_windows = 1;
  502. wi[ch].grouping[0] = 1;
  503. /* Only the lowest 12 coefficients are used in a LFE channel.
  504. * The expression below results in only the bottom 8 coefficients
  505. * being used for 11.025kHz to 16kHz sample rates.
  506. */
  507. ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
  508. } else {
  509. wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel,
  510. ics->window_sequence[0]);
  511. }
  512. ics->window_sequence[1] = ics->window_sequence[0];
  513. ics->window_sequence[0] = wi[ch].window_type[0];
  514. ics->use_kb_window[1] = ics->use_kb_window[0];
  515. ics->use_kb_window[0] = wi[ch].window_shape;
  516. ics->num_windows = wi[ch].num_windows;
  517. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  518. ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
  519. ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb);
  520. ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  521. ff_swb_offset_128 [s->samplerate_index]:
  522. ff_swb_offset_1024[s->samplerate_index];
  523. ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  524. ff_tns_max_bands_128 [s->samplerate_index]:
  525. ff_tns_max_bands_1024[s->samplerate_index];
  526. clip_avoidance_factor = 0.0f;
  527. for (w = 0; w < ics->num_windows; w++)
  528. ics->group_len[w] = wi[ch].grouping[w];
  529. for (w = 0; w < ics->num_windows; w++) {
  530. if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
  531. ics->window_clipping[w] = 1;
  532. clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
  533. } else {
  534. ics->window_clipping[w] = 0;
  535. }
  536. }
  537. if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
  538. ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
  539. } else {
  540. ics->clip_avoidance_factor = 1.0f;
  541. }
  542. apply_window_and_mdct(s, sce, overlap);
  543. if (s->options.ltp && s->coder->update_ltp) {
  544. s->coder->update_ltp(s, sce);
  545. apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]);
  546. s->mdct1024.mdct_calc(&s->mdct1024, sce->lcoeffs, sce->ret_buf);
  547. }
  548. if (isnan(cpe->ch->coeffs[0])) {
  549. av_log(avctx, AV_LOG_ERROR, "Input contains NaN\n");
  550. return AVERROR(EINVAL);
  551. }
  552. avoid_clipping(s, sce);
  553. }
  554. start_ch += chans;
  555. }
  556. if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels, 0)) < 0)
  557. return ret;
  558. frame_bits = its = 0;
  559. do {
  560. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  561. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  562. put_bitstream_info(s, LIBAVCODEC_IDENT);
  563. start_ch = 0;
  564. target_bits = 0;
  565. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  566. for (i = 0; i < s->chan_map[0]; i++) {
  567. FFPsyWindowInfo* wi = windows + start_ch;
  568. const float *coeffs[2];
  569. tag = s->chan_map[i+1];
  570. chans = tag == TYPE_CPE ? 2 : 1;
  571. cpe = &s->cpe[i];
  572. cpe->common_window = 0;
  573. memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
  574. memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
  575. put_bits(&s->pb, 3, tag);
  576. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  577. for (ch = 0; ch < chans; ch++) {
  578. sce = &cpe->ch[ch];
  579. coeffs[ch] = sce->coeffs;
  580. sce->ics.predictor_present = 0;
  581. sce->ics.ltp.present = 0;
  582. memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
  583. memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
  584. memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
  585. for (w = 0; w < 128; w++)
  586. if (sce->band_type[w] > RESERVED_BT)
  587. sce->band_type[w] = 0;
  588. }
  589. s->psy.bitres.alloc = -1;
  590. s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
  591. s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
  592. if (s->psy.bitres.alloc > 0) {
  593. /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
  594. target_bits += s->psy.bitres.alloc
  595. * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
  596. s->psy.bitres.alloc /= chans;
  597. }
  598. s->cur_type = tag;
  599. for (ch = 0; ch < chans; ch++) {
  600. s->cur_channel = start_ch + ch;
  601. if (s->options.pns && s->coder->mark_pns)
  602. s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
  603. s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
  604. }
  605. if (chans > 1
  606. && wi[0].window_type[0] == wi[1].window_type[0]
  607. && wi[0].window_shape == wi[1].window_shape) {
  608. cpe->common_window = 1;
  609. for (w = 0; w < wi[0].num_windows; w++) {
  610. if (wi[0].grouping[w] != wi[1].grouping[w]) {
  611. cpe->common_window = 0;
  612. break;
  613. }
  614. }
  615. }
  616. for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
  617. sce = &cpe->ch[ch];
  618. s->cur_channel = start_ch + ch;
  619. if (s->options.tns && s->coder->search_for_tns)
  620. s->coder->search_for_tns(s, sce);
  621. if (s->options.tns && s->coder->apply_tns_filt)
  622. s->coder->apply_tns_filt(s, sce);
  623. if (sce->tns.present)
  624. tns_mode = 1;
  625. if (s->options.pns && s->coder->search_for_pns)
  626. s->coder->search_for_pns(s, avctx, sce);
  627. }
  628. s->cur_channel = start_ch;
  629. if (s->options.intensity_stereo) { /* Intensity Stereo */
  630. if (s->coder->search_for_is)
  631. s->coder->search_for_is(s, avctx, cpe);
  632. if (cpe->is_mode) is_mode = 1;
  633. apply_intensity_stereo(cpe);
  634. }
  635. if (s->options.pred) { /* Prediction */
  636. for (ch = 0; ch < chans; ch++) {
  637. sce = &cpe->ch[ch];
  638. s->cur_channel = start_ch + ch;
  639. if (s->options.pred && s->coder->search_for_pred)
  640. s->coder->search_for_pred(s, sce);
  641. if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
  642. }
  643. if (s->coder->adjust_common_pred)
  644. s->coder->adjust_common_pred(s, cpe);
  645. for (ch = 0; ch < chans; ch++) {
  646. sce = &cpe->ch[ch];
  647. s->cur_channel = start_ch + ch;
  648. if (s->options.pred && s->coder->apply_main_pred)
  649. s->coder->apply_main_pred(s, sce);
  650. }
  651. s->cur_channel = start_ch;
  652. }
  653. if (s->options.mid_side) { /* Mid/Side stereo */
  654. if (s->options.mid_side == -1 && s->coder->search_for_ms)
  655. s->coder->search_for_ms(s, cpe);
  656. else if (cpe->common_window)
  657. memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
  658. apply_mid_side_stereo(cpe);
  659. }
  660. adjust_frame_information(cpe, chans);
  661. if (s->options.ltp) { /* LTP */
  662. for (ch = 0; ch < chans; ch++) {
  663. sce = &cpe->ch[ch];
  664. s->cur_channel = start_ch + ch;
  665. if (s->coder->search_for_ltp)
  666. s->coder->search_for_ltp(s, sce, cpe->common_window);
  667. if (sce->ics.ltp.present) pred_mode = 1;
  668. }
  669. s->cur_channel = start_ch;
  670. if (s->coder->adjust_common_ltp)
  671. s->coder->adjust_common_ltp(s, cpe);
  672. }
  673. if (chans == 2) {
  674. put_bits(&s->pb, 1, cpe->common_window);
  675. if (cpe->common_window) {
  676. put_ics_info(s, &cpe->ch[0].ics);
  677. if (s->coder->encode_main_pred)
  678. s->coder->encode_main_pred(s, &cpe->ch[0]);
  679. if (s->coder->encode_ltp_info)
  680. s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
  681. encode_ms_info(&s->pb, cpe);
  682. if (cpe->ms_mode) ms_mode = 1;
  683. }
  684. }
  685. for (ch = 0; ch < chans; ch++) {
  686. s->cur_channel = start_ch + ch;
  687. encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
  688. }
  689. start_ch += chans;
  690. }
  691. if (avctx->flags & CODEC_FLAG_QSCALE) {
  692. /* When using a constant Q-scale, don't mess with lambda */
  693. break;
  694. }
  695. /* rate control stuff
  696. * allow between the nominal bitrate, and what psy's bit reservoir says to target
  697. * but drift towards the nominal bitrate always
  698. */
  699. frame_bits = put_bits_count(&s->pb);
  700. rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
  701. rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
  702. too_many_bits = FFMAX(target_bits, rate_bits);
  703. too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
  704. too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
  705. /* When using ABR, be strict (but only for increasing) */
  706. too_few_bits = too_few_bits - too_few_bits/8;
  707. too_many_bits = too_many_bits + too_many_bits/2;
  708. if ( its == 0 /* for steady-state Q-scale tracking */
  709. || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
  710. || frame_bits >= 6144 * s->channels - 3 )
  711. {
  712. float ratio = ((float)rate_bits) / frame_bits;
  713. if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
  714. /*
  715. * This path is for steady-state Q-scale tracking
  716. * When frame bits fall within the stable range, we still need to adjust
  717. * lambda to maintain it like so in a stable fashion (large jumps in lambda
  718. * create artifacts and should be avoided), but slowly
  719. */
  720. ratio = sqrtf(sqrtf(ratio));
  721. ratio = av_clipf(ratio, 0.9f, 1.1f);
  722. } else {
  723. /* Not so fast though */
  724. ratio = sqrtf(ratio);
  725. }
  726. s->lambda = FFMIN(s->lambda * ratio, 65536.f);
  727. /* Keep iterating if we must reduce and lambda is in the sky */
  728. if (ratio > 0.9f && ratio < 1.1f) {
  729. break;
  730. } else {
  731. if (is_mode || ms_mode || tns_mode || pred_mode) {
  732. for (i = 0; i < s->chan_map[0]; i++) {
  733. // Must restore coeffs
  734. chans = tag == TYPE_CPE ? 2 : 1;
  735. cpe = &s->cpe[i];
  736. for (ch = 0; ch < chans; ch++)
  737. memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
  738. }
  739. }
  740. its++;
  741. }
  742. } else {
  743. break;
  744. }
  745. } while (1);
  746. if (s->options.ltp && s->coder->ltp_insert_new_frame)
  747. s->coder->ltp_insert_new_frame(s);
  748. put_bits(&s->pb, 3, TYPE_END);
  749. flush_put_bits(&s->pb);
  750. s->last_frame_pb_count = put_bits_count(&s->pb);
  751. s->lambda_sum += s->lambda;
  752. s->lambda_count++;
  753. if (!frame)
  754. s->last_frame++;
  755. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  756. &avpkt->duration);
  757. avpkt->size = put_bits_count(&s->pb) >> 3;
  758. *got_packet_ptr = 1;
  759. return 0;
  760. }
  761. static av_cold int aac_encode_end(AVCodecContext *avctx)
  762. {
  763. AACEncContext *s = avctx->priv_data;
  764. av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_sum / s->lambda_count);
  765. ff_mdct_end(&s->mdct1024);
  766. ff_mdct_end(&s->mdct128);
  767. ff_psy_end(&s->psy);
  768. ff_lpc_end(&s->lpc);
  769. if (s->psypp)
  770. ff_psy_preprocess_end(s->psypp);
  771. av_freep(&s->buffer.samples);
  772. av_freep(&s->cpe);
  773. av_freep(&s->fdsp);
  774. ff_af_queue_close(&s->afq);
  775. return 0;
  776. }
  777. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  778. {
  779. int ret = 0;
  780. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  781. if (!s->fdsp)
  782. return AVERROR(ENOMEM);
  783. // window init
  784. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  785. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  786. ff_init_ff_sine_windows(10);
  787. ff_init_ff_sine_windows(7);
  788. if ((ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0)) < 0)
  789. return ret;
  790. if ((ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0)) < 0)
  791. return ret;
  792. return 0;
  793. }
  794. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  795. {
  796. int ch;
  797. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), alloc_fail);
  798. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->cpe, s->chan_map[0], sizeof(ChannelElement), alloc_fail);
  799. FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + AV_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
  800. for(ch = 0; ch < s->channels; ch++)
  801. s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
  802. return 0;
  803. alloc_fail:
  804. return AVERROR(ENOMEM);
  805. }
  806. static av_cold void aac_encode_init_tables(void)
  807. {
  808. ff_aac_tableinit();
  809. }
  810. static av_cold int aac_encode_init(AVCodecContext *avctx)
  811. {
  812. AACEncContext *s = avctx->priv_data;
  813. int i, ret = 0;
  814. const uint8_t *sizes[2];
  815. uint8_t grouping[AAC_MAX_CHANNELS];
  816. int lengths[2];
  817. s->channels = avctx->channels;
  818. s->chan_map = aac_chan_configs[s->channels-1];
  819. s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
  820. s->last_frame_pb_count = 0;
  821. avctx->extradata_size = 5;
  822. avctx->frame_size = 1024;
  823. avctx->initial_padding = 1024;
  824. avctx->bit_rate = (int)FFMIN(
  825. 6144 * s->channels / 1024.0 * avctx->sample_rate,
  826. avctx->bit_rate);
  827. avctx->profile = avctx->profile == FF_PROFILE_UNKNOWN ? FF_PROFILE_AAC_LOW :
  828. avctx->profile;
  829. for (i = 0; i < 16; i++)
  830. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
  831. break;
  832. s->samplerate_index = i;
  833. ERROR_IF(s->samplerate_index == 16 ||
  834. s->samplerate_index >= ff_aac_swb_size_1024_len ||
  835. s->samplerate_index >= ff_aac_swb_size_128_len,
  836. "Unsupported sample rate %d\n", avctx->sample_rate);
  837. ERROR_IF(s->channels > AAC_MAX_CHANNELS || s->channels == 7,
  838. "Unsupported number of channels: %d\n", s->channels);
  839. WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  840. "Too many bits %f > %d per frame requested, clamping to max\n",
  841. 1024.0 * avctx->bit_rate / avctx->sample_rate,
  842. 6144 * s->channels);
  843. for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
  844. if (avctx->profile == aacenc_profiles[i])
  845. break;
  846. ERROR_IF(i == FF_ARRAY_ELEMS(aacenc_profiles),
  847. "Unsupported encoding profile: %d\n", avctx->profile);
  848. if (avctx->profile == FF_PROFILE_MPEG2_AAC_LOW) {
  849. avctx->profile = FF_PROFILE_AAC_LOW;
  850. ERROR_IF(s->options.pred,
  851. "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  852. ERROR_IF(s->options.ltp,
  853. "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  854. WARN_IF(s->options.pns,
  855. "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
  856. s->options.pns = 0;
  857. } else if (avctx->profile == FF_PROFILE_AAC_LTP) {
  858. s->options.ltp = 1;
  859. ERROR_IF(s->options.pred,
  860. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  861. } else if (avctx->profile == FF_PROFILE_AAC_MAIN) {
  862. s->options.pred = 1;
  863. ERROR_IF(s->options.ltp,
  864. "LTP prediction unavailable in the \"aac_main\" profile\n");
  865. } else if (s->options.ltp) {
  866. avctx->profile = FF_PROFILE_AAC_LTP;
  867. WARN_IF(1,
  868. "Chainging profile to \"aac_ltp\"\n");
  869. ERROR_IF(s->options.pred,
  870. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  871. } else if (s->options.pred) {
  872. avctx->profile = FF_PROFILE_AAC_MAIN;
  873. WARN_IF(1,
  874. "Chainging profile to \"aac_main\"\n");
  875. ERROR_IF(s->options.ltp,
  876. "LTP prediction unavailable in the \"aac_main\" profile\n");
  877. }
  878. s->profile = avctx->profile;
  879. s->coder = &ff_aac_coders[s->options.coder];
  880. if (s->options.coder != AAC_CODER_TWOLOOP) {
  881. ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  882. "Coders other than twoloop require -strict -2 and some may be removed in the future\n");
  883. WARN_IF(s->options.coder == AAC_CODER_FAAC,
  884. "The FAAC-like coder will be removed in the near future, please use twoloop!\n");
  885. s->options.intensity_stereo = 0;
  886. s->options.pns = 0;
  887. }
  888. if ((ret = dsp_init(avctx, s)) < 0)
  889. goto fail;
  890. if ((ret = alloc_buffers(avctx, s)) < 0)
  891. goto fail;
  892. put_audio_specific_config(avctx);
  893. sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
  894. sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
  895. lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
  896. lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
  897. for (i = 0; i < s->chan_map[0]; i++)
  898. grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
  899. if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
  900. s->chan_map[0], grouping)) < 0)
  901. goto fail;
  902. s->psypp = ff_psy_preprocess_init(avctx);
  903. ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
  904. av_lfg_init(&s->lfg, 0x72adca55);
  905. if (HAVE_MIPSDSP)
  906. ff_aac_coder_init_mips(s);
  907. if ((ret = ff_thread_once(&aac_table_init, &aac_encode_init_tables)) != 0)
  908. return AVERROR_UNKNOWN;
  909. ff_af_queue_init(avctx, &s->afq);
  910. return 0;
  911. fail:
  912. aac_encode_end(avctx);
  913. return ret;
  914. }
  915. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  916. static const AVOption aacenc_options[] = {
  917. {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, -1, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
  918. {"faac", "FAAC-inspired method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAAC}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  919. {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  920. {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  921. {"fast", "Constant quantizer", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  922. {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
  923. {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  924. {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  925. {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  926. {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  927. {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  928. {NULL}
  929. };
  930. static const AVClass aacenc_class = {
  931. "AAC encoder",
  932. av_default_item_name,
  933. aacenc_options,
  934. LIBAVUTIL_VERSION_INT,
  935. };
  936. AVCodec ff_aac_encoder = {
  937. .name = "aac",
  938. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  939. .type = AVMEDIA_TYPE_AUDIO,
  940. .id = AV_CODEC_ID_AAC,
  941. .priv_data_size = sizeof(AACEncContext),
  942. .init = aac_encode_init,
  943. .encode2 = aac_encode_frame,
  944. .close = aac_encode_end,
  945. .supported_samplerates = mpeg4audio_sample_rates,
  946. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  947. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
  948. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  949. AV_SAMPLE_FMT_NONE },
  950. .priv_class = &aacenc_class,
  951. };