1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2019, 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 <stdint.h>
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_scale_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
20*77c1e3ccSAndroid Build Coastguard Worker
21*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MISMATCH_DEBUG
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
23*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_MISMATCH_DEBUG
24*77c1e3ccSAndroid Build Coastguard Worker
25*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
27*77c1e3ccSAndroid Build Coastguard Worker
28*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
29*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encode_strategy.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder_alloc.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/firstpass.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/gop_structure.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pass2_strategy.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/temporal_filter.h"
36*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
37*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/thirdpass.h"
38*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_THREE_PASS
39*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tpl_model.h"
40*77c1e3ccSAndroid Build Coastguard Worker
41*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
42*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tune_vmaf.h"
43*77c1e3ccSAndroid Build Coastguard Worker #endif
44*77c1e3ccSAndroid Build Coastguard Worker
45*77c1e3ccSAndroid Build Coastguard Worker #define TEMPORAL_FILTER_KEY_FRAME (CONFIG_REALTIME_ONLY ? 0 : 1)
46*77c1e3ccSAndroid Build Coastguard Worker
set_refresh_frame_flags(RefreshFrameInfo * const refresh_frame,bool refresh_gf,bool refresh_bwdref,bool refresh_arf)47*77c1e3ccSAndroid Build Coastguard Worker static inline void set_refresh_frame_flags(
48*77c1e3ccSAndroid Build Coastguard Worker RefreshFrameInfo *const refresh_frame, bool refresh_gf, bool refresh_bwdref,
49*77c1e3ccSAndroid Build Coastguard Worker bool refresh_arf) {
50*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->golden_frame = refresh_gf;
51*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->bwd_ref_frame = refresh_bwdref;
52*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame = refresh_arf;
53*77c1e3ccSAndroid Build Coastguard Worker }
54*77c1e3ccSAndroid Build Coastguard Worker
av1_configure_buffer_updates(AV1_COMP * const cpi,RefreshFrameInfo * const refresh_frame,const FRAME_UPDATE_TYPE type,const REFBUF_STATE refbuf_state,int force_refresh_all)55*77c1e3ccSAndroid Build Coastguard Worker void av1_configure_buffer_updates(AV1_COMP *const cpi,
56*77c1e3ccSAndroid Build Coastguard Worker RefreshFrameInfo *const refresh_frame,
57*77c1e3ccSAndroid Build Coastguard Worker const FRAME_UPDATE_TYPE type,
58*77c1e3ccSAndroid Build Coastguard Worker const REFBUF_STATE refbuf_state,
59*77c1e3ccSAndroid Build Coastguard Worker int force_refresh_all) {
60*77c1e3ccSAndroid Build Coastguard Worker // NOTE(weitinglin): Should we define another function to take care of
61*77c1e3ccSAndroid Build Coastguard Worker // cpi->rc.is_$Source_Type to make this function as it is in the comment?
62*77c1e3ccSAndroid Build Coastguard Worker const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
63*77c1e3ccSAndroid Build Coastguard Worker &cpi->ext_flags.refresh_frame;
64*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.is_src_frame_alt_ref = 0;
65*77c1e3ccSAndroid Build Coastguard Worker
66*77c1e3ccSAndroid Build Coastguard Worker switch (type) {
67*77c1e3ccSAndroid Build Coastguard Worker case KF_UPDATE:
68*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, true, true);
69*77c1e3ccSAndroid Build Coastguard Worker break;
70*77c1e3ccSAndroid Build Coastguard Worker
71*77c1e3ccSAndroid Build Coastguard Worker case LF_UPDATE:
72*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, false, false, false);
73*77c1e3ccSAndroid Build Coastguard Worker break;
74*77c1e3ccSAndroid Build Coastguard Worker
75*77c1e3ccSAndroid Build Coastguard Worker case GF_UPDATE:
76*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, false, false);
77*77c1e3ccSAndroid Build Coastguard Worker break;
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker case OVERLAY_UPDATE:
80*77c1e3ccSAndroid Build Coastguard Worker if (refbuf_state == REFBUF_RESET)
81*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, true, true);
82*77c1e3ccSAndroid Build Coastguard Worker else
83*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, false, false);
84*77c1e3ccSAndroid Build Coastguard Worker
85*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.is_src_frame_alt_ref = 1;
86*77c1e3ccSAndroid Build Coastguard Worker break;
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Worker case ARF_UPDATE:
89*77c1e3ccSAndroid Build Coastguard Worker // NOTE: BWDREF does not get updated along with ALTREF_FRAME.
90*77c1e3ccSAndroid Build Coastguard Worker if (refbuf_state == REFBUF_RESET)
91*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, true, true);
92*77c1e3ccSAndroid Build Coastguard Worker else
93*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, false, false, true);
94*77c1e3ccSAndroid Build Coastguard Worker
95*77c1e3ccSAndroid Build Coastguard Worker break;
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker case INTNL_OVERLAY_UPDATE:
98*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, false, false, false);
99*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.is_src_frame_alt_ref = 1;
100*77c1e3ccSAndroid Build Coastguard Worker break;
101*77c1e3ccSAndroid Build Coastguard Worker
102*77c1e3ccSAndroid Build Coastguard Worker case INTNL_ARF_UPDATE:
103*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, false, true, false);
104*77c1e3ccSAndroid Build Coastguard Worker break;
105*77c1e3ccSAndroid Build Coastguard Worker
106*77c1e3ccSAndroid Build Coastguard Worker default: assert(0); break;
107*77c1e3ccSAndroid Build Coastguard Worker }
108*77c1e3ccSAndroid Build Coastguard Worker
109*77c1e3ccSAndroid Build Coastguard Worker if (ext_refresh_frame_flags->update_pending &&
110*77c1e3ccSAndroid Build Coastguard Worker (!is_stat_generation_stage(cpi))) {
111*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame,
112*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->golden_frame,
113*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->bwd_ref_frame,
114*77c1e3ccSAndroid Build Coastguard Worker ext_refresh_frame_flags->alt_ref_frame);
115*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *gf_group = &cpi->ppi->gf_group;
116*77c1e3ccSAndroid Build Coastguard Worker if (ext_refresh_frame_flags->golden_frame)
117*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] = GF_UPDATE;
118*77c1e3ccSAndroid Build Coastguard Worker if (ext_refresh_frame_flags->alt_ref_frame)
119*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] = ARF_UPDATE;
120*77c1e3ccSAndroid Build Coastguard Worker if (ext_refresh_frame_flags->bwd_ref_frame)
121*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] = INTNL_ARF_UPDATE;
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker
124*77c1e3ccSAndroid Build Coastguard Worker if (force_refresh_all)
125*77c1e3ccSAndroid Build Coastguard Worker set_refresh_frame_flags(refresh_frame, true, true, true);
126*77c1e3ccSAndroid Build Coastguard Worker }
127*77c1e3ccSAndroid Build Coastguard Worker
set_additional_frame_flags(const AV1_COMMON * const cm,unsigned int * const frame_flags)128*77c1e3ccSAndroid Build Coastguard Worker static void set_additional_frame_flags(const AV1_COMMON *const cm,
129*77c1e3ccSAndroid Build Coastguard Worker unsigned int *const frame_flags) {
130*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
131*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_INTRAONLY;
132*77c1e3ccSAndroid Build Coastguard Worker }
133*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_sframe(cm)) {
134*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_SWITCH;
135*77c1e3ccSAndroid Build Coastguard Worker }
136*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.error_resilient_mode) {
137*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_ERROR_RESILIENT;
138*77c1e3ccSAndroid Build Coastguard Worker }
139*77c1e3ccSAndroid Build Coastguard Worker }
140*77c1e3ccSAndroid Build Coastguard Worker
set_ext_overrides(AV1_COMMON * const cm,EncodeFrameParams * const frame_params,ExternalFlags * const ext_flags)141*77c1e3ccSAndroid Build Coastguard Worker static void set_ext_overrides(AV1_COMMON *const cm,
142*77c1e3ccSAndroid Build Coastguard Worker EncodeFrameParams *const frame_params,
143*77c1e3ccSAndroid Build Coastguard Worker ExternalFlags *const ext_flags) {
144*77c1e3ccSAndroid Build Coastguard Worker // Overrides the defaults with the externally supplied values with
145*77c1e3ccSAndroid Build Coastguard Worker // av1_update_reference() and av1_update_entropy() calls
146*77c1e3ccSAndroid Build Coastguard Worker // Note: The overrides are valid only for the next frame passed
147*77c1e3ccSAndroid Build Coastguard Worker // to av1_encode_lowlevel()
148*77c1e3ccSAndroid Build Coastguard Worker
149*77c1e3ccSAndroid Build Coastguard Worker if (ext_flags->use_s_frame) {
150*77c1e3ccSAndroid Build Coastguard Worker frame_params->frame_type = S_FRAME;
151*77c1e3ccSAndroid Build Coastguard Worker }
152*77c1e3ccSAndroid Build Coastguard Worker
153*77c1e3ccSAndroid Build Coastguard Worker if (ext_flags->refresh_frame_context_pending) {
154*77c1e3ccSAndroid Build Coastguard Worker cm->features.refresh_frame_context = ext_flags->refresh_frame_context;
155*77c1e3ccSAndroid Build Coastguard Worker ext_flags->refresh_frame_context_pending = 0;
156*77c1e3ccSAndroid Build Coastguard Worker }
157*77c1e3ccSAndroid Build Coastguard Worker cm->features.allow_ref_frame_mvs = ext_flags->use_ref_frame_mvs;
158*77c1e3ccSAndroid Build Coastguard Worker
159*77c1e3ccSAndroid Build Coastguard Worker frame_params->error_resilient_mode = ext_flags->use_error_resilient;
160*77c1e3ccSAndroid Build Coastguard Worker // A keyframe is already error resilient and keyframes with
161*77c1e3ccSAndroid Build Coastguard Worker // error_resilient_mode interferes with the use of show_existing_frame
162*77c1e3ccSAndroid Build Coastguard Worker // when forward reference keyframes are enabled.
163*77c1e3ccSAndroid Build Coastguard Worker frame_params->error_resilient_mode &= frame_params->frame_type != KEY_FRAME;
164*77c1e3ccSAndroid Build Coastguard Worker // For bitstream conformance, s-frames must be error-resilient
165*77c1e3ccSAndroid Build Coastguard Worker frame_params->error_resilient_mode |= frame_params->frame_type == S_FRAME;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker
choose_primary_ref_frame(AV1_COMP * const cpi,const EncodeFrameParams * const frame_params)168*77c1e3ccSAndroid Build Coastguard Worker static int choose_primary_ref_frame(
169*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) {
170*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
171*77c1e3ccSAndroid Build Coastguard Worker
172*77c1e3ccSAndroid Build Coastguard Worker const int intra_only = frame_params->frame_type == KEY_FRAME ||
173*77c1e3ccSAndroid Build Coastguard Worker frame_params->frame_type == INTRA_ONLY_FRAME;
174*77c1e3ccSAndroid Build Coastguard Worker if (intra_only || frame_params->error_resilient_mode ||
175*77c1e3ccSAndroid Build Coastguard Worker cpi->ext_flags.use_primary_ref_none) {
176*77c1e3ccSAndroid Build Coastguard Worker return PRIMARY_REF_NONE;
177*77c1e3ccSAndroid Build Coastguard Worker }
178*77c1e3ccSAndroid Build Coastguard Worker
179*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
180*77c1e3ccSAndroid Build Coastguard Worker if (cpi->use_ducky_encode) {
181*77c1e3ccSAndroid Build Coastguard Worker int wanted_fb = cpi->ppi->gf_group.primary_ref_idx[cpi->gf_frame_index];
182*77c1e3ccSAndroid Build Coastguard Worker for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
183*77c1e3ccSAndroid Build Coastguard Worker if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb)
184*77c1e3ccSAndroid Build Coastguard Worker return ref_frame - LAST_FRAME;
185*77c1e3ccSAndroid Build Coastguard Worker }
186*77c1e3ccSAndroid Build Coastguard Worker
187*77c1e3ccSAndroid Build Coastguard Worker return PRIMARY_REF_NONE;
188*77c1e3ccSAndroid Build Coastguard Worker }
189*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
190*77c1e3ccSAndroid Build Coastguard Worker
191*77c1e3ccSAndroid Build Coastguard Worker // In large scale case, always use Last frame's frame contexts.
192*77c1e3ccSAndroid Build Coastguard Worker // Note(yunqing): In other cases, primary_ref_frame is chosen based on
193*77c1e3ccSAndroid Build Coastguard Worker // cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index], which also controls
194*77c1e3ccSAndroid Build Coastguard Worker // frame bit allocation.
195*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.large_scale) return (LAST_FRAME - LAST_FRAME);
196*77c1e3ccSAndroid Build Coastguard Worker
197*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc || cpi->ppi->rtc_ref.set_ref_frame_config)
198*77c1e3ccSAndroid Build Coastguard Worker return av1_svc_primary_ref_frame(cpi);
199*77c1e3ccSAndroid Build Coastguard Worker
200*77c1e3ccSAndroid Build Coastguard Worker // Find the most recent reference frame with the same reference type as the
201*77c1e3ccSAndroid Build Coastguard Worker // current frame
202*77c1e3ccSAndroid Build Coastguard Worker const int current_ref_type = get_current_frame_ref_type(cpi);
203*77c1e3ccSAndroid Build Coastguard Worker int wanted_fb = cpi->ppi->fb_of_context_type[current_ref_type];
204*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
205*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
206*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *const gf_group = &cpi->ppi->gf_group;
207*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
208*77c1e3ccSAndroid Build Coastguard Worker int frame_level = gf_group->frame_parallel_level[cpi->gf_frame_index];
209*77c1e3ccSAndroid Build Coastguard Worker // Book keep wanted_fb of frame_parallel_level 1 frame in an FP2 set.
210*77c1e3ccSAndroid Build Coastguard Worker if (frame_level == 1) {
211*77c1e3ccSAndroid Build Coastguard Worker cpi->wanted_fb = wanted_fb;
212*77c1e3ccSAndroid Build Coastguard Worker }
213*77c1e3ccSAndroid Build Coastguard Worker // Use the wanted_fb of level 1 frame in an FP2 for a level 2 frame in the
214*77c1e3ccSAndroid Build Coastguard Worker // set.
215*77c1e3ccSAndroid Build Coastguard Worker if (frame_level == 2 &&
216*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index - 1] == INTNL_ARF_UPDATE) {
217*77c1e3ccSAndroid Build Coastguard Worker assert(gf_group->frame_parallel_level[cpi->gf_frame_index - 1] == 1);
218*77c1e3ccSAndroid Build Coastguard Worker wanted_fb = cpi->wanted_fb;
219*77c1e3ccSAndroid Build Coastguard Worker }
220*77c1e3ccSAndroid Build Coastguard Worker }
221*77c1e3ccSAndroid Build Coastguard Worker }
222*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
223*77c1e3ccSAndroid Build Coastguard Worker int primary_ref_frame = PRIMARY_REF_NONE;
224*77c1e3ccSAndroid Build Coastguard Worker for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
225*77c1e3ccSAndroid Build Coastguard Worker if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) {
226*77c1e3ccSAndroid Build Coastguard Worker primary_ref_frame = ref_frame - LAST_FRAME;
227*77c1e3ccSAndroid Build Coastguard Worker }
228*77c1e3ccSAndroid Build Coastguard Worker }
229*77c1e3ccSAndroid Build Coastguard Worker
230*77c1e3ccSAndroid Build Coastguard Worker return primary_ref_frame;
231*77c1e3ccSAndroid Build Coastguard Worker }
232*77c1e3ccSAndroid Build Coastguard Worker
adjust_frame_rate(AV1_COMP * cpi,int64_t ts_start,int64_t ts_end)233*77c1e3ccSAndroid Build Coastguard Worker static void adjust_frame_rate(AV1_COMP *cpi, int64_t ts_start, int64_t ts_end) {
234*77c1e3ccSAndroid Build Coastguard Worker TimeStamps *time_stamps = &cpi->time_stamps;
235*77c1e3ccSAndroid Build Coastguard Worker int64_t this_duration;
236*77c1e3ccSAndroid Build Coastguard Worker int step = 0;
237*77c1e3ccSAndroid Build Coastguard Worker
238*77c1e3ccSAndroid Build Coastguard Worker // Clear down mmx registers
239*77c1e3ccSAndroid Build Coastguard Worker
240*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc && cpi->ppi->rtc_ref.set_ref_frame_config &&
241*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_spatial_layers > 1) {
242*77c1e3ccSAndroid Build Coastguard Worker // ts_start is the timestamp for the current frame and ts_end is the
243*77c1e3ccSAndroid Build Coastguard Worker // expected next timestamp given the duration passed into codec_encode().
244*77c1e3ccSAndroid Build Coastguard Worker // See the setting in encoder_encode() in av1_cx_iface.c:
245*77c1e3ccSAndroid Build Coastguard Worker // ts_start = timebase_units_to_ticks(cpi_data.timestamp_ratio, ptsvol),
246*77c1e3ccSAndroid Build Coastguard Worker // ts_end = timebase_units_to_ticks(cpi_data.timestamp_ratio, ptsvol +
247*77c1e3ccSAndroid Build Coastguard Worker // duration). So the difference ts_end - ts_start is the duration passed
248*77c1e3ccSAndroid Build Coastguard Worker // in by the user. For spatial layers SVC set the framerate based directly
249*77c1e3ccSAndroid Build Coastguard Worker // on the duration, and bypass the adjustments below.
250*77c1e3ccSAndroid Build Coastguard Worker this_duration = ts_end - ts_start;
251*77c1e3ccSAndroid Build Coastguard Worker if (this_duration > 0) {
252*77c1e3ccSAndroid Build Coastguard Worker cpi->new_framerate = 10000000.0 / this_duration;
253*77c1e3ccSAndroid Build Coastguard Worker av1_new_framerate(cpi, cpi->new_framerate);
254*77c1e3ccSAndroid Build Coastguard Worker time_stamps->prev_ts_start = ts_start;
255*77c1e3ccSAndroid Build Coastguard Worker time_stamps->prev_ts_end = ts_end;
256*77c1e3ccSAndroid Build Coastguard Worker return;
257*77c1e3ccSAndroid Build Coastguard Worker }
258*77c1e3ccSAndroid Build Coastguard Worker }
259*77c1e3ccSAndroid Build Coastguard Worker
260*77c1e3ccSAndroid Build Coastguard Worker if (ts_start == time_stamps->first_ts_start) {
261*77c1e3ccSAndroid Build Coastguard Worker this_duration = ts_end - ts_start;
262*77c1e3ccSAndroid Build Coastguard Worker step = 1;
263*77c1e3ccSAndroid Build Coastguard Worker } else {
264*77c1e3ccSAndroid Build Coastguard Worker int64_t last_duration =
265*77c1e3ccSAndroid Build Coastguard Worker time_stamps->prev_ts_end - time_stamps->prev_ts_start;
266*77c1e3ccSAndroid Build Coastguard Worker
267*77c1e3ccSAndroid Build Coastguard Worker this_duration = ts_end - time_stamps->prev_ts_end;
268*77c1e3ccSAndroid Build Coastguard Worker
269*77c1e3ccSAndroid Build Coastguard Worker // do a step update if the duration changes by 10%
270*77c1e3ccSAndroid Build Coastguard Worker if (last_duration)
271*77c1e3ccSAndroid Build Coastguard Worker step = (int)((this_duration - last_duration) * 10 / last_duration);
272*77c1e3ccSAndroid Build Coastguard Worker }
273*77c1e3ccSAndroid Build Coastguard Worker
274*77c1e3ccSAndroid Build Coastguard Worker if (this_duration) {
275*77c1e3ccSAndroid Build Coastguard Worker if (step) {
276*77c1e3ccSAndroid Build Coastguard Worker cpi->new_framerate = 10000000.0 / this_duration;
277*77c1e3ccSAndroid Build Coastguard Worker av1_new_framerate(cpi, cpi->new_framerate);
278*77c1e3ccSAndroid Build Coastguard Worker } else {
279*77c1e3ccSAndroid Build Coastguard Worker // Average this frame's rate into the last second's average
280*77c1e3ccSAndroid Build Coastguard Worker // frame rate. If we haven't seen 1 second yet, then average
281*77c1e3ccSAndroid Build Coastguard Worker // over the whole interval seen.
282*77c1e3ccSAndroid Build Coastguard Worker const double interval =
283*77c1e3ccSAndroid Build Coastguard Worker AOMMIN((double)(ts_end - time_stamps->first_ts_start), 10000000.0);
284*77c1e3ccSAndroid Build Coastguard Worker double avg_duration = 10000000.0 / cpi->framerate;
285*77c1e3ccSAndroid Build Coastguard Worker avg_duration *= (interval - avg_duration + this_duration);
286*77c1e3ccSAndroid Build Coastguard Worker avg_duration /= interval;
287*77c1e3ccSAndroid Build Coastguard Worker cpi->new_framerate = (10000000.0 / avg_duration);
288*77c1e3ccSAndroid Build Coastguard Worker // For parallel frames update cpi->framerate with new_framerate
289*77c1e3ccSAndroid Build Coastguard Worker // during av1_post_encode_updates()
290*77c1e3ccSAndroid Build Coastguard Worker double framerate =
291*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
292*77c1e3ccSAndroid Build Coastguard Worker ? cpi->framerate
293*77c1e3ccSAndroid Build Coastguard Worker : cpi->new_framerate;
294*77c1e3ccSAndroid Build Coastguard Worker av1_new_framerate(cpi, framerate);
295*77c1e3ccSAndroid Build Coastguard Worker }
296*77c1e3ccSAndroid Build Coastguard Worker }
297*77c1e3ccSAndroid Build Coastguard Worker
298*77c1e3ccSAndroid Build Coastguard Worker time_stamps->prev_ts_start = ts_start;
299*77c1e3ccSAndroid Build Coastguard Worker time_stamps->prev_ts_end = ts_end;
300*77c1e3ccSAndroid Build Coastguard Worker }
301*77c1e3ccSAndroid Build Coastguard Worker
302*77c1e3ccSAndroid Build Coastguard Worker // Determine whether there is a forced keyframe pending in the lookahead buffer
is_forced_keyframe_pending(struct lookahead_ctx * lookahead,const int up_to_index,const COMPRESSOR_STAGE compressor_stage)303*77c1e3ccSAndroid Build Coastguard Worker int is_forced_keyframe_pending(struct lookahead_ctx *lookahead,
304*77c1e3ccSAndroid Build Coastguard Worker const int up_to_index,
305*77c1e3ccSAndroid Build Coastguard Worker const COMPRESSOR_STAGE compressor_stage) {
306*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i <= up_to_index; i++) {
307*77c1e3ccSAndroid Build Coastguard Worker const struct lookahead_entry *e =
308*77c1e3ccSAndroid Build Coastguard Worker av1_lookahead_peek(lookahead, i, compressor_stage);
309*77c1e3ccSAndroid Build Coastguard Worker if (e == NULL) {
310*77c1e3ccSAndroid Build Coastguard Worker // We have reached the end of the lookahead buffer and not early-returned
311*77c1e3ccSAndroid Build Coastguard Worker // so there isn't a forced key-frame pending.
312*77c1e3ccSAndroid Build Coastguard Worker return -1;
313*77c1e3ccSAndroid Build Coastguard Worker } else if (e->flags == AOM_EFLAG_FORCE_KF) {
314*77c1e3ccSAndroid Build Coastguard Worker return i;
315*77c1e3ccSAndroid Build Coastguard Worker } else {
316*77c1e3ccSAndroid Build Coastguard Worker continue;
317*77c1e3ccSAndroid Build Coastguard Worker }
318*77c1e3ccSAndroid Build Coastguard Worker }
319*77c1e3ccSAndroid Build Coastguard Worker return -1; // Never reached
320*77c1e3ccSAndroid Build Coastguard Worker }
321*77c1e3ccSAndroid Build Coastguard Worker
322*77c1e3ccSAndroid Build Coastguard Worker // Check if we should encode an ARF or internal ARF. If not, try a LAST
323*77c1e3ccSAndroid Build Coastguard Worker // Do some setup associated with the chosen source
324*77c1e3ccSAndroid Build Coastguard Worker // temporal_filtered, flush, and frame_update_type are outputs.
325*77c1e3ccSAndroid Build Coastguard Worker // Return the frame source, or NULL if we couldn't find one
choose_frame_source(AV1_COMP * const cpi,int * const flush,int * pop_lookahead,struct lookahead_entry ** last_source,int * const show_frame)326*77c1e3ccSAndroid Build Coastguard Worker static struct lookahead_entry *choose_frame_source(
327*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, int *const flush, int *pop_lookahead,
328*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry **last_source, int *const show_frame) {
329*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
330*77c1e3ccSAndroid Build Coastguard Worker const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
331*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *source = NULL;
332*77c1e3ccSAndroid Build Coastguard Worker
333*77c1e3ccSAndroid Build Coastguard Worker // Source index in lookahead buffer.
334*77c1e3ccSAndroid Build Coastguard Worker int src_index = gf_group->arf_src_offset[cpi->gf_frame_index];
335*77c1e3ccSAndroid Build Coastguard Worker
336*77c1e3ccSAndroid Build Coastguard Worker // TODO(Aasaipriya): Forced key frames need to be fixed when rc_mode != AOM_Q
337*77c1e3ccSAndroid Build Coastguard Worker if (src_index &&
338*77c1e3ccSAndroid Build Coastguard Worker (is_forced_keyframe_pending(cpi->ppi->lookahead, src_index,
339*77c1e3ccSAndroid Build Coastguard Worker cpi->compressor_stage) != -1) &&
340*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.rc_cfg.mode != AOM_Q && !is_stat_generation_stage(cpi)) {
341*77c1e3ccSAndroid Build Coastguard Worker src_index = 0;
342*77c1e3ccSAndroid Build Coastguard Worker *flush = 1;
343*77c1e3ccSAndroid Build Coastguard Worker }
344*77c1e3ccSAndroid Build Coastguard Worker
345*77c1e3ccSAndroid Build Coastguard Worker // If the current frame is arf, then we should not pop from the lookahead
346*77c1e3ccSAndroid Build Coastguard Worker // buffer. If the current frame is not arf, then pop it. This assumes the
347*77c1e3ccSAndroid Build Coastguard Worker // first frame in the GF group is not arf. May need to change if it is not
348*77c1e3ccSAndroid Build Coastguard Worker // true.
349*77c1e3ccSAndroid Build Coastguard Worker *pop_lookahead = (src_index == 0);
350*77c1e3ccSAndroid Build Coastguard Worker // If this is a key frame and keyframe filtering is enabled with overlay,
351*77c1e3ccSAndroid Build Coastguard Worker // then do not pop.
352*77c1e3ccSAndroid Build Coastguard Worker if (*pop_lookahead && cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1 &&
353*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE &&
354*77c1e3ccSAndroid Build Coastguard Worker !is_stat_generation_stage(cpi) && cpi->ppi->lookahead) {
355*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz &&
356*77c1e3ccSAndroid Build Coastguard Worker (*flush ||
357*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz ==
358*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].pop_sz)) {
359*77c1e3ccSAndroid Build Coastguard Worker *pop_lookahead = 0;
360*77c1e3ccSAndroid Build Coastguard Worker }
361*77c1e3ccSAndroid Build Coastguard Worker }
362*77c1e3ccSAndroid Build Coastguard Worker
363*77c1e3ccSAndroid Build Coastguard Worker // LAP stage does not have ARFs or forward key-frames,
364*77c1e3ccSAndroid Build Coastguard Worker // hence, always pop_lookahead here.
365*77c1e3ccSAndroid Build Coastguard Worker if (is_stat_generation_stage(cpi)) {
366*77c1e3ccSAndroid Build Coastguard Worker *pop_lookahead = 1;
367*77c1e3ccSAndroid Build Coastguard Worker src_index = 0;
368*77c1e3ccSAndroid Build Coastguard Worker }
369*77c1e3ccSAndroid Build Coastguard Worker
370*77c1e3ccSAndroid Build Coastguard Worker *show_frame = *pop_lookahead;
371*77c1e3ccSAndroid Build Coastguard Worker
372*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
373*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_ENCODE) {
374*77c1e3ccSAndroid Build Coastguard Worker #else
375*77c1e3ccSAndroid Build Coastguard Worker {
376*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
377*77c1e3ccSAndroid Build Coastguard Worker // Future frame in parallel encode set
378*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->src_offset[cpi->gf_frame_index] != 0 &&
379*77c1e3ccSAndroid Build Coastguard Worker !is_stat_generation_stage(cpi))
380*77c1e3ccSAndroid Build Coastguard Worker src_index = gf_group->src_offset[cpi->gf_frame_index];
381*77c1e3ccSAndroid Build Coastguard Worker }
382*77c1e3ccSAndroid Build Coastguard Worker if (*show_frame) {
383*77c1e3ccSAndroid Build Coastguard Worker // show frame, pop from buffer
384*77c1e3ccSAndroid Build Coastguard Worker // Get last frame source.
385*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_number > 0) {
386*77c1e3ccSAndroid Build Coastguard Worker *last_source = av1_lookahead_peek(cpi->ppi->lookahead, src_index - 1,
387*77c1e3ccSAndroid Build Coastguard Worker cpi->compressor_stage);
388*77c1e3ccSAndroid Build Coastguard Worker }
389*77c1e3ccSAndroid Build Coastguard Worker // Read in the source frame.
390*77c1e3ccSAndroid Build Coastguard Worker source = av1_lookahead_peek(cpi->ppi->lookahead, src_index,
391*77c1e3ccSAndroid Build Coastguard Worker cpi->compressor_stage);
392*77c1e3ccSAndroid Build Coastguard Worker } else {
393*77c1e3ccSAndroid Build Coastguard Worker // no show frames are arf frames
394*77c1e3ccSAndroid Build Coastguard Worker source = av1_lookahead_peek(cpi->ppi->lookahead, src_index,
395*77c1e3ccSAndroid Build Coastguard Worker cpi->compressor_stage);
396*77c1e3ccSAndroid Build Coastguard Worker if (source != NULL) {
397*77c1e3ccSAndroid Build Coastguard Worker cm->showable_frame = 1;
398*77c1e3ccSAndroid Build Coastguard Worker }
399*77c1e3ccSAndroid Build Coastguard Worker }
400*77c1e3ccSAndroid Build Coastguard Worker return source;
401*77c1e3ccSAndroid Build Coastguard Worker }
402*77c1e3ccSAndroid Build Coastguard Worker
403*77c1e3ccSAndroid Build Coastguard Worker // Don't allow a show_existing_frame to coincide with an error resilient or
404*77c1e3ccSAndroid Build Coastguard Worker // S-Frame. An exception can be made in the case of a keyframe, since it does
405*77c1e3ccSAndroid Build Coastguard Worker // not depend on any previous frames.
406*77c1e3ccSAndroid Build Coastguard Worker static int allow_show_existing(const AV1_COMP *const cpi,
407*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_flags) {
408*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.frame_number == 0) return 0;
409*77c1e3ccSAndroid Build Coastguard Worker
410*77c1e3ccSAndroid Build Coastguard Worker const struct lookahead_entry *lookahead_src =
411*77c1e3ccSAndroid Build Coastguard Worker av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage);
412*77c1e3ccSAndroid Build Coastguard Worker if (lookahead_src == NULL) return 1;
413*77c1e3ccSAndroid Build Coastguard Worker
414*77c1e3ccSAndroid Build Coastguard Worker const int is_error_resilient =
415*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.tool_cfg.error_resilient_mode ||
416*77c1e3ccSAndroid Build Coastguard Worker (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
417*77c1e3ccSAndroid Build Coastguard Worker const int is_s_frame = cpi->oxcf.kf_cfg.enable_sframe ||
418*77c1e3ccSAndroid Build Coastguard Worker (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
419*77c1e3ccSAndroid Build Coastguard Worker const int is_key_frame =
420*77c1e3ccSAndroid Build Coastguard Worker (cpi->rc.frames_to_key == 0) || (frame_flags & FRAMEFLAGS_KEY);
421*77c1e3ccSAndroid Build Coastguard Worker return !(is_error_resilient || is_s_frame) || is_key_frame;
422*77c1e3ccSAndroid Build Coastguard Worker }
423*77c1e3ccSAndroid Build Coastguard Worker
424*77c1e3ccSAndroid Build Coastguard Worker // Update frame_flags to tell the encoder's caller what sort of frame was
425*77c1e3ccSAndroid Build Coastguard Worker // encoded.
426*77c1e3ccSAndroid Build Coastguard Worker static void update_frame_flags(const AV1_COMMON *const cm,
427*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame,
428*77c1e3ccSAndroid Build Coastguard Worker unsigned int *frame_flags) {
429*77c1e3ccSAndroid Build Coastguard Worker if (encode_show_existing_frame(cm)) {
430*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_GOLDEN;
431*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_BWDREF;
432*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_ALTREF;
433*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_KEY;
434*77c1e3ccSAndroid Build Coastguard Worker return;
435*77c1e3ccSAndroid Build Coastguard Worker }
436*77c1e3ccSAndroid Build Coastguard Worker
437*77c1e3ccSAndroid Build Coastguard Worker if (refresh_frame->golden_frame) {
438*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_GOLDEN;
439*77c1e3ccSAndroid Build Coastguard Worker } else {
440*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_GOLDEN;
441*77c1e3ccSAndroid Build Coastguard Worker }
442*77c1e3ccSAndroid Build Coastguard Worker
443*77c1e3ccSAndroid Build Coastguard Worker if (refresh_frame->alt_ref_frame) {
444*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_ALTREF;
445*77c1e3ccSAndroid Build Coastguard Worker } else {
446*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_ALTREF;
447*77c1e3ccSAndroid Build Coastguard Worker }
448*77c1e3ccSAndroid Build Coastguard Worker
449*77c1e3ccSAndroid Build Coastguard Worker if (refresh_frame->bwd_ref_frame) {
450*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_BWDREF;
451*77c1e3ccSAndroid Build Coastguard Worker } else {
452*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_BWDREF;
453*77c1e3ccSAndroid Build Coastguard Worker }
454*77c1e3ccSAndroid Build Coastguard Worker
455*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_type == KEY_FRAME) {
456*77c1e3ccSAndroid Build Coastguard Worker *frame_flags |= FRAMEFLAGS_KEY;
457*77c1e3ccSAndroid Build Coastguard Worker } else {
458*77c1e3ccSAndroid Build Coastguard Worker *frame_flags &= ~(uint32_t)FRAMEFLAGS_KEY;
459*77c1e3ccSAndroid Build Coastguard Worker }
460*77c1e3ccSAndroid Build Coastguard Worker }
461*77c1e3ccSAndroid Build Coastguard Worker
462*77c1e3ccSAndroid Build Coastguard Worker #define DUMP_REF_FRAME_IMAGES 0
463*77c1e3ccSAndroid Build Coastguard Worker
464*77c1e3ccSAndroid Build Coastguard Worker #if DUMP_REF_FRAME_IMAGES == 1
465*77c1e3ccSAndroid Build Coastguard Worker static int dump_one_image(AV1_COMMON *cm,
466*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *const ref_buf,
467*77c1e3ccSAndroid Build Coastguard Worker char *file_name) {
468*77c1e3ccSAndroid Build Coastguard Worker int h;
469*77c1e3ccSAndroid Build Coastguard Worker FILE *f_ref = NULL;
470*77c1e3ccSAndroid Build Coastguard Worker
471*77c1e3ccSAndroid Build Coastguard Worker if (ref_buf == NULL) {
472*77c1e3ccSAndroid Build Coastguard Worker printf("Frame data buffer is NULL.\n");
473*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_MEM_ERROR;
474*77c1e3ccSAndroid Build Coastguard Worker }
475*77c1e3ccSAndroid Build Coastguard Worker
476*77c1e3ccSAndroid Build Coastguard Worker if ((f_ref = fopen(file_name, "wb")) == NULL) {
477*77c1e3ccSAndroid Build Coastguard Worker printf("Unable to open file %s to write.\n", file_name);
478*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_MEM_ERROR;
479*77c1e3ccSAndroid Build Coastguard Worker }
480*77c1e3ccSAndroid Build Coastguard Worker
481*77c1e3ccSAndroid Build Coastguard Worker // --- Y ---
482*77c1e3ccSAndroid Build Coastguard Worker for (h = 0; h < cm->height; ++h) {
483*77c1e3ccSAndroid Build Coastguard Worker fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
484*77c1e3ccSAndroid Build Coastguard Worker }
485*77c1e3ccSAndroid Build Coastguard Worker // --- U ---
486*77c1e3ccSAndroid Build Coastguard Worker for (h = 0; h < (cm->height >> 1); ++h) {
487*77c1e3ccSAndroid Build Coastguard Worker fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
488*77c1e3ccSAndroid Build Coastguard Worker f_ref);
489*77c1e3ccSAndroid Build Coastguard Worker }
490*77c1e3ccSAndroid Build Coastguard Worker // --- V ---
491*77c1e3ccSAndroid Build Coastguard Worker for (h = 0; h < (cm->height >> 1); ++h) {
492*77c1e3ccSAndroid Build Coastguard Worker fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
493*77c1e3ccSAndroid Build Coastguard Worker f_ref);
494*77c1e3ccSAndroid Build Coastguard Worker }
495*77c1e3ccSAndroid Build Coastguard Worker
496*77c1e3ccSAndroid Build Coastguard Worker fclose(f_ref);
497*77c1e3ccSAndroid Build Coastguard Worker
498*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
499*77c1e3ccSAndroid Build Coastguard Worker }
500*77c1e3ccSAndroid Build Coastguard Worker
501*77c1e3ccSAndroid Build Coastguard Worker static void dump_ref_frame_images(AV1_COMP *cpi) {
502*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
503*77c1e3ccSAndroid Build Coastguard Worker MV_REFERENCE_FRAME ref_frame;
504*77c1e3ccSAndroid Build Coastguard Worker
505*77c1e3ccSAndroid Build Coastguard Worker for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
506*77c1e3ccSAndroid Build Coastguard Worker char file_name[256] = "";
507*77c1e3ccSAndroid Build Coastguard Worker snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
508*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_number, ref_frame);
509*77c1e3ccSAndroid Build Coastguard Worker dump_one_image(cm, get_ref_frame_yv12_buf(cpi, ref_frame), file_name);
510*77c1e3ccSAndroid Build Coastguard Worker }
511*77c1e3ccSAndroid Build Coastguard Worker }
512*77c1e3ccSAndroid Build Coastguard Worker #endif // DUMP_REF_FRAME_IMAGES == 1
513*77c1e3ccSAndroid Build Coastguard Worker
514*77c1e3ccSAndroid Build Coastguard Worker int av1_get_refresh_ref_frame_map(int refresh_frame_flags) {
515*77c1e3ccSAndroid Build Coastguard Worker int ref_map_index;
516*77c1e3ccSAndroid Build Coastguard Worker
517*77c1e3ccSAndroid Build Coastguard Worker for (ref_map_index = 0; ref_map_index < REF_FRAMES; ++ref_map_index)
518*77c1e3ccSAndroid Build Coastguard Worker if ((refresh_frame_flags >> ref_map_index) & 1) break;
519*77c1e3ccSAndroid Build Coastguard Worker
520*77c1e3ccSAndroid Build Coastguard Worker if (ref_map_index == REF_FRAMES) ref_map_index = INVALID_IDX;
521*77c1e3ccSAndroid Build Coastguard Worker return ref_map_index;
522*77c1e3ccSAndroid Build Coastguard Worker }
523*77c1e3ccSAndroid Build Coastguard Worker
524*77c1e3ccSAndroid Build Coastguard Worker static int get_free_ref_map_index(RefFrameMapPair ref_map_pairs[REF_FRAMES]) {
525*77c1e3ccSAndroid Build Coastguard Worker for (int idx = 0; idx < REF_FRAMES; ++idx)
526*77c1e3ccSAndroid Build Coastguard Worker if (ref_map_pairs[idx].disp_order == -1) return idx;
527*77c1e3ccSAndroid Build Coastguard Worker return INVALID_IDX;
528*77c1e3ccSAndroid Build Coastguard Worker }
529*77c1e3ccSAndroid Build Coastguard Worker
530*77c1e3ccSAndroid Build Coastguard Worker static int get_refresh_idx(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
531*77c1e3ccSAndroid Build Coastguard Worker int update_arf, GF_GROUP *gf_group, int gf_index,
532*77c1e3ccSAndroid Build Coastguard Worker int enable_refresh_skip, int cur_frame_disp) {
533*77c1e3ccSAndroid Build Coastguard Worker int arf_count = 0;
534*77c1e3ccSAndroid Build Coastguard Worker int oldest_arf_order = INT32_MAX;
535*77c1e3ccSAndroid Build Coastguard Worker int oldest_arf_idx = -1;
536*77c1e3ccSAndroid Build Coastguard Worker
537*77c1e3ccSAndroid Build Coastguard Worker int oldest_frame_order = INT32_MAX;
538*77c1e3ccSAndroid Build Coastguard Worker int oldest_idx = -1;
539*77c1e3ccSAndroid Build Coastguard Worker
540*77c1e3ccSAndroid Build Coastguard Worker for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) {
541*77c1e3ccSAndroid Build Coastguard Worker RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx];
542*77c1e3ccSAndroid Build Coastguard Worker if (ref_pair.disp_order == -1) continue;
543*77c1e3ccSAndroid Build Coastguard Worker const int frame_order = ref_pair.disp_order;
544*77c1e3ccSAndroid Build Coastguard Worker const int reference_frame_level = ref_pair.pyr_level;
545*77c1e3ccSAndroid Build Coastguard Worker // Keep future frames and three closest previous frames in output order.
546*77c1e3ccSAndroid Build Coastguard Worker if (frame_order > cur_frame_disp - 3) continue;
547*77c1e3ccSAndroid Build Coastguard Worker
548*77c1e3ccSAndroid Build Coastguard Worker if (enable_refresh_skip) {
549*77c1e3ccSAndroid Build Coastguard Worker int skip_frame = 0;
550*77c1e3ccSAndroid Build Coastguard Worker // Prevent refreshing a frame in gf_group->skip_frame_refresh.
551*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; i++) {
552*77c1e3ccSAndroid Build Coastguard Worker int frame_to_skip = gf_group->skip_frame_refresh[gf_index][i];
553*77c1e3ccSAndroid Build Coastguard Worker if (frame_to_skip == INVALID_IDX) break;
554*77c1e3ccSAndroid Build Coastguard Worker if (frame_order == frame_to_skip) {
555*77c1e3ccSAndroid Build Coastguard Worker skip_frame = 1;
556*77c1e3ccSAndroid Build Coastguard Worker break;
557*77c1e3ccSAndroid Build Coastguard Worker }
558*77c1e3ccSAndroid Build Coastguard Worker }
559*77c1e3ccSAndroid Build Coastguard Worker if (skip_frame) continue;
560*77c1e3ccSAndroid Build Coastguard Worker }
561*77c1e3ccSAndroid Build Coastguard Worker
562*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the oldest level 1 frame if the current frame is also level
563*77c1e3ccSAndroid Build Coastguard Worker // 1.
564*77c1e3ccSAndroid Build Coastguard Worker if (reference_frame_level == 1) {
565*77c1e3ccSAndroid Build Coastguard Worker // If there are more than 2 level 1 frames in the reference list,
566*77c1e3ccSAndroid Build Coastguard Worker // discard the oldest.
567*77c1e3ccSAndroid Build Coastguard Worker if (frame_order < oldest_arf_order) {
568*77c1e3ccSAndroid Build Coastguard Worker oldest_arf_order = frame_order;
569*77c1e3ccSAndroid Build Coastguard Worker oldest_arf_idx = map_idx;
570*77c1e3ccSAndroid Build Coastguard Worker }
571*77c1e3ccSAndroid Build Coastguard Worker arf_count++;
572*77c1e3ccSAndroid Build Coastguard Worker continue;
573*77c1e3ccSAndroid Build Coastguard Worker }
574*77c1e3ccSAndroid Build Coastguard Worker
575*77c1e3ccSAndroid Build Coastguard Worker // Update the overall oldest reference frame.
576*77c1e3ccSAndroid Build Coastguard Worker if (frame_order < oldest_frame_order) {
577*77c1e3ccSAndroid Build Coastguard Worker oldest_frame_order = frame_order;
578*77c1e3ccSAndroid Build Coastguard Worker oldest_idx = map_idx;
579*77c1e3ccSAndroid Build Coastguard Worker }
580*77c1e3ccSAndroid Build Coastguard Worker }
581*77c1e3ccSAndroid Build Coastguard Worker if (update_arf && arf_count > 2) return oldest_arf_idx;
582*77c1e3ccSAndroid Build Coastguard Worker if (oldest_idx >= 0) return oldest_idx;
583*77c1e3ccSAndroid Build Coastguard Worker if (oldest_arf_idx >= 0) return oldest_arf_idx;
584*77c1e3ccSAndroid Build Coastguard Worker if (oldest_idx == -1) {
585*77c1e3ccSAndroid Build Coastguard Worker assert(arf_count > 2 && enable_refresh_skip);
586*77c1e3ccSAndroid Build Coastguard Worker return oldest_arf_idx;
587*77c1e3ccSAndroid Build Coastguard Worker }
588*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "No valid refresh index found");
589*77c1e3ccSAndroid Build Coastguard Worker return -1;
590*77c1e3ccSAndroid Build Coastguard Worker }
591*77c1e3ccSAndroid Build Coastguard Worker
592*77c1e3ccSAndroid Build Coastguard Worker // Computes the reference refresh index for INTNL_ARF_UPDATE frame.
593*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_refresh_idx_for_intnl_arf(
594*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *cpi, RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
595*77c1e3ccSAndroid Build Coastguard Worker int gf_index) {
596*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *const gf_group = &cpi->ppi->gf_group;
597*77c1e3ccSAndroid Build Coastguard Worker
598*77c1e3ccSAndroid Build Coastguard Worker // Search for the open slot to store the current frame.
599*77c1e3ccSAndroid Build Coastguard Worker int free_fb_index = get_free_ref_map_index(ref_frame_map_pairs);
600*77c1e3ccSAndroid Build Coastguard Worker
601*77c1e3ccSAndroid Build Coastguard Worker // Use a free slot if available.
602*77c1e3ccSAndroid Build Coastguard Worker if (free_fb_index != INVALID_IDX) {
603*77c1e3ccSAndroid Build Coastguard Worker return free_fb_index;
604*77c1e3ccSAndroid Build Coastguard Worker } else {
605*77c1e3ccSAndroid Build Coastguard Worker int enable_refresh_skip = !is_one_pass_rt_params(cpi);
606*77c1e3ccSAndroid Build Coastguard Worker int refresh_idx =
607*77c1e3ccSAndroid Build Coastguard Worker get_refresh_idx(ref_frame_map_pairs, 0, gf_group, gf_index,
608*77c1e3ccSAndroid Build Coastguard Worker enable_refresh_skip, gf_group->display_idx[gf_index]);
609*77c1e3ccSAndroid Build Coastguard Worker return refresh_idx;
610*77c1e3ccSAndroid Build Coastguard Worker }
611*77c1e3ccSAndroid Build Coastguard Worker }
612*77c1e3ccSAndroid Build Coastguard Worker
613*77c1e3ccSAndroid Build Coastguard Worker int av1_get_refresh_frame_flags(
614*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params,
615*77c1e3ccSAndroid Build Coastguard Worker FRAME_UPDATE_TYPE frame_update_type, int gf_index, int cur_disp_order,
616*77c1e3ccSAndroid Build Coastguard Worker RefFrameMapPair ref_frame_map_pairs[REF_FRAMES]) {
617*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
618*77c1e3ccSAndroid Build Coastguard Worker const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
619*77c1e3ccSAndroid Build Coastguard Worker &cpi->ext_flags.refresh_frame;
620*77c1e3ccSAndroid Build Coastguard Worker
621*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *gf_group = &cpi->ppi->gf_group;
622*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->refbuf_state[gf_index] == REFBUF_RESET)
623*77c1e3ccSAndroid Build Coastguard Worker return SELECT_ALL_BUF_SLOTS;
624*77c1e3ccSAndroid Build Coastguard Worker
625*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning): Deprecate the following operations.
626*77c1e3ccSAndroid Build Coastguard Worker // Switch frames and shown key-frames overwrite all reference slots
627*77c1e3ccSAndroid Build Coastguard Worker if (frame_params->frame_type == S_FRAME) return SELECT_ALL_BUF_SLOTS;
628*77c1e3ccSAndroid Build Coastguard Worker
629*77c1e3ccSAndroid Build Coastguard Worker // show_existing_frames don't actually send refresh_frame_flags so set the
630*77c1e3ccSAndroid Build Coastguard Worker // flags to 0 to keep things consistent.
631*77c1e3ccSAndroid Build Coastguard Worker if (frame_params->show_existing_frame) return 0;
632*77c1e3ccSAndroid Build Coastguard Worker
633*77c1e3ccSAndroid Build Coastguard Worker const RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
634*77c1e3ccSAndroid Build Coastguard Worker if (is_frame_droppable(rtc_ref, ext_refresh_frame_flags)) return 0;
635*77c1e3ccSAndroid Build Coastguard Worker
636*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
637*77c1e3ccSAndroid Build Coastguard Worker if (cpi->use_ducky_encode &&
638*77c1e3ccSAndroid Build Coastguard Worker cpi->ducky_encode_info.frame_info.gop_mode == DUCKY_ENCODE_GOP_MODE_RCL) {
639*77c1e3ccSAndroid Build Coastguard Worker int new_fb_map_idx = cpi->ppi->gf_group.update_ref_idx[gf_index];
640*77c1e3ccSAndroid Build Coastguard Worker if (new_fb_map_idx == INVALID_IDX) return 0;
641*77c1e3ccSAndroid Build Coastguard Worker return 1 << new_fb_map_idx;
642*77c1e3ccSAndroid Build Coastguard Worker }
643*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
644*77c1e3ccSAndroid Build Coastguard Worker
645*77c1e3ccSAndroid Build Coastguard Worker int refresh_mask = 0;
646*77c1e3ccSAndroid Build Coastguard Worker if (ext_refresh_frame_flags->update_pending) {
647*77c1e3ccSAndroid Build Coastguard Worker if (rtc_ref->set_ref_frame_config ||
648*77c1e3ccSAndroid Build Coastguard Worker use_rtc_reference_structure_one_layer(cpi)) {
649*77c1e3ccSAndroid Build Coastguard Worker for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
650*77c1e3ccSAndroid Build Coastguard Worker int ref_frame_map_idx = rtc_ref->ref_idx[i];
651*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= rtc_ref->refresh[ref_frame_map_idx]
652*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
653*77c1e3ccSAndroid Build Coastguard Worker }
654*77c1e3ccSAndroid Build Coastguard Worker return refresh_mask;
655*77c1e3ccSAndroid Build Coastguard Worker }
656*77c1e3ccSAndroid Build Coastguard Worker // Unfortunately the encoder interface reflects the old refresh_*_frame
657*77c1e3ccSAndroid Build Coastguard Worker // flags so we have to replicate the old refresh_frame_flags logic here in
658*77c1e3ccSAndroid Build Coastguard Worker // order to preserve the behaviour of the flag overrides.
659*77c1e3ccSAndroid Build Coastguard Worker int ref_frame_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME);
660*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
661*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->last_frame << ref_frame_map_idx;
662*77c1e3ccSAndroid Build Coastguard Worker
663*77c1e3ccSAndroid Build Coastguard Worker ref_frame_map_idx = get_ref_frame_map_idx(cm, EXTREF_FRAME);
664*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
665*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->bwd_ref_frame
666*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
667*77c1e3ccSAndroid Build Coastguard Worker
668*77c1e3ccSAndroid Build Coastguard Worker ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF2_FRAME);
669*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
670*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->alt2_ref_frame
671*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
672*77c1e3ccSAndroid Build Coastguard Worker
673*77c1e3ccSAndroid Build Coastguard Worker if (frame_update_type == OVERLAY_UPDATE) {
674*77c1e3ccSAndroid Build Coastguard Worker ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME);
675*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
676*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->golden_frame
677*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
678*77c1e3ccSAndroid Build Coastguard Worker } else {
679*77c1e3ccSAndroid Build Coastguard Worker ref_frame_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME);
680*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
681*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->golden_frame
682*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
683*77c1e3ccSAndroid Build Coastguard Worker
684*77c1e3ccSAndroid Build Coastguard Worker ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME);
685*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame_map_idx != INVALID_IDX)
686*77c1e3ccSAndroid Build Coastguard Worker refresh_mask |= ext_refresh_frame_flags->alt_ref_frame
687*77c1e3ccSAndroid Build Coastguard Worker << ref_frame_map_idx;
688*77c1e3ccSAndroid Build Coastguard Worker }
689*77c1e3ccSAndroid Build Coastguard Worker return refresh_mask;
690*77c1e3ccSAndroid Build Coastguard Worker }
691*77c1e3ccSAndroid Build Coastguard Worker
692*77c1e3ccSAndroid Build Coastguard Worker // Search for the open slot to store the current frame.
693*77c1e3ccSAndroid Build Coastguard Worker int free_fb_index = get_free_ref_map_index(ref_frame_map_pairs);
694*77c1e3ccSAndroid Build Coastguard Worker
695*77c1e3ccSAndroid Build Coastguard Worker // No refresh necessary for these frame types.
696*77c1e3ccSAndroid Build Coastguard Worker if (frame_update_type == OVERLAY_UPDATE ||
697*77c1e3ccSAndroid Build Coastguard Worker frame_update_type == INTNL_OVERLAY_UPDATE)
698*77c1e3ccSAndroid Build Coastguard Worker return refresh_mask;
699*77c1e3ccSAndroid Build Coastguard Worker
700*77c1e3ccSAndroid Build Coastguard Worker // If there is an open slot, refresh that one instead of replacing a
701*77c1e3ccSAndroid Build Coastguard Worker // reference.
702*77c1e3ccSAndroid Build Coastguard Worker if (free_fb_index != INVALID_IDX) {
703*77c1e3ccSAndroid Build Coastguard Worker refresh_mask = 1 << free_fb_index;
704*77c1e3ccSAndroid Build Coastguard Worker return refresh_mask;
705*77c1e3ccSAndroid Build Coastguard Worker }
706*77c1e3ccSAndroid Build Coastguard Worker const int enable_refresh_skip = !is_one_pass_rt_params(cpi);
707*77c1e3ccSAndroid Build Coastguard Worker const int update_arf = frame_update_type == ARF_UPDATE;
708*77c1e3ccSAndroid Build Coastguard Worker const int refresh_idx =
709*77c1e3ccSAndroid Build Coastguard Worker get_refresh_idx(ref_frame_map_pairs, update_arf, &cpi->ppi->gf_group,
710*77c1e3ccSAndroid Build Coastguard Worker gf_index, enable_refresh_skip, cur_disp_order);
711*77c1e3ccSAndroid Build Coastguard Worker return 1 << refresh_idx;
712*77c1e3ccSAndroid Build Coastguard Worker }
713*77c1e3ccSAndroid Build Coastguard Worker
714*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
715*77c1e3ccSAndroid Build Coastguard Worker // Apply temporal filtering to source frames and encode the filtered frame.
716*77c1e3ccSAndroid Build Coastguard Worker // If the current frame does not require filtering, this function is identical
717*77c1e3ccSAndroid Build Coastguard Worker // to av1_encode() except that tpl is not performed.
718*77c1e3ccSAndroid Build Coastguard Worker static int denoise_and_encode(AV1_COMP *const cpi, uint8_t *const dest,
719*77c1e3ccSAndroid Build Coastguard Worker size_t dest_size,
720*77c1e3ccSAndroid Build Coastguard Worker EncodeFrameInput *const frame_input,
721*77c1e3ccSAndroid Build Coastguard Worker const EncodeFrameParams *const frame_params,
722*77c1e3ccSAndroid Build Coastguard Worker size_t *const frame_size) {
723*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
724*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.pass == 2) start_timing(cpi, denoise_and_encode_time);
725*77c1e3ccSAndroid Build Coastguard Worker #endif
726*77c1e3ccSAndroid Build Coastguard Worker const AV1EncoderConfig *const oxcf = &cpi->oxcf;
727*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
728*77c1e3ccSAndroid Build Coastguard Worker
729*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *const gf_group = &cpi->ppi->gf_group;
730*77c1e3ccSAndroid Build Coastguard Worker FRAME_UPDATE_TYPE update_type =
731*77c1e3ccSAndroid Build Coastguard Worker get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
732*77c1e3ccSAndroid Build Coastguard Worker const int is_second_arf =
733*77c1e3ccSAndroid Build Coastguard Worker av1_gop_is_second_arf(gf_group, cpi->gf_frame_index);
734*77c1e3ccSAndroid Build Coastguard Worker
735*77c1e3ccSAndroid Build Coastguard Worker // Decide whether to apply temporal filtering to the source frame.
736*77c1e3ccSAndroid Build Coastguard Worker int apply_filtering =
737*77c1e3ccSAndroid Build Coastguard Worker av1_is_temporal_filter_on(oxcf) && !is_stat_generation_stage(cpi);
738*77c1e3ccSAndroid Build Coastguard Worker if (update_type != KF_UPDATE && update_type != ARF_UPDATE && !is_second_arf) {
739*77c1e3ccSAndroid Build Coastguard Worker apply_filtering = 0;
740*77c1e3ccSAndroid Build Coastguard Worker }
741*77c1e3ccSAndroid Build Coastguard Worker if (apply_filtering) {
742*77c1e3ccSAndroid Build Coastguard Worker if (frame_params->frame_type == KEY_FRAME) {
743*77c1e3ccSAndroid Build Coastguard Worker // TODO(angiebird): Move the noise level check to av1_tf_info_filtering.
744*77c1e3ccSAndroid Build Coastguard Worker // Decide whether it is allowed to perform key frame filtering
745*77c1e3ccSAndroid Build Coastguard Worker int allow_kf_filtering = oxcf->kf_cfg.enable_keyframe_filtering &&
746*77c1e3ccSAndroid Build Coastguard Worker !frame_params->show_existing_frame &&
747*77c1e3ccSAndroid Build Coastguard Worker !is_lossless_requested(&oxcf->rc_cfg);
748*77c1e3ccSAndroid Build Coastguard Worker if (allow_kf_filtering) {
749*77c1e3ccSAndroid Build Coastguard Worker double y_noise_level = 0.0;
750*77c1e3ccSAndroid Build Coastguard Worker av1_estimate_noise_level(
751*77c1e3ccSAndroid Build Coastguard Worker frame_input->source, &y_noise_level, AOM_PLANE_Y, AOM_PLANE_Y,
752*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth, NOISE_ESTIMATION_EDGE_THRESHOLD);
753*77c1e3ccSAndroid Build Coastguard Worker apply_filtering = y_noise_level > 0;
754*77c1e3ccSAndroid Build Coastguard Worker } else {
755*77c1e3ccSAndroid Build Coastguard Worker apply_filtering = 0;
756*77c1e3ccSAndroid Build Coastguard Worker }
757*77c1e3ccSAndroid Build Coastguard Worker // If we are doing kf filtering, set up a few things.
758*77c1e3ccSAndroid Build Coastguard Worker if (apply_filtering) {
759*77c1e3ccSAndroid Build Coastguard Worker av1_setup_past_independence(cm);
760*77c1e3ccSAndroid Build Coastguard Worker }
761*77c1e3ccSAndroid Build Coastguard Worker } else if (is_second_arf) {
762*77c1e3ccSAndroid Build Coastguard Worker apply_filtering = cpi->sf.hl_sf.second_alt_ref_filtering;
763*77c1e3ccSAndroid Build Coastguard Worker }
764*77c1e3ccSAndroid Build Coastguard Worker }
765*77c1e3ccSAndroid Build Coastguard Worker
766*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
767*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.pass == 2) start_timing(cpi, apply_filtering_time);
768*77c1e3ccSAndroid Build Coastguard Worker #endif
769*77c1e3ccSAndroid Build Coastguard Worker // Save the pointer to the original source image.
770*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *source_buffer = frame_input->source;
771*77c1e3ccSAndroid Build Coastguard Worker // apply filtering to frame
772*77c1e3ccSAndroid Build Coastguard Worker if (apply_filtering) {
773*77c1e3ccSAndroid Build Coastguard Worker int show_existing_alt_ref = 0;
774*77c1e3ccSAndroid Build Coastguard Worker FRAME_DIFF frame_diff;
775*77c1e3ccSAndroid Build Coastguard Worker int top_index = 0;
776*77c1e3ccSAndroid Build Coastguard Worker int bottom_index = 0;
777*77c1e3ccSAndroid Build Coastguard Worker const int q_index = av1_rc_pick_q_and_bounds(
778*77c1e3ccSAndroid Build Coastguard Worker cpi, cpi->oxcf.frm_dim_cfg.width, cpi->oxcf.frm_dim_cfg.height,
779*77c1e3ccSAndroid Build Coastguard Worker cpi->gf_frame_index, &bottom_index, &top_index);
780*77c1e3ccSAndroid Build Coastguard Worker
781*77c1e3ccSAndroid Build Coastguard Worker // TODO(bohanli): figure out why we need frame_type in cm here.
782*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_type = frame_params->frame_type;
783*77c1e3ccSAndroid Build Coastguard Worker if (update_type == KF_UPDATE || update_type == ARF_UPDATE) {
784*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *tf_buf = av1_tf_info_get_filtered_buf(
785*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->tf_info, cpi->gf_frame_index, &frame_diff);
786*77c1e3ccSAndroid Build Coastguard Worker if (tf_buf != NULL) {
787*77c1e3ccSAndroid Build Coastguard Worker frame_input->source = tf_buf;
788*77c1e3ccSAndroid Build Coastguard Worker show_existing_alt_ref = av1_check_show_filtered_frame(
789*77c1e3ccSAndroid Build Coastguard Worker tf_buf, &frame_diff, q_index, cm->seq_params->bit_depth);
790*77c1e3ccSAndroid Build Coastguard Worker if (show_existing_alt_ref) {
791*77c1e3ccSAndroid Build Coastguard Worker cpi->common.showable_frame |= 1;
792*77c1e3ccSAndroid Build Coastguard Worker } else {
793*77c1e3ccSAndroid Build Coastguard Worker cpi->common.showable_frame = 0;
794*77c1e3ccSAndroid Build Coastguard Worker }
795*77c1e3ccSAndroid Build Coastguard Worker }
796*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->frame_type[cpi->gf_frame_index] != KEY_FRAME) {
797*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->show_existing_alt_ref = show_existing_alt_ref;
798*77c1e3ccSAndroid Build Coastguard Worker }
799*77c1e3ccSAndroid Build Coastguard Worker }
800*77c1e3ccSAndroid Build Coastguard Worker
801*77c1e3ccSAndroid Build Coastguard Worker if (is_second_arf) {
802*77c1e3ccSAndroid Build Coastguard Worker // Allocate the memory for tf_buf_second_arf buffer, only when it is
803*77c1e3ccSAndroid Build Coastguard Worker // required.
804*77c1e3ccSAndroid Build Coastguard Worker int ret = aom_realloc_frame_buffer(
805*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->tf_info.tf_buf_second_arf, oxcf->frm_dim_cfg.width,
806*77c1e3ccSAndroid Build Coastguard Worker oxcf->frm_dim_cfg.height, cm->seq_params->subsampling_x,
807*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->subsampling_y, cm->seq_params->use_highbitdepth,
808*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
809*77c1e3ccSAndroid Build Coastguard Worker NULL, cpi->alloc_pyramid, 0);
810*77c1e3ccSAndroid Build Coastguard Worker if (ret)
811*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
812*77c1e3ccSAndroid Build Coastguard Worker "Failed to allocate tf_buf_second_arf");
813*77c1e3ccSAndroid Build Coastguard Worker
814*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *tf_buf_second_arf =
815*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->tf_info.tf_buf_second_arf;
816*77c1e3ccSAndroid Build Coastguard Worker // We didn't apply temporal filtering for second arf ahead in
817*77c1e3ccSAndroid Build Coastguard Worker // av1_tf_info_filtering().
818*77c1e3ccSAndroid Build Coastguard Worker const int arf_src_index = gf_group->arf_src_offset[cpi->gf_frame_index];
819*77c1e3ccSAndroid Build Coastguard Worker // Right now, we are still using tf_buf_second_arf due to
820*77c1e3ccSAndroid Build Coastguard Worker // implementation complexity.
821*77c1e3ccSAndroid Build Coastguard Worker // TODO(angiebird): Reuse tf_info->tf_buf here.
822*77c1e3ccSAndroid Build Coastguard Worker av1_temporal_filter(cpi, arf_src_index, cpi->gf_frame_index, &frame_diff,
823*77c1e3ccSAndroid Build Coastguard Worker tf_buf_second_arf);
824*77c1e3ccSAndroid Build Coastguard Worker show_existing_alt_ref = av1_check_show_filtered_frame(
825*77c1e3ccSAndroid Build Coastguard Worker tf_buf_second_arf, &frame_diff, q_index, cm->seq_params->bit_depth);
826*77c1e3ccSAndroid Build Coastguard Worker if (show_existing_alt_ref) {
827*77c1e3ccSAndroid Build Coastguard Worker aom_extend_frame_borders(tf_buf_second_arf, av1_num_planes(cm));
828*77c1e3ccSAndroid Build Coastguard Worker frame_input->source = tf_buf_second_arf;
829*77c1e3ccSAndroid Build Coastguard Worker }
830*77c1e3ccSAndroid Build Coastguard Worker // Currently INTNL_ARF_UPDATE only do show_existing.
831*77c1e3ccSAndroid Build Coastguard Worker cpi->common.showable_frame |= 1;
832*77c1e3ccSAndroid Build Coastguard Worker }
833*77c1e3ccSAndroid Build Coastguard Worker
834*77c1e3ccSAndroid Build Coastguard Worker // Copy source metadata to the temporal filtered frame
835*77c1e3ccSAndroid Build Coastguard Worker if (source_buffer->metadata &&
836*77c1e3ccSAndroid Build Coastguard Worker aom_copy_metadata_to_frame_buffer(frame_input->source,
837*77c1e3ccSAndroid Build Coastguard Worker source_buffer->metadata)) {
838*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(
839*77c1e3ccSAndroid Build Coastguard Worker cm->error, AOM_CODEC_MEM_ERROR,
840*77c1e3ccSAndroid Build Coastguard Worker "Failed to copy source metadata to the temporal filtered frame");
841*77c1e3ccSAndroid Build Coastguard Worker }
842*77c1e3ccSAndroid Build Coastguard Worker }
843*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
844*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.pass == 2) end_timing(cpi, apply_filtering_time);
845*77c1e3ccSAndroid Build Coastguard Worker #endif
846*77c1e3ccSAndroid Build Coastguard Worker
847*77c1e3ccSAndroid Build Coastguard Worker int set_mv_params = frame_params->frame_type == KEY_FRAME ||
848*77c1e3ccSAndroid Build Coastguard Worker update_type == ARF_UPDATE || update_type == GF_UPDATE;
849*77c1e3ccSAndroid Build Coastguard Worker cm->show_frame = frame_params->show_frame;
850*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_type = frame_params->frame_type;
851*77c1e3ccSAndroid Build Coastguard Worker // TODO(bohanli): Why is this? what part of it is necessary?
852*77c1e3ccSAndroid Build Coastguard Worker av1_set_frame_size(cpi, cm->width, cm->height);
853*77c1e3ccSAndroid Build Coastguard Worker if (set_mv_params) av1_set_mv_search_params(cpi);
854*77c1e3ccSAndroid Build Coastguard Worker
855*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_COMMAND
856*77c1e3ccSAndroid Build Coastguard Worker if (frame_params->frame_type == KEY_FRAME) {
857*77c1e3ccSAndroid Build Coastguard Worker char filepath[] = "rd_command.txt";
858*77c1e3ccSAndroid Build Coastguard Worker av1_read_rd_command(filepath, &cpi->rd_command);
859*77c1e3ccSAndroid Build Coastguard Worker }
860*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_RD_COMMAND
861*77c1e3ccSAndroid Build Coastguard Worker if (cpi->gf_frame_index == 0 && !is_stat_generation_stage(cpi)) {
862*77c1e3ccSAndroid Build Coastguard Worker // perform tpl after filtering
863*77c1e3ccSAndroid Build Coastguard Worker int allow_tpl =
864*77c1e3ccSAndroid Build Coastguard Worker oxcf->gf_cfg.lag_in_frames > 1 && oxcf->algo_cfg.enable_tpl_model;
865*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->size > MAX_LENGTH_TPL_FRAME_STATS) {
866*77c1e3ccSAndroid Build Coastguard Worker allow_tpl = 0;
867*77c1e3ccSAndroid Build Coastguard Worker }
868*77c1e3ccSAndroid Build Coastguard Worker if (frame_params->frame_type != KEY_FRAME) {
869*77c1e3ccSAndroid Build Coastguard Worker // In rare case, it's possible to have non ARF/GF update_type here.
870*77c1e3ccSAndroid Build Coastguard Worker // We should set allow_tpl to zero in the situation
871*77c1e3ccSAndroid Build Coastguard Worker allow_tpl =
872*77c1e3ccSAndroid Build Coastguard Worker allow_tpl && (update_type == ARF_UPDATE || update_type == GF_UPDATE ||
873*77c1e3ccSAndroid Build Coastguard Worker (cpi->use_ducky_encode &&
874*77c1e3ccSAndroid Build Coastguard Worker cpi->ducky_encode_info.frame_info.gop_mode ==
875*77c1e3ccSAndroid Build Coastguard Worker DUCKY_ENCODE_GOP_MODE_RCL));
876*77c1e3ccSAndroid Build Coastguard Worker }
877*77c1e3ccSAndroid Build Coastguard Worker
878*77c1e3ccSAndroid Build Coastguard Worker if (allow_tpl) {
879*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->skip_tpl_setup_stats) {
880*77c1e3ccSAndroid Build Coastguard Worker av1_tpl_preload_rc_estimate(cpi, frame_params);
881*77c1e3ccSAndroid Build Coastguard Worker av1_tpl_setup_stats(cpi, 0, frame_params);
882*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY && !CONFIG_THREE_PASS
883*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->gf_frame_index == 0);
884*77c1e3ccSAndroid Build Coastguard Worker av1_vbr_rc_update_q_index_list(&cpi->vbr_rc_info, &cpi->ppi->tpl_data,
885*77c1e3ccSAndroid Build Coastguard Worker gf_group, cm->seq_params->bit_depth);
886*77c1e3ccSAndroid Build Coastguard Worker #endif
887*77c1e3ccSAndroid Build Coastguard Worker }
888*77c1e3ccSAndroid Build Coastguard Worker } else {
889*77c1e3ccSAndroid Build Coastguard Worker av1_init_tpl_stats(&cpi->ppi->tpl_data);
890*77c1e3ccSAndroid Build Coastguard Worker }
891*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
892*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.pass == AOM_RC_SECOND_PASS &&
893*77c1e3ccSAndroid Build Coastguard Worker cpi->second_pass_log_stream != NULL) {
894*77c1e3ccSAndroid Build Coastguard Worker TPL_INFO *tpl_info;
895*77c1e3ccSAndroid Build Coastguard Worker AOM_CHECK_MEM_ERROR(cm->error, tpl_info, aom_malloc(sizeof(*tpl_info)));
896*77c1e3ccSAndroid Build Coastguard Worker av1_pack_tpl_info(tpl_info, gf_group, &cpi->ppi->tpl_data);
897*77c1e3ccSAndroid Build Coastguard Worker av1_write_tpl_info(tpl_info, cpi->second_pass_log_stream,
898*77c1e3ccSAndroid Build Coastguard Worker cpi->common.error);
899*77c1e3ccSAndroid Build Coastguard Worker aom_free(tpl_info);
900*77c1e3ccSAndroid Build Coastguard Worker }
901*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
902*77c1e3ccSAndroid Build Coastguard Worker }
903*77c1e3ccSAndroid Build Coastguard Worker
904*77c1e3ccSAndroid Build Coastguard Worker if (av1_encode(cpi, dest, dest_size, frame_input, frame_params, frame_size) !=
905*77c1e3ccSAndroid Build Coastguard Worker AOM_CODEC_OK) {
906*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
907*77c1e3ccSAndroid Build Coastguard Worker }
908*77c1e3ccSAndroid Build Coastguard Worker
909*77c1e3ccSAndroid Build Coastguard Worker // Set frame_input source to true source for psnr calculation.
910*77c1e3ccSAndroid Build Coastguard Worker if (apply_filtering && is_psnr_calc_enabled(cpi)) {
911*77c1e3ccSAndroid Build Coastguard Worker cpi->source = av1_realloc_and_scale_if_required(
912*77c1e3ccSAndroid Build Coastguard Worker cm, source_buffer, &cpi->scaled_source, cm->features.interp_filter, 0,
913*77c1e3ccSAndroid Build Coastguard Worker false, true, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
914*77c1e3ccSAndroid Build Coastguard Worker cpi->unscaled_source = source_buffer;
915*77c1e3ccSAndroid Build Coastguard Worker }
916*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
917*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.pass == 2) end_timing(cpi, denoise_and_encode_time);
918*77c1e3ccSAndroid Build Coastguard Worker #endif
919*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
920*77c1e3ccSAndroid Build Coastguard Worker }
921*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
922*77c1e3ccSAndroid Build Coastguard Worker
923*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
924*77c1e3ccSAndroid Build Coastguard Worker // Struct to keep track of relevant reference frame data.
925*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
926*77c1e3ccSAndroid Build Coastguard Worker int map_idx;
927*77c1e3ccSAndroid Build Coastguard Worker int disp_order;
928*77c1e3ccSAndroid Build Coastguard Worker int pyr_level;
929*77c1e3ccSAndroid Build Coastguard Worker int used;
930*77c1e3ccSAndroid Build Coastguard Worker } RefBufMapData;
931*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
932*77c1e3ccSAndroid Build Coastguard Worker
933*77c1e3ccSAndroid Build Coastguard Worker // Comparison function to sort reference frames in ascending display order.
934*77c1e3ccSAndroid Build Coastguard Worker static int compare_map_idx_pair_asc(const void *a, const void *b) {
935*77c1e3ccSAndroid Build Coastguard Worker if (((RefBufMapData *)a)->disp_order == ((RefBufMapData *)b)->disp_order) {
936*77c1e3ccSAndroid Build Coastguard Worker return 0;
937*77c1e3ccSAndroid Build Coastguard Worker } else if (((const RefBufMapData *)a)->disp_order >
938*77c1e3ccSAndroid Build Coastguard Worker ((const RefBufMapData *)b)->disp_order) {
939*77c1e3ccSAndroid Build Coastguard Worker return 1;
940*77c1e3ccSAndroid Build Coastguard Worker } else {
941*77c1e3ccSAndroid Build Coastguard Worker return -1;
942*77c1e3ccSAndroid Build Coastguard Worker }
943*77c1e3ccSAndroid Build Coastguard Worker }
944*77c1e3ccSAndroid Build Coastguard Worker
945*77c1e3ccSAndroid Build Coastguard Worker // Checks to see if a particular reference frame is already in the reference
946*77c1e3ccSAndroid Build Coastguard Worker // frame map.
947*77c1e3ccSAndroid Build Coastguard Worker static int is_in_ref_map(RefBufMapData *map, int disp_order, int n_frames) {
948*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < n_frames; i++) {
949*77c1e3ccSAndroid Build Coastguard Worker if (disp_order == map[i].disp_order) return 1;
950*77c1e3ccSAndroid Build Coastguard Worker }
951*77c1e3ccSAndroid Build Coastguard Worker return 0;
952*77c1e3ccSAndroid Build Coastguard Worker }
953*77c1e3ccSAndroid Build Coastguard Worker
954*77c1e3ccSAndroid Build Coastguard Worker // Add a reference buffer index to a named reference slot.
955*77c1e3ccSAndroid Build Coastguard Worker static void add_ref_to_slot(RefBufMapData *ref, int *const remapped_ref_idx,
956*77c1e3ccSAndroid Build Coastguard Worker int frame) {
957*77c1e3ccSAndroid Build Coastguard Worker remapped_ref_idx[frame - LAST_FRAME] = ref->map_idx;
958*77c1e3ccSAndroid Build Coastguard Worker ref->used = 1;
959*77c1e3ccSAndroid Build Coastguard Worker }
960*77c1e3ccSAndroid Build Coastguard Worker
961*77c1e3ccSAndroid Build Coastguard Worker // Threshold dictating when we are allowed to start considering
962*77c1e3ccSAndroid Build Coastguard Worker // leaving lowest level frames unmapped.
963*77c1e3ccSAndroid Build Coastguard Worker #define LOW_LEVEL_FRAMES_TR 5
964*77c1e3ccSAndroid Build Coastguard Worker
965*77c1e3ccSAndroid Build Coastguard Worker // Find which reference buffer should be left out of the named mapping.
966*77c1e3ccSAndroid Build Coastguard Worker // This is because there are 8 reference buffers and only 7 named slots.
967*77c1e3ccSAndroid Build Coastguard Worker static void set_unmapped_ref(RefBufMapData *buffer_map, int n_bufs,
968*77c1e3ccSAndroid Build Coastguard Worker int n_min_level_refs, int min_level,
969*77c1e3ccSAndroid Build Coastguard Worker int cur_frame_disp) {
970*77c1e3ccSAndroid Build Coastguard Worker int max_dist = 0;
971*77c1e3ccSAndroid Build Coastguard Worker int unmapped_idx = -1;
972*77c1e3ccSAndroid Build Coastguard Worker if (n_bufs <= ALTREF_FRAME) return;
973*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < n_bufs; i++) {
974*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].used) continue;
975*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].pyr_level != min_level ||
976*77c1e3ccSAndroid Build Coastguard Worker n_min_level_refs >= LOW_LEVEL_FRAMES_TR) {
977*77c1e3ccSAndroid Build Coastguard Worker int dist = abs(cur_frame_disp - buffer_map[i].disp_order);
978*77c1e3ccSAndroid Build Coastguard Worker if (dist > max_dist) {
979*77c1e3ccSAndroid Build Coastguard Worker max_dist = dist;
980*77c1e3ccSAndroid Build Coastguard Worker unmapped_idx = i;
981*77c1e3ccSAndroid Build Coastguard Worker }
982*77c1e3ccSAndroid Build Coastguard Worker }
983*77c1e3ccSAndroid Build Coastguard Worker }
984*77c1e3ccSAndroid Build Coastguard Worker assert(unmapped_idx >= 0 && "Unmapped reference not found");
985*77c1e3ccSAndroid Build Coastguard Worker buffer_map[unmapped_idx].used = 1;
986*77c1e3ccSAndroid Build Coastguard Worker }
987*77c1e3ccSAndroid Build Coastguard Worker
988*77c1e3ccSAndroid Build Coastguard Worker void av1_get_ref_frames(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
989*77c1e3ccSAndroid Build Coastguard Worker int cur_frame_disp, const AV1_COMP *cpi, int gf_index,
990*77c1e3ccSAndroid Build Coastguard Worker int is_parallel_encode,
991*77c1e3ccSAndroid Build Coastguard Worker int remapped_ref_idx[REF_FRAMES]) {
992*77c1e3ccSAndroid Build Coastguard Worker int buf_map_idx = 0;
993*77c1e3ccSAndroid Build Coastguard Worker
994*77c1e3ccSAndroid Build Coastguard Worker // Initialize reference frame mappings.
995*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; ++i) remapped_ref_idx[i] = INVALID_IDX;
996*77c1e3ccSAndroid Build Coastguard Worker
997*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
998*77c1e3ccSAndroid Build Coastguard Worker if (cpi->use_ducky_encode &&
999*77c1e3ccSAndroid Build Coastguard Worker cpi->ducky_encode_info.frame_info.gop_mode == DUCKY_ENCODE_GOP_MODE_RCL) {
1000*77c1e3ccSAndroid Build Coastguard Worker for (int rf = LAST_FRAME; rf < REF_FRAMES; ++rf) {
1001*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->gf_group.ref_frame_list[gf_index][rf] != INVALID_IDX) {
1002*77c1e3ccSAndroid Build Coastguard Worker remapped_ref_idx[rf - LAST_FRAME] =
1003*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.ref_frame_list[gf_index][rf];
1004*77c1e3ccSAndroid Build Coastguard Worker }
1005*77c1e3ccSAndroid Build Coastguard Worker }
1006*77c1e3ccSAndroid Build Coastguard Worker
1007*77c1e3ccSAndroid Build Coastguard Worker int valid_rf_idx = 0;
1008*77c1e3ccSAndroid Build Coastguard Worker static const int ref_frame_type_order[REF_FRAMES - LAST_FRAME] = {
1009*77c1e3ccSAndroid Build Coastguard Worker GOLDEN_FRAME, ALTREF_FRAME, LAST_FRAME, BWDREF_FRAME,
1010*77c1e3ccSAndroid Build Coastguard Worker ALTREF2_FRAME, LAST2_FRAME, LAST3_FRAME
1011*77c1e3ccSAndroid Build Coastguard Worker };
1012*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES - LAST_FRAME; i++) {
1013*77c1e3ccSAndroid Build Coastguard Worker int rf = ref_frame_type_order[i];
1014*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[rf - LAST_FRAME] != INVALID_IDX) {
1015*77c1e3ccSAndroid Build Coastguard Worker valid_rf_idx = remapped_ref_idx[rf - LAST_FRAME];
1016*77c1e3ccSAndroid Build Coastguard Worker break;
1017*77c1e3ccSAndroid Build Coastguard Worker }
1018*77c1e3ccSAndroid Build Coastguard Worker }
1019*77c1e3ccSAndroid Build Coastguard Worker
1020*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; ++i) {
1021*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[i] == INVALID_IDX) {
1022*77c1e3ccSAndroid Build Coastguard Worker remapped_ref_idx[i] = valid_rf_idx;
1023*77c1e3ccSAndroid Build Coastguard Worker }
1024*77c1e3ccSAndroid Build Coastguard Worker }
1025*77c1e3ccSAndroid Build Coastguard Worker
1026*77c1e3ccSAndroid Build Coastguard Worker return;
1027*77c1e3ccSAndroid Build Coastguard Worker }
1028*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
1029*77c1e3ccSAndroid Build Coastguard Worker
1030*77c1e3ccSAndroid Build Coastguard Worker RefBufMapData buffer_map[REF_FRAMES];
1031*77c1e3ccSAndroid Build Coastguard Worker int n_bufs = 0;
1032*77c1e3ccSAndroid Build Coastguard Worker memset(buffer_map, 0, REF_FRAMES * sizeof(buffer_map[0]));
1033*77c1e3ccSAndroid Build Coastguard Worker int min_level = MAX_ARF_LAYERS;
1034*77c1e3ccSAndroid Build Coastguard Worker int max_level = 0;
1035*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *gf_group = &cpi->ppi->gf_group;
1036*77c1e3ccSAndroid Build Coastguard Worker int skip_ref_unmapping = 0;
1037*77c1e3ccSAndroid Build Coastguard Worker int is_one_pass_rt = is_one_pass_rt_params(cpi);
1038*77c1e3ccSAndroid Build Coastguard Worker
1039*77c1e3ccSAndroid Build Coastguard Worker // Go through current reference buffers and store display order, pyr level,
1040*77c1e3ccSAndroid Build Coastguard Worker // and map index.
1041*77c1e3ccSAndroid Build Coastguard Worker for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) {
1042*77c1e3ccSAndroid Build Coastguard Worker // Get reference frame buffer.
1043*77c1e3ccSAndroid Build Coastguard Worker RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx];
1044*77c1e3ccSAndroid Build Coastguard Worker if (ref_pair.disp_order == -1) continue;
1045*77c1e3ccSAndroid Build Coastguard Worker const int frame_order = ref_pair.disp_order;
1046*77c1e3ccSAndroid Build Coastguard Worker // Avoid duplicates.
1047*77c1e3ccSAndroid Build Coastguard Worker if (is_in_ref_map(buffer_map, frame_order, n_bufs)) continue;
1048*77c1e3ccSAndroid Build Coastguard Worker const int reference_frame_level = ref_pair.pyr_level;
1049*77c1e3ccSAndroid Build Coastguard Worker
1050*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the lowest and highest levels that currently exist.
1051*77c1e3ccSAndroid Build Coastguard Worker if (reference_frame_level < min_level) min_level = reference_frame_level;
1052*77c1e3ccSAndroid Build Coastguard Worker if (reference_frame_level > max_level) max_level = reference_frame_level;
1053*77c1e3ccSAndroid Build Coastguard Worker
1054*77c1e3ccSAndroid Build Coastguard Worker buffer_map[n_bufs].map_idx = map_idx;
1055*77c1e3ccSAndroid Build Coastguard Worker buffer_map[n_bufs].disp_order = frame_order;
1056*77c1e3ccSAndroid Build Coastguard Worker buffer_map[n_bufs].pyr_level = reference_frame_level;
1057*77c1e3ccSAndroid Build Coastguard Worker buffer_map[n_bufs].used = 0;
1058*77c1e3ccSAndroid Build Coastguard Worker n_bufs++;
1059*77c1e3ccSAndroid Build Coastguard Worker }
1060*77c1e3ccSAndroid Build Coastguard Worker
1061*77c1e3ccSAndroid Build Coastguard Worker // Sort frames in ascending display order.
1062*77c1e3ccSAndroid Build Coastguard Worker qsort(buffer_map, n_bufs, sizeof(buffer_map[0]), compare_map_idx_pair_asc);
1063*77c1e3ccSAndroid Build Coastguard Worker
1064*77c1e3ccSAndroid Build Coastguard Worker int n_min_level_refs = 0;
1065*77c1e3ccSAndroid Build Coastguard Worker int closest_past_ref = -1;
1066*77c1e3ccSAndroid Build Coastguard Worker int golden_idx = -1;
1067*77c1e3ccSAndroid Build Coastguard Worker int altref_idx = -1;
1068*77c1e3ccSAndroid Build Coastguard Worker
1069*77c1e3ccSAndroid Build Coastguard Worker // Find the GOLDEN_FRAME and BWDREF_FRAME.
1070*77c1e3ccSAndroid Build Coastguard Worker // Also collect various stats about the reference frames for the remaining
1071*77c1e3ccSAndroid Build Coastguard Worker // mappings.
1072*77c1e3ccSAndroid Build Coastguard Worker for (int i = n_bufs - 1; i >= 0; i--) {
1073*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].pyr_level == min_level) {
1074*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the number of lowest level frames.
1075*77c1e3ccSAndroid Build Coastguard Worker n_min_level_refs++;
1076*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].disp_order < cur_frame_disp && golden_idx == -1 &&
1077*77c1e3ccSAndroid Build Coastguard Worker remapped_ref_idx[GOLDEN_FRAME - LAST_FRAME] == INVALID_IDX) {
1078*77c1e3ccSAndroid Build Coastguard Worker // Save index for GOLDEN.
1079*77c1e3ccSAndroid Build Coastguard Worker golden_idx = i;
1080*77c1e3ccSAndroid Build Coastguard Worker } else if (buffer_map[i].disp_order > cur_frame_disp &&
1081*77c1e3ccSAndroid Build Coastguard Worker altref_idx == -1 &&
1082*77c1e3ccSAndroid Build Coastguard Worker remapped_ref_idx[ALTREF_FRAME - LAST_FRAME] == INVALID_IDX) {
1083*77c1e3ccSAndroid Build Coastguard Worker // Save index for ALTREF.
1084*77c1e3ccSAndroid Build Coastguard Worker altref_idx = i;
1085*77c1e3ccSAndroid Build Coastguard Worker }
1086*77c1e3ccSAndroid Build Coastguard Worker } else if (buffer_map[i].disp_order == cur_frame_disp) {
1087*77c1e3ccSAndroid Build Coastguard Worker // Map the BWDREF_FRAME if this is the show_existing_frame.
1088*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[i], remapped_ref_idx, BWDREF_FRAME);
1089*77c1e3ccSAndroid Build Coastguard Worker }
1090*77c1e3ccSAndroid Build Coastguard Worker
1091*77c1e3ccSAndroid Build Coastguard Worker // During parallel encodes of lower layer frames, exclude the first frame
1092*77c1e3ccSAndroid Build Coastguard Worker // (frame_parallel_level 1) from being used for the reference assignment of
1093*77c1e3ccSAndroid Build Coastguard Worker // the second frame (frame_parallel_level 2).
1094*77c1e3ccSAndroid Build Coastguard Worker if (!is_one_pass_rt && gf_group->frame_parallel_level[gf_index] == 2 &&
1095*77c1e3ccSAndroid Build Coastguard Worker gf_group->frame_parallel_level[gf_index - 1] == 1 &&
1096*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[gf_index - 1] == INTNL_ARF_UPDATE) {
1097*77c1e3ccSAndroid Build Coastguard Worker assert(gf_group->update_type[gf_index] == INTNL_ARF_UPDATE);
1098*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1099*77c1e3ccSAndroid Build Coastguard Worker is_parallel_encode = (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_ENCODE)
1100*77c1e3ccSAndroid Build Coastguard Worker ? is_parallel_encode
1101*77c1e3ccSAndroid Build Coastguard Worker : 0;
1102*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
1103*77c1e3ccSAndroid Build Coastguard Worker // If parallel cpis are active, use ref_idx_to_skip, else, use display
1104*77c1e3ccSAndroid Build Coastguard Worker // index.
1105*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(is_parallel_encode, cpi->ref_idx_to_skip != INVALID_IDX));
1106*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(!is_parallel_encode,
1107*77c1e3ccSAndroid Build Coastguard Worker gf_group->skip_frame_as_ref[gf_index] != INVALID_IDX));
1108*77c1e3ccSAndroid Build Coastguard Worker buffer_map[i].used = is_parallel_encode
1109*77c1e3ccSAndroid Build Coastguard Worker ? (buffer_map[i].map_idx == cpi->ref_idx_to_skip)
1110*77c1e3ccSAndroid Build Coastguard Worker : (buffer_map[i].disp_order ==
1111*77c1e3ccSAndroid Build Coastguard Worker gf_group->skip_frame_as_ref[gf_index]);
1112*77c1e3ccSAndroid Build Coastguard Worker // In case a ref frame is excluded from being used during assignment,
1113*77c1e3ccSAndroid Build Coastguard Worker // skip the call to set_unmapped_ref(). Applicable in steady state.
1114*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].used) skip_ref_unmapping = 1;
1115*77c1e3ccSAndroid Build Coastguard Worker }
1116*77c1e3ccSAndroid Build Coastguard Worker
1117*77c1e3ccSAndroid Build Coastguard Worker // Keep track of where the frames change from being past frames to future
1118*77c1e3ccSAndroid Build Coastguard Worker // frames.
1119*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[i].disp_order < cur_frame_disp && closest_past_ref < 0)
1120*77c1e3ccSAndroid Build Coastguard Worker closest_past_ref = i;
1121*77c1e3ccSAndroid Build Coastguard Worker }
1122*77c1e3ccSAndroid Build Coastguard Worker
1123*77c1e3ccSAndroid Build Coastguard Worker // Do not map GOLDEN and ALTREF based on their pyramid level if all reference
1124*77c1e3ccSAndroid Build Coastguard Worker // frames have the same level.
1125*77c1e3ccSAndroid Build Coastguard Worker if (n_min_level_refs <= n_bufs) {
1126*77c1e3ccSAndroid Build Coastguard Worker // Map the GOLDEN_FRAME.
1127*77c1e3ccSAndroid Build Coastguard Worker if (golden_idx > -1)
1128*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[golden_idx], remapped_ref_idx, GOLDEN_FRAME);
1129*77c1e3ccSAndroid Build Coastguard Worker // Map the ALTREF_FRAME.
1130*77c1e3ccSAndroid Build Coastguard Worker if (altref_idx > -1)
1131*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[altref_idx], remapped_ref_idx, ALTREF_FRAME);
1132*77c1e3ccSAndroid Build Coastguard Worker }
1133*77c1e3ccSAndroid Build Coastguard Worker
1134*77c1e3ccSAndroid Build Coastguard Worker // Find the buffer to be excluded from the mapping.
1135*77c1e3ccSAndroid Build Coastguard Worker if (!skip_ref_unmapping)
1136*77c1e3ccSAndroid Build Coastguard Worker set_unmapped_ref(buffer_map, n_bufs, n_min_level_refs, min_level,
1137*77c1e3ccSAndroid Build Coastguard Worker cur_frame_disp);
1138*77c1e3ccSAndroid Build Coastguard Worker
1139*77c1e3ccSAndroid Build Coastguard Worker // Place past frames in LAST_FRAME, LAST2_FRAME, and LAST3_FRAME.
1140*77c1e3ccSAndroid Build Coastguard Worker for (int frame = LAST_FRAME; frame < GOLDEN_FRAME; frame++) {
1141*77c1e3ccSAndroid Build Coastguard Worker // Continue if the current ref slot is already full.
1142*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue;
1143*77c1e3ccSAndroid Build Coastguard Worker // Find the next unmapped reference buffer
1144*77c1e3ccSAndroid Build Coastguard Worker // in decreasing ouptut order relative to current picture.
1145*77c1e3ccSAndroid Build Coastguard Worker int next_buf_max = 0;
1146*77c1e3ccSAndroid Build Coastguard Worker int next_disp_order = INT_MIN;
1147*77c1e3ccSAndroid Build Coastguard Worker for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) {
1148*77c1e3ccSAndroid Build Coastguard Worker if (!buffer_map[buf_map_idx].used &&
1149*77c1e3ccSAndroid Build Coastguard Worker buffer_map[buf_map_idx].disp_order < cur_frame_disp &&
1150*77c1e3ccSAndroid Build Coastguard Worker buffer_map[buf_map_idx].disp_order > next_disp_order) {
1151*77c1e3ccSAndroid Build Coastguard Worker next_disp_order = buffer_map[buf_map_idx].disp_order;
1152*77c1e3ccSAndroid Build Coastguard Worker next_buf_max = buf_map_idx;
1153*77c1e3ccSAndroid Build Coastguard Worker }
1154*77c1e3ccSAndroid Build Coastguard Worker }
1155*77c1e3ccSAndroid Build Coastguard Worker buf_map_idx = next_buf_max;
1156*77c1e3ccSAndroid Build Coastguard Worker if (buf_map_idx < 0) break;
1157*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[buf_map_idx].used) break;
1158*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
1159*77c1e3ccSAndroid Build Coastguard Worker }
1160*77c1e3ccSAndroid Build Coastguard Worker
1161*77c1e3ccSAndroid Build Coastguard Worker // Place future frames (if there are any) in BWDREF_FRAME and ALTREF2_FRAME.
1162*77c1e3ccSAndroid Build Coastguard Worker for (int frame = BWDREF_FRAME; frame < REF_FRAMES; frame++) {
1163*77c1e3ccSAndroid Build Coastguard Worker // Continue if the current ref slot is already full.
1164*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue;
1165*77c1e3ccSAndroid Build Coastguard Worker // Find the next unmapped reference buffer
1166*77c1e3ccSAndroid Build Coastguard Worker // in increasing ouptut order relative to current picture.
1167*77c1e3ccSAndroid Build Coastguard Worker int next_buf_max = 0;
1168*77c1e3ccSAndroid Build Coastguard Worker int next_disp_order = INT_MAX;
1169*77c1e3ccSAndroid Build Coastguard Worker for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) {
1170*77c1e3ccSAndroid Build Coastguard Worker if (!buffer_map[buf_map_idx].used &&
1171*77c1e3ccSAndroid Build Coastguard Worker buffer_map[buf_map_idx].disp_order > cur_frame_disp &&
1172*77c1e3ccSAndroid Build Coastguard Worker buffer_map[buf_map_idx].disp_order < next_disp_order) {
1173*77c1e3ccSAndroid Build Coastguard Worker next_disp_order = buffer_map[buf_map_idx].disp_order;
1174*77c1e3ccSAndroid Build Coastguard Worker next_buf_max = buf_map_idx;
1175*77c1e3ccSAndroid Build Coastguard Worker }
1176*77c1e3ccSAndroid Build Coastguard Worker }
1177*77c1e3ccSAndroid Build Coastguard Worker buf_map_idx = next_buf_max;
1178*77c1e3ccSAndroid Build Coastguard Worker if (buf_map_idx < 0) break;
1179*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[buf_map_idx].used) break;
1180*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
1181*77c1e3ccSAndroid Build Coastguard Worker }
1182*77c1e3ccSAndroid Build Coastguard Worker
1183*77c1e3ccSAndroid Build Coastguard Worker // Place remaining past frames.
1184*77c1e3ccSAndroid Build Coastguard Worker buf_map_idx = closest_past_ref;
1185*77c1e3ccSAndroid Build Coastguard Worker for (int frame = LAST_FRAME; frame < REF_FRAMES; frame++) {
1186*77c1e3ccSAndroid Build Coastguard Worker // Continue if the current ref slot is already full.
1187*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue;
1188*77c1e3ccSAndroid Build Coastguard Worker // Find the next unmapped reference buffer.
1189*77c1e3ccSAndroid Build Coastguard Worker for (; buf_map_idx >= 0; buf_map_idx--) {
1190*77c1e3ccSAndroid Build Coastguard Worker if (!buffer_map[buf_map_idx].used) break;
1191*77c1e3ccSAndroid Build Coastguard Worker }
1192*77c1e3ccSAndroid Build Coastguard Worker if (buf_map_idx < 0) break;
1193*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[buf_map_idx].used) break;
1194*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
1195*77c1e3ccSAndroid Build Coastguard Worker }
1196*77c1e3ccSAndroid Build Coastguard Worker
1197*77c1e3ccSAndroid Build Coastguard Worker // Place remaining future frames.
1198*77c1e3ccSAndroid Build Coastguard Worker buf_map_idx = n_bufs - 1;
1199*77c1e3ccSAndroid Build Coastguard Worker for (int frame = ALTREF_FRAME; frame >= LAST_FRAME; frame--) {
1200*77c1e3ccSAndroid Build Coastguard Worker // Continue if the current ref slot is already full.
1201*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue;
1202*77c1e3ccSAndroid Build Coastguard Worker // Find the next unmapped reference buffer.
1203*77c1e3ccSAndroid Build Coastguard Worker for (; buf_map_idx > closest_past_ref; buf_map_idx--) {
1204*77c1e3ccSAndroid Build Coastguard Worker if (!buffer_map[buf_map_idx].used) break;
1205*77c1e3ccSAndroid Build Coastguard Worker }
1206*77c1e3ccSAndroid Build Coastguard Worker if (buf_map_idx < 0) break;
1207*77c1e3ccSAndroid Build Coastguard Worker if (buffer_map[buf_map_idx].used) break;
1208*77c1e3ccSAndroid Build Coastguard Worker add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
1209*77c1e3ccSAndroid Build Coastguard Worker }
1210*77c1e3ccSAndroid Build Coastguard Worker
1211*77c1e3ccSAndroid Build Coastguard Worker // Fill any slots that are empty (should only happen for the first 7 frames).
1212*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; ++i)
1213*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx[i] == INVALID_IDX) remapped_ref_idx[i] = 0;
1214*77c1e3ccSAndroid Build Coastguard Worker }
1215*77c1e3ccSAndroid Build Coastguard Worker
1216*77c1e3ccSAndroid Build Coastguard Worker int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size,
1217*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dest, size_t dest_size,
1218*77c1e3ccSAndroid Build Coastguard Worker unsigned int *frame_flags, int64_t *const time_stamp,
1219*77c1e3ccSAndroid Build Coastguard Worker int64_t *const time_end,
1220*77c1e3ccSAndroid Build Coastguard Worker const aom_rational64_t *const timestamp_ratio,
1221*77c1e3ccSAndroid Build Coastguard Worker int *const pop_lookahead, int flush) {
1222*77c1e3ccSAndroid Build Coastguard Worker AV1EncoderConfig *const oxcf = &cpi->oxcf;
1223*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1224*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *gf_group = &cpi->ppi->gf_group;
1225*77c1e3ccSAndroid Build Coastguard Worker ExternalFlags *const ext_flags = &cpi->ext_flags;
1226*77c1e3ccSAndroid Build Coastguard Worker GFConfig *const gf_cfg = &oxcf->gf_cfg;
1227*77c1e3ccSAndroid Build Coastguard Worker
1228*77c1e3ccSAndroid Build Coastguard Worker EncodeFrameInput frame_input;
1229*77c1e3ccSAndroid Build Coastguard Worker EncodeFrameParams frame_params;
1230*77c1e3ccSAndroid Build Coastguard Worker size_t frame_size;
1231*77c1e3ccSAndroid Build Coastguard Worker memset(&frame_input, 0, sizeof(frame_input));
1232*77c1e3ccSAndroid Build Coastguard Worker memset(&frame_params, 0, sizeof(frame_params));
1233*77c1e3ccSAndroid Build Coastguard Worker frame_size = 0;
1234*77c1e3ccSAndroid Build Coastguard Worker
1235*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
1236*77c1e3ccSAndroid Build Coastguard Worker VBR_RATECTRL_INFO *vbr_rc_info = &cpi->vbr_rc_info;
1237*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->pass == AOM_RC_THIRD_PASS && vbr_rc_info->ready == 0) {
1238*77c1e3ccSAndroid Build Coastguard Worker THIRD_PASS_FRAME_INFO frame_info[MAX_THIRD_PASS_BUF];
1239*77c1e3ccSAndroid Build Coastguard Worker av1_open_second_pass_log(cpi, 1);
1240*77c1e3ccSAndroid Build Coastguard Worker FILE *second_pass_log_stream = cpi->second_pass_log_stream;
1241*77c1e3ccSAndroid Build Coastguard Worker fseek(second_pass_log_stream, 0, SEEK_END);
1242*77c1e3ccSAndroid Build Coastguard Worker size_t file_size = ftell(second_pass_log_stream);
1243*77c1e3ccSAndroid Build Coastguard Worker rewind(second_pass_log_stream);
1244*77c1e3ccSAndroid Build Coastguard Worker size_t read_size = 0;
1245*77c1e3ccSAndroid Build Coastguard Worker while (read_size < file_size) {
1246*77c1e3ccSAndroid Build Coastguard Worker THIRD_PASS_GOP_INFO gop_info;
1247*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error = cpi->common.error;
1248*77c1e3ccSAndroid Build Coastguard Worker // Read in GOP information from the second pass file.
1249*77c1e3ccSAndroid Build Coastguard Worker av1_read_second_pass_gop_info(second_pass_log_stream, &gop_info, error);
1250*77c1e3ccSAndroid Build Coastguard Worker TPL_INFO *tpl_info;
1251*77c1e3ccSAndroid Build Coastguard Worker AOM_CHECK_MEM_ERROR(cm->error, tpl_info, aom_malloc(sizeof(*tpl_info)));
1252*77c1e3ccSAndroid Build Coastguard Worker av1_read_tpl_info(tpl_info, second_pass_log_stream, error);
1253*77c1e3ccSAndroid Build Coastguard Worker // Read in per-frame info from second-pass encoding
1254*77c1e3ccSAndroid Build Coastguard Worker av1_read_second_pass_per_frame_info(second_pass_log_stream, frame_info,
1255*77c1e3ccSAndroid Build Coastguard Worker gop_info.num_frames, error);
1256*77c1e3ccSAndroid Build Coastguard Worker av1_vbr_rc_append_tpl_info(vbr_rc_info, tpl_info);
1257*77c1e3ccSAndroid Build Coastguard Worker read_size = ftell(second_pass_log_stream);
1258*77c1e3ccSAndroid Build Coastguard Worker aom_free(tpl_info);
1259*77c1e3ccSAndroid Build Coastguard Worker }
1260*77c1e3ccSAndroid Build Coastguard Worker av1_close_second_pass_log(cpi);
1261*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.rc_cfg.mode == AOM_Q) {
1262*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->base_q_index = cpi->oxcf.rc_cfg.cq_level;
1263*77c1e3ccSAndroid Build Coastguard Worker av1_vbr_rc_compute_q_indices(
1264*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->base_q_index, vbr_rc_info->total_frame_count,
1265*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->qstep_ratio_list, cm->seq_params->bit_depth,
1266*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->q_index_list);
1267*77c1e3ccSAndroid Build Coastguard Worker } else {
1268*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->base_q_index = av1_vbr_rc_info_estimate_base_q(
1269*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->total_bit_budget, cm->seq_params->bit_depth,
1270*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->scale_factors, vbr_rc_info->total_frame_count,
1271*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->update_type_list, vbr_rc_info->qstep_ratio_list,
1272*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->txfm_stats_list, vbr_rc_info->q_index_list, NULL);
1273*77c1e3ccSAndroid Build Coastguard Worker }
1274*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->ready = 1;
1275*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RATECTRL_LOG
1276*77c1e3ccSAndroid Build Coastguard Worker rc_log_record_chunk_info(&cpi->rc_log, vbr_rc_info->base_q_index,
1277*77c1e3ccSAndroid Build Coastguard Worker vbr_rc_info->total_frame_count);
1278*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_RATECTRL_LOG
1279*77c1e3ccSAndroid Build Coastguard Worker }
1280*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
1281*77c1e3ccSAndroid Build Coastguard Worker
1282*77c1e3ccSAndroid Build Coastguard Worker // Check if we need to stuff more src frames
1283*77c1e3ccSAndroid Build Coastguard Worker if (flush == 0) {
1284*77c1e3ccSAndroid Build Coastguard Worker int srcbuf_size =
1285*77c1e3ccSAndroid Build Coastguard Worker av1_lookahead_depth(cpi->ppi->lookahead, cpi->compressor_stage);
1286*77c1e3ccSAndroid Build Coastguard Worker int pop_size =
1287*77c1e3ccSAndroid Build Coastguard Worker av1_lookahead_pop_sz(cpi->ppi->lookahead, cpi->compressor_stage);
1288*77c1e3ccSAndroid Build Coastguard Worker
1289*77c1e3ccSAndroid Build Coastguard Worker // Continue buffering look ahead buffer.
1290*77c1e3ccSAndroid Build Coastguard Worker if (srcbuf_size < pop_size) return -1;
1291*77c1e3ccSAndroid Build Coastguard Worker }
1292*77c1e3ccSAndroid Build Coastguard Worker
1293*77c1e3ccSAndroid Build Coastguard Worker if (!av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage)) {
1294*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1295*77c1e3ccSAndroid Build Coastguard Worker if (flush && oxcf->pass == AOM_RC_FIRST_PASS &&
1296*77c1e3ccSAndroid Build Coastguard Worker !cpi->ppi->twopass.first_pass_done) {
1297*77c1e3ccSAndroid Build Coastguard Worker av1_end_first_pass(cpi); /* get last stats packet */
1298*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.first_pass_done = 1;
1299*77c1e3ccSAndroid Build Coastguard Worker }
1300*77c1e3ccSAndroid Build Coastguard Worker #endif
1301*77c1e3ccSAndroid Build Coastguard Worker return -1;
1302*77c1e3ccSAndroid Build Coastguard Worker }
1303*77c1e3ccSAndroid Build Coastguard Worker
1304*77c1e3ccSAndroid Build Coastguard Worker // TODO(sarahparker) finish bit allocation for one pass pyramid
1305*77c1e3ccSAndroid Build Coastguard Worker if (has_no_stats_stage(cpi)) {
1306*77c1e3ccSAndroid Build Coastguard Worker gf_cfg->gf_max_pyr_height =
1307*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(gf_cfg->gf_max_pyr_height, USE_ALTREF_FOR_ONE_PASS);
1308*77c1e3ccSAndroid Build Coastguard Worker gf_cfg->gf_min_pyr_height =
1309*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(gf_cfg->gf_min_pyr_height, gf_cfg->gf_max_pyr_height);
1310*77c1e3ccSAndroid Build Coastguard Worker }
1311*77c1e3ccSAndroid Build Coastguard Worker
1312*77c1e3ccSAndroid Build Coastguard Worker // Allocation of mi buffers.
1313*77c1e3ccSAndroid Build Coastguard Worker alloc_mb_mode_info_buffers(cpi);
1314*77c1e3ccSAndroid Build Coastguard Worker
1315*77c1e3ccSAndroid Build Coastguard Worker cpi->skip_tpl_setup_stats = 0;
1316*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1317*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->pass != AOM_RC_FIRST_PASS) {
1318*77c1e3ccSAndroid Build Coastguard Worker TplParams *const tpl_data = &cpi->ppi->tpl_data;
1319*77c1e3ccSAndroid Build Coastguard Worker if (tpl_data->tpl_stats_pool[0] == NULL) {
1320*77c1e3ccSAndroid Build Coastguard Worker av1_setup_tpl_buffers(cpi->ppi, &cm->mi_params, oxcf->frm_dim_cfg.width,
1321*77c1e3ccSAndroid Build Coastguard Worker oxcf->frm_dim_cfg.height, 0,
1322*77c1e3ccSAndroid Build Coastguard Worker oxcf->gf_cfg.lag_in_frames);
1323*77c1e3ccSAndroid Build Coastguard Worker }
1324*77c1e3ccSAndroid Build Coastguard Worker }
1325*77c1e3ccSAndroid Build Coastguard Worker cpi->twopass_frame.this_frame = NULL;
1326*77c1e3ccSAndroid Build Coastguard Worker const int use_one_pass_rt_params = is_one_pass_rt_params(cpi);
1327*77c1e3ccSAndroid Build Coastguard Worker if (!use_one_pass_rt_params && !is_stat_generation_stage(cpi)) {
1328*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1329*77c1e3ccSAndroid Build Coastguard Worker start_timing(cpi, av1_get_second_pass_params_time);
1330*77c1e3ccSAndroid Build Coastguard Worker #endif
1331*77c1e3ccSAndroid Build Coastguard Worker
1332*77c1e3ccSAndroid Build Coastguard Worker // Initialise frame_level_rate_correction_factors with value previous
1333*77c1e3ccSAndroid Build Coastguard Worker // to the parallel frames.
1334*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
1335*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < RATE_FACTOR_LEVELS; i++) {
1336*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.frame_level_rate_correction_factors[i] =
1337*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1338*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE)
1339*77c1e3ccSAndroid Build Coastguard Worker ? cpi->ppi->p_rc.temp_rate_correction_factors[i]
1340*77c1e3ccSAndroid Build Coastguard Worker :
1341*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
1342*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->p_rc.rate_correction_factors[i];
1343*77c1e3ccSAndroid Build Coastguard Worker }
1344*77c1e3ccSAndroid Build Coastguard Worker }
1345*77c1e3ccSAndroid Build Coastguard Worker
1346*77c1e3ccSAndroid Build Coastguard Worker // copy mv_stats from ppi to frame_level cpi.
1347*77c1e3ccSAndroid Build Coastguard Worker cpi->mv_stats = cpi->ppi->mv_stats;
1348*77c1e3ccSAndroid Build Coastguard Worker av1_get_second_pass_params(cpi, &frame_params, *frame_flags);
1349*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1350*77c1e3ccSAndroid Build Coastguard Worker end_timing(cpi, av1_get_second_pass_params_time);
1351*77c1e3ccSAndroid Build Coastguard Worker #endif
1352*77c1e3ccSAndroid Build Coastguard Worker }
1353*77c1e3ccSAndroid Build Coastguard Worker #endif
1354*77c1e3ccSAndroid Build Coastguard Worker
1355*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi)) {
1356*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning): fwd key frame always uses show existing frame?
1357*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE &&
1358*77c1e3ccSAndroid Build Coastguard Worker gf_group->refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
1359*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_existing_frame = 1;
1360*77c1e3ccSAndroid Build Coastguard Worker } else {
1361*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_existing_frame =
1362*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->show_existing_alt_ref &&
1363*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) ||
1364*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE;
1365*77c1e3ccSAndroid Build Coastguard Worker }
1366*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags);
1367*77c1e3ccSAndroid Build Coastguard Worker
1368*77c1e3ccSAndroid Build Coastguard Worker // Special handling to reset 'show_existing_frame' in case of dropped
1369*77c1e3ccSAndroid Build Coastguard Worker // frames.
1370*77c1e3ccSAndroid Build Coastguard Worker if (oxcf->rc_cfg.drop_frames_water_mark &&
1371*77c1e3ccSAndroid Build Coastguard Worker (gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE ||
1372*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE)) {
1373*77c1e3ccSAndroid Build Coastguard Worker // During the encode of an OVERLAY_UPDATE/INTNL_OVERLAY_UPDATE frame, loop
1374*77c1e3ccSAndroid Build Coastguard Worker // over the gf group to check if the corresponding
1375*77c1e3ccSAndroid Build Coastguard Worker // ARF_UPDATE/INTNL_ARF_UPDATE frame was dropped.
1376*77c1e3ccSAndroid Build Coastguard Worker int cur_disp_idx = gf_group->display_idx[cpi->gf_frame_index];
1377*77c1e3ccSAndroid Build Coastguard Worker for (int idx = 0; idx < cpi->gf_frame_index; idx++) {
1378*77c1e3ccSAndroid Build Coastguard Worker if (cur_disp_idx == gf_group->display_idx[idx]) {
1379*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(
1380*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE,
1381*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[idx] == ARF_UPDATE));
1382*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(gf_group->update_type[cpi->gf_frame_index] ==
1383*77c1e3ccSAndroid Build Coastguard Worker INTNL_OVERLAY_UPDATE,
1384*77c1e3ccSAndroid Build Coastguard Worker gf_group->update_type[idx] == INTNL_ARF_UPDATE));
1385*77c1e3ccSAndroid Build Coastguard Worker // Reset show_existing_frame and set cpi->is_dropped_frame to true if
1386*77c1e3ccSAndroid Build Coastguard Worker // the frame was dropped during its first encode.
1387*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->is_frame_dropped[idx]) {
1388*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_existing_frame = 0;
1389*77c1e3ccSAndroid Build Coastguard Worker assert(!cpi->is_dropped_frame);
1390*77c1e3ccSAndroid Build Coastguard Worker cpi->is_dropped_frame = true;
1391*77c1e3ccSAndroid Build Coastguard Worker }
1392*77c1e3ccSAndroid Build Coastguard Worker break;
1393*77c1e3ccSAndroid Build Coastguard Worker }
1394*77c1e3ccSAndroid Build Coastguard Worker }
1395*77c1e3ccSAndroid Build Coastguard Worker }
1396*77c1e3ccSAndroid Build Coastguard Worker
1397*77c1e3ccSAndroid Build Coastguard Worker // Reset show_existing_alt_ref decision to 0 after it is used.
1398*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) {
1399*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->show_existing_alt_ref = 0;
1400*77c1e3ccSAndroid Build Coastguard Worker }
1401*77c1e3ccSAndroid Build Coastguard Worker } else {
1402*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_existing_frame = 0;
1403*77c1e3ccSAndroid Build Coastguard Worker }
1404*77c1e3ccSAndroid Build Coastguard Worker
1405*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *source = NULL;
1406*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *last_source = NULL;
1407*77c1e3ccSAndroid Build Coastguard Worker if (frame_params.show_existing_frame) {
1408*77c1e3ccSAndroid Build Coastguard Worker source = av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage);
1409*77c1e3ccSAndroid Build Coastguard Worker *pop_lookahead = 1;
1410*77c1e3ccSAndroid Build Coastguard Worker frame_params.show_frame = 1;
1411*77c1e3ccSAndroid Build Coastguard Worker } else {
1412*77c1e3ccSAndroid Build Coastguard Worker source = choose_frame_source(cpi, &flush, pop_lookahead, &last_source,
1413*77c1e3ccSAndroid Build Coastguard Worker &frame_params.show_frame);
1414*77c1e3ccSAndroid Build Coastguard Worker }
1415*77c1e3ccSAndroid Build Coastguard Worker
1416*77c1e3ccSAndroid Build Coastguard Worker if (source == NULL) { // If no source was found, we can't encode a frame.
1417*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1418*77c1e3ccSAndroid Build Coastguard Worker if (flush && oxcf->pass == AOM_RC_FIRST_PASS &&
1419*77c1e3ccSAndroid Build Coastguard Worker !cpi->ppi->twopass.first_pass_done) {
1420*77c1e3ccSAndroid Build Coastguard Worker av1_end_first_pass(cpi); /* get last stats packet */
1421*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->twopass.first_pass_done = 1;
1422*77c1e3ccSAndroid Build Coastguard Worker }
1423*77c1e3ccSAndroid Build Coastguard Worker #endif
1424*77c1e3ccSAndroid Build Coastguard Worker return -1;
1425*77c1e3ccSAndroid Build Coastguard Worker }
1426*77c1e3ccSAndroid Build Coastguard Worker
1427*77c1e3ccSAndroid Build Coastguard Worker // reset src_offset to allow actual encode call for this frame to get its
1428*77c1e3ccSAndroid Build Coastguard Worker // source.
1429*77c1e3ccSAndroid Build Coastguard Worker gf_group->src_offset[cpi->gf_frame_index] = 0;
1430*77c1e3ccSAndroid Build Coastguard Worker
1431*77c1e3ccSAndroid Build Coastguard Worker // Source may be changed if temporal filtered later.
1432*77c1e3ccSAndroid Build Coastguard Worker frame_input.source = &source->img;
1433*77c1e3ccSAndroid Build Coastguard Worker if ((cpi->ppi->use_svc || cpi->rc.prev_frame_is_dropped) &&
1434*77c1e3ccSAndroid Build Coastguard Worker last_source != NULL)
1435*77c1e3ccSAndroid Build Coastguard Worker av1_svc_set_last_source(cpi, &frame_input, &last_source->img);
1436*77c1e3ccSAndroid Build Coastguard Worker else
1437*77c1e3ccSAndroid Build Coastguard Worker frame_input.last_source = last_source != NULL ? &last_source->img : NULL;
1438*77c1e3ccSAndroid Build Coastguard Worker frame_input.ts_duration = source->ts_end - source->ts_start;
1439*77c1e3ccSAndroid Build Coastguard Worker // Save unfiltered source. It is used in av1_get_second_pass_params().
1440*77c1e3ccSAndroid Build Coastguard Worker cpi->unfiltered_source = frame_input.source;
1441*77c1e3ccSAndroid Build Coastguard Worker
1442*77c1e3ccSAndroid Build Coastguard Worker *time_stamp = source->ts_start;
1443*77c1e3ccSAndroid Build Coastguard Worker *time_end = source->ts_end;
1444*77c1e3ccSAndroid Build Coastguard Worker if (source->ts_start < cpi->time_stamps.first_ts_start) {
1445*77c1e3ccSAndroid Build Coastguard Worker cpi->time_stamps.first_ts_start = source->ts_start;
1446*77c1e3ccSAndroid Build Coastguard Worker cpi->time_stamps.prev_ts_end = source->ts_start;
1447*77c1e3ccSAndroid Build Coastguard Worker }
1448*77c1e3ccSAndroid Build Coastguard Worker
1449*77c1e3ccSAndroid Build Coastguard Worker av1_apply_encoding_flags(cpi, source->flags);
1450*77c1e3ccSAndroid Build Coastguard Worker *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
1451*77c1e3ccSAndroid Build Coastguard Worker
1452*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1453*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
1454*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
1455*77c1e3ccSAndroid Build Coastguard Worker cpi->framerate = cpi->temp_framerate;
1456*77c1e3ccSAndroid Build Coastguard Worker }
1457*77c1e3ccSAndroid Build Coastguard Worker }
1458*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
1459*77c1e3ccSAndroid Build Coastguard Worker
1460*77c1e3ccSAndroid Build Coastguard Worker // Shown frames and arf-overlay frames need frame-rate considering
1461*77c1e3ccSAndroid Build Coastguard Worker if (frame_params.show_frame)
1462*77c1e3ccSAndroid Build Coastguard Worker adjust_frame_rate(cpi, source->ts_start, source->ts_end);
1463*77c1e3ccSAndroid Build Coastguard Worker
1464*77c1e3ccSAndroid Build Coastguard Worker if (!frame_params.show_existing_frame) {
1465*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1466*77c1e3ccSAndroid Build Coastguard Worker if (cpi->film_grain_table) {
1467*77c1e3ccSAndroid Build Coastguard Worker cm->cur_frame->film_grain_params_present = aom_film_grain_table_lookup(
1468*77c1e3ccSAndroid Build Coastguard Worker cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
1469*77c1e3ccSAndroid Build Coastguard Worker &cm->film_grain_params);
1470*77c1e3ccSAndroid Build Coastguard Worker } else {
1471*77c1e3ccSAndroid Build Coastguard Worker cm->cur_frame->film_grain_params_present =
1472*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->film_grain_params_present;
1473*77c1e3ccSAndroid Build Coastguard Worker }
1474*77c1e3ccSAndroid Build Coastguard Worker #endif
1475*77c1e3ccSAndroid Build Coastguard Worker // only one operating point supported now
1476*77c1e3ccSAndroid Build Coastguard Worker const int64_t pts64 = ticks_to_timebase_units(timestamp_ratio, *time_stamp);
1477*77c1e3ccSAndroid Build Coastguard Worker if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
1478*77c1e3ccSAndroid Build Coastguard Worker
1479*77c1e3ccSAndroid Build Coastguard Worker cm->frame_presentation_time = (uint32_t)pts64;
1480*77c1e3ccSAndroid Build Coastguard Worker }
1481*77c1e3ccSAndroid Build Coastguard Worker
1482*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1483*77c1e3ccSAndroid Build Coastguard Worker start_timing(cpi, av1_get_one_pass_rt_params_time);
1484*77c1e3ccSAndroid Build Coastguard Worker #endif
1485*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
1486*77c1e3ccSAndroid Build Coastguard Worker av1_get_one_pass_rt_params(cpi, &frame_params.frame_type, &frame_input,
1487*77c1e3ccSAndroid Build Coastguard Worker *frame_flags);
1488*77c1e3ccSAndroid Build Coastguard Worker if (use_rtc_reference_structure_one_layer(cpi))
1489*77c1e3ccSAndroid Build Coastguard Worker av1_set_rtc_reference_structure_one_layer(cpi, cpi->gf_frame_index == 0);
1490*77c1e3ccSAndroid Build Coastguard Worker #else
1491*77c1e3ccSAndroid Build Coastguard Worker if (use_one_pass_rt_params) {
1492*77c1e3ccSAndroid Build Coastguard Worker av1_get_one_pass_rt_params(cpi, &frame_params.frame_type, &frame_input,
1493*77c1e3ccSAndroid Build Coastguard Worker *frame_flags);
1494*77c1e3ccSAndroid Build Coastguard Worker if (use_rtc_reference_structure_one_layer(cpi))
1495*77c1e3ccSAndroid Build Coastguard Worker av1_set_rtc_reference_structure_one_layer(cpi, cpi->gf_frame_index == 0);
1496*77c1e3ccSAndroid Build Coastguard Worker }
1497*77c1e3ccSAndroid Build Coastguard Worker #endif
1498*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1499*77c1e3ccSAndroid Build Coastguard Worker end_timing(cpi, av1_get_one_pass_rt_params_time);
1500*77c1e3ccSAndroid Build Coastguard Worker #endif
1501*77c1e3ccSAndroid Build Coastguard Worker
1502*77c1e3ccSAndroid Build Coastguard Worker FRAME_UPDATE_TYPE frame_update_type =
1503*77c1e3ccSAndroid Build Coastguard Worker get_frame_update_type(gf_group, cpi->gf_frame_index);
1504*77c1e3ccSAndroid Build Coastguard Worker
1505*77c1e3ccSAndroid Build Coastguard Worker if (frame_params.show_existing_frame &&
1506*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type != KEY_FRAME) {
1507*77c1e3ccSAndroid Build Coastguard Worker // Force show-existing frames to be INTER, except forward keyframes
1508*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type = INTER_FRAME;
1509*77c1e3ccSAndroid Build Coastguard Worker }
1510*77c1e3ccSAndroid Build Coastguard Worker
1511*77c1e3ccSAndroid Build Coastguard Worker // Per-frame encode speed. In theory this can vary, but things may have
1512*77c1e3ccSAndroid Build Coastguard Worker // been written assuming speed-level will not change within a sequence, so
1513*77c1e3ccSAndroid Build Coastguard Worker // this parameter should be used with caution.
1514*77c1e3ccSAndroid Build Coastguard Worker frame_params.speed = oxcf->speed;
1515*77c1e3ccSAndroid Build Coastguard Worker
1516*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1517*77c1e3ccSAndroid Build Coastguard Worker // Set forced key frames when necessary. For two-pass encoding / lap mode,
1518*77c1e3ccSAndroid Build Coastguard Worker // this is already handled by av1_get_second_pass_params. However when no
1519*77c1e3ccSAndroid Build Coastguard Worker // stats are available, we still need to check if the new frame is a keyframe.
1520*77c1e3ccSAndroid Build Coastguard Worker // For one pass rt, this is already checked in av1_get_one_pass_rt_params.
1521*77c1e3ccSAndroid Build Coastguard Worker if (!use_one_pass_rt_params &&
1522*77c1e3ccSAndroid Build Coastguard Worker (is_stat_generation_stage(cpi) || has_no_stats_stage(cpi))) {
1523*77c1e3ccSAndroid Build Coastguard Worker // Current frame is coded as a key-frame for any of the following cases:
1524*77c1e3ccSAndroid Build Coastguard Worker // 1) First frame of a video
1525*77c1e3ccSAndroid Build Coastguard Worker // 2) For all-intra frame encoding
1526*77c1e3ccSAndroid Build Coastguard Worker // 3) When a key-frame is forced
1527*77c1e3ccSAndroid Build Coastguard Worker const int kf_requested =
1528*77c1e3ccSAndroid Build Coastguard Worker (cm->current_frame.frame_number == 0 ||
1529*77c1e3ccSAndroid Build Coastguard Worker oxcf->kf_cfg.key_freq_max == 0 || (*frame_flags & FRAMEFLAGS_KEY));
1530*77c1e3ccSAndroid Build Coastguard Worker if (kf_requested && frame_update_type != OVERLAY_UPDATE &&
1531*77c1e3ccSAndroid Build Coastguard Worker frame_update_type != INTNL_OVERLAY_UPDATE) {
1532*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type = KEY_FRAME;
1533*77c1e3ccSAndroid Build Coastguard Worker } else if (is_stat_generation_stage(cpi)) {
1534*77c1e3ccSAndroid Build Coastguard Worker // For stats generation, set the frame type to inter here.
1535*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type = INTER_FRAME;
1536*77c1e3ccSAndroid Build Coastguard Worker }
1537*77c1e3ccSAndroid Build Coastguard Worker }
1538*77c1e3ccSAndroid Build Coastguard Worker #endif
1539*77c1e3ccSAndroid Build Coastguard Worker
1540*77c1e3ccSAndroid Build Coastguard Worker // Work out some encoding parameters specific to the pass:
1541*77c1e3ccSAndroid Build Coastguard Worker if (has_no_stats_stage(cpi) && oxcf->q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
1542*77c1e3ccSAndroid Build Coastguard Worker av1_cyclic_refresh_update_parameters(cpi);
1543*77c1e3ccSAndroid Build Coastguard Worker } else if (is_stat_generation_stage(cpi)) {
1544*77c1e3ccSAndroid Build Coastguard Worker cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&oxcf->rc_cfg);
1545*77c1e3ccSAndroid Build Coastguard Worker } else if (is_stat_consumption_stage(cpi)) {
1546*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MISMATCH_DEBUG
1547*77c1e3ccSAndroid Build Coastguard Worker mismatch_move_frame_idx_w();
1548*77c1e3ccSAndroid Build Coastguard Worker #endif
1549*77c1e3ccSAndroid Build Coastguard Worker #if TXCOEFF_COST_TIMER
1550*77c1e3ccSAndroid Build Coastguard Worker cm->txcoeff_cost_timer = 0;
1551*77c1e3ccSAndroid Build Coastguard Worker cm->txcoeff_cost_count = 0;
1552*77c1e3ccSAndroid Build Coastguard Worker #endif
1553*77c1e3ccSAndroid Build Coastguard Worker }
1554*77c1e3ccSAndroid Build Coastguard Worker
1555*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi))
1556*77c1e3ccSAndroid Build Coastguard Worker set_ext_overrides(cm, &frame_params, ext_flags);
1557*77c1e3ccSAndroid Build Coastguard Worker
1558*77c1e3ccSAndroid Build Coastguard Worker // Shown keyframes and S frames refresh all reference buffers
1559*77c1e3ccSAndroid Build Coastguard Worker const int force_refresh_all =
1560*77c1e3ccSAndroid Build Coastguard Worker ((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) ||
1561*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type == S_FRAME) &&
1562*77c1e3ccSAndroid Build Coastguard Worker !frame_params.show_existing_frame;
1563*77c1e3ccSAndroid Build Coastguard Worker
1564*77c1e3ccSAndroid Build Coastguard Worker av1_configure_buffer_updates(
1565*77c1e3ccSAndroid Build Coastguard Worker cpi, &frame_params.refresh_frame, frame_update_type,
1566*77c1e3ccSAndroid Build Coastguard Worker gf_group->refbuf_state[cpi->gf_frame_index], force_refresh_all);
1567*77c1e3ccSAndroid Build Coastguard Worker
1568*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi)) {
1569*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *ref_frame_buf[INTER_REFS_PER_FRAME];
1570*77c1e3ccSAndroid Build Coastguard Worker
1571*77c1e3ccSAndroid Build Coastguard Worker RefFrameMapPair ref_frame_map_pairs[REF_FRAMES];
1572*77c1e3ccSAndroid Build Coastguard Worker init_ref_map_pair(cpi, ref_frame_map_pairs);
1573*77c1e3ccSAndroid Build Coastguard Worker const int order_offset = gf_group->arf_src_offset[cpi->gf_frame_index];
1574*77c1e3ccSAndroid Build Coastguard Worker const int cur_frame_disp =
1575*77c1e3ccSAndroid Build Coastguard Worker cpi->common.current_frame.frame_number + order_offset;
1576*77c1e3ccSAndroid Build Coastguard Worker
1577*77c1e3ccSAndroid Build Coastguard Worker int get_ref_frames = 0;
1578*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1579*77c1e3ccSAndroid Build Coastguard Worker get_ref_frames =
1580*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 1 : 0;
1581*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_FPMT_TEST
1582*77c1e3ccSAndroid Build Coastguard Worker if (get_ref_frames ||
1583*77c1e3ccSAndroid Build Coastguard Worker gf_group->frame_parallel_level[cpi->gf_frame_index] == 0) {
1584*77c1e3ccSAndroid Build Coastguard Worker if (!ext_flags->refresh_frame.update_pending) {
1585*77c1e3ccSAndroid Build Coastguard Worker av1_get_ref_frames(ref_frame_map_pairs, cur_frame_disp, cpi,
1586*77c1e3ccSAndroid Build Coastguard Worker cpi->gf_frame_index, 1, cm->remapped_ref_idx);
1587*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->ppi->rtc_ref.set_ref_frame_config ||
1588*77c1e3ccSAndroid Build Coastguard Worker use_rtc_reference_structure_one_layer(cpi)) {
1589*77c1e3ccSAndroid Build Coastguard Worker for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++)
1590*77c1e3ccSAndroid Build Coastguard Worker cm->remapped_ref_idx[i] = cpi->ppi->rtc_ref.ref_idx[i];
1591*77c1e3ccSAndroid Build Coastguard Worker }
1592*77c1e3ccSAndroid Build Coastguard Worker }
1593*77c1e3ccSAndroid Build Coastguard Worker
1594*77c1e3ccSAndroid Build Coastguard Worker // Get the reference frames
1595*77c1e3ccSAndroid Build Coastguard Worker bool has_ref_frames = false;
1596*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1597*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *ref_frame =
1598*77c1e3ccSAndroid Build Coastguard Worker get_ref_frame_buf(cm, ref_frame_priority_order[i]);
1599*77c1e3ccSAndroid Build Coastguard Worker ref_frame_buf[i] = ref_frame != NULL ? &ref_frame->buf : NULL;
1600*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame != NULL) has_ref_frames = true;
1601*77c1e3ccSAndroid Build Coastguard Worker }
1602*77c1e3ccSAndroid Build Coastguard Worker if (!has_ref_frames && (frame_params.frame_type == INTER_FRAME ||
1603*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type == S_FRAME)) {
1604*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
1605*77c1e3ccSAndroid Build Coastguard Worker }
1606*77c1e3ccSAndroid Build Coastguard Worker
1607*77c1e3ccSAndroid Build Coastguard Worker // Work out which reference frame slots may be used.
1608*77c1e3ccSAndroid Build Coastguard Worker frame_params.ref_frame_flags =
1609*77c1e3ccSAndroid Build Coastguard Worker get_ref_frame_flags(&cpi->sf, is_one_pass_rt_params(cpi), ref_frame_buf,
1610*77c1e3ccSAndroid Build Coastguard Worker ext_flags->ref_frame_flags);
1611*77c1e3ccSAndroid Build Coastguard Worker
1612*77c1e3ccSAndroid Build Coastguard Worker // Set primary_ref_frame of non-reference frames as PRIMARY_REF_NONE.
1613*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->gf_group.is_frame_non_ref[cpi->gf_frame_index]) {
1614*77c1e3ccSAndroid Build Coastguard Worker frame_params.primary_ref_frame = PRIMARY_REF_NONE;
1615*77c1e3ccSAndroid Build Coastguard Worker } else {
1616*77c1e3ccSAndroid Build Coastguard Worker frame_params.primary_ref_frame =
1617*77c1e3ccSAndroid Build Coastguard Worker choose_primary_ref_frame(cpi, &frame_params);
1618*77c1e3ccSAndroid Build Coastguard Worker }
1619*77c1e3ccSAndroid Build Coastguard Worker
1620*77c1e3ccSAndroid Build Coastguard Worker frame_params.order_offset = gf_group->arf_src_offset[cpi->gf_frame_index];
1621*77c1e3ccSAndroid Build Coastguard Worker
1622*77c1e3ccSAndroid Build Coastguard Worker // Call av1_get_refresh_frame_flags() if refresh index not available.
1623*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->refresh_idx_available) {
1624*77c1e3ccSAndroid Build Coastguard Worker frame_params.refresh_frame_flags = av1_get_refresh_frame_flags(
1625*77c1e3ccSAndroid Build Coastguard Worker cpi, &frame_params, frame_update_type, cpi->gf_frame_index,
1626*77c1e3ccSAndroid Build Coastguard Worker cur_frame_disp, ref_frame_map_pairs);
1627*77c1e3ccSAndroid Build Coastguard Worker } else {
1628*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->ref_refresh_index != INVALID_IDX);
1629*77c1e3ccSAndroid Build Coastguard Worker frame_params.refresh_frame_flags = (1 << cpi->ref_refresh_index);
1630*77c1e3ccSAndroid Build Coastguard Worker }
1631*77c1e3ccSAndroid Build Coastguard Worker
1632*77c1e3ccSAndroid Build Coastguard Worker // Make the frames marked as is_frame_non_ref to non-reference frames.
1633*77c1e3ccSAndroid Build Coastguard Worker if (gf_group->is_frame_non_ref[cpi->gf_frame_index])
1634*77c1e3ccSAndroid Build Coastguard Worker frame_params.refresh_frame_flags = 0;
1635*77c1e3ccSAndroid Build Coastguard Worker
1636*77c1e3ccSAndroid Build Coastguard Worker frame_params.existing_fb_idx_to_show = INVALID_IDX;
1637*77c1e3ccSAndroid Build Coastguard Worker // Find the frame buffer to show based on display order.
1638*77c1e3ccSAndroid Build Coastguard Worker if (frame_params.show_existing_frame) {
1639*77c1e3ccSAndroid Build Coastguard Worker for (int frame = 0; frame < REF_FRAMES; frame++) {
1640*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *const buf = cm->ref_frame_map[frame];
1641*77c1e3ccSAndroid Build Coastguard Worker if (buf == NULL) continue;
1642*77c1e3ccSAndroid Build Coastguard Worker const int frame_order = (int)buf->display_order_hint;
1643*77c1e3ccSAndroid Build Coastguard Worker if (frame_order == cur_frame_disp)
1644*77c1e3ccSAndroid Build Coastguard Worker frame_params.existing_fb_idx_to_show = frame;
1645*77c1e3ccSAndroid Build Coastguard Worker }
1646*77c1e3ccSAndroid Build Coastguard Worker }
1647*77c1e3ccSAndroid Build Coastguard Worker }
1648*77c1e3ccSAndroid Build Coastguard Worker
1649*77c1e3ccSAndroid Build Coastguard Worker // The way frame_params->remapped_ref_idx is setup is a placeholder.
1650*77c1e3ccSAndroid Build Coastguard Worker // Currently, reference buffer assignment is done by update_ref_frame_map()
1651*77c1e3ccSAndroid Build Coastguard Worker // which is called by high-level strategy AFTER encoding a frame. It
1652*77c1e3ccSAndroid Build Coastguard Worker // modifies cm->remapped_ref_idx. If you want to use an alternative method
1653*77c1e3ccSAndroid Build Coastguard Worker // to determine reference buffer assignment, just put your assignments into
1654*77c1e3ccSAndroid Build Coastguard Worker // frame_params->remapped_ref_idx here and they will be used when encoding
1655*77c1e3ccSAndroid Build Coastguard Worker // this frame. If frame_params->remapped_ref_idx is setup independently of
1656*77c1e3ccSAndroid Build Coastguard Worker // cm->remapped_ref_idx then update_ref_frame_map() will have no effect.
1657*77c1e3ccSAndroid Build Coastguard Worker memcpy(frame_params.remapped_ref_idx, cm->remapped_ref_idx,
1658*77c1e3ccSAndroid Build Coastguard Worker REF_FRAMES * sizeof(*cm->remapped_ref_idx));
1659*77c1e3ccSAndroid Build Coastguard Worker
1660*77c1e3ccSAndroid Build Coastguard Worker cpi->td.mb.rdmult_delta_qindex = cpi->td.mb.delta_qindex = 0;
1661*77c1e3ccSAndroid Build Coastguard Worker
1662*77c1e3ccSAndroid Build Coastguard Worker if (!frame_params.show_existing_frame) {
1663*77c1e3ccSAndroid Build Coastguard Worker cm->quant_params.using_qmatrix = oxcf->q_cfg.using_qm;
1664*77c1e3ccSAndroid Build Coastguard Worker }
1665*77c1e3ccSAndroid Build Coastguard Worker
1666*77c1e3ccSAndroid Build Coastguard Worker const int is_intra_frame = frame_params.frame_type == KEY_FRAME ||
1667*77c1e3ccSAndroid Build Coastguard Worker frame_params.frame_type == INTRA_ONLY_FRAME;
1668*77c1e3ccSAndroid Build Coastguard Worker FeatureFlags *const features = &cm->features;
1669*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi) &&
1670*77c1e3ccSAndroid Build Coastguard Worker (oxcf->pass == AOM_RC_ONE_PASS || oxcf->pass >= AOM_RC_SECOND_PASS) &&
1671*77c1e3ccSAndroid Build Coastguard Worker is_intra_frame) {
1672*77c1e3ccSAndroid Build Coastguard Worker av1_set_screen_content_options(cpi, features);
1673*77c1e3ccSAndroid Build Coastguard Worker }
1674*77c1e3ccSAndroid Build Coastguard Worker
1675*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
1676*77c1e3ccSAndroid Build Coastguard Worker if (av1_encode(cpi, dest, dest_size, &frame_input, &frame_params,
1677*77c1e3ccSAndroid Build Coastguard Worker &frame_size) != AOM_CODEC_OK) {
1678*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
1679*77c1e3ccSAndroid Build Coastguard Worker }
1680*77c1e3ccSAndroid Build Coastguard Worker #else
1681*77c1e3ccSAndroid Build Coastguard Worker if (has_no_stats_stage(cpi) && oxcf->mode == REALTIME &&
1682*77c1e3ccSAndroid Build Coastguard Worker gf_cfg->lag_in_frames == 0) {
1683*77c1e3ccSAndroid Build Coastguard Worker if (av1_encode(cpi, dest, dest_size, &frame_input, &frame_params,
1684*77c1e3ccSAndroid Build Coastguard Worker &frame_size) != AOM_CODEC_OK) {
1685*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
1686*77c1e3ccSAndroid Build Coastguard Worker }
1687*77c1e3ccSAndroid Build Coastguard Worker } else if (denoise_and_encode(cpi, dest, dest_size, &frame_input,
1688*77c1e3ccSAndroid Build Coastguard Worker &frame_params, &frame_size) != AOM_CODEC_OK) {
1689*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
1690*77c1e3ccSAndroid Build Coastguard Worker }
1691*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_REALTIME_ONLY
1692*77c1e3ccSAndroid Build Coastguard Worker
1693*77c1e3ccSAndroid Build Coastguard Worker // This is used in rtc temporal filter case. Use true source in the PSNR
1694*77c1e3ccSAndroid Build Coastguard Worker // calculation.
1695*77c1e3ccSAndroid Build Coastguard Worker if (is_psnr_calc_enabled(cpi) && cpi->sf.rt_sf.use_rtc_tf) {
1696*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->orig_source.buffer_alloc_sz > 0);
1697*77c1e3ccSAndroid Build Coastguard Worker cpi->source = &cpi->orig_source;
1698*77c1e3ccSAndroid Build Coastguard Worker }
1699*77c1e3ccSAndroid Build Coastguard Worker
1700*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi)) {
1701*77c1e3ccSAndroid Build Coastguard Worker // First pass doesn't modify reference buffer assignment or produce frame
1702*77c1e3ccSAndroid Build Coastguard Worker // flags
1703*77c1e3ccSAndroid Build Coastguard Worker update_frame_flags(&cpi->common, &cpi->refresh_frame, frame_flags);
1704*77c1e3ccSAndroid Build Coastguard Worker set_additional_frame_flags(cm, frame_flags);
1705*77c1e3ccSAndroid Build Coastguard Worker }
1706*77c1e3ccSAndroid Build Coastguard Worker
1707*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1708*77c1e3ccSAndroid Build Coastguard Worker #if TXCOEFF_COST_TIMER
1709*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi)) {
1710*77c1e3ccSAndroid Build Coastguard Worker cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
1711*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
1712*77c1e3ccSAndroid Build Coastguard Worker "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
1713*77c1e3ccSAndroid Build Coastguard Worker "in us\n",
1714*77c1e3ccSAndroid Build Coastguard Worker cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
1715*77c1e3ccSAndroid Build Coastguard Worker cm->cum_txcoeff_cost_timer);
1716*77c1e3ccSAndroid Build Coastguard Worker }
1717*77c1e3ccSAndroid Build Coastguard Worker #endif
1718*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
1719*77c1e3ccSAndroid Build Coastguard Worker
1720*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
1721*77c1e3ccSAndroid Build Coastguard Worker if (!is_stat_generation_stage(cpi) &&
1722*77c1e3ccSAndroid Build Coastguard Worker (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
1723*77c1e3ccSAndroid Build Coastguard Worker oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN)) {
1724*77c1e3ccSAndroid Build Coastguard Worker av1_update_vmaf_curve(cpi);
1725*77c1e3ccSAndroid Build Coastguard Worker }
1726*77c1e3ccSAndroid Build Coastguard Worker #endif
1727*77c1e3ccSAndroid Build Coastguard Worker
1728*77c1e3ccSAndroid Build Coastguard Worker *size = frame_size;
1729*77c1e3ccSAndroid Build Coastguard Worker
1730*77c1e3ccSAndroid Build Coastguard Worker // Leave a signal for a higher level caller about if this frame is droppable
1731*77c1e3ccSAndroid Build Coastguard Worker if (*size > 0) {
1732*77c1e3ccSAndroid Build Coastguard Worker cpi->droppable =
1733*77c1e3ccSAndroid Build Coastguard Worker is_frame_droppable(&cpi->ppi->rtc_ref, &ext_flags->refresh_frame);
1734*77c1e3ccSAndroid Build Coastguard Worker }
1735*77c1e3ccSAndroid Build Coastguard Worker
1736*77c1e3ccSAndroid Build Coastguard Worker // For SVC, or when frame-dropper is enabled:
1737*77c1e3ccSAndroid Build Coastguard Worker // keep track of the (unscaled) source corresponding to the refresh of LAST
1738*77c1e3ccSAndroid Build Coastguard Worker // reference (base temporal layer - TL0). Copy only for the
1739*77c1e3ccSAndroid Build Coastguard Worker // top spatial enhancement layer so all spatial layers of the next
1740*77c1e3ccSAndroid Build Coastguard Worker // superframe have last_source to be aligned with previous TL0 superframe.
1741*77c1e3ccSAndroid Build Coastguard Worker // Avoid cases where resolution changes for unscaled source (top spatial
1742*77c1e3ccSAndroid Build Coastguard Worker // layer). Only needs to be done for frame that are encoded (size > 0).
1743*77c1e3ccSAndroid Build Coastguard Worker if (*size > 0 &&
1744*77c1e3ccSAndroid Build Coastguard Worker (cpi->ppi->use_svc || cpi->oxcf.rc_cfg.drop_frames_water_mark > 0) &&
1745*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1 &&
1746*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.temporal_layer_id == 0 &&
1747*77c1e3ccSAndroid Build Coastguard Worker cpi->unscaled_source->y_width == cpi->svc.source_last_TL0.y_width &&
1748*77c1e3ccSAndroid Build Coastguard Worker cpi->unscaled_source->y_height == cpi->svc.source_last_TL0.y_height) {
1749*77c1e3ccSAndroid Build Coastguard Worker aom_yv12_copy_y(cpi->unscaled_source, &cpi->svc.source_last_TL0, 1);
1750*77c1e3ccSAndroid Build Coastguard Worker aom_yv12_copy_u(cpi->unscaled_source, &cpi->svc.source_last_TL0, 1);
1751*77c1e3ccSAndroid Build Coastguard Worker aom_yv12_copy_v(cpi->unscaled_source, &cpi->svc.source_last_TL0, 1);
1752*77c1e3ccSAndroid Build Coastguard Worker }
1753*77c1e3ccSAndroid Build Coastguard Worker
1754*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
1755*77c1e3ccSAndroid Build Coastguard Worker }
1756