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.

627 lines
22KB

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