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 "cnst.h"
44 : #include "prot.h"
45 : #include "rom_enc.h"
46 : #include "rom_com.h"
47 : #include "wmc_auto.h"
48 :
49 : /*--------------------------------------------------------------------------*
50 : * hvq_enc()
51 : *
52 : * Harmonic VQ encoder
53 : *--------------------------------------------------------------------------*/
54 :
55 : /*! r: Consumed bits */
56 1327 : int16_t hvq_enc(
57 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
58 : const int16_t bwidth, /* i : audio bandwidth */
59 : #ifdef DEBUGGING
60 : const int16_t idchan, /* i : channel ID */
61 : #endif
62 : const int32_t core_brate, /* i : core bitrate */
63 : const int16_t hvq_bits, /* i : HVQ bit budget */
64 : const int16_t Npeaks, /* i : Number of peaks */
65 : const int16_t *ynrm, /* i : Envelope coefficients */
66 : int16_t *R, /* i/o: Bit allocation/updated bit allocation */
67 : int16_t *peaks, /* i/o: Peak pos. / Encoded peak pos. */
68 : float *nf_gains, /* i/o: Noise fill gains / Quant. nf gains */
69 : float *noise_level, /* o : Quantized noise level */
70 : const float *pe_gains, /* i : Peak gains */
71 : const float *coefs, /* i : spectrum coefficients */
72 : float *coefs_out /* o : encoded spectrum coefficients */
73 : )
74 : {
75 : int16_t bin_th, q, j, i;
76 : int16_t nf_cnt;
77 : int16_t q_noise_level_idx[HVQ_BWE_NOISE_BANDS];
78 : float q_noise_level[HVQ_BWE_NOISE_BANDS];
79 : float d, nf, nf_mean, pe, pe_mean, nfpe;
80 : int16_t bits_used;
81 : float lb_nfpe;
82 :
83 1327 : bits_used = 0;
84 :
85 1327 : bin_th = HVQ_THRES_BIN_32k;
86 1327 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
87 : {
88 770 : bin_th = HVQ_THRES_BIN_24k;
89 : }
90 :
91 1327 : nf = 800;
92 1327 : pe = 800;
93 1327 : q = bin_th;
94 :
95 : /* Find HB noise level */
96 3981 : for ( i = 0; i < HVQ_BWE_NOISE_BANDS; i++ )
97 : {
98 2654 : nf_cnt = 0;
99 2654 : nf_mean = EPSILON;
100 2654 : pe_mean = EPSILON;
101 501214 : for ( j = 0; j < ( L_FRAME32k - bin_th ) / HVQ_BWE_NOISE_BANDS; j++, q++ )
102 : {
103 498560 : d = (float) fabs( coefs[q] );
104 :
105 498560 : if ( d > pe )
106 : {
107 28078 : pe = HVQ_BWE_WEIGHT2 * pe + ( 1 - HVQ_BWE_WEIGHT2 ) * d;
108 : }
109 : else
110 : {
111 470482 : pe = HVQ_BWE_WEIGHT1 * pe + ( 1 - HVQ_BWE_WEIGHT1 ) * d;
112 470482 : if ( d > nf )
113 : {
114 353047 : nf = HVQ_BWE_WEIGHT1 * nf + ( 1 - HVQ_BWE_WEIGHT1 ) * d;
115 : }
116 : else
117 : {
118 117435 : nf = HVQ_BWE_WEIGHT2 * nf + ( 1 - HVQ_BWE_WEIGHT2 ) * d;
119 : }
120 470482 : nf_mean += nf;
121 470482 : nf_cnt++;
122 : }
123 :
124 498560 : pe_mean += pe;
125 : }
126 :
127 2654 : if ( nf_cnt > 0 )
128 : {
129 2654 : nf_mean /= nf_cnt;
130 : }
131 2654 : pe_mean /= ( L_FRAME32k - bin_th ) / HVQ_BWE_NOISE_BANDS;
132 :
133 2654 : nfpe = HVQ_NFPE_FACTOR * nf_mean / pe_mean;
134 2654 : noise_level[i] = nfpe * nfpe * nfpe;
135 :
136 2654 : q_noise_level_idx[i] = usquant( noise_level[i], &q_noise_level[i], 0.0f, 0.1f, 4 );
137 2654 : push_indice( hBstr, IND_HVQ_BWE_NL, q_noise_level_idx[i], 2 );
138 2654 : bits_used += 2;
139 :
140 2654 : noise_level[i] = q_noise_level[i];
141 : }
142 :
143 3981 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
144 : {
145 2654 : lb_nfpe = HVQ_LB_NFPE_FACTOR * nf_gains[i] / pe_gains[i];
146 2654 : lb_nfpe = lb_nfpe * lb_nfpe * lb_nfpe;
147 :
148 2654 : if ( lb_nfpe > 0.5f )
149 : {
150 128 : lb_nfpe = 0.5f;
151 : }
152 2654 : nf_gains[i] *= 2 * lb_nfpe;
153 : }
154 :
155 : #ifdef DEBUGGING
156 : bits_used += peak_vq_enc( hBstr, bwidth, idchan, coefs, coefs_out, core_brate, hvq_bits - bits_used, Npeaks, ynrm, R, peaks, &nf_gains[0] );
157 : #else
158 1327 : bits_used += peak_vq_enc( hBstr, bwidth, coefs, coefs_out, core_brate, hvq_bits - bits_used, Npeaks, ynrm, R, peaks, &nf_gains[0] );
159 : #endif
160 :
161 :
162 1327 : return bits_used;
163 : }
|