Coverage Report

Created: 2026-08-14 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/libtheora/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
0
int oc_quant_params_clone(th_quant_info *_dst,const th_quant_info *_src){
23
0
  int i;
24
0
  memcpy(_dst,_src,sizeof(*_dst));
25
0
  memset(_dst->qi_ranges,0,sizeof(_dst->qi_ranges));
26
0
  for(i=0;i<6;i++){
27
0
    int nranges;
28
0
    int qti;
29
0
    int pli;
30
0
    int qtj;
31
0
    int plj;
32
0
    int pdup;
33
0
    int qdup;
34
0
    qti=i/3;
35
0
    pli=i%3;
36
0
    qtj=(i-1)/3;
37
0
    plj=(i-1)%3;
38
0
    nranges=_src->qi_ranges[qti][pli].nranges;
39
    /*Check for those duplicates that can be cleanly handled by
40
       oc_quant_params_clear().*/
41
0
    pdup=i>0&&nranges<=_src->qi_ranges[qtj][plj].nranges;
42
0
    qdup=qti>0&&nranges<=_src->qi_ranges[0][pli].nranges;
43
0
    _dst->qi_ranges[qti][pli].nranges=nranges;
44
0
    if(pdup&&_src->qi_ranges[qti][pli].sizes==_src->qi_ranges[qtj][plj].sizes){
45
0
      _dst->qi_ranges[qti][pli].sizes=_dst->qi_ranges[qtj][plj].sizes;
46
0
    }
47
0
    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
0
    else{
51
0
      int *sizes;
52
0
      sizes=(int *)_ogg_malloc(nranges*sizeof(*sizes));
53
      /*Note: The caller is responsible for cleaning up any partially
54
         constructed qinfo.*/
55
0
      if(sizes==NULL)return TH_EFAULT;
56
0
      memcpy(sizes,_src->qi_ranges[qti][pli].sizes,nranges*sizeof(*sizes));
57
0
      _dst->qi_ranges[qti][pli].sizes=sizes;
58
0
    }
59
0
    if(pdup&&_src->qi_ranges[qti][pli].base_matrices==
60
0
     _src->qi_ranges[qtj][plj].base_matrices){
61
0
      _dst->qi_ranges[qti][pli].base_matrices=
62
0
       _dst->qi_ranges[qtj][plj].base_matrices;
63
0
    }
64
0
    else if(qdup&&_src->qi_ranges[1][pli].base_matrices==
65
0
     _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
0
    else{
70
0
      th_quant_base *base_matrices;
71
0
      base_matrices=(th_quant_base *)_ogg_malloc(
72
0
       (nranges+1)*sizeof(*base_matrices));
73
      /*Note: The caller is responsible for cleaning up any partially
74
         constructed qinfo.*/
75
0
      if(base_matrices==NULL)return TH_EFAULT;
76
0
      memcpy(base_matrices,_src->qi_ranges[qti][pli].base_matrices,
77
0
       (nranges+1)*sizeof(*base_matrices));
78
0
      _dst->qi_ranges[qti][pli].base_matrices=
79
0
       (const th_quant_base *)base_matrices;
80
0
    }
81
0
  }
82
0
  return 0;
83
0
}
84
85
0
void oc_quant_params_pack(oggpack_buffer *_opb,const th_quant_info *_qinfo){
86
0
  const th_quant_ranges *qranges;
87
0
  const th_quant_base   *base_mats[2*3*64];
88
0
  int                    indices[2][3][64];
89
0
  int                    nbase_mats;
90
0
  int                    nbits;
91
0
  int                    ci;
92
0
  int                    qi;
93
0
  int                    qri;
94
0
  int                    qti;
95
0
  int                    pli;
96
0
  int                    qtj;
97
0
  int                    plj;
98
0
  int                    bmi;
99
0
  int                    i;
100
0
  i=_qinfo->loop_filter_limits[0];
101
0
  for(qi=1;qi<64;qi++)i=OC_MAXI(i,_qinfo->loop_filter_limits[qi]);
102
0
  nbits=OC_ILOG_32(i);
103
0
  oggpackB_write(_opb,nbits,3);
104
0
  for(qi=0;qi<64;qi++){
105
0
    oggpackB_write(_opb,_qinfo->loop_filter_limits[qi],nbits);
106
0
  }
107
  /*580 bits for VP3.*/
108
0
  i=1;
109
0
  for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->ac_scale[qi],i);
110
0
  nbits=OC_ILOGNZ_32(i);
111
0
  oggpackB_write(_opb,nbits-1,4);
112
0
  for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->ac_scale[qi],nbits);
113
  /*516 bits for VP3.*/
114
0
  i=1;
115
0
  for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->dc_scale[qi],i);
116
0
  nbits=OC_ILOGNZ_32(i);
117
0
  oggpackB_write(_opb,nbits-1,4);
118
0
  for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->dc_scale[qi],nbits);
119
  /*Consolidate any duplicate base matrices.*/
120
0
  nbase_mats=0;
121
0
  for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
122
0
    qranges=_qinfo->qi_ranges[qti]+pli;
123
0
    for(qri=0;qri<=qranges->nranges;qri++){
124
0
      for(bmi=0;;bmi++){
125
0
        if(bmi>=nbase_mats){
126
0
          base_mats[bmi]=qranges->base_matrices+qri;
127
0
          indices[qti][pli][qri]=nbase_mats++;
128
0
          break;
129
0
        }
130
0
        else if(memcmp(base_mats[bmi][0],qranges->base_matrices[qri],
131
0
         sizeof(base_mats[bmi][0]))==0){
132
0
          indices[qti][pli][qri]=bmi;
133
0
          break;
134
0
        }
135
0
      }
136
0
    }
