Coverage Report

Created: 2025-11-09 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/quant_bands.c
Line
Count
Source
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
4
/*
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
9
   - Redistributions of source code must retain the above copyright
10
   notice, this list of conditions and the following disclaimer.
11
12
   - Redistributions in binary form must reproduce the above copyright
13
   notice, this list of conditions and the following disclaimer in the
14
   documentation and/or other materials provided with the distribution.
15
16
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
#endif
32
33
#include "quant_bands.h"
34
#include "laplace.h"
35
#include <math.h>
36
#include "os_support.h"
37
#include "arch.h"
38
#include "mathops.h"
39
#include "stack_alloc.h"
40
#include "rate.h"
41
42
#ifdef FIXED_POINT
43
/* Mean energy in each band quantized in Q4 */
44
const signed char eMeans[25] = {
45
      103,100, 92, 85, 81,
46
       77, 72, 70, 78, 75,
47
       73, 71, 78, 74, 69,
48
       72, 70, 74, 76, 71,
49
       60, 60, 60, 60, 60
50
};
51
#else
52
/* Mean energy in each band quantized in Q4 and converted back to float */
53
const opus_val16 eMeans[25] = {
54
      6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f,
55
      4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f,
56
      4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f,
57
      4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f,
58
      3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f
59
};
60
#endif
61
/* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */
62
#ifdef FIXED_POINT
63
static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384};
64
static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554};
65
static const opus_val16 beta_intra = 4915;
66
#else
67
static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
68
static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
69
static const opus_val16 beta_intra = 4915/32768.;
70
#endif
71
72
/*Parameters of the Laplace-like probability models used for the coarse energy.
73
  There is one pair of parameters for each frame size, prediction type
74
   (inter/intra), and band number.
75
  The first number of each pair is the probability of 0, and the second is the
76
   decay rate, both in Q8 precision.*/
77
static const unsigned char e_prob_model[4][2][42] = {
78
   /*120 sample frames.*/
79
   {
80
      /*Inter*/
81
      {
82
          72, 127,  65, 129,  66, 128,  65, 128,  64, 128,  62, 128,  64, 128,
83
          64, 128,  92,  78,  92,  79,  92,  78,  90,  79, 116,  41, 115,  40,
84
         114,  40, 132,  26, 132,  26, 145,  17, 161,  12, 176,  10, 177,  11
85
      },
86
      /*Intra*/
87
      {
88
          24, 179,  48, 138,  54, 135,  54, 132,  53, 134,  56, 133,  55, 132,
89
          55, 132,  61, 114,  70,  96,  74,  88,  75,  88,  87,  74,  89,  66,
90
          91,  67, 100,  59, 108,  50, 120,  40, 122,  37,  97,  43,  78,  50
91
      }
92
   },
93
   /*240 sample frames.*/
94
   {
95
      /*Inter*/
96
      {
97
          83,  78,  84,  81,  88,  75,  86,  74,  87,  71,  90,  73,  93,  74,
98
          93,  74, 109,  40, 114,  36, 117,  34, 117,  34, 143,  17, 145,  18,
99
         146,  19, 162,  12, 165,  10, 178,   7, 189,   6, 190,   8, 177,   9
100
      },
101
      /*Intra*/
102
      {
103
          23, 178,  54, 115,  63, 102,  66,  98,  69,  99,  74,  89,  71,  91,
104
          73,  91,  78,  89,  86,  80,  92,  66,  93,  64, 102,  59, 103,  60,
105
         104,  60, 117,  52, 123,  44, 138,  35, 133,  31,  97,  38,  77,  45
106
      }
107
   },
108
   /*480 sample frames.*/
109
   {
110
      /*Inter*/
111
      {
112
          61,  90,  93,  60, 105,  42, 107,  41, 110,  45, 116,  38, 113,  38,
113
         112,  38, 124,  26, 132,  27, 136,  19, 140,  20, 155,  14, 159,  16,
114
         158,  18, 170,  13, 177,  10, 187,   8, 192,   6, 175,   9, 159,  10
115
      },
116
      /*Intra*/
117
      {
118
          21, 178,  59, 110,  71,  86,  75,  85,  84,  83,  91,  66,  88,  73,
119
          87,  72,  92,  75,  98,  72, 105,  58, 107,  54, 115,  52, 114,  55,
120
         112,  56, 129,  51, 132,  40, 150,  33, 140,  29,  98,  35,  77,  42
121
      }
122
   },
123
   /*960 sample frames.*/
124
   {
125
      /*Inter*/
126
      {
127
          42, 121,  96,  66, 108,  43, 111,  40, 117,  44, 123,  32, 120,  36,
128
         119,  33, 127,  33, 134,  34, 139,  21, 147,  23, 152,  20, 158,  25,
129
         154,  26, 166,  21, 173,  16, 184,  13, 184,  10, 150,  13, 139,  15
130
      },
131
      /*Intra*/
132
      {
133
          22, 178,  63, 114,  74,  82,  84,  83,  92,  82, 103,  62,  96,  72,
134
          96,  67, 101,  73, 107,  72, 113,  55, 118,  52, 125,  52, 118,  52,
135
         117,  55, 135,  49, 137,  39, 157,  32, 145,  29,  97,  33,  77,  40
136
      }
137
   }
138
};
139
140
static const unsigned char small_energy_icdf[3]={2,1,0};
141
142
static opus_val32 loss_distortion(const celt_glog *eBands, celt_glog *oldEBands, int start, int end, int len, int C)
143
664k
{
144
664k
   int c, i;
145
664k
   opus_val32 dist = 0;
146
880k
   c=0; do {
147
13.0M
      for (i=start;i<end;i++)
148
12.1M
      {
149
12.1M
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
12.1M
         dist = MAC16_16(dist, d,d);
151
12.1M
      }
152
880k
   } while (++c<C);
153
664k
   return MIN32(200,SHR32(dist,14));
154
664k
}
quant_bands.c:loss_distortion
Line
Count
Source
143
332k
{
144
332k
   int c, i;
145
332k
   opus_val32 dist = 0;
146
440k
   c=0; do {
147
6.52M
      for (i=start;i<end;i++)
148
6.08M
      {
149
6.08M
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
6.08M
         dist = MAC16_16(dist, d,d);
151
6.08M
      }
152
440k
   } while (++c<C);
153
332k
   return MIN32(200,SHR32(dist,14));
154
332k
}
quant_bands.c:loss_distortion
Line
Count
Source
143
332k
{
144
332k
   int c, i;
145
332k
   opus_val32 dist = 0;
146
440k
   c=0; do {
147
6.52M
      for (i=start;i<end;i++)
148
6.08M
      {
149
6.08M
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
6.08M
         dist = MAC16_16(dist, d,d);
151
6.08M
      }
152
440k
   } while (++c<C);
153
332k
   return MIN32(200,SHR32(dist,14));
154
332k
}
155
156
static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
157
      const celt_glog *eBands, celt_glog *oldEBands,
