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