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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "isar_prot.h"
36 : #include "ivas_prot.h"
37 : #include "prot.h"
38 : #ifdef DEBUGGING
39 : #include "debug.h"
40 : #endif
41 : #include "wmc_auto.h"
42 :
43 :
44 : /*-------------------------------------------------------------------------
45 : * Function isar_splitBinLCLDDecOpen()
46 : *
47 : *
48 : *------------------------------------------------------------------------*/
49 :
50 664 : ivas_error isar_splitBinLCLDDecOpen(
51 : ISAR_BIN_HR_SPLIT_LCLD_DEC_HANDLE *hSplitBinLCLDDec, /* i/o: ISAR LCLD decoder handle */
52 : const int32_t iSampleRate,
53 : const int16_t iChannels,
54 : const int16_t iNumBlocks,
55 : const int16_t iNumIterations )
56 : {
57 : int16_t n;
58 : ISAR_BIN_HR_SPLIT_LCLD_DEC_HANDLE splitBinLCLDDec;
59 : ivas_error error;
60 :
61 664 : if ( ( splitBinLCLDDec = (ISAR_BIN_HR_SPLIT_LCLD_DEC_HANDLE) malloc( sizeof( ISAR_BIN_HR_SPLIT_LCLD_DEC ) ) ) == NULL )
62 : {
63 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD decoder Module \n" ) );
64 : }
65 :
66 664 : splitBinLCLDDec->pLcld_dec = NULL; /* place holder for CLDFB decoder handle */
67 :
68 664 : splitBinLCLDDec->iChannels = iChannels;
69 :
70 664 : if ( ( error = CreateLCLDDecoder( &splitBinLCLDDec->psLCLDDecoder, iSampleRate, iChannels, iNumBlocks, 0 ) ) != IVAS_ERR_OK )
71 : {
72 0 : return error;
73 : }
74 :
75 664 : if ( ( splitBinLCLDDec->pppfDecLCLDReal = (float ***) malloc( iChannels * sizeof( float ** ) ) ) == NULL )
76 : {
77 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD decoder Module \n" ) );
78 : }
79 664 : if ( ( splitBinLCLDDec->pppfDecLCLDImag = (float ***) malloc( iChannels * sizeof( float ** ) ) ) == NULL )
80 : {
81 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD decoder Module \n" ) );
82 : }
83 :
84 1992 : for ( n = 0; n < splitBinLCLDDec->iChannels; n++ )
85 : {
86 1328 : if ( ( splitBinLCLDDec->pppfDecLCLDReal[n] = (float **) malloc( CLDFB_NO_COL_MAX * sizeof( float * ) ) ) == NULL )
87 : {
88 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD decoder Module \n" ) );
89 : }
90 1328 : if ( ( splitBinLCLDDec->pppfDecLCLDImag[n] = (float **) malloc( CLDFB_NO_COL_MAX * sizeof( float * ) ) ) == NULL )
91 : {
92 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD decoder Module \n" ) );
93 : }
94 : }
95 :
96 : #ifdef CLDFB_DEBUG
97 : splitBinLCLDDec->numFrame = 0;
98 : char cldfbFilename[50] = "cldfb_out.bin";
99 : if ( ( splitBinLCLDDec->cldfbOut = fopen( cldfbFilename, "wb" ) ) == NULL )
100 : {
101 : fprintf( stderr, "Error: CLDFB bitstream file %s could not be opened\n\n", cldfbFilename );
102 : exit( -1 );
103 : }
104 : int16_t num_bands = CLDFB_NO_CHANNELS_MAX;
105 : fwrite( &iChannels, sizeof( int16_t ), 1, splitBinLCLDDec->cldfbOut );
106 : fwrite( &num_bands, sizeof( int16_t ), 1, splitBinLCLDDec->cldfbOut );
107 : #endif
108 :
109 664 : if ( ( error = isar_splitBinRendPLCOpen( &splitBinLCLDDec->hSplitRendPLC, GetNumSubSets( splitBinLCLDDec->psLCLDDecoder ) ) ) != IVAS_ERR_OK )
110 : {
111 0 : return error;
112 : }
113 664 : splitBinLCLDDec->iNumBlocks = iNumBlocks;
114 664 : splitBinLCLDDec->iNumIterations = iNumIterations;
115 :
116 664 : *hSplitBinLCLDDec = splitBinLCLDDec;
117 :
118 664 : return IVAS_ERR_OK;
119 : }
120 :
121 :
122 : /*-------------------------------------------------------------------------
123 : * Function isar_splitBinLCLDDecClose()
124 : *
125 : *
126 : *------------------------------------------------------------------------*/
127 :
128 664 : void isar_splitBinLCLDDecClose(
129 : ISAR_BIN_HR_SPLIT_LCLD_DEC_HANDLE *hSplitBinLCLDDec /* o : ISAR LCLD decoder handle */
130 : )
131 : {
132 : int16_t n;
133 :
134 664 : if ( ( *hSplitBinLCLDDec ) != NULL )
135 : {
136 664 : if ( ( *hSplitBinLCLDDec )->psLCLDDecoder != NULL )
137 : {
138 664 : DeleteLCLDDecoder( ( *hSplitBinLCLDDec )->psLCLDDecoder );
139 : }
140 :
141 1992 : for ( n = 0; n < ( *hSplitBinLCLDDec )->iChannels; n++ )
142 : {
143 1328 : free( ( *hSplitBinLCLDDec )->pppfDecLCLDReal[n] );
144 1328 : free( ( *hSplitBinLCLDDec )->pppfDecLCLDImag[n] );
145 : }
146 664 : free( ( *hSplitBinLCLDDec )->pppfDecLCLDReal );
147 664 : free( ( *hSplitBinLCLDDec )->pppfDecLCLDImag );
148 :
149 : #ifdef CLDFB_DEBUG
150 : if ( ( *hSplitBinLCLDDec )->cldfbOut != NULL )
151 : {
152 : fclose( ( *hSplitBinLCLDDec )->cldfbOut );
153 : }
154 : #endif
155 664 : isar_splitBinRendPLCClose( &( *hSplitBinLCLDDec )->hSplitRendPLC );
156 :
157 664 : free( *hSplitBinLCLDDec );
158 664 : *hSplitBinLCLDDec = NULL;
159 : }
160 :
161 664 : return;
162 : }
163 :
164 :
165 : /*-------------------------------------------------------------------------
166 : * Function isar_splitBinLCLDDecProcess()
167 : *
168 : *
169 : *------------------------------------------------------------------------*/
170 :
171 133227 : void isar_splitBinLCLDDecProcess(
172 : ISAR_BIN_HR_SPLIT_LCLD_DEC_HANDLE hSplitBinLCLDDec, /* i/o: ISAR LCLD decoder handle */
173 : ISAR_SPLIT_REND_BITS_HANDLE pBits, /* i/o: ISAR bits handle */
174 : float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : Binaural signals, real part */
175 : float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : Binaural signals, imag. part */
176 : const int16_t bfi /* i : BFI flag */
177 : )
178 : {
179 : int16_t k, n;
180 : int16_t itr;
181 133227 : push_wmops( "isar_splitBinLCLDDecProcess" );
182 :
183 133227 : assert( hSplitBinLCLDDec != NULL );
184 133227 : assert( Cldfb_Out_Real != NULL );
185 133227 : assert( Cldfb_Out_Imag != NULL );
186 133227 : assert( pBits != NULL );
187 :
188 : #ifdef CLDFB_DEBUG
189 : printf( "Bytes read = %d\n", iBytesWritten );
190 : #endif
191 133227 : if ( !bfi )
192 : {
193 266454 : for ( itr = 0; itr < hSplitBinLCLDDec->iNumIterations; itr++ )
194 : {
195 : /* Initialized with zeros....... */
196 399681 : for ( n = 0; n < hSplitBinLCLDDec->iChannels; n++ )
197 : {
198 4368734 : for ( k = 0; k < hSplitBinLCLDDec->iNumBlocks; k++ )
199 : {
200 4102280 : hSplitBinLCLDDec->pppfDecLCLDReal[n][k] = Cldfb_Out_Real[n][hSplitBinLCLDDec->iNumBlocks * itr + k];
201 4102280 : hSplitBinLCLDDec->pppfDecLCLDImag[n][k] = Cldfb_Out_Imag[n][hSplitBinLCLDDec->iNumBlocks * itr + k];
202 4102280 : set_f( hSplitBinLCLDDec->pppfDecLCLDReal[n][k], 0, CLDFB_NO_CHANNELS_MAX );
203 4102280 : set_f( hSplitBinLCLDDec->pppfDecLCLDImag[n][k], 0, CLDFB_NO_CHANNELS_MAX );
204 : }
205 : }
206 :
207 133227 : DecodeLCLDFrame( hSplitBinLCLDDec->psLCLDDecoder, pBits, hSplitBinLCLDDec->pppfDecLCLDReal, hSplitBinLCLDDec->pppfDecLCLDImag );
208 :
209 : #ifdef CLDFB_DEBUG
210 : printf( "Frame Decoded = %d\n", ++hSplitBinLCLDDec->numFrame );
211 : int16_t writeByte = 0;
212 : for ( k = 0; k < CLDFB_NO_COL_MAX; k++ )
213 : {
214 : for ( int16_t b = 0; b < CLDFB_NO_CHANNELS_MAX; b++ )
215 : {
216 : for ( n = 0; n < hSplitBinLCLDDec->iChannels; n++ )
217 : {
218 : writeByte = fwrite( &hSplitBinLCLDDec->pppfDecLCLDReal[n][k][b], sizeof( float ), 1, hSplitBinLCLDDec->cldfbOut );
219 : if ( writeByte != 1 )
220 : exit( -1 );
221 : writeByte = fwrite( &hSplitBinLCLDDec->pppfDecLCLDImag[n][k][b], sizeof( float ), 1, hSplitBinLCLDDec->cldfbOut );
222 : if ( writeByte != 1 )
223 : exit( -1 );
224 : }
225 : }
226 : }
227 : #endif
228 133227 : if ( AnyDecodingFailed( hSplitBinLCLDDec->psLCLDDecoder ) )
229 : {
230 : /* continue concealing */
231 0 : isar_splitBinRendPLC( hSplitBinLCLDDec->hSplitRendPLC, Cldfb_Out_Real, Cldfb_Out_Imag, (int16_t) hSplitBinLCLDDec->iChannels, hSplitBinLCLDDec->iNumBlocks,
232 0 : hSplitBinLCLDDec->iNumIterations, GetDecodingFailedStatus( hSplitBinLCLDDec->psLCLDDecoder ) );
233 : }
234 133227 : if ( AnyDecodingFailedPrev( hSplitBinLCLDDec->psLCLDDecoder ) )
235 : {
236 : /* cross-fade recovered frame into good frame */
237 0 : isar_splitBinRendPLC_xf( hSplitBinLCLDDec->hSplitRendPLC, Cldfb_Out_Real, Cldfb_Out_Imag, (int16_t) hSplitBinLCLDDec->iChannels, hSplitBinLCLDDec->iNumBlocks,
238 0 : hSplitBinLCLDDec->iNumIterations, GetDecodingFailedStatus( hSplitBinLCLDDec->psLCLDDecoder ), GetDecodingFailedPrevStatus( hSplitBinLCLDDec->psLCLDDecoder ) );
239 : }
240 : }
241 : }
242 : else
243 : {
244 : /* set states in decoder */
245 0 : SetDecodingUnresolved( hSplitBinLCLDDec->psLCLDDecoder );
246 :
247 : /* do PLC for lost split renderer frame */
248 0 : isar_splitBinRendPLC( hSplitBinLCLDDec->hSplitRendPLC, Cldfb_Out_Real, Cldfb_Out_Imag, (int16_t) hSplitBinLCLDDec->iChannels, hSplitBinLCLDDec->iNumBlocks,
249 0 : hSplitBinLCLDDec->iNumIterations, GetDecodingFailedStatus( hSplitBinLCLDDec->psLCLDDecoder ) );
250 : }
251 :
252 : /* save PLC state */
253 133227 : isar_splitBinRendPLCsaveState( hSplitBinLCLDDec->hSplitRendPLC, Cldfb_Out_Real, Cldfb_Out_Imag, (int16_t) hSplitBinLCLDDec->iChannels, hSplitBinLCLDDec->iNumBlocks, hSplitBinLCLDDec->iNumIterations );
254 :
255 133227 : pop_wmops();
256 :
257 133227 : return;
258 : }
|