Coverage Report

Created: 2026-05-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/theora/lib/rate.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
/*A rough lookup table for tan(x), 0<=x<pi/2.
21
  The values are Q12 fixed-point and spaced at 5 degree intervals.
22
  These decisions are somewhat arbitrary, but sufficient for the 2nd order
23
   Bessel follower below.
24
  Values of x larger than 85 degrees are extrapolated from the last interval,
25
   which is way off, but "good enough".*/
26
static unsigned short OC_ROUGH_TAN_LOOKUP[18]={
27
      0,  358,  722, 1098, 1491, 1910,
28
   2365, 2868, 3437, 4096, 4881, 5850,
29
   7094, 8784,11254,15286,23230,46817
30
};
31
32
/*_alpha is Q24 in the range [0,0.5).
33
  The return values is 5.12.*/
34
28.7k
static int oc_warp_alpha(int _alpha){
35
28.7k
  int i;
36
28.7k
  int d;
37
28.7k
  int t0;
38
28.7k
  int t1;
39
28.7k
  i=_alpha*36>>24;
40
28.7k
  if(i>=17)i=16;
41
28.7k
  t0=OC_ROUGH_TAN_LOOKUP[i];
42
28.7k
  t1=OC_ROUGH_TAN_LOOKUP[i+1];
43
28.7k
  d=_alpha*36-(i<<24);
44
28.7k
  return (int)(((ogg_int64_t)t0<<32)+(t1-t0<<8)*(ogg_int64_t)d>>32);
45
28.7k
}
46
47
/*Re-initialize the Bessel filter coefficients with the specified delay.
48
  This does not alter the x/y state, but changes the reaction time of the
49
   filter.
50
  Altering the time constant of a reactive filter without alterning internal
51
   state is something that has to be done carefully, but our design operates at
52
   high enough delays and with small enough time constant changes to make it
53
   safe.*/
54
9.58k
static void oc_iir_filter_reinit(oc_iir_filter *_f,int _delay){
55
9.58k
  int         alpha;
56
9.58k
  ogg_int64_t one48;
57
9.58k
  ogg_int64_t warp;
58
9.58k
  ogg_int64_t k1;
59
9.58k
  ogg_int64_t k2;
60
9.58k
  ogg_int64_t d;
61
9.58k
  ogg_int64_t a;
62
9.58k
  ogg_int64_t ik2;
63
9.58k
  ogg_int64_t b1;
64
9.58k
  ogg_int64_t b2;
65
  /*This borrows some code from an unreleased version of Postfish.
66
    See the recipe at http://unicorn.us.com/alex/2polefilters.html for details
67
     on deriving the filter coefficients.*/
68
  /*alpha is Q24*/
69
9.58k
  alpha=(1<<24)/_delay;
70
9.58k
  one48=(ogg_int64_t)1<<48;
71
  /*warp is 7.12*/
72
9.58k
  warp=OC_MAXI(oc_warp_alpha(alpha),1);
73
  /*k1 is 9.12*/
74
9.58k
  k1=3*warp;
75
  /*k2 is 16.24.*/
76
9.58k
  k2=k1*warp;
77
  /*d is 16.15.*/
78
9.58k
  d=((1<<12)+k1<<12)+k2+256>>9;
79
  /*a is 0.32, since d is larger than both 1.0 and k2.*/
80
9.58k
  a=(k2<<23)/d;
81
  /*ik2 is 25.24.*/
82
9.58k
  ik2=one48/k2;
83
  /*b1 is Q56; in practice, the integer ranges between -2 and 2.*/
84
9.58k
  b1=2*a*(ik2-(1<<24));
85
  /*b2 is Q56; in practice, the integer ranges between -2 and 2.*/
86
9.58k
  b2=(one48<<8)-(4*a<<24)-b1;
87
  /*All of the filter parameters are Q24.*/
88
9.58k
  _f->c[0]=(ogg_int32_t)(b1+((ogg_int64_t)1<<31)>>32);
89
9.58k
  _f->c[1]=(ogg_int32_t)(b2+((ogg_int64_t)1<<31)>>32);
90
9.58k
  _f->g=(ogg_int32_t)(a+128>>8);
91
9.58k
}
92
93
/*Initialize a 2nd order low-pass Bessel filter with the corresponding delay
94
   and initial value.
95
  _value is Q24.*/
96
7.73k
static void oc_iir_filter_init(oc_iir_filter *_f,int _delay,ogg_int32_t _value){
97
7.73k
  oc_iir_filter_reinit(_f,_delay);
98
7.73k
  _f->y[1]=_f->y[0]=_f->x[1]=_f->x[0]=_value;
99
7.73k
}
100
101
47.4k
static ogg_int64_t oc_iir_filter_update(oc_iir_filter *_f,ogg_int32_t _x){
102
47.4k
  ogg_int64_t c0;
103
47.4k
  ogg_int64_t c1;
104
47.4k
  ogg_int64_t g;
105
47.4k
  ogg_int64_t x0;
106
47.4k
  ogg_int64_t x1;
107
47.4k
  ogg_int64_t y0;
108
47.4k
  ogg_int64_t y1;
109
47.4k
  ogg_int64_t ya;
110
47.4k
  c0=_f->c[0];
111
47.4k
  c1=_f->c[1];
112
47.4k
  g=_f->g;
113
47.4k
  x0=_f->x[0];
114
47.4k
  x1=_f->x[1];
115
47.4k
  y0=_f->y[0];
116
47.4k
  y1=_f->y[1];
117
47.4k
  ya=(_x+x0*2+x1)*g+y0*c0+y1*c1+(1<<23)>>24;
118
47.4k
  _f->x[1]=(ogg_int32_t)x0;
119
47.4k
  _f->x[0]=_x;
120
47.4k
  _f->y[1]=(ogg_int32_t)y0;
121
47.4k
  _f->y[0]=(ogg_int32_t)ya;
122
47.4k
  return ya;
123
47.4k
}
124
125
126
127
/*Search for the quantizer that matches the target most closely.
128
  We don't assume a linear ordering, but when there are ties we pick the
129
   quantizer closest to the old one.*/
130
static int oc_enc_find_qi_for_target(oc_enc_ctx *_enc,int _qti,int _qi_old,
131
101k
 int _qi_min,ogg_int64_t _log_qtarget){
132
101k
  ogg_int64_t best_qdiff;
133
101k
  int         best_qi;
134
101k
  int         qi;
135
101k
  best_qi=_qi_min;
136
101k
  best_qdiff=_enc->log_qavg[_qti][best_qi]-_log_qtarget;
137
101k
  best_qdiff=best_qdiff+OC_SIGNMASK(best_qdiff)^OC_SIGNMASK(best_qdiff);
138
6.50M
  for(qi=_qi_min+1;qi<64;qi++){
139
6.39M
    ogg_int64_t qdiff;
140
6.39M
    qdiff=_enc->log_qavg[_qti][qi]-_log_qtarget;
141
6.39M
    qdiff=qdiff+OC_SIGNMASK(qdiff)^OC_SIGNMASK(qdiff);
142
6.39M
    if(qdiff<best_qdiff||
143
4.76M
     qdiff==best_qdiff&&abs(qi-_qi_old)<abs(best_qi-_qi_old)){
144
4.76M
      best_qi=qi;
145
4.76M
      best_qdiff=qdiff;
146
4.76M
    }
147
6.39M
  }
148
101k
  return best_qi;
149
101k
}
150
151
51.4k
void oc_enc_calc_lambda(oc_enc_ctx *_enc,int _qti){
152
51.4k
  ogg_int64_t lq;
153
51.4k
  int         qi;
154
51.4k
  int         qi1;
155
51.4k
  int         nqis;
156
  /*For now, lambda is fixed depending on the qi value and frame type:
157
      lambda=qscale*(qavg[qti][qi]**2),
158
     where qscale=0.2125.
159
    This was derived by exhaustively searching for the optimal quantizer for
160
     the AC coefficients in each block from a number of test sequences for a
161
     number of fixed lambda values and fitting the peaks of the resulting
162
     histograms (on the log(qavg) scale).
163
    The same model applies to both inter and intra frames.
164
    A more adaptive scheme might perform better.*/
165
51.4k
  qi=_enc->state.qis[0];
166
  /*If rate control is active, use the lambda for the _target_ quantizer.
167
    This allows us to scale to rates slightly lower than we'd normally be able
168
     to reach, and give the rate control a semblance of "fractional qi"
169
     precision.
170
    TODO: Add API for changing QI, and allow extra precision.*/
171
51.4k
  if(_enc->state.info.target_bitrate>0)lq=_enc->rc.log_qtarget;
172
5.77k
  else lq=_enc->log_qavg[_qti][qi];
173
  /*The resulting lambda value is less than 0x500000.*/
174
51.4k
  _enc->lambda=(int)oc_bexp64(2*lq-0x4780BD468D6B62BLL);
175
  /*Select additional quantizers.
176
    The R-D optimal block AC quantizer statistics suggest that the distribution
177
     is roughly Gaussian-like with a slight positive skew.
178
    K-means clustering on log_qavg to select 3 quantizers produces cluster
179
     centers of {log_qavg-0.6,log_qavg,log_qavg+0.7}.
180
    Experiments confirm these are relatively good choices.
181
182
    Although we do greedy R-D optimization of the qii flags to avoid switching
183
     too frequently, this becomes ineffective at low rates, either because we
184
     do a poor job of predicting the actual R-D cost, or the greedy
185
     optimization is not sufficient.
186
    Therefore adaptive quantization is disabled above an (experimentally
187
     suggested) threshold of log_qavg=7.00 (e.g., below INTRA qi=12 or
188
     INTER qi=20 with current matrices).
189
    This may need to be revised if the R-D cost estimation or qii flag
190
     optimization strategies change.*/
191
51.4k
  nqis=1;
192
51.4k
  if(lq<(OC_Q57(56)>>3)&&!_enc->vp3_compatible&&
193
27.9k
   _enc->sp_level<OC_SP_LEVEL_FAST_ANALYSIS){
194
27.9k
    qi1=oc_enc_find_qi_for_target(_enc,_qti,OC_MAXI(qi-1,0),0,
195
27.9k
     lq+(OC_Q57(7)+5)/10);
196
27.9k
    if(qi1!=qi)_enc->state.qis[nqis++]=qi1;
197
27.9k
    qi1=oc_enc_find_qi_for_target(_enc,_qti,OC_MINI(qi+1,63),0,
198
27.9k
     lq-(OC_Q57(6)+5)/10);
199
27.9k
    if(qi1!=qi&&qi1!=_enc->state.qis[nqis-1])_enc->state.qis[nqis++]=qi1;
200
27.9k
  }
201
51.4k
  _enc->state.nqis=nqis;
202
51.4k
}
203
204
/*Binary exponential of _log_scale with 24-bit fractional precision and
205
   saturation.
206
  _log_scale: A binary logarithm in Q24 format.
207
  Return: The binary exponential in Q24 format, saturated to 2**47-1 if
208
   _log_scale was too large.*/