158
      opus_int32 budget, opus_int32 tell,
159
      const unsigned char *prob_model, celt_glog *error, ec_enc *enc,
160
      int C, int LM, int intra, celt_glog max_decay, int lfe)
161
488k
{
162
488k
   int i, c;
163
488k
   int badness = 0;
164
488k
   opus_val32 prev[2] = {0,0};
165
488k
   opus_val16 coef;
166
488k
   opus_val16 beta;
167
168
488k
   if (tell+3 <= budget)
169
416k
      ec_enc_bit_logp(enc, intra, 3);
170
488k
   if (intra)
171
225k
   {
172
225k
      coef = 0;
173
225k
      beta = beta_intra;
174
263k
   } else {
175
263k
      beta = beta_coef[LM];
176
263k
      coef = pred_coef[LM];
177
263k
   }
178
179
   /* Encode at a fixed coarse resolution */
180
7.26M
   for (i=start;i<end;i++)
181
6.77M
   {
182
6.77M
      c=0;
183
9.08M
      do {
184
9.08M
         int bits_left;
185
9.08M
         int qi, qi0;
186
9.08M
         opus_val32 q;
187
9.08M
         celt_glog x;
188
9.08M
         opus_val32 f, tmp;
189
9.08M
         celt_glog oldE;
190
9.08M
         celt_glog decay_bound;
191
9.08M
         x = eBands[i+c*m->nbEBands];
192
9.08M
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
#ifdef FIXED_POINT
194
4.97M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
4.97M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
4.97M
         decay_bound = MAXG(-GCONST(28.f), SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay));
198
#else
199
         f = x-coef*oldE-prev[c];
200
         /* Rounding to nearest integer here is really important! */
201
         qi = (int)floor(.5f+f);
202
4.10M
         decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay;
203
#endif
204
         /* Prevent the energy from going down too quickly (e.g. for bands
205
            that have just one bin) */
206
9.08M
         if (qi < 0 && x < decay_bound)
207
1.45M
         {
208
1.45M
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
1.45M
            if (qi > 0)
210
387k
               qi = 0;
211
1.45M
         }
212
9.08M
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
9.08M
         tell = ec_tell(enc);
216
9.08M
         bits_left = budget-tell-3*C*(end-i);
217
9.08M
         if (i!=start && bits_left < 30)
218
1.47M
         {
219
1.47M
            if (bits_left < 24)
220
1.41M
               qi = IMIN(1, qi);
221
1.47M
            if (bits_left < 16)
222
1.36M
               qi = IMAX(-1, qi);
223
1.47M
         }
224
9.08M
         if (lfe && i>=2)
225
22.4k
            qi = IMIN(qi, 0);
226
9.08M
         if (budget-tell >= 15)
227
7.76M
         {
228
7.76M
            int pi;
229
7.76M
            pi = 2*IMIN(i,20);
230
7.76M
            ec_laplace_encode(enc, &qi,
231
7.76M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
7.76M
         }
233
1.32M
         else if(budget-tell >= 2)
234
45.4k
         {
235
45.4k
            qi = IMAX(-1, IMIN(qi, 1));
236
45.4k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
45.4k
         }
238
1.27M
         else if(budget-tell >= 1)
239
2.27k
         {
240
2.27k
            qi = IMIN(0, qi);
241
2.27k
            ec_enc_bit_logp(enc, -qi, 1);
242
2.27k
         }
243
1.27M
         else
244
1.27M
            qi = -1;
245
9.08M
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
9.08M
         badness += abs(qi0-qi);
247
9.08M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
9.08M
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
4.97M
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
9.08M
         oldEBands[i+c*m->nbEBands] = tmp;
254
9.08M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
9.08M
      } while (++c < C);
256
6.77M
   }
257
488k
   return lfe ? 0 : badness;
258
488k
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
262k
{
162
262k
   int i, c;
163
262k
   int badness = 0;
164
262k
   opus_val32 prev[2] = {0,0};
165
262k
   opus_val16 coef;
166
262k
   opus_val16 beta;
167
168
262k
   if (tell+3 <= budget)
169
232k
      ec_enc_bit_logp(enc, intra, 3);
170
262k
   if (intra)
171
124k
   {
172
124k
      coef = 0;
173
124k
      beta = beta_intra;
174
138k
   } else {
175
138k
      beta = beta_coef[LM];
176
138k
      coef = pred_coef[LM];
177
138k
   }
178
179
   /* Encode at a fixed coarse resolution */
180
3.88M
   for (i=start;i<end;i++)
181
3.62M
   {
182
3.62M
      c=0;
183
4.97M
      do {
184
4.97M
         int bits_left;
185
4.97M
         int qi, qi0;
186
4.97M
         opus_val32 q;
187
4.97M
         celt_glog x;
188
4.97M
         opus_val32 f, tmp;
189
4.97M
         celt_glog oldE;
190
4.97M
         celt_glog decay_bound;
191
4.97M
         x = eBands[i+c*m->nbEBands];
192
4.97M
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
4.97M
#ifdef FIXED_POINT
194
4.97M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
4.97M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
4.97M
         decay_bound = MAXG(-GCONST(28.f), SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay));
198
#else
199
         f = x-coef*oldE-prev[c];
200
         /* Rounding to nearest integer here is really important! */
201
         qi = (int)floor(.5f+f);
202
         decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay;
203
#endif
204
         /* Prevent the energy from going down too quickly (e.g. for bands
205
            that have just one bin) */
206
4.97M
         if (qi < 0 && x < decay_bound)
207
725k
         {
208
725k
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
725k
            if (qi > 0)
210
189k
               qi = 0;
211
725k
         }
212
4.97M
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
4.97M
         tell = ec_tell(enc);
216
4.97M
         bits_left = budget-tell-3*C*(end-i);
217
4.97M
         if (i!=start && bits_left < 30)
218
646k
         {
219
646k
            if (bits_left < 24)
220
619k
               qi = IMIN(1, qi);
221
646k
            if (bits_left < 16)
222
589k
               qi = IMAX(-1, qi);
223
646k
         }
224
4.97M
         if (lfe && i>=2)
225
12.0k
            qi = IMIN(qi, 0);
226
4.97M
         if (budget-tell >= 15)
227
4.41M
         {
228
4.41M
            int pi;
229
4.41M
            pi = 2*IMIN(i,20);
230
4.41M
            ec_laplace_encode(enc, &qi,
231
4.41M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
4.41M
         }
233
565k
         else if(budget-tell >= 2)
234
22.1k
         {
235
22.1k
            qi = IMAX(-1, IMIN(qi, 1));
236
22.1k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
22.1k
         }
238
542k
         else if(budget-tell >= 1)
239
1.12k
         {
240
1.12k
            qi = IMIN(0, qi);
241
1.12k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.12k
         }
243
541k
         else
244
541k
            qi = -1;
245
4.97M
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
4.97M
         badness += abs(qi0-qi);
247
4.97M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
4.97M
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
4.97M
#ifdef FIXED_POINT
251
4.97M
         tmp = MAX32(-GCONST(28.f), tmp);
252
4.97M
#endif
253
4.97M
         oldEBands[i+c*m->nbEBands] = tmp;
254
4.97M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
4.97M
      } while (++c < C);
256
3.62M
   }
257
262k
   return lfe ? 0 : badness;
258
262k
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
225k
{
162
225k
   int i, c;
163
225k
   int badness = 0;
164
225k
   opus_val32 prev[2] = {0,0};
165
225k
   opus_val16 coef;
166
225k
   opus_val16 beta;
167
168
225k
   if (tell+3 <= budget)
169
184k
      ec_enc_bit_logp(enc, intra, 3);
170
225k
   if (intra)
171
100k
   {
172
100k
      coef = 0;
173
100k
      beta = beta_intra;
174
125k
   } else {
175
125k
      beta = beta_coef[LM];
176
125k
      coef = pred_coef[LM];
177
125k
   }
178
179
   /* Encode at a fixed coarse resolution */
180
3.37M
   for (i=start;i<end;i++)
181
3.15M
   {
182
3.15M
      c=0;
183
4.10M
      do {
184
4.10M
         int bits_left;
185
4.10M
         int qi, qi0;
186
4.10M
         opus_val32 q;
187
4.10M
         celt_glog x;
188
4.10M
         opus_val32 f, tmp;
189
4.10M
         celt_glog oldE;
190
4.10M
         celt_glog decay_bound;
191
4.10M
         x = eBands[i+c*m->nbEBands];
192
4.10M
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
#ifdef FIXED_POINT
194
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
         decay_bound = MAXG(-GCONST(28.f), SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay));
198
#else
199
4.10M
         f = x-coef*oldE-prev[c];
200
         /* Rounding to nearest integer here is really important! */
201
4.10M
         qi = (int)floor(.5f+f);
202
4.10M
         decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay;
203
4.10M
#endif
204
         /* Prevent the energy from going down too quickly (e.g. for bands
205
            that have just one bin) */
206
4.10M
         if (qi < 0 && x < decay_bound)
207
724k
         {
208
724k
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
724k
            if (qi > 0)
210
197k
               qi = 0;
211
724k
         }
212
4.10M
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
4.10M
         tell = ec_tell(enc);
216
4.10M
         bits_left = budget-tell-3*C*(end-i);
217
4.10M
         if (i!=start && bits_left < 30)
218
826k
         {
219
826k
            if (bits_left < 24)
220
798k
               qi = IMIN(1, qi);
221
826k
            if (bits_left < 16)
222
770k
               qi = IMAX(-1, qi);
223
826k
         }
224
4.10M
         if (lfe && i>=2)
225
10.3k
            qi = IMIN(qi, 0);
226
4.10M
         if (budget-tell >= 15)
227
3.35M
         {
228
3.35M
            int pi;
229
3.35M
            pi = 2*IMIN(i,20);
230
3.35M
            ec_laplace_encode(enc, &qi,
231
3.35M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
3.35M
         }
233
755k
         else if(budget-tell >= 2)
234
23.3k
         {
235
23.3k
            qi = IMAX(-1, IMIN(qi, 1));
236
23.3k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
23.3k
         }
238
732k
         else if(budget-tell >= 1)
239
1.14k
         {
240
1.14k
            qi = IMIN(0, qi);
241
1.14k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.14k
         }
243
731k
         else
244
731k
            qi = -1;
245
4.10M
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
4.10M
         badness += abs(qi0-qi);
247
4.10M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
4.10M
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
4.10M
         oldEBands[i+c*m->nbEBands] = tmp;
254
4.10M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
4.10M
      } while (++c < C);
256
3.15M
   }
257
225k
   return lfe ? 0 : badness;
258
225k
}
259
260
void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
261
      const celt_glog *eBands, celt_glog *oldEBands, opus_uint32 budget,
262
      celt_glog *error, ec_enc *enc, int C, int LM, int nbAvailableBytes,
263
      int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, int lfe)
264
664k
{
265
664k
   int intra;
266
664k
   celt_glog max_decay;
267
664k
   VARDECL(celt_glog, oldEBands_intra);
268
664k
   VARDECL(celt_glog, error_intra);
269
664k
   ec_enc enc_start_state;
270
664k
   opus_uint32 tell;
271
664k
   int badness1=0;
272
664k
   opus_int32 intra_bias;
273
664k
   opus_val32 new_distortion;
274
664k
   SAVE_STACK;
275
276
664k
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
664k
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
664k
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
664k
   tell = ec_tell(enc);
281
664k
   if (tell+3 > budget)
282
144k
      two_pass = intra = 0;
283
284
664k
   max_decay = GCONST(16.f);
285
664k
   if (end-start>10)
286
627k
   {
287
#ifdef FIXED_POINT
288
313k
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
313k
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
#endif
292
627k
   }
293
664k
   if (lfe)
294
2.85k
      max_decay = GCONST(3.f);
295
664k
   enc_start_state = *enc;
296
297
664k
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
664k
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
664k
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
664k
   if (two_pass || intra)
302
450k
   {
303
450k
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
450k
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
450k
   }
306
307
664k
   if (!intra)
308
527k
   {
309
527k
      unsigned char *intra_buf;
310
527k
      ec_enc enc_intra_state;
311
527k
      opus_int32 tell_intra;
312
527k
      opus_uint32 nstart_bytes;
313
527k
      opus_uint32 nintra_bytes;
314
527k
      opus_uint32 save_bytes;
315
527k
      int badness2;
316
527k
      VARDECL(unsigned char, intra_bits);
317
318
527k
      tell_intra = ec_tell_frac(enc);
319
320
527k
      enc_intra_state = *enc;
321
322
527k
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
527k
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
527k
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
527k
      save_bytes = nintra_bytes-nstart_bytes;
326
527k
      if (save_bytes == 0)
327
214k
         save_bytes = ALLOC_NONE;
328
527k
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
527k
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
527k
      *enc = enc_start_state;
333
334
527k
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
527k
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
527k
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
231k
      {
339
231k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
231k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
231k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
231k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
231k
         intra = 1;
345
231k
      }
346
527k
   } else {
347
136k
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
136k
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
136k
   }
350
351
664k
   if (intra)
352
367k
      *delayedIntra = new_distortion;
353
296k
   else
354
296k
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
664k
            new_distortion);
356
357
664k
   RESTORE_STACK;
358
664k
}
quant_coarse_energy
Line
Count
Source
264
332k
{
265
332k
   int intra;
266
332k
   celt_glog max_decay;
267
332k
   VARDECL(celt_glog, oldEBands_intra);
268
332k
   VARDECL(celt_glog, error_intra);
269
332k
   ec_enc enc_start_state;
270
332k
   opus_uint32 tell;
271
332k
   int badness1=0;
272
332k
   opus_int32 intra_bias;
273
332k
   opus_val32 new_distortion;
274
332k
   SAVE_STACK;
275
276
332k
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
332k
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
332k
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
332k
   tell = ec_tell(enc);
281
332k
   if (tell+3 > budget)
282
72.1k
      two_pass = intra = 0;
283
284
332k
   max_decay = GCONST(16.f);
285
332k
   if (end-start>10)
286
313k
   {
287
313k
#ifdef FIXED_POINT
288
313k
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
#endif
292
313k
   }
293
332k
   if (lfe)
294
1.42k
      max_decay = GCONST(3.f);
295
332k
   enc_start_state = *enc;
296
297
332k
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
332k
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
332k
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
332k
   if (two_pass || intra)
302
225k
   {
303
225k
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
225k
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
225k
   }
306
307
332k
   if (!intra)
308
263k
   {
309
263k
      unsigned char *intra_buf;
310
263k
      ec_enc enc_intra_state;
311
263k
      opus_int32 tell_intra;
312
263k
      opus_uint32 nstart_bytes;
313
263k
      opus_uint32 nintra_bytes;
314
263k
      opus_uint32 save_bytes;
315
263k
      int badness2;
316
263k
      VARDECL(unsigned char, intra_bits);
317
318
263k
      tell_intra = ec_tell_frac(enc);
319
320
263k
      enc_intra_state = *enc;
321
322
263k
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
263k
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
263k
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
263k
      save_bytes = nintra_bytes-nstart_bytes;
326
263k
      if (save_bytes == 0)
327
107k
         save_bytes = ALLOC_NONE;
328
263k
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
263k
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
263k
      *enc = enc_start_state;
333
334
263k
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
263k
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
263k
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
115k
      {
339
115k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
115k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
115k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
115k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
115k
         intra = 1;
345
115k
      }
346
263k
   } else {
347
68.1k
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
68.1k
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
68.1k
   }
350
351
332k
   if (intra)
352
183k
      *delayedIntra = new_distortion;
353
148k
   else
354
148k
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
332k
            new_distortion);
