Home | History | Annotate | Download | only in encoder
      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 
     14 #include "config/aom_config.h"
     15 #include "config/aom_scale_rtcd.h"
     16 
     17 #include "aom/aom_codec.h"
     18 #include "aom/aom_encoder.h"
     19 
     20 #include "aom_ports/system_state.h"
     21 
     22 #if CONFIG_MISMATCH_DEBUG
     23 #include "aom_util/debug_util.h"
     24 #endif  // CONFIG_MISMATCH_DEBUG
     25 
     26 #include "av1/common/onyxc_int.h"
     27 
     28 #include "av1/encoder/encoder.h"
     29 #include "av1/encoder/encode_strategy.h"
     30 #include "av1/encoder/firstpass.h"
     31 #include "av1/encoder/pass2_strategy.h"
     32 #include "av1/encoder/temporal_filter.h"
     33 #include "av1/encoder/tpl_model.h"
     34 
     35 void av1_configure_buffer_updates(AV1_COMP *const cpi,
     36                                   EncodeFrameParams *const frame_params,
     37                                   const FRAME_UPDATE_TYPE type,
     38                                   int force_refresh_all) {
     39   // NOTE(weitinglin): Should we define another function to take care of
     40   // cpi->rc.is_$Source_Type to make this function as it is in the comment?
     41 
     42   cpi->rc.is_src_frame_alt_ref = 0;
     43   cpi->rc.is_src_frame_internal_arf = 0;
     44 
     45   switch (type) {
     46     case KF_UPDATE:
     47       frame_params->refresh_last_frame = 1;
     48       frame_params->refresh_golden_frame = 1;
     49       frame_params->refresh_bwd_ref_frame = 1;
     50       frame_params->refresh_alt2_ref_frame = 1;
     51       frame_params->refresh_alt_ref_frame = 1;
     52       break;
     53 
     54     case LF_UPDATE:
     55       frame_params->refresh_last_frame = 1;
     56       frame_params->refresh_golden_frame = 0;
     57       frame_params->refresh_bwd_ref_frame = 0;
     58       frame_params->refresh_alt2_ref_frame = 0;
     59       frame_params->refresh_alt_ref_frame = 0;
     60       break;
     61 
     62     case GF_UPDATE:
     63       // TODO(zoeliu): To further investigate whether 'refresh_last_frame' is
     64       //               needed.
     65       frame_params->refresh_last_frame = 1;
     66       frame_params->refresh_golden_frame = 1;
     67       frame_params->refresh_bwd_ref_frame = 0;
     68       frame_params->refresh_alt2_ref_frame = 0;
     69       frame_params->refresh_alt_ref_frame = 0;
     70       break;
     71 
     72     case OVERLAY_UPDATE:
     73       frame_params->refresh_last_frame = 0;
     74       frame_params->refresh_golden_frame = 1;
     75       frame_params->refresh_bwd_ref_frame = 0;
     76       frame_params->refresh_alt2_ref_frame = 0;
     77       frame_params->refresh_alt_ref_frame = 0;
     78 
     79       cpi->rc.is_src_frame_alt_ref = 1;
     80       break;
     81 
     82     case ARF_UPDATE:
     83       frame_params->refresh_last_frame = 0;
     84       frame_params->refresh_golden_frame = 0;
     85       // NOTE: BWDREF does not get updated along with ALTREF_FRAME.
     86       frame_params->refresh_bwd_ref_frame = 0;
     87       frame_params->refresh_alt2_ref_frame = 0;
     88       frame_params->refresh_alt_ref_frame = 1;
     89       break;
     90 
     91     case INTNL_OVERLAY_UPDATE:
     92       frame_params->refresh_last_frame = 1;
     93       frame_params->refresh_golden_frame = 0;
     94       frame_params->refresh_bwd_ref_frame = 0;
     95       frame_params->refresh_alt2_ref_frame = 0;
     96       frame_params->refresh_alt_ref_frame = 0;
     97 
     98       cpi->rc.is_src_frame_alt_ref = 1;
     99       cpi->rc.is_src_frame_internal_arf = 1;
    100       break;
    101 
    102     case INTNL_ARF_UPDATE:
    103       frame_params->refresh_last_frame = 0;
    104       frame_params->refresh_golden_frame = 0;
    105       if (cpi->oxcf.pass == 2) {
    106         frame_params->refresh_bwd_ref_frame = 1;
    107         frame_params->refresh_alt2_ref_frame = 0;
    108       } else {
    109         frame_params->refresh_bwd_ref_frame = 0;
    110         frame_params->refresh_alt2_ref_frame = 1;
    111       }
    112       frame_params->refresh_alt_ref_frame = 0;
    113       break;
    114 
    115     default: assert(0); break;
    116   }
    117 
    118   if (cpi->ext_refresh_frame_flags_pending &&
    119       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2)) {
    120     frame_params->refresh_last_frame = cpi->ext_refresh_last_frame;
    121     frame_params->refresh_golden_frame = cpi->ext_refresh_golden_frame;
    122     frame_params->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
    123     frame_params->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
    124     frame_params->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
    125   }
    126 
    127   if (force_refresh_all) {
    128     frame_params->refresh_last_frame = 1;
    129     frame_params->refresh_golden_frame = 1;
    130     frame_params->refresh_bwd_ref_frame = 1;
    131     frame_params->refresh_alt2_ref_frame = 1;
    132     frame_params->refresh_alt_ref_frame = 1;
    133   }
    134 }
    135 
    136 static void set_additional_frame_flags(const AV1_COMMON *const cm,
    137                                        unsigned int *const frame_flags) {
    138   if (frame_is_intra_only(cm)) *frame_flags |= FRAMEFLAGS_INTRAONLY;
    139   if (frame_is_sframe(cm)) *frame_flags |= FRAMEFLAGS_SWITCH;
    140   if (cm->error_resilient_mode) *frame_flags |= FRAMEFLAGS_ERROR_RESILIENT;
    141 }
    142 
    143 static INLINE void update_keyframe_counters(AV1_COMP *cpi) {
    144   if (cpi->common.show_frame) {
    145     if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
    146         cpi->common.current_frame.frame_type == KEY_FRAME) {
    147       // If this is a show_existing_frame with a source other than altref,
    148       // or if it is not a displayed forward keyframe, the keyframe update
    149       // counters were incremented when it was originally encoded.
    150       cpi->rc.frames_since_key++;
    151       cpi->rc.frames_to_key--;
    152     }
    153   }
    154 }
    155 
    156 static INLINE int is_frame_droppable(const AV1_COMP *const cpi) {
    157   return !(cpi->refresh_alt_ref_frame || cpi->refresh_alt2_ref_frame ||
    158            cpi->refresh_bwd_ref_frame || cpi->refresh_golden_frame ||
    159            cpi->refresh_last_frame);
    160 }
    161 
    162 static INLINE void update_frames_till_gf_update(AV1_COMP *cpi) {
    163   // TODO(weitinglin): Updating this counter for is_frame_droppable
    164   // is a work-around to handle the condition when a frame is drop.
    165   // We should fix the cpi->common.show_frame flag
    166   // instead of checking the other condition to update the counter properly.
    167   if (cpi->common.show_frame || is_frame_droppable(cpi)) {
    168     // Decrement count down till next gf
    169     if (cpi->rc.frames_till_gf_update_due > 0)
    170       cpi->rc.frames_till_gf_update_due--;
    171   }
    172 }
    173 
    174 static INLINE void update_twopass_gf_group_index(AV1_COMP *cpi) {
    175   // Increment the gf group index ready for the next frame. If this is
    176   // a show_existing_frame with a source other than altref, or if it is not
    177   // a displayed forward keyframe, the index was incremented when it was
    178   // originally encoded.
    179   if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
    180       cpi->common.current_frame.frame_type == KEY_FRAME) {
    181     ++cpi->twopass.gf_group.index;
    182   }
    183 }
    184 
    185 static void update_rc_counts(AV1_COMP *cpi) {
    186   update_keyframe_counters(cpi);
    187   update_frames_till_gf_update(cpi);
    188   if (cpi->oxcf.pass == 2) update_twopass_gf_group_index(cpi);
    189 }
    190 
    191 static void check_show_existing_frame(AV1_COMP *const cpi,
    192                                       EncodeFrameParams *const frame_params) {
    193   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    194   AV1_COMMON *const cm = &cpi->common;
    195   const FRAME_UPDATE_TYPE frame_update_type =
    196       gf_group->update_type[gf_group->index];
    197   const int which_arf = (gf_group->arf_update_idx[gf_group->index] > 0);
    198 
    199   if (cm->show_existing_frame == 1) {
    200     frame_params->show_existing_frame = 0;
    201   } else if (cpi->is_arf_filter_off[which_arf] &&
    202              (frame_update_type == OVERLAY_UPDATE ||
    203               frame_update_type == INTNL_OVERLAY_UPDATE)) {
    204     // Other parameters related to OVERLAY_UPDATE will be taken care of
    205     // in av1_get_second_pass_params(cpi)
    206     frame_params->show_existing_frame = 1;
    207     frame_params->existing_fb_idx_to_show =
    208         (frame_update_type == OVERLAY_UPDATE)
    209             ? get_ref_frame_map_idx(cm, ALTREF_FRAME)
    210             : get_ref_frame_map_idx(cm, BWDREF_FRAME);
    211   }
    212 }
    213 
    214 static void set_ext_overrides(AV1_COMP *const cpi,
    215                               EncodeFrameParams *const frame_params) {
    216   // Overrides the defaults with the externally supplied values with
    217   // av1_update_reference() and av1_update_entropy() calls
    218   // Note: The overrides are valid only for the next frame passed
    219   // to av1_encode_lowlevel()
    220 
    221   AV1_COMMON *const cm = &cpi->common;
    222 
    223   if (cpi->ext_use_s_frame) {
    224     frame_params->frame_type = S_FRAME;
    225   }
    226 
    227   if (cpi->ext_refresh_frame_context_pending) {
    228     cm->refresh_frame_context = cpi->ext_refresh_frame_context;
    229     cpi->ext_refresh_frame_context_pending = 0;
    230   }
    231   cm->allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
    232 
    233   frame_params->error_resilient_mode = cpi->ext_use_error_resilient;
    234   // A keyframe is already error resilient and keyframes with
    235   // error_resilient_mode interferes with the use of show_existing_frame
    236   // when forward reference keyframes are enabled.
    237   frame_params->error_resilient_mode &= frame_params->frame_type != KEY_FRAME;
    238   // For bitstream conformance, s-frames must be error-resilient
    239   frame_params->error_resilient_mode |= frame_params->frame_type == S_FRAME;
    240 }
    241 
    242 static int get_ref_frame_flags(const AV1_COMP *const cpi) {
    243   const AV1_COMMON *const cm = &cpi->common;
    244 
    245   const RefCntBuffer *last_buf = get_ref_frame_buf(cm, LAST_FRAME);
    246   const RefCntBuffer *last2_buf = get_ref_frame_buf(cm, LAST2_FRAME);
    247   const RefCntBuffer *last3_buf = get_ref_frame_buf(cm, LAST3_FRAME);
    248   const RefCntBuffer *golden_buf = get_ref_frame_buf(cm, GOLDEN_FRAME);
    249   const RefCntBuffer *bwd_buf = get_ref_frame_buf(cm, BWDREF_FRAME);
    250   const RefCntBuffer *alt2_buf = get_ref_frame_buf(cm, ALTREF2_FRAME);
    251   const RefCntBuffer *alt_buf = get_ref_frame_buf(cm, ALTREF_FRAME);
    252 
    253   // No.1 Priority: LAST_FRAME
    254   const int last2_is_last = (last2_buf == last_buf);
    255   const int last3_is_last = (last3_buf == last_buf);
    256   const int gld_is_last = (golden_buf == last_buf);
    257   const int bwd_is_last = (bwd_buf == last_buf);
    258   const int alt2_is_last = (alt2_buf == last_buf);
    259   const int alt_is_last = (alt_buf == last_buf);
    260 
    261   // No.2 Priority: ALTREF_FRAME
    262   const int last2_is_alt = (last2_buf == alt_buf);
    263   const int last3_is_alt = (last3_buf == alt_buf);
    264   const int gld_is_alt = (golden_buf == alt_buf);
    265   const int bwd_is_alt = (bwd_buf == alt_buf);
    266   const int alt2_is_alt = (alt2_buf == alt_buf);
    267 
    268   // No.3 Priority: LAST2_FRAME
    269   const int last3_is_last2 = (last3_buf == last2_buf);
    270   const int gld_is_last2 = (golden_buf == last2_buf);
    271   const int bwd_is_last2 = (bwd_buf == last2_buf);
    272   const int alt2_is_last2 = (alt2_buf == last2_buf);
    273 
    274   // No.4 Priority: LAST3_FRAME
    275   const int gld_is_last3 = (golden_buf == last3_buf);
    276   const int bwd_is_last3 = (bwd_buf == last3_buf);
    277   const int alt2_is_last3 = (alt2_buf == last3_buf);
    278 
    279   // No.5 Priority: GOLDEN_FRAME
    280   const int bwd_is_gld = (bwd_buf == golden_buf);
    281   const int alt2_is_gld = (alt2_buf == golden_buf);
    282 
    283   // No.6 Priority: BWDREF_FRAME
    284   const int alt2_is_bwd = (alt2_buf == bwd_buf);
    285 
    286   // No.7 Priority: ALTREF2_FRAME
    287 
    288   // cpi->ext_ref_frame_flags allows certain reference types to be disabled
    289   // by the external interface.  These are set by av1_apply_encoding_flags().
    290   // Start with what the external interface allows, then suppress any reference
    291   // types which we have found to be duplicates.
    292 
    293   int flags = cpi->ext_ref_frame_flags;
    294 
    295   if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
    296 
    297   if (alt_is_last) flags &= ~AOM_ALT_FLAG;
    298 
    299   if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
    300 
    301   if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
    302 
    303   if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
    304     flags &= ~AOM_GOLD_FLAG;
    305 
    306   if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 || bwd_is_gld))
    307     flags &= ~AOM_BWD_FLAG;
    308 
    309   if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
    310        alt2_is_gld || alt2_is_bwd))
    311     flags &= ~AOM_ALT2_FLAG;
    312 
    313   return flags;
    314 }
    315 
    316 static int get_current_frame_ref_type(
    317     const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) {
    318   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    319   // We choose the reference "type" of this frame from the flags which indicate
    320   // which reference frames will be refreshed by it.  More than one of these
    321   // flags may be set, so the order here implies an order of precedence.
    322   // This is just used to choose the primary_ref_frame (as the most recent
    323   // reference buffer of the same reference-type as the current frame)
    324 
    325   const int intra_only = frame_params->frame_type == KEY_FRAME ||
    326                          frame_params->frame_type == INTRA_ONLY_FRAME;
    327   if (intra_only || frame_params->error_resilient_mode ||
    328       cpi->ext_use_primary_ref_none)
    329     return REGULAR_FRAME;
    330   else if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
    331     return INTERNAL_ARF_FRAME;
    332   else if (frame_params->refresh_alt_ref_frame)
    333     return ARF_FRAME;
    334   else if (cpi->rc.is_src_frame_alt_ref)
    335     return OVERLAY_FRAME;
    336   else if (frame_params->refresh_golden_frame)
    337     return GLD_FRAME;
    338   else if (frame_params->refresh_bwd_ref_frame)
    339     return BRF_FRAME;
    340   else
    341     return REGULAR_FRAME;
    342 }
    343 
    344 static int choose_primary_ref_frame(
    345     const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params) {
    346   const AV1_COMMON *const cm = &cpi->common;
    347 
    348   const int intra_only = frame_params->frame_type == KEY_FRAME ||
    349                          frame_params->frame_type == INTRA_ONLY_FRAME;
    350   if (intra_only || frame_params->error_resilient_mode ||
    351       cpi->ext_use_primary_ref_none) {
    352     return PRIMARY_REF_NONE;
    353   }
    354 
    355   // Find the most recent reference frame with the same reference type as the
    356   // current frame
    357   const FRAME_CONTEXT_INDEX current_ref_type =
    358       get_current_frame_ref_type(cpi, frame_params);
    359   int wanted_fb = cpi->fb_of_context_type[current_ref_type];
    360 
    361   int primary_ref_frame = PRIMARY_REF_NONE;
    362   for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
    363     if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) {
    364       primary_ref_frame = ref_frame - LAST_FRAME;
    365     }
    366   }
    367   return primary_ref_frame;
    368 }
    369 
    370 static void update_fb_of_context_type(
    371     const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params,
    372     int *const fb_of_context_type) {
    373   const AV1_COMMON *const cm = &cpi->common;
    374 
    375   if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
    376       cpi->ext_use_primary_ref_none) {
    377     for (int i = 0; i < REF_FRAMES; i++) {
    378       fb_of_context_type[i] = -1;
    379     }
    380     fb_of_context_type[REGULAR_FRAME] =
    381         cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME)
    382                        : get_ref_frame_map_idx(cm, ALTREF_FRAME);
    383   }
    384 
    385   if (!encode_show_existing_frame(cm)) {
    386     // Refresh fb_of_context_type[]: see encoder.h for explanation
    387     if (cm->current_frame.frame_type == KEY_FRAME) {
    388       // All ref frames are refreshed, pick one that will live long enough
    389       fb_of_context_type[REGULAR_FRAME] = 0;
    390     } else {
    391       // If more than one frame is refreshed, it doesn't matter which one we
    392       // pick so pick the first.  LST sometimes doesn't refresh any: this is ok
    393       const int current_frame_ref_type =
    394           get_current_frame_ref_type(cpi, frame_params);
    395       for (int i = 0; i < REF_FRAMES; i++) {
    396         if (cm->current_frame.refresh_frame_flags & (1 << i)) {
    397           fb_of_context_type[current_frame_ref_type] = i;
    398           break;
    399         }
    400       }
    401     }
    402   }
    403 }
    404 
    405 static int get_order_offset(const GF_GROUP *const gf_group,
    406                             const EncodeFrameParams *const frame_params) {
    407   // shown frame by definition has order offset 0
    408   // show_existing_frame ignores order_offset and simply takes the order_hint
    409   // from the reference frame being shown.
    410   if (frame_params->show_frame || frame_params->show_existing_frame) return 0;
    411 
    412   const int arf_offset =
    413       AOMMIN((MAX_GF_INTERVAL - 1), gf_group->arf_src_offset[gf_group->index]);
    414   return AOMMIN((MAX_GF_INTERVAL - 1), arf_offset);
    415 }
    416 
    417 static void adjust_frame_rate(AV1_COMP *cpi,
    418                               const struct lookahead_entry *source) {
    419   int64_t this_duration;
    420   int step = 0;
    421 
    422   // Clear down mmx registers
    423   aom_clear_system_state();
    424 
    425   if (source->ts_start == cpi->first_time_stamp_ever) {
    426     this_duration = source->ts_end - source->ts_start;
    427     step = 1;
    428   } else {
    429     int64_t last_duration =
    430         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
    431 
    432     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
    433 
    434     // do a step update if the duration changes by 10%
    435     if (last_duration)
    436       step = (int)((this_duration - last_duration) * 10 / last_duration);
    437   }
    438 
    439   if (this_duration) {
    440     if (step) {
    441       av1_new_framerate(cpi, 10000000.0 / this_duration);
    442     } else {
    443       // Average this frame's rate into the last second's average
    444       // frame rate. If we haven't seen 1 second yet, then average
    445       // over the whole interval seen.
    446       const double interval = AOMMIN(
    447           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
    448       double avg_duration = 10000000.0 / cpi->framerate;
    449       avg_duration *= (interval - avg_duration + this_duration);
    450       avg_duration /= interval;
    451 
    452       av1_new_framerate(cpi, 10000000.0 / avg_duration);
    453     }
    454   }
    455   cpi->last_time_stamp_seen = source->ts_start;
    456   cpi->last_end_time_stamp_seen = source->ts_end;
    457 }
    458 
    459 // If this is an alt-ref, returns the offset of the source frame used
    460 // as the arf midpoint. Otherwise, returns 0.
    461 static int get_arf_src_index(AV1_COMP *cpi) {
    462   RATE_CONTROL *const rc = &cpi->rc;
    463   int arf_src_index = 0;
    464   if (cpi->oxcf.pass == 2) {
    465     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    466     if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
    467       assert(is_altref_enabled(cpi));
    468       arf_src_index = gf_group->arf_src_offset[gf_group->index];
    469     }
    470   } else if (rc->source_alt_ref_pending) {
    471     arf_src_index = rc->frames_till_gf_update_due;
    472   }
    473   return arf_src_index;
    474 }
    475 
    476 // If this is an internal alt-ref, returns the offset of the source frame used
    477 // as the internal arf midpoint. Otherwise, returns 0.
    478 static int get_internal_arf_src_index(AV1_COMP *cpi) {
    479   int internal_arf_src_index = 0;
    480   if (cpi->oxcf.pass == 2) {
    481     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    482     if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
    483       assert(is_altref_enabled(cpi) && cpi->internal_altref_allowed);
    484       internal_arf_src_index = gf_group->arf_src_offset[gf_group->index];
    485     }
    486   }
    487   return internal_arf_src_index;
    488 }
    489 
    490 // Called if this frame is an ARF or ARF2. Also handles forward-keyframes
    491 // For an ARF set arf2=0, for ARF2 set arf2=1
    492 // temporal_filtered is set to 1 if we temporally filter the ARF frame, so that
    493 // the correct post-filter buffer can be used.
    494 static struct lookahead_entry *setup_arf_or_arf2(
    495     AV1_COMP *const cpi, const int arf_src_index, const int arf2,
    496     int *temporal_filtered, EncodeFrameParams *const frame_params) {
    497   AV1_COMMON *const cm = &cpi->common;
    498   RATE_CONTROL *const rc = &cpi->rc;
    499   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    500 
    501   assert(arf_src_index <= rc->frames_to_key);
    502   *temporal_filtered = 0;
    503 
    504   struct lookahead_entry *source =
    505       av1_lookahead_peek(cpi->lookahead, arf_src_index);
    506 
    507   if (source != NULL) {
    508     cm->showable_frame = 1;
    509     cpi->alt_ref_source = source;
    510 
    511     // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf
    512     if (!arf2 && arf_src_index == rc->frames_to_key) {
    513       // Skip temporal filtering and mark as intra_only if we have a fwd_kf
    514       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    515       int which_arf = gf_group->arf_update_idx[gf_group->index];
    516       cpi->is_arf_filter_off[which_arf] = 1;
    517       cpi->no_show_kf = 1;
    518     } else {
    519       if (oxcf->arnr_max_frames > 0) {
    520         // Produce the filtered ARF frame.
    521         av1_temporal_filter(cpi, arf_src_index);
    522         aom_extend_frame_borders(&cpi->alt_ref_buffer, av1_num_planes(cm));
    523         *temporal_filtered = 1;
    524       }
    525     }
    526     frame_params->show_frame = 0;
    527   }
    528   rc->source_alt_ref_pending = 0;
    529   return source;
    530 }
    531 
    532 // Determine whether there is a forced keyframe pending in the lookahead buffer
    533 static int is_forced_keyframe_pending(struct lookahead_ctx *lookahead,
    534                                       const int up_to_index) {
    535   for (int i = 0; i <= up_to_index; i++) {
    536     const struct lookahead_entry *e = av1_lookahead_peek(lookahead, i);
    537     if (e == NULL) {
    538       // We have reached the end of the lookahead buffer and not early-returned
    539       // so there isn't a forced key-frame pending.
    540       return 0;
    541     } else if (e->flags == AOM_EFLAG_FORCE_KF) {
    542       return 1;
    543     } else {
    544       continue;
    545     }
    546   }
    547   return 0;  // Never reached
    548 }
    549 
    550 // Check if we should encode an ARF or internal ARF.  If not, try a LAST
    551 // Do some setup associated with the chosen source
    552 // temporal_filtered, flush, and frame_update_type are outputs.
    553 // Return the frame source, or NULL if we couldn't find one
    554 struct lookahead_entry *choose_frame_source(
    555     AV1_COMP *const cpi, int *const temporal_filtered, int *const flush,
    556     struct lookahead_entry **last_source, FRAME_UPDATE_TYPE *frame_update_type,
    557     EncodeFrameParams *const frame_params) {
    558   AV1_COMMON *const cm = &cpi->common;
    559   struct lookahead_entry *source = NULL;
    560   *temporal_filtered = 0;
    561 
    562   // Should we encode an alt-ref frame.
    563   int arf_src_index = get_arf_src_index(cpi);
    564   if (arf_src_index &&
    565       is_forced_keyframe_pending(cpi->lookahead, arf_src_index)) {
    566     arf_src_index = 0;
    567     *flush = 1;
    568   }
    569 
    570   if (arf_src_index) {
    571     source = setup_arf_or_arf2(cpi, arf_src_index, 0, temporal_filtered,
    572                                frame_params);
    573     *frame_update_type = ARF_UPDATE;
    574   }
    575 
    576   // Should we encode an internal Alt-ref frame (mutually exclusive to ARF)
    577   arf_src_index = get_internal_arf_src_index(cpi);
    578   if (arf_src_index &&
    579       is_forced_keyframe_pending(cpi->lookahead, arf_src_index)) {
    580     arf_src_index = 0;
    581     *flush = 1;
    582   }
    583 
    584   if (arf_src_index) {
    585     source = setup_arf_or_arf2(cpi, arf_src_index, 1, temporal_filtered,
    586                                frame_params);
    587     *frame_update_type = INTNL_ARF_UPDATE;
    588   }
    589 
    590   if (!source) {
    591     // Get last frame source.
    592     if (cm->current_frame.frame_number > 0) {
    593       *last_source = av1_lookahead_peek(cpi->lookahead, -1);
    594     }
    595     // Read in the source frame.
    596     source = av1_lookahead_pop(cpi->lookahead, *flush);
    597     if (source == NULL) return NULL;
    598     *frame_update_type = LF_UPDATE;  // Default update type
    599     frame_params->show_frame = 1;
    600 
    601     // Check to see if the frame should be encoded as an arf overlay.
    602     if (cpi->alt_ref_source == source) {
    603       *frame_update_type = OVERLAY_UPDATE;
    604       cpi->alt_ref_source = NULL;
    605     }
    606   }
    607   return source;
    608 }
    609 
    610 // Don't allow a show_existing_frame to coincide with an error resilient or
    611 // S-Frame. An exception can be made in the case of a keyframe, since it does
    612 // not depend on any previous frames.
    613 static int allow_show_existing(const AV1_COMP *const cpi,
    614                                unsigned int frame_flags) {
    615   if (cpi->common.current_frame.frame_number == 0) return 0;
    616 
    617   const struct lookahead_entry *lookahead_src =
    618       av1_lookahead_peek(cpi->lookahead, 0);
    619   if (lookahead_src == NULL) return 1;
    620 
    621   const int is_error_resilient =
    622       cpi->oxcf.error_resilient_mode ||
    623       (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
    624   const int is_s_frame =
    625       cpi->oxcf.s_frame_mode || (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
    626   const int is_key_frame =
    627       (cpi->rc.frames_to_key == 0) || (frame_flags & FRAMEFLAGS_KEY);
    628   return !(is_error_resilient || is_s_frame) || is_key_frame;
    629 }
    630 
    631 // Update frame_flags to tell the encoder's caller what sort of frame was
    632 // encoded.
    633 static void update_frame_flags(AV1_COMP *cpi, unsigned int *frame_flags) {
    634   if (encode_show_existing_frame(&cpi->common)) {
    635     *frame_flags &= ~FRAMEFLAGS_GOLDEN;
    636     *frame_flags &= ~FRAMEFLAGS_BWDREF;
    637     *frame_flags &= ~FRAMEFLAGS_ALTREF;
    638     *frame_flags &= ~FRAMEFLAGS_KEY;
    639     return;
    640   }
    641 
    642   if (cpi->refresh_golden_frame == 1) {
    643     *frame_flags |= FRAMEFLAGS_GOLDEN;
    644   } else {
    645     *frame_flags &= ~FRAMEFLAGS_GOLDEN;
    646   }
    647 
    648   if (cpi->refresh_alt_ref_frame == 1) {
    649     *frame_flags |= FRAMEFLAGS_ALTREF;
    650   } else {
    651     *frame_flags &= ~FRAMEFLAGS_ALTREF;
    652   }
    653 
    654   if (cpi->refresh_bwd_ref_frame == 1) {
    655     *frame_flags |= FRAMEFLAGS_BWDREF;
    656   } else {
    657     *frame_flags &= ~FRAMEFLAGS_BWDREF;
    658   }
    659 
    660   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
    661     *frame_flags |= FRAMEFLAGS_KEY;
    662   } else {
    663     *frame_flags &= ~FRAMEFLAGS_KEY;
    664   }
    665 }
    666 
    667 #define DUMP_REF_FRAME_IMAGES 0
    668 
    669 #if DUMP_REF_FRAME_IMAGES == 1
    670 static int dump_one_image(AV1_COMMON *cm,
    671                           const YV12_BUFFER_CONFIG *const ref_buf,
    672                           char *file_name) {
    673   int h;
    674   FILE *f_ref = NULL;
    675 
    676   if (ref_buf == NULL) {
    677     printf("Frame data buffer is NULL.\n");
    678     return AOM_CODEC_MEM_ERROR;
    679   }
    680 
    681   if ((f_ref = fopen(file_name, "wb")) == NULL) {
    682     printf("Unable to open file %s to write.\n", file_name);
    683     return AOM_CODEC_MEM_ERROR;
    684   }
    685 
    686   // --- Y ---
    687   for (h = 0; h < cm->height; ++h) {
    688     fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
    689   }
    690   // --- U ---
    691   for (h = 0; h < (cm->height >> 1); ++h) {
    692     fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
    693            f_ref);
    694   }
    695   // --- V ---
    696   for (h = 0; h < (cm->height >> 1); ++h) {
    697     fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
    698            f_ref);
    699   }
    700 
    701   fclose(f_ref);
    702 
    703   return AOM_CODEC_OK;
    704 }
    705 
    706 static void dump_ref_frame_images(AV1_COMP *cpi) {
    707   AV1_COMMON *const cm = &cpi->common;
    708   MV_REFERENCE_FRAME ref_frame;
    709 
    710   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    711     char file_name[256] = "";
    712     snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
    713              cm->current_frame.frame_number, ref_frame);
    714     dump_one_image(cm, get_ref_frame_yv12_buf(cpi, ref_frame), file_name);
    715   }
    716 }
    717 #endif  // DUMP_REF_FRAME_IMAGES == 1
    718 
    719 // Assign new_ref in the new mapping to point at the reference buffer pointed at
    720 // by old_ref in the old_map.  The new mapping is stored in *new_map, while the
    721 // old map comes from cm->remapped_ref_idx[].
    722 static void assign_new_map(AV1_COMMON *const cm, int *new_map, int new_ref,
    723                            int old_ref) {
    724   new_map[new_ref - LAST_FRAME] = cm->remapped_ref_idx[old_ref - LAST_FRAME];
    725 }
    726 
    727 // Generate a new reference frame mapping.  This function updates
    728 // cm->remapped_ref_idx[] depending on the frame_update_type of this frame.
    729 // This determines which references (e.g. LAST_FRAME, ALTREF_FRAME) point at the
    730 // 8 underlying buffers and, together with get_refresh_frame_flags(), implements
    731 // our reference frame management strategy.
    732 static void update_ref_frame_map(AV1_COMP *cpi,
    733                                  FRAME_UPDATE_TYPE frame_update_type) {
    734   AV1_COMMON *const cm = &cpi->common;
    735 
    736   // If check_frame_refs_short_signaling() decided to set
    737   // frame_refs_short_signaling=1 then we update remapped_ref_idx[] here.  Every
    738   // reference will still map to the same RefCntBuffer (through ref_frame_map[])
    739   // after this, but that does not necessarily mean that remapped_ref_idx[] is
    740   // unchanged.
    741   if (cm->current_frame.frame_refs_short_signaling) {
    742     const int lst_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME);
    743     const int gld_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME);
    744     av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_map_idx, gld_map_idx);
    745   }
    746 
    747   // For shown keyframes and S-frames all buffers are refreshed, but we don't
    748   // change any of the mapping.
    749   if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
    750       frame_is_sframe(cm)) {
    751     return;
    752   }
    753 
    754   // Initialize the new reference map as a copy of the old one.
    755   int new_map[REF_FRAMES];
    756   memcpy(new_map, cm->remapped_ref_idx, sizeof(new_map));
    757 
    758   // The reference management strategy is currently as follows.  See
    759   // gop_structure.c for more details of the structure and DOI
    760   // 10.1109/DCC.2018.00045 for a higher-level explanation
    761   //
    762   // * ALTREF_FRAME and GOLDEN_FRAME are kept separate from the other
    763   //   references.  When we code an ALTREF it refreshes the ALTREF buffer.  When
    764   //   we code an OVERLAY the old GOLDEN becomes the new ALTREF and the old
    765   //   ALTREF (possibly refreshed by the OVERLAY) becomes the new GOLDEN.
    766   // * LAST_FRAME, LAST2_FRAME, and LAST3_FRAME work like a FIFO.  When we code
    767   //   a frame which does a last-frame update we pick a buffer to refresh and
    768   //   then point the LAST_FRAME reference at it.  The old LAST_FRAME becomes
    769   //   LAST2_FRAME and the old LAST2_FRAME becomes LAST3_FRAME.  The old
    770   //   LAST3_FRAME is re-used somewhere else.
    771   // * BWDREF, ALTREF2, and EXTREF act like a stack structure, so we can
    772   //   "push" and "pop" internal alt-ref frames through the three references.
    773   // * When we code a BRF or internal-ARF (they work the same in this
    774   //   structure) we push it onto the bwdref stack.  Because we have a finite
    775   //   number of buffers, we actually refresh EXTREF, the bottom of the stack,
    776   //   and rotate the three references to make EXTREF the top.
    777   // * When we code an INTNL_OVERLAY we refresh BWDREF, then pop it off of the
    778   //   bwdref stack and push it into the last-frame FIFO.  The old LAST3
    779   //   buffer gets pushed out of the last-frame FIFO and becomes the new
    780   //   EXTREF, bottom of the bwdref stack.
    781   // * LAST_BIPRED just acts like a LAST_FRAME.  The BWDREF will have an
    782   //   INTNL_OVERLAY and so can do its own ref map update.
    783   //
    784   // Note that this function runs *after* a frame has been coded, so it does not
    785   // affect reference assignment of the current frame, it only affects future
    786   // frames.  This is why we refresh buffers using the old reference map before
    787   // remapping them.
    788   //
    789   // show_existing_frames don't refresh any buffers or send the reference map to
    790   // the decoder, but we can still update our reference map if we want to: the
    791   // decoder will update its map next time we code a non-show-existing frame.
    792 
    793   if (frame_update_type == OVERLAY_UPDATE) {
    794     // We want the old golden-frame to become our new ARF so swap the
    795     // references.  If cpi->preserve_arf_as_gld == 0 then we will refresh the
    796     // old ARF before it becomes our new GF
    797     assign_new_map(cm, new_map, ALTREF_FRAME, GOLDEN_FRAME);
    798     assign_new_map(cm, new_map, GOLDEN_FRAME, ALTREF_FRAME);
    799   } else if (frame_update_type == INTNL_OVERLAY_UPDATE &&
    800              encode_show_existing_frame(cm)) {
    801     // Note that because encode_show_existing_frame(cm) we don't refresh any
    802     // buffers.
    803     // Pop BWDREF (shown as current frame) from the bwdref stack and make it
    804     // the new LAST_FRAME.
    805     assign_new_map(cm, new_map, LAST_FRAME, BWDREF_FRAME);
    806 
    807     // Progress the last-frame FIFO and the bwdref stack
    808     assign_new_map(cm, new_map, LAST2_FRAME, LAST_FRAME);
    809     assign_new_map(cm, new_map, LAST3_FRAME, LAST2_FRAME);
    810     assign_new_map(cm, new_map, BWDREF_FRAME, ALTREF2_FRAME);
    811     assign_new_map(cm, new_map, ALTREF2_FRAME, EXTREF_FRAME);
    812     assign_new_map(cm, new_map, EXTREF_FRAME, LAST3_FRAME);
    813   } else if (frame_update_type == INTNL_ARF_UPDATE &&
    814              !cm->show_existing_frame) {
    815     // We want to push the current frame onto the bwdref stack.  We refresh
    816     // EXTREF (the old bottom of the stack) and rotate the references so it
    817     // becomes BWDREF, the top of the stack.
    818     assign_new_map(cm, new_map, BWDREF_FRAME, EXTREF_FRAME);
    819     assign_new_map(cm, new_map, ALTREF2_FRAME, BWDREF_FRAME);
    820     assign_new_map(cm, new_map, EXTREF_FRAME, ALTREF2_FRAME);
    821   }
    822 
    823   if ((frame_update_type == LF_UPDATE || frame_update_type == GF_UPDATE ||
    824        frame_update_type == INTNL_OVERLAY_UPDATE) &&
    825       !encode_show_existing_frame(cm) &&
    826       (!cm->show_existing_frame || frame_update_type == INTNL_OVERLAY_UPDATE)) {
    827     // A standard last-frame: we refresh the LAST3_FRAME buffer and then push it
    828     // into the last-frame FIFO.
    829     assign_new_map(cm, new_map, LAST3_FRAME, LAST2_FRAME);
    830     assign_new_map(cm, new_map, LAST2_FRAME, LAST_FRAME);
    831     assign_new_map(cm, new_map, LAST_FRAME, LAST3_FRAME);
    832   }
    833 
    834   memcpy(cm->remapped_ref_idx, new_map, sizeof(new_map));
    835 
    836 #if DUMP_REF_FRAME_IMAGES == 1
    837   // Dump out all reference frame images.
    838   dump_ref_frame_images(cpi);
    839 #endif  // DUMP_REF_FRAME_IMAGES
    840 }
    841 
    842 static int get_refresh_frame_flags(const AV1_COMP *const cpi,
    843                                    const EncodeFrameParams *const frame_params,
    844                                    FRAME_UPDATE_TYPE frame_update_type) {
    845   const AV1_COMMON *const cm = &cpi->common;
    846 
    847   // Switch frames and shown key-frames overwrite all reference slots
    848   if ((frame_params->frame_type == KEY_FRAME && frame_params->show_frame) ||
    849       frame_params->frame_type == S_FRAME)
    850     return 0xFF;
    851 
    852   // show_existing_frames don't actually send refresh_frame_flags so set the
    853   // flags to 0 to keep things consistent.
    854   if (frame_params->show_existing_frame &&
    855       (!frame_params->error_resilient_mode ||
    856        frame_params->frame_type == KEY_FRAME)) {
    857     return 0;
    858   }
    859 
    860   int refresh_mask = 0;
    861 
    862   if (cpi->ext_refresh_frame_flags_pending) {
    863     // Unfortunately the encoder interface reflects the old refresh_*_frame
    864     // flags so we have to replicate the old refresh_frame_flags logic here in
    865     // order to preserve the behaviour of the flag overrides.
    866     refresh_mask |= cpi->ext_refresh_last_frame
    867                     << get_ref_frame_map_idx(cm, LAST3_FRAME);
    868     refresh_mask |= cpi->ext_refresh_bwd_ref_frame
    869                     << get_ref_frame_map_idx(cm, EXTREF_FRAME);
    870     refresh_mask |= cpi->ext_refresh_alt2_ref_frame
    871                     << get_ref_frame_map_idx(cm, ALTREF2_FRAME);
    872     if (frame_update_type == OVERLAY_UPDATE) {
    873       if (!cpi->preserve_arf_as_gld) {
    874         refresh_mask |= cpi->ext_refresh_golden_frame
    875                         << get_ref_frame_map_idx(cm, ALTREF_FRAME);
    876       }
    877     } else {
    878       refresh_mask |= cpi->ext_refresh_golden_frame
    879                       << get_ref_frame_map_idx(cm, GOLDEN_FRAME);
    880       refresh_mask |= cpi->ext_refresh_alt_ref_frame
    881                       << get_ref_frame_map_idx(cm, ALTREF_FRAME);
    882     }
    883     return refresh_mask;
    884   }
    885 
    886   // See update_ref_frame_map() for a thorough description of the reference
    887   // buffer management strategy currently in use.  This function just decides
    888   // which buffers should be refreshed.
    889 
    890   switch (frame_update_type) {
    891     case KF_UPDATE:
    892       // Note that a real shown key-frame or S-frame refreshes every buffer,
    893       // handled in a special case above.  This case is for frames which aren't
    894       // really a shown key-frame or S-frame but want to refresh all the
    895       // important buffers.
    896       refresh_mask |= 1 << get_ref_frame_map_idx(cm, LAST3_FRAME);
    897       refresh_mask |= 1 << get_ref_frame_map_idx(cm, EXTREF_FRAME);
    898       refresh_mask |= 1 << get_ref_frame_map_idx(cm, ALTREF2_FRAME);
    899       refresh_mask |= 1 << get_ref_frame_map_idx(cm, GOLDEN_FRAME);
    900       refresh_mask |= 1 << get_ref_frame_map_idx(cm, ALTREF_FRAME);
    901       break;
    902     case LF_UPDATE:
    903       // Refresh LAST3, which becomes the new LAST while LAST becomes LAST2
    904       // and LAST2 becomes the new LAST3 (like a FIFO but circular)
    905       refresh_mask |= 1 << get_ref_frame_map_idx(cm, LAST3_FRAME);
    906       break;
    907     case GF_UPDATE:
    908       // In addition to refreshing the GF buffer, we refresh LAST3 and push it
    909       // into the last-frame FIFO.
    910       refresh_mask |= 1 << get_ref_frame_map_idx(cm, LAST3_FRAME);
    911       refresh_mask |= 1 << get_ref_frame_map_idx(cm, GOLDEN_FRAME);
    912       break;
    913     case OVERLAY_UPDATE:
    914       if (!cpi->preserve_arf_as_gld) {
    915         // The result of our OVERLAY should become the GOLDEN_FRAME but we'd
    916         // like to keep the old GOLDEN as our new ALTREF.  So we refresh the
    917         // ALTREF and swap around the ALTREF and GOLDEN references.
    918         refresh_mask |= 1 << get_ref_frame_map_idx(cm, ALTREF_FRAME);
    919       }
    920       break;
    921     case ARF_UPDATE:
    922       refresh_mask |= 1 << get_ref_frame_map_idx(cm, ALTREF_FRAME);
    923       break;
    924     case INTNL_OVERLAY_UPDATE:
    925       // INTNL_OVERLAY may be a show_existing_frame in which case we don't
    926       // refresh anything and the BWDREF or ALTREF2 being shown becomes the new
    927       // LAST_FRAME.  But, if it's not a show_existing_frame, then we update as
    928       // though it's a normal LF_UPDATE: we refresh LAST3 and
    929       // update_ref_frame_map() makes that the new LAST_FRAME.
    930       refresh_mask |= 1 << get_ref_frame_map_idx(cm, LAST3_FRAME);
    931       break;
    932     case INTNL_ARF_UPDATE:
    933       if (cpi->oxcf.pass == 2) {
    934         // Push the new ARF2 onto the bwdref stack.  We refresh EXTREF which is
    935         // at the bottom of the stack then move it to the top.
    936         refresh_mask |= 1 << get_ref_frame_map_idx(cm, EXTREF_FRAME);
    937       } else {
    938         // ARF2 just gets stored in the ARF2 slot, no reference map change.
    939         refresh_mask |= 1 << get_ref_frame_map_idx(cm, ALTREF2_FRAME);
    940       }
    941       break;
    942     default: assert(0); break;
    943   }
    944   return refresh_mask;
    945 }
    946 
    947 int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size,
    948                         uint8_t *const dest, unsigned int *frame_flags,
    949                         int64_t *const time_stamp, int64_t *const time_end,
    950                         const aom_rational_t *const timebase, int flush) {
    951   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    952   AV1_COMMON *const cm = &cpi->common;
    953 
    954   EncodeFrameInput frame_input;
    955   EncodeFrameParams frame_params;
    956   EncodeFrameResults frame_results;
    957   memset(&frame_input, 0, sizeof(frame_input));
    958   memset(&frame_params, 0, sizeof(frame_params));
    959   memset(&frame_results, 0, sizeof(frame_results));
    960 
    961   if (oxcf->pass == 0 || oxcf->pass == 2) {
    962     check_show_existing_frame(cpi, &frame_params);
    963     frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags);
    964   } else {
    965     frame_params.show_existing_frame = 0;
    966   }
    967 
    968   int temporal_filtered = 0;
    969   struct lookahead_entry *source = NULL;
    970   struct lookahead_entry *last_source = NULL;
    971   FRAME_UPDATE_TYPE frame_update_type;
    972   if (frame_params.show_existing_frame) {
    973     source = av1_lookahead_pop(cpi->lookahead, flush);
    974     frame_update_type = LF_UPDATE;
    975   } else {
    976     source = choose_frame_source(cpi, &temporal_filtered, &flush, &last_source,
    977                                  &frame_update_type, &frame_params);
    978   }
    979 
    980   // In pass 2 we get the frame_update_type from gf_group
    981   if (oxcf->pass == 2) {
    982     frame_update_type =
    983         cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
    984   }
    985 
    986   if (source == NULL) {  // If no source was found, we can't encode a frame.
    987     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
    988       av1_end_first_pass(cpi); /* get last stats packet */
    989       cpi->twopass.first_pass_done = 1;
    990     }
    991     return -1;
    992   }
    993 
    994   frame_input.source = temporal_filtered ? &cpi->alt_ref_buffer : &source->img;
    995   frame_input.last_source = last_source != NULL ? &last_source->img : NULL;
    996   frame_input.ts_duration = source->ts_end - source->ts_start;
    997 
    998   *time_stamp = source->ts_start;
    999   *time_end = source->ts_end;
   1000   if (source->ts_start < cpi->first_time_stamp_ever) {
   1001     cpi->first_time_stamp_ever = source->ts_start;
   1002     cpi->last_end_time_stamp_seen = source->ts_start;
   1003   }
   1004 
   1005   av1_apply_encoding_flags(cpi, source->flags);
   1006   if (!frame_params.show_existing_frame)
   1007     *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
   1008 
   1009   const int is_overlay = frame_params.show_existing_frame &&
   1010                          (frame_update_type == OVERLAY_UPDATE ||
   1011                           frame_update_type == INTNL_OVERLAY_UPDATE);
   1012   if (frame_params.show_frame || is_overlay) {
   1013     // Shown frames and arf-overlay frames need frame-rate considering
   1014     adjust_frame_rate(cpi, source);
   1015   }
   1016 
   1017   if (frame_params.show_existing_frame) {
   1018     // show_existing_frame implies this frame is shown!
   1019     frame_params.show_frame = 1;
   1020   } else {
   1021     if (cpi->film_grain_table) {
   1022       cm->cur_frame->film_grain_params_present = aom_film_grain_table_lookup(
   1023           cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
   1024           &cm->film_grain_params);
   1025     } else {
   1026       cm->cur_frame->film_grain_params_present =
   1027           cm->seq_params.film_grain_params_present;
   1028     }
   1029     // only one operating point supported now
   1030     const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
   1031     if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
   1032     cpi->common.frame_presentation_time = (uint32_t)pts64;
   1033   }
   1034 
   1035   if (oxcf->pass == 2 && (!frame_params.show_existing_frame || is_overlay)) {
   1036     // GF_GROUP needs updating for arf overlays as well as non-show-existing
   1037     av1_get_second_pass_params(cpi, &frame_params, *frame_flags);
   1038     frame_update_type =
   1039         cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
   1040   }
   1041 
   1042   if (frame_params.show_existing_frame &&
   1043       frame_params.frame_type != KEY_FRAME) {
   1044     // Force show-existing frames to be INTER, except forward keyframes
   1045     frame_params.frame_type = INTER_FRAME;
   1046   }
   1047 
   1048   // TODO(david.turner (at) argondesign.com): Move all the encode strategy
   1049   // (largely near av1_get_compressed_data) in here
   1050 
   1051   // TODO(david.turner (at) argondesign.com): Change all the encode strategy to
   1052   // modify frame_params instead of cm or cpi.
   1053 
   1054   // Per-frame encode speed.  In theory this can vary, but things may have been
   1055   // written assuming speed-level will not change within a sequence, so this
   1056   // parameter should be used with caution.
   1057   frame_params.speed = oxcf->speed;
   1058 
   1059   if (!frame_params.show_existing_frame) {
   1060     cm->using_qmatrix = cpi->oxcf.using_qm;
   1061     cm->min_qmlevel = cpi->oxcf.qm_minlevel;
   1062     cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
   1063     if (cpi->twopass.gf_group.index == 1 && cpi->oxcf.enable_tpl_model) {
   1064       av1_configure_buffer_updates(cpi, &frame_params, frame_update_type, 0);
   1065       av1_set_frame_size(cpi, cm->width, cm->height);
   1066       av1_tpl_setup_stats(cpi, &frame_input);
   1067     }
   1068   }
   1069 
   1070   // Work out some encoding parameters specific to the pass:
   1071   if (oxcf->pass == 0) {
   1072     if (cpi->oxcf.rc_mode == AOM_CBR) {
   1073       av1_rc_get_one_pass_cbr_params(cpi, &frame_update_type, &frame_params,
   1074                                      *frame_flags);
   1075     } else {
   1076       av1_rc_get_one_pass_vbr_params(cpi, &frame_update_type, &frame_params,
   1077                                      *frame_flags);
   1078     }
   1079   } else if (oxcf->pass == 1) {
   1080     cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&cpi->oxcf);
   1081     const int kf_requested = (cm->current_frame.frame_number == 0 ||
   1082                               (*frame_flags & FRAMEFLAGS_KEY));
   1083     if (kf_requested && frame_update_type != OVERLAY_UPDATE &&
   1084         frame_update_type != INTNL_OVERLAY_UPDATE) {
   1085       frame_params.frame_type = KEY_FRAME;
   1086     } else {
   1087       frame_params.frame_type = INTER_FRAME;
   1088     }
   1089   } else if (oxcf->pass == 2) {
   1090 #if CONFIG_MISMATCH_DEBUG
   1091     mismatch_move_frame_idx_w();
   1092 #endif
   1093 #if TXCOEFF_COST_TIMER
   1094     cm->txcoeff_cost_timer = 0;
   1095     cm->txcoeff_cost_count = 0;
   1096 #endif
   1097   }
   1098 
   1099   if (oxcf->pass == 0 || oxcf->pass == 2) set_ext_overrides(cpi, &frame_params);
   1100 
   1101   // Shown keyframes and S frames refresh all reference buffers
   1102   const int force_refresh_all =
   1103       ((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) ||
   1104        frame_params.frame_type == S_FRAME) &&
   1105       !frame_params.show_existing_frame;
   1106 
   1107   av1_configure_buffer_updates(cpi, &frame_params, frame_update_type,
   1108                                force_refresh_all);
   1109 
   1110   if (oxcf->pass == 0 || oxcf->pass == 2) {
   1111     // Work out which reference frame slots may be used.
   1112     frame_params.ref_frame_flags = get_ref_frame_flags(cpi);
   1113 
   1114     frame_params.primary_ref_frame =
   1115         choose_primary_ref_frame(cpi, &frame_params);
   1116     frame_params.order_offset =
   1117         get_order_offset(&cpi->twopass.gf_group, &frame_params);
   1118 
   1119     frame_params.refresh_frame_flags =
   1120         get_refresh_frame_flags(cpi, &frame_params, frame_update_type);
   1121   }
   1122 
   1123   // The way frame_params->remapped_ref_idx is setup is a placeholder.
   1124   // Currently, reference buffer assignment is done by update_ref_frame_map()
   1125   // which is called by high-level strategy AFTER encoding a frame.  It modifies
   1126   // cm->remapped_ref_idx.  If you want to use an alternative method to
   1127   // determine reference buffer assignment, just put your assignments into
   1128   // frame_params->remapped_ref_idx here and they will be used when encoding
   1129   // this frame.  If frame_params->remapped_ref_idx is setup independently of
   1130   // cm->remapped_ref_idx then update_ref_frame_map() will have no effect.
   1131   memcpy(frame_params.remapped_ref_idx, cm->remapped_ref_idx,
   1132          REF_FRAMES * sizeof(*cm->remapped_ref_idx));
   1133 
   1134   if (av1_encode(cpi, dest, &frame_input, &frame_params, &frame_results) !=
   1135       AOM_CODEC_OK) {
   1136     return AOM_CODEC_ERROR;
   1137   }
   1138 
   1139   if (oxcf->pass == 0 || oxcf->pass == 2) {
   1140     // First pass doesn't modify reference buffer assignment or produce frame
   1141     // flags
   1142     update_frame_flags(cpi, frame_flags);
   1143     update_ref_frame_map(cpi, frame_update_type);
   1144   }
   1145 
   1146   if (oxcf->pass == 2) {
   1147 #if TXCOEFF_COST_TIMER
   1148     cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
   1149     fprintf(stderr,
   1150             "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
   1151             "in us\n",
   1152             cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
   1153             cm->cum_txcoeff_cost_timer);
   1154 #endif
   1155     av1_twopass_postencode_update(cpi);
   1156   }
   1157 
   1158   if (oxcf->pass == 0 || oxcf->pass == 2) {
   1159     update_fb_of_context_type(cpi, &frame_params, cpi->fb_of_context_type);
   1160     set_additional_frame_flags(cm, frame_flags);
   1161     update_rc_counts(cpi);
   1162   }
   1163 
   1164   // Unpack frame_results:
   1165   *size = frame_results.size;
   1166 
   1167   // Leave a signal for a higher level caller about if this frame is droppable
   1168   if (*size > 0) {
   1169     cpi->droppable = is_frame_droppable(cpi);
   1170   }
   1171 
   1172   return AOM_CODEC_OK;
   1173 }
   1174