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: sbr_update_freq_scale.c
22 ------------------------------------------------------------------------------
29 ------------------------------------------------------------------------------
30 INPUT AND OUTPUT DEFINITIONS
34 ------------------------------------------------------------------------------
38 ------------------------------------------------------------------------------
42 ------------------------------------------------------------------------------
45 SC 29 Software Copyright Licencing Disclaimer:
47 This software module was originally developed by
53 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
54 standards for reference purposes and its performance may not have been
55 optimized. This software module is an implementation of one or more tools as
56 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
57 ISO/IEC gives users free license to this software module or modifications
58 thereof for use in products claiming conformance to audiovisual and
59 image-coding related ITU Recommendations and/or ISO/IEC International
60 Standards. ISO/IEC gives users the same free license to this software module or
61 modifications thereof for research purposes and further ISO/IEC standardisation.
62 Those intending to use this software module in products are advised that its
63 use may infringe existing patents. ISO/IEC have no liability for use of this
64 software module or modifications thereof. Copyright is not released for
65 products that do not conform to audiovisual and image-coding related ITU
66 Recommendations and/or ISO/IEC International Standards.
67 The original developer retains full right to modify and use the code for its
68 own purpose, assign or donate the code to a third party and to inhibit third
69 parties from using the code for products that do not conform to audiovisual and
70 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
71 This copyright notice must be included in all copies or derivative works.
72 Copyright (c) ISO/IEC 2002.
74 ------------------------------------------------------------------------------
77 ------------------------------------------------------------------------------
81 /*----------------------------------------------------------------------------
83 ----------------------------------------------------------------------------*/
88 #include "sbr_update_freq_scale.h"
89 #include "shellsort.h"
94 #include "fxp_mul32.h"
96 #define Q_fmt(x) (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F))
97 #define Q28fmt(x) (Int32)(x*((Int32)1<<28) + (x>=0?0.5F:-0.5F))
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 ----------------------------------------------------------------------------*/
121 /*----------------------------------------------------------------------------
122 ; EXTERNAL FUNCTION REFERENCES
123 ; Declare functions defined elsewhere and referenced in this module
124 ----------------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------------
127 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
128 ; Declare variables used in this module but defined elsewhere
129 ----------------------------------------------------------------------------*/
131 /*----------------------------------------------------------------------------
133 ----------------------------------------------------------------------------*/
137 void sbr_update_freq_scale(Int32 * v_k_master,
141 const Int32 freqScale,
142 const Int32 alterScale,
143 const Int32 channelOffset)
150 if (freqScale > 0) /*Bark mode*/
156 Int32 d[MAX_SECOND_REGION];
157 Int32 d2[MAX_SECOND_REGION];
158 Int32 w[2] = {Q_fmt(1.0F), Q_fmt(1.0F)};
165 b_p_o = (freqScale == 1) ? 12 : 8;
166 b_p_o = (freqScale == 2) ? 10 : b_p_o;
168 w[1] = (alterScale == 0) ? Q_fmt(0.5f) : Q_fmt(0.384615384615386f);
170 if (usb > fxp_mul32_Q28(lsbM, Q28fmt(2.2449)))
181 for (reg = 0; reg < regions; reg++)
186 tmp_q1 = pv_log2((k[1] << 20) / k[0]);
188 tmp_q1 = fxp_mul32_Q15(tmp_q1, b_p_o);
189 tmp_q1 = (tmp_q1 + 32) >> 6;
191 numBands = tmp_q1 << 1;
194 CalcBands(d, k[0], k[1], numBands); /* CalcBands => d */
195 shellsort(d, numBands); /* SortBands sort d */
196 cumSum(k[0] - channelOffset,
199 (v_k_master + *h_num_bands)); /* cumsum */
201 *h_num_bands += numBands; /* Output nr of bands */
205 tmp_q1 = pv_log2((k[reg + 1] << 20) / k[reg]);
207 tmp_q1 = fxp_mul32_Q30(tmp_q1, w[reg]);
208 tmp_q1 = fxp_mul32_Q15(tmp_q1, b_p_o);
209 tmp_q1 = (tmp_q1 + 16) >> 5;
211 numBands2 = tmp_q1 << 1;
213 CalcBands(d2, k[reg], k[reg+1], numBands2); /* CalcBands => d */
214 shellsort(d2, numBands2); /* SortBands sort d */
215 if (d[numBands-1] > d2[0])
218 Int32 change = d[numBands-1] - d2[0];
219 /* Limit the change so that the last band cannot get narrower than the first one */
220 if (change > (d2[numBands2-1] - d2[0]) >> 1)
222 change = (d2[numBands2-1] - d2[0]) >> 1;
226 d2[numBands2-1] -= change;
227 shellsort(d2, numBands2);
230 cumSum(k[reg] - channelOffset,
233 v_k_master + *h_num_bands); /* cumsum */
235 *h_num_bands += numBands2; /* Output nr of bands */
243 Int32 diff_tot[MAX_OCTAVE + MAX_SECOND_REGION];
250 numBands = (usb - lsbM) >> 1;
252 k2_achived = lsbM + numBands;
256 numBands = usb - lsbM;
257 if (numBands & 0x1) /* equivalent rounding */
262 k2_achived = lsbM + (numBands << 1);
265 k2_diff = usb - k2_achived;
267 for (i = 0; i < numBands; i++)
272 if (k2_diff < 0) /* If linear scale wasn't achived */
274 incr = 1; /* and we got too large SBR area */
278 if (k2_diff > 0) /* If linear scale wasn't achived */
280 incr = -1; /* and we got too small SBR area */
284 /* Adjust diff vector to get spec. SBR range */
295 v_k_master); /* cumsum */
297 *h_num_bands = numBands; /* Output nr of bands */
302 void CalcBands(Int32 * diff,
315 for (i = 1; i <= num_bands; i++)
317 /* float temp=(start * pow( (float)stop/start, (float)i/num_bands)); */
319 tmp_q1 = pv_log2((stop << 20) / start);
321 tmp_q1 = fxp_mul32_Q20(tmp_q1, (i << 27) / num_bands);
322 tmp_q1 = pv_pow2(tmp_q1);
324 tmp_q1 = fxp_mul32_Q20(tmp_q1, start);
326 current = (tmp_q1 + 16) >> 5;
328 diff[i-1] = current - previous;
332 } /* End CalcBands */
335 void cumSum(Int32 start_value,
338 Int32 * start_adress)
341 Int32 *pt_start_adress = start_adress;
342 Int32 *pt_start_adress_1 = start_adress;
343 Int32 *pt_diff = diff;
345 if (length > 0) /* avoid possible error on loop */
347 *(pt_start_adress_1++) = start_value;
349 for (i = (length >> 1); i != 0; i--)
351 *(pt_start_adress_1++) = *(pt_start_adress++) + *(pt_diff++);
352 *(pt_start_adress_1++) = *(pt_start_adress++) + *(pt_diff++);
357 *(pt_start_adress_1) = *(pt_start_adress) + *(pt_diff);