Coverage Report

Created: 2025-08-26 07:18

/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
112M
{
144
112M
   int c, i;
145
112M
   opus_val32 dist = 0;
146
146M
   c=0; do {
147
2.39G
      for (i=start;i<end;i++)
148
2.24G
      {
149
2.24G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
2.24G
         dist = MAC16_16(dist, d,d);
151
2.24G
      }
152
146M
   } while (++c<C);
153
112M
   return MIN32(200,SHR32(dist,14));
154
112M
}
quant_bands.c:loss_distortion
Line
Count
Source
143
56.4M
{
144
56.4M
   int c, i;
145
56.4M
   opus_val32 dist = 0;
146
73.3M
   c=0; do {
147
1.19G
      for (i=start;i<end;i++)
148
1.12G
      {
149
1.12G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
1.12G
         dist = MAC16_16(dist, d,d);
151
1.12G
      }
152
73.3M
   } while (++c<C);
153
56.4M
   return MIN32(200,SHR32(dist,14));
154
56.4M
}
quant_bands.c:loss_distortion
Line
Count
Source
143
56.4M
{
144
56.4M
   int c, i;
145
56.4M
   opus_val32 dist = 0;
146
73.3M
   c=0; do {
147
1.19G
      for (i=start;i<end;i++)
148
1.12G
      {
149
1.12G
         celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7);
150
1.12G
         dist = MAC16_16(dist, d,d);
151
1.12G
      }
152
73.3M
   } while (++c<C);
153
56.4M
   return MIN32(200,SHR32(dist,14));