356
357
332k
   RESTORE_STACK;
358
332k
}
quant_coarse_energy
Line
Count
Source
264
332k
{
265
332k
   int intra;
266
332k
   celt_glog max_decay;
267
332k
   VARDECL(celt_glog, oldEBands_intra);
268
332k
   VARDECL(celt_glog, error_intra);
269
332k
   ec_enc enc_start_state;
270
332k
   opus_uint32 tell;
271
332k
   int badness1=0;
272
332k
   opus_int32 intra_bias;
273
332k
   opus_val32 new_distortion;
274
332k
   SAVE_STACK;
275
276
332k
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
332k
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
332k
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
332k
   tell = ec_tell(enc);
281
332k
   if (tell+3 > budget)
282
72.1k
      two_pass = intra = 0;
283
284
332k
   max_decay = GCONST(16.f);
285
332k
   if (end-start>10)
286
313k
   {
287
#ifdef FIXED_POINT
288
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
313k
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
313k
#endif
292
313k
   }
293
332k
   if (lfe)
294
1.42k
      max_decay = GCONST(3.f);
295
332k
   enc_start_state = *enc;
296
297
332k
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
332k
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
332k
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
332k
   if (two_pass || intra)
302
225k
   {
303
225k
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
225k
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
225k
   }
306
307
332k
   if (!intra)
308
263k
   {
309
263k
      unsigned char *intra_buf;
310
263k
      ec_enc enc_intra_state;
311
263k
      opus_int32 tell_intra;
312
263k
      opus_uint32 nstart_bytes;
313
263k
      opus_uint32 nintra_bytes;
314
263k
      opus_uint32 save_bytes;
315
263k
      int badness2;
316
263k
      VARDECL(unsigned char, intra_bits);
317
318
263k
      tell_intra = ec_tell_frac(enc);
319
320
263k
      enc_intra_state = *enc;
321
322
263k
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
263k
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
263k
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
263k
      save_bytes = nintra_bytes-nstart_bytes;
326
263k
      if (save_bytes == 0)
327
107k
         save_bytes = ALLOC_NONE;
328
263k
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
263k
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
263k
      *enc = enc_start_state;
333
334
263k
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
263k
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
263k
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
115k
      {
339
115k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
115k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
115k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
115k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
115k
         intra = 1;
345
115k
      }
346
263k
   } else {
347
68.1k
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
68.1k
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
68.1k
   }
350
351
332k
   if (intra)
352
183k
      *delayedIntra = new_distortion;
353
148k
   else
354
148k
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
332k
            new_distortion);
