1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /*
12  * decode_B.c
13  *
14  * This file contains definition of funtions for decoding.
15  * Decoding of lower-band, including normal-decoding and RCU decoding.
16  * Decoding of upper-band, including 8-12 kHz, when the bandwidth is
17  * 0-12 kHz, and 8-16 kHz, when the bandwidth is 0-16 kHz.
18  *
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "modules/audio_coding/codecs/isac/main/source/codec.h"
26 #include "modules/audio_coding/codecs/isac/main/source/entropy_coding.h"
27 #include "modules/audio_coding/codecs/isac/main/source/pitch_estimator.h"
28 #include "modules/audio_coding/codecs/isac/main/source/bandwidth_estimator.h"
29 #include "modules/audio_coding/codecs/isac/main/source/structs.h"
30 #include "modules/audio_coding/codecs/isac/main/source/settings.h"
31 #include "modules/audio_coding/codecs/isac/main/source/pitch_filter.h"
32 
33 /*
34  * function to decode the bitstream
35  * returns the total number of bytes in the stream
36  */
WebRtcIsac_DecodeLb(const TransformTables * transform_tables,float * signal_out,ISACLBDecStruct * ISACdecLB_obj,int16_t * current_framesamples,int16_t isRCUPayload)37 int WebRtcIsac_DecodeLb(const TransformTables* transform_tables,
38                         float* signal_out, ISACLBDecStruct* ISACdecLB_obj,
39                         int16_t* current_framesamples,
40                         int16_t isRCUPayload) {
41   int k;
42   int len, err;
43   int16_t bandwidthInd;
44 
45   float LP_dec_float[FRAMESAMPLES_HALF];
46   float HP_dec_float[FRAMESAMPLES_HALF];
47 
48   double LPw[FRAMESAMPLES_HALF];
49   double HPw[FRAMESAMPLES_HALF];
50   double LPw_pf[FRAMESAMPLES_HALF];
51 
52   double lo_filt_coef[(ORDERLO + 1)*SUBFRAMES];
53   double hi_filt_coef[(ORDERHI + 1)*SUBFRAMES];
54 
55   double real_f[FRAMESAMPLES_HALF];
56   double imag_f[FRAMESAMPLES_HALF];
57 
58   double PitchLags[4];
59   double PitchGains[4];
60   double AvgPitchGain;
61   int16_t PitchGains_Q12[4];
62   int16_t AvgPitchGain_Q12;
63 
64   float gain;
65 
66   int frame_nb; /* counter */
67   int frame_mode; /* 0 30ms, 1 for 60ms */
68   /* Processed_samples: 480 (30, 60 ms). Cannot take other values. */
69 
70   WebRtcIsac_ResetBitstream(&(ISACdecLB_obj->bitstr_obj));
71 
72   len = 0;
73 
74   /* Decode framelength and BW estimation - not used,
75      only for stream pointer*/
76   err = WebRtcIsac_DecodeFrameLen(&ISACdecLB_obj->bitstr_obj,
77                                   current_framesamples);
78   if (err < 0) {
79     return err;
80   }
81 
82   /* Frame_mode:
83    * 0: indicates 30 ms frame (480 samples)
84    * 1: indicates 60 ms frame (960 samples) */
85   frame_mode = *current_framesamples / MAX_FRAMESAMPLES;
86 
87   err = WebRtcIsac_DecodeSendBW(&ISACdecLB_obj->bitstr_obj, &bandwidthInd);
88   if (err < 0) {
89     return err;
90   }
91 
92   /* One loop if it's one frame (20 or 30ms), 2 loops if 2 frames
93      bundled together (60ms). */
94   for (frame_nb = 0; frame_nb <= frame_mode; frame_nb++) {
95     /* Decode & de-quantize pitch parameters */
96     err = WebRtcIsac_DecodePitchGain(&ISACdecLB_obj->bitstr_obj,
97                                      PitchGains_Q12);
98     if (err < 0) {
99       return err;
100     }
101 
102     err = WebRtcIsac_DecodePitchLag(&ISACdecLB_obj->bitstr_obj, PitchGains_Q12,
103                                     PitchLags);
104     if (err < 0) {
105       return err;
106     }
107 
108     AvgPitchGain_Q12 = (PitchGains_Q12[0] + PitchGains_Q12[1] +
109         PitchGains_Q12[2] + PitchGains_Q12[3]) >> 2;
110 
111     /* Decode & de-quantize filter coefficients. */
112     err = WebRtcIsac_DecodeLpc(&ISACdecLB_obj->bitstr_obj, lo_filt_coef,
113                                hi_filt_coef);
114     if (err < 0) {
115       return err;
116     }
117     /* Decode & de-quantize spectrum. */
118     len = WebRtcIsac_DecodeSpec(&ISACdecLB_obj->bitstr_obj, AvgPitchGain_Q12,
119                                 kIsacLowerBand, real_f, imag_f);
120     if (len < 0) {
121       return len;
122     }
123 
124     /* Inverse transform. */
125     WebRtcIsac_Spec2time(transform_tables, real_f, imag_f, LPw, HPw,
126                          &ISACdecLB_obj->fftstr_obj);
127 
128     /* Convert PitchGains back to float for pitchfilter_post */
129     for (k = 0; k < 4; k++) {
130       PitchGains[k] = ((float)PitchGains_Q12[k]) / 4096;
131     }
132     if (isRCUPayload) {
133       for (k = 0; k < 240; k++) {
134         LPw[k] *= RCU_TRANSCODING_SCALE_INVERSE;
135         HPw[k] *= RCU_TRANSCODING_SCALE_INVERSE;
136       }
137     }
138 
139     /* Inverse pitch filter. */
140     WebRtcIsac_PitchfilterPost(LPw, LPw_pf, &ISACdecLB_obj->pitchfiltstr_obj,
141                                PitchLags, PitchGains);
142     /* Convert AvgPitchGain back to float for computation of gain. */
143     AvgPitchGain = ((float)AvgPitchGain_Q12) / 4096;
144     gain = 1.0f - 0.45f * (float)AvgPitchGain;
145 
146     for (k = 0; k < FRAMESAMPLES_HALF; k++) {
147       /* Reduce gain to compensate for pitch enhancer. */
148       LPw_pf[k] *= gain;
149     }
150 
151     if (isRCUPayload) {
152       for (k = 0; k < FRAMESAMPLES_HALF; k++) {
153         /* Compensation for transcoding gain changes. */
154         LPw_pf[k] *= RCU_TRANSCODING_SCALE;
155         HPw[k] *= RCU_TRANSCODING_SCALE;
156       }
157     }
158     /* Perceptual post-filtering (using normalized lattice filter). */
159     WebRtcIsac_NormLatticeFilterAr(
160         ORDERLO, ISACdecLB_obj->maskfiltstr_obj.PostStateLoF,
161         (ISACdecLB_obj->maskfiltstr_obj).PostStateLoG, LPw_pf, lo_filt_coef,
162         LP_dec_float);
163     WebRtcIsac_NormLatticeFilterAr(
164         ORDERHI, ISACdecLB_obj->maskfiltstr_obj.PostStateHiF,
165         (ISACdecLB_obj->maskfiltstr_obj).PostStateHiG, HPw, hi_filt_coef,
166         HP_dec_float);
167 
168     /* Recombine the 2 bands. */
169     WebRtcIsac_FilterAndCombineFloat(LP_dec_float, HP_dec_float,
170                                      signal_out + frame_nb * FRAMESAMPLES,
171                                      &ISACdecLB_obj->postfiltbankstr_obj);
172   }
173   return len;
174 }
175 
176 
177 /*
178  * This decode function is called when the codec is operating in 16 kHz
179  * bandwidth to decode the upperband, i.e. 8-16 kHz.
180  *
181  * Contrary to lower-band, the upper-band (8-16 kHz) is not split in
182  * frequency, but split to 12 sub-frames, i.e. twice as lower-band.
183  */
WebRtcIsac_DecodeUb16(const TransformTables * transform_tables,float * signal_out,ISACUBDecStruct * ISACdecUB_obj,int16_t isRCUPayload)184 int WebRtcIsac_DecodeUb16(const TransformTables* transform_tables,
185                           float* signal_out, ISACUBDecStruct* ISACdecUB_obj,
186                           int16_t isRCUPayload) {
187   int len, err;
188 
189   double halfFrameFirst[FRAMESAMPLES_HALF];
190   double halfFrameSecond[FRAMESAMPLES_HALF];
191 
192   double percepFilterParam[(UB_LPC_ORDER + 1) * (SUBFRAMES << 1) +
193                            (UB_LPC_ORDER + 1)];
194 
195   double real_f[FRAMESAMPLES_HALF];
196   double imag_f[FRAMESAMPLES_HALF];
197   const int16_t kAveragePitchGain = 0; /* No pitch-gain for upper-band. */
198   len = 0;
199 
200   /* Decode & de-quantize filter coefficients. */
201   memset(percepFilterParam, 0, sizeof(percepFilterParam));
202   err = WebRtcIsac_DecodeInterpolLpcUb(&ISACdecUB_obj->bitstr_obj,
203                                        percepFilterParam, isac16kHz);
204   if (err < 0) {
205     return err;
206   }
207 
208   /* Decode & de-quantize spectrum. */
209   len = WebRtcIsac_DecodeSpec(&ISACdecUB_obj->bitstr_obj, kAveragePitchGain,
210                               kIsacUpperBand16, real_f, imag_f);
211   if (len < 0) {
212     return len;
213   }
214   if (isRCUPayload) {
215     int n;
216     for (n = 0; n < 240; n++) {
217       real_f[n] *= RCU_TRANSCODING_SCALE_UB_INVERSE;
218       imag_f[n] *= RCU_TRANSCODING_SCALE_UB_INVERSE;
219     }
220   }
221   /* Inverse transform. */
222   WebRtcIsac_Spec2time(transform_tables,
223                        real_f, imag_f, halfFrameFirst, halfFrameSecond,
224                        &ISACdecUB_obj->fftstr_obj);
225 
226   /* Perceptual post-filtering (using normalized lattice filter). */
227   WebRtcIsac_NormLatticeFilterAr(
228       UB_LPC_ORDER, ISACdecUB_obj->maskfiltstr_obj.PostStateLoF,
229       (ISACdecUB_obj->maskfiltstr_obj).PostStateLoG, halfFrameFirst,
230       &percepFilterParam[(UB_LPC_ORDER + 1)], signal_out);
231 
232   WebRtcIsac_NormLatticeFilterAr(
233       UB_LPC_ORDER, ISACdecUB_obj->maskfiltstr_obj.PostStateLoF,
234       (ISACdecUB_obj->maskfiltstr_obj).PostStateLoG, halfFrameSecond,
235       &percepFilterParam[(UB_LPC_ORDER + 1) * SUBFRAMES + (UB_LPC_ORDER + 1)],
236       &signal_out[FRAMESAMPLES_HALF]);
237 
238   return len;
239 }
240 
241 /*
242  * This decode function is called when the codec operates at 0-12 kHz
243  * bandwidth to decode the upperband, i.e. 8-12 kHz.
244  *
245  * At the encoder the upper-band is split into two band, 8-12 kHz & 12-16
246  * kHz, and only 8-12 kHz is encoded. At the decoder, 8-12 kHz band is
247  * reconstructed and 12-16 kHz replaced with zeros. Then two bands
248  * are combined, to reconstruct the upperband 8-16 kHz.
249  */
WebRtcIsac_DecodeUb12(const TransformTables * transform_tables,float * signal_out,ISACUBDecStruct * ISACdecUB_obj,int16_t isRCUPayload)250 int WebRtcIsac_DecodeUb12(const TransformTables* transform_tables,
251                           float* signal_out, ISACUBDecStruct* ISACdecUB_obj,
252                           int16_t isRCUPayload) {
253   int len, err;
254 
255   float LP_dec_float[FRAMESAMPLES_HALF];
256   float HP_dec_float[FRAMESAMPLES_HALF];
257 
258   double LPw[FRAMESAMPLES_HALF];
259   double HPw[FRAMESAMPLES_HALF];
260 
261   double percepFilterParam[(UB_LPC_ORDER + 1)*SUBFRAMES];
262 
263   double real_f[FRAMESAMPLES_HALF];
264   double imag_f[FRAMESAMPLES_HALF];
265   const int16_t kAveragePitchGain = 0; /* No pitch-gain for upper-band. */
266   len = 0;
267 
268   /* Decode & dequantize filter coefficients. */
269   err = WebRtcIsac_DecodeInterpolLpcUb(&ISACdecUB_obj->bitstr_obj,
270                                        percepFilterParam, isac12kHz);
271   if (err < 0) {
272     return err;
273   }
274 
275   /* Decode & de-quantize spectrum. */
276   len = WebRtcIsac_DecodeSpec(&ISACdecUB_obj->bitstr_obj, kAveragePitchGain,
277                               kIsacUpperBand12, real_f, imag_f);
278   if (len < 0) {
279     return len;
280   }
281 
282   if (isRCUPayload) {
283     int n;
284     for (n = 0; n < 240; n++) {
285       real_f[n] *= RCU_TRANSCODING_SCALE_UB_INVERSE;
286       imag_f[n] *= RCU_TRANSCODING_SCALE_UB_INVERSE;
287     }
288   }
289   /* Inverse transform. */
290   WebRtcIsac_Spec2time(transform_tables,
291                        real_f, imag_f, LPw, HPw, &ISACdecUB_obj->fftstr_obj);
292   /* perceptual post-filtering (using normalized lattice filter) */
293   WebRtcIsac_NormLatticeFilterAr(UB_LPC_ORDER,
294                                  ISACdecUB_obj->maskfiltstr_obj.PostStateLoF,
295                                  (ISACdecUB_obj->maskfiltstr_obj).PostStateLoG,
296                                  LPw, percepFilterParam, LP_dec_float);
297   /* Zero for 12-16 kHz. */
298   memset(HP_dec_float, 0, sizeof(float) * (FRAMESAMPLES_HALF));
299   /* Recombine the 2 bands. */
300   WebRtcIsac_FilterAndCombineFloat(HP_dec_float, LP_dec_float, signal_out,
301                                    &ISACdecUB_obj->postfiltbankstr_obj);
302   return len;
303 }
304