154
56.4M
}
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
58.1M
{
162
58.1M
   int i, c;
163
58.1M
   int badness = 0;
164
58.1M
   opus_val32 prev[2] = {0,0};
165
58.1M
   opus_val16 coef;
166
58.1M
   opus_val16 beta;
167
168
58.1M
   if (tell+3 <= budget)
169
5.90M
      ec_enc_bit_logp(enc, intra, 3);
170
58.1M
   if (intra)
171
3.73M
   {
172
3.73M
      coef = 0;
173
3.73M
      beta = beta_intra;
174
54.4M
   } else {
175
54.4M
      beta = beta_coef[LM];
176
54.4M
      coef = pred_coef[LM];
177
54.4M
   }
178
179
   /* Encode at a fixed coarse resolution */
180
935M
   for (i=start;i<end;i++)
181
876M
   {
182
876M
      c=0;
183
1.14G
      do {
184
1.14G
         int bits_left;
185
1.14G
         int qi, qi0;
186
1.14G
         opus_val32 q;
187
1.14G
         celt_glog x;
188
1.14G
         opus_val32 f, tmp;
189
1.14G
         celt_glog oldE;
190
1.14G
         celt_glog decay_bound;
191
1.14G
         x = eBands[i+c*m->nbEBands];
192
1.14G
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
#ifdef FIXED_POINT
194
5.04M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
5.04M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
5.04M
         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.13G
         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.14G
         if (qi < 0 && x < decay_bound)
207
1.07G
         {
208
1.07G
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
1.07G
            if (qi > 0)
210
763k
               qi = 0;
211
1.07G
         }
212
1.14G
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
1.14G
         tell = ec_tell(enc);
216
1.14G
         bits_left = budget-tell-3*C*(end-i);
217
1.14G
         if (i!=start && bits_left < 30)
218
1.01G
         {
219
1.01G
            if (bits_left < 24)
220
1.00G
               qi = IMIN(1, qi);
221
1.01G
            if (bits_left < 16)
222
1.00G
               qi = IMAX(-1, qi);
223
1.01G
         }
224
1.14G
         if (lfe && i>=2)
225
27.3k
            qi = IMIN(qi, 0);
226
1.14G
         if (budget-tell >= 15)
227
66.2M
         {
228
66.2M
            int pi;
229
66.2M
            pi = 2*IMIN(i,20);
230
66.2M
            ec_laplace_encode(enc, &qi,
231
66.2M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
66.2M
         }
233
1.07G
         else if(budget-tell >= 2)
234
61.5k
         {
235
61.5k
            qi = IMAX(-1, IMIN(qi, 1));
236
61.5k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
61.5k
         }
238
1.07G
         else if(budget-tell >= 1)
239
2.80k
         {
240
2.80k
            qi = IMIN(0, qi);
241
2.80k
            ec_enc_bit_logp(enc, -qi, 1);
242
2.80k
         }
243
1.07G
         else
244
1.07G
            qi = -1;
245
1.14G
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
1.14G
         badness += abs(qi0-qi);
247
1.14G
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
1.14G
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
5.04M
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
1.14G
         oldEBands[i+c*m->nbEBands] = tmp;
254
1.14G
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
1.14G
      } while (++c < C);
256
876M
   }
257
58.1M
   return lfe ? 0 : badness;
258
58.1M
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
269k
{
162
269k
   int i, c;
163
269k
   int badness = 0;
164
269k
   opus_val32 prev[2] = {0,0};
165
269k
   opus_val16 coef;
166
269k
   opus_val16 beta;
167
168
269k
   if (tell+3 <= budget)
169
235k
      ec_enc_bit_logp(enc, intra, 3);
170
269k
   if (intra)
171
126k
   {
172
126k
      coef = 0;
173
126k
      beta = beta_intra;
174
142k
   } else {
175
142k
      beta = beta_coef[LM];
176
142k
      coef = pred_coef[LM];
177
142k
   }
178
179
   /* Encode at a fixed coarse resolution */
180
3.97M
   for (i=start;i<end;i++)
181
3.70M
   {
182
3.70M
      c=0;
183
5.04M
      do {
184
5.04M
         int bits_left;
185
5.04M
         int qi, qi0;
186
5.04M
         opus_val32 q;
187
5.04M
         celt_glog x;
188
5.04M
         opus_val32 f, tmp;
189
5.04M
         celt_glog oldE;
190
5.04M
         celt_glog decay_bound;
191
5.04M
         x = eBands[i+c*m->nbEBands];
192
5.04M
         oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
193
5.04M
#ifdef FIXED_POINT
194
5.04M
         f = x - MULT16_32_Q15(coef,oldE) - prev[c];
195
         /* Rounding to nearest integer here is really important! */
196
5.04M
         qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT;
197
5.04M
         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
5.04M
         if (qi < 0 && x < decay_bound)
207
735k
         {
208
735k
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
735k
            if (qi > 0)
210
197k
               qi = 0;
211
735k
         }
212
5.04M
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
5.04M
         tell = ec_tell(enc);
216
5.04M
         bits_left = budget-tell-3*C*(end-i);
217
5.04M
         if (i!=start && bits_left < 30)
218
680k
         {
219
680k
            if (bits_left < 24)
220
652k
               qi = IMIN(1, qi);
221
680k
            if (bits_left < 16)
222
622k
               qi = IMAX(-1, qi);
223
680k
         }
224
5.04M
         if (lfe && i>=2)
225
14.9k
            qi = IMIN(qi, 0);
226
5.04M
         if (budget-tell >= 15)
227
4.44M
         {
228
4.44M
            int pi;
229
4.44M
            pi = 2*IMIN(i,20);
230
4.44M
            ec_laplace_encode(enc, &qi,
231
4.44M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
4.44M
         }
233
600k
         else if(budget-tell >= 2)
234
22.5k
         {
235
22.5k
            qi = IMAX(-1, IMIN(qi, 1));
236
22.5k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
22.5k
         }
238
577k
         else if(budget-tell >= 1)
239
1.22k
         {
240
1.22k
            qi = IMIN(0, qi);
241
1.22k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.22k
         }
243
576k
         else
244
576k
            qi = -1;
245
5.04M
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
5.04M
         badness += abs(qi0-qi);
247
5.04M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
5.04M
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
5.04M
#ifdef FIXED_POINT
251
5.04M
         tmp = MAX32(-GCONST(28.f), tmp);
252
5.04M
#endif
253
5.04M
         oldEBands[i+c*m->nbEBands] = tmp;
254
5.04M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
5.04M
      } while (++c < C);
256
3.70M
   }
257
269k
   return lfe ? 0 : badness;
258
269k
}
quant_bands.c:quant_coarse_energy_impl
Line
Count
Source
161
57.9M
{
162
57.9M
   int i, c;
163
57.9M
   int badness = 0;
164
57.9M
   opus_val32 prev[2] = {0,0};
165
57.9M
   opus_val16 coef;
166
57.9M
   opus_val16 beta;
167
168
57.9M
   if (tell+3 <= budget)
169
5.66M
      ec_enc_bit_logp(enc, intra, 3);
170
57.9M
   if (intra)
171
3.60M
   {
172
3.60M
      coef = 0;
173
3.60M
      beta = beta_intra;
174
54.3M
   } else {
175
54.3M
      beta = beta_coef[LM];
176
54.3M
      coef = pred_coef[LM];
177
54.3M
   }
178
179
   /* Encode at a fixed coarse resolution */
180
931M
   for (i=start;i<end;i++)
181
873M
   {
182
873M
      c=0;
183
1.13G
      do {
184
1.13G
         int bits_left;
185
1.13G
         int qi, qi0;
186
1.13G
         opus_val32 q;
187
1.13G
         celt_glog x;
188
1.13G
         opus_val32 f, tmp;
189
1.13G
         celt_glog oldE;
190
1.13G
         celt_glog decay_bound;
191
1.13G
         x = eBands[i+c*m->nbEBands];
192
1.13G
         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.13G
         f = x-coef*oldE-prev[c];
200
         /* Rounding to nearest integer here is really important! */
201
1.13G
         qi = (int)floor(.5f+f);
202
1.13G
         decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay;
203
1.13G
#endif
204
         /* Prevent the energy from going down too quickly (e.g. for bands
205
            that have just one bin) */
206
1.13G
         if (qi < 0 && x < decay_bound)
207
1.07G
         {
208
1.07G
            qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT);
209
1.07G
            if (qi > 0)
210
566k
               qi = 0;
211
1.07G
         }
212
1.13G
         qi0 = qi;
213
         /* If we don't have enough bits to encode all the energy, just assume
214
             something safe. */
215
1.13G
         tell = ec_tell(enc);
216
1.13G
         bits_left = budget-tell-3*C*(end-i);
217
1.13G
         if (i!=start && bits_left < 30)
218
1.01G
         {
219
1.01G
            if (bits_left < 24)
220
1.00G
               qi = IMIN(1, qi);
221
1.01G
            if (bits_left < 16)
222
1.00G
               qi = IMAX(-1, qi);
223
1.01G
         }
224
1.13G
         if (lfe && i>=2)
225
12.3k
            qi = IMIN(qi, 0);
226
1.13G
         if (budget-tell >= 15)
227
61.7M
         {
228
61.7M
            int pi;
229
61.7M
            pi = 2*IMIN(i,20);
230
61.7M
            ec_laplace_encode(enc, &qi,
231
61.7M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
232
61.7M
         }
233
1.07G
         else if(budget-tell >= 2)
234
38.9k
         {
235
38.9k
            qi = IMAX(-1, IMIN(qi, 1));
236
38.9k
            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
237
38.9k
         }
238
1.07G
         else if(budget-tell >= 1)
239
1.58k
         {
240
1.58k
            qi = IMIN(0, qi);
241
1.58k
            ec_enc_bit_logp(enc, -qi, 1);
242
1.58k
         }
243
1.07G
         else
244
1.07G
            qi = -1;
245
1.13G
         error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT);
246
1.13G
         badness += abs(qi0-qi);
247
1.13G
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
248
249
1.13G
         tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q;
250
#ifdef FIXED_POINT
251
         tmp = MAX32(-GCONST(28.f), tmp);
252
#endif
253
1.13G
         oldEBands[i+c*m->nbEBands] = tmp;
254
1.13G
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
255
1.13G
      } while (++c < C);
256
873M
   }
257
57.9M
   return lfe ? 0 : badness;
258
57.9M
}
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
112M
{
265
112M
   int intra;
266
112M
   celt_glog max_decay;
267
112M
   VARDECL(celt_glog, oldEBands_intra);
268
112M
   VARDECL(celt_glog, error_intra);
269
112M
   ec_enc enc_start_state;
270
112M
   opus_uint32 tell;
271
112M
   int badness1=0;
272
112M
   opus_int32 intra_bias;
273
112M
   opus_val32 new_distortion;
274
112M
   SAVE_STACK;
275
276
112M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
112M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
112M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
112M
   tell = ec_tell(enc);
281
112M
   if (tell+3 > budget)
282
104M
      two_pass = intra = 0;
283
284
112M
   max_decay = GCONST(16.f);
285
112M
   if (end-start>10)
286
109M
   {
287
#ifdef FIXED_POINT
288
54.5M
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
54.5M
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
#endif
292
109M
   }
293
112M
   if (lfe)
294
3.49k
      max_decay = GCONST(3.f);
295
112M
   enc_start_state = *enc;
296
297
112M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
112M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
112M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
112M
   if (two_pass || intra)
302
7.46M
   {
303
7.46M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
7.46M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
7.46M
   }
306
307
112M
   if (!intra)
308
108M
   {
309
108M
      unsigned char *intra_buf;
310
108M
      ec_enc enc_intra_state;
311
108M
      opus_int32 tell_intra;
312
108M
      opus_uint32 nstart_bytes;
313
108M
      opus_uint32 nintra_bytes;
314
108M
      opus_uint32 save_bytes;
315
108M
      int badness2;
316
108M
      VARDECL(unsigned char, intra_bits);
317
318
108M
      tell_intra = ec_tell_frac(enc);
319
320
108M
      enc_intra_state = *enc;
321
322
108M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
108M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
108M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
108M
      save_bytes = nintra_bytes-nstart_bytes;
326
108M
      if (save_bytes == 0)
327
105M
         save_bytes = ALLOC_NONE;
328
108M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
108M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
108M
      *enc = enc_start_state;
333
334
108M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
108M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
108M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
1.22M
      {
339
1.22M
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
1.22M
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
1.22M
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
1.22M
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
1.22M
         intra = 1;
345
1.22M
      }
346
108M
   } else {
347
3.97M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
3.97M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
3.97M
   }
350
351
112M
   if (intra)
352
5.20M
      *delayedIntra = new_distortion;
353
107M
   else
354
107M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
112M
            new_distortion);
356
357
112M
   RESTORE_STACK;
358
112M
}
quant_coarse_energy
Line
Count
Source
264
56.4M
{
265
56.4M
   int intra;
266
56.4M
   celt_glog max_decay;
267
56.4M
   VARDECL(celt_glog, oldEBands_intra);
268
56.4M
   VARDECL(celt_glog, error_intra);
269
56.4M
   ec_enc enc_start_state;
270
56.4M
   opus_uint32 tell;
271
56.4M
   int badness1=0;
272
56.4M
   opus_int32 intra_bias;
273
56.4M
   opus_val32 new_distortion;
274
56.4M
   SAVE_STACK;
275
276
56.4M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
56.4M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
56.4M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
56.4M
   tell = ec_tell(enc);
281
56.4M
   if (tell+3 > budget)
282
52.2M
      two_pass = intra = 0;
283
284
56.4M
   max_decay = GCONST(16.f);
285
56.4M
   if (end-start>10)
286
54.5M
   {
287
54.5M
#ifdef FIXED_POINT
288
54.5M
      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
54.5M
   }
293
56.4M
   if (lfe)
294
1.74k
      max_decay = GCONST(3.f);
295
56.4M
   enc_start_state = *enc;
296
297
56.4M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
56.4M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
56.4M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
56.4M
   if (two_pass || intra)
302
3.73M
   {
303
3.73M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
3.73M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
3.73M
   }
306
307
56.4M
   if (!intra)
308
54.4M
   {
309
54.4M
      unsigned char *intra_buf;
310
54.4M
      ec_enc enc_intra_state;
311
54.4M
      opus_int32 tell_intra;
312
54.4M
      opus_uint32 nstart_bytes;
313
54.4M
      opus_uint32 nintra_bytes;
314
54.4M
      opus_uint32 save_bytes;
315
54.4M
      int badness2;
316
54.4M
      VARDECL(unsigned char, intra_bits);
317
318
54.4M
      tell_intra = ec_tell_frac(enc);
319
320
54.4M
      enc_intra_state = *enc;
321
322
54.4M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
54.4M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
54.4M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
54.4M
      save_bytes = nintra_bytes-nstart_bytes;
326
54.4M
      if (save_bytes == 0)
327
52.7M
         save_bytes = ALLOC_NONE;
328
54.4M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
54.4M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
54.4M
      *enc = enc_start_state;
333
334
54.4M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
54.4M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
54.4M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
613k
      {
339
613k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
613k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
613k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
613k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
613k
         intra = 1;
345
613k
      }
346
54.4M
   } else {
347
1.98M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
1.98M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
1.98M
   }
350
351
56.4M
   if (intra)
352
2.60M
      *delayedIntra = new_distortion;
353
53.8M
   else
354
53.8M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
56.4M
            new_distortion);
356
357
56.4M
   RESTORE_STACK;
358
56.4M
}
quant_coarse_energy
Line
Count
Source
264
56.4M
{
265
56.4M
   int intra;
266
56.4M
   celt_glog max_decay;
267
56.4M
   VARDECL(celt_glog, oldEBands_intra);
268
56.4M
   VARDECL(celt_glog, error_intra);
269
56.4M
   ec_enc enc_start_state;
270
56.4M
   opus_uint32 tell;
271
56.4M
   int badness1=0;
272
56.4M
   opus_int32 intra_bias;
273
56.4M
   opus_val32 new_distortion;
274
56.4M
   SAVE_STACK;
275
276
56.4M
   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
277
56.4M
   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
278
56.4M
   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
279
280
56.4M
   tell = ec_tell(enc);
281
56.4M
   if (tell+3 > budget)
282
52.2M
      two_pass = intra = 0;
283
284
56.4M
   max_decay = GCONST(16.f);
285
56.4M
   if (end-start>10)
286
54.5M
   {
287
#ifdef FIXED_POINT
288
      max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3);
289
#else
290
54.5M
      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
291
54.5M
#endif
292
54.5M
   }
