Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/theora/lib/enquant.c
Line
Count
Source
1
/********************************************************************
2
 *                                                                  *
3
 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
4
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7
 *                                                                  *
8
 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009                *
9
 * by the Xiph.Org Foundation https://www.xiph.org/                 *
10
 *                                                                  *
11
 ********************************************************************
12
13
  function:
14
15
 ********************************************************************/
16
#include <stdlib.h>
17
#include <string.h>
18
#include "encint.h"
19
20
21
22
3.23k
int oc_quant_params_clone(th_quant_info *_dst,const th_quant_info *_src){
23
3.23k
  int i;
24
3.23k
  memcpy(_dst,_src,sizeof(*_dst));
25
3.23k
  memset(_dst->qi_ranges,0,sizeof(_dst->qi_ranges));
26
22.6k
  for(i=0;i<6;i++){
27
19.3k
    int nranges;
28
19.3k
    int qti;
29
19.3k
    int pli;
30
19.3k
    int qtj;
31
19.3k
    int plj;
32
19.3k
    int pdup;
33
19.3k
    int qdup;
34
19.3k
    qti=i/3;
35
19.3k
    pli=i%3;
36
19.3k
    qtj=(i-1)/3;
37
19.3k
    plj=(i-1)%3;
38
19.3k
    nranges=_src->qi_ranges[qti][pli].nranges;
39
    /*Check for those duplicates that can be cleanly handled by
40
       oc_quant_params_clear().*/
41
19.3k
    pdup=i>0&&nranges<=_src->qi_ranges[qtj][plj].nranges;
42
19.3k
    qdup=qti>0&&nranges<=_src->qi_ranges[0][pli].nranges;
43
19.3k
    _dst->qi_ranges[qti][pli].nranges=nranges;
44
19.3k
    if(pdup&&_src->qi_ranges[qti][pli].sizes==_src->qi_ranges[qtj][plj].sizes){
45
16.1k
      _dst->qi_ranges[qti][pli].sizes=_dst->qi_ranges[qtj][plj].sizes;
46
16.1k
    }
47
3.23k
    else if(qdup&&_src->qi_ranges[1][pli].sizes==_src->qi_ranges[0][pli].sizes){
48
0
      _dst->qi_ranges[1][pli].sizes=_dst->qi_ranges[0][pli].sizes;
49
0
    }
50
3.23k
    else{
51
3.23k
      int *sizes;
52
3.23k
      sizes=(int *)_ogg_malloc(nranges*sizeof(*sizes));
53
      /*Note: The caller is responsible for cleaning up any partially
54
         constructed qinfo.*/
55
3.23k
      if(sizes==NULL)return TH_EFAULT;
56
3.23k
      memcpy(sizes,_src->qi_ranges[qti][pli].sizes,nranges*sizeof(*sizes));
57
3.23k
      _dst->qi_ranges[qti][pli].sizes=sizes;
58
3.23k
    }
59
19.3k
    if(pdup&&_src->qi_ranges[qti][pli].base_matrices==
60
16.1k
     _src->qi_ranges[qtj][plj].base_matrices){
61
9.69k
      _dst->qi_ranges[qti][pli].base_matrices=
62
9.69k
       _dst->qi_ranges[qtj][plj].base_matrices;
63
9.69k
    }
64
9.69k
    else if(qdup&&_src->qi_ranges[1][pli].base_matrices==
65
3.23k
     _src->qi_ranges[0][pli].base_matrices){
66
0
      _dst->qi_ranges[1][pli].base_matrices=
67
0
       _dst->qi_ranges[0][pli].base_matrices;
68
0
    }
69
9.69k
    else{
70
9.69k
      th_quant_base *base_matrices;
71
9.69k
      base_matrices=(th_quant_base *)_ogg_malloc(
72
9.69k
       (nranges+1)*sizeof(*base_matrices));
73
      /*Note: The caller is responsible for cleaning up any partially
74
         constructed qinfo.*/
75
9.69k
      if(base_matrices==NULL)return TH_EFAULT;
76
9.69k
      memcpy(base_matrices,_src->qi_ranges[qti][pli].base_matrices,
77
9.69k
       (nranges+1)*sizeof(*base_matrices));
78
9.69k
      _dst->qi_ranges[qti][pli].base_matrices=
79
9.69k
       (const th_quant_base *)base_matrices;
80
9.69k
    }
81
19.3k
  }
82
3.23k
  return 0;
83
3.23k
}
84
85
3.23k
void oc_quant_params_pack(oggpack_buffer *_opb,const th_quant_info *_qinfo){
86
3.23k
  const th_quant_ranges *qranges;
87
3.23k
  const th_quant_base   *base_mats[2*3*64];
88
3.23k
  int                    indices[2][3][64];
89
3.23k
  int                    nbase_mats;
90
3.23k
  int                    nbits;
91
3.23k
  int                    ci;
92
3.23k
  int                    qi;
93
3.23k
  int                    qri;
94
3.23k
  int                    qti;
95
3.23k
  int                    pli;
96
3.23k
  int                    qtj;
97
3.23k
  int                    plj;
98
3.23k
  int                    bmi;
99
3.23k
  int                    i;
100
3.23k
  i=_qinfo->loop_filter_limits[0];
101
206k
  for(qi=1;qi<64;qi++)i=OC_MAXI(i,_qinfo->loop_filter_limits[qi]);
102
3.23k
  nbits=OC_ILOG_32(i);
103
3.23k
  oggpackB_write(_opb,nbits,3);
104
210k
  for(qi=0;qi<64;qi++){
105
206k
    oggpackB_write(_opb,_qinfo->loop_filter_limits[qi],nbits);
106
206k
  }
107
  /*580 bits for VP3.*/
108
3.23k
  i=1;
109
210k
  for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->ac_scale[qi],i);
