2
0

aacenc.c 43 KB

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