293
56.4M
   if (lfe)
294
1.74k
      max_decay = GCONST(3.f);
295
56.4M
   enc_start_state = *enc;
296
297
56.4M
   ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog);
298
56.4M
   ALLOC(error_intra, C*m->nbEBands, celt_glog);
299
56.4M
   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
300
301
56.4M
   if (two_pass || intra)
302
3.73M
   {
303
3.73M
      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
304
3.73M
            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
305
3.73M
   }
306
307
56.4M
   if (!intra)
308
54.4M
   {
309
54.4M
      unsigned char *intra_buf;
310
54.4M
      ec_enc enc_intra_state;
311
54.4M
      opus_int32 tell_intra;
312
54.4M
      opus_uint32 nstart_bytes;
313
54.4M
      opus_uint32 nintra_bytes;
314
54.4M
      opus_uint32 save_bytes;
315
54.4M
      int badness2;
316
54.4M
      VARDECL(unsigned char, intra_bits);
317
318
54.4M
      tell_intra = ec_tell_frac(enc);
319
320
54.4M
      enc_intra_state = *enc;
321
322
54.4M
      nstart_bytes = ec_range_bytes(&enc_start_state);
323
54.4M
      nintra_bytes = ec_range_bytes(&enc_intra_state);
324
54.4M
      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
325
54.4M
      save_bytes = nintra_bytes-nstart_bytes;
326
54.4M
      if (save_bytes == 0)
327
52.7M
         save_bytes = ALLOC_NONE;
328
54.4M
      ALLOC(intra_bits, save_bytes, unsigned char);
329
      /* Copy bits from intra bit-stream */
330
54.4M
      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
331
332
54.4M
      *enc = enc_start_state;
333
334
54.4M
      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
335
54.4M
            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
336
337
54.4M
      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
338
613k
      {
339
613k
         *enc = enc_intra_state;
340
         /* Copy intra bits to bit-stream */
341
613k
         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
342
613k
         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
343
613k
         OPUS_COPY(error, error_intra, C*m->nbEBands);
344
613k
         intra = 1;
345
613k
      }
346
54.4M
   } else {
347
1.98M
      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
348
1.98M
      OPUS_COPY(error, error_intra, C*m->nbEBands);
349
1.98M
   }