356
357
332k
   RESTORE_STACK;
358
332k
}
359
360
void quant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, celt_glog *error, int *prev_quant, int *extra_quant, ec_enc *enc, int C)
361
332k
{
362
332k
   int i, c;
363
   /* Encode finer resolution */
364
4.92M
   for (i=start;i<end;i++)
365
4.59M
   {
366
4.59M
      opus_int16 extra, prev;
367
4.59M
      extra = 1<<extra_quant[i];
368
4.59M
      if (extra_quant[i] <= 0)
369
1.88M
         continue;
370
2.71M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
2.71M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
2.71M
      c=0;
373
3.71M
      do {
374
3.71M
         int q2;
375
3.71M
         celt_glog offset;
376
#ifdef FIXED_POINT
377
         /* Has to be without rounding */
378
2.06M
         q2 = VSHR32(ADD32(error[i+c*m->nbEBands], SHR32(GCONST(.5f), prev)), DB_SHIFT-extra_quant[i]-prev);
379
#else
380
         q2 = (int)floor((error[i+c*m->nbEBands]*(1<<prev)+.5f)*extra);
381
#endif
382
3.71M
         if (q2 > extra-1)
383
4.97k
            q2 = extra-1;
384
3.71M
         if (q2<0)
385
183k
            q2 = 0;
386
3.71M
         ec_enc_bits(enc, q2, extra_quant[i]);
387
#ifdef FIXED_POINT
388
2.06M
         offset = SUB32(VSHR32(2*q2+1, extra_quant[i]-DB_SHIFT+1), GCONST(.5f));
389
2.06M
         offset = SHR32(offset, prev);
390
#else
391
         offset = (q2+.5f)*(1<<(14-extra_quant[i]))*(1.f/16384) - .5f;
392
         offset *= (1<<(14-prev))*(1.f/16384);
393
#endif
394
3.71M
         oldEBands[i+c*m->nbEBands] += offset;
395
3.71M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
3.71M
      } while (++c < C);
398
2.71M
   }
399
332k
}
quant_fine_energy
Line
Count
Source
361
173k
{
362
173k
   int i, c;
363
   /* Encode finer resolution */
364
2.55M
   for (i=start;i<end;i++)
365
2.38M
   {
366
2.38M
      opus_int16 extra, prev;
367
2.38M
      extra = 1<<extra_quant[i];
368
2.38M
      if (extra_quant[i] <= 0)
369
904k
         continue;
370
1.47M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
1.47M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
1.47M
      c=0;
373
2.06M
      do {
374
2.06M
         int q2;
375
2.06M
         celt_glog offset;
376
2.06M
#ifdef FIXED_POINT
377
         /* Has to be without rounding */
378
2.06M
         q2 = VSHR32(ADD32(error[i+c*m->nbEBands], SHR32(GCONST(.5f), prev)), DB_SHIFT-extra_quant[i]-prev);
379
#else
380
         q2 = (int)floor((error[i+c*m->nbEBands]*(1<<prev)+.5f)*extra);
381
#endif
382
2.06M
         if (q2 > extra-1)
383
2.47k
            q2 = extra-1;
384
2.06M
         if (q2<0)
385
109k
            q2 = 0;
386
2.06M
         ec_enc_bits(enc, q2, extra_quant[i]);
387
2.06M
#ifdef FIXED_POINT
388
2.06M
         offset = SUB32(VSHR32(2*q2+1, extra_quant[i]-DB_SHIFT+1), GCONST(.5f));
389
2.06M
         offset = SHR32(offset, prev);
390
#else
391
         offset = (q2+.5f)*(1<<(14-extra_quant[i]))*(1.f/16384) - .5f;
392
         offset *= (1<<(14-prev))*(1.f/16384);
393
#endif
394
2.06M
         oldEBands[i+c*m->nbEBands] += offset;
395
2.06M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
2.06M
      } while (++c < C);
398
1.47M
   }
399
173k
}
quant_fine_energy
Line
Count
Source
361
158k
{
362
158k
   int i, c;
363
   /* Encode finer resolution */
364
2.36M
   for (i=start;i<end;i++)
365
2.21M
   {
366
2.21M
      opus_int16 extra, prev;
367
2.21M
      extra = 1<<extra_quant[i];
368
2.21M
      if (extra_quant[i] <= 0)
369
976k
         continue;
370
1.23M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
1.23M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
1.23M
      c=0;
373
1.64M
      do {
374
1.64M
         int q2;
375
1.64M
         celt_glog offset;
376
#ifdef FIXED_POINT
377
         /* Has to be without rounding */
378
         q2 = VSHR32(ADD32(error[i+c*m->nbEBands], SHR32(GCONST(.5f), prev)), DB_SHIFT-extra_quant[i]-prev);
379
#else
380
1.64M
         q2 = (int)floor((error[i+c*m->nbEBands]*(1<<prev)+.5f)*extra);
381
1.64M
#endif
382
1.64M
         if (q2 > extra-1)
383
2.49k
            q2 = extra-1;
384
1.64M
         if (q2<0)
385
74.3k
            q2 = 0;
386
1.64M
         ec_enc_bits(enc, q2, extra_quant[i]);
387
#ifdef FIXED_POINT
388
         offset = SUB32(VSHR32(2*q2+1, extra_quant[i]-DB_SHIFT+1), GCONST(.5f));
389
         offset = SHR32(offset, prev);
390
#else
391
1.64M
         offset = (q2+.5f)*(1<<(14-extra_quant[i]))*(1.f/16384) - .5f;
392
1.64M
         offset *= (1<<(14-prev))*(1.f/16384);
393
1.64M
#endif
394
1.64M
         oldEBands[i+c*m->nbEBands] += offset;
395
1.64M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
1.64M
      } while (++c < C);
398
1.23M
   }
399
158k
}
400
401
void quant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *oldEBands, celt_glog *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C)
402
664k
{
403
664k
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
1.99M
   for (prio=0;prio<2;prio++)
407
1.32M
   {
408
3.27M
      for (i=start;i<end && bits_left>=C ;i++)
409
1.94M
      {
410
1.94M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
1.15M
            continue;
412
795k
         c=0;
413
1.09M
         do {
414
1.09M
            int q2;
415
1.09M
            celt_glog offset;
416
1.09M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
1.09M
            ec_enc_bits(enc, q2, 1);
418
#ifdef FIXED_POINT
419
545k
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
420
#else
421
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
422
#endif
423
1.09M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
1.09M
            error[i+c*m->nbEBands] -= offset;
425
1.09M
            bits_left--;
426
1.09M
         } while (++c < C);
427
795k
      }
428
1.32M
   }
429
664k
}
quant_energy_finalise
Line
Count
Source
402
332k
{
403
332k
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
996k
   for (prio=0;prio<2;prio++)
407
664k
   {
408
1.63M
      for (i=start;i<end && bits_left>=C ;i++)
409
974k
      {
410
974k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
576k
            continue;
412
397k
         c=0;
413
545k
         do {
414
545k
            int q2;
415
545k
            celt_glog offset;
416
545k
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
545k
            ec_enc_bits(enc, q2, 1);
418
545k
#ifdef FIXED_POINT
419
545k
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
420
#else
421
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
422
#endif
423
545k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
545k
            error[i+c*m->nbEBands] -= offset;
425
545k
            bits_left--;
426
545k
         } while (++c < C);
427
397k
      }
428
664k
   }
429
332k
}
quant_energy_finalise
Line
Count
Source
402
332k
{
403
332k
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
996k
   for (prio=0;prio<2;prio++)
407
664k
   {
408
1.63M
      for (i=start;i<end && bits_left>=C ;i++)
409
974k
      {
410
974k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
576k
            continue;
412
397k
         c=0;
413
545k
         do {
414
545k
            int q2;
415
545k
            celt_glog offset;
416
545k
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
545k
            ec_enc_bits(enc, q2, 1);
418
#ifdef FIXED_POINT
419
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
420
#else
421
545k
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
422
545k
#endif
423
545k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
545k
            error[i+c*m->nbEBands] -= offset;
425
545k
            bits_left--;
426
545k
         } while (++c < C);
427
397k
      }
428
664k
   }
429
332k
}
430
431
void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, int intra, ec_dec *dec, int C, int LM)
432
305k
{
433
305k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
305k
   int i, c;
435
305k
   opus_val64 prev[2] = {0, 0};
436
305k
   opus_val16 coef;
437
305k
   opus_val16 beta;
438
305k
   opus_int32 budget;
439
305k
   opus_int32 tell;
440
441
305k
   if (intra)
442
26.8k
   {
443
26.8k
      coef = 0;
444
26.8k
      beta = beta_intra;
445
278k
   } else {
446
278k
      beta = beta_coef[LM];
447
278k
      coef = pred_coef[LM];
448
278k
   }
449
450
305k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
5.17M
   for (i=start;i<end;i++)
454
4.87M
   {
455
4.87M
      c=0;
456
6.55M
      do {
457
6.55M
         int qi;
458
6.55M
         opus_val32 q;
459
6.55M
         opus_val32 tmp;
460
         /* It would be better to express this invariant as a
461
            test on C at function entry, but that isn't enough
462
            to make the static analyzer happy. */
463
6.55M
         celt_sig_assert(c<2);
464
6.55M
         tell = ec_tell(dec);
465
6.55M
         if(budget-tell>=15)
466
2.84M
         {
467
2.84M
            int pi;
468
2.84M
            pi = 2*IMIN(i,20);
469
2.84M
            qi = ec_laplace_decode(dec,
470
2.84M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
2.84M
         }
472
3.70M
         else if(budget-tell>=2)
473
1.22M
         {
474
1.22M
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
1.22M
            qi = (qi>>1)^-(qi&1);
476
1.22M
         }
477
2.48M
         else if(budget-tell>=1)
478
93.1k
         {
479
93.1k
            qi = -ec_dec_bit_logp(dec, 1);
480
93.1k
         }
481
2.38M
         else
482
2.38M
            qi = -1;
483
6.55M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
6.55M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
6.55M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
#ifdef FIXED_POINT
488
3.77M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
#endif
490
6.55M
         oldEBands[i+c*m->nbEBands] = tmp;
491
6.55M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
6.55M
      } while (++c < C);
493
4.87M
   }
494
305k
}
unquant_coarse_energy
Line
Count
Source
432
168k
{
433
168k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
168k
   int i, c;
435
168k
   opus_val64 prev[2] = {0, 0};
436
168k
   opus_val16 coef;
437
168k
   opus_val16 beta;
438
168k
   opus_int32 budget;
439
168k
   opus_int32 tell;
440
441
168k
   if (intra)
442
15.5k
   {
443
15.5k
      coef = 0;
444
15.5k
      beta = beta_intra;
445
153k
   } else {
446
153k
      beta = beta_coef[LM];
447
153k
      coef = pred_coef[LM];
448
153k
   }
449
450
168k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.92M
   for (i=start;i<end;i++)
454
2.75M
   {
455
2.75M
      c=0;
456
3.77M
      do {
457
3.77M
         int qi;
458
3.77M
         opus_val32 q;
459
3.77M
         opus_val32 tmp;
460
         /* It would be better to express this invariant as a
461
            test on C at function entry, but that isn't enough
462
            to make the static analyzer happy. */
463
3.77M
         celt_sig_assert(c<2);
464
3.77M
         tell = ec_tell(dec);
465
3.77M
         if(budget-tell>=15)
466
1.60M
         {
467
1.60M
            int pi;
468
1.60M
            pi = 2*IMIN(i,20);
469
1.60M
            qi = ec_laplace_decode(dec,
470
1.60M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.60M
         }
472
2.16M
         else if(budget-tell>=2)
473
693k
         {
474
693k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
693k
            qi = (qi>>1)^-(qi&1);
476
693k
         }
477
1.47M
         else if(budget-tell>=1)
478
55.5k
         {
479
55.5k
            qi = -ec_dec_bit_logp(dec, 1);
480
55.5k
         }
481
1.41M
         else
482
1.41M
            qi = -1;
483
3.77M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
3.77M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
3.77M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
3.77M
#ifdef FIXED_POINT
488
3.77M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
3.77M
#endif
490
3.77M
         oldEBands[i+c*m->nbEBands] = tmp;
491
3.77M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
3.77M
      } while (++c < C);
493
2.75M
   }
494
168k
}
unquant_coarse_energy
Line
Count
Source
432
136k
{
433
136k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
136k
   int i, c;
435
136k
   opus_val64 prev[2] = {0, 0};
436
136k
   opus_val16 coef;
437
136k
   opus_val16 beta;
438
136k
   opus_int32 budget;
439
136k
   opus_int32 tell;
440
441
136k
   if (intra)
442
11.3k
   {
443
11.3k
      coef = 0;
444
11.3k
      beta = beta_intra;
445
125k
   } else {
446
125k
      beta = beta_coef[LM];
447
125k
      coef = pred_coef[LM];
448
125k
   }
449
450
136k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.25M
   for (i=start;i<end;i++)
454
2.11M
   {
455
2.11M
      c=0;
456
2.78M
      do {
457
2.78M
         int qi;
458
2.78M
         opus_val32 q;
459
2.78M
         opus_val32 tmp;
460
         /* It would be better to express this invariant as a
461
            test on C at function entry, but that isn't enough
462
            to make the static analyzer happy. */
463
2.78M
         celt_sig_assert(c<2);
464
2.78M
         tell = ec_tell(dec);
465
2.78M
         if(budget-tell>=15)
466
1.24M
         {
467
1.24M
            int pi;
468
1.24M
            pi = 2*IMIN(i,20);
469
1.24M
            qi = ec_laplace_decode(dec,
470
1.24M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.24M
         }
472
1.54M
         else if(budget-tell>=2)
473
533k
         {
474
533k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
533k
            qi = (qi>>1)^-(qi&1);
476
533k
         }
477
1.00M
         else if(budget-tell>=1)
478
37.5k
         {
479
37.5k
            qi = -ec_dec_bit_logp(dec, 1);
480
37.5k
         }
481
971k
         else
482
971k
            qi = -1;
483
2.78M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
2.78M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
2.78M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
#ifdef FIXED_POINT
488
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
#endif
490
2.78M
         oldEBands[i+c*m->nbEBands] = tmp;
491
2.78M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
2.78M
      } while (++c < C);
493
2.11M
   }
494
136k
}
495
496
void unquant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, int *prev_quant, int *extra_quant, ec_dec *dec, int C)
497
315k
{
498
315k
   int i, c;
499
   /* Decode finer resolution */
500
5.37M
   for (i=start;i<end;i++)
501
5.05M
   {
502
5.05M
      opus_int16 extra, prev;
503
5.05M
      extra = extra_quant[i];
504
5.05M
      if (extra_quant[i] <= 0)
505
3.79M
         continue;
506
1.26M
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
1.24M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
1.24M
      c=0;
509
1.64M
      do {
510
1.64M
         int q2;
511
1.64M
         celt_glog offset;
512
1.64M
         q2 = ec_dec_bits(dec, extra);
513
#ifdef FIXED_POINT
514
911k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
911k
         offset = SHR32(offset, prev);
516
#else
517
         offset = (q2+.5f)*(1<<(14-extra))*(1.f/16384) - .5f;
518
         offset *= (1<<(14-prev))*(1.f/16384);
519
#endif
520
1.64M
         oldEBands[i+c*m->nbEBands] += offset;
521
1.64M
      } while (++c < C);
522
1.24M
   }
523
315k
}
unquant_fine_energy
Line
Count
Source
497
174k
{
498
174k
   int i, c;
499
   /* Decode finer resolution */
500
3.03M
   for (i=start;i<end;i++)
501
2.86M
   {
502
2.86M
      opus_int16 extra, prev;
503
2.86M
      extra = extra_quant[i];
504
2.86M
      if (extra_quant[i] <= 0)
505
2.16M
         continue;
506
695k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
682k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
682k
      c=0;
509
911k
      do {
510
911k
         int q2;
511
911k
         celt_glog offset;
512
911k
         q2 = ec_dec_bits(dec, extra);
513
911k
#ifdef FIXED_POINT
514
911k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
911k
         offset = SHR32(offset, prev);
516
#else
517
         offset = (q2+.5f)*(1<<(14-extra))*(1.f/16384) - .5f;
518
         offset *= (1<<(14-prev))*(1.f/16384);
519
#endif
520
911k
         oldEBands[i+c*m->nbEBands] += offset;
521
911k
      } while (++c < C);
522
682k
   }
523
174k
}
unquant_fine_energy
Line
Count
Source
497
141k
{
498
141k
   int i, c;
499
   /* Decode finer resolution */
500
2.33M
   for (i=start;i<end;i++)
501
2.19M
   {
502
2.19M
      opus_int16 extra, prev;
503
2.19M
      extra = extra_quant[i];
504
2.19M
      if (extra_quant[i] <= 0)
505
1.62M
         continue;
506
571k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
564k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
564k
      c=0;
509
730k
      do {
510
730k
         int q2;
511
730k
         celt_glog offset;
512
730k
         q2 = ec_dec_bits(dec, extra);
513
#ifdef FIXED_POINT
514
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
         offset = SHR32(offset, prev);
516
#else
517
730k
         offset = (q2+.5f)*(1<<(14-extra))*(1.f/16384) - .5f;
518
730k
         offset *= (1<<(14-prev))*(1.f/16384);
519
730k
#endif
520
730k
         oldEBands[i+c*m->nbEBands] += offset;
521
730k
      } while (++c < C);
522
564k
   }
523
141k
}
524
525
void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int C)
526
595k
{
527
595k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
1.78M
   for (prio=0;prio<2;prio++)
531
1.19M
   {
532
3.12M
      for (i=start;i<end && bits_left>=C ;i++)
533
1.93M
      {
534
1.93M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
1.53M
            continue;
536
397k
         c=0;
537
561k
         do {
538
561k
            int q2;
539
561k
            celt_glog offset;
540
561k
            q2 = ec_dec_bits(dec, 1);
541
#ifdef FIXED_POINT
542
280k
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
543
#else
544
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
545
#endif
546
561k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
561k
            bits_left--;
548
561k
         } while (++c < C);
549
397k
      }
550
1.19M
   }
551
595k
}
unquant_energy_finalise
Line
Count
Source
526
297k
{
527
297k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
893k
   for (prio=0;prio<2;prio++)
531
595k
   {
532
1.56M
      for (i=start;i<end && bits_left>=C ;i++)
533
966k
      {
534
966k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
767k
            continue;
536
198k
         c=0;
537
280k
         do {
538
280k
            int q2;
539
280k
            celt_glog offset;
540
280k
            q2 = ec_dec_bits(dec, 1);
541
280k
#ifdef FIXED_POINT
542
280k
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
543
#else
544
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
545
#endif
546
280k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
280k
            bits_left--;
548
280k
         } while (++c < C);
549
198k
      }
550
595k
   }
551
297k
}
unquant_energy_finalise
Line
Count
Source
526
297k
{
527
297k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
893k
   for (prio=0;prio<2;prio++)
531
595k
   {
532
1.56M
      for (i=start;i<end && bits_left>=C ;i++)
533
966k
      {
534
966k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
767k
            continue;
536
198k
         c=0;
537
280k
         do {
538
280k
            int q2;
539
280k
            celt_glog offset;
540
280k
            q2 = ec_dec_bits(dec, 1);
541
#ifdef FIXED_POINT
542
            offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1);
543
#else
544
280k
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
545
280k
#endif
546
280k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
280k
            bits_left--;
548
280k
         } while (++c < C);
549
198k
      }
550
595k
   }
551
297k
}
552
553
void amp2Log2(const CELTMode *m, int effEnd, int end,
554
      celt_ener *bandE, celt_glog *bandLogE, int C)
