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 299764 : 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 299764 : pyramidSearch( x, dim, pulses, gain, y, xq );
71 :
72 299764 : entry = mpvq_encode_vec( y, dim, pulses );
73 299764 : if ( dim != 1 )
74 : {
75 299722 : rc_enc_bits( hBstr, hPVQ, (uint32_t) entry.lead_sign_ind, 1 );
76 299722 : rc_enc_uniform( hBstr, hPVQ, entry.index, entry.size );
77 : }
78 : else
79 : {
80 42 : rc_enc_bits( hBstr, hPVQ, (uint32_t) entry.lead_sign_ind, 1 );
81 : }
82 299764 : return;
83 : }
84 :
85 : /*! r: L1 norm value */
86 299764 : 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 299764 : float ftmp, r = 0.0f;
95 :
96 4134116 : for ( i = 0; i < L; i++ )
97 : {
98 3834352 : ftmp = s[i];
99 3834352 : sign_s[i] = (int16_t) sign( ftmp );
100 3834352 : abso_s[i] = (float) fabs( ftmp );
101 3834352 : r += abso_s[i];
102 : }
103 :
104 299764 : return r;
105 : }
106 :
107 :
108 299764 : 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 299764 : int16_t n = 0;
128 :
129 299764 : high_pulse_dens = Ptot > (int16_t) ( 0.501892089843750f * L );
130 299764 : sL1 = L1norm( s, abso_s, sign_s, L );
131 299764 : if ( high_pulse_dens && sL1 > 0 && A > 0 )
132 : {
133 132350 : P = Ptot - PYR_OFFSET;
134 132350 : zscale = P / sL1;
135 1369326 : for ( i = 0; i < L; i++ )
136 : {
137 1236976 : z[i] = (int16_t) floor( zscale * abso_s[i] );
138 : }
139 : }
140 : else
141 : {
142 2764790 : for ( i = 0; i < L; i++ )
143 : {
144 2597376 : z[i] = 0;
145 : }
146 : }
147 :
148 299764 : energy = 0.0f;
149 299764 : if ( sL1 > 0 && A > 0 )
150 : {
151 299318 : xcorr = 0.0f;
152 299318 : P = 0;
153 4127886 : for ( i = 0; i < L; i++ )
154 : {
155 3828568 : xcorr += abso_s[i] * z[i];
156 3828568 : energy += z[i] * z[i];
157 3828568 : P += z[i];
158 : }
159 299318 : energy = 0.5f * energy;
160 1495227 : while ( P < Ptot )
161 : {
162 1195909 : energy += 0.5f;
163 1195909 : n = 0;
164 1195909 : xcorr1sq = xcorr + abso_s[0];
165 1195909 : xcorr1sq *= xcorr1sq;
166 1195909 : energy1 = energy + z[0];
167 :
168 15862149 : for ( i = 1; i < L; i++ )
169 : {
170 14666240 : xcorr2sq = xcorr + abso_s[i];
171 14666240 : xcorr2sq *= xcorr2sq;
172 14666240 : energy2 = energy + z[i];
173 :
174 14666240 : if ( ( xcorr1sq * energy2 ) < ( xcorr2sq * energy1 ) )
175 : {
176 2675894 : n = i;
177 2675894 : xcorr1sq = xcorr2sq;
178 2675894 : energy1 = energy2;
179 : }
180 : }
181 :
182 1195909 : z[n]++;
183 1195909 : xcorr += abso_s[n];
184 1195909 : energy = energy1;
185 1195909 : P++;
186 : }
187 : }
188 : else
189 : {
190 446 : if ( L > 1 )
191 : {
192 446 : z[0] = (int16_t) ( Ptot * 0.5f );
193 446 : z[L - 1] = -( Ptot - z[0] );
194 446 : energy = 0.5f * ( z[0] * z[0] + z[L - 1] * z[L - 1] );
195 : }
196 : else
197 : {
198 0 : z[0] = Ptot;
199 0 : energy = 0.5f * ( z[0] * z[0] );
200 : }
201 : }
202 :
203 299764 : sscale = (float) A / (float) sqrt( 2.0f * energy );
204 4134116 : for ( i = 0; i < L; i++ )
205 : {
206 3834352 : ztak[i] = z[i] * sign_s[i];
207 3834352 : stak[i] = sscale * ztak[i];
208 : }
209 :
210 299764 : return;
211 : }
|