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.

736 lines
25KB

  1. /*
  2. * AAC encoder
  3. * Copyright (C) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * add temporal noise shaping
  29. ***********************************/
  30. #include "libavutil/opt.h"
  31. #include "avcodec.h"
  32. #include "put_bits.h"
  33. #include "dsputil.h"
  34. #include "mpeg4audio.h"
  35. #include "kbdwin.h"
  36. #include "sinewin.h"
  37. #include "aac.h"
  38. #include "aactab.h"
  39. #include "aacenc.h"
  40. #include "psymodel.h"
  41. #define AAC_MAX_CHANNELS 6
  42. #define ERROR_IF(cond, ...) \
  43. if (cond) { \
  44. av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
  45. return AVERROR(EINVAL); \
  46. }
  47. float ff_aac_pow34sf_tab[428];
  48. static const uint8_t swb_size_1024_96[] = {
  49. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  50. 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
  51. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  52. };
  53. static const uint8_t swb_size_1024_64[] = {
  54. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
  55. 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
  56. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
  57. };
  58. static const uint8_t swb_size_1024_48[] = {
  59. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  60. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  61. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  62. 96
  63. };
  64. static const uint8_t swb_size_1024_32[] = {
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
  66. 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
  67. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  68. };
  69. static const uint8_t swb_size_1024_24[] = {
  70. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  71. 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
  72. 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  73. };
  74. static const uint8_t swb_size_1024_16[] = {
  75. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  76. 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
  77. 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  78. };
  79. static const uint8_t swb_size_1024_8[] = {
  80. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  81. 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
  82. 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  83. };
  84. static const uint8_t *swb_size_1024[] = {
  85. swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
  86. swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
  87. swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
  88. swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
  89. };
  90. static const uint8_t swb_size_128_96[] = {
  91. 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  92. };
  93. static const uint8_t swb_size_128_48[] = {
  94. 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
  95. };
  96. static const uint8_t swb_size_128_24[] = {
  97. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
  98. };
  99. static const uint8_t swb_size_128_16[] = {
  100. 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  101. };
  102. static const uint8_t swb_size_128_8[] = {
  103. 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  104. };
  105. static const uint8_t *swb_size_128[] = {
  106. /* the last entry on the following row is swb_size_128_64 but is a
  107. duplicate of swb_size_128_96 */
  108. swb_size_128_96, swb_size_128_96, swb_size_128_96,
  109. swb_size_128_48, swb_size_128_48, swb_size_128_48,
  110. swb_size_128_24, swb_size_128_24, swb_size_128_16,
  111. swb_size_128_16, swb_size_128_16, swb_size_128_8
  112. };
  113. /** default channel configurations */
  114. static const uint8_t aac_chan_configs[6][5] = {
  115. {1, TYPE_SCE}, // 1 channel - single channel element
  116. {1, TYPE_CPE}, // 2 channels - channel pair
  117. {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
  118. {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
  119. {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
  120. {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
  121. };
  122. /**
  123. * Make AAC audio config object.
  124. * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
  125. */
  126. static void put_audio_specific_config(AVCodecContext *avctx)
  127. {
  128. PutBitContext pb;
  129. AACEncContext *s = avctx->priv_data;
  130. init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
  131. put_bits(&pb, 5, 2); //object type - AAC-LC
  132. put_bits(&pb, 4, s->samplerate_index); //sample rate index
  133. put_bits(&pb, 4, s->channels);
  134. //GASpecificConfig
  135. put_bits(&pb, 1, 0); //frame length - 1024 samples
  136. put_bits(&pb, 1, 0); //does not depend on core coder
  137. put_bits(&pb, 1, 0); //is not extension
  138. //Explicitly Mark SBR absent
  139. put_bits(&pb, 11, 0x2b7); //sync extension
  140. put_bits(&pb, 5, AOT_SBR);
  141. put_bits(&pb, 1, 0);
  142. flush_put_bits(&pb);
  143. }
  144. static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
  145. float *audio)
  146. {
  147. int i, k;
  148. const int chans = s->channels;
  149. const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  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. float *output = sce->ret;
  153. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  154. memcpy(output, sce->saved, sizeof(float)*1024);
  155. if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
  156. memset(output, 0, sizeof(output[0]) * 448);
  157. for (i = 448; i < 576; i++)
  158. output[i] = sce->saved[i] * pwindow[i - 448];
  159. for (i = 576; i < 704; i++)
  160. output[i] = sce->saved[i];
  161. }
  162. if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
  163. for (i = 0; i < 1024; i++) {
  164. output[i+1024] = audio[i * chans] * lwindow[1024 - i - 1];
  165. sce->saved[i] = audio[i * chans] * lwindow[i];
  166. }
  167. } else {
  168. for (i = 0; i < 448; i++)
  169. output[i+1024] = audio[i * chans];
  170. for (; i < 576; i++)
  171. output[i+1024] = audio[i * chans] * swindow[576 - i - 1];
  172. memset(output+1024+576, 0, sizeof(output[0]) * 448);
  173. for (i = 0; i < 1024; i++)
  174. sce->saved[i] = audio[i * chans];
  175. }
  176. s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
  177. } else {
  178. for (k = 0; k < 1024; k += 128) {
  179. for (i = 448 + k; i < 448 + k + 256; i++)
  180. output[i - 448 - k] = (i < 1024)
  181. ? sce->saved[i]
  182. : audio[(i-1024)*chans];
  183. s->dsp.vector_fmul (output, output, k ? swindow : pwindow, 128);
  184. s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
  185. s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
  186. }
  187. for (i = 0; i < 1024; i++)
  188. sce->saved[i] = audio[i * chans];
  189. }
  190. }
  191. /**
  192. * Encode ics_info element.
  193. * @see Table 4.6 (syntax of ics_info)
  194. */
  195. static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
  196. {
  197. int w;
  198. put_bits(&s->pb, 1, 0); // ics_reserved bit
  199. put_bits(&s->pb, 2, info->window_sequence[0]);
  200. put_bits(&s->pb, 1, info->use_kb_window[0]);
  201. if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  202. put_bits(&s->pb, 6, info->max_sfb);
  203. put_bits(&s->pb, 1, 0); // no prediction
  204. } else {
  205. put_bits(&s->pb, 4, info->max_sfb);
  206. for (w = 1; w < 8; w++)
  207. put_bits(&s->pb, 1, !info->group_len[w]);
  208. }
  209. }
  210. /**
  211. * Encode MS data.
  212. * @see 4.6.8.1 "Joint Coding - M/S Stereo"
  213. */
  214. static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
  215. {
  216. int i, w;
  217. put_bits(pb, 2, cpe->ms_mode);
  218. if (cpe->ms_mode == 1)
  219. for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
  220. for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
  221. put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
  222. }
  223. /**
  224. * Produce integer coefficients from scalefactors provided by the model.
  225. */
  226. static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
  227. {
  228. int i, w, w2, g, ch;
  229. int start, maxsfb, cmaxsfb;
  230. for (ch = 0; ch < chans; ch++) {
  231. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  232. start = 0;
  233. maxsfb = 0;
  234. cpe->ch[ch].pulse.num_pulse = 0;
  235. for (w = 0; w < ics->num_windows*16; w += 16) {
  236. for (g = 0; g < ics->num_swb; g++) {
  237. //apply M/S
  238. if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
  239. for (i = 0; i < ics->swb_sizes[g]; i++) {
  240. cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
  241. cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
  242. }
  243. }
  244. start += ics->swb_sizes[g];
  245. }
  246. for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
  247. ;
  248. maxsfb = FFMAX(maxsfb, cmaxsfb);
  249. }
  250. ics->max_sfb = maxsfb;
  251. //adjust zero bands for window groups
  252. for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
  253. for (g = 0; g < ics->max_sfb; g++) {
  254. i = 1;
  255. for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
  256. if (!cpe->ch[ch].zeroes[w2*16 + g]) {
  257. i = 0;
  258. break;
  259. }
  260. }
  261. cpe->ch[ch].zeroes[w*16 + g] = i;
  262. }
  263. }
  264. }
  265. if (chans > 1 && cpe->common_window) {
  266. IndividualChannelStream *ics0 = &cpe->ch[0].ics;
  267. IndividualChannelStream *ics1 = &cpe->ch[1].ics;
  268. int msc = 0;
  269. ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
  270. ics1->max_sfb = ics0->max_sfb;
  271. for (w = 0; w < ics0->num_windows*16; w += 16)
  272. for (i = 0; i < ics0->max_sfb; i++)
  273. if (cpe->ms_mask[w+i])
  274. msc++;
  275. if (msc == 0 || ics0->max_sfb == 0)
  276. cpe->ms_mode = 0;
  277. else
  278. cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
  279. }
  280. }
  281. /**
  282. * Encode scalefactor band coding type.
  283. */
  284. static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
  285. {
  286. int w;
  287. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  288. s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
  289. }
  290. /**
  291. * Encode scalefactors.
  292. */
  293. static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
  294. SingleChannelElement *sce)
  295. {
  296. int off = sce->sf_idx[0], diff;
  297. int i, w;
  298. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  299. for (i = 0; i < sce->ics.max_sfb; i++) {
  300. if (!sce->zeroes[w*16 + i]) {
  301. diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
  302. if (diff < 0 || diff > 120)
  303. av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
  304. off = sce->sf_idx[w*16 + i];
  305. put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * Encode pulse data.
  312. */
  313. static void encode_pulses(AACEncContext *s, Pulse *pulse)
  314. {
  315. int i;
  316. put_bits(&s->pb, 1, !!pulse->num_pulse);
  317. if (!pulse->num_pulse)
  318. return;
  319. put_bits(&s->pb, 2, pulse->num_pulse - 1);
  320. put_bits(&s->pb, 6, pulse->start);
  321. for (i = 0; i < pulse->num_pulse; i++) {
  322. put_bits(&s->pb, 5, pulse->pos[i]);
  323. put_bits(&s->pb, 4, pulse->amp[i]);
  324. }
  325. }
  326. /**
  327. * Encode spectral coefficients processed by psychoacoustic model.
  328. */
  329. static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
  330. {
  331. int start, i, w, w2;
  332. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  333. start = 0;
  334. for (i = 0; i < sce->ics.max_sfb; i++) {
  335. if (sce->zeroes[w*16 + i]) {
  336. start += sce->ics.swb_sizes[i];
  337. continue;
  338. }
  339. for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
  340. s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
  341. sce->ics.swb_sizes[i],
  342. sce->sf_idx[w*16 + i],
  343. sce->band_type[w*16 + i],
  344. s->lambda);
  345. start += sce->ics.swb_sizes[i];
  346. }
  347. }
  348. }
  349. /**
  350. * Encode one channel of audio data.
  351. */
  352. static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
  353. SingleChannelElement *sce,
  354. int common_window)
  355. {
  356. put_bits(&s->pb, 8, sce->sf_idx[0]);
  357. if (!common_window)
  358. put_ics_info(s, &sce->ics);
  359. encode_band_info(s, sce);
  360. encode_scale_factors(avctx, s, sce);
  361. encode_pulses(s, &sce->pulse);
  362. put_bits(&s->pb, 1, 0); //tns
  363. put_bits(&s->pb, 1, 0); //ssr
  364. encode_spectral_coeffs(s, sce);
  365. return 0;
  366. }
  367. /**
  368. * Write some auxiliary information about the created AAC file.
  369. */
  370. static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
  371. const char *name)
  372. {
  373. int i, namelen, padbits;
  374. namelen = strlen(name) + 2;
  375. put_bits(&s->pb, 3, TYPE_FIL);
  376. put_bits(&s->pb, 4, FFMIN(namelen, 15));
  377. if (namelen >= 15)
  378. put_bits(&s->pb, 8, namelen - 16);
  379. put_bits(&s->pb, 4, 0); //extension type - filler
  380. padbits = 8 - (put_bits_count(&s->pb) & 7);
  381. avpriv_align_put_bits(&s->pb);
  382. for (i = 0; i < namelen - 2; i++)
  383. put_bits(&s->pb, 8, name[i]);
  384. put_bits(&s->pb, 12 - padbits, 0);
  385. }
  386. static int aac_encode_frame(AVCodecContext *avctx,
  387. uint8_t *frame, int buf_size, void *data)
  388. {
  389. AACEncContext *s = avctx->priv_data;
  390. float *samples = s->samples, *samples2, *la;
  391. ChannelElement *cpe;
  392. int i, ch, w, g, chans, tag, start_ch;
  393. int chan_el_counter[4];
  394. FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
  395. if (s->last_frame)
  396. return 0;
  397. if (data) {
  398. if (!s->psypp) {
  399. memcpy(s->samples + 1024 * s->channels, data,
  400. 1024 * s->channels * sizeof(s->samples[0]));
  401. } else {
  402. start_ch = 0;
  403. samples2 = s->samples + 1024 * s->channels;
  404. for (i = 0; i < s->chan_map[0]; i++) {
  405. tag = s->chan_map[i+1];
  406. chans = tag == TYPE_CPE ? 2 : 1;
  407. ff_psy_preprocess(s->psypp, (float*)data + start_ch,
  408. samples2 + start_ch, start_ch, chans);
  409. start_ch += chans;
  410. }
  411. }
  412. }
  413. if (!avctx->frame_number) {
  414. memcpy(s->samples, s->samples + 1024 * s->channels,
  415. 1024 * s->channels * sizeof(s->samples[0]));
  416. return 0;
  417. }
  418. start_ch = 0;
  419. for (i = 0; i < s->chan_map[0]; i++) {
  420. FFPsyWindowInfo* wi = windows + start_ch;
  421. tag = s->chan_map[i+1];
  422. chans = tag == TYPE_CPE ? 2 : 1;
  423. cpe = &s->cpe[i];
  424. for (ch = 0; ch < chans; ch++) {
  425. IndividualChannelStream *ics = &cpe->ch[ch].ics;
  426. int cur_channel = start_ch + ch;
  427. samples2 = samples + cur_channel;
  428. la = samples2 + (448+64) * s->channels;
  429. if (!data)
  430. la = NULL;
  431. if (tag == TYPE_LFE) {
  432. wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
  433. wi[ch].window_shape = 0;
  434. wi[ch].num_windows = 1;
  435. wi[ch].grouping[0] = 1;
  436. /* Only the lowest 12 coefficients are used in a LFE channel.
  437. * The expression below results in only the bottom 8 coefficients
  438. * being used for 11.025kHz to 16kHz sample rates.
  439. */
  440. ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
  441. } else {
  442. wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
  443. ics->window_sequence[0]);
  444. }
  445. ics->window_sequence[1] = ics->window_sequence[0];
  446. ics->window_sequence[0] = wi[ch].window_type[0];
  447. ics->use_kb_window[1] = ics->use_kb_window[0];
  448. ics->use_kb_window[0] = wi[ch].window_shape;
  449. ics->num_windows = wi[ch].num_windows;
  450. ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
  451. ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
  452. for (w = 0; w < ics->num_windows; w++)
  453. ics->group_len[w] = wi[ch].grouping[w];
  454. apply_window_and_mdct(s, &cpe->ch[ch], samples2);
  455. }
  456. start_ch += chans;
  457. }
  458. do {
  459. int frame_bits;
  460. init_put_bits(&s->pb, frame, buf_size*8);
  461. if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
  462. put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
  463. start_ch = 0;
  464. memset(chan_el_counter, 0, sizeof(chan_el_counter));
  465. for (i = 0; i < s->chan_map[0]; i++) {
  466. FFPsyWindowInfo* wi = windows + start_ch;
  467. const float *coeffs[2];
  468. tag = s->chan_map[i+1];
  469. chans = tag == TYPE_CPE ? 2 : 1;
  470. cpe = &s->cpe[i];
  471. put_bits(&s->pb, 3, tag);
  472. put_bits(&s->pb, 4, chan_el_counter[tag]++);
  473. for (ch = 0; ch < chans; ch++)
  474. coeffs[ch] = cpe->ch[ch].coeffs;
  475. s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
  476. for (ch = 0; ch < chans; ch++) {
  477. s->cur_channel = start_ch * 2 + ch;
  478. s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
  479. }
  480. cpe->common_window = 0;
  481. if (chans > 1
  482. && wi[0].window_type[0] == wi[1].window_type[0]
  483. && wi[0].window_shape == wi[1].window_shape) {
  484. cpe->common_window = 1;
  485. for (w = 0; w < wi[0].num_windows; w++) {
  486. if (wi[0].grouping[w] != wi[1].grouping[w]) {
  487. cpe->common_window = 0;
  488. break;
  489. }
  490. }
  491. }
  492. s->cur_channel = start_ch * 2;
  493. if (s->options.stereo_mode && cpe->common_window) {
  494. if (s->options.stereo_mode > 0) {
  495. IndividualChannelStream *ics = &cpe->ch[0].ics;
  496. for (w = 0; w < ics->num_windows; w += ics->group_len[w])
  497. for (g = 0; g < ics->num_swb; g++)
  498. cpe->ms_mask[w*16+g] = 1;
  499. } else if (s->coder->search_for_ms) {
  500. s->coder->search_for_ms(s, cpe, s->lambda);
  501. }
  502. }
  503. adjust_frame_information(s, cpe, chans);
  504. if (chans == 2) {
  505. put_bits(&s->pb, 1, cpe->common_window);
  506. if (cpe->common_window) {
  507. put_ics_info(s, &cpe->ch[0].ics);
  508. encode_ms_info(&s->pb, cpe);
  509. }
  510. }
  511. for (ch = 0; ch < chans; ch++) {
  512. s->cur_channel = start_ch + ch;
  513. encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
  514. }
  515. start_ch += chans;
  516. }
  517. frame_bits = put_bits_count(&s->pb);
  518. if (frame_bits <= 6144 * s->channels - 3) {
  519. s->psy.bitres.bits = frame_bits / s->channels;
  520. break;
  521. }
  522. s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
  523. } while (1);
  524. put_bits(&s->pb, 3, TYPE_END);
  525. flush_put_bits(&s->pb);
  526. avctx->frame_bits = put_bits_count(&s->pb);
  527. // rate control stuff
  528. if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
  529. float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
  530. s->lambda *= ratio;
  531. s->lambda = FFMIN(s->lambda, 65536.f);
  532. }
  533. if (!data)
  534. s->last_frame = 1;
  535. memcpy(s->samples, s->samples + 1024 * s->channels,
  536. 1024 * s->channels * sizeof(s->samples[0]));
  537. return put_bits_count(&s->pb)>>3;
  538. }
  539. static av_cold int aac_encode_end(AVCodecContext *avctx)
  540. {
  541. AACEncContext *s = avctx->priv_data;
  542. ff_mdct_end(&s->mdct1024);
  543. ff_mdct_end(&s->mdct128);
  544. ff_psy_end(&s->psy);
  545. if (s->psypp)
  546. ff_psy_preprocess_end(s->psypp);
  547. av_freep(&s->samples);
  548. av_freep(&s->cpe);
  549. return 0;
  550. }
  551. static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
  552. {
  553. int ret = 0;
  554. dsputil_init(&s->dsp, avctx);
  555. // window init
  556. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  557. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  558. ff_init_ff_sine_windows(10);
  559. ff_init_ff_sine_windows(7);
  560. if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
  561. return ret;
  562. if (ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0))
  563. return ret;
  564. return 0;
  565. }
  566. static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
  567. {
  568. FF_ALLOC_OR_GOTO (avctx, s->samples, 2 * 1024 * s->channels * sizeof(s->samples[0]), alloc_fail);
  569. FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
  570. FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
  571. return 0;
  572. alloc_fail:
  573. return AVERROR(ENOMEM);
  574. }
  575. static av_cold int aac_encode_init(AVCodecContext *avctx)
  576. {
  577. AACEncContext *s = avctx->priv_data;
  578. int i, ret = 0;
  579. const uint8_t *sizes[2];
  580. uint8_t grouping[AAC_MAX_CHANNELS];
  581. int lengths[2];
  582. avctx->frame_size = 1024;
  583. for (i = 0; i < 16; i++)
  584. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
  585. break;
  586. s->channels = avctx->channels;
  587. ERROR_IF(i == 16,
  588. "Unsupported sample rate %d\n", avctx->sample_rate);
  589. ERROR_IF(s->channels > AAC_MAX_CHANNELS,
  590. "Unsupported number of channels: %d\n", s->channels);
  591. ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
  592. "Unsupported profile %d\n", avctx->profile);
  593. ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
  594. "Too many bits per frame requested\n");
  595. s->samplerate_index = i;
  596. s->chan_map = aac_chan_configs[s->channels-1];
  597. if (ret = dsp_init(avctx, s))
  598. goto fail;
  599. if (ret = alloc_buffers(avctx, s))
  600. goto fail;
  601. avctx->extradata_size = 5;
  602. put_audio_specific_config(avctx);
  603. sizes[0] = swb_size_1024[i];
  604. sizes[1] = swb_size_128[i];
  605. lengths[0] = ff_aac_num_swb_1024[i];
  606. lengths[1] = ff_aac_num_swb_128[i];
  607. for (i = 0; i < s->chan_map[0]; i++)
  608. grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
  609. if (ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping))
  610. goto fail;
  611. s->psypp = ff_psy_preprocess_init(avctx);
  612. s->coder = &ff_aac_coders[2];
  613. s->lambda = avctx->global_quality ? avctx->global_quality : 120;
  614. ff_aac_tableinit();
  615. for (i = 0; i < 428; i++)
  616. ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
  617. return 0;
  618. fail:
  619. aac_encode_end(avctx);
  620. return ret;
  621. }
  622. #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  623. static const AVOption aacenc_options[] = {
  624. {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.dbl = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
  625. {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.dbl = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
  626. {"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
  627. {"ms_force", "Force Mid/Side for the whole frame if possible", 0, AV_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
  628. {NULL}
  629. };
  630. static const AVClass aacenc_class = {
  631. "AAC encoder",
  632. av_default_item_name,
  633. aacenc_options,
  634. LIBAVUTIL_VERSION_INT,
  635. };
  636. AVCodec ff_aac_encoder = {
  637. .name = "aac",
  638. .type = AVMEDIA_TYPE_AUDIO,
  639. .id = CODEC_ID_AAC,
  640. .priv_data_size = sizeof(AACEncContext),
  641. .init = aac_encode_init,
  642. .encode = aac_encode_frame,
  643. .close = aac_encode_end,
  644. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
  645. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
  646. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  647. .priv_class = &aacenc_class,
  648. };