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.

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