1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
20 Filename: ps_init_stereo_mixing.c
22 ------------------------------------------------------------------------------
29 ------------------------------------------------------------------------------
30 INPUT AND OUTPUT DEFINITIONS
34 ------------------------------------------------------------------------------
37 initialize mixing procedure type Ra, type Rb is not supported
39 ------------------------------------------------------------------------------
43 ------------------------------------------------------------------------------
46 SC 29 Software Copyright Licencing Disclaimer:
48 This software module was originally developed by
54 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
55 standards for reference purposes and its performance may not have been
56 optimized. This software module is an implementation of one or more tools as
57 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
58 ISO/IEC gives users free license to this software module or modifications
59 thereof for use in products claiming conformance to audiovisual and
60 image-coding related ITU Recommendations and/or ISO/IEC International
61 Standards. ISO/IEC gives users the same free license to this software module or
62 modifications thereof for research purposes and further ISO/IEC standardisation.
63 Those intending to use this software module in products are advised that its
64 use may infringe existing patents. ISO/IEC have no liability for use of this
65 software module or modifications thereof. Copyright is not released for
66 products that do not conform to audiovisual and image-coding related ITU
67 Recommendations and/or ISO/IEC International Standards.
68 The original developer retains full right to modify and use the code for its
69 own purpose, assign or donate the code to a third party and to inhibit third
70 parties from using the code for products that do not conform to audiovisual and
71 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
72 This copyright notice must be included in all copies or derivative works.
73 Copyright (c) ISO/IEC 2003.
75 ------------------------------------------------------------------------------
78 ------------------------------------------------------------------------------
82 /*----------------------------------------------------------------------------
84 ----------------------------------------------------------------------------*/
88 #ifdef PARAMETRICSTEREO
90 #include "pv_audio_type_defs.h"
91 #include "fxp_mul32.h"
93 #include "aac_mem_funcs.h"
96 #include "ps_all_pass_filter_coeff.h"
97 #include "ps_init_stereo_mixing.h"
99 /*----------------------------------------------------------------------------
101 ; Define module specific macros here
102 ----------------------------------------------------------------------------*/
105 /*----------------------------------------------------------------------------
107 ; Include all pre-processor statements here. Include conditional
108 ; compile variables also.
109 ----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------
112 ; LOCAL FUNCTION DEFINITIONS
113 ; Function Prototype declaration
114 ----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------
117 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
118 ; Variable declaration - defined here and used outside this module
119 ----------------------------------------------------------------------------*/
123 ; c(b) = 10^(iid(b)/20)
125 ; Intensity differences
128 ; c_1(b) = ----------------
132 ; c_2(b) = ----------------
140 #define Q30_fmt(x) (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F))
142 const Int32 scaleFactors[NO_IID_LEVELS] =
144 Q30_fmt(1.411983f), Q30_fmt(1.403138f), Q30_fmt(1.386877f),
145 Q30_fmt(1.348400f), Q30_fmt(1.291249f), Q30_fmt(1.196037f),
146 Q30_fmt(1.107372f), Q30_fmt(1.000000f), Q30_fmt(0.879617f),
147 Q30_fmt(0.754649f), Q30_fmt(0.576780f), Q30_fmt(0.426401f),
148 Q30_fmt(0.276718f), Q30_fmt(0.176645f), Q30_fmt(0.079402f)
151 const Int32 scaleFactorsFine[NO_IID_LEVELS_FINE] =
153 Q30_fmt(1.414207f), Q30_fmt(1.414191f), Q30_fmt(1.414143f),
154 Q30_fmt(1.413990f), Q30_fmt(1.413507f), Q30_fmt(1.411983f),
155 Q30_fmt(1.409773f), Q30_fmt(1.405395f), Q30_fmt(1.396780f),
156 Q30_fmt(1.380053f), Q30_fmt(1.348400f), Q30_fmt(1.313920f),
157 Q30_fmt(1.264310f), Q30_fmt(1.196037f), Q30_fmt(1.107372f),
158 Q30_fmt(1.000000f), Q30_fmt(0.879617f), Q30_fmt(0.754649f),
159 Q30_fmt(0.633656f), Q30_fmt(0.523081f), Q30_fmt(0.426401f),
160 Q30_fmt(0.308955f), Q30_fmt(0.221375f), Q30_fmt(0.157688f),
161 Q30_fmt(0.111982f), Q30_fmt(0.079402f), Q30_fmt(0.044699f),
162 Q30_fmt(0.025145f), Q30_fmt(0.014141f), Q30_fmt(0.007953f),
168 * alphas ranged between 0 and pi/2
169 * alpha(b) = (1/2)*arccos( gamma(b))
172 * gamma 1 0.937 0.84118 0.60092 0.36764 0 -0.589 -1
178 const Int32 scaled_alphas[NO_ICC_LEVELS] =
180 Q30_fmt(0.00000000000000f), Q30_fmt(0.12616764875355f),
181 Q30_fmt(0.20199707286122f), Q30_fmt(0.32744135137762f),
182 Q30_fmt(0.42225800677370f), Q30_fmt(0.55536025173035f),
183 Q30_fmt(0.77803595530059f), Q30_fmt(1.11072050346071f)
186 const Int32 cos_alphas[NO_ICC_LEVELS] =
188 Q30_fmt(1.00000000000000f), Q30_fmt(0.98412391153249f),
189 Q30_fmt(0.95947390717984f), Q30_fmt(0.89468446298319f),
190 Q30_fmt(0.82693418207478f), Q30_fmt(0.70710689672598f),
191 Q30_fmt(0.45332071670080f), Q30_fmt(0.00000032679490f)
194 const Int32 sin_alphas[NO_ICC_LEVELS] =
196 Q30_fmt(0.00000000000000f), Q30_fmt(0.17748275057029f),
197 Q30_fmt(0.28179748302823f), Q30_fmt(0.44669868110000f),
198 Q30_fmt(0.56229872711603f), Q30_fmt(0.70710666564709f),
199 Q30_fmt(0.89134747871404f), Q30_fmt(1.00000000000000f)
204 /*----------------------------------------------------------------------------
205 ; EXTERNAL FUNCTION REFERENCES
206 ; Declare functions defined elsewhere and referenced in this module
207 ----------------------------------------------------------------------------*/
209 /*----------------------------------------------------------------------------
210 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
211 ; Declare variables used in this module but defined elsewhere
212 ----------------------------------------------------------------------------*/
214 /*----------------------------------------------------------------------------
216 ----------------------------------------------------------------------------*/
218 Int32 ps_init_stereo_mixing(STRUCT_PS_DEC *pms,
228 const Int32 *pScaleFactors;
246 noIidSteps = NO_IID_STEPS_FINE; /* NO_IID_STEPS_FINE == 15 */
247 pScaleFactors = scaleFactorsFine;
251 noIidSteps = NO_IID_STEPS; /* NO_IID_STEPS == 7 */
252 pScaleFactors = scaleFactors;
257 pms->lastUsb = pms->usb;
259 if (usb != pms->lastUsb && pms->lastUsb != 0)
266 invEnvLength = pms->aEnvStartStop[env + 1] - pms->aEnvStartStop[env];
268 if (invEnvLength == (Int32) pms->noSubSamples)
270 invEnvLength = pms->invNoSubSamples;
274 invEnvLength = Q30_fmt(1.0f) / invEnvLength;
277 if (invEnvLength == 32) /* more likely value */
279 for (group = 0; group < NO_IID_GROUPS; group++) /* == 22 */
281 bin = bins2groupMap[group];
284 * c(b) = 10^(iid(b)/20)
287 tmp = pms->aaIidIndex[env][bin];
290 * Intensity differences
293 * c_1(b) = ----------------
297 scaleR = pScaleFactors[noIidSteps + tmp];
301 * c_2(b) = ----------------
306 scaleL = pScaleFactors[noIidSteps - tmp];
310 * alpha(b) = (1/2)*arccos( gamma(b))
312 tmp = pms->aaIccIndex[env][bin];
314 cos_alpha = cos_alphas[ tmp];
315 sin_alpha = sin_alphas[ tmp];
318 * beta(b) = alpha(b)/sqrt(2)*( c_1(b) - c_2(b))
321 beta = fxp_mul32_Q30(scaled_alphas[ tmp], (scaleR - scaleL));
323 cos_beta = pv_cosine(beta);
324 sin_beta = pv_sine(beta);
326 temp1 = fxp_mul32_Q30(cos_beta, cos_alpha);
327 temp2 = fxp_mul32_Q30(sin_beta, sin_alpha);
331 * h11(b) = cos( alpha(b) + beta(b))* c_2(b)
332 * h12(b) = cos( beta(b) - alpha(b))* c_1(b)
335 h11 = fxp_mul32_Q30(scaleL, (temp1 - temp2));
336 h12 = fxp_mul32_Q30(scaleR, (temp1 + temp2));
338 temp1 = fxp_mul32_Q30(sin_beta, cos_alpha);
339 temp2 = fxp_mul32_Q30(cos_beta, sin_alpha);
342 * h21(b) = sin( alpha(b) + beta(b))* c_2(b)
343 * h22(b) = sin( beta(b) - alpha(b))* c_1(b)
346 h21 = fxp_mul32_Q30(scaleL, (temp1 + temp2));
347 h22 = fxp_mul32_Q30(scaleR, (temp1 - temp2));
351 * Linear interpolation
353 * Hij(k, n_e+1) - Hij(k, n_e)
354 * Hij(k,n) = Hij(k, n_e) + (n - n_e)*---------------------------
358 ptr_tmp = &pms->h11Prev[group];
359 pms->H11[group] = *ptr_tmp;
360 pms->deltaH11[group] = (h11 - *ptr_tmp) >> 5;
363 ptr_tmp = &pms->h12Prev[group];
364 pms->H12[group] = *ptr_tmp;
365 pms->deltaH12[group] = (h12 - *ptr_tmp) >> 5;
368 ptr_tmp = &pms->h21Prev[group];
369 pms->H21[group] = *ptr_tmp;
370 pms->deltaH21[group] = (h21 - *ptr_tmp) >> 5;
373 ptr_tmp = &pms->h22Prev[group];
374 pms->H22[group] = *ptr_tmp;
375 pms->deltaH22[group] = (h22 - *ptr_tmp) >> 5;
384 for (group = 0; group < NO_IID_GROUPS; group++) /* == 22 */
386 bin = bins2groupMap[group];
389 * c(b) = 10^(iid(b)/20)
392 tmp = pms->aaIidIndex[env][bin];
395 * Intensity differences
398 * c_1(b) = ----------------
402 scaleR = pScaleFactors[noIidSteps + tmp];
406 * c_2(b) = ----------------
411 scaleL = pScaleFactors[noIidSteps - tmp];
415 * alpha(b) = (1/2)*arccos( gamma(b))
417 tmp = pms->aaIccIndex[env][bin];
419 cos_alpha = cos_alphas[ tmp];
420 sin_alpha = sin_alphas[ tmp];
423 * beta(b) = alpha(b)/sqrt(2)*( c_1(b) - c_2(b))
426 beta = fxp_mul32_Q30(scaled_alphas[ tmp], (scaleR - scaleL));
428 cos_beta = pv_cosine(beta);
429 sin_beta = pv_sine(beta);
431 temp1 = fxp_mul32_Q30(cos_beta, cos_alpha);
432 temp2 = fxp_mul32_Q30(sin_beta, sin_alpha);
436 * h11(b) = cos( alpha(b) + beta(b))* c_2(b)
437 * h12(b) = cos( beta(b) - alpha(b))* c_1(b)
440 h11 = fxp_mul32_Q30(scaleL, (temp1 - temp2));
441 h12 = fxp_mul32_Q30(scaleR, (temp1 + temp2));
443 temp1 = fxp_mul32_Q30(sin_beta, cos_alpha);
444 temp2 = fxp_mul32_Q30(cos_beta, sin_alpha);
447 * h21(b) = sin( alpha(b) + beta(b))* c_2(b)
448 * h22(b) = sin( beta(b) - alpha(b))* c_1(b)
451 h21 = fxp_mul32_Q30(scaleL, (temp1 + temp2));
452 h22 = fxp_mul32_Q30(scaleR, (temp1 - temp2));
456 * Linear interpolation
458 * Hij(k, n_e+1) - Hij(k, n_e)
459 * Hij(k,n) = Hij(k, n_e) + (n - n_e)*---------------------------
463 ptr_tmp = &pms->h11Prev[group];
464 pms->deltaH11[group] = fxp_mul32_Q30((h11 - *ptr_tmp), invEnvLength);
465 pms->H11[group] = *ptr_tmp;
468 ptr_tmp = &pms->h12Prev[group];
469 pms->deltaH12[group] = fxp_mul32_Q30((h12 - *ptr_tmp), invEnvLength);
470 pms->H12[group] = *ptr_tmp;
473 ptr_tmp = &pms->h21Prev[group];
474 pms->deltaH21[group] = fxp_mul32_Q30((h21 - *ptr_tmp), invEnvLength);
475 pms->H21[group] = *ptr_tmp;
478 ptr_tmp = &pms->h22Prev[group];
479 pms->deltaH22[group] = fxp_mul32_Q30((h22 - *ptr_tmp), invEnvLength);
480 pms->H22[group] = *ptr_tmp;
490 } /* END ps_init_stereo_mixing */