110
3.23k
  nbits=OC_ILOGNZ_32(i);
111
3.23k
  oggpackB_write(_opb,nbits-1,4);
112
210k
  for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->ac_scale[qi],nbits);
113
  /*516 bits for VP3.*/
114
3.23k
  i=1;
115
210k
  for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->dc_scale[qi],i);
116
3.23k
  nbits=OC_ILOGNZ_32(i);
117
3.23k
  oggpackB_write(_opb,nbits-1,4);
118
210k
  for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->dc_scale[qi],nbits);
119
  /*Consolidate any duplicate base matrices.*/
120
3.23k
  nbase_mats=0;
121
25.8k
  for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
122
19.3k
    qranges=_qinfo->qi_ranges[qti]+pli;
123
96.9k
    for(qri=0;qri<=qranges->nranges;qri++){
124
607k
      for(bmi=0;;bmi++){
125
607k
        if(bmi>=nbase_mats){
126
38.7k
          base_mats[bmi]=qranges->base_matrices+qri;
127
38.7k
          indices[qti][pli][qri]=nbase_mats++;
128
38.7k
          break;
129
38.7k
        }
130
568k
        else if(memcmp(base_mats[bmi][0],qranges->base_matrices[qri],
131
568k
         sizeof(base_mats[bmi][0]))==0){
132
38.7k
          indices[qti][pli][qri]=bmi;
133
38.7k
          break;
134
38.7k
        }
135
607k
      }
136
77.5k
    }
137
19.3k
  }
138
  /*Write out the list of unique base matrices.
139
    1545 bits for VP3 matrices.*/
140
3.23k
  oggpackB_write(_opb,nbase_mats-1,9);
141
42.0k
  for(bmi=0;bmi<nbase_mats;bmi++){
142
2.52M
    for(ci=0;ci<64;ci++)oggpackB_write(_opb,base_mats[bmi][0][ci],8);
143
38.7k
  }
144
  /*Now store quant ranges and their associated indices into the base matrix
145
     list.
146
    46 bits for VP3 matrices.*/
147
3.23k
  nbits=OC_ILOG_32(nbase_mats-1);