209
0
static ogg_int64_t oc_bexp_q24(ogg_int32_t _log_scale){
210
0
  if(_log_scale<(ogg_int32_t)23<<24){
211
0
    ogg_int64_t ret;
212
0
    ret=oc_bexp64(((ogg_int64_t)_log_scale<<33)+OC_Q57(24));
213
0
    return ret<0x7FFFFFFFFFFFLL?ret:0x7FFFFFFFFFFFLL;
214
0
  }
215
0
  return 0x7FFFFFFFFFFFLL;
216
0
}
217
218
/*Convenience function converts Q57 value to a clamped 32-bit Q24 value
219
  _in: input in Q57 format.
220
  Return: same number in Q24 */
221
34.6k
static ogg_int32_t oc_q57_to_q24(ogg_int64_t _in){
222
34.6k
  ogg_int64_t ret;
223
34.6k
  ret=_in+((ogg_int64_t)1<<32)>>33;
224
  /*0x80000000 is automatically converted to unsigned on 32-bit systems.
225
    -0x7FFFFFFF-1 is needed to avoid "promoting" the whole expression to
226
    unsigned.*/
227
34.6k
  return (ogg_int32_t)OC_CLAMPI(-0x7FFFFFFF-1,ret,0x7FFFFFFF);
228
34.6k
}
229
230
/*Binary exponential of _log_scale with 24-bit fractional precision and
231
   saturation.
232
  _log_scale: A binary logarithm in Q57 format.
233
  Return: The binary exponential in Q24 format, saturated to 2**31-1 if
234
   _log_scale was too large.*/