350
351
56.4M
   if (intra)
352
2.60M
      *delayedIntra = new_distortion;
353
53.8M
   else
354
53.8M
      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
355
56.4M
            new_distortion);
356
357
56.4M
   RESTORE_STACK;
358
56.4M
}
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
56.4M
{
362
56.4M
   int i, c;
363
   /* Encode finer resolution */
364
915M
   for (i=start;i<end;i++)
365
859M
   {
366
859M
      opus_int16 extra, prev;
367
859M
      extra = 1<<extra_quant[i];
368
859M
      if (extra_quant[i] <= 0)
369
831M
         continue;
370
27.6M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
27.6M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
27.6M
      c=0;
373
32.1M
      do {
374
32.1M
         int q2;
375
32.1M
         celt_glog offset;
376
#ifdef FIXED_POINT
377
         /* Has to be without rounding */
378
2.08M
         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
32.1M
         if (q2 > extra-1)
383
30.1k
            q2 = extra-1;
384
32.1M
         if (q2<0)
385
5.81M
            q2 = 0;
386
32.1M
         ec_enc_bits(enc, q2, extra_quant[i]);
387
#ifdef FIXED_POINT
388
2.08M
         offset = SUB32(VSHR32(2*q2+1, extra_quant[i]-DB_SHIFT+1), GCONST(.5f));
389
2.08M
         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
32.1M
         oldEBands[i+c*m->nbEBands] += offset;
395
32.1M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
32.1M
      } while (++c < C);
398
27.6M
   }
399
56.4M
}
quant_fine_energy
Line
Count
Source
361
178k
{
362
178k
   int i, c;
363
   /* Encode finer resolution */
364
2.62M
   for (i=start;i<end;i++)
365
2.44M
   {
366
2.44M
      opus_int16 extra, prev;
367
2.44M
      extra = 1<<extra_quant[i];
368
2.44M
      if (extra_quant[i] <= 0)
369
941k
         continue;
370
1.50M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
1.50M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
1.50M
      c=0;
373
2.08M
      do {
374
2.08M
         int q2;
375
2.08M
         celt_glog offset;
376
2.08M
#ifdef FIXED_POINT
377
         /* Has to be without rounding */
378
2.08M
         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.08M
         if (q2 > extra-1)
383
2.76k
            q2 = extra-1;
384
2.08M
         if (q2<0)
385
106k
            q2 = 0;
386
2.08M
         ec_enc_bits(enc, q2, extra_quant[i]);
387
2.08M
#ifdef FIXED_POINT
388
2.08M
         offset = SUB32(VSHR32(2*q2+1, extra_quant[i]-DB_SHIFT+1), GCONST(.5f));
389
2.08M
         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.08M
         oldEBands[i+c*m->nbEBands] += offset;
395
2.08M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
2.08M
      } while (++c < C);
398
1.50M
   }
399
178k
}
quant_fine_energy
Line
Count
Source
361
56.2M
{
362
56.2M
   int i, c;
363
   /* Encode finer resolution */
364
913M
   for (i=start;i<end;i++)
365
857M
   {
366
857M
      opus_int16 extra, prev;
367
857M
      extra = 1<<extra_quant[i];
368
857M
      if (extra_quant[i] <= 0)
369
830M
         continue;
370
26.1M
      if (ec_tell(enc)+C*extra_quant[i] > (opus_int32)enc->storage*8) continue;
371
26.1M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
372
26.1M
      c=0;
373
30.0M
      do {
374
30.0M
         int q2;
375
30.0M
         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
30.0M
         q2 = (int)floor((error[i+c*m->nbEBands]*(1<<prev)+.5f)*extra);
381
30.0M
#endif
382
30.0M
         if (q2 > extra-1)
383
27.4k
            q2 = extra-1;
384
30.0M
         if (q2<0)
385
5.70M
            q2 = 0;
386
30.0M
         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
30.0M
         offset = (q2+.5f)*(1<<(14-extra_quant[i]))*(1.f/16384) - .5f;
392
30.0M
         offset *= (1<<(14-prev))*(1.f/16384);
393
30.0M
#endif
394
30.0M
         oldEBands[i+c*m->nbEBands] += offset;
395
30.0M
         error[i+c*m->nbEBands] -= offset;
396
         /*printf ("%f ", error[i] - offset);*/
397
30.0M
      } while (++c < C);
398
26.1M
   }
399
56.2M
}
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
112M
{
403
112M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
338M
   for (prio=0;prio<2;prio++)
407
225M
   {
408
253M
      for (i=start;i<end && bits_left>=C ;i++)
409
28.0M
      {
410
28.0M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
14.7M
            continue;
412
13.2M
         c=0;
413
15.1M
         do {
414
15.1M
            int q2;
415
15.1M
            celt_glog offset;
416
15.1M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
15.1M
            ec_enc_bits(enc, q2, 1);
418
#ifdef FIXED_POINT
419
7.58M
            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
15.1M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
15.1M
            error[i+c*m->nbEBands] -= offset;
425
15.1M
            bits_left--;
426
15.1M
         } while (++c < C);
427
13.2M
      }
428
225M
   }
429
112M
}
quant_energy_finalise
Line
Count
Source
402
56.4M
{
403
56.4M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
169M
   for (prio=0;prio<2;prio++)
407
112M
   {
408
126M
      for (i=start;i<end && bits_left>=C ;i++)
409
14.0M
      {
410
14.0M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
7.37M
            continue;
412
6.62M
         c=0;
413
7.58M
         do {
414
7.58M
            int q2;
415
7.58M
            celt_glog offset;
416
7.58M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
7.58M
            ec_enc_bits(enc, q2, 1);
418
7.58M
#ifdef FIXED_POINT
419
7.58M
            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
7.58M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
7.58M
            error[i+c*m->nbEBands] -= offset;
425
7.58M
            bits_left--;
426
7.58M
         } while (++c < C);
427
6.62M
      }
428
112M
   }
429
56.4M
}
quant_energy_finalise
Line
Count
Source
402
56.4M
{
403
56.4M
   int i, prio, c;
404
405
   /* Use up the remaining bits */
406
169M
   for (prio=0;prio<2;prio++)
407
112M
   {
408
126M
      for (i=start;i<end && bits_left>=C ;i++)
409
14.0M
      {
410
14.0M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
411
7.37M
            continue;
412
6.62M
         c=0;
413
7.58M
         do {
414
7.58M
            int q2;
415
7.58M
            celt_glog offset;
416
7.58M
            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
417
7.58M
            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
7.58M
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
422
7.58M
#endif
423
7.58M
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
424
7.58M
            error[i+c*m->nbEBands] -= offset;
425
7.58M
            bits_left--;
426
7.58M
         } while (++c < C);
427
6.62M
      }
428
112M
   }
429
56.4M
}
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
293k
{
433
293k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
293k
   int i, c;
435
293k
   opus_val64 prev[2] = {0, 0};
436
293k
   opus_val16 coef;
437
293k
   opus_val16 beta;
438
293k
   opus_int32 budget;
439
293k
   opus_int32 tell;
440
441
293k
   if (intra)
442
25.6k
   {
443
25.6k
      coef = 0;
444
25.6k
      beta = beta_intra;
445
268k
   } else {
446
268k
      beta = beta_coef[LM];
447
268k
      coef = pred_coef[LM];
448
268k
   }
449
450
293k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
4.93M
   for (i=start;i<end;i++)
454
4.64M
   {
455
4.64M
      c=0;
456
6.31M
      do {
457
6.31M
         int qi;
458
6.31M
         opus_val32 q;
459
6.31M
         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.31M
         celt_sig_assert(c<2);
464
6.31M
         tell = ec_tell(dec);
465
6.31M
         if(budget-tell>=15)
466
2.79M
         {
467
2.79M
            int pi;
468
2.79M
            pi = 2*IMIN(i,20);
469
2.79M
            qi = ec_laplace_decode(dec,
470
2.79M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
2.79M
         }
472
3.52M
         else if(budget-tell>=2)
473
1.13M
         {
474
1.13M
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
1.13M
            qi = (qi>>1)^-(qi&1);
476
1.13M
         }
477
2.38M
         else if(budget-tell>=1)
478
86.6k
         {
479
86.6k
            qi = -ec_dec_bit_logp(dec, 1);
480
86.6k
         }
481
2.29M
         else
482
2.29M
            qi = -1;
483
6.31M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
6.31M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
6.31M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
#ifdef FIXED_POINT
488
3.66M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
#endif
490
6.31M
         oldEBands[i+c*m->nbEBands] = tmp;
491
6.31M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
6.31M
      } while (++c < C);
493
4.64M
   }
494
293k
}
unquant_coarse_energy
Line
Count
Source
432
164k
{
433
164k
   const unsigned char *prob_model = e_prob_model[LM][intra];
434
164k
   int i, c;
435
164k
   opus_val64 prev[2] = {0, 0};
436
164k
   opus_val16 coef;
437
164k
   opus_val16 beta;
438
164k
   opus_int32 budget;
439
164k
   opus_int32 tell;
440
441
164k
   if (intra)
442
15.0k
   {
443
15.0k
      coef = 0;
444
15.0k
      beta = beta_intra;
445
149k
   } else {
446
149k
      beta = beta_coef[LM];
447
149k
      coef = pred_coef[LM];
448
149k
   }
449
450
164k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.83M
   for (i=start;i<end;i++)
454
2.67M
   {
455
2.67M
      c=0;
456
3.66M
      do {
457
3.66M
         int qi;
458
3.66M
         opus_val32 q;
459
3.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
3.66M
         celt_sig_assert(c<2);
464
3.66M
         tell = ec_tell(dec);
465
3.66M
         if(budget-tell>=15)
466
1.54M
         {
467
1.54M
            int pi;
468
1.54M
            pi = 2*IMIN(i,20);
469
1.54M
            qi = ec_laplace_decode(dec,
470
1.54M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.54M
         }
472
2.12M
         else if(budget-tell>=2)
473
681k
         {
474
681k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
681k
            qi = (qi>>1)^-(qi&1);
476
681k
         }
477
1.44M
         else if(budget-tell>=1)
478
54.6k
         {
479
54.6k
            qi = -ec_dec_bit_logp(dec, 1);
480
54.6k
         }
481
1.38M
         else
482
1.38M
            qi = -1;
483
3.66M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
3.66M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
3.66M
         tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q;
487
3.66M
#ifdef FIXED_POINT
488
3.66M
         tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp));
489
3.66M
#endif
490
3.66M
         oldEBands[i+c*m->nbEBands] = tmp;
491
3.66M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
3.66M
      } while (++c < C);
493
2.67M
   }
494
164k
}
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
10.5k
   {
443
10.5k
      coef = 0;
444
10.5k
      beta = beta_intra;
445
118k
   } else {
446
118k
      beta = beta_coef[LM];
447
118k
      coef = pred_coef[LM];
448
118k
   }
