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.

630 lines
22KB

  1. /*
  2. * AAC encoder psychoacoustic model
  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 psychoacoustic model
  24. */
  25. #include "avcodec.h"
  26. #include "aactab.h"
  27. #include "psymodel.h"
  28. /***********************************
  29. * TODOs:
  30. * thresholds linearization after their modifications for attaining given bitrate
  31. * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
  32. * control quality for quality-based output
  33. **********************************/
  34. /**
  35. * constants for 3GPP AAC psychoacoustic model
  36. * @{
  37. */
  38. #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
  39. #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
  40. #define PSY_3GPP_RPEMIN 0.01f
  41. #define PSY_3GPP_RPELEV 2.0f
  42. /* LAME psy model constants */
  43. #define PSY_LAME_FIR_LEN 21 ///< LAME psy model FIR order
  44. #define AAC_BLOCK_SIZE_LONG 1024 ///< long block size
  45. #define AAC_BLOCK_SIZE_SHORT 128 ///< short block size
  46. #define AAC_NUM_BLOCKS_SHORT 8 ///< number of blocks in a short sequence
  47. #define PSY_LAME_NUM_SUBBLOCKS 3 ///< Number of sub-blocks in each short block
  48. /**
  49. * @}
  50. */
  51. /**
  52. * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
  53. */
  54. typedef struct AacPsyBand{
  55. float energy; ///< band energy
  56. float ffac; ///< form factor
  57. float thr; ///< energy threshold
  58. float min_snr; ///< minimal SNR
  59. float thr_quiet; ///< threshold in quiet
  60. }AacPsyBand;
  61. /**
  62. * single/pair channel context for psychoacoustic model
  63. */
  64. typedef struct AacPsyChannel{
  65. AacPsyBand band[128]; ///< bands information
  66. AacPsyBand prev_band[128]; ///< bands information from the previous frame
  67. float win_energy; ///< sliding average of channel energy
  68. float iir_state[2]; ///< hi-pass IIR filter state
  69. uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
  70. enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
  71. /* LAME psy model specific members */
  72. float attack_threshold; ///< attack threshold for this channel
  73. float prev_energy_subshort[AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
  74. int prev_attack; ///< attack value for the last short block in the previous sequence
  75. }AacPsyChannel;
  76. /**
  77. * psychoacoustic model frame type-dependent coefficients
  78. */
  79. typedef struct AacPsyCoeffs{
  80. float ath [64]; ///< absolute threshold of hearing per bands
  81. float barks [64]; ///< Bark value for each spectral band in long frame
  82. float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
  83. float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
  84. }AacPsyCoeffs;
  85. /**
  86. * 3GPP TS26.403-inspired psychoacoustic model specific data
  87. */
  88. typedef struct AacPsyContext{
  89. AacPsyCoeffs psy_coef[2];
  90. AacPsyChannel *ch;
  91. }AacPsyContext;
  92. /**
  93. * LAME psy model preset struct
  94. */
  95. typedef struct {
  96. int quality; ///< Quality to map the rest of the vaules to.
  97. /* This is overloaded to be both kbps per channel in ABR mode, and
  98. * requested quality in constant quality mode.
  99. */
  100. float st_lrm; ///< short threshold for L, R, and M channels
  101. } PsyLamePreset;
  102. /**
  103. * LAME psy model preset table for ABR
  104. */
  105. static const PsyLamePreset psy_abr_map[] = {
  106. /* TODO: Tuning. These were taken from LAME. */
  107. /* kbps/ch st_lrm */
  108. { 8, 6.60},
  109. { 16, 6.60},
  110. { 24, 6.60},
  111. { 32, 6.60},
  112. { 40, 6.60},
  113. { 48, 6.60},
  114. { 56, 6.60},
  115. { 64, 6.40},
  116. { 80, 6.00},
  117. { 96, 5.60},
  118. {112, 5.20},
  119. {128, 5.20},
  120. {160, 5.20}
  121. };
  122. /**
  123. * LAME psy model preset table for constant quality
  124. */
  125. static const PsyLamePreset psy_vbr_map[] = {
  126. /* vbr_q st_lrm */
  127. { 0, 4.20},
  128. { 1, 4.20},
  129. { 2, 4.20},
  130. { 3, 4.20},
  131. { 4, 4.20},
  132. { 5, 4.20},
  133. { 6, 4.20},
  134. { 7, 4.20},
  135. { 8, 4.20},
  136. { 9, 4.20},
  137. {10, 4.20}
  138. };
  139. /**
  140. * LAME psy model FIR coefficient table
  141. */
  142. static const float psy_fir_coeffs[] = {
  143. -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
  144. -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
  145. -5.52212e-17 * 2, -0.313819 * 2
  146. };
  147. /**
  148. * calculates the attack threshold for ABR from the above table for the LAME psy model
  149. */
  150. static float lame_calc_attack_threshold(int bitrate)
  151. {
  152. /* Assume max bitrate to start with */
  153. int lower_range = 12, upper_range = 12;
  154. int lower_range_kbps = psy_abr_map[12].quality;
  155. int upper_range_kbps = psy_abr_map[12].quality;
  156. int i;
  157. /* Determine which bitrates the value specified falls between.
  158. * If the loop ends without breaking our above assumption of 320kbps was correct.
  159. */
  160. for (i = 1; i < 13; i++) {
  161. if (FFMAX(bitrate, psy_abr_map[i].quality) != bitrate) {
  162. upper_range = i;
  163. upper_range_kbps = psy_abr_map[i ].quality;
  164. lower_range = i - 1;
  165. lower_range_kbps = psy_abr_map[i - 1].quality;
  166. break; /* Upper range found */
  167. }
  168. }
  169. /* Determine which range the value specified is closer to */
  170. if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))
  171. return psy_abr_map[lower_range].st_lrm;
  172. return psy_abr_map[upper_range].st_lrm;
  173. }
  174. /**
  175. * LAME psy model specific initialization
  176. */
  177. static void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx) {
  178. int i;
  179. for (i = 0; i < avctx->channels; i++) {
  180. AacPsyChannel *pch = &ctx->ch[i];
  181. if (avctx->flags & CODEC_FLAG_QSCALE)
  182. pch->attack_threshold = psy_vbr_map[avctx->global_quality / FF_QP2LAMBDA].st_lrm;
  183. else
  184. pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->channels / 1000);
  185. for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++)
  186. pch->prev_energy_subshort[i] = 10.0f;
  187. }
  188. }
  189. /**
  190. * Calculate Bark value for given line.
  191. */
  192. static av_cold float calc_bark(float f)
  193. {
  194. return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
  195. }
  196. #define ATH_ADD 4
  197. /**
  198. * Calculate ATH value for given frequency.
  199. * Borrowed from Lame.
  200. */
  201. static av_cold float ath(float f, float add)
  202. {
  203. f /= 1000.0f;
  204. return 3.64 * pow(f, -0.8)
  205. - 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
  206. + 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
  207. + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
  208. }
  209. static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
  210. AacPsyContext *pctx;
  211. float bark;
  212. int i, j, g, start;
  213. float prev, minscale, minath;
  214. ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
  215. pctx = (AacPsyContext*) ctx->model_priv_data;
  216. minath = ath(3410, ATH_ADD);
  217. for (j = 0; j < 2; j++) {
  218. AacPsyCoeffs *coeffs = &pctx->psy_coef[j];
  219. float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
  220. i = 0;
  221. prev = 0.0;
  222. for (g = 0; g < ctx->num_bands[j]; g++) {
  223. i += ctx->bands[j][g];
  224. bark = calc_bark((i-1) * line_to_frequency);
  225. coeffs->barks[g] = (bark + prev) / 2.0;
  226. prev = bark;
  227. }
  228. for (g = 0; g < ctx->num_bands[j] - 1; g++) {
  229. coeffs->spread_low[g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_LOW);
  230. coeffs->spread_hi [g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_HI);
  231. }
  232. start = 0;
  233. for (g = 0; g < ctx->num_bands[j]; g++) {
  234. minscale = ath(start * line_to_frequency, ATH_ADD);
  235. for (i = 1; i < ctx->bands[j][g]; i++)
  236. minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
  237. coeffs->ath[g] = minscale - minath;
  238. start += ctx->bands[j][g];
  239. }
  240. }
  241. pctx->ch = av_mallocz(sizeof(AacPsyChannel) * ctx->avctx->channels);
  242. lame_window_init(pctx, ctx->avctx);
  243. return 0;
  244. }
  245. /**
  246. * IIR filter used in block switching decision
  247. */
  248. static float iir_filter(int in, float state[2])
  249. {
  250. float ret;
  251. ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
  252. state[0] = in;
  253. state[1] = ret;
  254. return ret;
  255. }
  256. /**
  257. * window grouping information stored as bits (0 - new group, 1 - group continues)
  258. */
  259. static const uint8_t window_grouping[9] = {
  260. 0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
  261. };
  262. /**
  263. * Tell encoder which window types to use.
  264. * @see 3GPP TS26.403 5.4.1 "Blockswitching"
  265. */
  266. static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
  267. const int16_t *audio, const int16_t *la,
  268. int channel, int prev_type)
  269. {
  270. int i, j;
  271. int br = ctx->avctx->bit_rate / ctx->avctx->channels;
  272. int attack_ratio = br <= 16000 ? 18 : 10;
  273. AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
  274. AacPsyChannel *pch = &pctx->ch[channel];
  275. uint8_t grouping = 0;
  276. int next_type = pch->next_window_seq;
  277. FFPsyWindowInfo wi;
  278. memset(&wi, 0, sizeof(wi));
  279. if (la) {
  280. float s[8], v;
  281. int switch_to_eight = 0;
  282. float sum = 0.0, sum2 = 0.0;
  283. int attack_n = 0;
  284. int stay_short = 0;
  285. for (i = 0; i < 8; i++) {
  286. for (j = 0; j < 128; j++) {
  287. v = iir_filter(la[(i*128+j)*ctx->avctx->channels], pch->iir_state);
  288. sum += v*v;
  289. }
  290. s[i] = sum;
  291. sum2 += sum;
  292. }
  293. for (i = 0; i < 8; i++) {
  294. if (s[i] > pch->win_energy * attack_ratio) {
  295. attack_n = i + 1;
  296. switch_to_eight = 1;
  297. break;
  298. }
  299. }
  300. pch->win_energy = pch->win_energy*7/8 + sum2/64;
  301. wi.window_type[1] = prev_type;
  302. switch (prev_type) {
  303. case ONLY_LONG_SEQUENCE:
  304. wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
  305. next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
  306. break;
  307. case LONG_START_SEQUENCE:
  308. wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
  309. grouping = pch->next_grouping;
  310. next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
  311. break;
  312. case LONG_STOP_SEQUENCE:
  313. wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
  314. next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
  315. break;
  316. case EIGHT_SHORT_SEQUENCE:
  317. stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
  318. wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
  319. grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
  320. next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
  321. break;
  322. }
  323. pch->next_grouping = window_grouping[attack_n];
  324. pch->next_window_seq = next_type;
  325. } else {
  326. for (i = 0; i < 3; i++)
  327. wi.window_type[i] = prev_type;
  328. grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
  329. }
  330. wi.window_shape = 1;
  331. if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
  332. wi.num_windows = 1;
  333. wi.grouping[0] = 1;
  334. } else {
  335. int lastgrp = 0;
  336. wi.num_windows = 8;
  337. for (i = 0; i < 8; i++) {
  338. if (!((grouping >> i) & 1))
  339. lastgrp = i;
  340. wi.grouping[lastgrp]++;
  341. }
  342. }
  343. return wi;
  344. }
  345. /**
  346. * Calculate band thresholds as suggested in 3GPP TS26.403
  347. */
  348. static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
  349. const float *coefs, FFPsyWindowInfo *wi)
  350. {
  351. AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
  352. AacPsyChannel *pch = &pctx->ch[channel];
  353. int start = 0;
  354. int i, w, g;
  355. const int num_bands = ctx->num_bands[wi->num_windows == 8];
  356. const uint8_t* band_sizes = ctx->bands[wi->num_windows == 8];
  357. AacPsyCoeffs *coeffs = &pctx->psy_coef[wi->num_windows == 8];
  358. //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
  359. for (w = 0; w < wi->num_windows*16; w += 16) {
  360. for (g = 0; g < num_bands; g++) {
  361. AacPsyBand *band = &pch->band[w+g];
  362. band->energy = 0.0f;
  363. for (i = 0; i < band_sizes[g]; i++)
  364. band->energy += coefs[start+i] * coefs[start+i];
  365. band->energy *= 1.0f / (512*512);
  366. band->thr = band->energy * 0.001258925f;
  367. start += band_sizes[g];
  368. ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].energy = band->energy;
  369. }
  370. }
  371. //modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
  372. for (w = 0; w < wi->num_windows*16; w += 16) {
  373. AacPsyBand *band = &pch->band[w];
  374. for (g = 1; g < num_bands; g++)
  375. band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
  376. for (g = num_bands - 2; g >= 0; g--)
  377. band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g]);
  378. for (g = 0; g < num_bands; g++) {
  379. band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
  380. if (wi->num_windows != 8 && wi->window_type[1] != EIGHT_SHORT_SEQUENCE)
  381. band[g].thr_quiet = FFMAX(PSY_3GPP_RPEMIN*band[g].thr_quiet,
  382. FFMIN(band[g].thr_quiet,
  383. PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
  384. band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
  385. ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].threshold = band[g].thr;
  386. }
  387. }
  388. memcpy(pch->prev_band, pch->band, sizeof(pch->band));
  389. }
  390. static av_cold void psy_3gpp_end(FFPsyContext *apc)
  391. {
  392. AacPsyContext *pctx = (AacPsyContext*) apc->model_priv_data;
  393. av_freep(&pctx->ch);
  394. av_freep(&apc->model_priv_data);
  395. }
  396. static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int uselongblock)
  397. {
  398. int blocktype = ONLY_LONG_SEQUENCE;
  399. if (uselongblock) {
  400. if (ctx->next_window_seq == EIGHT_SHORT_SEQUENCE)
  401. blocktype = LONG_STOP_SEQUENCE;
  402. } else {
  403. blocktype = EIGHT_SHORT_SEQUENCE;
  404. if (ctx->next_window_seq == ONLY_LONG_SEQUENCE)
  405. ctx->next_window_seq = LONG_START_SEQUENCE;
  406. if (ctx->next_window_seq == LONG_STOP_SEQUENCE)
  407. ctx->next_window_seq = EIGHT_SHORT_SEQUENCE;
  408. }
  409. wi->window_type[0] = ctx->next_window_seq;
  410. ctx->next_window_seq = blocktype;
  411. }
  412. static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx,
  413. const int16_t *audio, const int16_t *la,
  414. int channel, int prev_type)
  415. {
  416. AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
  417. AacPsyChannel *pch = &pctx->ch[channel];
  418. int grouping = 0;
  419. int uselongblock = 1;
  420. int attacks[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
  421. int i;
  422. FFPsyWindowInfo wi;
  423. memset(&wi, 0, sizeof(wi));
  424. if (la) {
  425. float hpfsmpl[AAC_BLOCK_SIZE_LONG];
  426. float const *pf = hpfsmpl;
  427. float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
  428. float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
  429. float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
  430. int chans = ctx->avctx->channels;
  431. const int16_t *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN) * chans;
  432. int j, att_sum = 0;
  433. /* LAME comment: apply high pass filter of fs/4 */
  434. for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
  435. float sum1, sum2;
  436. sum1 = firbuf[(i + ((PSY_LAME_FIR_LEN - 1) / 2)) * chans];
  437. sum2 = 0.0;
  438. for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
  439. sum1 += psy_fir_coeffs[j] * (firbuf[(i + j) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j) * chans]);
  440. sum2 += psy_fir_coeffs[j + 1] * (firbuf[(i + j + 1) * chans] + firbuf[(i + PSY_LAME_FIR_LEN - j - 1) * chans]);
  441. }
  442. hpfsmpl[i] = sum1 + sum2;
  443. }
  444. /* Calculate the energies of each sub-shortblock */
  445. for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
  446. energy_subshort[i] = pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS)];
  447. assert(pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)] > 0);
  448. attack_intensity[i] = energy_subshort[i] / pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)];
  449. energy_short[0] += energy_subshort[i];
  450. }
  451. for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
  452. float const *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
  453. float p = 1.0f;
  454. for (; pf < pfe; pf++)
  455. if (p < fabsf(*pf))
  456. p = fabsf(*pf);
  457. pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
  458. energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
  459. /* FIXME: The indexes below are [i + 3 - 2] in the LAME source.
  460. * Obviously the 3 and 2 have some significance, or this would be just [i + 1]
  461. * (which is what we use here). What the 3 stands for is ambigious, as it is both
  462. * number of short blocks, and the number of sub-short blocks.
  463. * It seems that LAME is comparing each sub-block to sub-block + 1 in the
  464. * previous block.
  465. */
  466. if (p > energy_subshort[i + 1])
  467. p = p / energy_subshort[i + 1];
  468. else if (energy_subshort[i + 1] > p * 10.0f)
  469. p = energy_subshort[i + 1] / (p * 10.0f);
  470. else
  471. p = 0.0;
  472. attack_intensity[i + PSY_LAME_NUM_SUBBLOCKS] = p;
  473. }
  474. /* compare energy between sub-short blocks */
  475. for (i = 0; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
  476. if (!attacks[i / PSY_LAME_NUM_SUBBLOCKS])
  477. if (attack_intensity[i] > pch->attack_threshold)
  478. attacks[i / PSY_LAME_NUM_SUBBLOCKS] = (i % PSY_LAME_NUM_SUBBLOCKS) + 1;
  479. /* should have energy change between short blocks, in order to avoid periodic signals */
  480. /* Good samples to show the effect are Trumpet test songs */
  481. /* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
  482. /* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
  483. for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++) {
  484. float const u = energy_short[i - 1];
  485. float const v = energy_short[i];
  486. float const m = FFMAX(u, v);
  487. if (m < 40000) { /* (2) */
  488. if (u < 1.7f * v && v < 1.7f * u) { /* (1) */
  489. if (i == 1 && attacks[0] < attacks[i])
  490. attacks[0] = 0;
  491. attacks[i] = 0;
  492. }
  493. }
  494. att_sum += attacks[i];
  495. }
  496. if (attacks[0] <= pch->prev_attack)
  497. attacks[0] = 0;
  498. att_sum += attacks[0];
  499. /* 3 below indicates the previous attack happened in the last sub-block of the previous sequence */
  500. if (pch->prev_attack == 3 || att_sum) {
  501. uselongblock = 0;
  502. if (attacks[1] && attacks[0])
  503. attacks[1] = 0;
  504. if (attacks[2] && attacks[1])
  505. attacks[2] = 0;
  506. if (attacks[3] && attacks[2])
  507. attacks[3] = 0;
  508. if (attacks[4] && attacks[3])
  509. attacks[4] = 0;
  510. if (attacks[5] && attacks[4])
  511. attacks[5] = 0;
  512. if (attacks[6] && attacks[5])
  513. attacks[6] = 0;
  514. if (attacks[7] && attacks[6])
  515. attacks[7] = 0;
  516. if (attacks[8] && attacks[7])
  517. attacks[8] = 0;
  518. }
  519. } else {
  520. /* We have no lookahead info, so just use same type as the previous sequence. */
  521. uselongblock = !(prev_type == EIGHT_SHORT_SEQUENCE);
  522. }
  523. lame_apply_block_type(pch, &wi, uselongblock);
  524. wi.window_type[1] = prev_type;
  525. if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
  526. wi.num_windows = 1;
  527. wi.grouping[0] = 1;
  528. if (wi.window_type[0] == LONG_START_SEQUENCE)
  529. wi.window_shape = 0;
  530. else
  531. wi.window_shape = 1;
  532. } else {
  533. int lastgrp = 0;
  534. wi.num_windows = 8;
  535. wi.window_shape = 0;
  536. for (i = 0; i < 8; i++) {
  537. if (!((pch->next_grouping >> i) & 1))
  538. lastgrp = i;
  539. wi.grouping[lastgrp]++;
  540. }
  541. }
  542. /* Determine grouping, based on the location of the first attack, and save for
  543. * the next frame.
  544. * FIXME: Move this to analysis.
  545. * TODO: Tune groupings depending on attack location
  546. * TODO: Handle more than one attack in a group
  547. */
  548. for (i = 0; i < 9; i++) {
  549. if (attacks[i]) {
  550. grouping = i;
  551. break;
  552. }
  553. }
  554. pch->next_grouping = window_grouping[grouping];
  555. pch->prev_attack = attacks[8];
  556. return wi;
  557. }
  558. const FFPsyModel ff_aac_psy_model =
  559. {
  560. .name = "3GPP TS 26.403-inspired model",
  561. .init = psy_3gpp_init,
  562. .window = psy_lame_window,
  563. .analyze = psy_3gpp_analyze,
  564. .end = psy_3gpp_end,
  565. };