235
2.57k
static ogg_int32_t oc_bexp64_q24(ogg_int64_t _log_scale){
236
2.57k
  if(_log_scale<OC_Q57(8)){
237
2.57k
    ogg_int64_t ret;
238
2.57k
    ret=oc_bexp64(_log_scale+OC_Q57(24));
239
2.57k
    return ret<0x7FFFFFFF?(ogg_int32_t)ret:0x7FFFFFFF;
240
2.57k
  }
241
0
  return 0x7FFFFFFF;
242
2.57k
}
243
244
245
2.57k
static void oc_enc_rc_reset(oc_enc_ctx *_enc){
246
2.57k
  ogg_int64_t npixels;
247
2.57k
  ogg_int64_t ibpp;
248
2.57k
  int         inter_delay;
249
  /*TODO: These parameters should be exposed in a th_encode_ctl() API.*/
250
2.57k
  _enc->rc.bits_per_frame=(_enc->state.info.target_bitrate*
251
2.57k
   (ogg_int64_t)_enc->state.info.fps_denominator)/
252
2.57k
   _enc->state.info.fps_numerator;
253
  /*Insane framerates or frame sizes mean insane bitrates.
254
    Let's not get carried away.*/
255
2.57k
  if(_enc->rc.bits_per_frame>0x400000000000LL){
256
104
    _enc->rc.bits_per_frame=(ogg_int64_t)0x400000000000LL;
257
104
  }
258
2.47k
  else if(_enc->rc.bits_per_frame<32)_enc->rc.bits_per_frame=32;
259
2.57k
  _enc->rc.buf_delay=OC_MAXI(_enc->rc.buf_delay,12);
260
2.57k
  _enc->rc.max=_enc->rc.bits_per_frame*_enc->rc.buf_delay;
261
  /*Start with a buffer fullness of 50% plus 25% of the amount we plan to spend
262
     on a single keyframe interval.
263
    We can require fully half the bits in an interval for a keyframe, so this
264
     initial level gives us maximum flexibility for over/under-shooting in
265
     subsequent frames.*/
266
2.57k
  _enc->rc.target=(_enc->rc.max+1>>1)+(_enc->rc.bits_per_frame+2>>2)*
267
2.57k
   OC_MINI(_enc->keyframe_frequency_force,_enc->rc.buf_delay);
268
2.57k
  _enc->rc.fullness=_enc->rc.target;
269
  /*Pick exponents and initial scales for quantizer selection.*/
270
2.57k
  npixels=_enc->state.info.frame_width*
271
2.57k
   (ogg_int64_t)_enc->state.info.frame_height;
272
2.57k
  _enc->rc.log_npixels=oc_blog64(npixels);
273
2.57k
  ibpp=npixels/_enc->rc.bits_per_frame;
274
2.57k
  if(ibpp<1){
275
1.29k
    _enc->rc.exp[0]=59;
276
1.29k
    _enc->rc.log_scale[0]=oc_blog64(1997)-OC_Q57(8);
277
1.29k
  }
278
1.28k
  else if(ibpp<2){
279
36
    _enc->rc.exp[0]=55;
280
36
    _enc->rc.log_scale[0]=oc_blog64(1604)-OC_Q57(8);
281
36
  }
282
1.24k
  else{
283
1.24k
    _enc->rc.exp[0]=48;
284
1.24k
    _enc->rc.log_scale[0]=oc_blog64(834)-OC_Q57(8);
285
1.24k
  }
286
2.57k
  if(ibpp<4){
287
1.44k
    _enc->rc.exp[1]=100;
288
1.44k
    _enc->rc.log_scale[1]=oc_blog64(2249)-OC_Q57(8);
289
1.44k
  }
290
1.13k
  else if(ibpp<8){
291
71
    _enc->rc.exp[1]=95;
292
71
    _enc->rc.log_scale[1]=oc_blog64(1751)-OC_Q57(8);
293
71
  }
294
1.06k
  else{
295
1.06k
    _enc->rc.exp[1]=73;
296
1.06k
    _enc->rc.log_scale[1]=oc_blog64(1260)-OC_Q57(8);
297
1.06k
  }
298
2.57k
  _enc->rc.prev_drop_count=0;
299
2.57k
  _enc->rc.log_drop_scale=OC_Q57(0);
300
  /*Set up second order followers, initialized according to corresponding
301
     time constants.*/
302
2.57k
  oc_iir_filter_init(&_enc->rc.scalefilter[0],4,
303
2.57k
   oc_q57_to_q24(_enc->rc.log_scale[0]));
304
2.57k
  inter_delay=(_enc->rc.twopass?
305
2.57k
   OC_MAXI(_enc->keyframe_frequency_force,12):_enc->rc.buf_delay)>>1;
306
2.57k
  _enc->rc.inter_count=0;
307
  /*We clamp the actual inter_delay to a minimum of 10 to work within the range
308
     of values where later incrementing the delay works as designed.
309
    10 is not an exact choice, but rather a good working trade-off.*/
310
2.57k
  _enc->rc.inter_delay=10;
311
2.57k
  _enc->rc.inter_delay_target=inter_delay;
312
2.57k
  oc_iir_filter_init(&_enc->rc.scalefilter[1],_enc->rc.inter_delay,
313
2.57k
   oc_q57_to_q24(_enc->rc.log_scale[1]));
314
2.57k
  oc_iir_filter_init(&_enc->rc.vfrfilter,4,
315
2.57k
   oc_bexp64_q24(_enc->rc.log_drop_scale));
316
2.57k
}
317
318
2.87k
void oc_rc_state_init(oc_rc_state *_rc,oc_enc_ctx *_enc){
319
2.87k
  _rc->twopass=0;
320
2.87k
  _rc->twopass_buffer_bytes=0;
321
2.87k
  _rc->twopass_force_kf=0;
322
2.87k
  _rc->frame_metrics=NULL;
323
2.87k
  _rc->rate_bias=0;
324
2.87k
  if(_enc->state.info.target_bitrate>0){
325
    /*The buffer size is set equal to the keyframe interval, clamped to the
326
       range [12,256] frames.
327
      The 12 frame minimum gives us some chance to distribute bit estimation
328
       errors.
329
      The 256 frame maximum means we'll require 8-10 seconds of pre-buffering
330
       at 24-30 fps, which is not unreasonable.*/
331
2.57k
    _rc->buf_delay=_enc->keyframe_frequency_force>256?
332
2.57k
     256:_enc->keyframe_frequency_force;
333
    /*By default, enforce all buffer constraints.*/
334
2.57k
    _rc->drop_frames=1;
335
2.57k
    _rc->cap_overflow=1;
336
2.57k
    _rc->cap_underflow=0;
337
2.57k
    oc_enc_rc_reset(_enc);
338
2.57k
  }
339
2.87k
}
340
341
2.87k
void oc_rc_state_clear(oc_rc_state *_rc){
342
2.87k
  _ogg_free(_rc->frame_metrics);
343
2.87k
}
344
345
0
void oc_enc_rc_resize(oc_enc_ctx *_enc){
346
  /*If encoding has not yet begun, reset the buffer state.*/
347
0
  if(_enc->state.curframe_num<0)oc_enc_rc_reset(_enc);
348
0
  else{
349
0
    int idt;
350
    /*Otherwise, update the bounds on the buffer, but not the current
351
       fullness.*/
352
0
    _enc->rc.bits_per_frame=(_enc->state.info.target_bitrate*
353
0
     (ogg_int64_t)_enc->state.info.fps_denominator)/
354
0
     _enc->state.info.fps_numerator;
355
    /*Insane framerates or frame sizes mean insane bitrates.
356
      Let's not get carried away.*/
357
0
    if(_enc->rc.bits_per_frame>0x400000000000LL){
358
0
      _enc->rc.bits_per_frame=(ogg_int64_t)0x400000000000LL;
359
0
    }
360
0
    else if(_enc->rc.bits_per_frame<32)_enc->rc.bits_per_frame=32;
361
0
    _enc->rc.buf_delay=OC_MAXI(_enc->rc.buf_delay,12);
362
0
    _enc->rc.max=_enc->rc.bits_per_frame*_enc->rc.buf_delay;
363
0
    _enc->rc.target=(_enc->rc.max+1>>1)+(_enc->rc.bits_per_frame+2>>2)*
364
0
     OC_MINI(_enc->keyframe_frequency_force,_enc->rc.buf_delay);
365
    /*Update the INTER-frame scale filter delay.
366
      We jump to it immediately if we've already seen enough frames; otherwise
367
       it is simply set as the new target.*/
368
0
    _enc->rc.inter_delay_target=idt=OC_MAXI(_enc->rc.buf_delay>>1,10);
369
0
    if(idt<OC_MINI(_enc->rc.inter_delay,_enc->rc.inter_count)){
370
0
      oc_iir_filter_init(&_enc->rc.scalefilter[1],idt,
371
0
       _enc->rc.scalefilter[1].y[0]);
372
0
      _enc->rc.inter_delay=idt;
373
0
    }
374
0
  }
375
  /*If we're in pass-2 mode, make sure the frame metrics array is big enough
376
     to hold frame statistics for the full buffer.*/
377
0
  if(_enc->rc.twopass==2){
378
0
    int cfm;
379
0
    int buf_delay;
380
0
    int reset_window;
381
0
    buf_delay=_enc->rc.buf_delay;
382
0
    reset_window=_enc->rc.frame_metrics==NULL&&(_enc->rc.frames_total[0]==0||
383
0
     buf_delay<_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
384
0
     +_enc->rc.frames_total[2]);
385
0
    cfm=_enc->rc.cframe_metrics;
386
    /*Only try to resize the frame metrics buffer if a) it's too small and
387
       b) we were using a finite buffer, or are about to start.*/
388
0
    if(cfm<buf_delay&&(_enc->rc.frame_metrics!=NULL||reset_window)){
389
0
      oc_frame_metrics *fm;
390
0
      int               nfm;
391
0
      int               fmh;
392
0
      fm=(oc_frame_metrics *)_ogg_realloc(_enc->rc.frame_metrics,
393
0
       buf_delay*sizeof(*_enc->rc.frame_metrics));
394
0
      if(fm==NULL){
395
        /*We failed to allocate a finite buffer.*/
396
        /*If we don't have a valid 2-pass header yet, just return; we'll reset
397
           the buffer size when we read the header.*/
398
0
        if(_enc->rc.frames_total[0]==0)return;
399
        /*Otherwise revert to the largest finite buffer previously set, or to
400
           whole-file buffering if we were still using that.*/
401
0
        _enc->rc.buf_delay=_enc->rc.frame_metrics!=NULL?
402
0
         cfm:_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
403
0
         +_enc->rc.frames_total[2];
404
0
        oc_enc_rc_resize(_enc);
405
0
        return;
406
0
      }
407
0
      _enc->rc.frame_metrics=fm;
408
0
      _enc->rc.cframe_metrics=buf_delay;
409
      /*Re-organize the circular buffer.*/
410
0
      fmh=_enc->rc.frame_metrics_head;
411
0
      nfm=_enc->rc.nframe_metrics;
412
0
      if(fmh+nfm>cfm){
413
0
        int shift;
414
0
        shift=OC_MINI(fmh+nfm-cfm,buf_delay-cfm);
415
0
        memcpy(fm+cfm,fm,OC_MINI(fmh+nfm-cfm,buf_delay-cfm)*sizeof(*fm));
416
0
        if(fmh+nfm>buf_delay)memmove(fm,fm+shift,fmh+nfm-buf_delay);
417
0
      }
418
0
    }
419
    /*We were using whole-file buffering; now we're not.*/
420
0
    if(reset_window){
421
0
      _enc->rc.nframes[0]=_enc->rc.nframes[1]=_enc->rc.nframes[2]=0;
422
0
      _enc->rc.scale_sum[0]=_enc->rc.scale_sum[1]=0;
423
0
      _enc->rc.scale_window_end=_enc->rc.scale_window0=
424
0
       _enc->state.curframe_num+_enc->prev_dup_count+1;
425
0
      if(_enc->rc.twopass_buffer_bytes){
426
0
        int qti;
427
        /*We already read the metrics for the first frame in the window.*/
428
0
        *(_enc->rc.frame_metrics)=*&_enc->rc.cur_metrics;
429
0
        _enc->rc.nframe_metrics++;
430
0
        qti=_enc->rc.cur_metrics.frame_type;
431
0
        _enc->rc.nframes[qti]++;
432
0
        _enc->rc.nframes[2]+=_enc->rc.cur_metrics.dup_count;
433
0
        _enc->rc.scale_sum[qti]+=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
434
0
        _enc->rc.scale_window_end+=_enc->rc.cur_metrics.dup_count+1;
435
0
        if(_enc->rc.scale_window_end-_enc->rc.scale_window0<buf_delay){
436
          /*We need more frame data.*/
437
0
          _enc->rc.twopass_buffer_bytes=0;
438
0
        }
439
0
      }
440
0
    }
441
    /*Otherwise, we could shrink the size of the current window, if necessary,
442
       but leaving it like it is lets us adapt to the new buffer size more
443
       gracefully.*/
444
0
  }
445
0
}
446
447
/*Scale the number of frames by the number of expected drops/duplicates.*/
448
45.6k
static int oc_rc_scale_drop(oc_rc_state *_rc,int _nframes){
449
45.6k
  if(_rc->prev_drop_count>0||_rc->log_drop_scale>OC_Q57(0)){
450
12.6k
    ogg_int64_t dup_scale;
451
12.6k
    dup_scale=oc_bexp64((_rc->log_drop_scale
452
12.6k
     +oc_blog64(_rc->prev_drop_count+1)>>1)+OC_Q57(8));
453
12.6k
    if(dup_scale<_nframes<<8){
454
12.3k
      int dup_scalei;
455
12.3k
      dup_scalei=(int)dup_scale;
456
12.3k
      if(dup_scalei>0)_nframes=((_nframes<<8)+dup_scalei-1)/dup_scalei;
457
12.3k
    }
458
283
    else _nframes=!!_nframes;
459
12.6k
  }
460
45.6k
  return _nframes;
461
45.6k
}
462
463
45.6k
int oc_enc_select_qi(oc_enc_ctx *_enc,int _qti,int _clamp){
464
45.6k
  ogg_int64_t  rate_total;
465
45.6k
  ogg_int64_t  rate_bias;
466
45.6k
  int          nframes[2];
467
45.6k
  int          buf_delay;
468
45.6k
  int          buf_pad;
469
45.6k
  ogg_int64_t  log_qtarget;
470
45.6k
  ogg_int64_t  log_scale0;
471
45.6k
  ogg_int64_t  log_cur_scale;
472
45.6k
  ogg_int64_t  log_qexp;
473
45.6k
  int          exp0;
474
45.6k
  int          old_qi;
475
45.6k
  int          qi;
476
  /*Figure out how to re-distribute bits so that we hit our fullness target
477
     before the last keyframe in our current buffer window (after the current
478
     frame), or the end of the buffer window, whichever comes first.*/
479
45.6k
  log_cur_scale=(ogg_int64_t)_enc->rc.scalefilter[_qti].y[0]<<33;
480
45.6k
  buf_pad=0;
481
45.6k
  switch(_enc->rc.twopass){
482
45.6k
    default:{
483
45.6k
      ogg_uint32_t next_key_frame;
484
      /*Single pass mode: assume only forced keyframes and attempt to estimate
485
         the drop count for VFR content.*/
486
45.6k
      next_key_frame=_qti?_enc->keyframe_frequency_force
487
24.3k
       -(_enc->state.curframe_num-_enc->state.keyframe_num):0;
488
45.6k
      nframes[0]=(_enc->rc.buf_delay-OC_MINI(next_key_frame,_enc->rc.buf_delay)
489
45.6k
       +_enc->keyframe_frequency_force-1)/_enc->keyframe_frequency_force;
490
45.6k
      if(nframes[0]+_qti>1){
491
7.23k
        nframes[0]--;
492
7.23k
        buf_delay=next_key_frame+nframes[0]*_enc->keyframe_frequency_force;
493
7.23k
      }
494
38.4k
      else buf_delay=_enc->rc.buf_delay;
495
45.6k
      nframes[1]=buf_delay-nframes[0];
496
      /*Downgrade the delta frame rate to correspond to the recent drop count
497
         history.*/
498
45.6k
      nframes[1]=oc_rc_scale_drop(&_enc->rc,nframes[1]);
499
45.6k
    }break;
500
0
    case 1:{
501
      /*Pass 1 mode: use a fixed qi value.*/
502
0
      qi=_enc->state.qis[0];
503
0
      _enc->rc.log_qtarget=_enc->log_qavg[_qti][qi];
504
0
      return qi;
505
0
    }break;
506
0
    case 2:{
507
0
      ogg_int64_t scale_sum[2];
508
0
      int         qti;
509
      /*Pass 2 mode: we know exactly how much of each frame type there is in
510
         the current buffer window, and have estimates for the scales.*/
511
0
      nframes[0]=_enc->rc.nframes[0];
512
0
      nframes[1]=_enc->rc.nframes[1];
513
0
      scale_sum[0]=_enc->rc.scale_sum[0];
514
0
      scale_sum[1]=_enc->rc.scale_sum[1];
515
      /*The window size can be slightly larger than the buffer window for VFR
516
         content; clamp it down, if appropriate (the excess will all be dup
517
         frames).*/
518
0
      buf_delay=OC_MINI(_enc->rc.scale_window_end-_enc->rc.scale_window0,
519
0
       _enc->rc.buf_delay);
520
      /*If we're approaching the end of the file, add some slack to keep us
521
         from slamming into a rail.
522
        Our rate accuracy goes down, but it keeps the result sensible.
523
        We position the target where the first forced keyframe beyond the end
524
         of the file would be (for consistency with 1-pass mode).*/
525
0
      buf_pad=OC_MINI(_enc->rc.buf_delay,_enc->state.keyframe_num
526
0
       +_enc->keyframe_frequency_force-_enc->rc.scale_window0);
527
0
      if(buf_delay<buf_pad)buf_pad-=buf_delay;
528
0
      else{
529
        /*Otherwise, search for the last keyframe in the buffer window and
530
           target that.*/
531
0
        buf_pad=0;
532
        /*TODO: Currently we only do this when using a finite buffer; we could
533
           save the position of the last keyframe in the summary data and do it
534
           with a whole-file buffer as well, but it isn't likely to make a
535
           difference.*/
536
0
        if(_enc->rc.frame_metrics!=NULL){
537
0
          int fmi;
538
0
          int fm_tail;
539
0
          fm_tail=_enc->rc.frame_metrics_head+_enc->rc.nframe_metrics;
540
0
          if(fm_tail>=_enc->rc.cframe_metrics)fm_tail-=_enc->rc.cframe_metrics;
541
0
          for(fmi=fm_tail;;){
542
0
            oc_frame_metrics *m;
543
0
            fmi--;
544
0
            if(fmi<0)fmi+=_enc->rc.cframe_metrics;
545
            /*Stop before we remove the first frame.*/
546
0
            if(fmi==_enc->rc.frame_metrics_head)break;
547
0
            m=_enc->rc.frame_metrics+fmi;
548
            /*If we find a keyframe, remove it and everything past it.*/
549
0
            if(m->frame_type==OC_INTRA_FRAME){
550
0
              do{
551
0
                qti=m->frame_type;
552
0
                nframes[qti]--;
553
0
                scale_sum[qti]-=oc_bexp_q24(m->log_scale);
554
0
                buf_delay-=m->dup_count+1;
555
0
                fmi++;
556
0
                if(fmi>=_enc->rc.cframe_metrics)fmi=0;
557
0
                m=_enc->rc.frame_metrics+fmi;
558
0
              }
559
0
              while(fmi!=fm_tail);
560
              /*And stop scanning backwards.*/
561
0
              break;
562
0
            }
563
0
          }
564
0
        }
565
0
      }
566
      /*If we're not using the same frame type as in pass 1 (because someone
567
         changed the keyframe interval), remove that scale estimate.
568
        We'll add in a replacement for the correct frame type below.*/
569
0
      qti=_enc->rc.cur_metrics.frame_type;
570
0
      if(qti!=_qti){
571
0
        nframes[qti]--;
572
0
        scale_sum[qti]-=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
573
0
      }
574
      /*Compute log_scale estimates for each frame type from the pass-1 scales
575
         we measured in the current window.*/
576
0
      for(qti=0;qti<2;qti++){
577
0
        _enc->rc.log_scale[qti]=nframes[qti]>0?
578
0
         oc_blog64(scale_sum[qti])-oc_blog64(nframes[qti])-OC_Q57(24):
579
0
         -_enc->rc.log_npixels;
580
0
      }
581
      /*If we're not using the same frame type as in pass 1, add a scale
582
         estimate for the corresponding frame using the current low-pass
583
         filter value.
584
        This is mostly to ensure we have a valid estimate even when pass 1 had
585
         no frames of this type in the buffer window.
586
        TODO: We could also plan ahead and figure out how many keyframes we'll
587
         be forced to add in the current buffer window.*/
588
0
      qti=_enc->rc.cur_metrics.frame_type;
589
0
      if(qti!=_qti){
590
0
        ogg_int64_t scale;
591
0
        scale=_enc->rc.log_scale[_qti]<OC_Q57(23)?
592
0
         oc_bexp64(_enc->rc.log_scale[_qti]+OC_Q57(24)):0x7FFFFFFFFFFFLL;
593
0
        scale*=nframes[_qti];
594
0
        nframes[_qti]++;
595
0
        scale+=oc_bexp_q24(log_cur_scale>>33);
596
0
        _enc->rc.log_scale[_qti]=oc_blog64(scale)
597
0
         -oc_blog64(nframes[qti])-OC_Q57(24);
598
0
      }
599
0
      else log_cur_scale=(ogg_int64_t)_enc->rc.cur_metrics.log_scale<<33;
600
      /*Add the padding from above.
601
        This basically reverts to 1-pass estimations in the last keyframe
602
         interval.*/
603
0
      if(buf_pad>0){
604
0
        ogg_int64_t scale;
605
0
        int         nextra_frames;
606
        /*Extend the buffer.*/
607
0
        buf_delay+=buf_pad;
608
        /*Add virtual delta frames according to the estimated drop count.*/
609
0
        nextra_frames=oc_rc_scale_drop(&_enc->rc,buf_pad);
610
        /*And blend in the low-pass filtered scale according to how many frames
611
           we added.*/
612
0
        scale=
613
0
         oc_bexp64(_enc->rc.log_scale[1]+OC_Q57(24))*(ogg_int64_t)nframes[1]
614
0
         +oc_bexp_q24(_enc->rc.scalefilter[1].y[0])*(ogg_int64_t)nextra_frames;
615
0
        nframes[1]+=nextra_frames;
616
0
        _enc->rc.log_scale[1]=oc_blog64(scale)-oc_blog64(nframes[1])-OC_Q57(24);
617
0
      }
618
0
    }break;
619
45.6k
  }
620
  /*If we've been missing our target, add a penalty term.*/
621
45.6k
  rate_bias=(_enc->rc.rate_bias/(_enc->state.curframe_num+1000))*
622
45.6k
   (buf_delay-buf_pad);
623
  /*rate_total is the total bits available over the next buf_delay frames.*/
624
45.6k
  rate_total=_enc->rc.fullness-_enc->rc.target+rate_bias
625
45.6k
   +buf_delay*_enc->rc.bits_per_frame;
626
45.6k
  log_scale0=_enc->rc.log_scale[_qti]+_enc->rc.log_npixels;
627
  /*If there aren't enough bits to achieve our desired fullness level, use the
628
     minimum quality permitted.*/
629
45.6k
  if(rate_total<=buf_delay)log_qtarget=OC_QUANT_MAX_LOG;
630
41.2k
  else{
631
41.2k
    static const ogg_int64_t LOG_KEY_RATIO=0x0137222BB70747BALL;
632
41.2k
    ogg_int64_t log_scale1;
633
41.2k
    ogg_int64_t rlo;
634
41.2k
    ogg_int64_t rhi;
635
41.2k
    log_scale1=_enc->rc.log_scale[1-_qti]+_enc->rc.log_npixels;
636
41.2k
    rlo=0;
637
41.2k
    rhi=(rate_total+nframes[_qti]-1)/nframes[_qti];
638
792k
    while(rlo<rhi){
639
752k
      ogg_int64_t curr;
640
752k
      ogg_int64_t rdiff;
641
752k
      ogg_int64_t log_rpow;
642
752k
      ogg_int64_t rscale;
643
752k
      curr=rlo+rhi>>1;
644
752k
      log_rpow=oc_blog64(curr)-log_scale0;
645
752k
      log_rpow=(log_rpow+(_enc->rc.exp[_qti]>>1))/_enc->rc.exp[_qti];
646
752k
      if(_qti)log_rpow+=LOG_KEY_RATIO>>6;
647
442k
      else log_rpow-=LOG_KEY_RATIO>>6;
648
752k
      log_rpow*=_enc->rc.exp[1-_qti];
649
752k
      rscale=nframes[1-_qti]*oc_bexp64(log_scale1+log_rpow);
650
752k
      rdiff=nframes[_qti]*curr+rscale-rate_total;
651
752k
      if(rdiff<0)rlo=curr+1;
652
207k
      else if(rdiff>0)rhi=curr-1;
653
769
      else break;
654
752k
    }
655
41.2k
    log_qtarget=OC_Q57(2)-((oc_blog64(rlo)-log_scale0+(_enc->rc.exp[_qti]>>1))/
656
41.2k
     _enc->rc.exp[_qti]<<6);
657
41.2k
    log_qtarget=OC_MINI(log_qtarget,OC_QUANT_MAX_LOG);
658
41.2k
  }
659
  /*The above allocation looks only at the total rate we'll accumulate in the
660
     next buf_delay frames.
661
    However, we could overflow the buffer on the very next frame, so check for
662
     that here, if we're not using a soft target.*/
663
45.6k
  exp0=_enc->rc.exp[_qti];
664
45.6k
  if(_enc->rc.cap_overflow){
665
45.6k
    ogg_int64_t margin;
666
45.6k
    ogg_int64_t soft_limit;
667
45.6k
    ogg_int64_t log_soft_limit;
668
    /*Allow 3% of the buffer for prediction error.
669
      This should be plenty, and we don't mind if we go a bit over; we only
670
       want to keep these bits from being completely wasted.*/
671
45.6k
    margin=_enc->rc.max+31>>5;
672
    /*We want to use at least this many bits next frame.*/
673
45.6k
    soft_limit=_enc->rc.fullness+_enc->rc.bits_per_frame-(_enc->rc.max-margin);
674
45.6k
    log_soft_limit=oc_blog64(soft_limit);
675
    /*If we're predicting we won't use that many...*/
676
45.6k
    log_qexp=(log_qtarget-OC_Q57(2)>>6)*exp0;
677
45.6k
    if(log_scale0-log_qexp<log_soft_limit){
678
      /*Scale the adjustment based on how far into the margin we are.*/
679
10.7k
      log_qexp+=(log_scale0-log_soft_limit-log_qexp>>32)*
680
10.7k
       ((OC_MINI(margin,soft_limit)<<32)/margin);
681
10.7k
      log_qtarget=((log_qexp+(exp0>>1))/exp0<<6)+OC_Q57(2);
682
10.7k
    }
683
45.6k
  }
684
  /*If this was not one of the initial frames, limit the change in quality.*/
685
45.6k
  old_qi=_enc->state.qis[0];
686
45.6k
  if(_clamp){
687
40.4k
    ogg_int64_t log_qmin;
688
40.4k
    ogg_int64_t log_qmax;
689
    /*Clamp the target quantizer to within [0.8*Q,1.2*Q], where Q is the
690
       current quantizer.
691
      TODO: With user-specified quant matrices, we need to enlarge these limits
692
       if they don't actually let us change qi values.*/
693
40.4k
    log_qmin=_enc->log_qavg[_qti][old_qi]-0x00A4D3C25E68DC58LL;
694
40.4k
    log_qmax=_enc->log_qavg[_qti][old_qi]+0x00A4D3C25E68DC58LL;
695
40.4k
    log_qtarget=OC_CLAMPI(log_qmin,log_qtarget,log_qmax);
696
40.4k
  }
697
  /*The above allocation looks only at the total rate we'll accumulate in the
698
     next buf_delay frames.
699
    However, we could bust the budget on the very next frame, so check for that
700
     here, if we're not using a soft target.*/
701
  /* Disabled when our minimum qi > 0; if we saturate log_qtarget to
702
     to the maximum possible size when we have a minimum qi, the
703
     resulting lambda will interact very strangely with SKIP.  The
704
     resulting artifacts look like waterfalls. */
705
45.6k
  if(_enc->state.info.quality==0){
706
45.6k
    ogg_int64_t log_hard_limit;
707
    /*Compute the maximum number of bits we can use in the next frame.
708
      Allow 50% of the rate for a single frame for prediction error.
709
      This may not be enough for keyframes or sudden changes in complexity.*/
710
45.6k
    log_hard_limit=oc_blog64(_enc->rc.fullness+(_enc->rc.bits_per_frame>>1));
711
    /*If we're predicting we'll use more than this...*/
712
45.6k
    log_qexp=(log_qtarget-OC_Q57(2)>>6)*exp0;
713
45.6k
    if(log_scale0-log_qexp>log_hard_limit){
714
      /*Force the target to hit our limit exactly.*/
715
11.9k
      log_qexp=log_scale0-log_hard_limit;
716
11.9k
      log_qtarget=((log_qexp+(exp0>>1))/exp0<<6)+OC_Q57(2);
717
      /*If that target is unreasonable, oh well; we'll have to drop.*/
718
11.9k
      log_qtarget=OC_MINI(log_qtarget,OC_QUANT_MAX_LOG);
719
11.9k
    }
720
45.6k
  }
721
  /*Compute a final estimate of the number of bits we plan to use.*/
722
45.6k
  log_qexp=(log_qtarget-OC_Q57(2)>>6)*_enc->rc.exp[_qti];
723
45.6k
  _enc->rc.rate_bias+=oc_bexp64(log_cur_scale+_enc->rc.log_npixels-log_qexp);
724
45.6k
  qi=oc_enc_find_qi_for_target(_enc,_qti,old_qi,
725
45.6k
   _enc->state.info.quality,log_qtarget);
726
  /*Save the quantizer target for lambda calculations.*/
727
45.6k
  _enc->rc.log_qtarget=log_qtarget;
728
45.6k
  return qi;
729
45.6k
}
730
731
int oc_enc_update_rc_state(oc_enc_ctx *_enc,
732
31.5k
 long _bits,int _qti,int _qi,int _trial,int _droppable){
733
31.5k
  ogg_int64_t buf_delta;
734
31.5k
  ogg_int64_t log_scale;
735
31.5k
  int         dropped;
736
31.5k
  dropped=0;
737
  /* Drop frames also disabled for now in the case of infinite-buffer
738
     two-pass mode */
739
31.5k
  if(!_enc->rc.drop_frames||_enc->rc.twopass&&_enc->rc.frame_metrics==NULL){
740
0
    _droppable=0;
741
0
  }
742
31.5k
  buf_delta=_enc->rc.bits_per_frame*(1+_enc->dup_count);
743
31.5k
  if(_bits<=0){
744
    /*We didn't code any blocks in this frame.*/
745
1.97k
    log_scale=OC_Q57(-64);
746
1.97k
    _bits=0;
747
1.97k
  }
748
29.5k
  else{
749
29.5k
    ogg_int64_t log_bits;
750
29.5k
    ogg_int64_t log_qexp;
751
    /*Compute the estimated scale factor for this frame type.*/
752
29.5k
    log_bits=oc_blog64(_bits);
753
29.5k
    log_qexp=_enc->rc.log_qtarget-OC_Q57(2);
754
29.5k
    log_qexp=(log_qexp>>6)*(_enc->rc.exp[_qti]);
755
29.5k
    log_scale=OC_MINI(log_bits-_enc->rc.log_npixels+log_qexp,OC_Q57(16));
756
29.5k
  }
757
  /*Special two-pass processing.*/
758
31.5k
  switch(_enc->rc.twopass){
759
0
    case 1:{
760
      /*Pass 1 mode: save the metrics for this frame.*/
761
0
      _enc->rc.cur_metrics.log_scale=oc_q57_to_q24(log_scale);
762
0
      _enc->rc.cur_metrics.dup_count=_enc->dup_count;
763
0
      _enc->rc.cur_metrics.frame_type=_enc->state.frame_type;
764
0
      _enc->rc.cur_metrics.activity_avg=_enc->activity_avg;
765
0
      _enc->rc.twopass_buffer_bytes=0;
766
0
    }break;
767
0
    case 2:{
768
      /*Pass 2 mode:*/
769
0
      if(!_trial){
770
0
        ogg_int64_t next_frame_num;
771
0
        int         qti;
772
        /*Move the current metrics back one frame.*/
773
0
        *&_enc->rc.prev_metrics=*&_enc->rc.cur_metrics;
774
0
        next_frame_num=_enc->state.curframe_num+_enc->dup_count+1;
775
        /*Back out the last frame's statistics from the sliding window.*/
776
0
        qti=_enc->rc.prev_metrics.frame_type;
777
0
        _enc->rc.frames_left[qti]--;
778
0
        _enc->rc.frames_left[2]-=_enc->rc.prev_metrics.dup_count;
779
0
        _enc->rc.nframes[qti]--;
780
0
        _enc->rc.nframes[2]-=_enc->rc.prev_metrics.dup_count;
781
0
        _enc->rc.scale_sum[qti]-=oc_bexp_q24(_enc->rc.prev_metrics.log_scale);
782
0
        _enc->rc.scale_window0=(int)next_frame_num;
783
        /*Free the corresponding entry in the circular buffer.*/
784
0
        if(_enc->rc.frame_metrics!=NULL){
785
0
          _enc->rc.nframe_metrics--;
786
0
          _enc->rc.frame_metrics_head++;
787
0
          if(_enc->rc.frame_metrics_head>=_enc->rc.cframe_metrics){
788
0
            _enc->rc.frame_metrics_head=0;
789
0
          }
790
0
        }
791
        /*Mark us ready for the next 2-pass packet.*/
792
0
        _enc->rc.twopass_buffer_bytes=0;
793
        /*Update state, so the user doesn't have to keep calling 2pass_in after
794
           they've fed in all the data when we're using a finite buffer.*/
795
0
        _enc->prev_dup_count=_enc->dup_count;
796
0
        oc_enc_rc_2pass_in(_enc,NULL,0);
797
0
      }
798
0
    }break;
799
31.5k
  }
800
  /*Common to all passes:*/
801
31.5k
  if(_bits>0){
802
29.5k
    if(_trial){
803
3.57k
      oc_iir_filter *f;
804
      /*Use the estimated scale factor directly if this was a trial.*/
805
3.57k
      f=_enc->rc.scalefilter+_qti;
806
3.57k
      f->y[1]=f->y[0]=f->x[1]=f->x[0]=oc_q57_to_q24(log_scale);
807
3.57k
      _enc->rc.log_scale[_qti]=log_scale;
808
3.57k
    }
809
25.9k
    else{
810
      /*Lengthen the time constant for the INTER filter as we collect more
811
         frame statistics, until we reach our target.*/
812
25.9k
      if(_enc->rc.inter_delay<_enc->rc.inter_delay_target&&
813
22.7k
       _enc->rc.inter_count>=_enc->rc.inter_delay&&_qti==OC_INTER_FRAME){
814
1.84k
        oc_iir_filter_reinit(&_enc->rc.scalefilter[1],++_enc->rc.inter_delay);
815
1.84k
      }
816
      /*Otherwise update the low-pass scale filter for this frame type,
817
         regardless of whether or not we dropped this frame.*/
818
25.9k
      _enc->rc.log_scale[_qti]=oc_iir_filter_update(
819
25.9k
       _enc->rc.scalefilter+_qti,oc_q57_to_q24(log_scale))<<33;
820
      /*If this frame busts our budget, it must be dropped.*/
821
25.9k
      if(_droppable&&_enc->rc.fullness+buf_delta<_bits){
822
4.44k
        _enc->rc.prev_drop_count+=1+_enc->dup_count;
823
4.44k
        _bits=0;
824
4.44k
        dropped=1;
825
4.44k
      }
826
21.5k
      else{
827
21.5k
        ogg_uint32_t drop_count;
828
        /*Update a low-pass filter to estimate the "real" frame rate taking
829
           drops and duplicates into account.
830
          This is only done if the frame is coded, as it needs the final
831
           count of dropped frames.*/
832
21.5k
        drop_count=_enc->rc.prev_drop_count+1;
833
21.5k
        if(drop_count>0x7F)drop_count=0x7FFFFFFF;
834
21.5k
        else drop_count<<=24;
835
21.5k
        _enc->rc.log_drop_scale=oc_blog64(oc_iir_filter_update(
836
21.5k
         &_enc->rc.vfrfilter,drop_count))-OC_Q57(24);
837
        /*Initialize the drop count for this frame to the user-requested dup
838
           count.
839
          It will be increased if we drop more frames.*/
840
21.5k
        _enc->rc.prev_drop_count=_enc->dup_count;
841
21.5k
      }
842
25.9k
    }
843
    /*Increment the INTER frame count, for filter adaptation purposes.*/
844
29.5k
    if(_enc->rc.inter_count<INT_MAX)_enc->rc.inter_count+=_qti;
845
29.5k
  }
846
  /*Increase the drop count.*/
847
1.97k
  else _enc->rc.prev_drop_count+=1+_enc->dup_count;
848
  /*And update the buffer fullness level.*/
849
31.5k
  if(!_trial){
850
27.8k
    _enc->rc.fullness+=buf_delta-_bits;
851
    /*If we're too quick filling the buffer and overflow is capped,
852
      that rate is lost forever.*/
853
27.8k
    if(_enc->rc.cap_overflow&&_enc->rc.fullness>_enc->rc.max){
854
6.72k
      _enc->rc.fullness=_enc->rc.max;
855
6.72k
    }
856
    /*If we're too quick draining the buffer and underflow is capped,
857
      don't try to make up that rate later.*/
858
27.8k
    if(_enc->rc.cap_underflow&&_enc->rc.fullness<0){
859
0
      _enc->rc.fullness=0;
860
0
    }
861
    /*Adjust the bias for the real bits we've used.*/
862
27.8k
    _enc->rc.rate_bias-=_bits;
863
27.8k
  }
864
31.5k
  return dropped;
865
31.5k
}
866
867
0
#define OC_RC_2PASS_VERSION   (2)
868
0
#define OC_RC_2PASS_HDR_SZ    (38)
869
0
#define OC_RC_2PASS_PACKET_SZ (12)
870
871
0
static void oc_rc_buffer_val(oc_rc_state *_rc,ogg_int64_t _val,int _bytes){
872
0
  while(_bytes-->0){
873
0
    _rc->twopass_buffer[_rc->twopass_buffer_bytes++]=(unsigned char)(_val&0xFF);
874
0
    _val>>=8;
875
0
  }
876
0
}
877
878
0
int oc_enc_rc_2pass_out(oc_enc_ctx *_enc,unsigned char **_buf){
879
0
  if(_enc->rc.twopass_buffer_bytes==0){
880
0
    if(_enc->rc.twopass==0){
881
0
      int qi;
882
      /*Pick first-pass qi for scale calculations.*/
883
0
      qi=oc_enc_select_qi(_enc,0,0);
884
0
      _enc->state.nqis=1;
885
0
      _enc->state.qis[0]=qi;
886
0
      _enc->rc.twopass=1;
887
0
      _enc->rc.frames_total[0]=_enc->rc.frames_total[1]=
888
0
       _enc->rc.frames_total[2]=0;
889
0
      _enc->rc.scale_sum[0]=_enc->rc.scale_sum[1]=0;
890
      /*Fill in dummy summary values.*/
891
0
      oc_rc_buffer_val(&_enc->rc,0x5032544F,4);
892
0
      oc_rc_buffer_val(&_enc->rc,OC_RC_2PASS_VERSION,4);
893
0
      oc_rc_buffer_val(&_enc->rc,0,OC_RC_2PASS_HDR_SZ-8);
894
0
    }
895
0
    else{
896
0
      int qti;
897
0
      qti=_enc->rc.cur_metrics.frame_type;
898
0
      _enc->rc.scale_sum[qti]+=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
899
0
      _enc->rc.frames_total[qti]++;
900
0
      _enc->rc.frames_total[2]+=_enc->rc.cur_metrics.dup_count;
901
0
      oc_rc_buffer_val(&_enc->rc,
902
0
       _enc->rc.cur_metrics.dup_count|_enc->rc.cur_metrics.frame_type<<31,4);
903
0
      oc_rc_buffer_val(&_enc->rc,_enc->rc.cur_metrics.log_scale,4);
904
0
      oc_rc_buffer_val(&_enc->rc,_enc->rc.cur_metrics.activity_avg,4);
905
0
    }
906
0
  }
907
0
  else if(_enc->packet_state==OC_PACKET_DONE&&
908
0
   _enc->rc.twopass_buffer_bytes!=OC_RC_2PASS_HDR_SZ){
909
0
    _enc->rc.twopass_buffer_bytes=0;
910
0
    oc_rc_buffer_val(&_enc->rc,0x5032544F,4);
911
0
    oc_rc_buffer_val(&_enc->rc,OC_RC_2PASS_VERSION,4);
912
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[0],4);
913
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[1],4);
914
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[2],4);
915
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.exp[0],1);
916
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.exp[1],1);
917
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.scale_sum[0],8);
918
0
    oc_rc_buffer_val(&_enc->rc,_enc->rc.scale_sum[1],8);