148
22.6k
  for(i=0;i<6;i++){
149
19.3k
    qti=i/3;
150
19.3k
    pli=i%3;
151
19.3k
    qranges=_qinfo->qi_ranges[qti]+pli;
152
19.3k
    if(i>0){
153
16.1k
      if(qti>0){
154
9.69k
        if(qranges->nranges==_qinfo->qi_ranges[qti-1][pli].nranges&&
155
9.69k
         memcmp(qranges->sizes,_qinfo->qi_ranges[qti-1][pli].sizes,
156
9.69k
         qranges->nranges*sizeof(qranges->sizes[0]))==0&&
157
9.69k
         memcmp(indices[qti][pli],indices[qti-1][pli],
158
9.69k
         (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
159
0
          oggpackB_write(_opb,1,2);
160
0
          continue;
161
0
        }
162
9.69k
      }
163
16.1k
      qtj=(i-1)/3;
164
16.1k
      plj=(i-1)%3;
165
16.1k
      if(qranges->nranges==_qinfo->qi_ranges[qtj][plj].nranges&&
166
16.1k
       memcmp(qranges->sizes,_qinfo->qi_ranges[qtj][plj].sizes,
167
16.1k
       qranges->nranges*sizeof(qranges->sizes[0]))==0&&
168
16.1k
       memcmp(indices[qti][pli],indices[qtj][plj],
169
16.1k
       (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
170
9.69k
        oggpackB_write(_opb,0,1+(qti>0));
171
9.69k
        continue;
172
9.69k
      }
173
6.46k
      oggpackB_write(_opb,1,1);
174
6.46k
    }
175
9.69k
    oggpackB_write(_opb,indices[qti][pli][0],nbits);
176
38.7k
    for(qi=qri=0;qi<63;qri++){
177
29.0k
      oggpackB_write(_opb,qranges->sizes[qri]-1,OC_ILOG_32(62-qi));
178
29.0k
      qi+=qranges->sizes[qri];
179
29.0k
      oggpackB_write(_opb,indices[qti][pli][qri+1],nbits);
180
29.0k
    }
181
9.69k
  }
182
3.23k
}
183
184
79.4M
void oc_iquant_init(oc_iquant *_this,ogg_uint16_t _d){
185
79.4M
  ogg_uint32_t t;
186
79.4M
  int          l;
187
79.4M
  _d<<=1;
188
79.4M
  l=OC_ILOGNZ_32(_d)-1;
189
79.4M
  t=1+((ogg_uint32_t)1<<16+l)/_d;
190
79.4M
  _this->m=(ogg_int16_t)(t-0x10000);
191
79.4M
  _this->l=l;
192
79.4M
}
193
194
void oc_enc_enquant_table_init_c(void *_enquant,
195
0
 const ogg_uint16_t _dequant[64]){
196
0
  oc_iquant *enquant;
197
0
  int        zzi;
198
  /*In the original VP3.2 code, the rounding offset and the size of the
199
     dead zone around 0 were controlled by a "sharpness" parameter.
200
    We now R-D optimize the tokens for each block after quantization,
201
     so the rounding offset should always be 1/2, and an explicit dead
202
     zone is unnecessary.
203
    Hence, all of that VP3.2 code is gone from here, and the remaining
204
     floating point code has been implemented as equivalent integer
205
     code with exact precision.*/
206
0
  enquant=(oc_iquant *)_enquant;
207
0
  for(zzi=0;zzi<64;zzi++)oc_iquant_init(enquant+zzi,_dequant[zzi]);
208
0
}
209
210
0
void oc_enc_enquant_table_fixup_c(void *_enquant[3][3][2],int _nqis){
211
0
  int pli;
212
0
  int qii;
213
0
  int qti;
214
0
  for(pli=0;pli<3;pli++)for(qii=1;qii<_nqis;qii++)for(qti=0;qti<2;qti++){
215
0
    *((oc_iquant *)_enquant[pli][qii][qti])=
216
0
     *((oc_iquant *)_enquant[pli][0][qti]);
217
0
  }
218
0
}
219
220
int oc_enc_quantize_c(ogg_int16_t _qdct[64],const ogg_int16_t _dct[64],
221
0
 const ogg_uint16_t _dequant[64],const void *_enquant){
222
0
  const oc_iquant *enquant;
223
0
  int              nonzero;
224
0
  int              zzi;
225
0
  int              val;
226
0
  int              d;
227
0
  int              s;
228
0
  enquant=(const oc_iquant *)_enquant;
229
0
  nonzero=0;
230
0
  for(zzi=0;zzi<64;zzi++){
231
0
    val=_dct[zzi];
232
0
    d=_dequant[zzi];
233
0
    val=val<<1;
234
0
    if(abs(val)>=d){
235
0
      s=OC_SIGNMASK(val);
236
      /*The bias added here rounds ties away from zero, since token
237
         optimization can only decrease the magnitude of the quantized
238
         value.*/
239
0
      val+=d+s^s;
240
      /*Note the arithmetic right shift is not guaranteed by ANSI C.
241
        Hopefully no one still uses ones-complement architectures.*/
242
0
      val=((enquant[zzi].m*(ogg_int32_t)val>>16)+val>>enquant[zzi].l)-s;
243
0
      _qdct[zzi]=(ogg_int16_t)val;
244
0
      nonzero=zzi;
245
0
    }
246
0
    else _qdct[zzi]=0;
247
0
  }
248
0
  return nonzero;
249
0
}
250
251
252
253
/*This table gives the square root of the fraction of the squared magnitude of
254
   each DCT coefficient relative to the total, scaled by 2**16, for both INTRA
255
   and INTER modes.
256
  These values were measured after motion-compensated prediction, before
257
   quantization, over a large set of test video (from QCIF to 1080p) encoded at
258
   all possible rates.
259
  The DC coefficient takes into account the DPCM prediction (using the
260
   quantized values from neighboring blocks, as the encoder does, but still
261
   before quantization of the coefficient in the current block).
262
  The results differ significantly from the expected variance (e.g., using an
263
   AR(1) model of the signal with rho=0.95, as is frequently done to compute
264
   the coding gain of the DCT).
265
  We use them to estimate an "average" quantizer for a given quantizer matrix,
266
   as this is used to parameterize a number of the rate control decisions.
267
  These values are themselves probably quantizer-matrix dependent, since the
268
   shape of the matrix affects the noise distribution in the reference frames,
269
   but they should at least give us _some_ amount of adaptivity to different
270
   matrices, as opposed to hard-coding a table of average Q values for the
271
   current set.
272
  The main features they capture are that a) only a few of the quantizers in
273
   the upper-left corner contribute anything significant at all (though INTER
274
   mode is significantly flatter) and b) the DPCM prediction of the DC
275
   coefficient gives a very minor improvement in the INTRA case and a quite
276
   significant one in the INTER case (over the expected variance).*/
277
static const ogg_uint16_t OC_RPSD[2][64]={
278
  {
279
    52725,17370,10399, 6867, 5115, 3798, 2942, 2076,
280
    17370, 9900, 6948, 4994, 3836, 2869, 2229, 1619,
281
    10399, 6948, 5516, 4202, 3376, 2573, 2015, 1461,
282
     6867, 4994, 4202, 3377, 2800, 2164, 1718, 1243,
283
     5115, 3836, 3376, 2800, 2391, 1884, 1530, 1091,
284
     3798, 2869, 2573, 2164, 1884, 1495, 1212,  873,
285
     2942, 2229, 2015, 1718, 1530, 1212, 1001,  704,
286
     2076, 1619, 1461, 1243, 1091,  873,  704,  474
287
  },
288
  {
289
    23411,15604,13529,11601,10683, 8958, 7840, 6142,
290
    15604,11901,10718, 9108, 8290, 6961, 6023, 4487,
291
    13529,10718, 9961, 8527, 7945, 6689, 5742, 4333,
292
    11601, 9108, 8527, 7414, 7084, 5923, 5175, 3743,
293
    10683, 8290, 7945, 7084, 6771, 5754, 4793, 3504,
294
     8958, 6961, 6689, 5923, 5754, 4679, 3936, 2989,
295
     7840, 6023, 5742, 5175, 4793, 3936, 3522, 2558,
296
     6142, 4487, 4333, 3743, 3504, 2989, 2558, 1829
297
  }
298
};
299
300
/*The fraction of the squared magnitude of the residuals in each color channel
301
   relative to the total, scaled by 2**16, for each pixel format.
302
  These values were measured after motion-compensated prediction, before
303
   quantization, over a large set of test video encoded at all possible rates.
304
  TODO: These values are only from INTER frames; they should be re-measured for
305
   INTRA frames.*/
306
static const ogg_uint16_t OC_PCD[4][3]={
307
  {59926, 3038, 2572},
308
  {55201, 5597, 4738},
309
  {55201, 5597, 4738},
310
  {47682, 9669, 8185}
311
};
312
313
314
/*Compute "average" quantizers for each qi level to use for rate control.
315
  We do one for each color channel, as well as an average across color
316
   channels, separately for INTER and INTRA, since their behavior is very
317
   different.
318
  The basic approach is to compute a harmonic average of the squared quantizer,
319
   weighted by the expected squared magnitude of the DCT coefficients.
320
  Under the (not quite true) assumption that DCT coefficients are
321
   Laplacian-distributed, this preserves the product Q*lambda, where
322
   lambda=sqrt(2/sigma**2) is the Laplacian distribution parameter (not to be
323
   confused with the lambda used in R-D optimization throughout most of the
324
   rest of the code), when the distributions from multiple coefficients are
325
   pooled.
326
  The value Q*lambda completely determines the entropy of coefficients drawn
327
   from a Laplacian distribution, and thus the expected bitrate.*/
328
void oc_enquant_qavg_init(ogg_int64_t _log_qavg[2][64],
329
 ogg_int16_t _log_plq[64][3][2],ogg_uint16_t _chroma_rd_scale[2][64][2],
330
3.23k
 ogg_uint16_t *_dequant[64][3][2],int _pixel_fmt){
331
3.23k
  int qi;
332
3.23k
  int pli;
333
3.23k
  int qti;
334
3.23k
  int ci;
335
420k
  for(qti=0;qti<2;qti++)for(qi=0;qi<64;qi++){
336
413k
    ogg_int64_t  q2;
337
413k
    ogg_uint32_t qp[3];
338
413k
    ogg_uint32_t cqp;
339
413k
    ogg_uint32_t d;
340
413k
    q2=0;
341
1.65M
    for(pli=0;pli<3;pli++){
342
1.24M
      qp[pli]=0;
343
80.6M
      for(ci=0;ci<64;ci++){
344
79.4M
        unsigned rq;
345
79.4M
        unsigned qd;
346
79.4M
        qd=_dequant[qi][pli][qti][OC_IZIG_ZAG[ci]];
347
79.4M
        rq=(OC_RPSD[qti][ci]+(qd>>1))/qd;
348
79.4M
        qp[pli]+=rq*(ogg_uint32_t)rq;
349
79.4M
      }
350
1.24M
      q2+=OC_PCD[_pixel_fmt][pli]*(ogg_int64_t)qp[pli];
351
      /*plq=1.0/sqrt(qp)*/
352
1.24M
      _log_plq[qi][pli][qti]=
353
1.24M
       (ogg_int16_t)(OC_Q10(32)-oc_blog32_q10(qp[pli])>>1);
354
1.24M
    }
355
413k
    d=OC_PCD[_pixel_fmt][1]+OC_PCD[_pixel_fmt][2];
356
413k
    cqp=(ogg_uint32_t)((OC_PCD[_pixel_fmt][1]*(ogg_int64_t)qp[1]+
357
413k
     OC_PCD[_pixel_fmt][2]*(ogg_int64_t)qp[2]+(d>>1))/d);
358
    /*chroma_rd_scale=clamp(0.25,cqp/qp[0],4)*/
359
413k
    d=OC_MAXI(qp[0]+(1<<OC_RD_SCALE_BITS-1)>>OC_RD_SCALE_BITS,1);
360
413k
    d=OC_CLAMPI(1<<OC_RD_SCALE_BITS-2,(cqp+(d>>1))/d,4<<OC_RD_SCALE_BITS);
361
413k
    _chroma_rd_scale[qti][qi][0]=(ogg_int16_t)d;
362
    /*chroma_rd_iscale=clamp(0.25,qp[0]/cqp,4)*/
363
413k
    d=OC_MAXI(OC_RD_ISCALE(cqp,1),1);
364
413k
    d=OC_CLAMPI(1<<OC_RD_ISCALE_BITS-2,(qp[0]+(d>>1))/d,4<<OC_RD_ISCALE_BITS);
365
413k
    _chroma_rd_scale[qti][qi][1]=(ogg_int16_t)d;
366
    /*qavg=1.0/sqrt(q2).*/
367
413k
    _log_qavg[qti][qi]=OC_Q57(48)-oc_blog64(q2)>>1;
368
413k
  }
369
3.23k
}