555
1.24M
{
556
1.24M
   int c, i;
557
1.24M
   c=0;
558
1.66M
   do {
559
26.6M
      for (i=0;i<effEnd;i++)
560
24.9M
      {
561
24.9M
         bandLogE[i+c*m->nbEBands] =
562
24.9M
               celt_log2_db(bandE[i+c*m->nbEBands])
563
24.9M
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
16.6M
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
#endif
568
24.9M
      }
569
1.66M
      for (i=effEnd;i<end;i++)
570
1.11M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
1.66M
   } while (++c < C);
572
1.24M
}
amp2Log2
Line
Count
Source
555
415k
{
556
415k
   int c, i;
557
415k
   c=0;
558
555k
   do {
559
8.87M
      for (i=0;i<effEnd;i++)
560
8.32M
      {
561
8.32M
         bandLogE[i+c*m->nbEBands] =
562
8.32M
               celt_log2_db(bandE[i+c*m->nbEBands])
563
8.32M
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
8.32M
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
8.32M
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
8.32M
#endif
568
8.32M
      }
569
555k
      for (i=effEnd;i<end;i++)
570
555k
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
555k
   } while (++c < C);
572
415k
}
amp2Log2
Line
Count
Source
555
415k
{
556
415k
   int c, i;
557
415k
   c=0;
558
555k
   do {
559
8.87M
      for (i=0;i<effEnd;i++)
560
8.32M
      {
561
8.32M
         bandLogE[i+c*m->nbEBands] =
562
8.32M
               celt_log2_db(bandE[i+c*m->nbEBands])
563
8.32M
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
8.32M
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
8.32M
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
8.32M
#endif
568
8.32M
      }
569
555k
      for (i=effEnd;i<end;i++)
570
555k
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
555k
   } while (++c < C);
572
415k
}
amp2Log2
Line
Count
Source
555
415k
{
556
415k
   int c, i;
557
415k
   c=0;
558
555k
   do {
559
8.87M
      for (i=0;i<effEnd;i++)
560
8.32M
      {
561
8.32M
         bandLogE[i+c*m->nbEBands] =
562
8.32M
               celt_log2_db(bandE[i+c*m->nbEBands])
563
8.32M
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
#endif
568
8.32M
      }
569
555k
      for (i=effEnd;i<end;i++)
570
0
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
555k
   } while (++c < C);
572
415k
}