2
0

aacsbr_template.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * AAC Spectral Band Replication decoding functions
  3. * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
  4. * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
  5. *
  6. * Fixed point code
  7. * Copyright (c) 2013
  8. * MIPS Technologies, Inc., California.
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * @file
  28. * AAC Spectral Band Replication decoding functions
  29. * @author Robert Swain ( rob opendot cl )
  30. * @author Stanislav Ocovaj ( stanislav.ocovaj@imgtec.com )
  31. * @author Zoran Basaric ( zoran.basaric@imgtec.com )
  32. */
  33. #include "libavutil/qsort.h"
  34. static av_cold void aacsbr_tableinit(void)
  35. {
  36. int n;
  37. for (n = 1; n < 320; n++)
  38. sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
  39. sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
  40. sbr_qmf_window_us[512] = -sbr_qmf_window_us[512];
  41. for (n = 0; n < 320; n++)
  42. sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
  43. }
  44. av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
  45. {
  46. static const struct {
  47. const void *sbr_codes, *sbr_bits;
  48. const unsigned int table_size, elem_size;
  49. } sbr_tmp[] = {
  50. SBR_VLC_ROW(t_huffman_env_1_5dB),
  51. SBR_VLC_ROW(f_huffman_env_1_5dB),
  52. SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
  53. SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
  54. SBR_VLC_ROW(t_huffman_env_3_0dB),
  55. SBR_VLC_ROW(f_huffman_env_3_0dB),
  56. SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
  57. SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
  58. SBR_VLC_ROW(t_huffman_noise_3_0dB),
  59. SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
  60. };
  61. // SBR VLC table initialization
  62. SBR_INIT_VLC_STATIC(0, 1098);
  63. SBR_INIT_VLC_STATIC(1, 1092);
  64. SBR_INIT_VLC_STATIC(2, 768);
  65. SBR_INIT_VLC_STATIC(3, 1026);
  66. SBR_INIT_VLC_STATIC(4, 1058);
  67. SBR_INIT_VLC_STATIC(5, 1052);
  68. SBR_INIT_VLC_STATIC(6, 544);
  69. SBR_INIT_VLC_STATIC(7, 544);
  70. SBR_INIT_VLC_STATIC(8, 592);
  71. SBR_INIT_VLC_STATIC(9, 512);
  72. aacsbr_tableinit();
  73. AAC_RENAME(ff_ps_init)();
  74. }
  75. /** Places SBR in pure upsampling mode. */
  76. static void sbr_turnoff(SpectralBandReplication *sbr) {
  77. sbr->start = 0;
  78. sbr->ready_for_dequant = 0;
  79. // Init defults used in pure upsampling mode
  80. sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
  81. sbr->m[1] = 0;
  82. // Reset values for first SBR header
  83. sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
  84. memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
  85. }
  86. av_cold void AAC_RENAME(ff_aac_sbr_ctx_init)(AACContext *ac, SpectralBandReplication *sbr, int id_aac)
  87. {
  88. if(sbr->mdct.mdct_bits)
  89. return;
  90. sbr->kx[0] = sbr->kx[1];
  91. sbr->id_aac = id_aac;
  92. sbr_turnoff(sbr);
  93. sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
  94. sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
  95. /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
  96. * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
  97. * and scale back down at synthesis. */
  98. AAC_RENAME_32(ff_mdct_init)(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
  99. AAC_RENAME_32(ff_mdct_init)(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
  100. AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
  101. AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
  102. aacsbr_func_ptr_init(&sbr->c);
  103. }
  104. av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(SpectralBandReplication *sbr)
  105. {
  106. AAC_RENAME_32(ff_mdct_end)(&sbr->mdct);
  107. AAC_RENAME_32(ff_mdct_end)(&sbr->mdct_ana);
  108. }
  109. static int qsort_comparison_function_int16(const void *a, const void *b)
  110. {
  111. return *(const int16_t *)a - *(const int16_t *)b;
  112. }
  113. static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
  114. {
  115. int i;
  116. for (i = 0; i <= last_el; i++)
  117. if (table[i] == needle)
  118. return 1;
  119. return 0;
  120. }
  121. /// Limiter Frequency Band Table (14496-3 sp04 p198)
  122. static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
  123. {
  124. int k;
  125. if (sbr->bs_limiter_bands > 0) {
  126. static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f), //2^(0.49/1.2)
  127. Q23(1.18509277094158210129f), //2^(0.49/2)
  128. Q23(1.11987160404675912501f) }; //2^(0.49/3)
  129. const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
  130. int16_t patch_borders[7];
  131. uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
  132. patch_borders[0] = sbr->kx[1];
  133. for (k = 1; k <= sbr->num_patches; k++)
  134. patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
  135. memcpy(sbr->f_tablelim, sbr->f_tablelow,
  136. (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
  137. if (sbr->num_patches > 1)
  138. memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
  139. (sbr->num_patches - 1) * sizeof(patch_borders[0]));
  140. AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
  141. uint16_t,
  142. qsort_comparison_function_int16);
  143. sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
  144. while (out < sbr->f_tablelim + sbr->n_lim) {
  145. #if USE_FIXED
  146. if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
  147. #else
  148. if (*in >= *out * lim_bands_per_octave_warped) {
  149. #endif /* USE_FIXED */
  150. *++out = *in++;
  151. } else if (*in == *out ||
  152. !in_table_int16(patch_borders, sbr->num_patches, *in)) {
  153. in++;
  154. sbr->n_lim--;
  155. } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
  156. *out = *in++;
  157. sbr->n_lim--;
  158. } else {
  159. *++out = *in++;
  160. }
  161. }
  162. } else {
  163. sbr->f_tablelim[0] = sbr->f_tablelow[0];
  164. sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
  165. sbr->n_lim = 1;
  166. }
  167. }
  168. static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
  169. {
  170. unsigned int cnt = get_bits_count(gb);
  171. uint8_t bs_header_extra_1;
  172. uint8_t bs_header_extra_2;
  173. int old_bs_limiter_bands = sbr->bs_limiter_bands;
  174. SpectrumParameters old_spectrum_params;
  175. sbr->start = 1;
  176. sbr->ready_for_dequant = 0;
  177. // Save last spectrum parameters variables to compare to new ones
  178. memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
  179. sbr->bs_amp_res_header = get_bits1(gb);
  180. sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
  181. sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
  182. sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
  183. skip_bits(gb, 2); // bs_reserved
  184. bs_header_extra_1 = get_bits1(gb);
  185. bs_header_extra_2 = get_bits1(gb);
  186. if (bs_header_extra_1) {
  187. sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
  188. sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
  189. sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
  190. } else {
  191. sbr->spectrum_params.bs_freq_scale = 2;
  192. sbr->spectrum_params.bs_alter_scale = 1;
  193. sbr->spectrum_params.bs_noise_bands = 2;
  194. }
  195. // Check if spectrum parameters changed
  196. if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
  197. sbr->reset = 1;
  198. if (bs_header_extra_2) {
  199. sbr->bs_limiter_bands = get_bits(gb, 2);
  200. sbr->bs_limiter_gains = get_bits(gb, 2);
  201. sbr->bs_interpol_freq = get_bits1(gb);
  202. sbr->bs_smoothing_mode = get_bits1(gb);
  203. } else {
  204. sbr->bs_limiter_bands = 2;
  205. sbr->bs_limiter_gains = 2;
  206. sbr->bs_interpol_freq = 1;
  207. sbr->bs_smoothing_mode = 1;
  208. }
  209. if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
  210. sbr_make_f_tablelim(sbr);
  211. return get_bits_count(gb) - cnt;
  212. }
  213. static int array_min_int16(const int16_t *array, int nel)
  214. {
  215. int i, min = array[0];
  216. for (i = 1; i < nel; i++)
  217. min = FFMIN(array[i], min);
  218. return min;
  219. }
  220. static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
  221. {
  222. // Requirements (14496-3 sp04 p205)
  223. if (n_master <= 0) {
  224. av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
  225. return -1;
  226. }
  227. if (bs_xover_band >= n_master) {
  228. av_log(avctx, AV_LOG_ERROR,
  229. "Invalid bitstream, crossover band index beyond array bounds: %d\n",
  230. bs_xover_band);
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. /// Master Frequency Band Table (14496-3 sp04 p194)
  236. static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
  237. SpectrumParameters *spectrum)
  238. {
  239. unsigned int temp, max_qmf_subbands = 0;
  240. unsigned int start_min, stop_min;
  241. int k;
  242. const int8_t *sbr_offset_ptr;
  243. int16_t stop_dk[13];
  244. switch (sbr->sample_rate) {
  245. case 16000:
  246. sbr_offset_ptr = sbr_offset[0];
  247. break;
  248. case 22050:
  249. sbr_offset_ptr = sbr_offset[1];
  250. break;
  251. case 24000:
  252. sbr_offset_ptr = sbr_offset[2];
  253. break;
  254. case 32000:
  255. sbr_offset_ptr = sbr_offset[3];
  256. break;
  257. case 44100: case 48000: case 64000:
  258. sbr_offset_ptr = sbr_offset[4];
  259. break;
  260. case 88200: case 96000: case 128000: case 176400: case 192000:
  261. sbr_offset_ptr = sbr_offset[5];
  262. break;
  263. default:
  264. av_log(ac->avctx, AV_LOG_ERROR,
  265. "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
  266. return -1;
  267. }
  268. if (sbr->sample_rate < 32000) {
  269. temp = 3000;
  270. } else if (sbr->sample_rate < 64000) {
  271. temp = 4000;
  272. } else
  273. temp = 5000;
  274. start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
  275. stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
  276. sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
  277. if (spectrum->bs_stop_freq < 14) {
  278. sbr->k[2] = stop_min;
  279. make_bands(stop_dk, stop_min, 64, 13);
  280. AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
  281. for (k = 0; k < spectrum->bs_stop_freq; k++)
  282. sbr->k[2] += stop_dk[k];
  283. } else if (spectrum->bs_stop_freq == 14) {
  284. sbr->k[2] = 2*sbr->k[0];
  285. } else if (spectrum->bs_stop_freq == 15) {
  286. sbr->k[2] = 3*sbr->k[0];
  287. } else {
  288. av_log(ac->avctx, AV_LOG_ERROR,
  289. "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
  290. return -1;
  291. }
  292. sbr->k[2] = FFMIN(64, sbr->k[2]);
  293. // Requirements (14496-3 sp04 p205)
  294. if (sbr->sample_rate <= 32000) {
  295. max_qmf_subbands = 48;
  296. } else if (sbr->sample_rate == 44100) {
  297. max_qmf_subbands = 35;
  298. } else if (sbr->sample_rate >= 48000)
  299. max_qmf_subbands = 32;
  300. else
  301. av_assert0(0);
  302. if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
  303. av_log(ac->avctx, AV_LOG_ERROR,
  304. "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
  305. return -1;
  306. }
  307. if (!spectrum->bs_freq_scale) {
  308. int dk, k2diff;
  309. dk = spectrum->bs_alter_scale + 1;
  310. sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
  311. if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
  312. return -1;
  313. for (k = 1; k <= sbr->n_master; k++)
  314. sbr->f_master[k] = dk;
  315. k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
  316. if (k2diff < 0) {
  317. sbr->f_master[1]--;
  318. sbr->f_master[2]-= (k2diff < -1);
  319. } else if (k2diff) {
  320. sbr->f_master[sbr->n_master]++;
  321. }
  322. sbr->f_master[0] = sbr->k[0];
  323. for (k = 1; k <= sbr->n_master; k++)
  324. sbr->f_master[k] += sbr->f_master[k - 1];
  325. } else {
  326. int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
  327. int two_regions, num_bands_0;
  328. int vdk0_max, vdk1_min;
  329. int16_t vk0[49];
  330. #if USE_FIXED
  331. int tmp, nz = 0;
  332. #endif /* USE_FIXED */
  333. if (49 * sbr->k[2] > 110 * sbr->k[0]) {
  334. two_regions = 1;
  335. sbr->k[1] = 2 * sbr->k[0];
  336. } else {
  337. two_regions = 0;
  338. sbr->k[1] = sbr->k[2];
  339. }
  340. #if USE_FIXED
  341. tmp = (sbr->k[1] << 23) / sbr->k[0];
  342. while (tmp < 0x40000000) {
  343. tmp <<= 1;
  344. nz++;
  345. }
  346. tmp = fixed_log(tmp - 0x80000000);
  347. tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
  348. tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
  349. num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
  350. #else
  351. num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
  352. #endif /* USE_FIXED */
  353. if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
  354. av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
  355. return -1;
  356. }
  357. vk0[0] = 0;
  358. make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
  359. AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
  360. vdk0_max = vk0[num_bands_0];
  361. vk0[0] = sbr->k[0];
  362. for (k = 1; k <= num_bands_0; k++) {
  363. if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
  364. av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
  365. return -1;
  366. }
  367. vk0[k] += vk0[k-1];
  368. }
  369. if (two_regions) {
  370. int16_t vk1[49];
  371. #if USE_FIXED
  372. int num_bands_1;
  373. tmp = (sbr->k[2] << 23) / sbr->k[1];
  374. nz = 0;
  375. while (tmp < 0x40000000) {
  376. tmp <<= 1;
  377. nz++;
  378. }
  379. tmp = fixed_log(tmp - 0x80000000);
  380. tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
  381. tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
  382. if (spectrum->bs_alter_scale)
  383. tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
  384. num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
  385. #else
  386. float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
  387. : 1.0f; // bs_alter_scale = {0,1}
  388. int num_bands_1 = lrintf(half_bands * invwarp *
  389. log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
  390. #endif /* USE_FIXED */
  391. make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
  392. vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
  393. if (vdk1_min < vdk0_max) {
  394. int change;
  395. AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
  396. change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
  397. vk1[1] += change;
  398. vk1[num_bands_1] -= change;
  399. }
  400. AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
  401. vk1[0] = sbr->k[1];
  402. for (k = 1; k <= num_bands_1; k++) {
  403. if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
  404. av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
  405. return -1;
  406. }
  407. vk1[k] += vk1[k-1];
  408. }
  409. sbr->n_master = num_bands_0 + num_bands_1;
  410. if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
  411. return -1;
  412. memcpy(&sbr->f_master[0], vk0,
  413. (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
  414. memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
  415. num_bands_1 * sizeof(sbr->f_master[0]));
  416. } else {
  417. sbr->n_master = num_bands_0;
  418. if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
  419. return -1;
  420. memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
  421. }
  422. }
  423. return 0;
  424. }
  425. /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
  426. static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
  427. {
  428. int i, k, last_k = -1, last_msb = -1, sb = 0;
  429. int msb = sbr->k[0];
  430. int usb = sbr->kx[1];
  431. int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
  432. sbr->num_patches = 0;
  433. if (goal_sb < sbr->kx[1] + sbr->m[1]) {
  434. for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
  435. } else
  436. k = sbr->n_master;
  437. do {
  438. int odd = 0;
  439. if (k == last_k && msb == last_msb) {
  440. av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
  441. return AVERROR_INVALIDDATA;
  442. }
  443. last_k = k;
  444. last_msb = msb;
  445. for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
  446. sb = sbr->f_master[i];
  447. odd = (sb + sbr->k[0]) & 1;
  448. }
  449. // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
  450. // After this check the final number of patches can still be six which is
  451. // illegal however the Coding Technologies decoder check stream has a final
  452. // count of 6 patches
  453. if (sbr->num_patches > 5) {
  454. av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
  455. return -1;
  456. }
  457. sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
  458. sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
  459. if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
  460. usb = sb;
  461. msb = sb;
  462. sbr->num_patches++;
  463. } else
  464. msb = sbr->kx[1];
  465. if (sbr->f_master[k] - sb < 3)
  466. k = sbr->n_master;
  467. } while (sb != sbr->kx[1] + sbr->m[1]);
  468. if (sbr->num_patches > 1 &&
  469. sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
  470. sbr->num_patches--;
  471. return 0;
  472. }
  473. /// Derived Frequency Band Tables (14496-3 sp04 p197)
  474. static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
  475. {
  476. int k, temp;
  477. #if USE_FIXED
  478. int nz = 0;
  479. #endif /* USE_FIXED */
  480. sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
  481. sbr->n[0] = (sbr->n[1] + 1) >> 1;
  482. memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
  483. (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
  484. sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
  485. sbr->kx[1] = sbr->f_tablehigh[0];
  486. // Requirements (14496-3 sp04 p205)
  487. if (sbr->kx[1] + sbr->m[1] > 64) {
  488. av_log(ac->avctx, AV_LOG_ERROR,
  489. "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
  490. return -1;
  491. }
  492. if (sbr->kx[1] > 32) {
  493. av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
  494. return -1;
  495. }
  496. sbr->f_tablelow[0] = sbr->f_tablehigh[0];
  497. temp = sbr->n[1] & 1;
  498. for (k = 1; k <= sbr->n[0]; k++)
  499. sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
  500. #if USE_FIXED
  501. temp = (sbr->k[2] << 23) / sbr->kx[1];
  502. while (temp < 0x40000000) {
  503. temp <<= 1;
  504. nz++;
  505. }
  506. temp = fixed_log(temp - 0x80000000);
  507. temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
  508. temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
  509. sbr->n_q = (temp + 0x400000) >> 23;
  510. if (sbr->n_q < 1)
  511. sbr->n_q = 1;
  512. #else
  513. sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
  514. log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
  515. #endif /* USE_FIXED */
  516. if (sbr->n_q > 5) {
  517. av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
  518. return -1;
  519. }
  520. sbr->f_tablenoise[0] = sbr->f_tablelow[0];
  521. temp = 0;
  522. for (k = 1; k <= sbr->n_q; k++) {
  523. temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
  524. sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
  525. }
  526. if (sbr_hf_calc_npatches(ac, sbr) < 0)
  527. return -1;
  528. sbr_make_f_tablelim(sbr);
  529. sbr->data[0].f_indexnoise = 0;
  530. sbr->data[1].f_indexnoise = 0;
  531. return 0;
  532. }
  533. static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
  534. int elements)
  535. {
  536. int i;
  537. for (i = 0; i < elements; i++) {
  538. vec[i] = get_bits1(gb);
  539. }
  540. }
  541. /** ceil(log2(index+1)) */
  542. static const int8_t ceil_log2[] = {
  543. 0, 1, 2, 2, 3, 3,
  544. };
  545. static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
  546. GetBitContext *gb, SBRData *ch_data)
  547. {
  548. int i;
  549. int bs_pointer = 0;
  550. // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
  551. int abs_bord_trail = 16;
  552. int num_rel_lead, num_rel_trail;
  553. unsigned bs_num_env_old = ch_data->bs_num_env;
  554. int bs_frame_class, bs_num_env;
  555. ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
  556. ch_data->bs_amp_res = sbr->bs_amp_res_header;
  557. ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
  558. switch (bs_frame_class = get_bits(gb, 2)) {
  559. case FIXFIX:
  560. bs_num_env = 1 << get_bits(gb, 2);
  561. if (bs_num_env > 4) {
  562. av_log(ac->avctx, AV_LOG_ERROR,
  563. "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
  564. bs_num_env);
  565. return -1;
  566. }
  567. ch_data->bs_num_env = bs_num_env;
  568. num_rel_lead = ch_data->bs_num_env - 1;
  569. if (ch_data->bs_num_env == 1)
  570. ch_data->bs_amp_res = 0;
  571. ch_data->t_env[0] = 0;
  572. ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
  573. abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
  574. ch_data->bs_num_env;
  575. for (i = 0; i < num_rel_lead; i++)
  576. ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
  577. ch_data->bs_freq_res[1] = get_bits1(gb);
  578. for (i = 1; i < ch_data->bs_num_env; i++)
  579. ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
  580. break;
  581. case FIXVAR:
  582. abs_bord_trail += get_bits(gb, 2);
  583. num_rel_trail = get_bits(gb, 2);
  584. ch_data->bs_num_env = num_rel_trail + 1;
  585. ch_data->t_env[0] = 0;
  586. ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
  587. for (i = 0; i < num_rel_trail; i++)
  588. ch_data->t_env[ch_data->bs_num_env - 1 - i] =
  589. ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
  590. bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
  591. for (i = 0; i < ch_data->bs_num_env; i++)
  592. ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
  593. break;
  594. case VARFIX:
  595. ch_data->t_env[0] = get_bits(gb, 2);
  596. num_rel_lead = get_bits(gb, 2);
  597. ch_data->bs_num_env = num_rel_lead + 1;
  598. ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
  599. for (i = 0; i < num_rel_lead; i++)
  600. ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
  601. bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
  602. get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
  603. break;
  604. case VARVAR:
  605. ch_data->t_env[0] = get_bits(gb, 2);
  606. abs_bord_trail += get_bits(gb, 2);
  607. num_rel_lead = get_bits(gb, 2);
  608. num_rel_trail = get_bits(gb, 2);
  609. bs_num_env = num_rel_lead + num_rel_trail + 1;
  610. if (bs_num_env > 5) {
  611. av_log(ac->avctx, AV_LOG_ERROR,
  612. "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
  613. bs_num_env);
  614. return -1;
  615. }
  616. ch_data->bs_num_env = bs_num_env;
  617. ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
  618. for (i = 0; i < num_rel_lead; i++)
  619. ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
  620. for (i = 0; i < num_rel_trail; i++)
  621. ch_data->t_env[ch_data->bs_num_env - 1 - i] =
  622. ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
  623. bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
  624. get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
  625. break;
  626. }
  627. ch_data->bs_frame_class = bs_frame_class;
  628. av_assert0(bs_pointer >= 0);
  629. if (bs_pointer > ch_data->bs_num_env + 1) {
  630. av_log(ac->avctx, AV_LOG_ERROR,
  631. "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
  632. bs_pointer);
  633. return -1;
  634. }
  635. for (i = 1; i <= ch_data->bs_num_env; i++) {
  636. if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
  637. av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
  638. return -1;
  639. }
  640. }
  641. ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
  642. ch_data->t_q[0] = ch_data->t_env[0];
  643. ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
  644. if (ch_data->bs_num_noise > 1) {
  645. int idx;
  646. if (ch_data->bs_frame_class == FIXFIX) {
  647. idx = ch_data->bs_num_env >> 1;
  648. } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
  649. idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
  650. } else { // VARFIX
  651. if (!bs_pointer)
  652. idx = 1;
  653. else if (bs_pointer == 1)
  654. idx = ch_data->bs_num_env - 1;
  655. else // bs_pointer > 1
  656. idx = bs_pointer - 1;
  657. }
  658. ch_data->t_q[1] = ch_data->t_env[idx];
  659. }
  660. ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
  661. ch_data->e_a[1] = -1;
  662. if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
  663. ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
  664. } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
  665. ch_data->e_a[1] = bs_pointer - 1;
  666. return 0;
  667. }
  668. static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
  669. //These variables are saved from the previous frame rather than copied
  670. dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
  671. dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
  672. dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
  673. //These variables are read from the bitstream and therefore copied
  674. memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
  675. memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
  676. memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
  677. dst->bs_num_env = src->bs_num_env;
  678. dst->bs_amp_res = src->bs_amp_res;
  679. dst->bs_num_noise = src->bs_num_noise;
  680. dst->bs_frame_class = src->bs_frame_class;
  681. dst->e_a[1] = src->e_a[1];
  682. }
  683. /// Read how the envelope and noise floor data is delta coded
  684. static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
  685. SBRData *ch_data)
  686. {
  687. get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
  688. get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
  689. }
  690. /// Read inverse filtering data
  691. static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
  692. SBRData *ch_data)
  693. {
  694. int i;
  695. memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
  696. for (i = 0; i < sbr->n_q; i++)
  697. ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
  698. }
  699. static int read_sbr_envelope(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
  700. SBRData *ch_data, int ch)
  701. {
  702. int bits;
  703. int i, j, k;
  704. VLC_TYPE (*t_huff)[2], (*f_huff)[2];
  705. int t_lav, f_lav;
  706. const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
  707. const int odd = sbr->n[1] & 1;
  708. if (sbr->bs_coupling && ch) {
  709. if (ch_data->bs_amp_res) {
  710. bits = 5;
  711. t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
  712. t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
  713. f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
  714. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
  715. } else {
  716. bits = 6;
  717. t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
  718. t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
  719. f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
  720. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
  721. }
  722. } else {
  723. if (ch_data->bs_amp_res) {
  724. bits = 6;
  725. t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
  726. t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
  727. f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
  728. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
  729. } else {
  730. bits = 7;
  731. t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
  732. t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
  733. f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
  734. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
  735. }
  736. }
  737. for (i = 0; i < ch_data->bs_num_env; i++) {
  738. if (ch_data->bs_df_env[i]) {
  739. // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
  740. if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
  741. for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
  742. ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
  743. if (ch_data->env_facs_q[i + 1][j] > 127U) {
  744. av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
  745. return AVERROR_INVALIDDATA;
  746. }
  747. }
  748. } else if (ch_data->bs_freq_res[i + 1]) {
  749. for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
  750. k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
  751. ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
  752. if (ch_data->env_facs_q[i + 1][j] > 127U) {
  753. av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
  754. return AVERROR_INVALIDDATA;
  755. }
  756. }
  757. } else {
  758. for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
  759. k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
  760. ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
  761. if (ch_data->env_facs_q[i + 1][j] > 127U) {
  762. av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
  763. return AVERROR_INVALIDDATA;
  764. }
  765. }
  766. }
  767. } else {
  768. ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
  769. for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
  770. ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
  771. if (ch_data->env_facs_q[i + 1][j] > 127U) {
  772. av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
  773. return AVERROR_INVALIDDATA;
  774. }
  775. }
  776. }
  777. }
  778. //assign 0th elements of env_facs_q from last elements
  779. memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
  780. sizeof(ch_data->env_facs_q[0]));
  781. return 0;
  782. }
  783. static int read_sbr_noise(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
  784. SBRData *ch_data, int ch)
  785. {
  786. int i, j;
  787. VLC_TYPE (*t_huff)[2], (*f_huff)[2];
  788. int t_lav, f_lav;
  789. int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
  790. if (sbr->bs_coupling && ch) {
  791. t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
  792. t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
  793. f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
  794. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
  795. } else {
  796. t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
  797. t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
  798. f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
  799. f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
  800. }
  801. for (i = 0; i < ch_data->bs_num_noise; i++) {
  802. if (ch_data->bs_df_noise[i]) {
  803. for (j = 0; j < sbr->n_q; j++) {
  804. ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
  805. if (ch_data->noise_facs_q[i + 1][j] > 30U) {
  806. av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
  807. return AVERROR_INVALIDDATA;
  808. }
  809. }
  810. } else {
  811. ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
  812. for (j = 1; j < sbr->n_q; j++) {
  813. ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
  814. if (ch_data->noise_facs_q[i + 1][j] > 30U) {
  815. av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
  816. return AVERROR_INVALIDDATA;
  817. }
  818. }
  819. }
  820. }
  821. //assign 0th elements of noise_facs_q from last elements
  822. memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
  823. sizeof(ch_data->noise_facs_q[0]));
  824. return 0;
  825. }
  826. static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
  827. GetBitContext *gb,
  828. int bs_extension_id, int *num_bits_left)
  829. {
  830. switch (bs_extension_id) {
  831. case EXTENSION_ID_PS:
  832. if (!ac->oc[1].m4ac.ps) {
  833. av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
  834. skip_bits_long(gb, *num_bits_left); // bs_fill_bits
  835. *num_bits_left = 0;
  836. } else {
  837. *num_bits_left -= AAC_RENAME(ff_ps_read_data)(ac->avctx, gb, &sbr->ps, *num_bits_left);
  838. ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
  839. }
  840. break;
  841. default:
  842. // some files contain 0-padding
  843. if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
  844. avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
  845. skip_bits_long(gb, *num_bits_left); // bs_fill_bits
  846. *num_bits_left = 0;
  847. break;
  848. }
  849. }
  850. static int read_sbr_single_channel_element(AACContext *ac,
  851. SpectralBandReplication *sbr,
  852. GetBitContext *gb)
  853. {
  854. int ret;
  855. if (get_bits1(gb)) // bs_data_extra
  856. skip_bits(gb, 4); // bs_reserved
  857. if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
  858. return -1;
  859. read_sbr_dtdf(sbr, gb, &sbr->data[0]);
  860. read_sbr_invf(sbr, gb, &sbr->data[0]);
  861. if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  862. return ret;
  863. if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  864. return ret;
  865. if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
  866. get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
  867. return 0;
  868. }
  869. static int read_sbr_channel_pair_element(AACContext *ac,
  870. SpectralBandReplication *sbr,
  871. GetBitContext *gb)
  872. {
  873. int ret;
  874. if (get_bits1(gb)) // bs_data_extra
  875. skip_bits(gb, 8); // bs_reserved
  876. if ((sbr->bs_coupling = get_bits1(gb))) {
  877. if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
  878. return -1;
  879. copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
  880. read_sbr_dtdf(sbr, gb, &sbr->data[0]);
  881. read_sbr_dtdf(sbr, gb, &sbr->data[1]);
  882. read_sbr_invf(sbr, gb, &sbr->data[0]);
  883. memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
  884. memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
  885. if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  886. return ret;
  887. if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  888. return ret;
  889. if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
  890. return ret;
  891. if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
  892. return ret;
  893. } else {
  894. if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
  895. read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
  896. return -1;
  897. read_sbr_dtdf(sbr, gb, &sbr->data[0]);
  898. read_sbr_dtdf(sbr, gb, &sbr->data[1]);
  899. read_sbr_invf(sbr, gb, &sbr->data[0]);
  900. read_sbr_invf(sbr, gb, &sbr->data[1]);
  901. if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  902. return ret;
  903. if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
  904. return ret;
  905. if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
  906. return ret;
  907. if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
  908. return ret;
  909. }
  910. if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
  911. get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
  912. if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
  913. get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
  914. return 0;
  915. }
  916. static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
  917. GetBitContext *gb, int id_aac)
  918. {
  919. unsigned int cnt = get_bits_count(gb);
  920. sbr->id_aac = id_aac;
  921. sbr->ready_for_dequant = 1;
  922. if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
  923. if (read_sbr_single_channel_element(ac, sbr, gb)) {
  924. sbr_turnoff(sbr);
  925. return get_bits_count(gb) - cnt;
  926. }
  927. } else if (id_aac == TYPE_CPE) {
  928. if (read_sbr_channel_pair_element(ac, sbr, gb)) {
  929. sbr_turnoff(sbr);
  930. return get_bits_count(gb) - cnt;
  931. }
  932. } else {
  933. av_log(ac->avctx, AV_LOG_ERROR,
  934. "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
  935. sbr_turnoff(sbr);
  936. return get_bits_count(gb) - cnt;
  937. }
  938. if (get_bits1(gb)) { // bs_extended_data
  939. int num_bits_left = get_bits(gb, 4); // bs_extension_size
  940. if (num_bits_left == 15)
  941. num_bits_left += get_bits(gb, 8); // bs_esc_count
  942. num_bits_left <<= 3;
  943. while (num_bits_left > 7) {
  944. num_bits_left -= 2;
  945. read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
  946. }
  947. if (num_bits_left < 0) {
  948. av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
  949. }
  950. if (num_bits_left > 0)
  951. skip_bits(gb, num_bits_left);
  952. }
  953. return get_bits_count(gb) - cnt;
  954. }
  955. static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
  956. {
  957. int err;
  958. err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
  959. if (err >= 0)
  960. err = sbr_make_f_derived(ac, sbr);
  961. if (err < 0) {
  962. av_log(ac->avctx, AV_LOG_ERROR,
  963. "SBR reset failed. Switching SBR to pure upsampling mode.\n");
  964. sbr_turnoff(sbr);
  965. }
  966. }
  967. /**
  968. * Decode Spectral Band Replication extension data; reference: table 4.55.
  969. *
  970. * @param crc flag indicating the presence of CRC checksum
  971. * @param cnt length of TYPE_FIL syntactic element in bytes
  972. *
  973. * @return Returns number of bytes consumed from the TYPE_FIL element.
  974. */
  975. int AAC_RENAME(ff_decode_sbr_extension)(AACContext *ac, SpectralBandReplication *sbr,
  976. GetBitContext *gb_host, int crc, int cnt, int id_aac)
  977. {
  978. unsigned int num_sbr_bits = 0, num_align_bits;
  979. unsigned bytes_read;
  980. GetBitContext gbc = *gb_host, *gb = &gbc;
  981. skip_bits_long(gb_host, cnt*8 - 4);
  982. sbr->reset = 0;
  983. if (!sbr->sample_rate)
  984. sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
  985. if (!ac->oc[1].m4ac.ext_sample_rate)
  986. ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
  987. if (crc) {
  988. skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
  989. num_sbr_bits += 10;
  990. }
  991. //Save some state from the previous frame.
  992. sbr->kx[0] = sbr->kx[1];
  993. sbr->m[0] = sbr->m[1];
  994. sbr->kx_and_m_pushed = 1;
  995. num_sbr_bits++;
  996. if (get_bits1(gb)) // bs_header_flag
  997. num_sbr_bits += read_sbr_header(sbr, gb);
  998. if (sbr->reset)
  999. sbr_reset(ac, sbr);
  1000. if (sbr->start)
  1001. num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
  1002. num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
  1003. bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
  1004. if (bytes_read > cnt) {
  1005. av_log(ac->avctx, AV_LOG_ERROR,
  1006. "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
  1007. sbr_turnoff(sbr);
  1008. }
  1009. return cnt;
  1010. }
  1011. /**
  1012. * Analysis QMF Bank (14496-3 sp04 p206)
  1013. *
  1014. * @param x pointer to the beginning of the first sample window
  1015. * @param W array of complex-valued samples split into subbands
  1016. */
  1017. #ifndef sbr_qmf_analysis
  1018. #if USE_FIXED
  1019. static void sbr_qmf_analysis(AVFixedDSPContext *dsp, FFTContext *mdct,
  1020. #else
  1021. static void sbr_qmf_analysis(AVFloatDSPContext *dsp, FFTContext *mdct,
  1022. #endif /* USE_FIXED */
  1023. SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
  1024. INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
  1025. {
  1026. int i;
  1027. #if USE_FIXED
  1028. int j;
  1029. #endif
  1030. memcpy(x , x+1024, (320-32)*sizeof(x[0]));
  1031. memcpy(x+288, in, 1024*sizeof(x[0]));
  1032. for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
  1033. // are not supported
  1034. dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
  1035. sbrdsp->sum64x5(z);
  1036. sbrdsp->qmf_pre_shuffle(z);
  1037. #if USE_FIXED
  1038. for (j = 64; j < 128; j++) {
  1039. if (z[j] > 1<<24) {
  1040. av_log(NULL, AV_LOG_WARNING,
  1041. "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
  1042. z[j], 1<<24);
  1043. z[j] = 1<<24;
  1044. } else if (z[j] < -(1<<24)) {
  1045. av_log(NULL, AV_LOG_WARNING,
  1046. "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
  1047. z[j], -(1<<24));
  1048. z[j] = -(1<<24);
  1049. }
  1050. }
  1051. #endif
  1052. mdct->imdct_half(mdct, z, z+64);
  1053. sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
  1054. x += 32;
  1055. }
  1056. }
  1057. #endif
  1058. /**
  1059. * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
  1060. * (14496-3 sp04 p206)
  1061. */
  1062. #ifndef sbr_qmf_synthesis
  1063. static void sbr_qmf_synthesis(FFTContext *mdct,
  1064. #if USE_FIXED
  1065. SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
  1066. #else
  1067. SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
  1068. #endif /* USE_FIXED */
  1069. INTFLOAT *out, INTFLOAT X[2][38][64],
  1070. INTFLOAT mdct_buf[2][64],
  1071. INTFLOAT *v0, int *v_off, const unsigned int div)
  1072. {
  1073. int i, n;
  1074. const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
  1075. const int step = 128 >> div;
  1076. INTFLOAT *v;
  1077. for (i = 0; i < 32; i++) {
  1078. if (*v_off < step) {
  1079. int saved_samples = (1280 - 128) >> div;
  1080. memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
  1081. *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
  1082. } else {
  1083. *v_off -= step;
  1084. }
  1085. v = v0 + *v_off;
  1086. if (div) {
  1087. for (n = 0; n < 32; n++) {
  1088. X[0][i][ n] = -X[0][i][n];
  1089. X[0][i][32+n] = X[1][i][31-n];
  1090. }
  1091. mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
  1092. sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
  1093. } else {
  1094. sbrdsp->neg_odd_64(X[1][i]);
  1095. mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
  1096. mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
  1097. sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
  1098. }
  1099. dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
  1100. dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
  1101. dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
  1102. dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
  1103. dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
  1104. dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
  1105. dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
  1106. dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
  1107. dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
  1108. dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
  1109. out += 64 >> div;
  1110. }
  1111. }
  1112. #endif
  1113. /// Generate the subband filtered lowband
  1114. static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
  1115. INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
  1116. int buf_idx)
  1117. {
  1118. int i, k;
  1119. const int t_HFGen = 8;
  1120. const int i_f = 32;
  1121. memset(X_low, 0, 32*sizeof(*X_low));
  1122. for (k = 0; k < sbr->kx[1]; k++) {
  1123. for (i = t_HFGen; i < i_f + t_HFGen; i++) {
  1124. X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
  1125. X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
  1126. }
  1127. }
  1128. buf_idx = 1-buf_idx;
  1129. for (k = 0; k < sbr->kx[0]; k++) {
  1130. for (i = 0; i < t_HFGen; i++) {
  1131. X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
  1132. X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. /// High Frequency Generator (14496-3 sp04 p215)
  1138. static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
  1139. INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
  1140. const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
  1141. const INTFLOAT bw_array[5], const uint8_t *t_env,
  1142. int bs_num_env)
  1143. {
  1144. int j, x;
  1145. int g = 0;
  1146. int k = sbr->kx[1];
  1147. for (j = 0; j < sbr->num_patches; j++) {
  1148. for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
  1149. const int p = sbr->patch_start_subband[j] + x;
  1150. while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
  1151. g++;
  1152. g--;
  1153. if (g < 0) {
  1154. av_log(ac->avctx, AV_LOG_ERROR,
  1155. "ERROR : no subband found for frequency %d\n", k);
  1156. return -1;
  1157. }
  1158. sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
  1159. X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
  1160. alpha0[p], alpha1[p], bw_array[g],
  1161. 2 * t_env[0], 2 * t_env[bs_num_env]);
  1162. }
  1163. }
  1164. if (k < sbr->m[1] + sbr->kx[1])
  1165. memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
  1166. return 0;
  1167. }
  1168. /// Generate the subband filtered lowband
  1169. static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
  1170. const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
  1171. const INTFLOAT X_low[32][40][2], int ch)
  1172. {
  1173. int k, i;
  1174. const int i_f = 32;
  1175. const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
  1176. memset(X, 0, 2*sizeof(*X));
  1177. for (k = 0; k < sbr->kx[0]; k++) {
  1178. for (i = 0; i < i_Temp; i++) {
  1179. X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
  1180. X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
  1181. }
  1182. }
  1183. for (; k < sbr->kx[0] + sbr->m[0]; k++) {
  1184. for (i = 0; i < i_Temp; i++) {
  1185. X[0][i][k] = Y0[i + i_f][k][0];
  1186. X[1][i][k] = Y0[i + i_f][k][1];
  1187. }
  1188. }
  1189. for (k = 0; k < sbr->kx[1]; k++) {
  1190. for (i = i_Temp; i < 38; i++) {
  1191. X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
  1192. X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
  1193. }
  1194. }
  1195. for (; k < sbr->kx[1] + sbr->m[1]; k++) {
  1196. for (i = i_Temp; i < i_f; i++) {
  1197. X[0][i][k] = Y1[i][k][0];
  1198. X[1][i][k] = Y1[i][k][1];
  1199. }
  1200. }
  1201. return 0;
  1202. }
  1203. /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
  1204. * (14496-3 sp04 p217)
  1205. */
  1206. static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
  1207. SBRData *ch_data, int e_a[2])
  1208. {
  1209. int e, i, m;
  1210. memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
  1211. for (e = 0; e < ch_data->bs_num_env; e++) {
  1212. const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
  1213. uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
  1214. int k;
  1215. if (sbr->kx[1] != table[0]) {
  1216. av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
  1217. "Derived frequency tables were not regenerated.\n");
  1218. sbr_turnoff(sbr);
  1219. return AVERROR_BUG;
  1220. }
  1221. for (i = 0; i < ilim; i++)
  1222. for (m = table[i]; m < table[i + 1]; m++)
  1223. sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
  1224. // ch_data->bs_num_noise > 1 => 2 noise floors
  1225. k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
  1226. for (i = 0; i < sbr->n_q; i++)
  1227. for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
  1228. sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
  1229. for (i = 0; i < sbr->n[1]; i++) {
  1230. if (ch_data->bs_add_harmonic_flag) {
  1231. const unsigned int m_midpoint =
  1232. (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
  1233. ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
  1234. (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
  1235. }
  1236. }
  1237. for (i = 0; i < ilim; i++) {
  1238. int additional_sinusoid_present = 0;
  1239. for (m = table[i]; m < table[i + 1]; m++) {
  1240. if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
  1241. additional_sinusoid_present = 1;
  1242. break;
  1243. }
  1244. }
  1245. memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
  1246. (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
  1247. }
  1248. }
  1249. memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
  1250. return 0;
  1251. }
  1252. /// Estimation of current envelope (14496-3 sp04 p218)
  1253. static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
  1254. SpectralBandReplication *sbr, SBRData *ch_data)
  1255. {
  1256. int e, m;
  1257. int kx1 = sbr->kx[1];
  1258. if (sbr->bs_interpol_freq) {
  1259. for (e = 0; e < ch_data->bs_num_env; e++) {
  1260. #if USE_FIXED
  1261. const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
  1262. #else
  1263. const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
  1264. #endif /* USE_FIXED */
  1265. int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
  1266. int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
  1267. for (m = 0; m < sbr->m[1]; m++) {
  1268. AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
  1269. #if USE_FIXED
  1270. e_curr[e][m] = av_mul_sf(sum, recip_env_size);
  1271. #else
  1272. e_curr[e][m] = sum * recip_env_size;
  1273. #endif /* USE_FIXED */
  1274. }
  1275. }
  1276. } else {
  1277. int k, p;
  1278. for (e = 0; e < ch_data->bs_num_env; e++) {
  1279. const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
  1280. int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
  1281. int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
  1282. const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
  1283. for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
  1284. #if USE_FIXED
  1285. SoftFloat sum = FLOAT_0;
  1286. const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
  1287. for (k = table[p]; k < table[p + 1]; k++) {
  1288. sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
  1289. }
  1290. sum = av_mul_sf(sum, den);
  1291. #else
  1292. float sum = 0.0f;
  1293. const int den = env_size * (table[p + 1] - table[p]);
  1294. for (k = table[p]; k < table[p + 1]; k++) {
  1295. sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
  1296. }
  1297. sum /= den;
  1298. #endif /* USE_FIXED */
  1299. for (k = table[p]; k < table[p + 1]; k++) {
  1300. e_curr[e][k - kx1] = sum;
  1301. }
  1302. }
  1303. }
  1304. }
  1305. }
  1306. void AAC_RENAME(ff_sbr_apply)(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
  1307. INTFLOAT* L, INTFLOAT* R)
  1308. {
  1309. int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
  1310. int ch;
  1311. int nch = (id_aac == TYPE_CPE) ? 2 : 1;
  1312. int err;
  1313. if (id_aac != sbr->id_aac) {
  1314. av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
  1315. "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
  1316. sbr_turnoff(sbr);
  1317. }
  1318. if (sbr->start && !sbr->ready_for_dequant) {
  1319. av_log(ac->avctx, AV_LOG_ERROR,
  1320. "No quantized data read for sbr_dequant.\n");
  1321. sbr_turnoff(sbr);
  1322. }
  1323. if (!sbr->kx_and_m_pushed) {
  1324. sbr->kx[0] = sbr->kx[1];
  1325. sbr->m[0] = sbr->m[1];
  1326. } else {
  1327. sbr->kx_and_m_pushed = 0;
  1328. }
  1329. if (sbr->start) {
  1330. sbr_dequant(sbr, id_aac);
  1331. sbr->ready_for_dequant = 0;
  1332. }
  1333. for (ch = 0; ch < nch; ch++) {
  1334. /* decode channel */
  1335. sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
  1336. (INTFLOAT*)sbr->qmf_filter_scratch,
  1337. sbr->data[ch].W, sbr->data[ch].Ypos);
  1338. sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
  1339. (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
  1340. sbr->data[ch].Ypos);
  1341. sbr->data[ch].Ypos ^= 1;
  1342. if (sbr->start) {
  1343. sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
  1344. (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
  1345. sbr_chirp(sbr, &sbr->data[ch]);
  1346. av_assert0(sbr->data[ch].bs_num_env > 0);
  1347. sbr_hf_gen(ac, sbr, sbr->X_high,
  1348. (const INTFLOAT (*)[40][2]) sbr->X_low,
  1349. (const INTFLOAT (*)[2]) sbr->alpha0,
  1350. (const INTFLOAT (*)[2]) sbr->alpha1,
  1351. sbr->data[ch].bw_array, sbr->data[ch].t_env,
  1352. sbr->data[ch].bs_num_env);
  1353. // hf_adj
  1354. err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
  1355. if (!err) {
  1356. sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
  1357. sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
  1358. sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
  1359. (const INTFLOAT (*)[40][2]) sbr->X_high,
  1360. sbr, &sbr->data[ch],
  1361. sbr->data[ch].e_a);
  1362. }
  1363. }
  1364. /* synthesis */
  1365. sbr->c.sbr_x_gen(sbr, sbr->X[ch],
  1366. (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
  1367. (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
  1368. (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
  1369. }
  1370. if (ac->oc[1].m4ac.ps == 1) {
  1371. if (sbr->ps.start) {
  1372. AAC_RENAME(ff_ps_apply)(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
  1373. } else {
  1374. memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
  1375. }
  1376. nch = 2;
  1377. }
  1378. sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
  1379. L, sbr->X[0], sbr->qmf_filter_scratch,
  1380. sbr->data[0].synthesis_filterbank_samples,
  1381. &sbr->data[0].synthesis_filterbank_samples_offset,
  1382. downsampled);
  1383. if (nch == 2)
  1384. sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
  1385. R, sbr->X[1], sbr->qmf_filter_scratch,
  1386. sbr->data[1].synthesis_filterbank_samples,
  1387. &sbr->data[1].synthesis_filterbank_samples_offset,
  1388. downsampled);
  1389. }
  1390. static void aacsbr_func_ptr_init(AACSBRContext *c)
  1391. {
  1392. c->sbr_lf_gen = sbr_lf_gen;
  1393. c->sbr_hf_assemble = sbr_hf_assemble;
  1394. c->sbr_x_gen = sbr_x_gen;
  1395. c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
  1396. #if !USE_FIXED
  1397. if(ARCH_MIPS)
  1398. ff_aacsbr_func_ptr_init_mips(c);
  1399. #endif
  1400. }