Coverage Report

Created: 2025-07-11 07:51

/src/opus/celt/quant_bands.c
Line
Count
Source (jump to first uncovered line)
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
125M
{
144
125M
   int c, i;
145
125M
   opus_val32 dist = 0;
146
164M
   c=0; do {
147
2.67G
      for (i=start;i<end;i++)
148
2.50G
      {
149
2.50G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
2.50G
         dist = MAC16_16(dist, d,d);
151
2.50G
      }
152
164M
   } while (++c<C);
153
125M
   return MIN32(200,SHR32(dist,14));
154
125M
}
quant_bands.c:loss_distortion
Line
Count
Source
143
62.8M
{
144
62.8M
   int c, i;
145
62.8M
   opus_val32 dist = 0;
146
82.0M
   c=0; do {
147
1.33G
      for (i=start;i<end;i++)
148
1.25G
      {
149
1.25G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
1.25G
         dist = MAC16_16(dist, d,d);
151
1.25G
      }
152
82.0M
   } while (++c<C);
153
62.8M
   return MIN32(200,SHR32(dist,14));
154
62.8M
}
quant_bands.c:loss_distortion
Line
Count
Source
143
62.8M
{
144
62.8M
   int c, i;
145
62.8M
   opus_val32 dist = 0;
146
82.0M
   c=0; do {
147
1.33G
      for (i=start;i<end;i++)
148
1.25G
      {
149
1.25G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
1.25G
         dist = MAC16_16(dist, d,d);
151
1.25G
      }
152
82.0M
   } while (++c<C);
153
62.8M
   return MIN32(200,SHR32(dist,14));
154
62.8M
}
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
64.8M
{
162
64.8M
   int i, c;
163
64.8M
   int badness = 0;
164
64.8M
   opus_val32 prev[2] = {0,0};
165
64.8M
   opus_val16 coef;
166
64.8M
   opus_val16 beta;
167
168
64.8M
   if (tell+3 <= budget)
169
6.60M
      ec_enc_bit_logp(enc, intra, 3);
170
64.8M
   if (intra)
171
4.06M
   {
172
4.06M
      coef = 0;
173
4.06M
      beta = beta_intra;
174
60.7M
   } else {
175
60.7M
      beta = beta_coef[LM];
176
60.7M
      coef = pred_coef[LM];
177
60.7M
   }
178
179
   /* Encode at a fixed coarse resolution */
180
1.03G
   for (i=start;i<end;i++)
181
972M
   {
182
972M
      c=0;
183
1.27G
      do {
184
1.27G
         int bits_left;
185
1.27G
         int qi, qi0;
186
1.27G
         opus_val32 q;
187
1.27G
         celt_glog x;
188
1.27G
         opus_val32 f, tmp;
189
1.27G
         celt_glog oldE;
190
1.27G
         celt_glog decay_bound;
191
1.27G
         x = eBands[i+c*m->nbEBands];
192
1.27G
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
#ifdef FIXED_POINT
194
4.92M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
4.92M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
4.92M
         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
1.27G
         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
1.27G
         if (qi < 0 && x < decay_bound)
207
1.20G
         {
208
1.20G
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
1.20G
            if (qi > 0)
210
725k
               qi = 0;
211
1.20G
         }
212
1.27G
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
1.27G
         tell = ec_tell(enc);
216
1.27G
         bits_left = budget-tell-3*C*(end-i);
217
1.27G
         if (i!=start && bits_left < 30)
218
1.12G
         {
219
1.12G
            if (bits_left < 24)
220
1.12G
               qi = IMIN(1, qi);
221
1.12G
            if (bits_left < 16)
222
1.12G
               qi = IMAX(-1, qi);
223
1.12G
         }
224
1.27G
         if (lfe && i>=2)
225
28.8k
            qi = IMIN(qi, 0);
226
1.27G
         if (budget-tell >= 15)
227
71.2M
         {
228
71.2M
            int pi;
229
71.2M
            pi = 2*IMIN(i,20);
230
71.2M
            ec_laplace_encode(enc, &qi,
231
71.2M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
71.2M
         }
233
1.20G
         else if(budget-tell >= 2)
234
60.1k
         {
235
60.1k
            qi = IMAX(-1, IMIN(qi, 1));
236
60.1k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
60.1k
         }
238
1.20G
         else if(budget-tell >= 1)
239
2.64k
         {
240
2.64k
            qi = IMIN(0, qi);
241
2.64k
            ec_enc_bit_logp(enc, -qi, 1);
242
2.64k
         }
243
1.20G
         else
244
1.20G
            qi = -1;
245
1.27G
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
1.27G
         badness += abs(qi0-qi);
247
1.27G
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
1.27G
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
4.92M
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
1.27G
         oldEBands[i+c*m->nbEBands] = tmp;
254
1.27G
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
1.27G
      } while (++c < C);
256
972M
   }
257
64.8M
   return lfe ? 0 : badness;
258
64.8M
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
263k
{
162
263k
   int i, c;
163
263k
   int badness = 0;
164
263k
   opus_val32 prev[2] = {0,0};
165
263k
   opus_val16 coef;
166
263k
   opus_val16 beta;
167
168
263k
   if (tell+3 <= budget)
169
232k
      ec_enc_bit_logp(enc, intra, 3);
170
263k
   if (intra)
171
125k
   {
172
125k
      coef = 0;
173
125k
      beta = beta_intra;
174
137k
   } else {
175
137k
      beta = beta_coef[LM];
176
137k
      coef = pred_coef[LM];
177
137k
   }
178
179
   /* Encode at a fixed coarse resolution */
180
3.89M
   for (i=start;i<end;i++)
181
3.63M
   {
182
3.63M
      c=0;
183
4.92M
      do {
184
4.92M
         int bits_left;
185
4.92M
         int qi, qi0;
186
4.92M
         opus_val32 q;
187
4.92M
         celt_glog x;
188
4.92M
         opus_val32 f, tmp;
189
4.92M
         celt_glog oldE;
190
4.92M
         celt_glog decay_bound;
191
4.92M
         x = eBands[i+c*m->nbEBands];
192
4.92M
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
4.92M
#ifdef FIXED_POINT
194
4.92M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
4.92M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
4.92M
         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.92M
         if (qi < 0 && x < decay_bound)
207
721k
         {
208
721k
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
721k
            if (qi > 0)
210
182k
               qi = 0;
211
721k
         }
212
4.92M
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
4.92M
         tell = ec_tell(enc);
216
4.92M
         bits_left = budget-tell-3*C*(end-i);
217
4.92M
         if (i!=start && bits_left < 30)
218
639k
         {
219
639k
            if (bits_left < 24)
220
613k
               qi = IMIN(1, qi);
221
639k
            if (bits_left < 16)
222
582k
               qi = IMAX(-1, qi);
223
639k
         }
224
4.92M
         if (lfe && i>=2)
225
16.1k
            qi = IMIN(qi, 0);
226
4.92M
         if (budget-tell >= 15)
227
4.36M
         {
228
4.36M
            int pi;
229
4.36M
            pi = 2*IMIN(i,20);
230
4.36M
            ec_laplace_encode(enc, &qi,
231
4.36M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
4.36M
         }
233
555k
         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
533k
         else if(budget-tell >= 1)
239
1.13k
         {
240
1.13k
            qi = IMIN(0, qi);
241
1.13k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.13k
         }
243
531k
         else
244
531k
            qi = -1;
245
4.92M
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
4.92M
         badness += abs(qi0-qi);
247
4.92M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
4.92M
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
4.92M
#ifdef FIXED_POINT
251
4.92M
         tmp = MAX32(-GCONST(28.f), tmp);
252
4.92M
#endif
253
4.92M
         oldEBands[i+c*m->nbEBands] = tmp;
254
4.92M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
4.92M
      } while (++c < C);
256
3.63M
   }
257
263k
   return lfe ? 0 : badness;
258
263k
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
64.5M
{
162
64.5M
   int i, c;
163
64.5M
   int badness = 0;
164
64.5M
   opus_val32 prev[2] = {0,0};
165
64.5M
   opus_val16 coef;
166
64.5M
   opus_val16 beta;
167
168
64.5M
   if (tell+3 <= budget)
169
6.36M
      ec_enc_bit_logp(enc, intra, 3);
170
64.5M
   if (intra)
171
3.93M
   {
172
3.93M
      coef = 0;
173
3.93M
      beta = beta_intra;
174
60.6M
   } else {
175
60.6M
      beta = beta_coef[LM];
176
60.6M
      coef = pred_coef[LM];
177
60.6M
   }
178
179
   /* Encode at a fixed coarse resolution */
180
1.03G
   for (i=start;i<end;i++)
181
968M
   {
182
968M
      c=0;
183
1.27G
      do {
184
1.27G
         int bits_left;
185
1.27G
         int qi, qi0;
186
1.27G
         opus_val32 q;
187
1.27G
         celt_glog x;
188
1.27G
         opus_val32 f, tmp;
189
1.27G
         celt_glog oldE;
190
1.27G
         celt_glog decay_bound;
191
1.27G
         x = eBands[i+c*m->nbEBands];
192
1.27G
         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
1.27G
         f = x-coef*oldE-prev[c];
200
         /* Rounding to nearest integer here is really important! */
201
1.27G
         qi = (int)floor(.5f+f);
202
1.27G
         decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay;
203
1.27G
#endif
204
         /* Prevent the energy from going down too quickly (e.g. for bands
205
            that have just one bin) */
206
1.27G
         if (qi < 0 && x < decay_bound)
207
1.20G
         {
208
1.20G
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
1.20G
            if (qi > 0)
210
542k
               qi = 0;
211
1.20G
         }
212
1.27G
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
1.27G
         tell = ec_tell(enc);
216
1.27G
         bits_left = budget-tell-3*C*(end-i);
217
1.27G
         if (i!=start && bits_left < 30)
218
1.12G
         {
219
1.12G
            if (bits_left < 24)
220
1.12G
               qi = IMIN(1, qi);
221
1.12G
            if (bits_left < 16)
222
1.12G
               qi = IMAX(-1, qi);
223
1.12G
         }
224
1.27G
         if (lfe && i>=2)
225
12.6k
            qi = IMIN(qi, 0);
226
1.27G
         if (budget-tell >= 15)
227
66.8M
         {
228
66.8M
            int pi;
229
66.8M
            pi = 2*IMIN(i,20);
230
66.8M
            ec_laplace_encode(enc, &qi,
231
66.8M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
66.8M
         }
233
1.20G
         else if(budget-tell >= 2)
234
38.0k
         {
235
38.0k
            qi = IMAX(-1, IMIN(qi, 1));
236
38.0k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
38.0k
         }
238
1.20G
         else if(budget-tell >= 1)
239
1.50k
         {
240
1.50k
            qi = IMIN(0, qi);
241
1.50k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.50k
         }
243
1.20G
         else
244
1.20G
            qi = -1;
245
1.27G
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
1.27G
         badness += abs(qi0-qi);
247
1.27G
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
1.27G
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
1.27G
         oldEBands[i+c*m->nbEBands] = tmp;
254
1.27G
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
1.27G
      } while (++c < C);
256
968M
   }
257
64.5M
   return lfe ? 0 : badness;
258
64.5M
}
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
125M
{
265
125M
   int intra;
266
125M
   celt_glog max_decay;
267
125M
   VARDECL(celt_glog, oldEBands_intra);
268
125M
   VARDECL(celt_glog, error_intra);
269
125M
   ec_enc enc_start_state;
270
125M
   opus_uint32 tell;
271
125M
   int badness1=0;
272
125M
   opus_int32 intra_bias;
273
125M
   opus_val32 new_distortion;
274
125M
   SAVE_STACK;
275
276
125M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
125M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
125M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
125M
   tell = ec_tell(enc);
281
125M
   if (tell+3 > budget)
282
116M
      two_pass = intra = 0;
283
284
125M
   max_decay = GCONST(16.f);
285
125M
   if (end-start>10)
286
121M
   {
287
#ifdef FIXED_POINT
288
60.6M
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
60.6M
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
#endif
292
121M
   }
293
125M
   if (lfe)
294
3.59k
      max_decay = GCONST(3.f);
295
125M
   enc_start_state = *enc;
296
297
125M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
125M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
125M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
125M
   if (two_pass || intra)
302
8.12M
   {
303
8.12M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
8.12M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
8.12M
   }
306
307
125M
   if (!intra)
308
121M
   {
309
121M
      unsigned char *intra_buf;
310
121M
      ec_enc enc_intra_state;
311
121M
      opus_int32 tell_intra;
312
121M
      opus_uint32 nstart_bytes;
313
121M
      opus_uint32 nintra_bytes;
314
121M
      opus_uint32 save_bytes;
315
121M
      int badness2;
316
121M
      VARDECL(unsigned char, intra_bits);
317
318
121M
      tell_intra = ec_tell_frac(enc);
319
320
121M
      enc_intra_state = *enc;
321
322
121M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
121M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
121M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
121M
      save_bytes = nintra_bytes-nstart_bytes;
326
121M
      if (save_bytes == 0)
327
117M
         save_bytes = ALLOC_NONE;
328
121M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
121M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
121M
      *enc = enc_start_state;
333
334
121M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
121M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
121M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
1.25M
      {
339
1.25M
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
1.25M
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
1.25M
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
1.25M
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
1.25M
         intra = 1;
345
1.25M
      }
346
121M
   } else {
347
4.24M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
4.24M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
4.24M
   }
350
351
125M
   if (intra)
352
5.49M
      *delayedIntra = new_distortion;
353
120M
   else
354
120M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
125M
            new_distortion);
356
357
125M
   RESTORE_STACK;
358
125M
}
quant_coarse_energy
Line
Count
Source
264
62.8M
{
265
62.8M
   int intra;
266
62.8M
   celt_glog max_decay;
267
62.8M
   VARDECL(celt_glog, oldEBands_intra);
268
62.8M
   VARDECL(celt_glog, error_intra);
269
62.8M
   ec_enc enc_start_state;
270
62.8M
   opus_uint32 tell;
271
62.8M
   int badness1=0;
272
62.8M
   opus_int32 intra_bias;
273
62.8M
   opus_val32 new_distortion;
274
62.8M
   SAVE_STACK;
275
276
62.8M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
62.8M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
62.8M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
62.8M
   tell = ec_tell(enc);
281
62.8M
   if (tell+3 > budget)
282
58.2M
      two_pass = intra = 0;
283
284
62.8M
   max_decay = GCONST(16.f);
285
62.8M
   if (end-start>10)
286
60.6M
   {
287
60.6M
#ifdef FIXED_POINT
288
60.6M
      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
60.6M
   }
293
62.8M
   if (lfe)
294
1.79k
      max_decay = GCONST(3.f);
295
62.8M
   enc_start_state = *enc;
296
297
62.8M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
62.8M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
62.8M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
62.8M
   if (two_pass || intra)
302
4.06M
   {
303
4.06M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
4.06M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
4.06M
   }
306
307
62.8M
   if (!intra)
308
60.7M
   {
309
60.7M
      unsigned char *intra_buf;
310
60.7M
      ec_enc enc_intra_state;
311
60.7M
      opus_int32 tell_intra;
312
60.7M
      opus_uint32 nstart_bytes;
313
60.7M
      opus_uint32 nintra_bytes;
314
60.7M
      opus_uint32 save_bytes;
315
60.7M
      int badness2;
316
60.7M
      VARDECL(unsigned char, intra_bits);
317
318
60.7M
      tell_intra = ec_tell_frac(enc);
319
320
60.7M
      enc_intra_state = *enc;
321
322
60.7M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
60.7M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
60.7M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
60.7M
      save_bytes = nintra_bytes-nstart_bytes;
326
60.7M
      if (save_bytes == 0)
327
58.8M
         save_bytes = ALLOC_NONE;
328
60.7M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
60.7M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
60.7M
      *enc = enc_start_state;
333
334
60.7M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
60.7M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
60.7M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
626k
      {
339
626k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
626k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
626k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
626k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
626k
         intra = 1;
345
626k
      }
346
60.7M
   } else {
347
2.12M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
2.12M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
2.12M
   }
350
351
62.8M
   if (intra)
352
2.74M
      *delayedIntra = new_distortion;
353
60.1M
   else
354
60.1M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
62.8M
            new_distortion);
356
357
62.8M
   RESTORE_STACK;
358
62.8M
}
quant_coarse_energy
Line
Count
Source
264
62.8M
{
265
62.8M
   int intra;
266
62.8M
   celt_glog max_decay;
267
62.8M
   VARDECL(celt_glog, oldEBands_intra);
268
62.8M
   VARDECL(celt_glog, error_intra);
269
62.8M
   ec_enc enc_start_state;
270
62.8M
   opus_uint32 tell;
271
62.8M
   int badness1=0;
272
62.8M
   opus_int32 intra_bias;
273
62.8M
   opus_val32 new_distortion;
274
62.8M
   SAVE_STACK;
275
276
62.8M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
62.8M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
62.8M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
62.8M
   tell = ec_tell(enc);
281
62.8M
   if (tell+3 > budget)
282
58.2M
      two_pass = intra = 0;
283
284
62.8M
   max_decay = GCONST(16.f);
285
62.8M
   if (end-start>10)
286
60.6M
   {
287
#ifdef FIXED_POINT
288
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
60.6M
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
60.6M
#endif
292
60.6M
   }
293
62.8M
   if (lfe)
294
1.79k
      max_decay = GCONST(3.f);
295
62.8M
   enc_start_state = *enc;
296
297
62.8M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
62.8M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
62.8M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
62.8M
   if (two_pass || intra)
302
4.06M
   {
303
4.06M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
4.06M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
4.06M
   }
306
307
62.8M
   if (!intra)
308
60.7M
   {
309
60.7M
      unsigned char *intra_buf;
310
60.7M
      ec_enc enc_intra_state;
311
60.7M
      opus_int32 tell_intra;
312
60.7M
      opus_uint32 nstart_bytes;
313
60.7M
      opus_uint32 nintra_bytes;
314
60.7M
      opus_uint32 save_bytes;
315
60.7M
      int badness2;
316
60.7M
      VARDECL(unsigned char, intra_bits);
317
318
60.7M
      tell_intra = ec_tell_frac(enc);
319
320
60.7M
      enc_intra_state = *enc;
321
322
60.7M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
60.7M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
60.7M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
60.7M
      save_bytes = nintra_bytes-nstart_bytes;
326
60.7M
      if (save_bytes == 0)
327
58.8M
         save_bytes = ALLOC_NONE;
328
60.7M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
60.7M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
60.7M
      *enc = enc_start_state;
333
334
60.7M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
60.7M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
60.7M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
626k
      {
339
626k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
626k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
626k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
626k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
626k
         intra = 1;
345
626k
      }
346
60.7M
   } else {
347
2.12M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
2.12M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
2.12M
   }
350
351
62.8M
   if (intra)
352
2.74M
      *delayedIntra = new_distortion;
353
60.1M
   else
354
60.1M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
62.8M
            new_distortion);
356
357
62.8M
   RESTORE_STACK;
358
62.8M
}
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
62.8M
{
362
62.8M
   int i, c;
363
   /* Encode finer resolution */
364
1.01G
   for (i=start;i<end;i++)
365
954M
   {
366
954M
      opus_int16 extra, prev;
367
954M
      extra = 1<<extra_quant[i];
368
954M
      if (extra_quant[i] <= 0)
369
924M
         continue;
370
29.9M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
29.9M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
29.9M
      c=0;
373
34.4M
      do {
374
34.4M
         int q2;
375
34.4M
         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
34.4M
         if (q2 > extra-1)
383
35.7k
            q2 = extra-1;
384
34.4M
         if (q2<0)
385
6.81M
            q2 = 0;
386
34.4M
         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
34.4M
         oldEBands[i+c*m->nbEBands] += offset;
395
34.4M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
34.4M
      } while (++c < C);
398
29.9M
   }
399
62.8M
}
quant_fine_energy
Line
Count
Source
361
173k
{
362
173k
   int i, c;
363
   /* Encode finer resolution */
364
2.56M
   for (i=start;i<end;i++)
365
2.39M
   {
366
2.39M
      opus_int16 extra, prev;
367
2.39M
      extra = 1<<extra_quant[i];
368
2.39M
      if (extra_quant[i] <= 0)
369
900k
         continue;
370
1.48M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
1.48M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
1.48M
      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.96k
            q2 = extra-1;
384
2.06M
         if (q2<0)
385
103k
            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.48M
   }
399
173k
}
quant_fine_energy
Line
Count
Source
361
62.7M
{
362
62.7M
   int i, c;
363
   /* Encode finer resolution */
364
1.01G
   for (i=start;i<end;i++)
365
952M
   {
366
952M
      opus_int16 extra, prev;
367
952M
      extra = 1<<extra_quant[i];
368
952M
      if (extra_quant[i] <= 0)
369
923M
         continue;
370
28.4M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
28.4M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
28.4M
      c=0;
373
32.4M
      do {
374
32.4M
         int q2;
375
32.4M
         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
32.4M
         q2 = (int)floor((error[i+c*m->nbEBands]*(1<<prev)+.5f)*extra);
381
32.4M
#endif
382
32.4M
         if (q2 > extra-1)
383
32.7k
            q2 = extra-1;
384
32.4M
         if (q2<0)
385
6.70M
            q2 = 0;
386
32.4M
         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
32.4M
         offset = (q2+.5f)*(1<<(14-extra_quant[i]))*(1.f/16384) - .5f;
392
32.4M
         offset *= (1<<(14-prev))*(1.f/16384);
393
32.4M
#endif
394
32.4M
         oldEBands[i+c*m->nbEBands] += offset;
395
32.4M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
32.4M
      } while (++c < C);
398
28.4M
   }
399
62.7M
}
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
125M
{
403
125M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
377M
   for (prio=0;prio<2;prio++)
407
251M
   {
408
282M
      for (i=start;i<end && bits_left>=C ;i++)
409
30.9M
      {
410
30.9M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
16.2M
            continue;
412
14.7M
         c=0;
413
16.7M
         do {
414
16.7M
            int q2;
415
16.7M
            celt_glog offset;
416
16.7M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
16.7M
            ec_enc_bits(enc, q2, 1);
418
#ifdef FIXED_POINT
419
8.37M
            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
16.7M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
16.7M
            error[i+c*m->nbEBands] -= offset;
425
16.7M
            bits_left--;
426
16.7M
         } while (++c < C);
427
14.7M
      }
428
251M
   }
429
125M
}
quant_energy_finalise
Line
Count
Source
402
62.8M
{
403
62.8M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
188M
   for (prio=0;prio<2;prio++)
407
125M
   {
408
141M
      for (i=start;i<end && bits_left>=C ;i++)
409
15.4M
      {
410
15.4M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
8.10M
            continue;
412
7.37M
         c=0;
413
8.37M
         do {
414
8.37M
            int q2;
415
8.37M
            celt_glog offset;
416
8.37M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
8.37M
            ec_enc_bits(enc, q2, 1);
418
8.37M
#ifdef FIXED_POINT
419
8.37M
            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
8.37M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
8.37M
            error[i+c*m->nbEBands] -= offset;
425
8.37M
            bits_left--;
426
8.37M
         } while (++c < C);
427
7.37M
      }
428
125M
   }
429
62.8M
}
quant_energy_finalise
Line
Count
Source
402
62.8M
{
403
62.8M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
188M
   for (prio=0;prio<2;prio++)
407
125M
   {
408
141M
      for (i=start;i<end && bits_left>=C ;i++)
409
15.4M
      {
410
15.4M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
8.10M
            continue;
412
7.37M
         c=0;
413
8.37M
         do {
414
8.37M
            int q2;
415
8.37M
            celt_glog offset;
416
8.37M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
8.37M
            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
8.37M
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
422
8.37M
#endif
423
8.37M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
8.37M
            error[i+c*m->nbEBands] -= offset;
425
8.37M
            bits_left--;
426
8.37M
         } while (++c < C);
427
7.37M
      }
428
125M
   }
429
62.8M
}
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
287k
{
433
287k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
287k
   int i, c;
435
287k
   opus_val64 prev[2] = {0, 0};
436
287k
   opus_val16 coef;
437
287k
   opus_val16 beta;
438
287k
   opus_int32 budget;
439
287k
   opus_int32 tell;
440
441
287k
   if (intra)
442
24.7k
   {
443
24.7k
      coef = 0;
444
24.7k
      beta = beta_intra;
445
262k
   } else {
446
262k
      beta = beta_coef[LM];
447
262k
      coef = pred_coef[LM];
448
262k
   }
449
450
287k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
4.78M
   for (i=start;i<end;i++)
454
4.50M
   {
455
4.50M
      c=0;
456
6.18M
      do {
457
6.18M
         int qi;
458
6.18M
         opus_val32 q;
459
6.18M
         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.18M
         celt_sig_assert(c<2);
464
6.18M
         tell = ec_tell(dec);
465
6.18M
         if(budget-tell>=15)
466
2.76M
         {
467
2.76M
            int pi;
468
2.76M
            pi = 2*IMIN(i,20);
469
2.76M
            qi = ec_laplace_decode(dec,
470
2.76M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
2.76M
         }
472
3.42M
         else if(budget-tell>=2)
473
1.10M
         {
474
1.10M
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
1.10M
            qi = (qi>>1)^-(qi&1);
476
1.10M
         }
477
2.31M
         else if(budget-tell>=1)
478
83.5k
         {
479
83.5k
            qi = -ec_dec_bit_logp(dec, 1);
480
83.5k
         }
481
2.23M
         else
482
2.23M
            qi = -1;
483
6.18M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
6.18M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
6.18M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
#ifdef FIXED_POINT
488
3.52M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
#endif
490
6.18M
         oldEBands[i+c*m->nbEBands] = tmp;
491
6.18M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
6.18M
      } while (++c < C);
493
4.50M
   }
494
287k
}
unquant_coarse_energy
Line
Count
Source
432
157k
{
433
157k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
157k
   int i, c;
435
157k
   opus_val64 prev[2] = {0, 0};
436
157k
   opus_val16 coef;
437
157k
   opus_val16 beta;
438
157k
   opus_int32 budget;
439
157k
   opus_int32 tell;
440
441
157k
   if (intra)
442
14.7k
   {
443
14.7k
      coef = 0;
444
14.7k
      beta = beta_intra;
445
143k
   } else {
446
143k
      beta = beta_coef[LM];
447
143k
      coef = pred_coef[LM];
448
143k
   }
449
450
157k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.67M
   for (i=start;i<end;i++)
454
2.51M
   {
455
2.51M
      c=0;
456
3.52M
      do {
457
3.52M
         int qi;
458
3.52M
         opus_val32 q;
459
3.52M
         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.52M
         celt_sig_assert(c<2);
464
3.52M
         tell = ec_tell(dec);
465
3.52M
         if(budget-tell>=15)
466
1.50M
         {
467
1.50M
            int pi;
468
1.50M
            pi = 2*IMIN(i,20);
469
1.50M
            qi = ec_laplace_decode(dec,
470
1.50M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.50M
         }
472
2.01M
         else if(budget-tell>=2)
473
652k
         {
474
652k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
652k
            qi = (qi>>1)^-(qi&1);
476
652k
         }
477
1.36M
         else if(budget-tell>=1)
478
51.2k
         {
479
51.2k
            qi = -ec_dec_bit_logp(dec, 1);
480
51.2k
         }
481
1.31M
         else
482
1.31M
            qi = -1;
483
3.52M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
3.52M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
3.52M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
3.52M
#ifdef FIXED_POINT
488
3.52M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
3.52M
#endif
490
3.52M
         oldEBands[i+c*m->nbEBands] = tmp;
491
3.52M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
3.52M
      } while (++c < C);
493
2.51M
   }
494
157k
}
unquant_coarse_energy
Line
Count
Source
432
129k
{
433
129k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
129k
   int i, c;
435
129k
   opus_val64 prev[2] = {0, 0};
436
129k
   opus_val16 coef;
437
129k
   opus_val16 beta;
438
129k
   opus_int32 budget;
439
129k
   opus_int32 tell;
440
441
129k
   if (intra)
442
9.98k
   {
443
9.98k
      coef = 0;
444
9.98k
      beta = beta_intra;
445
119k
   } else {
446
119k
      beta = beta_coef[LM];
447
119k
      coef = pred_coef[LM];
448
119k
   }
449
450
129k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.11M
   for (i=start;i<end;i++)
454
1.98M
   {
455
1.98M
      c=0;
456
2.66M
      do {
457
2.66M
         int qi;
458
2.66M
         opus_val32 q;
459
2.66M
         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.66M
         celt_sig_assert(c<2);
464
2.66M
         tell = ec_tell(dec);
465
2.66M
         if(budget-tell>=15)
466
1.26M
         {
467
1.26M
            int pi;
468
1.26M
            pi = 2*IMIN(i,20);
469
1.26M
            qi = ec_laplace_decode(dec,
470
1.26M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.26M
         }
472
1.40M
         else if(budget-tell>=2)
473
452k
         {
474
452k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
452k
            qi = (qi>>1)^-(qi&1);
476
452k
         }
477
952k
         else if(budget-tell>=1)
478
32.2k
         {
479
32.2k
            qi = -ec_dec_bit_logp(dec, 1);
480
32.2k
         }
481
920k
         else
482
920k
            qi = -1;
483
2.66M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
2.66M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
2.66M
         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.66M
         oldEBands[i+c*m->nbEBands] = tmp;
491
2.66M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
2.66M
      } while (++c < C);
493
1.98M
   }
494
129k
}
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
297k
{
498
297k
   int i, c;
499
   /* Decode finer resolution */
500
4.99M
   for (i=start;i<end;i++)
501
4.70M
   {
502
4.70M
      opus_int16 extra, prev;
503
4.70M
      extra = extra_quant[i];
504
4.70M
      if (extra_quant[i] <= 0)
505
3.47M
         continue;
506
1.22M
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
1.19M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
1.19M
      c=0;
509
1.60M
      do {
510
1.60M
         int q2;
511
1.60M
         celt_glog offset;
512
1.60M
         q2 = ec_dec_bits(dec, extra);
513
#ifdef FIXED_POINT
514
849k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
849k
         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.60M
         oldEBands[i+c*m->nbEBands] += offset;
521
1.60M
      } while (++c < C);
522
1.19M
   }
523
297k
}
unquant_fine_energy
Line
Count
Source
497
164k
{
498
164k
   int i, c;
499
   /* Decode finer resolution */
500
2.80M
   for (i=start;i<end;i++)
501
2.64M
   {
502
2.64M
      opus_int16 extra, prev;
503
2.64M
      extra = extra_quant[i];
504
2.64M
      if (extra_quant[i] <= 0)
505
2.00M
         continue;
506
634k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
618k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
618k
      c=0;
509
849k
      do {
510
849k
         int q2;
511
849k
         celt_glog offset;
512
849k
         q2 = ec_dec_bits(dec, extra);
513
849k
#ifdef FIXED_POINT
514
849k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
849k
         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
849k
         oldEBands[i+c*m->nbEBands] += offset;
521
849k
      } while (++c < C);
522
618k
   }
523
164k
}
unquant_fine_energy
Line
Count
Source
497
133k
{
498
133k
   int i, c;
499
   /* Decode finer resolution */
500
2.19M
   for (i=start;i<end;i++)
501
2.06M
   {
502
2.06M
      opus_int16 extra, prev;
503
2.06M
      extra = extra_quant[i];
504
2.06M
      if (extra_quant[i] <= 0)
505
1.47M
         continue;
506
588k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
581k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
581k
      c=0;
509
755k
      do {
510
755k
         int q2;
511
755k
         celt_glog offset;
512
755k
         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
755k
         offset = (q2+.5f)*(1<<(14-extra))*(1.f/16384) - .5f;
518
755k
         offset *= (1<<(14-prev))*(1.f/16384);
519
755k
#endif
520
755k
         oldEBands[i+c*m->nbEBands] += offset;
521
755k
      } while (++c < C);
522
581k
   }
523
133k
}
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
557k
{
527
557k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
1.67M
   for (prio=0;prio<2;prio++)
531
1.11M
   {
532
2.91M
      for (i=start;i<end && bits_left>=C ;i++)
533
1.79M
      {
534
1.79M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
1.41M
            continue;
536
385k
         c=0;
537
552k
         do {
538
552k
            int q2;
539
552k
            celt_glog offset;
540
552k
            q2 = ec_dec_bits(dec, 1);
541
#ifdef FIXED_POINT
542
276k
            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
552k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
552k
            bits_left--;
548
552k
         } while (++c < C);
549
385k
      }
550
1.11M
   }
551
557k
}
unquant_energy_finalise
Line
Count
Source
526
278k
{
527
278k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
836k
   for (prio=0;prio<2;prio++)
531
557k
   {
532
1.45M
      for (i=start;i<end && bits_left>=C ;i++)
533
899k
      {
534
899k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
706k
            continue;
536
192k
         c=0;
537
276k
         do {
538
276k
            int q2;
539
276k
            celt_glog offset;
540
276k
            q2 = ec_dec_bits(dec, 1);
541
276k
#ifdef FIXED_POINT
542
276k
            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
276k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
276k
            bits_left--;
548
276k
         } while (++c < C);
549
192k
      }
550
557k
   }
551
278k
}
unquant_energy_finalise
Line
Count
Source
526
278k
{
527
278k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
836k
   for (prio=0;prio<2;prio++)
531
557k
   {
532
1.45M
      for (i=start;i<end && bits_left>=C ;i++)
533
899k
      {
534
899k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
706k
            continue;
536
192k
         c=0;
537
276k
         do {
538
276k
            int q2;
539
276k
            celt_glog offset;
540
276k
            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
276k
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
545
276k
#endif
546
276k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
276k
            bits_left--;
548
276k
         } while (++c < C);
549
192k
      }
550
557k
   }
551
278k
}
552
553
void amp2Log2(const CELTMode *m, int effEnd, int end,
554
      celt_ener *bandE, celt_glog *bandLogE, int C)
555
189M
{
556
189M
   int c, i;
557
189M
   c=0;
558
247M
   do {
559
4.15G
      for (i=0;i<effEnd;i++)
560
3.90G
      {
561
3.90G
         bandLogE[i+c*m->nbEBands] =
562
3.90G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
3.90G
               - 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
2.60G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
#endif
568
3.90G
      }
569
247M
      for (i=effEnd;i<end;i++)
570
164M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
247M
   } while (++c < C);
572
189M
}
amp2Log2
Line
Count
Source
555
63.1M
{
556
63.1M
   int c, i;
557
63.1M
   c=0;
558
82.4M
   do {
559
1.38G
      for (i=0;i<effEnd;i++)
560
1.30G
      {
561
1.30G
         bandLogE[i+c*m->nbEBands] =
562
1.30G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.30G
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
1.30G
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
1.30G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
1.30G
#endif
568
1.30G
      }
569
82.4M
      for (i=effEnd;i<end;i++)
570
82.4M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
82.4M
   } while (++c < C);
572
63.1M
}
amp2Log2
Line
Count
Source
555
63.1M
{
556
63.1M
   int c, i;
557
63.1M
   c=0;
558
82.4M
   do {
559
1.38G
      for (i=0;i<effEnd;i++)
560
1.30G
      {
561
1.30G
         bandLogE[i+c*m->nbEBands] =
562
1.30G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.30G
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
1.30G
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
1.30G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
1.30G
#endif
568
1.30G
      }
569
82.4M
      for (i=effEnd;i<end;i++)
570
82.4M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
82.4M
   } while (++c < C);
572
63.1M
}
amp2Log2
Line
Count
Source
555
63.1M
{
556
63.1M
   int c, i;
557
63.1M
   c=0;
558
82.4M
   do {
559
1.38G
      for (i=0;i<effEnd;i++)
560
1.30G
      {
561
1.30G
         bandLogE[i+c*m->nbEBands] =
562
1.30G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.30G
               - 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
1.30G
      }
569
82.4M
      for (i=effEnd;i<end;i++)
570
0
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
82.4M
   } while (++c < C);
572
63.1M
}