919
0
  }
920
0
  else{
921
    /*The data for this frame has already been retrieved.*/
922
0
    *_buf=NULL;
923
0
    return 0;
924
0
  }
925
0
  *_buf=_enc->rc.twopass_buffer;
926
0
  return _enc->rc.twopass_buffer_bytes;
927
0
}
928
929
static size_t oc_rc_buffer_fill(oc_rc_state *_rc,
930
0
 unsigned char *_buf,size_t _bytes,size_t _consumed,size_t _goal){
931
0
  while(_rc->twopass_buffer_fill<_goal&&_consumed<_bytes){
932
0
    _rc->twopass_buffer[_rc->twopass_buffer_fill++]=_buf[_consumed++];
933
0
  }
934
0
  return _consumed;
935
0
}
936
937
0
static ogg_int64_t oc_rc_unbuffer_val(oc_rc_state *_rc,int _bytes){
938
0
  ogg_int64_t ret;
939
0
  int         shift;
940
0
  ret=0;
941
0
  shift=0;
942
0
  while(_bytes-->0){
943
0
    ret|=((ogg_int64_t)_rc->twopass_buffer[_rc->twopass_buffer_bytes++])<<shift;
944
0
    shift+=8;
945
0
  }
946
0
  return ret;
947
0
}
948
949
0
int oc_enc_rc_2pass_in(oc_enc_ctx *_enc,unsigned char *_buf,size_t _bytes){
950
0
  size_t consumed;
951
0
  consumed=0;
952
  /*Enable pass 2 mode if this is the first call.*/
953
0
  if(_enc->rc.twopass==0){
954
0
    _enc->rc.twopass=2;
955
0
    _enc->rc.twopass_buffer_fill=0;
956
0
    _enc->rc.frames_total[0]=0;
957
0
    _enc->rc.nframe_metrics=0;
958
0
    _enc->rc.cframe_metrics=0;
959
0
    _enc->rc.frame_metrics_head=0;
960
0
    _enc->rc.scale_window0=0;
961
0
    _enc->rc.scale_window_end=0;
962
0
  }
963
  /*If we haven't got a valid summary header yet, try to parse one.*/
964
0
  if(_enc->rc.frames_total[0]==0){
965
0
    if(!_buf){
966
0
      int frames_needed;
967
      /*If we're using a whole-file buffer, we just need the first frame.
968
        Otherwise, we may need as many as one per buffer slot.*/
969
0
      frames_needed=_enc->rc.frame_metrics==NULL?1:_enc->rc.buf_delay;
970
0
      return OC_RC_2PASS_HDR_SZ+frames_needed*OC_RC_2PASS_PACKET_SZ
971
0
       -_enc->rc.twopass_buffer_fill;
972
0
    }
973
0
    consumed=oc_rc_buffer_fill(&_enc->rc,
974
0
     _buf,_bytes,consumed,OC_RC_2PASS_HDR_SZ);
975
0
    if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_HDR_SZ){
976
0
      ogg_int64_t scale_sum[2];
977
0
      int         exp[2];
978
0
      int         buf_delay;
979
      /*Read the summary header data.*/
980
      /*Check the magic value and version number.*/
981
0
      if(oc_rc_unbuffer_val(&_enc->rc,4)!=0x5032544F||
982
0
       oc_rc_unbuffer_val(&_enc->rc,4)!=OC_RC_2PASS_VERSION){
983
0
        _enc->rc.twopass_buffer_bytes=0;
984
0
        return TH_ENOTFORMAT;
985
0
      }
986
0
      _enc->rc.frames_total[0]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
987
0
      _enc->rc.frames_total[1]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
988
0
      _enc->rc.frames_total[2]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
989
0
      exp[0]=(int)oc_rc_unbuffer_val(&_enc->rc,1);
990
0
      exp[1]=(int)oc_rc_unbuffer_val(&_enc->rc,1);
991
0
      scale_sum[0]=oc_rc_unbuffer_val(&_enc->rc,8);
992
0
      scale_sum[1]=oc_rc_unbuffer_val(&_enc->rc,8);
993
      /*Make sure the file claims to have at least one frame.
994
        Otherwise we probably got the placeholder data from an aborted pass 1.
995
        Also make sure the total frame count doesn't overflow an integer.*/
996
0
      buf_delay=_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
997
0
       +_enc->rc.frames_total[2];
998
0
      if(_enc->rc.frames_total[0]==0||buf_delay<0||
999
0
       (ogg_uint32_t)buf_delay<_enc->rc.frames_total[0]||
1000
0
       (ogg_uint32_t)buf_delay<_enc->rc.frames_total[1]){
1001
0
        _enc->rc.frames_total[0]=0;
1002
0
        _enc->rc.twopass_buffer_bytes=0;
1003
0
        return TH_EBADHEADER;
1004
0
      }
1005
      /*Got a valid header; set up pass 2.*/
1006
0
      _enc->rc.frames_left[0]=_enc->rc.frames_total[0];
1007
0
      _enc->rc.frames_left[1]=_enc->rc.frames_total[1];
1008
0
      _enc->rc.frames_left[2]=_enc->rc.frames_total[2];
1009
      /*If the user hasn't specified a buffer size, use the whole file.*/
1010
0
      if(_enc->rc.frame_metrics==NULL){
1011
0
        _enc->rc.buf_delay=buf_delay;
1012
0
        _enc->rc.nframes[0]=_enc->rc.frames_total[0];
1013
0
        _enc->rc.nframes[1]=_enc->rc.frames_total[1];
1014
0
        _enc->rc.nframes[2]=_enc->rc.frames_total[2];
1015
0
        _enc->rc.scale_sum[0]=scale_sum[0];
1016
0
        _enc->rc.scale_sum[1]=scale_sum[1];
1017
0
        _enc->rc.scale_window_end=buf_delay;
1018
0
        oc_enc_rc_reset(_enc);
1019
0
      }
1020
0
      _enc->rc.exp[0]=exp[0];
1021
0
      _enc->rc.exp[1]=exp[1];
1022
      /*Clear the header data from the buffer to make room for packet data.*/
1023
0
      _enc->rc.twopass_buffer_fill=0;
1024
0
      _enc->rc.twopass_buffer_bytes=0;
1025
0
    }
1026
0
  }
