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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "cnst.h"
41 : #include "stl.h"
42 : #include "basop_util.h"
43 : #include "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * DecodeIndex()
49 : *
50 : *
51 : *-------------------------------------------------------------------*/
52 :
53 58212 : int16_t DecodeIndex(
54 : Decoder_State *st,
55 : const int16_t Bandwidth,
56 : int16_t *PeriodicityIndex )
57 : {
58 58212 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
59 58212 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
60 :
61 58212 : if ( ( hTcxDec->tcx_hm_LtpPitchLag > 0 ) && ( hTcxLtpDec->tcxltp_gain > kLtpHmGainThr ) )
62 : {
63 32118 : int16_t LtpPitchIndex = ( ( hTcxDec->tcx_hm_LtpPitchLag + ( 1 << ( kLtpHmFractionalResolution - 1 ) ) ) >> kLtpHmFractionalResolution ) - 2;
64 32118 : *PeriodicityIndex = kLtpHmFlag;
65 32118 : *PeriodicityIndex |= get_next_indice( st, NumRatioBits[Bandwidth][LtpPitchIndex] );
66 32118 : ++*PeriodicityIndex;
67 32118 : *PeriodicityIndex |= LtpPitchIndex << 9;
68 :
69 32118 : return NumRatioBits[Bandwidth][LtpPitchIndex];
70 : }
71 : else
72 : {
73 26094 : *PeriodicityIndex = get_next_indice( st, 8 );
74 :
75 26094 : return 8;
76 : }
77 : }
78 :
79 :
80 : /*-------------------------------------------------------------------*
81 : * tcx_hm_dequantize_gain()
82 : *
83 : *
84 : *-------------------------------------------------------------------*/
85 :
86 20118 : static int16_t tcx_hm_dequantize_gain(
87 : const int16_t coder_type, /* i : GC/VC coder type */
88 : const int16_t gain_idx, /* i : quantization index */
89 : Word16 *gain /* o : dequantized gain (Q11) */
90 : )
91 : {
92 20118 : assert( 0 <= coder_type && coder_type <= UNVOICED );
93 :
94 : /* safety check in case of bit errors */
95 20118 : if ( !( 0 <= gain_idx && gain_idx < ( 1 << kTcxHmNumGainBits ) ) )
96 : {
97 0 : *gain = 0;
98 0 : return 1;
99 : }
100 :
101 20118 : *gain = qGains[coder_type][gain_idx];
102 :
103 20118 : return 0;
104 : }
105 :
106 :
107 : /*-------------------------------------------------------------------*
108 : * tcx_hm_decode()
109 : *
110 : *
111 : *-------------------------------------------------------------------*/
112 :
113 20118 : void tcx_hm_decode(
114 : const int16_t L_frame, /* i : number of spectral lines */
115 : Word32 env[], /* i/o: envelope shape (Q16) */
116 : const int16_t targetBits, /* i : target bit budget */
117 : const int16_t coder_type, /* i : GC/VC coder type */
118 : const int16_t prm_hm[], /* i : HM parameters */
119 : const int16_t LtpPitchLag, /* i : LTP pitch lag or -1 if none */
120 : int16_t *hm_bits /* o : bit consumption */
121 : )
122 : {
123 : int32_t lag;
124 : int32_t tmpL;
125 : int16_t NumTargetBits, fract_res;
126 : Word16 p[2 * kTcxHmParabolaHalfWidth + 1], gain;
127 :
128 20118 : *hm_bits = 0;
129 :
130 20118 : if ( !( coder_type == VOICED || coder_type == GENERIC ) )
131 : {
132 : /* A bit error was encountered */
133 0 : *hm_bits = -1;
134 0 : return;
135 : }
136 :
137 20118 : NumTargetBits = CountIndexBits( L_frame >= 256, prm_hm[1] ) + targetBits;
138 :
139 20118 : if ( coder_type == VOICED )
140 : {
141 7014 : NumTargetBits += kTcxHmNumGainBits;
142 : }
143 20118 : *hm_bits = NumTargetBits - targetBits + 1;
144 :
145 : /* Convert the index to lag */
146 20118 : UnmapIndex( prm_hm[1], L_frame >= 256, LtpPitchLag, ( NumTargetBits <= kSmallerLagsTargetBitsThreshold ) || ( L_frame < 256 ), &fract_res, &tmpL );
147 20118 : lag = tmpL;
148 :
149 : /* Render the harmonic model */
150 20118 : if ( tcx_hm_render( lag, fract_res, p ) )
151 : {
152 : /* A bit error was encountered */
153 0 : *hm_bits = -1;
154 0 : return;
155 : }
156 :
157 : /* Dequantize gain */
158 :
159 20118 : if ( tcx_hm_dequantize_gain( coder_type == VOICED, prm_hm[2], &gain ) )
160 : {
161 : /* A bit error was encountered */
162 0 : *hm_bits = -1;
163 0 : return;
164 : }
165 20118 : tcx_hm_modify_envelope( gain, lag, fract_res, p, env, L_frame );
166 :
167 20118 : return;
168 : }
|