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.

1154 lines
43KB

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