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.

1065 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. isnan(cpe->ch->coeffs[ 128]) ||
  550. isnan(cpe->ch->coeffs[2*128]) ||
  551. isnan(cpe->ch->coeffs[3*128]) ||
  552. isnan(cpe->ch->coeffs[4*128]) ||
  553. isnan(cpe->ch->coeffs[5*128]) ||
  554. isnan(cpe->ch->coeffs[6*128]) ||
  555. isnan(cpe->ch->coeffs[7*128])
  556. ) {
  557. av_log(avctx, AV_LOG_ERROR, "Input contains NaN\n");
  558. return AVERROR(EINVAL);
  559. }
  560. avoid_clipping(s, sce);
  561. }
  562. start_ch += chans;
  563. }
  564. if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels, 0)) < 0)
  565. return ret;
  566. frame_bits = its = 0;
  567. do {
  568. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  569. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  570. put_bitstream_info(s, LIBAVCODEC_IDENT);
  571. start_ch = 0;
  572. target_bits = 0;
  573. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  574. for (i = 0; i < s->chan_map[0]; i++) {
  575. FFPsyWindowInfo* wi = windows + start_ch;
  576. const float *coeffs[2];
  577. tag = s->chan_map[i+1];
  578. chans = tag == TYPE_CPE ? 2 : 1;
  579. cpe = &s->cpe[i];
  580. cpe->common_window = 0;
  581. memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
  582. memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
  583. put_bits(&s->pb, 3, tag);
  584. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  585. for (ch = 0; ch < chans; ch++) {
  586. sce = &cpe->ch[ch];
  587. coeffs[ch] = sce->coeffs;
  588. sce->ics.predictor_present = 0;
  589. sce->ics.ltp.present = 0;
  590. memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
  591. memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
  592. memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
  593. for (w = 0; w < 128; w++)
  594. if (sce->band_type[w] > RESERVED_BT)
  595. sce->band_type[w] = 0;
  596. }
  597. s->psy.bitres.alloc = -1;
  598. s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
  599. s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
  600. if (s->psy.bitres.alloc > 0) {
  601. /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
  602. target_bits += s->psy.bitres.alloc
  603. * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
  604. s->psy.bitres.alloc /= chans;
  605. }
  606. s->cur_type = tag;
  607. for (ch = 0; ch < chans; ch++) {
  608. s->cur_channel = start_ch + ch;
  609. if (s->options.pns && s->coder->mark_pns)
  610. s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
  611. s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
  612. }
  613. if (chans > 1
  614. && wi[0].window_type[0] == wi[1].window_type[0]
  615. && wi[0].window_shape == wi[1].window_shape) {
  616. cpe->common_window = 1;
  617. for (w = 0; w < wi[0].num_windows; w++) {
  618. if (wi[0].grouping[w] != wi[1].grouping[w]) {
  619. cpe->common_window = 0;
  620. break;
  621. }
  622. }
  623. }
  624. for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
  625. sce = &cpe->ch[ch];
  626. s->cur_channel = start_ch + ch;
  627. if (s->options.tns && s->coder->search_for_tns)
  628. s->coder->search_for_tns(s, sce);
  629. if (s->options.tns && s->coder->apply_tns_filt)
  630. s->coder->apply_tns_filt(s, sce);
  631. if (sce->tns.present)
  632. tns_mode = 1;
  633. if (s->options.pns && s->coder->search_for_pns)
  634. s->coder->search_for_pns(s, avctx, sce);
  635. }
  636. s->cur_channel = start_ch;
  637. if (s->options.intensity_stereo) { /* Intensity Stereo */
  638. if (s->coder->search_for_is)
  639. s->coder->search_for_is(s, avctx, cpe);
  640. if (cpe->is_mode) is_mode = 1;
  641. apply_intensity_stereo(cpe);
  642. }
  643. if (s->options.pred) { /* Prediction */
  644. for (ch = 0; ch < chans; ch++) {
  645. sce = &cpe->ch[ch];
  646. s->cur_channel = start_ch + ch;
  647. if (s->options.pred && s->coder->search_for_pred)
  648. s->coder->search_for_pred(s, sce);
  649. if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
  650. }
  651. if (s->coder->adjust_common_pred)
  652. s->coder->adjust_common_pred(s, cpe);
  653. for (ch = 0; ch < chans; ch++) {
  654. sce = &cpe->ch[ch];
  655. s->cur_channel = start_ch + ch;
  656. if (s->options.pred && s->coder->apply_main_pred)
  657. s->coder->apply_main_pred(s, sce);
  658. }
  659. s->cur_channel = start_ch;
  660. }
  661. if (s->options.mid_side) { /* Mid/Side stereo */
  662. if (s->options.mid_side == -1 && s->coder->search_for_ms)
  663. s->coder->search_for_ms(s, cpe);
  664. else if (cpe->common_window)
  665. memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
  666. apply_mid_side_stereo(cpe);
  667. }
  668. adjust_frame_information(cpe, chans);
  669. if (s->options.ltp) { /* LTP */
  670. for (ch = 0; ch < chans; ch++) {
  671. sce = &cpe->ch[ch];
  672. s->cur_channel = start_ch + ch;
  673. if (s->coder->search_for_ltp)
  674. s->coder->search_for_ltp(s, sce, cpe->common_window);
  675. if (sce->ics.ltp.present) pred_mode = 1;
  676. }
  677. s->cur_channel = start_ch;
  678. if (s->coder->adjust_common_ltp)
  679. s->coder->adjust_common_ltp(s, cpe);
  680. }
  681. if (chans == 2) {
  682. put_bits(&s->pb, 1, cpe->common_window);
  683. if (cpe->common_window) {
  684. put_ics_info(s, &cpe->ch[0].ics);
  685. if (s->coder->encode_main_pred)
  686. s->coder->encode_main_pred(s, &cpe->ch[0]);
  687. if (s->coder->encode_ltp_info)
  688. s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
  689. encode_ms_info(&s->pb, cpe);
  690. if (cpe->ms_mode) ms_mode = 1;
  691. }
  692. }
  693. for (ch = 0; ch < chans; ch++) {
  694. s->cur_channel = start_ch + ch;
  695. encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
  696. }
  697. start_ch += chans;
  698. }
  699. if (avctx->flags & CODEC_FLAG_QSCALE) {
  700. /* When using a constant Q-scale, don't mess with lambda */
  701. break;
  702. }
  703. /* rate control stuff
  704. * allow between the nominal bitrate, and what psy's bit reservoir says to target
  705. * but drift towards the nominal bitrate always
  706. */
  707. frame_bits = put_bits_count(&s->pb);
  708. rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
  709. rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
  710. too_many_bits = FFMAX(target_bits, rate_bits);
  711. too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
  712. too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
  713. /* When using ABR, be strict (but only for increasing) */
  714. too_few_bits = too_few_bits - too_few_bits/8;
  715. too_many_bits = too_many_bits + too_many_bits/2;
  716. if ( its == 0 /* for steady-state Q-scale tracking */
  717. || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
  718. || frame_bits >= 6144 * s->channels - 3 )
  719. {
  720. float ratio = ((float)rate_bits) / frame_bits;
  721. if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
  722. /*
  723. * This path is for steady-state Q-scale tracking
  724. * When frame bits fall within the stable range, we still need to adjust
  725. * lambda to maintain it like so in a stable fashion (large jumps in lambda
  726. * create artifacts and should be avoided), but slowly
  727. */
  728. ratio = sqrtf(sqrtf(ratio));
  729. ratio = av_clipf(ratio, 0.9f, 1.1f);
  730. } else {
  731. /* Not so fast though */
  732. ratio = sqrtf(ratio);
  733. }
  734. s->lambda = FFMIN(s->lambda * ratio, 65536.f);
  735. /* Keep iterating if we must reduce and lambda is in the sky */
  736. if (ratio > 0.9f && ratio < 1.1f) {
  737. break;
  738. } else {
  739. if (is_mode || ms_mode || tns_mode || pred_mode) {
  740. for (i = 0; i < s->chan_map[0]; i++) {
  741. // Must restore coeffs
  742. chans = tag == TYPE_CPE ? 2 : 1;
  743. cpe = &s->cpe[i];
  744. for (ch = 0; ch < chans; ch++)
  745. memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
  746. }
  747. }
  748. its++;
  749. }
  750. } else {
  751. break;
  752. }
  753. } while (1);
  754. if (s->options.ltp && s->coder->ltp_insert_new_frame)
  755. s->coder->ltp_insert_new_frame(s);
  756. put_bits(&s->pb, 3, TYPE_END);
  757. flush_put_bits(&s->pb);
  758. s->last_frame_pb_count = put_bits_count(&s->pb);
  759. s->lambda_sum += s->lambda;
  760. s->lambda_count++;
  761. if (!frame)
  762. s->last_frame++;
  763. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  764. &avpkt->duration);
  765. avpkt->size = put_bits_count(&s->pb) >> 3;
  766. *got_packet_ptr = 1;
  767. return 0;
  768. }
  769. static av_cold int aac_encode_end(AVCodecContext *avctx)
  770. {
  771. AACEncContext *s = avctx->priv_data;
  772. av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_sum / s->lambda_count);
  773. ff_mdct_end(&s->mdct1024);
  774. ff_mdct_end(&s->mdct128);
  775. ff_psy_end(&s->psy);
  776. ff_lpc_end(&s->lpc);
  777. if (s->psypp)
  778. ff_psy_preprocess_end(s->psypp);
  779. av_freep(&s->buffer.samples);
  780. av_freep(&s->cpe);
  781. av_freep(&s->fdsp);
  782. ff_af_queue_close(&s->afq);
  783. return 0;
  784. }
  785. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  786. {
  787. int ret = 0;
  788. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  789. if (!s->fdsp)
  790. return AVERROR(ENOMEM);
  791. // window init
  792. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  793. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  794. ff_init_ff_sine_windows(10);
  795. ff_init_ff_sine_windows(7);
  796. if ((ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0)) < 0)
  797. return ret;
  798. if ((ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0)) < 0)
  799. return ret;
  800. return 0;
  801. }
  802. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  803. {
  804. int ch;
  805. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), alloc_fail);
  806. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->cpe, s->chan_map[0], sizeof(ChannelElement), alloc_fail);
  807. FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + AV_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
  808. for(ch = 0; ch < s->channels; ch++)
  809. s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
  810. return 0;
  811. alloc_fail:
  812. return AVERROR(ENOMEM);
  813. }
  814. static av_cold void aac_encode_init_tables(void)
  815. {
  816. ff_aac_tableinit();
  817. }
  818. static av_cold int aac_encode_init(AVCodecContext *avctx)
  819. {
  820. AACEncContext *s = avctx->priv_data;
  821. int i, ret = 0;
  822. const uint8_t *sizes[2];
  823. uint8_t grouping[AAC_MAX_CHANNELS];
  824. int lengths[2];
  825. s->channels = avctx->channels;
  826. s->chan_map = aac_chan_configs[s->channels-1];
  827. s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
  828. s->last_frame_pb_count = 0;
  829. avctx->extradata_size = 5;
  830. avctx->frame_size = 1024;
  831. avctx->initial_padding = 1024;
  832. avctx->bit_rate = (int)FFMIN(
  833. 6144 * s->channels / 1024.0 * avctx->sample_rate,
  834. avctx->bit_rate);
  835. avctx->profile = avctx->profile == FF_PROFILE_UNKNOWN ? FF_PROFILE_AAC_LOW :
  836. avctx->profile;
  837. for (i = 0; i < 16; i++)
  838. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
  839. break;
  840. s->samplerate_index = i;
  841. ERROR_IF(s->samplerate_index == 16 ||
  842. s->samplerate_index >= ff_aac_swb_size_1024_len ||
  843. s->samplerate_index >= ff_aac_swb_size_128_len,
  844. "Unsupported sample rate %d\n", avctx->sample_rate);
  845. ERROR_IF(s->channels > AAC_MAX_CHANNELS || s->channels == 7,
  846. "Unsupported number of channels: %d\n", s->channels);
  847. WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  848. "Too many bits %f > %d per frame requested, clamping to max\n",
  849. 1024.0 * avctx->bit_rate / avctx->sample_rate,
  850. 6144 * s->channels);
  851. for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
  852. if (avctx->profile == aacenc_profiles[i])
  853. break;
  854. ERROR_IF(i == FF_ARRAY_ELEMS(aacenc_profiles),
  855. "Unsupported encoding profile: %d\n", avctx->profile);
  856. if (avctx->profile == FF_PROFILE_MPEG2_AAC_LOW) {
  857. avctx->profile = FF_PROFILE_AAC_LOW;
  858. ERROR_IF(s->options.pred,
  859. "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  860. ERROR_IF(s->options.ltp,
  861. "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  862. WARN_IF(s->options.pns,
  863. "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
  864. s->options.pns = 0;
  865. } else if (avctx->profile == FF_PROFILE_AAC_LTP) {
  866. s->options.ltp = 1;
  867. ERROR_IF(s->options.pred,
  868. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  869. } else if (avctx->profile == FF_PROFILE_AAC_MAIN) {
  870. s->options.pred = 1;
  871. ERROR_IF(s->options.ltp,
  872. "LTP prediction unavailable in the \"aac_main\" profile\n");
  873. } else if (s->options.ltp) {
  874. avctx->profile = FF_PROFILE_AAC_LTP;
  875. WARN_IF(1,
  876. "Chainging profile to \"aac_ltp\"\n");
  877. ERROR_IF(s->options.pred,
  878. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  879. } else if (s->options.pred) {
  880. avctx->profile = FF_PROFILE_AAC_MAIN;
  881. WARN_IF(1,
  882. "Chainging profile to \"aac_main\"\n");
  883. ERROR_IF(s->options.ltp,
  884. "LTP prediction unavailable in the \"aac_main\" profile\n");
  885. }
  886. s->profile = avctx->profile;
  887. s->coder = &ff_aac_coders[s->options.coder];
  888. if (s->options.coder != AAC_CODER_TWOLOOP) {
  889. ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  890. "Coders other than twoloop require -strict -2 and some may be removed in the future\n");
  891. WARN_IF(s->options.coder == AAC_CODER_FAAC,
  892. "The FAAC-like coder will be removed in the near future, please use twoloop!\n");
  893. s->options.intensity_stereo = 0;
  894. s->options.pns = 0;
  895. }
  896. if ((ret = dsp_init(avctx, s)) < 0)
  897. goto fail;
  898. if ((ret = alloc_buffers(avctx, s)) < 0)
  899. goto fail;
  900. put_audio_specific_config(avctx);
  901. sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
  902. sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
  903. lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
  904. lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
  905. for (i = 0; i < s->chan_map[0]; i++)
  906. grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
  907. if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
  908. s->chan_map[0], grouping)) < 0)
  909. goto fail;
  910. s->psypp = ff_psy_preprocess_init(avctx);
  911. ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
  912. av_lfg_init(&s->lfg, 0x72adca55);
  913. if (HAVE_MIPSDSP)
  914. ff_aac_coder_init_mips(s);
  915. if ((ret = ff_thread_once(&aac_table_init, &aac_encode_init_tables)) != 0)
  916. return AVERROR_UNKNOWN;
  917. ff_af_queue_init(avctx, &s->afq);
  918. return 0;
  919. fail:
  920. aac_encode_end(avctx);
  921. return ret;
  922. }
  923. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  924. static const AVOption aacenc_options[] = {
  925. {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, -1, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
  926. {"faac", "FAAC-inspired method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAAC}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  927. {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  928. {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  929. {"fast", "Constant quantizer", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  930. {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
  931. {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  932. {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  933. {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  934. {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  935. {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  936. {NULL}
  937. };
  938. static const AVClass aacenc_class = {
  939. "AAC encoder",
  940. av_default_item_name,
  941. aacenc_options,
  942. LIBAVUTIL_VERSION_INT,
  943. };
  944. AVCodec ff_aac_encoder = {
  945. .name = "aac",
  946. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  947. .type = AVMEDIA_TYPE_AUDIO,
  948. .id = AV_CODEC_ID_AAC,
  949. .priv_data_size = sizeof(AACEncContext),
  950. .init = aac_encode_init,
  951. .encode2 = aac_encode_frame,
  952. .close = aac_encode_end,
  953. .supported_samplerates = mpeg4audio_sample_rates,
  954. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  955. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
  956. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  957. AV_SAMPLE_FMT_NONE },
  958. .priv_class = &aacenc_class,
  959. };