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.

1094 lines
41KB

  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/libm.h"
  30. #include "libavutil/thread.h"
  31. #include "libavutil/float_dsp.h"
  32. #include "libavutil/opt.h"
  33. #include "avcodec.h"
  34. #include "put_bits.h"
  35. #include "internal.h"
  36. #include "mpeg4audio.h"
  37. #include "kbdwin.h"
  38. #include "sinewin.h"
  39. #include "aac.h"
  40. #include "aactab.h"
  41. #include "aacenc.h"
  42. #include "aacenctab.h"
  43. #include "aacenc_utils.h"
  44. #include "psymodel.h"
  45. static AVOnce aac_table_init = AV_ONCE_INIT;
  46. /**
  47. * Make AAC audio config object.
  48. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  49. */
  50. static void put_audio_specific_config(AVCodecContext *avctx)
  51. {
  52. PutBitContext pb;
  53. AACEncContext *s = avctx->priv_data;
  54. int channels = s->channels - (s->channels == 8 ? 1 : 0);
  55. init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
  56. put_bits(&pb, 5, s->profile+1); //profile
  57. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  58. put_bits(&pb, 4, channels);
  59. //GASpecificConfig
  60. put_bits(&pb, 1, 0); //frame length - 1024 samples
  61. put_bits(&pb, 1, 0); //does not depend on core coder
  62. put_bits(&pb, 1, 0); //is not extension
  63. //Explicitly Mark SBR absent
  64. put_bits(&pb, 11, 0x2b7); //sync extension
  65. put_bits(&pb, 5, AOT_SBR);
  66. put_bits(&pb, 1, 0);
  67. flush_put_bits(&pb);
  68. }
  69. void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
  70. {
  71. ++s->quantize_band_cost_cache_generation;
  72. if (s->quantize_band_cost_cache_generation == 0) {
  73. memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));
  74. s->quantize_band_cost_cache_generation = 1;
  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. const 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. int k;
  490. float clip_avoidance_factor;
  491. sce = &cpe->ch[ch];
  492. ics = &sce->ics;
  493. s->cur_channel = start_ch + ch;
  494. overlap = &samples[s->cur_channel][0];
  495. samples2 = overlap + 1024;
  496. la = samples2 + (448+64);
  497. if (!frame)
  498. la = NULL;
  499. if (tag == TYPE_LFE) {
  500. wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE;
  501. wi[ch].window_shape = 0;
  502. wi[ch].num_windows = 1;
  503. wi[ch].grouping[0] = 1;
  504. wi[ch].clipping[0] = 0;
  505. /* Only the lowest 12 coefficients are used in a LFE channel.
  506. * The expression below results in only the bottom 8 coefficients
  507. * being used for 11.025kHz to 16kHz sample rates.
  508. */
  509. ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
  510. } else {
  511. wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel,
  512. ics->window_sequence[0]);
  513. }
  514. ics->window_sequence[1] = ics->window_sequence[0];
  515. ics->window_sequence[0] = wi[ch].window_type[0];
  516. ics->use_kb_window[1] = ics->use_kb_window[0];
  517. ics->use_kb_window[0] = wi[ch].window_shape;
  518. ics->num_windows = wi[ch].num_windows;
  519. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  520. ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
  521. ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb);
  522. ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  523. ff_swb_offset_128 [s->samplerate_index]:
  524. ff_swb_offset_1024[s->samplerate_index];
  525. ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
  526. ff_tns_max_bands_128 [s->samplerate_index]:
  527. ff_tns_max_bands_1024[s->samplerate_index];
  528. for (w = 0; w < ics->num_windows; w++)
  529. ics->group_len[w] = wi[ch].grouping[w];
  530. /* Calculate input sample maximums and evaluate clipping risk */
  531. clip_avoidance_factor = 0.0f;
  532. for (w = 0; w < ics->num_windows; w++) {
  533. const float *wbuf = overlap + w * 128;
  534. const int wlen = 2048 / ics->num_windows;
  535. float max = 0;
  536. int j;
  537. /* mdct input is 2 * output */
  538. for (j = 0; j < wlen; j++)
  539. max = FFMAX(max, fabsf(wbuf[j]));
  540. wi[ch].clipping[w] = max;
  541. }
  542. for (w = 0; w < ics->num_windows; w++) {
  543. if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
  544. ics->window_clipping[w] = 1;
  545. clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
  546. } else {
  547. ics->window_clipping[w] = 0;
  548. }
  549. }
  550. if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
  551. ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
  552. } else {
  553. ics->clip_avoidance_factor = 1.0f;
  554. }
  555. apply_window_and_mdct(s, sce, overlap);
  556. if (s->options.ltp && s->coder->update_ltp) {
  557. s->coder->update_ltp(s, sce);
  558. apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]);
  559. s->mdct1024.mdct_calc(&s->mdct1024, sce->lcoeffs, sce->ret_buf);
  560. }
  561. for (k = 0; k < 1024; k++) {
  562. if (!isfinite(cpe->ch[ch].coeffs[k])) {
  563. av_log(avctx, AV_LOG_ERROR, "Input contains NaN/+-Inf\n");
  564. return AVERROR(EINVAL);
  565. }
  566. }
  567. avoid_clipping(s, sce);
  568. }
  569. start_ch += chans;
  570. }
  571. if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels, 0)) < 0)
  572. return ret;
  573. frame_bits = its = 0;
  574. do {
  575. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  576. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  577. put_bitstream_info(s, LIBAVCODEC_IDENT);
  578. start_ch = 0;
  579. target_bits = 0;
  580. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  581. for (i = 0; i < s->chan_map[0]; i++) {
  582. FFPsyWindowInfo* wi = windows + start_ch;
  583. const float *coeffs[2];
  584. tag = s->chan_map[i+1];
  585. chans = tag == TYPE_CPE ? 2 : 1;
  586. cpe = &s->cpe[i];
  587. cpe->common_window = 0;
  588. memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
  589. memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
  590. put_bits(&s->pb, 3, tag);
  591. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  592. for (ch = 0; ch < chans; ch++) {
  593. sce = &cpe->ch[ch];
  594. coeffs[ch] = sce->coeffs;
  595. sce->ics.predictor_present = 0;
  596. sce->ics.ltp.present = 0;
  597. memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
  598. memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
  599. memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
  600. for (w = 0; w < 128; w++)
  601. if (sce->band_type[w] > RESERVED_BT)
  602. sce->band_type[w] = 0;
  603. }
  604. s->psy.bitres.alloc = -1;
  605. s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
  606. s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
  607. if (s->psy.bitres.alloc > 0) {
  608. /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
  609. target_bits += s->psy.bitres.alloc
  610. * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
  611. s->psy.bitres.alloc /= chans;
  612. }
  613. s->cur_type = tag;
  614. for (ch = 0; ch < chans; ch++) {
  615. s->cur_channel = start_ch + ch;
  616. if (s->options.pns && s->coder->mark_pns)
  617. s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
  618. s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
  619. }
  620. if (chans > 1
  621. && wi[0].window_type[0] == wi[1].window_type[0]
  622. && wi[0].window_shape == wi[1].window_shape) {
  623. cpe->common_window = 1;
  624. for (w = 0; w < wi[0].num_windows; w++) {
  625. if (wi[0].grouping[w] != wi[1].grouping[w]) {
  626. cpe->common_window = 0;
  627. break;
  628. }
  629. }
  630. }
  631. for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
  632. sce = &cpe->ch[ch];
  633. s->cur_channel = start_ch + ch;
  634. if (s->options.tns && s->coder->search_for_tns)
  635. s->coder->search_for_tns(s, sce);
  636. if (s->options.tns && s->coder->apply_tns_filt)
  637. s->coder->apply_tns_filt(s, sce);
  638. if (sce->tns.present)
  639. tns_mode = 1;
  640. if (s->options.pns && s->coder->search_for_pns)
  641. s->coder->search_for_pns(s, avctx, sce);
  642. }
  643. s->cur_channel = start_ch;
  644. if (s->options.intensity_stereo) { /* Intensity Stereo */
  645. if (s->coder->search_for_is)
  646. s->coder->search_for_is(s, avctx, cpe);
  647. if (cpe->is_mode) is_mode = 1;
  648. apply_intensity_stereo(cpe);
  649. }
  650. if (s->options.pred) { /* Prediction */
  651. for (ch = 0; ch < chans; ch++) {
  652. sce = &cpe->ch[ch];
  653. s->cur_channel = start_ch + ch;
  654. if (s->options.pred && s->coder->search_for_pred)
  655. s->coder->search_for_pred(s, sce);
  656. if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
  657. }
  658. if (s->coder->adjust_common_pred)
  659. s->coder->adjust_common_pred(s, cpe);
  660. for (ch = 0; ch < chans; ch++) {
  661. sce = &cpe->ch[ch];
  662. s->cur_channel = start_ch + ch;
  663. if (s->options.pred && s->coder->apply_main_pred)
  664. s->coder->apply_main_pred(s, sce);
  665. }
  666. s->cur_channel = start_ch;
  667. }
  668. if (s->options.mid_side) { /* Mid/Side stereo */
  669. if (s->options.mid_side == -1 && s->coder->search_for_ms)
  670. s->coder->search_for_ms(s, cpe);
  671. else if (cpe->common_window)
  672. memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
  673. apply_mid_side_stereo(cpe);
  674. }
  675. adjust_frame_information(cpe, chans);
  676. if (s->options.ltp) { /* LTP */
  677. for (ch = 0; ch < chans; ch++) {
  678. sce = &cpe->ch[ch];
  679. s->cur_channel = start_ch + ch;
  680. if (s->coder->search_for_ltp)
  681. s->coder->search_for_ltp(s, sce, cpe->common_window);
  682. if (sce->ics.ltp.present) pred_mode = 1;
  683. }
  684. s->cur_channel = start_ch;
  685. if (s->coder->adjust_common_ltp)
  686. s->coder->adjust_common_ltp(s, cpe);
  687. }
  688. if (chans == 2) {
  689. put_bits(&s->pb, 1, cpe->common_window);
  690. if (cpe->common_window) {
  691. put_ics_info(s, &cpe->ch[0].ics);
  692. if (s->coder->encode_main_pred)
  693. s->coder->encode_main_pred(s, &cpe->ch[0]);
  694. if (s->coder->encode_ltp_info)
  695. s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
  696. encode_ms_info(&s->pb, cpe);
  697. if (cpe->ms_mode) ms_mode = 1;
  698. }
  699. }
  700. for (ch = 0; ch < chans; ch++) {
  701. s->cur_channel = start_ch + ch;
  702. encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
  703. }
  704. start_ch += chans;
  705. }
  706. if (avctx->flags & CODEC_FLAG_QSCALE) {
  707. /* When using a constant Q-scale, don't mess with lambda */
  708. break;
  709. }
  710. /* rate control stuff
  711. * allow between the nominal bitrate, and what psy's bit reservoir says to target
  712. * but drift towards the nominal bitrate always
  713. */
  714. frame_bits = put_bits_count(&s->pb);
  715. rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
  716. rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
  717. too_many_bits = FFMAX(target_bits, rate_bits);
  718. too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
  719. too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
  720. /* When using ABR, be strict (but only for increasing) */
  721. too_few_bits = too_few_bits - too_few_bits/8;
  722. too_many_bits = too_many_bits + too_many_bits/2;
  723. if ( its == 0 /* for steady-state Q-scale tracking */
  724. || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
  725. || frame_bits >= 6144 * s->channels - 3 )
  726. {
  727. float ratio = ((float)rate_bits) / frame_bits;
  728. if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
  729. /*
  730. * This path is for steady-state Q-scale tracking
  731. * When frame bits fall within the stable range, we still need to adjust
  732. * lambda to maintain it like so in a stable fashion (large jumps in lambda
  733. * create artifacts and should be avoided), but slowly
  734. */
  735. ratio = sqrtf(sqrtf(ratio));
  736. ratio = av_clipf(ratio, 0.9f, 1.1f);
  737. } else {
  738. /* Not so fast though */
  739. ratio = sqrtf(ratio);
  740. }
  741. s->lambda = FFMIN(s->lambda * ratio, 65536.f);
  742. /* Keep iterating if we must reduce and lambda is in the sky */
  743. if (ratio > 0.9f && ratio < 1.1f) {
  744. break;
  745. } else {
  746. if (is_mode || ms_mode || tns_mode || pred_mode) {
  747. for (i = 0; i < s->chan_map[0]; i++) {
  748. // Must restore coeffs
  749. chans = tag == TYPE_CPE ? 2 : 1;
  750. cpe = &s->cpe[i];
  751. for (ch = 0; ch < chans; ch++)
  752. memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
  753. }
  754. }
  755. its++;
  756. }
  757. } else {
  758. break;
  759. }
  760. } while (1);
  761. if (s->options.ltp && s->coder->ltp_insert_new_frame)
  762. s->coder->ltp_insert_new_frame(s);
  763. put_bits(&s->pb, 3, TYPE_END);
  764. flush_put_bits(&s->pb);
  765. s->last_frame_pb_count = put_bits_count(&s->pb);
  766. s->lambda_sum += s->lambda;
  767. s->lambda_count++;
  768. if (!frame)
  769. s->last_frame++;
  770. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  771. &avpkt->duration);
  772. avpkt->size = put_bits_count(&s->pb) >> 3;
  773. *got_packet_ptr = 1;
  774. return 0;
  775. }
  776. static av_cold int aac_encode_end(AVCodecContext *avctx)
  777. {
  778. AACEncContext *s = avctx->priv_data;
  779. av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_sum / s->lambda_count);
  780. ff_mdct_end(&s->mdct1024);
  781. ff_mdct_end(&s->mdct128);
  782. ff_psy_end(&s->psy);
  783. ff_lpc_end(&s->lpc);
  784. if (s->psypp)
  785. ff_psy_preprocess_end(s->psypp);
  786. av_freep(&s->buffer.samples);
  787. av_freep(&s->cpe);
  788. av_freep(&s->fdsp);
  789. ff_af_queue_close(&s->afq);
  790. return 0;
  791. }
  792. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  793. {
  794. int ret = 0;
  795. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  796. if (!s->fdsp)
  797. return AVERROR(ENOMEM);
  798. // window init
  799. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  800. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  801. ff_init_ff_sine_windows(10);
  802. ff_init_ff_sine_windows(7);
  803. if ((ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0)) < 0)
  804. return ret;
  805. if ((ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0)) < 0)
  806. return ret;
  807. return 0;
  808. }
  809. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  810. {
  811. int ch;
  812. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), alloc_fail);
  813. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->cpe, s->chan_map[0], sizeof(ChannelElement), alloc_fail);
  814. FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + AV_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
  815. for(ch = 0; ch < s->channels; ch++)
  816. s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
  817. return 0;
  818. alloc_fail:
  819. return AVERROR(ENOMEM);
  820. }
  821. static av_cold void aac_encode_init_tables(void)
  822. {
  823. ff_aac_tableinit();
  824. }
  825. static av_cold int aac_encode_init(AVCodecContext *avctx)
  826. {
  827. AACEncContext *s = avctx->priv_data;
  828. int i, ret = 0;
  829. const uint8_t *sizes[2];
  830. uint8_t grouping[AAC_MAX_CHANNELS];
  831. int lengths[2];
  832. /* Constants */
  833. s->last_frame_pb_count = 0;
  834. avctx->extradata_size = 5;
  835. avctx->frame_size = 1024;
  836. avctx->initial_padding = 1024;
  837. s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
  838. /* Channel map and unspecified bitrate guessing */
  839. s->channels = avctx->channels;
  840. ERROR_IF(s->channels > AAC_MAX_CHANNELS || s->channels == 7,
  841. "Unsupported number of channels: %d\n", s->channels);
  842. s->chan_map = aac_chan_configs[s->channels-1];
  843. if (!avctx->bit_rate) {
  844. for (i = 1; i <= s->chan_map[0]; i++) {
  845. avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
  846. s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */
  847. 69000 ; /* SCE */
  848. }
  849. }
  850. /* Samplerate */
  851. for (i = 0; i < 16; i++)
  852. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
  853. break;
  854. s->samplerate_index = i;
  855. ERROR_IF(s->samplerate_index == 16 ||
  856. s->samplerate_index >= ff_aac_swb_size_1024_len ||
  857. s->samplerate_index >= ff_aac_swb_size_128_len,
  858. "Unsupported sample rate %d\n", avctx->sample_rate);
  859. /* Bitrate limiting */
  860. WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  861. "Too many bits %f > %d per frame requested, clamping to max\n",
  862. 1024.0 * avctx->bit_rate / avctx->sample_rate,
  863. 6144 * s->channels);
  864. avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
  865. avctx->bit_rate);
  866. /* Profile and option setting */
  867. avctx->profile = avctx->profile == FF_PROFILE_UNKNOWN ? FF_PROFILE_AAC_LOW :
  868. avctx->profile;
  869. for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
  870. if (avctx->profile == aacenc_profiles[i])
  871. break;
  872. if (avctx->profile == FF_PROFILE_MPEG2_AAC_LOW) {
  873. avctx->profile = FF_PROFILE_AAC_LOW;
  874. ERROR_IF(s->options.pred,
  875. "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  876. ERROR_IF(s->options.ltp,
  877. "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
  878. WARN_IF(s->options.pns,
  879. "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
  880. s->options.pns = 0;
  881. } else if (avctx->profile == FF_PROFILE_AAC_LTP) {
  882. s->options.ltp = 1;
  883. ERROR_IF(s->options.pred,
  884. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  885. } else if (avctx->profile == FF_PROFILE_AAC_MAIN) {
  886. s->options.pred = 1;
  887. ERROR_IF(s->options.ltp,
  888. "LTP prediction unavailable in the \"aac_main\" profile\n");
  889. } else if (s->options.ltp) {
  890. avctx->profile = FF_PROFILE_AAC_LTP;
  891. WARN_IF(1,
  892. "Chainging profile to \"aac_ltp\"\n");
  893. ERROR_IF(s->options.pred,
  894. "Main prediction unavailable in the \"aac_ltp\" profile\n");
  895. } else if (s->options.pred) {
  896. avctx->profile = FF_PROFILE_AAC_MAIN;
  897. WARN_IF(1,
  898. "Chainging profile to \"aac_main\"\n");
  899. ERROR_IF(s->options.ltp,
  900. "LTP prediction unavailable in the \"aac_main\" profile\n");
  901. }
  902. s->profile = avctx->profile;
  903. /* Coder limitations */
  904. s->coder = &ff_aac_coders[s->options.coder];
  905. if (s->options.coder == AAC_CODER_ANMR) {
  906. ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  907. "The ANMR coder requires -strict -2 and some may be removed in the future\n");
  908. s->options.intensity_stereo = 0;
  909. s->options.pns = 0;
  910. }
  911. ERROR_IF(s->options.ltp && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
  912. "The LPT profile requires experimental compliance, add -strict -2 to enable!\n");
  913. /* M/S introduces horrible artifacts with multichannel files, this is temporary */
  914. if (s->channels > 3)
  915. s->options.mid_side = 0;
  916. if ((ret = dsp_init(avctx, s)) < 0)
  917. goto fail;
  918. if ((ret = alloc_buffers(avctx, s)) < 0)
  919. goto fail;
  920. put_audio_specific_config(avctx);
  921. sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
  922. sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
  923. lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
  924. lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
  925. for (i = 0; i < s->chan_map[0]; i++)
  926. grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
  927. if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
  928. s->chan_map[0], grouping)) < 0)
  929. goto fail;
  930. s->psypp = ff_psy_preprocess_init(avctx);
  931. ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
  932. av_lfg_init(&s->lfg, 0x72adca55);
  933. if (HAVE_MIPSDSP)
  934. ff_aac_coder_init_mips(s);
  935. if ((ret = ff_thread_once(&aac_table_init, &aac_encode_init_tables)) != 0)
  936. return AVERROR_UNKNOWN;
  937. ff_af_queue_init(avctx, &s->afq);
  938. return 0;
  939. fail:
  940. aac_encode_end(avctx);
  941. return ret;
  942. }
  943. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  944. static const AVOption aacenc_options[] = {
  945. {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
  946. {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  947. {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  948. {"fast", "Constant quantizer", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
  949. {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
  950. {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  951. {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  952. {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
  953. {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  954. {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
  955. {NULL}
  956. };
  957. static const AVClass aacenc_class = {
  958. "AAC encoder",
  959. av_default_item_name,
  960. aacenc_options,
  961. LIBAVUTIL_VERSION_INT,
  962. };
  963. static const AVCodecDefault aac_encode_defaults[] = {
  964. { "b", "0" },
  965. { NULL }
  966. };
  967. AVCodec ff_aac_encoder = {
  968. .name = "aac",
  969. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  970. .type = AVMEDIA_TYPE_AUDIO,
  971. .id = AV_CODEC_ID_AAC,
  972. .priv_data_size = sizeof(AACEncContext),
  973. .init = aac_encode_init,
  974. .encode2 = aac_encode_frame,
  975. .close = aac_encode_end,
  976. .defaults = aac_encode_defaults,
  977. .supported_samplerates = mpeg4audio_sample_rates,
  978. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  979. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
  980. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  981. AV_SAMPLE_FMT_NONE },
  982. .priv_class = &aacenc_class,
  983. };