1027
0
  if(_enc->rc.frames_total[0]!=0){
1028
0
    ogg_int64_t curframe_num;
1029
0
    int         nframes_total;
1030
0
    curframe_num=_enc->state.curframe_num;
1031
0
    if(curframe_num>=0){
1032
      /*We just encoded a frame; make sure things matched.*/
1033
0
      if(_enc->rc.prev_metrics.dup_count!=_enc->prev_dup_count){
1034
0
        _enc->rc.twopass_buffer_bytes=0;
1035
0
        return TH_EINVAL;
1036
0
      }
1037
0
    }
1038
0
    curframe_num+=_enc->prev_dup_count+1;
1039
0
    nframes_total=_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
1040
0
     +_enc->rc.frames_total[2];
1041
0
    if(curframe_num>=nframes_total){
1042
      /*We don't want any more data after the last frame, and we don't want to
1043
         allow any more frames to be encoded.*/
1044
0
      _enc->rc.twopass_buffer_bytes=0;
1045
0
    }
1046
0
    else if(_enc->rc.twopass_buffer_bytes==0){
1047
0
      if(_enc->rc.frame_metrics==NULL){
1048
        /*We're using a whole-file buffer:*/
1049
0
        if(!_buf)return OC_RC_2PASS_PACKET_SZ-_enc->rc.twopass_buffer_fill;
1050
0
        consumed=oc_rc_buffer_fill(&_enc->rc,
1051
0
         _buf,_bytes,consumed,OC_RC_2PASS_PACKET_SZ);
1052
0
        if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_PACKET_SZ){
1053
0
          ogg_uint32_t dup_count;
1054
0
          ogg_int32_t  log_scale;
1055
0
          unsigned     activity;
1056
0
          int          qti;
1057
0
          int          arg;
1058
          /*Read the metrics for the next frame.*/
1059
0
          dup_count=oc_rc_unbuffer_val(&_enc->rc,4);
1060
0
          log_scale=oc_rc_unbuffer_val(&_enc->rc,4);
1061
0
          activity=oc_rc_unbuffer_val(&_enc->rc,4);
1062
0
          _enc->rc.cur_metrics.log_scale=log_scale;
1063
0
          qti=(dup_count&0x80000000)>>31;
1064
0
          _enc->rc.cur_metrics.dup_count=dup_count&0x7FFFFFFF;
1065
0
          _enc->rc.cur_metrics.frame_type=qti;
1066
0
          _enc->rc.twopass_force_kf=qti==OC_INTRA_FRAME;
1067
0
          _enc->activity_avg=_enc->rc.cur_metrics.activity_avg=activity;
1068
          /*"Helpfully" set the dup count back to what it was in pass 1.*/
1069
0
          arg=_enc->rc.cur_metrics.dup_count;
1070
0
          th_encode_ctl(_enc,TH_ENCCTL_SET_DUP_COUNT,&arg,sizeof(arg));
1071
          /*Clear the buffer for the next frame.*/
1072
0
          _enc->rc.twopass_buffer_fill=0;
1073
0
        }
1074
0
      }
1075
0
      else{
1076
0
        int frames_needed;
1077
        /*We're using a finite buffer:*/
1078
0
        frames_needed=OC_MINI(_enc->rc.buf_delay-OC_MINI(_enc->rc.buf_delay,
1079
0
         _enc->rc.scale_window_end-_enc->rc.scale_window0),
1080
0
         _enc->rc.frames_left[0]+_enc->rc.frames_left[1]
1081
0
         -_enc->rc.nframes[0]-_enc->rc.nframes[1]);
1082
0
        while(frames_needed>0){
1083
0
          if(!_buf){
1084
0
            return OC_RC_2PASS_PACKET_SZ*frames_needed
1085
0
           -_enc->rc.twopass_buffer_fill;
1086
0
          }
1087
0
          consumed=oc_rc_buffer_fill(&_enc->rc,
1088
0
           _buf,_bytes,consumed,OC_RC_2PASS_PACKET_SZ);
1089
0
          if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_PACKET_SZ){
1090
0
            oc_frame_metrics *m;
1091
0
            int               fmi;
1092
0
            ogg_uint32_t      dup_count;
1093
0
            ogg_int32_t       log_scale;
1094
0
            int               qti;
1095
0
            unsigned          activity;
1096
            /*Read the metrics for the next frame.*/
1097
0
            dup_count=oc_rc_unbuffer_val(&_enc->rc,4);
1098
0
            log_scale=oc_rc_unbuffer_val(&_enc->rc,4);
1099
0
            activity=oc_rc_unbuffer_val(&_enc->rc,4);
1100
            /*Add the to the circular buffer.*/
1101
0
            fmi=_enc->rc.frame_metrics_head+_enc->rc.nframe_metrics++;
1102
0
            if(fmi>=_enc->rc.cframe_metrics)fmi-=_enc->rc.cframe_metrics;
1103
0
            m=_enc->rc.frame_metrics+fmi;
1104
0
            m->log_scale=log_scale;
1105
0
            qti=(dup_count&0x80000000)>>31;
1106
0
            m->dup_count=dup_count&0x7FFFFFFF;
1107
0
            m->frame_type=qti;
1108
0
            m->activity_avg=activity;
1109
            /*And accumulate the statistics over the window.*/
1110
0
            _enc->rc.nframes[qti]++;
1111
0
            _enc->rc.nframes[2]+=m->dup_count;
1112
0
            _enc->rc.scale_sum[qti]+=oc_bexp_q24(m->log_scale);
1113
0
            _enc->rc.scale_window_end+=m->dup_count+1;
1114
            /*Compute an upper bound on the number of remaining packets needed
1115
               for the current window.*/
1116
0
            frames_needed=OC_MINI(_enc->rc.buf_delay-OC_MINI(_enc->rc.buf_delay,
1117
0
             _enc->rc.scale_window_end-_enc->rc.scale_window0),
1118
0
             _enc->rc.frames_left[0]+_enc->rc.frames_left[1]
1119
0
             -_enc->rc.nframes[0]-_enc->rc.nframes[1]);
1120
            /*Clear the buffer for the next frame.*/
1121
0
            _enc->rc.twopass_buffer_fill=0;
1122
0
            _enc->rc.twopass_buffer_bytes=0;
1123
0
          }
1124
          /*Go back for more data.*/
1125
0
          else break;
1126
0
        }
1127
        /*If we've got all the frames we need, fill in the current metrics.
1128
          We're ready to go.*/
1129
0
        if(frames_needed<=0){
1130
0
          int arg;
1131
0
          *&_enc->rc.cur_metrics=
1132
0
           *(_enc->rc.frame_metrics+_enc->rc.frame_metrics_head);
1133
0
          _enc->rc.twopass_force_kf=
1134
0
           _enc->rc.cur_metrics.frame_type==OC_INTRA_FRAME;
1135
0
          _enc->activity_avg=_enc->rc.cur_metrics.activity_avg;
1136
          /*"Helpfully" set the dup count back to what it was in pass 1.*/
1137
0
          arg=_enc->rc.cur_metrics.dup_count;
1138
0
          th_encode_ctl(_enc,TH_ENCCTL_SET_DUP_COUNT,&arg,sizeof(arg));
1139
          /*Mark us ready for the next frame.*/
1140
0
          _enc->rc.twopass_buffer_bytes=1;
1141
0
        }
1142
0
      }
1143
0
    }
1144
0
  }
1145
0
  return (int)consumed;
1146
0
}