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 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 14 | #include "config/aom_config.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 15 | #include "config/aom_scale_rtcd.h" |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 16 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 17 | #include "aom/aom_codec.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 18 | #include "aom/aom_encoder.h" |
| 19 | |
| 20 | #include "aom_ports/system_state.h" |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 21 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 22 | #if CONFIG_MISMATCH_DEBUG |
| 23 | #include "aom_util/debug_util.h" |
| 24 | #endif // CONFIG_MISMATCH_DEBUG |
| 25 | |
Wan-Teh Chang | f2d15ee | 2020-03-10 09:24:43 -0700 | [diff] [blame] | 26 | #include "av1/common/av1_common_int.h" |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 27 | #include "av1/common/reconinter.h" |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 28 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 29 | #include "av1/encoder/encoder.h" |
| 30 | #include "av1/encoder/encode_strategy.h" |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 31 | #include "av1/encoder/encodeframe.h" |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 32 | #include "av1/encoder/firstpass.h" |
David Turner | 0fa8c49 | 2019-02-06 16:38:13 +0000 | [diff] [blame] | 33 | #include "av1/encoder/pass2_strategy.h" |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 34 | #include "av1/encoder/temporal_filter.h" |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 35 | #include "av1/encoder/tpl_model.h" |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 36 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 37 | #define TEMPORAL_FILTER_KEY_FRAME (CONFIG_REALTIME_ONLY ? 0 : 1) |
| 38 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 39 | static INLINE void set_refresh_frame_flags( |
| 40 | RefreshFrameFlagsInfo *const refresh_frame_flags, bool refresh_gf, |
| 41 | bool refresh_bwdref, bool refresh_arf) { |
| 42 | refresh_frame_flags->golden_frame = refresh_gf; |
| 43 | refresh_frame_flags->bwd_ref_frame = refresh_bwdref; |
| 44 | refresh_frame_flags->alt_ref_frame = refresh_arf; |
| 45 | } |
| 46 | |
| 47 | void av1_configure_buffer_updates( |
| 48 | AV1_COMP *const cpi, RefreshFrameFlagsInfo *const refresh_frame_flags, |
| 49 | const FRAME_UPDATE_TYPE type, int force_refresh_all) { |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 50 | // NOTE(weitinglin): Should we define another function to take care of |
| 51 | // cpi->rc.is_$Source_Type to make this function as it is in the comment? |
| 52 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 53 | const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
| 54 | &cpi->ext_flags.refresh_frame; |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 55 | cpi->rc.is_src_frame_alt_ref = 0; |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 56 | |
| 57 | switch (type) { |
| 58 | case KF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 59 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 60 | break; |
| 61 | |
| 62 | case LF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 63 | set_refresh_frame_flags(refresh_frame_flags, false, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 64 | break; |
| 65 | |
| 66 | case GF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 67 | set_refresh_frame_flags(refresh_frame_flags, true, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 68 | break; |
| 69 | |
| 70 | case OVERLAY_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 71 | set_refresh_frame_flags(refresh_frame_flags, true, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 72 | cpi->rc.is_src_frame_alt_ref = 1; |
| 73 | break; |
| 74 | |
| 75 | case ARF_UPDATE: |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 76 | // NOTE: BWDREF does not get updated along with ALTREF_FRAME. |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 77 | set_refresh_frame_flags(refresh_frame_flags, false, false, true); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 78 | break; |
| 79 | |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 80 | case INTNL_OVERLAY_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 81 | set_refresh_frame_flags(refresh_frame_flags, false, false, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 82 | cpi->rc.is_src_frame_alt_ref = 1; |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 83 | break; |
| 84 | |
| 85 | case INTNL_ARF_UPDATE: |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 86 | set_refresh_frame_flags(refresh_frame_flags, false, true, false); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 87 | break; |
| 88 | |
| 89 | default: assert(0); break; |
| 90 | } |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 91 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 92 | if (ext_refresh_frame_flags->update_pending && |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 93 | (!is_stat_generation_stage(cpi))) |
| 94 | set_refresh_frame_flags(refresh_frame_flags, |
| 95 | ext_refresh_frame_flags->golden_frame, |
| 96 | ext_refresh_frame_flags->bwd_ref_frame, |
| 97 | ext_refresh_frame_flags->alt_ref_frame); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 98 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 99 | if (force_refresh_all) |
| 100 | set_refresh_frame_flags(refresh_frame_flags, true, true, true); |
David Turner | ce9b590 | 2019-01-23 17:25:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 103 | static void set_additional_frame_flags(const AV1_COMMON *const cm, |
| 104 | unsigned int *const frame_flags) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 105 | if (frame_is_intra_only(cm)) { |
| 106 | *frame_flags |= FRAMEFLAGS_INTRAONLY; |
| 107 | } |
| 108 | if (frame_is_sframe(cm)) { |
| 109 | *frame_flags |= FRAMEFLAGS_SWITCH; |
| 110 | } |
| 111 | if (cm->features.error_resilient_mode) { |
| 112 | *frame_flags |= FRAMEFLAGS_ERROR_RESILIENT; |
| 113 | } |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static INLINE void update_keyframe_counters(AV1_COMP *cpi) { |
Urvang Joshi | 6ce7fbc | 2019-03-22 15:24:21 -0700 | [diff] [blame] | 117 | if (cpi->common.show_frame) { |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 118 | if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref || |
| 119 | cpi->common.current_frame.frame_type == KEY_FRAME) { |
| 120 | // If this is a show_existing_frame with a source other than altref, |
| 121 | // or if it is not a displayed forward keyframe, the keyframe update |
| 122 | // counters were incremented when it was originally encoded. |
| 123 | cpi->rc.frames_since_key++; |
| 124 | cpi->rc.frames_to_key--; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 129 | static INLINE int is_frame_droppable( |
| 130 | const SVC *const svc, |
| 131 | const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags) { |
Jingning Han | 98d20de | 2019-08-14 12:59:47 -0700 | [diff] [blame] | 132 | // Droppable frame is only used by external refresh flags. VoD setting won't |
| 133 | // trigger its use case. |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 134 | if (svc->external_ref_frame_config) |
| 135 | return svc->non_reference_frame; |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 136 | else if (ext_refresh_frame_flags->update_pending) |
| 137 | return !(ext_refresh_frame_flags->alt_ref_frame || |
| 138 | ext_refresh_frame_flags->alt2_ref_frame || |
| 139 | ext_refresh_frame_flags->bwd_ref_frame || |
| 140 | ext_refresh_frame_flags->golden_frame || |
| 141 | ext_refresh_frame_flags->last_frame); |
Marco Paniconi | 91a5002 | 2019-08-01 18:56:39 -0700 | [diff] [blame] | 142 | else |
Jingning Han | 98d20de | 2019-08-14 12:59:47 -0700 | [diff] [blame] | 143 | return 0; |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static INLINE void update_frames_till_gf_update(AV1_COMP *cpi) { |
| 147 | // TODO(weitinglin): Updating this counter for is_frame_droppable |
| 148 | // is a work-around to handle the condition when a frame is drop. |
| 149 | // We should fix the cpi->common.show_frame flag |
| 150 | // instead of checking the other condition to update the counter properly. |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 151 | if (cpi->common.show_frame || |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 152 | is_frame_droppable(&cpi->svc, &cpi->ext_flags.refresh_frame)) { |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 153 | // Decrement count down till next gf |
| 154 | if (cpi->rc.frames_till_gf_update_due > 0) |
| 155 | cpi->rc.frames_till_gf_update_due--; |
| 156 | } |
| 157 | } |
| 158 | |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 159 | static INLINE void update_gf_group_index(AV1_COMP *cpi) { |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 160 | // Increment the gf group index ready for the next frame. If this is |
| 161 | // a show_existing_frame with a source other than altref, or if it is not |
| 162 | // a displayed forward keyframe, the index was incremented when it was |
| 163 | // originally encoded. |
| 164 | if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref || |
| 165 | cpi->common.current_frame.frame_type == KEY_FRAME) { |
Sarah Parker | e1b2201 | 2019-06-06 16:35:25 -0700 | [diff] [blame] | 166 | ++cpi->gf_group.index; |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | static void update_rc_counts(AV1_COMP *cpi) { |
| 171 | update_keyframe_counters(cpi); |
| 172 | update_frames_till_gf_update(cpi); |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 173 | update_gf_group_index(cpi); |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 176 | static void set_ext_overrides(AV1_COMMON *const cm, |
| 177 | EncodeFrameParams *const frame_params, |
| 178 | ExternalFlags *const ext_flags) { |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 179 | // Overrides the defaults with the externally supplied values with |
| 180 | // av1_update_reference() and av1_update_entropy() calls |
| 181 | // Note: The overrides are valid only for the next frame passed |
| 182 | // to av1_encode_lowlevel() |
| 183 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 184 | if (ext_flags->use_s_frame) { |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 185 | frame_params->frame_type = S_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 186 | } |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 187 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 188 | if (ext_flags->refresh_frame_context_pending) { |
| 189 | cm->features.refresh_frame_context = ext_flags->refresh_frame_context; |
| 190 | ext_flags->refresh_frame_context_pending = 0; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 191 | } |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 192 | cm->features.allow_ref_frame_mvs = ext_flags->use_ref_frame_mvs; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 193 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 194 | frame_params->error_resilient_mode = ext_flags->use_error_resilient; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 195 | // A keyframe is already error resilient and keyframes with |
| 196 | // error_resilient_mode interferes with the use of show_existing_frame |
| 197 | // when forward reference keyframes are enabled. |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 198 | frame_params->error_resilient_mode &= frame_params->frame_type != KEY_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 199 | // For bitstream conformance, s-frames must be error-resilient |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 200 | frame_params->error_resilient_mode |= frame_params->frame_type == S_FRAME; |
David Turner | 07dbd8e | 2019-01-08 17:16:25 +0000 | [diff] [blame] | 201 | } |
| 202 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 203 | static int get_current_frame_ref_type( |
| 204 | const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 205 | // We choose the reference "type" of this frame from the flags which indicate |
Urvang Joshi | 2e4aaf2 | 2019-05-08 11:38:00 -0700 | [diff] [blame] | 206 | // which reference frames will be refreshed by it. More than one of these |
| 207 | // flags may be set, so the order here implies an order of precedence. This is |
| 208 | // just used to choose the primary_ref_frame (as the most recent reference |
| 209 | // buffer of the same reference-type as the current frame) |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 210 | |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 211 | (void)frame_params; |
| 212 | // TODO(jingning): This table should be a lot simpler with the new |
| 213 | // ARF system in place. Keep frame_params for the time being as we are |
| 214 | // still evaluating a few design options. |
| 215 | switch (cpi->gf_group.layer_depth[cpi->gf_group.index]) { |
| 216 | case 0: return 0; |
| 217 | case 1: return 1; |
| 218 | case MAX_ARF_LAYERS: |
| 219 | case MAX_ARF_LAYERS + 1: return 4; |
| 220 | default: return 7; |
| 221 | } |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int choose_primary_ref_frame( |
| 225 | const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) { |
| 226 | const AV1_COMMON *const cm = &cpi->common; |
| 227 | |
| 228 | const int intra_only = frame_params->frame_type == KEY_FRAME || |
| 229 | frame_params->frame_type == INTRA_ONLY_FRAME; |
Marco Paniconi | 5b1e473 | 2019-08-08 18:57:53 -0700 | [diff] [blame] | 230 | if (intra_only || frame_params->error_resilient_mode || cpi->use_svc || |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 231 | cpi->ext_flags.use_primary_ref_none) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 232 | return PRIMARY_REF_NONE; |
| 233 | } |
| 234 | |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 235 | // In large scale case, always use Last frame's frame contexts. |
| 236 | // Note(yunqing): In other cases, primary_ref_frame is chosen based on |
| 237 | // cpi->gf_group.layer_depth[cpi->gf_group.index], which also controls |
| 238 | // frame bit allocation. |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 239 | if (cm->tiles.large_scale) return (LAST_FRAME - LAST_FRAME); |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 240 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 241 | // Find the most recent reference frame with the same reference type as the |
| 242 | // current frame |
Jingning Han | 6d87891 | 2020-02-28 10:23:35 -0800 | [diff] [blame] | 243 | const int current_ref_type = get_current_frame_ref_type(cpi, frame_params); |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 244 | int wanted_fb = cpi->fb_of_context_type[current_ref_type]; |
| 245 | |
| 246 | int primary_ref_frame = PRIMARY_REF_NONE; |
| 247 | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { |
| 248 | if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) { |
| 249 | primary_ref_frame = ref_frame - LAST_FRAME; |
| 250 | } |
| 251 | } |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 252 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 253 | return primary_ref_frame; |
| 254 | } |
| 255 | |
| 256 | static void update_fb_of_context_type( |
| 257 | const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params, |
| 258 | int *const fb_of_context_type) { |
| 259 | const AV1_COMMON *const cm = &cpi->common; |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 260 | const int current_frame_ref_type = |
| 261 | get_current_frame_ref_type(cpi, frame_params); |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 262 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 263 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 264 | cpi->ext_flags.use_primary_ref_none) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 265 | for (int i = 0; i < REF_FRAMES; i++) { |
| 266 | fb_of_context_type[i] = -1; |
| 267 | } |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 268 | fb_of_context_type[current_frame_ref_type] = |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 269 | cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME) |
| 270 | : get_ref_frame_map_idx(cm, ALTREF_FRAME); |
| 271 | } |
| 272 | |
| 273 | if (!encode_show_existing_frame(cm)) { |
| 274 | // Refresh fb_of_context_type[]: see encoder.h for explanation |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 275 | if (cm->current_frame.frame_type == KEY_FRAME) { |
| 276 | // All ref frames are refreshed, pick one that will live long enough |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 277 | fb_of_context_type[current_frame_ref_type] = 0; |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 278 | } else { |
| 279 | // If more than one frame is refreshed, it doesn't matter which one we |
| 280 | // pick so pick the first. LST sometimes doesn't refresh any: this is ok |
Jingning Han | 370d116 | 2019-07-03 10:24:03 -0700 | [diff] [blame] | 281 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 282 | for (int i = 0; i < REF_FRAMES; i++) { |
| 283 | if (cm->current_frame.refresh_frame_flags & (1 << i)) { |
| 284 | fb_of_context_type[current_frame_ref_type] = i; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 292 | static int get_order_offset(const GF_GROUP *const gf_group, |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 293 | const EncodeFrameParams *const frame_params) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 294 | // shown frame by definition has order offset 0 |
| 295 | // show_existing_frame ignores order_offset and simply takes the order_hint |
| 296 | // from the reference frame being shown. |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 297 | if (frame_params->show_frame || frame_params->show_existing_frame) return 0; |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 298 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 299 | const int arf_offset = |
| 300 | AOMMIN((MAX_GF_INTERVAL - 1), gf_group->arf_src_offset[gf_group->index]); |
Urvang Joshi | 6ce7fbc | 2019-03-22 15:24:21 -0700 | [diff] [blame] | 301 | return AOMMIN((MAX_GF_INTERVAL - 1), arf_offset); |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 304 | static void adjust_frame_rate(AV1_COMP *cpi, int64_t ts_start, int64_t ts_end) { |
| 305 | TimeStamps *time_stamps = &cpi->time_stamps; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 306 | int64_t this_duration; |
| 307 | int step = 0; |
| 308 | |
| 309 | // Clear down mmx registers |
| 310 | aom_clear_system_state(); |
| 311 | |
Marco Paniconi | 6397132 | 2019-08-15 21:32:05 -0700 | [diff] [blame] | 312 | if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) { |
| 313 | cpi->framerate = cpi->svc.base_framerate; |
| 314 | av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height); |
| 315 | return; |
| 316 | } |
| 317 | |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 318 | if (ts_start == time_stamps->first_ever) { |
| 319 | this_duration = ts_end - ts_start; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 320 | step = 1; |
| 321 | } else { |
| 322 | int64_t last_duration = |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 323 | time_stamps->prev_end_seen - time_stamps->prev_start_seen; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 324 | |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 325 | this_duration = ts_end - time_stamps->prev_end_seen; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 326 | |
| 327 | // do a step update if the duration changes by 10% |
| 328 | if (last_duration) |
| 329 | step = (int)((this_duration - last_duration) * 10 / last_duration); |
| 330 | } |
| 331 | |
| 332 | if (this_duration) { |
| 333 | if (step) { |
| 334 | av1_new_framerate(cpi, 10000000.0 / this_duration); |
| 335 | } else { |
| 336 | // Average this frame's rate into the last second's average |
| 337 | // frame rate. If we haven't seen 1 second yet, then average |
| 338 | // over the whole interval seen. |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 339 | const double interval = |
| 340 | AOMMIN((double)(ts_end - time_stamps->first_ever), 10000000.0); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 341 | double avg_duration = 10000000.0 / cpi->framerate; |
| 342 | avg_duration *= (interval - avg_duration + this_duration); |
| 343 | avg_duration /= interval; |
| 344 | |
| 345 | av1_new_framerate(cpi, 10000000.0 / avg_duration); |
| 346 | } |
| 347 | } |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 348 | time_stamps->prev_start_seen = ts_start; |
| 349 | time_stamps->prev_end_seen = ts_end; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Urvang Joshi | f70375a | 2019-03-22 23:30:19 -0700 | [diff] [blame] | 352 | // If this is an alt-ref, returns the offset of the source frame used |
| 353 | // as the arf midpoint. Otherwise, returns 0. |
Jingning Han | f39a1ea | 2019-08-16 10:43:08 -0700 | [diff] [blame] | 354 | static int get_arf_src_index(GF_GROUP *gf_group, int pass) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 355 | int arf_src_index = 0; |
Jingning Han | f39a1ea | 2019-08-16 10:43:08 -0700 | [diff] [blame] | 356 | if (pass != 1) arf_src_index = gf_group->arf_src_offset[gf_group->index]; |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 357 | return arf_src_index; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Called if this frame is an ARF or ARF2. Also handles forward-keyframes |
| 361 | // For an ARF set arf2=0, for ARF2 set arf2=1 |
| 362 | // temporal_filtered is set to 1 if we temporally filter the ARF frame, so that |
| 363 | // the correct post-filter buffer can be used. |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 364 | static struct lookahead_entry *setup_arf_frame( |
| 365 | AV1_COMP *const cpi, const int arf_src_index, int *code_arf, |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 366 | EncodeFrameParams *const frame_params, int *show_existing_alt_ref) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 367 | AV1_COMMON *const cm = &cpi->common; |
| 368 | RATE_CONTROL *const rc = &cpi->rc; |
Jerome Jiang | 7164747 | 2019-05-30 12:01:57 -0700 | [diff] [blame] | 369 | #if !CONFIG_REALTIME_ONLY |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 370 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
Jerome Jiang | 7164747 | 2019-05-30 12:01:57 -0700 | [diff] [blame] | 371 | #endif |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 372 | |
| 373 | assert(arf_src_index <= rc->frames_to_key); |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 374 | *code_arf = 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 375 | |
| 376 | struct lookahead_entry *source = |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 377 | av1_lookahead_peek(cpi->lookahead, arf_src_index, cpi->compressor_stage); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 378 | |
| 379 | if (source != NULL) { |
| 380 | cm->showable_frame = 1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 381 | |
| 382 | // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 383 | if (arf_src_index == rc->frames_to_key) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 384 | // Skip temporal filtering and mark as intra_only if we have a fwd_kf |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 385 | cpi->no_show_kf = 1; |
| 386 | } else { |
Jerome Jiang | 7164747 | 2019-05-30 12:01:57 -0700 | [diff] [blame] | 387 | #if !CONFIG_REALTIME_ONLY |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 388 | if (oxcf->arnr_max_frames > 0) { |
| 389 | // Produce the filtered ARF frame. |
Yunqing Wang | 1973f11 | 2019-10-18 15:50:04 -0700 | [diff] [blame] | 390 | cm->current_frame.frame_type = INTER_FRAME; |
| 391 | FRAME_UPDATE_TYPE frame_update_type = |
| 392 | get_frame_update_type(&cpi->gf_group); |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 393 | av1_configure_buffer_updates(cpi, &frame_params->refresh_frame, |
| 394 | frame_update_type, 0); |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 395 | *code_arf = |
| 396 | av1_temporal_filter(cpi, arf_src_index, show_existing_alt_ref); |
Yunqing Wang | b0d52fa | 2019-10-10 13:09:38 -0700 | [diff] [blame] | 397 | if (*code_arf) { |
| 398 | aom_extend_frame_borders(&cpi->alt_ref_buffer, av1_num_planes(cm)); |
| 399 | } |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 400 | } |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 401 | #else |
| 402 | (void)show_existing_alt_ref; |
Jerome Jiang | 7164747 | 2019-05-30 12:01:57 -0700 | [diff] [blame] | 403 | #endif |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 404 | } |
| 405 | frame_params->show_frame = 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 406 | } |
| 407 | rc->source_alt_ref_pending = 0; |
| 408 | return source; |
| 409 | } |
| 410 | |
| 411 | // Determine whether there is a forced keyframe pending in the lookahead buffer |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 412 | int is_forced_keyframe_pending(struct lookahead_ctx *lookahead, |
| 413 | const int up_to_index, |
| 414 | const COMPRESSOR_STAGE compressor_stage) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 415 | for (int i = 0; i <= up_to_index; i++) { |
Mufaddal Chakera | 73b7b70 | 2019-12-09 11:44:55 +0530 | [diff] [blame] | 416 | const struct lookahead_entry *e = |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 417 | av1_lookahead_peek(lookahead, i, compressor_stage); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 418 | if (e == NULL) { |
| 419 | // We have reached the end of the lookahead buffer and not early-returned |
| 420 | // so there isn't a forced key-frame pending. |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 421 | return -1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 422 | } else if (e->flags == AOM_EFLAG_FORCE_KF) { |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 423 | return (i + 1); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 424 | } else { |
| 425 | continue; |
| 426 | } |
| 427 | } |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 428 | return -1; // Never reached |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Urvang Joshi | f70375a | 2019-03-22 23:30:19 -0700 | [diff] [blame] | 431 | // 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] | 432 | // Do some setup associated with the chosen source |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 433 | // temporal_filtered, flush, and frame_update_type are outputs. |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 434 | // Return the frame source, or NULL if we couldn't find one |
Yaowu Xu | fac3d86 | 2019-04-26 15:43:03 -0700 | [diff] [blame] | 435 | static struct lookahead_entry *choose_frame_source( |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 436 | AV1_COMP *const cpi, int *const code_arf, int *const flush, |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 437 | struct lookahead_entry **last_source, EncodeFrameParams *const frame_params, |
| 438 | int *show_existing_alt_ref) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 439 | AV1_COMMON *const cm = &cpi->common; |
| 440 | struct lookahead_entry *source = NULL; |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 441 | *code_arf = 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 442 | |
| 443 | // Should we encode an alt-ref frame. |
Jingning Han | f39a1ea | 2019-08-16 10:43:08 -0700 | [diff] [blame] | 444 | int arf_src_index = get_arf_src_index(&cpi->gf_group, cpi->oxcf.pass); |
Aasaipriya | 9bc1dcb | 2020-03-13 17:46:07 +0530 | [diff] [blame] | 445 | // TODO(Aasaipriya): Forced key frames need to be fixed when rc_mode != AOM_Q |
| 446 | if (arf_src_index && |
| 447 | (is_forced_keyframe_pending(cpi->lookahead, arf_src_index, |
| 448 | cpi->compressor_stage) != -1) && |
| 449 | cpi->oxcf.rc_mode != AOM_Q) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 450 | arf_src_index = 0; |
| 451 | *flush = 1; |
| 452 | } |
| 453 | |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 454 | if (arf_src_index) |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 455 | source = setup_arf_frame(cpi, arf_src_index, code_arf, frame_params, |
| 456 | show_existing_alt_ref); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 457 | |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 458 | if (!source) { |
| 459 | // Get last frame source. |
| 460 | if (cm->current_frame.frame_number > 0) { |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 461 | *last_source = |
| 462 | av1_lookahead_peek(cpi->lookahead, -1, cpi->compressor_stage); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 463 | } |
| 464 | // Read in the source frame. |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 465 | source = av1_lookahead_pop(cpi->lookahead, *flush, cpi->compressor_stage); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 466 | if (source == NULL) return NULL; |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 467 | frame_params->show_frame = 1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 468 | } |
| 469 | return source; |
| 470 | } |
| 471 | |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 472 | // Don't allow a show_existing_frame to coincide with an error resilient or |
| 473 | // S-Frame. An exception can be made in the case of a keyframe, since it does |
| 474 | // not depend on any previous frames. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 475 | static int allow_show_existing(const AV1_COMP *const cpi, |
| 476 | unsigned int frame_flags) { |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 477 | if (cpi->common.current_frame.frame_number == 0) return 0; |
| 478 | |
| 479 | const struct lookahead_entry *lookahead_src = |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 480 | av1_lookahead_peek(cpi->lookahead, 0, cpi->compressor_stage); |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 481 | if (lookahead_src == NULL) return 1; |
| 482 | |
| 483 | const int is_error_resilient = |
| 484 | cpi->oxcf.error_resilient_mode || |
| 485 | (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT); |
| 486 | const int is_s_frame = |
| 487 | cpi->oxcf.s_frame_mode || (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME); |
| 488 | const int is_key_frame = |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 489 | (cpi->rc.frames_to_key == 0) || (frame_flags & FRAMEFLAGS_KEY); |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 490 | return !(is_error_resilient || is_s_frame) || is_key_frame; |
| 491 | } |
| 492 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 493 | // Update frame_flags to tell the encoder's caller what sort of frame was |
| 494 | // encoded. |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 495 | static void update_frame_flags( |
| 496 | const AV1_COMMON *const cm, |
| 497 | const RefreshFrameFlagsInfo *const refresh_frame_flags, |
| 498 | unsigned int *frame_flags) { |
| 499 | if (encode_show_existing_frame(cm)) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 500 | *frame_flags &= ~FRAMEFLAGS_GOLDEN; |
| 501 | *frame_flags &= ~FRAMEFLAGS_BWDREF; |
| 502 | *frame_flags &= ~FRAMEFLAGS_ALTREF; |
| 503 | *frame_flags &= ~FRAMEFLAGS_KEY; |
| 504 | return; |
| 505 | } |
| 506 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 507 | if (refresh_frame_flags->golden_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 508 | *frame_flags |= FRAMEFLAGS_GOLDEN; |
| 509 | } else { |
| 510 | *frame_flags &= ~FRAMEFLAGS_GOLDEN; |
| 511 | } |
| 512 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 513 | if (refresh_frame_flags->alt_ref_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 514 | *frame_flags |= FRAMEFLAGS_ALTREF; |
| 515 | } else { |
| 516 | *frame_flags &= ~FRAMEFLAGS_ALTREF; |
| 517 | } |
| 518 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 519 | if (refresh_frame_flags->bwd_ref_frame) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 520 | *frame_flags |= FRAMEFLAGS_BWDREF; |
| 521 | } else { |
| 522 | *frame_flags &= ~FRAMEFLAGS_BWDREF; |
| 523 | } |
| 524 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 525 | if (cm->current_frame.frame_type == KEY_FRAME) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 526 | *frame_flags |= FRAMEFLAGS_KEY; |
| 527 | } else { |
| 528 | *frame_flags &= ~FRAMEFLAGS_KEY; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | #define DUMP_REF_FRAME_IMAGES 0 |
| 533 | |
| 534 | #if DUMP_REF_FRAME_IMAGES == 1 |
| 535 | static int dump_one_image(AV1_COMMON *cm, |
| 536 | const YV12_BUFFER_CONFIG *const ref_buf, |
| 537 | char *file_name) { |
| 538 | int h; |
| 539 | FILE *f_ref = NULL; |
| 540 | |
| 541 | if (ref_buf == NULL) { |
| 542 | printf("Frame data buffer is NULL.\n"); |
| 543 | return AOM_CODEC_MEM_ERROR; |
| 544 | } |
| 545 | |
| 546 | if ((f_ref = fopen(file_name, "wb")) == NULL) { |
| 547 | printf("Unable to open file %s to write.\n", file_name); |
| 548 | return AOM_CODEC_MEM_ERROR; |
| 549 | } |
| 550 | |
| 551 | // --- Y --- |
| 552 | for (h = 0; h < cm->height; ++h) { |
| 553 | fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref); |
| 554 | } |
| 555 | // --- U --- |
| 556 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 557 | fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1), |
| 558 | f_ref); |
| 559 | } |
| 560 | // --- V --- |
| 561 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 562 | fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1), |
| 563 | f_ref); |
| 564 | } |
| 565 | |
| 566 | fclose(f_ref); |
| 567 | |
| 568 | return AOM_CODEC_OK; |
| 569 | } |
| 570 | |
| 571 | static void dump_ref_frame_images(AV1_COMP *cpi) { |
| 572 | AV1_COMMON *const cm = &cpi->common; |
| 573 | MV_REFERENCE_FRAME ref_frame; |
| 574 | |
| 575 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 576 | char file_name[256] = ""; |
| 577 | snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv", |
| 578 | cm->current_frame.frame_number, ref_frame); |
| 579 | dump_one_image(cm, get_ref_frame_yv12_buf(cpi, ref_frame), file_name); |
| 580 | } |
| 581 | } |
| 582 | #endif // DUMP_REF_FRAME_IMAGES == 1 |
| 583 | |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 584 | int av1_get_refresh_ref_frame_map(int refresh_frame_flags) { |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 585 | int ref_map_index = INVALID_IDX; |
| 586 | |
| 587 | for (ref_map_index = 0; ref_map_index < REF_FRAMES; ++ref_map_index) |
| 588 | if ((refresh_frame_flags >> ref_map_index) & 1) break; |
| 589 | |
| 590 | return ref_map_index; |
| 591 | } |
| 592 | |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 593 | static void update_arf_stack(int ref_map_index, |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 594 | RefBufferStack *ref_buffer_stack) { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 595 | if (ref_buffer_stack->arf_stack_size >= 0) { |
| 596 | if (ref_buffer_stack->arf_stack[0] == ref_map_index) |
| 597 | 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] | 598 | } |
| 599 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 600 | if (ref_buffer_stack->lst_stack_size) { |
| 601 | for (int i = ref_buffer_stack->lst_stack_size - 1; i >= 0; --i) { |
| 602 | if (ref_buffer_stack->lst_stack[i] == ref_map_index) { |
| 603 | for (int idx = i; idx < ref_buffer_stack->lst_stack_size - 1; ++idx) |
| 604 | ref_buffer_stack->lst_stack[idx] = |
| 605 | ref_buffer_stack->lst_stack[idx + 1]; |
Cheng Chen | c86d2bf | 2019-09-05 15:07:47 -0700 | [diff] [blame] | 606 | ref_buffer_stack->lst_stack[ref_buffer_stack->lst_stack_size - 1] = |
| 607 | INVALID_IDX; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 608 | --ref_buffer_stack->lst_stack_size; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 613 | if (ref_buffer_stack->gld_stack_size) { |
| 614 | for (int i = ref_buffer_stack->gld_stack_size - 1; i >= 0; --i) { |
| 615 | if (ref_buffer_stack->gld_stack[i] == ref_map_index) { |
| 616 | for (int idx = i; idx < ref_buffer_stack->gld_stack_size - 1; ++idx) |
| 617 | ref_buffer_stack->gld_stack[idx] = |
| 618 | ref_buffer_stack->gld_stack[idx + 1]; |
Cheng Chen | c86d2bf | 2019-09-05 15:07:47 -0700 | [diff] [blame] | 619 | ref_buffer_stack->gld_stack[ref_buffer_stack->gld_stack_size - 1] = |
| 620 | INVALID_IDX; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 621 | --ref_buffer_stack->gld_stack_size; |
Jingning Han | f58175c | 2019-07-07 15:02:00 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
Jingning Han | 647f2d1 | 2019-07-23 14:15:42 -0700 | [diff] [blame] | 627 | // Update reference frame stack info. |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 628 | void av1_update_ref_frame_map(AV1_COMP *cpi, |
| 629 | FRAME_UPDATE_TYPE frame_update_type, |
Sarah Parker | edffc65 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 630 | int show_existing_frame, int ref_map_index, |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 631 | RefBufferStack *ref_buffer_stack) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 632 | AV1_COMMON *const cm = &cpi->common; |
Jingning Han | 47aaf87 | 2019-07-21 14:56:32 -0700 | [diff] [blame] | 633 | // TODO(jingning): Consider the S-frame same as key frame for the |
| 634 | // reference frame tracking purpose. The logic might be better |
| 635 | // expressed than converting the frame update type. |
| 636 | if (frame_is_sframe(cm)) frame_update_type = KEY_FRAME; |
| 637 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 638 | if (is_frame_droppable(&cpi->svc, &cpi->ext_flags.refresh_frame)) return; |
Jingning Han | bcbbd8c | 2019-07-21 16:37:12 -0700 | [diff] [blame] | 639 | |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 640 | switch (frame_update_type) { |
| 641 | case KEY_FRAME: |
Sarah Parker | edffc65 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 642 | if (show_existing_frame) |
| 643 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 644 | &ref_buffer_stack->arf_stack_size); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 645 | stack_reset(ref_buffer_stack->lst_stack, |
| 646 | &ref_buffer_stack->lst_stack_size); |
| 647 | stack_reset(ref_buffer_stack->gld_stack, |
| 648 | &ref_buffer_stack->gld_stack_size); |
| 649 | stack_reset(ref_buffer_stack->arf_stack, |
| 650 | &ref_buffer_stack->arf_stack_size); |
| 651 | stack_push(ref_buffer_stack->gld_stack, &ref_buffer_stack->gld_stack_size, |
| 652 | ref_map_index); |
| 653 | break; |
| 654 | case GF_UPDATE: |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 655 | update_arf_stack(ref_map_index, ref_buffer_stack); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 656 | stack_push(ref_buffer_stack->gld_stack, &ref_buffer_stack->gld_stack_size, |
| 657 | ref_map_index); |
Fyodor Kyslov | 915b9b8 | 2019-11-11 11:53:03 -0800 | [diff] [blame] | 658 | // For nonrd_mode: update LAST as well on GF_UPDATE frame. |
chiyotsai | 56681b5 | 2019-12-16 11:15:25 -0800 | [diff] [blame] | 659 | if (cpi->sf.rt_sf.use_nonrd_pick_mode) |
Marco Paniconi | fa29e16 | 2019-08-05 18:08:04 -0700 | [diff] [blame] | 660 | stack_push(ref_buffer_stack->lst_stack, |
| 661 | &ref_buffer_stack->lst_stack_size, ref_map_index); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 662 | break; |
| 663 | case LF_UPDATE: |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 664 | update_arf_stack(ref_map_index, ref_buffer_stack); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 665 | stack_push(ref_buffer_stack->lst_stack, &ref_buffer_stack->lst_stack_size, |
| 666 | ref_map_index); |
| 667 | break; |
| 668 | case ARF_UPDATE: |
| 669 | case INTNL_ARF_UPDATE: |
Jingning Han | 4711eb1 | 2019-08-01 09:56:59 -0700 | [diff] [blame] | 670 | update_arf_stack(ref_map_index, ref_buffer_stack); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 671 | stack_push(ref_buffer_stack->arf_stack, &ref_buffer_stack->arf_stack_size, |
| 672 | ref_map_index); |
| 673 | break; |
| 674 | case OVERLAY_UPDATE: |
Jingning Han | 5738e03 | 2019-07-22 15:22:52 -0700 | [diff] [blame] | 675 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 676 | &ref_buffer_stack->arf_stack_size); |
| 677 | stack_push(ref_buffer_stack->gld_stack, &ref_buffer_stack->gld_stack_size, |
| 678 | ref_map_index); |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 679 | break; |
| 680 | case INTNL_OVERLAY_UPDATE: |
| 681 | ref_map_index = stack_pop(ref_buffer_stack->arf_stack, |
| 682 | &ref_buffer_stack->arf_stack_size); |
| 683 | stack_push(ref_buffer_stack->lst_stack, &ref_buffer_stack->lst_stack_size, |
| 684 | ref_map_index); |
| 685 | break; |
| 686 | default: assert(0 && "unknown type"); |
Jingning Han | cf6d325 | 2019-07-03 14:26:45 -0700 | [diff] [blame] | 687 | } |
Jingning Han | 84a7050 | 2019-07-19 11:38:14 -0700 | [diff] [blame] | 688 | return; |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 691 | static int get_free_ref_map_index(const RefBufferStack *ref_buffer_stack) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 692 | for (int idx = 0; idx < REF_FRAMES; ++idx) { |
| 693 | int is_free = 1; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 694 | for (int i = 0; i < ref_buffer_stack->arf_stack_size; ++i) { |
| 695 | if (ref_buffer_stack->arf_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 696 | is_free = 0; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 701 | for (int i = 0; i < ref_buffer_stack->lst_stack_size; ++i) { |
| 702 | if (ref_buffer_stack->lst_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 703 | is_free = 0; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 708 | for (int i = 0; i < ref_buffer_stack->gld_stack_size; ++i) { |
| 709 | if (ref_buffer_stack->gld_stack[i] == idx) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 710 | is_free = 0; |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (is_free) return idx; |
| 716 | } |
| 717 | return INVALID_IDX; |
| 718 | } |
| 719 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 720 | int av1_get_refresh_frame_flags(const AV1_COMP *const cpi, |
| 721 | const EncodeFrameParams *const frame_params, |
| 722 | FRAME_UPDATE_TYPE frame_update_type, |
| 723 | const RefBufferStack *const ref_buffer_stack) { |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 724 | const AV1_COMMON *const cm = &cpi->common; |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 725 | const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
| 726 | &cpi->ext_flags.refresh_frame; |
| 727 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 728 | const SVC *const svc = &cpi->svc; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 729 | // Switch frames and shown key-frames overwrite all reference slots |
| 730 | if ((frame_params->frame_type == KEY_FRAME && frame_params->show_frame) || |
| 731 | frame_params->frame_type == S_FRAME) |
| 732 | return 0xFF; |
| 733 | |
| 734 | // show_existing_frames don't actually send refresh_frame_flags so set the |
| 735 | // flags to 0 to keep things consistent. |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 736 | if (frame_params->show_existing_frame && |
| 737 | (!frame_params->error_resilient_mode || |
| 738 | frame_params->frame_type == KEY_FRAME)) { |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 742 | if (is_frame_droppable(svc, ext_refresh_frame_flags)) return 0; |
Jingning Han | bcbbd8c | 2019-07-21 16:37:12 -0700 | [diff] [blame] | 743 | |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 744 | int refresh_mask = 0; |
| 745 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 746 | if (ext_refresh_frame_flags->update_pending) { |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 747 | if (svc->external_ref_frame_config) { |
Marco Paniconi | d8574e3 | 2019-08-04 21:30:12 -0700 | [diff] [blame] | 748 | for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 749 | int ref_frame_map_idx = svc->ref_idx[i]; |
| 750 | refresh_mask |= svc->refresh[ref_frame_map_idx] << ref_frame_map_idx; |
Marco Paniconi | d8574e3 | 2019-08-04 21:30:12 -0700 | [diff] [blame] | 751 | } |
| 752 | return refresh_mask; |
| 753 | } |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 754 | // Unfortunately the encoder interface reflects the old refresh_*_frame |
| 755 | // flags so we have to replicate the old refresh_frame_flags logic here in |
| 756 | // order to preserve the behaviour of the flag overrides. |
Marco Paniconi | 314bc36 | 2019-08-13 10:53:02 -0700 | [diff] [blame] | 757 | 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] | 758 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 759 | refresh_mask |= ext_refresh_frame_flags->last_frame << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 760 | |
| 761 | ref_frame_map_idx = get_ref_frame_map_idx(cm, EXTREF_FRAME); |
| 762 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 763 | refresh_mask |= ext_refresh_frame_flags->bwd_ref_frame |
| 764 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 765 | |
| 766 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF2_FRAME); |
| 767 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 768 | refresh_mask |= ext_refresh_frame_flags->alt2_ref_frame |
| 769 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 770 | |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 771 | if (frame_update_type == OVERLAY_UPDATE) { |
Jingning Han | 5738e03 | 2019-07-22 15:22:52 -0700 | [diff] [blame] | 772 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME); |
| 773 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 774 | refresh_mask |= ext_refresh_frame_flags->golden_frame |
| 775 | << ref_frame_map_idx; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 776 | } else { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 777 | ref_frame_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME); |
| 778 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 779 | refresh_mask |= ext_refresh_frame_flags->golden_frame |
| 780 | << ref_frame_map_idx; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 781 | |
| 782 | ref_frame_map_idx = get_ref_frame_map_idx(cm, ALTREF_FRAME); |
| 783 | if (ref_frame_map_idx != INVALID_IDX) |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 784 | refresh_mask |= ext_refresh_frame_flags->alt_ref_frame |
| 785 | << ref_frame_map_idx; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 786 | } |
| 787 | return refresh_mask; |
| 788 | } |
| 789 | |
Jingning Han | c9c172d | 2019-07-23 14:10:32 -0700 | [diff] [blame] | 790 | // Search for the open slot to store the current frame. |
Jingning Han | 81d6fbb | 2019-07-15 10:14:13 -0700 | [diff] [blame] | 791 | int free_fb_index = get_free_ref_map_index(ref_buffer_stack); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 792 | switch (frame_update_type) { |
| 793 | case KF_UPDATE: |
| 794 | case GF_UPDATE: |
| 795 | if (free_fb_index != INVALID_IDX) { |
| 796 | refresh_mask = 1 << free_fb_index; |
| 797 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 798 | if (ref_buffer_stack->gld_stack_size) |
| 799 | refresh_mask = |
| 800 | 1 << ref_buffer_stack |
| 801 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 802 | else |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 803 | refresh_mask = |
| 804 | 1 << ref_buffer_stack |
| 805 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 806 | } |
| 807 | break; |
| 808 | case LF_UPDATE: |
| 809 | if (free_fb_index != INVALID_IDX) { |
| 810 | refresh_mask = 1 << free_fb_index; |
| 811 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 812 | if (ref_buffer_stack->lst_stack_size >= 2) |
| 813 | refresh_mask = |
| 814 | 1 << ref_buffer_stack |
| 815 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 1af1c01 | 2020-02-10 10:51:56 -0800 | [diff] [blame] | 816 | else if (ref_buffer_stack->gld_stack_size >= 2) |
| 817 | refresh_mask = |
| 818 | 1 << ref_buffer_stack |
| 819 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 820 | else |
| 821 | assert(0 && "No ref map index found"); |
| 822 | } |
| 823 | break; |
| 824 | case ARF_UPDATE: |
| 825 | if (free_fb_index != INVALID_IDX) { |
| 826 | refresh_mask = 1 << free_fb_index; |
| 827 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 828 | if (ref_buffer_stack->gld_stack_size >= 3) |
| 829 | refresh_mask = |
| 830 | 1 << ref_buffer_stack |
| 831 | ->gld_stack[ref_buffer_stack->gld_stack_size - 1]; |
| 832 | else if (ref_buffer_stack->lst_stack_size >= 2) |
| 833 | refresh_mask = |
| 834 | 1 << ref_buffer_stack |
| 835 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 836 | else |
| 837 | assert(0 && "No ref map index found"); |
| 838 | } |
| 839 | break; |
| 840 | case INTNL_ARF_UPDATE: |
| 841 | if (free_fb_index != INVALID_IDX) { |
| 842 | refresh_mask = 1 << free_fb_index; |
| 843 | } else { |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 844 | refresh_mask = |
| 845 | 1 << ref_buffer_stack |
| 846 | ->lst_stack[ref_buffer_stack->lst_stack_size - 1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 847 | } |
| 848 | break; |
Jingning Han | 5738e03 | 2019-07-22 15:22:52 -0700 | [diff] [blame] | 849 | case OVERLAY_UPDATE: break; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 850 | case INTNL_OVERLAY_UPDATE: break; |
| 851 | default: assert(0); break; |
| 852 | } |
| 853 | |
| 854 | return refresh_mask; |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 857 | #if !CONFIG_REALTIME_ONLY |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 858 | void setup_mi(AV1_COMP *const cpi, YV12_BUFFER_CONFIG *src) { |
| 859 | AV1_COMMON *const cm = &cpi->common; |
| 860 | const int num_planes = av1_num_planes(cm); |
| 861 | MACROBLOCK *const x = &cpi->td.mb; |
| 862 | MACROBLOCKD *const xd = &x->e_mbd; |
| 863 | |
| 864 | av1_setup_src_planes(x, src, 0, 0, num_planes, cm->seq_params.sb_size); |
| 865 | |
| 866 | av1_setup_block_planes(xd, cm->seq_params.subsampling_x, |
| 867 | cm->seq_params.subsampling_y, num_planes); |
| 868 | |
Urvang Joshi | 2603bfe | 2020-03-25 13:33:18 -0700 | [diff] [blame] | 869 | set_mi_offsets(&cm->mi_params, xd, 0, 0); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 872 | // Apply temporal filtering to key frames and encode the filtered frame. |
| 873 | // If the current frame is not key frame, this function is identical to |
| 874 | // av1_encode(). |
| 875 | static int denoise_and_encode(AV1_COMP *const cpi, uint8_t *const dest, |
| 876 | EncodeFrameInput *const frame_input, |
| 877 | EncodeFrameParams *const frame_params, |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 878 | EncodeFrameResults *const frame_results) { |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 879 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
| 880 | AV1_COMMON *const cm = &cpi->common; |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 881 | |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 882 | // Decide whether to apply temporal filtering to the source frame. |
| 883 | int apply_filtering = |
| 884 | frame_params->frame_type == KEY_FRAME && |
| 885 | oxcf->enable_keyframe_filtering && !is_stat_generation_stage(cpi) && |
Sarah Parker | 21f1e6e | 2019-11-15 16:48:29 -0800 | [diff] [blame] | 886 | !frame_params->show_existing_frame && |
yjshen | 0c94222 | 2020-01-23 13:55:46 -0800 | [diff] [blame] | 887 | cpi->rc.frames_to_key > TF_NUM_FILTERING_FRAMES_FOR_KEY_FRAME && |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 888 | !is_lossless_requested(oxcf) && oxcf->arnr_max_frames > 0; |
| 889 | if (apply_filtering) { |
| 890 | const double y_noise_level = av1_estimate_noise_from_single_plane( |
| 891 | frame_input->source, 0, cm->seq_params.bit_depth); |
| 892 | apply_filtering = y_noise_level > 0; |
| 893 | } |
| 894 | |
Yunqing Wang | ce71fd7 | 2019-10-18 17:23:58 -0700 | [diff] [blame] | 895 | // Save the pointer to the original source image. |
| 896 | YV12_BUFFER_CONFIG *source_kf_buffer = frame_input->source; |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 897 | |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 898 | // Apply filtering to key frame. |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 899 | if (apply_filtering) { |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 900 | // Initialization for frame motion estimation. |
| 901 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 902 | av1_init_mi_buffers(&cm->mi_params); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 903 | setup_mi(cpi, frame_input->source); |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 904 | av1_init_macroblockd(cm, xd); |
Vishesh | d131791 | 2020-04-07 14:39:44 +0530 | [diff] [blame] | 905 | memset( |
| 906 | cpi->mbmi_ext_info.frame_base, 0, |
| 907 | cpi->mbmi_ext_info.alloc_size * sizeof(*cpi->mbmi_ext_info.frame_base)); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 908 | |
| 909 | av1_set_speed_features_framesize_independent(cpi, oxcf->speed); |
| 910 | av1_set_speed_features_framesize_dependent(cpi, oxcf->speed); |
| 911 | av1_set_rd_speed_thresholds(cpi); |
| 912 | av1_setup_frame_buf_refs(cm); |
| 913 | av1_setup_frame_sign_bias(cm); |
| 914 | av1_frame_init_quantizer(cpi); |
| 915 | av1_setup_past_independence(cm); |
| 916 | |
Sarah Parker | 21f1e6e | 2019-11-15 16:48:29 -0800 | [diff] [blame] | 917 | if (!frame_params->show_frame) { |
| 918 | int arf_src_index = get_arf_src_index(&cpi->gf_group, cpi->oxcf.pass); |
| 919 | av1_temporal_filter(cpi, -1 * arf_src_index, NULL); |
| 920 | } else { |
| 921 | av1_temporal_filter(cpi, -1, NULL); |
| 922 | } |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 923 | aom_extend_frame_borders(&cpi->alt_ref_buffer, av1_num_planes(cm)); |
Cheng Chen | 5083a9f | 2019-07-12 15:33:34 -0700 | [diff] [blame] | 924 | // Use the filtered frame for encoding. |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 925 | frame_input->source = &cpi->alt_ref_buffer; |
Akshata Jadhav | c209884 | 2019-11-26 16:16:29 +0530 | [diff] [blame] | 926 | // Copy metadata info to alt-ref buffer. |
| 927 | aom_remove_metadata_from_frame_buffer(frame_input->source); |
| 928 | aom_copy_metadata_to_frame_buffer(frame_input->source, |
| 929 | source_kf_buffer->metadata); |
Hui Su | b148537 | 2020-04-16 23:31:39 -0700 | [diff] [blame] | 930 | } |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 931 | |
Hui Su | b148537 | 2020-04-16 23:31:39 -0700 | [diff] [blame] | 932 | if (frame_params->frame_type == KEY_FRAME && !is_stat_generation_stage(cpi) && |
| 933 | oxcf->enable_tpl_model && oxcf->lag_in_frames > 0 && |
| 934 | frame_params->show_frame) { |
| 935 | av1_tpl_setup_stats(cpi, 0, frame_params, frame_input); |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | if (av1_encode(cpi, dest, frame_input, frame_params, frame_results) != |
| 939 | AOM_CODEC_OK) { |
| 940 | return AOM_CODEC_ERROR; |
| 941 | } |
| 942 | |
| 943 | // Set frame_input source to true source for psnr calculation. |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 944 | if (apply_filtering) { |
Yunqing Wang | ce71fd7 | 2019-10-18 17:23:58 -0700 | [diff] [blame] | 945 | cpi->source = source_kf_buffer; |
| 946 | cpi->unscaled_source = source_kf_buffer; |
Jingning Han | 97b64a7 | 2019-10-08 11:40:51 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Cheng Chen | 7abe313 | 2019-06-19 11:55:28 -0700 | [diff] [blame] | 949 | return AOM_CODEC_OK; |
| 950 | } |
| 951 | #endif // !CONFIG_REALTIME_ONLY |
| 952 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 953 | static INLINE int find_unused_ref_frame(const int *used_ref_frames, |
| 954 | const int *stack, int stack_size) { |
| 955 | for (int i = 0; i < stack_size; ++i) { |
| 956 | const int this_ref = stack[i]; |
| 957 | int ref_idx = 0; |
| 958 | for (ref_idx = 0; ref_idx <= ALTREF_FRAME - LAST_FRAME; ++ref_idx) { |
| 959 | if (this_ref == used_ref_frames[ref_idx]) break; |
| 960 | } |
| 961 | |
| 962 | // not in use |
| 963 | if (ref_idx > ALTREF_FRAME - LAST_FRAME) return this_ref; |
| 964 | } |
| 965 | |
| 966 | return INVALID_IDX; |
| 967 | } |
| 968 | |
Jingning Han | dae645a | 2019-08-01 10:00:11 -0700 | [diff] [blame] | 969 | void av1_get_ref_frames(AV1_COMP *const cpi, RefBufferStack *ref_buffer_stack) { |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 970 | AV1_COMMON *cm = &cpi->common; |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 971 | int *const remapped_ref_idx = cm->remapped_ref_idx; |
| 972 | int *const arf_stack = ref_buffer_stack->arf_stack; |
| 973 | int *const lst_stack = ref_buffer_stack->lst_stack; |
| 974 | int *const gld_stack = ref_buffer_stack->gld_stack; |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 975 | const int arf_stack_size = ref_buffer_stack->arf_stack_size; |
| 976 | const int lst_stack_size = ref_buffer_stack->lst_stack_size; |
| 977 | const int gld_stack_size = ref_buffer_stack->gld_stack_size; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 978 | |
| 979 | // Initialization |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 980 | 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] | 981 | |
| 982 | if (arf_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 983 | 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] | 984 | |
| 985 | if (arf_stack_size > 1) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 986 | remapped_ref_idx[BWDREF_FRAME - LAST_FRAME] = arf_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 987 | |
| 988 | if (arf_stack_size > 2) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 989 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = arf_stack[1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | if (lst_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 993 | remapped_ref_idx[LAST_FRAME - LAST_FRAME] = lst_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 994 | |
| 995 | if (lst_stack_size > 1) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 996 | remapped_ref_idx[LAST2_FRAME - LAST_FRAME] = lst_stack[1]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (gld_stack_size) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1000 | remapped_ref_idx[GOLDEN_FRAME - LAST_FRAME] = gld_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1001 | |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1002 | // If there are more frames in the golden stack, assign them to BWDREF, |
| 1003 | // ALTREF2, or LAST3. |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1004 | if (gld_stack_size > 1) { |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1005 | if (arf_stack_size <= 2) { |
| 1006 | if (arf_stack_size <= 1) { |
| 1007 | remapped_ref_idx[BWDREF_FRAME - LAST_FRAME] = gld_stack[1]; |
| 1008 | if (gld_stack_size > 2) |
| 1009 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = gld_stack[2]; |
| 1010 | } else { |
| 1011 | remapped_ref_idx[ALTREF2_FRAME - LAST_FRAME] = gld_stack[1]; |
| 1012 | } |
| 1013 | } else { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1014 | remapped_ref_idx[LAST3_FRAME - LAST_FRAME] = gld_stack[1]; |
Hui Su | 483d25d | 2020-04-15 00:28:08 -0700 | [diff] [blame] | 1015 | } |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | for (int idx = ALTREF_FRAME - LAST_FRAME; idx >= 0; --idx) { |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1020 | int ref_map_index = remapped_ref_idx[idx]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (ref_map_index != INVALID_IDX) continue; |
| 1023 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1024 | ref_map_index = |
| 1025 | find_unused_ref_frame(remapped_ref_idx, arf_stack, arf_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1026 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1027 | if (ref_map_index == INVALID_IDX) { |
| 1028 | ref_map_index = |
| 1029 | find_unused_ref_frame(remapped_ref_idx, gld_stack, gld_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1032 | if (ref_map_index == INVALID_IDX) { |
| 1033 | ref_map_index = |
| 1034 | find_unused_ref_frame(remapped_ref_idx, lst_stack, lst_stack_size); |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (ref_map_index != INVALID_IDX) |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1038 | remapped_ref_idx[idx] = ref_map_index; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1039 | else |
Hui Su | 5f7ee81 | 2020-02-20 15:58:04 -0800 | [diff] [blame] | 1040 | remapped_ref_idx[idx] = ref_buffer_stack->gld_stack[0]; |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1044 | int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size, |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1045 | uint8_t *const dest, unsigned int *frame_flags, |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1046 | int64_t *const time_stamp, int64_t *const time_end, |
Yue Chen | 1bc5be6 | 2018-08-24 13:57:32 -0700 | [diff] [blame] | 1047 | const aom_rational64_t *const timestamp_ratio, |
| 1048 | int flush) { |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1049 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 1050 | AV1_COMMON *const cm = &cpi->common; |
Jingning Han | a729393 | 2019-08-15 15:46:49 -0700 | [diff] [blame] | 1051 | GF_GROUP *gf_group = &cpi->gf_group; |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 1052 | ExternalFlags *const ext_flags = &cpi->ext_flags; |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1053 | |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1054 | EncodeFrameInput frame_input; |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1055 | EncodeFrameParams frame_params; |
| 1056 | EncodeFrameResults frame_results; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1057 | memset(&frame_input, 0, sizeof(frame_input)); |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1058 | memset(&frame_params, 0, sizeof(frame_params)); |
| 1059 | memset(&frame_results, 0, sizeof(frame_results)); |
| 1060 | |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1061 | // TODO(sarahparker) finish bit allocation for one pass pyramid |
Urvang Joshi | b44f48f | 2020-01-27 11:09:48 -0800 | [diff] [blame] | 1062 | if (has_no_stats_stage(cpi) && oxcf->rc_mode != AOM_Q) { |
| 1063 | cpi->oxcf.gf_max_pyr_height = |
| 1064 | AOMMIN(cpi->oxcf.gf_max_pyr_height, USE_ALTREF_FOR_ONE_PASS); |
| 1065 | cpi->oxcf.gf_min_pyr_height = |
| 1066 | AOMMIN(cpi->oxcf.gf_min_pyr_height, cpi->oxcf.gf_max_pyr_height); |
| 1067 | } |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1068 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1069 | if (!is_stat_generation_stage(cpi)) { |
Sarah Parker | 2beb1d1 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1070 | // If this is a forward keyframe, mark as a show_existing_frame |
| 1071 | if (cpi->oxcf.fwd_kf_enabled && (gf_group->index == gf_group->size) && |
| 1072 | gf_group->update_type[1] == ARF_UPDATE && cpi->rc.frames_to_key == 0) { |
| 1073 | frame_params.show_existing_frame = 1; |
| 1074 | } else { |
| 1075 | frame_params.show_existing_frame = |
chiyotsai | 6b43013 | 2019-12-18 10:33:51 -0800 | [diff] [blame] | 1076 | ((oxcf->enable_overlay == 0 || cpi->sf.hl_sf.disable_overlay_frames || |
Sarah Parker | 2beb1d1 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1077 | cpi->show_existing_alt_ref) && |
| 1078 | gf_group->update_type[gf_group->index] == OVERLAY_UPDATE) || |
| 1079 | gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE; |
| 1080 | } |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1081 | frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags); |
Yunqing Wang | 1973f11 | 2019-10-18 15:50:04 -0700 | [diff] [blame] | 1082 | |
| 1083 | // Reset show_existing_alt_ref decision to 0 after it is used. |
| 1084 | if (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE) { |
| 1085 | cpi->show_existing_alt_ref = 0; |
| 1086 | } |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 1087 | } else { |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1088 | frame_params.show_existing_frame = 0; |
David Turner | b0c0aa3 | 2019-01-28 16:17:13 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 1091 | int code_arf = 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1092 | struct lookahead_entry *source = NULL; |
| 1093 | struct lookahead_entry *last_source = NULL; |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1094 | if (frame_params.show_existing_frame) { |
Mufaddal Chakera | ac82868 | 2019-12-13 16:31:42 +0530 | [diff] [blame] | 1095 | source = av1_lookahead_pop(cpi->lookahead, flush, cpi->compressor_stage); |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1096 | frame_params.show_frame = 1; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1097 | } else { |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 1098 | int show_existing_alt_ref = 0; |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 1099 | source = choose_frame_source(cpi, &code_arf, &flush, &last_source, |
Yunqing Wang | 699fa7f | 2019-10-15 17:40:46 -0700 | [diff] [blame] | 1100 | &frame_params, &show_existing_alt_ref); |
Yunqing Wang | 1973f11 | 2019-10-18 15:50:04 -0700 | [diff] [blame] | 1101 | if (gf_group->update_type[gf_group->index] == ARF_UPDATE) |
| 1102 | cpi->show_existing_alt_ref = show_existing_alt_ref; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | 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] | 1106 | #if !CONFIG_REALTIME_ONLY |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1107 | if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) { |
| 1108 | av1_end_first_pass(cpi); /* get last stats packet */ |
| 1109 | cpi->twopass.first_pass_done = 1; |
| 1110 | } |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1111 | #endif |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1112 | return -1; |
| 1113 | } |
| 1114 | |
Jingning Han | bb78298 | 2019-08-15 11:28:46 -0700 | [diff] [blame] | 1115 | frame_input.source = code_arf ? &cpi->alt_ref_buffer : &source->img; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1116 | frame_input.last_source = last_source != NULL ? &last_source->img : NULL; |
| 1117 | frame_input.ts_duration = source->ts_end - source->ts_start; |
Cheng Chen | e1a7a3e | 2020-03-18 18:23:19 -0700 | [diff] [blame] | 1118 | // Save unfiltered source. It is used in av1_get_second_pass_params(). |
| 1119 | cpi->unfiltered_source = frame_input.source; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1120 | |
| 1121 | *time_stamp = source->ts_start; |
| 1122 | *time_end = source->ts_end; |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 1123 | if (source->ts_start < cpi->time_stamps.first_ever) { |
| 1124 | cpi->time_stamps.first_ever = source->ts_start; |
| 1125 | cpi->time_stamps.prev_end_seen = source->ts_start; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | av1_apply_encoding_flags(cpi, source->flags); |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1129 | if (!frame_params.show_existing_frame) |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1130 | *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1131 | |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1132 | // Shown frames and arf-overlay frames need frame-rate considering |
Deepa K G | fb89ce0 | 2020-04-06 13:34:42 +0530 | [diff] [blame] | 1133 | if (frame_params.show_frame) |
| 1134 | adjust_frame_rate(cpi, source->ts_start, source->ts_end); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1135 | |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1136 | if (!frame_params.show_existing_frame) { |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1137 | if (cpi->film_grain_table) { |
Neil Birkbeck | bd40ca7 | 2019-03-02 13:25:50 -0800 | [diff] [blame] | 1138 | cm->cur_frame->film_grain_params_present = aom_film_grain_table_lookup( |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1139 | cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */, |
| 1140 | &cm->film_grain_params); |
Neil Birkbeck | bd40ca7 | 2019-03-02 13:25:50 -0800 | [diff] [blame] | 1141 | } else { |
| 1142 | cm->cur_frame->film_grain_params_present = |
| 1143 | cm->seq_params.film_grain_params_present; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1144 | } |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1145 | // only one operating point supported now |
Yue Chen | 1bc5be6 | 2018-08-24 13:57:32 -0700 | [diff] [blame] | 1146 | const int64_t pts64 = ticks_to_timebase_units(timestamp_ratio, *time_stamp); |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1147 | if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR; |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1148 | cm->frame_presentation_time = (uint32_t)pts64; |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1151 | #if CONFIG_REALTIME_ONLY |
| 1152 | av1_get_one_pass_rt_params(cpi, &frame_params, *frame_flags); |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1153 | #else |
Mufaddal Chakera | 3bcc72c | 2019-12-11 14:38:37 +0530 | [diff] [blame] | 1154 | if (has_no_stats_stage(cpi) && oxcf->mode == REALTIME && |
| 1155 | oxcf->lag_in_frames == 0) |
Marco Paniconi | cea99e2 | 2019-07-16 18:36:31 -0700 | [diff] [blame] | 1156 | av1_get_one_pass_rt_params(cpi, &frame_params, *frame_flags); |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1157 | else if (!is_stat_generation_stage(cpi)) |
Jingning Han | 491198d | 2020-02-13 21:53:41 -0800 | [diff] [blame] | 1158 | av1_get_second_pass_params(cpi, &frame_params, &frame_input, *frame_flags); |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1159 | #endif |
Jingning Han | d392c01 | 2019-09-19 15:48:08 -0700 | [diff] [blame] | 1160 | FRAME_UPDATE_TYPE frame_update_type = get_frame_update_type(gf_group); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1161 | |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1162 | if (frame_params.show_existing_frame && |
| 1163 | frame_params.frame_type != KEY_FRAME) { |
David Turner | 475a313 | 2019-01-18 15:17:17 +0000 | [diff] [blame] | 1164 | // Force show-existing frames to be INTER, except forward keyframes |
| 1165 | frame_params.frame_type = INTER_FRAME; |
| 1166 | } |
| 1167 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1168 | // TODO(david.turner@argondesign.com): Move all the encode strategy |
| 1169 | // (largely near av1_get_compressed_data) in here |
| 1170 | |
| 1171 | // TODO(david.turner@argondesign.com): Change all the encode strategy to |
| 1172 | // modify frame_params instead of cm or cpi. |
| 1173 | |
David Turner | 04b70d8 | 2019-01-24 15:39:19 +0000 | [diff] [blame] | 1174 | // Per-frame encode speed. In theory this can vary, but things may have been |
| 1175 | // written assuming speed-level will not change within a sequence, so this |
| 1176 | // parameter should be used with caution. |
| 1177 | frame_params.speed = oxcf->speed; |
| 1178 | |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1179 | // Work out some encoding parameters specific to the pass: |
Mufaddal Chakera | 3bcc72c | 2019-12-11 14:38:37 +0530 | [diff] [blame] | 1180 | if (has_no_stats_stage(cpi) && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) { |
Sarah Parker | 97803fc | 2019-05-17 14:15:37 -0700 | [diff] [blame] | 1181 | av1_cyclic_refresh_update_parameters(cpi); |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1182 | } else if (is_stat_generation_stage(cpi)) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1183 | cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&cpi->oxcf); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1184 | const int kf_requested = (cm->current_frame.frame_number == 0 || |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1185 | (*frame_flags & FRAMEFLAGS_KEY)); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1186 | if (kf_requested && frame_update_type != OVERLAY_UPDATE && |
| 1187 | frame_update_type != INTNL_OVERLAY_UPDATE) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1188 | frame_params.frame_type = KEY_FRAME; |
| 1189 | } else { |
| 1190 | frame_params.frame_type = INTER_FRAME; |
David Turner | cb5e36f | 2019-01-17 17:15:25 +0000 | [diff] [blame] | 1191 | } |
Hamsalekha S | 37cc1d1 | 2019-12-12 19:27:41 +0530 | [diff] [blame] | 1192 | } else if (is_stat_consumption_stage(cpi)) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1193 | #if CONFIG_MISMATCH_DEBUG |
| 1194 | mismatch_move_frame_idx_w(); |
| 1195 | #endif |
| 1196 | #if TXCOEFF_COST_TIMER |
| 1197 | cm->txcoeff_cost_timer = 0; |
| 1198 | cm->txcoeff_cost_count = 0; |
| 1199 | #endif |
| 1200 | } |
| 1201 | |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 1202 | if (!is_stat_generation_stage(cpi)) |
| 1203 | set_ext_overrides(cm, &frame_params, ext_flags); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1204 | |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1205 | // Shown keyframes and S frames refresh all reference buffers |
| 1206 | const int force_refresh_all = |
| 1207 | ((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) || |
| 1208 | frame_params.frame_type == S_FRAME) && |
David Turner | e86ee0d | 2019-02-18 17:16:28 +0000 | [diff] [blame] | 1209 | !frame_params.show_existing_frame; |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1210 | |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 1211 | av1_configure_buffer_updates(cpi, &frame_params.refresh_frame, |
| 1212 | frame_update_type, force_refresh_all); |
David Turner | 4f1f181 | 2019-01-24 17:00:24 +0000 | [diff] [blame] | 1213 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1214 | if (!is_stat_generation_stage(cpi)) { |
Deepa K G | 140bc83 | 2019-10-30 17:16:29 +0530 | [diff] [blame] | 1215 | const RefCntBuffer *ref_frames[INTER_REFS_PER_FRAME]; |
| 1216 | const YV12_BUFFER_CONFIG *ref_frame_buf[INTER_REFS_PER_FRAME]; |
| 1217 | |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1218 | if (!ext_flags->refresh_frame.update_pending) { |
Jingning Han | dae645a | 2019-08-01 10:00:11 -0700 | [diff] [blame] | 1219 | av1_get_ref_frames(cpi, &cpi->ref_buffer_stack); |
Marco Paniconi | d8574e3 | 2019-08-04 21:30:12 -0700 | [diff] [blame] | 1220 | } else if (cpi->svc.external_ref_frame_config) { |
Marco Paniconi | 6714211 | 2019-07-24 15:00:31 -0700 | [diff] [blame] | 1221 | for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) |
| 1222 | cm->remapped_ref_idx[i] = cpi->svc.ref_idx[i]; |
| 1223 | } |
Jingning Han | 0a2af4e | 2019-07-08 19:30:03 -0700 | [diff] [blame] | 1224 | |
Deepa K G | 140bc83 | 2019-10-30 17:16:29 +0530 | [diff] [blame] | 1225 | // Get the reference frames |
| 1226 | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
| 1227 | ref_frames[i] = get_ref_frame_buf(cm, ref_frame_priority_order[i]); |
| 1228 | ref_frame_buf[i] = ref_frames[i] != NULL ? &ref_frames[i]->buf : NULL; |
| 1229 | } |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1230 | // Work out which reference frame slots may be used. |
Vishesh | a195ca3 | 2020-04-07 18:46:20 +0530 | [diff] [blame] | 1231 | frame_params.ref_frame_flags = get_ref_frame_flags( |
| 1232 | &cpi->sf, ref_frame_buf, ext_flags->ref_frame_flags); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1233 | |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 1234 | frame_params.primary_ref_frame = |
| 1235 | choose_primary_ref_frame(cpi, &frame_params); |
Sarah Parker | e1b2201 | 2019-06-06 16:35:25 -0700 | [diff] [blame] | 1236 | frame_params.order_offset = get_order_offset(&cpi->gf_group, &frame_params); |
David Turner | 6e8b4d9 | 2019-02-18 15:01:33 +0000 | [diff] [blame] | 1237 | |
Jingning Han | 42266ca | 2019-07-12 14:37:16 -0700 | [diff] [blame] | 1238 | frame_params.refresh_frame_flags = av1_get_refresh_frame_flags( |
| 1239 | cpi, &frame_params, frame_update_type, &cpi->ref_buffer_stack); |
Jingning Han | a729393 | 2019-08-15 15:46:49 -0700 | [diff] [blame] | 1240 | |
| 1241 | frame_params.existing_fb_idx_to_show = |
| 1242 | frame_params.show_existing_frame |
Debargha Mukherjee | a2074dd | 2019-09-04 10:03:44 -0700 | [diff] [blame] | 1243 | ? (frame_update_type == INTNL_OVERLAY_UPDATE |
| 1244 | ? get_ref_frame_map_idx(cm, BWDREF_FRAME) |
| 1245 | : get_ref_frame_map_idx(cm, ALTREF_FRAME)) |
Jingning Han | a729393 | 2019-08-15 15:46:49 -0700 | [diff] [blame] | 1246 | : INVALID_IDX; |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1249 | // The way frame_params->remapped_ref_idx is setup is a placeholder. |
David Turner | f459229 | 2019-02-21 11:50:30 +0000 | [diff] [blame] | 1250 | // Currently, reference buffer assignment is done by update_ref_frame_map() |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1251 | // which is called by high-level strategy AFTER encoding a frame. It modifies |
| 1252 | // cm->remapped_ref_idx. If you want to use an alternative method to |
| 1253 | // determine reference buffer assignment, just put your assignments into |
| 1254 | // frame_params->remapped_ref_idx here and they will be used when encoding |
| 1255 | // this frame. If frame_params->remapped_ref_idx is setup independently of |
David Turner | f459229 | 2019-02-21 11:50:30 +0000 | [diff] [blame] | 1256 | // 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] | 1257 | memcpy(frame_params.remapped_ref_idx, cm->remapped_ref_idx, |
| 1258 | REF_FRAMES * sizeof(*cm->remapped_ref_idx)); |
| 1259 | |
Ravi Chaudhary | eeae729 | 2019-06-26 13:32:59 +0530 | [diff] [blame] | 1260 | cpi->td.mb.e_mbd.delta_qindex = 0; |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1261 | |
| 1262 | if (!frame_params.show_existing_frame) { |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 1263 | cm->quant_params.using_qmatrix = cpi->oxcf.using_qm; |
Jerome Jiang | d413f4a | 2019-09-23 14:53:03 -0700 | [diff] [blame] | 1264 | #if !CONFIG_REALTIME_ONLY |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1265 | if (oxcf->lag_in_frames > 0 && !is_stat_generation_stage(cpi)) { |
Sarah Parker | e1b2201 | 2019-06-06 16:35:25 -0700 | [diff] [blame] | 1266 | if (cpi->gf_group.index == 1 && cpi->oxcf.enable_tpl_model) { |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 1267 | av1_configure_buffer_updates(cpi, &frame_params.refresh_frame, |
| 1268 | frame_update_type, 0); |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1269 | av1_set_frame_size(cpi, cm->width, cm->height); |
Jingning Han | 491198d | 2020-02-13 21:53:41 -0800 | [diff] [blame] | 1270 | av1_tpl_setup_stats(cpi, 0, &frame_params, &frame_input); |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1271 | assert(cpi->num_gf_group_show_frames == 1); |
| 1272 | } |
| 1273 | } |
Jerome Jiang | d413f4a | 2019-09-23 14:53:03 -0700 | [diff] [blame] | 1274 | #endif |
Yue Chen | 4e585cc | 2019-06-03 14:47:16 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1277 | #if CONFIG_REALTIME_ONLY |
David Turner | dedd8ff | 2019-01-23 13:59:46 +0000 | [diff] [blame] | 1278 | if (av1_encode(cpi, dest, &frame_input, &frame_params, &frame_results) != |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1279 | AOM_CODEC_OK) { |
| 1280 | return AOM_CODEC_ERROR; |
| 1281 | } |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1282 | #else |
Hui Su | 028ad7d | 2020-04-13 23:24:32 -0700 | [diff] [blame] | 1283 | if (denoise_and_encode(cpi, dest, &frame_input, &frame_params, |
| 1284 | &frame_results) != AOM_CODEC_OK) { |
Cheng Chen | 0fcf6f8 | 2019-10-11 11:41:19 -0700 | [diff] [blame] | 1285 | return AOM_CODEC_ERROR; |
| 1286 | } |
| 1287 | #endif // CONFIG_REALTIME_ONLY |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1288 | if (!is_stat_generation_stage(cpi)) |
| 1289 | cpi->num_gf_group_show_frames += frame_params.show_frame; |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1290 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1291 | if (!is_stat_generation_stage(cpi)) { |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1292 | // First pass doesn't modify reference buffer assignment or produce frame |
| 1293 | // flags |
Jayasanker J | 24cb9bc | 2020-04-15 13:43:10 +0530 | [diff] [blame] | 1294 | update_frame_flags(&cpi->common, &cpi->refresh_frame, frame_flags); |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1295 | if (!ext_flags->refresh_frame.update_pending) { |
Marco Paniconi | 8d335b7 | 2019-08-06 09:07:07 -0700 | [diff] [blame] | 1296 | int ref_map_index = |
| 1297 | av1_get_refresh_ref_frame_map(cm->current_frame.refresh_frame_flags); |
Sarah Parker | edffc65 | 2019-10-25 16:30:32 -0700 | [diff] [blame] | 1298 | av1_update_ref_frame_map(cpi, frame_update_type, cm->show_existing_frame, |
| 1299 | ref_map_index, &cpi->ref_buffer_stack); |
Marco Paniconi | 8d335b7 | 2019-08-06 09:07:07 -0700 | [diff] [blame] | 1300 | } |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1303 | #if !CONFIG_REALTIME_ONLY |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1304 | if (!is_stat_generation_stage(cpi)) { |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1305 | #if TXCOEFF_COST_TIMER |
| 1306 | cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer; |
| 1307 | fprintf(stderr, |
| 1308 | "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld " |
| 1309 | "in us\n", |
| 1310 | cm->txcoeff_cost_count, cm->txcoeff_cost_timer, |
| 1311 | cm->cum_txcoeff_cost_timer); |
| 1312 | #endif |
| 1313 | av1_twopass_postencode_update(cpi); |
| 1314 | } |
Jerome Jiang | 2612b4d | 2019-05-29 17:46:47 -0700 | [diff] [blame] | 1315 | #endif // !CONFIG_REALTIME_ONLY |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1316 | |
Mufaddal Chakera | e732612 | 2019-12-04 14:49:09 +0530 | [diff] [blame] | 1317 | if (!is_stat_generation_stage(cpi)) { |
David Turner | a7f133c | 2019-01-22 14:47:16 +0000 | [diff] [blame] | 1318 | update_fb_of_context_type(cpi, &frame_params, cpi->fb_of_context_type); |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 1319 | set_additional_frame_flags(cm, frame_flags); |
David Turner | ddbff44 | 2019-01-21 14:58:42 +0000 | [diff] [blame] | 1320 | update_rc_counts(cpi); |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1323 | // Unpack frame_results: |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1324 | *size = frame_results.size; |
| 1325 | |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1326 | // Leave a signal for a higher level caller about if this frame is droppable |
| 1327 | if (*size > 0) { |
Vishesh | 38c05d7 | 2020-04-14 12:19:14 +0530 | [diff] [blame] | 1328 | cpi->droppable = is_frame_droppable(&cpi->svc, &ext_flags->refresh_frame); |
David Turner | 1539bb0 | 2019-01-24 15:28:13 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Marco Paniconi | 5b1e473 | 2019-08-08 18:57:53 -0700 | [diff] [blame] | 1331 | if (cpi->use_svc) av1_save_layer_context(cpi); |
| 1332 | |
David Turner | 056f7cd | 2019-01-07 17:48:13 +0000 | [diff] [blame] | 1333 | return AOM_CODEC_OK; |
| 1334 | } |