aacps.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * MPEG-4 Parametric Stereo decoding functions
  3. * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
  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. * Note: Rounding-to-nearest used unless otherwise stated
  22. *
  23. */
  24. #include <stdint.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/mathematics.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "aacps.h"
  30. #if USE_FIXED
  31. #include "aacps_fixed_tablegen.h"
  32. #else
  33. #include "libavutil/internal.h"
  34. #include "aacps_tablegen.h"
  35. #endif /* USE_FIXED */
  36. #include "aacpsdata.c"
  37. #define PS_BASELINE 0 ///< Operate in Baseline PS mode
  38. ///< Baseline implies 10 or 20 stereo bands,
  39. ///< mixing mode A, and no ipd/opd
  40. #define numQMFSlots 32 //numTimeSlots * RATE
  41. static const int8_t num_env_tab[2][4] = {
  42. { 0, 1, 2, 4, },
  43. { 1, 2, 3, 4, },
  44. };
  45. static const int8_t nr_iidicc_par_tab[] = {
  46. 10, 20, 34, 10, 20, 34,
  47. };
  48. static const int8_t nr_iidopd_par_tab[] = {
  49. 5, 11, 17, 5, 11, 17,
  50. };
  51. enum {
  52. huff_iid_df1,
  53. huff_iid_dt1,
  54. huff_iid_df0,
  55. huff_iid_dt0,
  56. huff_icc_df,
  57. huff_icc_dt,
  58. huff_ipd_df,
  59. huff_ipd_dt,
  60. huff_opd_df,
  61. huff_opd_dt,
  62. };
  63. static const int huff_iid[] = {
  64. huff_iid_df0,
  65. huff_iid_df1,
  66. huff_iid_dt0,
  67. huff_iid_dt1,
  68. };
  69. static VLC vlc_ps[10];
  70. #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
  71. /** \
  72. * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
  73. * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
  74. * bitstream. \
  75. * \
  76. * @param avctx contains the current codec context \
  77. * @param gb pointer to the input bitstream \
  78. * @param ps pointer to the Parametric Stereo context \
  79. * @param PAR pointer to the parameter to be read \
  80. * @param e envelope to decode \
  81. * @param dt 1: time delta-coded, 0: frequency delta-coded \
  82. */ \
  83. static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
  84. int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
  85. { \
  86. int b, num = ps->nr_ ## PAR ## _par; \
  87. VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
  88. if (dt) { \
  89. int e_prev = e ? e - 1 : ps->num_env_old - 1; \
  90. e_prev = FFMAX(e_prev, 0); \
  91. for (b = 0; b < num; b++) { \
  92. int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
  93. if (MASK) val &= MASK; \
  94. PAR[e][b] = val; \
  95. if (ERR_CONDITION) \
  96. goto err; \
  97. } \
  98. } else { \
  99. int val = 0; \
  100. for (b = 0; b < num; b++) { \
  101. val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
  102. if (MASK) val &= MASK; \
  103. PAR[e][b] = val; \
  104. if (ERR_CONDITION) \
  105. goto err; \
  106. } \
  107. } \
  108. return 0; \
  109. err: \
  110. av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
  111. return -1; \
  112. }
  113. READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
  114. READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
  115. READ_PAR_DATA(ipdopd, 0, 0x07, 0)
  116. static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
  117. {
  118. int e;
  119. int count = get_bits_count(gb);
  120. if (ps_extension_id)
  121. return 0;
  122. ps->enable_ipdopd = get_bits1(gb);
  123. if (ps->enable_ipdopd) {
  124. for (e = 0; e < ps->num_env; e++) {
  125. int dt = get_bits1(gb);
  126. read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
  127. dt = get_bits1(gb);
  128. read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
  129. }
  130. }
  131. skip_bits1(gb); //reserved_ps
  132. return get_bits_count(gb) - count;
  133. }
  134. static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
  135. {
  136. int i;
  137. for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
  138. opd_hist[i] = 0;
  139. ipd_hist[i] = 0;
  140. }
  141. }
  142. int AAC_RENAME(ff_ps_read_data)(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
  143. {
  144. int e;
  145. int bit_count_start = get_bits_count(gb_host);
  146. int header;
  147. int bits_consumed;
  148. GetBitContext gbc = *gb_host, *gb = &gbc;
  149. header = get_bits1(gb);
  150. if (header) { //enable_ps_header
  151. ps->enable_iid = get_bits1(gb);
  152. if (ps->enable_iid) {
  153. int iid_mode = get_bits(gb, 3);
  154. if (iid_mode > 5) {
  155. av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
  156. iid_mode);
  157. goto err;
  158. }
  159. ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
  160. ps->iid_quant = iid_mode > 2;
  161. ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
  162. }
  163. ps->enable_icc = get_bits1(gb);
  164. if (ps->enable_icc) {
  165. ps->icc_mode = get_bits(gb, 3);
  166. if (ps->icc_mode > 5) {
  167. av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
  168. ps->icc_mode);
  169. goto err;
  170. }
  171. ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
  172. }
  173. ps->enable_ext = get_bits1(gb);
  174. }
  175. ps->frame_class = get_bits1(gb);
  176. ps->num_env_old = ps->num_env;
  177. ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
  178. ps->border_position[0] = -1;
  179. if (ps->frame_class) {
  180. for (e = 1; e <= ps->num_env; e++) {
  181. ps->border_position[e] = get_bits(gb, 5);
  182. if (ps->border_position[e] < ps->border_position[e-1]) {
  183. av_log(avctx, AV_LOG_ERROR, "border_position non monotone.\n");
  184. goto err;
  185. }
  186. }
  187. } else
  188. for (e = 1; e <= ps->num_env; e++)
  189. ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
  190. if (ps->enable_iid) {
  191. for (e = 0; e < ps->num_env; e++) {
  192. int dt = get_bits1(gb);
  193. if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
  194. goto err;
  195. }
  196. } else
  197. memset(ps->iid_par, 0, sizeof(ps->iid_par));
  198. if (ps->enable_icc)
  199. for (e = 0; e < ps->num_env; e++) {
  200. int dt = get_bits1(gb);
  201. if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
  202. goto err;
  203. }
  204. else
  205. memset(ps->icc_par, 0, sizeof(ps->icc_par));
  206. if (ps->enable_ext) {
  207. int cnt = get_bits(gb, 4);
  208. if (cnt == 15) {
  209. cnt += get_bits(gb, 8);
  210. }
  211. cnt *= 8;
  212. while (cnt > 7) {
  213. int ps_extension_id = get_bits(gb, 2);
  214. cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
  215. }
  216. if (cnt < 0) {
  217. av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
  218. goto err;
  219. }
  220. skip_bits(gb, cnt);
  221. }
  222. ps->enable_ipdopd &= !PS_BASELINE;
  223. //Fix up envelopes
  224. if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
  225. //Create a fake envelope
  226. int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
  227. int b;
  228. if (source >= 0 && source != ps->num_env) {
  229. if (ps->enable_iid) {
  230. memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
  231. }
  232. if (ps->enable_icc) {
  233. memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
  234. }
  235. if (ps->enable_ipdopd) {
  236. memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
  237. memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
  238. }
  239. }
  240. if (ps->enable_iid){
  241. for (b = 0; b < ps->nr_iid_par; b++) {
  242. if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
  243. av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
  244. goto err;
  245. }
  246. }
  247. }
  248. if (ps->enable_icc){
  249. for (b = 0; b < ps->nr_iid_par; b++) {
  250. if (ps->icc_par[ps->num_env][b] > 7U) {
  251. av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
  252. goto err;
  253. }
  254. }
  255. }
  256. ps->num_env++;
  257. ps->border_position[ps->num_env] = numQMFSlots - 1;
  258. }
  259. ps->is34bands_old = ps->is34bands;
  260. if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
  261. ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
  262. (ps->enable_icc && ps->nr_icc_par == 34);
  263. //Baseline
  264. if (!ps->enable_ipdopd) {
  265. memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
  266. memset(ps->opd_par, 0, sizeof(ps->opd_par));
  267. }
  268. if (header)
  269. ps->start = 1;
  270. bits_consumed = get_bits_count(gb) - bit_count_start;
  271. if (bits_consumed <= bits_left) {
  272. skip_bits_long(gb_host, bits_consumed);
  273. return bits_consumed;
  274. }
  275. av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
  276. err:
  277. ps->start = 0;
  278. skip_bits_long(gb_host, bits_left);
  279. memset(ps->iid_par, 0, sizeof(ps->iid_par));
  280. memset(ps->icc_par, 0, sizeof(ps->icc_par));
  281. memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
  282. memset(ps->opd_par, 0, sizeof(ps->opd_par));
  283. return bits_left;
  284. }
  285. /** Split one subband into 2 subsubbands with a symmetric real filter.
  286. * The filter must have its non-center even coefficients equal to zero. */
  287. static void hybrid2_re(INTFLOAT (*in)[2], INTFLOAT (*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
  288. {
  289. int i, j;
  290. for (i = 0; i < len; i++, in++) {
  291. INT64FLOAT re_in = AAC_MUL31(filter[6], in[6][0]); //real inphase
  292. INT64FLOAT re_op = 0.0f; //real out of phase
  293. INT64FLOAT im_in = AAC_MUL31(filter[6], in[6][1]); //imag inphase
  294. INT64FLOAT im_op = 0.0f; //imag out of phase
  295. for (j = 0; j < 6; j += 2) {
  296. re_op += (INT64FLOAT)filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
  297. im_op += (INT64FLOAT)filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
  298. }
  299. #if USE_FIXED
  300. re_op = (re_op + 0x40000000) >> 31;
  301. im_op = (im_op + 0x40000000) >> 31;
  302. #endif /* USE_FIXED */
  303. out[ reverse][i][0] = (INTFLOAT)(re_in + re_op);
  304. out[ reverse][i][1] = (INTFLOAT)(im_in + im_op);
  305. out[!reverse][i][0] = (INTFLOAT)(re_in - re_op);
  306. out[!reverse][i][1] = (INTFLOAT)(im_in - im_op);
  307. }
  308. }
  309. /** Split one subband into 6 subsubbands with a complex filter */
  310. static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
  311. TABLE_CONST INTFLOAT (*filter)[8][2], int len)
  312. {
  313. int i;
  314. int N = 8;
  315. LOCAL_ALIGNED_16(INTFLOAT, temp, [8], [2]);
  316. for (i = 0; i < len; i++, in++) {
  317. dsp->hybrid_analysis(temp, in, (const INTFLOAT (*)[8][2]) filter, 1, N);
  318. out[0][i][0] = temp[6][0];
  319. out[0][i][1] = temp[6][1];
  320. out[1][i][0] = temp[7][0];
  321. out[1][i][1] = temp[7][1];
  322. out[2][i][0] = temp[0][0];
  323. out[2][i][1] = temp[0][1];
  324. out[3][i][0] = temp[1][0];
  325. out[3][i][1] = temp[1][1];
  326. out[4][i][0] = temp[2][0] + temp[5][0];
  327. out[4][i][1] = temp[2][1] + temp[5][1];
  328. out[5][i][0] = temp[3][0] + temp[4][0];
  329. out[5][i][1] = temp[3][1] + temp[4][1];
  330. }
  331. }
  332. static void hybrid4_8_12_cx(PSDSPContext *dsp,
  333. INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
  334. TABLE_CONST INTFLOAT (*filter)[8][2], int N, int len)
  335. {
  336. int i;
  337. for (i = 0; i < len; i++, in++) {
  338. dsp->hybrid_analysis(out[0] + i, in, (const INTFLOAT (*)[8][2]) filter, 32, N);
  339. }
  340. }
  341. static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2],
  342. INTFLOAT in[5][44][2], INTFLOAT L[2][38][64],
  343. int is34, int len)
  344. {
  345. int i, j;
  346. for (i = 0; i < 5; i++) {
  347. for (j = 0; j < 38; j++) {
  348. in[i][j+6][0] = L[0][j][i];
  349. in[i][j+6][1] = L[1][j][i];
  350. }
  351. }
  352. if (is34) {
  353. hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
  354. hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
  355. hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
  356. hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
  357. hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
  358. dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
  359. } else {
  360. hybrid6_cx(dsp, in[0], out, f20_0_8, len);
  361. hybrid2_re(in[1], out+6, g1_Q2, len, 1);
  362. hybrid2_re(in[2], out+8, g1_Q2, len, 0);
  363. dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
  364. }
  365. //update in_buf
  366. for (i = 0; i < 5; i++) {
  367. memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
  368. }
  369. }
  370. static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64],
  371. INTFLOAT in[91][32][2], int is34, int len)
  372. {
  373. int i, n;
  374. if (is34) {
  375. for (n = 0; n < len; n++) {
  376. memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
  377. memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
  378. for (i = 0; i < 12; i++) {
  379. out[0][n][0] += (UINTFLOAT)in[ i][n][0];
  380. out[1][n][0] += (UINTFLOAT)in[ i][n][1];
  381. }
  382. for (i = 0; i < 8; i++) {
  383. out[0][n][1] += (UINTFLOAT)in[12+i][n][0];
  384. out[1][n][1] += (UINTFLOAT)in[12+i][n][1];
  385. }
  386. for (i = 0; i < 4; i++) {
  387. out[0][n][2] += (UINTFLOAT)in[20+i][n][0];
  388. out[1][n][2] += (UINTFLOAT)in[20+i][n][1];
  389. out[0][n][3] += (UINTFLOAT)in[24+i][n][0];
  390. out[1][n][3] += (UINTFLOAT)in[24+i][n][1];
  391. out[0][n][4] += (UINTFLOAT)in[28+i][n][0];
  392. out[1][n][4] += (UINTFLOAT)in[28+i][n][1];
  393. }
  394. }
  395. dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
  396. } else {
  397. for (n = 0; n < len; n++) {
  398. out[0][n][0] = (UINTFLOAT)in[0][n][0] + in[1][n][0] + in[2][n][0] +
  399. (UINTFLOAT)in[3][n][0] + in[4][n][0] + in[5][n][0];
  400. out[1][n][0] = (UINTFLOAT)in[0][n][1] + in[1][n][1] + in[2][n][1] +
  401. (UINTFLOAT)in[3][n][1] + in[4][n][1] + in[5][n][1];
  402. out[0][n][1] = (UINTFLOAT)in[6][n][0] + in[7][n][0];
  403. out[1][n][1] = (UINTFLOAT)in[6][n][1] + in[7][n][1];
  404. out[0][n][2] = (UINTFLOAT)in[8][n][0] + in[9][n][0];
  405. out[1][n][2] = (UINTFLOAT)in[8][n][1] + in[9][n][1];
  406. }
  407. dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
  408. }
  409. }
  410. /// All-pass filter decay slope
  411. #define DECAY_SLOPE Q30(0.05f)
  412. /// Number of frequency bands that can be addressed by the parameter index, b(k)
  413. static const int NR_PAR_BANDS[] = { 20, 34 };
  414. static const int NR_IPDOPD_BANDS[] = { 11, 17 };
  415. /// Number of frequency bands that can be addressed by the sub subband index, k
  416. static const int NR_BANDS[] = { 71, 91 };
  417. /// Start frequency band for the all-pass filter decay slope
  418. static const int DECAY_CUTOFF[] = { 10, 32 };
  419. /// Number of all-pass filer bands
  420. static const int NR_ALLPASS_BANDS[] = { 30, 50 };
  421. /// First stereo band using the short one sample delay
  422. static const int SHORT_DELAY_BAND[] = { 42, 62 };
  423. /** Table 8.46 */
  424. static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
  425. {
  426. int b;
  427. if (full)
  428. b = 9;
  429. else {
  430. b = 4;
  431. par_mapped[10] = 0;
  432. }
  433. for (; b >= 0; b--) {
  434. par_mapped[2*b+1] = par_mapped[2*b] = par[b];
  435. }
  436. }
  437. static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
  438. {
  439. par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
  440. par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
  441. par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
  442. par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
  443. par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
  444. par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
  445. par_mapped[ 6] = par[10];
  446. par_mapped[ 7] = par[11];
  447. par_mapped[ 8] = ( par[12] + par[13]) / 2;
  448. par_mapped[ 9] = ( par[14] + par[15]) / 2;
  449. par_mapped[10] = par[16];
  450. if (full) {
  451. par_mapped[11] = par[17];
  452. par_mapped[12] = par[18];
  453. par_mapped[13] = par[19];
  454. par_mapped[14] = ( par[20] + par[21]) / 2;
  455. par_mapped[15] = ( par[22] + par[23]) / 2;
  456. par_mapped[16] = ( par[24] + par[25]) / 2;
  457. par_mapped[17] = ( par[26] + par[27]) / 2;
  458. par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
  459. par_mapped[19] = ( par[32] + par[33]) / 2;
  460. }
  461. }
  462. static void map_val_34_to_20(INTFLOAT par[PS_MAX_NR_IIDICC])
  463. {
  464. #if USE_FIXED
  465. par[ 0] = (int)(((int64_t)(par[ 0] + (unsigned)(par[ 1]>>1)) * 1431655765 + \
  466. 0x40000000) >> 31);
  467. par[ 1] = (int)(((int64_t)((par[ 1]>>1) + (unsigned)par[ 2]) * 1431655765 + \
  468. 0x40000000) >> 31);
  469. par[ 2] = (int)(((int64_t)(par[ 3] + (unsigned)(par[ 4]>>1)) * 1431655765 + \
  470. 0x40000000) >> 31);
  471. par[ 3] = (int)(((int64_t)((par[ 4]>>1) + (unsigned)par[ 5]) * 1431655765 + \
  472. 0x40000000) >> 31);
  473. #else
  474. par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
  475. par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
  476. par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
  477. par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
  478. #endif /* USE_FIXED */
  479. par[ 4] = AAC_HALF_SUM(par[ 6], par[ 7]);
  480. par[ 5] = AAC_HALF_SUM(par[ 8], par[ 9]);
  481. par[ 6] = par[10];
  482. par[ 7] = par[11];
  483. par[ 8] = AAC_HALF_SUM(par[12], par[13]);
  484. par[ 9] = AAC_HALF_SUM(par[14], par[15]);
  485. par[10] = par[16];
  486. par[11] = par[17];
  487. par[12] = par[18];
  488. par[13] = par[19];
  489. par[14] = AAC_HALF_SUM(par[20], par[21]);
  490. par[15] = AAC_HALF_SUM(par[22], par[23]);
  491. par[16] = AAC_HALF_SUM(par[24], par[25]);
  492. par[17] = AAC_HALF_SUM(par[26], par[27]);
  493. #if USE_FIXED
  494. par[18] = (((par[28]+2)>>2) + ((par[29]+2)>>2) + ((par[30]+2)>>2) + ((par[31]+2)>>2));
  495. #else
  496. par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
  497. #endif /* USE_FIXED */
  498. par[19] = AAC_HALF_SUM(par[32], par[33]);
  499. }
  500. static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
  501. {
  502. if (full) {
  503. par_mapped[33] = par[9];
  504. par_mapped[32] = par[9];
  505. par_mapped[31] = par[9];
  506. par_mapped[30] = par[9];
  507. par_mapped[29] = par[9];
  508. par_mapped[28] = par[9];
  509. par_mapped[27] = par[8];
  510. par_mapped[26] = par[8];
  511. par_mapped[25] = par[8];
  512. par_mapped[24] = par[8];
  513. par_mapped[23] = par[7];
  514. par_mapped[22] = par[7];
  515. par_mapped[21] = par[7];
  516. par_mapped[20] = par[7];
  517. par_mapped[19] = par[6];
  518. par_mapped[18] = par[6];
  519. par_mapped[17] = par[5];
  520. par_mapped[16] = par[5];
  521. } else {
  522. par_mapped[16] = 0;
  523. }
  524. par_mapped[15] = par[4];
  525. par_mapped[14] = par[4];
  526. par_mapped[13] = par[4];
  527. par_mapped[12] = par[4];
  528. par_mapped[11] = par[3];
  529. par_mapped[10] = par[3];
  530. par_mapped[ 9] = par[2];
  531. par_mapped[ 8] = par[2];
  532. par_mapped[ 7] = par[2];
  533. par_mapped[ 6] = par[2];
  534. par_mapped[ 5] = par[1];
  535. par_mapped[ 4] = par[1];
  536. par_mapped[ 3] = par[1];
  537. par_mapped[ 2] = par[0];
  538. par_mapped[ 1] = par[0];
  539. par_mapped[ 0] = par[0];
  540. }
  541. static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
  542. {
  543. if (full) {
  544. par_mapped[33] = par[19];
  545. par_mapped[32] = par[19];
  546. par_mapped[31] = par[18];
  547. par_mapped[30] = par[18];
  548. par_mapped[29] = par[18];
  549. par_mapped[28] = par[18];
  550. par_mapped[27] = par[17];
  551. par_mapped[26] = par[17];
  552. par_mapped[25] = par[16];
  553. par_mapped[24] = par[16];
  554. par_mapped[23] = par[15];
  555. par_mapped[22] = par[15];
  556. par_mapped[21] = par[14];
  557. par_mapped[20] = par[14];
  558. par_mapped[19] = par[13];
  559. par_mapped[18] = par[12];
  560. par_mapped[17] = par[11];
  561. }
  562. par_mapped[16] = par[10];
  563. par_mapped[15] = par[ 9];
  564. par_mapped[14] = par[ 9];
  565. par_mapped[13] = par[ 8];
  566. par_mapped[12] = par[ 8];
  567. par_mapped[11] = par[ 7];
  568. par_mapped[10] = par[ 6];
  569. par_mapped[ 9] = par[ 5];
  570. par_mapped[ 8] = par[ 5];
  571. par_mapped[ 7] = par[ 4];
  572. par_mapped[ 6] = par[ 4];
  573. par_mapped[ 5] = par[ 3];
  574. par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
  575. par_mapped[ 3] = par[ 2];
  576. par_mapped[ 2] = par[ 1];
  577. par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
  578. par_mapped[ 0] = par[ 0];
  579. }
  580. static void map_val_20_to_34(INTFLOAT par[PS_MAX_NR_IIDICC])
  581. {
  582. par[33] = par[19];
  583. par[32] = par[19];
  584. par[31] = par[18];
  585. par[30] = par[18];
  586. par[29] = par[18];
  587. par[28] = par[18];
  588. par[27] = par[17];
  589. par[26] = par[17];
  590. par[25] = par[16];
  591. par[24] = par[16];
  592. par[23] = par[15];
  593. par[22] = par[15];
  594. par[21] = par[14];
  595. par[20] = par[14];
  596. par[19] = par[13];
  597. par[18] = par[12];
  598. par[17] = par[11];
  599. par[16] = par[10];
  600. par[15] = par[ 9];
  601. par[14] = par[ 9];
  602. par[13] = par[ 8];
  603. par[12] = par[ 8];
  604. par[11] = par[ 7];
  605. par[10] = par[ 6];
  606. par[ 9] = par[ 5];
  607. par[ 8] = par[ 5];
  608. par[ 7] = par[ 4];
  609. par[ 6] = par[ 4];
  610. par[ 5] = par[ 3];
  611. par[ 4] = AAC_HALF_SUM(par[ 2], par[ 3]);
  612. par[ 3] = par[ 2];
  613. par[ 2] = par[ 1];
  614. par[ 1] = AAC_HALF_SUM(par[ 0], par[ 1]);
  615. }
  616. static void decorrelation(PSContext *ps, INTFLOAT (*out)[32][2], const INTFLOAT (*s)[32][2], int is34)
  617. {
  618. LOCAL_ALIGNED_16(INTFLOAT, power, [34], [PS_QMF_TIME_SLOTS]);
  619. LOCAL_ALIGNED_16(INTFLOAT, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
  620. INTFLOAT *peak_decay_nrg = ps->peak_decay_nrg;
  621. INTFLOAT *power_smooth = ps->power_smooth;
  622. INTFLOAT *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
  623. INTFLOAT (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
  624. INTFLOAT (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
  625. #if !USE_FIXED
  626. const float transient_impact = 1.5f;
  627. const float a_smooth = 0.25f; ///< Smoothing coefficient
  628. #endif /* USE_FIXED */
  629. const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
  630. int i, k, m, n;
  631. int n0 = 0, nL = 32;
  632. const INTFLOAT peak_decay_factor = Q31(0.76592833836465f);
  633. memset(power, 0, 34 * sizeof(*power));
  634. if (is34 != ps->is34bands_old) {
  635. memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
  636. memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
  637. memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
  638. memset(ps->delay, 0, sizeof(ps->delay));
  639. memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
  640. }
  641. for (k = 0; k < NR_BANDS[is34]; k++) {
  642. int i = k_to_i[k];
  643. ps->dsp.add_squares(power[i], s[k], nL - n0);
  644. }
  645. //Transient detection
  646. #if USE_FIXED
  647. for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
  648. for (n = n0; n < nL; n++) {
  649. int decayed_peak;
  650. decayed_peak = (int)(((int64_t)peak_decay_factor * \
  651. peak_decay_nrg[i] + 0x40000000) >> 31);
  652. peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
  653. power_smooth[i] += (power[i][n] + 2LL - power_smooth[i]) >> 2;
  654. peak_decay_diff_smooth[i] += (peak_decay_nrg[i] + 2LL - power[i][n] - \
  655. peak_decay_diff_smooth[i]) >> 2;
  656. if (peak_decay_diff_smooth[i]) {
  657. transient_gain[i][n] = FFMIN(power_smooth[i]*43691LL / peak_decay_diff_smooth[i], 1<<16);
  658. } else
  659. transient_gain[i][n] = 1 << 16;
  660. }
  661. }
  662. #else
  663. for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
  664. for (n = n0; n < nL; n++) {
  665. float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
  666. float denom;
  667. peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
  668. power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
  669. peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
  670. denom = transient_impact * peak_decay_diff_smooth[i];
  671. transient_gain[i][n] = (denom > power_smooth[i]) ?
  672. power_smooth[i] / denom : 1.0f;
  673. }
  674. }
  675. #endif /* USE_FIXED */
  676. //Decorrelation and transient reduction
  677. // PS_AP_LINKS - 1
  678. // -----
  679. // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
  680. //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
  681. // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
  682. // m = 0
  683. //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
  684. for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
  685. int b = k_to_i[k];
  686. #if USE_FIXED
  687. int g_decay_slope;
  688. if (k - DECAY_CUTOFF[is34] <= 0) {
  689. g_decay_slope = 1 << 30;
  690. }
  691. else if (k - DECAY_CUTOFF[is34] >= 20) {
  692. g_decay_slope = 0;
  693. }
  694. else {
  695. g_decay_slope = (1 << 30) - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
  696. }
  697. #else
  698. float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
  699. g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
  700. #endif /* USE_FIXED */
  701. memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
  702. memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
  703. for (m = 0; m < PS_AP_LINKS; m++) {
  704. memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
  705. }
  706. ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
  707. phi_fract[is34][k],
  708. (const INTFLOAT (*)[2]) Q_fract_allpass[is34][k],
  709. transient_gain[b], g_decay_slope, nL - n0);
  710. }
  711. for (; k < SHORT_DELAY_BAND[is34]; k++) {
  712. int i = k_to_i[k];
  713. memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
  714. memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
  715. //H = delay 14
  716. ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
  717. transient_gain[i], nL - n0);
  718. }
  719. for (; k < NR_BANDS[is34]; k++) {
  720. int i = k_to_i[k];
  721. memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
  722. memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
  723. //H = delay 1
  724. ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
  725. transient_gain[i], nL - n0);
  726. }
  727. }
  728. static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
  729. int8_t (*par)[PS_MAX_NR_IIDICC],
  730. int num_par, int num_env, int full)
  731. {
  732. int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
  733. int e;
  734. if (num_par == 20 || num_par == 11) {
  735. for (e = 0; e < num_env; e++) {
  736. map_idx_20_to_34(par_mapped[e], par[e], full);
  737. }
  738. } else if (num_par == 10 || num_par == 5) {
  739. for (e = 0; e < num_env; e++) {
  740. map_idx_10_to_34(par_mapped[e], par[e], full);
  741. }
  742. } else {
  743. *p_par_mapped = par;
  744. }
  745. }
  746. static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
  747. int8_t (*par)[PS_MAX_NR_IIDICC],
  748. int num_par, int num_env, int full)
  749. {
  750. int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
  751. int e;
  752. if (num_par == 34 || num_par == 17) {
  753. for (e = 0; e < num_env; e++) {
  754. map_idx_34_to_20(par_mapped[e], par[e], full);
  755. }
  756. } else if (num_par == 10 || num_par == 5) {
  757. for (e = 0; e < num_env; e++) {
  758. map_idx_10_to_20(par_mapped[e], par[e], full);
  759. }
  760. } else {
  761. *p_par_mapped = par;
  762. }
  763. }
  764. static void stereo_processing(PSContext *ps, INTFLOAT (*l)[32][2], INTFLOAT (*r)[32][2], int is34)
  765. {
  766. int e, b, k;
  767. INTFLOAT (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
  768. INTFLOAT (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
  769. INTFLOAT (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
  770. INTFLOAT (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
  771. int8_t *opd_hist = ps->opd_hist;
  772. int8_t *ipd_hist = ps->ipd_hist;
  773. int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
  774. int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
  775. int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
  776. int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
  777. int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
  778. int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
  779. int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
  780. int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
  781. const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
  782. TABLE_CONST INTFLOAT (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
  783. //Remapping
  784. if (ps->num_env_old) {
  785. memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
  786. memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
  787. memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
  788. memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
  789. memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
  790. memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
  791. memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
  792. memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
  793. }
  794. if (is34) {
  795. remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
  796. remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
  797. if (ps->enable_ipdopd) {
  798. remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
  799. remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
  800. }
  801. if (!ps->is34bands_old) {
  802. map_val_20_to_34(H11[0][0]);
  803. map_val_20_to_34(H11[1][0]);
  804. map_val_20_to_34(H12[0][0]);
  805. map_val_20_to_34(H12[1][0]);
  806. map_val_20_to_34(H21[0][0]);
  807. map_val_20_to_34(H21[1][0]);
  808. map_val_20_to_34(H22[0][0]);
  809. map_val_20_to_34(H22[1][0]);
  810. ipdopd_reset(ipd_hist, opd_hist);
  811. }
  812. } else {
  813. remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
  814. remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
  815. if (ps->enable_ipdopd) {
  816. remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
  817. remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
  818. }
  819. if (ps->is34bands_old) {
  820. map_val_34_to_20(H11[0][0]);
  821. map_val_34_to_20(H11[1][0]);
  822. map_val_34_to_20(H12[0][0]);
  823. map_val_34_to_20(H12[1][0]);
  824. map_val_34_to_20(H21[0][0]);
  825. map_val_34_to_20(H21[1][0]);
  826. map_val_34_to_20(H22[0][0]);
  827. map_val_34_to_20(H22[1][0]);
  828. ipdopd_reset(ipd_hist, opd_hist);
  829. }
  830. }
  831. //Mixing
  832. for (e = 0; e < ps->num_env; e++) {
  833. for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
  834. INTFLOAT h11, h12, h21, h22;
  835. h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
  836. h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
  837. h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
  838. h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
  839. if (!PS_BASELINE && ps->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
  840. //The spec say says to only run this smoother when enable_ipdopd
  841. //is set but the reference decoder appears to run it constantly
  842. INTFLOAT h11i, h12i, h21i, h22i;
  843. INTFLOAT ipd_adj_re, ipd_adj_im;
  844. int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
  845. int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
  846. INTFLOAT opd_re = pd_re_smooth[opd_idx];
  847. INTFLOAT opd_im = pd_im_smooth[opd_idx];
  848. INTFLOAT ipd_re = pd_re_smooth[ipd_idx];
  849. INTFLOAT ipd_im = pd_im_smooth[ipd_idx];
  850. opd_hist[b] = opd_idx & 0x3F;
  851. ipd_hist[b] = ipd_idx & 0x3F;
  852. ipd_adj_re = AAC_MADD30(opd_re, ipd_re, opd_im, ipd_im);
  853. ipd_adj_im = AAC_MSUB30(opd_im, ipd_re, opd_re, ipd_im);
  854. h11i = AAC_MUL30(h11, opd_im);
  855. h11 = AAC_MUL30(h11, opd_re);
  856. h12i = AAC_MUL30(h12, ipd_adj_im);
  857. h12 = AAC_MUL30(h12, ipd_adj_re);
  858. h21i = AAC_MUL30(h21, opd_im);
  859. h21 = AAC_MUL30(h21, opd_re);
  860. h22i = AAC_MUL30(h22, ipd_adj_im);
  861. h22 = AAC_MUL30(h22, ipd_adj_re);
  862. H11[1][e+1][b] = h11i;
  863. H12[1][e+1][b] = h12i;
  864. H21[1][e+1][b] = h21i;
  865. H22[1][e+1][b] = h22i;
  866. }
  867. H11[0][e+1][b] = h11;
  868. H12[0][e+1][b] = h12;
  869. H21[0][e+1][b] = h21;
  870. H22[0][e+1][b] = h22;
  871. }
  872. for (k = 0; k < NR_BANDS[is34]; k++) {
  873. LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
  874. LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
  875. int start = ps->border_position[e];
  876. int stop = ps->border_position[e+1];
  877. INTFLOAT width = Q30(1.f) / ((stop - start) ? (stop - start) : 1);
  878. #if USE_FIXED
  879. width = FFMIN(2U*width, INT_MAX);
  880. #endif
  881. b = k_to_i[k];
  882. h[0][0] = H11[0][e][b];
  883. h[0][1] = H12[0][e][b];
  884. h[0][2] = H21[0][e][b];
  885. h[0][3] = H22[0][e][b];
  886. if (!PS_BASELINE && ps->enable_ipdopd) {
  887. //Is this necessary? ps_04_new seems unchanged
  888. if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
  889. h[1][0] = -H11[1][e][b];
  890. h[1][1] = -H12[1][e][b];
  891. h[1][2] = -H21[1][e][b];
  892. h[1][3] = -H22[1][e][b];
  893. } else {
  894. h[1][0] = H11[1][e][b];
  895. h[1][1] = H12[1][e][b];
  896. h[1][2] = H21[1][e][b];
  897. h[1][3] = H22[1][e][b];
  898. }
  899. }
  900. //Interpolation
  901. h_step[0][0] = AAC_MSUB31_V3(H11[0][e+1][b], h[0][0], width);
  902. h_step[0][1] = AAC_MSUB31_V3(H12[0][e+1][b], h[0][1], width);
  903. h_step[0][2] = AAC_MSUB31_V3(H21[0][e+1][b], h[0][2], width);
  904. h_step[0][3] = AAC_MSUB31_V3(H22[0][e+1][b], h[0][3], width);
  905. if (!PS_BASELINE && ps->enable_ipdopd) {
  906. h_step[1][0] = AAC_MSUB31_V3(H11[1][e+1][b], h[1][0], width);
  907. h_step[1][1] = AAC_MSUB31_V3(H12[1][e+1][b], h[1][1], width);
  908. h_step[1][2] = AAC_MSUB31_V3(H21[1][e+1][b], h[1][2], width);
  909. h_step[1][3] = AAC_MSUB31_V3(H22[1][e+1][b], h[1][3], width);
  910. }
  911. if (stop - start)
  912. ps->dsp.stereo_interpolate[!PS_BASELINE && ps->enable_ipdopd](
  913. l[k] + 1 + start, r[k] + 1 + start,
  914. h, h_step, stop - start);
  915. }
  916. }
  917. }
  918. int AAC_RENAME(ff_ps_apply)(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
  919. {
  920. INTFLOAT (*Lbuf)[32][2] = ps->Lbuf;
  921. INTFLOAT (*Rbuf)[32][2] = ps->Rbuf;
  922. const int len = 32;
  923. int is34 = ps->is34bands;
  924. top += NR_BANDS[is34] - 64;
  925. memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
  926. if (top < NR_ALLPASS_BANDS[is34])
  927. memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
  928. hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
  929. decorrelation(ps, Rbuf, (const INTFLOAT (*)[32][2]) Lbuf, is34);
  930. stereo_processing(ps, Lbuf, Rbuf, is34);
  931. hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
  932. hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
  933. return 0;
  934. }
  935. #define PS_INIT_VLC_STATIC(num, size) \
  936. INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
  937. ps_tmp[num].ps_bits, 1, 1, \
  938. ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
  939. size);
  940. #define PS_VLC_ROW(name) \
  941. { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
  942. av_cold void AAC_RENAME(ff_ps_init)(void) {
  943. // Syntax initialization
  944. static const struct {
  945. const void *ps_codes, *ps_bits;
  946. const unsigned int table_size, elem_size;
  947. } ps_tmp[] = {
  948. PS_VLC_ROW(huff_iid_df1),
  949. PS_VLC_ROW(huff_iid_dt1),
  950. PS_VLC_ROW(huff_iid_df0),
  951. PS_VLC_ROW(huff_iid_dt0),
  952. PS_VLC_ROW(huff_icc_df),
  953. PS_VLC_ROW(huff_icc_dt),
  954. PS_VLC_ROW(huff_ipd_df),
  955. PS_VLC_ROW(huff_ipd_dt),
  956. PS_VLC_ROW(huff_opd_df),
  957. PS_VLC_ROW(huff_opd_dt),
  958. };
  959. PS_INIT_VLC_STATIC(0, 1544);
  960. PS_INIT_VLC_STATIC(1, 832);
  961. PS_INIT_VLC_STATIC(2, 1024);
  962. PS_INIT_VLC_STATIC(3, 1036);
  963. PS_INIT_VLC_STATIC(4, 544);
  964. PS_INIT_VLC_STATIC(5, 544);
  965. PS_INIT_VLC_STATIC(6, 512);
  966. PS_INIT_VLC_STATIC(7, 512);
  967. PS_INIT_VLC_STATIC(8, 512);
  968. PS_INIT_VLC_STATIC(9, 512);
  969. ps_tableinit();
  970. }
  971. av_cold void AAC_RENAME(ff_ps_ctx_init)(PSContext *ps)
  972. {
  973. AAC_RENAME(ff_psdsp_init)(&ps->dsp);
  974. }