1 /*
2  * Copyright (c) 2001-2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #if !defined(_ratectrl_xiph_H)
13 #define _ratectrl_xiph_H (1)
14 
15 #include "av1/encoder/ratectrl.h"
16 #include "aom/internal/aom_codec_internal.h"
17 
18 /*Frame types.*/
19 #define OD_I_FRAME (0)
20 #define OD_P_FRAME (1)
21 #define OD_GOLDEN_P_FRAME (2)
22 #define OD_ALTREF_P_FRAME (3)
23 
24 #define OD_FRAME_NSUBTYPES (OD_ALTREF_P_FRAME + 1)
25 
26 /* Periodic boost (in between golden frames) strength - lower is more */
27 #define OD_PERIODIC_BOOST_DIV (10)
28 
29 /* Constants for frame QP modulation <- tweak these
30  * Adjusts how the rate control system decides the quantizers per frame
31  * (sub)type */
32 #define OD_MQP_I (0.98)
33 #define OD_MQP_P (1.06)
34 #define OD_MQP_GP (0.99)
35 #define OD_MQP_AP (0.92)
36 #define OD_DQP_I (-2)
37 #define OD_DQP_P (0)
38 #define OD_DQP_GP (-2)
39 #define OD_DQP_AP (-2)
40 
41 /*Fractional_coded_quantizer ~=
42    log2(quantizer / (1 << OD_COEFF_SHIFT))*6.307 + 6.235*/
43 /*Base/scale factor for linear quantizer to fractional coded quantizer
44    conversion (6.307 * 2^12) */
45 #define OD_LOG_QUANTIZER_BASE_Q12 (0x0064EB)
46 /*Inverse of above scale factor.*/
47 #define OD_LOG_QUANTIZER_EXP_Q12 (0x000289)
48 /*Offset for linear quantizer to fractional coded quantizer
49    conversion (6.235 * 2^45) */
50 #define OD_LOG_QUANTIZER_OFFSET_Q45 (0x0000C7851EB851ECLL)
51 
52 #define OD_RC_2PASS_MAGIC (0x53015641) /* [A, V, 1, S] in little endian */
53 #define OD_RC_2PASS_SUMMARY_SZ (4 + 1 + (4 + 4 + 8) * OD_FRAME_NSUBTYPES)
54 #define OD_RC_2PASS_PACKET_SZ (1 + 4)
55 #define OD_RC_2PASS_MIN (OD_RC_2PASS_PACKET_SZ + OD_RC_2PASS_SUMMARY_SZ)
56 #define OD_RC_2PASS_VERSION (1)
57 
58 /*A 2nd order low-pass Bessel follower.
59   We use this for rate control because it has fast reaction time, but is
60    critically damped.*/
61 typedef struct od_iir_bessel2 {
62   int32_t c[2];
63   int64_t g;
64   int32_t x[2];
65   int32_t y[2];
66 } od_iir_bessel2;
67 
68 /* The 2-pass metrics associated with a single frame. */
69 typedef struct od_frame_metrics {
70   /*The log base 2 of the scale factor for this frame in Q24 format.*/
71   int64_t log_scale;
72   /*The frame type from pass 1.*/
73   unsigned frame_type : 1;
74 } od_frame_metrics;
75 
76 /*Rate control setup and working state information.*/
77 typedef struct od_rc_state {
78   /* Image format */
79   int frame_width;
80   int frame_height;
81   int bit_depth;
82 
83   /* Framerate */
84   double framerate;
85   /* Keyframe rate */
86   int keyframe_rate;
87   /* Golden frame period */
88   int goldenframe_rate;
89   /* Altref frame period */
90   int altref_rate;
91   /*The target bit-rate in bits per second.*/
92   int64_t target_bitrate;
93   /* Quality level for non-bitrate-targeting */
94   int quality;
95   /* Copied from oxcf->frame_periodic_boost */
96   int periodic_boosts;
97   /* Max Q */
98   int maxq;
99   /* Min Q */
100   int minq;
101   /* Quantizer to use for the first pass */
102   int firstpass_quant;
103 
104   /* 2-pass metrics */
105   od_frame_metrics cur_metrics;
106 
107   /* 2-pass state */
108   int64_t scale_sum[OD_FRAME_NSUBTYPES];
109   int nframes[OD_FRAME_NSUBTYPES];
110 
111   /* 2-pass bytestream reader/writer context */
112   uint8_t *twopass_buffer;
113   int twopass_buffer_bytes;
114 
115   /* Pass 1 stats packet storage */
116   uint8_t firstpass_buffer[OD_RC_2PASS_SUMMARY_SZ];
117 
118   /* Every state packet from the first pass in a single buffer */
119   uint8_t *twopass_allframes_buf;
120   size_t twopass_allframes_buf_size;
121 
122   /* Actual returned quantizer */
123   int target_quantizer;
124   /*The full-precision, unmodulated quantizer upon which
125     our modulated quantizers are based.*/
126   int base_quantizer;
127 
128   /* Increments by 1 for each frame. */
129   int64_t cur_frame;
130 
131   /* End of input flag */
132   int end_of_input;
133   /* Closed GOP flag */
134   int closed_gop;
135   /*The number of frames over which to distribute the reservoir usage.*/
136   int reservoir_frame_delay;
137   /*Will we drop frames to meet bitrate target?*/
138   unsigned char drop_frames;
139   /*Do we respect the maximum reservoir fullness?*/
140   unsigned char cap_overflow;
141   /*Can the reservoir go negative?*/
142   unsigned char cap_underflow;
143   /*Two-pass mode state.
144     0 => 1-pass encoding.
145     1 => 1st pass of 2-pass encoding.
146     2 => 2nd pass of 2-pass encoding.*/
147   int twopass_state;
148   /*The log of the number of pixels in a frame in Q57 format.*/
149   int64_t log_npixels;
150   /*The target average bits per frame.*/
151   int64_t bits_per_frame;
152   /*The current bit reservoir fullness (bits available to be used).*/
153   int64_t reservoir_fullness;
154   /*The target buffer fullness.
155     This is where we'd like to be by the last keyframe the appears in the next
156      buf_delay frames.*/
157   int64_t reservoir_target;
158   /*The maximum buffer fullness (total size of the buffer).*/
159   int64_t reservoir_max;
160   /*The log of estimated scale factor for the rate model in Q57 format.*/
161   int64_t log_scale[OD_FRAME_NSUBTYPES];
162   /*The exponent used in the rate model in Q8 format.*/
163   unsigned exp[OD_FRAME_NSUBTYPES];
164   /*The log of an estimated scale factor used to obtain the real framerate, for
165      VFR sources or, e.g., 12 fps content doubled to 24 fps, etc.*/
166   int64_t log_drop_scale[OD_FRAME_NSUBTYPES];
167   /*The total drop count from the previous frame.*/
168   uint32_t prev_drop_count[OD_FRAME_NSUBTYPES];
169   /*Second-order lowpass filters to track scale and VFR/drops.*/
170   od_iir_bessel2 scalefilter[OD_FRAME_NSUBTYPES];
171   od_iir_bessel2 vfrfilter[OD_FRAME_NSUBTYPES];
172   int frame_count[OD_FRAME_NSUBTYPES];
173   int inter_p_delay;
174   int inter_delay_target;
175   /*The total accumulated estimation bias.*/
176   int64_t rate_bias;
177 } od_rc_state;
178 
179 int od_enc_rc_init(od_rc_state *rc, int64_t bitrate, int delay_ms);
180 
181 int od_enc_rc_select_quantizers_and_lambdas(od_rc_state *rc,
182                                             int is_golden_frame,
183                                             int is_altref_frame, int frame_type,
184                                             int *bottom_idx, int *top_idx);
185 
186 /* Returns 1 if the frame should be dropped */
187 int od_enc_rc_update_state(od_rc_state *rc, int64_t bits, int is_golden_frame,
188                            int is_altref_frame, int frame_type, int droppable);
189 
190 int od_frame_type(od_rc_state *rc, int64_t coding_frame_count, int *is_golden,
191                   int *is_altref, int64_t *ip_count);
192 
193 int od_enc_rc_resize(od_rc_state *rc);
194 
195 int od_enc_rc_2pass_out(od_rc_state *rc, struct aom_codec_pkt_list *pkt_list,
196                         int summary);
197 
198 int od_enc_rc_2pass_in(od_rc_state *rc);
199 
200 #endif
201