1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
19*77c1e3ccSAndroid Build Coastguard Worker
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_once.h"
24*77c1e3ccSAndroid Build Coastguard Worker
25*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/alloccommon.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_cyclicrefresh.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
28*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymode.h"
29*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/quant_common.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/seg_common.h"
31*77c1e3ccSAndroid Build Coastguard Worker
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder_utils.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encode_strategy.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/gop_structure.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/mcomp.h"
37*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/random.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ratectrl.h"
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
41*77c1e3ccSAndroid Build Coastguard Worker
42*77c1e3ccSAndroid Build Coastguard Worker #define USE_UNRESTRICTED_Q_IN_CQ_MODE 0
43*77c1e3ccSAndroid Build Coastguard Worker
44*77c1e3ccSAndroid Build Coastguard Worker // Max rate target for 1080P and below encodes under normal circumstances
45*77c1e3ccSAndroid Build Coastguard Worker // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
46*77c1e3ccSAndroid Build Coastguard Worker #define MAX_MB_RATE 250
47*77c1e3ccSAndroid Build Coastguard Worker #define MAXRATE_1080P 2025000
48*77c1e3ccSAndroid Build Coastguard Worker
49*77c1e3ccSAndroid Build Coastguard Worker #define MIN_BPB_FACTOR 0.005
50*77c1e3ccSAndroid Build Coastguard Worker #define MAX_BPB_FACTOR 50
51*77c1e3ccSAndroid Build Coastguard Worker
52*77c1e3ccSAndroid Build Coastguard Worker #define SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO 0
53*77c1e3ccSAndroid Build Coastguard Worker #define SUPERRES_QADJ_PER_DENOM_KEYFRAME 2
54*77c1e3ccSAndroid Build Coastguard Worker #define SUPERRES_QADJ_PER_DENOM_ARFFRAME 0
55*77c1e3ccSAndroid Build Coastguard Worker
56*77c1e3ccSAndroid Build Coastguard Worker #define FRAME_OVERHEAD_BITS 200
57*77c1e3ccSAndroid Build Coastguard Worker #define ASSIGN_MINQ_TABLE(bit_depth, name) \
58*77c1e3ccSAndroid Build Coastguard Worker do { \
59*77c1e3ccSAndroid Build Coastguard Worker switch (bit_depth) { \
60*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_8: name = name##_8; break; \
61*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_10: name = name##_10; break; \
62*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_12: name = name##_12; break; \
63*77c1e3ccSAndroid Build Coastguard Worker default: \
64*77c1e3ccSAndroid Build Coastguard Worker assert(0 && \
65*77c1e3ccSAndroid Build Coastguard Worker "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
66*77c1e3ccSAndroid Build Coastguard Worker " or AOM_BITS_12"); \
67*77c1e3ccSAndroid Build Coastguard Worker name = NULL; \
68*77c1e3ccSAndroid Build Coastguard Worker } \
69*77c1e3ccSAndroid Build Coastguard Worker } while (0)
70*77c1e3ccSAndroid Build Coastguard Worker
71*77c1e3ccSAndroid Build Coastguard Worker // Tables relating active max Q to active min Q
72*77c1e3ccSAndroid Build Coastguard Worker static int kf_low_motion_minq_8[QINDEX_RANGE];
73*77c1e3ccSAndroid Build Coastguard Worker static int kf_high_motion_minq_8[QINDEX_RANGE];
74*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_low_motion_minq_8[QINDEX_RANGE];
75*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_high_motion_minq_8[QINDEX_RANGE];
76*77c1e3ccSAndroid Build Coastguard Worker static int inter_minq_8[QINDEX_RANGE];
77*77c1e3ccSAndroid Build Coastguard Worker static int rtc_minq_8[QINDEX_RANGE];
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker static int kf_low_motion_minq_10[QINDEX_RANGE];
80*77c1e3ccSAndroid Build Coastguard Worker static int kf_high_motion_minq_10[QINDEX_RANGE];
81*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_low_motion_minq_10[QINDEX_RANGE];
82*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_high_motion_minq_10[QINDEX_RANGE];
83*77c1e3ccSAndroid Build Coastguard Worker static int inter_minq_10[QINDEX_RANGE];
84*77c1e3ccSAndroid Build Coastguard Worker static int rtc_minq_10[QINDEX_RANGE];
85*77c1e3ccSAndroid Build Coastguard Worker static int kf_low_motion_minq_12[QINDEX_RANGE];
86*77c1e3ccSAndroid Build Coastguard Worker static int kf_high_motion_minq_12[QINDEX_RANGE];
87*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_low_motion_minq_12[QINDEX_RANGE];
88*77c1e3ccSAndroid Build Coastguard Worker static int arfgf_high_motion_minq_12[QINDEX_RANGE];
89*77c1e3ccSAndroid Build Coastguard Worker static int inter_minq_12[QINDEX_RANGE];
90*77c1e3ccSAndroid Build Coastguard Worker static int rtc_minq_12[QINDEX_RANGE];
91*77c1e3ccSAndroid Build Coastguard Worker
92*77c1e3ccSAndroid Build Coastguard Worker static int gf_high = 2400;
93*77c1e3ccSAndroid Build Coastguard Worker static int gf_low = 300;
94*77c1e3ccSAndroid Build Coastguard Worker #ifdef STRICT_RC
95*77c1e3ccSAndroid Build Coastguard Worker static int kf_high = 3200;
96*77c1e3ccSAndroid Build Coastguard Worker #else
97*77c1e3ccSAndroid Build Coastguard Worker static int kf_high = 5000;
98*77c1e3ccSAndroid Build Coastguard Worker #endif
99*77c1e3ccSAndroid Build Coastguard Worker static int kf_low = 400;
100*77c1e3ccSAndroid Build Coastguard Worker
101*77c1e3ccSAndroid Build Coastguard Worker // How many times less pixels there are to encode given the current scaling.
102*77c1e3ccSAndroid Build Coastguard Worker // Temporary replacement for rcf_mult and rate_thresh_mult.
resize_rate_factor(const FrameDimensionCfg * const frm_dim_cfg,int width,int height)103*77c1e3ccSAndroid Build Coastguard Worker static double resize_rate_factor(const FrameDimensionCfg *const frm_dim_cfg,
104*77c1e3ccSAndroid Build Coastguard Worker int width, int height) {
105*77c1e3ccSAndroid Build Coastguard Worker return (double)(frm_dim_cfg->width * frm_dim_cfg->height) / (width * height);
106*77c1e3ccSAndroid Build Coastguard Worker }
107*77c1e3ccSAndroid Build Coastguard Worker
108*77c1e3ccSAndroid Build Coastguard Worker // Functions to compute the active minq lookup table entries based on a
109*77c1e3ccSAndroid Build Coastguard Worker // formulaic approach to facilitate easier adjustment of the Q tables.
110*77c1e3ccSAndroid Build Coastguard Worker // The formulae were derived from computing a 3rd order polynomial best
111*77c1e3ccSAndroid Build Coastguard Worker // fit to the original data (after plotting real maxq vs minq (not q index))
get_minq_index(double maxq,double x3,double x2,double x1,aom_bit_depth_t bit_depth)112*77c1e3ccSAndroid Build Coastguard Worker static int get_minq_index(double maxq, double x3, double x2, double x1,
113*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
114*77c1e3ccSAndroid Build Coastguard Worker const double minqtarget = AOMMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
115*77c1e3ccSAndroid Build Coastguard Worker
116*77c1e3ccSAndroid Build Coastguard Worker // Special case handling to deal with the step from q2.0
117*77c1e3ccSAndroid Build Coastguard Worker // down to lossless mode represented by q 1.0.
118*77c1e3ccSAndroid Build Coastguard Worker if (minqtarget <= 2.0) return 0;
119*77c1e3ccSAndroid Build Coastguard Worker
120*77c1e3ccSAndroid Build Coastguard Worker return av1_find_qindex(minqtarget, bit_depth, 0, QINDEX_RANGE - 1);
121*77c1e3ccSAndroid Build Coastguard Worker }
122*77c1e3ccSAndroid Build Coastguard Worker
init_minq_luts(int * kf_low_m,int * kf_high_m,int * arfgf_low,int * arfgf_high,int * inter,int * rtc,aom_bit_depth_t bit_depth)123*77c1e3ccSAndroid Build Coastguard Worker static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
124*77c1e3ccSAndroid Build Coastguard Worker int *arfgf_high, int *inter, int *rtc,
125*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
126*77c1e3ccSAndroid Build Coastguard Worker int i;
127*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < QINDEX_RANGE; i++) {
128*77c1e3ccSAndroid Build Coastguard Worker const double maxq = av1_convert_qindex_to_q(i, bit_depth);
129*77c1e3ccSAndroid Build Coastguard Worker kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
130*77c1e3ccSAndroid Build Coastguard Worker kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
131*77c1e3ccSAndroid Build Coastguard Worker arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
132*77c1e3ccSAndroid Build Coastguard Worker arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
133*77c1e3ccSAndroid Build Coastguard Worker inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
134*77c1e3ccSAndroid Build Coastguard Worker rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
135*77c1e3ccSAndroid Build Coastguard Worker }
136*77c1e3ccSAndroid Build Coastguard Worker }
137*77c1e3ccSAndroid Build Coastguard Worker
rc_init_minq_luts(void)138*77c1e3ccSAndroid Build Coastguard Worker static void rc_init_minq_luts(void) {
139*77c1e3ccSAndroid Build Coastguard Worker init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
140*77c1e3ccSAndroid Build Coastguard Worker arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
141*77c1e3ccSAndroid Build Coastguard Worker inter_minq_8, rtc_minq_8, AOM_BITS_8);
142*77c1e3ccSAndroid Build Coastguard Worker init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
143*77c1e3ccSAndroid Build Coastguard Worker arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
144*77c1e3ccSAndroid Build Coastguard Worker inter_minq_10, rtc_minq_10, AOM_BITS_10);
145*77c1e3ccSAndroid Build Coastguard Worker init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
146*77c1e3ccSAndroid Build Coastguard Worker arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
147*77c1e3ccSAndroid Build Coastguard Worker inter_minq_12, rtc_minq_12, AOM_BITS_12);
148*77c1e3ccSAndroid Build Coastguard Worker }
149*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_init_minq_luts(void)150*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_init_minq_luts(void) { aom_once(rc_init_minq_luts); }
151*77c1e3ccSAndroid Build Coastguard Worker
152*77c1e3ccSAndroid Build Coastguard Worker // These functions use formulaic calculations to make playing with the
153*77c1e3ccSAndroid Build Coastguard Worker // quantizer tables easier. If necessary they can be replaced by lookup
154*77c1e3ccSAndroid Build Coastguard Worker // tables if and when things settle down in the experimental bitstream
av1_convert_qindex_to_q(int qindex,aom_bit_depth_t bit_depth)155*77c1e3ccSAndroid Build Coastguard Worker double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
156*77c1e3ccSAndroid Build Coastguard Worker // Convert the index to a real Q value (scaled down to match old Q values)
157*77c1e3ccSAndroid Build Coastguard Worker switch (bit_depth) {
158*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_8: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 4.0;
159*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_10: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 16.0;
160*77c1e3ccSAndroid Build Coastguard Worker case AOM_BITS_12: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 64.0;
161*77c1e3ccSAndroid Build Coastguard Worker default:
162*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
163*77c1e3ccSAndroid Build Coastguard Worker return -1.0;
164*77c1e3ccSAndroid Build Coastguard Worker }
165*77c1e3ccSAndroid Build Coastguard Worker }
166*77c1e3ccSAndroid Build Coastguard Worker
167*77c1e3ccSAndroid Build Coastguard Worker // Gets the appropriate bpmb enumerator based on the frame and content type
get_bpmb_enumerator(FRAME_TYPE frame_type,const int is_screen_content_type)168*77c1e3ccSAndroid Build Coastguard Worker static int get_bpmb_enumerator(FRAME_TYPE frame_type,
169*77c1e3ccSAndroid Build Coastguard Worker const int is_screen_content_type) {
170*77c1e3ccSAndroid Build Coastguard Worker int enumerator;
171*77c1e3ccSAndroid Build Coastguard Worker
172*77c1e3ccSAndroid Build Coastguard Worker if (is_screen_content_type) {
173*77c1e3ccSAndroid Build Coastguard Worker enumerator = (frame_type == KEY_FRAME) ? 1000000 : 750000;
174*77c1e3ccSAndroid Build Coastguard Worker } else {
175*77c1e3ccSAndroid Build Coastguard Worker enumerator = (frame_type == KEY_FRAME) ? 2000000 : 1500000;
176*77c1e3ccSAndroid Build Coastguard Worker }
177*77c1e3ccSAndroid Build Coastguard Worker
178*77c1e3ccSAndroid Build Coastguard Worker return enumerator;
179*77c1e3ccSAndroid Build Coastguard Worker }
180*77c1e3ccSAndroid Build Coastguard Worker
get_init_ratio(double sse)181*77c1e3ccSAndroid Build Coastguard Worker static int get_init_ratio(double sse) { return (int)(300000 / sse); }
182*77c1e3ccSAndroid Build Coastguard Worker
183*77c1e3ccSAndroid Build Coastguard Worker // Adjustment based on spatial content and last encoded keyframe.
184*77c1e3ccSAndroid Build Coastguard Worker // Allow for increase in enumerator to reduce overshoot.
adjust_rtc_keyframe(const RATE_CONTROL * rc,int enumerator)185*77c1e3ccSAndroid Build Coastguard Worker static int adjust_rtc_keyframe(const RATE_CONTROL *rc, int enumerator) {
186*77c1e3ccSAndroid Build Coastguard Worker // Don't adjust if most of the image is flat.
187*77c1e3ccSAndroid Build Coastguard Worker if (rc->perc_flat_blocks_keyframe > 70) return enumerator;
188*77c1e3ccSAndroid Build Coastguard Worker if (rc->last_encoded_size_keyframe == 0 ||
189*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_scene_change < rc->frames_since_key) {
190*77c1e3ccSAndroid Build Coastguard Worker // Very first frame, or if scene change happened after last keyframe.
191*77c1e3ccSAndroid Build Coastguard Worker if (rc->frame_spatial_variance > 1000 ||
192*77c1e3ccSAndroid Build Coastguard Worker (rc->frame_spatial_variance > 500 &&
193*77c1e3ccSAndroid Build Coastguard Worker rc->perc_flat_blocks_keyframe == 0))
194*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 3;
195*77c1e3ccSAndroid Build Coastguard Worker else if (rc->frame_spatial_variance > 500 &&
196*77c1e3ccSAndroid Build Coastguard Worker rc->perc_flat_blocks_keyframe < 10)
197*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 2;
198*77c1e3ccSAndroid Build Coastguard Worker else if (rc->frame_spatial_variance > 400)
199*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 1;
200*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->frames_since_scene_change >= rc->frames_since_key) {
201*77c1e3ccSAndroid Build Coastguard Worker // There was no scene change before previous encoded keyframe, so
202*77c1e3ccSAndroid Build Coastguard Worker // use the last_encoded/target_size_keyframe.
203*77c1e3ccSAndroid Build Coastguard Worker if (rc->last_encoded_size_keyframe > 4 * rc->last_target_size_keyframe &&
204*77c1e3ccSAndroid Build Coastguard Worker rc->frame_spatial_variance > 500)
205*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 3;
206*77c1e3ccSAndroid Build Coastguard Worker else if (rc->last_encoded_size_keyframe >
207*77c1e3ccSAndroid Build Coastguard Worker 2 * rc->last_target_size_keyframe &&
208*77c1e3ccSAndroid Build Coastguard Worker rc->frame_spatial_variance > 200)
209*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 2;
210*77c1e3ccSAndroid Build Coastguard Worker else if (rc->last_encoded_size_keyframe > rc->last_target_size_keyframe)
211*77c1e3ccSAndroid Build Coastguard Worker return enumerator << 1;
212*77c1e3ccSAndroid Build Coastguard Worker }
213*77c1e3ccSAndroid Build Coastguard Worker return enumerator;
214*77c1e3ccSAndroid Build Coastguard Worker }
215*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_bits_per_mb(const AV1_COMP * cpi,FRAME_TYPE frame_type,int qindex,double correction_factor,int accurate_estimate)216*77c1e3ccSAndroid Build Coastguard Worker int av1_rc_bits_per_mb(const AV1_COMP *cpi, FRAME_TYPE frame_type, int qindex,
217*77c1e3ccSAndroid Build Coastguard Worker double correction_factor, int accurate_estimate) {
218*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
219*77c1e3ccSAndroid Build Coastguard Worker const int is_screen_content_type = cpi->is_screen_content_type;
220*77c1e3ccSAndroid Build Coastguard Worker const aom_bit_depth_t bit_depth = cm->seq_params->bit_depth;
221*77c1e3ccSAndroid Build Coastguard Worker const double q = av1_convert_qindex_to_q(qindex, bit_depth);
222*77c1e3ccSAndroid Build Coastguard Worker int enumerator = get_bpmb_enumerator(frame_type, is_screen_content_type);
223*77c1e3ccSAndroid Build Coastguard Worker
224*77c1e3ccSAndroid Build Coastguard Worker assert(correction_factor <= MAX_BPB_FACTOR &&
225*77c1e3ccSAndroid Build Coastguard Worker correction_factor >= MIN_BPB_FACTOR);
226*77c1e3ccSAndroid Build Coastguard Worker
227*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_CBR && frame_type != KEY_FRAME &&
228*77c1e3ccSAndroid Build Coastguard Worker accurate_estimate && cpi->rec_sse != UINT64_MAX) {
229*77c1e3ccSAndroid Build Coastguard Worker const int mbs = cm->mi_params.MBs;
230*77c1e3ccSAndroid Build Coastguard Worker const double sse_sqrt =
231*77c1e3ccSAndroid Build Coastguard Worker (double)((int)sqrt((double)(cpi->rec_sse)) << BPER_MB_NORMBITS) /
232*77c1e3ccSAndroid Build Coastguard Worker (double)mbs;
233*77c1e3ccSAndroid Build Coastguard Worker const int ratio = (cpi->rc.bit_est_ratio == 0) ? get_init_ratio(sse_sqrt)
234*77c1e3ccSAndroid Build Coastguard Worker : cpi->rc.bit_est_ratio;
235*77c1e3ccSAndroid Build Coastguard Worker // Clamp the enumerator to lower the q fluctuations.
236*77c1e3ccSAndroid Build Coastguard Worker enumerator = AOMMIN(AOMMAX((int)(ratio * sse_sqrt), 20000), 170000);
237*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->oxcf.rc_cfg.mode == AOM_CBR && frame_type == KEY_FRAME &&
238*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.rt_sf.rc_adjust_keyframe && bit_depth == 8 &&
239*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.max_intra_bitrate_pct > 0 &&
240*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.spatial_layer_id == 0) {
241*77c1e3ccSAndroid Build Coastguard Worker enumerator = adjust_rtc_keyframe(&cpi->rc, enumerator);
242*77c1e3ccSAndroid Build Coastguard Worker }
243*77c1e3ccSAndroid Build Coastguard Worker // q based adjustment to baseline enumerator
244*77c1e3ccSAndroid Build Coastguard Worker return (int)(enumerator * correction_factor / q);
245*77c1e3ccSAndroid Build Coastguard Worker }
246*77c1e3ccSAndroid Build Coastguard Worker
av1_estimate_bits_at_q(const AV1_COMP * cpi,int q,double correction_factor)247*77c1e3ccSAndroid Build Coastguard Worker int av1_estimate_bits_at_q(const AV1_COMP *cpi, int q,
248*77c1e3ccSAndroid Build Coastguard Worker double correction_factor) {
249*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
250*77c1e3ccSAndroid Build Coastguard Worker const FRAME_TYPE frame_type = cm->current_frame.frame_type;
251*77c1e3ccSAndroid Build Coastguard Worker const int mbs = cm->mi_params.MBs;
252*77c1e3ccSAndroid Build Coastguard Worker const int bpm =
253*77c1e3ccSAndroid Build Coastguard Worker (int)(av1_rc_bits_per_mb(cpi, frame_type, q, correction_factor,
254*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.hl_sf.accurate_bit_estimate));
255*77c1e3ccSAndroid Build Coastguard Worker return AOMMAX(FRAME_OVERHEAD_BITS,
256*77c1e3ccSAndroid Build Coastguard Worker (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
257*77c1e3ccSAndroid Build Coastguard Worker }
258*77c1e3ccSAndroid Build Coastguard Worker
clamp_pframe_target_size(const AV1_COMP * const cpi,int64_t target,FRAME_UPDATE_TYPE frame_update_type)259*77c1e3ccSAndroid Build Coastguard Worker static int clamp_pframe_target_size(const AV1_COMP *const cpi, int64_t target,
260*77c1e3ccSAndroid Build Coastguard Worker FRAME_UPDATE_TYPE frame_update_type) {
261*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
262*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
263*77c1e3ccSAndroid Build Coastguard Worker const int min_frame_target =
264*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
265*77c1e3ccSAndroid Build Coastguard Worker // Clip the frame target to the minimum setup value.
266*77c1e3ccSAndroid Build Coastguard Worker if (frame_update_type == OVERLAY_UPDATE ||
267*77c1e3ccSAndroid Build Coastguard Worker frame_update_type == INTNL_OVERLAY_UPDATE) {
268*77c1e3ccSAndroid Build Coastguard Worker // If there is an active ARF at this location use the minimum
269*77c1e3ccSAndroid Build Coastguard Worker // bits on this frame even if it is a constructed arf.
270*77c1e3ccSAndroid Build Coastguard Worker // The active maximum quantizer insures that an appropriate
271*77c1e3ccSAndroid Build Coastguard Worker // number of bits will be spent if needed for constructed ARFs.
272*77c1e3ccSAndroid Build Coastguard Worker target = min_frame_target;
273*77c1e3ccSAndroid Build Coastguard Worker } else if (target < min_frame_target) {
274*77c1e3ccSAndroid Build Coastguard Worker target = min_frame_target;
275*77c1e3ccSAndroid Build Coastguard Worker }
276*77c1e3ccSAndroid Build Coastguard Worker
277*77c1e3ccSAndroid Build Coastguard Worker // Clip the frame target to the maximum allowed value.
278*77c1e3ccSAndroid Build Coastguard Worker if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
279*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->max_inter_bitrate_pct) {
280*77c1e3ccSAndroid Build Coastguard Worker const int64_t max_rate =
281*77c1e3ccSAndroid Build Coastguard Worker (int64_t)rc->avg_frame_bandwidth * rc_cfg->max_inter_bitrate_pct / 100;
282*77c1e3ccSAndroid Build Coastguard Worker target = AOMMIN(target, max_rate);
283*77c1e3ccSAndroid Build Coastguard Worker }
284*77c1e3ccSAndroid Build Coastguard Worker
285*77c1e3ccSAndroid Build Coastguard Worker return (int)target;
286*77c1e3ccSAndroid Build Coastguard Worker }
287*77c1e3ccSAndroid Build Coastguard Worker
clamp_iframe_target_size(const AV1_COMP * const cpi,int64_t target)288*77c1e3ccSAndroid Build Coastguard Worker static int clamp_iframe_target_size(const AV1_COMP *const cpi, int64_t target) {
289*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
290*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
291*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->max_intra_bitrate_pct) {
292*77c1e3ccSAndroid Build Coastguard Worker const int64_t max_rate =
293*77c1e3ccSAndroid Build Coastguard Worker (int64_t)rc->avg_frame_bandwidth * rc_cfg->max_intra_bitrate_pct / 100;
294*77c1e3ccSAndroid Build Coastguard Worker target = AOMMIN(target, max_rate);
295*77c1e3ccSAndroid Build Coastguard Worker }
296*77c1e3ccSAndroid Build Coastguard Worker if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
297*77c1e3ccSAndroid Build Coastguard Worker return (int)target;
298*77c1e3ccSAndroid Build Coastguard Worker }
299*77c1e3ccSAndroid Build Coastguard Worker
300*77c1e3ccSAndroid Build Coastguard Worker // Update the buffer level for higher temporal layers, given the encoded current
301*77c1e3ccSAndroid Build Coastguard Worker // temporal layer.
update_layer_buffer_level(SVC * svc,int encoded_frame_size,bool is_screen)302*77c1e3ccSAndroid Build Coastguard Worker static void update_layer_buffer_level(SVC *svc, int encoded_frame_size,
303*77c1e3ccSAndroid Build Coastguard Worker bool is_screen) {
304*77c1e3ccSAndroid Build Coastguard Worker const int current_temporal_layer = svc->temporal_layer_id;
305*77c1e3ccSAndroid Build Coastguard Worker for (int i = current_temporal_layer + 1; i < svc->number_temporal_layers;
306*77c1e3ccSAndroid Build Coastguard Worker ++i) {
307*77c1e3ccSAndroid Build Coastguard Worker const int layer =
308*77c1e3ccSAndroid Build Coastguard Worker LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
309*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
310*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc;
311*77c1e3ccSAndroid Build Coastguard Worker lp_rc->bits_off_target +=
312*77c1e3ccSAndroid Build Coastguard Worker (int)round(lc->target_bandwidth / lc->framerate) - encoded_frame_size;
313*77c1e3ccSAndroid Build Coastguard Worker // Clip buffer level to maximum buffer size for the layer.
314*77c1e3ccSAndroid Build Coastguard Worker lp_rc->bits_off_target =
315*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(lp_rc->bits_off_target, lp_rc->maximum_buffer_size);
316*77c1e3ccSAndroid Build Coastguard Worker lp_rc->buffer_level = lp_rc->bits_off_target;
317*77c1e3ccSAndroid Build Coastguard Worker
318*77c1e3ccSAndroid Build Coastguard Worker // For screen-content mode: don't let buffer level go below threshold,
319*77c1e3ccSAndroid Build Coastguard Worker // given here as -rc->maximum_ buffer_size, to allow buffer to come back
320*77c1e3ccSAndroid Build Coastguard Worker // up sooner after slide change with big overshoot.
321*77c1e3ccSAndroid Build Coastguard Worker if (is_screen) {
322*77c1e3ccSAndroid Build Coastguard Worker lp_rc->bits_off_target =
323*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(lp_rc->bits_off_target, -lp_rc->maximum_buffer_size);
324*77c1e3ccSAndroid Build Coastguard Worker lp_rc->buffer_level = lp_rc->bits_off_target;
325*77c1e3ccSAndroid Build Coastguard Worker }
326*77c1e3ccSAndroid Build Coastguard Worker }
327*77c1e3ccSAndroid Build Coastguard Worker }
328*77c1e3ccSAndroid Build Coastguard Worker // Update the buffer level: leaky bucket model.
update_buffer_level(AV1_COMP * cpi,int encoded_frame_size)329*77c1e3ccSAndroid Build Coastguard Worker static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) {
330*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
331*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
332*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
333*77c1e3ccSAndroid Build Coastguard Worker
334*77c1e3ccSAndroid Build Coastguard Worker // Non-viewable frames are a special case and are treated as pure overhead.
335*77c1e3ccSAndroid Build Coastguard Worker if (!cm->show_frame)
336*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target -= encoded_frame_size;
337*77c1e3ccSAndroid Build Coastguard Worker else
338*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
339*77c1e3ccSAndroid Build Coastguard Worker
340*77c1e3ccSAndroid Build Coastguard Worker // Clip the buffer level to the maximum specified buffer size.
341*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target =
342*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
343*77c1e3ccSAndroid Build Coastguard Worker // For screen-content mode: don't let buffer level go below threshold,
344*77c1e3ccSAndroid Build Coastguard Worker // given here as -rc->maximum_ buffer_size, to allow buffer to come back
345*77c1e3ccSAndroid Build Coastguard Worker // up sooner after slide change with big overshoot.
346*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN)
347*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target =
348*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(p_rc->bits_off_target, -p_rc->maximum_buffer_size);
349*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level = p_rc->bits_off_target;
350*77c1e3ccSAndroid Build Coastguard Worker
351*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc)
352*77c1e3ccSAndroid Build Coastguard Worker update_layer_buffer_level(&cpi->svc, encoded_frame_size,
353*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN);
354*77c1e3ccSAndroid Build Coastguard Worker
355*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
356*77c1e3ccSAndroid Build Coastguard Worker /* The variable temp_buffer_level is introduced for quality
357*77c1e3ccSAndroid Build Coastguard Worker * simulation purpose, it retains the value previous to the parallel
358*77c1e3ccSAndroid Build Coastguard Worker * encode frames. The variable is updated based on the update flag.
359*77c1e3ccSAndroid Build Coastguard Worker *
360*77c1e3ccSAndroid Build Coastguard Worker * If there exist show_existing_frames between parallel frames, then to
361*77c1e3ccSAndroid Build Coastguard Worker * retain the temp state do not update it. */
362*77c1e3ccSAndroid Build Coastguard Worker int show_existing_between_parallel_frames =
363*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
364*77c1e3ccSAndroid Build Coastguard Worker INTNL_OVERLAY_UPDATE &&
365*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
366*77c1e3ccSAndroid Build Coastguard Worker
367*77c1e3ccSAndroid Build Coastguard Worker if (cpi->do_frame_data_update && !show_existing_between_parallel_frames &&
368*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
369*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_buffer_level = p_rc->buffer_level;
370*77c1e3ccSAndroid Build Coastguard Worker }
371*77c1e3ccSAndroid Build Coastguard Worker #endif
372*77c1e3ccSAndroid Build Coastguard Worker }
373*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_get_default_min_gf_interval(int width,int height,double framerate)374*77c1e3ccSAndroid Build Coastguard Worker int av1_rc_get_default_min_gf_interval(int width, int height,
375*77c1e3ccSAndroid Build Coastguard Worker double framerate) {
376*77c1e3ccSAndroid Build Coastguard Worker // Assume we do not need any constraint lower than 4K 20 fps
377*77c1e3ccSAndroid Build Coastguard Worker static const double factor_safe = 3840 * 2160 * 20.0;
378*77c1e3ccSAndroid Build Coastguard Worker const double factor = (double)width * height * framerate;
379*77c1e3ccSAndroid Build Coastguard Worker const int default_interval =
380*77c1e3ccSAndroid Build Coastguard Worker clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
381*77c1e3ccSAndroid Build Coastguard Worker
382*77c1e3ccSAndroid Build Coastguard Worker if (factor <= factor_safe)
383*77c1e3ccSAndroid Build Coastguard Worker return default_interval;
384*77c1e3ccSAndroid Build Coastguard Worker else
385*77c1e3ccSAndroid Build Coastguard Worker return AOMMAX(default_interval,
386*77c1e3ccSAndroid Build Coastguard Worker (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
387*77c1e3ccSAndroid Build Coastguard Worker // Note this logic makes:
388*77c1e3ccSAndroid Build Coastguard Worker // 4K24: 5
389*77c1e3ccSAndroid Build Coastguard Worker // 4K30: 6
390*77c1e3ccSAndroid Build Coastguard Worker // 4K60: 12
391*77c1e3ccSAndroid Build Coastguard Worker }
392*77c1e3ccSAndroid Build Coastguard Worker
393*77c1e3ccSAndroid Build Coastguard Worker // Note get_default_max_gf_interval() requires the min_gf_interval to
394*77c1e3ccSAndroid Build Coastguard Worker // be passed in to ensure that the max_gf_interval returned is at least as big
395*77c1e3ccSAndroid Build Coastguard Worker // as that.
get_default_max_gf_interval(double framerate,int min_gf_interval)396*77c1e3ccSAndroid Build Coastguard Worker static int get_default_max_gf_interval(double framerate, int min_gf_interval) {
397*77c1e3ccSAndroid Build Coastguard Worker int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
398*77c1e3ccSAndroid Build Coastguard Worker interval += (interval & 0x01); // Round to even value
399*77c1e3ccSAndroid Build Coastguard Worker interval = AOMMAX(MAX_GF_INTERVAL, interval);
400*77c1e3ccSAndroid Build Coastguard Worker return AOMMAX(interval, min_gf_interval);
401*77c1e3ccSAndroid Build Coastguard Worker }
402*77c1e3ccSAndroid Build Coastguard Worker
av1_primary_rc_init(const AV1EncoderConfig * oxcf,PRIMARY_RATE_CONTROL * p_rc)403*77c1e3ccSAndroid Build Coastguard Worker void av1_primary_rc_init(const AV1EncoderConfig *oxcf,
404*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *p_rc) {
405*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
406*77c1e3ccSAndroid Build Coastguard Worker
407*77c1e3ccSAndroid Build Coastguard Worker int worst_allowed_q = rc_cfg->worst_allowed_q;
408*77c1e3ccSAndroid Build Coastguard Worker
409*77c1e3ccSAndroid Build Coastguard Worker int min_gf_interval = oxcf->gf_cfg.min_gf_interval;
410*77c1e3ccSAndroid Build Coastguard Worker int max_gf_interval = oxcf->gf_cfg.max_gf_interval;
411*77c1e3ccSAndroid Build Coastguard Worker if (min_gf_interval == 0)
412*77c1e3ccSAndroid Build Coastguard Worker min_gf_interval = av1_rc_get_default_min_gf_interval(
413*77c1e3ccSAndroid Build Coastguard Worker oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
414*77c1e3ccSAndroid Build Coastguard Worker oxcf->input_cfg.init_framerate);
415*77c1e3ccSAndroid Build Coastguard Worker if (max_gf_interval == 0)
416*77c1e3ccSAndroid Build Coastguard Worker max_gf_interval = get_default_max_gf_interval(
417*77c1e3ccSAndroid Build Coastguard Worker oxcf->input_cfg.init_framerate, min_gf_interval);
418*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval = (min_gf_interval + max_gf_interval) / 2;
419*77c1e3ccSAndroid Build Coastguard Worker p_rc->this_key_frame_forced = 0;
420*77c1e3ccSAndroid Build Coastguard Worker p_rc->next_key_frame_forced = 0;
421*77c1e3ccSAndroid Build Coastguard Worker p_rc->ni_frames = 0;
422*77c1e3ccSAndroid Build Coastguard Worker
423*77c1e3ccSAndroid Build Coastguard Worker p_rc->tot_q = 0.0;
424*77c1e3ccSAndroid Build Coastguard Worker p_rc->total_actual_bits = 0;
425*77c1e3ccSAndroid Build Coastguard Worker p_rc->total_target_bits = 0;
426*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level = p_rc->starting_buffer_level;
427*77c1e3ccSAndroid Build Coastguard Worker
428*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->target_seq_level_idx[0] < SEQ_LEVELS) {
429*77c1e3ccSAndroid Build Coastguard Worker worst_allowed_q = 255;
430*77c1e3ccSAndroid Build Coastguard Worker }
431*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->pass == AOM_RC_ONE_PASS && rc_cfg->mode == AOM_CBR) {
432*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[KEY_FRAME] = worst_allowed_q;
433*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] = worst_allowed_q;
434*77c1e3ccSAndroid Build Coastguard Worker } else {
435*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[KEY_FRAME] =
436*77c1e3ccSAndroid Build Coastguard Worker (worst_allowed_q + rc_cfg->best_allowed_q) / 2;
437*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] =
438*77c1e3ccSAndroid Build Coastguard Worker (worst_allowed_q + rc_cfg->best_allowed_q) / 2;
439*77c1e3ccSAndroid Build Coastguard Worker }
440*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_q = av1_convert_qindex_to_q(rc_cfg->worst_allowed_q,
441*77c1e3ccSAndroid Build Coastguard Worker oxcf->tool_cfg.bit_depth);
442*77c1e3ccSAndroid Build Coastguard Worker p_rc->last_q[KEY_FRAME] = rc_cfg->best_allowed_q;
443*77c1e3ccSAndroid Build Coastguard Worker p_rc->last_q[INTER_FRAME] = rc_cfg->worst_allowed_q;
444*77c1e3ccSAndroid Build Coastguard Worker
445*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < RATE_FACTOR_LEVELS; ++i) {
446*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[i] = 0.7;
447*77c1e3ccSAndroid Build Coastguard Worker }
448*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[KF_STD] = 1.0;
449*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target = p_rc->starting_buffer_level;
450*77c1e3ccSAndroid Build Coastguard Worker
451*77c1e3ccSAndroid Build Coastguard Worker p_rc->rolling_target_bits = AOMMAX(
452*77c1e3ccSAndroid Build Coastguard Worker 1, (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate));
453*77c1e3ccSAndroid Build Coastguard Worker p_rc->rolling_actual_bits = AOMMAX(
454*77c1e3ccSAndroid Build Coastguard Worker 1, (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate));
455*77c1e3ccSAndroid Build Coastguard Worker }
456*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_init(const AV1EncoderConfig * oxcf,RATE_CONTROL * rc)457*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_init(const AV1EncoderConfig *oxcf, RATE_CONTROL *rc) {
458*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
459*77c1e3ccSAndroid Build Coastguard Worker
460*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key = 8; // Sensible default for first frame.
461*77c1e3ccSAndroid Build Coastguard Worker rc->frames_to_fwd_kf = oxcf->kf_cfg.fwd_kf_dist;
462*77c1e3ccSAndroid Build Coastguard Worker
463*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due = 0;
464*77c1e3ccSAndroid Build Coastguard Worker rc->ni_av_qi = rc_cfg->worst_allowed_q;
465*77c1e3ccSAndroid Build Coastguard Worker rc->ni_tot_qi = 0;
466*77c1e3ccSAndroid Build Coastguard Worker
467*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
468*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
469*77c1e3ccSAndroid Build Coastguard Worker if (rc->min_gf_interval == 0)
470*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
471*77c1e3ccSAndroid Build Coastguard Worker oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
472*77c1e3ccSAndroid Build Coastguard Worker oxcf->input_cfg.init_framerate);
473*77c1e3ccSAndroid Build Coastguard Worker if (rc->max_gf_interval == 0)
474*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval = get_default_max_gf_interval(
475*77c1e3ccSAndroid Build Coastguard Worker oxcf->input_cfg.init_framerate, rc->min_gf_interval);
476*77c1e3ccSAndroid Build Coastguard Worker rc->avg_frame_low_motion = 0;
477*77c1e3ccSAndroid Build Coastguard Worker
478*77c1e3ccSAndroid Build Coastguard Worker rc->resize_state = ORIG;
479*77c1e3ccSAndroid Build Coastguard Worker rc->resize_avg_qp = 0;
480*77c1e3ccSAndroid Build Coastguard Worker rc->resize_buffer_underflow = 0;
481*77c1e3ccSAndroid Build Coastguard Worker rc->resize_count = 0;
482*77c1e3ccSAndroid Build Coastguard Worker rc->rtc_external_ratectrl = 0;
483*77c1e3ccSAndroid Build Coastguard Worker rc->frame_level_fast_extra_bits = 0;
484*77c1e3ccSAndroid Build Coastguard Worker rc->use_external_qp_one_pass = 0;
485*77c1e3ccSAndroid Build Coastguard Worker rc->percent_blocks_inactive = 0;
486*77c1e3ccSAndroid Build Coastguard Worker rc->force_max_q = 0;
487*77c1e3ccSAndroid Build Coastguard Worker rc->postencode_drop = 0;
488*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_scene_change = 0;
489*77c1e3ccSAndroid Build Coastguard Worker }
490*77c1e3ccSAndroid Build Coastguard Worker
check_buffer_below_thresh(AV1_COMP * cpi,int64_t buffer_level,int drop_mark)491*77c1e3ccSAndroid Build Coastguard Worker static bool check_buffer_below_thresh(AV1_COMP *cpi, int64_t buffer_level,
492*77c1e3ccSAndroid Build Coastguard Worker int drop_mark) {
493*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
494*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->ppi->use_svc || cpi->svc.number_spatial_layers == 1 ||
495*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.framedrop_mode == AOM_LAYER_DROP) {
496*77c1e3ccSAndroid Build Coastguard Worker return (buffer_level <= drop_mark);
497*77c1e3ccSAndroid Build Coastguard Worker } else {
498*77c1e3ccSAndroid Build Coastguard Worker // For SVC in the AOM_FULL_SUPERFRAME_DROP): the condition on
499*77c1e3ccSAndroid Build Coastguard Worker // buffer is checked on current and upper spatial layers.
500*77c1e3ccSAndroid Build Coastguard Worker for (int i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
501*77c1e3ccSAndroid Build Coastguard Worker const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
502*77c1e3ccSAndroid Build Coastguard Worker svc->number_temporal_layers);
503*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
504*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *lrc = &lc->p_rc;
505*77c1e3ccSAndroid Build Coastguard Worker // Exclude check for layer whose bitrate is 0.
506*77c1e3ccSAndroid Build Coastguard Worker if (lc->target_bandwidth > 0) {
507*77c1e3ccSAndroid Build Coastguard Worker const int drop_thresh = cpi->oxcf.rc_cfg.drop_frames_water_mark;
508*77c1e3ccSAndroid Build Coastguard Worker const int drop_mark_layer =
509*77c1e3ccSAndroid Build Coastguard Worker (int)(drop_thresh * lrc->optimal_buffer_level / 100);
510*77c1e3ccSAndroid Build Coastguard Worker if (lrc->buffer_level <= drop_mark_layer) return true;
511*77c1e3ccSAndroid Build Coastguard Worker }
512*77c1e3ccSAndroid Build Coastguard Worker }
513*77c1e3ccSAndroid Build Coastguard Worker return false;
514*77c1e3ccSAndroid Build Coastguard Worker }
515*77c1e3ccSAndroid Build Coastguard Worker }
516*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_drop_frame(AV1_COMP * cpi)517*77c1e3ccSAndroid Build Coastguard Worker int av1_rc_drop_frame(AV1_COMP *cpi) {
518*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *oxcf = &cpi->oxcf;
519*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
520*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
521*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
522*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
523*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
524*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
525*77c1e3ccSAndroid Build Coastguard Worker int64_t buffer_level =
526*77c1e3ccSAndroid Build Coastguard Worker simulate_parallel_frame ? p_rc->temp_buffer_level : p_rc->buffer_level;
527*77c1e3ccSAndroid Build Coastguard Worker #else
528*77c1e3ccSAndroid Build Coastguard Worker int64_t buffer_level = p_rc->buffer_level;
529*77c1e3ccSAndroid Build Coastguard Worker #endif
530*77c1e3ccSAndroid Build Coastguard Worker // Never drop on key frame, or for frame whose base layer is key.
531*77c1e3ccSAndroid Build Coastguard Worker // If drop_count_consec hits or exceeds max_consec_drop then don't drop.
532*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_type == KEY_FRAME ||
533*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->use_svc &&
534*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame) ||
535*77c1e3ccSAndroid Build Coastguard Worker !oxcf->rc_cfg.drop_frames_water_mark ||
536*77c1e3ccSAndroid Build Coastguard Worker (rc->max_consec_drop > 0 &&
537*77c1e3ccSAndroid Build Coastguard Worker rc->drop_count_consec >= rc->max_consec_drop)) {
538*77c1e3ccSAndroid Build Coastguard Worker return 0;
539*77c1e3ccSAndroid Build Coastguard Worker } else {
540*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
541*77c1e3ccSAndroid Build Coastguard Worker // In the full_superframe framedrop mode for svc, if the previous spatial
542*77c1e3ccSAndroid Build Coastguard Worker // layer was dropped, drop the current spatial layer.
543*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
544*77c1e3ccSAndroid Build Coastguard Worker svc->drop_spatial_layer[svc->spatial_layer_id - 1] &&
545*77c1e3ccSAndroid Build Coastguard Worker svc->framedrop_mode == AOM_FULL_SUPERFRAME_DROP)
546*77c1e3ccSAndroid Build Coastguard Worker return 1;
547*77c1e3ccSAndroid Build Coastguard Worker // -1 is passed here for drop_mark since we are checking if
548*77c1e3ccSAndroid Build Coastguard Worker // buffer goes below 0 (<= -1).
549*77c1e3ccSAndroid Build Coastguard Worker if (check_buffer_below_thresh(cpi, buffer_level, -1)) {
550*77c1e3ccSAndroid Build Coastguard Worker // Always drop if buffer is below 0.
551*77c1e3ccSAndroid Build Coastguard Worker rc->drop_count_consec++;
552*77c1e3ccSAndroid Build Coastguard Worker return 1;
553*77c1e3ccSAndroid Build Coastguard Worker } else {
554*77c1e3ccSAndroid Build Coastguard Worker // If buffer is below drop_mark, for now just drop every other frame
555*77c1e3ccSAndroid Build Coastguard Worker // (starting with the next frame) until it increases back over drop_mark.
556*77c1e3ccSAndroid Build Coastguard Worker const int drop_mark = (int)(oxcf->rc_cfg.drop_frames_water_mark *
557*77c1e3ccSAndroid Build Coastguard Worker p_rc->optimal_buffer_level / 100);
558*77c1e3ccSAndroid Build Coastguard Worker const bool buffer_below_thresh =
559*77c1e3ccSAndroid Build Coastguard Worker check_buffer_below_thresh(cpi, buffer_level, drop_mark);
560*77c1e3ccSAndroid Build Coastguard Worker if (!buffer_below_thresh && rc->decimation_factor > 0) {
561*77c1e3ccSAndroid Build Coastguard Worker --rc->decimation_factor;
562*77c1e3ccSAndroid Build Coastguard Worker } else if (buffer_below_thresh && rc->decimation_factor == 0) {
563*77c1e3ccSAndroid Build Coastguard Worker rc->decimation_factor = 1;
564*77c1e3ccSAndroid Build Coastguard Worker }
565*77c1e3ccSAndroid Build Coastguard Worker if (rc->decimation_factor > 0) {
566*77c1e3ccSAndroid Build Coastguard Worker if (rc->decimation_count > 0) {
567*77c1e3ccSAndroid Build Coastguard Worker --rc->decimation_count;
568*77c1e3ccSAndroid Build Coastguard Worker rc->drop_count_consec++;
569*77c1e3ccSAndroid Build Coastguard Worker return 1;
570*77c1e3ccSAndroid Build Coastguard Worker } else {
571*77c1e3ccSAndroid Build Coastguard Worker rc->decimation_count = rc->decimation_factor;
572*77c1e3ccSAndroid Build Coastguard Worker return 0;
573*77c1e3ccSAndroid Build Coastguard Worker }
574*77c1e3ccSAndroid Build Coastguard Worker } else {
575*77c1e3ccSAndroid Build Coastguard Worker rc->decimation_count = 0;
576*77c1e3ccSAndroid Build Coastguard Worker return 0;
577*77c1e3ccSAndroid Build Coastguard Worker }
578*77c1e3ccSAndroid Build Coastguard Worker }
579*77c1e3ccSAndroid Build Coastguard Worker }
580*77c1e3ccSAndroid Build Coastguard Worker }
581*77c1e3ccSAndroid Build Coastguard Worker
adjust_q_cbr(const AV1_COMP * cpi,int q,int active_worst_quality,int width,int height)582*77c1e3ccSAndroid Build Coastguard Worker static int adjust_q_cbr(const AV1_COMP *cpi, int q, int active_worst_quality,
583*77c1e3ccSAndroid Build Coastguard Worker int width, int height) {
584*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
585*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
586*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
587*77c1e3ccSAndroid Build Coastguard Worker const SVC *const svc = &cpi->svc;
588*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
589*77c1e3ccSAndroid Build Coastguard Worker // Flag to indicate previous frame has overshoot, and buffer level
590*77c1e3ccSAndroid Build Coastguard Worker // for current frame is low (less than ~half of optimal). For such
591*77c1e3ccSAndroid Build Coastguard Worker // (inter) frames, if the source_sad is non-zero, relax the max_delta_up
592*77c1e3ccSAndroid Build Coastguard Worker // and clamp applied below.
593*77c1e3ccSAndroid Build Coastguard Worker const bool overshoot_buffer_low =
594*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame == -1 && rc->frame_source_sad > 1000 &&
595*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level < (p_rc->optimal_buffer_level >> 1) &&
596*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key > 4;
597*77c1e3ccSAndroid Build Coastguard Worker int max_delta_down;
598*77c1e3ccSAndroid Build Coastguard Worker int max_delta_up = overshoot_buffer_low ? 120 : 20;
599*77c1e3ccSAndroid Build Coastguard Worker const int change_avg_frame_bandwidth =
600*77c1e3ccSAndroid Build Coastguard Worker abs(rc->avg_frame_bandwidth - rc->prev_avg_frame_bandwidth) >
601*77c1e3ccSAndroid Build Coastguard Worker 0.1 * (rc->avg_frame_bandwidth);
602*77c1e3ccSAndroid Build Coastguard Worker
603*77c1e3ccSAndroid Build Coastguard Worker // Set the maximum adjustment down for Q for this frame.
604*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
605*77c1e3ccSAndroid Build Coastguard Worker cpi->cyclic_refresh->apply_cyclic_refresh) {
606*77c1e3ccSAndroid Build Coastguard Worker // For static screen type content limit the Q drop till the start of the
607*77c1e3ccSAndroid Build Coastguard Worker // next refresh cycle.
608*77c1e3ccSAndroid Build Coastguard Worker if (cpi->is_screen_content_type &&
609*77c1e3ccSAndroid Build Coastguard Worker (cpi->cyclic_refresh->sb_index > cpi->cyclic_refresh->last_sb_index)) {
610*77c1e3ccSAndroid Build Coastguard Worker max_delta_down = AOMMIN(8, AOMMAX(1, rc->q_1_frame / 32));
611*77c1e3ccSAndroid Build Coastguard Worker } else {
612*77c1e3ccSAndroid Build Coastguard Worker max_delta_down = AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8));
613*77c1e3ccSAndroid Build Coastguard Worker }
614*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->ppi->use_svc && cpi->is_screen_content_type) {
615*77c1e3ccSAndroid Build Coastguard Worker // Link max_delta_up to max_delta_down and buffer status.
616*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->buffer_level > p_rc->optimal_buffer_level) {
617*77c1e3ccSAndroid Build Coastguard Worker max_delta_up = AOMMAX(4, max_delta_down);
618*77c1e3ccSAndroid Build Coastguard Worker } else if (!overshoot_buffer_low) {
619*77c1e3ccSAndroid Build Coastguard Worker max_delta_up = AOMMAX(8, max_delta_down);
620*77c1e3ccSAndroid Build Coastguard Worker }
621*77c1e3ccSAndroid Build Coastguard Worker }
622*77c1e3ccSAndroid Build Coastguard Worker } else {
623*77c1e3ccSAndroid Build Coastguard Worker max_delta_down = (cpi->is_screen_content_type)
624*77c1e3ccSAndroid Build Coastguard Worker ? AOMMIN(8, AOMMAX(1, rc->q_1_frame / 16))
625*77c1e3ccSAndroid Build Coastguard Worker : AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8));
626*77c1e3ccSAndroid Build Coastguard Worker }
627*77c1e3ccSAndroid Build Coastguard Worker // For screen static content with stable buffer level: relax the
628*77c1e3ccSAndroid Build Coastguard Worker // limit on max_delta_down and apply bias qp, based on buffer fullness.
629*77c1e3ccSAndroid Build Coastguard Worker // Only for high speeds levels for now to avoid bdrate regression.
630*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.rc_faster_convergence_static == 1 &&
631*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.rt_sf.check_scene_detection && rc->frame_source_sad == 0 &&
632*77c1e3ccSAndroid Build Coastguard Worker rc->static_since_last_scene_change &&
633*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level > (p_rc->optimal_buffer_level >> 1) &&
634*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
635*77c1e3ccSAndroid Build Coastguard Worker cpi->cyclic_refresh->counter_encode_maxq_scene_change > 4) {
636*77c1e3ccSAndroid Build Coastguard Worker int qp_delta = 32;
637*77c1e3ccSAndroid Build Coastguard Worker int qp_bias = 16;
638*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->buffer_level > p_rc->optimal_buffer_level) {
639*77c1e3ccSAndroid Build Coastguard Worker qp_delta = 60;
640*77c1e3ccSAndroid Build Coastguard Worker qp_bias = 32;
641*77c1e3ccSAndroid Build Coastguard Worker }
642*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.rc_1_frame == 1) q = q - qp_bias;
643*77c1e3ccSAndroid Build Coastguard Worker max_delta_down = AOMMAX(max_delta_down, qp_delta);
644*77c1e3ccSAndroid Build Coastguard Worker max_delta_up = AOMMIN(max_delta_up, 4);
645*77c1e3ccSAndroid Build Coastguard Worker }
646*77c1e3ccSAndroid Build Coastguard Worker
647*77c1e3ccSAndroid Build Coastguard Worker // If resolution changes or avg_frame_bandwidth significantly changed,
648*77c1e3ccSAndroid Build Coastguard Worker // then set this flag to indicate change in target bits per macroblock.
649*77c1e3ccSAndroid Build Coastguard Worker const int change_target_bits_mb =
650*77c1e3ccSAndroid Build Coastguard Worker cm->prev_frame &&
651*77c1e3ccSAndroid Build Coastguard Worker (width != cm->prev_frame->width || height != cm->prev_frame->height ||
652*77c1e3ccSAndroid Build Coastguard Worker change_avg_frame_bandwidth);
653*77c1e3ccSAndroid Build Coastguard Worker // Apply some control/clamp to QP under certain conditions.
654*77c1e3ccSAndroid Build Coastguard Worker // Delay the use of the clamping for svc until after num_temporal_layers,
655*77c1e3ccSAndroid Build Coastguard Worker // to make they have been set for each temporal layer.
656*77c1e3ccSAndroid Build Coastguard Worker // Check for rc->q_1/2_frame > 0 in case they have not been set due to
657*77c1e3ccSAndroid Build Coastguard Worker // dropped frames.
658*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_intra_only(cm) && rc->frames_since_key > 1 &&
659*77c1e3ccSAndroid Build Coastguard Worker rc->q_1_frame > 0 && rc->q_2_frame > 0 &&
660*77c1e3ccSAndroid Build Coastguard Worker (!cpi->ppi->use_svc ||
661*77c1e3ccSAndroid Build Coastguard Worker svc->current_superframe > (unsigned int)svc->number_temporal_layers) &&
662*77c1e3ccSAndroid Build Coastguard Worker !change_target_bits_mb && !cpi->rc.rtc_external_ratectrl &&
663*77c1e3ccSAndroid Build Coastguard Worker (!cpi->oxcf.rc_cfg.gf_cbr_boost_pct ||
664*77c1e3ccSAndroid Build Coastguard Worker !(refresh_frame->alt_ref_frame || refresh_frame->golden_frame))) {
665*77c1e3ccSAndroid Build Coastguard Worker // If in the previous two frames we have seen both overshoot and undershoot
666*77c1e3ccSAndroid Build Coastguard Worker // clamp Q between the two.
667*77c1e3ccSAndroid Build Coastguard Worker if (rc->rc_1_frame * rc->rc_2_frame == -1 &&
668*77c1e3ccSAndroid Build Coastguard Worker rc->q_1_frame != rc->q_2_frame && !overshoot_buffer_low) {
669*77c1e3ccSAndroid Build Coastguard Worker int qclamp = clamp(q, AOMMIN(rc->q_1_frame, rc->q_2_frame),
670*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(rc->q_1_frame, rc->q_2_frame));
671*77c1e3ccSAndroid Build Coastguard Worker // If the previous frame had overshoot and the current q needs to
672*77c1e3ccSAndroid Build Coastguard Worker // increase above the clamped value, reduce the clamp for faster reaction
673*77c1e3ccSAndroid Build Coastguard Worker // to overshoot.
674*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.rc_1_frame == -1 && q > qclamp && rc->frames_since_key > 10)
675*77c1e3ccSAndroid Build Coastguard Worker q = (q + qclamp) >> 1;
676*77c1e3ccSAndroid Build Coastguard Worker else
677*77c1e3ccSAndroid Build Coastguard Worker q = qclamp;
678*77c1e3ccSAndroid Build Coastguard Worker }
679*77c1e3ccSAndroid Build Coastguard Worker // Adjust Q base on source content change from scene detection.
680*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.check_scene_detection && rc->prev_avg_source_sad > 0 &&
681*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key > 10 && rc->frame_source_sad > 0 &&
682*77c1e3ccSAndroid Build Coastguard Worker !cpi->rc.rtc_external_ratectrl) {
683*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
684*77c1e3ccSAndroid Build Coastguard Worker double delta =
685*77c1e3ccSAndroid Build Coastguard Worker (double)rc->avg_source_sad / (double)rc->prev_avg_source_sad - 1.0;
686*77c1e3ccSAndroid Build Coastguard Worker // Push Q downwards if content change is decreasing and buffer level
687*77c1e3ccSAndroid Build Coastguard Worker // is stable (at least 1/4-optimal level), so not overshooting. Do so
688*77c1e3ccSAndroid Build Coastguard Worker // only for high Q to avoid excess overshoot.
689*77c1e3ccSAndroid Build Coastguard Worker // Else reduce decrease in Q from previous frame if content change is
690*77c1e3ccSAndroid Build Coastguard Worker // increasing and buffer is below max (so not undershooting).
691*77c1e3ccSAndroid Build Coastguard Worker if (delta < 0.0 &&
692*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level > (p_rc->optimal_buffer_level >> 2) &&
693*77c1e3ccSAndroid Build Coastguard Worker q > (rc->worst_quality >> 1)) {
694*77c1e3ccSAndroid Build Coastguard Worker double q_adj_factor = 1.0 + 0.5 * tanh(4.0 * delta);
695*77c1e3ccSAndroid Build Coastguard Worker double q_val = av1_convert_qindex_to_q(q, bit_depth);
696*77c1e3ccSAndroid Build Coastguard Worker q += av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
697*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->q_1_frame - q > 0 && delta > 0.1 &&
698*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level < AOMMIN(p_rc->maximum_buffer_size,
699*77c1e3ccSAndroid Build Coastguard Worker p_rc->optimal_buffer_level << 1)) {
700*77c1e3ccSAndroid Build Coastguard Worker q = (3 * q + rc->q_1_frame) >> 2;
701*77c1e3ccSAndroid Build Coastguard Worker }
702*77c1e3ccSAndroid Build Coastguard Worker }
703*77c1e3ccSAndroid Build Coastguard Worker // Limit the decrease in Q from previous frame.
704*77c1e3ccSAndroid Build Coastguard Worker if (rc->q_1_frame - q > max_delta_down) q = rc->q_1_frame - max_delta_down;
705*77c1e3ccSAndroid Build Coastguard Worker // Limit the increase in Q from previous frame.
706*77c1e3ccSAndroid Build Coastguard Worker else if (q - rc->q_1_frame > max_delta_up)
707*77c1e3ccSAndroid Build Coastguard Worker q = rc->q_1_frame + max_delta_up;
708*77c1e3ccSAndroid Build Coastguard Worker }
709*77c1e3ccSAndroid Build Coastguard Worker // Adjustment for temporal layers.
710*77c1e3ccSAndroid Build Coastguard Worker if (svc->number_temporal_layers > 1 && svc->spatial_layer_id == 0 &&
711*77c1e3ccSAndroid Build Coastguard Worker !change_target_bits_mb && !cpi->rc.rtc_external_ratectrl &&
712*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.resize_cfg.resize_mode != RESIZE_DYNAMIC) {
713*77c1e3ccSAndroid Build Coastguard Worker if (svc->temporal_layer_id > 0) {
714*77c1e3ccSAndroid Build Coastguard Worker // Constrain enhancement relative to the previous base TL0.
715*77c1e3ccSAndroid Build Coastguard Worker // Get base temporal layer TL0.
716*77c1e3ccSAndroid Build Coastguard Worker const int layer = LAYER_IDS_TO_IDX(0, 0, svc->number_temporal_layers);
717*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
718*77c1e3ccSAndroid Build Coastguard Worker // lc->rc.avg_frame_bandwidth and lc->p_rc.last_q correspond to the
719*77c1e3ccSAndroid Build Coastguard Worker // last TL0 frame.
720*77c1e3ccSAndroid Build Coastguard Worker const int last_qindex_tl0 =
721*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key < svc->number_temporal_layers
722*77c1e3ccSAndroid Build Coastguard Worker ? lc->p_rc.last_q[KEY_FRAME]
723*77c1e3ccSAndroid Build Coastguard Worker : lc->p_rc.last_q[INTER_FRAME];
724*77c1e3ccSAndroid Build Coastguard Worker if (rc->avg_frame_bandwidth < lc->rc.avg_frame_bandwidth &&
725*77c1e3ccSAndroid Build Coastguard Worker q < last_qindex_tl0 - 4)
726*77c1e3ccSAndroid Build Coastguard Worker q = last_qindex_tl0 - 4;
727*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->svc.temporal_layer_id == 0 && !frame_is_intra_only(cm) &&
728*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level > (p_rc->optimal_buffer_level >> 2) &&
729*77c1e3ccSAndroid Build Coastguard Worker rc->frame_source_sad < 100000) {
730*77c1e3ccSAndroid Build Coastguard Worker // Push base TL0 Q down if buffer is stable and frame_source_sad
731*77c1e3ccSAndroid Build Coastguard Worker // is below threshold.
732*77c1e3ccSAndroid Build Coastguard Worker int delta = (svc->number_temporal_layers == 2) ? 4 : 10;
733*77c1e3ccSAndroid Build Coastguard Worker q = q - delta;
734*77c1e3ccSAndroid Build Coastguard Worker }
735*77c1e3ccSAndroid Build Coastguard Worker }
736*77c1e3ccSAndroid Build Coastguard Worker // For non-svc (single layer): if resolution has increased push q closer
737*77c1e3ccSAndroid Build Coastguard Worker // to the active_worst to avoid excess overshoot.
738*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->ppi->use_svc && cm->prev_frame &&
739*77c1e3ccSAndroid Build Coastguard Worker (width * height > 1.5 * cm->prev_frame->width * cm->prev_frame->height))
740*77c1e3ccSAndroid Build Coastguard Worker q = (q + active_worst_quality) >> 1;
741*77c1e3ccSAndroid Build Coastguard Worker // For single layer RPS: Bias Q based on distance of closest reference.
742*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.bias_recovery_frame) {
743*77c1e3ccSAndroid Build Coastguard Worker const int min_dist = av1_svc_get_min_ref_dist(cpi);
744*77c1e3ccSAndroid Build Coastguard Worker q = q - AOMMIN(min_dist, 20);
745*77c1e3ccSAndroid Build Coastguard Worker }
746*77c1e3ccSAndroid Build Coastguard Worker return AOMMAX(AOMMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
747*77c1e3ccSAndroid Build Coastguard Worker }
748*77c1e3ccSAndroid Build Coastguard Worker
749*77c1e3ccSAndroid Build Coastguard Worker static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = {
750*77c1e3ccSAndroid Build Coastguard Worker KF_STD, // KF_UPDATE
751*77c1e3ccSAndroid Build Coastguard Worker INTER_NORMAL, // LF_UPDATE
752*77c1e3ccSAndroid Build Coastguard Worker GF_ARF_STD, // GF_UPDATE
753*77c1e3ccSAndroid Build Coastguard Worker GF_ARF_STD, // ARF_UPDATE
754*77c1e3ccSAndroid Build Coastguard Worker INTER_NORMAL, // OVERLAY_UPDATE
755*77c1e3ccSAndroid Build Coastguard Worker INTER_NORMAL, // INTNL_OVERLAY_UPDATE
756*77c1e3ccSAndroid Build Coastguard Worker GF_ARF_LOW, // INTNL_ARF_UPDATE
757*77c1e3ccSAndroid Build Coastguard Worker };
758*77c1e3ccSAndroid Build Coastguard Worker
get_rate_factor_level(const GF_GROUP * const gf_group,int gf_frame_index)759*77c1e3ccSAndroid Build Coastguard Worker static RATE_FACTOR_LEVEL get_rate_factor_level(const GF_GROUP *const gf_group,
760*77c1e3ccSAndroid Build Coastguard Worker int gf_frame_index) {
761*77c1e3ccSAndroid Build Coastguard Worker const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_frame_index];
762*77c1e3ccSAndroid Build Coastguard Worker assert(update_type < FRAME_UPDATE_TYPES);
763*77c1e3ccSAndroid Build Coastguard Worker return rate_factor_levels[update_type];
764*77c1e3ccSAndroid Build Coastguard Worker }
765*77c1e3ccSAndroid Build Coastguard Worker
766*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Gets a rate vs Q correction factor
767*77c1e3ccSAndroid Build Coastguard Worker *
768*77c1e3ccSAndroid Build Coastguard Worker * This function returns the current value of a correction factor used to
769*77c1e3ccSAndroid Build Coastguard Worker * dynamically adjust the relationship between Q and the expected number
770*77c1e3ccSAndroid Build Coastguard Worker * of bits for the frame.
771*77c1e3ccSAndroid Build Coastguard Worker *
772*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
773*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder instance structure
774*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Frame width
775*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Frame height
776*77c1e3ccSAndroid Build Coastguard Worker *
777*77c1e3ccSAndroid Build Coastguard Worker * \return Returns a correction factor for the current frame
778*77c1e3ccSAndroid Build Coastguard Worker */
get_rate_correction_factor(const AV1_COMP * cpi,int width,int height)779*77c1e3ccSAndroid Build Coastguard Worker static double get_rate_correction_factor(const AV1_COMP *cpi, int width,
780*77c1e3ccSAndroid Build Coastguard Worker int height) {
781*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
782*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
783*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
784*77c1e3ccSAndroid Build Coastguard Worker double rcf;
785*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factors_kfstd;
786*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factors_gfarfstd;
787*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factors_internormal;
788*77c1e3ccSAndroid Build Coastguard Worker
789*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factors_kfstd =
790*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
791*77c1e3ccSAndroid Build Coastguard Worker ? rc->frame_level_rate_correction_factors[KF_STD]
792*77c1e3ccSAndroid Build Coastguard Worker : p_rc->rate_correction_factors[KF_STD];
793*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factors_gfarfstd =
794*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
795*77c1e3ccSAndroid Build Coastguard Worker ? rc->frame_level_rate_correction_factors[GF_ARF_STD]
796*77c1e3ccSAndroid Build Coastguard Worker : p_rc->rate_correction_factors[GF_ARF_STD];
797*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factors_internormal =
798*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
799*77c1e3ccSAndroid Build Coastguard Worker ? rc->frame_level_rate_correction_factors[INTER_NORMAL]
800*77c1e3ccSAndroid Build Coastguard Worker : p_rc->rate_correction_factors[INTER_NORMAL];
801*77c1e3ccSAndroid Build Coastguard Worker
802*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_type == KEY_FRAME) {
803*77c1e3ccSAndroid Build Coastguard Worker rcf = rate_correction_factors_kfstd;
804*77c1e3ccSAndroid Build Coastguard Worker } else if (is_stat_consumption_stage(cpi)) {
805*77c1e3ccSAndroid Build Coastguard Worker const RATE_FACTOR_LEVEL rf_lvl =
806*77c1e3ccSAndroid Build Coastguard Worker get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index);
807*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factors_rflvl =
808*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
809*77c1e3ccSAndroid Build Coastguard Worker ? rc->frame_level_rate_correction_factors[rf_lvl]
810*77c1e3ccSAndroid Build Coastguard Worker : p_rc->rate_correction_factors[rf_lvl];
811*77c1e3ccSAndroid Build Coastguard Worker rcf = rate_correction_factors_rflvl;
812*77c1e3ccSAndroid Build Coastguard Worker } else {
813*77c1e3ccSAndroid Build Coastguard Worker if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) &&
814*77c1e3ccSAndroid Build Coastguard Worker !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
815*77c1e3ccSAndroid Build Coastguard Worker (cpi->oxcf.rc_cfg.mode != AOM_CBR ||
816*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20))
817*77c1e3ccSAndroid Build Coastguard Worker rcf = rate_correction_factors_gfarfstd;
818*77c1e3ccSAndroid Build Coastguard Worker else
819*77c1e3ccSAndroid Build Coastguard Worker rcf = rate_correction_factors_internormal;
820*77c1e3ccSAndroid Build Coastguard Worker }
821*77c1e3ccSAndroid Build Coastguard Worker rcf *= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
822*77c1e3ccSAndroid Build Coastguard Worker return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
823*77c1e3ccSAndroid Build Coastguard Worker }
824*77c1e3ccSAndroid Build Coastguard Worker
825*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Sets a rate vs Q correction factor
826*77c1e3ccSAndroid Build Coastguard Worker *
827*77c1e3ccSAndroid Build Coastguard Worker * This function updates the current value of a correction factor used to
828*77c1e3ccSAndroid Build Coastguard Worker * dynamically adjust the relationship between Q and the expected number
829*77c1e3ccSAndroid Build Coastguard Worker * of bits for the frame.
830*77c1e3ccSAndroid Build Coastguard Worker *
831*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
832*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder instance structure
833*77c1e3ccSAndroid Build Coastguard Worker * \param[in] is_encode_stage Indicates if recode loop or post-encode
834*77c1e3ccSAndroid Build Coastguard Worker * \param[in] factor New correction factor
835*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Frame width
836*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Frame height
837*77c1e3ccSAndroid Build Coastguard Worker *
838*77c1e3ccSAndroid Build Coastguard Worker * \remark Updates the rate correction factor for the
839*77c1e3ccSAndroid Build Coastguard Worker * current frame type in cpi->rc.
840*77c1e3ccSAndroid Build Coastguard Worker */
set_rate_correction_factor(AV1_COMP * cpi,int is_encode_stage,double factor,int width,int height)841*77c1e3ccSAndroid Build Coastguard Worker static void set_rate_correction_factor(AV1_COMP *cpi, int is_encode_stage,
842*77c1e3ccSAndroid Build Coastguard Worker double factor, int width, int height) {
843*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
844*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
845*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
846*77c1e3ccSAndroid Build Coastguard Worker int update_default_rcf = 1;
847*77c1e3ccSAndroid Build Coastguard Worker // Normalize RCF to account for the size-dependent scaling factor.
848*77c1e3ccSAndroid Build Coastguard Worker factor /= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
849*77c1e3ccSAndroid Build Coastguard Worker
850*77c1e3ccSAndroid Build Coastguard Worker factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
851*77c1e3ccSAndroid Build Coastguard Worker
852*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_type == KEY_FRAME) {
853*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[KF_STD] = factor;
854*77c1e3ccSAndroid Build Coastguard Worker } else if (is_stat_consumption_stage(cpi)) {
855*77c1e3ccSAndroid Build Coastguard Worker const RATE_FACTOR_LEVEL rf_lvl =
856*77c1e3ccSAndroid Build Coastguard Worker get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index);
857*77c1e3ccSAndroid Build Coastguard Worker if (is_encode_stage &&
858*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
859*77c1e3ccSAndroid Build Coastguard Worker rc->frame_level_rate_correction_factors[rf_lvl] = factor;
860*77c1e3ccSAndroid Build Coastguard Worker update_default_rcf = 0;
861*77c1e3ccSAndroid Build Coastguard Worker }
862*77c1e3ccSAndroid Build Coastguard Worker if (update_default_rcf) p_rc->rate_correction_factors[rf_lvl] = factor;
863*77c1e3ccSAndroid Build Coastguard Worker } else {
864*77c1e3ccSAndroid Build Coastguard Worker if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) &&
865*77c1e3ccSAndroid Build Coastguard Worker !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
866*77c1e3ccSAndroid Build Coastguard Worker (cpi->oxcf.rc_cfg.mode != AOM_CBR ||
867*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20)) {
868*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[GF_ARF_STD] = factor;
869*77c1e3ccSAndroid Build Coastguard Worker } else {
870*77c1e3ccSAndroid Build Coastguard Worker if (is_encode_stage &&
871*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
872*77c1e3ccSAndroid Build Coastguard Worker rc->frame_level_rate_correction_factors[INTER_NORMAL] = factor;
873*77c1e3ccSAndroid Build Coastguard Worker update_default_rcf = 0;
874*77c1e3ccSAndroid Build Coastguard Worker }
875*77c1e3ccSAndroid Build Coastguard Worker if (update_default_rcf)
876*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[INTER_NORMAL] = factor;
877*77c1e3ccSAndroid Build Coastguard Worker }
878*77c1e3ccSAndroid Build Coastguard Worker }
879*77c1e3ccSAndroid Build Coastguard Worker }
880*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_update_rate_correction_factors(AV1_COMP * cpi,int is_encode_stage,int width,int height)881*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int is_encode_stage,
882*77c1e3ccSAndroid Build Coastguard Worker int width, int height) {
883*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
884*77c1e3ccSAndroid Build Coastguard Worker double correction_factor = 1.0;
885*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factor =
886*77c1e3ccSAndroid Build Coastguard Worker get_rate_correction_factor(cpi, width, height);
887*77c1e3ccSAndroid Build Coastguard Worker double adjustment_limit;
888*77c1e3ccSAndroid Build Coastguard Worker int projected_size_based_on_q = 0;
889*77c1e3ccSAndroid Build Coastguard Worker int cyclic_refresh_active =
890*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled;
891*77c1e3ccSAndroid Build Coastguard Worker
892*77c1e3ccSAndroid Build Coastguard Worker // Do not update the rate factors for arf overlay frames.
893*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.is_src_frame_alt_ref) return;
894*77c1e3ccSAndroid Build Coastguard Worker
895*77c1e3ccSAndroid Build Coastguard Worker // Don't update rate correction factors here on scene changes as
896*77c1e3ccSAndroid Build Coastguard Worker // it is already reset in av1_encodedframe_overshoot_cbr(),
897*77c1e3ccSAndroid Build Coastguard Worker // but reset variables related to previous frame q and size.
898*77c1e3ccSAndroid Build Coastguard Worker // Note that the counter of frames since the last scene change
899*77c1e3ccSAndroid Build Coastguard Worker // is only valid when cyclic refresh mode is enabled and that
900*77c1e3ccSAndroid Build Coastguard Worker // this break out only applies to scene changes that are not
901*77c1e3ccSAndroid Build Coastguard Worker // recorded as INTRA only key frames.
902*77c1e3ccSAndroid Build Coastguard Worker if ((cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) &&
903*77c1e3ccSAndroid Build Coastguard Worker (cpi->cyclic_refresh->counter_encode_maxq_scene_change == 0) &&
904*77c1e3ccSAndroid Build Coastguard Worker !frame_is_intra_only(cm) && !cpi->ppi->use_svc) {
905*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.q_2_frame = cm->quant_params.base_qindex;
906*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.q_1_frame = cm->quant_params.base_qindex;
907*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_2_frame = 0;
908*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 0;
909*77c1e3ccSAndroid Build Coastguard Worker return;
910*77c1e3ccSAndroid Build Coastguard Worker }
911*77c1e3ccSAndroid Build Coastguard Worker
912*77c1e3ccSAndroid Build Coastguard Worker // Clear down mmx registers to allow floating point in what follows
913*77c1e3ccSAndroid Build Coastguard Worker
914*77c1e3ccSAndroid Build Coastguard Worker // Work out how big we would have expected the frame to be at this Q given
915*77c1e3ccSAndroid Build Coastguard Worker // the current correction factor.
916*77c1e3ccSAndroid Build Coastguard Worker // Stay in double to avoid int overflow when values are large
917*77c1e3ccSAndroid Build Coastguard Worker if (cyclic_refresh_active) {
918*77c1e3ccSAndroid Build Coastguard Worker projected_size_based_on_q =
919*77c1e3ccSAndroid Build Coastguard Worker av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
920*77c1e3ccSAndroid Build Coastguard Worker } else {
921*77c1e3ccSAndroid Build Coastguard Worker projected_size_based_on_q = av1_estimate_bits_at_q(
922*77c1e3ccSAndroid Build Coastguard Worker cpi, cm->quant_params.base_qindex, rate_correction_factor);
923*77c1e3ccSAndroid Build Coastguard Worker }
924*77c1e3ccSAndroid Build Coastguard Worker // Work out a size correction factor.
925*77c1e3ccSAndroid Build Coastguard Worker if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
926*77c1e3ccSAndroid Build Coastguard Worker correction_factor = (double)cpi->rc.projected_frame_size /
927*77c1e3ccSAndroid Build Coastguard Worker (double)projected_size_based_on_q;
928*77c1e3ccSAndroid Build Coastguard Worker
929*77c1e3ccSAndroid Build Coastguard Worker // Clamp correction factor to prevent anything too extreme
930*77c1e3ccSAndroid Build Coastguard Worker correction_factor = AOMMAX(correction_factor, 0.25);
931*77c1e3ccSAndroid Build Coastguard Worker
932*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.q_2_frame = cpi->rc.q_1_frame;
933*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.q_1_frame = cm->quant_params.base_qindex;
934*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
935*77c1e3ccSAndroid Build Coastguard Worker if (correction_factor > 1.1)
936*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = -1;
937*77c1e3ccSAndroid Build Coastguard Worker else if (correction_factor < 0.9)
938*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 1;
939*77c1e3ccSAndroid Build Coastguard Worker else
940*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 0;
941*77c1e3ccSAndroid Build Coastguard Worker
942*77c1e3ccSAndroid Build Coastguard Worker // Decide how heavily to dampen the adjustment
943*77c1e3ccSAndroid Build Coastguard Worker if (correction_factor > 0.0) {
944*77c1e3ccSAndroid Build Coastguard Worker if (cpi->is_screen_content_type) {
945*77c1e3ccSAndroid Build Coastguard Worker adjustment_limit =
946*77c1e3ccSAndroid Build Coastguard Worker 0.25 + 0.5 * AOMMIN(0.5, fabs(log10(correction_factor)));
947*77c1e3ccSAndroid Build Coastguard Worker } else {
948*77c1e3ccSAndroid Build Coastguard Worker adjustment_limit =
949*77c1e3ccSAndroid Build Coastguard Worker 0.25 + 0.75 * AOMMIN(0.5, fabs(log10(correction_factor)));
950*77c1e3ccSAndroid Build Coastguard Worker }
951*77c1e3ccSAndroid Build Coastguard Worker } else {
952*77c1e3ccSAndroid Build Coastguard Worker adjustment_limit = 0.75;
953*77c1e3ccSAndroid Build Coastguard Worker }
954*77c1e3ccSAndroid Build Coastguard Worker
955*77c1e3ccSAndroid Build Coastguard Worker // Adjustment to delta Q and number of blocks updated in cyclic refresh
956*77c1e3ccSAndroid Build Coastguard Worker // based on over or under shoot of target in current frame.
957*77c1e3ccSAndroid Build Coastguard Worker if (cyclic_refresh_active && cpi->rc.this_frame_target > 0) {
958*77c1e3ccSAndroid Build Coastguard Worker CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
959*77c1e3ccSAndroid Build Coastguard Worker if (correction_factor > 1.25) {
960*77c1e3ccSAndroid Build Coastguard Worker cr->percent_refresh_adjustment =
961*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(cr->percent_refresh_adjustment - 1, -5);
962*77c1e3ccSAndroid Build Coastguard Worker cr->rate_ratio_qdelta_adjustment =
963*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(cr->rate_ratio_qdelta_adjustment - 0.05, -0.0);
964*77c1e3ccSAndroid Build Coastguard Worker } else if (correction_factor < 0.5) {
965*77c1e3ccSAndroid Build Coastguard Worker cr->percent_refresh_adjustment =
966*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(cr->percent_refresh_adjustment + 1, 5);
967*77c1e3ccSAndroid Build Coastguard Worker cr->rate_ratio_qdelta_adjustment =
968*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(cr->rate_ratio_qdelta_adjustment + 0.05, 0.25);
969*77c1e3ccSAndroid Build Coastguard Worker }
970*77c1e3ccSAndroid Build Coastguard Worker }
971*77c1e3ccSAndroid Build Coastguard Worker
972*77c1e3ccSAndroid Build Coastguard Worker if (correction_factor > 1.01) {
973*77c1e3ccSAndroid Build Coastguard Worker // We are not already at the worst allowable quality
974*77c1e3ccSAndroid Build Coastguard Worker correction_factor = (1.0 + ((correction_factor - 1.0) * adjustment_limit));
975*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor = rate_correction_factor * correction_factor;
976*77c1e3ccSAndroid Build Coastguard Worker // Keep rate_correction_factor within limits
977*77c1e3ccSAndroid Build Coastguard Worker if (rate_correction_factor > MAX_BPB_FACTOR)
978*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor = MAX_BPB_FACTOR;
979*77c1e3ccSAndroid Build Coastguard Worker } else if (correction_factor < 0.99) {
980*77c1e3ccSAndroid Build Coastguard Worker // We are not already at the best allowable quality
981*77c1e3ccSAndroid Build Coastguard Worker correction_factor = 1.0 / correction_factor;
982*77c1e3ccSAndroid Build Coastguard Worker correction_factor = (1.0 + ((correction_factor - 1.0) * adjustment_limit));
983*77c1e3ccSAndroid Build Coastguard Worker correction_factor = 1.0 / correction_factor;
984*77c1e3ccSAndroid Build Coastguard Worker
985*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor = rate_correction_factor * correction_factor;
986*77c1e3ccSAndroid Build Coastguard Worker
987*77c1e3ccSAndroid Build Coastguard Worker // Keep rate_correction_factor within limits
988*77c1e3ccSAndroid Build Coastguard Worker if (rate_correction_factor < MIN_BPB_FACTOR)
989*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor = MIN_BPB_FACTOR;
990*77c1e3ccSAndroid Build Coastguard Worker }
991*77c1e3ccSAndroid Build Coastguard Worker
992*77c1e3ccSAndroid Build Coastguard Worker set_rate_correction_factor(cpi, is_encode_stage, rate_correction_factor,
993*77c1e3ccSAndroid Build Coastguard Worker width, height);
994*77c1e3ccSAndroid Build Coastguard Worker }
995*77c1e3ccSAndroid Build Coastguard Worker
996*77c1e3ccSAndroid Build Coastguard Worker // Calculate rate for the given 'q'.
get_bits_per_mb(const AV1_COMP * cpi,int use_cyclic_refresh,double correction_factor,int q)997*77c1e3ccSAndroid Build Coastguard Worker static int get_bits_per_mb(const AV1_COMP *cpi, int use_cyclic_refresh,
998*77c1e3ccSAndroid Build Coastguard Worker double correction_factor, int q) {
999*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1000*77c1e3ccSAndroid Build Coastguard Worker return use_cyclic_refresh
1001*77c1e3ccSAndroid Build Coastguard Worker ? av1_cyclic_refresh_rc_bits_per_mb(cpi, q, correction_factor)
1002*77c1e3ccSAndroid Build Coastguard Worker : av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type, q,
1003*77c1e3ccSAndroid Build Coastguard Worker correction_factor,
1004*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.hl_sf.accurate_bit_estimate);
1005*77c1e3ccSAndroid Build Coastguard Worker }
1006*77c1e3ccSAndroid Build Coastguard Worker
1007*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Searches for a Q index value predicted to give an average macro
1008*77c1e3ccSAndroid Build Coastguard Worker * block rate closest to the target value.
1009*77c1e3ccSAndroid Build Coastguard Worker *
1010*77c1e3ccSAndroid Build Coastguard Worker * Similar to find_qindex_by_rate() function, but returns a q index with a
1011*77c1e3ccSAndroid Build Coastguard Worker * rate just above or below the desired rate, depending on which of the two
1012*77c1e3ccSAndroid Build Coastguard Worker * rates is closer to the desired rate.
1013*77c1e3ccSAndroid Build Coastguard Worker * Also, respects the selected aq_mode when computing the rate.
1014*77c1e3ccSAndroid Build Coastguard Worker *
1015*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
1016*77c1e3ccSAndroid Build Coastguard Worker * \param[in] desired_bits_per_mb Target bits per mb
1017*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder instance structure
1018*77c1e3ccSAndroid Build Coastguard Worker * \param[in] correction_factor Current Q to rate correction factor
1019*77c1e3ccSAndroid Build Coastguard Worker * \param[in] best_qindex Min allowed Q value.
1020*77c1e3ccSAndroid Build Coastguard Worker * \param[in] worst_qindex Max allowed Q value.
1021*77c1e3ccSAndroid Build Coastguard Worker *
1022*77c1e3ccSAndroid Build Coastguard Worker * \return Returns a correction factor for the current frame
1023*77c1e3ccSAndroid Build Coastguard Worker */
find_closest_qindex_by_rate(int desired_bits_per_mb,const AV1_COMP * cpi,double correction_factor,int best_qindex,int worst_qindex)1024*77c1e3ccSAndroid Build Coastguard Worker static int find_closest_qindex_by_rate(int desired_bits_per_mb,
1025*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *cpi,
1026*77c1e3ccSAndroid Build Coastguard Worker double correction_factor,
1027*77c1e3ccSAndroid Build Coastguard Worker int best_qindex, int worst_qindex) {
1028*77c1e3ccSAndroid Build Coastguard Worker const int use_cyclic_refresh = cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
1029*77c1e3ccSAndroid Build Coastguard Worker cpi->cyclic_refresh->apply_cyclic_refresh;
1030*77c1e3ccSAndroid Build Coastguard Worker
1031*77c1e3ccSAndroid Build Coastguard Worker // Find 'qindex' based on 'desired_bits_per_mb'.
1032*77c1e3ccSAndroid Build Coastguard Worker assert(best_qindex <= worst_qindex);
1033*77c1e3ccSAndroid Build Coastguard Worker int low = best_qindex;
1034*77c1e3ccSAndroid Build Coastguard Worker int high = worst_qindex;
1035*77c1e3ccSAndroid Build Coastguard Worker while (low < high) {
1036*77c1e3ccSAndroid Build Coastguard Worker const int mid = (low + high) >> 1;
1037*77c1e3ccSAndroid Build Coastguard Worker const int mid_bits_per_mb =
1038*77c1e3ccSAndroid Build Coastguard Worker get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, mid);
1039*77c1e3ccSAndroid Build Coastguard Worker if (mid_bits_per_mb > desired_bits_per_mb) {
1040*77c1e3ccSAndroid Build Coastguard Worker low = mid + 1;
1041*77c1e3ccSAndroid Build Coastguard Worker } else {
1042*77c1e3ccSAndroid Build Coastguard Worker high = mid;
1043*77c1e3ccSAndroid Build Coastguard Worker }
1044*77c1e3ccSAndroid Build Coastguard Worker }
1045*77c1e3ccSAndroid Build Coastguard Worker assert(low == high);
1046*77c1e3ccSAndroid Build Coastguard Worker
1047*77c1e3ccSAndroid Build Coastguard Worker // Calculate rate difference of this q index from the desired rate.
1048*77c1e3ccSAndroid Build Coastguard Worker const int curr_q = low;
1049*77c1e3ccSAndroid Build Coastguard Worker const int curr_bits_per_mb =
1050*77c1e3ccSAndroid Build Coastguard Worker get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, curr_q);
1051*77c1e3ccSAndroid Build Coastguard Worker const int curr_bit_diff = (curr_bits_per_mb <= desired_bits_per_mb)
1052*77c1e3ccSAndroid Build Coastguard Worker ? desired_bits_per_mb - curr_bits_per_mb
1053*77c1e3ccSAndroid Build Coastguard Worker : INT_MAX;
1054*77c1e3ccSAndroid Build Coastguard Worker assert((curr_bit_diff != INT_MAX && curr_bit_diff >= 0) ||
1055*77c1e3ccSAndroid Build Coastguard Worker curr_q == worst_qindex);
1056*77c1e3ccSAndroid Build Coastguard Worker
1057*77c1e3ccSAndroid Build Coastguard Worker // Calculate rate difference for previous q index too.
1058*77c1e3ccSAndroid Build Coastguard Worker const int prev_q = curr_q - 1;
1059*77c1e3ccSAndroid Build Coastguard Worker int prev_bit_diff;
1060*77c1e3ccSAndroid Build Coastguard Worker if (curr_bit_diff == INT_MAX || curr_q == best_qindex) {
1061*77c1e3ccSAndroid Build Coastguard Worker prev_bit_diff = INT_MAX;
1062*77c1e3ccSAndroid Build Coastguard Worker } else {
1063*77c1e3ccSAndroid Build Coastguard Worker const int prev_bits_per_mb =
1064*77c1e3ccSAndroid Build Coastguard Worker get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, prev_q);
1065*77c1e3ccSAndroid Build Coastguard Worker assert(prev_bits_per_mb > desired_bits_per_mb);
1066*77c1e3ccSAndroid Build Coastguard Worker prev_bit_diff = prev_bits_per_mb - desired_bits_per_mb;
1067*77c1e3ccSAndroid Build Coastguard Worker }
1068*77c1e3ccSAndroid Build Coastguard Worker
1069*77c1e3ccSAndroid Build Coastguard Worker // Pick one of the two q indices, depending on which one has rate closer to
1070*77c1e3ccSAndroid Build Coastguard Worker // the desired rate.
1071*77c1e3ccSAndroid Build Coastguard Worker return (curr_bit_diff <= prev_bit_diff) ? curr_q : prev_q;
1072*77c1e3ccSAndroid Build Coastguard Worker }
1073*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_regulate_q(const AV1_COMP * cpi,int target_bits_per_frame,int active_best_quality,int active_worst_quality,int width,int height)1074*77c1e3ccSAndroid Build Coastguard Worker int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
1075*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality, int active_worst_quality,
1076*77c1e3ccSAndroid Build Coastguard Worker int width, int height) {
1077*77c1e3ccSAndroid Build Coastguard Worker const int MBs = av1_get_MBs(width, height);
1078*77c1e3ccSAndroid Build Coastguard Worker const double correction_factor =
1079*77c1e3ccSAndroid Build Coastguard Worker get_rate_correction_factor(cpi, width, height);
1080*77c1e3ccSAndroid Build Coastguard Worker const int target_bits_per_mb =
1081*77c1e3ccSAndroid Build Coastguard Worker (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / MBs);
1082*77c1e3ccSAndroid Build Coastguard Worker
1083*77c1e3ccSAndroid Build Coastguard Worker int q =
1084*77c1e3ccSAndroid Build Coastguard Worker find_closest_qindex_by_rate(target_bits_per_mb, cpi, correction_factor,
1085*77c1e3ccSAndroid Build Coastguard Worker active_best_quality, active_worst_quality);
1086*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_CBR && has_no_stats_stage(cpi))
1087*77c1e3ccSAndroid Build Coastguard Worker return adjust_q_cbr(cpi, q, active_worst_quality, width, height);
1088*77c1e3ccSAndroid Build Coastguard Worker
1089*77c1e3ccSAndroid Build Coastguard Worker return q;
1090*77c1e3ccSAndroid Build Coastguard Worker }
1091*77c1e3ccSAndroid Build Coastguard Worker
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)1092*77c1e3ccSAndroid Build Coastguard Worker static int get_active_quality(int q, int gfu_boost, int low, int high,
1093*77c1e3ccSAndroid Build Coastguard Worker int *low_motion_minq, int *high_motion_minq) {
1094*77c1e3ccSAndroid Build Coastguard Worker if (gfu_boost > high) {
1095*77c1e3ccSAndroid Build Coastguard Worker return low_motion_minq[q];
1096*77c1e3ccSAndroid Build Coastguard Worker } else if (gfu_boost < low) {
1097*77c1e3ccSAndroid Build Coastguard Worker return high_motion_minq[q];
1098*77c1e3ccSAndroid Build Coastguard Worker } else {
1099*77c1e3ccSAndroid Build Coastguard Worker const int gap = high - low;
1100*77c1e3ccSAndroid Build Coastguard Worker const int offset = high - gfu_boost;
1101*77c1e3ccSAndroid Build Coastguard Worker const int qdiff = high_motion_minq[q] - low_motion_minq[q];
1102*77c1e3ccSAndroid Build Coastguard Worker const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
1103*77c1e3ccSAndroid Build Coastguard Worker return low_motion_minq[q] + adjustment;
1104*77c1e3ccSAndroid Build Coastguard Worker }
1105*77c1e3ccSAndroid Build Coastguard Worker }
1106*77c1e3ccSAndroid Build Coastguard Worker
get_kf_active_quality(const PRIMARY_RATE_CONTROL * const p_rc,int q,aom_bit_depth_t bit_depth)1107*77c1e3ccSAndroid Build Coastguard Worker static int get_kf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
1108*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
1109*77c1e3ccSAndroid Build Coastguard Worker int *kf_low_motion_minq;
1110*77c1e3ccSAndroid Build Coastguard Worker int *kf_high_motion_minq;
1111*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
1112*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
1113*77c1e3ccSAndroid Build Coastguard Worker return get_active_quality(q, p_rc->kf_boost, kf_low, kf_high,
1114*77c1e3ccSAndroid Build Coastguard Worker kf_low_motion_minq, kf_high_motion_minq);
1115*77c1e3ccSAndroid Build Coastguard Worker }
1116*77c1e3ccSAndroid Build Coastguard Worker
get_gf_active_quality_no_rc(int gfu_boost,int q,aom_bit_depth_t bit_depth)1117*77c1e3ccSAndroid Build Coastguard Worker static int get_gf_active_quality_no_rc(int gfu_boost, int q,
1118*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
1119*77c1e3ccSAndroid Build Coastguard Worker int *arfgf_low_motion_minq;
1120*77c1e3ccSAndroid Build Coastguard Worker int *arfgf_high_motion_minq;
1121*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
1122*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
1123*77c1e3ccSAndroid Build Coastguard Worker return get_active_quality(q, gfu_boost, gf_low, gf_high,
1124*77c1e3ccSAndroid Build Coastguard Worker arfgf_low_motion_minq, arfgf_high_motion_minq);
1125*77c1e3ccSAndroid Build Coastguard Worker }
1126*77c1e3ccSAndroid Build Coastguard Worker
get_gf_active_quality(const PRIMARY_RATE_CONTROL * const p_rc,int q,aom_bit_depth_t bit_depth)1127*77c1e3ccSAndroid Build Coastguard Worker static int get_gf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
1128*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
1129*77c1e3ccSAndroid Build Coastguard Worker return get_gf_active_quality_no_rc(p_rc->gfu_boost, q, bit_depth);
1130*77c1e3ccSAndroid Build Coastguard Worker }
1131*77c1e3ccSAndroid Build Coastguard Worker
get_gf_high_motion_quality(int q,aom_bit_depth_t bit_depth)1132*77c1e3ccSAndroid Build Coastguard Worker static int get_gf_high_motion_quality(int q, aom_bit_depth_t bit_depth) {
1133*77c1e3ccSAndroid Build Coastguard Worker int *arfgf_high_motion_minq;
1134*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
1135*77c1e3ccSAndroid Build Coastguard Worker return arfgf_high_motion_minq[q];
1136*77c1e3ccSAndroid Build Coastguard Worker }
1137*77c1e3ccSAndroid Build Coastguard Worker
calc_active_worst_quality_no_stats_vbr(const AV1_COMP * cpi)1138*77c1e3ccSAndroid Build Coastguard Worker static int calc_active_worst_quality_no_stats_vbr(const AV1_COMP *cpi) {
1139*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1140*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1141*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1142*77c1e3ccSAndroid Build Coastguard Worker const unsigned int curr_frame = cpi->common.current_frame.frame_number;
1143*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality;
1144*77c1e3ccSAndroid Build Coastguard Worker int last_q_key_frame;
1145*77c1e3ccSAndroid Build Coastguard Worker int last_q_inter_frame;
1146*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1147*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1148*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1149*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1150*77c1e3ccSAndroid Build Coastguard Worker last_q_key_frame = simulate_parallel_frame ? p_rc->temp_last_q[KEY_FRAME]
1151*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_q[KEY_FRAME];
1152*77c1e3ccSAndroid Build Coastguard Worker last_q_inter_frame = simulate_parallel_frame ? p_rc->temp_last_q[INTER_FRAME]
1153*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_q[INTER_FRAME];
1154*77c1e3ccSAndroid Build Coastguard Worker #else
1155*77c1e3ccSAndroid Build Coastguard Worker last_q_key_frame = p_rc->last_q[KEY_FRAME];
1156*77c1e3ccSAndroid Build Coastguard Worker last_q_inter_frame = p_rc->last_q[INTER_FRAME];
1157*77c1e3ccSAndroid Build Coastguard Worker #endif
1158*77c1e3ccSAndroid Build Coastguard Worker
1159*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_type == KEY_FRAME) {
1160*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1161*77c1e3ccSAndroid Build Coastguard Worker curr_frame == 0 ? rc->worst_quality : last_q_key_frame * 2;
1162*77c1e3ccSAndroid Build Coastguard Worker } else {
1163*77c1e3ccSAndroid Build Coastguard Worker if (!rc->is_src_frame_alt_ref &&
1164*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || refresh_frame->bwd_ref_frame ||
1165*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame)) {
1166*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1167*77c1e3ccSAndroid Build Coastguard Worker curr_frame == 1 ? last_q_key_frame * 5 / 4 : last_q_inter_frame;
1168*77c1e3ccSAndroid Build Coastguard Worker } else {
1169*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1170*77c1e3ccSAndroid Build Coastguard Worker curr_frame == 1 ? last_q_key_frame * 2 : last_q_inter_frame * 2;
1171*77c1e3ccSAndroid Build Coastguard Worker }
1172*77c1e3ccSAndroid Build Coastguard Worker }
1173*77c1e3ccSAndroid Build Coastguard Worker return AOMMIN(active_worst_quality, rc->worst_quality);
1174*77c1e3ccSAndroid Build Coastguard Worker }
1175*77c1e3ccSAndroid Build Coastguard Worker
1176*77c1e3ccSAndroid Build Coastguard Worker // Adjust active_worst_quality level based on buffer level.
calc_active_worst_quality_no_stats_cbr(const AV1_COMP * cpi)1177*77c1e3ccSAndroid Build Coastguard Worker static int calc_active_worst_quality_no_stats_cbr(const AV1_COMP *cpi) {
1178*77c1e3ccSAndroid Build Coastguard Worker // Adjust active_worst_quality: If buffer is above the optimal/target level,
1179*77c1e3ccSAndroid Build Coastguard Worker // bring active_worst_quality down depending on fullness of buffer.
1180*77c1e3ccSAndroid Build Coastguard Worker // If buffer is below the optimal level, let the active_worst_quality go from
1181*77c1e3ccSAndroid Build Coastguard Worker // ambient Q (at buffer = optimal level) to worst_quality level
1182*77c1e3ccSAndroid Build Coastguard Worker // (at buffer = critical level).
1183*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1184*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
1185*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
1186*77c1e3ccSAndroid Build Coastguard Worker const SVC *const svc = &cpi->svc;
1187*77c1e3ccSAndroid Build Coastguard Worker unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
1188*77c1e3ccSAndroid Build Coastguard Worker // Buffer level below which we push active_worst to worst_quality.
1189*77c1e3ccSAndroid Build Coastguard Worker int64_t critical_level = p_rc->optimal_buffer_level >> 3;
1190*77c1e3ccSAndroid Build Coastguard Worker int64_t buff_lvl_step = 0;
1191*77c1e3ccSAndroid Build Coastguard Worker int adjustment = 0;
1192*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality;
1193*77c1e3ccSAndroid Build Coastguard Worker int ambient_qp;
1194*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) return rc->worst_quality;
1195*77c1e3ccSAndroid Build Coastguard Worker // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
1196*77c1e3ccSAndroid Build Coastguard Worker // for the first few frames following key frame. These are both initialized
1197*77c1e3ccSAndroid Build Coastguard Worker // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
1198*77c1e3ccSAndroid Build Coastguard Worker // So for first few frames following key, the qp of that key frame is weighted
1199*77c1e3ccSAndroid Build Coastguard Worker // into the active_worst_quality setting. For SVC the key frame should
1200*77c1e3ccSAndroid Build Coastguard Worker // correspond to layer (0, 0), so use that for layer context.
1201*77c1e3ccSAndroid Build Coastguard Worker int avg_qindex_key = p_rc->avg_frame_qindex[KEY_FRAME];
1202*77c1e3ccSAndroid Build Coastguard Worker if (svc->number_temporal_layers > 1) {
1203*77c1e3ccSAndroid Build Coastguard Worker int layer = LAYER_IDS_TO_IDX(0, 0, svc->number_temporal_layers);
1204*77c1e3ccSAndroid Build Coastguard Worker const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1205*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
1206*77c1e3ccSAndroid Build Coastguard Worker avg_qindex_key =
1207*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(lp_rc->avg_frame_qindex[KEY_FRAME], lp_rc->last_q[KEY_FRAME]);
1208*77c1e3ccSAndroid Build Coastguard Worker }
1209*77c1e3ccSAndroid Build Coastguard Worker if (svc->temporal_layer_id > 0 &&
1210*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key < 2 * svc->number_temporal_layers) {
1211*77c1e3ccSAndroid Build Coastguard Worker ambient_qp = avg_qindex_key;
1212*77c1e3ccSAndroid Build Coastguard Worker } else {
1213*77c1e3ccSAndroid Build Coastguard Worker ambient_qp =
1214*77c1e3ccSAndroid Build Coastguard Worker (cm->current_frame.frame_number < num_frames_weight_key)
1215*77c1e3ccSAndroid Build Coastguard Worker ? AOMMIN(p_rc->avg_frame_qindex[INTER_FRAME], avg_qindex_key)
1216*77c1e3ccSAndroid Build Coastguard Worker : p_rc->avg_frame_qindex[INTER_FRAME];
1217*77c1e3ccSAndroid Build Coastguard Worker }
1218*77c1e3ccSAndroid Build Coastguard Worker ambient_qp = AOMMIN(rc->worst_quality, ambient_qp);
1219*77c1e3ccSAndroid Build Coastguard Worker
1220*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->buffer_level > p_rc->optimal_buffer_level) {
1221*77c1e3ccSAndroid Build Coastguard Worker // Adjust down.
1222*77c1e3ccSAndroid Build Coastguard Worker int max_adjustment_down; // Maximum adjustment down for Q
1223*77c1e3ccSAndroid Build Coastguard Worker
1224*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && !cpi->ppi->use_svc &&
1225*77c1e3ccSAndroid Build Coastguard Worker (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN)) {
1226*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp);
1227*77c1e3ccSAndroid Build Coastguard Worker max_adjustment_down = AOMMIN(4, active_worst_quality / 16);
1228*77c1e3ccSAndroid Build Coastguard Worker } else {
1229*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4);
1230*77c1e3ccSAndroid Build Coastguard Worker max_adjustment_down = active_worst_quality / 3;
1231*77c1e3ccSAndroid Build Coastguard Worker }
1232*77c1e3ccSAndroid Build Coastguard Worker
1233*77c1e3ccSAndroid Build Coastguard Worker if (max_adjustment_down) {
1234*77c1e3ccSAndroid Build Coastguard Worker buff_lvl_step =
1235*77c1e3ccSAndroid Build Coastguard Worker ((p_rc->maximum_buffer_size - p_rc->optimal_buffer_level) /
1236*77c1e3ccSAndroid Build Coastguard Worker max_adjustment_down);
1237*77c1e3ccSAndroid Build Coastguard Worker if (buff_lvl_step)
1238*77c1e3ccSAndroid Build Coastguard Worker adjustment = (int)((p_rc->buffer_level - p_rc->optimal_buffer_level) /
1239*77c1e3ccSAndroid Build Coastguard Worker buff_lvl_step);
1240*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality -= adjustment;
1241*77c1e3ccSAndroid Build Coastguard Worker }
1242*77c1e3ccSAndroid Build Coastguard Worker } else if (p_rc->buffer_level > critical_level) {
1243*77c1e3ccSAndroid Build Coastguard Worker // Adjust up from ambient Q.
1244*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp);
1245*77c1e3ccSAndroid Build Coastguard Worker if (critical_level) {
1246*77c1e3ccSAndroid Build Coastguard Worker buff_lvl_step = (p_rc->optimal_buffer_level - critical_level);
1247*77c1e3ccSAndroid Build Coastguard Worker if (buff_lvl_step) {
1248*77c1e3ccSAndroid Build Coastguard Worker adjustment = (int)((rc->worst_quality - ambient_qp) *
1249*77c1e3ccSAndroid Build Coastguard Worker (p_rc->optimal_buffer_level - p_rc->buffer_level) /
1250*77c1e3ccSAndroid Build Coastguard Worker buff_lvl_step);
1251*77c1e3ccSAndroid Build Coastguard Worker }
1252*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality += adjustment;
1253*77c1e3ccSAndroid Build Coastguard Worker }
1254*77c1e3ccSAndroid Build Coastguard Worker } else {
1255*77c1e3ccSAndroid Build Coastguard Worker // Set to worst_quality if buffer is below critical level.
1256*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = rc->worst_quality;
1257*77c1e3ccSAndroid Build Coastguard Worker }
1258*77c1e3ccSAndroid Build Coastguard Worker return active_worst_quality;
1259*77c1e3ccSAndroid Build Coastguard Worker }
1260*77c1e3ccSAndroid Build Coastguard Worker
1261*77c1e3ccSAndroid Build Coastguard Worker // Calculate the active_best_quality level.
calc_active_best_quality_no_stats_cbr(const AV1_COMP * cpi,int active_worst_quality,int width,int height)1262*77c1e3ccSAndroid Build Coastguard Worker static int calc_active_best_quality_no_stats_cbr(const AV1_COMP *cpi,
1263*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality,
1264*77c1e3ccSAndroid Build Coastguard Worker int width, int height) {
1265*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1266*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1267*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1268*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1269*77c1e3ccSAndroid Build Coastguard Worker const CurrentFrame *const current_frame = &cm->current_frame;
1270*77c1e3ccSAndroid Build Coastguard Worker int *rtc_minq;
1271*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
1272*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = rc->best_quality;
1273*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, rtc_minq);
1274*77c1e3ccSAndroid Build Coastguard Worker
1275*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
1276*77c1e3ccSAndroid Build Coastguard Worker // Handle the special case for key frames forced when we have reached
1277*77c1e3ccSAndroid Build Coastguard Worker // the maximum key frame interval. Here force the Q to a range
1278*77c1e3ccSAndroid Build Coastguard Worker // based on the ambient Q to reduce the risk of popping.
1279*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->this_key_frame_forced) {
1280*77c1e3ccSAndroid Build Coastguard Worker int qindex = p_rc->last_boosted_qindex;
1281*77c1e3ccSAndroid Build Coastguard Worker double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1282*77c1e3ccSAndroid Build Coastguard Worker int delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1283*77c1e3ccSAndroid Build Coastguard Worker (last_boosted_q * 0.75), bit_depth);
1284*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1285*77c1e3ccSAndroid Build Coastguard Worker } else if (current_frame->frame_number > 0) {
1286*77c1e3ccSAndroid Build Coastguard Worker // not first frame of one pass and kf_boost is set
1287*77c1e3ccSAndroid Build Coastguard Worker double q_adj_factor = 1.0;
1288*77c1e3ccSAndroid Build Coastguard Worker double q_val;
1289*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_kf_active_quality(
1290*77c1e3ccSAndroid Build Coastguard Worker p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth);
1291*77c1e3ccSAndroid Build Coastguard Worker // Allow somewhat lower kf minq with small image formats.
1292*77c1e3ccSAndroid Build Coastguard Worker if ((width * height) <= (352 * 288)) {
1293*77c1e3ccSAndroid Build Coastguard Worker q_adj_factor -= 0.25;
1294*77c1e3ccSAndroid Build Coastguard Worker }
1295*77c1e3ccSAndroid Build Coastguard Worker // Convert the adjustment factor to a qindex delta
1296*77c1e3ccSAndroid Build Coastguard Worker // on active_best_quality.
1297*77c1e3ccSAndroid Build Coastguard Worker q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
1298*77c1e3ccSAndroid Build Coastguard Worker active_best_quality +=
1299*77c1e3ccSAndroid Build Coastguard Worker av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1300*77c1e3ccSAndroid Build Coastguard Worker }
1301*77c1e3ccSAndroid Build Coastguard Worker } else if (!rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
1302*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.gf_cbr_boost_pct &&
1303*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1304*77c1e3ccSAndroid Build Coastguard Worker // Use the lower of active_worst_quality and recent
1305*77c1e3ccSAndroid Build Coastguard Worker // average Q as basis for GF/ARF best Q limit unless last frame was
1306*77c1e3ccSAndroid Build Coastguard Worker // a key frame.
1307*77c1e3ccSAndroid Build Coastguard Worker int q = active_worst_quality;
1308*77c1e3ccSAndroid Build Coastguard Worker if (rc->frames_since_key > 1 &&
1309*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1310*77c1e3ccSAndroid Build Coastguard Worker q = p_rc->avg_frame_qindex[INTER_FRAME];
1311*77c1e3ccSAndroid Build Coastguard Worker }
1312*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1313*77c1e3ccSAndroid Build Coastguard Worker } else {
1314*77c1e3ccSAndroid Build Coastguard Worker // Use the lower of active_worst_quality and recent/average Q.
1315*77c1e3ccSAndroid Build Coastguard Worker FRAME_TYPE frame_type =
1316*77c1e3ccSAndroid Build Coastguard Worker (current_frame->frame_number > 1) ? INTER_FRAME : KEY_FRAME;
1317*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->avg_frame_qindex[frame_type] < active_worst_quality)
1318*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = rtc_minq[p_rc->avg_frame_qindex[frame_type]];
1319*77c1e3ccSAndroid Build Coastguard Worker else
1320*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = rtc_minq[active_worst_quality];
1321*77c1e3ccSAndroid Build Coastguard Worker }
1322*77c1e3ccSAndroid Build Coastguard Worker return active_best_quality;
1323*77c1e3ccSAndroid Build Coastguard Worker }
1324*77c1e3ccSAndroid Build Coastguard Worker
1325*77c1e3ccSAndroid Build Coastguard Worker #if RT_PASSIVE_STRATEGY
get_q_passive_strategy(const AV1_COMP * const cpi,const int q_candidate,const int threshold)1326*77c1e3ccSAndroid Build Coastguard Worker static int get_q_passive_strategy(const AV1_COMP *const cpi,
1327*77c1e3ccSAndroid Build Coastguard Worker const int q_candidate, const int threshold) {
1328*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1329*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1330*77c1e3ccSAndroid Build Coastguard Worker const CurrentFrame *const current_frame = &cm->current_frame;
1331*77c1e3ccSAndroid Build Coastguard Worker int sum = 0;
1332*77c1e3ccSAndroid Build Coastguard Worker int count = 0;
1333*77c1e3ccSAndroid Build Coastguard Worker int i = 1;
1334*77c1e3ccSAndroid Build Coastguard Worker while (i < MAX_Q_HISTORY) {
1335*77c1e3ccSAndroid Build Coastguard Worker int frame_id = current_frame->frame_number - i;
1336*77c1e3ccSAndroid Build Coastguard Worker if (frame_id <= 0) break;
1337*77c1e3ccSAndroid Build Coastguard Worker sum += p_rc->q_history[frame_id % MAX_Q_HISTORY];
1338*77c1e3ccSAndroid Build Coastguard Worker ++count;
1339*77c1e3ccSAndroid Build Coastguard Worker ++i;
1340*77c1e3ccSAndroid Build Coastguard Worker }
1341*77c1e3ccSAndroid Build Coastguard Worker if (count > 0) {
1342*77c1e3ccSAndroid Build Coastguard Worker const int avg_q = sum / count;
1343*77c1e3ccSAndroid Build Coastguard Worker if (abs(avg_q - q_candidate) <= threshold) return avg_q;
1344*77c1e3ccSAndroid Build Coastguard Worker }
1345*77c1e3ccSAndroid Build Coastguard Worker return q_candidate;
1346*77c1e3ccSAndroid Build Coastguard Worker }
1347*77c1e3ccSAndroid Build Coastguard Worker #endif // RT_PASSIVE_STRATEGY
1348*77c1e3ccSAndroid Build Coastguard Worker
1349*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Picks q and q bounds given CBR rate control parameters in \c cpi->rc.
1350*77c1e3ccSAndroid Build Coastguard Worker *
1351*77c1e3ccSAndroid Build Coastguard Worker * Handles the special case when using:
1352*77c1e3ccSAndroid Build Coastguard Worker * - Constant bit-rate mode: \c cpi->oxcf.rc_cfg.mode == \ref AOM_CBR, and
1353*77c1e3ccSAndroid Build Coastguard Worker * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are
1354*77c1e3ccSAndroid Build Coastguard Worker * NOT available.
1355*77c1e3ccSAndroid Build Coastguard Worker *
1356*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
1357*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
1358*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Coded frame width
1359*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Coded frame height
1360*77c1e3ccSAndroid Build Coastguard Worker * \param[out] bottom_index Bottom bound for q index (best quality)
1361*77c1e3ccSAndroid Build Coastguard Worker * \param[out] top_index Top bound for q index (worst quality)
1362*77c1e3ccSAndroid Build Coastguard Worker * \return Returns selected q index to be used for encoding this frame.
1363*77c1e3ccSAndroid Build Coastguard Worker */
rc_pick_q_and_bounds_no_stats_cbr(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1364*77c1e3ccSAndroid Build Coastguard Worker static int rc_pick_q_and_bounds_no_stats_cbr(const AV1_COMP *cpi, int width,
1365*77c1e3ccSAndroid Build Coastguard Worker int height, int *bottom_index,
1366*77c1e3ccSAndroid Build Coastguard Worker int *top_index) {
1367*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1368*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1369*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1370*77c1e3ccSAndroid Build Coastguard Worker const CurrentFrame *const current_frame = &cm->current_frame;
1371*77c1e3ccSAndroid Build Coastguard Worker int q;
1372*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi);
1373*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = calc_active_best_quality_no_stats_cbr(
1374*77c1e3ccSAndroid Build Coastguard Worker cpi, active_worst_quality, width, height);
1375*77c1e3ccSAndroid Build Coastguard Worker assert(has_no_stats_stage(cpi));
1376*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->oxcf.rc_cfg.mode == AOM_CBR);
1377*77c1e3ccSAndroid Build Coastguard Worker
1378*77c1e3ccSAndroid Build Coastguard Worker // Clip the active best and worst quality values to limits
1379*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1380*77c1e3ccSAndroid Build Coastguard Worker clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1381*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1382*77c1e3ccSAndroid Build Coastguard Worker clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1383*77c1e3ccSAndroid Build Coastguard Worker
1384*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality;
1385*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = active_best_quality;
1386*77c1e3ccSAndroid Build Coastguard Worker
1387*77c1e3ccSAndroid Build Coastguard Worker // Limit Q range for the adaptive loop.
1388*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME && !p_rc->this_key_frame_forced &&
1389*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_number != 0) {
1390*77c1e3ccSAndroid Build Coastguard Worker int qdelta = 0;
1391*77c1e3ccSAndroid Build Coastguard Worker qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1392*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, 2.0);
1393*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality + qdelta;
1394*77c1e3ccSAndroid Build Coastguard Worker *top_index = AOMMAX(*top_index, *bottom_index);
1395*77c1e3ccSAndroid Build Coastguard Worker }
1396*77c1e3ccSAndroid Build Coastguard Worker
1397*77c1e3ccSAndroid Build Coastguard Worker q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1398*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, width, height);
1399*77c1e3ccSAndroid Build Coastguard Worker #if RT_PASSIVE_STRATEGY
1400*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type != KEY_FRAME &&
1401*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1402*77c1e3ccSAndroid Build Coastguard Worker q = get_q_passive_strategy(cpi, q, 50);
1403*77c1e3ccSAndroid Build Coastguard Worker }
1404*77c1e3ccSAndroid Build Coastguard Worker #endif // RT_PASSIVE_STRATEGY
1405*77c1e3ccSAndroid Build Coastguard Worker if (q > *top_index) {
1406*77c1e3ccSAndroid Build Coastguard Worker // Special case when we are targeting the max allowed rate
1407*77c1e3ccSAndroid Build Coastguard Worker if (rc->this_frame_target >= rc->max_frame_bandwidth)
1408*77c1e3ccSAndroid Build Coastguard Worker *top_index = q;
1409*77c1e3ccSAndroid Build Coastguard Worker else
1410*77c1e3ccSAndroid Build Coastguard Worker q = *top_index;
1411*77c1e3ccSAndroid Build Coastguard Worker }
1412*77c1e3ccSAndroid Build Coastguard Worker
1413*77c1e3ccSAndroid Build Coastguard Worker assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1414*77c1e3ccSAndroid Build Coastguard Worker assert(*bottom_index <= rc->worst_quality &&
1415*77c1e3ccSAndroid Build Coastguard Worker *bottom_index >= rc->best_quality);
1416*77c1e3ccSAndroid Build Coastguard Worker assert(q <= rc->worst_quality && q >= rc->best_quality);
1417*77c1e3ccSAndroid Build Coastguard Worker return q;
1418*77c1e3ccSAndroid Build Coastguard Worker }
1419*77c1e3ccSAndroid Build Coastguard Worker
gf_group_pyramid_level(const GF_GROUP * gf_group,int gf_index)1420*77c1e3ccSAndroid Build Coastguard Worker static int gf_group_pyramid_level(const GF_GROUP *gf_group, int gf_index) {
1421*77c1e3ccSAndroid Build Coastguard Worker return gf_group->layer_depth[gf_index];
1422*77c1e3ccSAndroid Build Coastguard Worker }
1423*77c1e3ccSAndroid Build Coastguard Worker
get_active_cq_level(const RATE_CONTROL * rc,const PRIMARY_RATE_CONTROL * p_rc,const AV1EncoderConfig * const oxcf,int intra_only,aom_superres_mode superres_mode,int superres_denom)1424*77c1e3ccSAndroid Build Coastguard Worker static int get_active_cq_level(const RATE_CONTROL *rc,
1425*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *p_rc,
1426*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf,
1427*77c1e3ccSAndroid Build Coastguard Worker int intra_only, aom_superres_mode superres_mode,
1428*77c1e3ccSAndroid Build Coastguard Worker int superres_denom) {
1429*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
1430*77c1e3ccSAndroid Build Coastguard Worker static const double cq_adjust_threshold = 0.1;
1431*77c1e3ccSAndroid Build Coastguard Worker int active_cq_level = rc_cfg->cq_level;
1432*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->mode == AOM_CQ || rc_cfg->mode == AOM_Q) {
1433*77c1e3ccSAndroid Build Coastguard Worker // printf("Superres %d %d %d = %d\n", superres_denom, intra_only,
1434*77c1e3ccSAndroid Build Coastguard Worker // rc->frames_to_key, !(intra_only && rc->frames_to_key <= 1));
1435*77c1e3ccSAndroid Build Coastguard Worker if ((superres_mode == AOM_SUPERRES_QTHRESH ||
1436*77c1e3ccSAndroid Build Coastguard Worker superres_mode == AOM_SUPERRES_AUTO) &&
1437*77c1e3ccSAndroid Build Coastguard Worker superres_denom != SCALE_NUMERATOR) {
1438*77c1e3ccSAndroid Build Coastguard Worker int mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO;
1439*77c1e3ccSAndroid Build Coastguard Worker if (intra_only && rc->frames_to_key <= 1) {
1440*77c1e3ccSAndroid Build Coastguard Worker mult = 0;
1441*77c1e3ccSAndroid Build Coastguard Worker } else if (intra_only) {
1442*77c1e3ccSAndroid Build Coastguard Worker mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME;
1443*77c1e3ccSAndroid Build Coastguard Worker } else {
1444*77c1e3ccSAndroid Build Coastguard Worker mult = SUPERRES_QADJ_PER_DENOM_ARFFRAME;
1445*77c1e3ccSAndroid Build Coastguard Worker }
1446*77c1e3ccSAndroid Build Coastguard Worker active_cq_level = AOMMAX(
1447*77c1e3ccSAndroid Build Coastguard Worker active_cq_level - ((superres_denom - SCALE_NUMERATOR) * mult), 0);
1448*77c1e3ccSAndroid Build Coastguard Worker }
1449*77c1e3ccSAndroid Build Coastguard Worker }
1450*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->mode == AOM_CQ && p_rc->total_target_bits > 0) {
1451*77c1e3ccSAndroid Build Coastguard Worker const double x = (double)p_rc->total_actual_bits / p_rc->total_target_bits;
1452*77c1e3ccSAndroid Build Coastguard Worker if (x < cq_adjust_threshold) {
1453*77c1e3ccSAndroid Build Coastguard Worker active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1454*77c1e3ccSAndroid Build Coastguard Worker }
1455*77c1e3ccSAndroid Build Coastguard Worker }
1456*77c1e3ccSAndroid Build Coastguard Worker return active_cq_level;
1457*77c1e3ccSAndroid Build Coastguard Worker }
1458*77c1e3ccSAndroid Build Coastguard Worker
1459*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Picks q and q bounds given non-CBR rate control params in \c cpi->rc.
1460*77c1e3ccSAndroid Build Coastguard Worker *
1461*77c1e3ccSAndroid Build Coastguard Worker * Handles the special case when using:
1462*77c1e3ccSAndroid Build Coastguard Worker * - Any rate control other than constant bit-rate mode:
1463*77c1e3ccSAndroid Build Coastguard Worker * \c cpi->oxcf.rc_cfg.mode != \ref AOM_CBR, and
1464*77c1e3ccSAndroid Build Coastguard Worker * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are
1465*77c1e3ccSAndroid Build Coastguard Worker * NOT available.
1466*77c1e3ccSAndroid Build Coastguard Worker *
1467*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
1468*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
1469*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Coded frame width
1470*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Coded frame height
1471*77c1e3ccSAndroid Build Coastguard Worker * \param[out] bottom_index Bottom bound for q index (best quality)
1472*77c1e3ccSAndroid Build Coastguard Worker * \param[out] top_index Top bound for q index (worst quality)
1473*77c1e3ccSAndroid Build Coastguard Worker * \return Returns selected q index to be used for encoding this frame.
1474*77c1e3ccSAndroid Build Coastguard Worker */
rc_pick_q_and_bounds_no_stats(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1475*77c1e3ccSAndroid Build Coastguard Worker static int rc_pick_q_and_bounds_no_stats(const AV1_COMP *cpi, int width,
1476*77c1e3ccSAndroid Build Coastguard Worker int height, int *bottom_index,
1477*77c1e3ccSAndroid Build Coastguard Worker int *top_index) {
1478*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1479*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1480*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1481*77c1e3ccSAndroid Build Coastguard Worker const CurrentFrame *const current_frame = &cm->current_frame;
1482*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1483*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1484*77c1e3ccSAndroid Build Coastguard Worker const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode;
1485*77c1e3ccSAndroid Build Coastguard Worker
1486*77c1e3ccSAndroid Build Coastguard Worker assert(has_no_stats_stage(cpi));
1487*77c1e3ccSAndroid Build Coastguard Worker assert(rc_mode == AOM_VBR ||
1488*77c1e3ccSAndroid Build Coastguard Worker (!USE_UNRESTRICTED_Q_IN_CQ_MODE && rc_mode == AOM_CQ) ||
1489*77c1e3ccSAndroid Build Coastguard Worker rc_mode == AOM_Q);
1490*77c1e3ccSAndroid Build Coastguard Worker
1491*77c1e3ccSAndroid Build Coastguard Worker const int cq_level =
1492*77c1e3ccSAndroid Build Coastguard Worker get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
1493*77c1e3ccSAndroid Build Coastguard Worker cpi->superres_mode, cm->superres_scale_denominator);
1494*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
1495*77c1e3ccSAndroid Build Coastguard Worker
1496*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality;
1497*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = calc_active_worst_quality_no_stats_vbr(cpi);
1498*77c1e3ccSAndroid Build Coastguard Worker int q;
1499*77c1e3ccSAndroid Build Coastguard Worker int *inter_minq;
1500*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1501*77c1e3ccSAndroid Build Coastguard Worker
1502*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
1503*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_Q) {
1504*77c1e3ccSAndroid Build Coastguard Worker const int qindex = cq_level;
1505*77c1e3ccSAndroid Build Coastguard Worker const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1506*77c1e3ccSAndroid Build Coastguard Worker const int delta_qindex =
1507*77c1e3ccSAndroid Build Coastguard Worker av1_compute_qdelta(rc, q_val, q_val * 0.25, bit_depth);
1508*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1509*77c1e3ccSAndroid Build Coastguard Worker } else if (p_rc->this_key_frame_forced) {
1510*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1511*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1512*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1513*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1514*77c1e3ccSAndroid Build Coastguard Worker int qindex = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex
1515*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_boosted_qindex;
1516*77c1e3ccSAndroid Build Coastguard Worker #else
1517*77c1e3ccSAndroid Build Coastguard Worker int qindex = p_rc->last_boosted_qindex;
1518*77c1e3ccSAndroid Build Coastguard Worker #endif
1519*77c1e3ccSAndroid Build Coastguard Worker const double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1520*77c1e3ccSAndroid Build Coastguard Worker const int delta_qindex = av1_compute_qdelta(
1521*77c1e3ccSAndroid Build Coastguard Worker rc, last_boosted_q, last_boosted_q * 0.75, bit_depth);
1522*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1523*77c1e3ccSAndroid Build Coastguard Worker } else { // not first frame of one pass and kf_boost is set
1524*77c1e3ccSAndroid Build Coastguard Worker double q_adj_factor = 1.0;
1525*77c1e3ccSAndroid Build Coastguard Worker
1526*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_kf_active_quality(
1527*77c1e3ccSAndroid Build Coastguard Worker p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth);
1528*77c1e3ccSAndroid Build Coastguard Worker
1529*77c1e3ccSAndroid Build Coastguard Worker // Allow somewhat lower kf minq with small image formats.
1530*77c1e3ccSAndroid Build Coastguard Worker if ((width * height) <= (352 * 288)) {
1531*77c1e3ccSAndroid Build Coastguard Worker q_adj_factor -= 0.25;
1532*77c1e3ccSAndroid Build Coastguard Worker }
1533*77c1e3ccSAndroid Build Coastguard Worker
1534*77c1e3ccSAndroid Build Coastguard Worker // Convert the adjustment factor to a qindex delta on active_best_quality.
1535*77c1e3ccSAndroid Build Coastguard Worker {
1536*77c1e3ccSAndroid Build Coastguard Worker const double q_val =
1537*77c1e3ccSAndroid Build Coastguard Worker av1_convert_qindex_to_q(active_best_quality, bit_depth);
1538*77c1e3ccSAndroid Build Coastguard Worker active_best_quality +=
1539*77c1e3ccSAndroid Build Coastguard Worker av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1540*77c1e3ccSAndroid Build Coastguard Worker }
1541*77c1e3ccSAndroid Build Coastguard Worker }
1542*77c1e3ccSAndroid Build Coastguard Worker } else if (!rc->is_src_frame_alt_ref &&
1543*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1544*77c1e3ccSAndroid Build Coastguard Worker // Use the lower of active_worst_quality and recent
1545*77c1e3ccSAndroid Build Coastguard Worker // average Q as basis for GF/ARF best Q limit unless last frame was
1546*77c1e3ccSAndroid Build Coastguard Worker // a key frame.
1547*77c1e3ccSAndroid Build Coastguard Worker q = (rc->frames_since_key > 1 &&
1548*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
1549*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->avg_frame_qindex[INTER_FRAME]
1550*77c1e3ccSAndroid Build Coastguard Worker : p_rc->avg_frame_qindex[KEY_FRAME];
1551*77c1e3ccSAndroid Build Coastguard Worker // For constrained quality don't allow Q less than the cq level
1552*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_CQ) {
1553*77c1e3ccSAndroid Build Coastguard Worker if (q < cq_level) q = cq_level;
1554*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1555*77c1e3ccSAndroid Build Coastguard Worker // Constrained quality use slightly lower active best.
1556*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = active_best_quality * 15 / 16;
1557*77c1e3ccSAndroid Build Coastguard Worker } else if (rc_mode == AOM_Q) {
1558*77c1e3ccSAndroid Build Coastguard Worker const int qindex = cq_level;
1559*77c1e3ccSAndroid Build Coastguard Worker const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1560*77c1e3ccSAndroid Build Coastguard Worker const int delta_qindex =
1561*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->alt_ref_frame)
1562*77c1e3ccSAndroid Build Coastguard Worker ? av1_compute_qdelta(rc, q_val, q_val * 0.40, bit_depth)
1563*77c1e3ccSAndroid Build Coastguard Worker : av1_compute_qdelta(rc, q_val, q_val * 0.50, bit_depth);
1564*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1565*77c1e3ccSAndroid Build Coastguard Worker } else {
1566*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1567*77c1e3ccSAndroid Build Coastguard Worker }
1568*77c1e3ccSAndroid Build Coastguard Worker } else {
1569*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_Q) {
1570*77c1e3ccSAndroid Build Coastguard Worker const int qindex = cq_level;
1571*77c1e3ccSAndroid Build Coastguard Worker const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1572*77c1e3ccSAndroid Build Coastguard Worker const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
1573*77c1e3ccSAndroid Build Coastguard Worker 0.70, 1.0, 0.85, 1.0 };
1574*77c1e3ccSAndroid Build Coastguard Worker const int delta_qindex = av1_compute_qdelta(
1575*77c1e3ccSAndroid Build Coastguard Worker rc, q_val,
1576*77c1e3ccSAndroid Build Coastguard Worker q_val * delta_rate[current_frame->frame_number % FIXED_GF_INTERVAL],
1577*77c1e3ccSAndroid Build Coastguard Worker bit_depth);
1578*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1579*77c1e3ccSAndroid Build Coastguard Worker } else {
1580*77c1e3ccSAndroid Build Coastguard Worker // Use the lower of active_worst_quality and recent/average Q.
1581*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1582*77c1e3ccSAndroid Build Coastguard Worker (current_frame->frame_number > 1)
1583*77c1e3ccSAndroid Build Coastguard Worker ? inter_minq[p_rc->avg_frame_qindex[INTER_FRAME]]
1584*77c1e3ccSAndroid Build Coastguard Worker : inter_minq[p_rc->avg_frame_qindex[KEY_FRAME]];
1585*77c1e3ccSAndroid Build Coastguard Worker // For the constrained quality mode we don't want
1586*77c1e3ccSAndroid Build Coastguard Worker // q to fall below the cq level.
1587*77c1e3ccSAndroid Build Coastguard Worker if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1588*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = cq_level;
1589*77c1e3ccSAndroid Build Coastguard Worker }
1590*77c1e3ccSAndroid Build Coastguard Worker }
1591*77c1e3ccSAndroid Build Coastguard Worker }
1592*77c1e3ccSAndroid Build Coastguard Worker
1593*77c1e3ccSAndroid Build Coastguard Worker // Clip the active best and worst quality values to limits
1594*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1595*77c1e3ccSAndroid Build Coastguard Worker clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1596*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1597*77c1e3ccSAndroid Build Coastguard Worker clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1598*77c1e3ccSAndroid Build Coastguard Worker
1599*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality;
1600*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = active_best_quality;
1601*77c1e3ccSAndroid Build Coastguard Worker
1602*77c1e3ccSAndroid Build Coastguard Worker // Limit Q range for the adaptive loop.
1603*77c1e3ccSAndroid Build Coastguard Worker {
1604*77c1e3ccSAndroid Build Coastguard Worker int qdelta = 0;
1605*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME &&
1606*77c1e3ccSAndroid Build Coastguard Worker !p_rc->this_key_frame_forced && current_frame->frame_number != 0) {
1607*77c1e3ccSAndroid Build Coastguard Worker qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1608*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, 2.0);
1609*77c1e3ccSAndroid Build Coastguard Worker } else if (!rc->is_src_frame_alt_ref &&
1610*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1611*77c1e3ccSAndroid Build Coastguard Worker qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1612*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, 1.75);
1613*77c1e3ccSAndroid Build Coastguard Worker }
1614*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality + qdelta;
1615*77c1e3ccSAndroid Build Coastguard Worker *top_index = AOMMAX(*top_index, *bottom_index);
1616*77c1e3ccSAndroid Build Coastguard Worker }
1617*77c1e3ccSAndroid Build Coastguard Worker
1618*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_Q) {
1619*77c1e3ccSAndroid Build Coastguard Worker q = active_best_quality;
1620*77c1e3ccSAndroid Build Coastguard Worker // Special case code to try and match quality with forced key frames
1621*77c1e3ccSAndroid Build Coastguard Worker } else if ((current_frame->frame_type == KEY_FRAME) &&
1622*77c1e3ccSAndroid Build Coastguard Worker p_rc->this_key_frame_forced) {
1623*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1624*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1625*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1626*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1627*77c1e3ccSAndroid Build Coastguard Worker q = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex
1628*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_boosted_qindex;
1629*77c1e3ccSAndroid Build Coastguard Worker #else
1630*77c1e3ccSAndroid Build Coastguard Worker q = p_rc->last_boosted_qindex;
1631*77c1e3ccSAndroid Build Coastguard Worker #endif
1632*77c1e3ccSAndroid Build Coastguard Worker } else {
1633*77c1e3ccSAndroid Build Coastguard Worker q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1634*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, width, height);
1635*77c1e3ccSAndroid Build Coastguard Worker if (q > *top_index) {
1636*77c1e3ccSAndroid Build Coastguard Worker // Special case when we are targeting the max allowed rate
1637*77c1e3ccSAndroid Build Coastguard Worker if (rc->this_frame_target >= rc->max_frame_bandwidth)
1638*77c1e3ccSAndroid Build Coastguard Worker *top_index = q;
1639*77c1e3ccSAndroid Build Coastguard Worker else
1640*77c1e3ccSAndroid Build Coastguard Worker q = *top_index;
1641*77c1e3ccSAndroid Build Coastguard Worker }
1642*77c1e3ccSAndroid Build Coastguard Worker }
1643*77c1e3ccSAndroid Build Coastguard Worker
1644*77c1e3ccSAndroid Build Coastguard Worker assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1645*77c1e3ccSAndroid Build Coastguard Worker assert(*bottom_index <= rc->worst_quality &&
1646*77c1e3ccSAndroid Build Coastguard Worker *bottom_index >= rc->best_quality);
1647*77c1e3ccSAndroid Build Coastguard Worker assert(q <= rc->worst_quality && q >= rc->best_quality);
1648*77c1e3ccSAndroid Build Coastguard Worker return q;
1649*77c1e3ccSAndroid Build Coastguard Worker }
1650*77c1e3ccSAndroid Build Coastguard Worker
1651*77c1e3ccSAndroid Build Coastguard Worker static const double arf_layer_deltas[MAX_ARF_LAYERS + 1] = { 2.50, 2.00, 1.75,
1652*77c1e3ccSAndroid Build Coastguard Worker 1.50, 1.25, 1.15,
1653*77c1e3ccSAndroid Build Coastguard Worker 1.0 };
frame_type_qdelta(const AV1_COMP * cpi,int q)1654*77c1e3ccSAndroid Build Coastguard Worker static int frame_type_qdelta(const AV1_COMP *cpi, int q) {
1655*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
1656*77c1e3ccSAndroid Build Coastguard Worker const RATE_FACTOR_LEVEL rf_lvl =
1657*77c1e3ccSAndroid Build Coastguard Worker get_rate_factor_level(gf_group, cpi->gf_frame_index);
1658*77c1e3ccSAndroid Build Coastguard Worker const FRAME_TYPE frame_type = gf_group->frame_type[cpi->gf_frame_index];
1659*77c1e3ccSAndroid Build Coastguard Worker const int arf_layer = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
1660*77c1e3ccSAndroid Build Coastguard Worker const double rate_factor =
1661*77c1e3ccSAndroid Build Coastguard Worker (rf_lvl == INTER_NORMAL) ? 1.0 : arf_layer_deltas[arf_layer];
1662*77c1e3ccSAndroid Build Coastguard Worker
1663*77c1e3ccSAndroid Build Coastguard Worker return av1_compute_qdelta_by_rate(cpi, frame_type, q, rate_factor);
1664*77c1e3ccSAndroid Build Coastguard Worker }
1665*77c1e3ccSAndroid Build Coastguard Worker
1666*77c1e3ccSAndroid Build Coastguard Worker // This unrestricted Q selection on CQ mode is useful when testing new features,
1667*77c1e3ccSAndroid Build Coastguard Worker // but may lead to Q being out of range on current RC restrictions
1668*77c1e3ccSAndroid Build Coastguard Worker #if USE_UNRESTRICTED_Q_IN_CQ_MODE
rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1669*77c1e3ccSAndroid Build Coastguard Worker static int rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP *cpi, int width,
1670*77c1e3ccSAndroid Build Coastguard Worker int height, int *bottom_index,
1671*77c1e3ccSAndroid Build Coastguard Worker int *top_index) {
1672*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1673*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1674*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1675*77c1e3ccSAndroid Build Coastguard Worker const int cq_level =
1676*77c1e3ccSAndroid Build Coastguard Worker get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode,
1677*77c1e3ccSAndroid Build Coastguard Worker cm->superres_scale_denominator);
1678*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
1679*77c1e3ccSAndroid Build Coastguard Worker const int q = (int)av1_convert_qindex_to_q(cq_level, bit_depth);
1680*77c1e3ccSAndroid Build Coastguard Worker (void)width;
1681*77c1e3ccSAndroid Build Coastguard Worker (void)height;
1682*77c1e3ccSAndroid Build Coastguard Worker assert(has_no_stats_stage(cpi));
1683*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->oxcf.rc_cfg.mode == AOM_CQ);
1684*77c1e3ccSAndroid Build Coastguard Worker
1685*77c1e3ccSAndroid Build Coastguard Worker *top_index = q;
1686*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = q;
1687*77c1e3ccSAndroid Build Coastguard Worker
1688*77c1e3ccSAndroid Build Coastguard Worker return q;
1689*77c1e3ccSAndroid Build Coastguard Worker }
1690*77c1e3ccSAndroid Build Coastguard Worker #endif // USE_UNRESTRICTED_Q_IN_CQ_MODE
1691*77c1e3ccSAndroid Build Coastguard Worker
1692*77c1e3ccSAndroid Build Coastguard Worker #define STATIC_MOTION_THRESH 95
get_intra_q_and_bounds(const AV1_COMP * cpi,int width,int height,int * active_best,int * active_worst,int cq_level)1693*77c1e3ccSAndroid Build Coastguard Worker static void get_intra_q_and_bounds(const AV1_COMP *cpi, int width, int height,
1694*77c1e3ccSAndroid Build Coastguard Worker int *active_best, int *active_worst,
1695*77c1e3ccSAndroid Build Coastguard Worker int cq_level) {
1696*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1697*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1698*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1699*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1700*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality;
1701*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = *active_worst;
1702*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
1703*77c1e3ccSAndroid Build Coastguard Worker
1704*77c1e3ccSAndroid Build Coastguard Worker if (rc->frames_to_key <= 1 && oxcf->rc_cfg.mode == AOM_Q) {
1705*77c1e3ccSAndroid Build Coastguard Worker // If the next frame is also a key frame or the current frame is the
1706*77c1e3ccSAndroid Build Coastguard Worker // only frame in the sequence in AOM_Q mode, just use the cq_level
1707*77c1e3ccSAndroid Build Coastguard Worker // as q.
1708*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = cq_level;
1709*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = cq_level;
1710*77c1e3ccSAndroid Build Coastguard Worker } else if (p_rc->this_key_frame_forced) {
1711*77c1e3ccSAndroid Build Coastguard Worker // Handle the special case for key frames forced when we have reached
1712*77c1e3ccSAndroid Build Coastguard Worker // the maximum key frame interval. Here force the Q to a range
1713*77c1e3ccSAndroid Build Coastguard Worker // based on the ambient Q to reduce the risk of popping.
1714*77c1e3ccSAndroid Build Coastguard Worker double last_boosted_q;
1715*77c1e3ccSAndroid Build Coastguard Worker int delta_qindex;
1716*77c1e3ccSAndroid Build Coastguard Worker int qindex;
1717*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1718*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1719*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1720*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1721*77c1e3ccSAndroid Build Coastguard Worker int last_boosted_qindex = simulate_parallel_frame
1722*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->temp_last_boosted_qindex
1723*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_boosted_qindex;
1724*77c1e3ccSAndroid Build Coastguard Worker #else
1725*77c1e3ccSAndroid Build Coastguard Worker int last_boosted_qindex = p_rc->last_boosted_qindex;
1726*77c1e3ccSAndroid Build Coastguard Worker #endif
1727*77c1e3ccSAndroid Build Coastguard Worker if (is_stat_consumption_stage_twopass(cpi) &&
1728*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1729*77c1e3ccSAndroid Build Coastguard Worker qindex = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex);
1730*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = qindex;
1731*77c1e3ccSAndroid Build Coastguard Worker last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1732*77c1e3ccSAndroid Build Coastguard Worker delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1733*77c1e3ccSAndroid Build Coastguard Worker last_boosted_q * 1.25, bit_depth);
1734*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1735*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(qindex + delta_qindex, active_worst_quality);
1736*77c1e3ccSAndroid Build Coastguard Worker } else {
1737*77c1e3ccSAndroid Build Coastguard Worker qindex = last_boosted_qindex;
1738*77c1e3ccSAndroid Build Coastguard Worker last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1739*77c1e3ccSAndroid Build Coastguard Worker delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1740*77c1e3ccSAndroid Build Coastguard Worker last_boosted_q * 0.50, bit_depth);
1741*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1742*77c1e3ccSAndroid Build Coastguard Worker }
1743*77c1e3ccSAndroid Build Coastguard Worker } else {
1744*77c1e3ccSAndroid Build Coastguard Worker // Not forced keyframe.
1745*77c1e3ccSAndroid Build Coastguard Worker double q_adj_factor = 1.0;
1746*77c1e3ccSAndroid Build Coastguard Worker double q_val;
1747*77c1e3ccSAndroid Build Coastguard Worker
1748*77c1e3ccSAndroid Build Coastguard Worker // Baseline value derived from active_worst_quality and kf boost.
1749*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1750*77c1e3ccSAndroid Build Coastguard Worker get_kf_active_quality(p_rc, active_worst_quality, bit_depth);
1751*77c1e3ccSAndroid Build Coastguard Worker if (cpi->is_screen_content_type) {
1752*77c1e3ccSAndroid Build Coastguard Worker active_best_quality /= 2;
1753*77c1e3ccSAndroid Build Coastguard Worker }
1754*77c1e3ccSAndroid Build Coastguard Worker
1755*77c1e3ccSAndroid Build Coastguard Worker if (is_stat_consumption_stage_twopass(cpi) &&
1756*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1757*77c1e3ccSAndroid Build Coastguard Worker active_best_quality /= 3;
1758*77c1e3ccSAndroid Build Coastguard Worker }
1759*77c1e3ccSAndroid Build Coastguard Worker
1760*77c1e3ccSAndroid Build Coastguard Worker // Allow somewhat lower kf minq with small image formats.
1761*77c1e3ccSAndroid Build Coastguard Worker if ((width * height) <= (352 * 288)) {
1762*77c1e3ccSAndroid Build Coastguard Worker q_adj_factor -= 0.25;
1763*77c1e3ccSAndroid Build Coastguard Worker }
1764*77c1e3ccSAndroid Build Coastguard Worker
1765*77c1e3ccSAndroid Build Coastguard Worker // Make a further adjustment based on the kf zero motion measure.
1766*77c1e3ccSAndroid Build Coastguard Worker if (is_stat_consumption_stage_twopass(cpi))
1767*77c1e3ccSAndroid Build Coastguard Worker q_adj_factor +=
1768*77c1e3ccSAndroid Build Coastguard Worker 0.05 - (0.001 * (double)cpi->ppi->twopass.kf_zeromotion_pct);
1769*77c1e3ccSAndroid Build Coastguard Worker
1770*77c1e3ccSAndroid Build Coastguard Worker // Convert the adjustment factor to a qindex delta
1771*77c1e3ccSAndroid Build Coastguard Worker // on active_best_quality.
1772*77c1e3ccSAndroid Build Coastguard Worker q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
1773*77c1e3ccSAndroid Build Coastguard Worker active_best_quality +=
1774*77c1e3ccSAndroid Build Coastguard Worker av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1775*77c1e3ccSAndroid Build Coastguard Worker
1776*77c1e3ccSAndroid Build Coastguard Worker // Tweak active_best_quality for AOM_Q mode when superres is on, as this
1777*77c1e3ccSAndroid Build Coastguard Worker // will be used directly as 'q' later.
1778*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->rc_cfg.mode == AOM_Q &&
1779*77c1e3ccSAndroid Build Coastguard Worker (cpi->superres_mode == AOM_SUPERRES_QTHRESH ||
1780*77c1e3ccSAndroid Build Coastguard Worker cpi->superres_mode == AOM_SUPERRES_AUTO) &&
1781*77c1e3ccSAndroid Build Coastguard Worker cm->superres_scale_denominator != SCALE_NUMERATOR) {
1782*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1783*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(active_best_quality -
1784*77c1e3ccSAndroid Build Coastguard Worker ((cm->superres_scale_denominator - SCALE_NUMERATOR) *
1785*77c1e3ccSAndroid Build Coastguard Worker SUPERRES_QADJ_PER_DENOM_KEYFRAME),
1786*77c1e3ccSAndroid Build Coastguard Worker 0);
1787*77c1e3ccSAndroid Build Coastguard Worker }
1788*77c1e3ccSAndroid Build Coastguard Worker }
1789*77c1e3ccSAndroid Build Coastguard Worker *active_best = active_best_quality;
1790*77c1e3ccSAndroid Build Coastguard Worker *active_worst = active_worst_quality;
1791*77c1e3ccSAndroid Build Coastguard Worker }
1792*77c1e3ccSAndroid Build Coastguard Worker
adjust_active_best_and_worst_quality(const AV1_COMP * cpi,const int is_intrl_arf_boost,int * active_worst,int * active_best)1793*77c1e3ccSAndroid Build Coastguard Worker static void adjust_active_best_and_worst_quality(const AV1_COMP *cpi,
1794*77c1e3ccSAndroid Build Coastguard Worker const int is_intrl_arf_boost,
1795*77c1e3ccSAndroid Build Coastguard Worker int *active_worst,
1796*77c1e3ccSAndroid Build Coastguard Worker int *active_best) {
1797*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1798*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1799*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1800*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = *active_best;
1801*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = *active_worst;
1802*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1803*77c1e3ccSAndroid Build Coastguard Worker #endif
1804*77c1e3ccSAndroid Build Coastguard Worker // Extension to max or min Q if undershoot or overshoot is outside
1805*77c1e3ccSAndroid Build Coastguard Worker // the permitted range.
1806*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode != AOM_Q) {
1807*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1808*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1809*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1810*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1811*77c1e3ccSAndroid Build Coastguard Worker const int extend_minq = simulate_parallel_frame
1812*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->temp_extend_minq
1813*77c1e3ccSAndroid Build Coastguard Worker : cpi->ppi->twopass.extend_minq;
1814*77c1e3ccSAndroid Build Coastguard Worker const int extend_maxq = simulate_parallel_frame
1815*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->temp_extend_maxq
1816*77c1e3ccSAndroid Build Coastguard Worker : cpi->ppi->twopass.extend_maxq;
1817*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1818*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm) ||
1819*77c1e3ccSAndroid Build Coastguard Worker (!rc->is_src_frame_alt_ref &&
1820*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || is_intrl_arf_boost ||
1821*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame))) {
1822*77c1e3ccSAndroid Build Coastguard Worker active_best_quality -= extend_minq;
1823*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality += (extend_maxq / 2);
1824*77c1e3ccSAndroid Build Coastguard Worker } else {
1825*77c1e3ccSAndroid Build Coastguard Worker active_best_quality -= extend_minq / 2;
1826*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality += extend_maxq;
1827*77c1e3ccSAndroid Build Coastguard Worker }
1828*77c1e3ccSAndroid Build Coastguard Worker #else
1829*77c1e3ccSAndroid Build Coastguard Worker (void)is_intrl_arf_boost;
1830*77c1e3ccSAndroid Build Coastguard Worker active_best_quality -= cpi->ppi->twopass.extend_minq / 8;
1831*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality += cpi->ppi->twopass.extend_maxq / 4;
1832*77c1e3ccSAndroid Build Coastguard Worker #endif
1833*77c1e3ccSAndroid Build Coastguard Worker }
1834*77c1e3ccSAndroid Build Coastguard Worker
1835*77c1e3ccSAndroid Build Coastguard Worker #ifndef STRICT_RC
1836*77c1e3ccSAndroid Build Coastguard Worker // Static forced key frames Q restrictions dealt with elsewhere.
1837*77c1e3ccSAndroid Build Coastguard Worker if (!(frame_is_intra_only(cm)) || !p_rc->this_key_frame_forced ||
1838*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1839*77c1e3ccSAndroid Build Coastguard Worker const int qdelta = frame_type_qdelta(cpi, active_worst_quality);
1840*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1841*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(active_worst_quality + qdelta, active_best_quality);
1842*77c1e3ccSAndroid Build Coastguard Worker }
1843*77c1e3ccSAndroid Build Coastguard Worker #endif
1844*77c1e3ccSAndroid Build Coastguard Worker
1845*77c1e3ccSAndroid Build Coastguard Worker // Modify active_best_quality for downscaled normal frames.
1846*77c1e3ccSAndroid Build Coastguard Worker if (av1_frame_scaled(cm) && !frame_is_kf_gf_arf(cpi)) {
1847*77c1e3ccSAndroid Build Coastguard Worker int qdelta = av1_compute_qdelta_by_rate(cpi, cm->current_frame.frame_type,
1848*77c1e3ccSAndroid Build Coastguard Worker active_best_quality, 2.0);
1849*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1850*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(active_best_quality + qdelta, rc->best_quality);
1851*77c1e3ccSAndroid Build Coastguard Worker }
1852*77c1e3ccSAndroid Build Coastguard Worker
1853*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
1854*77c1e3ccSAndroid Build Coastguard Worker clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1855*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
1856*77c1e3ccSAndroid Build Coastguard Worker clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1857*77c1e3ccSAndroid Build Coastguard Worker
1858*77c1e3ccSAndroid Build Coastguard Worker *active_best = active_best_quality;
1859*77c1e3ccSAndroid Build Coastguard Worker *active_worst = active_worst_quality;
1860*77c1e3ccSAndroid Build Coastguard Worker }
1861*77c1e3ccSAndroid Build Coastguard Worker
1862*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Gets a Q value to use for the current frame
1863*77c1e3ccSAndroid Build Coastguard Worker *
1864*77c1e3ccSAndroid Build Coastguard Worker *
1865*77c1e3ccSAndroid Build Coastguard Worker * Selects a Q value from a permitted range that we estimate
1866*77c1e3ccSAndroid Build Coastguard Worker * will result in approximately the target number of bits.
1867*77c1e3ccSAndroid Build Coastguard Worker *
1868*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
1869*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder instance structure
1870*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Width of frame
1871*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Height of frame
1872*77c1e3ccSAndroid Build Coastguard Worker * \param[in] active_worst_quality Max Q allowed
1873*77c1e3ccSAndroid Build Coastguard Worker * \param[in] active_best_quality Min Q allowed
1874*77c1e3ccSAndroid Build Coastguard Worker *
1875*77c1e3ccSAndroid Build Coastguard Worker * \return The suggested Q for this frame.
1876*77c1e3ccSAndroid Build Coastguard Worker */
get_q(const AV1_COMP * cpi,const int width,const int height,const int active_worst_quality,const int active_best_quality)1877*77c1e3ccSAndroid Build Coastguard Worker static int get_q(const AV1_COMP *cpi, const int width, const int height,
1878*77c1e3ccSAndroid Build Coastguard Worker const int active_worst_quality,
1879*77c1e3ccSAndroid Build Coastguard Worker const int active_best_quality) {
1880*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1881*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1882*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1883*77c1e3ccSAndroid Build Coastguard Worker int q;
1884*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1885*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
1886*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1887*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg;
1888*77c1e3ccSAndroid Build Coastguard Worker int last_boosted_qindex = simulate_parallel_frame
1889*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->temp_last_boosted_qindex
1890*77c1e3ccSAndroid Build Coastguard Worker : p_rc->last_boosted_qindex;
1891*77c1e3ccSAndroid Build Coastguard Worker #else
1892*77c1e3ccSAndroid Build Coastguard Worker int last_boosted_qindex = p_rc->last_boosted_qindex;
1893*77c1e3ccSAndroid Build Coastguard Worker #endif
1894*77c1e3ccSAndroid Build Coastguard Worker
1895*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_Q ||
1896*77c1e3ccSAndroid Build Coastguard Worker (frame_is_intra_only(cm) && !p_rc->this_key_frame_forced &&
1897*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH &&
1898*77c1e3ccSAndroid Build Coastguard Worker rc->frames_to_key > 1)) {
1899*77c1e3ccSAndroid Build Coastguard Worker q = active_best_quality;
1900*77c1e3ccSAndroid Build Coastguard Worker // Special case code to try and match quality with forced key frames.
1901*77c1e3ccSAndroid Build Coastguard Worker } else if (frame_is_intra_only(cm) && p_rc->this_key_frame_forced) {
1902*77c1e3ccSAndroid Build Coastguard Worker // If static since last kf use better of last boosted and last kf q.
1903*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1904*77c1e3ccSAndroid Build Coastguard Worker q = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex);
1905*77c1e3ccSAndroid Build Coastguard Worker } else {
1906*77c1e3ccSAndroid Build Coastguard Worker q = AOMMIN(last_boosted_qindex,
1907*77c1e3ccSAndroid Build Coastguard Worker (active_best_quality + active_worst_quality) / 2);
1908*77c1e3ccSAndroid Build Coastguard Worker }
1909*77c1e3ccSAndroid Build Coastguard Worker q = clamp(q, active_best_quality, active_worst_quality);
1910*77c1e3ccSAndroid Build Coastguard Worker } else {
1911*77c1e3ccSAndroid Build Coastguard Worker q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1912*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, width, height);
1913*77c1e3ccSAndroid Build Coastguard Worker if (q > active_worst_quality) {
1914*77c1e3ccSAndroid Build Coastguard Worker // Special case when we are targeting the max allowed rate.
1915*77c1e3ccSAndroid Build Coastguard Worker if (rc->this_frame_target < rc->max_frame_bandwidth) {
1916*77c1e3ccSAndroid Build Coastguard Worker q = active_worst_quality;
1917*77c1e3ccSAndroid Build Coastguard Worker }
1918*77c1e3ccSAndroid Build Coastguard Worker }
1919*77c1e3ccSAndroid Build Coastguard Worker q = AOMMAX(q, active_best_quality);
1920*77c1e3ccSAndroid Build Coastguard Worker }
1921*77c1e3ccSAndroid Build Coastguard Worker return q;
1922*77c1e3ccSAndroid Build Coastguard Worker }
1923*77c1e3ccSAndroid Build Coastguard Worker
1924*77c1e3ccSAndroid Build Coastguard Worker // Returns |active_best_quality| for an inter frame.
1925*77c1e3ccSAndroid Build Coastguard Worker // The |active_best_quality| depends on different rate control modes:
1926*77c1e3ccSAndroid Build Coastguard Worker // VBR, Q, CQ, CBR.
1927*77c1e3ccSAndroid Build Coastguard Worker // The returning active_best_quality could further be adjusted in
1928*77c1e3ccSAndroid Build Coastguard Worker // adjust_active_best_and_worst_quality().
get_active_best_quality(const AV1_COMP * const cpi,const int active_worst_quality,const int cq_level,const int gf_index)1929*77c1e3ccSAndroid Build Coastguard Worker static int get_active_best_quality(const AV1_COMP *const cpi,
1930*77c1e3ccSAndroid Build Coastguard Worker const int active_worst_quality,
1931*77c1e3ccSAndroid Build Coastguard Worker const int cq_level, const int gf_index) {
1932*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1933*77c1e3ccSAndroid Build Coastguard Worker const int bit_depth = cm->seq_params->bit_depth;
1934*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
1935*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1936*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1937*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1938*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *gf_group = &cpi->ppi->gf_group;
1939*77c1e3ccSAndroid Build Coastguard Worker const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode;
1940*77c1e3ccSAndroid Build Coastguard Worker int *inter_minq;
1941*77c1e3ccSAndroid Build Coastguard Worker ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1942*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = 0;
1943*77c1e3ccSAndroid Build Coastguard Worker const int is_intrl_arf_boost =
1944*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
1945*77c1e3ccSAndroid Build Coastguard Worker int is_leaf_frame =
1946*77c1e3ccSAndroid Build Coastguard Worker !(gf_group->update_type[gf_index] == ARF_UPDATE ||
1947*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index] == GF_UPDATE || is_intrl_arf_boost);
1948*77c1e3ccSAndroid Build Coastguard Worker
1949*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning): Consider to rework this hack that covers issues incurred
1950*77c1e3ccSAndroid Build Coastguard Worker // in lightfield setting.
1951*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.large_scale) {
1952*77c1e3ccSAndroid Build Coastguard Worker is_leaf_frame = !(refresh_frame->golden_frame ||
1953*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame || is_intrl_arf_boost);
1954*77c1e3ccSAndroid Build Coastguard Worker }
1955*77c1e3ccSAndroid Build Coastguard Worker const int is_overlay_frame = rc->is_src_frame_alt_ref;
1956*77c1e3ccSAndroid Build Coastguard Worker
1957*77c1e3ccSAndroid Build Coastguard Worker if (is_leaf_frame || is_overlay_frame) {
1958*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_Q) return cq_level;
1959*77c1e3ccSAndroid Build Coastguard Worker
1960*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = inter_minq[active_worst_quality];
1961*77c1e3ccSAndroid Build Coastguard Worker // For the constrained quality mode we don't want
1962*77c1e3ccSAndroid Build Coastguard Worker // q to fall below the cq level.
1963*77c1e3ccSAndroid Build Coastguard Worker if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1964*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = cq_level;
1965*77c1e3ccSAndroid Build Coastguard Worker }
1966*77c1e3ccSAndroid Build Coastguard Worker return active_best_quality;
1967*77c1e3ccSAndroid Build Coastguard Worker }
1968*77c1e3ccSAndroid Build Coastguard Worker
1969*77c1e3ccSAndroid Build Coastguard Worker // Determine active_best_quality for frames that are not leaf or overlay.
1970*77c1e3ccSAndroid Build Coastguard Worker int q = active_worst_quality;
1971*77c1e3ccSAndroid Build Coastguard Worker // Use the lower of active_worst_quality and recent
1972*77c1e3ccSAndroid Build Coastguard Worker // average Q as basis for GF/ARF best Q limit unless last frame was
1973*77c1e3ccSAndroid Build Coastguard Worker // a key frame.
1974*77c1e3ccSAndroid Build Coastguard Worker if (rc->frames_since_key > 1 &&
1975*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1976*77c1e3ccSAndroid Build Coastguard Worker q = p_rc->avg_frame_qindex[INTER_FRAME];
1977*77c1e3ccSAndroid Build Coastguard Worker }
1978*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_CQ && q < cq_level) q = cq_level;
1979*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1980*77c1e3ccSAndroid Build Coastguard Worker // Constrained quality use slightly lower active best.
1981*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_CQ) active_best_quality = active_best_quality * 15 / 16;
1982*77c1e3ccSAndroid Build Coastguard Worker const int min_boost = get_gf_high_motion_quality(q, bit_depth);
1983*77c1e3ccSAndroid Build Coastguard Worker const int boost = min_boost - active_best_quality;
1984*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = min_boost - (int)(boost * p_rc->arf_boost_factor);
1985*77c1e3ccSAndroid Build Coastguard Worker if (!is_intrl_arf_boost) return active_best_quality;
1986*77c1e3ccSAndroid Build Coastguard Worker
1987*77c1e3ccSAndroid Build Coastguard Worker if (rc_mode == AOM_Q || rc_mode == AOM_CQ) active_best_quality = p_rc->arf_q;
1988*77c1e3ccSAndroid Build Coastguard Worker int this_height = gf_group_pyramid_level(gf_group, gf_index);
1989*77c1e3ccSAndroid Build Coastguard Worker while (this_height > 1) {
1990*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
1991*77c1e3ccSAndroid Build Coastguard Worker --this_height;
1992*77c1e3ccSAndroid Build Coastguard Worker }
1993*77c1e3ccSAndroid Build Coastguard Worker return active_best_quality;
1994*77c1e3ccSAndroid Build Coastguard Worker }
1995*77c1e3ccSAndroid Build Coastguard Worker
1996*77c1e3ccSAndroid Build Coastguard Worker // Returns the q_index for a single frame in the GOP.
1997*77c1e3ccSAndroid Build Coastguard Worker // This function assumes that rc_mode == AOM_Q mode.
av1_q_mode_get_q_index(int base_q_index,int gf_update_type,int gf_pyramid_level,int arf_q)1998*77c1e3ccSAndroid Build Coastguard Worker int av1_q_mode_get_q_index(int base_q_index, int gf_update_type,
1999*77c1e3ccSAndroid Build Coastguard Worker int gf_pyramid_level, int arf_q) {
2000*77c1e3ccSAndroid Build Coastguard Worker const int is_intrl_arf_boost = gf_update_type == INTNL_ARF_UPDATE;
2001*77c1e3ccSAndroid Build Coastguard Worker int is_leaf_or_overlay_frame = gf_update_type == LF_UPDATE ||
2002*77c1e3ccSAndroid Build Coastguard Worker gf_update_type == OVERLAY_UPDATE ||
2003*77c1e3ccSAndroid Build Coastguard Worker gf_update_type == INTNL_OVERLAY_UPDATE;
2004*77c1e3ccSAndroid Build Coastguard Worker
2005*77c1e3ccSAndroid Build Coastguard Worker if (is_leaf_or_overlay_frame) return base_q_index;
2006*77c1e3ccSAndroid Build Coastguard Worker
2007*77c1e3ccSAndroid Build Coastguard Worker if (!is_intrl_arf_boost) return arf_q;
2008*77c1e3ccSAndroid Build Coastguard Worker
2009*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = arf_q;
2010*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = base_q_index;
2011*77c1e3ccSAndroid Build Coastguard Worker
2012*77c1e3ccSAndroid Build Coastguard Worker while (gf_pyramid_level > 1) {
2013*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
2014*77c1e3ccSAndroid Build Coastguard Worker --gf_pyramid_level;
2015*77c1e3ccSAndroid Build Coastguard Worker }
2016*77c1e3ccSAndroid Build Coastguard Worker return active_best_quality;
2017*77c1e3ccSAndroid Build Coastguard Worker }
2018*77c1e3ccSAndroid Build Coastguard Worker
rc_pick_q_and_bounds_q_mode(const AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)2019*77c1e3ccSAndroid Build Coastguard Worker static int rc_pick_q_and_bounds_q_mode(const AV1_COMP *cpi, int width,
2020*77c1e3ccSAndroid Build Coastguard Worker int height, int gf_index,
2021*77c1e3ccSAndroid Build Coastguard Worker int *bottom_index, int *top_index) {
2022*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
2023*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
2024*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2025*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2026*77c1e3ccSAndroid Build Coastguard Worker const int cq_level =
2027*77c1e3ccSAndroid Build Coastguard Worker get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
2028*77c1e3ccSAndroid Build Coastguard Worker cpi->superres_mode, cm->superres_scale_denominator);
2029*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = 0;
2030*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = rc->active_worst_quality;
2031*77c1e3ccSAndroid Build Coastguard Worker int q;
2032*77c1e3ccSAndroid Build Coastguard Worker
2033*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
2034*77c1e3ccSAndroid Build Coastguard Worker get_intra_q_and_bounds(cpi, width, height, &active_best_quality,
2035*77c1e3ccSAndroid Build Coastguard Worker &active_worst_quality, cq_level);
2036*77c1e3ccSAndroid Build Coastguard Worker } else {
2037*77c1e3ccSAndroid Build Coastguard Worker // Active best quality limited by previous layer.
2038*77c1e3ccSAndroid Build Coastguard Worker active_best_quality =
2039*77c1e3ccSAndroid Build Coastguard Worker get_active_best_quality(cpi, active_worst_quality, cq_level, gf_index);
2040*77c1e3ccSAndroid Build Coastguard Worker }
2041*77c1e3ccSAndroid Build Coastguard Worker
2042*77c1e3ccSAndroid Build Coastguard Worker if (cq_level > 0) active_best_quality = AOMMAX(1, active_best_quality);
2043*77c1e3ccSAndroid Build Coastguard Worker
2044*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality;
2045*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = active_best_quality;
2046*77c1e3ccSAndroid Build Coastguard Worker
2047*77c1e3ccSAndroid Build Coastguard Worker *top_index = AOMMAX(*top_index, rc->best_quality);
2048*77c1e3ccSAndroid Build Coastguard Worker *top_index = AOMMIN(*top_index, rc->worst_quality);
2049*77c1e3ccSAndroid Build Coastguard Worker
2050*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = AOMMAX(*bottom_index, rc->best_quality);
2051*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = AOMMIN(*bottom_index, rc->worst_quality);
2052*77c1e3ccSAndroid Build Coastguard Worker
2053*77c1e3ccSAndroid Build Coastguard Worker q = active_best_quality;
2054*77c1e3ccSAndroid Build Coastguard Worker
2055*77c1e3ccSAndroid Build Coastguard Worker q = AOMMAX(q, rc->best_quality);
2056*77c1e3ccSAndroid Build Coastguard Worker q = AOMMIN(q, rc->worst_quality);
2057*77c1e3ccSAndroid Build Coastguard Worker
2058*77c1e3ccSAndroid Build Coastguard Worker assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
2059*77c1e3ccSAndroid Build Coastguard Worker assert(*bottom_index <= rc->worst_quality &&
2060*77c1e3ccSAndroid Build Coastguard Worker *bottom_index >= rc->best_quality);
2061*77c1e3ccSAndroid Build Coastguard Worker assert(q <= rc->worst_quality && q >= rc->best_quality);
2062*77c1e3ccSAndroid Build Coastguard Worker
2063*77c1e3ccSAndroid Build Coastguard Worker return q;
2064*77c1e3ccSAndroid Build Coastguard Worker }
2065*77c1e3ccSAndroid Build Coastguard Worker
2066*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Picks q and q bounds given rate control parameters in \c cpi->rc.
2067*77c1e3ccSAndroid Build Coastguard Worker *
2068*77c1e3ccSAndroid Build Coastguard Worker * Handles the general cases not covered by
2069*77c1e3ccSAndroid Build Coastguard Worker * \ref rc_pick_q_and_bounds_no_stats_cbr() and
2070*77c1e3ccSAndroid Build Coastguard Worker * \ref rc_pick_q_and_bounds_no_stats()
2071*77c1e3ccSAndroid Build Coastguard Worker *
2072*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
2073*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
2074*77c1e3ccSAndroid Build Coastguard Worker * \param[in] width Coded frame width
2075*77c1e3ccSAndroid Build Coastguard Worker * \param[in] height Coded frame height
2076*77c1e3ccSAndroid Build Coastguard Worker * \param[in] gf_index Index of this frame in the golden frame group
2077*77c1e3ccSAndroid Build Coastguard Worker * \param[out] bottom_index Bottom bound for q index (best quality)
2078*77c1e3ccSAndroid Build Coastguard Worker * \param[out] top_index Top bound for q index (worst quality)
2079*77c1e3ccSAndroid Build Coastguard Worker * \return Returns selected q index to be used for encoding this frame.
2080*77c1e3ccSAndroid Build Coastguard Worker */
rc_pick_q_and_bounds(const AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)2081*77c1e3ccSAndroid Build Coastguard Worker static int rc_pick_q_and_bounds(const AV1_COMP *cpi, int width, int height,
2082*77c1e3ccSAndroid Build Coastguard Worker int gf_index, int *bottom_index,
2083*77c1e3ccSAndroid Build Coastguard Worker int *top_index) {
2084*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
2085*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
2086*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2087*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2088*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
2089*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *gf_group = &cpi->ppi->gf_group;
2090*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(has_no_stats_stage(cpi),
2091*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.mode == AOM_Q &&
2092*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index] != ARF_UPDATE));
2093*77c1e3ccSAndroid Build Coastguard Worker const int cq_level =
2094*77c1e3ccSAndroid Build Coastguard Worker get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
2095*77c1e3ccSAndroid Build Coastguard Worker cpi->superres_mode, cm->superres_scale_denominator);
2096*77c1e3ccSAndroid Build Coastguard Worker
2097*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->rc_cfg.mode == AOM_Q) {
2098*77c1e3ccSAndroid Build Coastguard Worker return rc_pick_q_and_bounds_q_mode(cpi, width, height, gf_index,
2099*77c1e3ccSAndroid Build Coastguard Worker bottom_index, top_index);
2100*77c1e3ccSAndroid Build Coastguard Worker }
2101*77c1e3ccSAndroid Build Coastguard Worker
2102*77c1e3ccSAndroid Build Coastguard Worker int active_best_quality = 0;
2103*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality = rc->active_worst_quality;
2104*77c1e3ccSAndroid Build Coastguard Worker int q;
2105*77c1e3ccSAndroid Build Coastguard Worker
2106*77c1e3ccSAndroid Build Coastguard Worker const int is_intrl_arf_boost =
2107*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
2108*77c1e3ccSAndroid Build Coastguard Worker
2109*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
2110*77c1e3ccSAndroid Build Coastguard Worker get_intra_q_and_bounds(cpi, width, height, &active_best_quality,
2111*77c1e3ccSAndroid Build Coastguard Worker &active_worst_quality, cq_level);
2112*77c1e3ccSAndroid Build Coastguard Worker #ifdef STRICT_RC
2113*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = 0;
2114*77c1e3ccSAndroid Build Coastguard Worker #endif
2115*77c1e3ccSAndroid Build Coastguard Worker } else {
2116*77c1e3ccSAndroid Build Coastguard Worker // Active best quality limited by previous layer.
2117*77c1e3ccSAndroid Build Coastguard Worker const int pyramid_level = gf_group_pyramid_level(gf_group, gf_index);
2118*77c1e3ccSAndroid Build Coastguard Worker
2119*77c1e3ccSAndroid Build Coastguard Worker if ((pyramid_level <= 1) || (pyramid_level > MAX_ARF_LAYERS)) {
2120*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = get_active_best_quality(cpi, active_worst_quality,
2121*77c1e3ccSAndroid Build Coastguard Worker cq_level, gf_index);
2122*77c1e3ccSAndroid Build Coastguard Worker } else {
2123*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2124*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
2125*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
2126*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
2127*77c1e3ccSAndroid Build Coastguard Worker int local_active_best_quality =
2128*77c1e3ccSAndroid Build Coastguard Worker simulate_parallel_frame
2129*77c1e3ccSAndroid Build Coastguard Worker ? p_rc->temp_active_best_quality[pyramid_level - 1]
2130*77c1e3ccSAndroid Build Coastguard Worker : p_rc->active_best_quality[pyramid_level - 1];
2131*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = local_active_best_quality + 1;
2132*77c1e3ccSAndroid Build Coastguard Worker #else
2133*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = p_rc->active_best_quality[pyramid_level - 1] + 1;
2134*77c1e3ccSAndroid Build Coastguard Worker #endif
2135*77c1e3ccSAndroid Build Coastguard Worker
2136*77c1e3ccSAndroid Build Coastguard Worker active_best_quality = AOMMIN(active_best_quality, active_worst_quality);
2137*77c1e3ccSAndroid Build Coastguard Worker #ifdef STRICT_RC
2138*77c1e3ccSAndroid Build Coastguard Worker active_best_quality += (active_worst_quality - active_best_quality) / 16;
2139*77c1e3ccSAndroid Build Coastguard Worker #else
2140*77c1e3ccSAndroid Build Coastguard Worker active_best_quality += (active_worst_quality - active_best_quality) / 2;
2141*77c1e3ccSAndroid Build Coastguard Worker #endif
2142*77c1e3ccSAndroid Build Coastguard Worker }
2143*77c1e3ccSAndroid Build Coastguard Worker
2144*77c1e3ccSAndroid Build Coastguard Worker // For alt_ref and GF frames (including internal arf frames) adjust the
2145*77c1e3ccSAndroid Build Coastguard Worker // worst allowed quality as well. This insures that even on hard
2146*77c1e3ccSAndroid Build Coastguard Worker // sections we don't clamp the Q at the same value for arf frames and
2147*77c1e3ccSAndroid Build Coastguard Worker // leaf (non arf) frames. This is important to the TPL model which assumes
2148*77c1e3ccSAndroid Build Coastguard Worker // Q drops with each arf level.
2149*77c1e3ccSAndroid Build Coastguard Worker if (!(rc->is_src_frame_alt_ref) &&
2150*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame || refresh_frame->alt_ref_frame ||
2151*77c1e3ccSAndroid Build Coastguard Worker is_intrl_arf_boost)) {
2152*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality =
2153*77c1e3ccSAndroid Build Coastguard Worker (active_best_quality + (3 * active_worst_quality) + 2) / 4;
2154*77c1e3ccSAndroid Build Coastguard Worker }
2155*77c1e3ccSAndroid Build Coastguard Worker }
2156*77c1e3ccSAndroid Build Coastguard Worker
2157*77c1e3ccSAndroid Build Coastguard Worker adjust_active_best_and_worst_quality(
2158*77c1e3ccSAndroid Build Coastguard Worker cpi, is_intrl_arf_boost, &active_worst_quality, &active_best_quality);
2159*77c1e3ccSAndroid Build Coastguard Worker q = get_q(cpi, width, height, active_worst_quality, active_best_quality);
2160*77c1e3ccSAndroid Build Coastguard Worker
2161*77c1e3ccSAndroid Build Coastguard Worker // Special case when we are targeting the max allowed rate.
2162*77c1e3ccSAndroid Build Coastguard Worker if (rc->this_frame_target >= rc->max_frame_bandwidth &&
2163*77c1e3ccSAndroid Build Coastguard Worker q > active_worst_quality) {
2164*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = q;
2165*77c1e3ccSAndroid Build Coastguard Worker }
2166*77c1e3ccSAndroid Build Coastguard Worker
2167*77c1e3ccSAndroid Build Coastguard Worker *top_index = active_worst_quality;
2168*77c1e3ccSAndroid Build Coastguard Worker *bottom_index = active_best_quality;
2169*77c1e3ccSAndroid Build Coastguard Worker
2170*77c1e3ccSAndroid Build Coastguard Worker assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
2171*77c1e3ccSAndroid Build Coastguard Worker assert(*bottom_index <= rc->worst_quality &&
2172*77c1e3ccSAndroid Build Coastguard Worker *bottom_index >= rc->best_quality);
2173*77c1e3ccSAndroid Build Coastguard Worker assert(q <= rc->worst_quality && q >= rc->best_quality);
2174*77c1e3ccSAndroid Build Coastguard Worker
2175*77c1e3ccSAndroid Build Coastguard Worker return q;
2176*77c1e3ccSAndroid Build Coastguard Worker }
2177*77c1e3ccSAndroid Build Coastguard Worker
rc_compute_variance_onepass_rt(AV1_COMP * cpi)2178*77c1e3ccSAndroid Build Coastguard Worker static void rc_compute_variance_onepass_rt(AV1_COMP *cpi) {
2179*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2180*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG const *const unscaled_src = cpi->unscaled_source;
2181*77c1e3ccSAndroid Build Coastguard Worker if (unscaled_src == NULL) return;
2182*77c1e3ccSAndroid Build Coastguard Worker
2183*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *src_y = unscaled_src->y_buffer;
2184*77c1e3ccSAndroid Build Coastguard Worker const int src_ystride = unscaled_src->y_stride;
2185*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
2186*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *pre_y = yv12->buffers[0];
2187*77c1e3ccSAndroid Build Coastguard Worker const int pre_ystride = yv12->strides[0];
2188*77c1e3ccSAndroid Build Coastguard Worker
2189*77c1e3ccSAndroid Build Coastguard Worker // TODO(yunqing): support scaled reference frames.
2190*77c1e3ccSAndroid Build Coastguard Worker if (cpi->scaled_ref_buf[LAST_FRAME - 1]) return;
2191*77c1e3ccSAndroid Build Coastguard Worker
2192*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < 2; ++i) {
2193*77c1e3ccSAndroid Build Coastguard Worker if (unscaled_src->widths[i] != yv12->widths[i] ||
2194*77c1e3ccSAndroid Build Coastguard Worker unscaled_src->heights[i] != yv12->heights[i]) {
2195*77c1e3ccSAndroid Build Coastguard Worker return;
2196*77c1e3ccSAndroid Build Coastguard Worker }
2197*77c1e3ccSAndroid Build Coastguard Worker }
2198*77c1e3ccSAndroid Build Coastguard Worker
2199*77c1e3ccSAndroid Build Coastguard Worker const int num_mi_cols = cm->mi_params.mi_cols;
2200*77c1e3ccSAndroid Build Coastguard Worker const int num_mi_rows = cm->mi_params.mi_rows;
2201*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = BLOCK_64X64;
2202*77c1e3ccSAndroid Build Coastguard Worker int num_samples = 0;
2203*77c1e3ccSAndroid Build Coastguard Worker // sse is computed on 64x64 blocks
2204*77c1e3ccSAndroid Build Coastguard Worker const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
2205*77c1e3ccSAndroid Build Coastguard Worker ? (cm->seq_params->mib_size >> 1)
2206*77c1e3ccSAndroid Build Coastguard Worker : cm->seq_params->mib_size;
2207*77c1e3ccSAndroid Build Coastguard Worker const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
2208*77c1e3ccSAndroid Build Coastguard Worker const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb;
2209*77c1e3ccSAndroid Build Coastguard Worker
2210*77c1e3ccSAndroid Build Coastguard Worker uint64_t fsse = 0;
2211*77c1e3ccSAndroid Build Coastguard Worker cpi->rec_sse = 0;
2212*77c1e3ccSAndroid Build Coastguard Worker
2213*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
2214*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
2215*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
2216*77c1e3ccSAndroid Build Coastguard Worker uint8_t src[64 * 64] = { 0 };
2217*77c1e3ccSAndroid Build Coastguard Worker // Apply 4x4 block averaging/denoising on source frame.
2218*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < 64; i += 4) {
2219*77c1e3ccSAndroid Build Coastguard Worker for (int j = 0; j < 64; j += 4) {
2220*77c1e3ccSAndroid Build Coastguard Worker const unsigned int avg =
2221*77c1e3ccSAndroid Build Coastguard Worker aom_avg_4x4(src_y + i * src_ystride + j, src_ystride);
2222*77c1e3ccSAndroid Build Coastguard Worker
2223*77c1e3ccSAndroid Build Coastguard Worker for (int m = 0; m < 4; ++m) {
2224*77c1e3ccSAndroid Build Coastguard Worker for (int n = 0; n < 4; ++n) src[i * 64 + j + m * 64 + n] = avg;
2225*77c1e3ccSAndroid Build Coastguard Worker }
2226*77c1e3ccSAndroid Build Coastguard Worker }
2227*77c1e3ccSAndroid Build Coastguard Worker }
2228*77c1e3ccSAndroid Build Coastguard Worker
2229*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fn_ptr[bsize].vf(src, 64, pre_y, pre_ystride, &sse);
2230*77c1e3ccSAndroid Build Coastguard Worker fsse += sse;
2231*77c1e3ccSAndroid Build Coastguard Worker num_samples++;
2232*77c1e3ccSAndroid Build Coastguard Worker src_y += 64;
2233*77c1e3ccSAndroid Build Coastguard Worker pre_y += 64;
2234*77c1e3ccSAndroid Build Coastguard Worker }
2235*77c1e3ccSAndroid Build Coastguard Worker src_y += (src_ystride << 6) - (sb_cols << 6);
2236*77c1e3ccSAndroid Build Coastguard Worker pre_y += (pre_ystride << 6) - (sb_cols << 6);
2237*77c1e3ccSAndroid Build Coastguard Worker }
2238*77c1e3ccSAndroid Build Coastguard Worker assert(num_samples > 0);
2239*77c1e3ccSAndroid Build Coastguard Worker // Ensure rec_sse > 0
2240*77c1e3ccSAndroid Build Coastguard Worker if (num_samples > 0) cpi->rec_sse = fsse > 0 ? fsse : 1;
2241*77c1e3ccSAndroid Build Coastguard Worker }
2242*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_pick_q_and_bounds(AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)2243*77c1e3ccSAndroid Build Coastguard Worker int av1_rc_pick_q_and_bounds(AV1_COMP *cpi, int width, int height, int gf_index,
2244*77c1e3ccSAndroid Build Coastguard Worker int *bottom_index, int *top_index) {
2245*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2246*77c1e3ccSAndroid Build Coastguard Worker int q;
2247*77c1e3ccSAndroid Build Coastguard Worker // TODO(sarahparker) merge no-stats vbr and altref q computation
2248*77c1e3ccSAndroid Build Coastguard Worker // with rc_pick_q_and_bounds().
2249*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *gf_group = &cpi->ppi->gf_group;
2250*77c1e3ccSAndroid Build Coastguard Worker if ((cpi->oxcf.rc_cfg.mode != AOM_Q ||
2251*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index] == ARF_UPDATE) &&
2252*77c1e3ccSAndroid Build Coastguard Worker has_no_stats_stage(cpi)) {
2253*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_CBR) {
2254*77c1e3ccSAndroid Build Coastguard Worker // TODO(yunqing): the results could be used for encoder optimization.
2255*77c1e3ccSAndroid Build Coastguard Worker cpi->rec_sse = UINT64_MAX;
2256*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.hl_sf.accurate_bit_estimate &&
2257*77c1e3ccSAndroid Build Coastguard Worker cpi->common.current_frame.frame_type != KEY_FRAME)
2258*77c1e3ccSAndroid Build Coastguard Worker rc_compute_variance_onepass_rt(cpi);
2259*77c1e3ccSAndroid Build Coastguard Worker
2260*77c1e3ccSAndroid Build Coastguard Worker q = rc_pick_q_and_bounds_no_stats_cbr(cpi, width, height, bottom_index,
2261*77c1e3ccSAndroid Build Coastguard Worker top_index);
2262*77c1e3ccSAndroid Build Coastguard Worker // preserve copy of active worst quality selected.
2263*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.active_worst_quality = *top_index;
2264*77c1e3ccSAndroid Build Coastguard Worker
2265*77c1e3ccSAndroid Build Coastguard Worker #if USE_UNRESTRICTED_Q_IN_CQ_MODE
2266*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) {
2267*77c1e3ccSAndroid Build Coastguard Worker q = rc_pick_q_and_bounds_no_stats_cq(cpi, width, height, bottom_index,
2268*77c1e3ccSAndroid Build Coastguard Worker top_index);
2269*77c1e3ccSAndroid Build Coastguard Worker #endif // USE_UNRESTRICTED_Q_IN_CQ_MODE
2270*77c1e3ccSAndroid Build Coastguard Worker } else {
2271*77c1e3ccSAndroid Build Coastguard Worker q = rc_pick_q_and_bounds_no_stats(cpi, width, height, bottom_index,
2272*77c1e3ccSAndroid Build Coastguard Worker top_index);
2273*77c1e3ccSAndroid Build Coastguard Worker }
2274*77c1e3ccSAndroid Build Coastguard Worker } else {
2275*77c1e3ccSAndroid Build Coastguard Worker q = rc_pick_q_and_bounds(cpi, width, height, gf_index, bottom_index,
2276*77c1e3ccSAndroid Build Coastguard Worker top_index);
2277*77c1e3ccSAndroid Build Coastguard Worker }
2278*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->update_type[gf_index] == ARF_UPDATE) p_rc->arf_q = q;
2279*77c1e3ccSAndroid Build Coastguard Worker
2280*77c1e3ccSAndroid Build Coastguard Worker return q;
2281*77c1e3ccSAndroid Build Coastguard Worker }
2282*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_compute_frame_size_bounds(const AV1_COMP * cpi,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)2283*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
2284*77c1e3ccSAndroid Build Coastguard Worker int *frame_under_shoot_limit,
2285*77c1e3ccSAndroid Build Coastguard Worker int *frame_over_shoot_limit) {
2286*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_Q) {
2287*77c1e3ccSAndroid Build Coastguard Worker *frame_under_shoot_limit = 0;
2288*77c1e3ccSAndroid Build Coastguard Worker *frame_over_shoot_limit = INT_MAX;
2289*77c1e3ccSAndroid Build Coastguard Worker } else {
2290*77c1e3ccSAndroid Build Coastguard Worker // For very small rate targets where the fractional adjustment
2291*77c1e3ccSAndroid Build Coastguard Worker // may be tiny make sure there is at least a minimum range.
2292*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->sf.hl_sf.recode_tolerance <= 100);
2293*77c1e3ccSAndroid Build Coastguard Worker const int tolerance = (int)AOMMAX(
2294*77c1e3ccSAndroid Build Coastguard Worker 100, ((int64_t)cpi->sf.hl_sf.recode_tolerance * frame_target) / 100);
2295*77c1e3ccSAndroid Build Coastguard Worker *frame_under_shoot_limit = AOMMAX(frame_target - tolerance, 0);
2296*77c1e3ccSAndroid Build Coastguard Worker *frame_over_shoot_limit = (int)AOMMIN((int64_t)frame_target + tolerance,
2297*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.max_frame_bandwidth);
2298*77c1e3ccSAndroid Build Coastguard Worker }
2299*77c1e3ccSAndroid Build Coastguard Worker }
2300*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_set_frame_target(AV1_COMP * cpi,int target,int width,int height)2301*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) {
2302*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
2303*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2304*77c1e3ccSAndroid Build Coastguard Worker
2305*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target = target;
2306*77c1e3ccSAndroid Build Coastguard Worker
2307*77c1e3ccSAndroid Build Coastguard Worker // Modify frame size target when down-scaled.
2308*77c1e3ccSAndroid Build Coastguard Worker if (av1_frame_scaled(cm) && cpi->oxcf.rc_cfg.mode != AOM_CBR) {
2309*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target = saturate_cast_double_to_int(
2310*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target *
2311*77c1e3ccSAndroid Build Coastguard Worker resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height));
2312*77c1e3ccSAndroid Build Coastguard Worker }
2313*77c1e3ccSAndroid Build Coastguard Worker
2314*77c1e3ccSAndroid Build Coastguard Worker // Target rate per SB64 (including partial SB64s.
2315*77c1e3ccSAndroid Build Coastguard Worker const int64_t sb64_target_rate =
2316*77c1e3ccSAndroid Build Coastguard Worker ((int64_t)rc->this_frame_target << 12) / (width * height);
2317*77c1e3ccSAndroid Build Coastguard Worker rc->sb64_target_rate = (int)AOMMIN(sb64_target_rate, INT_MAX);
2318*77c1e3ccSAndroid Build Coastguard Worker }
2319*77c1e3ccSAndroid Build Coastguard Worker
update_alt_ref_frame_stats(AV1_COMP * cpi)2320*77c1e3ccSAndroid Build Coastguard Worker static void update_alt_ref_frame_stats(AV1_COMP *cpi) {
2321*77c1e3ccSAndroid Build Coastguard Worker // this frame refreshes means next frames don't unless specified by user
2322*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2323*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_golden = 0;
2324*77c1e3ccSAndroid Build Coastguard Worker }
2325*77c1e3ccSAndroid Build Coastguard Worker
update_golden_frame_stats(AV1_COMP * cpi)2326*77c1e3ccSAndroid Build Coastguard Worker static void update_golden_frame_stats(AV1_COMP *cpi) {
2327*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2328*77c1e3ccSAndroid Build Coastguard Worker
2329*77c1e3ccSAndroid Build Coastguard Worker // Update the Golden frame usage counts.
2330*77c1e3ccSAndroid Build Coastguard Worker if (cpi->refresh_frame.golden_frame || rc->is_src_frame_alt_ref) {
2331*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_golden = 0;
2332*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->common.show_frame) {
2333*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_golden++;
2334*77c1e3ccSAndroid Build Coastguard Worker }
2335*77c1e3ccSAndroid Build Coastguard Worker }
2336*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_postencode_update(AV1_COMP * cpi,uint64_t bytes_used)2337*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
2338*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
2339*77c1e3ccSAndroid Build Coastguard Worker const CurrentFrame *const current_frame = &cm->current_frame;
2340*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2341*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2342*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
2343*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
2344*77c1e3ccSAndroid Build Coastguard Worker
2345*77c1e3ccSAndroid Build Coastguard Worker const int is_intrnl_arf =
2346*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE;
2347*77c1e3ccSAndroid Build Coastguard Worker
2348*77c1e3ccSAndroid Build Coastguard Worker const int qindex = cm->quant_params.base_qindex;
2349*77c1e3ccSAndroid Build Coastguard Worker
2350*77c1e3ccSAndroid Build Coastguard Worker #if RT_PASSIVE_STRATEGY
2351*77c1e3ccSAndroid Build Coastguard Worker const int frame_number = current_frame->frame_number % MAX_Q_HISTORY;
2352*77c1e3ccSAndroid Build Coastguard Worker p_rc->q_history[frame_number] = qindex;
2353*77c1e3ccSAndroid Build Coastguard Worker #endif // RT_PASSIVE_STRATEGY
2354*77c1e3ccSAndroid Build Coastguard Worker
2355*77c1e3ccSAndroid Build Coastguard Worker // Update rate control heuristics
2356*77c1e3ccSAndroid Build Coastguard Worker rc->projected_frame_size = (int)(bytes_used << 3);
2357*77c1e3ccSAndroid Build Coastguard Worker
2358*77c1e3ccSAndroid Build Coastguard Worker // Post encode loop adjustment of Q prediction.
2359*77c1e3ccSAndroid Build Coastguard Worker av1_rc_update_rate_correction_factors(cpi, 0, cm->width, cm->height);
2360*77c1e3ccSAndroid Build Coastguard Worker
2361*77c1e3ccSAndroid Build Coastguard Worker // Update bit estimation ratio.
2362*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_CBR &&
2363*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_type != KEY_FRAME &&
2364*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.hl_sf.accurate_bit_estimate) {
2365*77c1e3ccSAndroid Build Coastguard Worker const double q = av1_convert_qindex_to_q(cm->quant_params.base_qindex,
2366*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth);
2367*77c1e3ccSAndroid Build Coastguard Worker const int this_bit_est_ratio =
2368*77c1e3ccSAndroid Build Coastguard Worker (int)(rc->projected_frame_size * q / sqrt((double)cpi->rec_sse));
2369*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.bit_est_ratio =
2370*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.bit_est_ratio == 0
2371*77c1e3ccSAndroid Build Coastguard Worker ? this_bit_est_ratio
2372*77c1e3ccSAndroid Build Coastguard Worker : (7 * cpi->rc.bit_est_ratio + this_bit_est_ratio) / 8;
2373*77c1e3ccSAndroid Build Coastguard Worker }
2374*77c1e3ccSAndroid Build Coastguard Worker
2375*77c1e3ccSAndroid Build Coastguard Worker // Keep a record of last Q and ambient average Q.
2376*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME) {
2377*77c1e3ccSAndroid Build Coastguard Worker p_rc->last_q[KEY_FRAME] = qindex;
2378*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[KEY_FRAME] =
2379*77c1e3ccSAndroid Build Coastguard Worker ROUND_POWER_OF_TWO(3 * p_rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
2380*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.spatial_layer_id == 0) {
2381*77c1e3ccSAndroid Build Coastguard Worker rc->last_encoded_size_keyframe = rc->projected_frame_size;
2382*77c1e3ccSAndroid Build Coastguard Worker rc->last_target_size_keyframe = rc->this_frame_target;
2383*77c1e3ccSAndroid Build Coastguard Worker }
2384*77c1e3ccSAndroid Build Coastguard Worker } else {
2385*77c1e3ccSAndroid Build Coastguard Worker if ((cpi->ppi->use_svc && cpi->oxcf.rc_cfg.mode == AOM_CBR) ||
2386*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rtc_external_ratectrl ||
2387*77c1e3ccSAndroid Build Coastguard Worker (!rc->is_src_frame_alt_ref &&
2388*77c1e3ccSAndroid Build Coastguard Worker !(refresh_frame->golden_frame || is_intrnl_arf ||
2389*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame))) {
2390*77c1e3ccSAndroid Build Coastguard Worker p_rc->last_q[INTER_FRAME] = qindex;
2391*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO(
2392*77c1e3ccSAndroid Build Coastguard Worker 3 * p_rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
2393*77c1e3ccSAndroid Build Coastguard Worker p_rc->ni_frames++;
2394*77c1e3ccSAndroid Build Coastguard Worker p_rc->tot_q += av1_convert_qindex_to_q(qindex, cm->seq_params->bit_depth);
2395*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_q = p_rc->tot_q / p_rc->ni_frames;
2396*77c1e3ccSAndroid Build Coastguard Worker // Calculate the average Q for normal inter frames (not key or GFU
2397*77c1e3ccSAndroid Build Coastguard Worker // frames).
2398*77c1e3ccSAndroid Build Coastguard Worker rc->ni_tot_qi += qindex;
2399*77c1e3ccSAndroid Build Coastguard Worker rc->ni_av_qi = rc->ni_tot_qi / p_rc->ni_frames;
2400*77c1e3ccSAndroid Build Coastguard Worker }
2401*77c1e3ccSAndroid Build Coastguard Worker }
2402*77c1e3ccSAndroid Build Coastguard Worker // Keep record of last boosted (KF/GF/ARF) Q value.
2403*77c1e3ccSAndroid Build Coastguard Worker // If the current frame is coded at a lower Q then we also update it.
2404*77c1e3ccSAndroid Build Coastguard Worker // If all mbs in this group are skipped only update if the Q value is
2405*77c1e3ccSAndroid Build Coastguard Worker // better than that already stored.
2406*77c1e3ccSAndroid Build Coastguard Worker // This is used to help set quality in forced key frames to reduce popping
2407*77c1e3ccSAndroid Build Coastguard Worker if ((qindex < p_rc->last_boosted_qindex) ||
2408*77c1e3ccSAndroid Build Coastguard Worker (current_frame->frame_type == KEY_FRAME) ||
2409*77c1e3ccSAndroid Build Coastguard Worker (!p_rc->constrained_gf_group &&
2410*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->alt_ref_frame || is_intrnl_arf ||
2411*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame && !rc->is_src_frame_alt_ref)))) {
2412*77c1e3ccSAndroid Build Coastguard Worker p_rc->last_boosted_qindex = qindex;
2413*77c1e3ccSAndroid Build Coastguard Worker }
2414*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME) p_rc->last_kf_qindex = qindex;
2415*77c1e3ccSAndroid Build Coastguard Worker
2416*77c1e3ccSAndroid Build Coastguard Worker update_buffer_level(cpi, rc->projected_frame_size);
2417*77c1e3ccSAndroid Build Coastguard Worker rc->prev_avg_frame_bandwidth = rc->avg_frame_bandwidth;
2418*77c1e3ccSAndroid Build Coastguard Worker
2419*77c1e3ccSAndroid Build Coastguard Worker // Rolling monitors of whether we are over or underspending used to help
2420*77c1e3ccSAndroid Build Coastguard Worker // regulate min and Max Q in two pass.
2421*77c1e3ccSAndroid Build Coastguard Worker if (av1_frame_scaled(cm))
2422*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target = saturate_cast_double_to_int(
2423*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target /
2424*77c1e3ccSAndroid Build Coastguard Worker resize_rate_factor(&cpi->oxcf.frm_dim_cfg, cm->width, cm->height));
2425*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type != KEY_FRAME) {
2426*77c1e3ccSAndroid Build Coastguard Worker p_rc->rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
2427*77c1e3ccSAndroid Build Coastguard Worker (int64_t)p_rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
2428*77c1e3ccSAndroid Build Coastguard Worker p_rc->rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
2429*77c1e3ccSAndroid Build Coastguard Worker (int64_t)p_rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
2430*77c1e3ccSAndroid Build Coastguard Worker }
2431*77c1e3ccSAndroid Build Coastguard Worker
2432*77c1e3ccSAndroid Build Coastguard Worker // Actual bits spent
2433*77c1e3ccSAndroid Build Coastguard Worker p_rc->total_actual_bits += rc->projected_frame_size;
2434*77c1e3ccSAndroid Build Coastguard Worker p_rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
2435*77c1e3ccSAndroid Build Coastguard Worker
2436*77c1e3ccSAndroid Build Coastguard Worker if (is_altref_enabled(cpi->oxcf.gf_cfg.lag_in_frames,
2437*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.gf_cfg.enable_auto_arf) &&
2438*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame &&
2439*77c1e3ccSAndroid Build Coastguard Worker (current_frame->frame_type != KEY_FRAME && !frame_is_sframe(cm)))
2440*77c1e3ccSAndroid Build Coastguard Worker // Update the alternate reference frame stats as appropriate.
2441*77c1e3ccSAndroid Build Coastguard Worker update_alt_ref_frame_stats(cpi);
2442*77c1e3ccSAndroid Build Coastguard Worker else
2443*77c1e3ccSAndroid Build Coastguard Worker // Update the Golden frame stats as appropriate.
2444*77c1e3ccSAndroid Build Coastguard Worker update_golden_frame_stats(cpi);
2445*77c1e3ccSAndroid Build Coastguard Worker
2446*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2447*77c1e3ccSAndroid Build Coastguard Worker /*The variables temp_avg_frame_qindex, temp_last_q, temp_avg_q,
2448*77c1e3ccSAndroid Build Coastguard Worker * temp_last_boosted_qindex are introduced only for quality simulation
2449*77c1e3ccSAndroid Build Coastguard Worker * purpose, it retains the value previous to the parallel encode frames. The
2450*77c1e3ccSAndroid Build Coastguard Worker * variables are updated based on the update flag.
2451*77c1e3ccSAndroid Build Coastguard Worker *
2452*77c1e3ccSAndroid Build Coastguard Worker * If there exist show_existing_frames between parallel frames, then to
2453*77c1e3ccSAndroid Build Coastguard Worker * retain the temp state do not update it. */
2454*77c1e3ccSAndroid Build Coastguard Worker int show_existing_between_parallel_frames =
2455*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
2456*77c1e3ccSAndroid Build Coastguard Worker INTNL_OVERLAY_UPDATE &&
2457*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
2458*77c1e3ccSAndroid Build Coastguard Worker
2459*77c1e3ccSAndroid Build Coastguard Worker if (cpi->do_frame_data_update && !show_existing_between_parallel_frames &&
2460*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2461*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < FRAME_TYPES; i++) {
2462*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_last_q[i] = p_rc->last_q[i];
2463*77c1e3ccSAndroid Build Coastguard Worker }
2464*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_avg_q = p_rc->avg_q;
2465*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_last_boosted_qindex = p_rc->last_boosted_qindex;
2466*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_total_actual_bits = p_rc->total_actual_bits;
2467*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_projected_frame_size = rc->projected_frame_size;
2468*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < RATE_FACTOR_LEVELS; i++)
2469*77c1e3ccSAndroid Build Coastguard Worker p_rc->temp_rate_correction_factors[i] = p_rc->rate_correction_factors[i];
2470*77c1e3ccSAndroid Build Coastguard Worker }
2471*77c1e3ccSAndroid Build Coastguard Worker #endif
2472*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME) {
2473*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key = 0;
2474*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_scene_change = 0;
2475*77c1e3ccSAndroid Build Coastguard Worker }
2476*77c1e3ccSAndroid Build Coastguard Worker if (cpi->refresh_frame.golden_frame)
2477*77c1e3ccSAndroid Build Coastguard Worker rc->frame_num_last_gf_refresh = current_frame->frame_number;
2478*77c1e3ccSAndroid Build Coastguard Worker rc->prev_coded_width = cm->width;
2479*77c1e3ccSAndroid Build Coastguard Worker rc->prev_coded_height = cm->height;
2480*77c1e3ccSAndroid Build Coastguard Worker rc->frame_number_encoded++;
2481*77c1e3ccSAndroid Build Coastguard Worker rc->prev_frame_is_dropped = 0;
2482*77c1e3ccSAndroid Build Coastguard Worker rc->drop_count_consec = 0;
2483*77c1e3ccSAndroid Build Coastguard Worker }
2484*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_postencode_update_drop_frame(AV1_COMP * cpi)2485*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
2486*77c1e3ccSAndroid Build Coastguard Worker // Update buffer level with zero size, update frame counters, and return.
2487*77c1e3ccSAndroid Build Coastguard Worker update_buffer_level(cpi, 0);
2488*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_2_frame = 0;
2489*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 0;
2490*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.prev_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
2491*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.prev_coded_width = cpi->common.width;
2492*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.prev_coded_height = cpi->common.height;
2493*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.prev_frame_is_dropped = 1;
2494*77c1e3ccSAndroid Build Coastguard Worker // On a scene/slide change for dropped frame: reset the avg_source_sad to 0,
2495*77c1e3ccSAndroid Build Coastguard Worker // otherwise the avg_source_sad can get too large and subsequent frames
2496*77c1e3ccSAndroid Build Coastguard Worker // may miss the scene/slide detection.
2497*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.high_source_sad) cpi->rc.avg_source_sad = 0;
2498*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc && cpi->svc.number_spatial_layers > 1) {
2499*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = true;
2500*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = true;
2501*77c1e3ccSAndroid Build Coastguard Worker }
2502*77c1e3ccSAndroid Build Coastguard Worker }
2503*77c1e3ccSAndroid Build Coastguard Worker
av1_find_qindex(double desired_q,aom_bit_depth_t bit_depth,int best_qindex,int worst_qindex)2504*77c1e3ccSAndroid Build Coastguard Worker int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth,
2505*77c1e3ccSAndroid Build Coastguard Worker int best_qindex, int worst_qindex) {
2506*77c1e3ccSAndroid Build Coastguard Worker assert(best_qindex <= worst_qindex);
2507*77c1e3ccSAndroid Build Coastguard Worker int low = best_qindex;
2508*77c1e3ccSAndroid Build Coastguard Worker int high = worst_qindex;
2509*77c1e3ccSAndroid Build Coastguard Worker while (low < high) {
2510*77c1e3ccSAndroid Build Coastguard Worker const int mid = (low + high) >> 1;
2511*77c1e3ccSAndroid Build Coastguard Worker const double mid_q = av1_convert_qindex_to_q(mid, bit_depth);
2512*77c1e3ccSAndroid Build Coastguard Worker if (mid_q < desired_q) {
2513*77c1e3ccSAndroid Build Coastguard Worker low = mid + 1;
2514*77c1e3ccSAndroid Build Coastguard Worker } else {
2515*77c1e3ccSAndroid Build Coastguard Worker high = mid;
2516*77c1e3ccSAndroid Build Coastguard Worker }
2517*77c1e3ccSAndroid Build Coastguard Worker }
2518*77c1e3ccSAndroid Build Coastguard Worker assert(low == high);
2519*77c1e3ccSAndroid Build Coastguard Worker assert(av1_convert_qindex_to_q(low, bit_depth) >= desired_q ||
2520*77c1e3ccSAndroid Build Coastguard Worker low == worst_qindex);
2521*77c1e3ccSAndroid Build Coastguard Worker return low;
2522*77c1e3ccSAndroid Build Coastguard Worker }
2523*77c1e3ccSAndroid Build Coastguard Worker
av1_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget,aom_bit_depth_t bit_depth)2524*77c1e3ccSAndroid Build Coastguard Worker int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
2525*77c1e3ccSAndroid Build Coastguard Worker aom_bit_depth_t bit_depth) {
2526*77c1e3ccSAndroid Build Coastguard Worker const int start_index =
2527*77c1e3ccSAndroid Build Coastguard Worker av1_find_qindex(qstart, bit_depth, rc->best_quality, rc->worst_quality);
2528*77c1e3ccSAndroid Build Coastguard Worker const int target_index =
2529*77c1e3ccSAndroid Build Coastguard Worker av1_find_qindex(qtarget, bit_depth, rc->best_quality, rc->worst_quality);
2530*77c1e3ccSAndroid Build Coastguard Worker return target_index - start_index;
2531*77c1e3ccSAndroid Build Coastguard Worker }
2532*77c1e3ccSAndroid Build Coastguard Worker
2533*77c1e3ccSAndroid Build Coastguard Worker // Find q_index for the desired_bits_per_mb, within [best_qindex, worst_qindex],
2534*77c1e3ccSAndroid Build Coastguard Worker // assuming 'correction_factor' is 1.0.
2535*77c1e3ccSAndroid Build Coastguard Worker // To be precise, 'q_index' is the smallest integer, for which the corresponding
2536*77c1e3ccSAndroid Build Coastguard Worker // bits per mb <= desired_bits_per_mb.
2537*77c1e3ccSAndroid Build Coastguard Worker // If no such q index is found, returns 'worst_qindex'.
find_qindex_by_rate(const AV1_COMP * const cpi,int desired_bits_per_mb,FRAME_TYPE frame_type,int best_qindex,int worst_qindex)2538*77c1e3ccSAndroid Build Coastguard Worker static int find_qindex_by_rate(const AV1_COMP *const cpi,
2539*77c1e3ccSAndroid Build Coastguard Worker int desired_bits_per_mb, FRAME_TYPE frame_type,
2540*77c1e3ccSAndroid Build Coastguard Worker int best_qindex, int worst_qindex) {
2541*77c1e3ccSAndroid Build Coastguard Worker assert(best_qindex <= worst_qindex);
2542*77c1e3ccSAndroid Build Coastguard Worker int low = best_qindex;
2543*77c1e3ccSAndroid Build Coastguard Worker int high = worst_qindex;
2544*77c1e3ccSAndroid Build Coastguard Worker while (low < high) {
2545*77c1e3ccSAndroid Build Coastguard Worker const int mid = (low + high) >> 1;
2546*77c1e3ccSAndroid Build Coastguard Worker const int mid_bits_per_mb =
2547*77c1e3ccSAndroid Build Coastguard Worker av1_rc_bits_per_mb(cpi, frame_type, mid, 1.0, 0);
2548*77c1e3ccSAndroid Build Coastguard Worker if (mid_bits_per_mb > desired_bits_per_mb) {
2549*77c1e3ccSAndroid Build Coastguard Worker low = mid + 1;
2550*77c1e3ccSAndroid Build Coastguard Worker } else {
2551*77c1e3ccSAndroid Build Coastguard Worker high = mid;
2552*77c1e3ccSAndroid Build Coastguard Worker }
2553*77c1e3ccSAndroid Build Coastguard Worker }
2554*77c1e3ccSAndroid Build Coastguard Worker assert(low == high);
2555*77c1e3ccSAndroid Build Coastguard Worker assert(av1_rc_bits_per_mb(cpi, frame_type, low, 1.0, 0) <=
2556*77c1e3ccSAndroid Build Coastguard Worker desired_bits_per_mb ||
2557*77c1e3ccSAndroid Build Coastguard Worker low == worst_qindex);
2558*77c1e3ccSAndroid Build Coastguard Worker return low;
2559*77c1e3ccSAndroid Build Coastguard Worker }
2560*77c1e3ccSAndroid Build Coastguard Worker
av1_compute_qdelta_by_rate(const AV1_COMP * cpi,FRAME_TYPE frame_type,int qindex,double rate_target_ratio)2561*77c1e3ccSAndroid Build Coastguard Worker int av1_compute_qdelta_by_rate(const AV1_COMP *cpi, FRAME_TYPE frame_type,
2562*77c1e3ccSAndroid Build Coastguard Worker int qindex, double rate_target_ratio) {
2563*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
2564*77c1e3ccSAndroid Build Coastguard Worker
2565*77c1e3ccSAndroid Build Coastguard Worker // Look up the current projected bits per block for the base index
2566*77c1e3ccSAndroid Build Coastguard Worker const int base_bits_per_mb =
2567*77c1e3ccSAndroid Build Coastguard Worker av1_rc_bits_per_mb(cpi, frame_type, qindex, 1.0, 0);
2568*77c1e3ccSAndroid Build Coastguard Worker
2569*77c1e3ccSAndroid Build Coastguard Worker // Find the target bits per mb based on the base value and given ratio.
2570*77c1e3ccSAndroid Build Coastguard Worker const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
2571*77c1e3ccSAndroid Build Coastguard Worker
2572*77c1e3ccSAndroid Build Coastguard Worker const int target_index = find_qindex_by_rate(
2573*77c1e3ccSAndroid Build Coastguard Worker cpi, target_bits_per_mb, frame_type, rc->best_quality, rc->worst_quality);
2574*77c1e3ccSAndroid Build Coastguard Worker return target_index - qindex;
2575*77c1e3ccSAndroid Build Coastguard Worker }
2576*77c1e3ccSAndroid Build Coastguard Worker
set_gf_interval_range(const AV1_COMP * const cpi,RATE_CONTROL * const rc)2577*77c1e3ccSAndroid Build Coastguard Worker static void set_gf_interval_range(const AV1_COMP *const cpi,
2578*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc) {
2579*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2580*77c1e3ccSAndroid Build Coastguard Worker
2581*77c1e3ccSAndroid Build Coastguard Worker // Special case code for 1 pass fixed Q mode tests
2582*77c1e3ccSAndroid Build Coastguard Worker if ((has_no_stats_stage(cpi)) && (oxcf->rc_cfg.mode == AOM_Q)) {
2583*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
2584*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
2585*77c1e3ccSAndroid Build Coastguard Worker rc->static_scene_max_gf_interval = rc->min_gf_interval + 1;
2586*77c1e3ccSAndroid Build Coastguard Worker } else {
2587*77c1e3ccSAndroid Build Coastguard Worker // Set Maximum gf/arf interval
2588*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
2589*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
2590*77c1e3ccSAndroid Build Coastguard Worker if (rc->min_gf_interval == 0)
2591*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
2592*77c1e3ccSAndroid Build Coastguard Worker oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, cpi->framerate);
2593*77c1e3ccSAndroid Build Coastguard Worker if (rc->max_gf_interval == 0)
2594*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval =
2595*77c1e3ccSAndroid Build Coastguard Worker get_default_max_gf_interval(cpi->framerate, rc->min_gf_interval);
2596*77c1e3ccSAndroid Build Coastguard Worker /*
2597*77c1e3ccSAndroid Build Coastguard Worker * Extended max interval for genuinely static scenes like slide shows.
2598*77c1e3ccSAndroid Build Coastguard Worker * The no.of.stats available in the case of LAP is limited,
2599*77c1e3ccSAndroid Build Coastguard Worker * hence setting to max_gf_interval.
2600*77c1e3ccSAndroid Build Coastguard Worker */
2601*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->lap_enabled)
2602*77c1e3ccSAndroid Build Coastguard Worker rc->static_scene_max_gf_interval = rc->max_gf_interval + 1;
2603*77c1e3ccSAndroid Build Coastguard Worker else
2604*77c1e3ccSAndroid Build Coastguard Worker rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
2605*77c1e3ccSAndroid Build Coastguard Worker
2606*77c1e3ccSAndroid Build Coastguard Worker if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
2607*77c1e3ccSAndroid Build Coastguard Worker rc->max_gf_interval = rc->static_scene_max_gf_interval;
2608*77c1e3ccSAndroid Build Coastguard Worker
2609*77c1e3ccSAndroid Build Coastguard Worker // Clamp min to max
2610*77c1e3ccSAndroid Build Coastguard Worker rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval);
2611*77c1e3ccSAndroid Build Coastguard Worker }
2612*77c1e3ccSAndroid Build Coastguard Worker }
2613*77c1e3ccSAndroid Build Coastguard Worker
av1_rc_update_framerate(AV1_COMP * cpi,int width,int height)2614*77c1e3ccSAndroid Build Coastguard Worker void av1_rc_update_framerate(AV1_COMP *cpi, int width, int height) {
2615*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2616*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2617*77c1e3ccSAndroid Build Coastguard Worker const int MBs = av1_get_MBs(width, height);
2618*77c1e3ccSAndroid Build Coastguard Worker
2619*77c1e3ccSAndroid Build Coastguard Worker rc->avg_frame_bandwidth = saturate_cast_double_to_int(
2620*77c1e3ccSAndroid Build Coastguard Worker round(oxcf->rc_cfg.target_bandwidth / cpi->framerate));
2621*77c1e3ccSAndroid Build Coastguard Worker
2622*77c1e3ccSAndroid Build Coastguard Worker int64_t vbr_min_bits =
2623*77c1e3ccSAndroid Build Coastguard Worker (int64_t)rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmin_section / 100;
2624*77c1e3ccSAndroid Build Coastguard Worker vbr_min_bits = AOMMIN(vbr_min_bits, INT_MAX);
2625*77c1e3ccSAndroid Build Coastguard Worker
2626*77c1e3ccSAndroid Build Coastguard Worker rc->min_frame_bandwidth = AOMMAX((int)vbr_min_bits, FRAME_OVERHEAD_BITS);
2627*77c1e3ccSAndroid Build Coastguard Worker
2628*77c1e3ccSAndroid Build Coastguard Worker // A maximum bitrate for a frame is defined.
2629*77c1e3ccSAndroid Build Coastguard Worker // The baseline for this aligns with HW implementations that
2630*77c1e3ccSAndroid Build Coastguard Worker // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
2631*77c1e3ccSAndroid Build Coastguard Worker // per 16x16 MB (averaged over a frame). However this limit is extended if
2632*77c1e3ccSAndroid Build Coastguard Worker // a very high rate is given on the command line or the rate cannot
2633*77c1e3ccSAndroid Build Coastguard Worker // be achieved because of a user specified max q (e.g. when the user
2634*77c1e3ccSAndroid Build Coastguard Worker // specifies lossless encode.
2635*77c1e3ccSAndroid Build Coastguard Worker int64_t vbr_max_bits =
2636*77c1e3ccSAndroid Build Coastguard Worker (int64_t)rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmax_section / 100;
2637*77c1e3ccSAndroid Build Coastguard Worker vbr_max_bits = AOMMIN(vbr_max_bits, INT_MAX);
2638*77c1e3ccSAndroid Build Coastguard Worker
2639*77c1e3ccSAndroid Build Coastguard Worker rc->max_frame_bandwidth =
2640*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(AOMMAX((MBs * MAX_MB_RATE), MAXRATE_1080P), (int)vbr_max_bits);
2641*77c1e3ccSAndroid Build Coastguard Worker
2642*77c1e3ccSAndroid Build Coastguard Worker set_gf_interval_range(cpi, rc);
2643*77c1e3ccSAndroid Build Coastguard Worker }
2644*77c1e3ccSAndroid Build Coastguard Worker
2645*77c1e3ccSAndroid Build Coastguard Worker #define VBR_PCT_ADJUSTMENT_LIMIT 50
2646*77c1e3ccSAndroid Build Coastguard Worker // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(AV1_COMP * cpi,int * this_frame_target)2647*77c1e3ccSAndroid Build Coastguard Worker static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) {
2648*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2649*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2650*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2651*77c1e3ccSAndroid Build Coastguard Worker const int simulate_parallel_frame =
2652*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
2653*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
2654*77c1e3ccSAndroid Build Coastguard Worker int64_t vbr_bits_off_target = simulate_parallel_frame
2655*77c1e3ccSAndroid Build Coastguard Worker ? cpi->ppi->p_rc.temp_vbr_bits_off_target
2656*77c1e3ccSAndroid Build Coastguard Worker : p_rc->vbr_bits_off_target;
2657*77c1e3ccSAndroid Build Coastguard Worker #else
2658*77c1e3ccSAndroid Build Coastguard Worker int64_t vbr_bits_off_target = p_rc->vbr_bits_off_target;
2659*77c1e3ccSAndroid Build Coastguard Worker #endif
2660*77c1e3ccSAndroid Build Coastguard Worker int64_t frame_target = *this_frame_target;
2661*77c1e3ccSAndroid Build Coastguard Worker
2662*77c1e3ccSAndroid Build Coastguard Worker const double stats_count =
2663*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.stats_buf_ctx->total_stats != NULL
2664*77c1e3ccSAndroid Build Coastguard Worker ? cpi->ppi->twopass.stats_buf_ctx->total_stats->count
2665*77c1e3ccSAndroid Build Coastguard Worker : 0.0;
2666*77c1e3ccSAndroid Build Coastguard Worker const int frame_window =
2667*77c1e3ccSAndroid Build Coastguard Worker (int)AOMMIN(16, stats_count - cpi->common.current_frame.frame_number);
2668*77c1e3ccSAndroid Build Coastguard Worker assert(VBR_PCT_ADJUSTMENT_LIMIT <= 100);
2669*77c1e3ccSAndroid Build Coastguard Worker if (frame_window > 0) {
2670*77c1e3ccSAndroid Build Coastguard Worker const int64_t max_delta =
2671*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(llabs((vbr_bits_off_target / frame_window)),
2672*77c1e3ccSAndroid Build Coastguard Worker (frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100);
2673*77c1e3ccSAndroid Build Coastguard Worker
2674*77c1e3ccSAndroid Build Coastguard Worker // vbr_bits_off_target > 0 means we have extra bits to spend
2675*77c1e3ccSAndroid Build Coastguard Worker // vbr_bits_off_target < 0 we are currently overshooting
2676*77c1e3ccSAndroid Build Coastguard Worker frame_target += (vbr_bits_off_target >= 0) ? max_delta : -max_delta;
2677*77c1e3ccSAndroid Build Coastguard Worker }
2678*77c1e3ccSAndroid Build Coastguard Worker
2679*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2680*77c1e3ccSAndroid Build Coastguard Worker int64_t vbr_bits_off_target_fast =
2681*77c1e3ccSAndroid Build Coastguard Worker simulate_parallel_frame ? cpi->ppi->p_rc.temp_vbr_bits_off_target_fast
2682*77c1e3ccSAndroid Build Coastguard Worker : p_rc->vbr_bits_off_target_fast;
2683*77c1e3ccSAndroid Build Coastguard Worker #endif
2684*77c1e3ccSAndroid Build Coastguard Worker // Fast redistribution of bits arising from massive local undershoot.
2685*77c1e3ccSAndroid Build Coastguard Worker // Don't do it for kf,arf,gf or overlay frames.
2686*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_kf_gf_arf(cpi) &&
2687*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2688*77c1e3ccSAndroid Build Coastguard Worker vbr_bits_off_target_fast &&
2689*77c1e3ccSAndroid Build Coastguard Worker #else
2690*77c1e3ccSAndroid Build Coastguard Worker p_rc->vbr_bits_off_target_fast &&
2691*77c1e3ccSAndroid Build Coastguard Worker #endif
2692*77c1e3ccSAndroid Build Coastguard Worker !rc->is_src_frame_alt_ref) {
2693*77c1e3ccSAndroid Build Coastguard Worker int64_t one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, frame_target);
2694*77c1e3ccSAndroid Build Coastguard Worker int64_t fast_extra_bits;
2695*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2696*77c1e3ccSAndroid Build Coastguard Worker fast_extra_bits = AOMMIN(vbr_bits_off_target_fast, one_frame_bits);
2697*77c1e3ccSAndroid Build Coastguard Worker fast_extra_bits =
2698*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(fast_extra_bits,
2699*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(one_frame_bits / 8, vbr_bits_off_target_fast / 8));
2700*77c1e3ccSAndroid Build Coastguard Worker #else
2701*77c1e3ccSAndroid Build Coastguard Worker fast_extra_bits = AOMMIN(p_rc->vbr_bits_off_target_fast, one_frame_bits);
2702*77c1e3ccSAndroid Build Coastguard Worker fast_extra_bits =
2703*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(fast_extra_bits,
2704*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(one_frame_bits / 8, p_rc->vbr_bits_off_target_fast / 8));
2705*77c1e3ccSAndroid Build Coastguard Worker #endif
2706*77c1e3ccSAndroid Build Coastguard Worker fast_extra_bits = AOMMIN(fast_extra_bits, INT_MAX);
2707*77c1e3ccSAndroid Build Coastguard Worker if (fast_extra_bits > 0) {
2708*77c1e3ccSAndroid Build Coastguard Worker // Update frame_target only if additional bits are available from
2709*77c1e3ccSAndroid Build Coastguard Worker // local undershoot.
2710*77c1e3ccSAndroid Build Coastguard Worker frame_target += fast_extra_bits;
2711*77c1e3ccSAndroid Build Coastguard Worker }
2712*77c1e3ccSAndroid Build Coastguard Worker // Store the fast_extra_bits of the frame and reduce it from
2713*77c1e3ccSAndroid Build Coastguard Worker // vbr_bits_off_target_fast during postencode stage.
2714*77c1e3ccSAndroid Build Coastguard Worker rc->frame_level_fast_extra_bits = (int)fast_extra_bits;
2715*77c1e3ccSAndroid Build Coastguard Worker // Retaining the condition to update during postencode stage since
2716*77c1e3ccSAndroid Build Coastguard Worker // fast_extra_bits are calculated based on vbr_bits_off_target_fast.
2717*77c1e3ccSAndroid Build Coastguard Worker cpi->do_update_vbr_bits_off_target_fast = 1;
2718*77c1e3ccSAndroid Build Coastguard Worker }
2719*77c1e3ccSAndroid Build Coastguard Worker
2720*77c1e3ccSAndroid Build Coastguard Worker // Clamp the target for the frame to the maximum allowed for one frame.
2721*77c1e3ccSAndroid Build Coastguard Worker *this_frame_target = (int)AOMMIN(frame_target, INT_MAX);
2722*77c1e3ccSAndroid Build Coastguard Worker }
2723*77c1e3ccSAndroid Build Coastguard Worker
av1_set_target_rate(AV1_COMP * cpi,int width,int height)2724*77c1e3ccSAndroid Build Coastguard Worker void av1_set_target_rate(AV1_COMP *cpi, int width, int height) {
2725*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2726*77c1e3ccSAndroid Build Coastguard Worker int target_rate = rc->base_frame_target;
2727*77c1e3ccSAndroid Build Coastguard Worker
2728*77c1e3ccSAndroid Build Coastguard Worker // Correction to rate target based on prior over or under shoot.
2729*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_VBR || cpi->oxcf.rc_cfg.mode == AOM_CQ)
2730*77c1e3ccSAndroid Build Coastguard Worker vbr_rate_correction(cpi, &target_rate);
2731*77c1e3ccSAndroid Build Coastguard Worker av1_rc_set_frame_target(cpi, target_rate, width, height);
2732*77c1e3ccSAndroid Build Coastguard Worker }
2733*77c1e3ccSAndroid Build Coastguard Worker
av1_calc_pframe_target_size_one_pass_vbr(const AV1_COMP * const cpi,FRAME_UPDATE_TYPE frame_update_type)2734*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_pframe_target_size_one_pass_vbr(
2735*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
2736*77c1e3ccSAndroid Build Coastguard Worker static const int af_ratio = 10;
2737*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *const rc = &cpi->rc;
2738*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2739*77c1e3ccSAndroid Build Coastguard Worker int64_t target;
2740*77c1e3ccSAndroid Build Coastguard Worker #if USE_ALTREF_FOR_ONE_PASS
2741*77c1e3ccSAndroid Build Coastguard Worker if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
2742*77c1e3ccSAndroid Build Coastguard Worker frame_update_type == ARF_UPDATE) {
2743*77c1e3ccSAndroid Build Coastguard Worker target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval *
2744*77c1e3ccSAndroid Build Coastguard Worker af_ratio) /
2745*77c1e3ccSAndroid Build Coastguard Worker (p_rc->baseline_gf_interval + af_ratio - 1);
2746*77c1e3ccSAndroid Build Coastguard Worker } else {
2747*77c1e3ccSAndroid Build Coastguard Worker target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval) /
2748*77c1e3ccSAndroid Build Coastguard Worker (p_rc->baseline_gf_interval + af_ratio - 1);
2749*77c1e3ccSAndroid Build Coastguard Worker }
2750*77c1e3ccSAndroid Build Coastguard Worker #else
2751*77c1e3ccSAndroid Build Coastguard Worker target = rc->avg_frame_bandwidth;
2752*77c1e3ccSAndroid Build Coastguard Worker #endif
2753*77c1e3ccSAndroid Build Coastguard Worker return clamp_pframe_target_size(cpi, target, frame_update_type);
2754*77c1e3ccSAndroid Build Coastguard Worker }
2755*77c1e3ccSAndroid Build Coastguard Worker
av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP * const cpi)2756*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
2757*77c1e3ccSAndroid Build Coastguard Worker static const int kf_ratio = 25;
2758*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
2759*77c1e3ccSAndroid Build Coastguard Worker const int64_t target = (int64_t)rc->avg_frame_bandwidth * kf_ratio;
2760*77c1e3ccSAndroid Build Coastguard Worker return clamp_iframe_target_size(cpi, target);
2761*77c1e3ccSAndroid Build Coastguard Worker }
2762*77c1e3ccSAndroid Build Coastguard Worker
av1_calc_pframe_target_size_one_pass_cbr(const AV1_COMP * cpi,FRAME_UPDATE_TYPE frame_update_type)2763*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_pframe_target_size_one_pass_cbr(
2764*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
2765*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *oxcf = &cpi->oxcf;
2766*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
2767*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2768*77c1e3ccSAndroid Build Coastguard Worker const RateControlCfg *rc_cfg = &oxcf->rc_cfg;
2769*77c1e3ccSAndroid Build Coastguard Worker const int64_t diff = p_rc->optimal_buffer_level - p_rc->buffer_level;
2770*77c1e3ccSAndroid Build Coastguard Worker const int64_t one_pct_bits = 1 + p_rc->optimal_buffer_level / 100;
2771*77c1e3ccSAndroid Build Coastguard Worker int min_frame_target =
2772*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
2773*77c1e3ccSAndroid Build Coastguard Worker int64_t target;
2774*77c1e3ccSAndroid Build Coastguard Worker
2775*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->gf_cbr_boost_pct) {
2776*77c1e3ccSAndroid Build Coastguard Worker const int af_ratio_pct = rc_cfg->gf_cbr_boost_pct + 100;
2777*77c1e3ccSAndroid Build Coastguard Worker if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
2778*77c1e3ccSAndroid Build Coastguard Worker target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval *
2779*77c1e3ccSAndroid Build Coastguard Worker af_ratio_pct) /
2780*77c1e3ccSAndroid Build Coastguard Worker (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2781*77c1e3ccSAndroid Build Coastguard Worker } else {
2782*77c1e3ccSAndroid Build Coastguard Worker target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval *
2783*77c1e3ccSAndroid Build Coastguard Worker 100) /
2784*77c1e3ccSAndroid Build Coastguard Worker (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2785*77c1e3ccSAndroid Build Coastguard Worker }
2786*77c1e3ccSAndroid Build Coastguard Worker } else {
2787*77c1e3ccSAndroid Build Coastguard Worker target = rc->avg_frame_bandwidth;
2788*77c1e3ccSAndroid Build Coastguard Worker }
2789*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc) {
2790*77c1e3ccSAndroid Build Coastguard Worker // Note that for layers, avg_frame_bandwidth is the cumulative
2791*77c1e3ccSAndroid Build Coastguard Worker // per-frame-bandwidth. For the target size of this frame, use the
2792*77c1e3ccSAndroid Build Coastguard Worker // layer average frame size (i.e., non-cumulative per-frame-bw).
2793*77c1e3ccSAndroid Build Coastguard Worker int layer =
2794*77c1e3ccSAndroid Build Coastguard Worker LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
2795*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_temporal_layers);
2796*77c1e3ccSAndroid Build Coastguard Worker const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
2797*77c1e3ccSAndroid Build Coastguard Worker target = lc->avg_frame_size;
2798*77c1e3ccSAndroid Build Coastguard Worker min_frame_target = AOMMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
2799*77c1e3ccSAndroid Build Coastguard Worker }
2800*77c1e3ccSAndroid Build Coastguard Worker if (diff > 0) {
2801*77c1e3ccSAndroid Build Coastguard Worker // Lower the target bandwidth for this frame.
2802*77c1e3ccSAndroid Build Coastguard Worker const int pct_low =
2803*77c1e3ccSAndroid Build Coastguard Worker (int)AOMMIN(diff / one_pct_bits, rc_cfg->under_shoot_pct);
2804*77c1e3ccSAndroid Build Coastguard Worker target -= (target * pct_low) / 200;
2805*77c1e3ccSAndroid Build Coastguard Worker } else if (diff < 0) {
2806*77c1e3ccSAndroid Build Coastguard Worker // Increase the target bandwidth for this frame.
2807*77c1e3ccSAndroid Build Coastguard Worker const int pct_high =
2808*77c1e3ccSAndroid Build Coastguard Worker (int)AOMMIN(-diff / one_pct_bits, rc_cfg->over_shoot_pct);
2809*77c1e3ccSAndroid Build Coastguard Worker target += (target * pct_high) / 200;
2810*77c1e3ccSAndroid Build Coastguard Worker }
2811*77c1e3ccSAndroid Build Coastguard Worker if (rc_cfg->max_inter_bitrate_pct) {
2812*77c1e3ccSAndroid Build Coastguard Worker const int64_t max_rate =
2813*77c1e3ccSAndroid Build Coastguard Worker (int64_t)rc->avg_frame_bandwidth * rc_cfg->max_inter_bitrate_pct / 100;
2814*77c1e3ccSAndroid Build Coastguard Worker target = AOMMIN(target, max_rate);
2815*77c1e3ccSAndroid Build Coastguard Worker }
2816*77c1e3ccSAndroid Build Coastguard Worker if (target > INT_MAX) target = INT_MAX;
2817*77c1e3ccSAndroid Build Coastguard Worker return AOMMAX(min_frame_target, (int)target);
2818*77c1e3ccSAndroid Build Coastguard Worker }
2819*77c1e3ccSAndroid Build Coastguard Worker
av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP * cpi)2820*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
2821*77c1e3ccSAndroid Build Coastguard Worker const RATE_CONTROL *rc = &cpi->rc;
2822*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2823*77c1e3ccSAndroid Build Coastguard Worker int64_t target;
2824*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_number == 0) {
2825*77c1e3ccSAndroid Build Coastguard Worker target = ((p_rc->starting_buffer_level / 2) > INT_MAX)
2826*77c1e3ccSAndroid Build Coastguard Worker ? INT_MAX
2827*77c1e3ccSAndroid Build Coastguard Worker : (int)(p_rc->starting_buffer_level / 2);
2828*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_temporal_layers > 1 && target < (INT_MAX >> 2)) {
2829*77c1e3ccSAndroid Build Coastguard Worker target = target << AOMMIN(2, (cpi->svc.number_temporal_layers - 1));
2830*77c1e3ccSAndroid Build Coastguard Worker }
2831*77c1e3ccSAndroid Build Coastguard Worker } else {
2832*77c1e3ccSAndroid Build Coastguard Worker int kf_boost = 32;
2833*77c1e3ccSAndroid Build Coastguard Worker double framerate = cpi->framerate;
2834*77c1e3ccSAndroid Build Coastguard Worker
2835*77c1e3ccSAndroid Build Coastguard Worker kf_boost = AOMMAX(kf_boost, (int)round(2 * framerate - 16));
2836*77c1e3ccSAndroid Build Coastguard Worker if (rc->frames_since_key < framerate / 2) {
2837*77c1e3ccSAndroid Build Coastguard Worker kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
2838*77c1e3ccSAndroid Build Coastguard Worker }
2839*77c1e3ccSAndroid Build Coastguard Worker target = ((int64_t)(16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
2840*77c1e3ccSAndroid Build Coastguard Worker }
2841*77c1e3ccSAndroid Build Coastguard Worker return clamp_iframe_target_size(cpi, target);
2842*77c1e3ccSAndroid Build Coastguard Worker }
2843*77c1e3ccSAndroid Build Coastguard Worker
set_golden_update(AV1_COMP * const cpi)2844*77c1e3ccSAndroid Build Coastguard Worker static void set_golden_update(AV1_COMP *const cpi) {
2845*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2846*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2847*77c1e3ccSAndroid Build Coastguard Worker int divisor = 10;
2848*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
2849*77c1e3ccSAndroid Build Coastguard Worker divisor = cpi->cyclic_refresh->percent_refresh;
2850*77c1e3ccSAndroid Build Coastguard Worker
2851*77c1e3ccSAndroid Build Coastguard Worker // Set minimum gf_interval for GF update to a multiple of the refresh period,
2852*77c1e3ccSAndroid Build Coastguard Worker // with some max limit. Depending on past encoding stats, GF flag may be
2853*77c1e3ccSAndroid Build Coastguard Worker // reset and update may not occur until next baseline_gf_interval.
2854*77c1e3ccSAndroid Build Coastguard Worker const int gf_length_mult[2] = { 8, 4 };
2855*77c1e3ccSAndroid Build Coastguard Worker if (divisor > 0)
2856*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval =
2857*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(gf_length_mult[cpi->sf.rt_sf.gf_length_lvl] * (100 / divisor),
2858*77c1e3ccSAndroid Build Coastguard Worker MAX_GF_INTERVAL_RT);
2859*77c1e3ccSAndroid Build Coastguard Worker else
2860*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval = FIXED_GF_INTERVAL_RT;
2861*77c1e3ccSAndroid Build Coastguard Worker if (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 40)
2862*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval = 16;
2863*77c1e3ccSAndroid Build Coastguard Worker }
2864*77c1e3ccSAndroid Build Coastguard Worker
set_baseline_gf_interval(AV1_COMP * cpi,FRAME_TYPE frame_type)2865*77c1e3ccSAndroid Build Coastguard Worker static void set_baseline_gf_interval(AV1_COMP *cpi, FRAME_TYPE frame_type) {
2866*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2867*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2868*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *const gf_group = &cpi->ppi->gf_group;
2869*77c1e3ccSAndroid Build Coastguard Worker
2870*77c1e3ccSAndroid Build Coastguard Worker set_golden_update(cpi);
2871*77c1e3ccSAndroid Build Coastguard Worker
2872*77c1e3ccSAndroid Build Coastguard Worker if (p_rc->baseline_gf_interval > rc->frames_to_key &&
2873*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.kf_cfg.auto_key)
2874*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval = rc->frames_to_key;
2875*77c1e3ccSAndroid Build Coastguard Worker p_rc->gfu_boost = DEFAULT_GF_BOOST_RT;
2876*77c1e3ccSAndroid Build Coastguard Worker p_rc->constrained_gf_group =
2877*77c1e3ccSAndroid Build Coastguard Worker (p_rc->baseline_gf_interval >= rc->frames_to_key &&
2878*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.kf_cfg.auto_key)
2879*77c1e3ccSAndroid Build Coastguard Worker ? 1
2880*77c1e3ccSAndroid Build Coastguard Worker : 0;
2881*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due = p_rc->baseline_gf_interval;
2882*77c1e3ccSAndroid Build Coastguard Worker cpi->gf_frame_index = 0;
2883*77c1e3ccSAndroid Build Coastguard Worker // SVC does not use GF as periodic boost.
2884*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): Find better way to disable this for SVC.
2885*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc) {
2886*77c1e3ccSAndroid Build Coastguard Worker SVC *const svc = &cpi->svc;
2887*77c1e3ccSAndroid Build Coastguard Worker p_rc->baseline_gf_interval = MAX_STATIC_GF_GROUP_LENGTH - 1;
2888*77c1e3ccSAndroid Build Coastguard Worker p_rc->gfu_boost = 1;
2889*77c1e3ccSAndroid Build Coastguard Worker p_rc->constrained_gf_group = 0;
2890*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due = p_rc->baseline_gf_interval;
2891*77c1e3ccSAndroid Build Coastguard Worker for (int layer = 0;
2892*77c1e3ccSAndroid Build Coastguard Worker layer < svc->number_spatial_layers * svc->number_temporal_layers;
2893*77c1e3ccSAndroid Build Coastguard Worker ++layer) {
2894*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *const lc = &svc->layer_context[layer];
2895*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.baseline_gf_interval = p_rc->baseline_gf_interval;
2896*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.gfu_boost = p_rc->gfu_boost;
2897*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.constrained_gf_group = p_rc->constrained_gf_group;
2898*77c1e3ccSAndroid Build Coastguard Worker lc->rc.frames_till_gf_update_due = rc->frames_till_gf_update_due;
2899*77c1e3ccSAndroid Build Coastguard Worker lc->group_index = 0;
2900*77c1e3ccSAndroid Build Coastguard Worker }
2901*77c1e3ccSAndroid Build Coastguard Worker }
2902*77c1e3ccSAndroid Build Coastguard Worker gf_group->size = p_rc->baseline_gf_interval;
2903*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[0] = (frame_type == KEY_FRAME) ? KF_UPDATE : GF_UPDATE;
2904*77c1e3ccSAndroid Build Coastguard Worker gf_group->refbuf_state[cpi->gf_frame_index] =
2905*77c1e3ccSAndroid Build Coastguard Worker (frame_type == KEY_FRAME) ? REFBUF_RESET : REFBUF_UPDATE;
2906*77c1e3ccSAndroid Build Coastguard Worker }
2907*77c1e3ccSAndroid Build Coastguard Worker
av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP * cpi)2908*77c1e3ccSAndroid Build Coastguard Worker void av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP *cpi) {
2909*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2910*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2911*77c1e3ccSAndroid Build Coastguard Worker RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
2912*77c1e3ccSAndroid Build Coastguard Worker const int resize_pending = is_frame_resize_pending(cpi);
2913*77c1e3ccSAndroid Build Coastguard Worker if (!resize_pending && !rc->high_source_sad) {
2914*77c1e3ccSAndroid Build Coastguard Worker // Check if we should disable GF refresh (if period is up),
2915*77c1e3ccSAndroid Build Coastguard Worker // or force a GF refresh update (if we are at least halfway through
2916*77c1e3ccSAndroid Build Coastguard Worker // period) based on QP. Look into add info on segment deltaq.
2917*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2918*77c1e3ccSAndroid Build Coastguard Worker const int avg_qp = p_rc->avg_frame_qindex[INTER_FRAME];
2919*77c1e3ccSAndroid Build Coastguard Worker const int allow_gf_update =
2920*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due <= (p_rc->baseline_gf_interval - 10);
2921*77c1e3ccSAndroid Build Coastguard Worker int gf_update_changed = 0;
2922*77c1e3ccSAndroid Build Coastguard Worker int thresh = 87;
2923*77c1e3ccSAndroid Build Coastguard Worker if ((cm->current_frame.frame_number - cpi->rc.frame_num_last_gf_refresh) <
2924*77c1e3ccSAndroid Build Coastguard Worker FIXED_GF_INTERVAL_RT &&
2925*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due == 1 &&
2926*77c1e3ccSAndroid Build Coastguard Worker cm->quant_params.base_qindex > avg_qp) {
2927*77c1e3ccSAndroid Build Coastguard Worker // Disable GF refresh since QP is above the running average QP.
2928*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->refresh[rtc_ref->gld_idx_1layer] = 0;
2929*77c1e3ccSAndroid Build Coastguard Worker gf_update_changed = 1;
2930*77c1e3ccSAndroid Build Coastguard Worker cpi->refresh_frame.golden_frame = 0;
2931*77c1e3ccSAndroid Build Coastguard Worker } else if (allow_gf_update &&
2932*77c1e3ccSAndroid Build Coastguard Worker ((cm->quant_params.base_qindex < thresh * avg_qp / 100) ||
2933*77c1e3ccSAndroid Build Coastguard Worker (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 20))) {
2934*77c1e3ccSAndroid Build Coastguard Worker // Force refresh since QP is well below average QP or this is a high
2935*77c1e3ccSAndroid Build Coastguard Worker // motion frame.
2936*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->refresh[rtc_ref->gld_idx_1layer] = 1;
2937*77c1e3ccSAndroid Build Coastguard Worker gf_update_changed = 1;
2938*77c1e3ccSAndroid Build Coastguard Worker cpi->refresh_frame.golden_frame = 1;
2939*77c1e3ccSAndroid Build Coastguard Worker }
2940*77c1e3ccSAndroid Build Coastguard Worker if (gf_update_changed) {
2941*77c1e3ccSAndroid Build Coastguard Worker set_baseline_gf_interval(cpi, INTER_FRAME);
2942*77c1e3ccSAndroid Build Coastguard Worker int refresh_mask = 0;
2943*77c1e3ccSAndroid Build Coastguard Worker for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
2944*77c1e3ccSAndroid Build Coastguard Worker int ref_frame_map_idx = rtc_ref->ref_idx[i];
2945*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= rtc_ref->refresh[ref_frame_map_idx]
2946*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
2947*77c1e3ccSAndroid Build Coastguard Worker }
2948*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.refresh_frame_flags = refresh_mask;
2949*77c1e3ccSAndroid Build Coastguard Worker }
2950*77c1e3ccSAndroid Build Coastguard Worker }
2951*77c1e3ccSAndroid Build Coastguard Worker }
2952*77c1e3ccSAndroid Build Coastguard Worker
2953*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Setup the reference prediction structure for 1 pass real-time
2954*77c1e3ccSAndroid Build Coastguard Worker *
2955*77c1e3ccSAndroid Build Coastguard Worker * Set the reference prediction structure for 1 layer.
2956*77c1e3ccSAndroid Build Coastguard Worker * Current structure is to use 3 references (LAST, GOLDEN, ALTREF),
2957*77c1e3ccSAndroid Build Coastguard Worker * where ALT_REF always behind current by lag_alt frames, and GOLDEN is
2958*77c1e3ccSAndroid Build Coastguard Worker * either updated on LAST with period baseline_gf_interval (fixed slot)
2959*77c1e3ccSAndroid Build Coastguard Worker * or always behind current by lag_gld (gld_fixed_slot = 0, lag_gld <= 7).
2960*77c1e3ccSAndroid Build Coastguard Worker *
2961*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
2962*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
2963*77c1e3ccSAndroid Build Coastguard Worker * \param[in] gf_update Flag to indicate if GF is updated
2964*77c1e3ccSAndroid Build Coastguard Worker *
2965*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead the settings for the prediction
2966*77c1e3ccSAndroid Build Coastguard Worker * structure are set in \c cpi-ext_flags; and the buffer slot index
2967*77c1e3ccSAndroid Build Coastguard Worker * (for each of 7 references) and refresh flags (for each of the 8 slots)
2968*77c1e3ccSAndroid Build Coastguard Worker * are set in \c cpi->svc.ref_idx[] and \c cpi->svc.refresh[].
2969*77c1e3ccSAndroid Build Coastguard Worker */
av1_set_rtc_reference_structure_one_layer(AV1_COMP * cpi,int gf_update)2970*77c1e3ccSAndroid Build Coastguard Worker void av1_set_rtc_reference_structure_one_layer(AV1_COMP *cpi, int gf_update) {
2971*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2972*77c1e3ccSAndroid Build Coastguard Worker ExternalFlags *const ext_flags = &cpi->ext_flags;
2973*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
2974*77c1e3ccSAndroid Build Coastguard Worker ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
2975*77c1e3ccSAndroid Build Coastguard Worker &ext_flags->refresh_frame;
2976*77c1e3ccSAndroid Build Coastguard Worker RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
2977*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_number = (cpi->oxcf.rc_cfg.drop_frames_water_mark)
2978*77c1e3ccSAndroid Build Coastguard Worker ? rc->frame_number_encoded
2979*77c1e3ccSAndroid Build Coastguard Worker : cm->current_frame.frame_number;
2980*77c1e3ccSAndroid Build Coastguard Worker unsigned int lag_alt = 4;
2981*77c1e3ccSAndroid Build Coastguard Worker int last_idx = 0;
2982*77c1e3ccSAndroid Build Coastguard Worker int last_idx_refresh = 0;
2983*77c1e3ccSAndroid Build Coastguard Worker int gld_idx = 0;
2984*77c1e3ccSAndroid Build Coastguard Worker int alt_ref_idx = 0;
2985*77c1e3ccSAndroid Build Coastguard Worker int last2_idx = 0;
2986*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->update_pending = 1;
2987*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags = 0;
2988*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->last_frame = 1;
2989*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->golden_frame = 0;
2990*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->alt_ref_frame = 0;
2991*77c1e3ccSAndroid Build Coastguard Worker // Decide altref lag adaptively for rt
2992*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.sad_based_adp_altref_lag) {
2993*77c1e3ccSAndroid Build Coastguard Worker lag_alt = 6;
2994*77c1e3ccSAndroid Build Coastguard Worker const uint64_t th_frame_sad[4][3] = {
2995*77c1e3ccSAndroid Build Coastguard Worker { 18000, 18000, 18000 }, // HDRES CPU 9
2996*77c1e3ccSAndroid Build Coastguard Worker { 25000, 25000, 25000 }, // MIDRES CPU 9
2997*77c1e3ccSAndroid Build Coastguard Worker { 40000, 30000, 20000 }, // HDRES CPU 10
2998*77c1e3ccSAndroid Build Coastguard Worker { 30000, 25000, 20000 } // MIDRES CPU 10
2999*77c1e3ccSAndroid Build Coastguard Worker };
3000*77c1e3ccSAndroid Build Coastguard Worker int th_idx = cpi->sf.rt_sf.sad_based_adp_altref_lag - 1;
3001*77c1e3ccSAndroid Build Coastguard Worker assert(th_idx < 4);
3002*77c1e3ccSAndroid Build Coastguard Worker if (rc->avg_source_sad > th_frame_sad[th_idx][0])
3003*77c1e3ccSAndroid Build Coastguard Worker lag_alt = 3;
3004*77c1e3ccSAndroid Build Coastguard Worker else if (rc->avg_source_sad > th_frame_sad[th_idx][1])
3005*77c1e3ccSAndroid Build Coastguard Worker lag_alt = 4;
3006*77c1e3ccSAndroid Build Coastguard Worker else if (rc->avg_source_sad > th_frame_sad[th_idx][2])
3007*77c1e3ccSAndroid Build Coastguard Worker lag_alt = 5;
3008*77c1e3ccSAndroid Build Coastguard Worker }
3009*77c1e3ccSAndroid Build Coastguard Worker // This defines the reference structure for 1 layer (non-svc) RTC encoding.
3010*77c1e3ccSAndroid Build Coastguard Worker // To avoid the internal/default reference structure for non-realtime
3011*77c1e3ccSAndroid Build Coastguard Worker // overwriting this behavior, we use the "svc" ref parameters from the
3012*77c1e3ccSAndroid Build Coastguard Worker // external control SET_SVC_REF_FRAME_CONFIG.
3013*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): rename that control and the related internal parameters
3014*77c1e3ccSAndroid Build Coastguard Worker // to rtc_ref.
3015*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) rtc_ref->ref_idx[i] = 7;
3016*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; ++i) rtc_ref->refresh[i] = 0;
3017*77c1e3ccSAndroid Build Coastguard Worker // Set the reference frame flags.
3018*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags ^= AOM_LAST_FLAG;
3019*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->sf.rt_sf.force_only_last_ref) {
3020*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags ^= AOM_ALT_FLAG;
3021*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG;
3022*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1])
3023*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags ^= AOM_LAST2_FLAG;
3024*77c1e3ccSAndroid Build Coastguard Worker }
3025*77c1e3ccSAndroid Build Coastguard Worker const int sh = 6;
3026*77c1e3ccSAndroid Build Coastguard Worker // Moving index slot for last: 0 - (sh - 1).
3027*77c1e3ccSAndroid Build Coastguard Worker if (frame_number > 1) last_idx = ((frame_number - 1) % sh);
3028*77c1e3ccSAndroid Build Coastguard Worker // Moving index for refresh of last: one ahead for next frame.
3029*77c1e3ccSAndroid Build Coastguard Worker last_idx_refresh = (frame_number % sh);
3030*77c1e3ccSAndroid Build Coastguard Worker gld_idx = 6;
3031*77c1e3ccSAndroid Build Coastguard Worker
3032*77c1e3ccSAndroid Build Coastguard Worker // Moving index for alt_ref, lag behind LAST by lag_alt frames.
3033*77c1e3ccSAndroid Build Coastguard Worker if (frame_number > lag_alt) alt_ref_idx = ((frame_number - lag_alt) % sh);
3034*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) {
3035*77c1e3ccSAndroid Build Coastguard Worker // Moving index for LAST2, lag behind LAST by 2 frames.
3036*77c1e3ccSAndroid Build Coastguard Worker if (frame_number > 2) last2_idx = ((frame_number - 2) % sh);
3037*77c1e3ccSAndroid Build Coastguard Worker }
3038*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[0] = last_idx; // LAST
3039*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[1] = last_idx_refresh; // LAST2 (for refresh of last).
3040*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) {
3041*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[1] = last2_idx; // LAST2
3042*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[2] = last_idx_refresh; // LAST3 (for refresh of last).
3043*77c1e3ccSAndroid Build Coastguard Worker }
3044*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[3] = gld_idx; // GOLDEN
3045*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->ref_idx[6] = alt_ref_idx; // ALT_REF
3046*77c1e3ccSAndroid Build Coastguard Worker // Refresh this slot, which will become LAST on next frame.
3047*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->refresh[last_idx_refresh] = 1;
3048*77c1e3ccSAndroid Build Coastguard Worker // Update GOLDEN on period for fixed slot case.
3049*77c1e3ccSAndroid Build Coastguard Worker if (gf_update && cm->current_frame.frame_type != KEY_FRAME) {
3050*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->golden_frame = 1;
3051*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->refresh[gld_idx] = 1;
3052*77c1e3ccSAndroid Build Coastguard Worker }
3053*77c1e3ccSAndroid Build Coastguard Worker rtc_ref->gld_idx_1layer = gld_idx;
3054*77c1e3ccSAndroid Build Coastguard Worker // Set the flag to reduce the number of reference frame buffers used.
3055*77c1e3ccSAndroid Build Coastguard Worker // This assumes that slot 7 is never used.
3056*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers = 1;
3057*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[0] < 7);
3058*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[1] < 7);
3059*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[3] < 7);
3060*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[6] < 7);
3061*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1])
3062*77c1e3ccSAndroid Build Coastguard Worker cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[2] < 7);
3063*77c1e3ccSAndroid Build Coastguard Worker }
3064*77c1e3ccSAndroid Build Coastguard Worker
3065*77c1e3ccSAndroid Build Coastguard Worker // Returns whether the 64x64 block is active or inactive: used
3066*77c1e3ccSAndroid Build Coastguard Worker // by the scene detection, which is over 64x64 blocks.
set_block_is_active(unsigned char * const active_map_4x4,int mi_cols,int mi_rows,int sbi_col,int sbi_row)3067*77c1e3ccSAndroid Build Coastguard Worker static int set_block_is_active(unsigned char *const active_map_4x4, int mi_cols,
3068*77c1e3ccSAndroid Build Coastguard Worker int mi_rows, int sbi_col, int sbi_row) {
3069*77c1e3ccSAndroid Build Coastguard Worker int num_4x4 = 16;
3070*77c1e3ccSAndroid Build Coastguard Worker int r = sbi_row << 4;
3071*77c1e3ccSAndroid Build Coastguard Worker int c = sbi_col << 4;
3072*77c1e3ccSAndroid Build Coastguard Worker const int row_max = AOMMIN(num_4x4, mi_rows - r);
3073*77c1e3ccSAndroid Build Coastguard Worker const int col_max = AOMMIN(num_4x4, mi_cols - c);
3074*77c1e3ccSAndroid Build Coastguard Worker // Active map is set for 16x16 blocks, so only need to
3075*77c1e3ccSAndroid Build Coastguard Worker // check over16x16,
3076*77c1e3ccSAndroid Build Coastguard Worker for (int x = 0; x < row_max; x += 4) {
3077*77c1e3ccSAndroid Build Coastguard Worker for (int y = 0; y < col_max; y += 4) {
3078*77c1e3ccSAndroid Build Coastguard Worker if (active_map_4x4[(r + x) * mi_cols + (c + y)] == AM_SEGMENT_ID_ACTIVE)
3079*77c1e3ccSAndroid Build Coastguard Worker return 1;
3080*77c1e3ccSAndroid Build Coastguard Worker }
3081*77c1e3ccSAndroid Build Coastguard Worker }
3082*77c1e3ccSAndroid Build Coastguard Worker return 0;
3083*77c1e3ccSAndroid Build Coastguard Worker }
3084*77c1e3ccSAndroid Build Coastguard Worker
3085*77c1e3ccSAndroid Build Coastguard Worker // Returns the best sad for column or row motion of the superblock.
estimate_scroll_motion(const AV1_COMP * cpi,uint8_t * src_buf,uint8_t * last_src_buf,int src_stride,int ref_stride,BLOCK_SIZE bsize,int pos_col,int pos_row,int * best_intmv_col,int * best_intmv_row)3086*77c1e3ccSAndroid Build Coastguard Worker static unsigned int estimate_scroll_motion(
3087*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *cpi, uint8_t *src_buf, uint8_t *last_src_buf,
3088*77c1e3ccSAndroid Build Coastguard Worker int src_stride, int ref_stride, BLOCK_SIZE bsize, int pos_col, int pos_row,
3089*77c1e3ccSAndroid Build Coastguard Worker int *best_intmv_col, int *best_intmv_row) {
3090*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
3091*77c1e3ccSAndroid Build Coastguard Worker const int bw = block_size_wide[bsize];
3092*77c1e3ccSAndroid Build Coastguard Worker const int bh = block_size_high[bsize];
3093*77c1e3ccSAndroid Build Coastguard Worker const int full_search = 1;
3094*77c1e3ccSAndroid Build Coastguard Worker // Keep border a multiple of 16.
3095*77c1e3ccSAndroid Build Coastguard Worker const int border = (cpi->oxcf.border_in_pixels >> 4) << 4;
3096*77c1e3ccSAndroid Build Coastguard Worker // Make search_size_height larger to capture more common vertical scroll.
3097*77c1e3ccSAndroid Build Coastguard Worker // Increase the search if last two frames were dropped.
3098*77c1e3ccSAndroid Build Coastguard Worker // Values set based on screen test set.
3099*77c1e3ccSAndroid Build Coastguard Worker int search_size_width = 96;
3100*77c1e3ccSAndroid Build Coastguard Worker int search_size_height = cpi->rc.drop_count_consec > 1 ? 224 : 192;
3101*77c1e3ccSAndroid Build Coastguard Worker // Adjust based on boundary.
3102*77c1e3ccSAndroid Build Coastguard Worker if ((pos_col - search_size_width < -border) ||
3103*77c1e3ccSAndroid Build Coastguard Worker (pos_col + search_size_width > cm->width + border))
3104*77c1e3ccSAndroid Build Coastguard Worker search_size_width = border;
3105*77c1e3ccSAndroid Build Coastguard Worker if ((pos_row - search_size_height < -border) ||
3106*77c1e3ccSAndroid Build Coastguard Worker (pos_row + search_size_height > cm->height + border))
3107*77c1e3ccSAndroid Build Coastguard Worker search_size_height = border;
3108*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *ref_buf;
3109*77c1e3ccSAndroid Build Coastguard Worker const int row_norm_factor = mi_size_high_log2[bsize] + 1;
3110*77c1e3ccSAndroid Build Coastguard Worker const int col_norm_factor = 3 + (bw >> 5);
3111*77c1e3ccSAndroid Build Coastguard Worker const int ref_buf_width = (search_size_width << 1) + bw;
3112*77c1e3ccSAndroid Build Coastguard Worker const int ref_buf_height = (search_size_height << 1) + bh;
3113*77c1e3ccSAndroid Build Coastguard Worker int16_t *hbuf = (int16_t *)aom_malloc(ref_buf_width * sizeof(*hbuf));
3114*77c1e3ccSAndroid Build Coastguard Worker int16_t *vbuf = (int16_t *)aom_malloc(ref_buf_height * sizeof(*vbuf));
3115*77c1e3ccSAndroid Build Coastguard Worker int16_t *src_hbuf = (int16_t *)aom_malloc(bw * sizeof(*src_hbuf));
3116*77c1e3ccSAndroid Build Coastguard Worker int16_t *src_vbuf = (int16_t *)aom_malloc(bh * sizeof(*src_vbuf));
3117*77c1e3ccSAndroid Build Coastguard Worker if (!hbuf || !vbuf || !src_hbuf || !src_vbuf) {
3118*77c1e3ccSAndroid Build Coastguard Worker aom_free(hbuf);
3119*77c1e3ccSAndroid Build Coastguard Worker aom_free(vbuf);
3120*77c1e3ccSAndroid Build Coastguard Worker aom_free(src_hbuf);
3121*77c1e3ccSAndroid Build Coastguard Worker aom_free(src_vbuf);
3122*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
3123*77c1e3ccSAndroid Build Coastguard Worker "Failed to allocate hbuf, vbuf, src_hbuf, or src_vbuf");
3124*77c1e3ccSAndroid Build Coastguard Worker }
3125*77c1e3ccSAndroid Build Coastguard Worker // Set up prediction 1-D reference set for rows.
3126*77c1e3ccSAndroid Build Coastguard Worker ref_buf = last_src_buf - search_size_width;
3127*77c1e3ccSAndroid Build Coastguard Worker aom_int_pro_row(hbuf, ref_buf, ref_stride, ref_buf_width, bh,
3128*77c1e3ccSAndroid Build Coastguard Worker row_norm_factor);
3129*77c1e3ccSAndroid Build Coastguard Worker // Set up prediction 1-D reference set for cols
3130*77c1e3ccSAndroid Build Coastguard Worker ref_buf = last_src_buf - search_size_height * ref_stride;
3131*77c1e3ccSAndroid Build Coastguard Worker aom_int_pro_col(vbuf, ref_buf, ref_stride, bw, ref_buf_height,
3132*77c1e3ccSAndroid Build Coastguard Worker col_norm_factor);
3133*77c1e3ccSAndroid Build Coastguard Worker // Set up src 1-D reference set
3134*77c1e3ccSAndroid Build Coastguard Worker aom_int_pro_row(src_hbuf, src_buf, src_stride, bw, bh, row_norm_factor);
3135*77c1e3ccSAndroid Build Coastguard Worker aom_int_pro_col(src_vbuf, src_buf, src_stride, bw, bh, col_norm_factor);
3136*77c1e3ccSAndroid Build Coastguard Worker unsigned int best_sad;
3137*77c1e3ccSAndroid Build Coastguard Worker int best_sad_col, best_sad_row;
3138*77c1e3ccSAndroid Build Coastguard Worker // Find the best match per 1-D search
3139*77c1e3ccSAndroid Build Coastguard Worker *best_intmv_col =
3140*77c1e3ccSAndroid Build Coastguard Worker av1_vector_match(hbuf, src_hbuf, mi_size_wide_log2[bsize],
3141*77c1e3ccSAndroid Build Coastguard Worker search_size_width, full_search, &best_sad_col);
3142*77c1e3ccSAndroid Build Coastguard Worker *best_intmv_row =
3143*77c1e3ccSAndroid Build Coastguard Worker av1_vector_match(vbuf, src_vbuf, mi_size_high_log2[bsize],
3144*77c1e3ccSAndroid Build Coastguard Worker search_size_height, full_search, &best_sad_row);
3145*77c1e3ccSAndroid Build Coastguard Worker if (best_sad_col < best_sad_row) {
3146*77c1e3ccSAndroid Build Coastguard Worker *best_intmv_row = 0;
3147*77c1e3ccSAndroid Build Coastguard Worker best_sad = best_sad_col;
3148*77c1e3ccSAndroid Build Coastguard Worker } else {
3149*77c1e3ccSAndroid Build Coastguard Worker *best_intmv_col = 0;
3150*77c1e3ccSAndroid Build Coastguard Worker best_sad = best_sad_row;
3151*77c1e3ccSAndroid Build Coastguard Worker }
3152*77c1e3ccSAndroid Build Coastguard Worker aom_free(hbuf);
3153*77c1e3ccSAndroid Build Coastguard Worker aom_free(vbuf);
3154*77c1e3ccSAndroid Build Coastguard Worker aom_free(src_hbuf);
3155*77c1e3ccSAndroid Build Coastguard Worker aom_free(src_vbuf);
3156*77c1e3ccSAndroid Build Coastguard Worker return best_sad;
3157*77c1e3ccSAndroid Build Coastguard Worker }
3158*77c1e3ccSAndroid Build Coastguard Worker
3159*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Check for scene detection, for 1 pass real-time mode.
3160*77c1e3ccSAndroid Build Coastguard Worker *
3161*77c1e3ccSAndroid Build Coastguard Worker * Compute average source sad (temporal sad: between current source and
3162*77c1e3ccSAndroid Build Coastguard Worker * previous source) over a subset of superblocks. Use this is detect big changes
3163*77c1e3ccSAndroid Build Coastguard Worker * in content and set the \c cpi->rc.high_source_sad flag.
3164*77c1e3ccSAndroid Build Coastguard Worker *
3165*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
3166*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
3167*77c1e3ccSAndroid Build Coastguard Worker * \param[in] frame_input Current and last input source frames
3168*77c1e3ccSAndroid Build Coastguard Worker *
3169*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead the flag \c cpi->rc.high_source_sad
3170*77c1e3ccSAndroid Build Coastguard Worker * is set if scene change is detected, and \c cpi->rc.avg_source_sad is updated.
3171*77c1e3ccSAndroid Build Coastguard Worker */
rc_scene_detection_onepass_rt(AV1_COMP * cpi,const EncodeFrameInput * frame_input)3172*77c1e3ccSAndroid Build Coastguard Worker static void rc_scene_detection_onepass_rt(AV1_COMP *cpi,
3173*77c1e3ccSAndroid Build Coastguard Worker const EncodeFrameInput *frame_input) {
3174*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3175*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3176*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG const *const unscaled_src = frame_input->source;
3177*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG const *const unscaled_last_src = frame_input->last_source;
3178*77c1e3ccSAndroid Build Coastguard Worker uint8_t *src_y;
3179*77c1e3ccSAndroid Build Coastguard Worker int src_ystride;
3180*77c1e3ccSAndroid Build Coastguard Worker int src_width;
3181*77c1e3ccSAndroid Build Coastguard Worker int src_height;
3182*77c1e3ccSAndroid Build Coastguard Worker uint8_t *last_src_y;
3183*77c1e3ccSAndroid Build Coastguard Worker int last_src_ystride;
3184*77c1e3ccSAndroid Build Coastguard Worker int last_src_width;
3185*77c1e3ccSAndroid Build Coastguard Worker int last_src_height;
3186*77c1e3ccSAndroid Build Coastguard Worker int width = cm->width;
3187*77c1e3ccSAndroid Build Coastguard Worker int height = cm->height;
3188*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_spatial_layers > 1) {
3189*77c1e3ccSAndroid Build Coastguard Worker width = cpi->oxcf.frm_dim_cfg.width;
3190*77c1e3ccSAndroid Build Coastguard Worker height = cpi->oxcf.frm_dim_cfg.height;
3191*77c1e3ccSAndroid Build Coastguard Worker }
3192*77c1e3ccSAndroid Build Coastguard Worker if (width != cm->render_width || height != cm->render_height ||
3193*77c1e3ccSAndroid Build Coastguard Worker unscaled_src == NULL || unscaled_last_src == NULL) {
3194*77c1e3ccSAndroid Build Coastguard Worker aom_free(cpi->src_sad_blk_64x64);
3195*77c1e3ccSAndroid Build Coastguard Worker cpi->src_sad_blk_64x64 = NULL;
3196*77c1e3ccSAndroid Build Coastguard Worker }
3197*77c1e3ccSAndroid Build Coastguard Worker if (unscaled_src == NULL || unscaled_last_src == NULL) return;
3198*77c1e3ccSAndroid Build Coastguard Worker src_y = unscaled_src->y_buffer;
3199*77c1e3ccSAndroid Build Coastguard Worker src_ystride = unscaled_src->y_stride;
3200*77c1e3ccSAndroid Build Coastguard Worker src_width = unscaled_src->y_width;
3201*77c1e3ccSAndroid Build Coastguard Worker src_height = unscaled_src->y_height;
3202*77c1e3ccSAndroid Build Coastguard Worker last_src_y = unscaled_last_src->y_buffer;
3203*77c1e3ccSAndroid Build Coastguard Worker last_src_ystride = unscaled_last_src->y_stride;
3204*77c1e3ccSAndroid Build Coastguard Worker last_src_width = unscaled_last_src->y_width;
3205*77c1e3ccSAndroid Build Coastguard Worker last_src_height = unscaled_last_src->y_height;
3206*77c1e3ccSAndroid Build Coastguard Worker if (src_width != last_src_width || src_height != last_src_height) {
3207*77c1e3ccSAndroid Build Coastguard Worker aom_free(cpi->src_sad_blk_64x64);
3208*77c1e3ccSAndroid Build Coastguard Worker cpi->src_sad_blk_64x64 = NULL;
3209*77c1e3ccSAndroid Build Coastguard Worker return;
3210*77c1e3ccSAndroid Build Coastguard Worker }
3211*77c1e3ccSAndroid Build Coastguard Worker rc->high_source_sad = 0;
3212*77c1e3ccSAndroid Build Coastguard Worker rc->percent_blocks_with_motion = 0;
3213*77c1e3ccSAndroid Build Coastguard Worker rc->max_block_source_sad = 0;
3214*77c1e3ccSAndroid Build Coastguard Worker rc->prev_avg_source_sad = rc->avg_source_sad;
3215*77c1e3ccSAndroid Build Coastguard Worker int num_mi_cols = cm->mi_params.mi_cols;
3216*77c1e3ccSAndroid Build Coastguard Worker int num_mi_rows = cm->mi_params.mi_rows;
3217*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_spatial_layers > 1) {
3218*77c1e3ccSAndroid Build Coastguard Worker num_mi_cols = cpi->svc.mi_cols_full_resoln;
3219*77c1e3ccSAndroid Build Coastguard Worker num_mi_rows = cpi->svc.mi_rows_full_resoln;
3220*77c1e3ccSAndroid Build Coastguard Worker }
3221*77c1e3ccSAndroid Build Coastguard Worker int num_zero_temp_sad = 0;
3222*77c1e3ccSAndroid Build Coastguard Worker uint32_t min_thresh = 10000;
3223*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.higher_thresh_scene_detection) {
3224*77c1e3ccSAndroid Build Coastguard Worker min_thresh = cm->width * cm->height <= 320 * 240 && cpi->framerate < 10.0
3225*77c1e3ccSAndroid Build Coastguard Worker ? 50000
3226*77c1e3ccSAndroid Build Coastguard Worker : 100000;
3227*77c1e3ccSAndroid Build Coastguard Worker }
3228*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = BLOCK_64X64;
3229*77c1e3ccSAndroid Build Coastguard Worker // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
3230*77c1e3ccSAndroid Build Coastguard Worker uint64_t avg_sad = 0;
3231*77c1e3ccSAndroid Build Coastguard Worker uint64_t tmp_sad = 0;
3232*77c1e3ccSAndroid Build Coastguard Worker int num_samples = 0;
3233*77c1e3ccSAndroid Build Coastguard Worker const int thresh =
3234*77c1e3ccSAndroid Build Coastguard Worker cm->width * cm->height <= 320 * 240 && cpi->framerate < 10.0 ? 5 : 6;
3235*77c1e3ccSAndroid Build Coastguard Worker // SAD is computed on 64x64 blocks
3236*77c1e3ccSAndroid Build Coastguard Worker const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
3237*77c1e3ccSAndroid Build Coastguard Worker ? (cm->seq_params->mib_size >> 1)
3238*77c1e3ccSAndroid Build Coastguard Worker : cm->seq_params->mib_size;
3239*77c1e3ccSAndroid Build Coastguard Worker const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
3240*77c1e3ccSAndroid Build Coastguard Worker const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb;
3241*77c1e3ccSAndroid Build Coastguard Worker uint64_t sum_sq_thresh = 10000; // sum = sqrt(thresh / 64*64)) ~1.5
3242*77c1e3ccSAndroid Build Coastguard Worker int num_low_var_high_sumdiff = 0;
3243*77c1e3ccSAndroid Build Coastguard Worker int light_change = 0;
3244*77c1e3ccSAndroid Build Coastguard Worker // Flag to check light change or not.
3245*77c1e3ccSAndroid Build Coastguard Worker const int check_light_change = 0;
3246*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): There seems some difference along the bottom border when
3247*77c1e3ccSAndroid Build Coastguard Worker // using the source_last_tl0 for last_source (used for temporal layers or
3248*77c1e3ccSAndroid Build Coastguard Worker // when previous frame is dropped).
3249*77c1e3ccSAndroid Build Coastguard Worker // Remove this border parameter when issue is resolved: difference is that
3250*77c1e3ccSAndroid Build Coastguard Worker // non-zero sad exists along bottom border even though source is static.
3251*77c1e3ccSAndroid Build Coastguard Worker const int border =
3252*77c1e3ccSAndroid Build Coastguard Worker rc->prev_frame_is_dropped || cpi->svc.number_temporal_layers > 1;
3253*77c1e3ccSAndroid Build Coastguard Worker // Store blkwise SAD for later use
3254*77c1e3ccSAndroid Build Coastguard Worker if (width == cm->render_width && height == cm->render_height) {
3255*77c1e3ccSAndroid Build Coastguard Worker if (cpi->src_sad_blk_64x64 == NULL) {
3256*77c1e3ccSAndroid Build Coastguard Worker CHECK_MEM_ERROR(cm, cpi->src_sad_blk_64x64,
3257*77c1e3ccSAndroid Build Coastguard Worker (uint64_t *)aom_calloc(sb_cols * sb_rows,
3258*77c1e3ccSAndroid Build Coastguard Worker sizeof(*cpi->src_sad_blk_64x64)));
3259*77c1e3ccSAndroid Build Coastguard Worker }
3260*77c1e3ccSAndroid Build Coastguard Worker }
3261*77c1e3ccSAndroid Build Coastguard Worker const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
3262*77c1e3ccSAndroid Build Coastguard Worker const int mi_cols = mi_params->mi_cols;
3263*77c1e3ccSAndroid Build Coastguard Worker const int mi_rows = mi_params->mi_rows;
3264*77c1e3ccSAndroid Build Coastguard Worker unsigned char *const active_map_4x4 = cpi->active_map.map;
3265*77c1e3ccSAndroid Build Coastguard Worker // Avoid bottom and right border.
3266*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_row = 0; sbi_row < sb_rows - border; ++sbi_row) {
3267*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3268*77c1e3ccSAndroid Build Coastguard Worker int block_is_active = 1;
3269*77c1e3ccSAndroid Build Coastguard Worker if (cpi->active_map.enabled && rc->percent_blocks_inactive > 0) {
3270*77c1e3ccSAndroid Build Coastguard Worker block_is_active = set_block_is_active(active_map_4x4, mi_cols, mi_rows,
3271*77c1e3ccSAndroid Build Coastguard Worker sbi_col, sbi_row);
3272*77c1e3ccSAndroid Build Coastguard Worker }
3273*77c1e3ccSAndroid Build Coastguard Worker if (block_is_active) {
3274*77c1e3ccSAndroid Build Coastguard Worker tmp_sad = cpi->ppi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
3275*77c1e3ccSAndroid Build Coastguard Worker last_src_ystride);
3276*77c1e3ccSAndroid Build Coastguard Worker } else {
3277*77c1e3ccSAndroid Build Coastguard Worker tmp_sad = 0;
3278*77c1e3ccSAndroid Build Coastguard Worker }
3279*77c1e3ccSAndroid Build Coastguard Worker if (cpi->src_sad_blk_64x64 != NULL)
3280*77c1e3ccSAndroid Build Coastguard Worker cpi->src_sad_blk_64x64[sbi_col + sbi_row * sb_cols] = tmp_sad;
3281*77c1e3ccSAndroid Build Coastguard Worker if (check_light_change) {
3282*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse, variance;
3283*77c1e3ccSAndroid Build Coastguard Worker variance = cpi->ppi->fn_ptr[bsize].vf(src_y, src_ystride, last_src_y,
3284*77c1e3ccSAndroid Build Coastguard Worker last_src_ystride, &sse);
3285*77c1e3ccSAndroid Build Coastguard Worker // Note: sse - variance = ((sum * sum) >> 12)
3286*77c1e3ccSAndroid Build Coastguard Worker // Detect large lighting change.
3287*77c1e3ccSAndroid Build Coastguard Worker if (variance < (sse >> 1) && (sse - variance) > sum_sq_thresh) {
3288*77c1e3ccSAndroid Build Coastguard Worker num_low_var_high_sumdiff++;
3289*77c1e3ccSAndroid Build Coastguard Worker }
3290*77c1e3ccSAndroid Build Coastguard Worker }
3291*77c1e3ccSAndroid Build Coastguard Worker avg_sad += tmp_sad;
3292*77c1e3ccSAndroid Build Coastguard Worker num_samples++;
3293*77c1e3ccSAndroid Build Coastguard Worker if (tmp_sad == 0) num_zero_temp_sad++;
3294*77c1e3ccSAndroid Build Coastguard Worker if (tmp_sad > rc->max_block_source_sad)
3295*77c1e3ccSAndroid Build Coastguard Worker rc->max_block_source_sad = tmp_sad;
3296*77c1e3ccSAndroid Build Coastguard Worker
3297*77c1e3ccSAndroid Build Coastguard Worker src_y += 64;
3298*77c1e3ccSAndroid Build Coastguard Worker last_src_y += 64;
3299*77c1e3ccSAndroid Build Coastguard Worker }
3300*77c1e3ccSAndroid Build Coastguard Worker src_y += (src_ystride << 6) - (sb_cols << 6);
3301*77c1e3ccSAndroid Build Coastguard Worker last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
3302*77c1e3ccSAndroid Build Coastguard Worker }
3303*77c1e3ccSAndroid Build Coastguard Worker if (check_light_change && num_samples > 0 &&
3304*77c1e3ccSAndroid Build Coastguard Worker num_low_var_high_sumdiff > (num_samples >> 1))
3305*77c1e3ccSAndroid Build Coastguard Worker light_change = 1;
3306*77c1e3ccSAndroid Build Coastguard Worker if (num_samples > 0) avg_sad = avg_sad / num_samples;
3307*77c1e3ccSAndroid Build Coastguard Worker // Set high_source_sad flag if we detect very high increase in avg_sad
3308*77c1e3ccSAndroid Build Coastguard Worker // between current and previous frame value(s). Use minimum threshold
3309*77c1e3ccSAndroid Build Coastguard Worker // for cases where there is small change from content that is completely
3310*77c1e3ccSAndroid Build Coastguard Worker // static.
3311*77c1e3ccSAndroid Build Coastguard Worker if (!light_change &&
3312*77c1e3ccSAndroid Build Coastguard Worker avg_sad >
3313*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(min_thresh, (unsigned int)(rc->avg_source_sad * thresh)) &&
3314*77c1e3ccSAndroid Build Coastguard Worker rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
3315*77c1e3ccSAndroid Build Coastguard Worker num_zero_temp_sad < 3 * (num_samples >> 2))
3316*77c1e3ccSAndroid Build Coastguard Worker rc->high_source_sad = 1;
3317*77c1e3ccSAndroid Build Coastguard Worker else
3318*77c1e3ccSAndroid Build Coastguard Worker rc->high_source_sad = 0;
3319*77c1e3ccSAndroid Build Coastguard Worker rc->avg_source_sad = (3 * rc->avg_source_sad + avg_sad) >> 2;
3320*77c1e3ccSAndroid Build Coastguard Worker rc->frame_source_sad = avg_sad;
3321*77c1e3ccSAndroid Build Coastguard Worker if (num_samples > 0)
3322*77c1e3ccSAndroid Build Coastguard Worker rc->percent_blocks_with_motion =
3323*77c1e3ccSAndroid Build Coastguard Worker ((num_samples - num_zero_temp_sad) * 100) / num_samples;
3324*77c1e3ccSAndroid Build Coastguard Worker if (rc->frame_source_sad > 0) rc->static_since_last_scene_change = 0;
3325*77c1e3ccSAndroid Build Coastguard Worker if (rc->high_source_sad) {
3326*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.frames_since_scene_change = 0;
3327*77c1e3ccSAndroid Build Coastguard Worker rc->static_since_last_scene_change = 1;
3328*77c1e3ccSAndroid Build Coastguard Worker }
3329*77c1e3ccSAndroid Build Coastguard Worker // Update the high_motion_content_screen_rtc flag on TL0. Avoid the update
3330*77c1e3ccSAndroid Build Coastguard Worker // if too many consecutive frame drops occurred.
3331*77c1e3ccSAndroid Build Coastguard Worker const uint64_t thresh_high_motion = 9 * 64 * 64;
3332*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.temporal_layer_id == 0 && rc->drop_count_consec < 3) {
3333*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.high_motion_content_screen_rtc = 0;
3334*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.speed >= 11 &&
3335*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3336*77c1e3ccSAndroid Build Coastguard Worker rc->percent_blocks_with_motion > 40 &&
3337*77c1e3ccSAndroid Build Coastguard Worker rc->prev_avg_source_sad > thresh_high_motion &&
3338*77c1e3ccSAndroid Build Coastguard Worker rc->avg_source_sad > thresh_high_motion &&
3339*77c1e3ccSAndroid Build Coastguard Worker rc->avg_frame_low_motion < 60 && unscaled_src->y_width >= 1280 &&
3340*77c1e3ccSAndroid Build Coastguard Worker unscaled_src->y_height >= 720) {
3341*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.high_motion_content_screen_rtc = 1;
3342*77c1e3ccSAndroid Build Coastguard Worker // Compute fast coarse/global motion for 128x128 superblock centered
3343*77c1e3ccSAndroid Build Coastguard Worker // at middle of frames, to determine if motion is scroll.
3344*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): Only allow for 8 bit-depth for now.
3345*77c1e3ccSAndroid Build Coastguard Worker if (cm->seq_params->bit_depth == 8) {
3346*77c1e3ccSAndroid Build Coastguard Worker int pos_col = (unscaled_src->y_width >> 1) - 64;
3347*77c1e3ccSAndroid Build Coastguard Worker int pos_row = (unscaled_src->y_height >> 1) - 64;
3348*77c1e3ccSAndroid Build Coastguard Worker src_y = unscaled_src->y_buffer + pos_row * src_ystride + pos_col;
3349*77c1e3ccSAndroid Build Coastguard Worker last_src_y =
3350*77c1e3ccSAndroid Build Coastguard Worker unscaled_last_src->y_buffer + pos_row * last_src_ystride + pos_col;
3351*77c1e3ccSAndroid Build Coastguard Worker int best_intmv_col = 0;
3352*77c1e3ccSAndroid Build Coastguard Worker int best_intmv_row = 0;
3353*77c1e3ccSAndroid Build Coastguard Worker unsigned int y_sad = estimate_scroll_motion(
3354*77c1e3ccSAndroid Build Coastguard Worker cpi, src_y, last_src_y, src_ystride, last_src_ystride,
3355*77c1e3ccSAndroid Build Coastguard Worker BLOCK_128X128, pos_col, pos_row, &best_intmv_col, &best_intmv_row);
3356*77c1e3ccSAndroid Build Coastguard Worker if (y_sad < 100 &&
3357*77c1e3ccSAndroid Build Coastguard Worker (abs(best_intmv_col) > 16 || abs(best_intmv_row) > 16))
3358*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.high_motion_content_screen_rtc = 0;
3359*77c1e3ccSAndroid Build Coastguard Worker }
3360*77c1e3ccSAndroid Build Coastguard Worker }
3361*77c1e3ccSAndroid Build Coastguard Worker // Pass the flag value to all layer frames.
3362*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_spatial_layers > 1 ||
3363*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_temporal_layers > 1) {
3364*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
3365*77c1e3ccSAndroid Build Coastguard Worker for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
3366*77c1e3ccSAndroid Build Coastguard Worker for (int tl = 1; tl < svc->number_temporal_layers; ++tl) {
3367*77c1e3ccSAndroid Build Coastguard Worker const int layer =
3368*77c1e3ccSAndroid Build Coastguard Worker LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3369*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
3370*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *lrc = &lc->rc;
3371*77c1e3ccSAndroid Build Coastguard Worker lrc->high_motion_content_screen_rtc =
3372*77c1e3ccSAndroid Build Coastguard Worker rc->high_motion_content_screen_rtc;
3373*77c1e3ccSAndroid Build Coastguard Worker }
3374*77c1e3ccSAndroid Build Coastguard Worker }
3375*77c1e3ccSAndroid Build Coastguard Worker }
3376*77c1e3ccSAndroid Build Coastguard Worker }
3377*77c1e3ccSAndroid Build Coastguard Worker // Scene detection is only on base SLO, and using full/original resolution.
3378*77c1e3ccSAndroid Build Coastguard Worker // Pass the state to the upper spatial layers.
3379*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_spatial_layers > 1) {
3380*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
3381*77c1e3ccSAndroid Build Coastguard Worker for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
3382*77c1e3ccSAndroid Build Coastguard Worker int tl = svc->temporal_layer_id;
3383*77c1e3ccSAndroid Build Coastguard Worker const int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3384*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
3385*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *lrc = &lc->rc;
3386*77c1e3ccSAndroid Build Coastguard Worker lrc->high_source_sad = rc->high_source_sad;
3387*77c1e3ccSAndroid Build Coastguard Worker lrc->frame_source_sad = rc->frame_source_sad;
3388*77c1e3ccSAndroid Build Coastguard Worker lrc->avg_source_sad = rc->avg_source_sad;
3389*77c1e3ccSAndroid Build Coastguard Worker lrc->percent_blocks_with_motion = rc->percent_blocks_with_motion;
3390*77c1e3ccSAndroid Build Coastguard Worker lrc->max_block_source_sad = rc->max_block_source_sad;
3391*77c1e3ccSAndroid Build Coastguard Worker }
3392*77c1e3ccSAndroid Build Coastguard Worker }
3393*77c1e3ccSAndroid Build Coastguard Worker }
3394*77c1e3ccSAndroid Build Coastguard Worker
3395*77c1e3ccSAndroid Build Coastguard Worker // This is used as a reference when computing the source variance.
3396*77c1e3ccSAndroid Build Coastguard Worker static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = {
3397*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3398*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3399*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3400*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3401*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3402*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3403*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3404*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3405*77c1e3ccSAndroid Build Coastguard Worker 128, 128, 128, 128, 128, 128, 128, 128
3406*77c1e3ccSAndroid Build Coastguard Worker };
3407*77c1e3ccSAndroid Build Coastguard Worker
3408*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Compute spatial activity for keyframe, 1 pass real-time mode.
3409*77c1e3ccSAndroid Build Coastguard Worker *
3410*77c1e3ccSAndroid Build Coastguard Worker * Compute average spatial activity/variance for source frame over a
3411*77c1e3ccSAndroid Build Coastguard Worker * subset of superblocks.
3412*77c1e3ccSAndroid Build Coastguard Worker *
3413*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
3414*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
3415*77c1e3ccSAndroid Build Coastguard Worker * \param[in] src_y Input source buffer for y channel.
3416*77c1e3ccSAndroid Build Coastguard Worker * \param[in] src_ystride Input source stride for y channel.
3417*77c1e3ccSAndroid Build Coastguard Worker *
3418*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead the average spatial variance
3419*77c1e3ccSAndroid Build Coastguard Worker * computed is stored in flag \c cpi->rc.frame_spatial_variance.
3420*77c1e3ccSAndroid Build Coastguard Worker */
rc_spatial_act_keyframe_onepass_rt(AV1_COMP * cpi,uint8_t * src_y,int src_ystride)3421*77c1e3ccSAndroid Build Coastguard Worker static void rc_spatial_act_keyframe_onepass_rt(AV1_COMP *cpi, uint8_t *src_y,
3422*77c1e3ccSAndroid Build Coastguard Worker int src_ystride) {
3423*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3424*77c1e3ccSAndroid Build Coastguard Worker int num_mi_cols = cm->mi_params.mi_cols;
3425*77c1e3ccSAndroid Build Coastguard Worker int num_mi_rows = cm->mi_params.mi_rows;
3426*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = BLOCK_64X64;
3427*77c1e3ccSAndroid Build Coastguard Worker // Loop over sub-sample of frame, compute average over 64x64 blocks.
3428*77c1e3ccSAndroid Build Coastguard Worker uint64_t avg_variance = 0;
3429*77c1e3ccSAndroid Build Coastguard Worker int num_samples = 0;
3430*77c1e3ccSAndroid Build Coastguard Worker int num_zero_var_blocks = 0;
3431*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.perc_flat_blocks_keyframe = 0;
3432*77c1e3ccSAndroid Build Coastguard Worker const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
3433*77c1e3ccSAndroid Build Coastguard Worker ? (cm->seq_params->mib_size >> 1)
3434*77c1e3ccSAndroid Build Coastguard Worker : cm->seq_params->mib_size;
3435*77c1e3ccSAndroid Build Coastguard Worker const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
3436*77c1e3ccSAndroid Build Coastguard Worker const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb;
3437*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
3438*77c1e3ccSAndroid Build Coastguard Worker for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3439*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
3440*77c1e3ccSAndroid Build Coastguard Worker const unsigned int var =
3441*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->fn_ptr[bsize].vf(src_y, src_ystride, AV1_VAR_OFFS, 0, &sse);
3442*77c1e3ccSAndroid Build Coastguard Worker avg_variance += var;
3443*77c1e3ccSAndroid Build Coastguard Worker num_samples++;
3444*77c1e3ccSAndroid Build Coastguard Worker if (var == 0) num_zero_var_blocks++;
3445*77c1e3ccSAndroid Build Coastguard Worker src_y += 64;
3446*77c1e3ccSAndroid Build Coastguard Worker }
3447*77c1e3ccSAndroid Build Coastguard Worker src_y += (src_ystride << 6) - (sb_cols << 6);
3448*77c1e3ccSAndroid Build Coastguard Worker }
3449*77c1e3ccSAndroid Build Coastguard Worker if (num_samples > 0) {
3450*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.perc_flat_blocks_keyframe = 100 * num_zero_var_blocks / num_samples;
3451*77c1e3ccSAndroid Build Coastguard Worker avg_variance = avg_variance / num_samples;
3452*77c1e3ccSAndroid Build Coastguard Worker }
3453*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.frame_spatial_variance = avg_variance >> 12;
3454*77c1e3ccSAndroid Build Coastguard Worker }
3455*77c1e3ccSAndroid Build Coastguard Worker
3456*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Set the GF baseline interval for 1 pass real-time mode.
3457*77c1e3ccSAndroid Build Coastguard Worker *
3458*77c1e3ccSAndroid Build Coastguard Worker *
3459*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
3460*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
3461*77c1e3ccSAndroid Build Coastguard Worker * \param[in] frame_type frame type
3462*77c1e3ccSAndroid Build Coastguard Worker *
3463*77c1e3ccSAndroid Build Coastguard Worker * \return Return GF update flag, and update the \c cpi->rc with
3464*77c1e3ccSAndroid Build Coastguard Worker * the next GF interval settings.
3465*77c1e3ccSAndroid Build Coastguard Worker */
set_gf_interval_update_onepass_rt(AV1_COMP * cpi,FRAME_TYPE frame_type)3466*77c1e3ccSAndroid Build Coastguard Worker static int set_gf_interval_update_onepass_rt(AV1_COMP *cpi,
3467*77c1e3ccSAndroid Build Coastguard Worker FRAME_TYPE frame_type) {
3468*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3469*77c1e3ccSAndroid Build Coastguard Worker int gf_update = 0;
3470*77c1e3ccSAndroid Build Coastguard Worker const int resize_pending = is_frame_resize_pending(cpi);
3471*77c1e3ccSAndroid Build Coastguard Worker // GF update based on frames_till_gf_update_due, also
3472*77c1e3ccSAndroid Build Coastguard Worker // force update on resize pending frame or for scene change.
3473*77c1e3ccSAndroid Build Coastguard Worker if ((resize_pending || rc->high_source_sad ||
3474*77c1e3ccSAndroid Build Coastguard Worker rc->frames_till_gf_update_due == 0) &&
3475*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.temporal_layer_id == 0 && cpi->svc.spatial_layer_id == 0) {
3476*77c1e3ccSAndroid Build Coastguard Worker set_baseline_gf_interval(cpi, frame_type);
3477*77c1e3ccSAndroid Build Coastguard Worker gf_update = 1;
3478*77c1e3ccSAndroid Build Coastguard Worker }
3479*77c1e3ccSAndroid Build Coastguard Worker return gf_update;
3480*77c1e3ccSAndroid Build Coastguard Worker }
3481*77c1e3ccSAndroid Build Coastguard Worker
resize_reset_rc(AV1_COMP * cpi,int resize_width,int resize_height,int prev_width,int prev_height)3482*77c1e3ccSAndroid Build Coastguard Worker static void resize_reset_rc(AV1_COMP *cpi, int resize_width, int resize_height,
3483*77c1e3ccSAndroid Build Coastguard Worker int prev_width, int prev_height) {
3484*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3485*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3486*77c1e3ccSAndroid Build Coastguard Worker SVC *const svc = &cpi->svc;
3487*77c1e3ccSAndroid Build Coastguard Worker int target_bits_per_frame;
3488*77c1e3ccSAndroid Build Coastguard Worker int active_worst_quality;
3489*77c1e3ccSAndroid Build Coastguard Worker int qindex;
3490*77c1e3ccSAndroid Build Coastguard Worker double tot_scale_change = (double)(resize_width * resize_height) /
3491*77c1e3ccSAndroid Build Coastguard Worker (double)(prev_width * prev_height);
3492*77c1e3ccSAndroid Build Coastguard Worker // Disable the skip mv search for svc on resize frame.
3493*77c1e3ccSAndroid Build Coastguard Worker svc->skip_mvsearch_last = 0;
3494*77c1e3ccSAndroid Build Coastguard Worker svc->skip_mvsearch_gf = 0;
3495*77c1e3ccSAndroid Build Coastguard Worker svc->skip_mvsearch_altref = 0;
3496*77c1e3ccSAndroid Build Coastguard Worker // Reset buffer level to optimal, update target size.
3497*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level = p_rc->optimal_buffer_level;
3498*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target = p_rc->optimal_buffer_level;
3499*77c1e3ccSAndroid Build Coastguard Worker rc->this_frame_target =
3500*77c1e3ccSAndroid Build Coastguard Worker av1_calc_pframe_target_size_one_pass_cbr(cpi, INTER_FRAME);
3501*77c1e3ccSAndroid Build Coastguard Worker target_bits_per_frame = rc->this_frame_target;
3502*77c1e3ccSAndroid Build Coastguard Worker if (tot_scale_change > 4.0)
3503*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
3504*77c1e3ccSAndroid Build Coastguard Worker else if (tot_scale_change > 1.0)
3505*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] =
3506*77c1e3ccSAndroid Build Coastguard Worker (p_rc->avg_frame_qindex[INTER_FRAME] + rc->worst_quality) >> 1;
3507*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi);
3508*77c1e3ccSAndroid Build Coastguard Worker qindex = av1_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
3509*77c1e3ccSAndroid Build Coastguard Worker active_worst_quality, resize_width, resize_height);
3510*77c1e3ccSAndroid Build Coastguard Worker // If resize is down, check if projected q index is close to worst_quality,
3511*77c1e3ccSAndroid Build Coastguard Worker // and if so, reduce the rate correction factor (since likely can afford
3512*77c1e3ccSAndroid Build Coastguard Worker // lower q for resized frame).
3513*77c1e3ccSAndroid Build Coastguard Worker if (tot_scale_change < 1.0 && qindex > 90 * rc->worst_quality / 100)
3514*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
3515*77c1e3ccSAndroid Build Coastguard Worker // If resize is back up: check if projected q index is too much above the
3516*77c1e3ccSAndroid Build Coastguard Worker // previous index, and if so, reduce the rate correction factor
3517*77c1e3ccSAndroid Build Coastguard Worker // (since prefer to keep q for resized frame at least closet to previous q).
3518*77c1e3ccSAndroid Build Coastguard Worker // Also check if projected qindex is close to previous qindex, if so
3519*77c1e3ccSAndroid Build Coastguard Worker // increase correction factor (to push qindex higher and avoid overshoot).
3520*77c1e3ccSAndroid Build Coastguard Worker if (tot_scale_change >= 1.0) {
3521*77c1e3ccSAndroid Build Coastguard Worker if (tot_scale_change < 4.0 &&
3522*77c1e3ccSAndroid Build Coastguard Worker qindex > 130 * p_rc->last_q[INTER_FRAME] / 100)
3523*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[INTER_NORMAL] *= 0.8;
3524*77c1e3ccSAndroid Build Coastguard Worker if (qindex <= 120 * p_rc->last_q[INTER_FRAME] / 100)
3525*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[INTER_NORMAL] *= 1.5;
3526*77c1e3ccSAndroid Build Coastguard Worker }
3527*77c1e3ccSAndroid Build Coastguard Worker if (svc->number_temporal_layers > 1) {
3528*77c1e3ccSAndroid Build Coastguard Worker // Apply the same rate control reset to all temporal layers.
3529*77c1e3ccSAndroid Build Coastguard Worker for (int tl = 0; tl < svc->number_temporal_layers; tl++) {
3530*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = NULL;
3531*77c1e3ccSAndroid Build Coastguard Worker lc = &svc->layer_context[svc->spatial_layer_id *
3532*77c1e3ccSAndroid Build Coastguard Worker svc->number_temporal_layers +
3533*77c1e3ccSAndroid Build Coastguard Worker tl];
3534*77c1e3ccSAndroid Build Coastguard Worker lc->rc.resize_state = rc->resize_state;
3535*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.buffer_level = lc->p_rc.optimal_buffer_level;
3536*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.bits_off_target = lc->p_rc.optimal_buffer_level;
3537*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.rate_correction_factors[INTER_NORMAL] =
3538*77c1e3ccSAndroid Build Coastguard Worker p_rc->rate_correction_factors[INTER_NORMAL];
3539*77c1e3ccSAndroid Build Coastguard Worker lc->p_rc.avg_frame_qindex[INTER_FRAME] =
3540*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME];
3541*77c1e3ccSAndroid Build Coastguard Worker }
3542*77c1e3ccSAndroid Build Coastguard Worker }
3543*77c1e3ccSAndroid Build Coastguard Worker }
3544*77c1e3ccSAndroid Build Coastguard Worker
3545*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Check for resize based on Q, for 1 pass real-time mode.
3546*77c1e3ccSAndroid Build Coastguard Worker *
3547*77c1e3ccSAndroid Build Coastguard Worker * Check if we should resize, based on average QP from past x frames.
3548*77c1e3ccSAndroid Build Coastguard Worker * Only allow for resize at most 1/2 scale down for now, Scaling factor
3549*77c1e3ccSAndroid Build Coastguard Worker * for each step may be 3/4 or 1/2.
3550*77c1e3ccSAndroid Build Coastguard Worker *
3551*77c1e3ccSAndroid Build Coastguard Worker * \ingroup rate_control
3552*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top level encoder structure
3553*77c1e3ccSAndroid Build Coastguard Worker *
3554*77c1e3ccSAndroid Build Coastguard Worker * \remark Return resized width/height in \c cpi->resize_pending_params,
3555*77c1e3ccSAndroid Build Coastguard Worker * and update some resize counters in \c rc.
3556*77c1e3ccSAndroid Build Coastguard Worker */
dynamic_resize_one_pass_cbr(AV1_COMP * cpi)3557*77c1e3ccSAndroid Build Coastguard Worker static void dynamic_resize_one_pass_cbr(AV1_COMP *cpi) {
3558*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
3559*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3560*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3561*77c1e3ccSAndroid Build Coastguard Worker RESIZE_ACTION resize_action = NO_RESIZE;
3562*77c1e3ccSAndroid Build Coastguard Worker const int avg_qp_thr1 = 70;
3563*77c1e3ccSAndroid Build Coastguard Worker const int avg_qp_thr2 = 50;
3564*77c1e3ccSAndroid Build Coastguard Worker // Don't allow for resized frame to go below 160x90, resize in steps of 3/4.
3565*77c1e3ccSAndroid Build Coastguard Worker const int min_width = (160 * 4) / 3;
3566*77c1e3ccSAndroid Build Coastguard Worker const int min_height = (90 * 4) / 3;
3567*77c1e3ccSAndroid Build Coastguard Worker int down_size_on = 1;
3568*77c1e3ccSAndroid Build Coastguard Worker // Don't resize on key frame; reset the counters on key frame.
3569*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_type == KEY_FRAME) {
3570*77c1e3ccSAndroid Build Coastguard Worker rc->resize_avg_qp = 0;
3571*77c1e3ccSAndroid Build Coastguard Worker rc->resize_count = 0;
3572*77c1e3ccSAndroid Build Coastguard Worker rc->resize_buffer_underflow = 0;
3573*77c1e3ccSAndroid Build Coastguard Worker return;
3574*77c1e3ccSAndroid Build Coastguard Worker }
3575*77c1e3ccSAndroid Build Coastguard Worker // No resizing down if frame size is below some limit.
3576*77c1e3ccSAndroid Build Coastguard Worker if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0;
3577*77c1e3ccSAndroid Build Coastguard Worker
3578*77c1e3ccSAndroid Build Coastguard Worker // Resize based on average buffer underflow and QP over some window.
3579*77c1e3ccSAndroid Build Coastguard Worker // Ignore samples close to key frame, since QP is usually high after key.
3580*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.frames_since_key > cpi->framerate) {
3581*77c1e3ccSAndroid Build Coastguard Worker const int window = AOMMIN(30, (int)(2 * cpi->framerate));
3582*77c1e3ccSAndroid Build Coastguard Worker rc->resize_avg_qp += p_rc->last_q[INTER_FRAME];
3583*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->p_rc.buffer_level <
3584*77c1e3ccSAndroid Build Coastguard Worker (int)(30 * p_rc->optimal_buffer_level / 100))
3585*77c1e3ccSAndroid Build Coastguard Worker ++rc->resize_buffer_underflow;
3586*77c1e3ccSAndroid Build Coastguard Worker ++rc->resize_count;
3587*77c1e3ccSAndroid Build Coastguard Worker // Check for resize action every "window" frames.
3588*77c1e3ccSAndroid Build Coastguard Worker if (rc->resize_count >= window) {
3589*77c1e3ccSAndroid Build Coastguard Worker int avg_qp = rc->resize_avg_qp / rc->resize_count;
3590*77c1e3ccSAndroid Build Coastguard Worker // Resize down if buffer level has underflowed sufficient amount in past
3591*77c1e3ccSAndroid Build Coastguard Worker // window, and we are at original or 3/4 of original resolution.
3592*77c1e3ccSAndroid Build Coastguard Worker // Resize back up if average QP is low, and we are currently in a resized
3593*77c1e3ccSAndroid Build Coastguard Worker // down state, i.e. 1/2 or 3/4 of original resolution.
3594*77c1e3ccSAndroid Build Coastguard Worker // Currently, use a flag to turn 3/4 resizing feature on/off.
3595*77c1e3ccSAndroid Build Coastguard Worker if (rc->resize_buffer_underflow > (rc->resize_count >> 2) &&
3596*77c1e3ccSAndroid Build Coastguard Worker down_size_on) {
3597*77c1e3ccSAndroid Build Coastguard Worker if (rc->resize_state == THREE_QUARTER) {
3598*77c1e3ccSAndroid Build Coastguard Worker resize_action = DOWN_ONEHALF;
3599*77c1e3ccSAndroid Build Coastguard Worker rc->resize_state = ONE_HALF;
3600*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->resize_state == ORIG) {
3601*77c1e3ccSAndroid Build Coastguard Worker resize_action = DOWN_THREEFOUR;
3602*77c1e3ccSAndroid Build Coastguard Worker rc->resize_state = THREE_QUARTER;
3603*77c1e3ccSAndroid Build Coastguard Worker }
3604*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->resize_state != ORIG &&
3605*77c1e3ccSAndroid Build Coastguard Worker avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
3606*77c1e3ccSAndroid Build Coastguard Worker if (rc->resize_state == THREE_QUARTER ||
3607*77c1e3ccSAndroid Build Coastguard Worker avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100) {
3608*77c1e3ccSAndroid Build Coastguard Worker resize_action = UP_ORIG;
3609*77c1e3ccSAndroid Build Coastguard Worker rc->resize_state = ORIG;
3610*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->resize_state == ONE_HALF) {
3611*77c1e3ccSAndroid Build Coastguard Worker resize_action = UP_THREEFOUR;
3612*77c1e3ccSAndroid Build Coastguard Worker rc->resize_state = THREE_QUARTER;
3613*77c1e3ccSAndroid Build Coastguard Worker }
3614*77c1e3ccSAndroid Build Coastguard Worker }
3615*77c1e3ccSAndroid Build Coastguard Worker // Reset for next window measurement.
3616*77c1e3ccSAndroid Build Coastguard Worker rc->resize_avg_qp = 0;
3617*77c1e3ccSAndroid Build Coastguard Worker rc->resize_count = 0;
3618*77c1e3ccSAndroid Build Coastguard Worker rc->resize_buffer_underflow = 0;
3619*77c1e3ccSAndroid Build Coastguard Worker }
3620*77c1e3ccSAndroid Build Coastguard Worker }
3621*77c1e3ccSAndroid Build Coastguard Worker // If decision is to resize, reset some quantities, and check is we should
3622*77c1e3ccSAndroid Build Coastguard Worker // reduce rate correction factor,
3623*77c1e3ccSAndroid Build Coastguard Worker if (resize_action != NO_RESIZE) {
3624*77c1e3ccSAndroid Build Coastguard Worker int resize_width = cpi->oxcf.frm_dim_cfg.width;
3625*77c1e3ccSAndroid Build Coastguard Worker int resize_height = cpi->oxcf.frm_dim_cfg.height;
3626*77c1e3ccSAndroid Build Coastguard Worker int resize_scale_num = 1;
3627*77c1e3ccSAndroid Build Coastguard Worker int resize_scale_den = 1;
3628*77c1e3ccSAndroid Build Coastguard Worker if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
3629*77c1e3ccSAndroid Build Coastguard Worker resize_scale_num = 3;
3630*77c1e3ccSAndroid Build Coastguard Worker resize_scale_den = 4;
3631*77c1e3ccSAndroid Build Coastguard Worker } else if (resize_action == DOWN_ONEHALF) {
3632*77c1e3ccSAndroid Build Coastguard Worker resize_scale_num = 1;
3633*77c1e3ccSAndroid Build Coastguard Worker resize_scale_den = 2;
3634*77c1e3ccSAndroid Build Coastguard Worker }
3635*77c1e3ccSAndroid Build Coastguard Worker resize_width = resize_width * resize_scale_num / resize_scale_den;
3636*77c1e3ccSAndroid Build Coastguard Worker resize_height = resize_height * resize_scale_num / resize_scale_den;
3637*77c1e3ccSAndroid Build Coastguard Worker resize_reset_rc(cpi, resize_width, resize_height, cm->width, cm->height);
3638*77c1e3ccSAndroid Build Coastguard Worker }
3639*77c1e3ccSAndroid Build Coastguard Worker return;
3640*77c1e3ccSAndroid Build Coastguard Worker }
3641*77c1e3ccSAndroid Build Coastguard Worker
set_key_frame(AV1_COMP * cpi,unsigned int frame_flags)3642*77c1e3ccSAndroid Build Coastguard Worker static inline int set_key_frame(AV1_COMP *cpi, unsigned int frame_flags) {
3643*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3644*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3645*77c1e3ccSAndroid Build Coastguard Worker SVC *const svc = &cpi->svc;
3646*77c1e3ccSAndroid Build Coastguard Worker
3647*77c1e3ccSAndroid Build Coastguard Worker // Very first frame has to be key frame.
3648*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_number == 0) return 1;
3649*77c1e3ccSAndroid Build Coastguard Worker // Set key frame if forced by frame flags.
3650*77c1e3ccSAndroid Build Coastguard Worker if (frame_flags & FRAMEFLAGS_KEY) return 1;
3651*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->ppi->use_svc) {
3652*77c1e3ccSAndroid Build Coastguard Worker // Non-SVC
3653*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.kf_cfg.auto_key && rc->frames_to_key == 0) return 1;
3654*77c1e3ccSAndroid Build Coastguard Worker } else {
3655*77c1e3ccSAndroid Build Coastguard Worker // SVC
3656*77c1e3ccSAndroid Build Coastguard Worker if (svc->spatial_layer_id == 0 &&
3657*77c1e3ccSAndroid Build Coastguard Worker (cpi->oxcf.kf_cfg.auto_key &&
3658*77c1e3ccSAndroid Build Coastguard Worker (cpi->oxcf.kf_cfg.key_freq_max == 0 ||
3659*77c1e3ccSAndroid Build Coastguard Worker svc->current_superframe % cpi->oxcf.kf_cfg.key_freq_max == 0)))
3660*77c1e3ccSAndroid Build Coastguard Worker return 1;
3661*77c1e3ccSAndroid Build Coastguard Worker }
3662*77c1e3ccSAndroid Build Coastguard Worker
3663*77c1e3ccSAndroid Build Coastguard Worker return 0;
3664*77c1e3ccSAndroid Build Coastguard Worker }
3665*77c1e3ccSAndroid Build Coastguard Worker
3666*77c1e3ccSAndroid Build Coastguard Worker // Set to true if this frame is a recovery frame, for 1 layer RPS,
3667*77c1e3ccSAndroid Build Coastguard Worker // and whether we should apply some boost (QP, adjust speed features, etc).
3668*77c1e3ccSAndroid Build Coastguard Worker // Recovery frame here means frame whose closest reference suddenly
3669*77c1e3ccSAndroid Build Coastguard Worker // switched from previous frame to one much further away.
3670*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): Consider adding on/off flag to SVC_REF_FRAME_CONFIG to
3671*77c1e3ccSAndroid Build Coastguard Worker // allow more control for applications.
set_flag_rps_bias_recovery_frame(const AV1_COMP * const cpi)3672*77c1e3ccSAndroid Build Coastguard Worker static bool set_flag_rps_bias_recovery_frame(const AV1_COMP *const cpi) {
3673*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.set_ref_frame_config &&
3674*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_temporal_layers == 1 &&
3675*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_spatial_layers == 1 &&
3676*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->rtc_ref.reference_was_previous_frame) {
3677*77c1e3ccSAndroid Build Coastguard Worker int min_dist = av1_svc_get_min_ref_dist(cpi);
3678*77c1e3ccSAndroid Build Coastguard Worker // Only consider boost for this frame if its closest reference is further
3679*77c1e3ccSAndroid Build Coastguard Worker // than x frames away, using x = 4 for now.
3680*77c1e3ccSAndroid Build Coastguard Worker if (min_dist != INT_MAX && min_dist > 4) return true;
3681*77c1e3ccSAndroid Build Coastguard Worker }
3682*77c1e3ccSAndroid Build Coastguard Worker return false;
3683*77c1e3ccSAndroid Build Coastguard Worker }
3684*77c1e3ccSAndroid Build Coastguard Worker
av1_get_one_pass_rt_params(AV1_COMP * cpi,FRAME_TYPE * const frame_type,const EncodeFrameInput * frame_input,unsigned int frame_flags)3685*77c1e3ccSAndroid Build Coastguard Worker void av1_get_one_pass_rt_params(AV1_COMP *cpi, FRAME_TYPE *const frame_type,
3686*77c1e3ccSAndroid Build Coastguard Worker const EncodeFrameInput *frame_input,
3687*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_flags) {
3688*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *const rc = &cpi->rc;
3689*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3690*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3691*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *const gf_group = &cpi->ppi->gf_group;
3692*77c1e3ccSAndroid Build Coastguard Worker SVC *const svc = &cpi->svc;
3693*77c1e3ccSAndroid Build Coastguard Worker ResizePendingParams *const resize_pending_params =
3694*77c1e3ccSAndroid Build Coastguard Worker &cpi->resize_pending_params;
3695*77c1e3ccSAndroid Build Coastguard Worker int target;
3696*77c1e3ccSAndroid Build Coastguard Worker const int layer =
3697*77c1e3ccSAndroid Build Coastguard Worker LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
3698*77c1e3ccSAndroid Build Coastguard Worker svc->number_temporal_layers);
3699*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.max_consec_drop_ms > 0) {
3700*77c1e3ccSAndroid Build Coastguard Worker double framerate =
3701*77c1e3ccSAndroid Build Coastguard Worker cpi->framerate > 1 ? round(cpi->framerate) : cpi->framerate;
3702*77c1e3ccSAndroid Build Coastguard Worker rc->max_consec_drop = saturate_cast_double_to_int(
3703*77c1e3ccSAndroid Build Coastguard Worker ceil(cpi->oxcf.rc_cfg.max_consec_drop_ms * framerate / 1000));
3704*77c1e3ccSAndroid Build Coastguard Worker }
3705*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc) {
3706*77c1e3ccSAndroid Build Coastguard Worker av1_update_temporal_layer_framerate(cpi);
3707*77c1e3ccSAndroid Build Coastguard Worker av1_restore_layer_context(cpi);
3708*77c1e3ccSAndroid Build Coastguard Worker }
3709*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->rtc_ref.bias_recovery_frame = set_flag_rps_bias_recovery_frame(cpi);
3710*77c1e3ccSAndroid Build Coastguard Worker // Set frame type.
3711*77c1e3ccSAndroid Build Coastguard Worker if (set_key_frame(cpi, frame_flags)) {
3712*77c1e3ccSAndroid Build Coastguard Worker *frame_type = KEY_FRAME;
3713*77c1e3ccSAndroid Build Coastguard Worker p_rc->this_key_frame_forced =
3714*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_number != 0 && rc->frames_to_key == 0;
3715*77c1e3ccSAndroid Build Coastguard Worker rc->frames_to_key = cpi->oxcf.kf_cfg.key_freq_max;
3716*77c1e3ccSAndroid Build Coastguard Worker p_rc->kf_boost = DEFAULT_KF_BOOST_RT;
3717*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] = KF_UPDATE;
3718*77c1e3ccSAndroid Build Coastguard Worker gf_group->frame_type[cpi->gf_frame_index] = KEY_FRAME;
3719*77c1e3ccSAndroid Build Coastguard Worker gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_RESET;
3720*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc) {
3721*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_number > 0)
3722*77c1e3ccSAndroid Build Coastguard Worker av1_svc_reset_temporal_layers(cpi, 1);
3723*77c1e3ccSAndroid Build Coastguard Worker svc->layer_context[layer].is_key_frame = 1;
3724*77c1e3ccSAndroid Build Coastguard Worker }
3725*77c1e3ccSAndroid Build Coastguard Worker rc->frame_number_encoded = 0;
3726*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->rtc_ref.non_reference_frame = 0;
3727*77c1e3ccSAndroid Build Coastguard Worker rc->static_since_last_scene_change = 0;
3728*77c1e3ccSAndroid Build Coastguard Worker } else {
3729*77c1e3ccSAndroid Build Coastguard Worker *frame_type = INTER_FRAME;
3730*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] = LF_UPDATE;
3731*77c1e3ccSAndroid Build Coastguard Worker gf_group->frame_type[cpi->gf_frame_index] = INTER_FRAME;
3732*77c1e3ccSAndroid Build Coastguard Worker gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_UPDATE;
3733*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc) {
3734*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
3735*77c1e3ccSAndroid Build Coastguard Worker lc->is_key_frame =
3736*77c1e3ccSAndroid Build Coastguard Worker svc->spatial_layer_id == 0
3737*77c1e3ccSAndroid Build Coastguard Worker ? 0
3738*77c1e3ccSAndroid Build Coastguard Worker : svc->layer_context[svc->temporal_layer_id].is_key_frame;
3739*77c1e3ccSAndroid Build Coastguard Worker }
3740*77c1e3ccSAndroid Build Coastguard Worker // If the user is setting the reference structure with
3741*77c1e3ccSAndroid Build Coastguard Worker // set_ref_frame_config and did not set any references, set the
3742*77c1e3ccSAndroid Build Coastguard Worker // frame type to Intra-only.
3743*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.set_ref_frame_config) {
3744*77c1e3ccSAndroid Build Coastguard Worker int no_references_set = 1;
3745*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
3746*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.reference[i]) {
3747*77c1e3ccSAndroid Build Coastguard Worker no_references_set = 0;
3748*77c1e3ccSAndroid Build Coastguard Worker break;
3749*77c1e3ccSAndroid Build Coastguard Worker }
3750*77c1e3ccSAndroid Build Coastguard Worker }
3751*77c1e3ccSAndroid Build Coastguard Worker
3752*77c1e3ccSAndroid Build Coastguard Worker // Set to intra_only_frame if no references are set.
3753*77c1e3ccSAndroid Build Coastguard Worker // The stream can start decoding on INTRA_ONLY_FRAME so long as the
3754*77c1e3ccSAndroid Build Coastguard Worker // layer with the intra_only_frame doesn't signal a reference to a slot
3755*77c1e3ccSAndroid Build Coastguard Worker // that hasn't been set yet.
3756*77c1e3ccSAndroid Build Coastguard Worker if (no_references_set) *frame_type = INTRA_ONLY_FRAME;
3757*77c1e3ccSAndroid Build Coastguard Worker }
3758*77c1e3ccSAndroid Build Coastguard Worker }
3759*77c1e3ccSAndroid Build Coastguard Worker if (cpi->active_map.enabled && cpi->rc.percent_blocks_inactive == 100) {
3760*77c1e3ccSAndroid Build Coastguard Worker rc->frame_source_sad = 0;
3761*77c1e3ccSAndroid Build Coastguard Worker rc->avg_source_sad = (3 * rc->avg_source_sad + rc->frame_source_sad) >> 2;
3762*77c1e3ccSAndroid Build Coastguard Worker rc->percent_blocks_with_motion = 0;
3763*77c1e3ccSAndroid Build Coastguard Worker rc->high_source_sad = 0;
3764*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->sf.rt_sf.check_scene_detection &&
3765*77c1e3ccSAndroid Build Coastguard Worker svc->spatial_layer_id == 0) {
3766*77c1e3ccSAndroid Build Coastguard Worker if (rc->prev_coded_width == cm->width &&
3767*77c1e3ccSAndroid Build Coastguard Worker rc->prev_coded_height == cm->height) {
3768*77c1e3ccSAndroid Build Coastguard Worker rc_scene_detection_onepass_rt(cpi, frame_input);
3769*77c1e3ccSAndroid Build Coastguard Worker } else {
3770*77c1e3ccSAndroid Build Coastguard Worker aom_free(cpi->src_sad_blk_64x64);
3771*77c1e3ccSAndroid Build Coastguard Worker cpi->src_sad_blk_64x64 = NULL;
3772*77c1e3ccSAndroid Build Coastguard Worker }
3773*77c1e3ccSAndroid Build Coastguard Worker }
3774*77c1e3ccSAndroid Build Coastguard Worker if (((*frame_type == KEY_FRAME && cpi->sf.rt_sf.rc_adjust_keyframe) ||
3775*77c1e3ccSAndroid Build Coastguard Worker (cpi->sf.rt_sf.rc_compute_spatial_var_sc && rc->high_source_sad)) &&
3776*77c1e3ccSAndroid Build Coastguard Worker svc->spatial_layer_id == 0 && cm->seq_params->bit_depth == 8 &&
3777*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.max_intra_bitrate_pct > 0)
3778*77c1e3ccSAndroid Build Coastguard Worker rc_spatial_act_keyframe_onepass_rt(cpi, frame_input->source->y_buffer,
3779*77c1e3ccSAndroid Build Coastguard Worker frame_input->source->y_stride);
3780*77c1e3ccSAndroid Build Coastguard Worker // Check for dynamic resize, for single spatial layer for now.
3781*77c1e3ccSAndroid Build Coastguard Worker // For temporal layers only check on base temporal layer.
3782*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.resize_cfg.resize_mode == RESIZE_DYNAMIC) {
3783*77c1e3ccSAndroid Build Coastguard Worker if (svc->number_spatial_layers == 1 && svc->temporal_layer_id == 0)
3784*77c1e3ccSAndroid Build Coastguard Worker dynamic_resize_one_pass_cbr(cpi);
3785*77c1e3ccSAndroid Build Coastguard Worker if (rc->resize_state == THREE_QUARTER) {
3786*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->width = (3 + cpi->oxcf.frm_dim_cfg.width * 3) >> 2;
3787*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->height =
3788*77c1e3ccSAndroid Build Coastguard Worker (3 + cpi->oxcf.frm_dim_cfg.height * 3) >> 2;
3789*77c1e3ccSAndroid Build Coastguard Worker } else if (rc->resize_state == ONE_HALF) {
3790*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->width = (1 + cpi->oxcf.frm_dim_cfg.width) >> 1;
3791*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->height = (1 + cpi->oxcf.frm_dim_cfg.height) >> 1;
3792*77c1e3ccSAndroid Build Coastguard Worker } else {
3793*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->width = cpi->oxcf.frm_dim_cfg.width;
3794*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->height = cpi->oxcf.frm_dim_cfg.height;
3795*77c1e3ccSAndroid Build Coastguard Worker }
3796*77c1e3ccSAndroid Build Coastguard Worker } else if (is_frame_resize_pending(cpi)) {
3797*77c1e3ccSAndroid Build Coastguard Worker resize_reset_rc(cpi, resize_pending_params->width,
3798*77c1e3ccSAndroid Build Coastguard Worker resize_pending_params->height, cm->width, cm->height);
3799*77c1e3ccSAndroid Build Coastguard Worker }
3800*77c1e3ccSAndroid Build Coastguard Worker // Set the GF interval and update flag.
3801*77c1e3ccSAndroid Build Coastguard Worker if (!rc->rtc_external_ratectrl)
3802*77c1e3ccSAndroid Build Coastguard Worker set_gf_interval_update_onepass_rt(cpi, *frame_type);
3803*77c1e3ccSAndroid Build Coastguard Worker // Set target size.
3804*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_CBR) {
3805*77c1e3ccSAndroid Build Coastguard Worker if (*frame_type == KEY_FRAME || *frame_type == INTRA_ONLY_FRAME) {
3806*77c1e3ccSAndroid Build Coastguard Worker target = av1_calc_iframe_target_size_one_pass_cbr(cpi);
3807*77c1e3ccSAndroid Build Coastguard Worker } else {
3808*77c1e3ccSAndroid Build Coastguard Worker target = av1_calc_pframe_target_size_one_pass_cbr(
3809*77c1e3ccSAndroid Build Coastguard Worker cpi, gf_group->update_type[cpi->gf_frame_index]);
3810*77c1e3ccSAndroid Build Coastguard Worker }
3811*77c1e3ccSAndroid Build Coastguard Worker } else {
3812*77c1e3ccSAndroid Build Coastguard Worker if (*frame_type == KEY_FRAME || *frame_type == INTRA_ONLY_FRAME) {
3813*77c1e3ccSAndroid Build Coastguard Worker target = av1_calc_iframe_target_size_one_pass_vbr(cpi);
3814*77c1e3ccSAndroid Build Coastguard Worker } else {
3815*77c1e3ccSAndroid Build Coastguard Worker target = av1_calc_pframe_target_size_one_pass_vbr(
3816*77c1e3ccSAndroid Build Coastguard Worker cpi, gf_group->update_type[cpi->gf_frame_index]);
3817*77c1e3ccSAndroid Build Coastguard Worker }
3818*77c1e3ccSAndroid Build Coastguard Worker }
3819*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_Q)
3820*77c1e3ccSAndroid Build Coastguard Worker rc->active_worst_quality = cpi->oxcf.rc_cfg.cq_level;
3821*77c1e3ccSAndroid Build Coastguard Worker
3822*77c1e3ccSAndroid Build Coastguard Worker av1_rc_set_frame_target(cpi, target, cm->width, cm->height);
3823*77c1e3ccSAndroid Build Coastguard Worker rc->base_frame_target = target;
3824*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_type = *frame_type;
3825*77c1e3ccSAndroid Build Coastguard Worker // For fixed mode SVC: if KSVC is enabled remove inter layer
3826*77c1e3ccSAndroid Build Coastguard Worker // prediction on spatial enhancement layer frames for frames
3827*77c1e3ccSAndroid Build Coastguard Worker // whose base is not KEY frame.
3828*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc && !svc->use_flexible_mode && svc->ksvc_fixed_mode &&
3829*77c1e3ccSAndroid Build Coastguard Worker svc->number_spatial_layers > 1 &&
3830*77c1e3ccSAndroid Build Coastguard Worker !svc->layer_context[layer].is_key_frame) {
3831*77c1e3ccSAndroid Build Coastguard Worker ExternalFlags *const ext_flags = &cpi->ext_flags;
3832*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG;
3833*77c1e3ccSAndroid Build Coastguard Worker }
3834*77c1e3ccSAndroid Build Coastguard Worker }
3835*77c1e3ccSAndroid Build Coastguard Worker
3836*77c1e3ccSAndroid Build Coastguard Worker #define CHECK_INTER_LAYER_PRED(ref_frame) \
3837*77c1e3ccSAndroid Build Coastguard Worker ((cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) && \
3838*77c1e3ccSAndroid Build Coastguard Worker (av1_check_ref_is_low_spatial_res_super_frame(cpi, ref_frame)))
3839*77c1e3ccSAndroid Build Coastguard Worker
av1_encodedframe_overshoot_cbr(AV1_COMP * cpi,int * q)3840*77c1e3ccSAndroid Build Coastguard Worker int av1_encodedframe_overshoot_cbr(AV1_COMP *cpi, int *q) {
3841*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3842*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3843*77c1e3ccSAndroid Build Coastguard Worker double rate_correction_factor =
3844*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL];
3845*77c1e3ccSAndroid Build Coastguard Worker const int target_size = cpi->rc.avg_frame_bandwidth;
3846*77c1e3ccSAndroid Build Coastguard Worker double new_correction_factor;
3847*77c1e3ccSAndroid Build Coastguard Worker int target_bits_per_mb;
3848*77c1e3ccSAndroid Build Coastguard Worker double q2;
3849*77c1e3ccSAndroid Build Coastguard Worker int enumerator;
3850*77c1e3ccSAndroid Build Coastguard Worker int inter_layer_pred_on = 0;
3851*77c1e3ccSAndroid Build Coastguard Worker int is_screen_content = (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN);
3852*77c1e3ccSAndroid Build Coastguard Worker cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
3853*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.spatial_layer_id > 0) {
3854*77c1e3ccSAndroid Build Coastguard Worker // For spatial layers: check if inter-layer (spatial) prediction is used
3855*77c1e3ccSAndroid Build Coastguard Worker // (check if any reference is being used that is the lower spatial layer),
3856*77c1e3ccSAndroid Build Coastguard Worker inter_layer_pred_on = CHECK_INTER_LAYER_PRED(LAST_FRAME) ||
3857*77c1e3ccSAndroid Build Coastguard Worker CHECK_INTER_LAYER_PRED(GOLDEN_FRAME) ||
3858*77c1e3ccSAndroid Build Coastguard Worker CHECK_INTER_LAYER_PRED(ALTREF_FRAME);
3859*77c1e3ccSAndroid Build Coastguard Worker }
3860*77c1e3ccSAndroid Build Coastguard Worker // If inter-layer prediction is on: we expect to pull up the quality from
3861*77c1e3ccSAndroid Build Coastguard Worker // the lower spatial layer, so we can use a lower q.
3862*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.spatial_layer_id > 0 && inter_layer_pred_on) {
3863*77c1e3ccSAndroid Build Coastguard Worker *q = (cpi->rc.worst_quality + *q) >> 1;
3864*77c1e3ccSAndroid Build Coastguard Worker } else {
3865*77c1e3ccSAndroid Build Coastguard Worker // For easy scene changes used lower QP, otherwise set max-q.
3866*77c1e3ccSAndroid Build Coastguard Worker // If rt_sf->compute_spatial_var_sc is enabled relax the max-q
3867*77c1e3ccSAndroid Build Coastguard Worker // condition based on frame spatial variance.
3868*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.rt_sf.rc_compute_spatial_var_sc) {
3869*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.frame_spatial_variance < 100) {
3870*77c1e3ccSAndroid Build Coastguard Worker *q = (cpi->rc.worst_quality + *q) >> 1;
3871*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->rc.frame_spatial_variance < 400 ||
3872*77c1e3ccSAndroid Build Coastguard Worker (cpi->rc.frame_source_sad < 80000 &&
3873*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.frame_spatial_variance < 1000)) {
3874*77c1e3ccSAndroid Build Coastguard Worker *q = (3 * cpi->rc.worst_quality + *q) >> 2;
3875*77c1e3ccSAndroid Build Coastguard Worker } else {
3876*77c1e3ccSAndroid Build Coastguard Worker *q = cpi->rc.worst_quality;
3877*77c1e3ccSAndroid Build Coastguard Worker }
3878*77c1e3ccSAndroid Build Coastguard Worker } else {
3879*77c1e3ccSAndroid Build Coastguard Worker *q = (3 * cpi->rc.worst_quality + *q) >> 2;
3880*77c1e3ccSAndroid Build Coastguard Worker // For screen content use the max-q set by the user to allow for less
3881*77c1e3ccSAndroid Build Coastguard Worker // overshoot on slide changes.
3882*77c1e3ccSAndroid Build Coastguard Worker if (is_screen_content) *q = cpi->rc.worst_quality;
3883*77c1e3ccSAndroid Build Coastguard Worker }
3884*77c1e3ccSAndroid Build Coastguard Worker }
3885*77c1e3ccSAndroid Build Coastguard Worker // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
3886*77c1e3ccSAndroid Build Coastguard Worker // these parameters will affect QP selection for subsequent frames. If they
3887*77c1e3ccSAndroid Build Coastguard Worker // have settled down to a very different (low QP) state, then not adjusting
3888*77c1e3ccSAndroid Build Coastguard Worker // them may cause next frame to select low QP and overshoot again.
3889*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] = *q;
3890*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level = p_rc->optimal_buffer_level;
3891*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target = p_rc->optimal_buffer_level;
3892*77c1e3ccSAndroid Build Coastguard Worker // Reset rate under/over-shoot flags.
3893*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 0;
3894*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_2_frame = 0;
3895*77c1e3ccSAndroid Build Coastguard Worker // Adjust rate correction factor.
3896*77c1e3ccSAndroid Build Coastguard Worker target_bits_per_mb =
3897*77c1e3ccSAndroid Build Coastguard Worker (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->mi_params.MBs);
3898*77c1e3ccSAndroid Build Coastguard Worker // Reset rate correction factor: for now base it on target_bits_per_mb
3899*77c1e3ccSAndroid Build Coastguard Worker // and qp (==max_QP). This comes from the inverse computation of
3900*77c1e3ccSAndroid Build Coastguard Worker // av1_rc_bits_per_mb().
3901*77c1e3ccSAndroid Build Coastguard Worker q2 = av1_convert_qindex_to_q(*q, cm->seq_params->bit_depth);
3902*77c1e3ccSAndroid Build Coastguard Worker enumerator = get_bpmb_enumerator(INTER_NORMAL, is_screen_content);
3903*77c1e3ccSAndroid Build Coastguard Worker new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
3904*77c1e3ccSAndroid Build Coastguard Worker if (new_correction_factor > rate_correction_factor) {
3905*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor =
3906*77c1e3ccSAndroid Build Coastguard Worker (new_correction_factor + rate_correction_factor) / 2.0;
3907*77c1e3ccSAndroid Build Coastguard Worker if (rate_correction_factor > MAX_BPB_FACTOR)
3908*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor = MAX_BPB_FACTOR;
3909*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL] =
3910*77c1e3ccSAndroid Build Coastguard Worker rate_correction_factor;
3911*77c1e3ccSAndroid Build Coastguard Worker }
3912*77c1e3ccSAndroid Build Coastguard Worker // For temporal layers: reset the rate control parameters across all
3913*77c1e3ccSAndroid Build Coastguard Worker // temporal layers. Only do it for spatial enhancement layers when
3914*77c1e3ccSAndroid Build Coastguard Worker // inter_layer_pred_on is not set (off).
3915*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_temporal_layers > 1 &&
3916*77c1e3ccSAndroid Build Coastguard Worker (cpi->svc.spatial_layer_id == 0 || inter_layer_pred_on == 0)) {
3917*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
3918*77c1e3ccSAndroid Build Coastguard Worker for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
3919*77c1e3ccSAndroid Build Coastguard Worker int sl = svc->spatial_layer_id;
3920*77c1e3ccSAndroid Build Coastguard Worker const int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3921*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
3922*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *lrc = &lc->rc;
3923*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc;
3924*77c1e3ccSAndroid Build Coastguard Worker lp_rc->avg_frame_qindex[INTER_FRAME] = *q;
3925*77c1e3ccSAndroid Build Coastguard Worker lp_rc->buffer_level = lp_rc->optimal_buffer_level;
3926*77c1e3ccSAndroid Build Coastguard Worker lp_rc->bits_off_target = lp_rc->optimal_buffer_level;
3927*77c1e3ccSAndroid Build Coastguard Worker lrc->rc_1_frame = 0;
3928*77c1e3ccSAndroid Build Coastguard Worker lrc->rc_2_frame = 0;
3929*77c1e3ccSAndroid Build Coastguard Worker lp_rc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3930*77c1e3ccSAndroid Build Coastguard Worker }
3931*77c1e3ccSAndroid Build Coastguard Worker }
3932*77c1e3ccSAndroid Build Coastguard Worker return 1;
3933*77c1e3ccSAndroid Build Coastguard Worker }
3934*77c1e3ccSAndroid Build Coastguard Worker
av1_postencode_drop_cbr(AV1_COMP * cpi,size_t * size)3935*77c1e3ccSAndroid Build Coastguard Worker int av1_postencode_drop_cbr(AV1_COMP *cpi, size_t *size) {
3936*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3937*77c1e3ccSAndroid Build Coastguard Worker size_t frame_size = *size << 3;
3938*77c1e3ccSAndroid Build Coastguard Worker const int64_t new_buffer_level =
3939*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level + cpi->rc.avg_frame_bandwidth - (int64_t)frame_size;
3940*77c1e3ccSAndroid Build Coastguard Worker // Drop if new buffer level (given the encoded frame size) goes below a
3941*77c1e3ccSAndroid Build Coastguard Worker // threshold and encoded frame size is much larger than per-frame-bandwidth.
3942*77c1e3ccSAndroid Build Coastguard Worker // If the frame is already labelled as scene change (high_source_sad = 1)
3943*77c1e3ccSAndroid Build Coastguard Worker // or the QP is close to max, then no need to drop.
3944*77c1e3ccSAndroid Build Coastguard Worker const int qp_thresh = 3 * (cpi->rc.worst_quality >> 2);
3945*77c1e3ccSAndroid Build Coastguard Worker const int64_t buffer_thresh = p_rc->optimal_buffer_level >> 2;
3946*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->rc.high_source_sad && new_buffer_level < buffer_thresh &&
3947*77c1e3ccSAndroid Build Coastguard Worker frame_size > 8 * (unsigned int)cpi->rc.avg_frame_bandwidth &&
3948*77c1e3ccSAndroid Build Coastguard Worker cpi->common.quant_params.base_qindex < qp_thresh) {
3949*77c1e3ccSAndroid Build Coastguard Worker *size = 0;
3950*77c1e3ccSAndroid Build Coastguard Worker cpi->is_dropped_frame = true;
3951*77c1e3ccSAndroid Build Coastguard Worker restore_all_coding_context(cpi);
3952*77c1e3ccSAndroid Build Coastguard Worker av1_rc_postencode_update_drop_frame(cpi);
3953*77c1e3ccSAndroid Build Coastguard Worker // Force max_q on next fame. Reset some RC parameters.
3954*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.force_max_q = 1;
3955*77c1e3ccSAndroid Build Coastguard Worker p_rc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
3956*77c1e3ccSAndroid Build Coastguard Worker p_rc->buffer_level = p_rc->optimal_buffer_level;
3957*77c1e3ccSAndroid Build Coastguard Worker p_rc->bits_off_target = p_rc->optimal_buffer_level;
3958*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_1_frame = 0;
3959*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.rc_2_frame = 0;
3960*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.number_spatial_layers > 1 ||
3961*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_temporal_layers > 1) {
3962*77c1e3ccSAndroid Build Coastguard Worker SVC *svc = &cpi->svc;
3963*77c1e3ccSAndroid Build Coastguard Worker // Postencode drop is only checked on base spatial layer,
3964*77c1e3ccSAndroid Build Coastguard Worker // for now if max-q is set on base we force it on all layers.
3965*77c1e3ccSAndroid Build Coastguard Worker for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
3966*77c1e3ccSAndroid Build Coastguard Worker for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
3967*77c1e3ccSAndroid Build Coastguard Worker const int layer =
3968*77c1e3ccSAndroid Build Coastguard Worker LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3969*77c1e3ccSAndroid Build Coastguard Worker LAYER_CONTEXT *lc = &svc->layer_context[layer];
3970*77c1e3ccSAndroid Build Coastguard Worker RATE_CONTROL *lrc = &lc->rc;
3971*77c1e3ccSAndroid Build Coastguard Worker PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc;
3972*77c1e3ccSAndroid Build Coastguard Worker // Force max_q on next fame. Reset some RC parameters.
3973*77c1e3ccSAndroid Build Coastguard Worker lrc->force_max_q = 1;
3974*77c1e3ccSAndroid Build Coastguard Worker lp_rc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
3975*77c1e3ccSAndroid Build Coastguard Worker lp_rc->buffer_level = lp_rc->optimal_buffer_level;
3976*77c1e3ccSAndroid Build Coastguard Worker lp_rc->bits_off_target = lp_rc->optimal_buffer_level;
3977*77c1e3ccSAndroid Build Coastguard Worker lrc->rc_1_frame = 0;
3978*77c1e3ccSAndroid Build Coastguard Worker lrc->rc_2_frame = 0;
3979*77c1e3ccSAndroid Build Coastguard Worker }
3980*77c1e3ccSAndroid Build Coastguard Worker }
3981*77c1e3ccSAndroid Build Coastguard Worker }
3982*77c1e3ccSAndroid Build Coastguard Worker return 1;
3983*77c1e3ccSAndroid Build Coastguard Worker }
3984*77c1e3ccSAndroid Build Coastguard Worker return 0;
3985*77c1e3ccSAndroid Build Coastguard Worker }
3986