LCOV - code coverage report
Current view: top level - Codec - EbEncDecProcess.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 387 850 45.5 %
Date: 2019-11-25 17:38:06 Functions: 11 14 78.6 %

          Line data    Source code
       1             : /*
       2             : * Copyright(c) 2019 Intel Corporation
       3             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       4             : */
       5             : 
       6             : /*
       7             : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       8             : *
       9             : * This source code is subject to the terms of the BSD 2 Clause License and
      10             : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      11             : * was not distributed with this source code in the LICENSE file, you can
      12             : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      13             : * Media Patent License 1.0 was not distributed with this source code in the
      14             : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      15             : */
      16             : 
      17             : #include <stdlib.h>
      18             : 
      19             : #include "EbEncDecTasks.h"
      20             : #include "EbEncDecResults.h"
      21             : #include "EbDefinitions.h"
      22             : #include "EbCodingLoop.h"
      23             : #include "EbSvtAv1ErrorCodes.h"
      24             : #include "EbUtility.h"
      25             : #include "grainSynthesis.h"
      26             : 
      27             : void eb_av1_cdef_search(
      28             :     EncDecContext                *context_ptr,
      29             :     SequenceControlSet           *sequence_control_set_ptr,
      30             :     PictureControlSet            *picture_control_set_ptr
      31             : );
      32             : 
      33             : void eb_av1_cdef_frame(
      34             :     EncDecContext                *context_ptr,
      35             :     SequenceControlSet           *sequence_control_set_ptr,
      36             :     PictureControlSet            *pCs
      37             : );
      38             : 
      39             : void av1_cdef_search16bit(
      40             :     EncDecContext                *context_ptr,
      41             :     SequenceControlSet           *sequence_control_set_ptr,
      42             :     PictureControlSet            *picture_control_set_ptr
      43             : );
      44             : void av1_cdef_frame16bit(
      45             :     uint8_t is16bit,
      46             :     SequenceControlSet           *sequence_control_set_ptr,
      47             :     PictureControlSet            *pCs
      48             : );
      49             : 
      50             : void eb_av1_add_film_grain(EbPictureBufferDesc *src,
      51             :     EbPictureBufferDesc *dst,
      52             :     aom_film_grain_t *film_grain_ptr);
      53             : 
      54             : void eb_av1_loop_restoration_save_boundary_lines(const Yv12BufferConfig *frame, Av1Common *cm, int32_t after_cdef);
      55             : void eb_av1_pick_filter_restoration(const Yv12BufferConfig *src, Yv12BufferConfig * trial_frame_rst /*Av1Comp *cpi*/, Macroblock *x, Av1Common *const cm);
      56             : void eb_av1_loop_restoration_filter_frame(Yv12BufferConfig *frame, Av1Common *cm, int32_t optimized_lr);
      57             : 
      58             : const int16_t encMinDeltaQpWeightTab[MAX_TEMPORAL_LAYERS] = { 100, 100, 100, 100, 100, 100 };
      59             : const int16_t encMaxDeltaQpWeightTab[MAX_TEMPORAL_LAYERS] = { 100, 100, 100, 100, 100, 100 };
      60             : 
      61             : const int8_t  encMinDeltaQpISliceTab[4] = { -5, -5, -3, -2 };
      62             : 
      63             : const int8_t  encMinDeltaQpTab[4][MAX_TEMPORAL_LAYERS] = {
      64             :     { -4, -2, -2, -1, -1, -1 },
      65             :     { -4, -2, -2, -1, -1, -1 },
      66             :     { -3, -1, -1, -1, -1, -1 },
      67             :     { -1, -0, -0, -0, -0, -0 },
      68             : };
      69             : 
      70             : const int8_t  encMaxDeltaQpTab[4][MAX_TEMPORAL_LAYERS] = {
      71             :     { 4, 5, 5, 5, 5, 5 },
      72             :     { 4, 5, 5, 5, 5, 5 },
      73             :     { 4, 5, 5, 5, 5, 5 },
      74             :     { 4, 5, 5, 5, 5, 5 }
      75             : };
      76             : 
      77          16 : static void enc_dec_context_dctor(EbPtr p)
      78             : {
      79          16 :     EncDecContext* obj = (EncDecContext*)p;
      80          16 :     EB_DELETE(obj->md_context);
      81          16 :     EB_DELETE(obj->residual_buffer);
      82          16 :     EB_DELETE(obj->transform_buffer);
      83          16 :     EB_DELETE(obj->inverse_quant_buffer);
      84          16 :     EB_DELETE(obj->input_sample16bit_buffer);
      85          16 :     if (obj->is_md_rate_estimation_ptr_owner)
      86           0 :         EB_FREE(obj->md_rate_estimation_ptr);
      87          16 :     EB_FREE_ARRAY(obj->transform_inner_array_ptr);
      88          16 : }
      89             : 
      90             : /******************************************************
      91             :  * Enc Dec Context Constructor
      92             :  ******************************************************/
      93          16 : EbErrorType enc_dec_context_ctor(
      94             :     EncDecContext         *context_ptr,
      95             :     EbFifo                *mode_decision_configuration_input_fifo_ptr,
      96             :     EbFifo                *packetization_output_fifo_ptr,
      97             :     EbFifo                *feedback_fifo_ptr,
      98             :     EbFifo                *picture_demux_fifo_ptr,
      99             : #if PAL_SUP
     100             :     uint8_t                 cfg_palette,
     101             : #endif
     102             :     EbBool                  is16bit,
     103             :     EbColorFormat           color_format,
     104             :     uint8_t                 enable_hbd_mode_decision,
     105             :     uint32_t                max_input_luma_width,
     106             :     uint32_t                max_input_luma_height)
     107             : {
     108             :     (void)max_input_luma_width;
     109             :     (void)max_input_luma_height;
     110             : 
     111          16 :     context_ptr->dctor = enc_dec_context_dctor;
     112          16 :     context_ptr->is16bit = is16bit;
     113          16 :     context_ptr->color_format = color_format;
     114             : 
     115             :     // Input/Output System Resource Manager FIFOs
     116          16 :     context_ptr->mode_decision_input_fifo_ptr = mode_decision_configuration_input_fifo_ptr;
     117          16 :     context_ptr->enc_dec_output_fifo_ptr = packetization_output_fifo_ptr;
     118          16 :     context_ptr->enc_dec_feedback_fifo_ptr = feedback_fifo_ptr;
     119          16 :     context_ptr->picture_demux_output_fifo_ptr = picture_demux_fifo_ptr;
     120             : 
     121             :     // Trasform Scratch Memory
     122          16 :     EB_MALLOC_ARRAY(context_ptr->transform_inner_array_ptr, 3152); //refer to EbInvTransform_SSE2.as. case 32x32
     123             :     // MD rate Estimation tables
     124          16 :     EB_MALLOC(context_ptr->md_rate_estimation_ptr, sizeof(MdRateEstimationContext));
     125          16 :     context_ptr->is_md_rate_estimation_ptr_owner = EB_TRUE;
     126             : 
     127             :     // Prediction Buffer
     128             :     {
     129             :         EbPictureBufferDescInitData initData;
     130             : 
     131          16 :         initData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     132          16 :         initData.max_width = SB_STRIDE_Y;
     133          16 :         initData.max_height = SB_STRIDE_Y;
     134          16 :         initData.bit_depth = EB_8BIT;
     135          16 :         initData.left_padding = 0;
     136          16 :         initData.right_padding = 0;
     137          16 :         initData.top_padding = 0;
     138          16 :         initData.bot_padding = 0;
     139          16 :         initData.split_mode = EB_FALSE;
     140          16 :         initData.color_format = color_format;
     141             : 
     142          16 :         context_ptr->input_sample16bit_buffer = (EbPictureBufferDesc *)EB_NULL;
     143          16 :         if (is16bit) {
     144           0 :             initData.bit_depth = EB_16BIT;
     145             : 
     146           0 :             EB_NEW(
     147             :                 context_ptr->input_sample16bit_buffer,
     148             :                 eb_picture_buffer_desc_ctor,
     149             :                 (EbPtr)&initData);
     150             :         }
     151             :     }
     152             : 
     153             :     // Scratch Coeff Buffer
     154             :     {
     155             :         EbPictureBufferDescInitData initData;
     156             : 
     157          16 :         initData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     158          16 :         initData.max_width = SB_STRIDE_Y;
     159          16 :         initData.max_height = SB_STRIDE_Y;
     160          16 :         initData.bit_depth = EB_16BIT;
     161          16 :         initData.color_format = color_format;
     162          16 :         initData.left_padding = 0;
     163          16 :         initData.right_padding = 0;
     164          16 :         initData.top_padding = 0;
     165          16 :         initData.bot_padding = 0;
     166          16 :         initData.split_mode = EB_FALSE;
     167             : 
     168             :         EbPictureBufferDescInitData init32BitData;
     169             : 
     170          16 :         init32BitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     171          16 :         init32BitData.max_width = SB_STRIDE_Y;
     172          16 :         init32BitData.max_height = SB_STRIDE_Y;
     173          16 :         init32BitData.bit_depth = EB_32BIT;
     174          16 :         init32BitData.color_format = color_format;
     175          16 :         init32BitData.left_padding = 0;
     176          16 :         init32BitData.right_padding = 0;
     177          16 :         init32BitData.top_padding = 0;
     178          16 :         init32BitData.bot_padding = 0;
     179          16 :         init32BitData.split_mode = EB_FALSE;
     180          16 :         EB_NEW(
     181             :             context_ptr->inverse_quant_buffer,
     182             :             eb_picture_buffer_desc_ctor,
     183             :             (EbPtr)&init32BitData);
     184          16 :         EB_NEW(
     185             :             context_ptr->transform_buffer,
     186             :             eb_picture_buffer_desc_ctor,
     187             :             (EbPtr)&init32BitData);
     188          16 :         EB_NEW(
     189             :             context_ptr->residual_buffer,
     190             :             eb_picture_buffer_desc_ctor,
     191             :             (EbPtr)&initData);
     192             :     }
     193             : 
     194             :     // Mode Decision Context
     195             :  #if PAL_SUP
     196          16 :     EB_NEW(
     197             :         context_ptr->md_context,
     198             :         mode_decision_context_ctor,
     199             :         color_format, 0, 0, enable_hbd_mode_decision , cfg_palette );
     200             : #else
     201             :     EB_NEW(
     202             :         context_ptr->md_context,
     203             :         mode_decision_context_ctor,
     204             :         color_format, 0, 0, enable_hbd_mode_decision);
     205             : #endif
     206          16 :     if (enable_hbd_mode_decision)
     207           0 :         context_ptr->md_context->input_sample16bit_buffer = context_ptr->input_sample16bit_buffer;
     208             : 
     209          16 :     context_ptr->md_context->enc_dec_context_ptr = context_ptr;
     210             : 
     211          16 :     return EB_ErrorNone;
     212             : }
     213             : 
     214             : /**************************************************
     215             :  * Reset Segmentation Map
     216             :  *************************************************/
     217         120 : static void reset_segmentation_map(SegmentationNeighborMap *segmentation_map){
     218         120 :     if(segmentation_map->data!=NULL)
     219         120 :         EB_MEMSET(segmentation_map->data, ~0, segmentation_map->map_size);
     220         120 : }
     221             : 
     222             : /**************************************************
     223             :  * Reset Mode Decision Neighbor Arrays
     224             :  *************************************************/
     225         120 : static void ResetEncodePassNeighborArrays(PictureControlSet *picture_control_set_ptr)
     226             : {
     227         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_intra_luma_mode_neighbor_array);
     228         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_intra_chroma_mode_neighbor_array);
     229         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_mv_neighbor_array);
     230         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_skip_flag_neighbor_array);
     231         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_mode_type_neighbor_array);
     232         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_leaf_depth_neighbor_array);
     233         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_luma_recon_neighbor_array);
     234         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_cb_recon_neighbor_array);
     235         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_cr_recon_neighbor_array);
     236         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_luma_dc_sign_level_coeff_neighbor_array);
     237         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_cb_dc_sign_level_coeff_neighbor_array);
     238         120 :     neighbor_array_unit_reset(picture_control_set_ptr->ep_cr_dc_sign_level_coeff_neighbor_array);
     239             :     // TODO(Joel): 8-bit ep_luma_recon_neighbor_array (Cb,Cr) when is16bit==0?
     240         120 :     EbBool is16bit = (EbBool)(picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
     241         120 :     if (is16bit) {
     242           0 :         neighbor_array_unit_reset(picture_control_set_ptr->ep_luma_recon_neighbor_array16bit);
     243           0 :         neighbor_array_unit_reset(picture_control_set_ptr->ep_cb_recon_neighbor_array16bit);
     244           0 :         neighbor_array_unit_reset(picture_control_set_ptr->ep_cr_recon_neighbor_array16bit);
     245             :     }
     246         120 :     return;
     247             : }
     248             : 
     249             : /**************************************************
     250             :  * Reset Coding Loop
     251             :  **************************************************/
     252        7197 : static void ResetEncDec(
     253             :     EncDecContext         *context_ptr,
     254             :     PictureControlSet     *picture_control_set_ptr,
     255             :     SequenceControlSet    *sequence_control_set_ptr,
     256             :     uint32_t                   segment_index)
     257             : {
     258        7197 :     context_ptr->is16bit = (EbBool)(sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
     259             : 
     260             : #if ADD_DELTA_QP_SUPPORT
     261        7197 :     uint16_t picture_qp = picture_control_set_ptr->picture_qp;
     262        7197 :     context_ptr->qp = picture_qp;
     263        7197 :     context_ptr->qp_index = picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present ? (uint8_t)quantizer_to_qindex[context_ptr->qp] : (uint8_t)picture_control_set_ptr->parent_pcs_ptr->frm_hdr.quantization_params.base_q_idx;
     264             : #else
     265             :     context_ptr->qp = picture_control_set_ptr->picture_qp;
     266             : #endif
     267             :     // Asuming cb and cr offset to be the same for chroma QP in both slice and pps for lambda computation
     268             : 
     269        7197 :     context_ptr->chroma_qp = (uint8_t)context_ptr->qp;
     270             : 
     271             :     // Lambda Assignement
     272        7197 :     context_ptr->qp_index = (uint8_t)picture_control_set_ptr->
     273        7197 :         parent_pcs_ptr->frm_hdr.quantization_params.base_q_idx;
     274        7197 :     (*av1_lambda_assignment_function_table[picture_control_set_ptr->parent_pcs_ptr->pred_structure])(
     275             :         &context_ptr->fast_lambda,
     276             :         &context_ptr->full_lambda,
     277             :         &context_ptr->fast_chroma_lambda,
     278             :         &context_ptr->full_chroma_lambda,
     279        7197 :         (uint8_t)picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr->bit_depth,
     280        7197 :         context_ptr->qp_index,
     281        7197 :         context_ptr->md_context->hbd_mode_decision);
     282             :     // Reset MD rate Estimation table to initial values by copying from md_rate_estimation_array
     283        7198 :     if (context_ptr->is_md_rate_estimation_ptr_owner) {
     284          16 :         EB_FREE(context_ptr->md_rate_estimation_ptr);
     285          16 :         context_ptr->is_md_rate_estimation_ptr_owner = EB_FALSE;
     286             :     }
     287        7198 :     context_ptr->md_rate_estimation_ptr = picture_control_set_ptr->md_rate_estimation_array;
     288        7198 :     if (segment_index == 0){
     289         120 :         ResetEncodePassNeighborArrays(picture_control_set_ptr);
     290         120 :         reset_segmentation_map(picture_control_set_ptr->segmentation_neighbor_map);
     291             :     }
     292             : 
     293        7198 :     return;
     294             : }
     295             : 
     296             : /******************************************************
     297             :  * EncDec Configure LCU
     298             :  ******************************************************/
     299        7199 : static void EncDecConfigureLcu(
     300             :     EncDecContext         *context_ptr,
     301             :     LargestCodingUnit     *sb_ptr,
     302             :     PictureControlSet     *picture_control_set_ptr,
     303             :     uint8_t                    sb_qp)
     304             : {
     305        7199 :     context_ptr->qp = sb_qp;
     306             : 
     307             :     // Asuming cb and cr offset to be the same for chroma QP in both slice and pps for lambda computation
     308        7199 :     context_ptr->chroma_qp = (uint8_t)context_ptr->qp;
     309             :     /* Note(CHKN) : when Qp modulation varies QP on a sub-LCU(CU) basis,  Lamda has to change based on Cu->QP , and then this code has to move inside the CU loop in MD */
     310             :     (void)sb_ptr;
     311        7199 :     context_ptr->qp_index = (uint8_t)picture_control_set_ptr->
     312        7199 :         parent_pcs_ptr->frm_hdr.quantization_params.base_q_idx;
     313        7199 :     (*av1_lambda_assignment_function_table[picture_control_set_ptr->parent_pcs_ptr->pred_structure])(
     314             :         &context_ptr->fast_lambda,
     315             :         &context_ptr->full_lambda,
     316             :         &context_ptr->fast_chroma_lambda,
     317             :         &context_ptr->full_chroma_lambda,
     318        7199 :         (uint8_t)picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr->bit_depth,
     319        7199 :         context_ptr->qp_index,
     320        7199 :         context_ptr->md_context->hbd_mode_decision);
     321             : 
     322        7199 :     return;
     323             : }
     324             : 
     325             : /******************************************************
     326             :  * Update MD Segments
     327             :  *
     328             :  * This function is responsible for synchronizing the
     329             :  *   processing of MD Segment-rows.
     330             :  *   In short, the function starts processing
     331             :  *   of MD segment-rows as soon as their inputs are available
     332             :  *   and the previous segment-row has completed.  At
     333             :  *   any given time, only one segment row per picture
     334             :  *   is being processed.
     335             :  *
     336             :  * The function has two functions:
     337             :  *
     338             :  * (1) Update the Segment Completion Mask which tracks
     339             :  *   which MD Segment inputs are available.
     340             :  *
     341             :  * (2) Increment the segment-row counter (current_row_idx)
     342             :  *   as the segment-rows are completed.
     343             :  *
     344             :  * Since there is the potentential for thread collusion,
     345             :  *   a MUTEX a used to protect the sensitive data and
     346             :  *   the execution flow is separated into two paths
     347             :  *
     348             :  * (A) Initial update.
     349             :  *  -Update the Completion Mask [see (1) above]
     350             :  *  -If the picture is not currently being processed,
     351             :  *     check to see if the next segment-row is available
     352             :  *     and start processing.
     353             :  * (B) Continued processing
     354             :  *  -Upon the completion of a segment-row, check
     355             :  *     to see if the next segment-row's inputs have
     356             :  *     become available and begin processing if so.
     357             :  *
     358             :  * On last important point is that the thread-safe
     359             :  *   code section is kept minimally short. The MUTEX
     360             :  *   should NOT be locked for the entire processing
     361             :  *   of the segment-row (B) as this would block other
     362             :  *   threads from performing an update (A).
     363             :  ******************************************************/
     364        8621 : EbBool AssignEncDecSegments(
     365             :     EncDecSegments   *segmentPtr,
     366             :     uint16_t             *segmentInOutIndex,
     367             :     EncDecTasks      *taskPtr,
     368             :     EbFifo           *srmFifoPtr)
     369             : {
     370        8621 :     EbBool continueProcessingFlag = EB_FALSE;
     371             :     EbObjectWrapper *wrapper_ptr;
     372             :     EncDecTasks *feedbackTaskPtr;
     373             : 
     374        8621 :     uint32_t rowSegmentIndex = 0;
     375             :     uint32_t segment_index;
     376             :     uint32_t rightSegmentIndex;
     377             :     uint32_t bottomLeftSegmentIndex;
     378             : 
     379        8621 :     int16_t feedbackRowIndex = -1;
     380             : 
     381        8621 :     uint32_t selfAssigned = EB_FALSE;
     382             : 
     383             :     //static FILE *trace = 0;
     384             :     //
     385             :     //if(trace == 0) {
     386             :     //    trace = fopen("seg-trace.txt","w");
     387             :     //}
     388             : 
     389        8621 :     switch (taskPtr->input_type) {
     390         120 :     case ENCDEC_TASKS_MDC_INPUT:
     391             : 
     392             :         // The entire picture is provided by the MDC process, so
     393             :         //   no logic is necessary to clear input dependencies.
     394             : 
     395             :         // Start on Segment 0 immediately
     396         120 :         *segmentInOutIndex = segmentPtr->row_array[0].current_seg_index;
     397         120 :         taskPtr->input_type = ENCDEC_TASKS_CONTINUE;
     398         120 :         ++segmentPtr->row_array[0].current_seg_index;
     399         120 :         continueProcessingFlag = EB_TRUE;
     400             : 
     401             :         //fprintf(trace, "Start  Pic: %u Seg: %u\n",
     402             :         //    (unsigned) ((PictureControlSet*) taskPtr->picture_control_set_wrapper_ptr->object_ptr)->picture_number,
     403             :         //    *segmentInOutIndex);
     404             : 
     405         120 :         break;
     406             : 
     407        1301 :     case ENCDEC_TASKS_ENCDEC_INPUT:
     408             : 
     409             :         // Setup rowSegmentIndex to release the in_progress token
     410             :         //rowSegmentIndex = taskPtr->encDecSegmentRowArray[0];
     411             : 
     412             :         // Start on the assigned row immediately
     413        1301 :         *segmentInOutIndex = segmentPtr->row_array[taskPtr->enc_dec_segment_row].current_seg_index;
     414        1301 :         taskPtr->input_type = ENCDEC_TASKS_CONTINUE;
     415        1301 :         ++segmentPtr->row_array[taskPtr->enc_dec_segment_row].current_seg_index;
     416        1301 :         continueProcessingFlag = EB_TRUE;
     417             : 
     418             :         //fprintf(trace, "Start  Pic: %u Seg: %u\n",
     419             :         //    (unsigned) ((PictureControlSet*) taskPtr->picture_control_set_wrapper_ptr->object_ptr)->picture_number,
     420             :         //    *segmentInOutIndex);
     421             : 
     422        1301 :         break;
     423             : 
     424        7200 :     case ENCDEC_TASKS_CONTINUE:
     425             : 
     426             :         // Update the Dependency List for Right and Bottom Neighbors
     427        7200 :         segment_index = *segmentInOutIndex;
     428        7200 :         rowSegmentIndex = segment_index / segmentPtr->segment_band_count;
     429             : 
     430        7200 :         rightSegmentIndex = segment_index + 1;
     431        7200 :         bottomLeftSegmentIndex = segment_index + segmentPtr->segment_band_count;
     432             : 
     433             :         // Right Neighbor
     434        7200 :         if (segment_index < segmentPtr->row_array[rowSegmentIndex].ending_seg_index)
     435             :         {
     436        6478 :             eb_block_on_mutex(segmentPtr->row_array[rowSegmentIndex].assignment_mutex);
     437             : 
     438        6480 :             --segmentPtr->dep_map.dependency_map[rightSegmentIndex];
     439             : 
     440        6480 :             if (segmentPtr->dep_map.dependency_map[rightSegmentIndex] == 0) {
     441        5504 :                 *segmentInOutIndex = segmentPtr->row_array[rowSegmentIndex].current_seg_index;
     442        5504 :                 ++segmentPtr->row_array[rowSegmentIndex].current_seg_index;
     443        5504 :                 selfAssigned = EB_TRUE;
     444        5504 :                 continueProcessingFlag = EB_TRUE;
     445             : 
     446             :                 //fprintf(trace, "Start  Pic: %u Seg: %u\n",
     447             :                 //    (unsigned) ((PictureControlSet*) taskPtr->picture_control_set_wrapper_ptr->object_ptr)->picture_number,
     448             :                 //    *segmentInOutIndex);
     449             :             }
     450             : 
     451        6480 :             eb_release_mutex(segmentPtr->row_array[rowSegmentIndex].assignment_mutex);
     452             :         }
     453             : 
     454             :         // Bottom-left Neighbor
     455        7202 :         if (rowSegmentIndex < segmentPtr->segment_row_count - 1 && bottomLeftSegmentIndex >= segmentPtr->row_array[rowSegmentIndex + 1].starting_seg_index)
     456             :         {
     457        5400 :             eb_block_on_mutex(segmentPtr->row_array[rowSegmentIndex + 1].assignment_mutex);
     458             : 
     459        5400 :             --segmentPtr->dep_map.dependency_map[bottomLeftSegmentIndex];
     460             : 
     461        5400 :             if (segmentPtr->dep_map.dependency_map[bottomLeftSegmentIndex] == 0) {
     462        1576 :                 if (selfAssigned == EB_TRUE)
     463        1302 :                     feedbackRowIndex = (int16_t)rowSegmentIndex + 1;
     464             :                 else {
     465         274 :                     *segmentInOutIndex = segmentPtr->row_array[rowSegmentIndex + 1].current_seg_index;
     466         274 :                     ++segmentPtr->row_array[rowSegmentIndex + 1].current_seg_index;
     467         274 :                     selfAssigned = EB_TRUE;
     468         274 :                     continueProcessingFlag = EB_TRUE;
     469             : 
     470             :                     //fprintf(trace, "Start  Pic: %u Seg: %u\n",
     471             :                     //    (unsigned) ((PictureControlSet*) taskPtr->picture_control_set_wrapper_ptr->object_ptr)->picture_number,
     472             :                     //    *segmentInOutIndex);
     473             :                 }
     474             :             }
     475        5400 :             eb_release_mutex(segmentPtr->row_array[rowSegmentIndex + 1].assignment_mutex);
     476             :         }
     477             : 
     478        7202 :         if (feedbackRowIndex > 0) {
     479        1302 :             eb_get_empty_object(
     480             :                 srmFifoPtr,
     481             :                 &wrapper_ptr);
     482        1302 :             feedbackTaskPtr = (EncDecTasks*)wrapper_ptr->object_ptr;
     483        1302 :             feedbackTaskPtr->input_type = ENCDEC_TASKS_ENCDEC_INPUT;
     484        1302 :             feedbackTaskPtr->enc_dec_segment_row = feedbackRowIndex;
     485        1302 :             feedbackTaskPtr->picture_control_set_wrapper_ptr = taskPtr->picture_control_set_wrapper_ptr;
     486        1302 :             eb_post_full_object(wrapper_ptr);
     487             :         }
     488             : 
     489        7200 :         break;
     490             : 
     491           0 :     default:
     492           0 :         break;
     493             :     }
     494             : 
     495        8621 :     return continueProcessingFlag;
     496             : }
     497           0 : void ReconOutput(
     498             :     PictureControlSet    *picture_control_set_ptr,
     499             :     SequenceControlSet   *sequence_control_set_ptr) {
     500             :     EbObjectWrapper             *outputReconWrapperPtr;
     501             :     EbBufferHeaderType           *outputReconPtr;
     502           0 :     EncodeContext               *encode_context_ptr = sequence_control_set_ptr->encode_context_ptr;
     503           0 :     EbBool is16bit = (sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
     504             :     // The totalNumberOfReconFrames counter has to be write/read protected as
     505             :     //   it is used to determine the end of the stream.  If it is not protected
     506             :     //   the encoder might not properly terminate.
     507           0 :     eb_block_on_mutex(encode_context_ptr->total_number_of_recon_frame_mutex);
     508             : 
     509           0 :     if (!picture_control_set_ptr->parent_pcs_ptr->is_alt_ref) {
     510             :         // Get Recon Buffer
     511           0 :         eb_get_empty_object(
     512           0 :             sequence_control_set_ptr->encode_context_ptr->recon_output_fifo_ptr,
     513             :             &outputReconWrapperPtr);
     514           0 :         outputReconPtr = (EbBufferHeaderType*)outputReconWrapperPtr->object_ptr;
     515           0 :         outputReconPtr->flags = 0;
     516             : 
     517             :         // START READ/WRITE PROTECTED SECTION
     518           0 :         if (encode_context_ptr->total_number_of_recon_frames == encode_context_ptr->terminating_picture_number)
     519           0 :             outputReconPtr->flags = EB_BUFFERFLAG_EOS;
     520             : 
     521           0 :         encode_context_ptr->total_number_of_recon_frames++;
     522             : 
     523             :         //eb_release_mutex(encode_context_ptr->terminating_conditions_mutex);
     524             : 
     525             :         // STOP READ/WRITE PROTECTED SECTION
     526           0 :         outputReconPtr->n_filled_len = 0;
     527             : 
     528             :         // Copy the Reconstructed Picture to the Output Recon Buffer
     529             :         {
     530             :             uint32_t sampleTotalCount;
     531             :             uint8_t *reconReadPtr;
     532             :             uint8_t *reconWritePtr;
     533             : 
     534             :             EbPictureBufferDesc *recon_ptr;
     535             :             {
     536           0 :                 if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
     537           0 :                     recon_ptr = is16bit ?
     538           0 :                     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->reference_picture16bit :
     539           0 :                     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->reference_picture;
     540             :                 else {
     541           0 :                     if (is16bit)
     542           0 :                         recon_ptr = picture_control_set_ptr->recon_picture16bit_ptr;
     543             :                     else
     544           0 :                         recon_ptr = picture_control_set_ptr->recon_picture_ptr;
     545             :                 }
     546             :             }
     547             : 
     548             :             // FGN: Create a buffer if needed, copy the reconstructed picture and run the film grain synthesis algorithm
     549             : 
     550           0 :             if (sequence_control_set_ptr->seq_header.film_grain_params_present) {
     551             :                 EbPictureBufferDesc  *intermediateBufferPtr;
     552             :                 {
     553           0 :                     if (is16bit)
     554           0 :                         intermediateBufferPtr = picture_control_set_ptr->film_grain_picture16bit_ptr;
     555             :                     else
     556           0 :                         intermediateBufferPtr = picture_control_set_ptr->film_grain_picture_ptr;
     557             :                 }
     558             : 
     559             :                 aom_film_grain_t *film_grain_ptr;
     560             : 
     561           0 :                 if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
     562           0 :                     film_grain_ptr = &((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->film_grain_params;
     563             :                 else
     564           0 :                     film_grain_ptr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr.film_grain_params;
     565             : 
     566           0 :                 eb_av1_add_film_grain(recon_ptr, intermediateBufferPtr, film_grain_ptr);
     567           0 :                 recon_ptr = intermediateBufferPtr;
     568             :             }
     569             : 
     570             :             // End running the film grain
     571             :             // Y Recon Samples
     572           0 :             sampleTotalCount = ((recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right) * (recon_ptr->max_height - sequence_control_set_ptr->max_input_pad_bottom)) << is16bit;
     573           0 :             reconReadPtr = recon_ptr->buffer_y + (recon_ptr->origin_y << is16bit) * recon_ptr->stride_y + (recon_ptr->origin_x << is16bit);
     574           0 :             reconWritePtr = &(outputReconPtr->p_buffer[outputReconPtr->n_filled_len]);
     575             : 
     576           0 :             CHECK_REPORT_ERROR(
     577             :                 (outputReconPtr->n_filled_len + sampleTotalCount <= outputReconPtr->n_alloc_len),
     578             :                 encode_context_ptr->app_callback_ptr,
     579             :                 EB_ENC_ROB_OF_ERROR);
     580             : 
     581             :             // Initialize Y recon buffer
     582           0 :             picture_copy_kernel(
     583             :                 reconReadPtr,
     584           0 :                 recon_ptr->stride_y,
     585             :                 reconWritePtr,
     586           0 :                 recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right,
     587           0 :                 recon_ptr->width - sequence_control_set_ptr->pad_right,
     588           0 :                 recon_ptr->height - sequence_control_set_ptr->pad_bottom,
     589           0 :                 1 << is16bit);
     590             : 
     591           0 :             outputReconPtr->n_filled_len += sampleTotalCount;
     592             : 
     593             :             // U Recon Samples
     594           0 :             sampleTotalCount = ((recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right) * (recon_ptr->max_height - sequence_control_set_ptr->max_input_pad_bottom) >> 2) << is16bit;
     595           0 :             reconReadPtr = recon_ptr->buffer_cb + ((recon_ptr->origin_y << is16bit) >> 1) * recon_ptr->stride_cb + ((recon_ptr->origin_x << is16bit) >> 1);
     596           0 :             reconWritePtr = &(outputReconPtr->p_buffer[outputReconPtr->n_filled_len]);
     597             : 
     598           0 :             CHECK_REPORT_ERROR(
     599             :                 (outputReconPtr->n_filled_len + sampleTotalCount <= outputReconPtr->n_alloc_len),
     600             :                 encode_context_ptr->app_callback_ptr,
     601             :                 EB_ENC_ROB_OF_ERROR);
     602             : 
     603             :             // Initialize U recon buffer
     604           0 :             picture_copy_kernel(
     605             :                 reconReadPtr,
     606           0 :                 recon_ptr->stride_cb,
     607             :                 reconWritePtr,
     608           0 :                 (recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right) >> 1,
     609           0 :                 (recon_ptr->width - sequence_control_set_ptr->pad_right) >> 1,
     610           0 :                 (recon_ptr->height - sequence_control_set_ptr->pad_bottom) >> 1,
     611           0 :                 1 << is16bit);
     612           0 :             outputReconPtr->n_filled_len += sampleTotalCount;
     613             : 
     614             :             // V Recon Samples
     615           0 :             sampleTotalCount = ((recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right) * (recon_ptr->max_height - sequence_control_set_ptr->max_input_pad_bottom) >> 2) << is16bit;
     616           0 :             reconReadPtr = recon_ptr->buffer_cr + ((recon_ptr->origin_y << is16bit) >> 1) * recon_ptr->stride_cr + ((recon_ptr->origin_x << is16bit) >> 1);
     617           0 :             reconWritePtr = &(outputReconPtr->p_buffer[outputReconPtr->n_filled_len]);
     618             : 
     619           0 :             CHECK_REPORT_ERROR(
     620             :                 (outputReconPtr->n_filled_len + sampleTotalCount <= outputReconPtr->n_alloc_len),
     621             :                 encode_context_ptr->app_callback_ptr,
     622             :                 EB_ENC_ROB_OF_ERROR);
     623             : 
     624             :             // Initialize V recon buffer
     625             : 
     626           0 :             picture_copy_kernel(
     627             :                 reconReadPtr,
     628           0 :                 recon_ptr->stride_cr,
     629             :                 reconWritePtr,
     630           0 :                 (recon_ptr->max_width - sequence_control_set_ptr->max_input_pad_right) >> 1,
     631           0 :                 (recon_ptr->width - sequence_control_set_ptr->pad_right) >> 1,
     632           0 :                 (recon_ptr->height - sequence_control_set_ptr->pad_bottom) >> 1,
     633           0 :                 1 << is16bit);
     634           0 :             outputReconPtr->n_filled_len += sampleTotalCount;
     635           0 :             outputReconPtr->pts = picture_control_set_ptr->picture_number;
     636             :         }
     637             : 
     638             :         // Post the Recon object
     639           0 :         eb_post_full_object(outputReconWrapperPtr);
     640             :     }
     641             :     else {
     642             :         // Overlay and altref have 1 recon only, which is from overlay pictures. So the recon of the alt_ref is not sent to the application.
     643             :         // However, to hanlde the end of sequence properly, total_number_of_recon_frames is increamented
     644           0 :         encode_context_ptr->total_number_of_recon_frames++;
     645             :     }
     646           0 :     eb_release_mutex(encode_context_ptr->total_number_of_recon_frame_mutex);
     647           0 : }
     648             : 
     649           0 : void psnr_calculations(
     650             :     PictureControlSet    *picture_control_set_ptr,
     651             :     SequenceControlSet   *sequence_control_set_ptr){
     652           0 :     EbBool is16bit = (sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
     653             : 
     654           0 :     if (!is16bit) {
     655             :         EbPictureBufferDesc *recon_ptr;
     656             : 
     657           0 :         if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
     658           0 :             recon_ptr = ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->reference_picture;
     659             :         else
     660           0 :             recon_ptr = picture_control_set_ptr->recon_picture_ptr;
     661             : 
     662           0 :         EbPictureBufferDesc *input_picture_ptr = (EbPictureBufferDesc*)picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr;
     663             : 
     664           0 :         uint64_t sseTotal[3] = { 0 };
     665             :         uint32_t   columnIndex;
     666           0 :         uint32_t   row_index = 0;
     667           0 :         uint64_t   residualDistortion = 0;
     668             :         EbByte  inputBuffer;
     669             :         EbByte  reconCoeffBuffer;
     670             : 
     671             :         EbByte buffer_y;
     672             :         EbByte buffer_cb;
     673             :         EbByte buffer_cr;
     674             : 
     675             :         // if current source picture was temporally filtered, use an alternative buffer which stores
     676             :         // the original source picture
     677           0 :         if(picture_control_set_ptr->parent_pcs_ptr->temporal_filtering_on == EB_TRUE){
     678           0 :             buffer_y = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[0];
     679           0 :             buffer_cb = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[1];
     680           0 :             buffer_cr = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[2];
     681             :         }
     682             :         else {
     683           0 :             buffer_y = input_picture_ptr->buffer_y;
     684           0 :             buffer_cb = input_picture_ptr->buffer_cb;
     685           0 :             buffer_cr = input_picture_ptr->buffer_cr;
     686             :         }
     687             : 
     688           0 :         reconCoeffBuffer = &((recon_ptr->buffer_y)[recon_ptr->origin_x + recon_ptr->origin_y * recon_ptr->stride_y]);
     689           0 :         inputBuffer = &(buffer_y[input_picture_ptr->origin_x + input_picture_ptr->origin_y * input_picture_ptr->stride_y]);
     690             : 
     691           0 :         residualDistortion = 0;
     692             : 
     693           0 :         while (row_index < sequence_control_set_ptr->seq_header.max_frame_height) {
     694           0 :             columnIndex = 0;
     695           0 :             while (columnIndex < sequence_control_set_ptr->seq_header.max_frame_width) {
     696           0 :                 residualDistortion += (int64_t)SQR((int64_t)(inputBuffer[columnIndex]) - (reconCoeffBuffer[columnIndex]));
     697           0 :                 ++columnIndex;
     698             :             }
     699             : 
     700           0 :             inputBuffer += input_picture_ptr->stride_y;
     701           0 :             reconCoeffBuffer += recon_ptr->stride_y;
     702           0 :             ++row_index;
     703             :         }
     704             : 
     705           0 :         sseTotal[0] = residualDistortion;
     706             : 
     707           0 :         reconCoeffBuffer = &((recon_ptr->buffer_cb)[recon_ptr->origin_x / 2 + recon_ptr->origin_y / 2 * recon_ptr->stride_cb]);
     708           0 :         inputBuffer = &(buffer_cb[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cb]);
     709             : 
     710           0 :         residualDistortion = 0;
     711           0 :         row_index = 0;
     712           0 :         while (row_index < sequence_control_set_ptr->chroma_height) {
     713           0 :             columnIndex = 0;
     714           0 :             while (columnIndex < sequence_control_set_ptr->chroma_width) {
     715           0 :                 residualDistortion += (int64_t)SQR((int64_t)(inputBuffer[columnIndex]) - (reconCoeffBuffer[columnIndex]));
     716           0 :                 ++columnIndex;
     717             :             }
     718             : 
     719           0 :             inputBuffer += input_picture_ptr->stride_cb;
     720           0 :             reconCoeffBuffer += recon_ptr->stride_cb;
     721           0 :             ++row_index;
     722             :         }
     723             : 
     724           0 :         sseTotal[1] = residualDistortion;
     725             : 
     726           0 :         reconCoeffBuffer = &((recon_ptr->buffer_cr)[recon_ptr->origin_x / 2 + recon_ptr->origin_y / 2 * recon_ptr->stride_cr]);
     727           0 :         inputBuffer = &(buffer_cr[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cr]);
     728           0 :         residualDistortion = 0;
     729           0 :         row_index = 0;
     730             : 
     731           0 :         while (row_index < sequence_control_set_ptr->chroma_height) {
     732           0 :             columnIndex = 0;
     733           0 :             while (columnIndex < sequence_control_set_ptr->chroma_width) {
     734           0 :                 residualDistortion += (int64_t)SQR((int64_t)(inputBuffer[columnIndex]) - (reconCoeffBuffer[columnIndex]));
     735           0 :                 ++columnIndex;
     736             :             }
     737             : 
     738           0 :             inputBuffer += input_picture_ptr->stride_cr;
     739           0 :             reconCoeffBuffer += recon_ptr->stride_cr;
     740           0 :             ++row_index;
     741             :         }
     742             : 
     743           0 :         sseTotal[2] = residualDistortion;
     744           0 :         picture_control_set_ptr->parent_pcs_ptr->luma_sse = (uint32_t)sseTotal[0];
     745           0 :         picture_control_set_ptr->parent_pcs_ptr->cb_sse = (uint32_t)sseTotal[1];
     746           0 :         picture_control_set_ptr->parent_pcs_ptr->cr_sse = (uint32_t)sseTotal[2];
     747             : 
     748           0 :         if(picture_control_set_ptr->parent_pcs_ptr->temporal_filtering_on == EB_TRUE) {
     749           0 :             EB_FREE_ARRAY(buffer_y);
     750           0 :             EB_FREE_ARRAY(buffer_cb);
     751           0 :             EB_FREE_ARRAY(buffer_cr);
     752             :         }
     753             :     }
     754             :     else {
     755             :         EbPictureBufferDesc *recon_ptr;
     756             : 
     757           0 :         if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
     758           0 :             recon_ptr = ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->reference_picture16bit;
     759             :         else
     760           0 :             recon_ptr = picture_control_set_ptr->recon_picture16bit_ptr;
     761           0 :         EbPictureBufferDesc *input_picture_ptr = (EbPictureBufferDesc*)picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr;
     762             : 
     763           0 :         uint64_t sseTotal[3] = { 0 };
     764             :         uint32_t   columnIndex;
     765           0 :         uint32_t   row_index = 0;
     766           0 :         uint64_t   residualDistortion = 0;
     767             :         EbByte  inputBuffer;
     768             :         EbByte  inputBufferBitInc;
     769             :         uint16_t*  reconCoeffBuffer;
     770             : 
     771           0 :         if (sequence_control_set_ptr->static_config.ten_bit_format == 1) {
     772           0 :             const uint32_t luma_width = sequence_control_set_ptr->seq_header.max_frame_width;
     773           0 :             const uint32_t luma_height = sequence_control_set_ptr->seq_header.max_frame_height;
     774           0 :             const uint32_t chroma_width = sequence_control_set_ptr->chroma_width;
     775           0 :             const uint32_t picture_width_in_sb = (luma_width + 64 - 1) / 64;
     776           0 :             const uint32_t pictureHeighInLcu = (luma_height + 64 - 1) / 64;
     777           0 :             const uint32_t luma2BitWidth = luma_width / 4;
     778           0 :             const uint32_t chroma_height = luma_height / 2;
     779           0 :             const uint32_t chroma2BitWidth = luma_width / 8;
     780             :             uint32_t lcuNumberInHeight, lcuNumberInWidth;
     781             : 
     782           0 :             EbByte  inputBufferOrg = &((input_picture_ptr->buffer_y)[input_picture_ptr->origin_x + input_picture_ptr->origin_y * input_picture_ptr->stride_y]);
     783           0 :             uint16_t*  reconBufferOrg = (uint16_t*)(&((recon_ptr->buffer_y)[(recon_ptr->origin_x << is16bit) + (recon_ptr->origin_y << is16bit) * recon_ptr->stride_y]));;
     784             : 
     785           0 :             EbByte  inputBufferOrgU = &((input_picture_ptr->buffer_cb)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cb]);;
     786           0 :             uint16_t*  reconBufferOrgU = reconCoeffBuffer = (uint16_t*)(&((recon_ptr->buffer_cb)[(recon_ptr->origin_x << is16bit) / 2 + (recon_ptr->origin_y << is16bit) / 2 * recon_ptr->stride_cb]));;
     787             : 
     788           0 :             EbByte  inputBufferOrgV = &((input_picture_ptr->buffer_cr)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cr]);;
     789           0 :             uint16_t*  reconBufferOrgV = reconCoeffBuffer = (uint16_t*)(&((recon_ptr->buffer_cr)[(recon_ptr->origin_x << is16bit) / 2 + (recon_ptr->origin_y << is16bit) / 2 * recon_ptr->stride_cr]));;
     790             : 
     791           0 :             residualDistortion = 0;
     792           0 :             uint64_t residualDistortionU = 0;
     793           0 :             uint64_t residualDistortionV = 0;
     794             : 
     795           0 :             for (lcuNumberInHeight = 0; lcuNumberInHeight < pictureHeighInLcu; ++lcuNumberInHeight)
     796             :             {
     797           0 :                 for (lcuNumberInWidth = 0; lcuNumberInWidth < picture_width_in_sb; ++lcuNumberInWidth)
     798             :                 {
     799           0 :                     uint32_t tbOriginX = lcuNumberInWidth * 64;
     800           0 :                     uint32_t tbOriginY = lcuNumberInHeight * 64;
     801           0 :                     uint32_t sb_width = (luma_width - tbOriginX) < 64 ? (luma_width - tbOriginX) : 64;
     802           0 :                     uint32_t sb_height = (luma_height - tbOriginY) < 64 ? (luma_height - tbOriginY) : 64;
     803             : 
     804           0 :                     inputBuffer = inputBufferOrg + tbOriginY * input_picture_ptr->stride_y + tbOriginX;
     805           0 :                     inputBufferBitInc = input_picture_ptr->buffer_bit_inc_y + tbOriginY * luma2BitWidth + (tbOriginX / 4)*sb_height;
     806           0 :                     reconCoeffBuffer = reconBufferOrg + tbOriginY * recon_ptr->stride_y + tbOriginX;
     807             : 
     808             :                     uint64_t   j, k;
     809             :                     uint16_t   outPixel;
     810             :                     uint8_t    nBitPixel;
     811             :                     uint8_t   four2bitPels;
     812           0 :                     uint32_t     inn_stride = sb_width / 4;
     813             : 
     814           0 :                     for (j = 0; j < sb_height; j++)
     815             :                     {
     816           0 :                         for (k = 0; k < sb_width / 4; k++)
     817             :                         {
     818           0 :                             four2bitPels = inputBufferBitInc[k + j * inn_stride];
     819             : 
     820           0 :                             nBitPixel = (four2bitPels >> 6) & 3;
     821           0 :                             outPixel = inputBuffer[k * 4 + 0 + j * input_picture_ptr->stride_y] << 2;
     822           0 :                             outPixel = outPixel | nBitPixel;
     823           0 :                             residualDistortion += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 0 + j * recon_ptr->stride_y]);
     824             : 
     825           0 :                             nBitPixel = (four2bitPels >> 4) & 3;
     826           0 :                             outPixel = inputBuffer[k * 4 + 1 + j * input_picture_ptr->stride_y] << 2;
     827           0 :                             outPixel = outPixel | nBitPixel;
     828           0 :                             residualDistortion += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 1 + j * recon_ptr->stride_y]);
     829             : 
     830           0 :                             nBitPixel = (four2bitPels >> 2) & 3;
     831           0 :                             outPixel = inputBuffer[k * 4 + 2 + j * input_picture_ptr->stride_y] << 2;
     832           0 :                             outPixel = outPixel | nBitPixel;
     833           0 :                             residualDistortion += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 2 + j * recon_ptr->stride_y]);
     834             : 
     835           0 :                             nBitPixel = (four2bitPels >> 0) & 3;
     836           0 :                             outPixel = inputBuffer[k * 4 + 3 + j * input_picture_ptr->stride_y] << 2;
     837           0 :                             outPixel = outPixel | nBitPixel;
     838           0 :                             residualDistortion += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 3 + j * recon_ptr->stride_y]);
     839             :                         }
     840             :                     }
     841             : 
     842             :                     //U+V
     843             : 
     844           0 :                     tbOriginX = lcuNumberInWidth * 32;
     845           0 :                     tbOriginY = lcuNumberInHeight * 32;
     846           0 :                     sb_width = (chroma_width - tbOriginX) < 32 ? (chroma_width - tbOriginX) : 32;
     847           0 :                     sb_height = (chroma_height - tbOriginY) < 32 ? (chroma_height - tbOriginY) : 32;
     848             : 
     849           0 :                     inn_stride = sb_width / 4;
     850             : 
     851           0 :                     inputBuffer = inputBufferOrgU + tbOriginY * input_picture_ptr->stride_cb + tbOriginX;
     852             : 
     853           0 :                     inputBufferBitInc = input_picture_ptr->buffer_bit_inc_cb + tbOriginY * chroma2BitWidth + (tbOriginX / 4)*sb_height;
     854             : 
     855           0 :                     reconCoeffBuffer = reconBufferOrgU + tbOriginY * recon_ptr->stride_cb + tbOriginX;
     856             : 
     857           0 :                     for (j = 0; j < sb_height; j++)
     858             :                     {
     859           0 :                         for (k = 0; k < sb_width / 4; k++)
     860             :                         {
     861           0 :                             four2bitPels = inputBufferBitInc[k + j * inn_stride];
     862             : 
     863           0 :                             nBitPixel = (four2bitPels >> 6) & 3;
     864           0 :                             outPixel = inputBuffer[k * 4 + 0 + j * input_picture_ptr->stride_cb] << 2;
     865           0 :                             outPixel = outPixel | nBitPixel;
     866           0 :                             residualDistortionU += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 0 + j * recon_ptr->stride_cb]);
     867             : 
     868           0 :                             nBitPixel = (four2bitPels >> 4) & 3;
     869           0 :                             outPixel = inputBuffer[k * 4 + 1 + j * input_picture_ptr->stride_cb] << 2;
     870           0 :                             outPixel = outPixel | nBitPixel;
     871           0 :                             residualDistortionU += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 1 + j * recon_ptr->stride_cb]);
     872             : 
     873           0 :                             nBitPixel = (four2bitPels >> 2) & 3;
     874           0 :                             outPixel = inputBuffer[k * 4 + 2 + j * input_picture_ptr->stride_cb] << 2;
     875           0 :                             outPixel = outPixel | nBitPixel;
     876           0 :                             residualDistortionU += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 2 + j * recon_ptr->stride_cb]);
     877             : 
     878           0 :                             nBitPixel = (four2bitPels >> 0) & 3;
     879           0 :                             outPixel = inputBuffer[k * 4 + 3 + j * input_picture_ptr->stride_cb] << 2;
     880           0 :                             outPixel = outPixel | nBitPixel;
     881           0 :                             residualDistortionU += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 3 + j * recon_ptr->stride_cb]);
     882             :                         }
     883             :                     }
     884             : 
     885           0 :                     inputBuffer = inputBufferOrgV + tbOriginY * input_picture_ptr->stride_cr + tbOriginX;
     886           0 :                     inputBufferBitInc = input_picture_ptr->buffer_bit_inc_cr + tbOriginY * chroma2BitWidth + (tbOriginX / 4)*sb_height;
     887           0 :                     reconCoeffBuffer = reconBufferOrgV + tbOriginY * recon_ptr->stride_cr + tbOriginX;
     888             : 
     889           0 :                     for (j = 0; j < sb_height; j++)
     890             :                     {
     891           0 :                         for (k = 0; k < sb_width / 4; k++)
     892             :                         {
     893           0 :                             four2bitPels = inputBufferBitInc[k + j * inn_stride];
     894             : 
     895           0 :                             nBitPixel = (four2bitPels >> 6) & 3;
     896           0 :                             outPixel = inputBuffer[k * 4 + 0 + j * input_picture_ptr->stride_cr] << 2;
     897           0 :                             outPixel = outPixel | nBitPixel;
     898           0 :                             residualDistortionV += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 0 + j * recon_ptr->stride_cr]);
     899             : 
     900           0 :                             nBitPixel = (four2bitPels >> 4) & 3;
     901           0 :                             outPixel = inputBuffer[k * 4 + 1 + j * input_picture_ptr->stride_cr] << 2;
     902           0 :                             outPixel = outPixel | nBitPixel;
     903           0 :                             residualDistortionV += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 1 + j * recon_ptr->stride_cr]);
     904             : 
     905           0 :                             nBitPixel = (four2bitPels >> 2) & 3;
     906           0 :                             outPixel = inputBuffer[k * 4 + 2 + j * input_picture_ptr->stride_cr] << 2;
     907           0 :                             outPixel = outPixel | nBitPixel;
     908           0 :                             residualDistortionV += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 2 + j * recon_ptr->stride_cr]);
     909             : 
     910           0 :                             nBitPixel = (four2bitPels >> 0) & 3;
     911           0 :                             outPixel = inputBuffer[k * 4 + 3 + j * input_picture_ptr->stride_cr] << 2;
     912           0 :                             outPixel = outPixel | nBitPixel;
     913           0 :                             residualDistortionV += (int64_t)SQR((int64_t)outPixel - (int64_t)reconCoeffBuffer[k * 4 + 3 + j * recon_ptr->stride_cr]);
     914             :                         }
     915             :                     }
     916             :                 }
     917             :             }
     918             : 
     919           0 :             sseTotal[0] = residualDistortion;
     920           0 :             sseTotal[1] = residualDistortionU;
     921           0 :             sseTotal[2] = residualDistortionV;
     922             :         }
     923             :         else {
     924           0 :             reconCoeffBuffer = (uint16_t*)(&((recon_ptr->buffer_y)[(recon_ptr->origin_x << is16bit) + (recon_ptr->origin_y << is16bit) * recon_ptr->stride_y]));
     925             : 
     926             :             // if current source picture was temporally filtered, use an alternative buffer which stores
     927             :             // the original source picture
     928             :             EbByte buffer_y, buffer_bit_inc_y;
     929             :             EbByte buffer_cb, buffer_bit_inc_cb;
     930             :             EbByte buffer_cr, buffer_bit_inc_cr;
     931             : 
     932           0 :             if(picture_control_set_ptr->parent_pcs_ptr->temporal_filtering_on == EB_TRUE){
     933           0 :                 buffer_y = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[0];
     934           0 :                 buffer_bit_inc_y = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_bit_inc_ptr[0];
     935           0 :                 buffer_cb = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[1];
     936           0 :                 buffer_bit_inc_cb = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_bit_inc_ptr[1];
     937           0 :                 buffer_cr = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_ptr[2];
     938           0 :                 buffer_bit_inc_cr = picture_control_set_ptr->parent_pcs_ptr->save_enhanced_picture_bit_inc_ptr[2];
     939             :             }else{
     940           0 :                 buffer_y = input_picture_ptr->buffer_y;
     941           0 :                 buffer_bit_inc_y = input_picture_ptr->buffer_bit_inc_y;
     942           0 :                 buffer_cb = input_picture_ptr->buffer_cb;
     943           0 :                 buffer_bit_inc_cb = input_picture_ptr->buffer_bit_inc_cb;
     944           0 :                 buffer_cr = input_picture_ptr->buffer_cr;
     945           0 :                 buffer_bit_inc_cr = input_picture_ptr->buffer_bit_inc_cr;
     946             :             }
     947             : 
     948           0 :             inputBuffer = &((buffer_y)[input_picture_ptr->origin_x + input_picture_ptr->origin_y * input_picture_ptr->stride_y]);
     949           0 :             inputBufferBitInc = &((buffer_bit_inc_y)[input_picture_ptr->origin_x + input_picture_ptr->origin_y * input_picture_ptr->stride_bit_inc_y]);
     950             : 
     951           0 :             residualDistortion = 0;
     952             : 
     953           0 :             while (row_index < sequence_control_set_ptr->seq_header.max_frame_height) {
     954           0 :                 columnIndex = 0;
     955           0 :                 while (columnIndex < sequence_control_set_ptr->seq_header.max_frame_width) {
     956           0 :                     residualDistortion += (int64_t)SQR((int64_t)((((inputBuffer[columnIndex]) << 2) | ((inputBufferBitInc[columnIndex] >> 6) & 3))) - (reconCoeffBuffer[columnIndex]));
     957             : 
     958           0 :                     ++columnIndex;
     959             :                 }
     960             : 
     961           0 :                 inputBuffer += input_picture_ptr->stride_y;
     962           0 :                 inputBufferBitInc += input_picture_ptr->stride_bit_inc_y;
     963           0 :                 reconCoeffBuffer += recon_ptr->stride_y;
     964           0 :                 ++row_index;
     965             :             }
     966             : 
     967           0 :             sseTotal[0] = residualDistortion;
     968             : 
     969           0 :             reconCoeffBuffer = (uint16_t*)(&((recon_ptr->buffer_cb)[(recon_ptr->origin_x << is16bit) / 2 + (recon_ptr->origin_y << is16bit) / 2 * recon_ptr->stride_cb]));
     970           0 :             inputBuffer = &((buffer_cb)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cb]);
     971           0 :             inputBufferBitInc = &((buffer_bit_inc_cb)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_bit_inc_cb]);
     972             : 
     973           0 :             residualDistortion = 0;
     974           0 :             row_index = 0;
     975           0 :             while (row_index < sequence_control_set_ptr->chroma_height) {
     976           0 :                 columnIndex = 0;
     977           0 :                 while (columnIndex < sequence_control_set_ptr->chroma_width) {
     978           0 :                     residualDistortion += (int64_t)SQR((int64_t)((((inputBuffer[columnIndex]) << 2) | ((inputBufferBitInc[columnIndex] >> 6) & 3))) - (reconCoeffBuffer[columnIndex]));
     979           0 :                     ++columnIndex;
     980             :                 }
     981             : 
     982           0 :                 inputBuffer += input_picture_ptr->stride_cb;
     983           0 :                 inputBufferBitInc += input_picture_ptr->stride_bit_inc_cb;
     984           0 :                 reconCoeffBuffer += recon_ptr->stride_cb;
     985           0 :                 ++row_index;
     986             :             }
     987             : 
     988           0 :             sseTotal[1] = residualDistortion;
     989             : 
     990           0 :             reconCoeffBuffer = (uint16_t*)(&((recon_ptr->buffer_cr)[(recon_ptr->origin_x << is16bit) / 2 + (recon_ptr->origin_y << is16bit) / 2 * recon_ptr->stride_cr]));
     991           0 :             inputBuffer = &((buffer_cr)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_cr]);
     992           0 :             inputBufferBitInc = &((buffer_bit_inc_cr)[input_picture_ptr->origin_x / 2 + input_picture_ptr->origin_y / 2 * input_picture_ptr->stride_bit_inc_cr]);
     993             : 
     994           0 :             residualDistortion = 0;
     995           0 :             row_index = 0;
     996             : 
     997           0 :             while (row_index < sequence_control_set_ptr->chroma_height) {
     998           0 :                 columnIndex = 0;
     999           0 :                 while (columnIndex < sequence_control_set_ptr->chroma_width) {
    1000           0 :                     residualDistortion += (int64_t)SQR((int64_t)((((inputBuffer[columnIndex]) << 2) | ((inputBufferBitInc[columnIndex] >> 6) & 3))) - (reconCoeffBuffer[columnIndex]));
    1001           0 :                     ++columnIndex;
    1002             :                 }
    1003             : 
    1004           0 :                 inputBuffer += input_picture_ptr->stride_cr;
    1005           0 :                 inputBufferBitInc += input_picture_ptr->stride_bit_inc_cr;
    1006           0 :                 reconCoeffBuffer += recon_ptr->stride_cr;
    1007           0 :                 ++row_index;
    1008             :             }
    1009             : 
    1010           0 :             sseTotal[2] = residualDistortion;
    1011             : 
    1012           0 :             if(picture_control_set_ptr->parent_pcs_ptr->temporal_filtering_on == EB_TRUE) {
    1013           0 :                 EB_FREE_ARRAY(buffer_y);
    1014           0 :                 EB_FREE_ARRAY(buffer_cb);
    1015           0 :                 EB_FREE_ARRAY(buffer_cr);
    1016           0 :                 EB_FREE_ARRAY(buffer_bit_inc_y);
    1017           0 :                 EB_FREE_ARRAY(buffer_bit_inc_cb);
    1018           0 :                 EB_FREE_ARRAY(buffer_bit_inc_cr);
    1019             :             }
    1020             :         }
    1021             : 
    1022           0 :         picture_control_set_ptr->parent_pcs_ptr->luma_sse = (uint32_t)sseTotal[0];
    1023           0 :         picture_control_set_ptr->parent_pcs_ptr->cb_sse = (uint32_t)sseTotal[1];
    1024           0 :         picture_control_set_ptr->parent_pcs_ptr->cr_sse = (uint32_t)sseTotal[2];
    1025             :     }
    1026           0 : }
    1027             : 
    1028          68 : void PadRefAndSetFlags(
    1029             :     PictureControlSet    *picture_control_set_ptr,
    1030             :     SequenceControlSet   *sequence_control_set_ptr
    1031             : )
    1032             : {
    1033          68 :     EbReferenceObject   *referenceObject = (EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr;
    1034          68 :     EbPictureBufferDesc *refPicPtr = (EbPictureBufferDesc*)referenceObject->reference_picture;
    1035          68 :     EbPictureBufferDesc *refPic16BitPtr = (EbPictureBufferDesc*)referenceObject->reference_picture16bit;
    1036          68 :     EbBool                is16bit = (sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
    1037             : 
    1038          68 :     if (!is16bit) {
    1039             :         // Y samples
    1040          68 :         generate_padding(
    1041             :             refPicPtr->buffer_y,
    1042          68 :             refPicPtr->stride_y,
    1043          68 :             refPicPtr->width,
    1044          68 :             refPicPtr->height,
    1045          68 :             refPicPtr->origin_x,
    1046          68 :             refPicPtr->origin_y);
    1047             : 
    1048             :         // Cb samples
    1049          68 :         generate_padding(
    1050             :             refPicPtr->buffer_cb,
    1051          68 :             refPicPtr->stride_cb,
    1052          68 :             refPicPtr->width >> 1,
    1053          68 :             refPicPtr->height >> 1,
    1054          68 :             refPicPtr->origin_x >> 1,
    1055          68 :             refPicPtr->origin_y >> 1);
    1056             : 
    1057             :         // Cr samples
    1058          68 :         generate_padding(
    1059             :             refPicPtr->buffer_cr,
    1060          68 :             refPicPtr->stride_cr,
    1061          68 :             refPicPtr->width >> 1,
    1062          68 :             refPicPtr->height >> 1,
    1063          68 :             refPicPtr->origin_x >> 1,
    1064          68 :             refPicPtr->origin_y >> 1);
    1065             :     }
    1066             : 
    1067             :     //We need this for MCP
    1068          68 :     if (is16bit) {
    1069             :         // Y samples
    1070           0 :         generate_padding16_bit(
    1071             :             refPic16BitPtr->buffer_y,
    1072           0 :             refPic16BitPtr->stride_y << 1,
    1073           0 :             refPic16BitPtr->width << 1,
    1074           0 :             refPic16BitPtr->height,
    1075           0 :             refPic16BitPtr->origin_x << 1,
    1076           0 :             refPic16BitPtr->origin_y);
    1077             : 
    1078             :         // Cb samples
    1079           0 :         generate_padding16_bit(
    1080             :             refPic16BitPtr->buffer_cb,
    1081           0 :             refPic16BitPtr->stride_cb << 1,
    1082           0 :             refPic16BitPtr->width,
    1083           0 :             refPic16BitPtr->height >> 1,
    1084           0 :             refPic16BitPtr->origin_x,
    1085           0 :             refPic16BitPtr->origin_y >> 1);
    1086             : 
    1087             :         // Cr samples
    1088           0 :         generate_padding16_bit(
    1089             :             refPic16BitPtr->buffer_cr,
    1090           0 :             refPic16BitPtr->stride_cr << 1,
    1091           0 :             refPic16BitPtr->width,
    1092           0 :             refPic16BitPtr->height >> 1,
    1093           0 :             refPic16BitPtr->origin_x,
    1094           0 :             refPic16BitPtr->origin_y >> 1);
    1095             : 
    1096             :         // Hsan: unpack ref samples (to be used @ MD)
    1097           0 :         un_pack2d(
    1098           0 :             (uint16_t*) refPic16BitPtr->buffer_y,
    1099           0 :             refPic16BitPtr->stride_y,
    1100             :             refPicPtr->buffer_y,
    1101           0 :             refPicPtr->stride_y,
    1102             :             refPicPtr->buffer_bit_inc_y,
    1103           0 :             refPicPtr->stride_bit_inc_y,
    1104           0 :             refPic16BitPtr->width  + (refPicPtr->origin_x << 1),
    1105           0 :             refPic16BitPtr->height + (refPicPtr->origin_y << 1));
    1106             : 
    1107           0 :         un_pack2d(
    1108           0 :             (uint16_t*)refPic16BitPtr->buffer_cb,
    1109           0 :             refPic16BitPtr->stride_cb,
    1110             :             refPicPtr->buffer_cb,
    1111           0 :             refPicPtr->stride_cb,
    1112             :             refPicPtr->buffer_bit_inc_cb,
    1113           0 :             refPicPtr->stride_bit_inc_cb,
    1114           0 :             (refPic16BitPtr->width + (refPicPtr->origin_x << 1)) >> 1,
    1115           0 :             (refPic16BitPtr->height + (refPicPtr->origin_y << 1)) >> 1);
    1116             : 
    1117           0 :         un_pack2d(
    1118           0 :             (uint16_t*)refPic16BitPtr->buffer_cr,
    1119           0 :             refPic16BitPtr->stride_cr,
    1120             :             refPicPtr->buffer_cr,
    1121           0 :             refPicPtr->stride_cr,
    1122             :             refPicPtr->buffer_bit_inc_cr,
    1123           0 :             refPicPtr->stride_bit_inc_cr,
    1124           0 :             (refPic16BitPtr->width + (refPicPtr->origin_x << 1)) >> 1,
    1125           0 :             (refPic16BitPtr->height + (refPicPtr->origin_y << 1)) >> 1);
    1126             :     }
    1127             :     // set up the ref POC
    1128          68 :     referenceObject->ref_poc = picture_control_set_ptr->parent_pcs_ptr->picture_number;
    1129             : 
    1130             :     // set up the QP
    1131          68 :     referenceObject->qp = (uint8_t)picture_control_set_ptr->parent_pcs_ptr->picture_qp;
    1132             : 
    1133             :     // set up the Slice Type
    1134          68 :     referenceObject->slice_type = picture_control_set_ptr->parent_pcs_ptr->slice_type;
    1135             : #if TWO_PASS
    1136          68 :     referenceObject->referenced_area_avg = picture_control_set_ptr->parent_pcs_ptr->referenced_area_avg;
    1137             : #endif
    1138          68 : }
    1139             : 
    1140          68 : void CopyStatisticsToRefObject(
    1141             :     PictureControlSet    *picture_control_set_ptr,
    1142             :     SequenceControlSet   *sequence_control_set_ptr
    1143             : )
    1144             : {
    1145          68 :     picture_control_set_ptr->intra_coded_area = (100 * picture_control_set_ptr->intra_coded_area) / (sequence_control_set_ptr->seq_header.max_frame_width * sequence_control_set_ptr->seq_header.max_frame_height);
    1146          68 :     if (picture_control_set_ptr->slice_type == I_SLICE)
    1147           4 :         picture_control_set_ptr->intra_coded_area = 0;
    1148             : 
    1149          68 :     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->intra_coded_area = (uint8_t)(picture_control_set_ptr->intra_coded_area);
    1150             : 
    1151             :     uint32_t sb_index;
    1152        4148 :     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index)
    1153        4080 :         ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->non_moving_index_array[sb_index] = picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index];
    1154          68 :     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->tmp_layer_idx = (uint8_t)picture_control_set_ptr->temporal_layer_index;
    1155          68 :     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->is_scene_change = picture_control_set_ptr->parent_pcs_ptr->scene_change_flag;
    1156             : 
    1157          68 :     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->cdef_frame_strength = picture_control_set_ptr->parent_pcs_ptr->cdef_frame_strength;
    1158             : 
    1159          68 :     Av1Common* cm = picture_control_set_ptr->parent_pcs_ptr->av1_cm;
    1160          68 :     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->sg_frame_ep = cm->sg_frame_ep;
    1161          68 :     if (sequence_control_set_ptr->mfmv_enabled) {
    1162          38 :         ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->frame_type = picture_control_set_ptr->parent_pcs_ptr->frm_hdr.frame_type;
    1163          38 :         ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->order_hint = picture_control_set_ptr->parent_pcs_ptr->cur_order_hint;
    1164          38 :         memcpy(((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->ref_order_hint, picture_control_set_ptr->parent_pcs_ptr->ref_order_hint, 7 * sizeof(uint32_t));
    1165             :     }
    1166          68 : }
    1167             : 
    1168             : /******************************************************
    1169             : * Derive EncDec Settings for OQ
    1170             : Input   : encoder mode and tune
    1171             : Output  : EncDec Kernel signal(s)
    1172             : ******************************************************/
    1173        1422 : EbErrorType signal_derivation_enc_dec_kernel_oq(
    1174             :     SequenceControlSet    *sequence_control_set_ptr,
    1175             :     PictureControlSet     *picture_control_set_ptr,
    1176             :     ModeDecisionContext   *context_ptr) {
    1177        1422 :     EbErrorType return_error = EB_ErrorNone;
    1178             : 
    1179             :     // Set Chroma Mode
    1180             :     // Level                Settings
    1181             :     // CHROMA_MODE_0  0     Full chroma search @ MD
    1182             :     // CHROMA_MODE_1  1     Fast chroma search @ MD
    1183             :     // CHROMA_MODE_2  2     Chroma blind @ MD + CFL @ EP
    1184             :     // CHROMA_MODE_3  3     Chroma blind @ MD + no CFL @ EP
    1185        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1186           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1187           0 :             context_ptr->chroma_level = CHROMA_MODE_1;
    1188             :         else
    1189           0 :             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
    1190           0 :                 context_ptr->chroma_level = CHROMA_MODE_1;
    1191             :             else
    1192           0 :                 context_ptr->chroma_level = (sequence_control_set_ptr->encoder_bit_depth == EB_8BIT) ?
    1193             :                 CHROMA_MODE_2 :
    1194             :                 CHROMA_MODE_3;
    1195             :     else
    1196             :     if (MR_MODE)
    1197             :         context_ptr->chroma_level = CHROMA_MODE_0;
    1198             :     else
    1199        1422 :     if (picture_control_set_ptr->enc_mode == ENC_M0 && picture_control_set_ptr->temporal_layer_index == 0)
    1200          59 :         context_ptr->chroma_level = CHROMA_MODE_0;
    1201             :     else
    1202        1363 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1203         600 :         context_ptr->chroma_level = CHROMA_MODE_1;
    1204             :     else
    1205         763 :         context_ptr->chroma_level = (sequence_control_set_ptr->encoder_bit_depth == EB_8BIT) ?
    1206             :             CHROMA_MODE_2 :
    1207             :             CHROMA_MODE_3 ;
    1208             : 
    1209             :     // Set fast loop method
    1210             :     // 1 fast loop: SSD_SEARCH not supported
    1211             :     // Level                Settings
    1212             :     //  0                   Collapsed fast loop
    1213             :     //  1                   Decoupled fast loops ( intra/inter)
    1214        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1215           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1216           0 :             context_ptr->decouple_intra_inter_fast_loop = 0;
    1217             :         else
    1218           0 :             context_ptr->decouple_intra_inter_fast_loop = 1;
    1219             :     else
    1220        1422 :     context_ptr->decouple_intra_inter_fast_loop = 0;
    1221             : 
    1222             :     // Set the search method when decoupled fast loop is used
    1223             :     // Hsan: FULL_SAD_SEARCH not supported
    1224        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1225           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1226           0 :             context_ptr->decoupled_fast_loop_search_method = SSD_SEARCH;
    1227             :         else
    1228           0 :             context_ptr->decoupled_fast_loop_search_method = FULL_SAD_SEARCH;
    1229             :     else
    1230        1422 :         if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1231         659 :             context_ptr->decoupled_fast_loop_search_method = SSD_SEARCH;
    1232             :         else
    1233         763 :             context_ptr->decoupled_fast_loop_search_method = FULL_SAD_SEARCH;
    1234             : 
    1235             :     // Set the full loop escape level
    1236             :     // Level                Settings
    1237             :     // 0                    Off
    1238             :     // 1                    On but only INTRA
    1239             :     // 2                    On both INTRA and INTER
    1240        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1241           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1242           0 :             context_ptr->full_loop_escape = 0;
    1243             :         else
    1244           0 :             context_ptr->full_loop_escape = 2;
    1245             :     else
    1246        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M5)
    1247         659 :         context_ptr->full_loop_escape = 0;
    1248             :     else
    1249         763 :         context_ptr->full_loop_escape = 2;
    1250             : 
    1251             :     // Set global MV injection
    1252             :     // Level                Settings
    1253             :     // 0                    Injection off (Hsan: but not derivation as used by MV ref derivation)
    1254             :     // 1                    On
    1255             : #if GLOBAL_WARPED_MOTION
    1256        1422 :     if (sequence_control_set_ptr->static_config.enable_global_motion == EB_TRUE)
    1257             :     {
    1258        1422 :         if (picture_control_set_ptr->enc_mode == ENC_M0)
    1259         659 :             context_ptr->global_mv_injection = 1;
    1260             :         else
    1261         763 :             context_ptr->global_mv_injection = 0;
    1262             :     }
    1263             :     else
    1264           0 :         context_ptr->global_mv_injection = 0;
    1265             : #else
    1266             :     if (sequence_control_set_ptr->static_config.enable_global_warped_motion == EB_TRUE)
    1267             :     {
    1268             :         if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1269             :         {
    1270             :             if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1271             :                 context_ptr->global_mv_injection = 1;
    1272             :             else
    1273             :                 context_ptr->global_mv_injection = 0;
    1274             :         }
    1275             :         else
    1276             :         {
    1277             :             if (picture_control_set_ptr->enc_mode <= ENC_M7)
    1278             :                 context_ptr->global_mv_injection = 1;
    1279             :             else
    1280             :                 context_ptr->global_mv_injection = 0;
    1281             :         }
    1282             :     }
    1283             :     else
    1284             :         context_ptr->global_mv_injection = 0;
    1285             : #endif
    1286             : #if FIX_NEAREST_NEW
    1287        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M0 && picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
    1288             : #else
    1289             :     if (picture_control_set_ptr->enc_mode == ENC_M0)
    1290             : #endif
    1291         421 :         context_ptr->new_nearest_near_comb_injection = 1;
    1292             :     else
    1293        1001 :         context_ptr->new_nearest_near_comb_injection = 0;
    1294        1422 :     if (picture_control_set_ptr->enc_mode == ENC_M0)
    1295         659 :         context_ptr->nx4_4xn_parent_mv_injection = 1;
    1296             :     else
    1297         763 :         context_ptr->nx4_4xn_parent_mv_injection = 0;
    1298             : 
    1299             :     // Set warped motion injection
    1300             :     // Level                Settings
    1301             :     // 0                    OFF
    1302             :     // 1                    On
    1303        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1304           0 :         context_ptr->warped_motion_injection = 0;
    1305             :     else
    1306        1422 :     context_ptr->warped_motion_injection = 1;
    1307             : 
    1308             :     // Set unipred3x3 injection
    1309             :     // Level                Settings
    1310             :     // 0                    OFF
    1311             :     // 1                    ON FULL
    1312             :     // 2                    Reduced set
    1313        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1314           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1315           0 :             context_ptr->unipred3x3_injection = 1;
    1316             :         else
    1317           0 :             context_ptr->unipred3x3_injection = 0;
    1318             :     else
    1319        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1320         659 :         context_ptr->unipred3x3_injection = 1;
    1321         763 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1322           0 :         context_ptr->unipred3x3_injection = 2;
    1323             :     else
    1324         763 :         context_ptr->unipred3x3_injection = 0;
    1325             : 
    1326             :     // Set bipred3x3 injection
    1327             :     // Level                Settings
    1328             :     // 0                    OFF
    1329             :     // 1                    ON FULL
    1330             :     // 2                    Reduced set
    1331        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1332           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1333           0 :             context_ptr->bipred3x3_injection = 1;
    1334             :         else
    1335           0 :             context_ptr->bipred3x3_injection = 0;
    1336             :     else
    1337        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1338         659 :         context_ptr->bipred3x3_injection = 1;
    1339         763 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1340           0 :         context_ptr->bipred3x3_injection = 2;
    1341             :     else
    1342         763 :         context_ptr->bipred3x3_injection = 0;
    1343             : 
    1344             :     // Level                Settings
    1345             :     // 0                    Level 0: OFF
    1346             :     // 1                    Level 1: 7x5 full-pel search + sub-pel refinement off
    1347             :     // 2                    Level 2: 7x5 full-pel search +  (H + V) sub-pel refinement only = 4 half-pel + 4 quarter-pel = 8 positions + pred_me_distortion to pa_me_distortion deviation on
    1348             :     // 3                    Level 3: 7x5 full-pel search +  (H + V + D only ~ the best) sub-pel refinement = up to 6 half-pel + up to 6  quarter-pel = up to 12 positions + pred_me_distortion to pa_me_distortion deviation on
    1349             :     // 4                    Level 4: 7x5 full-pel search +  (H + V + D) sub-pel refinement = 8 half-pel + 8 quarter-pel = 16 positions + pred_me_distortion to pa_me_distortion deviation on
    1350             :     // 5                    Level 5: 7x5 full-pel search +  (H + V + D) sub-pel refinement = 8 half-pel + 8 quarter-pel = 16 positions + pred_me_distortion to pa_me_distortion deviation off
    1351        1422 :     if (picture_control_set_ptr->slice_type != I_SLICE)
    1352        1375 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1353         632 :             context_ptr->predictive_me_level = 4;
    1354         743 :         else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1355           0 :             context_ptr->predictive_me_level = 2;
    1356             :         else
    1357         743 :             context_ptr->predictive_me_level = 0;
    1358             :     else
    1359          47 :         context_ptr->predictive_me_level = 0;
    1360             : 
    1361             :     // Derive md_staging_mode
    1362             :     //
    1363             : #if REMOVE_MD_STAGE_1
    1364             :     // MD_STAGING_MODE_0
    1365             :     // Default Parameters
    1366             :     //
    1367             :     // MD_STAGING_MODE_1
    1368             :     //  __________________________________________________________________________________________________________________
    1369             :     // |        | md_stage_0                  | md_stage_2                     | md_stage_3                              |
    1370             :     // |________|_____________________________|________________________________|_________________________________________|
    1371             :     // |CLASS_0 |Prediction for Luma & Chroma |T, Q, Q-1, T-1 for Luma Only    |T, Q, Q-1, T-1 or Luma & Chroma          |
    1372             :     // |CLASS_6 |                             |No RDOQ                         |RDOQ                                     |
    1373             :     // |        |                             |No Tx Type Search               |Tx Type Search                           |
    1374             :     // |        |                             |No Tx Size Search               |Tx Size Search                           |
    1375             :     // |        |                             |                                |CFL vs. Independent                      |
    1376             :     // |________|_____________________________|________________________________|_________________________________________|
    1377             :     // |CLASS_1 |Prediction for Luma Only     |T, Q, Q-1, T-1 for Luma Only    |T, Q, Q-1, T-1 for Luma & Chroma         |
    1378             :     // |CLASS_2 |No Interpolation Search      |No RDOQ                         |RDOQ                                     |
    1379             :     // |CLASS_3 |Bilinear Interpolation       |No Tx Type Search               |Tx Type Search                           |
    1380             :     // |CLASS_4 |                             |No Tx Size Search               |Tx Size Search                           |
    1381             :     // |CLASS_5 |                             |Interpolation Search            |                                         |
    1382             :     // |________|_____________________________|________________________________|_________________________________________|
    1383             : 
    1384        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1385         659 :         context_ptr->md_staging_mode = MD_STAGING_MODE_1;
    1386             :     else
    1387         763 :         context_ptr->md_staging_mode = MD_STAGING_MODE_0;
    1388             : #else
    1389             :     // MD_STAGING_MODE_1
    1390             :     //  _______________________________________________________________________________________________________________________________________________
    1391             :     // |        | md_stage_0                  | md_stage_1                  | md_stage_2                     | md_stage_3                              |
    1392             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1393             :     // |CLASS_0 |Prediction for Luma & Chroma |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 or Luma & Chroma          |
    1394             :     // |        |No Interpolation Search      |                             |RDOQ                            |RDOQ                                     |
    1395             :     // |        |Regular Interpolation        |                             |No Tx Search                    |Tx Search                                |
    1396             :     // |        |                             |                             |No ATB                          |ATB                                      |
    1397             :     // |        |                             |                             |                                |CFL vs. Independent                      |
    1398             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1399             :     // |CLASS_1 |Prediction for Chroma        |Prediction for Luma & Chroma |Bypassed                        |T, Q, Q-1, T-1 for Luma & Chroma         |
    1400             :     // |        |No Interpolation Search      |Interpolation Search         |                                |Tx Search                                |
    1401             :     // |        |Bilinear Interpolation       |                             |                                |                                         |
    1402             :     // |        |                             |                             |                                |                                         |
    1403             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1404             :     // |CLASS_2 |Prediction for Chroma        |Prediction for Luma & Chroma |Bypassed                        |T, Q, Q-1, T-1 for Luma & Chroma         |
    1405             :     // |        |No Interpolation Search      |Interpolation Search         |                                |Tx Search                                |
    1406             :     // |        |Bilinear Interpolation       |                             |                                |                                         |
    1407             :     // |        |                             |                             |                                |                                         |
    1408             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1409             :     // |CLASS_3 |Prediction for Chroma        |Prediction for Luma & Chroma |Bypassed                        |T, Q, Q-1, T-1 for Luma & Chroma         |
    1410             :     // |        |No Interpolation Search      |Interpolation Search         |                                |Tx Search                                |
    1411             :     // |        |Bilinear Interpolation       |                             |                                |                                         |
    1412             :     // |        |                             |                             |                                |                                         |
    1413             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1414             :     //
    1415             :     // MD_STAGING_MODE_2
    1416             :     //  _______________________________________________________________________________________________________________________________________________
    1417             :     // |        | md_stage_0                  | md_stage_1                  | md_stage_2                     | md_stage_3                              |
    1418             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1419             :     // |CLASS_0 |Prediction for Luma & Chroma |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 or Luma & Chroma          |
    1420             :     // |        |No Interpolation Search      |                             |No RDOQ                         |RDOQ                                     |
    1421             :     // |        |Regular Interpolation        |                             |No Tx Search                    |Tx Search                                |
    1422             :     // |        |                             |                             |No ATB                          |ATB                                      |
    1423             :     // |        |                             |                             |                                |CFL vs. Independent                      |
    1424             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1425             :     // |CLASS_1 |Prediction for Chroma        |Prediction for Luma & Chroma |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1426             :     // |        |No Interpolation Search      |Interpolation Search         |No RDOQ                         |Tx Search                                |
    1427             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1428             :     // |        |                             |                             |                                |                                         |
    1429             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1430             :     // |CLASS_2 |Prediction for Chroma        |Prediction for Luma & Chroma |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1431             :     // |        |No Interpolation Search      |Interpolation Search         |No RDOQ                         |Tx Search                                |
    1432             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1433             :     // |        |                             |                             |                                |                                         |
    1434             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1435             :     // |CLASS_3 |Prediction for Chroma        |Prediction for Luma & Chroma |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1436             :     // |        |No Interpolation Search      |Interpolation Search         |No RDOQ                         |Tx Search                                |
    1437             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1438             :     // |        |                             |                             |                                |                                         |
    1439             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1440             :     //
    1441             :     // MD_STAGING_MODE_3
    1442             :     //  _______________________________________________________________________________________________________________________________________________
    1443             :     // |        | md_stage_0                  | md_stage_1                  | md_stage_2                     | md_stage_3                              |
    1444             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1445             :     // |CLASS_0 |Prediction for Luma & Chroma |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 or Luma & Chroma          |
    1446             :     // |        |No Interpolation Search      |                             |No RDOQ                         |RDOQ                                     |
    1447             :     // |        |Regular Interpolation        |                             |No Tx Search                    |Tx Search                                |
    1448             :     // |        |                             |                             |No ATB                          |ATB                                      |
    1449             :     // |        |                             |                             |                                |CFL vs. Independent                      |
    1450             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1451             :     // |CLASS_1 |Prediction for Chroma        |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1452             :     // |        |No Interpolation Search      |                             |No RDOQ                         |Tx Search                                |
    1453             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1454             :     // |        |                             |                             |                                |                                         |
    1455             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1456             :     // |CLASS_2 |Prediction for Chroma        |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1457             :     // |        |No Interpolation Search      |                             |No RDOQ                         |Tx Search                                |
    1458             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1459             :     // |        |                             |                             |                                |                                         |
    1460             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1461             :     // |CLASS_3 |Prediction for Chroma        |Bypassed                     |T, Q, Q-1, T-1 for Luma         |T, Q, Q-1, T-1 for Luma & Chroma         |
    1462             :     // |        |No Interpolation Search      |                             |No RDOQ                         |Tx Search                                |
    1463             :     // |        |Bilinear Interpolation       |                             |No Tx Search                    |                                         |
    1464             :     // |        |                             |                             |                                |                                         |
    1465             :     // |________|_____________________________|_____________________________|________________________________|_________________________________________|
    1466             : 
    1467             :     if (picture_control_set_ptr->enc_mode == ENC_M0)
    1468             :         context_ptr->md_staging_mode = MD_STAGING_MODE_1;
    1469             :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1470             :         context_ptr->md_staging_mode = MD_STAGING_MODE_3;
    1471             :     else
    1472             :         context_ptr->md_staging_mode = MD_STAGING_MODE_0; // Default structure = fast loop + full loop = md_stage_0 + md_stage_3
    1473             : #endif
    1474             :     // Combine MD Class1&2
    1475             :     // 0                    OFF
    1476             :     // 1                    ON
    1477        1422 :     context_ptr->combine_class12 = (picture_control_set_ptr->enc_mode == ENC_M0) ? 0 : 1;
    1478             : 
    1479             :     // Set interpolation filter search blk size
    1480             :     // Level                Settings
    1481             :     // 0                    ON for 8x8 and above
    1482             :     // 1                    ON for 16x16 and above
    1483             :     // 2                    ON for 32x32 and above
    1484        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1485         659 :         context_ptr->interpolation_filter_search_blk_size = 0;
    1486             :     else
    1487         763 :         context_ptr->interpolation_filter_search_blk_size = 1;
    1488             : 
    1489             :     // Set PF MD
    1490        1422 :     context_ptr->pf_md_mode = PF_OFF;
    1491             : 
    1492             :     // Derive Spatial SSE Flag
    1493        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1494           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M6)
    1495           0 :             context_ptr->spatial_sse_full_loop = EB_TRUE;
    1496             :         else
    1497           0 :             context_ptr->spatial_sse_full_loop = EB_FALSE;
    1498             :     else
    1499        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1500         659 :         context_ptr->spatial_sse_full_loop = EB_TRUE;
    1501             :     else
    1502         763 :         context_ptr->spatial_sse_full_loop = EB_FALSE;
    1503             : 
    1504        1422 :     if (context_ptr->chroma_level <= CHROMA_MODE_1)
    1505         659 :         context_ptr->blk_skip_decision = EB_TRUE;
    1506             :     else
    1507         763 :         context_ptr->blk_skip_decision = EB_FALSE;
    1508             :     // Derive Trellis Quant Coeff Optimization Flag
    1509        1422 :     if (picture_control_set_ptr->enc_mode == ENC_M0)
    1510         659 :         context_ptr->trellis_quant_coeff_optimization = EB_TRUE;
    1511             :     else
    1512         763 :         context_ptr->trellis_quant_coeff_optimization = EB_FALSE;
    1513             : 
    1514             :     // Derive redundant block
    1515        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1516           0 :         if (picture_control_set_ptr->enc_mode <= ENC_M1)
    1517           0 :             context_ptr->redundant_blk = EB_TRUE;
    1518             :         else
    1519           0 :             context_ptr->redundant_blk = EB_FALSE;
    1520             :     else
    1521        1422 :     if (picture_control_set_ptr->enc_mode <= ENC_M5)
    1522         659 :         context_ptr->redundant_blk = EB_TRUE;
    1523             :     else
    1524         763 :         context_ptr->redundant_blk = EB_FALSE;
    1525        1422 :     if (sequence_control_set_ptr->static_config.encoder_bit_depth == EB_8BIT)
    1526             : #if FIX_ESTIMATE_INTRA
    1527             :         if (MR_MODE)
    1528             : #else
    1529             :         if (MR_MODE || picture_control_set_ptr->enc_mode == ENC_M0)
    1530             : #endif
    1531             :             context_ptr->edge_based_skip_angle_intra = 0;
    1532             :         else
    1533             : #if FIX_ESTIMATE_INTRA
    1534        1422 :             context_ptr->edge_based_skip_angle_intra = (picture_control_set_ptr->enc_mode == ENC_M0 && picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? 0 : 1;
    1535             : #else
    1536             :             context_ptr->edge_based_skip_angle_intra = 1;
    1537             : #endif
    1538             :     else
    1539           0 :         context_ptr->edge_based_skip_angle_intra = 0;
    1540        1422 :     if (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected || picture_control_set_ptr->enc_mode == ENC_M0)
    1541         660 :         context_ptr->prune_ref_frame_for_rec_partitions = 0;
    1542             :     else
    1543         762 :         context_ptr->prune_ref_frame_for_rec_partitions = 1;
    1544             : 
    1545             : #if SPEED_OPT
    1546             :     // Derive INTER/INTER WEDGE variance TH
    1547             :     if (MR_MODE)
    1548             :         context_ptr->inter_inter_wedge_variance_th = 0;
    1549             :     else
    1550        1422 :         context_ptr->inter_inter_wedge_variance_th = 100;
    1551             : 
    1552             :     // Derive MD Exit TH
    1553             :     if (MR_MODE)
    1554             :         context_ptr->md_exit_th = 0;
    1555             :     else
    1556        1422 :         context_ptr->md_exit_th = (picture_control_set_ptr->parent_pcs_ptr->sc_content_detected) ? 10 : 18;
    1557             : 
    1558             : #if INTER_INTRA_CLASS_PRUNING
    1559             : 
    1560             :     // TH_S (for single candidate removal per class)
    1561             :     // Remove candidate if deviation to the best is higher than TH_S
    1562        1422 :     if (MR_MODE || picture_control_set_ptr->enc_mode == ENC_M0 || sequence_control_set_ptr->input_resolution == INPUT_SIZE_576p_RANGE_OR_LOWER)
    1563        1422 :         context_ptr->md_stage_1_cand_prune_th = (uint64_t)~0;
    1564           0 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1565           0 :         context_ptr->md_stage_1_cand_prune_th = sequence_control_set_ptr->static_config.md_stage_1_cand_prune_th;
    1566             :     else
    1567           0 :         context_ptr->md_stage_1_cand_prune_th = (uint64_t)~0;
    1568             : #else
    1569             :     if (MR_MODE)
    1570             :         context_ptr->dist_base_md_stage_0_count_th = (uint64_t)~0;
    1571             :     else
    1572             :         context_ptr->dist_base_md_stage_0_count_th = 75;
    1573             : #endif
    1574             : #endif
    1575             : 
    1576             : #if INTER_INTRA_CLASS_PRUNING
    1577             : 
    1578             :     // TH_C (for class removal)
    1579             :     // Remove class if deviation to the best higher than TH_C
    1580        1422 :     if (MR_MODE || picture_control_set_ptr->enc_mode == ENC_M0 || sequence_control_set_ptr->input_resolution == INPUT_SIZE_576p_RANGE_OR_LOWER)
    1581        1422 :         context_ptr->md_stage_1_class_prune_th = (uint64_t)~0;
    1582           0 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1583           0 :         context_ptr->md_stage_1_class_prune_th = sequence_control_set_ptr->static_config.md_stage_1_class_prune_th;
    1584             :     else
    1585           0 :         context_ptr->md_stage_1_class_prune_th = (uint64_t)~0;
    1586             : 
    1587             :     // TH_S (for single candidate removal per class)
    1588             :     // Remove candidate if deviation to the best is higher than TH_S
    1589        1422 :     if (MR_MODE || picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1590           0 :         context_ptr->md_stage_2_cand_prune_th = (uint64_t)~0;
    1591        1422 :     else if (picture_control_set_ptr->enc_mode <= ENC_M0)
    1592         659 :         context_ptr->md_stage_2_cand_prune_th = sequence_control_set_ptr->static_config.md_stage_2_cand_prune_th;
    1593         763 :     else if (picture_control_set_ptr->enc_mode <= ENC_M2)
    1594           0 :         context_ptr->md_stage_2_cand_prune_th = sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE ? 15 : 12;
    1595         763 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1596           0 :         context_ptr->md_stage_2_cand_prune_th = sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE ? 5 : 3;
    1597             :     else
    1598         763 :         context_ptr->md_stage_2_cand_prune_th = (uint64_t)~0;
    1599             :     
    1600             :     // TH_C (for class removal)
    1601             :     // Remove class if deviation to the best is higher than TH_C
    1602        1422 :     if (MR_MODE || picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
    1603           0 :         context_ptr->md_stage_2_class_prune_th = (uint64_t)~0;
    1604        1422 :     else if (picture_control_set_ptr->enc_mode <= ENC_M4)
    1605         659 :         context_ptr->md_stage_2_class_prune_th = sequence_control_set_ptr->static_config.md_stage_2_class_prune_th;
    1606             :     else // to be tested for m5-m8
    1607         763 :         context_ptr->md_stage_2_class_prune_th = (uint64_t)~0;
    1608             : 
    1609             : #endif
    1610             : 
    1611             : #if LESS_RECTANGULAR_CHECK_LEVEL
    1612             : 
    1613             :     // Weighting (expressed as a percentage) applied to
    1614             :     // square shape costs for determining if a and b
    1615             :     // shapes should be skipped. Namely:
    1616             :     // skip HA and HB if h_cost > (weighted sq_cost)
    1617             :     // skip VA and VB if v_cost > (weighted sq_cost)
    1618             : 
    1619             :     if (MR_MODE)
    1620             :         context_ptr->sq_weight = (uint32_t)~0;
    1621             :     else
    1622        1422 :         context_ptr->sq_weight = sequence_control_set_ptr->static_config.sq_weight;
    1623             : 
    1624             : #endif
    1625             : 
    1626             : 
    1627        1422 :     return return_error;
    1628             : }
    1629             : #if! PAL_SUP
    1630             : void move_cu_data(
    1631             :     CodingUnit *src_cu,
    1632             :     CodingUnit *dst_cu);
    1633             : #endif
    1634             : 
    1635             : void av1_estimate_syntax_rate___partial(
    1636             :     MdRateEstimationContext        *md_rate_estimation_array,
    1637             :     FRAME_CONTEXT                  *fc);
    1638             : /******************************************************
    1639             :  * EncDec Kernel
    1640             :  ******************************************************/
    1641          15 : void* enc_dec_kernel(void *input_ptr)
    1642             : {
    1643             :     // Context & SCS & PCS
    1644          15 :     EncDecContext                         *context_ptr = (EncDecContext*)input_ptr;
    1645             :     PictureControlSet                     *picture_control_set_ptr;
    1646             :     SequenceControlSet                    *sequence_control_set_ptr;
    1647             : 
    1648             :     // Input
    1649             :     EbObjectWrapper                       *encDecTasksWrapperPtr;
    1650             :     EncDecTasks                           *encDecTasksPtr;
    1651             : 
    1652             :     // Output
    1653             :     EbObjectWrapper                       *encDecResultsWrapperPtr;
    1654             :     EncDecResults                         *encDecResultsPtr;
    1655             :     // SB Loop variables
    1656             :     LargestCodingUnit                       *sb_ptr;
    1657             :     uint16_t                                 sb_index;
    1658             :     uint8_t                                  sb_sz;
    1659             :     uint8_t                                  lcuSizeLog2;
    1660             :     uint32_t                                 x_lcu_index;
    1661             :     uint32_t                                 y_lcu_index;
    1662             :     uint32_t                                 sb_origin_x;
    1663             :     uint32_t                                 sb_origin_y;
    1664             :     EbBool                                   lastLcuFlag;
    1665             :     EbBool                                   endOfRowFlag;
    1666             :     uint32_t                                 lcuRowIndexStart;
    1667             :     uint32_t                                 lcuRowIndexCount;
    1668             :     uint32_t                                 picture_width_in_sb;
    1669             :     MdcLcuData                              *mdcPtr;
    1670             : 
    1671             :     // Variables
    1672             :     EbBool                                   is16bit;
    1673             : 
    1674             :     // Segments
    1675             :     //EbBool                                 initialProcessCall;
    1676             :     uint16_t                                 segment_index;
    1677             :     uint32_t                                 xLcuStartIndex;
    1678             :     uint32_t                                 yLcuStartIndex;
    1679             :     uint32_t                                 lcuStartIndex;
    1680             :     uint32_t                                 lcuSegmentCount;
    1681             :     uint32_t                                 lcuSegmentIndex;
    1682             :     uint32_t                                 segmentRowIndex;
    1683             :     uint32_t                                 segmentBandIndex;
    1684             :     uint32_t                                 segmentBandSize;
    1685             :     EncDecSegments                          *segments_ptr;
    1686             : 
    1687          15 :     segment_index = 0;
    1688             : 
    1689             :     for (;;) {
    1690             :         // Get Mode Decision Results
    1691        1437 :         eb_get_full_object(
    1692             :             context_ptr->mode_decision_input_fifo_ptr,
    1693             :             &encDecTasksWrapperPtr);
    1694             : 
    1695        1421 :         encDecTasksPtr = (EncDecTasks*)encDecTasksWrapperPtr->object_ptr;
    1696        1421 :         picture_control_set_ptr = (PictureControlSet*)encDecTasksPtr->picture_control_set_wrapper_ptr->object_ptr;
    1697        1421 :         sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    1698        1421 :         segments_ptr = picture_control_set_ptr->enc_dec_segment_ctrl;
    1699        1421 :         lastLcuFlag = EB_FALSE;
    1700        1421 :         is16bit = (EbBool)(sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
    1701             :         (void)is16bit;
    1702             :         (void)endOfRowFlag;
    1703             : 
    1704             :         // EncDec Kernel Signal(s) derivation
    1705             : 
    1706        1421 :         signal_derivation_enc_dec_kernel_oq(
    1707             :             sequence_control_set_ptr,
    1708             :             picture_control_set_ptr,
    1709             :             context_ptr->md_context);
    1710             : 
    1711             :         // SB Constants
    1712        1422 :         sb_sz = (uint8_t)sequence_control_set_ptr->sb_size_pix;
    1713        1422 :         lcuSizeLog2 = (uint8_t)Log2f(sb_sz);
    1714        1422 :         context_ptr->sb_sz = sb_sz;
    1715        1422 :         picture_width_in_sb = (sequence_control_set_ptr->seq_header.max_frame_width + sb_sz - 1) >> lcuSizeLog2;
    1716        1422 :         endOfRowFlag = EB_FALSE;
    1717        1422 :         lcuRowIndexStart = lcuRowIndexCount = 0;
    1718        1422 :         context_ptr->tot_intra_coded_area = 0;
    1719             : 
    1720             :         // Segment-loop
    1721        8620 :         while (AssignEncDecSegments(segments_ptr, &segment_index, encDecTasksPtr, context_ptr->enc_dec_feedback_fifo_ptr) == EB_TRUE)
    1722             :         {
    1723        7200 :             xLcuStartIndex = segments_ptr->x_start_array[segment_index];
    1724        7200 :             yLcuStartIndex = segments_ptr->y_start_array[segment_index];
    1725        7200 :             lcuStartIndex = yLcuStartIndex * picture_width_in_sb + xLcuStartIndex;
    1726        7200 :             lcuSegmentCount = segments_ptr->valid_lcu_count_array[segment_index];
    1727             : 
    1728        7200 :             segmentRowIndex = segment_index / segments_ptr->segment_band_count;
    1729        7200 :             segmentBandIndex = segment_index - segmentRowIndex * segments_ptr->segment_band_count;
    1730        7200 :             segmentBandSize = (segments_ptr->lcu_band_count * (segmentBandIndex + 1) + segments_ptr->segment_band_count - 1) / segments_ptr->segment_band_count;
    1731             : 
    1732             :             // Reset Coding Loop State
    1733        7200 :             reset_mode_decision(
    1734             : #if EIGHT_PEL_PREDICTIVE_ME
    1735             :                 sequence_control_set_ptr,
    1736             : #endif
    1737             :                 context_ptr->md_context,
    1738             :                 picture_control_set_ptr,
    1739             :                 segment_index);
    1740             : 
    1741             :             // Reset EncDec Coding State
    1742        7197 :             ResetEncDec(    // HT done
    1743             :                 context_ptr,
    1744             :                 picture_control_set_ptr,
    1745             :                 sequence_control_set_ptr,
    1746             :                 segment_index);
    1747             : 
    1748        7198 :             if (picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr != NULL)
    1749        4080 :                 ((EbReferenceObject  *)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->average_intensity = picture_control_set_ptr->parent_pcs_ptr->average_intensity[0];
    1750       14396 :             for (y_lcu_index = yLcuStartIndex, lcuSegmentIndex = lcuStartIndex; lcuSegmentIndex < lcuStartIndex + lcuSegmentCount; ++y_lcu_index) {
    1751       14397 :                 for (x_lcu_index = xLcuStartIndex; x_lcu_index < picture_width_in_sb && (x_lcu_index + y_lcu_index < segmentBandSize) && lcuSegmentIndex < lcuStartIndex + lcuSegmentCount; ++x_lcu_index, ++lcuSegmentIndex) {
    1752        7199 :                     sb_index = (uint16_t)(y_lcu_index * picture_width_in_sb + x_lcu_index);
    1753        7199 :                     sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
    1754        7199 :                     sb_origin_x = x_lcu_index << lcuSizeLog2;
    1755        7199 :                     sb_origin_y = y_lcu_index << lcuSizeLog2;
    1756        7199 :                     lastLcuFlag = (sb_index == sequence_control_set_ptr->sb_tot_cnt - 1) ? EB_TRUE : EB_FALSE;
    1757        7199 :                     endOfRowFlag = (x_lcu_index == picture_width_in_sb - 1) ? EB_TRUE : EB_FALSE;
    1758        7199 :                     lcuRowIndexStart = (x_lcu_index == picture_width_in_sb - 1 && lcuRowIndexCount == 0) ? y_lcu_index : lcuRowIndexStart;
    1759        7199 :                     lcuRowIndexCount = (x_lcu_index == picture_width_in_sb - 1) ? lcuRowIndexCount + 1 : lcuRowIndexCount;
    1760        7199 :                     mdcPtr = &picture_control_set_ptr->mdc_sb_array[sb_index];
    1761        7199 :                     context_ptr->sb_index = sb_index;
    1762        7199 :                     context_ptr->md_context->cu_use_ref_src_flag = (picture_control_set_ptr->parent_pcs_ptr->use_src_ref) && (picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index].edge_block_num == EB_FALSE || picture_control_set_ptr->parent_pcs_ptr->sb_flat_noise_array[sb_index]) ? EB_TRUE : EB_FALSE;
    1763             : 
    1764        7199 :                     if (picture_control_set_ptr->update_cdf) {
    1765        3600 :                         picture_control_set_ptr->rate_est_array[sb_index] = *picture_control_set_ptr->md_rate_estimation_array;
    1766             : #if CABAC_SERIAL
    1767             :                         if (sb_index == 0)
    1768             :                             picture_control_set_ptr->ec_ctx_array[sb_index] = *picture_control_set_ptr->coeff_est_entropy_coder_ptr->fc;
    1769             :                         else
    1770             :                             picture_control_set_ptr->ec_ctx_array[sb_index] = picture_control_set_ptr->ec_ctx_array[sb_index - 1];
    1771             : #else
    1772        3600 :                         if (sb_origin_x == 0)
    1773         360 :                             picture_control_set_ptr->ec_ctx_array[sb_index] = *picture_control_set_ptr->coeff_est_entropy_coder_ptr->fc;
    1774             :                         else
    1775        3240 :                             picture_control_set_ptr->ec_ctx_array[sb_index] = picture_control_set_ptr->ec_ctx_array[sb_index - 1];
    1776             : #endif
    1777             : 
    1778             :                         //construct the tables using the latest CDFs : Coeff Only here ---to check if I am using all the uptodate CDFs here
    1779        3600 :                         av1_estimate_syntax_rate___partial(
    1780        3600 :                             &picture_control_set_ptr->rate_est_array[sb_index],
    1781        3600 :                             &picture_control_set_ptr->ec_ctx_array[sb_index]);
    1782             : 
    1783        3583 :                         av1_estimate_coefficients_rate(
    1784        3583 :                             &picture_control_set_ptr->rate_est_array[sb_index],
    1785        3583 :                             &picture_control_set_ptr->ec_ctx_array[sb_index]);
    1786             : 
    1787             :                         //let the candidate point to the new rate table.
    1788             :                         uint32_t  candidateIndex;
    1789     6355780 :                         for (candidateIndex = 0; candidateIndex < MODE_DECISION_CANDIDATE_MAX_COUNT; ++candidateIndex)
    1790     6352180 :                             context_ptr->md_context->fast_candidate_ptr_array[candidateIndex]->md_rate_estimation_ptr = &picture_control_set_ptr->rate_est_array[sb_index];
    1791             :                     }
    1792             :                     // Configure the LCU
    1793        7197 :                     mode_decision_configure_lcu(
    1794             :                         context_ptr->md_context,
    1795             :                         picture_control_set_ptr,
    1796        7197 :                         (uint8_t)sb_ptr->qp);
    1797             : 
    1798             :                     uint32_t lcuRow;
    1799        7199 :                     if (picture_control_set_ptr->parent_pcs_ptr->enable_in_loop_motion_estimation_flag) {
    1800             :                         EbPictureBufferDesc       *input_picture_ptr;
    1801             : 
    1802           0 :                         input_picture_ptr = picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr;
    1803             : 
    1804             :                         // Load the SB from the input to the intermediate SB buffer
    1805           0 :                         uint32_t bufferIndex = (input_picture_ptr->origin_y + sb_origin_y) * input_picture_ptr->stride_y + input_picture_ptr->origin_x + sb_origin_x;
    1806             : 
    1807             :                         // Copy the source superblock to the me local buffer
    1808           0 :                         uint32_t sb_height = (sequence_control_set_ptr->seq_header.max_frame_height - sb_origin_y) < MAX_SB_SIZE ? sequence_control_set_ptr->seq_header.max_frame_height - sb_origin_y : MAX_SB_SIZE;
    1809           0 :                         uint32_t sb_width = (sequence_control_set_ptr->seq_header.max_frame_width - sb_origin_x) < MAX_SB_SIZE ? sequence_control_set_ptr->seq_header.max_frame_width - sb_origin_x : MAX_SB_SIZE;
    1810           0 :                         uint32_t is_complete_sb = sequence_control_set_ptr->sb_geom[sb_index].is_complete_sb;
    1811             : 
    1812           0 :                         if (!is_complete_sb)
    1813           0 :                             memset(context_ptr->ss_mecontext->sb_buffer, 0, MAX_SB_SIZE*MAX_SB_SIZE);
    1814           0 :                         for (lcuRow = 0; lcuRow < sb_height; lcuRow++) {
    1815           0 :                             EB_MEMCPY((&(context_ptr->ss_mecontext->sb_buffer[lcuRow * MAX_SB_SIZE])), (&(input_picture_ptr->buffer_y[bufferIndex + lcuRow * input_picture_ptr->stride_y])), sb_width * sizeof(uint8_t));
    1816             :                         }
    1817             : 
    1818           0 :                         context_ptr->ss_mecontext->sb_src_ptr = &(context_ptr->ss_mecontext->sb_buffer[0]);
    1819           0 :                         context_ptr->ss_mecontext->sb_src_stride = context_ptr->ss_mecontext->sb_buffer_stride;
    1820             :                         // Set in-loop ME Search Area
    1821             :                         int16_t mv_l0_x;
    1822             :                         int16_t mv_l0_y;
    1823             :                         int16_t mv_l1_x;
    1824             :                         int16_t mv_l1_y;
    1825             : 
    1826           0 :                         mv_l0_x = 0;
    1827           0 :                         mv_l0_y = 0;
    1828           0 :                         mv_l1_x = 0;
    1829           0 :                         mv_l1_y = 0;
    1830             : 
    1831           0 :                         context_ptr->ss_mecontext->search_area_width = 64;
    1832           0 :                         context_ptr->ss_mecontext->search_area_height = 64;
    1833             : 
    1834             :                         // perform in-loop ME
    1835           0 :                         in_loop_motion_estimation_sblock(
    1836             :                             picture_control_set_ptr,
    1837             :                             sb_origin_x,
    1838             :                             sb_origin_y,
    1839             :                             mv_l0_x,
    1840             :                             mv_l0_y,
    1841             :                             mv_l1_x,
    1842             :                             mv_l1_y,
    1843             :                             context_ptr->ss_mecontext);
    1844             :                     }
    1845             : 
    1846        7199 :                     mode_decision_sb(
    1847             :                         sequence_control_set_ptr,
    1848             :                         picture_control_set_ptr,
    1849             :                         mdcPtr,
    1850             :                         sb_ptr,
    1851             :                         sb_origin_x,
    1852             :                         sb_origin_y,
    1853             :                         sb_index,
    1854             :                         context_ptr->ss_mecontext,
    1855             :                         context_ptr->md_context);
    1856             : 
    1857             :                     // Configure the LCU
    1858        7198 :                     EncDecConfigureLcu(
    1859             :                         context_ptr,
    1860             :                         sb_ptr,
    1861             :                         picture_control_set_ptr,
    1862        7198 :                         (uint8_t)sb_ptr->qp);
    1863             : 
    1864             : #if NO_ENCDEC
    1865             :                     no_enc_dec_pass(
    1866             :                         sequence_control_set_ptr,
    1867             :                         picture_control_set_ptr,
    1868             :                         sb_ptr,
    1869             :                         sb_index,
    1870             :                         sb_origin_x,
    1871             :                         sb_origin_y,
    1872             :                         sb_ptr->qp,
    1873             :                         context_ptr);
    1874             : #else
    1875             :                     // Encode Pass
    1876        7199 :                     av1_encode_pass(
    1877             :                         sequence_control_set_ptr,
    1878             :                         picture_control_set_ptr,
    1879             :                         sb_ptr,
    1880             :                         sb_index,
    1881             :                         sb_origin_x,
    1882             :                         sb_origin_y,
    1883             :                         context_ptr);
    1884             : #endif
    1885             : 
    1886        7199 :                     if (picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr != NULL)
    1887        4080 :                         ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->intra_coded_area_sb[sb_index] = (uint8_t)((100 * context_ptr->intra_coded_area_sb[sb_index]) / (64 * 64));
    1888             :                 }
    1889        7198 :                 xLcuStartIndex = (xLcuStartIndex > 0) ? xLcuStartIndex - 1 : 0;
    1890             :             }
    1891             :         }
    1892             : 
    1893        1422 :         eb_block_on_mutex(picture_control_set_ptr->intra_mutex);
    1894        1422 :         picture_control_set_ptr->intra_coded_area += (uint32_t)context_ptr->tot_intra_coded_area;
    1895        1422 :         eb_release_mutex(picture_control_set_ptr->intra_mutex);
    1896             : 
    1897        1422 :         if (lastLcuFlag) {
    1898             :             // Copy film grain data from parent picture set to the reference object for further reference
    1899         120 :             if (sequence_control_set_ptr->seq_header.film_grain_params_present)
    1900             :             {
    1901           0 :                 if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE && picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr) {
    1902           0 :                     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->film_grain_params
    1903           0 :                         = picture_control_set_ptr->parent_pcs_ptr->frm_hdr.film_grain_params;
    1904             :                 }
    1905             :             }
    1906         120 :             if (picture_control_set_ptr->parent_pcs_ptr->frame_end_cdf_update_mode && picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE && picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr)
    1907         304 :                 for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame)
    1908         266 :                     ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->global_motion[frame]
    1909         266 :                     = picture_control_set_ptr->parent_pcs_ptr->global_motion[frame];
    1910         120 :             EB_MEMCPY(picture_control_set_ptr->parent_pcs_ptr->av1x->sgrproj_restore_cost, context_ptr->md_rate_estimation_ptr->sgrproj_restore_fac_bits, 2 * sizeof(int32_t));
    1911         120 :             EB_MEMCPY(picture_control_set_ptr->parent_pcs_ptr->av1x->switchable_restore_cost, context_ptr->md_rate_estimation_ptr->switchable_restore_fac_bits, 3 * sizeof(int32_t));
    1912         120 :             EB_MEMCPY(picture_control_set_ptr->parent_pcs_ptr->av1x->wiener_restore_cost, context_ptr->md_rate_estimation_ptr->wiener_restore_fac_bits, 2 * sizeof(int32_t));
    1913         120 :             picture_control_set_ptr->parent_pcs_ptr->av1x->rdmult = context_ptr->full_lambda;
    1914             :         }
    1915             : 
    1916        1422 :         if (lastLcuFlag)
    1917             :         {
    1918             :             // Get Empty EncDec Results
    1919         120 :             eb_get_empty_object(
    1920             :                 context_ptr->enc_dec_output_fifo_ptr,
    1921             :                 &encDecResultsWrapperPtr);
    1922         120 :             encDecResultsPtr = (EncDecResults*)encDecResultsWrapperPtr->object_ptr;
    1923         120 :             encDecResultsPtr->picture_control_set_wrapper_ptr = encDecTasksPtr->picture_control_set_wrapper_ptr;
    1924             :             //CHKN these are not needed for DLF
    1925         120 :             encDecResultsPtr->completed_lcu_row_index_start = 0;
    1926         120 :             encDecResultsPtr->completed_lcu_row_count = ((sequence_control_set_ptr->seq_header.max_frame_height + sequence_control_set_ptr->sb_size_pix - 1) >> lcuSizeLog2);
    1927             :             // Post EncDec Results
    1928         120 :             eb_post_full_object(encDecResultsWrapperPtr);
    1929             :         }
    1930             :         // Release Mode Decision Results
    1931        1422 :         eb_release_object(encDecTasksWrapperPtr);
    1932             :     }
    1933             :     return EB_NULL;
    1934             : }
    1935             : 
    1936           0 : void eb_av1_add_film_grain(EbPictureBufferDesc *src,
    1937             :     EbPictureBufferDesc *dst,
    1938             :     aom_film_grain_t *film_grain_ptr) {
    1939             :     uint8_t *luma, *cb, *cr;
    1940             :     int32_t height, width, luma_stride, chroma_stride;
    1941           0 :     int32_t use_high_bit_depth = 0;
    1942           0 :     int32_t chroma_subsamp_x = 0;
    1943           0 :     int32_t chroma_subsamp_y = 0;
    1944             : 
    1945           0 :     aom_film_grain_t params = *film_grain_ptr;
    1946             : 
    1947           0 :     switch (src->bit_depth) {
    1948           0 :     case EB_8BIT:
    1949           0 :         params.bit_depth = 8;
    1950           0 :         use_high_bit_depth = 0;
    1951           0 :         chroma_subsamp_x = 1;
    1952           0 :         chroma_subsamp_y = 1;
    1953           0 :         break;
    1954           0 :     case EB_10BIT:
    1955           0 :         params.bit_depth = 10;
    1956           0 :         use_high_bit_depth = 1;
    1957           0 :         chroma_subsamp_x = 1;
    1958           0 :         chroma_subsamp_y = 1;
    1959           0 :         break;
    1960           0 :     default:  //todo: Throw an error if unknown format?
    1961           0 :         params.bit_depth = 10;
    1962           0 :         use_high_bit_depth = 1;
    1963           0 :         chroma_subsamp_x = 1;
    1964           0 :         chroma_subsamp_y = 1;
    1965             :     }
    1966             : 
    1967           0 :     dst->max_width = src->max_width;
    1968           0 :     dst->max_height = src->max_height;
    1969             : 
    1970           0 :     fgn_copy_rect(src->buffer_y + ((src->origin_y * src->stride_y + src->origin_x) << use_high_bit_depth), src->stride_y,
    1971           0 :         dst->buffer_y + ((dst->origin_y * dst->stride_y + dst->origin_x) << use_high_bit_depth), dst->stride_y,
    1972           0 :         dst->width, dst->height, use_high_bit_depth);
    1973             : 
    1974           0 :     fgn_copy_rect(src->buffer_cb + ((src->stride_cb * (src->origin_y >> chroma_subsamp_y)
    1975           0 :         + (src->origin_x >> chroma_subsamp_x)) << use_high_bit_depth), src->stride_cb,
    1976           0 :         dst->buffer_cb + ((dst->stride_cb * (dst->origin_y >> chroma_subsamp_y)
    1977           0 :             + (dst->origin_x >> chroma_subsamp_x)) << use_high_bit_depth), dst->stride_cb,
    1978           0 :         dst->width >> chroma_subsamp_x, dst->height >> chroma_subsamp_y,
    1979             :         use_high_bit_depth);
    1980             : 
    1981           0 :     fgn_copy_rect(src->buffer_cr + ((src->stride_cr * (src->origin_y >> chroma_subsamp_y)
    1982           0 :         + (src->origin_x >> chroma_subsamp_x)) << use_high_bit_depth), src->stride_cr,
    1983           0 :         dst->buffer_cr + ((dst->stride_cr * (dst->origin_y >> chroma_subsamp_y)
    1984           0 :             + (dst->origin_x >> chroma_subsamp_x)) << use_high_bit_depth), dst->stride_cr,
    1985           0 :         dst->width >> chroma_subsamp_x, dst->height >> chroma_subsamp_y,
    1986             :         use_high_bit_depth);
    1987             : 
    1988           0 :     luma = dst->buffer_y + ((dst->origin_y * dst->stride_y + dst->origin_x) << use_high_bit_depth);
    1989           0 :     cb = dst->buffer_cb + ((dst->stride_cb * (dst->origin_y >> chroma_subsamp_y)
    1990           0 :         + (dst->origin_x >> chroma_subsamp_x)) << use_high_bit_depth);
    1991           0 :     cr = dst->buffer_cr + ((dst->stride_cr * (dst->origin_y >> chroma_subsamp_y)
    1992           0 :         + (dst->origin_x >> chroma_subsamp_x)) << use_high_bit_depth);
    1993             : 
    1994           0 :     luma_stride = dst->stride_y;
    1995           0 :     chroma_stride = dst->stride_cb;
    1996             : 
    1997           0 :     width = dst->width;
    1998           0 :     height = dst->height;
    1999             : 
    2000           0 :     eb_av1_add_film_grain_run(&params, luma, cb, cr, height, width, luma_stride,
    2001             :         chroma_stride, use_high_bit_depth, chroma_subsamp_y,
    2002             :         chroma_subsamp_x);
    2003           0 :     return;
    2004             : }

Generated by: LCOV version 1.14