449
450
129k
   budget = dec->storage*8;
451
452
   /* Decode at a fixed coarse resolution */
453
2.09M
   for (i=start;i<end;i++)
454
1.96M
   {
455
1.96M
      c=0;
456
2.65M
      do {
457
2.65M
         int qi;
458
2.65M
         opus_val32 q;
459
2.65M
         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.65M
         celt_sig_assert(c<2);
464
2.65M
         tell = ec_tell(dec);
465
2.65M
         if(budget-tell>=15)
466
1.25M
         {
467
1.25M
            int pi;
468
1.25M
            pi = 2*IMIN(i,20);
469
1.25M
            qi = ec_laplace_decode(dec,
470
1.25M
                  prob_model[pi]<<7, prob_model[pi+1]<<6);
471
1.25M
         }
472
1.39M
         else if(budget-tell>=2)
473
454k
         {
474
454k
            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
475
454k
            qi = (qi>>1)^-(qi&1);
476
454k
         }
477
945k
         else if(budget-tell>=1)
478
31.9k
         {
479
31.9k
            qi = -ec_dec_bit_logp(dec, 1);
480
31.9k
         }
481
913k
         else
482
913k
            qi = -1;
483
2.65M
         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
484
485
2.65M
         oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]);
486
2.65M
         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.65M
         oldEBands[i+c*m->nbEBands] = tmp;
