David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
| 12 | #include <stdint.h> |
| 13 | |
bohanli | 2541b8a | 2020-10-14 17:33:13 -0700 | [diff] [blame] | 14 | #include "av1/common/blockd.h" |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 15 | #include "config/aom_config.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 16 | #include "config/aom_scale_rtcd.h" |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 17 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 18 | #include "aom/aom_codec.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 19 | #include "aom/aom_encoder.h" |
| 20 | |
| 21 | #include "aom_ports/system_state.h" |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 22 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 23 | #if CONFIG_MISMATCH_DEBUG |
| 24 | #include "aom_util/debug_util.h" |
| 25 | #endif // CONFIG_MISMATCH_DEBUG |
| 26 | |
Wan-Teh Chang | f2d15ee | 2020-03-10 09:24:43 -0700 | [diff] [blame] | 27 | #include "av1/common/av1_common_int.h" |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 28 | #include "av1/common/reconinter.h" |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 29 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 30 | #include "av1/encoder/encoder.h" |
| 31 | #include "av1/encoder/encode_strategy.h" |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 32 | #include "av1/encoder/encodeframe.h" |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 33 | #include "av1/encoder/firstpass.h" |
David Turner | 0fa8c49 | 2019-02-06 16:38:13 +0000 | [diff] [blame] | 34 | #include "av1/encoder/pass2_strategy.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 35 | #include "av1/encoder/temporal_filter.h" |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 36 | #include "av1/encoder/tpl_model.h" |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 37 | |
Sai Deng | aff2772 | 2020-08-31 12:06:09 -0700 | [diff] [blame] | 38 | #if CONFIG_TUNE_VMAF |
| 39 | #include "av1/encoder/tune_vmaf.h" |
| 40 | #endif |
| 41 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 42 | #define TEMPORAL_FILTER_KEY_FRAME (CONFIG_REALTIME_ONLY ? 0 : 1) |
| 43 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 44 | static INLINE void set_refresh_frame_flags( |
| 45 | RefreshFrameFlagsInfo *const refresh_frame_flags, bool refresh_gf, |
| 46 | bool refresh_bwdref, bool refresh_arf) { |
| 47 | refresh_frame_flags->golden_frame = refresh_gf; |
| 48 | refresh_frame_flags->bwd_ref_frame = refresh_bwdref; |
| 49 | refresh_frame_flags->alt_ref_frame = refresh_arf; |
| 50 | } |
| 51 | |
| 52 | void av1_configure_buffer_updates( |
| 53 | AV1_COMP *const cpi, RefreshFrameFlagsInfo *const refresh_frame_flags, |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 54 | const FRAME_UPDATE_TYPE type, const REFBUF_STATE refbuf_state, |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 55 | int force_refresh_all) { |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 56 | // NOTE(weitinglin): Should we define another function to take care of |
| 57 | // cpi->rc.is_$Source_Type to make this function as it is in the comment? |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 58 | const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
| 59 | &cpi->ext_flags.refresh_frame; |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 60 | cpi->rc.is_src_frame_alt_ref = 0; |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 61 | |
| 62 | switch (type) { |
| 63 | case KF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 64 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 65 | break; |
| 66 | |
| 67 | case LF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 68 | set_refresh_frame_flags(refresh_frame_flags, false, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 69 | break; |
| 70 | |
| 71 | case GF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 72 | set_refresh_frame_flags(refresh_frame_flags, true, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 73 | break; |
| 74 | |
| 75 | case OVERLAY_UPDATE: |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 76 | if (refbuf_state == REFBUF_RESET) |
bohanli | 2541b8a | 2020-10-14 17:33:13 -0700 | [diff] [blame] | 77 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 78 | else |
bohanli | 2541b8a | 2020-10-14 17:33:13 -0700 | [diff] [blame] | 79 | set_refresh_frame_flags(refresh_frame_flags, true, false, false); |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 80 | |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 81 | cpi->rc.is_src_frame_alt_ref = 1; |
| 82 | break; |
| 83 | |
| 84 | case ARF_UPDATE: |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 85 | // NOTE: BWDREF does not get updated along with ALTREF_FRAME. |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 86 | if (refbuf_state == REFBUF_RESET) |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 87 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 88 | else |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 89 | set_refresh_frame_flags(refresh_frame_flags, false, false, true); |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 90 | |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 93 | case INTNL_OVERLAY_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 94 | set_refresh_frame_flags(refresh_frame_flags, false, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 95 | cpi->rc.is_src_frame_alt_ref = 1; |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case INTNL_ARF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 99 | set_refresh_frame_flags(refresh_frame_flags, false, true, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 100 | break; |
| 101 | |
| 102 | default: assert(0); break; |
| 103 | } |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 104 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 105 | if (ext_refresh_frame_flags->update_pending && |
Jingning Han | a862e20 | 2021-05-14 10:18:50 -0700 | [diff] [blame] | 106 | (!is_stat_generation_stage(cpi))) { |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 107 | set_refresh_frame_flags(refresh_frame_flags, |
| 108 | ext_refresh_frame_flags->golden_frame, |
| 109 | ext_refresh_frame_flags->bwd_ref_frame, |
| 110 | ext_refresh_frame_flags->alt_ref_frame); |
Jingning Han | a862e20 | 2021-05-14 10:18:50 -0700 | [diff] [blame] | 111 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
| 112 | if (ext_refresh_frame_flags->golden_frame) |
| 113 | gf_group->update_type[cpi->gf_frame_index] = GF_UPDATE; |
| 114 | if (ext_refresh_frame_flags->alt_ref_frame) |
| 115 | gf_group->update_type[cpi->gf_frame_index] = ARF_UPDATE; |
| 116 | if (ext_refresh_frame_flags->bwd_ref_frame) |
| 117 | gf_group->update_type[cpi->gf_frame_index] = INTNL_ARF_UPDATE; |
| 118 | } |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 119 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 120 | if (force_refresh_all) |
| 121 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 122 | } |
| 123 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 124 | static void set_additional_frame_flags(const AV1_COMMON *const cm, |
| 125 | unsigned int *const frame_flags) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 126 | if (frame_is_intra_only(cm)) { |
| 127 | *frame_flags |= FRAMEFLAGS_INTRAONLY; |
| 128 | } |
| 129 | if (frame_is_sframe(cm)) { |
| 130 | *frame_flags |= FRAMEFLAGS_SWITCH; |
| 131 | } |
| 132 | if (cm->features.error_resilient_mode) { |
| 133 | *frame_flags |= FRAMEFLAGS_ERROR_RESILIENT; |
| 134 | } |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 137 | static void set_ext_overrides(AV1_COMMON *const cm, |
| 138 | EncodeFrameParams *const frame_params, |
| 139 | ExternalFlags *const ext_flags) { |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 140 | // Overrides the defaults with the externally supplied values with |
| 141 | // av1_update_reference() and av1_update_entropy() calls |
| 142 | // Note: The overrides are valid only for the next frame passed |
| 143 | // to av1_encode_lowlevel() |
| 144 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 145 | if (ext_flags->use_s_frame) { |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 146 | frame_params->frame_type = S_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 147 | } |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 148 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 149 | if (ext_flags->refresh_frame_context_pending) { |
| 150 | cm->features.refresh_frame_context = ext_flags->refresh_frame_context; |
| 151 | ext_flags->refresh_frame_context_pending = 0; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 152 | } |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 153 | cm->features.allow_ref_frame_mvs = ext_flags->use_ref_frame_mvs; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 154 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 155 | frame_params->error_resilient_mode = ext_flags->use_error_resilient; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 156 | // A keyframe is already error resilient and keyframes with |
| 157 | // error_resilient_mode interferes with the use of show_existing_frame |
| 158 | // when forward reference keyframes are enabled. |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 159 | frame_params->error_resilient_mode &= frame_params->frame_type != KEY_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 160 | // For bitstream conformance, s-frames must be error-resilient |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 161 | frame_params->error_resilient_mode |= frame_params->frame_type == S_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 164 | static int choose_primary_ref_frame( |
| 165 | const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) { |
| 166 | const AV1_COMMON *const cm = &cpi->common; |
| 167 | |
| 168 | const int intra_only = frame_params->frame_type == KEY_FRAME || |
| 169 | frame_params->frame_type == INTRA_ONLY_FRAME; |
Marco Paniconi | ee96834 | 2020-06-08 11:21:48 -0700 | [diff] [blame] | 170 | if (intra_only || frame_params->error_resilient_mode || |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 171 | cpi->ext_flags.use_primary_ref_none) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 172 | return PRIMARY_REF_NONE; |
| 173 | } |
| 174 | |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 175 | // In large scale case, always use Last frame's frame contexts. |
| 176 | // Note(yunqing): In other cases, primary_ref_frame is chosen based on |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 177 | // cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index], which also controls |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 178 | // frame bit allocation. |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 179 | if (cm->tiles.large_scale) return (LAST_FRAME - LAST_FRAME); |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 180 | |
Tarundeep Singh | 15eb4de | 2021-04-21 15:53:10 +0530 | [diff] [blame] | 181 | if (cpi->ppi->use_svc) return av1_svc_primary_ref_frame(cpi); |
Marco Paniconi | 42e1bdd | 2020-06-10 16:47:39 -0700 | [diff] [blame] | 182 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 183 | // Find the most recent reference frame with the same reference type as the |
| 184 | // current frame |
Tarundeep Singh | 0734c66 | 2021-06-04 14:52:52 +0530 | [diff] [blame] | 185 | const int current_ref_type = get_current_frame_ref_type(cpi); |
Tarundeep Singh | fd11f82 | 2021-05-27 13:09:06 +0530 | [diff] [blame] | 186 | int wanted_fb = cpi->ppi->fb_of_context_type[current_ref_type]; |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 187 | |
| 188 | int primary_ref_frame = PRIMARY_REF_NONE; |
| 189 | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { |
| 190 | if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) { |
| 191 | primary_ref_frame = ref_frame - LAST_FRAME; |
| 192 | } |
| 193 | } |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 194 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 195 | return primary_ref_frame; |
| 196 | } |
| 197 | |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 198 | static void adjust_frame_rate(AV1_COMP *cpi, int64_t ts_start, int64_t ts_end) { |
| 199 | TimeStamps *time_stamps = &cpi->time_stamps; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 200 | int64_t this_duration; |
| 201 | int step = 0; |
| 202 | |
| 203 | // Clear down mmx registers |
| 204 | aom_clear_system_state(); |
| 205 | |
Tarundeep Singh | 15eb4de | 2021-04-21 15:53:10 +0530 | [diff] [blame] | 206 | if (cpi->ppi->use_svc && cpi->svc.spatial_layer_id > 0) { |
Marco Paniconi | 6397132 | 2019-08-15 21:32:05 -0700 | [diff] [blame] | 207 | cpi->framerate = cpi->svc.base_framerate; |
| 208 | av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height); |
| 209 | return; |
| 210 | } |
| 211 | |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 212 | if (ts_start == time_stamps->first_ts_start) { |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 213 | this_duration = ts_end - ts_start; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 214 | step = 1; |
| 215 | } else { |
| 216 | int64_t last_duration = |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 217 | time_stamps->prev_ts_end - time_stamps->prev_ts_start; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 218 | |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 219 | this_duration = ts_end - time_stamps->prev_ts_end; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 220 | |
| 221 | // do a step update if the duration changes by 10% |
| 222 | if (last_duration) |
| 223 | step = (int)((this_duration - last_duration) * 10 / last_duration); |
| 224 | } |
| 225 | |
| 226 | if (this_duration) { |
| 227 | if (step) { |
| 228 | av1_new_framerate(cpi, 10000000.0 / this_duration); |
| 229 | } else { |
| 230 | // Average this frame's rate into the last second's average |
| 231 | // frame rate. If we haven't seen 1 second yet, then average |
| 232 | // over the whole interval seen. |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 233 | const double interval = |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 234 | AOMMIN((double)(ts_end - time_stamps->first_ts_start), 10000000.0); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 235 | double avg_duration = 10000000.0 / cpi->framerate; |
| 236 | avg_duration *= (interval - avg_duration + this_duration); |
| 237 | avg_duration /= interval; |
| 238 | |
| 239 | av1_new_framerate(cpi, 10000000.0 / avg_duration); |
| 240 | } |
| 241 | } |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 242 | time_stamps->prev_ts_start = ts_start; |
| 243 | time_stamps->prev_ts_end = ts_end; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 246 | // Determine whether there is a forced keyframe pending in the lookahead buffer |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 247 | int is_forced_keyframe_pending(struct lookahead_ctx *lookahead, |
| 248 | const int up_to_index, |
| 249 | const COMPRESSOR_STAGE compressor_stage) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 250 | for (int i = 0; i <= up_to_index; i++) { |
Mufaddal Chakera | 73b7b70 | 2019-12-09 11:44:55 +0530 | [diff] [blame] | 251 | const struct lookahead_entry *e = |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 252 | av1_lookahead_peek(lookahead, i, compressor_stage); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 253 | if (e == NULL) { |
| 254 | // We have reached the end of the lookahead buffer and not early-returned |
| 255 | // so there isn't a forced key-frame pending. |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 256 | return -1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 257 | } else if (e->flags == AOM_EFLAG_FORCE_KF) { |
Aasaipriya | 592cb00 | 2020-12-14 19:24:24 +0530 | [diff] [blame] | 258 | return i; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 259 | } else { |
| 260 | continue; |
| 261 | } |
| 262 | } |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 263 | return -1; // Never reached |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Urvang Joshi | f70375a | 2019-03-22 23:30:19 -0700 | [diff] [blame] | 266 | // Check if we should encode an ARF or internal ARF. If not, try a LAST |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 267 | // Do some setup associated with the chosen source |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 268 | // temporal_filtered, flush, and frame_update_type are outputs. |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 269 | // Return the frame source, or NULL if we couldn't find one |
Yaowu Xu | fac3d86 | 2019-04-26 15:43:03 -0700 | [diff] [blame] | 270 | static struct lookahead_entry *choose_frame_source( |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 271 | AV1_COMP *const cpi, int *const flush, int *pop_lookahead, |
| 272 | struct lookahead_entry **last_source, |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 273 | EncodeFrameParams *const frame_params) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 274 | AV1_COMMON *const cm = &cpi->common; |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 275 | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 276 | struct lookahead_entry *source = NULL; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 277 | |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 278 | // Source index in lookahead buffer. |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 279 | int src_index = gf_group->arf_src_offset[cpi->gf_frame_index]; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 280 | |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 281 | // TODO(Aasaipriya): Forced key frames need to be fixed when rc_mode != AOM_Q |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 282 | if (src_index && |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 283 | (is_forced_keyframe_pending(cpi->ppi->lookahead, src_index, |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 284 | cpi->compressor_stage) != -1) && |
Mufaddal Chakera | 186f8e3 | 2021-03-17 13:20:00 +0530 | [diff] [blame] | 285 | cpi->oxcf.rc_cfg.mode != AOM_Q && !is_stat_generation_stage(cpi)) { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 286 | src_index = 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 287 | *flush = 1; |
| 288 | } |
| 289 | |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 290 | // If the current frame is arf, then we should not pop from the lookahead |
| 291 | // buffer. If the current frame is not arf, then pop it. This assumes the |
| 292 | // first frame in the GF group is not arf. May need to change if it is not |
| 293 | // true. |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 294 | *pop_lookahead = (src_index == 0); |
bohanli | 1478d86 | 2020-06-17 20:53:44 -0700 | [diff] [blame] | 295 | // If this is a key frame and keyframe filtering is enabled with overlay, |
| 296 | // then do not pop. |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 297 | if (*pop_lookahead && cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1 && |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 298 | gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE && |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 299 | !is_stat_generation_stage(cpi) && cpi->ppi->lookahead) { |
| 300 | if (cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz && |
bohanli | 1478d86 | 2020-06-17 20:53:44 -0700 | [diff] [blame] | 301 | (*flush || |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 302 | cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz == |
| 303 | cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].pop_sz)) { |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 304 | *pop_lookahead = 0; |
bohanli | 1478d86 | 2020-06-17 20:53:44 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
Mufaddal Chakera | 186f8e3 | 2021-03-17 13:20:00 +0530 | [diff] [blame] | 307 | |
| 308 | // LAP stage does not have ARFs or forward key-frames, |
| 309 | // hence, always pop_lookahead here. |
| 310 | if (is_stat_generation_stage(cpi)) { |
| 311 | *pop_lookahead = 1; |
Mufaddal Chakera | 97f9271 | 2021-05-21 15:10:33 +0530 | [diff] [blame] | 312 | src_index = 0; |
Mufaddal Chakera | 186f8e3 | 2021-03-17 13:20:00 +0530 | [diff] [blame] | 313 | } |
| 314 | |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 315 | frame_params->show_frame = *pop_lookahead; |
Mufaddal Chakera | 97f9271 | 2021-05-21 15:10:33 +0530 | [diff] [blame] | 316 | |
| 317 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 318 | // Future frame in parallel encode set |
| 319 | if (gf_group->src_offset[cpi->gf_frame_index] != 0 && |
| 320 | !is_stat_generation_stage(cpi) && |
| 321 | 0 /*will be turned on along with frame parallel encode*/) { |
| 322 | src_index = gf_group->src_offset[cpi->gf_frame_index]; |
| 323 | // Don't remove future frames from lookahead_ctx. They will be |
| 324 | // removed in their actual encode call. |
| 325 | *pop_lookahead = 0; |
| 326 | } |
| 327 | #endif |
| 328 | if (frame_params->show_frame) { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 329 | // show frame, pop from buffer |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 330 | // Get last frame source. |
| 331 | if (cm->current_frame.frame_number > 0) { |
Mufaddal Chakera | 97f9271 | 2021-05-21 15:10:33 +0530 | [diff] [blame] | 332 | *last_source = av1_lookahead_peek(cpi->ppi->lookahead, src_index - 1, |
| 333 | cpi->compressor_stage); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 334 | } |
| 335 | // Read in the source frame. |
Mufaddal Chakera | 97f9271 | 2021-05-21 15:10:33 +0530 | [diff] [blame] | 336 | source = av1_lookahead_peek(cpi->ppi->lookahead, src_index, |
| 337 | cpi->compressor_stage); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 338 | } else { |
| 339 | // no show frames are arf frames |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 340 | source = av1_lookahead_peek(cpi->ppi->lookahead, src_index, |
| 341 | cpi->compressor_stage); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 342 | // When src_index == rc->frames_to_key, it indicates a fwd_kf |
bohanli | 6f22c7a | 2020-07-14 10:52:50 -0700 | [diff] [blame] | 343 | if (src_index == cpi->rc.frames_to_key && src_index != 0) { |
| 344 | cpi->no_show_fwd_kf = 1; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 345 | } |
| 346 | if (source != NULL) { |
| 347 | cm->showable_frame = 1; |
| 348 | } |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 349 | } |
| 350 | return source; |
| 351 | } |
| 352 | |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 353 | // Don't allow a show_existing_frame to coincide with an error resilient or |
| 354 | // S-Frame. An exception can be made in the case of a keyframe, since it does |
| 355 | // not depend on any previous frames. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 356 | static int allow_show_existing(const AV1_COMP *const cpi, |
| 357 | unsigned int frame_flags) { |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 358 | if (cpi->common.current_frame.frame_number == 0) return 0; |
| 359 | |
| 360 | const struct lookahead_entry *lookahead_src = |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 361 | av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage); |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 362 | if (lookahead_src == NULL) return 1; |
| 363 | |
| 364 | const int is_error_resilient = |
Vishesh | aa6a170 | 2020-06-30 19:27:22 +0530 | [diff] [blame] | 365 | cpi->oxcf.tool_cfg.error_resilient_mode || |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 366 | (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT); |
Vishesh | 7e9873d | 2020-06-08 15:41:33 +0530 | [diff] [blame] | 367 | const int is_s_frame = cpi->oxcf.kf_cfg.enable_sframe || |
| 368 | (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME); |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 369 | const int is_key_frame = |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 370 | (cpi->rc.frames_to_key == 0) || (frame_flags & FRAMEFLAGS_KEY); |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 371 | return !(is_error_resilient || is_s_frame) || is_key_frame; |
| 372 | } |
| 373 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 374 | // Update frame_flags to tell the encoder's caller what sort of frame was |
| 375 | // encoded. |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 376 | static void update_frame_flags( |
| 377 | const AV1_COMMON *const cm, |
| 378 | const RefreshFrameFlagsInfo *const refresh_frame_flags, |
| 379 | unsigned int *frame_flags) { |
| 380 | if (encode_show_existing_frame(cm)) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 381 | *frame_flags &= ~FRAMEFLAGS_GOLDEN; |
| 382 | *frame_flags &= ~FRAMEFLAGS_BWDREF; |
| 383 | *frame_flags &= ~FRAMEFLAGS_ALTREF; |
| 384 | *frame_flags &= ~FRAMEFLAGS_KEY; |
| 385 | return; |
| 386 | } |
| 387 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 388 | if (refresh_frame_flags->golden_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 389 | *frame_flags |= FRAMEFLAGS_GOLDEN; |
| 390 | } else { |
| 391 | *frame_flags &= ~FRAMEFLAGS_GOLDEN; |
| 392 | } |
| 393 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 394 | if (refresh_frame_flags->alt_ref_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 395 | *frame_flags |= FRAMEFLAGS_ALTREF; |
| 396 | } else { |
| 397 | *frame_flags &= ~FRAMEFLAGS_ALTREF; |
| 398 | } |
| 399 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 400 | if (refresh_frame_flags->bwd_ref_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 401 | *frame_flags |= FRAMEFLAGS_BWDREF; |
| 402 | } else { |
| 403 | *frame_flags &= ~FRAMEFLAGS_BWDREF; |
| 404 | } |
| 405 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 406 | if (cm->current_frame.frame_type == KEY_FRAME) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 407 | *frame_flags |= FRAMEFLAGS_KEY; |
| 408 | } else { |
| 409 | *frame_flags &= ~FRAMEFLAGS_KEY; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | #define DUMP_REF_FRAME_IMAGES 0 |
| 414 | |
| 415 | #if DUMP_REF_FRAME_IMAGES == 1 |
| 416 | static int dump_one_image(AV1_COMMON *cm, |
| 417 | const YV12_BUFFER_CONFIG *const ref_buf, |
| 418 | char *file_name) { |
| 419 | int h; |
| 420 | FILE *f_ref = NULL; |
| 421 | |
| 422 | if (ref_buf == NULL) { |
| 423 | printf("Frame data buffer is NULL.\n"); |
| 424 | return AOM_CODEC_MEM_ERROR; |
| 425 | } |
| 426 | |
| 427 | if ((f_ref = fopen(file_name, "wb")) == NULL) { |
| 428 | printf("Unable to open file %s to write.\n", file_name); |
| 429 | return AOM_CODEC_MEM_ERROR; |
| 430 | } |
| 431 | |
| 432 | // --- Y --- |
| 433 | for (h = 0; h < cm->height; ++h) { |
| 434 | fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref); |
| 435 | } |
| 436 | // --- U --- |
| 437 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 438 | fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1), |
| 439 | f_ref); |
| 440 | } |
| 441 | // --- V --- |
| 442 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 443 | fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1), |
| 444 | f_ref); |
| 445 | } |
| 446 | |
| 447 | fclose(f_ref); |
| 448 | |
| 449 | return AOM_CODEC_OK; |
| 450 | } |
| 451 | |
| 452 | static void dump_ref_frame_images(AV1_COMP *cpi) { |
| 453 | AV1_COMMON *const cm = &cpi->common; |
| 454 | MV_REFERENCE_FRAME ref_frame; |
| 455 | |
| 456 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 457 | char file_name[256] = ""; |
| 458 | snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv", |
| 459 | cm->current_frame.frame_number, ref_frame); |
| 460 | dump_one_image(cm, get_ref_frame_yv12_buf(cpi, ref_frame), file_name); |
| 461 | } |
| 462 | } |
| 463 | #endif // DUMP_REF_FRAME_IMAGES == 1 |
| 464 | |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 465 | int av1_get_refresh_ref_frame_map(int refresh_frame_flags) { |
Jingning Han | 8a6a1b8 | 2020-08-11 23:46:15 -0700 | [diff] [blame] | 466 | int ref_map_index; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 467 | |
| 468 | for (ref_map_index = 0; ref_map_index < REF_FRAMES; ++ref_map_index) |
| 469 | if ((refresh_frame_flags >> ref_map_index) & 1) break; |
| 470 | |
Jingning Han | 8a6a1b8 | 2020-08-11 23:46:15 -0700 | [diff] [blame] | 471 | if (ref_map_index == REF_FRAMES) ref_map_index = INVALID_IDX; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 472 | return ref_map_index; |
| 473 | } |
| 474 | |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 475 | static void update_arf_stack(int ref_map_index, |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 476 | RefBufferStack *ref_buffer_stack) { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 477 | if (ref_buffer_stack->arf_stack_size >= 0) { |
| 478 | if (ref_buffer_stack->arf_stack[0] == ref_map_index) |
| 479 | stack_pop(ref_buffer_stack->arf_stack, &ref_buffer_stack->arf_stack_size); |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 482 | if (ref_buffer_stack->lst_stack_size) { |
| 483 | for (int i = ref_buffer_stack->lst_stack_size - 1; i >= 0; --i) { |
| 484 | if (ref_buffer_stack->lst_stack[i] == ref_map_index) { |
| 485 | for (int idx = i; idx < ref_buffer_stack->lst_stack_size - 1; ++idx) |
| 486 | ref_buffer_stack->lst_stack[idx] = |
| 487 | ref_buffer_stack->lst_stack[idx + 1]; |
Cheng Chen | c86d2bf | 2019-09-05 15:07:47 -0700 | [diff] [blame] | 488 | ref_buffer_stack->lst_stack[ref_buffer_stack->lst_stack_size - 1] = |
| 489 | INVALID_IDX; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 490 | --ref_buffer_stack->lst_stack_size; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 495 | if (ref_buffer_stack->gld_stack_size) { |
| 496 | for (int i = ref_buffer_stack->gld_stack_size - 1; i >= 0; --i) { |
| 497 | if (ref_buffer_stack->gld_stack[i] == ref_map_index) { |
| 498 | for (int idx = i; idx < ref_buffer_stack->gld_stack_size - 1; ++idx) |
| 499 | ref_buffer_stack->gld_stack[idx] = |
| 500 | ref_buffer_stack->gld_stack[idx + 1]; |
Cheng Chen | c86d2bf | 2019-09-05 15:07:47 -0700 | [diff] [blame] | 501 | ref_buffer_stack->gld_stack[ref_buffer_stack->gld_stack_size - 1] = |
| 502 | INVALID_IDX; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 503 | --ref_buffer_stack->gld_stack_size; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Jingning Han | 647f2d1 | 2019-07-23 14:15:42 -0700 | [diff] [blame] | 509 | // Update reference frame stack info. |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 510 | void av1_update_ref_frame_map(AV1_COMP *cpi, |
| 511 | FRAME_UPDATE_TYPE frame_update_type, |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 512 | REFBUF_STATE refbuf_state, int ref_map_index, |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 513 | RefBufferStack *ref_buffer_stack) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 514 | AV1_COMMON *const cm = &cpi->common; |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 515 | |
Jingning Han | 47aaf87 | 2019-07-21 14:56:32 -0700 | [diff] [blame] | 516 | // TODO(jingning): Consider the S-frame same as key frame for the |
| 517 | // reference frame tracking purpose. The logic might be better |
| 518 | // expressed than converting the frame update type. |
Jingning Han | b55db1a | 2021-06-18 13:12:50 -0700 | [diff] [blame] | 519 | if (frame_is_sframe(cm)) frame_update_type = KF_UPDATE; |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 520 | if (is_frame_droppable(&cpi->svc, &cpi->ext_flags.refresh_frame)) return; |
Jingning Han | bcbbd8c | 2019-07-21 16:37:12 -0700 | [diff] [blame] | 521 | |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 522 | switch (frame_update_type) { |
Jingning Han | b55db1a | 2021-06-18 13:12:50 -0700 | [diff] [blame] | 523 | case KF_UPDATE: |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 524 | stack_reset(ref_buffer_stack->lst_stack, |
| 525 | &ref_buffer_stack->lst_stack_size); |
| 526 | stack_reset(ref_buffer_stack->gld_stack, |
| 527 | &ref_buffer_stack->gld_stack_size); |
| 528 | stack_reset(ref_buffer_stack->arf_stack, |
| 529 | &ref_buffer_stack->arf_stack_size); |
| 530 | stack_push(ref_buffer_stack->gld_stack, &ref_buffer_stack->gld_stack_size, |
| 531 | ref_map_index); |
| 532 | break; |
| 533 | case GF_UPDATE: |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 534 | update_arf_stack(ref_map_index, ref_buffer_stack); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 535 | stack_push(ref_buffer_stack->gld_stack, &ref_buffer_stack->gld_stack_size, |
| 536 | ref_map_index); |
Fyodor Kyslov | 915b9b8 | 2019-11-11 11:53:03 -0800 | [diff] [blame] | 537 | // For nonrd_mode: update LAST as well on GF_UPDATE frame. |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 538 | // TODO(jingning, marpan): Why replacing both reference frames with the |
| 539 | // same decoded frame? |
chiyotsai | 56681b5 | 2019-12-16 11:15:25 -0800 | [diff] [blame] | 540 | if (cpi->sf.rt_sf.use_nonrd_pick_mode) |
Marco Paniconi | fa29e16 | 2019-08-05 18:08:04 -0700 | [diff] [blame] | 541 | stack_push(ref_buffer_stack->lst_stack, |
| 542 | &ref_buffer_stack->lst_stack_size, ref_map_index); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 543 | break; |
| 544 | case LF_UPDATE: |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 545 | update_arf_stack(ref_map_index, ref_buffer_stack); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 546 | stack_push(ref_buffer_stack->lst_stack, &ref_buffer_stack->lst_stack_size, |
| 547 | ref_map_index); |
| 548 | break; |
| 549 | case ARF_UPDATE: |
| 550 | case INTNL_ARF_UPDATE: |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 551 | if (refbuf_state == REFBUF_RESET) { |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 552 | stack_reset(ref_buffer_stack->lst_stack, |
| 553 | &ref_buffer_stack->lst_stack_size); |
| 554 | stack_reset(ref_buffer_stack->gld_stack, |
| 555 | &ref_buffer_stack->gld_stack_size); |
| 556 | stack_reset(ref_buffer_stack->arf_stack, |
| 557 | &ref_buffer_stack->arf_stack_size); |
| 558 | } else { |
| 559 | update_arf_stack(ref_map_index, ref_buffer_stack); |
| 560 | } |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 561 | stack_push(ref_buffer_stack->arf_stack, &ref_buffer_stack->arf_stack_size, |
| 562 | ref_map_index); |
| 563 | break; |
| 564 | case OVERLAY_UPDATE: |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 565 | if (refbuf_state == REFBUF_RESET) { |
bohanli | 2541b8a | 2020-10-14 17:33:13 -0700 | [diff] [blame] | 566 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 567 | &ref_buffer_stack->arf_stack_size); |
| 568 | stack_reset(ref_buffer_stack->lst_stack, |
| 569 | &ref_buffer_stack->lst_stack_size); |
| 570 | stack_reset(ref_buffer_stack->gld_stack, |
| 571 | &ref_buffer_stack->gld_stack_size); |
| 572 | stack_reset(ref_buffer_stack->arf_stack, |
| 573 | &ref_buffer_stack->arf_stack_size); |
| 574 | stack_push(ref_buffer_stack->gld_stack, |
| 575 | &ref_buffer_stack->gld_stack_size, ref_map_index); |
| 576 | } else { |
| 577 | if (ref_map_index != INVALID_IDX) { |
| 578 | update_arf_stack(ref_map_index, ref_buffer_stack); |
| 579 | stack_push(ref_buffer_stack->lst_stack, |
| 580 | &ref_buffer_stack->lst_stack_size, ref_map_index); |
| 581 | } |
| 582 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 583 | &ref_buffer_stack->arf_stack_size); |
| 584 | stack_push(ref_buffer_stack->gld_stack, |
| 585 | &ref_buffer_stack->gld_stack_size, ref_map_index); |
Jingning Han | 1a7fbd7 | 2020-09-25 13:30:06 -0700 | [diff] [blame] | 586 | } |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 587 | break; |
| 588 | case INTNL_OVERLAY_UPDATE: |
| 589 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 590 | &ref_buffer_stack->arf_stack_size); |
| 591 | stack_push(ref_buffer_stack->lst_stack, &ref_buffer_stack->lst_stack_size, |
| 592 | ref_map_index); |
| 593 | break; |
| 594 | default: assert(0 && "unknown type"); |
Jingning Han | cf6d325 | 2019-07-03 14:26:45 -0700 | [diff] [blame] | 595 | } |
Jingning Han | 84a7050 | 2019-07-19 11:38:14 -0700 | [diff] [blame] | 596 | return; |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 599 | static int get_free_ref_map_index( |
| 600 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 601 | RefFrameMapPair ref_map_pairs[REF_FRAMES], |
| 602 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 603 | const RefBufferStack *ref_buffer_stack) { |
| 604 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 605 | (void)ref_buffer_stack; |
| 606 | for (int idx = 0; idx < REF_FRAMES; ++idx) |
| 607 | if (ref_map_pairs[idx].disp_order == -1) return idx; |
| 608 | return INVALID_IDX; |
| 609 | #else |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 610 | for (int idx = 0; idx < REF_FRAMES; ++idx) { |
| 611 | int is_free = 1; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 612 | for (int i = 0; i < ref_buffer_stack->arf_stack_size; ++i) { |
| 613 | if (ref_buffer_stack->arf_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 614 | is_free = 0; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 619 | for (int i = 0; i < ref_buffer_stack->lst_stack_size; ++i) { |
| 620 | if (ref_buffer_stack->lst_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 621 | is_free = 0; |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 626 | for (int i = 0; i < ref_buffer_stack->gld_stack_size; ++i) { |
| 627 | if (ref_buffer_stack->gld_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 628 | is_free = 0; |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (is_free) return idx; |
| 634 | } |
| 635 | return INVALID_IDX; |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 636 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 639 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 640 | static int get_refresh_idx(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], |
Remya Prakasan | a05ccde | 2021-06-07 21:24:11 +0530 | [diff] [blame] | 641 | int update_arf, |
| 642 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 643 | GF_GROUP *gf_group, int gf_index, |
| 644 | int enable_refresh_skip, |
| 645 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 646 | int cur_frame_disp) { |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 647 | int arf_count = 0; |
| 648 | int oldest_arf_order = INT32_MAX; |
| 649 | int oldest_arf_idx = -1; |
| 650 | |
| 651 | int oldest_frame_order = INT32_MAX; |
| 652 | int oldest_idx = -1; |
| 653 | |
| 654 | for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) { |
| 655 | RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx]; |
| 656 | if (ref_pair.disp_order == -1) continue; |
| 657 | const int frame_order = ref_pair.disp_order; |
| 658 | const int reference_frame_level = ref_pair.pyr_level; |
| 659 | // Do not refresh a future frame. |
| 660 | if (frame_order > cur_frame_disp) continue; |
| 661 | |
Remya Prakasan | a05ccde | 2021-06-07 21:24:11 +0530 | [diff] [blame] | 662 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 663 | if (enable_refresh_skip) { |
| 664 | int skip_frame = 0; |
| 665 | // Prevent refreshing a frame in gf_group->skip_frame_refresh. |
| 666 | for (int i = 0; i < REF_FRAMES; i++) { |
| 667 | int frame_to_skip = gf_group->skip_frame_refresh[gf_index][i]; |
| 668 | if (frame_to_skip == INVALID_IDX) break; |
| 669 | if (frame_order == frame_to_skip) { |
| 670 | skip_frame = 1; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | if (skip_frame) continue; |
| 675 | } |
| 676 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 677 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 678 | // Keep track of the oldest level 1 frame if the current frame is also level |
| 679 | // 1. |
| 680 | if (reference_frame_level == 1) { |
| 681 | // If there are more than 2 level 1 frames in the reference list, |
| 682 | // discard the oldest. |
| 683 | if (frame_order < oldest_arf_order) { |
| 684 | oldest_arf_order = frame_order; |
| 685 | oldest_arf_idx = map_idx; |
| 686 | } |
| 687 | arf_count++; |
| 688 | continue; |
| 689 | } |
| 690 | |
| 691 | // Update the overall oldest reference frame. |
| 692 | if (frame_order < oldest_frame_order) { |
| 693 | oldest_frame_order = frame_order; |
| 694 | oldest_idx = map_idx; |
| 695 | } |
| 696 | } |
| 697 | if (update_arf && arf_count > 2) return oldest_arf_idx; |
| 698 | if (oldest_idx >= 0) return oldest_idx; |
| 699 | if (oldest_arf_idx >= 0) return oldest_arf_idx; |
Remya Prakasan | a05ccde | 2021-06-07 21:24:11 +0530 | [diff] [blame] | 700 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 701 | if (oldest_idx == -1) { |
| 702 | assert(arf_count > 2 && enable_refresh_skip); |
| 703 | return oldest_arf_idx; |
| 704 | } |
| 705 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 706 | assert(0 && "No valid refresh index found"); |
| 707 | return -1; |
| 708 | } |
| 709 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 710 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 711 | int av1_get_refresh_frame_flags(const AV1_COMP *const cpi, |
| 712 | const EncodeFrameParams *const frame_params, |
| 713 | FRAME_UPDATE_TYPE frame_update_type, |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 714 | int gf_index, |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 715 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 716 | int cur_disp_order, |
| 717 | RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], |
| 718 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 719 | const RefBufferStack *const ref_buffer_stack) { |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 720 | const AV1_COMMON *const cm = &cpi->common; |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 721 | const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
| 722 | &cpi->ext_flags.refresh_frame; |
| 723 | |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 724 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
| 725 | if (gf_group->refbuf_state[gf_index] == REFBUF_RESET) |
| 726 | return SELECT_ALL_BUF_SLOTS; |
| 727 | |
| 728 | // TODO(jingning): Deprecate the following operations. |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 729 | // Switch frames and shown key-frames overwrite all reference slots |
Jingning Han | aa542f4 | 2021-06-19 14:45:37 -0700 | [diff] [blame^] | 730 | if (frame_params->frame_type == S_FRAME) return SELECT_ALL_BUF_SLOTS; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 731 | |
| 732 | // show_existing_frames don't actually send refresh_frame_flags so set the |
| 733 | // flags to 0 to keep things consistent. |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 734 | if (frame_params->show_existing_frame && |
| 735 | (!frame_params->error_resilient_mode || |
| 736 | frame_params->frame_type == KEY_FRAME)) { |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 740 | const SVC *const svc = &cpi->svc; |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 741 | if (is_frame_droppable(svc, ext_refresh_frame_flags)) return 0; |
Jingning Han | bcbbd8c | 2019-07-21 16:37:12 -0700 | [diff] [blame] | 742 | |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 743 | int refresh_mask = 0; |
| 744 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 745 | if (ext_refresh_frame_flags->update_pending) { |
Marco Paniconi | e5de332 | 2021-03-22 22:03:17 -0700 | [diff] [blame] | 746 | if (svc->set_ref_frame_config) { |
Marco Paniconi | d8574e3 | 2019-08-04 21:30:12 -0700 | [diff] [blame] | 747 | for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 748 | int ref_frame_map_idx = svc->ref_idx[i]; |
| 749 | refresh_mask |= svc->refresh[ref_frame_map_idx] << ref_frame_map_idx; |
Marco Paniconi | d8574e3 | 2019-08-04 21:30:12 -0700 | [diff] [blame] | 750 | } |
| 751 | return refresh_mask; |
| 752 | } |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 753 | // Unfortunately the encoder interface reflects the old refresh_*_frame |
| 754 | // flags so we have to replicate the old refresh_frame_flags logic here in |
| 755 | // order to preserve the behaviour of the flag overrides. |
Marco Paniconi | 314bc36 | 2019-08-13 10:53:02 -0700 | [diff] [blame] | 756 | int ref_frame_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 757 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 758 | refresh_mask |= ext_refresh_frame_flags->last_frame << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 759 | |
| 760 | ref_frame_map_idx = get_ref_frame_map_idx(cm, EXTREF_FRAME); |
| 761 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 762 | refresh_mask |= ext_refresh_frame_flags->bwd_ref_frame |
| 763 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 764 | |
| 765 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF2_FRAME); |
| 766 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 767 | refresh_mask |= ext_refresh_frame_flags->alt2_ref_frame |
| 768 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 769 | |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 770 | if (frame_update_type == OVERLAY_UPDATE) { |
Jingning Han | 5738e03 | 2019-07-22 15:22:52 -0700 | [diff] [blame] | 771 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME); |
| 772 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 773 | refresh_mask |= ext_refresh_frame_flags->golden_frame |
| 774 | << ref_frame_map_idx; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 775 | } else { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 776 | ref_frame_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME); |
| 777 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 778 | refresh_mask |= ext_refresh_frame_flags->golden_frame |
| 779 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 780 | |
| 781 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME); |
| 782 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 783 | refresh_mask |= ext_refresh_frame_flags->alt_ref_frame |
| 784 | << ref_frame_map_idx; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 785 | } |
| 786 | return refresh_mask; |
| 787 | } |
| 788 | |
Jingning Han | c9c172d | 2019-07-23 14:10:32 -0700 | [diff] [blame] | 789 | // Search for the open slot to store the current frame. |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 790 | int free_fb_index = get_free_ref_map_index( |
| 791 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 792 | ref_frame_map_pairs, |
| 793 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 794 | ref_buffer_stack); |
| 795 | |
| 796 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 797 | // No refresh necessary for these frame types. |
| 798 | if (frame_update_type == OVERLAY_UPDATE || |
| 799 | frame_update_type == INTNL_OVERLAY_UPDATE) |
| 800 | return refresh_mask; |
| 801 | |
| 802 | // If there is an open slot, refresh that one instead of replacing a |
| 803 | // reference. |
| 804 | if (free_fb_index != INVALID_IDX) { |
| 805 | refresh_mask = 1 << free_fb_index; |
| 806 | return refresh_mask; |
| 807 | } |
Remya Prakasan | a05ccde | 2021-06-07 21:24:11 +0530 | [diff] [blame] | 808 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 809 | const int enable_refresh_skip = !is_one_pass_rt_params(cpi); |
| 810 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 811 | const int update_arf = frame_update_type == ARF_UPDATE; |
| 812 | const int refresh_idx = |
Remya Prakasan | a05ccde | 2021-06-07 21:24:11 +0530 | [diff] [blame] | 813 | get_refresh_idx(ref_frame_map_pairs, update_arf, |
| 814 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 815 | &cpi->ppi->gf_group, gf_index, enable_refresh_skip, |
| 816 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 817 | cur_disp_order); |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 818 | return 1 << refresh_idx; |
| 819 | #else |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 820 | switch (frame_update_type) { |
| 821 | case KF_UPDATE: |
| 822 | case GF_UPDATE: |
| 823 | if (free_fb_index != INVALID_IDX) { |
| 824 | refresh_mask = 1 << free_fb_index; |
| 825 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 826 | if (ref_buffer_stack->gld_stack_size) |
| 827 | refresh_mask = |
| 828 | 1 << ref_buffer_stack |
| 829 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 830 | else |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 831 | refresh_mask = |
| 832 | 1 << ref_buffer_stack |
| 833 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 834 | } |
| 835 | break; |
| 836 | case LF_UPDATE: |
| 837 | if (free_fb_index != INVALID_IDX) { |
| 838 | refresh_mask = 1 << free_fb_index; |
| 839 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 840 | if (ref_buffer_stack->lst_stack_size >= 2) |
| 841 | refresh_mask = |
| 842 | 1 << ref_buffer_stack |
| 843 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 1af1c01 | 2020-02-10 10:51:56 -0800 | [diff] [blame] | 844 | else if (ref_buffer_stack->gld_stack_size >= 2) |
| 845 | refresh_mask = |
| 846 | 1 << ref_buffer_stack |
| 847 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 848 | else |
| 849 | assert(0 && "No ref map index found"); |
| 850 | } |
| 851 | break; |
| 852 | case ARF_UPDATE: |
| 853 | if (free_fb_index != INVALID_IDX) { |
| 854 | refresh_mask = 1 << free_fb_index; |
| 855 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 856 | if (ref_buffer_stack->gld_stack_size >= 3) |
| 857 | refresh_mask = |
| 858 | 1 << ref_buffer_stack |
| 859 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
| 860 | else if (ref_buffer_stack->lst_stack_size >= 2) |
| 861 | refresh_mask = |
| 862 | 1 << ref_buffer_stack |
| 863 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 864 | else |
| 865 | assert(0 && "No ref map index found"); |
| 866 | } |
| 867 | break; |
| 868 | case INTNL_ARF_UPDATE: |
| 869 | if (free_fb_index != INVALID_IDX) { |
| 870 | refresh_mask = 1 << free_fb_index; |
| 871 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 872 | refresh_mask = |
| 873 | 1 << ref_buffer_stack |
| 874 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 875 | } |
| 876 | break; |
Jingning Han | 1a7fbd7 | 2020-09-25 13:30:06 -0700 | [diff] [blame] | 877 | case OVERLAY_UPDATE: |
| 878 | if (free_fb_index != INVALID_IDX) refresh_mask = 1 << free_fb_index; |
| 879 | break; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 880 | case INTNL_OVERLAY_UPDATE: break; |
| 881 | default: assert(0); break; |
| 882 | } |
| 883 | |
| 884 | return refresh_mask; |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 885 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 888 | #if !CONFIG_REALTIME_ONLY |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 889 | void setup_mi(AV1_COMP *const cpi, YV12_BUFFER_CONFIG *src) { |
| 890 | AV1_COMMON *const cm = &cpi->common; |
| 891 | const int num_planes = av1_num_planes(cm); |
| 892 | MACROBLOCK *const x = &cpi->td.mb; |
| 893 | MACROBLOCKD *const xd = &x->e_mbd; |
| 894 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 895 | av1_setup_src_planes(x, src, 0, 0, num_planes, cm->seq_params->sb_size); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 896 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 897 | av1_setup_block_planes(xd, cm->seq_params->subsampling_x, |
| 898 | cm->seq_params->subsampling_y, num_planes); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 899 | |
Urvang Joshi | 2603bfe | 2020-03-25 13:33:18 -0700 | [diff] [blame] | 900 | set_mi_offsets(&cm->mi_params, xd, 0, 0); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 901 | } |
| 902 | |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 903 | // Apply temporal filtering to source frames and encode the filtered frame. |
| 904 | // If the current frame does not require filtering, this function is identical |
| 905 | // to av1_encode() except that tpl is not performed. |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 906 | static int denoise_and_encode(AV1_COMP *const cpi, uint8_t *const dest, |
| 907 | EncodeFrameInput *const frame_input, |
| 908 | EncodeFrameParams *const frame_params, |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 909 | EncodeFrameResults *const frame_results) { |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 910 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 911 | if (cpi->oxcf.pass == 2) start_timing(cpi, denoise_and_encode_time); |
| 912 | #endif |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 913 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
| 914 | AV1_COMMON *const cm = &cpi->common; |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 915 | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 916 | FRAME_UPDATE_TYPE update_type = |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 917 | get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index); |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 918 | |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 919 | // Decide whether to apply temporal filtering to the source frame. |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 920 | int apply_filtering = 0; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 921 | if (frame_params->frame_type == KEY_FRAME) { |
| 922 | // Decide whether it is allowed to perform key frame filtering |
| 923 | int allow_kf_filtering = |
| 924 | oxcf->kf_cfg.enable_keyframe_filtering && |
| 925 | !is_stat_generation_stage(cpi) && !frame_params->show_existing_frame && |
| 926 | cpi->rc.frames_to_key > cpi->oxcf.algo_cfg.arnr_max_frames && |
| 927 | !is_lossless_requested(&oxcf->rc_cfg) && |
Jayasanker J | 90aa0df | 2021-01-27 13:03:13 +0530 | [diff] [blame] | 928 | oxcf->algo_cfg.arnr_max_frames > 0 && oxcf->gf_cfg.lag_in_frames > 1; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 929 | if (allow_kf_filtering) { |
| 930 | const double y_noise_level = av1_estimate_noise_from_single_plane( |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 931 | frame_input->source, 0, cm->seq_params->bit_depth); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 932 | apply_filtering = y_noise_level > 0; |
Sarah Parker | 21f1e6e | 2019-11-15 16:48:29 -0800 | [diff] [blame] | 933 | } else { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 934 | apply_filtering = 0; |
Sarah Parker | 21f1e6e | 2019-11-15 16:48:29 -0800 | [diff] [blame] | 935 | } |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 936 | // If we are doing kf filtering, set up a few things. |
| 937 | if (apply_filtering) { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 938 | av1_setup_past_independence(cm); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 939 | } |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 940 | } else if (update_type == ARF_UPDATE || update_type == INTNL_ARF_UPDATE) { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 941 | // ARF |
| 942 | apply_filtering = oxcf->algo_cfg.arnr_max_frames > 0; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 943 | } |
Mufaddal Chakera | 186f8e3 | 2021-03-17 13:20:00 +0530 | [diff] [blame] | 944 | if (is_stat_generation_stage(cpi)) { |
| 945 | apply_filtering = 0; |
| 946 | } |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 947 | |
| 948 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 949 | if (cpi->oxcf.pass == 2) start_timing(cpi, apply_filtering_time); |
| 950 | #endif |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 951 | // Save the pointer to the original source image. |
| 952 | YV12_BUFFER_CONFIG *source_buffer = frame_input->source; |
| 953 | // apply filtering to frame |
| 954 | if (apply_filtering) { |
| 955 | int show_existing_alt_ref = 0; |
| 956 | // TODO(bohanli): figure out why we need frame_type in cm here. |
| 957 | cm->current_frame.frame_type = frame_params->frame_type; |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 958 | int arf_src_index = gf_group->arf_src_offset[cpi->gf_frame_index]; |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 959 | int is_forward_keyframe = 0; |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 960 | if (!frame_params->show_frame && cpi->no_show_fwd_kf) { |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 961 | // TODO(angiebird): Figure out why this condition yields forward keyframe. |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 962 | // fwd kf |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 963 | is_forward_keyframe = 1; |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 964 | } |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 965 | const int code_arf = |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 966 | av1_temporal_filter(cpi, arf_src_index, update_type, |
| 967 | is_forward_keyframe, &show_existing_alt_ref); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 968 | if (code_arf) { |
Tarundeep Singh | 4593fcf | 2021-03-31 00:53:31 +0530 | [diff] [blame] | 969 | aom_extend_frame_borders(&cpi->ppi->alt_ref_buffer, av1_num_planes(cm)); |
| 970 | frame_input->source = &cpi->ppi->alt_ref_buffer; |
Daniel Max Valenzuela | cab4d39 | 2020-07-10 15:28:11 -0700 | [diff] [blame] | 971 | aom_copy_metadata_to_frame_buffer(frame_input->source, |
| 972 | source_buffer->metadata); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 973 | } |
bohanli | cbe8e74 | 2020-08-17 14:19:17 -0700 | [diff] [blame] | 974 | // Currently INTNL_ARF_UPDATE only do show_existing. |
Angie Chiang | 7b0628b | 2021-01-10 18:07:49 -0800 | [diff] [blame] | 975 | if (update_type == ARF_UPDATE && !cpi->no_show_fwd_kf) { |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 976 | cpi->show_existing_alt_ref = show_existing_alt_ref; |
| 977 | } |
Hui Su | b148537 | 2020-04-16 23:31:39 -0700 | [diff] [blame] | 978 | } |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 979 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 980 | if (cpi->oxcf.pass == 2) end_timing(cpi, apply_filtering_time); |
| 981 | #endif |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 982 | |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 983 | // perform tpl after filtering |
Deepa K G | 23d914b | 2020-07-08 14:04:13 +0530 | [diff] [blame] | 984 | int allow_tpl = oxcf->gf_cfg.lag_in_frames > 1 && |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 985 | !is_stat_generation_stage(cpi) && |
| 986 | oxcf->algo_cfg.enable_tpl_model; |
| 987 | if (frame_params->frame_type == KEY_FRAME) { |
Bohan Li | 8e6d8b2 | 2021-01-14 14:57:15 -0800 | [diff] [blame] | 988 | // Don't do tpl for fwd key frames or fwd key frame overlays |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 989 | allow_tpl = allow_tpl && !cpi->sf.tpl_sf.disable_filtered_key_tpl && |
Bohan Li | 8e6d8b2 | 2021-01-14 14:57:15 -0800 | [diff] [blame] | 990 | !cpi->no_show_fwd_kf && |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 991 | gf_group->update_type[cpi->gf_frame_index] != OVERLAY_UPDATE; |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 992 | } else { |
| 993 | // Do tpl after ARF is filtered, or if no ARF, at the second frame of GF |
| 994 | // group. |
| 995 | // TODO(bohanli): if no ARF, just do it at the first frame. |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 996 | int gf_index = cpi->gf_frame_index; |
Jingning Han | d5a40c9 | 2020-08-13 00:28:28 -0700 | [diff] [blame] | 997 | allow_tpl = allow_tpl && (gf_group->update_type[gf_index] == ARF_UPDATE || |
| 998 | gf_group->update_type[gf_index] == GF_UPDATE); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 999 | if (allow_tpl) { |
| 1000 | // Need to set the size for TPL for ARF |
| 1001 | // TODO(bohanli): Why is this? what part of it is necessary? |
Urvang Joshi | 2e592e3 | 2020-10-05 23:23:09 -0700 | [diff] [blame] | 1002 | av1_set_frame_size(cpi, cm->superres_upscaled_width, |
| 1003 | cm->superres_upscaled_height); |
Yunqing Wang | 8c3f275 | 2020-06-18 16:06:54 -0700 | [diff] [blame] | 1004 | } |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 1005 | } |
Jingning Han | dcb4fdc | 2020-09-22 09:48:56 -0700 | [diff] [blame] | 1006 | |
Angie Chiang | 2d4bb0e | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 1007 | #if CONFIG_RD_COMMAND |
| 1008 | if (frame_params->frame_type == KEY_FRAME) { |
| 1009 | char filepath[] = "rd_command.txt"; |
| 1010 | av1_read_rd_command(filepath, &cpi->rd_command); |
| 1011 | } |
| 1012 | #endif // CONFIG_RD_COMMAND |
| 1013 | |
Deepa K G | 5a6eb3b | 2020-10-16 20:09:14 +0530 | [diff] [blame] | 1014 | if (allow_tpl == 0) { |
| 1015 | // Avoid the use of unintended TPL stats from previous GOP's results. |
Mufaddal Chakera | 7260d14 | 2021-04-12 01:03:40 +0530 | [diff] [blame] | 1016 | if (cpi->gf_frame_index == 0 && !is_stat_generation_stage(cpi)) |
| 1017 | av1_init_tpl_stats(&cpi->ppi->tpl_data); |
Deepa K G | 5a6eb3b | 2020-10-16 20:09:14 +0530 | [diff] [blame] | 1018 | } else { |
Jingning Han | 5c01a46 | 2021-05-11 13:31:21 -0700 | [diff] [blame] | 1019 | if (!cpi->skip_tpl_setup_stats) { |
| 1020 | av1_tpl_preload_rc_estimate(cpi, frame_params); |
Deepa K G | 5a6eb3b | 2020-10-16 20:09:14 +0530 | [diff] [blame] | 1021 | av1_tpl_setup_stats(cpi, 0, frame_params, frame_input); |
Jingning Han | 5c01a46 | 2021-05-11 13:31:21 -0700 | [diff] [blame] | 1022 | } |
Deepa K G | 5a6eb3b | 2020-10-16 20:09:14 +0530 | [diff] [blame] | 1023 | } |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (av1_encode(cpi, dest, frame_input, frame_params, frame_results) != |
| 1026 | AOM_CODEC_OK) { |
| 1027 | return AOM_CODEC_ERROR; |
| 1028 | } |
| 1029 | |
| 1030 | // Set frame_input source to true source for psnr calculation. |
Mufaddal Chakera | ea4c6d5 | 2020-06-12 15:09:14 +0530 | [diff] [blame] | 1031 | if (apply_filtering && is_psnr_calc_enabled(cpi)) { |
Urvang Joshi | 708a34f | 2020-10-06 16:05:26 -0700 | [diff] [blame] | 1032 | cpi->source = |
| 1033 | av1_scale_if_required(cm, source_buffer, &cpi->scaled_source, |
| 1034 | cm->features.interp_filter, 0, false, true); |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 1035 | cpi->unscaled_source = source_buffer; |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 1036 | } |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 1037 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 1038 | if (cpi->oxcf.pass == 2) end_timing(cpi, denoise_and_encode_time); |
| 1039 | #endif |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 1040 | return AOM_CODEC_OK; |
| 1041 | } |
| 1042 | #endif // !CONFIG_REALTIME_ONLY |
| 1043 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1044 | static INLINE int find_unused_ref_frame(const int *used_ref_frames, |
| 1045 | const int *stack, int stack_size) { |
| 1046 | for (int i = 0; i < stack_size; ++i) { |
| 1047 | const int this_ref = stack[i]; |
| 1048 | int ref_idx = 0; |
| 1049 | for (ref_idx = 0; ref_idx <= ALTREF_FRAME - LAST_FRAME; ++ref_idx) { |
| 1050 | if (this_ref == used_ref_frames[ref_idx]) break; |
| 1051 | } |
| 1052 | |
| 1053 | // not in use |
| 1054 | if (ref_idx > ALTREF_FRAME - LAST_FRAME) return this_ref; |
| 1055 | } |
| 1056 | |
| 1057 | return INVALID_IDX; |
| 1058 | } |
| 1059 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1060 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1061 | /*!\cond */ |
| 1062 | // Struct to keep track of relevant reference frame data. |
| 1063 | typedef struct { |
| 1064 | int map_idx; |
| 1065 | int disp_order; |
| 1066 | int pyr_level; |
| 1067 | int used; |
| 1068 | } RefBufMapData; |
| 1069 | /*!\endcond */ |
| 1070 | |
| 1071 | // Comparison function to sort reference frames in ascending display order. |
| 1072 | static int compare_map_idx_pair_asc(const void *a, const void *b) { |
| 1073 | if (((RefBufMapData *)a)->disp_order == ((RefBufMapData *)b)->disp_order) { |
| 1074 | return 0; |
| 1075 | } else if (((const RefBufMapData *)a)->disp_order > |
| 1076 | ((const RefBufMapData *)b)->disp_order) { |
| 1077 | return 1; |
| 1078 | } else { |
| 1079 | return -1; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | // Checks to see if a particular reference frame is already in the reference |
| 1084 | // frame map. |
| 1085 | static int is_in_ref_map(RefBufMapData *map, int disp_order, int n_frames) { |
| 1086 | for (int i = 0; i < n_frames; i++) { |
| 1087 | if (disp_order == map[i].disp_order) return 1; |
| 1088 | } |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | // Add a reference buffer index to a named reference slot. |
| 1093 | static void add_ref_to_slot(RefBufMapData *ref, int *const remapped_ref_idx, |
| 1094 | int frame) { |
| 1095 | remapped_ref_idx[frame - LAST_FRAME] = ref->map_idx; |
| 1096 | ref->used = 1; |
| 1097 | } |
| 1098 | |
| 1099 | // Threshold dictating when we are allowed to start considering |
| 1100 | // leaving lowest level frames unmapped. |
| 1101 | #define LOW_LEVEL_FRAMES_TR 5 |
| 1102 | |
| 1103 | // Find which reference buffer should be left out of the named mapping. |
| 1104 | // This is because there are 8 reference buffers and only 7 named slots. |
| 1105 | static void set_unmapped_ref(RefBufMapData *buffer_map, int n_bufs, |
| 1106 | int n_min_level_refs, int min_level, |
| 1107 | int cur_frame_disp) { |
| 1108 | int max_dist = 0; |
| 1109 | int unmapped_idx = -1; |
| 1110 | if (n_bufs <= ALTREF_FRAME) return; |
| 1111 | for (int i = 0; i < n_bufs; i++) { |
| 1112 | if (buffer_map[i].used) continue; |
| 1113 | if (buffer_map[i].pyr_level != min_level || |
| 1114 | n_min_level_refs >= LOW_LEVEL_FRAMES_TR) { |
| 1115 | int dist = abs(cur_frame_disp - buffer_map[i].disp_order); |
| 1116 | if (dist > max_dist) { |
| 1117 | max_dist = dist; |
| 1118 | unmapped_idx = i; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | assert(unmapped_idx >= 0 && "Unmapped reference not found"); |
| 1123 | buffer_map[unmapped_idx].used = 1; |
| 1124 | } |
| 1125 | |
| 1126 | static void get_ref_frames(AV1_COMP *const cpi, |
| 1127 | RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1128 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1129 | int gf_index, |
| 1130 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1131 | int cur_frame_disp) { |
| 1132 | AV1_COMMON *cm = &cpi->common; |
| 1133 | int *const remapped_ref_idx = cm->remapped_ref_idx; |
| 1134 | |
| 1135 | int buf_map_idx = 0; |
| 1136 | |
| 1137 | // Initialize reference frame mappings. |
| 1138 | for (int i = 0; i < REF_FRAMES; ++i) remapped_ref_idx[i] = INVALID_IDX; |
| 1139 | |
| 1140 | RefBufMapData buffer_map[REF_FRAMES]; |
| 1141 | int n_bufs = 0; |
| 1142 | memset(buffer_map, 0, REF_FRAMES * sizeof(buffer_map[0])); |
| 1143 | int min_level = MAX_ARF_LAYERS; |
| 1144 | int max_level = 0; |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1145 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1146 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
| 1147 | int skip_ref_unmapping = 0; |
| 1148 | int is_one_pass_rt = is_one_pass_rt_params(cpi); |
| 1149 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1150 | |
| 1151 | // Go through current reference buffers and store display order, pyr level, |
| 1152 | // and map index. |
| 1153 | for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) { |
| 1154 | // Get reference frame buffer. |
| 1155 | RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx]; |
| 1156 | if (ref_pair.disp_order == -1) continue; |
| 1157 | const int frame_order = ref_pair.disp_order; |
| 1158 | // Avoid duplicates. |
| 1159 | if (is_in_ref_map(buffer_map, frame_order, n_bufs)) continue; |
| 1160 | const int reference_frame_level = ref_pair.pyr_level; |
| 1161 | |
| 1162 | // Keep track of the lowest and highest levels that currently exist. |
| 1163 | if (reference_frame_level < min_level) min_level = reference_frame_level; |
| 1164 | if (reference_frame_level > max_level) max_level = reference_frame_level; |
| 1165 | |
| 1166 | buffer_map[n_bufs].map_idx = map_idx; |
| 1167 | buffer_map[n_bufs].disp_order = frame_order; |
| 1168 | buffer_map[n_bufs].pyr_level = reference_frame_level; |
| 1169 | buffer_map[n_bufs].used = 0; |
| 1170 | n_bufs++; |
| 1171 | } |
| 1172 | |
| 1173 | // Sort frames in ascending display order. |
| 1174 | qsort(buffer_map, n_bufs, sizeof(buffer_map[0]), compare_map_idx_pair_asc); |
| 1175 | |
| 1176 | int n_min_level_refs = 0; |
| 1177 | int n_past_high_level = 0; |
| 1178 | int closest_past_ref = -1; |
| 1179 | int golden_idx = -1; |
| 1180 | int altref_idx = -1; |
| 1181 | |
| 1182 | // Find the GOLDEN_FRAME and BWDREF_FRAME. |
| 1183 | // Also collect various stats about the reference frames for the remaining |
| 1184 | // mappings. |
| 1185 | for (int i = n_bufs - 1; i >= 0; i--) { |
| 1186 | if (buffer_map[i].pyr_level == min_level) { |
| 1187 | // Keep track of the number of lowest level frames. |
| 1188 | n_min_level_refs++; |
| 1189 | if (buffer_map[i].disp_order < cur_frame_disp && golden_idx == -1 && |
| 1190 | remapped_ref_idx[GOLDEN_FRAME - LAST_FRAME] == INVALID_IDX) { |
| 1191 | // Save index for GOLDEN. |
| 1192 | golden_idx = i; |
| 1193 | } else if (buffer_map[i].disp_order > cur_frame_disp && |
| 1194 | altref_idx == -1 && |
| 1195 | remapped_ref_idx[ALTREF_FRAME - LAST_FRAME] == INVALID_IDX) { |
| 1196 | // Save index for ALTREF. |
| 1197 | altref_idx = i; |
| 1198 | } |
| 1199 | } else if (buffer_map[i].disp_order == cur_frame_disp) { |
| 1200 | // Map the BWDREF_FRAME if this is the show_existing_frame. |
| 1201 | add_ref_to_slot(&buffer_map[i], remapped_ref_idx, BWDREF_FRAME); |
| 1202 | } |
| 1203 | |
| 1204 | // Keep track of the number of past frames that are not at the lowest level. |
| 1205 | if (buffer_map[i].disp_order < cur_frame_disp && |
| 1206 | buffer_map[i].pyr_level != min_level) |
| 1207 | n_past_high_level++; |
| 1208 | |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1209 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1210 | // During parallel encodes of lower layer frames, exclude the first frame |
| 1211 | // (frame_parallel_level 1) from being used for the reference assignment of |
| 1212 | // the second frame (frame_parallel_level 2). |
| 1213 | if (!is_one_pass_rt && |
| 1214 | gf_group->skip_frame_as_ref[gf_index] != INVALID_IDX) { |
| 1215 | assert(gf_group->frame_parallel_level[gf_index] == 2 && |
| 1216 | gf_group->update_type[gf_index] == INTNL_ARF_UPDATE); |
| 1217 | assert(gf_group->frame_parallel_level[gf_index - 1] == 1 && |
| 1218 | gf_group->update_type[gf_index - 1] == INTNL_ARF_UPDATE); |
| 1219 | if (buffer_map[i].disp_order == gf_group->skip_frame_as_ref[gf_index]) { |
| 1220 | buffer_map[i].used = 1; |
| 1221 | // In case a ref frame is excluded from being used during assignment, |
| 1222 | // skip the call to set_unmapped_ref(). Applicable in steady state. |
| 1223 | skip_ref_unmapping = 1; |
| 1224 | } |
| 1225 | } |
| 1226 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1227 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1228 | // Keep track of where the frames change from being past frames to future |
| 1229 | // frames. |
| 1230 | if (buffer_map[i].disp_order < cur_frame_disp && closest_past_ref < 0) |
| 1231 | closest_past_ref = i; |
| 1232 | } |
| 1233 | |
| 1234 | // Do not map GOLDEN and ALTREF based on their pyramid level if all reference |
| 1235 | // frames have the same level. |
| 1236 | if (n_min_level_refs <= n_bufs) { |
| 1237 | // Map the GOLDEN_FRAME. |
| 1238 | if (golden_idx > -1) |
| 1239 | add_ref_to_slot(&buffer_map[golden_idx], remapped_ref_idx, GOLDEN_FRAME); |
| 1240 | // Map the ALTREF_FRAME. |
| 1241 | if (altref_idx > -1) |
| 1242 | add_ref_to_slot(&buffer_map[altref_idx], remapped_ref_idx, ALTREF_FRAME); |
| 1243 | } |
| 1244 | |
| 1245 | // Find the buffer to be excluded from the mapping. |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1246 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1247 | if (!skip_ref_unmapping) |
| 1248 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1249 | set_unmapped_ref(buffer_map, n_bufs, n_min_level_refs, min_level, |
| 1250 | cur_frame_disp); |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1251 | |
| 1252 | // Place past frames in LAST_FRAME, LAST2_FRAME, and LAST3_FRAME. |
| 1253 | for (int frame = LAST_FRAME; frame < GOLDEN_FRAME; frame++) { |
| 1254 | // Continue if the current ref slot is already full. |
| 1255 | if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; |
| 1256 | // Find the next unmapped reference buffer |
| 1257 | // in decreasing ouptut order relative to current picture. |
| 1258 | int next_buf_max = 0; |
| 1259 | int next_disp_order = INT_MIN; |
| 1260 | for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) { |
| 1261 | if (!buffer_map[buf_map_idx].used && |
| 1262 | buffer_map[buf_map_idx].disp_order < cur_frame_disp && |
| 1263 | buffer_map[buf_map_idx].disp_order > next_disp_order) { |
| 1264 | next_disp_order = buffer_map[buf_map_idx].disp_order; |
| 1265 | next_buf_max = buf_map_idx; |
| 1266 | } |
| 1267 | } |
| 1268 | buf_map_idx = next_buf_max; |
| 1269 | if (buf_map_idx < 0) break; |
| 1270 | if (buffer_map[buf_map_idx].used) break; |
| 1271 | add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame); |
| 1272 | } |
| 1273 | |
| 1274 | // Place future frames (if there are any) in BWDREF_FRAME and ALTREF2_FRAME. |
| 1275 | for (int frame = BWDREF_FRAME; frame < REF_FRAMES; frame++) { |
| 1276 | // Continue if the current ref slot is already full. |
| 1277 | if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; |
| 1278 | // Find the next unmapped reference buffer |
| 1279 | // in increasing ouptut order relative to current picture. |
| 1280 | int next_buf_max = 0; |
| 1281 | int next_disp_order = INT_MAX; |
| 1282 | for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) { |
| 1283 | if (!buffer_map[buf_map_idx].used && |
| 1284 | buffer_map[buf_map_idx].disp_order > cur_frame_disp && |
| 1285 | buffer_map[buf_map_idx].disp_order < next_disp_order) { |
| 1286 | next_disp_order = buffer_map[buf_map_idx].disp_order; |
| 1287 | next_buf_max = buf_map_idx; |
| 1288 | } |
| 1289 | } |
| 1290 | buf_map_idx = next_buf_max; |
| 1291 | if (buf_map_idx < 0) break; |
| 1292 | if (buffer_map[buf_map_idx].used) break; |
| 1293 | add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame); |
| 1294 | } |
| 1295 | |
| 1296 | // Place remaining past frames. |
| 1297 | buf_map_idx = closest_past_ref; |
| 1298 | for (int frame = LAST_FRAME; frame < REF_FRAMES; frame++) { |
| 1299 | // Continue if the current ref slot is already full. |
| 1300 | if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; |
| 1301 | // Find the next unmapped reference buffer. |
| 1302 | for (; buf_map_idx >= 0; buf_map_idx--) { |
| 1303 | if (!buffer_map[buf_map_idx].used) break; |
| 1304 | } |
| 1305 | if (buf_map_idx < 0) break; |
| 1306 | if (buffer_map[buf_map_idx].used) break; |
| 1307 | add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame); |
| 1308 | } |
| 1309 | |
| 1310 | // Place remaining future frames. |
| 1311 | buf_map_idx = n_bufs - 1; |
| 1312 | for (int frame = ALTREF_FRAME; frame >= LAST_FRAME; frame--) { |
| 1313 | // Continue if the current ref slot is already full. |
| 1314 | if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; |
| 1315 | // Find the next unmapped reference buffer. |
| 1316 | for (; buf_map_idx > closest_past_ref; buf_map_idx--) { |
| 1317 | if (!buffer_map[buf_map_idx].used) break; |
| 1318 | } |
| 1319 | if (buf_map_idx < 0) break; |
| 1320 | if (buffer_map[buf_map_idx].used) break; |
| 1321 | add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame); |
| 1322 | } |
| 1323 | |
| 1324 | // Fill any slots that are empty (should only happen for the first 7 frames). |
| 1325 | for (int i = 0; i < REF_FRAMES; ++i) |
| 1326 | if (remapped_ref_idx[i] == INVALID_IDX) remapped_ref_idx[i] = 0; |
| 1327 | } |
| 1328 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1329 | |
Angie Chiang | d96bce1 | 2021-03-24 19:52:18 -0700 | [diff] [blame] | 1330 | void av1_get_ref_frames(const RefBufferStack *ref_buffer_stack, |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1331 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1332 | AV1_COMP *cpi, |
| 1333 | RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], |
| 1334 | int cur_frame_disp, |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1335 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1336 | int gf_index, |
| 1337 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1338 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
Angie Chiang | d96bce1 | 2021-03-24 19:52:18 -0700 | [diff] [blame] | 1339 | int remapped_ref_idx[REF_FRAMES]) { |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1340 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1341 | (void)ref_buffer_stack; |
| 1342 | (void)remapped_ref_idx; |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1343 | get_ref_frames(cpi, ref_frame_map_pairs, |
| 1344 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1345 | gf_index, |
| 1346 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1347 | cur_frame_disp); |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1348 | return; |
| 1349 | #else |
Angie Chiang | d96bce1 | 2021-03-24 19:52:18 -0700 | [diff] [blame] | 1350 | const int *const arf_stack = ref_buffer_stack->arf_stack; |
| 1351 | const int *const lst_stack = ref_buffer_stack->lst_stack; |
| 1352 | const int *const gld_stack = ref_buffer_stack->gld_stack; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 1353 | const int arf_stack_size = ref_buffer_stack->arf_stack_size; |
| 1354 | const int lst_stack_size = ref_buffer_stack->lst_stack_size; |
| 1355 | const int gld_stack_size = ref_buffer_stack->gld_stack_size; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1356 | |
| 1357 | // Initialization |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1358 | for (int i = 0; i < REF_FRAMES; ++i) remapped_ref_idx[i] = INVALID_IDX; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1359 | |
| 1360 | if (arf_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1361 | remapped_ref_idx[ALTREF_FRAME - LAST_FRAME] = arf_stack[arf_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1362 | |
| 1363 | if (arf_stack_size > 1) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1364 | remapped_ref_idx[BWDREF_FRAME - LAST_FRAME] = arf_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1365 | |
| 1366 | if (arf_stack_size > 2) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1367 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = arf_stack[1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | if (lst_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1371 | remapped_ref_idx[LAST_FRAME - LAST_FRAME] = lst_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (lst_stack_size > 1) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1374 | remapped_ref_idx[LAST2_FRAME - LAST_FRAME] = lst_stack[1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | if (gld_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1378 | remapped_ref_idx[GOLDEN_FRAME - LAST_FRAME] = gld_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1379 | |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1380 | // If there are more frames in the golden stack, assign them to BWDREF, |
| 1381 | // ALTREF2, or LAST3. |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1382 | if (gld_stack_size > 1) { |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1383 | if (arf_stack_size <= 2) { |
| 1384 | if (arf_stack_size <= 1) { |
| 1385 | remapped_ref_idx[BWDREF_FRAME - LAST_FRAME] = gld_stack[1]; |
| 1386 | if (gld_stack_size > 2) |
| 1387 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = gld_stack[2]; |
| 1388 | } else { |
| 1389 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = gld_stack[1]; |
| 1390 | } |
| 1391 | } else { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1392 | remapped_ref_idx[LAST3_FRAME - LAST_FRAME] = gld_stack[1]; |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1393 | } |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | for (int idx = ALTREF_FRAME - LAST_FRAME; idx >= 0; --idx) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1398 | int ref_map_index = remapped_ref_idx[idx]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1399 | |
| 1400 | if (ref_map_index != INVALID_IDX) continue; |
| 1401 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1402 | ref_map_index = |
| 1403 | find_unused_ref_frame(remapped_ref_idx, arf_stack, arf_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1404 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1405 | if (ref_map_index == INVALID_IDX) { |
| 1406 | ref_map_index = |
| 1407 | find_unused_ref_frame(remapped_ref_idx, gld_stack, gld_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1410 | if (ref_map_index == INVALID_IDX) { |
| 1411 | ref_map_index = |
| 1412 | find_unused_ref_frame(remapped_ref_idx, lst_stack, lst_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1415 | if (ref_map_index != INVALID_IDX) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1416 | remapped_ref_idx[idx] = ref_map_index; |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1417 | } else if (!gld_stack_size && arf_stack_size) { |
| 1418 | remapped_ref_idx[idx] = ref_buffer_stack->arf_stack[0]; |
| 1419 | } else { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1420 | remapped_ref_idx[idx] = ref_buffer_stack->gld_stack[0]; |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1421 | } |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1422 | } |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1423 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1426 | int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size, |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1427 | uint8_t *const dest, unsigned int *frame_flags, |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1428 | int64_t *const time_stamp, int64_t *const time_end, |
Yue Chen | 1bc5be6 | 2018-08-24 13:57:32 -0700 | [diff] [blame] | 1429 | const aom_rational64_t *const timestamp_ratio, |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1430 | int *const pop_lookahead, int flush) { |
Vishesh | 7e9873d | 2020-06-08 15:41:33 +0530 | [diff] [blame] | 1431 | AV1EncoderConfig *const oxcf = &cpi->oxcf; |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 1432 | AV1_COMMON *const cm = &cpi->common; |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 1433 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 1434 | ExternalFlags *const ext_flags = &cpi->ext_flags; |
Vishesh | 5b50e6d | 2020-06-10 19:20:07 +0530 | [diff] [blame] | 1435 | GFConfig *const gf_cfg = &oxcf->gf_cfg; |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1436 | |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1437 | EncodeFrameInput frame_input; |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1438 | EncodeFrameParams frame_params; |
| 1439 | EncodeFrameResults frame_results; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1440 | memset(&frame_input, 0, sizeof(frame_input)); |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1441 | memset(&frame_params, 0, sizeof(frame_params)); |
| 1442 | memset(&frame_results, 0, sizeof(frame_results)); |
| 1443 | |
Jingning Han | d3e827d | 2020-08-16 16:07:24 -0700 | [diff] [blame] | 1444 | // Check if we need to stuff more src frames |
| 1445 | if (flush == 0) { |
| 1446 | int srcbuf_size = |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 1447 | av1_lookahead_depth(cpi->ppi->lookahead, cpi->compressor_stage); |
| 1448 | int pop_size = |
| 1449 | av1_lookahead_pop_sz(cpi->ppi->lookahead, cpi->compressor_stage); |
Jingning Han | d3e827d | 2020-08-16 16:07:24 -0700 | [diff] [blame] | 1450 | |
| 1451 | // Continue buffering look ahead buffer. |
| 1452 | if (srcbuf_size < pop_size) return -1; |
| 1453 | } |
| 1454 | |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 1455 | if (!av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage)) { |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1456 | #if !CONFIG_REALTIME_ONLY |
Bohan Li | 445fdf6 | 2021-06-03 16:16:00 -0700 | [diff] [blame] | 1457 | if (flush && oxcf->pass == AOM_RC_FIRST_PASS && |
| 1458 | !cpi->ppi->twopass.first_pass_done) { |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1459 | av1_end_first_pass(cpi); /* get last stats packet */ |
Mufaddal Chakera | 358cf21 | 2021-02-25 14:41:56 +0530 | [diff] [blame] | 1460 | cpi->ppi->twopass.first_pass_done = 1; |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1461 | } |
| 1462 | #endif |
| 1463 | return -1; |
| 1464 | } |
| 1465 | |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1466 | // TODO(sarahparker) finish bit allocation for one pass pyramid |
Aasaipriya | 5feffea | 2020-04-15 12:43:05 +0530 | [diff] [blame] | 1467 | if (has_no_stats_stage(cpi)) { |
Vishesh | 5b50e6d | 2020-06-10 19:20:07 +0530 | [diff] [blame] | 1468 | gf_cfg->gf_max_pyr_height = |
| 1469 | AOMMIN(gf_cfg->gf_max_pyr_height, USE_ALTREF_FOR_ONE_PASS); |
| 1470 | gf_cfg->gf_min_pyr_height = |
| 1471 | AOMMIN(gf_cfg->gf_min_pyr_height, gf_cfg->gf_max_pyr_height); |
Urvang Joshi | b44f48f | 2020-01-27 11:09:48 -0800 | [diff] [blame] | 1472 | } |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1473 | |
Mufaddal Chakera | 7260d14 | 2021-04-12 01:03:40 +0530 | [diff] [blame] | 1474 | cpi->skip_tpl_setup_stats = 0; |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1475 | #if !CONFIG_REALTIME_ONLY |
Deepa K G | c29630a | 2021-05-31 13:19:41 +0530 | [diff] [blame] | 1476 | const int use_one_pass_rt_params = is_one_pass_rt_params(cpi); |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1477 | if (!use_one_pass_rt_params && !is_stat_generation_stage(cpi)) { |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 1478 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 1479 | start_timing(cpi, av1_get_second_pass_params_time); |
| 1480 | #endif |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1481 | av1_get_second_pass_params(cpi, &frame_params, &frame_input, *frame_flags); |
Yunqing Wang | 7a3ad54 | 2020-11-03 23:40:24 -0800 | [diff] [blame] | 1482 | #if CONFIG_COLLECT_COMPONENT_TIMING |
| 1483 | end_timing(cpi, av1_get_second_pass_params_time); |
| 1484 | #endif |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1485 | } |
| 1486 | #endif |
| 1487 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1488 | if (!is_stat_generation_stage(cpi)) { |
Sarah Parker | 2beb1d1 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1489 | // If this is a forward keyframe, mark as a show_existing_frame |
bohanli | 1629a4b | 2020-06-11 16:15:14 -0700 | [diff] [blame] | 1490 | // TODO(bohanli): find a consistent condition for fwd keyframes |
Jingning Han | 2dcb050 | 2020-08-20 20:35:26 -0700 | [diff] [blame] | 1491 | if (oxcf->kf_cfg.fwd_kf_enabled && |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1492 | gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE && |
Jingning Han | 2dcb050 | 2020-08-20 20:35:26 -0700 | [diff] [blame] | 1493 | cpi->rc.frames_to_key == 0) { |
Sarah Parker | 2beb1d1 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1494 | frame_params.show_existing_frame = 1; |
| 1495 | } else { |
| 1496 | frame_params.show_existing_frame = |
Jingning Han | 2dcb050 | 2020-08-20 20:35:26 -0700 | [diff] [blame] | 1497 | (cpi->show_existing_alt_ref && |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1498 | gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) || |
| 1499 | gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE; |
Sarah Parker | 2beb1d1 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1500 | } |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1501 | frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags); |
Yunqing Wang | 1973f11 | 2019-10-18 15:50:04 -0700 | [diff] [blame] | 1502 | |
| 1503 | // Reset show_existing_alt_ref decision to 0 after it is used. |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1504 | if (gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) { |
Yunqing Wang | 1973f11 | 2019-10-18 15:50:04 -0700 | [diff] [blame] | 1505 | cpi->show_existing_alt_ref = 0; |
| 1506 | } |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 1507 | } else { |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1508 | frame_params.show_existing_frame = 0; |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1511 | struct lookahead_entry *source = NULL; |
| 1512 | struct lookahead_entry *last_source = NULL; |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1513 | if (frame_params.show_existing_frame) { |
Mufaddal Chakera | a65d2ce | 2021-02-15 12:20:48 +0530 | [diff] [blame] | 1514 | source = av1_lookahead_peek(cpi->ppi->lookahead, 0, cpi->compressor_stage); |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1515 | *pop_lookahead = 1; |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1516 | frame_params.show_frame = 1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1517 | } else { |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1518 | source = choose_frame_source(cpi, &flush, pop_lookahead, &last_source, |
Angie Chiang | 470d116 | 2020-12-31 13:10:55 -0800 | [diff] [blame] | 1519 | &frame_params); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | if (source == NULL) { // If no source was found, we can't encode a frame. |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1523 | #if !CONFIG_REALTIME_ONLY |
Bohan Li | 445fdf6 | 2021-06-03 16:16:00 -0700 | [diff] [blame] | 1524 | if (flush && oxcf->pass == AOM_RC_FIRST_PASS && |
| 1525 | !cpi->ppi->twopass.first_pass_done) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1526 | av1_end_first_pass(cpi); /* get last stats packet */ |
Mufaddal Chakera | 358cf21 | 2021-02-25 14:41:56 +0530 | [diff] [blame] | 1527 | cpi->ppi->twopass.first_pass_done = 1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1528 | } |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1529 | #endif |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1530 | return -1; |
| 1531 | } |
Mufaddal Chakera | 97f9271 | 2021-05-21 15:10:33 +0530 | [diff] [blame] | 1532 | |
| 1533 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1534 | // reset src_offset to allow actual encode call for this frame to get its |
| 1535 | // source. |
| 1536 | gf_group->src_offset[cpi->gf_frame_index] = 0; |
| 1537 | #endif |
| 1538 | |
bohanli | 0db9c51 | 2020-06-12 17:43:06 -0700 | [diff] [blame] | 1539 | // Source may be changed if temporal filtered later. |
| 1540 | frame_input.source = &source->img; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1541 | frame_input.last_source = last_source != NULL ? &last_source->img : NULL; |
| 1542 | frame_input.ts_duration = source->ts_end - source->ts_start; |
Cheng Chen | e1a7a3e | 2020-03-18 18:23:19 -0700 | [diff] [blame] | 1543 | // Save unfiltered source. It is used in av1_get_second_pass_params(). |
| 1544 | cpi->unfiltered_source = frame_input.source; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1545 | |
| 1546 | *time_stamp = source->ts_start; |
| 1547 | *time_end = source->ts_end; |
Yunqing Wang | 15ab03c | 2020-11-24 16:45:25 -0800 | [diff] [blame] | 1548 | if (source->ts_start < cpi->time_stamps.first_ts_start) { |
| 1549 | cpi->time_stamps.first_ts_start = source->ts_start; |
| 1550 | cpi->time_stamps.prev_ts_end = source->ts_start; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | av1_apply_encoding_flags(cpi, source->flags); |
bohanli | cbe8e74 | 2020-08-17 14:19:17 -0700 | [diff] [blame] | 1554 | *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1555 | |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1556 | // Shown frames and arf-overlay frames need frame-rate considering |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 1557 | if (frame_params.show_frame) |
| 1558 | adjust_frame_rate(cpi, source->ts_start, source->ts_end); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1559 | |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1560 | if (!frame_params.show_existing_frame) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1561 | if (cpi->film_grain_table) { |
Neil Birkbeck | bd40ca7 | 2019-03-02 13:25:50 -0800 | [diff] [blame] | 1562 | cm->cur_frame->film_grain_params_present = aom_film_grain_table_lookup( |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1563 | cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */, |
| 1564 | &cm->film_grain_params); |
Neil Birkbeck | bd40ca7 | 2019-03-02 13:25:50 -0800 | [diff] [blame] | 1565 | } else { |
| 1566 | cm->cur_frame->film_grain_params_present = |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1567 | cm->seq_params->film_grain_params_present; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1568 | } |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1569 | // only one operating point supported now |
Yue Chen | 1bc5be6 | 2018-08-24 13:57:32 -0700 | [diff] [blame] | 1570 | const int64_t pts64 = ticks_to_timebase_units(timestamp_ratio, *time_stamp); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1571 | if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR; |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1572 | cm->frame_presentation_time = (uint32_t)pts64; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1575 | #if CONFIG_REALTIME_ONLY |
| 1576 | av1_get_one_pass_rt_params(cpi, &frame_params, *frame_flags); |
Tarundeep Singh | 9e77b30 | 2021-03-19 16:07:48 +0530 | [diff] [blame] | 1577 | if (cpi->oxcf.speed >= 5 && cpi->ppi->number_spatial_layers == 1 && |
| 1578 | cpi->ppi->number_temporal_layers == 1) |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1579 | av1_set_reference_structure_one_pass_rt(cpi, cpi->gf_frame_index == 0); |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1580 | #else |
chiyotsai | 8b8f8a2 | 2020-04-21 11:03:47 -0700 | [diff] [blame] | 1581 | if (use_one_pass_rt_params) { |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1582 | av1_get_one_pass_rt_params(cpi, &frame_params, *frame_flags); |
Tarundeep Singh | 9e77b30 | 2021-03-19 16:07:48 +0530 | [diff] [blame] | 1583 | if (cpi->oxcf.speed >= 5 && cpi->ppi->number_spatial_layers == 1 && |
| 1584 | cpi->ppi->number_temporal_layers == 1) |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1585 | av1_set_reference_structure_one_pass_rt(cpi, cpi->gf_frame_index == 0); |
chiyotsai | 8b8f8a2 | 2020-04-21 11:03:47 -0700 | [diff] [blame] | 1586 | } |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1587 | #endif |
Jingning Han | 3f3318f | 2020-08-16 16:12:10 -0700 | [diff] [blame] | 1588 | |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1589 | FRAME_UPDATE_TYPE frame_update_type = |
| 1590 | get_frame_update_type(gf_group, cpi->gf_frame_index); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1591 | |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1592 | if (frame_params.show_existing_frame && |
| 1593 | frame_params.frame_type != KEY_FRAME) { |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 1594 | // Force show-existing frames to be INTER, except forward keyframes |
| 1595 | frame_params.frame_type = INTER_FRAME; |
| 1596 | } |
| 1597 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1598 | // TODO(david.turner@argondesign.com): Move all the encode strategy |
| 1599 | // (largely near av1_get_compressed_data) in here |
| 1600 | |
| 1601 | // TODO(david.turner@argondesign.com): Change all the encode strategy to |
| 1602 | // modify frame_params instead of cm or cpi. |
| 1603 | |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1604 | // Per-frame encode speed. In theory this can vary, but things may have |
| 1605 | // been written assuming speed-level will not change within a sequence, so |
| 1606 | // this parameter should be used with caution. |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1607 | frame_params.speed = oxcf->speed; |
| 1608 | |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1609 | // Work out some encoding parameters specific to the pass: |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame] | 1610 | if (has_no_stats_stage(cpi) && oxcf->q_cfg.aq_mode == CYCLIC_REFRESH_AQ) { |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1611 | av1_cyclic_refresh_update_parameters(cpi); |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1612 | } else if (is_stat_generation_stage(cpi)) { |
Vishesh | 39e7409 | 2020-06-16 17:13:48 +0530 | [diff] [blame] | 1613 | cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&oxcf->rc_cfg); |
Jayasanker J | 312b85e | 2021-02-23 12:44:30 +0530 | [diff] [blame] | 1614 | // Current frame is coded as a key-frame for any of the following cases: |
| 1615 | // 1) First frame of a video |
| 1616 | // 2) For all-intra frame encoding |
| 1617 | // 3) When a key-frame is forced |
| 1618 | const int kf_requested = |
| 1619 | (cm->current_frame.frame_number == 0 || |
| 1620 | oxcf->kf_cfg.key_freq_max == 0 || (*frame_flags & FRAMEFLAGS_KEY)); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1621 | if (kf_requested && frame_update_type != OVERLAY_UPDATE && |
| 1622 | frame_update_type != INTNL_OVERLAY_UPDATE) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1623 | frame_params.frame_type = KEY_FRAME; |
| 1624 | } else { |
| 1625 | frame_params.frame_type = INTER_FRAME; |
David Turner | cb5e36f | 2019-01-17 17:15:25 +0000 | [diff] [blame] | 1626 | } |
Hamsalekha S | 37cc1d1 | 2019-12-12 19:27:41 +0530 | [diff] [blame] | 1627 | } else if (is_stat_consumption_stage(cpi)) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1628 | #if CONFIG_MISMATCH_DEBUG |
| 1629 | mismatch_move_frame_idx_w(); |
| 1630 | #endif |
| 1631 | #if TXCOEFF_COST_TIMER |
| 1632 | cm->txcoeff_cost_timer = 0; |
| 1633 | cm->txcoeff_cost_count = 0; |
| 1634 | #endif |
| 1635 | } |
| 1636 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 1637 | if (!is_stat_generation_stage(cpi)) |
| 1638 | set_ext_overrides(cm, &frame_params, ext_flags); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1639 | |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1640 | // Shown keyframes and S frames refresh all reference buffers |
| 1641 | const int force_refresh_all = |
| 1642 | ((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) || |
| 1643 | frame_params.frame_type == S_FRAME) && |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1644 | !frame_params.show_existing_frame; |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1645 | |
Jingning Han | 880bb35 | 2021-06-18 11:57:26 -0700 | [diff] [blame] | 1646 | av1_configure_buffer_updates( |
| 1647 | cpi, &frame_params.refresh_frame, frame_update_type, |
| 1648 | gf_group->refbuf_state[cpi->gf_frame_index], force_refresh_all); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1649 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1650 | if (!is_stat_generation_stage(cpi)) { |
Deepa K G | 140bc83 | 2019-10-30 17:16:29 +0530 | [diff] [blame] | 1651 | const RefCntBuffer *ref_frames[INTER_REFS_PER_FRAME]; |
| 1652 | const YV12_BUFFER_CONFIG *ref_frame_buf[INTER_REFS_PER_FRAME]; |
| 1653 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1654 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1655 | RefFrameMapPair ref_frame_map_pairs[REF_FRAMES]; |
| 1656 | init_ref_map_pair(cpi, ref_frame_map_pairs); |
| 1657 | const int order_offset = gf_group->arf_src_offset[cpi->gf_frame_index]; |
| 1658 | const int cur_frame_disp = |
| 1659 | cpi->common.current_frame.frame_number + order_offset; |
| 1660 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1661 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1662 | if (!ext_flags->refresh_frame.update_pending) { |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1663 | av1_get_ref_frames(&cpi->ref_buffer_stack, |
| 1664 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1665 | cpi, ref_frame_map_pairs, cur_frame_disp, |
Remya Prakasan | fa7262a | 2021-05-29 16:00:30 +0530 | [diff] [blame] | 1666 | #if CONFIG_FRAME_PARALLEL_ENCODE_2 |
| 1667 | cpi->gf_frame_index, |
| 1668 | #endif // CONFIG_FRAME_PARALLEL_ENCODE_2 |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1669 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1670 | cm->remapped_ref_idx); |
Marco Paniconi | e5de332 | 2021-03-22 22:03:17 -0700 | [diff] [blame] | 1671 | } else if (cpi->svc.set_ref_frame_config) { |
Marco Paniconi | 6714211 | 2019-07-24 15:00:31 -0700 | [diff] [blame] | 1672 | for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) |
| 1673 | cm->remapped_ref_idx[i] = cpi->svc.ref_idx[i]; |
| 1674 | } |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1675 | |
Deepa K G | 140bc83 | 2019-10-30 17:16:29 +0530 | [diff] [blame] | 1676 | // Get the reference frames |
| 1677 | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
| 1678 | ref_frames[i] = get_ref_frame_buf(cm, ref_frame_priority_order[i]); |
| 1679 | ref_frame_buf[i] = ref_frames[i] != NULL ? &ref_frames[i]->buf : NULL; |
| 1680 | } |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1681 | |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1682 | // Work out which reference frame slots may be used. |
Jingning Han | b16c3da | 2020-09-22 22:37:02 -0700 | [diff] [blame] | 1683 | frame_params.ref_frame_flags = get_ref_frame_flags( |
| 1684 | &cpi->sf, ref_frame_buf, ext_flags->ref_frame_flags); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1685 | |
Remya Prakasan | a68eaef | 2021-05-19 19:15:52 +0530 | [diff] [blame] | 1686 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1687 | // Set primary_ref_frame of non-reference frames as PRIMARY_REF_NONE. |
| 1688 | if (cpi->ppi->gf_group.is_frame_non_ref[cpi->gf_frame_index]) { |
| 1689 | frame_params.primary_ref_frame = PRIMARY_REF_NONE; |
| 1690 | } else { |
| 1691 | frame_params.primary_ref_frame = |
| 1692 | choose_primary_ref_frame(cpi, &frame_params); |
| 1693 | } |
| 1694 | #else |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 1695 | frame_params.primary_ref_frame = |
| 1696 | choose_primary_ref_frame(cpi, &frame_params); |
Remya Prakasan | a68eaef | 2021-05-19 19:15:52 +0530 | [diff] [blame] | 1697 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1698 | |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1699 | frame_params.order_offset = gf_group->arf_src_offset[cpi->gf_frame_index]; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 1700 | |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 1701 | frame_params.refresh_frame_flags = av1_get_refresh_frame_flags( |
| 1702 | cpi, &frame_params, frame_update_type, cpi->gf_frame_index, |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1703 | #if CONFIG_FRAME_PARALLEL_ENCODE |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 1704 | cur_frame_disp, ref_frame_map_pairs, |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1705 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
Jingning Han | 11eaaa9 | 2021-06-18 01:13:48 -0700 | [diff] [blame] | 1706 | &cpi->ref_buffer_stack); |
Remya Prakasan | a68eaef | 2021-05-19 19:15:52 +0530 | [diff] [blame] | 1707 | |
| 1708 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1709 | // Make the frames marked as is_frame_non_ref to non-reference frames. |
| 1710 | if (gf_group->is_frame_non_ref[cpi->gf_frame_index]) |
| 1711 | frame_params.refresh_frame_flags = 0; |
| 1712 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1713 | |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1714 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1715 | frame_params.existing_fb_idx_to_show = INVALID_IDX; |
| 1716 | // Find the frame buffer to show based on display order. |
| 1717 | if (frame_params.show_existing_frame) { |
| 1718 | for (int frame = 0; frame < REF_FRAMES; frame++) { |
| 1719 | const RefCntBuffer *const buf = cm->ref_frame_map[frame]; |
| 1720 | if (buf == NULL) continue; |
| 1721 | const int frame_order = (int)buf->display_order_hint; |
| 1722 | if (frame_order == cur_frame_disp) |
| 1723 | frame_params.existing_fb_idx_to_show = frame; |
| 1724 | } |
| 1725 | } |
| 1726 | #else |
Jingning Han | a729393 | 2019-08-15 15:46:49 -0700 | [diff] [blame] | 1727 | frame_params.existing_fb_idx_to_show = |
| 1728 | frame_params.show_existing_frame |
Debargha Mukherjee | a2074dd | 2019-09-04 10:03:44 -0700 | [diff] [blame] | 1729 | ? (frame_update_type == INTNL_OVERLAY_UPDATE |
| 1730 | ? get_ref_frame_map_idx(cm, BWDREF_FRAME) |
| 1731 | : get_ref_frame_map_idx(cm, ALTREF_FRAME)) |
Jingning Han | a729393 | 2019-08-15 15:46:49 -0700 | [diff] [blame] | 1732 | : INVALID_IDX; |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1733 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1736 | // The way frame_params->remapped_ref_idx is setup is a placeholder. |
David Turner | f459229 | 2019-02-21 11:50:30 +0000 | [diff] [blame] | 1737 | // Currently, reference buffer assignment is done by update_ref_frame_map() |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 1738 | // which is called by high-level strategy AFTER encoding a frame. It |
| 1739 | // modifies cm->remapped_ref_idx. If you want to use an alternative method |
| 1740 | // to determine reference buffer assignment, just put your assignments into |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1741 | // frame_params->remapped_ref_idx here and they will be used when encoding |
| 1742 | // this frame. If frame_params->remapped_ref_idx is setup independently of |
David Turner | f459229 | 2019-02-21 11:50:30 +0000 | [diff] [blame] | 1743 | // cm->remapped_ref_idx then update_ref_frame_map() will have no effect. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1744 | memcpy(frame_params.remapped_ref_idx, cm->remapped_ref_idx, |
| 1745 | REF_FRAMES * sizeof(*cm->remapped_ref_idx)); |
| 1746 | |
Urvang Joshi | 69a986c | 2020-04-27 16:13:11 -0700 | [diff] [blame] | 1747 | cpi->td.mb.delta_qindex = 0; |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1748 | |
| 1749 | if (!frame_params.show_existing_frame) { |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame] | 1750 | cm->quant_params.using_qmatrix = oxcf->q_cfg.using_qm; |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1751 | } |
Marco Paniconi | 34b0dd0 | 2020-07-29 16:41:41 -0700 | [diff] [blame] | 1752 | |
Aasaipriya Chandran | ad28124 | 2021-05-25 20:25:39 +0530 | [diff] [blame] | 1753 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1754 | // Copy previous frame's largest MV component from ppi to cpi. |
| 1755 | if (!is_stat_generation_stage(cpi) && cpi->do_frame_data_update) |
| 1756 | cpi->mv_search_params.max_mv_magnitude = cpi->ppi->max_mv_magnitude; |
| 1757 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
| 1758 | |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1759 | #if CONFIG_REALTIME_ONLY |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1760 | if (av1_encode(cpi, dest, &frame_input, &frame_params, &frame_results) != |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1761 | AOM_CODEC_OK) { |
| 1762 | return AOM_CODEC_ERROR; |
| 1763 | } |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1764 | #else |
Marco Paniconi | eca1195 | 2020-05-06 11:30:48 -0700 | [diff] [blame] | 1765 | if (has_no_stats_stage(cpi) && oxcf->mode == REALTIME && |
Vishesh | 5b50e6d | 2020-06-10 19:20:07 +0530 | [diff] [blame] | 1766 | gf_cfg->lag_in_frames == 0) { |
Marco Paniconi | eca1195 | 2020-05-06 11:30:48 -0700 | [diff] [blame] | 1767 | if (av1_encode(cpi, dest, &frame_input, &frame_params, &frame_results) != |
| 1768 | AOM_CODEC_OK) { |
| 1769 | return AOM_CODEC_ERROR; |
| 1770 | } |
| 1771 | } else if (denoise_and_encode(cpi, dest, &frame_input, &frame_params, |
| 1772 | &frame_results) != AOM_CODEC_OK) { |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1773 | return AOM_CODEC_ERROR; |
| 1774 | } |
| 1775 | #endif // CONFIG_REALTIME_ONLY |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1776 | |
Aasaipriya Chandran | ad28124 | 2021-05-25 20:25:39 +0530 | [diff] [blame] | 1777 | #if CONFIG_FRAME_PARALLEL_ENCODE |
| 1778 | // Store current frame's largest MV component in ppi. |
| 1779 | if (!is_stat_generation_stage(cpi) && cpi->do_frame_data_update) |
| 1780 | cpi->ppi->max_mv_magnitude = cpi->mv_search_params.max_mv_magnitude; |
| 1781 | #endif |
| 1782 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1783 | if (!is_stat_generation_stage(cpi)) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1784 | // First pass doesn't modify reference buffer assignment or produce frame |
| 1785 | // flags |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 1786 | update_frame_flags(&cpi->common, &cpi->refresh_frame, frame_flags); |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1787 | set_additional_frame_flags(cm, frame_flags); |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1788 | #if !CONFIG_FRAME_PARALLEL_ENCODE |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1789 | if (!ext_flags->refresh_frame.update_pending) { |
Marco Paniconi | 8d335b7 | 2019-08-06 09:07:07 -0700 | [diff] [blame] | 1790 | int ref_map_index = |
| 1791 | av1_get_refresh_ref_frame_map(cm->current_frame.refresh_frame_flags); |
Jingning Han | 1770136 | 2021-06-19 00:10:42 -0700 | [diff] [blame] | 1792 | av1_update_ref_frame_map(cpi, frame_update_type, |
| 1793 | gf_group->refbuf_state[cpi->gf_frame_index], |
| 1794 | ref_map_index, &cpi->ref_buffer_stack); |
Marco Paniconi | 8d335b7 | 2019-08-06 09:07:07 -0700 | [diff] [blame] | 1795 | } |
Remya Prakasan | 5ba0bfe | 2021-01-28 15:19:19 +0530 | [diff] [blame] | 1796 | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1799 | #if !CONFIG_REALTIME_ONLY |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1800 | #if TXCOEFF_COST_TIMER |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1801 | if (!is_stat_generation_stage(cpi)) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1802 | cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer; |
| 1803 | fprintf(stderr, |
| 1804 | "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld " |
| 1805 | "in us\n", |
| 1806 | cm->txcoeff_cost_count, cm->txcoeff_cost_timer, |
| 1807 | cm->cum_txcoeff_cost_timer); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1808 | } |
Tarundeep Singh | 27b80f0 | 2021-05-31 19:27:59 +0530 | [diff] [blame] | 1809 | #endif |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1810 | #endif // !CONFIG_REALTIME_ONLY |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1811 | |
Sai Deng | aff2772 | 2020-08-31 12:06:09 -0700 | [diff] [blame] | 1812 | #if CONFIG_TUNE_VMAF |
| 1813 | if (!is_stat_generation_stage(cpi) && |
| 1814 | (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING && |
| 1815 | oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN)) { |
Sai Deng | 282fad4 | 2020-09-17 15:24:29 -0700 | [diff] [blame] | 1816 | av1_update_vmaf_curve(cpi); |
Sai Deng | aff2772 | 2020-08-31 12:06:09 -0700 | [diff] [blame] | 1817 | } |
| 1818 | #endif |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1819 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1820 | // Unpack frame_results: |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1821 | *size = frame_results.size; |
| 1822 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1823 | // Leave a signal for a higher level caller about if this frame is droppable |
| 1824 | if (*size > 0) { |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1825 | cpi->droppable = is_frame_droppable(&cpi->svc, &ext_flags->refresh_frame); |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1828 | return AOM_CODEC_OK; |
| 1829 | } |