LCOV - code coverage report
Current view: top level - Codec - EbPictureDecisionProcess.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1372 1778 77.2 %
Date: 2019-11-25 17:12:20 Functions: 17 22 77.3 %

          Line data    Source code
       1             : /*
       2             : * Copyright(c) 2019 Intel Corporation
       3             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       4             : */
       5             : /*
       6             : * Copyright(c) 2019 Netflix, Inc.
       7             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       8             : */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : #include <limits.h>
      13             : 
      14             : #include "EbDefinitions.h"
      15             : #include "EbUtility.h"
      16             : #include "EbPictureControlSet.h"
      17             : #include "EbSequenceControlSet.h"
      18             : #include "EbPictureAnalysisProcess.h"
      19             : #include "EbPictureAnalysisResults.h"
      20             : #include "EbPictureDecisionProcess.h"
      21             : #include "EbPictureDecisionResults.h"
      22             : #include "EbReferenceObject.h"
      23             : #include "EbSvtAv1ErrorCodes.h"
      24             : #include "EbTemporalFiltering.h"
      25             : 
      26             : /************************************************
      27             :  * Defines
      28             :  ************************************************/
      29             : #define  LAY0_OFF  0
      30             : #define  LAY1_OFF  3
      31             : #define  LAY2_OFF  5
      32             : #if PRED_CHANGE_5L
      33             : #define  LAY3_OFF  6
      34             : #define  LAY4_OFF  7
      35             : #endif
      36             : extern PredictionStructureConfigEntry four_level_hierarchical_pred_struct[];
      37             : extern PredictionStructureConfigEntry five_level_hierarchical_pred_struct[];
      38             : 
      39         678 : uint64_t  get_ref_poc(PictureDecisionContext *context, uint64_t curr_picture_number, int32_t delta_poc)
      40             : {
      41             :     uint64_t ref_poc;
      42             : 
      43         678 :     if ((int64_t)curr_picture_number - (int64_t)delta_poc < (int64_t)context->key_poc)
      44          22 :         ref_poc = context->key_poc;
      45             :     else
      46         656 :         ref_poc = curr_picture_number - delta_poc;
      47             : 
      48         678 :     return ref_poc;
      49             : }
      50             : 
      51             : typedef struct {
      52             :     MvReferenceFrame ref_type;
      53             :     int used;
      54             :     uint64_t poc;
      55             : } RefFrameInfo;
      56             : 
      57             : MvReferenceFrame svt_get_ref_frame_type(uint8_t list, uint8_t ref_idx);
      58             : 
      59        1675 : static INLINE int get_relative_dist(const OrderHintInfo *oh, int a, int b) {
      60        1675 :     if (!oh->enable_order_hint) return 0;
      61             : 
      62        1675 :     const int bits = oh->order_hint_bits;
      63             : 
      64        1675 :     assert(bits >= 1);
      65        1675 :     assert(a >= 0 && a < (1 << bits));
      66        1675 :     assert(b >= 0 && b < (1 << bits));
      67             : 
      68        1675 :     int diff = a - b;
      69        1675 :     const int m = 1 << (bits - 1);
      70        1675 :     diff = (diff & (m - 1)) - (diff & m);
      71        1675 :     return diff;
      72             : }
      73             : 
      74         120 : void eb_av1_setup_skip_mode_allowed(PictureParentControlSet  *parent_pcs_ptr) {
      75             : 
      76         120 :     FrameHeader *frm_hdr = &parent_pcs_ptr->frm_hdr;
      77             : 
      78             :     RefFrameInfo ref_frame_arr_single[7];
      79             : 
      80         960 :     for (uint8_t i = 0; i < 7; ++i)
      81         840 :         ref_frame_arr_single[i].used = 1;
      82             : 
      83         960 :     for (uint8_t i = 0; i < 7; ++i) {
      84         840 :         ref_frame_arr_single[i].poc = parent_pcs_ptr->av1_ref_signal.ref_poc_array[i] % (uint64_t)(1 << (parent_pcs_ptr->sequence_control_set_ptr->seq_header.order_hint_info.order_hint_bits));
      85             :     }
      86             : 
      87             :     OrderHintInfo order_hint_info_st;
      88         120 :     order_hint_info_st.enable_order_hint = 1;
      89         120 :     order_hint_info_st.order_hint_bits = 6+1;
      90             : 
      91         120 :     const OrderHintInfo *const order_hint_info = &order_hint_info_st;// cm->seq_params.order_hint_info;
      92         120 :     SkipModeInfo *const skip_mode_info = &frm_hdr->skip_mode_params;// cm->current_frame.skip_mode_info;
      93             : 
      94         120 :     skip_mode_info->skip_mode_allowed = 0;
      95         120 :     skip_mode_info->ref_frame_idx_0 = INVALID_IDX;
      96         120 :     skip_mode_info->ref_frame_idx_1 = INVALID_IDX;
      97         120 :     parent_pcs_ptr->cur_order_hint = parent_pcs_ptr->picture_number % (uint64_t)(1 << (parent_pcs_ptr->sequence_control_set_ptr->seq_header.order_hint_info.order_hint_bits));
      98         960 :     for (uint8_t i = 0; i < 7; ++i)
      99         840 :         parent_pcs_ptr->ref_order_hint[i] = (uint32_t)ref_frame_arr_single[i].poc;
     100         120 :     if (/*!order_hint_info->enable_order_hint ||*/ parent_pcs_ptr->slice_type == I_SLICE /*frame_is_intra_only(cm)*/ ||
     101         116 :         frm_hdr->reference_mode == SINGLE_REFERENCE)
     102          10 :         return;
     103             : 
     104         110 :     const int cur_order_hint = parent_pcs_ptr->picture_number % (uint64_t)(1 << (parent_pcs_ptr->sequence_control_set_ptr->seq_header.order_hint_info.order_hint_bits));
     105         110 :     int ref_order_hints[2] = { -1, INT_MAX };
     106         110 :     int ref_idx[2] = { INVALID_IDX, INVALID_IDX };
     107             : 
     108             :     // Identify the nearest forward and backward references.
     109             : 
     110         880 :     for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
     111             :         //const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
     112             :         //if (buf == NULL) continue;
     113             : 
     114         770 :         if (ref_frame_arr_single[i].used == 0) continue;
     115             : 
     116         770 :         const int ref_order_hint = (const int)ref_frame_arr_single[i].poc;// buf->order_hint;
     117         770 :         if (get_relative_dist(order_hint_info, ref_order_hint, cur_order_hint) <
     118             :             0) {
     119             :             // Forward reference
     120         806 :             if (ref_order_hints[0] == -1 ||
     121         348 :                 get_relative_dist(order_hint_info, ref_order_hint,
     122             :                     ref_order_hints[0]) > 0) {
     123         110 :                 ref_order_hints[0] = ref_order_hint;
     124         110 :                 ref_idx[0] = i;
     125             :             }
     126             :         }
     127         312 :         else if (get_relative_dist(order_hint_info, ref_order_hint,
     128             :             cur_order_hint) > 0) {
     129             :             // Backward reference
     130         510 :             if (ref_order_hints[1] == INT_MAX ||
     131         203 :                 get_relative_dist(order_hint_info, ref_order_hint,
     132             :                     ref_order_hints[1]) < 0) {
     133         104 :                 ref_order_hints[1] = ref_order_hint;
     134         104 :                 ref_idx[1] = i;
     135             :             }
     136             :         }
     137             :     }
     138             : 
     139         110 :     if (ref_idx[0] != INVALID_IDX && ref_idx[1] != INVALID_IDX) {
     140             :         // == Bi-directional prediction ==
     141         104 :         skip_mode_info->skip_mode_allowed = 1;
     142         104 :         skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
     143         104 :         skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
     144             :     }
     145           6 :     else if (ref_idx[0] != INVALID_IDX && ref_idx[1] == INVALID_IDX) {
     146             :         // == Forward prediction only ==
     147             :         // Identify the second nearest forward reference.
     148           6 :         ref_order_hints[1] = -1;
     149          48 :         for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
     150             :             //const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
     151             :             //if (buf == NULL) continue;
     152          42 :             if (ref_frame_arr_single[i].used == 0) continue;
     153             : 
     154          42 :             const int ref_order_hint = (const int)ref_frame_arr_single[i].poc;// buf->order_hint;
     155          84 :             if ((ref_order_hints[0] != -1 &&
     156          42 :                 get_relative_dist(order_hint_info, ref_order_hint,
     157           0 :                     ref_order_hints[0]) < 0) &&
     158           0 :                     (ref_order_hints[1] == -1 ||
     159           0 :                         get_relative_dist(order_hint_info, ref_order_hint,
     160             :                             ref_order_hints[1]) > 0)) {
     161             :                 // Second closest forward reference
     162           0 :                 ref_order_hints[1] = ref_order_hint;
     163           0 :                 ref_idx[1] = i;
     164             :             }
     165             :         }
     166           6 :         if (ref_order_hints[1] != -1) {
     167           0 :             skip_mode_info->skip_mode_allowed = 1;
     168           0 :             skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
     169           0 :             skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
     170             :         }
     171             :     }
     172             : 
     173             :     //output: idx
     174             :     //0 :LAST
     175             :     //1 :LAST2
     176             :     //2 :LAST3
     177             :     //3 :GOLD
     178             :     //4 :BWD
     179             :     //5 :ALT2
     180             :     //6 :ALT
     181             : }
     182             : 
     183           0 : uint8_t  circ_dec(uint8_t max, uint8_t off, uint8_t input)
     184             : {
     185           0 :     int8_t x = input;
     186             : 
     187           0 :     x--;
     188           0 :     if (x < 0)
     189           0 :         x = max;
     190             : 
     191           0 :     if (off == 2)
     192             :     {
     193           0 :         x--;
     194           0 :         if (x < 0)
     195           0 :             x = max;
     196             :     }
     197             : 
     198           0 :     return x;
     199             : }
     200          10 : uint8_t  circ_inc(uint8_t max, uint8_t off, uint8_t input)
     201             : {
     202          10 :     input++;
     203          10 :     if (input >= max)
     204           2 :         input = 0;
     205             : 
     206          10 :     if (off == 2)
     207             :     {
     208           0 :         input++;
     209           0 :         if (input >= max)
     210           0 :             input = 0;
     211             :     }
     212             : 
     213          10 :     return input;
     214             : }
     215             : #define POC_CIRCULAR_ADD(base, offset/*, bits*/)             (/*(((int32_t) (base)) + ((int32_t) (offset)) > ((int32_t) (1 << (bits))))   ? ((base) + (offset) - (1 << (bits))) : \
     216             :                                                              (((int32_t) (base)) + ((int32_t) (offset)) < 0)                           ? ((base) + (offset) + (1 << (bits))) : \
     217             :                                                                                                                                        */((base) + (offset)))
     218             : 
     219             : #define FUTURE_WINDOW_WIDTH                 6
     220             : #define FLASH_TH                            5
     221             : #define FADE_TH                             3
     222             : #define SCENE_TH                            3000
     223             : #define NOISY_SCENE_TH                      4500    // SCD TH in presence of noise
     224             : #define HIGH_PICTURE_VARIANCE_TH            1500
     225             : #define NUM64x64INPIC(w,h)          ((w*h)>> (LOG2F(BLOCK_SIZE_64)<<1))
     226             : #define QUEUE_GET_PREVIOUS_SPOT(h)  ((h == 0) ? PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH - 1 : h - 1)
     227             : #define QUEUE_GET_NEXT_SPOT(h,off)  (( (h+off) >= PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH) ? h+off - PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH  : h + off)
     228             : 
     229             : #define WTH 64
     230             : #define OTH 64
     231             : #define FC_SKIP_TX_SR_TH025                     125 // Fast cost skip tx search threshold.
     232             : #define FC_SKIP_TX_SR_TH010                     110 // Fast cost skip tx search threshold.
     233             : 
     234           2 : void picture_decision_context_dctor(EbPtr p)
     235             : {
     236           2 :     PictureDecisionContext* obj = (PictureDecisionContext*)p;
     237             : 
     238           2 :     EB_FREE_2D(obj->ahd_running_avg);
     239           2 :     EB_FREE_2D(obj->ahd_running_avg_cr);
     240           2 :     EB_FREE_2D(obj->ahd_running_avg_cb);
     241           2 : }
     242             : 
     243             :  /************************************************
     244             :   * Picture Analysis Context Constructor
     245             :   ************************************************/
     246           2 : EbErrorType picture_decision_context_ctor(
     247             :     PictureDecisionContext *context_ptr,
     248             :     EbFifo *picture_analysis_results_input_fifo_ptr,
     249             :     EbFifo *picture_decision_results_output_fifo_ptr)
     250             : {
     251             :     uint32_t arrayRow, arrowColumn;
     252             : 
     253           2 :     context_ptr->dctor = picture_decision_context_dctor;
     254             : 
     255           2 :     context_ptr->picture_analysis_results_input_fifo_ptr = picture_analysis_results_input_fifo_ptr;
     256           2 :     context_ptr->picture_decision_results_output_fifo_ptr = picture_decision_results_output_fifo_ptr;
     257             : 
     258           8 :     EB_MALLOC_2D(context_ptr->ahd_running_avg_cb,  MAX_NUMBER_OF_REGIONS_IN_WIDTH, MAX_NUMBER_OF_REGIONS_IN_HEIGHT);
     259           8 :     EB_MALLOC_2D(context_ptr->ahd_running_avg_cr, MAX_NUMBER_OF_REGIONS_IN_WIDTH, MAX_NUMBER_OF_REGIONS_IN_HEIGHT);
     260           8 :     EB_MALLOC_2D(context_ptr->ahd_running_avg, MAX_NUMBER_OF_REGIONS_IN_WIDTH, MAX_NUMBER_OF_REGIONS_IN_HEIGHT);
     261             : 
     262          10 :     for (arrayRow = 0; arrayRow < MAX_NUMBER_OF_REGIONS_IN_HEIGHT; arrayRow++)
     263             :     {
     264          40 :         for (arrowColumn = 0; arrowColumn < MAX_NUMBER_OF_REGIONS_IN_WIDTH; arrowColumn++) {
     265          32 :             context_ptr->ahd_running_avg_cb[arrowColumn][arrayRow] = 0;
     266          32 :             context_ptr->ahd_running_avg_cr[arrowColumn][arrayRow] = 0;
     267          32 :             context_ptr->ahd_running_avg[arrowColumn][arrayRow] = 0;
     268             :         }
     269             :     }
     270             : 
     271           2 :     context_ptr->reset_running_avg = EB_TRUE;
     272             : 
     273           2 :     return EB_ErrorNone;
     274             : }
     275             : 
     276           0 : EbBool SceneTransitionDetector(
     277             :     PictureDecisionContext *context_ptr,
     278             :     SequenceControlSet                 *sequence_control_set_ptr,
     279             :     PictureParentControlSet           **ParentPcsWindow,
     280             :     uint32_t                                windowWidthFuture)
     281             : {
     282           0 :     PictureParentControlSet       *previousPictureControlSetPtr = ParentPcsWindow[0];
     283           0 :     PictureParentControlSet       *currentPictureControlSetPtr = ParentPcsWindow[1];
     284           0 :     PictureParentControlSet       *futurePictureControlSetPtr = ParentPcsWindow[2];
     285             : 
     286             :     // calculating the frame threshold based on the number of 64x64 blocks in the frame
     287             :     uint32_t  regionThreshHold;
     288             :     uint32_t  regionThreshHoldChroma;
     289             :     // this variable determines whether the running average should be reset to equal the ahd or not after detecting a scene change.
     290             :     //EbBool reset_running_avg = context_ptr->reset_running_avg;
     291             : 
     292             :     EbBool isAbruptChange; // this variable signals an abrubt change (scene change or flash)
     293             :     EbBool is_scene_change; // this variable signals a frame representing a scene change
     294             :     EbBool isFlash; // this variable signals a frame that contains a flash
     295             :     EbBool isFade; // this variable signals a frame that contains a fade
     296             :     EbBool gradualChange; // this signals the detection of a light scene change a small/localized flash or the start of a fade
     297             : 
     298             :     uint32_t  ahd; // accumulative histogram (absolute) differences between the past and current frame
     299             : 
     300             :     uint32_t  ahdCb;
     301             :     uint32_t  ahdCr;
     302             : 
     303           0 :     uint32_t  ahdErrorCb = 0;
     304           0 :     uint32_t  ahdErrorCr = 0;
     305             : 
     306           0 :     uint32_t **ahd_running_avg_cb = context_ptr->ahd_running_avg_cb;
     307           0 :     uint32_t **ahd_running_avg_cr = context_ptr->ahd_running_avg_cr;
     308           0 :     uint32_t **ahd_running_avg = context_ptr->ahd_running_avg;
     309             : 
     310           0 :     uint32_t  ahdError = 0; // the difference between the ahd and the running average at the current frame.
     311             : 
     312           0 :     uint8_t   aidFuturePast = 0; // this variable denotes the average intensity difference between the next and the past frames
     313           0 :     uint8_t   aidFuturePresent = 0;
     314           0 :     uint8_t   aidPresentPast = 0;
     315             : 
     316           0 :     uint32_t  bin = 0; // variable used to iterate through the bins of the histograms
     317             : 
     318             :     uint32_t  regionInPictureWidthIndex;
     319             :     uint32_t  regionInPictureHeightIndex;
     320             : 
     321             :     uint32_t  regionWidth;
     322             :     uint32_t  regionHeight;
     323             :     uint32_t  regionWidthOffset;
     324             :     uint32_t  regionHeightOffset;
     325             : 
     326           0 :     uint32_t  isAbruptChangeCount = 0;
     327           0 :     uint32_t  isSceneChangeCount = 0;
     328             : 
     329           0 :     uint32_t  regionCountThreshold = (sequence_control_set_ptr->scd_mode == SCD_MODE_2) ?
     330           0 :         (uint32_t)(((float)((sequence_control_set_ptr->picture_analysis_number_of_regions_per_width * sequence_control_set_ptr->picture_analysis_number_of_regions_per_height) * 75) / 100) + 0.5) :
     331           0 :         (uint32_t)(((float)((sequence_control_set_ptr->picture_analysis_number_of_regions_per_width * sequence_control_set_ptr->picture_analysis_number_of_regions_per_height) * 50) / 100) + 0.5);
     332             : 
     333           0 :     regionWidth = ParentPcsWindow[1]->enhanced_picture_ptr->width / sequence_control_set_ptr->picture_analysis_number_of_regions_per_width;
     334           0 :     regionHeight = ParentPcsWindow[1]->enhanced_picture_ptr->height / sequence_control_set_ptr->picture_analysis_number_of_regions_per_height;
     335             : 
     336             :     // Loop over regions inside the picture
     337           0 :     for (regionInPictureWidthIndex = 0; regionInPictureWidthIndex < sequence_control_set_ptr->picture_analysis_number_of_regions_per_width; regionInPictureWidthIndex++) {  // loop over horizontal regions
     338           0 :         for (regionInPictureHeightIndex = 0; regionInPictureHeightIndex < sequence_control_set_ptr->picture_analysis_number_of_regions_per_height; regionInPictureHeightIndex++) { // loop over vertical regions
     339             : 
     340           0 :             isAbruptChange = EB_FALSE;
     341           0 :             is_scene_change = EB_FALSE;
     342           0 :             isFlash = EB_FALSE;
     343           0 :             gradualChange = EB_FALSE;
     344             : 
     345             :             // Reset accumulative histogram (absolute) differences between the past and current frame
     346           0 :             ahd = 0;
     347           0 :             ahdCb = 0;
     348           0 :             ahdCr = 0;
     349             : 
     350           0 :             regionWidthOffset = (regionInPictureWidthIndex == sequence_control_set_ptr->picture_analysis_number_of_regions_per_width - 1) ?
     351           0 :                 ParentPcsWindow[1]->enhanced_picture_ptr->width - (sequence_control_set_ptr->picture_analysis_number_of_regions_per_width * regionWidth) :
     352             :                 0;
     353             : 
     354           0 :             regionHeightOffset = (regionInPictureHeightIndex == sequence_control_set_ptr->picture_analysis_number_of_regions_per_height - 1) ?
     355           0 :                 ParentPcsWindow[1]->enhanced_picture_ptr->height - (sequence_control_set_ptr->picture_analysis_number_of_regions_per_height * regionHeight) :
     356             :                 0;
     357             : 
     358           0 :             regionWidth += regionWidthOffset;
     359           0 :             regionHeight += regionHeightOffset;
     360             : 
     361           0 :             regionThreshHold = (
     362             :                 // Noise insertion/removal detection
     363           0 :                 ((ABS((int64_t)currentPictureControlSetPtr->pic_avg_variance - (int64_t)previousPictureControlSetPtr->pic_avg_variance)) > NOISE_VARIANCE_TH) &&
     364           0 :                 (currentPictureControlSetPtr->pic_avg_variance > HIGH_PICTURE_VARIANCE_TH || previousPictureControlSetPtr->pic_avg_variance > HIGH_PICTURE_VARIANCE_TH)) ?
     365           0 :                 NOISY_SCENE_TH * NUM64x64INPIC(regionWidth, regionHeight) : // SCD TH function of noise insertion/removal.
     366           0 :                 SCENE_TH * NUM64x64INPIC(regionWidth, regionHeight);
     367             : 
     368           0 :             regionThreshHoldChroma = regionThreshHold / 4;
     369             : 
     370           0 :             for (bin = 0; bin < HISTOGRAM_NUMBER_OF_BINS; ++bin) {
     371           0 :                 ahd += ABS((int32_t)currentPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][0][bin] - (int32_t)previousPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][0][bin]);
     372           0 :                 ahdCb += ABS((int32_t)currentPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][1][bin] - (int32_t)previousPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][1][bin]);
     373           0 :                 ahdCr += ABS((int32_t)currentPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][2][bin] - (int32_t)previousPictureControlSetPtr->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][2][bin]);
     374             :             }
     375             : 
     376           0 :             if (context_ptr->reset_running_avg) {
     377           0 :                 ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] = ahd;
     378           0 :                 ahd_running_avg_cb[regionInPictureWidthIndex][regionInPictureHeightIndex] = ahdCb;
     379           0 :                 ahd_running_avg_cr[regionInPictureWidthIndex][regionInPictureHeightIndex] = ahdCr;
     380             :             }
     381             : 
     382           0 :             ahdError = ABS((int32_t)ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] - (int32_t)ahd);
     383           0 :             ahdErrorCb = ABS((int32_t)ahd_running_avg_cb[regionInPictureWidthIndex][regionInPictureHeightIndex] - (int32_t)ahdCb);
     384           0 :             ahdErrorCr = ABS((int32_t)ahd_running_avg_cr[regionInPictureWidthIndex][regionInPictureHeightIndex] - (int32_t)ahdCr);
     385             : 
     386           0 :             if ((ahdError > regionThreshHold       && ahd >= ahdError) ||
     387           0 :                 (ahdErrorCb > regionThreshHoldChroma && ahdCb >= ahdErrorCb) ||
     388           0 :                 (ahdErrorCr > regionThreshHoldChroma && ahdCr >= ahdErrorCr)) {
     389           0 :                 isAbruptChange = EB_TRUE;
     390             :             }
     391           0 :             else if ((ahdError > (regionThreshHold >> 1)) && ahd >= ahdError)
     392           0 :                 gradualChange = EB_TRUE;
     393           0 :             if (isAbruptChange)
     394             :             {
     395           0 :                 aidFuturePast = (uint8_t)ABS((int16_t)futurePictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0] - (int16_t)previousPictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0]);
     396           0 :                 aidFuturePresent = (uint8_t)ABS((int16_t)futurePictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0] - (int16_t)currentPictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0]);
     397           0 :                 aidPresentPast = (uint8_t)ABS((int16_t)currentPictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0] - (int16_t)previousPictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0]);
     398             : 
     399           0 :                 if (aidFuturePast < FLASH_TH && aidFuturePresent >= FLASH_TH && aidPresentPast >= FLASH_TH) {
     400           0 :                     isFlash = EB_TRUE;
     401             :                     //printf ("\nFlash in frame# %i , %i\n", currentPictureControlSetPtr->picture_number,aidFuturePast);
     402             :                 }
     403           0 :                 else if (aidFuturePresent < FADE_TH && aidPresentPast < FADE_TH) {
     404           0 :                     isFade = EB_TRUE;
     405             :                     //printf ("\nFlash in frame# %i , %i\n", currentPictureControlSetPtr->picture_number,aidFuturePast);
     406             :                 }
     407             :                 else {
     408           0 :                     is_scene_change = EB_TRUE;
     409             :                     //printf ("\nScene Change in frame# %i , %i\n", currentPictureControlSetPtr->picture_number,aidFuturePast);
     410             :                 }
     411             :             }
     412           0 :             else if (gradualChange) {
     413           0 :                 aidFuturePast = (uint8_t)ABS((int16_t)futurePictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0] - (int16_t)previousPictureControlSetPtr->average_intensity_per_region[regionInPictureWidthIndex][regionInPictureHeightIndex][0]);
     414           0 :                 if (aidFuturePast < FLASH_TH) {
     415             :                     // proper action to be signalled
     416             :                     //printf ("\nLight Flash in frame# %i , %i\n", currentPictureControlSetPtr->picture_number,aidFuturePast);
     417           0 :                     ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] = (3 * ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] + ahd) / 4;
     418             :                 }
     419             :                 else {
     420             :                     // proper action to be signalled
     421             :                     //printf ("\nLight Scene Change / fade detected in frame# %i , %i\n", currentPictureControlSetPtr->picture_number,aidFuturePast);
     422           0 :                     ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] = (3 * ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] + ahd) / 4;
     423             :                 }
     424             :             }
     425             :             else
     426           0 :                 ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] = (3 * ahd_running_avg[regionInPictureWidthIndex][regionInPictureHeightIndex] + ahd) / 4;
     427           0 :             isAbruptChangeCount += isAbruptChange;
     428           0 :             isSceneChangeCount += is_scene_change;
     429             :         }
     430             :     }
     431             : 
     432             :     (void)windowWidthFuture;
     433             :     (void)isFlash;
     434             :     (void)isFade;
     435             : 
     436           0 :     if (isAbruptChangeCount >= regionCountThreshold)
     437           0 :         context_ptr->reset_running_avg = EB_TRUE;
     438             :     else
     439           0 :         context_ptr->reset_running_avg = EB_FALSE;
     440           0 :     if ((isSceneChangeCount >= regionCountThreshold) && ((!ParentPcsWindow[1]->fade_in_to_black) && (!ParentPcsWindow[1]->fade_out_from_black)))
     441           0 :         return(EB_TRUE);
     442             :     else
     443           0 :         return(EB_FALSE);
     444             : }
     445             : 
     446             : /***************************************************************************************************
     447             : * ReleasePrevPictureFromReorderQueue
     448             : ***************************************************************************************************/
     449         120 : EbErrorType ReleasePrevPictureFromReorderQueue(
     450             :     EncodeContext                 *encode_context_ptr) {
     451         120 :     EbErrorType return_error = EB_ErrorNone;
     452             : 
     453             :     PictureDecisionReorderEntry   *queuePreviousEntryPtr;
     454             :     int32_t                           previousEntryIndex;
     455             : 
     456             :     // Get the previous entry from the Picture Decision Reordering Queue (Entry N-1)
     457             :     // P.S. The previous entry in display order is needed for Scene Change Detection
     458         120 :     previousEntryIndex = (encode_context_ptr->picture_decision_reorder_queue_head_index == 0) ? PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH - 1 : encode_context_ptr->picture_decision_reorder_queue_head_index - 1;
     459         120 :     queuePreviousEntryPtr = encode_context_ptr->picture_decision_reorder_queue[previousEntryIndex];
     460             : 
     461             :     // SB activity classification based on (0,0) SAD & picture activity derivation
     462         120 :     if (queuePreviousEntryPtr->parent_pcs_wrapper_ptr) {
     463             :         // Reset the Picture Decision Reordering Queue Entry
     464             :         // P.S. The reset of the Picture Decision Reordering Queue Entry could not be done before running the Scene Change Detector
     465         118 :         queuePreviousEntryPtr->picture_number += PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH;
     466         118 :         queuePreviousEntryPtr->parent_pcs_wrapper_ptr = (EbObjectWrapper *)EB_NULL;
     467             :     }
     468             : 
     469         120 :     return return_error;
     470             : }
     471             : 
     472             : /***************************************************************************************************
     473             : * Initializes mini GOP activity array
     474             : *
     475             : ***************************************************************************************************/
     476           8 : EbErrorType initialize_mini_gop_activity_array(
     477             :     PictureDecisionContext        *context_ptr) {
     478           8 :     EbErrorType return_error = EB_ErrorNone;
     479             : 
     480             :     uint32_t MiniGopIndex;
     481             : 
     482             :     // Loop over all mini GOPs
     483         128 :     for (MiniGopIndex = 0; MiniGopIndex < MINI_GOP_MAX_COUNT; ++MiniGopIndex) {
     484         120 :         context_ptr->mini_gop_activity_array[MiniGopIndex] = (get_mini_gop_stats(MiniGopIndex)->hierarchical_levels == MIN_HIERARCHICAL_LEVEL) ?
     485         120 :             EB_FALSE :
     486             :             EB_TRUE;
     487             :     }
     488             : 
     489           8 :     return return_error;
     490             : }
     491             : 
     492             : /***************************************************************************************************
     493             : * Generates block picture map
     494             : *
     495             : *
     496             : ***************************************************************************************************/
     497           8 : EbErrorType generate_picture_window_split(
     498             :     PictureDecisionContext        *context_ptr,
     499             :     EncodeContext                 *encode_context_ptr) {
     500           8 :     EbErrorType return_error = EB_ErrorNone;
     501             : 
     502             :     uint32_t    MiniGopIndex;
     503             : 
     504           8 :     context_ptr->total_number_of_mini_gops = 0;
     505             : 
     506             :     // Loop over all mini GOPs
     507           8 :     MiniGopIndex = 0;
     508          84 :     while (MiniGopIndex < MINI_GOP_MAX_COUNT) {
     509             :         // Only for a valid mini GOP
     510          76 :         if (get_mini_gop_stats(MiniGopIndex)->end_index < encode_context_ptr->pre_assignment_buffer_count && context_ptr->mini_gop_activity_array[MiniGopIndex] == EB_FALSE) {
     511           8 :             context_ptr->mini_gop_start_index[context_ptr->total_number_of_mini_gops] = get_mini_gop_stats(MiniGopIndex)->start_index;
     512           8 :             context_ptr->mini_gop_end_index[context_ptr->total_number_of_mini_gops] = get_mini_gop_stats(MiniGopIndex)->end_index;
     513           8 :             context_ptr->mini_gop_length[context_ptr->total_number_of_mini_gops] = get_mini_gop_stats(MiniGopIndex)->lenght;
     514           8 :             context_ptr->mini_gop_hierarchical_levels[context_ptr->total_number_of_mini_gops] = get_mini_gop_stats(MiniGopIndex)->hierarchical_levels;
     515           8 :             context_ptr->mini_gop_intra_count[context_ptr->total_number_of_mini_gops] = 0;
     516           8 :             context_ptr->mini_gop_idr_count[context_ptr->total_number_of_mini_gops] = 0;
     517             : 
     518           8 :             context_ptr->total_number_of_mini_gops++;
     519             :         }
     520             : 
     521         152 :         MiniGopIndex += context_ptr->mini_gop_activity_array[MiniGopIndex] ?
     522         118 :             1 :
     523          42 :             mini_gop_offset[get_mini_gop_stats(MiniGopIndex)->hierarchical_levels - MIN_HIERARCHICAL_LEVEL];
     524             :     }
     525             : 
     526             :     // Only in presence of at least 1 valid mini GOP
     527           8 :     if (context_ptr->total_number_of_mini_gops != 0) {
     528           8 :         context_ptr->mini_gop_intra_count[context_ptr->total_number_of_mini_gops - 1] = encode_context_ptr->pre_assignment_buffer_intra_count;
     529           8 :         context_ptr->mini_gop_idr_count[context_ptr->total_number_of_mini_gops - 1] = encode_context_ptr->pre_assignment_buffer_idr_count;
     530             :     }
     531             : 
     532           8 :     return return_error;
     533             : }
     534             : 
     535             : /***************************************************************************************************
     536             : * Handles an incomplete picture window map
     537             : *
     538             : *
     539             : ***************************************************************************************************/
     540           8 : EbErrorType handle_incomplete_picture_window_map(
     541             :     PictureDecisionContext        *context_ptr,
     542             :     EncodeContext                 *encode_context_ptr) {
     543           8 :     EbErrorType return_error = EB_ErrorNone;
     544           8 :     if (context_ptr->total_number_of_mini_gops == 0) {
     545           0 :         context_ptr->mini_gop_start_index[context_ptr->total_number_of_mini_gops] = 0;
     546           0 :         context_ptr->mini_gop_end_index[context_ptr->total_number_of_mini_gops] = encode_context_ptr->pre_assignment_buffer_count - 1;
     547           0 :         context_ptr->mini_gop_length[context_ptr->total_number_of_mini_gops] = encode_context_ptr->pre_assignment_buffer_count - context_ptr->mini_gop_start_index[context_ptr->total_number_of_mini_gops];
     548           0 :         context_ptr->mini_gop_hierarchical_levels[context_ptr->total_number_of_mini_gops] = 3;// MIN_HIERARCHICAL_LEVEL; // AMIR to be updated after other predictions are supported
     549             : 
     550           0 :         context_ptr->total_number_of_mini_gops++;
     551             :     }
     552           8 :     else if (context_ptr->mini_gop_end_index[context_ptr->total_number_of_mini_gops - 1] < encode_context_ptr->pre_assignment_buffer_count - 1) {
     553           2 :         context_ptr->mini_gop_start_index[context_ptr->total_number_of_mini_gops] = context_ptr->mini_gop_end_index[context_ptr->total_number_of_mini_gops - 1] + 1;
     554           2 :         context_ptr->mini_gop_end_index[context_ptr->total_number_of_mini_gops] = encode_context_ptr->pre_assignment_buffer_count - 1;
     555           2 :         context_ptr->mini_gop_length[context_ptr->total_number_of_mini_gops] = encode_context_ptr->pre_assignment_buffer_count - context_ptr->mini_gop_start_index[context_ptr->total_number_of_mini_gops];
     556           2 :         context_ptr->mini_gop_hierarchical_levels[context_ptr->total_number_of_mini_gops] = 3;// MIN_HIERARCHICAL_LEVEL;// AMIR
     557           2 :         context_ptr->mini_gop_intra_count[context_ptr->total_number_of_mini_gops - 1] = 0;
     558           2 :         context_ptr->mini_gop_idr_count[context_ptr->total_number_of_mini_gops - 1] = 0;
     559             : 
     560           2 :         context_ptr->total_number_of_mini_gops++;
     561             :     }
     562             : 
     563           8 :     context_ptr->mini_gop_intra_count[context_ptr->total_number_of_mini_gops - 1] = encode_context_ptr->pre_assignment_buffer_intra_count;
     564           8 :     context_ptr->mini_gop_idr_count[context_ptr->total_number_of_mini_gops - 1] = encode_context_ptr->pre_assignment_buffer_idr_count;
     565             : 
     566           8 :     return return_error;
     567             : }
     568             : /***************************************************************************************************
     569             : * If a switch happens, then update the RPS of the base layer frame separating the 2 different prediction structures
     570             : * Clean up the reference queue dependant counts of the base layer frame separating the 2 different prediction structures
     571             : *
     572             : ***************************************************************************************************/
     573          12 : EbErrorType update_base_layer_reference_queue_dependent_count(
     574             :     PictureDecisionContext        *context_ptr,
     575             :     EncodeContext                 *encode_context_ptr,
     576             :     SequenceControlSet            *sequence_control_set_ptr,
     577             :     uint32_t                         MiniGopIndex) {
     578          12 :     if (!context_ptr || !encode_context_ptr || !sequence_control_set_ptr)
     579           0 :         return EB_ErrorBadParameter;
     580             : 
     581          12 :     EbErrorType return_error = EB_ErrorNone;
     582             : 
     583             :     PaReferenceQueueEntry         *input_entry_ptr;
     584             :     uint32_t                         input_queue_index;
     585             : 
     586             :     PredictionStructure           *next_pred_struct_ptr;
     587             :     PredictionStructureEntry      *next_base_layer_pred_position_ptr;
     588             : 
     589             :     uint32_t                         dependant_list_positive_entries;
     590             :     uint32_t                         dependant_list_removed_entries;
     591             :     uint32_t                         dep_list_count;
     592             : 
     593             :     uint32_t                         dep_idx;
     594             :     uint64_t                         dep_poc;
     595             : 
     596             :     PictureParentControlSet       *picture_control_set_ptr;
     597             : 
     598             :     // Get the 1st PCS mini GOP
     599          12 :     picture_control_set_ptr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[context_ptr->mini_gop_start_index[MiniGopIndex]]->object_ptr;
     600             : 
     601             :     // Derive the temporal layer difference between the current mini GOP and the previous mini GOP
     602          12 :     picture_control_set_ptr->hierarchical_layers_diff = (uint8_t)(encode_context_ptr->previous_mini_gop_hierarchical_levels - picture_control_set_ptr->hierarchical_levels);
     603             : 
     604             :     // Set init_pred_struct_position_flag to TRUE if mini GOP switch
     605          12 :     picture_control_set_ptr->init_pred_struct_position_flag = (picture_control_set_ptr->hierarchical_layers_diff != 0) ?
     606          12 :         EB_TRUE :
     607             :         EB_FALSE;
     608             : 
     609             :     // If the current mini GOP is different than the previous mini GOP update then update the positive dependant counts of the reference entry separating the 2 mini GOPs
     610          12 :     if (picture_control_set_ptr->hierarchical_layers_diff != 0) {
     611           2 :         input_queue_index = encode_context_ptr->picture_decision_pa_reference_queue_head_index;
     612             : 
     613          36 :         while (input_queue_index != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) {
     614          34 :             input_entry_ptr = encode_context_ptr->picture_decision_pa_reference_queue[input_queue_index];
     615             : 
     616             :             // Find the reference entry separating the 2 mini GOPs  (picture_control_set_ptr->picture_number is the POC of the first isput in the mini GOP)
     617          34 :             if (input_entry_ptr->picture_number == (picture_control_set_ptr->picture_number - 1)) {
     618             :                 // Update the positive dependant counts
     619             : 
     620             :                 // 1st step: remove all positive entries from the dependant list0 and dependant list1
     621           2 :                 dependant_list_positive_entries = 0;
     622          28 :                 for (dep_idx = 0; dep_idx < input_entry_ptr->list0.list_count; ++dep_idx) {
     623          26 :                     if (input_entry_ptr->list0.list[dep_idx] >= 0)
     624          26 :                         dependant_list_positive_entries++;
     625             :                 }
     626           2 :                 input_entry_ptr->list0.list_count = input_entry_ptr->list0.list_count - dependant_list_positive_entries;
     627           2 :                 dependant_list_positive_entries = 0;
     628          24 :                 for (dep_idx = 0; dep_idx < input_entry_ptr->list1.list_count; ++dep_idx) {
     629          22 :                     if (input_entry_ptr->list1.list[dep_idx] >= 0)
     630           4 :                         dependant_list_positive_entries++;
     631             :                 }
     632           2 :                 input_entry_ptr->list1.list_count = input_entry_ptr->list1.list_count - dependant_list_positive_entries;
     633             : 
     634             :                 // 2nd step: inherit the positive dependant counts of the current mini GOP
     635             :                 // Get the RPS set of the current mini GOP
     636           2 :                 next_pred_struct_ptr = get_prediction_structure(
     637             :                     encode_context_ptr->prediction_structure_group_ptr,
     638           2 :                     picture_control_set_ptr->pred_structure,
     639             :                     sequence_control_set_ptr->reference_count,
     640           2 :                     picture_control_set_ptr->hierarchical_levels);            // Number of temporal layer in the current mini GOP
     641             : 
     642             :                 // Get the RPS of a base layer input
     643           2 :                 next_base_layer_pred_position_ptr = next_pred_struct_ptr->pred_struct_entry_ptr_array[next_pred_struct_ptr->pred_struct_entry_count - 1];
     644             : 
     645          15 :                 for (dep_idx = 0; dep_idx < next_base_layer_pred_position_ptr->dep_list0.list_count; ++dep_idx) {
     646          13 :                     if (next_base_layer_pred_position_ptr->dep_list0.list[dep_idx] >= 0)
     647          13 :                         input_entry_ptr->list0.list[input_entry_ptr->list0.list_count++] = next_base_layer_pred_position_ptr->dep_list0.list[dep_idx];
     648             :                 }
     649             : 
     650          14 :                 for (dep_idx = 0; dep_idx < next_base_layer_pred_position_ptr->dep_list1.list_count; ++dep_idx) {
     651          12 :                     if (next_base_layer_pred_position_ptr->dep_list1.list[dep_idx] >= 0)
     652           2 :                         input_entry_ptr->list1.list[input_entry_ptr->list1.list_count++] = next_base_layer_pred_position_ptr->dep_list1.list[dep_idx];
     653             :                 }
     654             : 
     655             :                 // 3rd step: update the dependant count
     656           2 :                 dependant_list_removed_entries = input_entry_ptr->dep_list0_count + input_entry_ptr->dep_list1_count - input_entry_ptr->dependent_count;
     657           2 :                 input_entry_ptr->dep_list0_count = (input_entry_ptr->is_alt_ref) ? input_entry_ptr->list0.list_count + 1 : input_entry_ptr->list0.list_count;
     658           2 :                 input_entry_ptr->dep_list1_count = input_entry_ptr->list1.list_count;
     659           2 :                 input_entry_ptr->dependent_count = input_entry_ptr->dep_list0_count + input_entry_ptr->dep_list1_count - dependant_list_removed_entries;
     660             :             }
     661             :             else {
     662             :                 // Modify Dependent List0
     663          32 :                 dep_list_count = input_entry_ptr->list0.list_count;
     664         109 :                 for (dep_idx = 0; dep_idx < dep_list_count; ++dep_idx) {
     665             :                     // Adjust the latest currentInputPoc in case we're in a POC rollover scenario
     666             :                     // currentInputPoc += (currentInputPoc < input_entry_ptr->pocNumber) ? (1 << sequence_control_set_ptr->bitsForPictureOrderCount) : 0;
     667          77 :                     dep_poc = POC_CIRCULAR_ADD(
     668             :                         input_entry_ptr->picture_number, // can't use a value that gets reset
     669             :                         input_entry_ptr->list0.list[dep_idx]/*,
     670             :                                                          sequence_control_set_ptr->bitsForPictureOrderCount*/);
     671             : 
     672             :                                                          // If Dependent POC is greater or equal to the IDR POC
     673          77 :                     if (dep_poc >= picture_control_set_ptr->picture_number && input_entry_ptr->list0.list[dep_idx]) {
     674          18 :                         input_entry_ptr->list0.list[dep_idx] = 0;
     675             : 
     676             :                         // Decrement the Reference's reference_count
     677          18 :                         --input_entry_ptr->dependent_count;
     678          18 :                         CHECK_REPORT_ERROR(
     679             :                             (input_entry_ptr->dependent_count != ~0u),
     680             :                             encode_context_ptr->app_callback_ptr,
     681             :                             EB_ENC_PD_ERROR3);
     682             :                     }
     683             :                 }
     684             :                 // Modify Dependent List1
     685          32 :                 dep_list_count = input_entry_ptr->list1.list_count;
     686          82 :                 for (dep_idx = 0; dep_idx < dep_list_count; ++dep_idx) {
     687             :                     // Adjust the latest currentInputPoc in case we're in a POC rollover scenario
     688             :                     // currentInputPoc += (currentInputPoc < input_entry_ptr->pocNumber) ? (1 << sequence_control_set_ptr->bitsForPictureOrderCount) : 0;
     689          50 :                     dep_poc = POC_CIRCULAR_ADD(
     690             :                         input_entry_ptr->picture_number,
     691             :                         input_entry_ptr->list1.list[dep_idx]/*,
     692             :                                                          sequence_control_set_ptr->bitsForPictureOrderCount*/);
     693             : 
     694             :                     // If Dependent POC is greater or equal to the IDR POC
     695          50 :                     if ((dep_poc >= picture_control_set_ptr->picture_number) && input_entry_ptr->list1.list[dep_idx]) {
     696           2 :                         input_entry_ptr->list1.list[dep_idx] = 0;
     697             :                         // Decrement the Reference's reference_count
     698           2 :                         --input_entry_ptr->dependent_count;
     699             : 
     700           2 :                         CHECK_REPORT_ERROR(
     701             :                             (input_entry_ptr->dependent_count != ~0u),
     702             :                             encode_context_ptr->app_callback_ptr,
     703             :                             EB_ENC_PD_ERROR3);
     704             :                     }
     705             :                 }
     706             :             }
     707             :             // Increment the input_queue_index Iterator
     708          34 :             input_queue_index = (input_queue_index == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : input_queue_index + 1;
     709             :         }
     710             :     }
     711             : 
     712          12 :     return return_error;
     713             : }
     714             : 
     715           0 : EbBool is_supposedly_4L_reference_frame(
     716             :     PictureDecisionContext        *context_ptr,
     717             :     uint32_t                         MiniGopIndex,
     718             :     uint32_t                        picture_index) {
     719           0 :     if ((context_ptr->mini_gop_hierarchical_levels[MiniGopIndex] == 4 && context_ptr->mini_gop_length[MiniGopIndex] == 16 && (picture_index == 7 || picture_index == 23)) ||    // supposedly a 4L reference frame for 5L prediction structure
     720           0 :         (context_ptr->mini_gop_hierarchical_levels[MiniGopIndex] == 5 && context_ptr->mini_gop_length[MiniGopIndex] == 32 && (picture_index == 7 || picture_index == 23))) { // supposedly a 4L reference frame for 6L prediction structure
     721           0 :         return(EB_TRUE);
     722             :     }
     723             :     else
     724           0 :         return(EB_FALSE);
     725             : }
     726             : 
     727             : /***************************************************************************************************
     728             : * Generates mini GOP RPSs
     729             : *
     730             : *
     731             : ***************************************************************************************************/
     732          10 : EbErrorType GenerateMiniGopRps(
     733             :     PictureDecisionContext        *context_ptr,
     734             :     EncodeContext                 *encode_context_ptr) {
     735          10 :     EbErrorType return_error = EB_ErrorNone;
     736             :     SequenceControlSet            *sequence_control_set_ptr;
     737             :     uint32_t                         mini_gop_index;
     738             :     PictureParentControlSet    *picture_control_set_ptr;
     739             :     uint32_t                         pictureIndex;
     740             : 
     741             :     // Loop over all mini GOPs
     742          22 :     for (mini_gop_index = 0; mini_gop_index < context_ptr->total_number_of_mini_gops; ++mini_gop_index) {
     743             :         // Loop over picture within the mini GOP
     744         132 :         for (pictureIndex = context_ptr->mini_gop_start_index[mini_gop_index]; pictureIndex <= context_ptr->mini_gop_end_index[mini_gop_index]; pictureIndex++) {
     745         120 :             picture_control_set_ptr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pictureIndex]->object_ptr;
     746         120 :             sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
     747         120 :             picture_control_set_ptr->pred_structure = EB_PRED_RANDOM_ACCESS;
     748         120 :             picture_control_set_ptr->hierarchical_levels = (uint8_t)context_ptr->mini_gop_hierarchical_levels[mini_gop_index];
     749             : 
     750         120 :             picture_control_set_ptr->pred_struct_ptr = get_prediction_structure(
     751             :                 encode_context_ptr->prediction_structure_group_ptr,
     752         120 :                 picture_control_set_ptr->pred_structure,
     753             :                 sequence_control_set_ptr->reference_count,
     754         120 :                 picture_control_set_ptr->hierarchical_levels);
     755             :         }
     756             :     }
     757          10 :     return return_error;
     758             : }
     759             : 
     760             : /******************************************************
     761             : * Derive Multi-Processes Settings for OQ
     762             : Input   : encoder mode and tune
     763             : Output  : Multi-Processes signal(s)
     764             : ******************************************************/
     765         120 : EbErrorType signal_derivation_multi_processes_oq(
     766             :     SequenceControlSet        *sequence_control_set_ptr,
     767             :     PictureParentControlSet   *picture_control_set_ptr) {
     768         120 :     EbErrorType return_error = EB_ErrorNone;
     769         120 :     FrameHeader *frm_hdr = &picture_control_set_ptr->frm_hdr;
     770             :     //  MDC Partitioning Method              Settings
     771             :     //  PIC_ALL_DEPTH_MODE                   ALL sq and nsq: SB size -> 4x4
     772             :     //  PIC_ALL_C_DEPTH_MODE                 ALL sq and nsq: SB size -> 4x4  (No 4xN ; Nx4)
     773             :     //  PIC_SQ_DEPTH_MODE                    ONLY sq: SB size -> 4x4
     774             :     //  PIC_SQ_NON4_DEPTH_MODE               ONLY sq: SB size -> 8x8  (No 4x4)
     775             : 
     776         120 :     uint8_t sc_content_detected = picture_control_set_ptr->sc_content_detected;
     777             : 
     778             : #if TWO_PASS_USE_2NDP_ME_IN_1STP
     779         120 :     uint8_t enc_mode_hme = sequence_control_set_ptr->use_output_stat_file ? picture_control_set_ptr->snd_pass_enc_mode : picture_control_set_ptr->enc_mode;
     780         120 :     picture_control_set_ptr->enable_hme_flag = enable_hme_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     781             : 
     782         120 :     picture_control_set_ptr->enable_hme_level0_flag = enable_hme_level0_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     783         120 :     picture_control_set_ptr->enable_hme_level1_flag = enable_hme_level1_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     784         120 :     picture_control_set_ptr->enable_hme_level2_flag = enable_hme_level2_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     785             : 
     786         120 :     picture_control_set_ptr->tf_enable_hme_flag = tf_enable_hme_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     787         120 :     picture_control_set_ptr->tf_enable_hme_level0_flag = tf_enable_hme_level0_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     788         120 :     picture_control_set_ptr->tf_enable_hme_level1_flag = tf_enable_hme_level1_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     789         120 :     picture_control_set_ptr->tf_enable_hme_level2_flag = tf_enable_hme_level2_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][enc_mode_hme];
     790             : #else
     791             :     picture_control_set_ptr->enable_hme_flag = enable_hme_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     792             : 
     793             :     picture_control_set_ptr->enable_hme_level0_flag = enable_hme_level0_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     794             :     picture_control_set_ptr->enable_hme_level1_flag = enable_hme_level1_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     795             :     picture_control_set_ptr->enable_hme_level2_flag = enable_hme_level2_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     796             : 
     797             :     picture_control_set_ptr->tf_enable_hme_flag = tf_enable_hme_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     798             :     picture_control_set_ptr->tf_enable_hme_level0_flag = tf_enable_hme_level0_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     799             :     picture_control_set_ptr->tf_enable_hme_level1_flag = tf_enable_hme_level1_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     800             :     picture_control_set_ptr->tf_enable_hme_level2_flag = tf_enable_hme_level2_flag[picture_control_set_ptr->sc_content_detected][sequence_control_set_ptr->input_resolution][picture_control_set_ptr->enc_mode];
     801             : #endif
     802             : 
     803         120 :         if (sc_content_detected)
     804           0 :             if (picture_control_set_ptr->enc_mode <= ENC_M1)
     805           0 :                 picture_control_set_ptr->pic_depth_mode = PIC_ALL_DEPTH_MODE;
     806           0 :             else if (picture_control_set_ptr->enc_mode <= ENC_M3)
     807           0 :                 if (picture_control_set_ptr->temporal_layer_index == 0)
     808           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_ALL_DEPTH_MODE;
     809           0 :                 else if (picture_control_set_ptr->is_used_as_reference_flag)
     810           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_ALL_C_DEPTH_MODE;
     811             :                 else
     812           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_SQ_DEPTH_MODE;
     813           0 :             else if (picture_control_set_ptr->enc_mode <= ENC_M4)
     814           0 :                 if (picture_control_set_ptr->slice_type == I_SLICE)
     815           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_ALL_DEPTH_MODE;
     816             :                 else
     817           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_SQ_NON4_DEPTH_MODE;
     818             :             else
     819           0 :                 picture_control_set_ptr->pic_depth_mode = PIC_SQ_NON4_DEPTH_MODE;
     820             : 
     821         120 :         else if (picture_control_set_ptr->enc_mode <= ENC_M2)
     822          60 :             picture_control_set_ptr->pic_depth_mode = PIC_ALL_DEPTH_MODE;
     823             : 
     824             :         //Jing: TODO:
     825             :         //In M3, sb_sz may be 128x128, and init_sq_non4_block only works for 64x64 sb size
     826          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M3)
     827           0 :             if (picture_control_set_ptr->slice_type == I_SLICE)
     828           0 :                 picture_control_set_ptr->pic_depth_mode = PIC_ALL_C_DEPTH_MODE;
     829             :             else
     830           0 :                 if (sequence_control_set_ptr->seq_header.sb_size == BLOCK_128X128)
     831           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_SQ_DEPTH_MODE;
     832             :                 else
     833           0 :                     picture_control_set_ptr->pic_depth_mode = PIC_SQ_NON4_DEPTH_MODE;
     834             : 
     835          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M5)
     836           0 :             picture_control_set_ptr->pic_depth_mode = PIC_SQ_NON4_DEPTH_MODE;
     837             : 
     838             :         else
     839          60 :             if (picture_control_set_ptr->slice_type == I_SLICE)
     840           2 :                 picture_control_set_ptr->pic_depth_mode = PIC_SQ_NON4_DEPTH_MODE;
     841             :             else
     842          58 :                 picture_control_set_ptr->pic_depth_mode = PIC_SB_SWITCH_DEPTH_MODE;
     843             : 
     844             : 
     845         120 :         if (picture_control_set_ptr->pic_depth_mode < PIC_SQ_DEPTH_MODE)
     846          60 :             assert(sequence_control_set_ptr->nsq_present == 1 && "use nsq_present 1");
     847             : 
     848         120 :         picture_control_set_ptr->max_number_of_pus_per_sb = (picture_control_set_ptr->pic_depth_mode <= PIC_ALL_C_DEPTH_MODE) ? MAX_ME_PU_COUNT : SQUARE_PU_COUNT;
     849             : #if MDC_ADAPTIVE_LEVEL
     850             :         // Adaptive Ol  Level                    Settings
     851             :         // 0                                     OFF
     852             :         // 1                                     ON 
     853             :         //NM : Please note that the open_loop_partitioning is operational only when 
     854             :         // pic_depth_mode is set to PIC_ALL_DEPTH_MODE or PIC_ALL_C_DEPTH_MODE when 
     855             :         // the motion information information for NSQ is generated.
     856         120 :         if (sequence_control_set_ptr->static_config.olpd_refinement == -1) { //auto mode; if not set by cfg
     857         120 :             if (picture_control_set_ptr->pic_depth_mode <= PIC_ALL_C_DEPTH_MODE) {
     858          60 :                 if (MR_MODE || sc_content_detected || sequence_control_set_ptr->static_config.enable_hbd_mode_decision)
     859           0 :                     picture_control_set_ptr->enable_adaptive_ol_partitioning = 0;
     860          60 :                 else if (picture_control_set_ptr->enc_mode <= ENC_M0)
     861          60 :                     picture_control_set_ptr->enable_adaptive_ol_partitioning = 1;
     862             :                 else
     863           0 :                     picture_control_set_ptr->enable_adaptive_ol_partitioning = 0;
     864             :             }
     865             :             else
     866          60 :                 picture_control_set_ptr->enable_adaptive_ol_partitioning = 0;
     867             :         }
     868             :         else {
     869           0 :             picture_control_set_ptr->enable_adaptive_ol_partitioning = sequence_control_set_ptr->static_config.olpd_refinement;
     870             :         }
     871             : #else
     872             : 
     873             : #if PREDICT_NSQ_SHAPE
     874             :     // Depth Level                           Settings
     875             :     // 0                                     pred only
     876             :     // 1                                     pred + 1
     877             :     // 2                                     pred + 2
     878             :     // 3                                     pred + 3
     879             :     // 4                                     pred - 1 + 1
     880             :     // 5                                     pred - 1 + 2
     881             :     // 6                                     pred - 1 + 3
     882             :     // 7                                     All
     883             : 
     884             :     if (MR_MODE || sc_content_detected || sequence_control_set_ptr->static_config.enable_hbd_mode_decision)
     885             :         picture_control_set_ptr->mdc_depth_level = MAX_MDC_LEVEL;
     886             :     else if (picture_control_set_ptr->enc_mode == ENC_M0)
     887             :         picture_control_set_ptr->mdc_depth_level = (sequence_control_set_ptr->input_resolution == INPUT_SIZE_576p_RANGE_OR_LOWER) ? MAX_MDC_LEVEL : 6;
     888             :     else
     889             :         picture_control_set_ptr->mdc_depth_level = MAX_MDC_LEVEL; // Not tuned yet.
     890             : 
     891             : #endif
     892             : #endif
     893             : 
     894             :     // NSQ search Level                               Settings
     895             :     // NSQ_SEARCH_OFF                                 OFF
     896             :     // NSQ_SEARCH_LEVEL1                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 1 NSQ SHAPE
     897             :     // NSQ_SEARCH_LEVEL2                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 2 NSQ SHAPE
     898             :     // NSQ_SEARCH_LEVEL3                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 3 NSQ SHAPE
     899             :     // NSQ_SEARCH_LEVEL4                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 4 NSQ SHAPE
     900             :     // NSQ_SEARCH_LEVEL5                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 5 NSQ SHAPE
     901             :     // NSQ_SEARCH_LEVEL6                              Allow only NSQ Inter-NEAREST/NEAR/GLOBAL if parent SQ has no coeff + reordering nsq_table number and testing only 6 NSQ SHAPE
     902             :     // NSQ_SEARCH_FULL                                Allow NSQ Intra-FULL and Inter-FULL
     903             : 
     904             :         if (MR_MODE)
     905             :             picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_FULL;
     906         120 :         else if (sc_content_detected)
     907           0 :             if (picture_control_set_ptr->enc_mode <= ENC_M1)
     908           0 :                 picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL6;
     909           0 :             else if (picture_control_set_ptr->enc_mode <= ENC_M2)
     910           0 :                 if (picture_control_set_ptr->temporal_layer_index == 0)
     911           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL6;
     912           0 :                 else if (picture_control_set_ptr->is_used_as_reference_flag)
     913           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL4;
     914             :                 else
     915           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_OFF;
     916           0 :             else if (picture_control_set_ptr->enc_mode <= ENC_M3)
     917           0 :                 if (picture_control_set_ptr->temporal_layer_index == 0)
     918           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL6;
     919           0 :                 else if (picture_control_set_ptr->is_used_as_reference_flag)
     920           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL1;
     921             :                 else
     922           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_OFF;
     923             :             else
     924           0 :                     picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_OFF;
     925             : #if PREDICT_NSQ_SHAPE && !MDC_ADAPTIVE_LEVEL
     926             :         else if (picture_control_set_ptr->mdc_depth_level == (MAX_MDC_LEVEL - 1))
     927             :             picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL7;
     928             : #endif
     929         120 :         else if (picture_control_set_ptr->enc_mode <= ENC_M1)
     930          60 :             picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL6;
     931             : 
     932          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M2)
     933           0 :             if (picture_control_set_ptr->is_used_as_reference_flag)
     934           0 :                 picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL5;
     935             :             else
     936           0 :                 picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_LEVEL3;
     937             :         else
     938          60 :             picture_control_set_ptr->nsq_search_level = NSQ_SEARCH_OFF;
     939             : 
     940         120 :     if (picture_control_set_ptr->nsq_search_level > NSQ_SEARCH_OFF)
     941          60 :         assert(sequence_control_set_ptr->nsq_present == 1 && "use nsq_present 1");
     942             : 
     943         120 :     switch (picture_control_set_ptr->nsq_search_level) {
     944          60 :     case NSQ_SEARCH_OFF:
     945          60 :         picture_control_set_ptr->nsq_max_shapes_md = 0;
     946          60 :         break;
     947           0 :     case NSQ_SEARCH_LEVEL1:
     948           0 :         picture_control_set_ptr->nsq_max_shapes_md = 1;
     949           0 :         break;
     950           0 :     case NSQ_SEARCH_LEVEL2:
     951           0 :         picture_control_set_ptr->nsq_max_shapes_md = 2;
     952           0 :         break;
     953           0 :     case NSQ_SEARCH_LEVEL3:
     954           0 :         picture_control_set_ptr->nsq_max_shapes_md = 3;
     955           0 :         break;
     956           0 :     case NSQ_SEARCH_LEVEL4:
     957           0 :         picture_control_set_ptr->nsq_max_shapes_md = 4;
     958           0 :         break;
     959           0 :     case NSQ_SEARCH_LEVEL5:
     960           0 :         picture_control_set_ptr->nsq_max_shapes_md = 5;
     961           0 :         break;
     962          60 :     case NSQ_SEARCH_LEVEL6:
     963          60 :         picture_control_set_ptr->nsq_max_shapes_md = 6;
     964          60 :         break;
     965             : #if PREDICT_NSQ_SHAPE
     966           0 :     case NSQ_SEARCH_LEVEL7:
     967           0 :         picture_control_set_ptr->nsq_max_shapes_md = 7;
     968           0 :         break;
     969             : #endif
     970           0 :     case NSQ_SEARCH_FULL:
     971           0 :         picture_control_set_ptr->nsq_max_shapes_md = 6;
     972           0 :         break;
     973           0 :     default:
     974           0 :         printf("nsq_search_level is not supported\n");
     975           0 :         break;
     976             :     }
     977             : 
     978         120 :     if (picture_control_set_ptr->nsq_search_level == NSQ_SEARCH_OFF)
     979          60 :         if (picture_control_set_ptr->pic_depth_mode <= PIC_ALL_C_DEPTH_MODE) picture_control_set_ptr->pic_depth_mode = PIC_SQ_DEPTH_MODE;
     980         120 :     if (picture_control_set_ptr->pic_depth_mode > PIC_SQ_DEPTH_MODE)
     981          60 :         assert(picture_control_set_ptr->nsq_search_level == NSQ_SEARCH_OFF);
     982             :     // Interpolation search Level                     Settings
     983             :     // 0                                              OFF
     984             :     // 1                                              Interpolation search at inter-depth
     985             :     // 2                                              Interpolation search at full loop
     986             :     // 3                                              Chroma blind interpolation search at fast loop
     987             :     // 4                                              Interpolation search at fast loop
     988             : 
     989             :         if (MR_MODE)
     990             :             picture_control_set_ptr->interpolation_search_level = IT_SEARCH_FAST_LOOP;
     991         120 :         else if (sc_content_detected)
     992           0 :             picture_control_set_ptr->interpolation_search_level = IT_SEARCH_OFF;
     993         120 :         else if (picture_control_set_ptr->enc_mode <= ENC_M1)
     994          60 :             picture_control_set_ptr->interpolation_search_level = IT_SEARCH_FAST_LOOP_UV_BLIND;
     995          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M3)
     996           0 :             if (picture_control_set_ptr->is_used_as_reference_flag)
     997           0 :                 picture_control_set_ptr->interpolation_search_level = IT_SEARCH_FAST_LOOP_UV_BLIND;
     998             :             else
     999           0 :                 picture_control_set_ptr->interpolation_search_level = IT_SEARCH_OFF;
    1000          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M7)
    1001           0 :             if (picture_control_set_ptr->temporal_layer_index == 0)
    1002           0 :                 picture_control_set_ptr->interpolation_search_level = IT_SEARCH_FAST_LOOP_UV_BLIND;
    1003             :             else
    1004           0 :                 picture_control_set_ptr->interpolation_search_level = IT_SEARCH_OFF;
    1005             :         else
    1006          60 :             picture_control_set_ptr->interpolation_search_level = IT_SEARCH_OFF;
    1007             : 
    1008             :     // Loop filter Level                            Settings
    1009             :     // 0                                            OFF
    1010             :     // 1                                            CU-BASED
    1011             :     // 2                                            LIGHT FRAME-BASED
    1012             :     // 3                                            FULL FRAME-BASED
    1013             : 
    1014             :     //for now only I frames are allowed to use sc tools.
    1015             :     //TODO: we can force all frames in GOP with the same detection status of leading I frame.
    1016         120 :     if (picture_control_set_ptr->slice_type == I_SLICE) {
    1017           4 :         frm_hdr->allow_screen_content_tools = picture_control_set_ptr->sc_content_detected;
    1018           4 :         if (picture_control_set_ptr->enc_mode <= ENC_M5)
    1019           2 :             frm_hdr->allow_intrabc =  picture_control_set_ptr->sc_content_detected;
    1020             :         else
    1021           2 :             frm_hdr->allow_intrabc =  0;
    1022             : 
    1023             :         //IBC Modes:   0:Slow   1:Fast   2:Faster
    1024           4 :         if (picture_control_set_ptr->enc_mode <= ENC_M2)
    1025           2 :             picture_control_set_ptr->ibc_mode = 0;
    1026             :         else
    1027           2 :             picture_control_set_ptr->ibc_mode = 1;
    1028             :     }
    1029             :     else {
    1030             : #if PAL_SUP
    1031             :         //this will enable sc tools for P frames. hence change bitstream even if palette mode is OFF
    1032         116 :         frm_hdr->allow_screen_content_tools = picture_control_set_ptr->sc_content_detected;
    1033             : #else
    1034             :         frm_hdr->allow_screen_content_tools = 0;
    1035             : #endif
    1036         116 :         frm_hdr->allow_intrabc = 0;
    1037             :     }
    1038             : 
    1039             : #if PAL_SUP
    1040             : 
    1041             :    /*Palette Modes:
    1042             :         0:OFF
    1043             :         1:Slow    NIC=7/4/4
    1044             :         2:        NIC=7/2/2
    1045             :         3:        NIC=7/2/2 + No K means for non ref
    1046             :         4:        NIC=4/2/1
    1047             :         5:        NIC=4/2/1 + No K means for Inter frame
    1048             :         6:Fastest NIC=4/2/1 + No K means for non base + step for non base for most dominent
    1049             : 
    1050             :     */
    1051         120 :     if (frm_hdr->allow_screen_content_tools)
    1052           0 :         if (sequence_control_set_ptr->static_config.enable_palette == -1)//auto mode; if not set by cfg
    1053           0 :             picture_control_set_ptr->palette_mode =
    1054           0 :             (sequence_control_set_ptr->static_config.encoder_bit_depth == EB_8BIT ||
    1055           0 :             (sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT && sequence_control_set_ptr->static_config.enable_hbd_mode_decision == 0)) &&
    1056           0 :             picture_control_set_ptr->enc_mode == ENC_M0 ? 6 : 0;
    1057             :         else
    1058           0 :             picture_control_set_ptr->palette_mode = sequence_control_set_ptr->static_config.enable_palette;
    1059             :     else
    1060         120 :         picture_control_set_ptr->palette_mode = 0;
    1061             : 
    1062             : 
    1063             : 
    1064         120 :     assert(picture_control_set_ptr->palette_mode<7);
    1065             : #endif
    1066         120 :     if (!picture_control_set_ptr->sequence_control_set_ptr->static_config.disable_dlf_flag && frm_hdr->allow_intrabc == 0) {
    1067         120 :     if (sc_content_detected)
    1068           0 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1069           0 :             picture_control_set_ptr->loop_filter_mode = 3;
    1070           0 :         else if (picture_control_set_ptr->enc_mode == ENC_M1)
    1071           0 :             picture_control_set_ptr->loop_filter_mode = picture_control_set_ptr->is_used_as_reference_flag ? 3 : 0;
    1072             :         else
    1073           0 :             picture_control_set_ptr->loop_filter_mode = 0;
    1074             :     else
    1075             : 
    1076         120 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1077          60 :             picture_control_set_ptr->loop_filter_mode = 3;
    1078          60 :         else if (picture_control_set_ptr->enc_mode <= ENC_M5)
    1079           0 :             picture_control_set_ptr->loop_filter_mode = picture_control_set_ptr->is_used_as_reference_flag ? 3 : 0;
    1080             :         else
    1081          60 :             picture_control_set_ptr->loop_filter_mode = picture_control_set_ptr->is_used_as_reference_flag ? 1 : 0;
    1082             :     }
    1083             :     else
    1084           0 :         picture_control_set_ptr->loop_filter_mode = 0;
    1085             :     // CDEF Level                                   Settings
    1086             :     // 0                                            OFF
    1087             :     // 1                                            1 step refinement
    1088             :     // 2                                            4 step refinement
    1089             :     // 3                                            8 step refinement
    1090             :     // 4                                            16 step refinement
    1091             :     // 5                                            64 step refinement
    1092         120 :     if (sequence_control_set_ptr->seq_header.enable_cdef && frm_hdr->allow_intrabc == 0) {
    1093         120 :         if (sc_content_detected)
    1094           0 :             if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1095           0 :                 picture_control_set_ptr->cdef_filter_mode = 4;
    1096             :             else
    1097           0 :                 picture_control_set_ptr->cdef_filter_mode = 0;
    1098             :         else
    1099         120 :         if (picture_control_set_ptr->enc_mode <= ENC_M7)
    1100          60 :             picture_control_set_ptr->cdef_filter_mode = 4;
    1101             :         else
    1102          60 :             picture_control_set_ptr->cdef_filter_mode = 2;
    1103             :     }
    1104             :     else
    1105           0 :         picture_control_set_ptr->cdef_filter_mode = 0;
    1106             : 
    1107             :     // SG Level                                    Settings
    1108             :     // 0                                            OFF
    1109             :     // 1                                            0 step refinement
    1110             :     // 2                                            1 step refinement
    1111             :     // 3                                            4 step refinement
    1112             :     // 4                                            16 step refinement
    1113             : 
    1114         120 :     Av1Common* cm = picture_control_set_ptr->av1_cm;
    1115         120 :     if (sc_content_detected)
    1116           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1117           0 :             cm->sg_filter_mode = 4;
    1118             :         else
    1119           0 :             cm->sg_filter_mode = 0;
    1120             :     else
    1121         120 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1122          60 :         cm->sg_filter_mode = 4;
    1123          60 :     else if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1124           0 :         cm->sg_filter_mode = 3;
    1125             :     else
    1126          60 :         cm->sg_filter_mode = 1;
    1127             : 
    1128             :     // WN Level                                     Settings
    1129             :     // 0                                            OFF
    1130             :     // 1                                            3-Tap luma/ 3-Tap chroma
    1131             :     // 2                                            5-Tap luma/ 5-Tap chroma
    1132             :     // 3                                            7-Tap luma/ 5-Tap chroma
    1133             : 
    1134         120 :     if (sc_content_detected)
    1135           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1136           0 :             cm->wn_filter_mode = 3;
    1137             :         else
    1138           0 :             cm->wn_filter_mode = 0;
    1139             :     else
    1140             : 
    1141         120 :     if (picture_control_set_ptr->enc_mode <= ENC_M5)
    1142          60 :         cm->wn_filter_mode = 3;
    1143          60 :     else if (picture_control_set_ptr->enc_mode <= ENC_M7)
    1144           0 :         cm->wn_filter_mode = 2;
    1145             :     else
    1146          60 :         cm->wn_filter_mode = 0;
    1147             : 
    1148             :     // Tx_search Level                                Settings
    1149             :     // 0                                              OFF
    1150             :     // 1                                              Tx search at encdec
    1151             :     // 2                                              Tx search at inter-depth
    1152             :     // 3                                              Tx search at full loop
    1153         120 :     if (sc_content_detected)
    1154           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1155           0 :             picture_control_set_ptr->tx_search_level = TX_SEARCH_FULL_LOOP;
    1156             :         else
    1157           0 :             if (picture_control_set_ptr->is_used_as_reference_flag)
    1158           0 :                 picture_control_set_ptr->tx_search_level = TX_SEARCH_FULL_LOOP;
    1159             :             else
    1160           0 :                 picture_control_set_ptr->tx_search_level = TX_SEARCH_ENC_DEC;
    1161             :     else
    1162         120 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1163          60 :         picture_control_set_ptr->tx_search_level = TX_SEARCH_FULL_LOOP;
    1164          60 :     else if (picture_control_set_ptr->enc_mode <= ENC_M7) {
    1165           0 :         if (picture_control_set_ptr->temporal_layer_index == 0)
    1166           0 :             picture_control_set_ptr->tx_search_level = TX_SEARCH_FULL_LOOP;
    1167             :         else
    1168           0 :             picture_control_set_ptr->tx_search_level = TX_SEARCH_ENC_DEC;
    1169             :     }
    1170             :     else
    1171          60 :         picture_control_set_ptr->tx_search_level = TX_SEARCH_ENC_DEC;
    1172             : 
    1173             :     // Set tx search skip weights (MAX_MODE_COST: no skipping; 0: always skipping)
    1174         120 :     if (picture_control_set_ptr->tx_search_level == TX_SEARCH_ENC_DEC)
    1175          60 :         picture_control_set_ptr->tx_weight = MAX_MODE_COST;
    1176          60 :     else if (!MR_MODE && picture_control_set_ptr->enc_mode <= ENC_M1)
    1177          60 :         picture_control_set_ptr->tx_weight = FC_SKIP_TX_SR_TH025;
    1178             :     else if (!MR_MODE){
    1179           0 :         if (picture_control_set_ptr->is_used_as_reference_flag)
    1180           0 :             picture_control_set_ptr->tx_weight = FC_SKIP_TX_SR_TH025;
    1181             :         else
    1182           0 :             picture_control_set_ptr->tx_weight = FC_SKIP_TX_SR_TH010;
    1183             :     }
    1184             : 
    1185             :     // Set tx search reduced set falg (0: full tx set; 1: reduced tx set; 1: two tx))
    1186         120 :     if (sc_content_detected)
    1187           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1188           0 :             picture_control_set_ptr->tx_search_reduced_set = 0;
    1189           0 :         else if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1190           0 :             if (picture_control_set_ptr->tx_search_level == TX_SEARCH_ENC_DEC)
    1191           0 :                 picture_control_set_ptr->tx_search_reduced_set = 0;
    1192             :             else
    1193           0 :                 picture_control_set_ptr->tx_search_reduced_set = 1;
    1194           0 :         else if (picture_control_set_ptr->enc_mode <= ENC_M7)
    1195           0 :             picture_control_set_ptr->tx_search_reduced_set = 1;
    1196             :         else
    1197           0 :             picture_control_set_ptr->tx_search_reduced_set = 2;
    1198             :     else
    1199             : 
    1200         120 :     if (picture_control_set_ptr->tx_search_level == TX_SEARCH_ENC_DEC)
    1201          60 :         picture_control_set_ptr->tx_search_reduced_set = 0;
    1202          60 :     else if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1203          60 :         picture_control_set_ptr->tx_search_reduced_set = 0;
    1204           0 :     else if (picture_control_set_ptr->enc_mode <= ENC_M3)
    1205           0 :         if (picture_control_set_ptr->is_used_as_reference_flag)
    1206           0 :             picture_control_set_ptr->tx_search_reduced_set = 0;
    1207             :         else
    1208           0 :             picture_control_set_ptr->tx_search_reduced_set = 1;
    1209             :     else
    1210           0 :         picture_control_set_ptr->tx_search_reduced_set = 1;
    1211             : 
    1212             :     // Intra prediction modes                       Settings
    1213             :     // 0                                            FULL
    1214             :     // 1                                            LIGHT per block : disable_z2_prediction && disable_angle_refinement  for 64/32/4
    1215             :     // 2                                            OFF per block : disable_angle_prediction for 64/32/4
    1216             :     // 3                                            OFF : disable_angle_prediction
    1217             :     // 4                                            OIS based Intra
    1218             :     // 5                                            Light OIS based Intra
    1219             : 
    1220         120 :     if (picture_control_set_ptr->slice_type == I_SLICE)
    1221           4 :     if (sc_content_detected)
    1222           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1223           0 :             picture_control_set_ptr->intra_pred_mode = 0;
    1224             :         else
    1225           0 :             picture_control_set_ptr->intra_pred_mode = 4;
    1226             :     else
    1227           4 :         if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1228           2 :             picture_control_set_ptr->intra_pred_mode = 0;
    1229             :         else
    1230           2 :             picture_control_set_ptr->intra_pred_mode = 4;
    1231             :     else {
    1232         116 :     if (sc_content_detected)
    1233           0 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1234           0 :             picture_control_set_ptr->intra_pred_mode = 0;
    1235           0 :         else if (picture_control_set_ptr->enc_mode <= ENC_M2)
    1236           0 :             if (picture_control_set_ptr->temporal_layer_index == 0)
    1237           0 :                 picture_control_set_ptr->intra_pred_mode = 1;
    1238             :             else
    1239           0 :                 picture_control_set_ptr->intra_pred_mode = 2;
    1240           0 :         else if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1241           0 :             if (picture_control_set_ptr->temporal_layer_index == 0)
    1242           0 :                 picture_control_set_ptr->intra_pred_mode = 2;
    1243             :             else
    1244           0 :                 picture_control_set_ptr->intra_pred_mode = 3;
    1245             :         else
    1246           0 :             picture_control_set_ptr->intra_pred_mode = 4;
    1247             :     else
    1248             : 
    1249         116 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1250          58 :             picture_control_set_ptr->intra_pred_mode = 0;
    1251          58 :         else if (picture_control_set_ptr->enc_mode  <= ENC_M1)
    1252           0 :             if (picture_control_set_ptr->temporal_layer_index == 0)
    1253           0 :                 picture_control_set_ptr->intra_pred_mode = 1;
    1254             :             else
    1255           0 :                 picture_control_set_ptr->intra_pred_mode = 2;
    1256          58 :         else if(picture_control_set_ptr->enc_mode <= ENC_M6)
    1257           0 :             if (picture_control_set_ptr->temporal_layer_index == 0)
    1258           0 :                 picture_control_set_ptr->intra_pred_mode = 1;
    1259             :             else
    1260           0 :                 picture_control_set_ptr->intra_pred_mode = 3;
    1261             :         else
    1262          58 :             picture_control_set_ptr->intra_pred_mode = 4;
    1263             :     }
    1264             : 
    1265             :     if (MR_MODE)
    1266             :         picture_control_set_ptr->intra_pred_mode = 0;
    1267             : 
    1268             :     // Skip sub blk based on neighbors depth        Settings
    1269             :     // 0                                            OFF
    1270             :     // 1                                            ON
    1271         120 :     picture_control_set_ptr->skip_sub_blks =   0;
    1272             : 
    1273         120 :         if (picture_control_set_ptr->sc_content_detected)
    1274           0 :             picture_control_set_ptr->cu8x8_mode = (picture_control_set_ptr->temporal_layer_index > 0) ?
    1275           0 :             CU_8x8_MODE_1 :
    1276             :             CU_8x8_MODE_0;
    1277             :         else
    1278         120 :         if (picture_control_set_ptr->enc_mode <= ENC_M8)
    1279         120 :             picture_control_set_ptr->cu8x8_mode = CU_8x8_MODE_0;
    1280             :         else
    1281           0 :             picture_control_set_ptr->cu8x8_mode = (picture_control_set_ptr->temporal_layer_index > 0) ?
    1282           0 :             CU_8x8_MODE_1 :
    1283             :             CU_8x8_MODE_0;
    1284             : 
    1285             :         // Set atb mode      Settings
    1286             :         // 0                 OFF: no transform partitioning
    1287             :         // 1                 ON for INTRA blocks
    1288             : #if ATB_10_BIT
    1289         120 :         if (picture_control_set_ptr->enc_mode <= ENC_M1 &&  !sequence_control_set_ptr->static_config.enable_hbd_mode_decision)
    1290             : #else
    1291             :         if (picture_control_set_ptr->enc_mode <= ENC_M1 && sequence_control_set_ptr->static_config.encoder_bit_depth == EB_8BIT)
    1292             : #endif
    1293             : #if SPEED_OPT
    1294          60 :             picture_control_set_ptr->atb_mode = (MR_MODE || picture_control_set_ptr->temporal_layer_index == 0) ? 1 : 0;
    1295             : #else
    1296             :             picture_control_set_ptr->atb_mode = 1;
    1297             : #endif
    1298             :         else
    1299          60 :             picture_control_set_ptr->atb_mode = 0;
    1300             : 
    1301             :         // Set skip atb                          Settings
    1302             :         // 0                                     OFF
    1303             :         // 1                                     ON
    1304             : 
    1305             : #if SPEED_OPT
    1306         120 :         if (MR_MODE || picture_control_set_ptr->sc_content_detected)
    1307             : #else
    1308             :         if (MR_MODE || picture_control_set_ptr->enc_mode == ENC_M0 || picture_control_set_ptr->sc_content_detected)
    1309             : #endif
    1310           0 :             picture_control_set_ptr->coeff_based_skip_atb = 0;
    1311             :         else
    1312         120 :             picture_control_set_ptr->coeff_based_skip_atb = 1;
    1313             : 
    1314             :         // Set Wedge mode      Settings
    1315             :         // 0                 FULL: Full search
    1316             :         // 1                 Fast: If two predictors are very similar, skip wedge compound mode search
    1317             :         // 2                 Fast: estimate Wedge sign
    1318             :         // 3                 Fast: Mode 1 & Mode 2
    1319             : 
    1320         120 :         picture_control_set_ptr->wedge_mode = 0;
    1321             : 
    1322             : #if II_COMP_FLAG
    1323             :         // inter intra pred                      Settings
    1324             :         // 0                                     OFF
    1325             :         // 1                                     ON
    1326         120 :             picture_control_set_ptr->enable_inter_intra = picture_control_set_ptr->slice_type != I_SLICE ? sequence_control_set_ptr->seq_header.enable_interintra_compound : 0;
    1327             : #endif
    1328             :         // Set compound mode      Settings
    1329             :         // 0                 OFF: No compond mode search : AVG only
    1330             :         // 1                 ON: compond mode search: AVG/DIST/DIFF
    1331             :         // 2                 ON: AVG/DIST/DIFF/WEDGE
    1332         120 :         if (sequence_control_set_ptr->compound_mode)
    1333         120 :             picture_control_set_ptr->compound_mode = picture_control_set_ptr->sc_content_detected ? 0 :
    1334          60 :             picture_control_set_ptr->enc_mode <= ENC_M1 ? 2 : 1;
    1335             :         else
    1336          60 :             picture_control_set_ptr->compound_mode = 0;
    1337             : 
    1338             :         // set compound_types_to_try
    1339         120 :         if (picture_control_set_ptr->compound_mode)
    1340          60 :             picture_control_set_ptr->compound_types_to_try = picture_control_set_ptr->compound_mode == 1 ? MD_COMP_DIFF0 : MD_COMP_WEDGE;
    1341             :         else
    1342          60 :             picture_control_set_ptr->compound_types_to_try = MD_COMP_AVG;
    1343             : 
    1344             :         // Set frame end cdf update mode      Settings
    1345             :         // 0                                     OFF
    1346             :         // 1                                     ON
    1347         120 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1348          60 :             picture_control_set_ptr->frame_end_cdf_update_mode = 1;
    1349             :         else
    1350          60 :             picture_control_set_ptr->frame_end_cdf_update_mode = 0;
    1351         120 :         if (picture_control_set_ptr->sc_content_detected || picture_control_set_ptr->enc_mode == ENC_M0 || picture_control_set_ptr->enc_mode >= ENC_M4)
    1352         120 :             picture_control_set_ptr->prune_unipred_at_me = 0;
    1353             :         else
    1354           0 :             picture_control_set_ptr->prune_unipred_at_me = 1;
    1355             :         //CHKN: Temporal MVP should be disabled for pictures beloning to 4L MiniGop preceeded by 5L miniGOP. in this case the RPS is wrong(known issue). check RPS construction for more info.
    1356         120 :         if ((sequence_control_set_ptr->static_config.hierarchical_levels == 4 && picture_control_set_ptr->hierarchical_levels == 3) ||
    1357          98 :             picture_control_set_ptr->slice_type == I_SLICE)
    1358          26 :             picture_control_set_ptr->frm_hdr.use_ref_frame_mvs = 0;
    1359             :         else
    1360          94 :             picture_control_set_ptr->frm_hdr.use_ref_frame_mvs = sequence_control_set_ptr->mfmv_enabled;
    1361         120 :     return return_error;
    1362             : }
    1363             : 
    1364             : int8_t av1_ref_frame_type(const MvReferenceFrame *const rf);
    1365             : //set the ref frame types used for this picture,
    1366         120 : void set_all_ref_frame_type(SequenceControlSet *sequence_control_set_ptr, PictureParentControlSet  *parent_pcs_ptr, MvReferenceFrame ref_frame_arr[], uint8_t* tot_ref_frames)
    1367             : {
    1368             :     MvReferenceFrame rf[2];
    1369         120 :     *tot_ref_frames = 0;
    1370             : 
    1371             :     //printf("POC %i  totRef L0:%i   totRef L1: %i\n", parent_pcs_ptr->picture_number, parent_pcs_ptr->ref_list0_count, parent_pcs_ptr->ref_list1_count);
    1372             : 
    1373             :     //single ref - List0
    1374         352 :     for (uint8_t ref_idx0 = 0; ref_idx0 < parent_pcs_ptr->ref_list0_count; ++ref_idx0) {
    1375         232 :         rf[0] = svt_get_ref_frame_type(REF_LIST_0, ref_idx0);
    1376         232 :         ref_frame_arr[(*tot_ref_frames)++] = rf[0];
    1377             :     }
    1378             : 
    1379             :     //single ref - List1
    1380         283 :     for (uint8_t ref_idx1 = 0; ref_idx1 < parent_pcs_ptr->ref_list1_count; ++ref_idx1) {
    1381         163 :         rf[1] = svt_get_ref_frame_type(REF_LIST_1, ref_idx1);
    1382         163 :         ref_frame_arr[(*tot_ref_frames)++] = rf[1];
    1383             :     }
    1384             : 
    1385             :     //compound Bi-Dir
    1386         352 :     for (uint8_t ref_idx0 = 0; ref_idx0 < parent_pcs_ptr->ref_list0_count; ++ref_idx0) {
    1387         605 :         for (uint8_t ref_idx1 = 0; ref_idx1 < parent_pcs_ptr->ref_list1_count; ++ref_idx1) {
    1388         373 :             rf[0] = svt_get_ref_frame_type(REF_LIST_0, ref_idx0);
    1389         373 :             rf[1] = svt_get_ref_frame_type(REF_LIST_1, ref_idx1);
    1390         373 :             ref_frame_arr[(*tot_ref_frames)++] = av1_ref_frame_type(rf);
    1391             :         }
    1392             :     }
    1393             : 
    1394         120 :     if (sequence_control_set_ptr->mrp_mode == 0 && parent_pcs_ptr->slice_type == B_SLICE)
    1395             :     {
    1396             : 
    1397             :         //compound Uni-Dir
    1398          55 :         if (parent_pcs_ptr->ref_list0_count > 1) {
    1399          41 :             rf[0] = LAST_FRAME;
    1400          41 :             rf[1] = LAST2_FRAME;
    1401          41 :             ref_frame_arr[(*tot_ref_frames)++] = av1_ref_frame_type(rf);
    1402          41 :             if (parent_pcs_ptr->ref_list0_count > 2) {
    1403          41 :                 rf[1] = LAST3_FRAME;
    1404          41 :                 ref_frame_arr[(*tot_ref_frames)++] = av1_ref_frame_type(rf);
    1405          41 :                 if (parent_pcs_ptr->ref_list0_count > 3) {
    1406          25 :                     rf[1] = GOLDEN_FRAME;
    1407          25 :                     ref_frame_arr[(*tot_ref_frames)++] = av1_ref_frame_type(rf);
    1408             :                 }
    1409             :             }
    1410             :         }
    1411          55 :         if (parent_pcs_ptr->ref_list1_count > 2) {
    1412          16 :             rf[0] = BWDREF_FRAME;
    1413          16 :             rf[1] = ALTREF_FRAME;
    1414          16 :             ref_frame_arr[(*tot_ref_frames)++] = av1_ref_frame_type(rf);
    1415             :         }
    1416             :         }
    1417         120 :     }
    1418             : 
    1419             : /*************************************************
    1420             : * AV1 Reference Picture Signalling:
    1421             : * Stateless derivation of RPS info to be stored in
    1422             : * Picture Header
    1423             : *
    1424             : * This function uses the picture index from the just
    1425             : * collected miniGop to derive the RPS(refIndexes+refresh)
    1426             : * the miniGop is always 4L but could be complete (8 pictures)
    1427             : or non-complete (less than 8 pictures).
    1428             : * We get to this function if the picture is:
    1429             : * 1) first Key frame
    1430             : * 2) part of a complete RA MiniGop where the last frame could be a regular I for open GOP
    1431             : * 3) part of complete LDP MiniGop where the last frame could be Key frame for closed GOP
    1432             : * 4) part of non-complete LDP MiniGop where the last frame is a regularI+SceneChange.
    1433             : This miniGOP has P frames with predStruct=LDP, and the last frame=I with pred struct=RA.
    1434             : * 5) part of non-complete LDP MiniGop at the end of the stream.This miniGOP has P frames with
    1435             : predStruct=LDP, and the last frame=I with pred struct=RA.
    1436             : *
    1437             : *Note: the  SceneChange I has pred_type = EB_PRED_RANDOM_ACCESS. if SChange is aligned on the miniGop,
    1438             : we do not break the GOP.
    1439             : *************************************************/
    1440         120 : void  Av1GenerateRpsInfo(
    1441             :     PictureParentControlSet       *picture_control_set_ptr,
    1442             :     EncodeContext                 *encode_context_ptr,
    1443             :     PictureDecisionContext        *context_ptr,
    1444             :     uint32_t                       picture_index,
    1445             :     uint32_t                       mini_gop_index
    1446             : )
    1447             : {
    1448             :     (void)encode_context_ptr;
    1449         120 :     Av1RpsNode *av1_rps = &picture_control_set_ptr->av1_ref_signal;
    1450         120 :     FrameHeader *frm_hdr = &picture_control_set_ptr->frm_hdr;
    1451             : 
    1452         120 :     PredictionStructureEntry *pred_position_ptr = picture_control_set_ptr->pred_struct_ptr->pred_struct_entry_ptr_array[picture_control_set_ptr->pred_struct_index];
    1453             :     //set Frame Type
    1454         120 :     if (picture_control_set_ptr->slice_type == I_SLICE)
    1455           4 :         frm_hdr->frame_type = picture_control_set_ptr->idr_flag ? KEY_FRAME : INTRA_ONLY_FRAME;
    1456             :     else
    1457         116 :         frm_hdr->frame_type = INTER_FRAME;
    1458             : 
    1459         120 :     picture_control_set_ptr->intra_only = picture_control_set_ptr->slice_type == I_SLICE ? 1 : 0;
    1460             : 
    1461             :     //RPS for Flat GOP
    1462         120 :     if (picture_control_set_ptr->hierarchical_levels == 0)
    1463             :     {
    1464           0 :         memset(av1_rps->ref_dpb_index, 0, 7);
    1465           0 :         av1_rps->refresh_frame_mask = 1;
    1466           0 :         frm_hdr->show_frame = EB_TRUE;
    1467           0 :         picture_control_set_ptr->has_show_existing = EB_FALSE;
    1468             :     }
    1469         120 :     else if (picture_control_set_ptr->hierarchical_levels == 3)//RPS for 4L GOP
    1470             :     {
    1471             : 
    1472             :         uint8_t gop_i;
    1473          22 :         EbBool is_trailing_frames = EB_FALSE;
    1474             : 
    1475          22 :         if (frm_hdr->frame_type == KEY_FRAME)
    1476             :         {
    1477           0 :             context_ptr->lay0_toggle = 0;
    1478           0 :             context_ptr->lay1_toggle = 0;
    1479           0 :             context_ptr->lay2_toggle = 0;
    1480             : 
    1481           0 :             frm_hdr->show_frame = EB_TRUE;
    1482           0 :             picture_control_set_ptr->has_show_existing = EB_FALSE;
    1483           0 :             return;
    1484             :         }
    1485             : 
    1486             :         //picture_index has this order:
    1487             :         //         0     2    4      6
    1488             :         //            1          5
    1489             :         //                 3
    1490             :         //                              7(could be an I)
    1491             :         //
    1492             : 
    1493             :         //DPB: Loc7|Loc6|Loc5|Loc4|Loc3|Loc2|Loc1|Loc0
    1494             :         //Layer 0 : circular move 0-1-2
    1495             :         //Layer 1 : circular move 3-4
    1496             :         //Layer 2 : circular move 5-6
    1497             : #if PRED_CHANGE
    1498             :         //Layer 3 : 7
    1499             : #else
    1500             :         //Layer 3 : not kept. DPB Location 7 is unused.
    1501             : #endif
    1502             : 
    1503             :         //pic_num
    1504             :         //         1     3    5      7    9     11     13      15
    1505             :         //            2          6           10            14
    1506             :         //                 4                        12
    1507             :         //
    1508             :         //base0:0                   base1:8                          base2:16
    1509          22 :         if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_LOW_DELAY_P &&
    1510           6 :                 (context_ptr->mini_gop_length[mini_gop_index] < 8) &&
    1511           6 :                 picture_control_set_ptr->sequence_control_set_ptr->static_config.pred_structure == EB_PRED_RANDOM_ACCESS ) {
    1512           6 :             is_trailing_frames = EB_TRUE;
    1513             :         }
    1514             : 
    1515             : 
    1516          22 :         const uint8_t  base0_idx = context_ptr->lay0_toggle == 0 ? 1 : context_ptr->lay0_toggle == 1 ? 2 : 0;   //the oldest L0 picture in the DPB
    1517          22 :         const uint8_t  base1_idx = context_ptr->lay0_toggle == 0 ? 2 : context_ptr->lay0_toggle == 1 ? 0 : 1;   //the middle L0 picture in the DPB
    1518          22 :         const uint8_t  base2_idx = context_ptr->lay0_toggle == 0 ? 0 : context_ptr->lay0_toggle == 1 ? 1 : 2;   //the newest L0 picture in the DPB
    1519             : 
    1520          22 :         const uint8_t  lay1_0_idx = context_ptr->lay1_toggle == 0 ? LAY1_OFF + 1 : LAY1_OFF + 0;   //the oldest L1 picture in the DPB
    1521          22 :         const uint8_t  lay1_1_idx = context_ptr->lay1_toggle == 0 ? LAY1_OFF + 0 : LAY1_OFF + 1;   //the newest L1 picture in the DPB
    1522             : 
    1523          22 :         const uint8_t  lay2_0_idx = picture_index < 4 ? LAY2_OFF + 1 : LAY2_OFF + 0;   //the oldest L2 picture in the DPB
    1524          22 :         const uint8_t  lay2_1_idx = picture_index < 4 ? LAY2_OFF + 0 : LAY2_OFF + 1;   //the newest L2 picture in the DPB
    1525             : #if PRED_CHANGE
    1526          22 :         const uint8_t  lay3_idx = 7;    //the newest L3 picture in the DPB
    1527             : #endif
    1528             :         /*in 5L struct, we switich to  4L at the end of the seq.
    1529             :         the current pred struct is reset, and the previous 5L minGop is out of reach!
    1530             :         four_level_hierarchical_pred_struct will be set to follow 4L order, but this will generate some RPS mismatch for some frames.
    1531             :         PKTization DPB can detect those  */
    1532             : 
    1533          22 :         switch (picture_control_set_ptr->temporal_layer_index) {
    1534           2 :         case 0:
    1535             : 
    1536             :             //{8, 0, 0, 0},     // GOP Index 0 - Ref List 0
    1537             :             //{8, 0,  0, 0}      // GOP Index 0 - Ref List 1
    1538           2 :             av1_rps->ref_dpb_index[LAST] = base1_idx;
    1539           2 :             av1_rps->ref_dpb_index[LAST2] = av1_rps->ref_dpb_index[LAST];
    1540           2 :             av1_rps->ref_dpb_index[LAST3] = av1_rps->ref_dpb_index[LAST];
    1541           2 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    1542             : 
    1543           2 :             av1_rps->ref_dpb_index[BWD] = base1_idx;
    1544           2 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    1545           2 :             av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1546           2 :             gop_i = 0;
    1547           2 :             av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1548           2 :             av1_rps->ref_poc_array[LAST2] = av1_rps->ref_poc_array[LAST];
    1549           2 :             av1_rps->ref_poc_array[LAST3] = av1_rps->ref_poc_array[LAST];
    1550           2 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    1551             : 
    1552           2 :             av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1553           2 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    1554           2 :             av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1555             : 
    1556           2 :             av1_rps->refresh_frame_mask = 1 << context_ptr->lay0_toggle;
    1557             : 
    1558           2 :             break;
    1559             : 
    1560           2 :         case 1:
    1561             :             //{ 4, 8, 12,  0},   // GOP Index 4 - Ref List 0
    1562             :             //{-4,  0, 0,  0}     // GOP Index 4 - Ref List 1
    1563           2 :             av1_rps->ref_dpb_index[LAST] = base1_idx;
    1564           2 :             av1_rps->ref_dpb_index[LAST2] = lay1_0_idx;
    1565           2 :             av1_rps->ref_dpb_index[LAST3] = base0_idx;
    1566           2 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    1567             : 
    1568           2 :             av1_rps->ref_dpb_index[BWD] = base2_idx;
    1569           2 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    1570           2 :             av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1571             : 
    1572           2 :             gop_i = 4;
    1573           2 :             av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1574           2 :             av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1575           2 :             av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1576           2 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    1577             : 
    1578           2 :             av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1579           2 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    1580           2 :             av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1581           2 :             av1_rps->refresh_frame_mask = 1 << (LAY1_OFF + context_ptr->lay1_toggle);
    1582             : 
    1583           2 :             break;
    1584             : 
    1585           6 :         case 2:
    1586             : 
    1587           6 :             if (picture_index == 1) {
    1588             :                 //{  2,  4,  6,  10}    // GOP Index 2 - Ref List 0
    1589             :                 //{ -2, -6,  0,   0}    // GOP Index 2 - Ref List 1
    1590             : 
    1591           4 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    1592           4 :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    1593           4 :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    1594           4 :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    1595             : 
    1596           4 :                 av1_rps->ref_dpb_index[BWD] = lay1_1_idx;
    1597           4 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    1598           4 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1599           4 :                 gop_i = 2;
    1600           4 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1601           4 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1602           4 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1603           4 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1604             : 
    1605           4 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1606           4 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    1607           4 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1608             :             }
    1609           2 :             else if (picture_index == 5) {
    1610             :                 //{ 2, 4, 6, 10}   // GOP Index 6 - Ref List 0
    1611             :                 //{ -2,  0, 0,  0 }    // GOP Index 6 - Ref List 1
    1612             : 
    1613           2 :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    1614           2 :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    1615           2 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    1616           2 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;// av1_rps->ref_dpb_index[LAST];
    1617             : 
    1618           2 :                 av1_rps->ref_dpb_index[BWD] = base2_idx;
    1619           2 :                 av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    1620           2 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1621           2 :                 gop_i = 6;
    1622           2 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1623           2 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1624           2 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1625           2 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1626             : 
    1627           2 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1628           2 :                 av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    1629           2 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1630             :             }
    1631             : 
    1632           6 :             av1_rps->refresh_frame_mask = 1 << (LAY2_OFF + context_ptr->lay2_toggle);
    1633             :             //toggle 3->4
    1634           6 :             if (!is_trailing_frames || (context_ptr->mini_gop_length[mini_gop_index] >= 7 && is_trailing_frames)) {
    1635             :                 //For trailing frames, Only toggle it if we are sure we have 2 layer 2 frames in trailing frames
    1636           4 :                 context_ptr->lay2_toggle = 1 - context_ptr->lay2_toggle;
    1637             :             }
    1638             : 
    1639           6 :             break;
    1640             : 
    1641          12 :         case 3:
    1642             :             // update RPS for the overlay frame.
    1643          12 :             if (picture_control_set_ptr->is_overlay) {
    1644             :                 //{ 0, 0, 0, 0}         // GOP Index 1 - Ref List 0
    1645             :                 //{ 0, 0, 0, 0 }       // GOP Index 1 - Ref List 1
    1646           0 :                 av1_rps->ref_dpb_index[LAST]  = base1_idx;
    1647           0 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    1648           0 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    1649           0 :                 av1_rps->ref_dpb_index[GOLD]  = base1_idx;
    1650             : 
    1651           0 :                 av1_rps->ref_dpb_index[BWD]  = base1_idx;
    1652           0 :                 av1_rps->ref_dpb_index[ALT]  = base1_idx;
    1653           0 :                 av1_rps->ref_dpb_index[ALT2] = base1_idx;
    1654           0 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1655           0 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1656           0 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1657           0 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1658             : 
    1659           0 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1660           0 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1661           0 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    1662             :             }
    1663             :             else
    1664             : 
    1665          12 :             if (picture_index == 0) {
    1666             : #if PRED_CHANGE
    1667             :                 //{ 1, 3, 5, 8}         // GOP Index 1 - Ref List 0
    1668             : #else
    1669             :                 //{ 1, 3, 5, 9}         // GOP Index 1 - Ref List 0
    1670             : #endif
    1671             :                 //{ -1, -3, -7,  0 }    // GOP Index 1 - Ref List 1
    1672           4 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    1673           4 :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    1674           4 :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    1675             : #if PRED_CHANGE
    1676           4 :                 av1_rps->ref_dpb_index[GOLD] = lay3_idx;
    1677             : #else
    1678             :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    1679             : #endif
    1680           4 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    1681           4 :                 av1_rps->ref_dpb_index[ALT] = lay1_1_idx;
    1682           4 :                 av1_rps->ref_dpb_index[ALT2] = base2_idx;
    1683           4 :                 gop_i = 1;
    1684           4 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1685           4 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1686           4 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1687           4 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1688             : 
    1689           4 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1690           4 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    1691           4 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    1692             :             }
    1693           8 :             else if (picture_index == 2) {
    1694             : #if PRED_CHANGE_MOD
    1695             :                 // { 1, 3, 2, 5},        // GOP Index 3 - Ref List 0
    1696             : #elif PRED_CHANGE
    1697             :                 // { 1, 2, 3, 5},        // GOP Index 3 - Ref List 0
    1698             : #else
    1699             :                 //{ 1, 3, 5, 7},         //    GOP Index 3 - Ref List 0
    1700             : #endif
    1701             :                 //{ -1,  -5, 0,  0 }     //     GOP Index 3 - Ref List 1
    1702             : #if PRED_CHANGE_MOD
    1703           4 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    1704           4 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    1705           4 :                 av1_rps->ref_dpb_index[LAST3] = lay3_idx;
    1706           4 :                 av1_rps->ref_dpb_index[GOLD] = lay2_0_idx;
    1707             : #elif PRED_CHANGE
    1708             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    1709             :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    1710             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    1711             :                 av1_rps->ref_dpb_index[GOLD] = lay2_0_idx;
    1712             : #else
    1713             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    1714             :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    1715             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    1716             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    1717             : #endif
    1718           4 :                 av1_rps->ref_dpb_index[BWD] = lay1_1_idx;
    1719           4 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    1720           4 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1721           4 :                 gop_i = 3;
    1722           4 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1723           4 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1724           4 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1725           4 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1726             : 
    1727           4 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1728           4 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    1729           4 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1730             :             }
    1731           4 :             else if (picture_index == 4) {
    1732             : #if PRED_CHANGE
    1733             :                // { 1, 3, 5, 4},    // GOP Index 5 - Ref List 0
    1734             : #else
    1735             :                 //{ 1, 3, 5, 9},         // GOP Index 5 - Ref List 0
    1736             : #endif
    1737             :                 //{ -1,  -3, 0,  0 }     // GOP Index 5 - Ref List 1
    1738           2 :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    1739           2 :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    1740           2 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    1741             : #if PRED_CHANGE
    1742           2 :                 av1_rps->ref_dpb_index[GOLD] = lay3_idx;
    1743             : #else
    1744             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    1745             : #endif
    1746           2 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    1747           2 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    1748           2 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1749           2 :                 gop_i = 5;
    1750           2 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1751           2 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1752           2 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1753           2 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1754             : 
    1755           2 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1756           2 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    1757           2 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1758             :             }
    1759           2 :             else if (picture_index == 6) {
    1760             : #if PRED_CHANGE
    1761             :                 //{ 1,  3, 5,  6},     //  GOP Index 7 - Ref List 0
    1762             : #else
    1763             :                 //{ 1, 3, 5, 7},         //  GOP Index 7 - Ref List 0
    1764             : #endif
    1765             :                 //{ -1,  0, 0,  0 }      // GOP Index 7 - Ref List 1
    1766           2 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    1767           2 :                 av1_rps->ref_dpb_index[LAST2] = lay1_1_idx;
    1768           2 :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    1769             : #if PRED_CHANGE
    1770           2 :                 av1_rps->ref_dpb_index[GOLD] = lay3_idx;
    1771             : #else
    1772             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    1773             : #endif
    1774           2 :                 av1_rps->ref_dpb_index[BWD] = base2_idx;
    1775           2 :                 av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    1776           2 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1777           2 :                 gop_i = 7;
    1778           2 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1779           2 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1780           2 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    1781           2 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    1782             : 
    1783           2 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, four_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1784           2 :                 av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    1785           2 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1786             :             }
    1787             :             else
    1788           0 :                 printf("Error in GOp indexing\n");
    1789             : #if PRED_CHANGE
    1790          12 :             if (picture_index == 0)
    1791           4 :                 av1_rps->refresh_frame_mask = 1 << (lay3_idx);
    1792             :             else
    1793           8 :                 av1_rps->refresh_frame_mask = 0;
    1794             : #else
    1795             :             av1_rps->refresh_frame_mask = 0;
    1796             : #endif
    1797          12 :             break;
    1798             : 
    1799           0 :         default:
    1800           0 :             printf("Error: unexpected picture mini Gop number\n");
    1801           0 :             break;
    1802             :         }
    1803             : 
    1804             : 
    1805             :         // Jing: Check for reference number, remove invalid dependancy
    1806          22 :         if (pred_position_ptr->ref_list0.reference_list_count < 4) {
    1807          17 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    1808          17 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    1809             :         }
    1810          22 :         if (pred_position_ptr->ref_list0.reference_list_count < 3) {
    1811          15 :             av1_rps->ref_dpb_index[LAST3] = av1_rps->ref_dpb_index[LAST];
    1812          15 :             av1_rps->ref_poc_array[LAST3] = av1_rps->ref_poc_array[LAST];
    1813             :         }
    1814          22 :         if (pred_position_ptr->ref_list0.reference_list_count < 2) {
    1815          15 :             av1_rps->ref_dpb_index[LAST2] = av1_rps->ref_dpb_index[LAST];
    1816          15 :             av1_rps->ref_poc_array[LAST2] = av1_rps->ref_poc_array[LAST];
    1817             :         }
    1818             : 
    1819             :         {
    1820          22 :             int tmp = av1_rps->ref_dpb_index[ALT];
    1821          22 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[ALT2];
    1822          22 :             av1_rps->ref_dpb_index[ALT2] = tmp;
    1823             : 
    1824          22 :             uint64_t tmp1 = av1_rps->ref_poc_array[ALT];
    1825          22 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[ALT2];
    1826          22 :             av1_rps->ref_poc_array[ALT2] = tmp1;
    1827             :         }
    1828             : 
    1829             :         // update RPS for the overlay frame.
    1830          22 :         if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_LOW_DELAY_P || picture_control_set_ptr->is_overlay)
    1831             :         {
    1832             :             //P frames
    1833           6 :             av1_rps->ref_dpb_index[4] = av1_rps->ref_dpb_index[5] = av1_rps->ref_dpb_index[6] = av1_rps->ref_dpb_index[0];
    1834           6 :             av1_rps->ref_poc_array[4] = av1_rps->ref_poc_array[5] = av1_rps->ref_poc_array[6] = av1_rps->ref_poc_array[0];
    1835             : 
    1836           6 :             frm_hdr->show_frame = EB_TRUE;
    1837           6 :             picture_control_set_ptr->has_show_existing = EB_FALSE;
    1838             :         }
    1839          16 :         else if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_RANDOM_ACCESS)
    1840             :         {
    1841             :             //Decide on Show Mecanism
    1842          16 :             if (picture_control_set_ptr->slice_type == I_SLICE)
    1843             :             {
    1844             :                 //3 cases for I slice:  1:Key Frame treated above.  2: broken MiniGop due to sc or intra refresh  3: complete miniGop due to sc or intra refresh
    1845           0 :                 if (context_ptr->mini_gop_length[mini_gop_index] < picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    1846             :                 {
    1847             :                     //Scene Change that breaks the mini gop and switch to LDP (if I scene change happens to be aligned with a complete miniGop, then we do not break the pred structure)
    1848           0 :                     frm_hdr->show_frame = EB_TRUE;
    1849           0 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    1850             :                 }
    1851             :                 else
    1852             :                 {
    1853           0 :                     frm_hdr->show_frame = EB_FALSE;
    1854           0 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    1855             :                 }
    1856             :             }
    1857             :             else//B pic
    1858             :             {
    1859          16 :                 if (context_ptr->mini_gop_length[0] != picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    1860           0 :                     printf("Error in GOp indexing3\n");
    1861             : #if PRED_CHANGE
    1862          16 :                 if (picture_control_set_ptr->is_used_as_reference_flag && picture_index != 0)
    1863             : #else
    1864             :                 if (picture_control_set_ptr->is_used_as_reference_flag)
    1865             : #endif
    1866             :                 {
    1867           8 :                     frm_hdr->show_frame = EB_FALSE;
    1868           8 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    1869             :                 }
    1870             :                 else
    1871             :                 {
    1872           8 :                     frm_hdr->show_frame = EB_TRUE;
    1873           8 :                     picture_control_set_ptr->has_show_existing = EB_TRUE;
    1874             : 
    1875           8 :                     if (picture_index == 0)
    1876           2 :                         frm_hdr->show_existing_frame = lay2_1_idx;
    1877           6 :                     else if (picture_index == 2)
    1878           2 :                         frm_hdr->show_existing_frame = lay1_1_idx;
    1879           4 :                     else if (picture_index == 4)
    1880           2 :                         frm_hdr->show_existing_frame = lay2_1_idx;
    1881           2 :                     else if (picture_index == 6)
    1882           2 :                         frm_hdr->show_existing_frame = base2_idx;
    1883             :                     else
    1884           0 :                         printf("Error in GOp indexing2\n");
    1885             :                 }
    1886             :             }
    1887             :         }
    1888             :         else {
    1889           0 :             printf("Error: Not supported GOP structure!");
    1890           0 :             exit(0);
    1891             :         }
    1892             : 
    1893             :         //last pic in MiniGop: Base layer toggling
    1894             :         //mini GOP toggling since last Key Frame.
    1895             :         //a regular I keeps the toggling process and does not reset the toggle.  K-0-1-0-1-0-K-0-1-0-1-K-0-1.....
    1896             :         //whoever needs a miniGOP Level toggling, this is the time
    1897          22 :         if (picture_index == context_ptr->mini_gop_end_index[mini_gop_index] % 8 && !picture_control_set_ptr->is_overlay) {
    1898             :             //Layer0 toggle 0->1->2
    1899           4 :             context_ptr->lay0_toggle = circ_inc(3, 1, context_ptr->lay0_toggle);
    1900             :             //Layer1 toggle 3->4
    1901           4 :             context_ptr->lay1_toggle = 1 - context_ptr->lay1_toggle;
    1902             :         }
    1903             :     }
    1904          98 :     else if (picture_control_set_ptr->hierarchical_levels == 4)//RPS for 4L GOP
    1905             :     {
    1906             :     if (1)//MRP
    1907             :     {
    1908             :         uint8_t gop_i;
    1909             :         //Av1RpsNode_t *av1_rps = &picture_control_set_ptr->av1RefSignal2;
    1910             : 
    1911             :         //Reset miniGop Toggling. The first miniGop after a KEY frame has toggle=0
    1912          98 :         if (frm_hdr->frame_type == KEY_FRAME)
    1913             :         {
    1914           2 :             context_ptr->lay0_toggle = 0;
    1915           2 :             context_ptr->lay1_toggle = 0;
    1916           2 :             context_ptr->lay2_toggle = 0;
    1917             : 
    1918           2 :             frm_hdr->show_frame = EB_TRUE;
    1919           2 :             picture_control_set_ptr->has_show_existing = EB_FALSE;
    1920           2 :             return;
    1921             :         }
    1922             : 
    1923             :         //picture_index has this order:
    1924             :         //         0     2    4      6    8     10     12      14
    1925             :         //            1          5           9            13
    1926             :         //                 3                        11
    1927             :         //                              7
    1928             :         //                                                          15(could be an I)
    1929             : 
    1930             :         //DPB: Loc7|Loc6|Loc5|Loc4|Loc3|Loc2|Loc1|Loc0
    1931             :         //Layer 0 : circular move 0-1-2
    1932             :         //Layer 1 : circular move 3-4
    1933             : #if PRED_CHANGE_5L
    1934             :         //Layer 2 : DPB Location 5
    1935             :         //Layer 3 : DPB Location 6
    1936             :         //Layer 4 : DPB Location 7
    1937             : #else
    1938             :         //Layer 2 : circular move 5-6
    1939             :         //Layer 3 : DPB Location 7
    1940             : #endif
    1941             :         //pic_num                  for poc 17
    1942             :         //         1     3    5      7    9     11     13      15         17    19     21    23   25     27    29    31
    1943             :         //            2          6           10            14                18           22          26          30
    1944             :         //                 4                        12:L2_0                         20:L2_1                 28
    1945             :         //                              8:L1_0                                                       24:L1_1
    1946             :         //base0:0                                               base1:16                                           base2:32
    1947             : 
    1948          96 :         const uint8_t  base0_idx = context_ptr->lay0_toggle == 0 ? 1 : context_ptr->lay0_toggle == 1 ? 2 : 0;   //the oldest L0 picture in the DPB
    1949          96 :         const uint8_t  base1_idx = context_ptr->lay0_toggle == 0 ? 2 : context_ptr->lay0_toggle == 1 ? 0 : 1;   //the middle L0 picture in the DPB
    1950          96 :         const uint8_t  base2_idx = context_ptr->lay0_toggle == 0 ? 0 : context_ptr->lay0_toggle == 1 ? 1 : 2;   //the newest L0 picture in the DPB
    1951             : 
    1952          96 :         const uint8_t  lay1_0_idx = context_ptr->lay1_toggle == 0 ? LAY1_OFF + 1 : LAY1_OFF + 0;   //the oldest L1 picture in the DPB
    1953          96 :         const uint8_t  lay1_1_idx = context_ptr->lay1_toggle == 0 ? LAY1_OFF + 0 : LAY1_OFF + 1;   //the newest L1 picture in the DPB
    1954             : #if PRED_CHANGE_5L
    1955          96 :         const uint8_t  lay2_1_idx = LAY2_OFF;   //the oldest L2 picture in the DPB
    1956          96 :         const uint8_t  lay3_idx = LAY3_OFF;    //the newest L3 picture in the DPB
    1957          96 :         const uint8_t  lay4_idx = LAY4_OFF;    //the newest L3 picture in the DPB
    1958             : #else
    1959             :         const uint8_t  lay2_0_idx = picture_index < 8 ? LAY2_OFF + 1 : LAY2_OFF + 0;   //the oldest L2 picture in the DPB
    1960             :         const uint8_t  lay2_1_idx = picture_index < 8 ? LAY2_OFF + 0 : LAY2_OFF + 1;   //the newest L2 picture in the DPB
    1961             : 
    1962             :         const uint8_t  lay3_idx = 7;    //the newest L3 picture in the DPB
    1963             : #endif
    1964          96 :         switch (picture_control_set_ptr->temporal_layer_index) {
    1965           6 :         case 0:
    1966             : 
    1967             :             //{16, 48, 0, 0},      // GOP Index 0 - Ref List 0
    1968             :            //{16, 32, 0, 0}       // GOP Index 0 - Ref List 1
    1969           6 :             av1_rps->ref_dpb_index[LAST] = base1_idx;
    1970           6 :             av1_rps->ref_dpb_index[LAST2] = base2_idx;
    1971           6 :             av1_rps->ref_dpb_index[LAST3] = av1_rps->ref_dpb_index[LAST];
    1972           6 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    1973             : 
    1974           6 :             av1_rps->ref_dpb_index[BWD] = base1_idx;
    1975           6 :             av1_rps->ref_dpb_index[ALT] = base0_idx;
    1976           6 :             av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    1977             : 
    1978           6 :             gop_i = 0;
    1979           6 :             av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    1980           6 :             av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    1981           6 :             av1_rps->ref_poc_array[LAST3] = av1_rps->ref_poc_array[LAST];
    1982           6 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    1983             : 
    1984           6 :             av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    1985           6 :             av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    1986           6 :             av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    1987             : 
    1988           6 :             av1_rps->refresh_frame_mask = 1 << context_ptr->lay0_toggle;
    1989             : 
    1990           6 :             break;
    1991             : 
    1992           6 :         case 1:
    1993             :             //{  8, 16, 24, 0},   // GOP Index 8 - Ref List 0
    1994             :             //{ -8, 0, 0, 0}      // GOP Index 8 - Ref List 1
    1995           6 :             av1_rps->ref_dpb_index[LAST] = base1_idx;
    1996           6 :             av1_rps->ref_dpb_index[LAST2] = lay1_0_idx;
    1997           6 :             av1_rps->ref_dpb_index[LAST3] = base0_idx;
    1998           6 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    1999             : 
    2000           6 :             av1_rps->ref_dpb_index[BWD] = base2_idx;
    2001           6 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    2002           6 :             av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2003             : 
    2004           6 :             gop_i = 8;
    2005           6 :             av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2006           6 :             av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2007           6 :             av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2008           6 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    2009             : 
    2010           6 :             av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2011           6 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    2012           6 :             av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2013           6 :             av1_rps->refresh_frame_mask = 1 << (LAY1_OFF + context_ptr->lay1_toggle);
    2014             : 
    2015           6 :             break;
    2016             : 
    2017          12 :         case 2:
    2018             : 
    2019          12 :             if (picture_index == 3) {
    2020             :                 //{  4,   8,  12,  20 },  // GOP Index 4 - Ref List 0
    2021             :                 //{ -4, -12,  0,  0 }     // GOP Index 4 - Ref List 1
    2022           6 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2023             : #if PRED_CHANGE_5L
    2024           6 :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2025             : #else
    2026             :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    2027             : #endif
    2028           6 :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    2029           6 :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    2030             : 
    2031           6 :                 av1_rps->ref_dpb_index[BWD] = lay1_1_idx;
    2032           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2033           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2034           6 :                 gop_i = 4;
    2035           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2036           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2037           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2038           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2039             : 
    2040           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2041           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2042           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2043             :             }
    2044           6 :             else if (picture_index == 11) {
    2045             :                 //{ 4, 8, 12, 0},       // GOP Index 12 - Ref List 0
    2046             :                 //{ -4,  0, 0,  0 }     // GOP Index 12 - Ref List 1
    2047           6 :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2048             : #if PRED_CHANGE_5L
    2049           6 :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2050             : #else
    2051             :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    2052             : #endif
    2053           6 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2054           6 :                 av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    2055             : 
    2056           6 :                 av1_rps->ref_dpb_index[BWD] = base2_idx;
    2057           6 :                 av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    2058           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2059           6 :                 gop_i = 12;
    2060           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2061           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2062           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2063           6 :                 av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    2064             : 
    2065           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2066           6 :                 av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    2067           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2068             :             }
    2069             : #if PRED_CHANGE_5L
    2070          12 :             av1_rps->refresh_frame_mask = 1 << (LAY2_OFF);
    2071             : #else
    2072             :             av1_rps->refresh_frame_mask = 1 << (LAY2_OFF + context_ptr->lay2_toggle);
    2073             : #endif
    2074             :             //toggle 3->4
    2075          12 :             context_ptr->lay2_toggle = 1 - context_ptr->lay2_toggle;
    2076             : 
    2077          12 :             break;
    2078             : 
    2079          24 :         case 3:
    2080             : 
    2081          24 :             if (picture_index == 1) {
    2082             : #if PRED_CHANGE_5L
    2083             :                 //{ 2, 4, 10, 18},        // GOP Index 2 - Ref List 0
    2084             :                 //{ -2, -6, -14,  0 }   // GOP Index 2 - Ref List 1
    2085           6 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2086           6 :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2087           6 :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    2088           6 :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    2089             : #else
    2090             :                 //{ 2, 4, 6, 10},        // GOP Index 2 - Ref List 0
    2091             :                 //{ -2, -6, -14,  0 }   // GOP Index 2 - Ref List 1
    2092             :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2093             :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2094             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2095             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2096             : #endif
    2097             : 
    2098           6 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    2099           6 :                 av1_rps->ref_dpb_index[ALT] = lay1_1_idx;
    2100           6 :                 av1_rps->ref_dpb_index[ALT2] = base2_idx;
    2101           6 :                 gop_i = 2;
    2102           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2103           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2104           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2105           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2106             : 
    2107           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2108           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2109           6 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    2110             :             }
    2111          18 :             else if (picture_index == 5) {
    2112             : #if PRED_CHANGE_5L
    2113             :                 //{ 2, 4, 6, 14},        // GOP Index 6 - Ref List 0
    2114             :                 //{ -2, -10,  0,  0 }   // GOP Index 6 - Ref List 1
    2115           6 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2116           6 :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2117           6 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2118           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2119             : #else
    2120             :                 //{ 2, 4, 6, 10},        // GOP Index 6 - Ref List 0
    2121             :                 //{ -2, -10,  0,  0 }   // GOP Index 6 - Ref List 1
    2122             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2123             :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2124             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2125             :                 av1_rps->ref_dpb_index[GOLD] = lay2_0_idx;
    2126             : #endif
    2127             : 
    2128           6 :                 av1_rps->ref_dpb_index[BWD] = lay1_1_idx;
    2129           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2130           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2131           6 :                 gop_i = 6;
    2132           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2133           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2134           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2135           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2136             : 
    2137           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2138           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2139           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2140             :             }
    2141          12 :             else if (picture_index == 9) {
    2142             : #if PRED_CHANGE_5L
    2143             :                 //{ 2, 4, 10, 18},       // GOP Index 10 - Ref List 0
    2144             :                 //{ -2, -6,  0,  0 }    // GOP Index 10 - Ref List 1
    2145           6 :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2146           6 :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2147           6 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2148           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2149             : #else
    2150             :                 //{ 2, 4, 6, 10},       // GOP Index 10 - Ref List 0
    2151             :                 //{ -2, -6,  0,  0 }    // GOP Index 10 - Ref List 1
    2152             :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2153             :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2154             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2155             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2156             : #endif
    2157             : 
    2158           6 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    2159           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2160           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2161           6 :                 gop_i = 10;
    2162           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2163           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2164           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2165           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2166             : 
    2167           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2168           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2169           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2170             :             }
    2171           6 :             else if (picture_index == 13) {
    2172             :                 //{ 2, 4, 6, 14},    // GOP Index 14 - Ref List 0
    2173             :                 //{ -2, 0,  0, 0 }   // GOP Index 14 - Ref List 1
    2174           6 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2175           6 :                 av1_rps->ref_dpb_index[LAST2] = lay3_idx;
    2176           6 :                 av1_rps->ref_dpb_index[LAST3] = lay1_1_idx;
    2177           6 :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2178             : 
    2179           6 :                 av1_rps->ref_dpb_index[BWD] = base2_idx;
    2180           6 :                 av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    2181           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2182           6 :                 gop_i = 14;
    2183           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2184           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2185           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2186           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2187             : 
    2188           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2189           6 :                 av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    2190           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2191             :             }
    2192             :             else
    2193           0 :                 printf("Error in GOp indexing\n");
    2194             : #if PRED_CHANGE_5L
    2195          24 :             av1_rps->refresh_frame_mask = 1 << (lay3_idx);
    2196             : #else
    2197             :             av1_rps->refresh_frame_mask = 1 << 7;
    2198             : #endif
    2199          24 :             break;
    2200             : 
    2201          48 :         case 4:
    2202             :             // update RPS for the overlay frame.
    2203          48 :             if (picture_control_set_ptr->is_overlay) {
    2204             :                 //{ 0, 0, 0, 0}         // GOP Index 1 - Ref List 0
    2205             :                 //{ 0, 0, 0, 0 }       // GOP Index 1 - Ref List 1
    2206           0 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2207           0 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2208           0 :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2209           0 :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2210             : 
    2211           0 :                 av1_rps->ref_dpb_index[BWD] = base1_idx;
    2212           0 :                 av1_rps->ref_dpb_index[ALT] = base1_idx;
    2213           0 :                 av1_rps->ref_dpb_index[ALT2] = base1_idx;
    2214           0 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2215           0 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2216           0 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2217           0 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2218             : 
    2219           0 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2220           0 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2221           0 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, 0);
    2222             :             }
    2223             :             else
    2224          48 :             if (picture_index == 0) {
    2225             : #if PRED_CHANGE_MOD
    2226             :                 //{ 1, 9, 8, 17},  // GOP Index 1 - Ref List 0
    2227             :                 //{ -1, -3, -7, 0 }   // GOP Index 1 - Ref List 1
    2228           6 :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2229           6 :                 av1_rps->ref_dpb_index[LAST2] = lay1_0_idx;
    2230           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2231           6 :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    2232             : #elif PRED_CHANGE_5L
    2233             :                 //{ 1, 8, 9, 17},  // GOP Index 1 - Ref List 0
    2234             :                 //{ -1, -3, -7, 0 }   // GOP Index 1 - Ref List 1
    2235             :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2236             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2237             :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    2238             :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    2239             : #else
    2240             :                 //{ 1, 5, 9, 17},  // GOP Index 1 - Ref List 0
    2241             :                 //{ -1, -3, -7, 0 }   // GOP Index 1 - Ref List 1
    2242             :                 av1_rps->ref_dpb_index[LAST] = base1_idx;
    2243             :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    2244             :                 av1_rps->ref_dpb_index[LAST3] = lay1_0_idx;
    2245             :                 av1_rps->ref_dpb_index[GOLD] = base0_idx;
    2246             : #endif
    2247             : 
    2248           6 :                 av1_rps->ref_dpb_index[BWD] = lay3_idx;
    2249           6 :                 av1_rps->ref_dpb_index[ALT] = lay2_1_idx;
    2250           6 :                 av1_rps->ref_dpb_index[ALT2] = lay1_1_idx;
    2251           6 :                 gop_i = 1;
    2252           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2253           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2254           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2255           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2256             : 
    2257           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2258           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2259           6 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    2260             :             }
    2261          42 :             else if (picture_index == 2) {
    2262             : #if PRED_CHANGE_MOD
    2263             :                 //{ 1, 3, 2, 11},  // GOP Index 3 - Ref List 0
    2264             :                //{ -1, -5, -13, 0 }   // GOP Index 3 - Ref List 1
    2265           6 :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2266           6 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2267           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2268           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2269             : #elif PRED_CHANGE_5L
    2270             :                 //{ 1, 2, 3, 11},  // GOP Index 3 - Ref List 0
    2271             :                //{ -1, -5, -13, 0 }   // GOP Index 3 - Ref List 1
    2272             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2273             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2274             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2275             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2276             : #else
    2277             :                 //{ 1, 3, 7, 11},  // GOP Index 3 - Ref List 0
    2278             :                //{ -1, -5, -13, 0 }   // GOP Index 3 - Ref List 1
    2279             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2280             :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2281             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2282             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2283             : #endif
    2284             : 
    2285           6 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    2286           6 :                 av1_rps->ref_dpb_index[ALT] = lay1_1_idx;
    2287           6 :                 av1_rps->ref_dpb_index[ALT2] = base2_idx;
    2288           6 :                 gop_i = 3;
    2289           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2290           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2291           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2292           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2293             : 
    2294           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2295           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2296           6 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    2297             :             }
    2298          36 :             else if (picture_index == 4) {
    2299             : #if PRED_CHANGE_MOD
    2300             :                 //{ 1, 5, 4, 13},  // GOP Index 5 - Ref List 0
    2301             :                //{ -1, -3, -11, 0 }   // GOP Index 5 - Ref List 1
    2302           6 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2303           6 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2304           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2305           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2306             : #elif PRED_CHANGE_5L
    2307             :                 //{ 1, 4, 5, 13},  // GOP Index 5 - Ref List 0
    2308             :                //{ -1, -3, -11, 0 }   // GOP Index 5 - Ref List 1
    2309             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2310             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2311             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2312             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2313             : #else
    2314             :                 //{ 1, 5, 9, 13},  // GOP Index 5 - Ref List 0
    2315             :                //{ -1, -3, -11, 0 }   // GOP Index 5 - Ref List 1
    2316             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2317             :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2318             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2319             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2320             : #endif
    2321           6 :                 av1_rps->ref_dpb_index[BWD] = lay3_idx;
    2322           6 :                 av1_rps->ref_dpb_index[ALT] = lay1_1_idx;
    2323           6 :                 av1_rps->ref_dpb_index[ALT2] = base2_idx;
    2324           6 :                 gop_i = 5;
    2325           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2326           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2327           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2328           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2329             : 
    2330           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2331           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2332           6 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    2333             :             }
    2334          30 :             else if (picture_index == 6) {
    2335             : #if PRED_CHANGE_MOD
    2336             :                 //{ 1, 3, 6, 7},  // GOP Index 7 - Ref List 0
    2337             :                //{ -1, -9, 0, 0 }   // GOP Index 7 - Ref List 1
    2338           6 :                av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2339           6 :                av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2340           6 :                av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2341           6 :                av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2342             : #elif PRED_CHANGE_5L
    2343             :                 //{ 1, 3, 6, 7},  // GOP Index 7 - Ref List 0
    2344             :                //{ -1, -9, 0, 0 }   // GOP Index 7 - Ref List 1
    2345             :                av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2346             :                av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2347             :                av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2348             :                av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2349             : #else
    2350             :                 //{ 1, 3, 7, 11},  // GOP Index 7 - Ref List 0
    2351             :                //{ -1, -9, 0, 0 }   // GOP Index 7 - Ref List 1
    2352             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2353             :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2354             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2355             :                 av1_rps->ref_dpb_index[GOLD] = lay2_0_idx;
    2356             : #endif
    2357           6 :                 av1_rps->ref_dpb_index[BWD] = lay1_1_idx;
    2358           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2359           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2360           6 :                 gop_i = 7;
    2361           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2362           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2363           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2364           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2365             : 
    2366           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2367           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2368           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2369             :             }
    2370          24 :             else if (picture_index == 8) {
    2371             : #if PRED_CHANGE_MOD
    2372             :                 //{ 1, 9, 8, 17},  // GOP Index 9 - Ref List 0
    2373             :                 //{ -1, -3, -7, 0 }   // GOP Index 9 - Ref List 1
    2374           6 :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2375           6 :                 av1_rps->ref_dpb_index[LAST2] = base1_idx;
    2376           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2377           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2378             : #elif PRED_CHANGE_5L
    2379             :                 //{ 1, 8, 9, 17},  // GOP Index 9 - Ref List 0
    2380             :                 //{ -1, -3, -7, 0 }   // GOP Index 9 - Ref List 1
    2381             :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2382             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2383             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2384             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2385             : #else
    2386             :                 //{ 1, 5, 9, 17},  // GOP Index 9 - Ref List 0
    2387             :                 //{ -1, -3, -7, 0 }   // GOP Index 9 - Ref List 1
    2388             :                 av1_rps->ref_dpb_index[LAST] = lay1_1_idx;
    2389             :                 av1_rps->ref_dpb_index[LAST2] = lay2_0_idx;
    2390             :                 av1_rps->ref_dpb_index[LAST3] = base1_idx;
    2391             :                 av1_rps->ref_dpb_index[GOLD] = lay1_0_idx;
    2392             : #endif
    2393           6 :                 av1_rps->ref_dpb_index[BWD] = lay3_idx;
    2394           6 :                 av1_rps->ref_dpb_index[ALT] = lay2_1_idx;
    2395           6 :                 av1_rps->ref_dpb_index[ALT2] = base2_idx;
    2396           6 :                 gop_i = 9;
    2397           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2398           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2399           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2400           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2401             : 
    2402           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2403           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2404           6 :                 av1_rps->ref_poc_array[ALT2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[2]);
    2405             :             }
    2406          18 :             else if (picture_index == 10) {
    2407             : #if PRED_CHANGE_MOD
    2408             :                 //{ 1, 3, 2, 11},  // GOP Index 11 - Ref List 0
    2409             :                 //{ -1, -5, 0, 0 }   // GOP Index 11 - Ref List 1
    2410           6 :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2411           6 :                 av1_rps->ref_dpb_index[LAST2] = lay1_1_idx;
    2412           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2413           6 :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2414             : #elif PRED_CHANGE_5L
    2415             :                 //{ 1, 2, 3, 11},  // GOP Index 11 - Ref List 0
    2416             :                 //{ -1, -5, 0, 0 }   // GOP Index 11 - Ref List 1
    2417             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2418             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2419             :                 av1_rps->ref_dpb_index[LAST3] = lay1_1_idx;
    2420             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2421             : #else
    2422             :                 //{ 1, 3, 7, 11},  // GOP Index 11 - Ref List 0
    2423             :                 //{ -1, -5, 0, 0 }   // GOP Index 11 - Ref List 1
    2424             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2425             :                 av1_rps->ref_dpb_index[LAST2] = lay1_1_idx;
    2426             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2427             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2428             : #endif
    2429             : 
    2430           6 :                 av1_rps->ref_dpb_index[BWD] = lay2_1_idx;
    2431           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2432           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2433           6 :                 gop_i = 11;
    2434           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2435           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2436           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2437           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2438             : 
    2439           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2440           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2441           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2442             :             }
    2443          12 :             else if (picture_index == 12) {
    2444             : #if PRED_CHANGE_MOD
    2445             :                 //{ 1, 5, 4, 13},  // GOP Index 13 - Ref List 0
    2446             :                 //{ -1, -3, 0, 0 }   // GOP Index 13 - Ref List 1
    2447           6 :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2448           6 :                 av1_rps->ref_dpb_index[LAST2] = lay1_1_idx;
    2449           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2450           6 :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2451             : #elif PRED_CHANGE_5L
    2452             :                 //{ 1, 4, 5, 13},  // GOP Index 13 - Ref List 0
    2453             :                 //{ -1, -3, 0, 0 }   // GOP Index 13 - Ref List 1
    2454             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2455             :                 av1_rps->ref_dpb_index[LAST2] = lay4_idx;
    2456             :                 av1_rps->ref_dpb_index[LAST3] = lay1_1_idx;
    2457             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2458             : #else
    2459             :                 //{ 1, 5, 9, 13},  // GOP Index 13 - Ref List 0
    2460             :                 //{ -1, -3, 0, 0 }   // GOP Index 13 - Ref List 1
    2461             :                 av1_rps->ref_dpb_index[LAST] = lay2_1_idx;
    2462             :                 av1_rps->ref_dpb_index[LAST2] = lay1_1_idx;
    2463             :                 av1_rps->ref_dpb_index[LAST3] = lay2_0_idx;
    2464             :                 av1_rps->ref_dpb_index[GOLD] = base1_idx;
    2465             : #endif
    2466             : 
    2467           6 :                 av1_rps->ref_dpb_index[BWD] = lay3_idx;
    2468           6 :                 av1_rps->ref_dpb_index[ALT] = base2_idx;
    2469           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2470           6 :                 gop_i = 13;
    2471           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2472           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2473           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2474           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2475             : 
    2476           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2477           6 :                 av1_rps->ref_poc_array[ALT] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[1]);
    2478           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2479             :             }
    2480           6 :             else if (picture_index == 14) {
    2481             : #if PRED_CHANGE_MOD
    2482             :                 //{ 1, 3, 6, 7},  // GOP Index 15 - Ref List 0
    2483             :                 //{ -1, 0, 0, 0 }   // GOP Index 15 - Ref List 1
    2484           6 :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2485           6 :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2486           6 :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2487           6 :                 av1_rps->ref_dpb_index[GOLD] = lay1_1_idx;
    2488             : #elif PRED_CHANGE_5L
    2489             :                 //{ 1, 3, 6, 7},  // GOP Index 15 - Ref List 0
    2490             :                 //{ -1, 0, 0, 0 }   // GOP Index 15 - Ref List 1
    2491             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2492             :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2493             :                 av1_rps->ref_dpb_index[LAST3] = lay4_idx;
    2494             :                 av1_rps->ref_dpb_index[GOLD] = lay1_1_idx;
    2495             : #else
    2496             :                 //{ 1, 3, 7, 11},  // GOP Index 15 - Ref List 0
    2497             :                 //{ -1, 0, 0, 0 }   // GOP Index 15 - Ref List 1
    2498             :                 av1_rps->ref_dpb_index[LAST] = lay3_idx;
    2499             :                 av1_rps->ref_dpb_index[LAST2] = lay2_1_idx;
    2500             :                 av1_rps->ref_dpb_index[LAST3] = lay1_1_idx;
    2501             :                 av1_rps->ref_dpb_index[GOLD] = lay2_0_idx;
    2502             : #endif
    2503             : 
    2504           6 :                 av1_rps->ref_dpb_index[BWD] = base2_idx;
    2505           6 :                 av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    2506           6 :                 av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2507           6 :                 gop_i = 15;
    2508           6 :                 av1_rps->ref_poc_array[LAST] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[0]);
    2509           6 :                 av1_rps->ref_poc_array[LAST2] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[1]);
    2510           6 :                 av1_rps->ref_poc_array[LAST3] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[2]);
    2511           6 :                 av1_rps->ref_poc_array[GOLD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list0[3]);
    2512             : 
    2513           6 :                 av1_rps->ref_poc_array[BWD] = get_ref_poc(context_ptr, picture_control_set_ptr->picture_number, five_level_hierarchical_pred_struct[gop_i].ref_list1[0]);
    2514           6 :                 av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    2515           6 :                 av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2516             :             }
    2517             :             else
    2518           0 :                 printf("Error in GOp indexing\n");
    2519             : #if PRED_CHANGE_5L
    2520          48 :             if (picture_index == 0 || picture_index == 8)
    2521          12 :                 av1_rps->refresh_frame_mask = 1 << (lay4_idx);
    2522             :             else
    2523          36 :                 av1_rps->refresh_frame_mask = 0;
    2524             : #else
    2525             :             av1_rps->refresh_frame_mask = 0;
    2526             : #endif
    2527          48 :             break;
    2528             : 
    2529           0 :         default:
    2530           0 :             printf("Error: unexpected picture mini Gop number\n");
    2531           0 :             break;
    2532             :         }
    2533             : 
    2534             : 
    2535             :         // Jing: Check for reference number, remove invalid dependancy
    2536          96 :         if (pred_position_ptr->ref_list0.reference_list_count < 4) {
    2537          73 :             av1_rps->ref_dpb_index[GOLD] = av1_rps->ref_dpb_index[LAST];
    2538          73 :             av1_rps->ref_poc_array[GOLD] = av1_rps->ref_poc_array[LAST];
    2539             :         }
    2540          96 :         if (pred_position_ptr->ref_list0.reference_list_count < 3) {
    2541          59 :             av1_rps->ref_dpb_index[LAST3] = av1_rps->ref_dpb_index[LAST];
    2542          59 :             av1_rps->ref_poc_array[LAST3] = av1_rps->ref_poc_array[LAST];
    2543             :         }
    2544          96 :         if (pred_position_ptr->ref_list0.reference_list_count < 2) {
    2545          59 :             av1_rps->ref_dpb_index[LAST2] = av1_rps->ref_dpb_index[LAST];
    2546          59 :             av1_rps->ref_poc_array[LAST2] = av1_rps->ref_poc_array[LAST];
    2547             :         }
    2548             : 
    2549             :         //Only for layer0 in five layer case
    2550          96 :         if (pred_position_ptr->ref_list1.reference_list_count < 2) {
    2551          61 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[BWD];
    2552          61 :             av1_rps->ref_dpb_index[ALT2] = av1_rps->ref_dpb_index[BWD];
    2553          61 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[BWD];
    2554          61 :             av1_rps->ref_poc_array[ALT2] = av1_rps->ref_poc_array[BWD];
    2555             :         }
    2556             : 
    2557             :         {
    2558          96 :             int tmp = av1_rps->ref_dpb_index[ALT];
    2559          96 :             av1_rps->ref_dpb_index[ALT] = av1_rps->ref_dpb_index[ALT2];
    2560          96 :             av1_rps->ref_dpb_index[ALT2] = tmp;
    2561             : 
    2562          96 :             uint64_t tmp1 = av1_rps->ref_poc_array[ALT];
    2563          96 :             av1_rps->ref_poc_array[ALT] = av1_rps->ref_poc_array[ALT2];
    2564          96 :             av1_rps->ref_poc_array[ALT2] = tmp1;
    2565             :         }
    2566             : 
    2567             :         // update RPS for the overlay frame.
    2568          96 :         if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_LOW_DELAY_P || picture_control_set_ptr->is_overlay)
    2569             :         {
    2570             :             //P frames.
    2571           0 :             av1_rps->ref_dpb_index[4] = av1_rps->ref_dpb_index[5] = av1_rps->ref_dpb_index[6] = av1_rps->ref_dpb_index[0];
    2572           0 :             av1_rps->ref_poc_array[4] = av1_rps->ref_poc_array[5] = av1_rps->ref_poc_array[6] = av1_rps->ref_poc_array[0];
    2573           0 :             frm_hdr->show_frame = EB_TRUE;
    2574           0 :             picture_control_set_ptr->has_show_existing = EB_FALSE;
    2575             :         }
    2576          96 :         else if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_RANDOM_ACCESS)
    2577             :         {
    2578             :             // av1_rps->ref_dpb_index[1] = av1_rps->ref_dpb_index[2] = av1_rps->ref_dpb_index[3] = av1_rps->ref_dpb_index[0];
    2579             :             //av1_rps->ref_dpb_index[5] = av1_rps->ref_dpb_index[6] = av1_rps->ref_dpb_index[4];
    2580             : 
    2581             :             //Decide on Show Mecanism
    2582          96 :             if (picture_control_set_ptr->slice_type == I_SLICE)
    2583             :             {
    2584             :                 //3 cases for I slice:  1:Key Frame treated above.  2: broken MiniGop due to sc or intra refresh  3: complete miniGop due to sc or intra refresh
    2585           2 :                 if (context_ptr->mini_gop_length[0] < picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    2586             :                 {
    2587             :                     //Scene Change that breaks the mini gop and switch to LDP (if I scene change happens to be aligned with a complete miniGop, then we do not break the pred structure)
    2588           0 :                     frm_hdr->show_frame = EB_TRUE;
    2589           0 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    2590             :                 }
    2591             :                 else
    2592             :                 {
    2593           2 :                     frm_hdr->show_frame = EB_FALSE;
    2594           2 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    2595             :                 }
    2596             :             }
    2597             :             else//B pic
    2598             :             {
    2599          94 :                 if (context_ptr->mini_gop_length[0] != picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    2600           0 :                     printf("Error in GOp indexing3\n");
    2601             : #if PRED_CHANGE_5L
    2602          94 :                 if (picture_control_set_ptr->is_used_as_reference_flag && picture_index != 0 && picture_index != 8)
    2603             : #else
    2604             :                 if (picture_control_set_ptr->is_used_as_reference_flag)
    2605             : #endif
    2606             :                 {
    2607          46 :                     frm_hdr->show_frame = EB_FALSE;
    2608          46 :                     picture_control_set_ptr->has_show_existing = EB_FALSE;
    2609             :                 }
    2610             :                 else
    2611             :                 {
    2612          48 :                     frm_hdr->show_frame = EB_TRUE;
    2613          48 :                     picture_control_set_ptr->has_show_existing = EB_TRUE;
    2614             : 
    2615          48 :                     if (picture_index == 0)
    2616           6 :                         frm_hdr->show_existing_frame = lay3_idx;
    2617          42 :                     else if (picture_index == 2)
    2618           6 :                         frm_hdr->show_existing_frame = lay2_1_idx;
    2619          36 :                     else if (picture_index == 4)
    2620           6 :                         frm_hdr->show_existing_frame = lay3_idx;
    2621          30 :                     else if (picture_index == 6)
    2622           6 :                         frm_hdr->show_existing_frame = lay1_1_idx;
    2623          24 :                     else if (picture_index == 8)
    2624           6 :                         frm_hdr->show_existing_frame = lay3_idx;
    2625          18 :                     else if (picture_index == 10)
    2626           6 :                         frm_hdr->show_existing_frame = lay2_1_idx;
    2627          12 :                     else if (picture_index == 12)
    2628           6 :                         frm_hdr->show_existing_frame = lay3_idx;
    2629           6 :                     else if (picture_index == 14)
    2630           6 :                         frm_hdr->show_existing_frame = base2_idx;
    2631             :                     else
    2632           0 :                         printf("Error in GOp indexing2\n");
    2633             :                 }
    2634             :             }
    2635             :         }
    2636             :         else {
    2637           0 :             printf("Error: Not supported GOP structure!");
    2638           0 :             exit(0);
    2639             :         }
    2640             : 
    2641             :         //last pic in MiniGop: Base layer toggling
    2642             :         //mini GOP toggling since last Key Frame.
    2643             :         //a regular I keeps the toggling process and does not reset the toggle.  K-0-1-0-1-0-K-0-1-0-1-K-0-1.....
    2644             :         //whoever needs a miniGOP Level toggling, this is the time
    2645          96 :         if (picture_index == context_ptr->mini_gop_end_index[0] && !picture_control_set_ptr->is_overlay) {
    2646             :             //Layer0 toggle 0->1->2
    2647           6 :             context_ptr->lay0_toggle = circ_inc(3, 1, context_ptr->lay0_toggle);
    2648             :             //Layer1 toggle 3->4
    2649           6 :             context_ptr->lay1_toggle = 1 - context_ptr->lay1_toggle;
    2650             :         }
    2651             :     }
    2652             :     else
    2653             :     {
    2654             :     //Reset miniGop Toggling. The first miniGop after a KEY frame has toggle=0
    2655             :     if (frm_hdr->frame_type == KEY_FRAME)
    2656             :     {
    2657             :         context_ptr->mini_gop_toggle = 0;
    2658             :         frm_hdr->show_frame = EB_TRUE;
    2659             :         picture_control_set_ptr->has_show_existing = EB_FALSE;
    2660             :         return;
    2661             :     }
    2662             : 
    2663             :     //         0     2    4      6    8     10     12      14
    2664             :     //            1          5           9            13
    2665             :     //                 3                        11
    2666             :     //                              7
    2667             : 
    2668             :     //DPB: Loc7|Loc6|Loc5|Loc4|Loc3|Loc2|Loc1|Loc0
    2669             :     //Layer 0 : toggling bwteween DPB Location 0, and  locations 3-4-5-6-7
    2670             :     //Layer 1 : DPB Location 1
    2671             :     //Layer 2 : DPB Location 2
    2672             :     //Layer 3 : DPB Location 3
    2673             : 
    2674             :     //         1     3    5      7    9     11     13      15
    2675             :     //            2          6           10            14
    2676             :     //                 4                        12
    2677             :     //                              8
    2678             :     //base0:0                                               base1:16
    2679             : 
    2680             :     const uint8_t  base0_idx = context_ptr->mini_gop_toggle ? 0 : 3; //Base layer for prediction from past
    2681             :     const uint8_t  base1_idx = context_ptr->mini_gop_toggle ? 3 : 0; //Base layer for prediction from future
    2682             :     const uint8_t  layer1_idx = 1;
    2683             :     const uint8_t  layer2_idx = 2;
    2684             :     const uint8_t  layer3_idx1 = 4;
    2685             :     const uint8_t  layer3_idx2 = 5;
    2686             : 
    2687             :     switch (picture_control_set_ptr->temporal_layer_index) {
    2688             :     case 0:
    2689             : 
    2690             :         av1_rps->ref_dpb_index[0] = base0_idx;
    2691             :         av1_rps->ref_dpb_index[6] = base0_idx;
    2692             :         av1_rps->refresh_frame_mask = context_ptr->mini_gop_toggle ? 200 : 1;
    2693             :         break;
    2694             :     case 1:
    2695             :         av1_rps->ref_dpb_index[0] = base0_idx;
    2696             :         av1_rps->ref_dpb_index[6] = base1_idx;
    2697             :         av1_rps->refresh_frame_mask = 2;
    2698             :         break;
    2699             :     case 2:
    2700             : 
    2701             :         if (picture_index == 3) {
    2702             :             av1_rps->ref_dpb_index[0] = base0_idx;
    2703             :             av1_rps->ref_dpb_index[6] = layer1_idx;
    2704             :     }
    2705             :         else if (picture_index == 11) {
    2706             :             av1_rps->ref_dpb_index[0] = layer1_idx;
    2707             :             av1_rps->ref_dpb_index[6] = base1_idx;
    2708             :         }
    2709             :         av1_rps->refresh_frame_mask = 4;
    2710             :         break;
    2711             :     case 3:
    2712             : 
    2713             :         if (picture_index == 1) {
    2714             :             av1_rps->ref_dpb_index[0] = base0_idx;
    2715             :             av1_rps->ref_dpb_index[6] = layer2_idx;
    2716             :             av1_rps->refresh_frame_mask = 16;
    2717             :         }
    2718             :         else if (picture_index == 5) {
    2719             :             av1_rps->ref_dpb_index[0] = layer2_idx;
    2720             :             av1_rps->ref_dpb_index[6] = layer1_idx;
    2721             :             av1_rps->refresh_frame_mask = 32;
    2722             :         }
    2723             :         else if (picture_index == 9) {
    2724             :             av1_rps->ref_dpb_index[0] = layer1_idx;
    2725             :             av1_rps->ref_dpb_index[6] = layer2_idx;
    2726             :             av1_rps->refresh_frame_mask = 16;
    2727             :         }
    2728             :         else if (picture_index == 13) {
    2729             :             av1_rps->ref_dpb_index[0] = layer2_idx;
    2730             :             av1_rps->ref_dpb_index[6] = base1_idx;
    2731             :             av1_rps->refresh_frame_mask = 32;
    2732             :         }
    2733             :         else
    2734             :             printf("Error in GOp indexing\n");
    2735             :         break;
    2736             :     case 4:
    2737             :         if (picture_index == 0) {
    2738             :             av1_rps->ref_dpb_index[0] = base0_idx;
    2739             :             av1_rps->ref_dpb_index[6] = layer3_idx1;
    2740             :         }
    2741             :         else if (picture_index == 2) {
    2742             :             av1_rps->ref_dpb_index[0] = layer3_idx1;
    2743             :             av1_rps->ref_dpb_index[6] = layer2_idx;
    2744             :         }
    2745             :         else if (picture_index == 4) {
    2746             :             av1_rps->ref_dpb_index[0] = layer2_idx;
    2747             :             av1_rps->ref_dpb_index[6] = layer3_idx2;
    2748             :         }
    2749             :         else if (picture_index == 6) {
    2750             :             av1_rps->ref_dpb_index[0] = layer3_idx2;
    2751             :             av1_rps->ref_dpb_index[6] = layer1_idx;
    2752             :         }
    2753             :         else if (picture_index == 8) {
    2754             :             av1_rps->ref_dpb_index[0] = layer1_idx;
    2755             :             av1_rps->ref_dpb_index[6] = layer3_idx1;
    2756             :         }
    2757             :         else if (picture_index == 10) {
    2758             :             av1_rps->ref_dpb_index[0] = layer3_idx1;
    2759             :             av1_rps->ref_dpb_index[6] = layer2_idx;
    2760             :         }
    2761             :         else if (picture_index == 12) {
    2762             :             av1_rps->ref_dpb_index[0] = layer2_idx;
    2763             :             av1_rps->ref_dpb_index[6] = layer3_idx2;
    2764             :         }
    2765             :         else if (picture_index == 14) {
    2766             :             av1_rps->ref_dpb_index[0] = layer3_idx2;
    2767             :             av1_rps->ref_dpb_index[6] = base1_idx;
    2768             :         }
    2769             :         else
    2770             :             printf("Error in GOp indexing\n");
    2771             :         av1_rps->refresh_frame_mask = 0;
    2772             :         break;
    2773             :     default:
    2774             :         printf("Error: unexpected picture mini Gop number\n");
    2775             :         break;
    2776             :     }
    2777             : 
    2778             :     if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_LOW_DELAY_P)
    2779             :     {
    2780             :         //P frames.
    2781             :         av1_rps->ref_dpb_index[4] = av1_rps->ref_dpb_index[5] = av1_rps->ref_dpb_index[6] = av1_rps->ref_dpb_index[0];
    2782             :         av1_rps->ref_poc_array[4] = av1_rps->ref_poc_array[5] = av1_rps->ref_poc_array[6] = av1_rps->ref_poc_array[0];
    2783             :         frm_hdr->show_frame = EB_TRUE;
    2784             :         picture_control_set_ptr->has_show_existing = EB_FALSE;
    2785             :     }
    2786             :     else if (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_RANDOM_ACCESS)
    2787             :     {
    2788             :         av1_rps->ref_dpb_index[1] = av1_rps->ref_dpb_index[2] = av1_rps->ref_dpb_index[3] = av1_rps->ref_dpb_index[0];
    2789             :         av1_rps->ref_dpb_index[4] = av1_rps->ref_dpb_index[5] = av1_rps->ref_dpb_index[6];
    2790             : 
    2791             :         //Decide on Show Mecanism
    2792             :         if (picture_control_set_ptr->slice_type == I_SLICE)
    2793             :         {
    2794             :             //3 cases for I slice:  1:Key Frame treated above.  2: broken MiniGop due to sc or intra refresh  3: complete miniGop due to sc or intra refresh
    2795             :             if (context_ptr->mini_gop_length[0] < picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    2796             :             {
    2797             :                 //Scene Change that breaks the mini gop and switch to LDP (if I scene change happens to be aligned with a complete miniGop, then we do not break the pred structure)
    2798             :                 frm_hdr->show_frame = EB_TRUE;
    2799             :                 picture_control_set_ptr->has_show_existing = EB_FALSE;
    2800             :             }
    2801             :             else
    2802             :             {
    2803             :                 frm_hdr->show_frame = EB_FALSE;
    2804             :                 picture_control_set_ptr->has_show_existing = EB_FALSE;
    2805             :             }
    2806             :         }
    2807             :         else//B pic
    2808             :         {
    2809             :             if (context_ptr->mini_gop_length[0] != picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    2810             :                 printf("Error in GOp indexing3\n");
    2811             : 
    2812             :             if (picture_control_set_ptr->is_used_as_reference_flag)
    2813             :             {
    2814             :                 frm_hdr->show_frame = EB_FALSE;
    2815             :                 picture_control_set_ptr->has_show_existing = EB_FALSE;
    2816             :             }
    2817             :             else
    2818             :             {
    2819             :                 frm_hdr->show_frame = EB_TRUE;
    2820             :                 picture_control_set_ptr->has_show_existing = EB_TRUE;
    2821             : 
    2822             :                 if (picture_index == 0)
    2823             :                     frm_hdr->show_existing_frame = layer3_idx1;
    2824             :                 else if (picture_index == 2)
    2825             :                     frm_hdr->show_existing_frame = layer2_idx;
    2826             :                 else if (picture_index == 4)
    2827             :                     frm_hdr->show_existing_frame = layer3_idx2;
    2828             :                 else if (picture_index == 6)
    2829             :                     frm_hdr->show_existing_frame = layer1_idx;
    2830             :                 else if (picture_index == 8)
    2831             :                     frm_hdr->show_existing_frame = layer3_idx1;
    2832             :                 else if (picture_index == 10)
    2833             :                     frm_hdr->show_existing_frame = layer2_idx;
    2834             :                 else if (picture_index == 12)
    2835             :                     frm_hdr->show_existing_frame = layer3_idx2;
    2836             :                 else if (picture_index == 14)
    2837             :                     frm_hdr->show_existing_frame = base1_idx;
    2838             :                 else
    2839             :                     printf("Error in GOp indexing2\n");
    2840             :             }
    2841             :         }
    2842             :     }
    2843             :     else {
    2844             :         printf("Error: Not supported GOP structure!");
    2845             :         exit(0);
    2846             :     }
    2847             : 
    2848             :     //last pic in MiniGop: mGop Toggling
    2849             :     //mini GOP toggling since last Key Frame.
    2850             :     //a regular I keeps the toggling process and does not reset the toggle.  K-0-1-0-1-0-K-0-1-0-1-K-0-1.....
    2851             :     if (picture_index == context_ptr->mini_gop_end_index[0])
    2852             :         context_ptr->mini_gop_toggle = 1 - context_ptr->mini_gop_toggle;
    2853             : 
    2854             :         }
    2855             :     }
    2856             : 
    2857             :     else
    2858             :     {
    2859           0 :         printf("Error: Not supported GOP structure!");
    2860           0 :         exit(0);
    2861             :     }
    2862             : }
    2863             : 
    2864             : /***************************************************************************************************
    2865             : // Perform Required Picture Analysis Processing for the Overlay frame
    2866             : ***************************************************************************************************/
    2867           0 : void perform_simple_picture_analysis_for_overlay(PictureParentControlSet     *picture_control_set_ptr) {
    2868             :     EbPictureBufferDesc           *input_padded_picture_ptr;
    2869             :     EbPictureBufferDesc           *input_picture_ptr;
    2870             :     EbPaReferenceObject           *paReferenceObject;
    2871             :     uint32_t                        picture_width_in_sb;
    2872             :     uint32_t                        pictureHeighInLcu;
    2873             :     uint32_t                        sb_total_count;
    2874             : 
    2875           0 :     SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    2876           0 :     input_picture_ptr               = picture_control_set_ptr->enhanced_picture_ptr;
    2877           0 :     paReferenceObject               = (EbPaReferenceObject*)picture_control_set_ptr->pa_reference_picture_wrapper_ptr->object_ptr;
    2878           0 :     input_padded_picture_ptr        = (EbPictureBufferDesc*)paReferenceObject->input_padded_picture_ptr;
    2879           0 :     picture_width_in_sb = (sequence_control_set_ptr->seq_header.max_frame_width + sequence_control_set_ptr->sb_sz - 1) / sequence_control_set_ptr->sb_sz;
    2880           0 :     pictureHeighInLcu   = (sequence_control_set_ptr->seq_header.max_frame_height + sequence_control_set_ptr->sb_sz - 1) / sequence_control_set_ptr->sb_sz;
    2881           0 :     sb_total_count      = picture_width_in_sb * pictureHeighInLcu;
    2882             : 
    2883             :     // Pad pictures to multiple min cu size
    2884           0 :     PadPictureToMultipleOfMinCuSizeDimensions(
    2885             :         sequence_control_set_ptr,
    2886             :         input_picture_ptr);
    2887             : 
    2888             :     // Pre processing operations performed on the input picture
    2889           0 :     PicturePreProcessingOperations(
    2890             :         picture_control_set_ptr,
    2891             :         sequence_control_set_ptr,
    2892             :         sb_total_count);
    2893           0 :     if (input_picture_ptr->color_format >= EB_YUV422) {
    2894             :         // Jing: Do the conversion of 422/444=>420 here since it's multi-threaded kernel
    2895             :         //       Reuse the Y, only add cb/cr in the newly created buffer desc
    2896             :         //       NOTE: since denoise may change the src, so this part is after PicturePreProcessingOperations()
    2897           0 :         picture_control_set_ptr->chroma_downsampled_picture_ptr->buffer_y = input_picture_ptr->buffer_y;
    2898           0 :         DownSampleChroma(input_picture_ptr, picture_control_set_ptr->chroma_downsampled_picture_ptr);
    2899             :     }
    2900             :     else
    2901           0 :         picture_control_set_ptr->chroma_downsampled_picture_ptr = input_picture_ptr;
    2902             :     // Pad input picture to complete border LCUs
    2903           0 :     PadPictureToMultipleOfLcuDimensions(
    2904             :         input_padded_picture_ptr);
    2905             :     // 1/4 & 1/16 input picture decimation
    2906           0 :     DownsampleDecimationInputPicture(
    2907             :         picture_control_set_ptr,
    2908             :         input_padded_picture_ptr,
    2909             :         (EbPictureBufferDesc*)paReferenceObject->quarter_decimated_picture_ptr,
    2910             :         (EbPictureBufferDesc*)paReferenceObject->sixteenth_decimated_picture_ptr);
    2911             : 
    2912             :     // 1/4 & 1/16 input picture downsampling through filtering
    2913           0 :     if (sequence_control_set_ptr->down_sampling_method_me_search == ME_FILTERED_DOWNSAMPLED) {
    2914           0 :         DownsampleFilteringInputPicture(
    2915             :             picture_control_set_ptr,
    2916             :             input_padded_picture_ptr,
    2917             :             (EbPictureBufferDesc*)paReferenceObject->quarter_filtered_picture_ptr,
    2918             :             (EbPictureBufferDesc*)paReferenceObject->sixteenth_filtered_picture_ptr);
    2919             :     }
    2920             :     // Gathering statistics of input picture, including Variance Calculation, Histogram Bins
    2921           0 :     GatheringPictureStatistics(
    2922             :         sequence_control_set_ptr,
    2923             :         picture_control_set_ptr,
    2924             :         picture_control_set_ptr->chroma_downsampled_picture_ptr, //420 input_picture_ptr
    2925             :         input_padded_picture_ptr,
    2926             :         paReferenceObject->sixteenth_decimated_picture_ptr, // Hsan: always use decimated until studying the trade offs
    2927             :         sb_total_count);
    2928             : 
    2929           0 :     picture_control_set_ptr->sc_content_detected = picture_control_set_ptr->alt_ref_ppcs_ptr->sc_content_detected;
    2930           0 : }
    2931             : /***************************************************************************************************
    2932             :  * Initialize the overlay frame
    2933             : ***************************************************************************************************/
    2934           0 : void initialize_overlay_frame(PictureParentControlSet     *picture_control_set_ptr) {
    2935           0 :     picture_control_set_ptr->fade_out_from_black = picture_control_set_ptr->alt_ref_ppcs_ptr->fade_out_from_black;
    2936           0 :     picture_control_set_ptr->fade_in_to_black = picture_control_set_ptr->alt_ref_ppcs_ptr->fade_in_to_black;
    2937           0 :     picture_control_set_ptr->scene_change_flag = EB_FALSE;
    2938           0 :     picture_control_set_ptr->cra_flag = EB_FALSE;
    2939           0 :     picture_control_set_ptr->idr_flag = EB_FALSE;
    2940           0 :     picture_control_set_ptr->target_bit_rate = picture_control_set_ptr->alt_ref_ppcs_ptr->target_bit_rate;
    2941           0 :     picture_control_set_ptr->last_idr_picture = picture_control_set_ptr->alt_ref_ppcs_ptr->last_idr_picture;
    2942           0 :     picture_control_set_ptr->use_rps_in_sps = EB_FALSE;
    2943           0 :     picture_control_set_ptr->open_gop_cra_flag = EB_FALSE;
    2944           0 :     picture_control_set_ptr->pred_structure = picture_control_set_ptr->alt_ref_ppcs_ptr->pred_structure;
    2945           0 :     picture_control_set_ptr->pred_struct_ptr = picture_control_set_ptr->alt_ref_ppcs_ptr->pred_struct_ptr;
    2946           0 :     picture_control_set_ptr->hierarchical_levels = picture_control_set_ptr->alt_ref_ppcs_ptr->hierarchical_levels;
    2947           0 :     picture_control_set_ptr->hierarchical_layers_diff = 0;
    2948           0 :     picture_control_set_ptr->init_pred_struct_position_flag = EB_FALSE;
    2949           0 :     picture_control_set_ptr->pre_assignment_buffer_count = picture_control_set_ptr->alt_ref_ppcs_ptr->pre_assignment_buffer_count;
    2950             : 
    2951           0 :     perform_simple_picture_analysis_for_overlay(picture_control_set_ptr);
    2952           0 :  }
    2953             : 
    2954             : /***************************************************************************************************
    2955             :  * Helper function. Compare two frames: center frame and target frame. Return the summation of  
    2956             :  * absolute difference between the two frames from a histogram of luma values
    2957             : ***************************************************************************************************/
    2958             : 
    2959          14 : static __inline uint32_t compute_luma_sad_between_center_and_target_frame(
    2960             :     int center_index,
    2961             :     int target_frame_index,
    2962             :     PictureParentControlSet *picture_control_set_ptr,
    2963             :     SequenceControlSet *sequence_control_set_ptr) {
    2964             : 
    2965          14 :     int32_t center_sum = 0, altref_sum = 0;
    2966          14 :     uint32_t ahd = 0;
    2967             : 
    2968        3598 :     for (int bin = 0; bin < HISTOGRAM_NUMBER_OF_BINS; ++bin) {
    2969        3584 :         center_sum = 0, altref_sum = 0;
    2970       17920 :         for (uint32_t regionInPictureWidthIndex = 0; regionInPictureWidthIndex < sequence_control_set_ptr->picture_analysis_number_of_regions_per_width; regionInPictureWidthIndex++) {
    2971       71680 :             for (uint32_t regionInPictureHeightIndex = 0; regionInPictureHeightIndex < sequence_control_set_ptr->picture_analysis_number_of_regions_per_height; regionInPictureHeightIndex++) {
    2972       57344 :                 center_sum += picture_control_set_ptr->temp_filt_pcs_list[center_index]->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][0][bin];
    2973       57344 :                 altref_sum += picture_control_set_ptr->temp_filt_pcs_list[target_frame_index]->picture_histogram[regionInPictureWidthIndex][regionInPictureHeightIndex][0][bin];
    2974             :             }
    2975             :         }
    2976        3584 :         ahd += ABS(center_sum - altref_sum);
    2977             :     }
    2978          14 :     return ahd;
    2979             : }
    2980             : 
    2981             : /***************************************************************************************************
    2982             :  * Picture Decision Kernel
    2983             :  *
    2984             :  * Notes on the Picture Decision:
    2985             :  *
    2986             :  * The Picture Decision process performs multi-picture level decisions, including setting of the prediction structure,
    2987             :  * setting the picture type and scene change detection.
    2988             :  *
    2989             :  * Inputs:
    2990             :  * Input Picture
    2991             :  *   -Input Picture Data
    2992             :  *
    2993             :  *  Outputs:
    2994             :  *   -Picture Control Set with fully available PA Reference List
    2995             :  *
    2996             :  *  For Low Delay Sequences, pictures are started into the encoder pipeline immediately.
    2997             :  *
    2998             :  *  For Random Access Sequences, pictures are held for up to a PredictionStructurePeriod
    2999             :  *    in order to determine if a Scene Change or Intra Frame is forthcoming. Either of
    3000             :  *    those events (and additionally a End of Sequence Flag) will change the expected
    3001             :  *    prediction structure.
    3002             :  *
    3003             :  *  Below is an example worksheet for how Intra Flags and Scene Change Flags interact
    3004             :  *    together to affect the prediction structure.
    3005             :  *
    3006             :  *  The base prediction structure for this example is a 3-Level Hierarchical Random Access,
    3007             :  *    Single Reference Prediction Structure:
    3008             :  *
    3009             :  *        b   b
    3010             :  *       / \ / \
    3011             :  *      /   B   \
    3012             :  *     /   / \   \
    3013             :  *    I-----------B
    3014             :  *
    3015             :  *  From this base structure, the following RPS positions are derived:
    3016             :  *
    3017             :  *    p   p       b   b       p   p
    3018             :  *     \   \     / \ / \     /   /
    3019             :  *      P   \   /   B   \   /   P
    3020             :  *       \   \ /   / \   \ /   /
    3021             :  *        ----I-----------B----
    3022             :  *
    3023             :  *    L L L   I  [ Normal ]   T T T
    3024             :  *    2 1 0   n               0 1 2
    3025             :  *            t
    3026             :  *            r
    3027             :  *            a
    3028             :  *
    3029             :  *  The RPS is composed of Leading Picture [L2-L0], Intra (CRA), Base/Normal Pictures,
    3030             :  *    and Trailing Pictures [T0-T2]. Generally speaking, Leading Pictures are useful
    3031             :  *    for handling scene changes without adding extraneous I-pictures and the Trailing
    3032             :  *    pictures are useful for terminating GOPs.
    3033             :  *
    3034             :  *  Here is a table of possible combinations of pictures needed to handle intra and
    3035             :  *    scene changes happening in quick succession.
    3036             :  *
    3037             :  *        Distance to scene change ------------>
    3038             :  *
    3039             :  *                  0              1                 2                3+
    3040             :  *   I
    3041             :  *   n
    3042             :  *   t   0        I   I           n/a               n/a              n/a
    3043             :  *   r
    3044             :  *   a              p              p
    3045             :  *                   \            /
    3046             :  *   P   1        I   I          I   I              n/a              n/a
    3047             :  *   e
    3048             :  *   r               p                               p
    3049             :  *   i                \                             /
    3050             :  *   o            p    \         p   p             /   p
    3051             :  *   d             \    \       /     \           /   /
    3052             :  *       2     I    -----I     I       I         I----    I          n/a
    3053             :  *   |
    3054             :  *   |            p   p           p   p            p   p            p   p
    3055             :  *   |             \   \         /     \          /     \          /   /
    3056             :  *   |              P   \       /   p   \        /   p   \        /   P
    3057             :  *   |               \   \     /     \   \      /   /     \      /   /
    3058             :  *   V   3+   I       ----I   I       ----I    I----       I    I----       I
    3059             :  *
    3060             :  *   The table is interpreted as follows:
    3061             :  *
    3062             :  *   If there are no SCs or Intras encountered for a PredPeriod, then the normal
    3063             :  *     prediction structure is applied.
    3064             :  *
    3065             :  *   If there is an intra in the PredPeriod, then one of the above combinations of
    3066             :  *     Leading and Trailing pictures is used.  If there is no scene change, the last
    3067             :  *     valid column consisting of Trailing Pictures only is used.  However, if there
    3068             :  *     is an upcoming scene change before the next intra, then one of the above patterns
    3069             :  *     is used. In the case of End of Sequence flags, only the last valid column of Trailing
    3070             :  *     Pictures is used. The intention here is that any combination of Intra Flag and Scene
    3071             :  *     Change flag can be coded.
    3072             :  *
    3073             :  ***************************************************************************************************/
    3074           2 : void* picture_decision_kernel(void *input_ptr)
    3075             : {
    3076           2 :     PictureDecisionContext        *context_ptr = (PictureDecisionContext*)input_ptr;
    3077             : 
    3078             :     PictureParentControlSet       *picture_control_set_ptr;
    3079             :     FrameHeader                   *frm_hdr;
    3080             : 
    3081             :     EncodeContext                 *encode_context_ptr;
    3082             :     SequenceControlSet            *sequence_control_set_ptr;
    3083             : 
    3084             :     EbObjectWrapper               *inputResultsWrapperPtr;
    3085             :     PictureAnalysisResults        *inputResultsPtr;
    3086             : 
    3087             :     EbObjectWrapper               *outputResultsWrapperPtr;
    3088             :     PictureDecisionResults        *outputResultsPtr;
    3089             : 
    3090             :     PredictionStructureEntry      *predPositionPtr;
    3091             : 
    3092             :     EbBool                          preAssignmentBufferFirstPassFlag;
    3093             :     EB_SLICE                         picture_type;
    3094             : 
    3095             :     PictureDecisionReorderEntry   *queueEntryPtr;
    3096             :     int32_t                           queueEntryIndex;
    3097             : 
    3098             :     int32_t                           previousEntryIndex;
    3099             : 
    3100           2 :     PaReferenceQueueEntry         *inputEntryPtr = (PaReferenceQueueEntry*)EB_NULL;;
    3101             :     uint32_t                           inputQueueIndex;
    3102             : 
    3103             :     PaReferenceQueueEntry         *paReferenceEntryPtr;
    3104             :     uint32_t                           paReferenceQueueIndex;
    3105             : 
    3106             :     uint64_t                           ref_poc;
    3107             : 
    3108             :     uint32_t                           depIdx;
    3109             :     uint64_t                           depPoc;
    3110             : 
    3111             :     uint32_t                           depListCount;
    3112             : 
    3113             :     // Dynamic GOP
    3114             :     uint32_t                           mini_gop_index;
    3115             :     uint32_t                           pictureIndex;
    3116             : 
    3117             :     EbBool                          windowAvail, framePasseThru;
    3118             :     uint32_t                           windowIndex;
    3119             :     uint32_t                           entryIndex;
    3120             :     PictureParentControlSet        *ParentPcsWindow[FUTURE_WINDOW_WIDTH + 2];
    3121             : 
    3122             :     // Debug
    3123           2 :     uint64_t                           loopCount = 0;
    3124             : 
    3125             :     for (;;) {
    3126             :         // Get Input Full Object
    3127         122 :         eb_get_full_object(
    3128             :             context_ptr->picture_analysis_results_input_fifo_ptr,
    3129             :             &inputResultsWrapperPtr);
    3130             : 
    3131         120 :         inputResultsPtr = (PictureAnalysisResults*)inputResultsWrapperPtr->object_ptr;
    3132         120 :         picture_control_set_ptr = (PictureParentControlSet*)inputResultsPtr->picture_control_set_wrapper_ptr->object_ptr;
    3133         120 :         sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    3134         120 :         frm_hdr = &picture_control_set_ptr->frm_hdr;
    3135         120 :         encode_context_ptr = (EncodeContext*)sequence_control_set_ptr->encode_context_ptr;
    3136         120 :         loopCount++;
    3137             : 
    3138             :         // Input Picture Analysis Results into the Picture Decision Reordering Queue
    3139             :         // P.S. Since the prior Picture Analysis processes stage is multithreaded, inputs to the Picture Decision Process
    3140             :         // can arrive out-of-display-order, so a the Picture Decision Reordering Queue is used to enforce processing of
    3141             :         // pictures in display order
    3142         120 :         if (!picture_control_set_ptr->is_overlay ) {
    3143         120 :             queueEntryIndex = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->picture_decision_reorder_queue[encode_context_ptr->picture_decision_reorder_queue_head_index]->picture_number);
    3144         120 :             queueEntryIndex += encode_context_ptr->picture_decision_reorder_queue_head_index;
    3145         120 :             queueEntryIndex = (queueEntryIndex > PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH - 1) ? queueEntryIndex - PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH : queueEntryIndex;
    3146         120 :             queueEntryPtr = encode_context_ptr->picture_decision_reorder_queue[queueEntryIndex];
    3147         120 :             if (queueEntryPtr->parent_pcs_wrapper_ptr != NULL) {
    3148           0 :                 CHECK_REPORT_ERROR_NC(
    3149             :                     encode_context_ptr->app_callback_ptr,
    3150             :                     EB_ENC_PD_ERROR8);
    3151             :             }
    3152             :             else {
    3153         120 :                 queueEntryPtr->parent_pcs_wrapper_ptr = inputResultsPtr->picture_control_set_wrapper_ptr;
    3154         120 :                 queueEntryPtr->picture_number = picture_control_set_ptr->picture_number;
    3155             :             }
    3156             : 
    3157         120 :             picture_control_set_ptr->pic_decision_reorder_queue_idx = queueEntryIndex;
    3158             :         }
    3159             :         // Process the head of the Picture Decision Reordering Queue (Entry N)
    3160             :         // P.S. The Picture Decision Reordering Queue should be parsed in the display order to be able to construct a pred structure
    3161         120 :         queueEntryPtr = encode_context_ptr->picture_decision_reorder_queue[encode_context_ptr->picture_decision_reorder_queue_head_index];
    3162             : 
    3163         240 :         while (queueEntryPtr->parent_pcs_wrapper_ptr != EB_NULL) {
    3164         234 :             if (((PictureParentControlSet *)(queueEntryPtr->parent_pcs_wrapper_ptr->object_ptr))->end_of_sequence_flag == EB_TRUE) {
    3165           2 :                 framePasseThru = EB_TRUE;
    3166             :             }
    3167             :             else
    3168         232 :                 framePasseThru = EB_FALSE;
    3169         234 :             windowAvail = EB_TRUE;
    3170         234 :             previousEntryIndex = QUEUE_GET_PREVIOUS_SPOT(encode_context_ptr->picture_decision_reorder_queue_head_index);
    3171             : 
    3172         234 :             ParentPcsWindow[0] = ParentPcsWindow[1] = ParentPcsWindow[2] = ParentPcsWindow[3] = ParentPcsWindow[4] = ParentPcsWindow[5] = NULL;
    3173             :             //for poc 0, ignore previous frame check
    3174         234 :             if (queueEntryPtr->picture_number > 0 && encode_context_ptr->picture_decision_reorder_queue[previousEntryIndex]->parent_pcs_wrapper_ptr == NULL)
    3175           0 :                 windowAvail = EB_FALSE;
    3176             :             else {
    3177             : 
    3178         234 :                 ParentPcsWindow[0] = queueEntryPtr->picture_number > 0 ? (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[previousEntryIndex]->parent_pcs_wrapper_ptr->object_ptr : NULL;
    3179         234 :                 ParentPcsWindow[1] = (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[encode_context_ptr->picture_decision_reorder_queue_head_index]->parent_pcs_wrapper_ptr->object_ptr;
    3180        1455 :                 for (windowIndex = 0; windowIndex < FUTURE_WINDOW_WIDTH; windowIndex++) {
    3181        1349 :                     entryIndex = QUEUE_GET_NEXT_SPOT(encode_context_ptr->picture_decision_reorder_queue_head_index, windowIndex + 1);
    3182        1349 :                     if (encode_context_ptr->picture_decision_reorder_queue[entryIndex]->parent_pcs_wrapper_ptr == NULL) {
    3183         116 :                         windowAvail = EB_FALSE;
    3184         116 :                         break;
    3185             :                     }
    3186        1233 :                     else if (((PictureParentControlSet *)(encode_context_ptr->picture_decision_reorder_queue[entryIndex]->parent_pcs_wrapper_ptr->object_ptr))->end_of_sequence_flag == EB_TRUE) {
    3187          12 :                         windowAvail = EB_FALSE;
    3188          12 :                         framePasseThru = EB_TRUE;
    3189          12 :                         break;
    3190             :                     }else {
    3191        1221 :                         ParentPcsWindow[2 + windowIndex] = (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[entryIndex]->parent_pcs_wrapper_ptr->object_ptr;
    3192             :                     }
    3193             :                 }
    3194             :             }
    3195             : 
    3196         234 :             picture_control_set_ptr = (PictureParentControlSet*)queueEntryPtr->parent_pcs_wrapper_ptr->object_ptr;
    3197         234 :             frm_hdr = &picture_control_set_ptr->frm_hdr;
    3198         234 :             picture_control_set_ptr->fade_out_from_black = 0;
    3199             : 
    3200         234 :             picture_control_set_ptr->fade_in_to_black = 0;
    3201         234 :             if (picture_control_set_ptr->idr_flag == EB_TRUE)
    3202          10 :                 context_ptr->last_solid_color_frame_poc = 0xFFFFFFFF;
    3203         234 :             if (windowAvail == EB_TRUE && queueEntryPtr->picture_number > 0) {
    3204         104 :                 if (sequence_control_set_ptr->static_config.scene_change_detection) {
    3205           0 :                     picture_control_set_ptr->scene_change_flag = SceneTransitionDetector(
    3206             :                         context_ptr,
    3207             :                         sequence_control_set_ptr,
    3208             :                         ParentPcsWindow,
    3209             :                         FUTURE_WINDOW_WIDTH);
    3210             :                 }
    3211             :                 else
    3212         104 :                     picture_control_set_ptr->scene_change_flag = EB_FALSE;
    3213         104 :                 picture_control_set_ptr->cra_flag = (picture_control_set_ptr->scene_change_flag == EB_TRUE) ?
    3214             :                     EB_TRUE :
    3215             :                     picture_control_set_ptr->cra_flag;
    3216             : 
    3217             :                 // Store scene change in context
    3218         104 :                 context_ptr->is_scene_change_detected = picture_control_set_ptr->scene_change_flag;
    3219             :             }
    3220             : 
    3221         234 :             if (windowAvail == EB_TRUE || framePasseThru == EB_TRUE)
    3222             :             {
    3223             :                 // Place the PCS into the Pre-Assignment Buffer
    3224             :                 // P.S. The Pre-Assignment Buffer is used to store a whole pre-structure
    3225         120 :                 encode_context_ptr->pre_assignment_buffer[encode_context_ptr->pre_assignment_buffer_count] = queueEntryPtr->parent_pcs_wrapper_ptr;
    3226             : 
    3227             :                 // Setup the PCS & SCS
    3228         120 :                 picture_control_set_ptr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[encode_context_ptr->pre_assignment_buffer_count]->object_ptr;
    3229         120 :                 frm_hdr = &picture_control_set_ptr->frm_hdr;
    3230             :                 // Set the POC Number
    3231         120 :                 picture_control_set_ptr->picture_number = (encode_context_ptr->current_input_poc + 1) /*& ((1 << sequence_control_set_ptr->bits_for_picture_order_count)-1)*/;
    3232         120 :                 encode_context_ptr->current_input_poc = picture_control_set_ptr->picture_number;
    3233             : 
    3234         120 :                 picture_control_set_ptr->pred_structure = EB_PRED_RANDOM_ACCESS;
    3235             : 
    3236         120 :                 picture_control_set_ptr->hierarchical_layers_diff = 0;
    3237             : 
    3238         120 :                 picture_control_set_ptr->init_pred_struct_position_flag = EB_FALSE;
    3239             : 
    3240         120 :                 picture_control_set_ptr->target_bit_rate = sequence_control_set_ptr->static_config.target_bit_rate;
    3241             : 
    3242         120 :                 ReleasePrevPictureFromReorderQueue(
    3243             :                     encode_context_ptr);
    3244             : 
    3245             :                 // If the Intra period length is 0, then introduce an intra for every picture
    3246         120 :                 if (sequence_control_set_ptr->intra_period_length == 0)
    3247           0 :                     picture_control_set_ptr->cra_flag = EB_TRUE;
    3248             :                 // If an #IntraPeriodLength has passed since the last Intra, then introduce a CRA or IDR based on Intra Refresh type
    3249         120 :                 else if (sequence_control_set_ptr->intra_period_length != -1) {
    3250         120 :                     picture_control_set_ptr->cra_flag =
    3251         120 :                         (sequence_control_set_ptr->intra_refresh_type != CRA_REFRESH) ?
    3252             :                         picture_control_set_ptr->cra_flag :
    3253         120 :                         (encode_context_ptr->intra_period_position == (uint32_t)sequence_control_set_ptr->intra_period_length) ?
    3254             :                         EB_TRUE :
    3255             :                         picture_control_set_ptr->cra_flag;
    3256             : 
    3257         120 :                     picture_control_set_ptr->idr_flag =
    3258         120 :                         (sequence_control_set_ptr->intra_refresh_type != IDR_REFRESH) ?
    3259             :                         picture_control_set_ptr->idr_flag :
    3260           0 :                         (encode_context_ptr->intra_period_position == (uint32_t)sequence_control_set_ptr->intra_period_length) ?
    3261             :                         EB_TRUE :
    3262             :                         picture_control_set_ptr->idr_flag;
    3263             :                 }
    3264             : 
    3265         120 :                 encode_context_ptr->pre_assignment_buffer_eos_flag = (picture_control_set_ptr->end_of_sequence_flag) ? (uint32_t)EB_TRUE : encode_context_ptr->pre_assignment_buffer_eos_flag;
    3266             : 
    3267             :                 // Increment the Pre-Assignment Buffer Intra Count
    3268         120 :                 encode_context_ptr->pre_assignment_buffer_intra_count += (picture_control_set_ptr->idr_flag || picture_control_set_ptr->cra_flag);
    3269         120 :                 encode_context_ptr->pre_assignment_buffer_idr_count += picture_control_set_ptr->idr_flag;
    3270         120 :                 encode_context_ptr->pre_assignment_buffer_count += 1;
    3271             : 
    3272         120 :                 if (sequence_control_set_ptr->static_config.rate_control_mode)
    3273             :                 {
    3274             :                     // Increment the Intra Period Position
    3275           0 :                     encode_context_ptr->intra_period_position = (encode_context_ptr->intra_period_position == (uint32_t)sequence_control_set_ptr->intra_period_length) ? 0 : encode_context_ptr->intra_period_position + 1;
    3276             :                 }
    3277             :                 else
    3278             :                 {
    3279             :                     // Increment the Intra Period Position
    3280         120 :                     encode_context_ptr->intra_period_position = ((encode_context_ptr->intra_period_position == (uint32_t)sequence_control_set_ptr->intra_period_length) || (picture_control_set_ptr->scene_change_flag == EB_TRUE)) ? 0 : encode_context_ptr->intra_period_position + 1;
    3281             :                 }
    3282             : 
    3283             :                 // Determine if Pictures can be released from the Pre-Assignment Buffer
    3284         120 :                 if ((encode_context_ptr->pre_assignment_buffer_intra_count > 0) ||
    3285         116 :                     (encode_context_ptr->pre_assignment_buffer_count == (uint32_t)(1 << sequence_control_set_ptr->static_config.hierarchical_levels)) ||
    3286         112 :                     (encode_context_ptr->pre_assignment_buffer_eos_flag == EB_TRUE) ||
    3287         110 :                     (picture_control_set_ptr->pred_structure == EB_PRED_LOW_DELAY_P) ||
    3288         110 :                     (picture_control_set_ptr->pred_structure == EB_PRED_LOW_DELAY_B))
    3289             :                 {
    3290             :                     // Initialize Picture Block Params
    3291          10 :                     context_ptr->mini_gop_start_index[0] = 0;
    3292          10 :                     context_ptr->mini_gop_end_index[0] = encode_context_ptr->pre_assignment_buffer_count - 1;
    3293          10 :                     context_ptr->mini_gop_length[0] = encode_context_ptr->pre_assignment_buffer_count;
    3294             : 
    3295          10 :                     context_ptr->mini_gop_hierarchical_levels[0] = sequence_control_set_ptr->static_config.hierarchical_levels;
    3296          10 :                     context_ptr->mini_gop_intra_count[0] = encode_context_ptr->pre_assignment_buffer_intra_count;
    3297          10 :                     context_ptr->mini_gop_idr_count[0] = encode_context_ptr->pre_assignment_buffer_idr_count;
    3298          10 :                     context_ptr->total_number_of_mini_gops = 1;
    3299             : 
    3300          20 :                     encode_context_ptr->previous_mini_gop_hierarchical_levels = (picture_control_set_ptr->picture_number == 0) ?
    3301          10 :                         sequence_control_set_ptr->static_config.hierarchical_levels :
    3302             :                         encode_context_ptr->previous_mini_gop_hierarchical_levels;
    3303             : 
    3304             :                     {
    3305          10 :                         if (encode_context_ptr->pre_assignment_buffer_count > 1)
    3306             :                         {
    3307           8 :                             initialize_mini_gop_activity_array(
    3308             :                                 context_ptr);
    3309             : 
    3310           8 :                             if (encode_context_ptr->pre_assignment_buffer_count == 16)
    3311           6 :                                 context_ptr->mini_gop_activity_array[L5_0_INDEX] = EB_FALSE;
    3312             :                             else {
    3313           2 :                                 context_ptr->mini_gop_activity_array[L4_0_INDEX] = EB_FALSE;
    3314           2 :                                 context_ptr->mini_gop_activity_array[L4_1_INDEX] = EB_FALSE;
    3315             :                             }
    3316             : 
    3317           8 :                             generate_picture_window_split(
    3318             :                                 context_ptr,
    3319             :                                 encode_context_ptr);
    3320             : 
    3321           8 :                             handle_incomplete_picture_window_map(
    3322             :                                 context_ptr,
    3323             :                                 encode_context_ptr);
    3324             :                         }
    3325             :                     }
    3326             : 
    3327          10 :                     GenerateMiniGopRps(
    3328             :                         context_ptr,
    3329             :                         encode_context_ptr);
    3330             : 
    3331             :                     // Loop over Mini GOPs
    3332             : 
    3333          22 :                     for (mini_gop_index = 0; mini_gop_index < context_ptr->total_number_of_mini_gops; ++mini_gop_index) {
    3334          12 :                         preAssignmentBufferFirstPassFlag = EB_TRUE;
    3335             :                         {
    3336          12 :                             update_base_layer_reference_queue_dependent_count(
    3337             :                                 context_ptr,
    3338             :                                 encode_context_ptr,
    3339             :                                 sequence_control_set_ptr,
    3340             :                                 mini_gop_index);
    3341             : 
    3342             :                             // Keep track of the number of hierarchical levels of the latest implemented mini GOP
    3343          12 :                             encode_context_ptr->previous_mini_gop_hierarchical_levels = context_ptr->mini_gop_hierarchical_levels[mini_gop_index];
    3344             :                         }
    3345             : 
    3346             :                         // 1st Loop over Pictures in the Pre-Assignment Buffer
    3347         132 :                         for (pictureIndex = context_ptr->mini_gop_start_index[mini_gop_index]; pictureIndex <= context_ptr->mini_gop_end_index[mini_gop_index]; ++pictureIndex) {
    3348         120 :                             picture_control_set_ptr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pictureIndex]->object_ptr;
    3349         120 :                             sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    3350         120 :                             frm_hdr = &picture_control_set_ptr->frm_hdr;
    3351             :                             // Keep track of the mini GOP size to which the input picture belongs - needed @ PictureManagerProcess()
    3352         120 :                             picture_control_set_ptr->pre_assignment_buffer_count = context_ptr->mini_gop_length[mini_gop_index];
    3353             : 
    3354             :                             // Update the Pred Structure if cutting short a Random Access period
    3355         120 :                             if ((context_ptr->mini_gop_length[mini_gop_index] < picture_control_set_ptr->pred_struct_ptr->pred_struct_period || context_ptr->mini_gop_idr_count[mini_gop_index] > 0) &&
    3356           8 :                                 picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_RANDOM_ACCESS &&
    3357           8 :                                 picture_control_set_ptr->idr_flag == EB_FALSE &&
    3358           6 :                                 picture_control_set_ptr->cra_flag == EB_FALSE)
    3359             :                             {
    3360             :                                 // Correct the Pred Index before switching structures
    3361           6 :                                 if (preAssignmentBufferFirstPassFlag == EB_TRUE)
    3362           2 :                                     encode_context_ptr->pred_struct_position -= picture_control_set_ptr->pred_struct_ptr->init_pic_index;
    3363          12 :                                 picture_control_set_ptr->pred_struct_ptr = get_prediction_structure(
    3364             :                                     encode_context_ptr->prediction_structure_group_ptr,
    3365             :                                     EB_PRED_LOW_DELAY_P,
    3366             :                                     sequence_control_set_ptr->reference_count,
    3367           6 :                                     picture_control_set_ptr->hierarchical_levels);
    3368             : 
    3369             :                                 // Set the RPS Override Flag - this current only will convert a Random Access structure to a Low Delay structure
    3370           6 :                                 picture_control_set_ptr->use_rps_in_sps = EB_FALSE;
    3371           6 :                                 picture_control_set_ptr->open_gop_cra_flag = EB_FALSE;
    3372             : 
    3373           6 :                                 picture_type = P_SLICE;
    3374             :                             }
    3375             :                             // Open GOP CRA - adjust the RPS
    3376         114 :                             else if ((context_ptr->mini_gop_length[mini_gop_index] == picture_control_set_ptr->pred_struct_ptr->pred_struct_period) &&
    3377             : 
    3378         112 :                                 (picture_control_set_ptr->pred_struct_ptr->pred_type == EB_PRED_RANDOM_ACCESS || picture_control_set_ptr->pred_struct_ptr->temporal_layer_count == 1) &&
    3379         112 :                                 picture_control_set_ptr->idr_flag == EB_FALSE &&
    3380         112 :                                 picture_control_set_ptr->cra_flag == EB_TRUE)
    3381             :                             {
    3382           2 :                                 picture_control_set_ptr->use_rps_in_sps = EB_FALSE;
    3383           2 :                                 picture_control_set_ptr->open_gop_cra_flag = EB_TRUE;
    3384             : 
    3385           2 :                                 picture_type = I_SLICE;
    3386             :                             }
    3387             :                             else {
    3388         112 :                                 picture_control_set_ptr->use_rps_in_sps = EB_FALSE;
    3389         112 :                                 picture_control_set_ptr->open_gop_cra_flag = EB_FALSE;
    3390             : 
    3391             :                                 // Set the Picture Type
    3392         112 :                                 picture_type =
    3393         112 :                                     (picture_control_set_ptr->idr_flag) ? I_SLICE :
    3394         110 :                                     (picture_control_set_ptr->cra_flag) ? I_SLICE :
    3395         110 :                                     (picture_control_set_ptr->pred_structure == EB_PRED_LOW_DELAY_P) ? P_SLICE :
    3396         110 :                                     (picture_control_set_ptr->pred_structure == EB_PRED_LOW_DELAY_B) ? B_SLICE :
    3397         110 :                                     (picture_control_set_ptr->pre_assignment_buffer_count == picture_control_set_ptr->pred_struct_ptr->pred_struct_period) ? ((pictureIndex == context_ptr->mini_gop_end_index[mini_gop_index] && sequence_control_set_ptr->static_config.base_layer_switch_mode) ? P_SLICE : B_SLICE) :
    3398             : 
    3399           0 :                                     (encode_context_ptr->pre_assignment_buffer_eos_flag) ? P_SLICE :
    3400             :                                     B_SLICE;
    3401             :                             }
    3402             :                             // If mini GOP switch, reset position
    3403         240 :                             encode_context_ptr->pred_struct_position = (picture_control_set_ptr->init_pred_struct_position_flag) ?
    3404         120 :                                 picture_control_set_ptr->pred_struct_ptr->init_pic_index :
    3405             :                                 encode_context_ptr->pred_struct_position;
    3406             : 
    3407             :                             // If Intra, reset position
    3408         120 :                             if (picture_control_set_ptr->idr_flag == EB_TRUE)
    3409           2 :                                 encode_context_ptr->pred_struct_position = picture_control_set_ptr->pred_struct_ptr->init_pic_index;
    3410         118 :                             else if (picture_control_set_ptr->cra_flag == EB_TRUE && context_ptr->mini_gop_length[mini_gop_index] < picture_control_set_ptr->pred_struct_ptr->pred_struct_period)
    3411           0 :                                 encode_context_ptr->pred_struct_position = picture_control_set_ptr->pred_struct_ptr->init_pic_index;
    3412         118 :                             else if (encode_context_ptr->elapsed_non_cra_count == 0) {
    3413             :                                 // If we are the picture directly after a CRA, we have to not use references that violate the CRA
    3414           4 :                                 encode_context_ptr->pred_struct_position = picture_control_set_ptr->pred_struct_ptr->init_pic_index + 1;
    3415             :                             }
    3416             :                             // Elif Scene Change, determine leading and trailing pictures
    3417             :                             //else if (encode_context_ptr->pre_assignment_buffer_scene_change_count > 0) {
    3418             :                             //    if(bufferIndex < encode_context_ptr->pre_assignment_buffer_scene_change_index) {
    3419             :                             //        ++encode_context_ptr->pred_struct_position;
    3420             :                             //        picture_type = P_SLICE;
    3421             :                             //    }
    3422             :                             //    else {
    3423             :                             //        encode_context_ptr->pred_struct_position = picture_control_set_ptr->pred_struct_ptr->init_pic_index + encode_context_ptr->pre_assignment_buffer_count - bufferIndex - 1;
    3424             :                             //    }
    3425             :                             //}
    3426             :                             // Else, Increment the position normally
    3427             :                             else
    3428         114 :                                 ++encode_context_ptr->pred_struct_position;
    3429             :                             // The poc number of the latest IDR picture is stored so that last_idr_picture (present in PCS) for the incoming pictures can be updated.
    3430             :                             // The last_idr_picture is used in reseting the poc (in entropy coding) whenever IDR is encountered.
    3431             :                             // Note IMP: This logic only works when display and decode order are the same. Currently for Random Access, IDR is inserted (similar to CRA) by using trailing P pictures (low delay fashion) and breaking prediction structure.
    3432             :                             // Note: When leading P pictures are implemented, this logic has to change..
    3433         120 :                             if (picture_control_set_ptr->idr_flag == EB_TRUE)
    3434           2 :                                 encode_context_ptr->last_idr_picture = picture_control_set_ptr->picture_number;
    3435             :                             else
    3436         118 :                                 picture_control_set_ptr->last_idr_picture = encode_context_ptr->last_idr_picture;
    3437             :                             // Cycle the PredStructPosition if its overflowed
    3438         240 :                             encode_context_ptr->pred_struct_position = (encode_context_ptr->pred_struct_position == picture_control_set_ptr->pred_struct_ptr->pred_struct_entry_count) ?
    3439         120 :                                 encode_context_ptr->pred_struct_position - picture_control_set_ptr->pred_struct_ptr->pred_struct_period :
    3440             :                                 encode_context_ptr->pred_struct_position;
    3441             : 
    3442         120 :                             predPositionPtr = picture_control_set_ptr->pred_struct_ptr->pred_struct_entry_ptr_array[encode_context_ptr->pred_struct_position];
    3443         120 :                             if (sequence_control_set_ptr->static_config.enable_overlays == EB_TRUE) {
    3444             :                                 // At this stage we know the prediction structure and the location of ALT_REF pictures.
    3445             :                                 // For every ALTREF picture, there is an overlay picture. They extra pictures are released
    3446             :                                 // is_alt_ref flag is set for non-slice base layer pictures
    3447           0 :                                 if (predPositionPtr->temporal_layer_index == 0 && picture_type != I_SLICE) {
    3448           0 :                                     picture_control_set_ptr->is_alt_ref = 1;
    3449           0 :                                     frm_hdr->show_frame = 0;
    3450           0 :                                     ((PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pictureIndex - 1]->object_ptr)->has_show_existing = EB_FALSE;
    3451             :                                 }
    3452             :                                 // release the overlay PCS for non alt ref pictures. First picture does not have overlay PCS
    3453           0 :                                 else if (picture_control_set_ptr->picture_number) {
    3454           0 :                                     eb_release_object(picture_control_set_ptr->overlay_ppcs_ptr->input_picture_wrapper_ptr);
    3455             :                                     // release the pa_reference_picture
    3456           0 :                                     eb_release_object(picture_control_set_ptr->overlay_ppcs_ptr->pa_reference_picture_wrapper_ptr);
    3457             :                                     // release the parent pcs
    3458           0 :                                     eb_release_object(picture_control_set_ptr->overlay_ppcs_ptr->p_pcs_wrapper_ptr);
    3459           0 :                                     picture_control_set_ptr->overlay_ppcs_ptr = EB_NULL;
    3460             :                                 }
    3461             :                             }
    3462         120 :                             PictureParentControlSet       *cur_picture_control_set_ptr = picture_control_set_ptr;
    3463         240 :                             for (uint8_t loop_index = 0; loop_index <= picture_control_set_ptr->is_alt_ref; loop_index++) {
    3464             :                                 // Set missing parts in the overlay  pictures
    3465         120 :                                 if (loop_index == 1) {
    3466           0 :                                     picture_control_set_ptr = picture_control_set_ptr->overlay_ppcs_ptr;
    3467           0 :                                     initialize_overlay_frame(picture_control_set_ptr);
    3468           0 :                                     picture_type = P_SLICE;
    3469             :                                 }
    3470             :                                 // Set the Slice type
    3471         120 :                                 picture_control_set_ptr->slice_type = picture_type;
    3472         120 :                                 ((EbPaReferenceObject*)picture_control_set_ptr->pa_reference_picture_wrapper_ptr->object_ptr)->slice_type = picture_control_set_ptr->slice_type;
    3473             : 
    3474         120 :                                 switch (picture_type) {
    3475           4 :                                 case I_SLICE:
    3476             : 
    3477             :                                     // Reset Prediction Structure Position & Reference Struct Position
    3478           4 :                                     if (picture_control_set_ptr->picture_number == 0)
    3479           2 :                                         encode_context_ptr->intra_period_position = 0;
    3480           4 :                                     encode_context_ptr->elapsed_non_cra_count = 0;
    3481             : 
    3482             :                                     //-------------------------------
    3483             :                                     // IDR
    3484             :                                     //-------------------------------
    3485           4 :                                     if (picture_control_set_ptr->idr_flag == EB_TRUE) {
    3486             :                                         // Set CRA flag
    3487           2 :                                         picture_control_set_ptr->cra_flag = EB_FALSE;
    3488             : 
    3489             :                                         // Reset the pictures since last IDR counter
    3490           2 :                                         encode_context_ptr->elapsed_non_idr_count = 0;
    3491             :                                         //log latest key frame poc
    3492           2 :                                         context_ptr->key_poc = picture_control_set_ptr->picture_number;
    3493             :                                     }
    3494             :                                     //-------------------------------
    3495             :                                     // CRA
    3496             :                                     //-------------------------------
    3497             :                                     else {
    3498             :                                         // Set a Random Access Point if not an IDR
    3499           2 :                                         picture_control_set_ptr->cra_flag = EB_TRUE;
    3500             :                                     }
    3501           4 :                                     break;
    3502             : 
    3503         116 :                                 case P_SLICE:
    3504             :                                 case B_SLICE:
    3505             : 
    3506             :                                     // Reset CRA and IDR Flag
    3507         116 :                                     picture_control_set_ptr->cra_flag = EB_FALSE;
    3508         116 :                                     picture_control_set_ptr->idr_flag = EB_FALSE;
    3509             : 
    3510         116 :                                     if (loop_index == 0)
    3511             :                                     {
    3512             :                                         // Increment & Clip the elapsed Non-IDR Counter. This is clipped rather than allowed to free-run
    3513             :                                         // inorder to avoid rollover issues.  This assumes that any the GOP period is less than MAX_ELAPSED_IDR_COUNT
    3514         116 :                                         encode_context_ptr->elapsed_non_idr_count = MIN(encode_context_ptr->elapsed_non_idr_count + 1, MAX_ELAPSED_IDR_COUNT);
    3515         116 :                                         encode_context_ptr->elapsed_non_cra_count = MIN(encode_context_ptr->elapsed_non_cra_count + 1, MAX_ELAPSED_IDR_COUNT);
    3516             :                                     }
    3517             : 
    3518         116 :                                     CHECK_REPORT_ERROR(
    3519             :                                         (picture_control_set_ptr->pred_struct_ptr->pred_struct_entry_count < MAX_ELAPSED_IDR_COUNT),
    3520             :                                         encode_context_ptr->app_callback_ptr,
    3521             :                                         EB_ENC_PD_ERROR1);
    3522             : 
    3523         116 :                                     break;
    3524             : 
    3525           0 :                                 default:
    3526             : 
    3527           0 :                                     CHECK_REPORT_ERROR_NC(
    3528             :                                         encode_context_ptr->app_callback_ptr,
    3529             :                                         EB_ENC_PD_ERROR2);
    3530             : 
    3531             :                                     break;
    3532             :                                 }
    3533         120 :                                 picture_control_set_ptr->pred_struct_index = (uint8_t)encode_context_ptr->pred_struct_position;
    3534         120 :                                 if (picture_control_set_ptr->is_overlay) {
    3535             :                                     // set the overlay frame as non reference frame with max temporal layer index
    3536           0 :                                     picture_control_set_ptr->temporal_layer_index = (uint8_t)picture_control_set_ptr->hierarchical_levels;
    3537           0 :                                     picture_control_set_ptr->is_used_as_reference_flag = EB_FALSE;
    3538             :                                 }
    3539             :                                 else {
    3540         120 :                                     picture_control_set_ptr->temporal_layer_index = (uint8_t)predPositionPtr->temporal_layer_index;
    3541         120 :                                     picture_control_set_ptr->is_used_as_reference_flag = predPositionPtr->is_referenced;
    3542             :                                 }
    3543             : 
    3544         120 :                                 preAssignmentBufferFirstPassFlag = EB_FALSE;
    3545             : 
    3546             :                                 // Film grain (assigning the random-seed)
    3547             :                                 {
    3548         120 :                                     uint16_t *fgn_random_seed_ptr = &picture_control_set_ptr->sequence_control_set_ptr->film_grain_random_seed;
    3549         120 :                                     frm_hdr->film_grain_params.random_seed = *fgn_random_seed_ptr;
    3550         120 :                                     *fgn_random_seed_ptr += 3381;  // Changing random seed for film grain
    3551         120 :                                     if (!(*fgn_random_seed_ptr))     // Random seed should not be zero
    3552           0 :                                         *fgn_random_seed_ptr += 7391;
    3553             :                                 }
    3554         120 :                                 Av1GenerateRpsInfo(
    3555             :                                     picture_control_set_ptr,
    3556             :                                     encode_context_ptr,
    3557             :                                     context_ptr,
    3558         120 :                                     pictureIndex - context_ptr->mini_gop_start_index[mini_gop_index],
    3559             :                                     mini_gop_index);
    3560         120 :                                 picture_control_set_ptr->allow_comp_inter_inter = 0;
    3561         120 :                                 picture_control_set_ptr->is_skip_mode_allowed = 0;
    3562             : 
    3563         120 :                                 frm_hdr->reference_mode = (ReferenceMode)0xFF;
    3564             : 
    3565         120 :                                 if (picture_control_set_ptr->slice_type != I_SLICE) {
    3566         116 :                                     picture_control_set_ptr->allow_comp_inter_inter = 1;
    3567         116 :                                     if (picture_control_set_ptr->slice_type == P_SLICE) {
    3568           6 :                                         picture_control_set_ptr->is_skip_mode_allowed = 0;
    3569           6 :                                         frm_hdr->reference_mode = SINGLE_REFERENCE;
    3570           6 :                                         picture_control_set_ptr->skip_mode_flag = 0;
    3571             :                                     }
    3572         110 :                                     else if (picture_control_set_ptr->temporal_layer_index == 0) {
    3573           6 :                                         frm_hdr->reference_mode = REFERENCE_MODE_SELECT;
    3574           6 :                                         frm_hdr->skip_mode_params.skip_mode_flag = 0;
    3575             :                                     }
    3576             :                                     else {
    3577         104 :                                         frm_hdr->reference_mode = REFERENCE_MODE_SELECT;
    3578         104 :                                         picture_control_set_ptr->is_skip_mode_allowed = 1;
    3579         104 :                                         picture_control_set_ptr->skip_mode_flag = 1;
    3580             :                                     }
    3581             :                                 }
    3582             : 
    3583         120 :                                 picture_control_set_ptr->av1_cm->mi_cols = picture_control_set_ptr->sequence_control_set_ptr->seq_header.max_frame_width >> MI_SIZE_LOG2;
    3584         120 :                                 picture_control_set_ptr->av1_cm->mi_rows = picture_control_set_ptr->sequence_control_set_ptr->seq_header.max_frame_height >> MI_SIZE_LOG2;
    3585             : 
    3586         120 :                                 memset(picture_control_set_ptr->av1_cm->ref_frame_sign_bias, 0, 8 * sizeof(int32_t));
    3587         120 :                                 if (frm_hdr->reference_mode == REFERENCE_MODE_SELECT && picture_control_set_ptr->temporal_layer_index)
    3588             :                                 {
    3589         104 :                                     picture_control_set_ptr->av1_cm->ref_frame_sign_bias[ALTREF_FRAME] =
    3590         104 :                                         picture_control_set_ptr->av1_cm->ref_frame_sign_bias[ALTREF2_FRAME] =
    3591         104 :                                         picture_control_set_ptr->av1_cm->ref_frame_sign_bias[BWDREF_FRAME] = 1;
    3592             :                                 }
    3593             : 
    3594         120 :                                 if (picture_control_set_ptr->slice_type == I_SLICE)
    3595           4 :                                     context_ptr->last_i_picture_sc_detection = picture_control_set_ptr->sc_content_detected;
    3596             :                                 else
    3597         116 :                                     picture_control_set_ptr->sc_content_detected = context_ptr->last_i_picture_sc_detection;
    3598             : 
    3599             :                                 // TODO: put this in EbMotionEstimationProcess?
    3600             :                                 // ME Kernel Multi-Processes Signal(s) derivation
    3601         120 :                                 signal_derivation_multi_processes_oq(
    3602             :                                 sequence_control_set_ptr,
    3603             :                                     picture_control_set_ptr);
    3604             : 
    3605             :                             // Set tx_mode
    3606         240 :                             frm_hdr->tx_mode = (picture_control_set_ptr->atb_mode) ?
    3607         120 :                                 TX_MODE_SELECT :
    3608             :                                 TX_MODE_LARGEST;
    3609             : 
    3610         120 :                                 picture_control_set_ptr->use_src_ref = EB_FALSE;
    3611         120 :                                 picture_control_set_ptr->enable_in_loop_motion_estimation_flag = EB_FALSE;
    3612         120 :                                 picture_control_set_ptr->limit_ois_to_dc_mode_flag = EB_FALSE;
    3613             : 
    3614             :                                 // Update the Dependant List Count - If there was an I-frame or Scene Change, then cleanup the Picture Decision PA Reference Queue Dependent Counts
    3615         120 :                                 if (picture_control_set_ptr->slice_type == I_SLICE)
    3616             :                                 {
    3617           4 :                                     inputQueueIndex = encode_context_ptr->picture_decision_pa_reference_queue_head_index;
    3618             : 
    3619          68 :                                     while (inputQueueIndex != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) {
    3620          64 :                                         inputEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[inputQueueIndex];
    3621             : 
    3622             :                                         // Modify Dependent List0
    3623          64 :                                         depListCount = inputEntryPtr->list0.list_count;
    3624         218 :                                         for (depIdx = 0; depIdx < depListCount; ++depIdx) {
    3625             :                                             // Adjust the latest current_input_poc in case we're in a POC rollover scenario
    3626             :                                             // current_input_poc += (current_input_poc < inputEntryPtr->pocNumber) ? (1 << sequence_control_set_ptr->bits_for_picture_order_count) : 0;
    3627             : 
    3628         154 :                                             depPoc = POC_CIRCULAR_ADD(
    3629             :                                                 inputEntryPtr->picture_number, // can't use a value that gets reset
    3630             :                                                 inputEntryPtr->list0.list[depIdx]/*,
    3631             :                                                 sequence_control_set_ptr->bits_for_picture_order_count*/);
    3632             : 
    3633             :                                                 // If Dependent POC is greater or equal to the IDR POC
    3634         154 :                                             if (depPoc >= picture_control_set_ptr->picture_number && inputEntryPtr->list0.list[depIdx]) {
    3635          22 :                                                 inputEntryPtr->list0.list[depIdx] = 0;
    3636             : 
    3637             :                                                 // Decrement the Reference's referenceCount
    3638          22 :                                                 --inputEntryPtr->dependent_count;
    3639             : 
    3640          22 :                                                 CHECK_REPORT_ERROR(
    3641             :                                                     (inputEntryPtr->dependent_count != ~0u),
    3642             :                                                     encode_context_ptr->app_callback_ptr,
    3643             :                                                     EB_ENC_PD_ERROR3);
    3644             :                                             }
    3645             :                                         }
    3646             : 
    3647             :                                         // Modify Dependent List1
    3648          64 :                                         depListCount = inputEntryPtr->list1.list_count;
    3649         146 :                                         for (depIdx = 0; depIdx < depListCount; ++depIdx) {
    3650             :                                             // Adjust the latest current_input_poc in case we're in a POC rollover scenario
    3651             :                                             // current_input_poc += (current_input_poc < inputEntryPtr->pocNumber) ? (1 << sequence_control_set_ptr->bits_for_picture_order_count) : 0;
    3652             : 
    3653          82 :                                             depPoc = POC_CIRCULAR_ADD(
    3654             :                                                 inputEntryPtr->picture_number,
    3655             :                                                 inputEntryPtr->list1.list[depIdx]/*,
    3656             :                                                 sequence_control_set_ptr->bits_for_picture_order_count*/);
    3657             : 
    3658             :                                                 // If Dependent POC is greater or equal to the IDR POC
    3659          82 :                                             if (((depPoc >= picture_control_set_ptr->picture_number) || (((picture_control_set_ptr->pre_assignment_buffer_count != picture_control_set_ptr->pred_struct_ptr->pred_struct_period) || (picture_control_set_ptr->idr_flag == EB_TRUE)) && (depPoc > (picture_control_set_ptr->picture_number - picture_control_set_ptr->pre_assignment_buffer_count)))) && inputEntryPtr->list1.list[depIdx]) {
    3660           6 :                                                 inputEntryPtr->list1.list[depIdx] = 0;
    3661             : 
    3662             :                                                 // Decrement the Reference's referenceCount
    3663           6 :                                                 --inputEntryPtr->dependent_count;
    3664             : 
    3665           6 :                                                 CHECK_REPORT_ERROR(
    3666             :                                                     (inputEntryPtr->dependent_count != ~0u),
    3667             :                                                     encode_context_ptr->app_callback_ptr,
    3668             :                                                     EB_ENC_PD_ERROR3);
    3669             :                                             }
    3670             :                                         }
    3671             : 
    3672             :                                         // Increment the inputQueueIndex Iterator
    3673          64 :                                         inputQueueIndex = (inputQueueIndex == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : inputQueueIndex + 1;
    3674             :                                     }
    3675             :                                 }
    3676         116 :                                 else if (picture_control_set_ptr->idr_flag == EB_TRUE) {
    3677             :                                     // Set the Picture Decision PA Reference Entry pointer
    3678           0 :                                     inputEntryPtr = (PaReferenceQueueEntry*)EB_NULL;
    3679             :                                 }
    3680             : 
    3681             :                                 // Place Picture in Picture Decision PA Reference Queue
    3682             :                                 // there is no need to add the overlay frames in the PA Reference Queue
    3683         120 :                                 if (!picture_control_set_ptr->is_overlay)
    3684             :                                 {
    3685         120 :                                     inputEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[encode_context_ptr->picture_decision_pa_reference_queue_tail_index];
    3686         120 :                                     inputEntryPtr->input_object_ptr = picture_control_set_ptr->pa_reference_picture_wrapper_ptr;
    3687         120 :                                     inputEntryPtr->picture_number = picture_control_set_ptr->picture_number;
    3688         120 :                                     inputEntryPtr->reference_entry_index = encode_context_ptr->picture_decision_pa_reference_queue_tail_index;
    3689         120 :                                     inputEntryPtr->is_alt_ref = picture_control_set_ptr->is_alt_ref;
    3690         120 :                                     encode_context_ptr->picture_decision_pa_reference_queue_tail_index =
    3691         120 :                                         (encode_context_ptr->picture_decision_pa_reference_queue_tail_index == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : encode_context_ptr->picture_decision_pa_reference_queue_tail_index + 1;
    3692             : 
    3693             :                                     // Check if the Picture Decision PA Reference is full
    3694         120 :                                     CHECK_REPORT_ERROR(
    3695             :                                         (((encode_context_ptr->picture_decision_pa_reference_queue_head_index != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) ||
    3696             :                                         (encode_context_ptr->picture_decision_pa_reference_queue[encode_context_ptr->picture_decision_pa_reference_queue_head_index]->input_object_ptr == EB_NULL))),
    3697             :                                         encode_context_ptr->app_callback_ptr,
    3698             :                                         EB_ENC_PD_ERROR4);
    3699             :                                 }
    3700             :                                 // Copy the reference lists into the inputEntry and
    3701             :                                 // set the Reference Counts Based on Temporal Layer and how many frames are active
    3702         236 :                                 picture_control_set_ptr->ref_list0_count = (picture_type == I_SLICE) ? 0 :
    3703         116 :                                                                             (picture_control_set_ptr->is_overlay) ? 1 : (uint8_t)predPositionPtr->ref_list0.reference_list_count;
    3704         120 :                                 picture_control_set_ptr->ref_list1_count = (picture_type == I_SLICE || picture_control_set_ptr->is_overlay) ? 0 : (uint8_t)predPositionPtr->ref_list1.reference_list_count;
    3705         120 :                                 if (!picture_control_set_ptr->is_overlay) {
    3706         120 :                                     inputEntryPtr->list0_ptr = &predPositionPtr->ref_list0;
    3707         120 :                                     inputEntryPtr->list1_ptr = &predPositionPtr->ref_list1;
    3708             :                                 }
    3709         120 :                                 if (!picture_control_set_ptr->is_overlay)
    3710             :                                 {
    3711             :                                     // Copy the Dependent Lists
    3712             :                                     // *Note - we are removing any leading picture dependencies for now
    3713         120 :                                     inputEntryPtr->list0.list_count = 0;
    3714         440 :                                     for (depIdx = 0; depIdx < predPositionPtr->dep_list0.list_count; ++depIdx) {
    3715         320 :                                         if (predPositionPtr->dep_list0.list[depIdx] >= 0)
    3716         302 :                                             inputEntryPtr->list0.list[inputEntryPtr->list0.list_count++] = predPositionPtr->dep_list0.list[depIdx];
    3717             :                                     }
    3718         120 :                                     inputEntryPtr->list1.list_count = predPositionPtr->dep_list1.list_count;
    3719         295 :                                     for (depIdx = 0; depIdx < predPositionPtr->dep_list1.list_count; ++depIdx)
    3720         175 :                                         inputEntryPtr->list1.list[depIdx] = predPositionPtr->dep_list1.list[depIdx];
    3721             :                                     // add the overlay picture to the dependant list
    3722         120 :                                     inputEntryPtr->dep_list0_count = (picture_control_set_ptr->is_alt_ref) ? inputEntryPtr->list0.list_count + 1 : inputEntryPtr->list0.list_count;
    3723             : 
    3724         120 :                                     inputEntryPtr->dep_list1_count = inputEntryPtr->list1.list_count;
    3725         120 :                                     inputEntryPtr->dependent_count = inputEntryPtr->dep_list0_count + inputEntryPtr->dep_list1_count;
    3726             : 
    3727         120 :                                     ((EbPaReferenceObject*)picture_control_set_ptr->pa_reference_picture_wrapper_ptr->object_ptr)->dependent_pictures_count = inputEntryPtr->dependent_count;
    3728             :                                 }
    3729             : 
    3730         120 :                                 CHECK_REPORT_ERROR(
    3731             :                                     (picture_control_set_ptr->pred_struct_ptr->pred_struct_period * REF_LIST_MAX_DEPTH < MAX_ELAPSED_IDR_COUNT),
    3732             :                                     encode_context_ptr->app_callback_ptr,
    3733             :                                     EB_ENC_PD_ERROR5);
    3734             : 
    3735             :                                 // Reset the PA Reference Lists
    3736         120 :                                 EB_MEMSET(picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_0], 0, REF_LIST_MAX_DEPTH * sizeof(EbObjectWrapper*));
    3737         120 :                                 EB_MEMSET(picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_1], 0, REF_LIST_MAX_DEPTH * sizeof(EbObjectWrapper*));
    3738             : 
    3739         120 :                                 EB_MEMSET(picture_control_set_ptr->ref_pic_poc_array[REF_LIST_0], 0, REF_LIST_MAX_DEPTH * sizeof(uint64_t));
    3740         120 :                                 EB_MEMSET(picture_control_set_ptr->ref_pic_poc_array[REF_LIST_1], 0, REF_LIST_MAX_DEPTH * sizeof(uint64_t));
    3741             :                             }
    3742         120 :                             picture_control_set_ptr = cur_picture_control_set_ptr;
    3743             : 
    3744         120 :                             if( sequence_control_set_ptr->enable_altrefs == EB_TRUE &&
    3745             : #if NON_KF_INTRA_TF_FIX
    3746             :                                 ((picture_control_set_ptr->slice_type == I_SLICE && picture_control_set_ptr->sc_content_detected == 0) ||
    3747             : #else
    3748         120 :                                 ( (picture_control_set_ptr->idr_flag && picture_control_set_ptr->sc_content_detected == 0) ||
    3749             : #endif
    3750         118 :                                   (picture_control_set_ptr->slice_type != I_SLICE && picture_control_set_ptr->temporal_layer_index == 0)
    3751             : #if TWO_PASS
    3752         112 :                                     || (sequence_control_set_ptr->use_input_stat_file && picture_control_set_ptr->temporal_layer_index == 1 && picture_control_set_ptr->sc_content_detected == 0)
    3753             : #endif
    3754           8 :                                     ) ) {
    3755           8 :                                 int altref_nframes = picture_control_set_ptr->sequence_control_set_ptr->static_config.altref_nframes;
    3756           8 :                                 if (picture_control_set_ptr->idr_flag) {
    3757             : 
    3758             :                                     //initilize list
    3759          22 :                                     for (int pic_itr = 0; pic_itr < ALTREF_MAX_NFRAMES; pic_itr++)
    3760          20 :                                         picture_control_set_ptr->temp_filt_pcs_list[pic_itr] = NULL;
    3761             : 
    3762           2 :                                     picture_control_set_ptr->temp_filt_pcs_list[0] = picture_control_set_ptr;
    3763             : 
    3764           2 :                                     uint32_t num_future_pics = 6;
    3765           2 :                                     uint32_t num_past_pics = 0;
    3766             :                                     uint32_t pic_i;
    3767             :                                     //search reord-queue to get the future pictures
    3768          14 :                                     for (pic_i = 0; pic_i < num_future_pics; pic_i++) {
    3769          12 :                                         int32_t q_index = QUEUE_GET_NEXT_SPOT(picture_control_set_ptr->pic_decision_reorder_queue_idx, pic_i + 1);
    3770          12 :                                         if (encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr != NULL) {
    3771          12 :                                             PictureParentControlSet* pcs_itr = (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr->object_ptr;
    3772          12 :                                             picture_control_set_ptr->temp_filt_pcs_list[pic_i + num_past_pics + 1] = pcs_itr;
    3773             : 
    3774             :                                         }
    3775             :                                         else
    3776           0 :                                            break;
    3777             :                                     }
    3778             : 
    3779           2 :                                     picture_control_set_ptr->past_altref_nframes = 0;
    3780           2 :                                     picture_control_set_ptr->future_altref_nframes = pic_i;
    3781           2 :                                     int index_center = 0;
    3782           2 :                                     uint32_t actual_future_pics = picture_control_set_ptr->future_altref_nframes;
    3783             :                                     int pic_itr, ahd;
    3784             : 
    3785           2 :                                     int ahd_th = (((sequence_control_set_ptr->seq_header.max_frame_width * sequence_control_set_ptr->seq_header.max_frame_height) * AHD_TH_WEIGHT) / 100);
    3786             : 
    3787             :                                     // Accumulative histogram absolute differences between the central and future frame
    3788           2 :                                     for (pic_itr = (index_center + actual_future_pics); pic_itr > index_center; pic_itr--) {
    3789           2 :                                         ahd = compute_luma_sad_between_center_and_target_frame(index_center, pic_itr, picture_control_set_ptr, sequence_control_set_ptr);
    3790           2 :                                         if (ahd < ahd_th)
    3791           2 :                                             break;
    3792             :                                     }
    3793           2 :                                     picture_control_set_ptr->future_altref_nframes = pic_itr - index_center;
    3794             :                                     //printf("\nPOC %d\t PAST %d\t FUTURE %d\n", picture_control_set_ptr->picture_number, picture_control_set_ptr->past_altref_nframes, picture_control_set_ptr->future_altref_nframes);
    3795             :                                 }
    3796             :                                 else
    3797             :                                 {
    3798           6 :                                 int num_past_pics = altref_nframes / 2;
    3799           6 :                                 int num_future_pics = altref_nframes - num_past_pics - 1;
    3800           6 :                                 assert(altref_nframes <= ALTREF_MAX_NFRAMES);
    3801             : 
    3802             :                                 //initilize list
    3803          66 :                                 for (int pic_itr = 0; pic_itr < ALTREF_MAX_NFRAMES; pic_itr++)
    3804          60 :                                     picture_control_set_ptr->temp_filt_pcs_list[pic_itr] = NULL;
    3805             : 
    3806             :                                 //get previous+current pictures from the the pre-assign buffer
    3807          30 :                                 for (int pic_itr = 0; pic_itr <= num_past_pics; pic_itr++) {
    3808          24 :                                     PictureParentControlSet* pcs_itr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pictureIndex - num_past_pics + pic_itr]->object_ptr;
    3809          24 :                                     picture_control_set_ptr->temp_filt_pcs_list[pic_itr] = pcs_itr;
    3810             :                                 }
    3811             : #if FIX_ALTREF
    3812           6 :                                 int actual_past_pics = num_past_pics;
    3813           6 :                                 int actual_future_pics = 0;
    3814             :                                 int pic_i;
    3815             :                                 //search reord-queue to get the future pictures
    3816          18 :                                 for (pic_i = 0; pic_i < num_future_pics; pic_i++) {
    3817          14 :                                     int32_t q_index = QUEUE_GET_NEXT_SPOT(picture_control_set_ptr->pic_decision_reorder_queue_idx, pic_i + 1);
    3818          14 :                                     if (encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr != NULL) {
    3819          12 :                                         PictureParentControlSet* pcs_itr = (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr->object_ptr;
    3820          12 :                                         picture_control_set_ptr->temp_filt_pcs_list[pic_i + num_past_pics + 1] = pcs_itr;
    3821          12 :                                         actual_future_pics++;
    3822             :                                     }
    3823             :                                     else
    3824           2 :                                         break;
    3825             :                                 }
    3826             : 
    3827             :                                 //search in pre-ass if still short
    3828           6 :                                 if (pic_i < num_future_pics) {
    3829           2 :                                     actual_future_pics = 0;
    3830           8 :                                     for (int pic_i_future = 0; pic_i_future < num_future_pics; pic_i_future++) {
    3831          60 :                                         for (uint32_t pic_i_pa = 0; pic_i_pa < encode_context_ptr->pre_assignment_buffer_count; pic_i_pa++) {
    3832          60 :                                             PictureParentControlSet* pcs_itr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pic_i_pa]->object_ptr;
    3833          60 :                                             if (pcs_itr->picture_number == picture_control_set_ptr->picture_number + pic_i_future + 1) {
    3834           6 :                                                 picture_control_set_ptr->temp_filt_pcs_list[pic_i_future + num_past_pics + 1] = pcs_itr;
    3835           6 :                                                 actual_future_pics++;
    3836           6 :                                                 break; //exist the pre-ass loop, go search the next
    3837             :                                             }
    3838             :                                         }
    3839             :                                     }
    3840             :                                 }
    3841             : #else
    3842             :                                 int actual_future_pics = 0;
    3843             :                                 int actual_past_pics = 0;
    3844             : 
    3845             :                                 int pic_i;
    3846             :                                 //search reorder-queue to get the future pictures
    3847             :                                 for (pic_i = num_past_pics + 1; pic_i < ALTREF_MAX_NFRAMES; pic_i++) {
    3848             :                                     int32_t q_index = QUEUE_GET_NEXT_SPOT(picture_control_set_ptr->pic_decision_reorder_queue_idx, pic_i - num_past_pics);
    3849             :                                     if (encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr != NULL && pic_i < altref_nframes) {
    3850             :                                         PictureParentControlSet* pcs_itr = (PictureParentControlSet *)encode_context_ptr->picture_decision_reorder_queue[q_index]->parent_pcs_wrapper_ptr->object_ptr;
    3851             :                                         picture_control_set_ptr->temp_filt_pcs_list[pic_i] = pcs_itr;
    3852             :                                     }
    3853             :                                     else
    3854             :                                         break;
    3855             :                                 }
    3856             : 
    3857             :                                 //search in pre-ass if still short
    3858             :                                 if (pic_i < num_future_pics) {
    3859             :                                     for (int pic_i_future = 0; pic_i_future < num_future_pics; pic_i_future++) {
    3860             :                                         for (uint32_t pic_i_pa = 0; pic_i_pa < encode_context_ptr->pre_assignment_buffer_count; pic_i_pa++) {
    3861             :                                             PictureParentControlSet* pcs_itr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pic_i_pa]->object_ptr;
    3862             :                                             if (pcs_itr->picture_number == picture_control_set_ptr->picture_number + pic_i_future + 1) {
    3863             :                                                 picture_control_set_ptr->temp_filt_pcs_list[pic_i_future + num_past_pics + 1] = pcs_itr;
    3864             :                                                 break; //exist the pre-ass loop, go search the next
    3865             :                                             }
    3866             :                                         }
    3867             :                                     }
    3868             :                                 }
    3869             : 
    3870             :                                 //get actual number of future pictures stored
    3871             :                                 for(pic_i = num_past_pics + 1; pic_i < ALTREF_MAX_NFRAMES; pic_i++) {
    3872             :                                     if(picture_control_set_ptr->temp_filt_pcs_list[pic_i] != NULL)
    3873             :                                         actual_future_pics++;
    3874             :                                 }
    3875             : 
    3876             :                                 actual_past_pics = actual_future_pics;
    3877             :                                 actual_past_pics += (altref_nframes + 1) & 0x1;
    3878             : #endif
    3879           6 :                                 int index_center = (uint8_t)(picture_control_set_ptr->sequence_control_set_ptr->static_config.altref_nframes / 2);
    3880             :                                 int pic_itr;
    3881             :                                 int ahd;
    3882             : 
    3883           6 :                                 int ahd_th = (((sequence_control_set_ptr->seq_header.max_frame_width * sequence_control_set_ptr->seq_header.max_frame_height) * AHD_TH_WEIGHT) / 100);
    3884             : 
    3885             :                                 // Accumulative histogram absolute differences between the central and past frame
    3886             : #if FIX_ALTREF
    3887           6 :                                 for (pic_itr = index_center - actual_past_pics; pic_itr < index_center; pic_itr++) {
    3888             : #else
    3889             :                                 for (pic_itr = index_center - actual_past_pics; pic_itr < index_center - 1; pic_itr++) {
    3890             : #endif
    3891           6 :                                     ahd = compute_luma_sad_between_center_and_target_frame(index_center, pic_itr, picture_control_set_ptr, sequence_control_set_ptr);
    3892             : 
    3893           6 :                                     if (ahd < ahd_th)
    3894           6 :                                         break;
    3895             :                                 }
    3896           6 :                                 picture_control_set_ptr->past_altref_nframes = actual_past_pics = index_center - pic_itr;
    3897             : 
    3898             :                                 // Accumulative histogram absolute differences between the central and past frame
    3899           6 :                                 for (pic_itr = (index_center + actual_future_pics); pic_itr > index_center; pic_itr--) {
    3900           6 :                                     ahd = compute_luma_sad_between_center_and_target_frame(index_center, pic_itr, picture_control_set_ptr, sequence_control_set_ptr);
    3901           6 :                                     if (ahd < ahd_th)
    3902           6 :                                         break;
    3903             :                                 }
    3904           6 :                                 picture_control_set_ptr->future_altref_nframes = pic_itr - index_center;
    3905             :                                 //printf("\nPOC %d\t PAST %d\t FUTURE %d\n", picture_control_set_ptr->picture_number, picture_control_set_ptr->past_altref_nframes, picture_control_set_ptr->future_altref_nframes);
    3906             : 
    3907             :                                 // adjust the temporal filtering pcs buffer to remove unused past pictures
    3908           6 :                                 if(actual_past_pics != num_past_pics) {
    3909             : 
    3910           0 :                                     pic_i = 0;
    3911           0 :                                     while (picture_control_set_ptr->temp_filt_pcs_list[pic_i] != NULL){
    3912           0 :                                         picture_control_set_ptr->temp_filt_pcs_list[pic_i] = picture_control_set_ptr->temp_filt_pcs_list[pic_i + num_past_pics - actual_past_pics];
    3913           0 :                                         pic_i++;
    3914             :                                     }
    3915             : 
    3916             :                                 }
    3917             :                                 }
    3918             : 
    3919           8 :                                 picture_control_set_ptr->temp_filt_prep_done = 0;
    3920             : 
    3921             :                                 // Start Filtering in ME processes
    3922             :                                 {
    3923             :                                     int16_t seg_idx;
    3924             : 
    3925             :                                     // Initialize Segments
    3926           8 :                                     picture_control_set_ptr->tf_segments_column_count = sequence_control_set_ptr->tf_segment_column_count;
    3927           8 :                                     picture_control_set_ptr->tf_segments_row_count    = sequence_control_set_ptr->tf_segment_row_count;
    3928           8 :                                     picture_control_set_ptr->tf_segments_total_count = (uint16_t)(picture_control_set_ptr->tf_segments_column_count  * picture_control_set_ptr->tf_segments_row_count);
    3929             : 
    3930           8 :                                     picture_control_set_ptr->temp_filt_seg_acc = 0;
    3931             : #if  TWO_PASS
    3932           8 :                                     if (picture_control_set_ptr->temporal_layer_index == 0)
    3933           8 :                                         picture_control_set_ptr->altref_strength = sequence_control_set_ptr->static_config.altref_strength;
    3934             :                                     else
    3935           0 :                                         picture_control_set_ptr->altref_strength = 2;
    3936             : #else
    3937             :                                     picture_control_set_ptr->altref_strength = sequence_control_set_ptr->static_config.altref_strength;
    3938             : #endif
    3939             : 
    3940         488 :                                     for (seg_idx = 0; seg_idx < picture_control_set_ptr->tf_segments_total_count; ++seg_idx) {
    3941         480 :                                         eb_get_empty_object(
    3942             :                                             context_ptr->picture_decision_results_output_fifo_ptr,
    3943             :                                             &outputResultsWrapperPtr);
    3944         480 :                                         outputResultsPtr = (PictureDecisionResults*)outputResultsWrapperPtr->object_ptr;
    3945         480 :                                         outputResultsPtr->picture_control_set_wrapper_ptr = encode_context_ptr->pre_assignment_buffer[pictureIndex];
    3946         480 :                                         outputResultsPtr->segment_index = seg_idx;
    3947         480 :                                         outputResultsPtr->task_type = 1;
    3948         480 :                                         eb_post_full_object(outputResultsWrapperPtr);
    3949             :                                     }
    3950             : 
    3951           8 :                                     eb_block_on_semaphore(picture_control_set_ptr->temp_filt_done_semaphore);
    3952             :                                 }
    3953             : 
    3954             :                             }else
    3955         112 :                                 picture_control_set_ptr->temporal_filtering_on = EB_FALSE; // set temporal filtering flag OFF for current picture
    3956             : 
    3957             :                         }
    3958             : 
    3959             :                         // Add 1 to the loop for the overlay picture. If the last picture is alt ref, increase the loop by 1 to add the overlay picture
    3960          12 :                         uint32_t has_overlay = ((PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[context_ptr->mini_gop_end_index[mini_gop_index]]->object_ptr)->is_alt_ref ? 1 : 0;
    3961         132 :                         for (pictureIndex = context_ptr->mini_gop_start_index[mini_gop_index]; pictureIndex <= context_ptr->mini_gop_end_index[mini_gop_index] + has_overlay; ++pictureIndex) {
    3962             :                             // 2nd Loop over Pictures in the Pre-Assignment Buffer
    3963             :                             // Assign the overlay pcs. Since Overlay picture is not added to the picture_decision_pa_reference_queue, in the next stage, the loop finds the alt_ref picture. The reference for overlay frame is hardcoded later
    3964         120 :                             if (has_overlay && pictureIndex == context_ptr->mini_gop_end_index[mini_gop_index] + has_overlay) {
    3965           0 :                                 picture_control_set_ptr = ((PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[context_ptr->mini_gop_end_index[mini_gop_index]]->object_ptr)->overlay_ppcs_ptr;
    3966           0 :                                 frm_hdr = &picture_control_set_ptr->frm_hdr;
    3967             :                             }
    3968             :                             else {
    3969         120 :                                 picture_control_set_ptr = (PictureParentControlSet*)encode_context_ptr->pre_assignment_buffer[pictureIndex]->object_ptr;
    3970         120 :                                 frm_hdr = &picture_control_set_ptr->frm_hdr;
    3971             :                             }
    3972         120 :                             picture_control_set_ptr->picture_number_alt = encode_context_ptr->picture_number_alt++;
    3973             : 
    3974             :                             // Set the Decode Order
    3975         120 :                             if ((context_ptr->mini_gop_idr_count[mini_gop_index] == 0) &&
    3976         118 :                                 (context_ptr->mini_gop_length[mini_gop_index] == picture_control_set_ptr->pred_struct_ptr->pred_struct_period) && !picture_control_set_ptr->is_overlay){
    3977         112 :                                 picture_control_set_ptr->decode_order = encode_context_ptr->decode_base_number + picture_control_set_ptr->pred_struct_ptr->pred_struct_entry_ptr_array[picture_control_set_ptr->pred_struct_index]->decode_order;
    3978             :                             }
    3979             :                             else
    3980           8 :                                 picture_control_set_ptr->decode_order = picture_control_set_ptr->picture_number_alt;
    3981         120 :                             encode_context_ptr->terminating_sequence_flag_received = (picture_control_set_ptr->end_of_sequence_flag == EB_TRUE) ?
    3982             :                                 EB_TRUE :
    3983             :                                 encode_context_ptr->terminating_sequence_flag_received;
    3984             : 
    3985         240 :                             encode_context_ptr->terminating_picture_number = (picture_control_set_ptr->end_of_sequence_flag == EB_TRUE) ?
    3986         120 :                                 picture_control_set_ptr->picture_number_alt :
    3987             :                                 encode_context_ptr->terminating_picture_number;
    3988             : 
    3989             :                             // Find the Reference in the Picture Decision PA Reference Queue
    3990         120 :                             inputQueueIndex = encode_context_ptr->picture_decision_pa_reference_queue_head_index;
    3991             : 
    3992             :                             do {
    3993             :                                 // Setup the Picture Decision PA Reference Queue Entry
    3994        1932 :                                 inputEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[inputQueueIndex];
    3995             : 
    3996             :                                 // Increment the referenceQueueIndex Iterator
    3997        1932 :                                 inputQueueIndex = (inputQueueIndex == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : inputQueueIndex + 1;
    3998        1932 :                             } while ((inputQueueIndex != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) && (inputEntryPtr->picture_number != picture_control_set_ptr->picture_number));
    3999             : 
    4000         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_0], 0, REF_LIST_MAX_DEPTH * sizeof(EbObjectWrapper*));
    4001         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_1], 0, REF_LIST_MAX_DEPTH * sizeof(EbObjectWrapper*));
    4002         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pic_poc_array[REF_LIST_0], 0, REF_LIST_MAX_DEPTH * sizeof(uint64_t));
    4003         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pic_poc_array[REF_LIST_1], 0, REF_LIST_MAX_DEPTH * sizeof(uint64_t));
    4004         120 :                             CHECK_REPORT_ERROR(
    4005             :                                 (inputEntryPtr->picture_number == picture_control_set_ptr->picture_number),
    4006             :                                 encode_context_ptr->app_callback_ptr,
    4007             :                                 EB_ENC_PD_ERROR7);
    4008             : 
    4009             :                             // Reset the PA Reference Lists
    4010         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pa_pic_ptr_array, 0, 2 * sizeof(EbObjectWrapper*));
    4011             : 
    4012         120 :                             EB_MEMSET(picture_control_set_ptr->ref_pic_poc_array, 0, 2 * sizeof(uint64_t));
    4013             : 
    4014             :                             // Configure List0
    4015         120 :                             if ((picture_control_set_ptr->slice_type == P_SLICE) || (picture_control_set_ptr->slice_type == B_SLICE)) {
    4016             :                                 uint8_t ref_pic_index;
    4017         348 :                                 for (ref_pic_index = 0; ref_pic_index < picture_control_set_ptr->ref_list0_count; ++ref_pic_index) {
    4018         232 :                                     if (picture_control_set_ptr->ref_list0_count) {
    4019             :                                         // hardcode the reference for the overlay frame
    4020         232 :                                         if(picture_control_set_ptr->is_overlay){
    4021           0 :                                             paReferenceQueueIndex = (uint32_t)CIRCULAR_ADD(
    4022             :                                                 (int32_t)inputEntryPtr->reference_entry_index,
    4023             :                                                 PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH);                                                                                             // Max
    4024             : 
    4025           0 :                                             paReferenceEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[paReferenceQueueIndex];
    4026             : 
    4027             :                                             // Calculate the Ref POC
    4028           0 :                                             ref_poc = POC_CIRCULAR_ADD(
    4029             :                                                 picture_control_set_ptr->picture_number,
    4030             :                                                 0);
    4031             :                                         }
    4032             :                                         else {
    4033         232 :                                             paReferenceQueueIndex = (uint32_t)CIRCULAR_ADD(
    4034             :                                                 ((int32_t)inputEntryPtr->reference_entry_index) - inputEntryPtr->list0_ptr->reference_list[ref_pic_index],
    4035             :                                                 PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH);                                                                                             // Max
    4036             : 
    4037         232 :                                             paReferenceEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[paReferenceQueueIndex];
    4038             : 
    4039             :                                             // Calculate the Ref POC
    4040         232 :                                             ref_poc = POC_CIRCULAR_ADD(
    4041             :                                                 picture_control_set_ptr->picture_number,
    4042             :                                                 -inputEntryPtr->list0_ptr->reference_list[ref_pic_index] /*
    4043             :                                                 sequence_control_set_ptr->bits_for_picture_order_count*/);
    4044             :                                         }
    4045             :                                             // Set the Reference Object
    4046         232 :                                         picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_0][ref_pic_index] = paReferenceEntryPtr->input_object_ptr;
    4047         232 :                                         picture_control_set_ptr->ref_pic_poc_array[REF_LIST_0][ref_pic_index] = ref_poc;
    4048             :                                         // Increment the PA Reference's liveCount by the number of tiles in the input picture
    4049         232 :                                         eb_object_inc_live_count(
    4050             :                                             paReferenceEntryPtr->input_object_ptr,
    4051             :                                             1);
    4052         232 :                                         --paReferenceEntryPtr->dependent_count;
    4053             :                                     }
    4054             :                                 }
    4055             :                             }
    4056             : 
    4057             :                             // Configure List1
    4058         120 :                             if (picture_control_set_ptr->slice_type == B_SLICE) {
    4059             :                                 uint8_t ref_pic_index;
    4060         273 :                                 for (ref_pic_index = 0; ref_pic_index < picture_control_set_ptr->ref_list1_count; ++ref_pic_index) {
    4061         163 :                                     if (picture_control_set_ptr->ref_list1_count) {
    4062         163 :                                         paReferenceQueueIndex = (uint32_t)CIRCULAR_ADD(
    4063             :                                             ((int32_t)inputEntryPtr->reference_entry_index) - inputEntryPtr->list1_ptr->reference_list[ref_pic_index],
    4064             :                                             PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH);                                                                                             // Max
    4065             : 
    4066         163 :                                         paReferenceEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[paReferenceQueueIndex];
    4067             : 
    4068             :                                         // Calculate the Ref POC
    4069         163 :                                         ref_poc = POC_CIRCULAR_ADD(
    4070             :                                             picture_control_set_ptr->picture_number,
    4071             :                                             -inputEntryPtr->list1_ptr->reference_list[ref_pic_index]/*,
    4072             :                                             sequence_control_set_ptr->bits_for_picture_order_count*/);
    4073             :                                         // Set the Reference Object
    4074         163 :                                         picture_control_set_ptr->ref_pa_pic_ptr_array[REF_LIST_1][ref_pic_index] = paReferenceEntryPtr->input_object_ptr;
    4075         163 :                                         picture_control_set_ptr->ref_pic_poc_array[REF_LIST_1][ref_pic_index] = ref_poc;
    4076             : 
    4077             :                                         // Increment the PA Reference's liveCount by the number of tiles in the input picture
    4078         163 :                                         eb_object_inc_live_count(
    4079             :                                             paReferenceEntryPtr->input_object_ptr,
    4080             :                                             1);
    4081         163 :                                         --paReferenceEntryPtr->dependent_count;
    4082             :                                     }
    4083             :                                 }
    4084             :                             }
    4085             : 
    4086         120 :                             eb_av1_setup_skip_mode_allowed(picture_control_set_ptr);
    4087             : 
    4088         120 :                             picture_control_set_ptr->is_skip_mode_allowed = frm_hdr->skip_mode_params.skip_mode_allowed;
    4089         120 :                             picture_control_set_ptr->skip_mode_flag = picture_control_set_ptr->is_skip_mode_allowed;
    4090             :                             //printf("POC:%i  skip_mode_allowed:%i  REF_SKIP_0: %i   REF_SKIP_1: %i \n",picture_control_set_ptr->picture_number, picture_control_set_ptr->skip_mode_info.skip_mode_allowed, picture_control_set_ptr->skip_mode_info.ref_frame_idx_0, picture_control_set_ptr->skip_mode_info.ref_frame_idx_1);
    4091             : 
    4092             :                             {
    4093         120 :                                 picture_control_set_ptr->intensity_transition_flag = EB_FALSE;
    4094         120 :                                 if (picture_control_set_ptr->ref_list0_count)
    4095         116 :                                     picture_control_set_ptr->scene_transition_flag[REF_LIST_0] = EB_FALSE;
    4096         120 :                                 if (picture_control_set_ptr->ref_list1_count)
    4097         110 :                                     picture_control_set_ptr->scene_transition_flag[REF_LIST_1] = EB_FALSE;
    4098             :                             }
    4099             : 
    4100             :                             //set the ref frame types used for this picture,
    4101         120 :                             set_all_ref_frame_type(sequence_control_set_ptr, picture_control_set_ptr, picture_control_set_ptr->ref_frame_type_arr, &picture_control_set_ptr->tot_ref_frame_types);
    4102             :                             // Initialize Segments
    4103         120 :                             picture_control_set_ptr->me_segments_column_count = (uint8_t)(sequence_control_set_ptr->me_segment_column_count_array[picture_control_set_ptr->temporal_layer_index]);
    4104         120 :                             picture_control_set_ptr->me_segments_row_count = (uint8_t)(sequence_control_set_ptr->me_segment_row_count_array[picture_control_set_ptr->temporal_layer_index]);
    4105         120 :                             picture_control_set_ptr->me_segments_total_count = (uint16_t)(picture_control_set_ptr->me_segments_column_count  * picture_control_set_ptr->me_segments_row_count);
    4106         120 :                             picture_control_set_ptr->me_segments_completion_mask = 0;
    4107             : 
    4108             :                             // Post the results to the ME processes
    4109             :                             {
    4110             :                                 uint32_t segment_index;
    4111             : 
    4112        7320 :                                 for (segment_index = 0; segment_index < picture_control_set_ptr->me_segments_total_count; ++segment_index) {
    4113             :                                     // Get Empty Results Object
    4114        7200 :                                     eb_get_empty_object(
    4115             :                                             context_ptr->picture_decision_results_output_fifo_ptr,
    4116             :                                             &outputResultsWrapperPtr);
    4117             : 
    4118        7200 :                                     outputResultsPtr = (PictureDecisionResults*)outputResultsWrapperPtr->object_ptr;
    4119        7200 :                                     if (picture_control_set_ptr->is_overlay)
    4120           0 :                                         outputResultsPtr->picture_control_set_wrapper_ptr = picture_control_set_ptr->p_pcs_wrapper_ptr;
    4121             :                                     else
    4122        7200 :                                         outputResultsPtr->picture_control_set_wrapper_ptr = encode_context_ptr->pre_assignment_buffer[pictureIndex];
    4123             : 
    4124        7200 :                                     outputResultsPtr->segment_index = segment_index;
    4125        7200 :                                     outputResultsPtr->task_type = 0;
    4126             :                                     // Post the Full Results Object
    4127        7200 :                                     eb_post_full_object(outputResultsWrapperPtr);
    4128             :                                 }
    4129             :                             }
    4130             : 
    4131         120 :                             if (pictureIndex == context_ptr->mini_gop_end_index[mini_gop_index] + has_overlay) {
    4132             :                                 // Increment the Decode Base Number
    4133          12 :                                 encode_context_ptr->decode_base_number += context_ptr->mini_gop_length[mini_gop_index] + has_overlay;
    4134             :                             }
    4135         120 :                             if (pictureIndex == encode_context_ptr->pre_assignment_buffer_count - 1 + has_overlay) {
    4136             : 
    4137             :                                 // Reset the Pre-Assignment Buffer
    4138          10 :                                 encode_context_ptr->pre_assignment_buffer_count = 0;
    4139          10 :                                 encode_context_ptr->pre_assignment_buffer_idr_count = 0;
    4140          10 :                                 encode_context_ptr->pre_assignment_buffer_intra_count = 0;
    4141          10 :                                 encode_context_ptr->pre_assignment_buffer_scene_change_count = 0;
    4142          10 :                                 encode_context_ptr->pre_assignment_buffer_eos_flag = EB_FALSE;
    4143             :                             }
    4144             :                         }
    4145             :                     } // End MINI GOPs loop
    4146             :                 }
    4147             : 
    4148             :                 // Walk the picture_decision_pa_reference_queue and remove entries that have been completely referenced.
    4149         120 :                 inputQueueIndex = encode_context_ptr->picture_decision_pa_reference_queue_head_index;
    4150             : 
    4151        1222 :                 while (inputQueueIndex != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) {
    4152        1102 :                     inputEntryPtr = encode_context_ptr->picture_decision_pa_reference_queue[inputQueueIndex];
    4153             : 
    4154             :                     // Remove the entry
    4155        1102 :                     if ((inputEntryPtr->dependent_count == 0) &&
    4156         816 :                         (inputEntryPtr->input_object_ptr)) {
    4157             :                         // Release the nominal live_count value
    4158         114 :                         eb_release_object(inputEntryPtr->input_object_ptr);
    4159         114 :                         inputEntryPtr->input_object_ptr = (EbObjectWrapper*)EB_NULL;
    4160             :                     }
    4161             : 
    4162             :                     // Increment the head_index if the head is null
    4163        1102 :                     encode_context_ptr->picture_decision_pa_reference_queue_head_index =
    4164        1206 :                         (encode_context_ptr->picture_decision_pa_reference_queue[encode_context_ptr->picture_decision_pa_reference_queue_head_index]->input_object_ptr) ? encode_context_ptr->picture_decision_pa_reference_queue_head_index :
    4165         104 :                         (encode_context_ptr->picture_decision_pa_reference_queue_head_index == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0
    4166         104 :                         : encode_context_ptr->picture_decision_pa_reference_queue_head_index + 1;
    4167             : 
    4168        1102 :                     CHECK_REPORT_ERROR(
    4169             :                         (((encode_context_ptr->picture_decision_pa_reference_queue_head_index != encode_context_ptr->picture_decision_pa_reference_queue_tail_index) ||
    4170             :                         (encode_context_ptr->picture_decision_pa_reference_queue[encode_context_ptr->picture_decision_pa_reference_queue_head_index]->input_object_ptr == EB_NULL))),
    4171             :                         encode_context_ptr->app_callback_ptr,
    4172             :                         EB_ENC_PD_ERROR4);
    4173             : 
    4174             :                     // Increment the inputQueueIndex Iterator
    4175        1102 :                     inputQueueIndex = (inputQueueIndex == PICTURE_DECISION_PA_REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : inputQueueIndex + 1;
    4176             :                 }
    4177             : 
    4178             :                 // Increment the Picture Decision Reordering Queue Head Ptr
    4179         120 :                 encode_context_ptr->picture_decision_reorder_queue_head_index = (encode_context_ptr->picture_decision_reorder_queue_head_index == PICTURE_DECISION_REORDER_QUEUE_MAX_DEPTH - 1) ? 0 : encode_context_ptr->picture_decision_reorder_queue_head_index + 1;
    4180             : 
    4181             :                 // Get the next entry from the Picture Decision Reordering Queue (Entry N+1)
    4182         120 :                 queueEntryPtr = encode_context_ptr->picture_decision_reorder_queue[encode_context_ptr->picture_decision_reorder_queue_head_index];
    4183             :             }
    4184         234 :             if (windowAvail == EB_FALSE && framePasseThru == EB_FALSE)
    4185         114 :                 break;
    4186             :         }
    4187             : 
    4188             :         // Release the Input Results
    4189         120 :         eb_release_object(inputResultsWrapperPtr);
    4190             :     }
    4191             : 
    4192             :     return EB_NULL;
    4193             : }

Generated by: LCOV version 1.14