491
2.65M
         prev[c] = prev[c] + q - MULT16_32_Q15(beta,q);
492
2.65M
      } while (++c < C);
493
1.96M
   }
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
304k
{
498
304k
   int i, c;
499
   /* Decode finer resolution */
500
5.13M
   for (i=start;i<end;i++)
501
4.83M
   {
502
4.83M
      opus_int16 extra, prev;
503
4.83M
      extra = extra_quant[i];
504
4.83M
      if (extra_quant[i] <= 0)
505
3.57M
         continue;
506
1.25M
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
1.23M
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
1.23M
      c=0;
509
1.63M
      do {
510
1.63M
         int q2;
511
1.63M
         celt_glog offset;
512
1.63M
         q2 = ec_dec_bits(dec, extra);
513
#ifdef FIXED_POINT
514
881k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
881k
         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.63M
         oldEBands[i+c*m->nbEBands] += offset;
521
1.63M
      } while (++c < C);
522
1.23M
   }
523
304k
}
unquant_fine_energy
Line
Count
Source
497
170k
{
498
170k
   int i, c;
499
   /* Decode finer resolution */
500
2.95M
   for (i=start;i<end;i++)
501
2.78M
   {
502
2.78M
      opus_int16 extra, prev;
503
2.78M
      extra = extra_quant[i];
504
2.78M
      if (extra_quant[i] <= 0)
505
2.11M
         continue;
506
666k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
652k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
652k
      c=0;
509
881k
      do {
510
881k
         int q2;
511
881k
         celt_glog offset;
512
881k
         q2 = ec_dec_bits(dec, extra);
513
881k
#ifdef FIXED_POINT
514
881k
         offset = SUB32(VSHR32(2*q2+1, extra-DB_SHIFT+1), GCONST(.5f));
515
881k
         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
881k
         oldEBands[i+c*m->nbEBands] += offset;
521
881k
      } while (++c < C);
522
652k
   }
523
170k
}
unquant_fine_energy
Line
Count
Source
497
133k
{
498
133k
   int i, c;
499
   /* Decode finer resolution */
500
2.17M
   for (i=start;i<end;i++)
501
2.04M
   {
502
2.04M
      opus_int16 extra, prev;
503
2.04M
      extra = extra_quant[i];
504
2.04M
      if (extra_quant[i] <= 0)
505
1.45M
         continue;
506
589k
      if (ec_tell(dec)+C*extra_quant[i] > (opus_int32)dec->storage*8) continue;
507
582k
      prev = (prev_quant!=NULL) ? prev_quant[i] : 0;
508
582k
      c=0;
509
754k
      do {
510
754k
         int q2;
511
754k
         celt_glog offset;
512
754k
         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
754k
         offset = (q2+.5f)*(1<<(14-extra))*(1.f/16384) - .5f;
518
754k
         offset *= (1<<(14-prev))*(1.f/16384);
519
754k
#endif
520
754k
         oldEBands[i+c*m->nbEBands] += offset;
521
754k
      } while (++c < C);
522
582k
   }
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
571k
{
527
571k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
1.71M
   for (prio=0;prio<2;prio++)
531
1.14M
   {
532
3.03M
      for (i=start;i<end && bits_left>=C ;i++)
533
1.89M
      {
534
1.89M
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
1.49M
            continue;
536
394k
         c=0;
537
566k
         do {
538
566k
            int q2;
539
566k
            celt_glog offset;
540
566k
            q2 = ec_dec_bits(dec, 1);
541
#ifdef FIXED_POINT
542
283k
            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
566k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
566k
            bits_left--;
548
566k
         } while (++c < C);
549
394k
      }
550
1.14M
   }
551
571k
}
unquant_energy_finalise
Line
Count
Source
526
285k
{
527
285k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
857k
   for (prio=0;prio<2;prio++)
531
571k
   {
532
1.51M
      for (i=start;i<end && bits_left>=C ;i++)
533
945k
      {
534
945k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
748k
            continue;
536
197k
         c=0;
537
283k
         do {
538
283k
            int q2;
539
283k
            celt_glog offset;
540
283k
            q2 = ec_dec_bits(dec, 1);
541
283k
#ifdef FIXED_POINT
542
283k
            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
283k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
283k
            bits_left--;
548
283k
         } while (++c < C);
549
197k
      }
550
571k
   }
551
285k
}
unquant_energy_finalise
Line
Count
Source
526
285k
{
527
285k
   int i, prio, c;
528
529
   /* Use up the remaining bits */
530
857k
   for (prio=0;prio<2;prio++)
531
571k
   {
532
1.51M
      for (i=start;i<end && bits_left>=C ;i++)
533
945k
      {
534
945k
         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
535
748k
            continue;
536
197k
         c=0;
537
283k
         do {
538
283k
            int q2;
539
283k
            celt_glog offset;
540
283k
            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
283k
            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
545
283k
#endif
546
283k
            if (oldEBands != NULL) oldEBands[i+c*m->nbEBands] += offset;
547
283k
            bits_left--;
548
283k
         } while (++c < C);
549
197k
      }
550
571k
   }
551
285k
}
552
553
void amp2Log2(const CELTMode *m, int effEnd, int end,
554
      celt_ener *bandE, celt_glog *bandLogE, int C)
555
170M
{
556
170M
   int c, i;
557
170M
   c=0;
558
221M
   do {
559
3.72G
      for (i=0;i<effEnd;i++)
560
3.50G
      {
561
3.50G
         bandLogE[i+c*m->nbEBands] =
562
3.50G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
3.50G
               - 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.33G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
#endif
568
3.50G
      }
569
221M
      for (i=effEnd;i<end;i++)
570
147M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
221M
   } while (++c < C);
572
170M
}
amp2Log2
Line
Count
Source
555
56.7M
{
556
56.7M
   int c, i;
557
56.7M
   c=0;
558
73.7M
   do {
559
1.24G
      for (i=0;i<effEnd;i++)
560
1.16G
      {
561
1.16G
         bandLogE[i+c*m->nbEBands] =
562
1.16G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.16G
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
1.16G
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
1.16G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
1.16G
#endif
568
1.16G
      }
569
73.7M
      for (i=effEnd;i<end;i++)
570
73.7M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
73.7M
   } while (++c < C);
572
56.7M
}
amp2Log2
Line
Count
Source
555
56.7M
{
556
56.7M
   int c, i;
557
56.7M
   c=0;
558
73.7M
   do {
559
1.24G
      for (i=0;i<effEnd;i++)
560
1.16G
      {
561
1.16G
         bandLogE[i+c*m->nbEBands] =
562
1.16G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.16G
               - SHL32((celt_glog)eMeans[i],DB_SHIFT-4);
564
1.16G
#ifdef FIXED_POINT
565
         /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */
566
1.16G
         bandLogE[i+c*m->nbEBands] += GCONST(2.f);
567
1.16G
#endif
568
1.16G
      }
569
73.7M
      for (i=effEnd;i<end;i++)
570
73.7M
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
73.7M
   } while (++c < C);
572
56.7M
}
amp2Log2
Line
Count
Source
555
56.7M
{
556
56.7M
   int c, i;
557
56.7M
   c=0;
558
73.7M
   do {
559
1.24G
      for (i=0;i<effEnd;i++)
560
1.16G
      {
561
1.16G
         bandLogE[i+c*m->nbEBands] =
562
1.16G
               celt_log2_db(bandE[i+c*m->nbEBands])
563
1.16G
               - 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.16G
      }
569
73.7M
      for (i=effEnd;i<end;i++)
570
0
         bandLogE[c*m->nbEBands+i] = -GCONST(14.f);
571
73.7M
   } while (++c < C);
572
56.7M
}