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 "cnst.h"
43 : #include "prot.h"
44 : #include "wmc_auto.h"
45 :
46 : /*-------------------------------------------------*
47 : * Local constants
48 : *-------------------------------------------------*/
49 :
50 : #define LIMIT_PIT_REL_LOWER 2 /* delta interval to extend pitch coding in relative Q */
51 : #define LIMIT_PIT_REL_UPPER 0
52 :
53 : /*-------------------------------------------------*
54 : * limit_T0()
55 : *
56 : * Close-loop pitch lag search limitation
57 : *-------------------------------------------------*/
58 :
59 19632109 : void limit_T0(
60 : const int16_t L_frame, /* i : length of the frame */
61 : const int16_t delta, /* i : Half the close-loop searched interval */
62 : const int16_t pit_flag, /* i : selecting absolute(0) or delta(1) pitch quantization */
63 : const int16_t limit_flag, /* i : flag for Q limits (0=restrained, 1=extended) */
64 : const int16_t T0, /* i : rough pitch estimate around which the search is done */
65 : const int16_t T0_frac, /* i : pitch estimate fractional part */
66 : int16_t *T0_min, /* o : lower pitch limit */
67 : int16_t *T0_max /* o : higher pitch limit */
68 : )
69 : {
70 : int16_t delta2, T1;
71 : int16_t pit_min, pit_max;
72 :
73 19632109 : if ( limit_flag == 0 ) /* restrained Q limits */
74 : {
75 : /* set limits */
76 2574730 : if ( L_frame == L_FRAME )
77 : {
78 2396065 : pit_max = PIT_MAX;
79 2396065 : pit_min = PIT_MIN;
80 : }
81 : else /* L_frame == L_FRAME16k */
82 : {
83 178665 : pit_max = PIT16k_MAX;
84 178665 : pit_min = PIT16k_MIN;
85 : }
86 :
87 2574730 : delta2 = 2 * delta - 1;
88 :
89 2574730 : T1 = T0;
90 2574730 : if ( T0_frac >= 2 )
91 : {
92 939595 : T1++;
93 : }
94 2574730 : *T0_min = T1 - delta;
95 :
96 2574730 : if ( *T0_min < pit_min )
97 : {
98 163890 : *T0_min = pit_min;
99 : }
100 2574730 : *T0_max = *T0_min + delta2;
101 :
102 2574730 : if ( *T0_max > pit_max )
103 : {
104 20152 : *T0_max = pit_max;
105 20152 : *T0_min = *T0_max - delta2;
106 : }
107 : }
108 : else /* extended Q limits */
109 : {
110 :
111 : /* set limits */
112 17057379 : if ( L_frame == L_FRAME )
113 : {
114 5989711 : pit_max = PIT_MAX;
115 5989711 : pit_min = PIT_MIN_EXTEND;
116 :
117 5989711 : if ( limit_flag == 2 )
118 : {
119 933778 : pit_min = PIT_MIN_DOUBLEEXTEND;
120 : }
121 : }
122 : else /* L_frame == L_FRAME16k */
123 : {
124 11067668 : pit_max = PIT16k_MAX;
125 11067668 : pit_min = PIT16k_MIN_EXTEND;
126 : }
127 :
128 17057379 : delta2 = 2 * delta - 1;
129 :
130 17057379 : T1 = T0;
131 17057379 : if ( T0_frac >= 2 )
132 : {
133 7475223 : T1++;
134 : }
135 17057379 : *T0_min = T1 - delta;
136 :
137 17057379 : if ( pit_flag == 0 )
138 : {
139 : /* subframes with absolute search: keep Q range */
140 2116416 : if ( *T0_min < pit_min )
141 : {
142 101890 : *T0_min = pit_min;
143 : }
144 2116416 : *T0_max = *T0_min + delta2;
145 :
146 2116416 : if ( *T0_max > pit_max )
147 : {
148 23443 : *T0_max = pit_max;
149 23443 : *T0_min = *T0_max - delta2;
150 : }
151 : }
152 : else
153 : {
154 : /* subframes with relative search: extend Q range */
155 14940963 : if ( *T0_min < pit_min - LIMIT_PIT_REL_LOWER )
156 : {
157 406873 : *T0_min = pit_min - LIMIT_PIT_REL_LOWER;
158 : }
159 :
160 14940963 : if ( *T0_min < L_INTERPOL )
161 : {
162 1666 : *T0_min = L_INTERPOL;
163 : }
164 14940963 : *T0_max = *T0_min + delta2;
165 :
166 14940963 : if ( *T0_max > pit_max + LIMIT_PIT_REL_UPPER )
167 : {
168 98769 : *T0_max = pit_max + LIMIT_PIT_REL_UPPER;
169 98769 : *T0_min = *T0_max - delta2;
170 : }
171 : }
172 : }
173 :
174 19632109 : return;
175 : }
176 :
177 :
178 : /*-------------------------------------------------*
179 : * Routine limit_T0_voiced()
180 : *
181 : * Close-loop pitch lag search limitation
182 : *-------------------------------------------------*/
183 :
184 115954 : void limit_T0_voiced(
185 : const int16_t nbits,
186 : const int16_t res,
187 : const int16_t T0, /* i : rough pitch estimate around which the search is done */
188 : const int16_t T0_frac, /* i : pitch estimate fractional part */
189 : const int16_t T0_res, /* i : pitch resolution */
190 : int16_t *T0_min, /* o : lower pitch limit */
191 : int16_t *T0_min_frac, /* o : lower pitch limit */
192 : int16_t *T0_max, /* o : higher pitch limit */
193 : int16_t *T0_max_frac, /* o : higher pitch limit */
194 : const int16_t pit_min, /* i : Minimum pitch lag */
195 : const int16_t pit_max /* i : Maximum pitch lag */
196 : )
197 : {
198 : int16_t T1, temp1, temp2;
199 :
200 : /* Mid-point */
201 115954 : T1 = T0;
202 115954 : if ( ( T0_res > 1 ) && ( T0_frac >= ( T0_res >> 1 ) ) )
203 : {
204 7071 : T1++;
205 : }
206 :
207 : /* Lower-bound */
208 115954 : temp1 = ( T1 * res ) - ( 1 << ( nbits - 1 ) );
209 115954 : temp2 = temp1 / res;
210 115954 : *T0_min = temp2;
211 115954 : *T0_min_frac = temp1 - temp2 * res;
212 115954 : if ( *T0_min < pit_min )
213 : {
214 15982 : *T0_min = pit_min;
215 15982 : *T0_min_frac = 0;
216 : }
217 :
218 : /* Higher-bound */
219 115954 : temp1 = ( *T0_min * res ) + *T0_min_frac + ( 1 << nbits ) - 1;
220 115954 : temp2 = temp1 / res;
221 115954 : *T0_max = temp2;
222 115954 : *T0_max_frac = temp1 - temp2 * res;
223 115954 : if ( *T0_max > pit_max )
224 : {
225 976 : *T0_max = pit_max;
226 976 : *T0_max_frac = res - 1;
227 976 : temp1 = ( *T0_max * res ) + *T0_max_frac - ( 1 << nbits ) + 1;
228 976 : temp2 = temp1 / res;
229 976 : *T0_min = temp2;
230 976 : *T0_min_frac = temp1 - temp2 * res;
231 : }
232 :
233 115954 : return;
234 : }
|