1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*
19
20 Filename: ps_init_stereo_mixing.c
21
22------------------------------------------------------------------------------
23 REVISION HISTORY
24
25
26 Who:                                   Date: MM/DD/YYYY
27 Description:
28
29------------------------------------------------------------------------------
30 INPUT AND OUTPUT DEFINITIONS
31
32
33
34------------------------------------------------------------------------------
35 FUNCTION DESCRIPTION
36
37      initialize mixing procedure  type Ra, type Rb is not supported
38
39------------------------------------------------------------------------------
40 REQUIREMENTS
41
42
43------------------------------------------------------------------------------
44 REFERENCES
45
46SC 29 Software Copyright Licencing Disclaimer:
47
48This software module was originally developed by
49  Coding Technologies
50
51and edited by
52  -
53
54in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
55standards for reference purposes and its performance may not have been
56optimized. This software module is an implementation of one or more tools as
57specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
58ISO/IEC gives users free license to this software module or modifications
59thereof for use in products claiming conformance to audiovisual and
60image-coding related ITU Recommendations and/or ISO/IEC International
61Standards. ISO/IEC gives users the same free license to this software module or
62modifications thereof for research purposes and further ISO/IEC standardisation.
63Those intending to use this software module in products are advised that its
64use may infringe existing patents. ISO/IEC have no liability for use of this
65software module or modifications thereof. Copyright is not released for
66products that do not conform to audiovisual and image-coding related ITU
67Recommendations and/or ISO/IEC International Standards.
68The original developer retains full right to modify and use the code for its
69own purpose, assign or donate the code to a third party and to inhibit third
70parties from using the code for products that do not conform to audiovisual and
71image-coding related ITU Recommendations and/or ISO/IEC International Standards.
72This copyright notice must be included in all copies or derivative works.
73Copyright (c) ISO/IEC 2003.
74
75------------------------------------------------------------------------------
76 PSEUDO-CODE
77
78------------------------------------------------------------------------------
79*/
80
81
82/*----------------------------------------------------------------------------
83; INCLUDES
84----------------------------------------------------------------------------*/
85
86#ifdef AAC_PLUS
87
88#ifdef PARAMETRICSTEREO
89
90#include    "pv_audio_type_defs.h"
91#include    "fxp_mul32.h"
92
93#include    "aac_mem_funcs.h"
94#include    "pv_sine.h"
95#include    "s_ps_dec.h"
96#include    "ps_all_pass_filter_coeff.h"
97#include    "ps_init_stereo_mixing.h"
98
99/*----------------------------------------------------------------------------
100; MACROS
101; Define module specific macros here
102----------------------------------------------------------------------------*/
103
104
105/*----------------------------------------------------------------------------
106; DEFINES
107; Include all pre-processor statements here. Include conditional
108; compile variables also.
109----------------------------------------------------------------------------*/
110
111/*----------------------------------------------------------------------------
112; LOCAL FUNCTION DEFINITIONS
113; Function Prototype declaration
114----------------------------------------------------------------------------*/
115
116/*----------------------------------------------------------------------------
117; LOCAL STORE/BUFFER/POINTER DEFINITIONS
118; Variable declaration - defined here and used outside this module
119----------------------------------------------------------------------------*/
120
121/*
122;
123;  c(b) = 10^(iid(b)/20)
124;
125;  Intensity differences
126;
127;                  sqrt(2)
128;   c_1(b) = ----------------
129;            sqrt( 1 + c^2(b))
130;
131;               sqrt(2)*c(b)
132;   c_2(b) = ----------------
133;            sqrt( 1 + c^2(b))
134;
135*/
136
137
138
139#define R_SHIFT     30
140#define Q30_fmt(x)   (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F))
141
142const Int32 scaleFactors[NO_IID_LEVELS] =
143{
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)
149};
150
151const Int32 scaleFactorsFine[NO_IID_LEVELS_FINE] =
152{
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),
163    Q30_fmt(0.004472f)
164};
165
166
167/*
168 *  alphas ranged between 0 and pi/2
169 *  alpha(b) = (1/2)*arccos( gamma(b))
170 *
171 *    b   0    1      2        3        4      5        6     7
172 *  gamma 1 0.937  0.84118  0.60092  0.36764   0    -0.589   -1
173 *
174 */
175
176
177
178const Int32 scaled_alphas[NO_ICC_LEVELS] =
179{
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)
184};
185
186const Int32 cos_alphas[NO_ICC_LEVELS] =
187{
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)
192};
193
194const Int32 sin_alphas[NO_ICC_LEVELS] =
195{
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)
200};
201
202
203
204/*----------------------------------------------------------------------------
205; EXTERNAL FUNCTION REFERENCES
206; Declare functions defined elsewhere and referenced in this module
207----------------------------------------------------------------------------*/
208
209/*----------------------------------------------------------------------------
210; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
211; Declare variables used in this module but defined elsewhere
212----------------------------------------------------------------------------*/
213
214/*----------------------------------------------------------------------------
215; FUNCTION CODE
216----------------------------------------------------------------------------*/
217
218Int32 ps_init_stereo_mixing(STRUCT_PS_DEC *pms,
219                            Int32 env,
220                            Int32 usb)
221{
222    Int32   group;
223    Int32   bin;
224    Int32   noIidSteps;
225    Int32   tmp;
226
227    Int32   invEnvLength;
228    const Int32  *pScaleFactors;
229    Int32   scaleR;
230    Int32   scaleL;
231    Int32   cos_alpha;
232    Int32   sin_alpha;
233    Int32   beta;
234    Int32   cos_beta;
235    Int32   sin_beta;
236    Int32   temp1;
237    Int32   temp2;
238    Int32   *ptr_tmp;
239    Int32   h11;
240    Int32   h12;
241    Int32   h21;
242    Int32   h22;
243
244    if (pms->bFineIidQ)
245    {
246        noIidSteps = NO_IID_STEPS_FINE;     /*  NO_IID_STEPS_FINE == 15  */
247        pScaleFactors = scaleFactorsFine;
248    }
249    else
250    {
251        noIidSteps = NO_IID_STEPS;          /*  NO_IID_STEPS == 7   */
252        pScaleFactors = scaleFactors;
253    }
254
255    if (env == 0)
256    {
257        pms->lastUsb = pms->usb;
258        pms->usb = usb;
259        if (usb != pms->lastUsb && pms->lastUsb != 0)
260        {
261            return(-1);
262
263        }
264    }
265
266    invEnvLength =  pms->aEnvStartStop[env + 1] - pms->aEnvStartStop[env];
267
268    if (invEnvLength == (Int32) pms->noSubSamples)
269    {
270        invEnvLength = pms->invNoSubSamples;
271    }
272    else
273    {
274        invEnvLength = Q30_fmt(1.0f) / invEnvLength;
275    }
276
277    if (invEnvLength == 32)     /*  more likely value  */
278    {
279        for (group = 0; group < NO_IID_GROUPS; group++)      /* == 22 */
280        {
281            bin = bins2groupMap[group];
282
283            /*
284             *  c(b) = 10^(iid(b)/20)
285             */
286
287            tmp = pms->aaIidIndex[env][bin];
288
289            /*
290             *  Intensity differences
291             *
292             *                  sqrt(2)
293             *   c_1(b) = ----------------
294             *            sqrt( 1 + c^2(b))
295             *
296             */
297            scaleR = pScaleFactors[noIidSteps + tmp];
298
299            /*
300             *               sqrt(2)*c(b)
301             *   c_2(b) = ----------------
302             *            sqrt( 1 + c^2(b))
303             *
304             */
305
306            scaleL = pScaleFactors[noIidSteps - tmp];
307
308
309            /*
310             *  alpha(b) = (1/2)*arccos( gamma(b))
311             */
312            tmp = pms->aaIccIndex[env][bin];
313
314            cos_alpha = cos_alphas[ tmp];
315            sin_alpha = sin_alphas[ tmp];
316
317            /*
318             *   beta(b) = alpha(b)/sqrt(2)*( c_1(b) - c_2(b))
319             */
320
321            beta   = fxp_mul32_Q30(scaled_alphas[ tmp], (scaleR - scaleL));
322
323            cos_beta = pv_cosine(beta);
324            sin_beta = pv_sine(beta);
325
326            temp1 = fxp_mul32_Q30(cos_beta, cos_alpha);
327            temp2 = fxp_mul32_Q30(sin_beta, sin_alpha);
328
329
330            /*
331             *  h11(b) = cos( alpha(b) +  beta(b))* c_2(b)
332             *  h12(b) = cos(  beta(b) - alpha(b))* c_1(b)
333             */
334
335            h11 = fxp_mul32_Q30(scaleL, (temp1 - temp2));
336            h12 = fxp_mul32_Q30(scaleR, (temp1 + temp2));
337
338            temp1 = fxp_mul32_Q30(sin_beta, cos_alpha);
339            temp2 = fxp_mul32_Q30(cos_beta, sin_alpha);
340
341            /*
342             *  h21(b) = sin( alpha(b) +  beta(b))* c_2(b)
343             *  h22(b) = sin(  beta(b) - alpha(b))* c_1(b)
344             */
345
346            h21 = fxp_mul32_Q30(scaleL, (temp1 + temp2));
347            h22 = fxp_mul32_Q30(scaleR, (temp1 - temp2));
348
349
350            /*
351             *   Linear interpolation
352             *
353             *                                       Hij(k, n_e+1) - Hij(k, n_e)
354             *    Hij(k,n) = Hij(k, n_e) + (n - n_e)*---------------------------
355             *                                              n_e+1 - n_e
356             */
357
358            ptr_tmp = &pms->h11Prev[group];
359            pms->H11[group]       = *ptr_tmp;
360            pms->deltaH11[group]  = (h11 - *ptr_tmp) >> 5;
361            *ptr_tmp              = h11;
362
363            ptr_tmp = &pms->h12Prev[group];
364            pms->H12[group]       = *ptr_tmp;
365            pms->deltaH12[group]  = (h12 - *ptr_tmp) >> 5;
366            *ptr_tmp              = h12;
367
368            ptr_tmp = &pms->h21Prev[group];
369            pms->H21[group]       = *ptr_tmp;
370            pms->deltaH21[group]  = (h21 - *ptr_tmp) >> 5;
371            *ptr_tmp              = h21;
372
373            ptr_tmp = &pms->h22Prev[group];
374            pms->H22[group]       = *ptr_tmp;
375            pms->deltaH22[group]  = (h22 - *ptr_tmp) >> 5;
376            *ptr_tmp              = h22;
377
378
379        } /* groups loop */
380    }
381    else
382    {
383
384        for (group = 0; group < NO_IID_GROUPS; group++)      /* == 22 */
385        {
386            bin = bins2groupMap[group];
387
388            /*
389             *  c(b) = 10^(iid(b)/20)
390             */
391
392            tmp = pms->aaIidIndex[env][bin];
393
394            /*
395             *  Intensity differences
396             *
397             *                  sqrt(2)
398             *   c_1(b) = ----------------
399             *            sqrt( 1 + c^2(b))
400             *
401             */
402            scaleR = pScaleFactors[noIidSteps + tmp];
403
404            /*
405             *               sqrt(2)*c(b)
406             *   c_2(b) = ----------------
407             *            sqrt( 1 + c^2(b))
408             *
409             */
410
411            scaleL = pScaleFactors[noIidSteps - tmp];
412
413
414            /*
415             *  alpha(b) = (1/2)*arccos( gamma(b))
416             */
417            tmp = pms->aaIccIndex[env][bin];
418
419            cos_alpha = cos_alphas[ tmp];
420            sin_alpha = sin_alphas[ tmp];
421
422            /*
423             *   beta(b) = alpha(b)/sqrt(2)*( c_1(b) - c_2(b))
424             */
425
426            beta   = fxp_mul32_Q30(scaled_alphas[ tmp], (scaleR - scaleL));
427
428            cos_beta = pv_cosine(beta);
429            sin_beta = pv_sine(beta);
430
431            temp1 = fxp_mul32_Q30(cos_beta, cos_alpha);
432            temp2 = fxp_mul32_Q30(sin_beta, sin_alpha);
433
434
435            /*
436             *  h11(b) = cos( alpha(b) +  beta(b))* c_2(b)
437             *  h12(b) = cos(  beta(b) - alpha(b))* c_1(b)
438             */
439
440            h11 = fxp_mul32_Q30(scaleL, (temp1 - temp2));
441            h12 = fxp_mul32_Q30(scaleR, (temp1 + temp2));
442
443            temp1 = fxp_mul32_Q30(sin_beta, cos_alpha);
444            temp2 = fxp_mul32_Q30(cos_beta, sin_alpha);
445
446            /*
447             *  h21(b) = sin( alpha(b) +  beta(b))* c_2(b)
448             *  h22(b) = sin(  beta(b) - alpha(b))* c_1(b)
449             */
450
451            h21 = fxp_mul32_Q30(scaleL, (temp1 + temp2));
452            h22 = fxp_mul32_Q30(scaleR, (temp1 - temp2));
453
454
455            /*
456             *   Linear interpolation
457             *
458             *                                       Hij(k, n_e+1) - Hij(k, n_e)
459             *    Hij(k,n) = Hij(k, n_e) + (n - n_e)*---------------------------
460             *                                              n_e+1 - n_e
461             */
462
463            ptr_tmp = &pms->h11Prev[group];
464            pms->deltaH11[group]  = fxp_mul32_Q30((h11 - *ptr_tmp), invEnvLength);
465            pms->H11[group]       = *ptr_tmp;
466            *ptr_tmp              = h11;
467
468            ptr_tmp = &pms->h12Prev[group];
469            pms->deltaH12[group]  = fxp_mul32_Q30((h12 - *ptr_tmp), invEnvLength);
470            pms->H12[group]       = *ptr_tmp;
471            *ptr_tmp              = h12;
472
473            ptr_tmp = &pms->h21Prev[group];
474            pms->deltaH21[group]  = fxp_mul32_Q30((h21 - *ptr_tmp), invEnvLength);
475            pms->H21[group]       = *ptr_tmp;
476            *ptr_tmp              = h21;
477
478            ptr_tmp = &pms->h22Prev[group];
479            pms->deltaH22[group]  = fxp_mul32_Q30((h22 - *ptr_tmp), invEnvLength);
480            pms->H22[group]       = *ptr_tmp;
481            *ptr_tmp              = h22;
482
483
484        } /* groups loop */
485    }
486
487
488    return (0);
489
490} /* END ps_init_stereo_mixing */
491
492#endif
493
494
495#endif
496
497