137
0
  }
138
  /*Write out the list of unique base matrices.
139
    1545 bits for VP3 matrices.*/
140
0
  oggpackB_write(_opb,nbase_mats-1,9);
141
0
  for(bmi=0;bmi<nbase_mats;bmi++){
142
0
    for(ci=0;ci<64;ci++)oggpackB_write(_opb,base_mats[bmi][0][ci],8);
143
0
  }
144
  /*Now store quant ranges and their associated indices into the base matrix
145
     list.
146
    46 bits for VP3 matrices.*/
147
0
  nbits=OC_ILOG_32(nbase_mats-1);
148
0
  for(i=0;i<6;i++){
149
0
    qti=i/3;
150
0
    pli=i%3;
151
0
    qranges=_qinfo->qi_ranges[qti]+pli;
152
0
    if(i>0){
153
0
      if(qti>0){
154
0
        if(qranges->nranges==_qinfo->qi_ranges[qti-1][pli].nranges&&
155
0
         memcmp(qranges->sizes,_qinfo->qi_ranges[qti-1][pli].sizes,
156
0
         qranges->nranges*sizeof(qranges->sizes[0]))==0&&
157
0
         memcmp(indices[qti][pli],indices[qti-1][pli],
158
0
         (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
159
0
          oggpackB_write(_opb,1,2);
160
0
          continue;
161
0
        }
162
0
      }
163
0
      qtj=(i-1)/3;
164
0
      plj=(i-1)%3;
165
0
      if(qranges->nranges==_qinfo->qi_ranges[qtj][plj].nranges&&
166
0
       memcmp(qranges->sizes,_qinfo->qi_ranges[qtj][plj].sizes,
167
0
       qranges->nranges*sizeof(qranges->sizes[0]))==0&&
168
0
       memcmp(indices[qti][pli],indices[qtj][plj],
169
0
       (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
170
0
        oggpackB_write(_opb,0,1+(qti>0));
171
0
        continue;
172
0
      }
173
0
      oggpackB_write(_opb,1,1);
174
0
    }
175
0
    oggpackB_write(_opb,indices[qti][pli][0],nbits);
176
0
    for(qi=qri=0;qi<63;qri++){
177
0
      oggpackB_write(_opb,qranges->sizes[qri]-1,OC_ILOG_32(62-qi));
178
0
      qi+=qranges->sizes[qri];
179
0
      oggpackB_write(_opb,indices[qti][pli][qri+1],nbits);
180
0
    }
181
0
  }
182
0
}
183
184
0
void oc_iquant_init(oc_iquant *_this,ogg_uint16_t _d){
185
0
  ogg_uint32_t t;
186
0
  int          l;
187
0
  _d<<=1;
188
0
  l=OC_ILOGNZ_32(_d)-1;
189
0
  t=1+((ogg_uint32_t)1<<16+l)/_d;
190
0
  _this->m=(ogg_int16_t)(t-0x10000);
191
0
  _this->l=l;
192
0
}
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
0
 ogg_uint16_t *_dequant[64][3][2],int _pixel_fmt){
331
0
  int qi;
332
0
  int pli;
333
0
  int qti;
334
0
  int ci;
335
0
  for(qti=0;qti<2;qti++)for(qi=0;qi<64;qi++){
336
0
    ogg_int64_t  q2;
337
0
    ogg_uint32_t qp[3];
338
0
    ogg_uint32_t cqp;
339
0
    ogg_uint32_t d;
340
0
    q2=0;
341
0
    for(pli=0;pli<3;pli++){
342
0
      qp[pli]=0;
343
0
      for(ci=0;ci<64;ci++){
344
0
        unsigned rq;
345
0
        unsigned qd;
346
0
        qd=_dequant[qi][pli][qti][OC_IZIG_ZAG[ci]];
347
0
        rq=(OC_RPSD[qti][ci]+(qd>>1))/qd;
348
0
        qp[pli]+=rq*(ogg_uint32_t)rq;
349
0
      }
350
0
      q2+=OC_PCD[_pixel_fmt][pli]*(ogg_int64_t)qp[pli];
351
      /*plq=1.0/sqrt(qp)*/
352
0
      _log_plq[qi][pli][qti]=
353
0
       (ogg_int16_t)(OC_Q10(32)-oc_blog32_q10(qp[pli])>>1);
354
0
    }
355
0
    d=OC_PCD[_pixel_fmt][1]+OC_PCD[_pixel_fmt][2];
356
0
    cqp=(ogg_uint32_t)((OC_PCD[_pixel_fmt][1]*(ogg_int64_t)qp[1]+
357
0
     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
0
    d=OC_MAXI(qp[0]+(1<<OC_RD_SCALE_BITS-1)>>OC_RD_SCALE_BITS,1);
360
0
    d=OC_CLAMPI(1<<OC_RD_SCALE_BITS-2,(cqp+(d>>1))/d,4<<OC_RD_SCALE_BITS);
361
0
    _chroma_rd_scale[qti][qi][0]=(ogg_int16_t)d;
362
    /*chroma_rd_iscale=clamp(0.25,qp[0]/cqp,4)*/
363
0
    d=OC_MAXI(OC_RD_ISCALE(cqp,1),1);
364
0
    d=OC_CLAMPI(1<<OC_RD_ISCALE_BITS-2,(qp[0]+(d>>1))/d,4<<OC_RD_ISCALE_BITS);
365
0
    _chroma_rd_scale[qti][qi][1]=(ogg_int16_t)d;
366
    /*qavg=1.0/sqrt(q2).*/
367
0
    _log_qavg[qti][qi]=OC_Q57(48)-oc_blog64(q2)>>1;
368
0
  }
369
0
}