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