Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "prot.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 :
49 : static void pyramidSearch( const float *const s, const int16_t L, const int16_t Ptot, const float A, int16_t *ztak, float *stak );
50 :
51 : /*-------------------------------------------------------------------*
52 : * pvq_encode()
53 : *
54 : *
55 : *--------------------------------------------------------------------*/
56 :
57 3003558 : void pvq_encode(
58 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
59 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
60 : const float *x, /* i : vector to quantize */
61 : int16_t *y, /* o : quantized vector (non-scaled short)*/
62 : float *xq, /* o : quantized vector (scaled float) */
63 : const int16_t pulses, /* i : number of allocated pulses */
64 : const int16_t dim, /* i : Length of vector */
65 : const float gain /* i : Gain */
66 : )
67 : {
68 : PvqEntry entry;
69 :
70 3003558 : pyramidSearch( x, dim, pulses, gain, y, xq );
71 :
72 3003558 : entry = mpvq_encode_vec( y, dim, pulses );
73 3003558 : if ( dim != 1 )
74 : {
75 3003019 : rc_enc_bits( hBstr, hPVQ, (uint32_t) entry.lead_sign_ind, 1 );
76 3003019 : rc_enc_uniform( hBstr, hPVQ, entry.index, entry.size );
77 : }
78 : else
79 : {
80 539 : rc_enc_bits( hBstr, hPVQ, (uint32_t) entry.lead_sign_ind, 1 );
81 : }
82 3003558 : return;
83 : }
84 :
85 : /*! r: L1 norm value */
86 3003558 : static float L1norm(
87 : const float *const s, /* i : input vector */
88 : float *abso_s, /* o : absolute vector */
89 : int16_t *sign_s, /* o : signs */
90 : const int16_t L /* i : vector length */
91 : )
92 : {
93 : int16_t i;
94 3003558 : float ftmp, r = 0.0f;
95 :
96 40877883 : for ( i = 0; i < L; i++ )
97 : {
98 37874325 : ftmp = s[i];
99 37874325 : sign_s[i] = (int16_t) sign( ftmp );
100 37874325 : abso_s[i] = (float) fabs( ftmp );
101 37874325 : r += abso_s[i];
102 : }
103 :
104 3003558 : return r;
105 : }
106 :
107 :
108 3003558 : void pyramidSearch(
109 : const float *const s, /* i : input vector */
110 : const int16_t L, /* i : vector length */
111 : const int16_t Ptot, /* i : assigned diracs */
112 : const float A, /* i : gain */
113 : int16_t *ztak, /* i : integer output vector */
114 : float *stak /* i : normalize output vector */
115 : )
116 : {
117 : int16_t i, high_pulse_dens;
118 : int16_t z[PVQ_MAX_BAND_SIZE];
119 : float sL1;
120 : float energy, xcorr;
121 : int16_t P;
122 : float energy1, energy2;
123 : float xcorr1sq, xcorr2sq;
124 : float zscale, sscale;
125 : float abso_s[PVQ_MAX_BAND_SIZE];
126 : int16_t sign_s[PVQ_MAX_BAND_SIZE];
127 3003558 : int16_t n = 0;
128 :
129 3003558 : high_pulse_dens = Ptot > (int16_t) ( 0.501892089843750f * L );
130 3003558 : sL1 = L1norm( s, abso_s, sign_s, L );
131 3003558 : if ( high_pulse_dens && sL1 > 0 && A > 0 )
132 : {
133 1263980 : P = Ptot - PYR_OFFSET;
134 1263980 : zscale = P / sL1;
135 12373941 : for ( i = 0; i < L; i++ )
136 : {
137 11109961 : z[i] = (int16_t) floor( zscale * abso_s[i] );
138 : }
139 : }
140 : else
141 : {
142 28503942 : for ( i = 0; i < L; i++ )
143 : {
144 26764364 : z[i] = 0;
145 : }
146 : }
147 :
148 3003558 : energy = 0.0f;
149 3003558 : if ( sL1 > 0 && A > 0 )
150 : {
151 2989027 : xcorr = 0.0f;
152 2989027 : P = 0;
153 40647407 : for ( i = 0; i < L; i++ )
154 : {
155 37658380 : xcorr += abso_s[i] * z[i];
156 37658380 : energy += z[i] * z[i];
157 37658380 : P += z[i];
158 : }
159 2989027 : energy = 0.5f * energy;
160 14662428 : while ( P < Ptot )
161 : {
162 11673401 : energy += 0.5f;
163 11673401 : n = 0;
164 11673401 : xcorr1sq = xcorr + abso_s[0];
165 11673401 : xcorr1sq *= xcorr1sq;
166 11673401 : energy1 = energy + z[0];
167 :
168 152628792 : for ( i = 1; i < L; i++ )
169 : {
170 140955391 : xcorr2sq = xcorr + abso_s[i];
171 140955391 : xcorr2sq *= xcorr2sq;
172 140955391 : energy2 = energy + z[i];
173 :
174 140955391 : if ( ( xcorr1sq * energy2 ) < ( xcorr2sq * energy1 ) )
175 : {
176 25517492 : n = i;
177 25517492 : xcorr1sq = xcorr2sq;
178 25517492 : energy1 = energy2;
179 : }
180 : }
181 :
182 11673401 : z[n]++;
183 11673401 : xcorr += abso_s[n];
184 11673401 : energy = energy1;
185 11673401 : P++;
186 : }
187 : }
188 : else
189 : {
190 14531 : if ( L > 1 )
191 : {
192 14529 : z[0] = (int16_t) ( Ptot * 0.5f );
193 14529 : z[L - 1] = -( Ptot - z[0] );
194 14529 : energy = 0.5f * ( z[0] * z[0] + z[L - 1] * z[L - 1] );
195 : }
196 : else
197 : {
198 2 : z[0] = Ptot;
199 2 : energy = 0.5f * ( z[0] * z[0] );
200 : }
201 : }
202 :
203 3003558 : sscale = (float) A / (float) sqrt( 2.0f * energy );
204 40877883 : for ( i = 0; i < L; i++ )
205 : {
206 37874325 : ztak[i] = z[i] * sign_s[i];
207 37874325 : stak[i] = sscale * ztak[i];
208 : }
209 :
210 3003558 : return;
211 : }
|