LCOV - code coverage report
Current view: top level - home/magsoft/trunks/SVT-AV1/Source/Lib/Encoder/Codec - EbResourceCoordinationProcess.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 260 480 54.2 %
Date: 2019-11-25 17:12:20 Functions: 5 9 55.6 %

          Line data    Source code
       1             : /*
       2             : * Copyright(c) 2019 Intel Corporation
       3             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       4             : */
       5             : 
       6             : #include <stdlib.h>
       7             : #include <string.h>
       8             : 
       9             : #include "EbDefinitions.h"
      10             : #include "EbSystemResourceManager.h"
      11             : #include "EbPictureControlSet.h"
      12             : #include "EbSequenceControlSet.h"
      13             : #include "EbPictureBufferDesc.h"
      14             : #include "EbResourceCoordinationProcess.h"
      15             : #include "EbResourceCoordinationResults.h"
      16             : #include "EbTransforms.h"
      17             : #include "EbTime.h"
      18             : #include "EbEntropyCoding.h"
      19             : 
      20             : void set_tile_info(PictureParentControlSet * pcs_ptr);
      21           2 : void resource_coordination_context_dctor(EbPtr p)
      22             : {
      23           2 :     ResourceCoordinationContext *obj = (ResourceCoordinationContext*)p;
      24             : 
      25           2 :     EB_FREE_ARRAY(obj->sequenceControlSetActiveArray);
      26           2 :     EB_FREE_ARRAY(obj->picture_number_array);
      27           2 : }
      28             : 
      29             : /************************************************
      30             :  * Resource Coordination Context Constructor
      31             :  ************************************************/
      32           2 : EbErrorType resource_coordination_context_ctor(
      33             :     ResourceCoordinationContext *context_ptr,
      34             :     EbFifo                        *inputBufferFifoPtr,
      35             :     EbFifo                        *resource_coordination_results_output_fifo_ptr,
      36             :     EbFifo                        **picture_control_set_fifo_ptr_array,
      37             :     EbSequenceControlSetInstance  **sequence_control_set_instance_array,
      38             :     EbFifo                         *sequence_control_set_empty_fifo_ptr,
      39             :     EbCallback                    **app_callback_ptr_array,
      40             :     uint32_t                       compute_segments_total_count_array,
      41             :     uint32_t                        encode_instances_total_count){
      42             : 
      43           2 :     context_ptr->dctor = resource_coordination_context_dctor;
      44             : 
      45           2 :     context_ptr->input_buffer_fifo_ptr = inputBufferFifoPtr;
      46           2 :     context_ptr->resource_coordination_results_output_fifo_ptr = resource_coordination_results_output_fifo_ptr;
      47           2 :     context_ptr->picture_control_set_fifo_ptr_array = picture_control_set_fifo_ptr_array;
      48           2 :     context_ptr->sequence_control_set_instance_array = sequence_control_set_instance_array;
      49           2 :     context_ptr->sequence_control_set_empty_fifo_ptr = sequence_control_set_empty_fifo_ptr;
      50           2 :     context_ptr->app_callback_ptr_array = app_callback_ptr_array;
      51           2 :     context_ptr->compute_segments_total_count_array = compute_segments_total_count_array;
      52           2 :     context_ptr->encode_instances_total_count = encode_instances_total_count;
      53             : 
      54             :     // Allocate SequenceControlSetActiveArray
      55           2 :     EB_CALLOC_ARRAY(context_ptr->sequenceControlSetActiveArray, context_ptr->encode_instances_total_count);
      56             : 
      57           2 :     EB_CALLOC_ARRAY(context_ptr->picture_number_array, context_ptr->encode_instances_total_count);
      58             : 
      59           2 :     context_ptr->average_enc_mod = 0;
      60           2 :     context_ptr->prev_enc_mod = 0;
      61           2 :     context_ptr->prev_enc_mode_delta = 0;
      62           2 :     context_ptr->cur_speed = 0; // speed x 1000
      63           2 :     context_ptr->previous_mode_change_buffer = 0;
      64           2 :     context_ptr->first_in_pic_arrived_time_seconds = 0;
      65           2 :     context_ptr->first_in_pic_arrived_timeu_seconds = 0;
      66           2 :     context_ptr->previous_frame_in_check1 = 0;
      67           2 :     context_ptr->previous_frame_in_check2 = 0;
      68           2 :     context_ptr->previous_frame_in_check3 = 0;
      69           2 :     context_ptr->previous_mode_change_frame_in = 0;
      70           2 :     context_ptr->prevs_time_seconds = 0;
      71           2 :     context_ptr->prevs_timeu_seconds = 0;
      72           2 :     context_ptr->prev_frame_out = 0;
      73           2 :     context_ptr->start_flag = EB_FALSE;
      74             : 
      75           2 :     context_ptr->previous_buffer_check1 = 0;
      76           2 :     context_ptr->prev_change_cond = 0;
      77             : 
      78           2 :     return EB_ErrorNone;
      79             : }
      80             : 
      81             : /******************************************************
      82             : * Derive Pre-Analysis settings for OQ
      83             : Input   : encoder mode and tune
      84             : Output  : Pre-Analysis signal(s)
      85             : ******************************************************/
      86         122 : EbErrorType signal_derivation_pre_analysis_oq(
      87             :     SequenceControlSet       *sequence_control_set_ptr,
      88             :     PictureParentControlSet  *picture_control_set_ptr) {
      89         122 :     EbErrorType return_error = EB_ErrorNone;
      90         122 :     uint8_t input_resolution = sequence_control_set_ptr->input_resolution;
      91             : 
      92             :     // HME Flags updated @ signal_derivation_multi_processes_oq
      93             : #if TWO_PASS_USE_2NDP_ME_IN_1STP
      94         122 :     uint8_t  hme_me_level = sequence_control_set_ptr->use_output_stat_file ? picture_control_set_ptr->snd_pass_enc_mode : picture_control_set_ptr->enc_mode;
      95             : #else
      96             :     uint8_t  hme_me_level = picture_control_set_ptr->enc_mode;
      97             : #endif
      98             :     // Derive HME Flag
      99         122 :     if (sequence_control_set_ptr->static_config.use_default_me_hme) {
     100         122 :         picture_control_set_ptr->enable_hme_flag = enable_hme_flag[0][input_resolution][hme_me_level] || enable_hme_flag[1][input_resolution][hme_me_level];
     101         122 :         picture_control_set_ptr->enable_hme_level0_flag = enable_hme_level0_flag[0][input_resolution][hme_me_level] || enable_hme_level0_flag[1][input_resolution][hme_me_level];
     102         122 :         picture_control_set_ptr->enable_hme_level1_flag = enable_hme_level1_flag[0][input_resolution][hme_me_level] || enable_hme_level1_flag[1][input_resolution][hme_me_level];
     103         122 :         picture_control_set_ptr->enable_hme_level2_flag = enable_hme_level2_flag[0][input_resolution][hme_me_level] || enable_hme_level2_flag[1][input_resolution][hme_me_level];
     104             :     }
     105             :     else {
     106           0 :         picture_control_set_ptr->enable_hme_flag = sequence_control_set_ptr->static_config.enable_hme_flag;
     107           0 :         picture_control_set_ptr->enable_hme_level0_flag = sequence_control_set_ptr->static_config.enable_hme_level0_flag;
     108           0 :         picture_control_set_ptr->enable_hme_level1_flag = sequence_control_set_ptr->static_config.enable_hme_level1_flag;
     109           0 :         picture_control_set_ptr->enable_hme_level2_flag = sequence_control_set_ptr->static_config.enable_hme_level2_flag;
     110             :     }
     111         122 :     picture_control_set_ptr->tf_enable_hme_flag = tf_enable_hme_flag[0][input_resolution][hme_me_level] || tf_enable_hme_flag[1][input_resolution][hme_me_level];
     112         122 :     picture_control_set_ptr->tf_enable_hme_level0_flag = tf_enable_hme_level0_flag[0][input_resolution][hme_me_level] || tf_enable_hme_level0_flag[1][input_resolution][hme_me_level];
     113         122 :     picture_control_set_ptr->tf_enable_hme_level1_flag = tf_enable_hme_level1_flag[0][input_resolution][hme_me_level] || tf_enable_hme_level1_flag[1][input_resolution][hme_me_level];
     114         122 :     picture_control_set_ptr->tf_enable_hme_level2_flag = tf_enable_hme_level2_flag[0][input_resolution][hme_me_level] || tf_enable_hme_level2_flag[1][input_resolution][hme_me_level];
     115             : 
     116         122 :     if (picture_control_set_ptr->enc_mode >= ENC_M8)
     117          61 :         sequence_control_set_ptr->seq_header.enable_restoration = 0;
     118         122 :     sequence_control_set_ptr->cdf_mode = (picture_control_set_ptr->enc_mode <= ENC_M6) ? 0 : 1;
     119         122 :     return return_error;
     120             : }
     121             : 
     122             : //******************************************************************************//
     123             : // Modify the Enc mode based on the buffer Status
     124             : // Inputs: TargetSpeed, Status of the SCbuffer
     125             : // Output: EncMod
     126             : //******************************************************************************//
     127           0 : void SpeedBufferControl(
     128             :     ResourceCoordinationContext   *context_ptr,
     129             :     PictureParentControlSet       *picture_control_set_ptr,
     130             :     SequenceControlSet            *sequence_control_set_ptr)
     131             : {
     132           0 :     uint64_t cursTimeSeconds = 0;
     133           0 :     uint64_t cursTimeuSeconds = 0;
     134           0 :     double overallDuration = 0.0;
     135           0 :     double instDuration = 0.0;
     136           0 :     int8_t  encoderModeDelta = 0;
     137           0 :     int64_t inputFramesCount = 0;
     138           0 :     int8_t changeCond = 0;
     139           0 :     int64_t targetFps = (sequence_control_set_ptr->static_config.injector_frame_rate >> 16);
     140             : 
     141           0 :     int64_t bufferTrshold1 = SC_FRAMES_INTERVAL_T1;
     142           0 :     int64_t bufferTrshold2 = SC_FRAMES_INTERVAL_T2;
     143           0 :     int64_t bufferTrshold3 = MIN(targetFps * 3, SC_FRAMES_INTERVAL_T3);
     144           0 :     eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->sc_buffer_mutex);
     145             : 
     146           0 :     if (sequence_control_set_ptr->encode_context_ptr->sc_frame_in == 0)
     147           0 :         EbStartTime(&context_ptr->first_in_pic_arrived_time_seconds, &context_ptr->first_in_pic_arrived_timeu_seconds);
     148           0 :     else if (sequence_control_set_ptr->encode_context_ptr->sc_frame_in == SC_FRAMES_TO_IGNORE)
     149           0 :         context_ptr->start_flag = EB_TRUE;
     150             :     // Compute duration since the start of the encode and since the previous checkpoint
     151           0 :     EbFinishTime(&cursTimeSeconds, &cursTimeuSeconds);
     152             : 
     153           0 :     EbComputeOverallElapsedTimeMs(
     154             :         context_ptr->first_in_pic_arrived_time_seconds,
     155             :         context_ptr->first_in_pic_arrived_timeu_seconds,
     156             :         cursTimeSeconds,
     157             :         cursTimeuSeconds,
     158             :         &overallDuration);
     159             : 
     160           0 :     EbComputeOverallElapsedTimeMs(
     161             :         context_ptr->prevs_time_seconds,
     162             :         context_ptr->prevs_timeu_seconds,
     163             :         cursTimeSeconds,
     164             :         cursTimeuSeconds,
     165             :         &instDuration);
     166             : 
     167           0 :     inputFramesCount = (int64_t)overallDuration *(sequence_control_set_ptr->static_config.injector_frame_rate >> 16) / 1000;
     168           0 :     sequence_control_set_ptr->encode_context_ptr->sc_buffer = inputFramesCount - sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     169             : 
     170           0 :     encoderModeDelta = 0;
     171             : 
     172             :     // Check every bufferTsshold1 for the changes (previous_frame_in_check1 variable)
     173           0 :     if ((sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_frame_in_check1 + bufferTrshold1 && sequence_control_set_ptr->encode_context_ptr->sc_frame_in >= SC_FRAMES_TO_IGNORE)) {
     174             :         // Go to a slower mode based on the fullness and changes of the buffer
     175           0 :         if (sequence_control_set_ptr->encode_context_ptr->sc_buffer < targetFps && (context_ptr->prev_enc_mode_delta > -1 || (context_ptr->prev_enc_mode_delta < 0 && sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_mode_change_frame_in + targetFps * 2))) {
     176           0 :             if (context_ptr->previous_buffer_check1 > sequence_control_set_ptr->encode_context_ptr->sc_buffer + bufferTrshold1) {
     177           0 :                 encoderModeDelta += -1;
     178           0 :                 changeCond = 2;
     179             :             }
     180           0 :             else if (context_ptr->previous_mode_change_buffer > bufferTrshold1 + sequence_control_set_ptr->encode_context_ptr->sc_buffer && sequence_control_set_ptr->encode_context_ptr->sc_buffer < bufferTrshold1) {
     181           0 :                 encoderModeDelta += -1;
     182           0 :                 changeCond = 4;
     183             :             }
     184             :         }
     185             : 
     186             :         // Go to a faster mode based on the fullness and changes of the buffer
     187           0 :         if (sequence_control_set_ptr->encode_context_ptr->sc_buffer > bufferTrshold1 + context_ptr->previous_buffer_check1) {
     188           0 :             encoderModeDelta += +1;
     189           0 :             changeCond = 1;
     190             :         }
     191           0 :         else if (sequence_control_set_ptr->encode_context_ptr->sc_buffer > bufferTrshold1 + context_ptr->previous_mode_change_buffer) {
     192           0 :             encoderModeDelta += +1;
     193           0 :             changeCond = 3;
     194             :         }
     195             : 
     196             :         // Update the encode mode based on the fullness of the buffer
     197             :         // If previous ChangeCond was the same, double the threshold2
     198           0 :         if (sequence_control_set_ptr->encode_context_ptr->sc_buffer > bufferTrshold3 &&
     199           0 :             (context_ptr->prev_change_cond != 7 || sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_mode_change_frame_in + bufferTrshold2 * 2) &&
     200           0 :             sequence_control_set_ptr->encode_context_ptr->sc_buffer > context_ptr->previous_mode_change_buffer) {
     201           0 :             encoderModeDelta += 1;
     202           0 :             changeCond = 7;
     203             :         }
     204           0 :         encoderModeDelta = CLIP3(-1, 1, encoderModeDelta);
     205           0 :         sequence_control_set_ptr->encode_context_ptr->enc_mode = (EbEncMode)CLIP3(1, MAX_ENC_PRESET, (int8_t)sequence_control_set_ptr->encode_context_ptr->enc_mode + encoderModeDelta);
     206             : 
     207             :         // Update previous stats
     208           0 :         context_ptr->previous_frame_in_check1 = sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     209           0 :         context_ptr->previous_buffer_check1 = sequence_control_set_ptr->encode_context_ptr->sc_buffer;
     210             : 
     211           0 :         if (encoderModeDelta) {
     212           0 :             context_ptr->previous_mode_change_buffer = sequence_control_set_ptr->encode_context_ptr->sc_buffer;
     213           0 :             context_ptr->previous_mode_change_frame_in = sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     214           0 :             context_ptr->prev_enc_mode_delta = encoderModeDelta;
     215             :         }
     216             :     }
     217             : 
     218             :     // Check every bufferTrshold2 for the changes (previous_frame_in_check2 variable)
     219           0 :     if ((sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_frame_in_check2 + bufferTrshold2 && sequence_control_set_ptr->encode_context_ptr->sc_frame_in >= SC_FRAMES_TO_IGNORE)) {
     220           0 :         encoderModeDelta = 0;
     221             : 
     222             :         // if no change in the encoder mode and buffer is low enough and level is not increasing, switch to a slower encoder mode
     223             :         // If previous ChangeCond was the same, double the threshold2
     224           0 :         if (encoderModeDelta == 0 && sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_mode_change_frame_in + bufferTrshold2 &&
     225           0 :             (context_ptr->prev_change_cond != 8 || sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_mode_change_frame_in + bufferTrshold2 * 2) &&
     226           0 :             ((sequence_control_set_ptr->encode_context_ptr->sc_buffer - context_ptr->previous_mode_change_buffer < (targetFps / 3)) || context_ptr->previous_mode_change_buffer == 0) &&
     227           0 :             sequence_control_set_ptr->encode_context_ptr->sc_buffer < bufferTrshold3) {
     228           0 :             encoderModeDelta = -1;
     229           0 :             changeCond = 8;
     230             :         }
     231             : 
     232           0 :         encoderModeDelta = CLIP3(-1, 1, encoderModeDelta);
     233           0 :         sequence_control_set_ptr->encode_context_ptr->enc_mode = (EbEncMode)CLIP3(1, MAX_ENC_PRESET, (int8_t)sequence_control_set_ptr->encode_context_ptr->enc_mode + encoderModeDelta);
     234             : 
     235             :         // Update previous stats
     236           0 :         context_ptr->previous_frame_in_check2 = sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     237             : 
     238           0 :         if (encoderModeDelta) {
     239           0 :             context_ptr->previous_mode_change_buffer = sequence_control_set_ptr->encode_context_ptr->sc_buffer;
     240           0 :             context_ptr->previous_mode_change_frame_in = sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     241           0 :             context_ptr->prev_enc_mode_delta = encoderModeDelta;
     242             :         }
     243             :     }
     244             :     // Check every SC_FRAMES_INTERVAL_SPEED frames for the speed calculation (previous_frame_in_check3 variable)
     245           0 :     if (context_ptr->start_flag || (sequence_control_set_ptr->encode_context_ptr->sc_frame_in > context_ptr->previous_frame_in_check3 + SC_FRAMES_INTERVAL_SPEED && sequence_control_set_ptr->encode_context_ptr->sc_frame_in >= SC_FRAMES_TO_IGNORE)) {
     246           0 :         if (context_ptr->start_flag)
     247           0 :             context_ptr->cur_speed = (uint64_t)(sequence_control_set_ptr->encode_context_ptr->sc_frame_out - 0) * 1000 / (uint64_t)(overallDuration);
     248             :         else {
     249           0 :             if (instDuration != 0)
     250           0 :                 context_ptr->cur_speed = (uint64_t)(sequence_control_set_ptr->encode_context_ptr->sc_frame_out - context_ptr->prev_frame_out) * 1000 / (uint64_t)(instDuration);
     251             :         }
     252           0 :         context_ptr->start_flag = EB_FALSE;
     253             : 
     254             :         // Update previous stats
     255           0 :         context_ptr->previous_frame_in_check3 = sequence_control_set_ptr->encode_context_ptr->sc_frame_in;
     256           0 :         context_ptr->prevs_time_seconds = cursTimeSeconds;
     257           0 :         context_ptr->prevs_timeu_seconds = cursTimeuSeconds;
     258           0 :         context_ptr->prev_frame_out = sequence_control_set_ptr->encode_context_ptr->sc_frame_out;
     259             :     }
     260           0 :     else if (sequence_control_set_ptr->encode_context_ptr->sc_frame_in < SC_FRAMES_TO_IGNORE && (overallDuration != 0))
     261           0 :         context_ptr->cur_speed = (uint64_t)(sequence_control_set_ptr->encode_context_ptr->sc_frame_out - 0) * 1000 / (uint64_t)(overallDuration);
     262           0 :     if (changeCond)
     263           0 :         context_ptr->prev_change_cond = changeCond;
     264           0 :     sequence_control_set_ptr->encode_context_ptr->sc_frame_in++;
     265           0 :     if (sequence_control_set_ptr->encode_context_ptr->sc_frame_in >= SC_FRAMES_TO_IGNORE)
     266           0 :         context_ptr->average_enc_mod += sequence_control_set_ptr->encode_context_ptr->enc_mode;
     267             :     else
     268           0 :         context_ptr->average_enc_mod = 0;
     269             :     // Set the encoder level
     270           0 :     picture_control_set_ptr->enc_mode = sequence_control_set_ptr->encode_context_ptr->enc_mode;
     271             : 
     272           0 :     eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->sc_buffer_mutex);
     273           0 :     context_ptr->prev_enc_mod = sequence_control_set_ptr->encode_context_ptr->enc_mode;
     274           0 : }
     275             : 
     276         122 : void ResetPcsAv1(
     277             :     PictureParentControlSet       *picture_control_set_ptr) {
     278         122 :     FrameHeader *frm_hdr = &picture_control_set_ptr->frm_hdr;
     279         122 :     Av1Common *cm = picture_control_set_ptr->av1_cm;
     280             : 
     281         122 :     picture_control_set_ptr->is_skip_mode_allowed = 0;
     282         122 :     picture_control_set_ptr->skip_mode_flag = 0;
     283         122 :     frm_hdr->frame_type                 = KEY_FRAME;
     284         122 :     frm_hdr->show_frame = 1;
     285         122 :     frm_hdr->showable_frame = 1;  // frame can be used as show existing frame in future
     286             :     // Flag for a frame used as a reference - not written to the bitstream
     287         122 :     picture_control_set_ptr->is_reference_frame = 0;
     288             :     // Flag signaling that the frame is encoded using only INTRA modes.
     289         122 :     picture_control_set_ptr->intra_only = 0;
     290             :     // uint8_t last_intra_only;
     291             : 
     292         122 :     frm_hdr->disable_cdf_update = 0;
     293         122 :     frm_hdr->allow_high_precision_mv = 0;
     294         122 :     frm_hdr->force_integer_mv = 0;  // 0 the default in AOM, 1 only integer
     295         122 :     frm_hdr->allow_warped_motion = 0;
     296             : 
     297             :     /* profile settings */
     298             : #if CONFIG_ENTROPY_STATS
     299             :     int32_t coef_cdf_category;
     300             : #endif
     301             : 
     302         122 :     frm_hdr->quantization_params.base_q_idx = 31;
     303         122 :     frm_hdr->quantization_params.delta_q_y_dc = 0;
     304         122 :     frm_hdr->quantization_params.delta_q_u_dc = 0;
     305         122 :     frm_hdr->quantization_params.delta_q_v_dc = 0;
     306         122 :     frm_hdr->quantization_params.delta_q_u_ac = 0;
     307         122 :     frm_hdr->quantization_params.delta_q_v_ac = 0;
     308             : 
     309         122 :     picture_control_set_ptr->separate_uv_delta_q = 0;
     310             :     // Encoder
     311         122 :     frm_hdr->quantization_params.using_qmatrix = 0;
     312         122 :     frm_hdr->quantization_params.qm_y = 5;
     313         122 :     frm_hdr->quantization_params.qm_u = 5;
     314         122 :     frm_hdr->quantization_params.qm_v = 5;
     315         122 :     frm_hdr->is_motion_mode_switchable = 0;
     316             :     // Flag signaling how frame contexts should be updated at the end of
     317             :     // a frame decode
     318         122 :     picture_control_set_ptr->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
     319             : 
     320         122 :     frm_hdr->loop_filter_params.filter_level[0] = 0;
     321         122 :     frm_hdr->loop_filter_params.filter_level[1] = 0;
     322         122 :     frm_hdr->loop_filter_params.filter_level_u = 0;
     323         122 :     frm_hdr->loop_filter_params.filter_level_v = 0;
     324         122 :     frm_hdr->loop_filter_params.sharpness_level = 0;
     325             : 
     326         122 :     frm_hdr->loop_filter_params.mode_ref_delta_enabled = 0;
     327         122 :     frm_hdr->loop_filter_params.mode_ref_delta_update = 0;
     328         122 :     frm_hdr->loop_filter_params.mode_deltas[0] = 0;
     329         122 :     frm_hdr->loop_filter_params.mode_deltas[1] = 0;
     330             : 
     331         122 :     frm_hdr->loop_filter_params.ref_deltas[0] = 1;
     332         122 :     frm_hdr->loop_filter_params.ref_deltas[1] = 0;
     333         122 :     frm_hdr->loop_filter_params.ref_deltas[2] = 0;
     334         122 :     frm_hdr->loop_filter_params.ref_deltas[3] = 0;
     335         122 :     frm_hdr->loop_filter_params.ref_deltas[4] = -1;
     336         122 :     frm_hdr->loop_filter_params.ref_deltas[5] = 0;
     337         122 :     frm_hdr->loop_filter_params.ref_deltas[6] = -1;
     338         122 :     frm_hdr->loop_filter_params.ref_deltas[7] = -1;
     339             : 
     340         122 :     frm_hdr->all_lossless = 0;
     341         122 :     frm_hdr->coded_lossless = 0;
     342         122 :     frm_hdr->reduced_tx_set= 0;
     343         122 :     frm_hdr->reference_mode = SINGLE_REFERENCE;
     344         122 :     picture_control_set_ptr->frame_context_idx = 0; /* Context to use/update */
     345        1098 :     for (int32_t i = 0; i < REF_FRAMES; i++)
     346         976 :         picture_control_set_ptr->fb_of_context_type[i] = 0;
     347         122 :     frm_hdr->primary_ref_frame = PRIMARY_REF_NONE;
     348         122 :     picture_control_set_ptr->frame_offset = picture_control_set_ptr->picture_number;
     349         122 :     frm_hdr->error_resilient_mode = 0;
     350         122 :     cm->tiles_info.uniform_tile_spacing_flag = 1;
     351         122 :     picture_control_set_ptr->large_scale_tile = 0;
     352         122 :     picture_control_set_ptr->film_grain_params_present = 0;
     353             : 
     354             :     //cdef_pri_damping & cdef_sec_damping are consolidated to cdef_damping
     355         122 :     frm_hdr->CDEF_params.cdef_damping = 0;
     356             :     //picture_control_set_ptr->cdef_pri_damping = 0;
     357             :     //picture_control_set_ptr->cdef_sec_damping = 0;
     358             : 
     359         122 :     picture_control_set_ptr->nb_cdef_strengths = 1;
     360        2074 :     for (int32_t i = 0; i < CDEF_MAX_STRENGTHS; i++) {
     361        1952 :         frm_hdr->CDEF_params.cdef_y_strength[i] = 0;
     362        1952 :         frm_hdr->CDEF_params.cdef_uv_strength[i] = 0;
     363             :     }
     364         122 :     frm_hdr->CDEF_params.cdef_bits = 0;
     365             : 
     366             : #if ADD_DELTA_QP_SUPPORT
     367         122 :     frm_hdr->delta_q_params.delta_q_present = 1;
     368         122 :     frm_hdr->delta_lf_params.delta_lf_present = 0;
     369         122 :     frm_hdr->delta_q_params.delta_q_res = DEFAULT_DELTA_Q_RES;
     370             : #else
     371             :     frm_hdr->delta_q_params.delta_q_present = 0;
     372             : #endif
     373             : 
     374         122 :     frm_hdr->delta_lf_params.delta_lf_present = 0;
     375         122 :     frm_hdr->delta_lf_params.delta_lf_res = 0;
     376         122 :     frm_hdr->delta_lf_params.delta_lf_multi = 0;
     377             : 
     378         122 :     frm_hdr->current_frame_id = 0;
     379         122 :     frm_hdr->frame_refs_short_signaling = 0;
     380         122 :     picture_control_set_ptr->allow_comp_inter_inter = 0;
     381             :     //  int32_t all_one_sided_refs;
     382         122 : }
     383             : /***********************************************
     384             : **** Copy the input buffer from the
     385             : **** sample application to the library buffers
     386             : ************************************************/
     387           0 : static EbErrorType copy_frame_buffer(
     388             :     SequenceControlSet            *sequence_control_set_ptr,
     389             :     uint8_t                          *dst,
     390             :     uint8_t                          *src)
     391             : {
     392           0 :     EbSvtAv1EncConfiguration        *config = &sequence_control_set_ptr->static_config;
     393           0 :     EbErrorType                      return_error = EB_ErrorNone;
     394             : 
     395           0 :     EbPictureBufferDesc             *dst_picture_ptr = (EbPictureBufferDesc*)dst;
     396           0 :     EbPictureBufferDesc             *src_picture_ptr = (EbPictureBufferDesc*)src;
     397             :     uint16_t                         inputRowIndex;
     398           0 :     EbBool                           is16BitInput = (EbBool)(config->encoder_bit_depth > EB_8BIT);
     399             : 
     400             :     // Need to include for Interlacing on the fly with pictureScanType = 1
     401             : 
     402           0 :     if (!is16BitInput) {
     403           0 :         uint32_t     lumaBufferOffset = (dst_picture_ptr->stride_y*sequence_control_set_ptr->top_padding + sequence_control_set_ptr->left_padding) << is16BitInput;
     404           0 :         uint32_t     chromaBufferOffset = (dst_picture_ptr->stride_cr*(sequence_control_set_ptr->top_padding >> 1) + (sequence_control_set_ptr->left_padding >> 1)) << is16BitInput;
     405           0 :         uint16_t     lumaStride = dst_picture_ptr->stride_y << is16BitInput;
     406           0 :         uint16_t     chromaStride = dst_picture_ptr->stride_cb << is16BitInput;
     407           0 :         uint16_t     lumaWidth = (uint16_t)(dst_picture_ptr->width - sequence_control_set_ptr->max_input_pad_right) << is16BitInput;
     408           0 :         uint16_t     chromaWidth = (lumaWidth >> 1) << is16BitInput;
     409           0 :         uint16_t     lumaHeight = (uint16_t)(dst_picture_ptr->height - sequence_control_set_ptr->max_input_pad_bottom);
     410             : 
     411             :         //uint16_t     lumaHeight  = input_picture_ptr->max_height;
     412             :         // Y
     413           0 :         for (inputRowIndex = 0; inputRowIndex < lumaHeight; inputRowIndex++) {
     414           0 :             EB_MEMCPY((dst_picture_ptr->buffer_y + lumaBufferOffset + lumaStride * inputRowIndex),
     415             :                 (src_picture_ptr->buffer_y + lumaBufferOffset + lumaStride * inputRowIndex),
     416             :                 lumaWidth);
     417             :         }
     418             : 
     419             :         // U
     420           0 :         for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     421           0 :             EB_MEMCPY((dst_picture_ptr->buffer_cb + chromaBufferOffset + chromaStride * inputRowIndex),
     422             :                 (src_picture_ptr->buffer_cb + chromaBufferOffset + chromaStride * inputRowIndex),
     423             :                 chromaWidth);
     424             :         }
     425             : 
     426             :         // V
     427           0 :         for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     428           0 :             EB_MEMCPY((dst_picture_ptr->buffer_cr + chromaBufferOffset + chromaStride * inputRowIndex),
     429             :                 (src_picture_ptr->buffer_cr + chromaBufferOffset + chromaStride * inputRowIndex),
     430             :                 chromaWidth);
     431             :         }
     432             :     }
     433           0 :     else if (is16BitInput && config->compressed_ten_bit_format == 1)
     434             :     {
     435           0 :         {
     436           0 :             uint32_t  lumaBufferOffset = (dst_picture_ptr->stride_y*sequence_control_set_ptr->top_padding + sequence_control_set_ptr->left_padding);
     437           0 :             uint32_t  chromaBufferOffset = (dst_picture_ptr->stride_cr*(sequence_control_set_ptr->top_padding >> 1) + (sequence_control_set_ptr->left_padding >> 1));
     438           0 :             uint16_t  lumaStride = dst_picture_ptr->stride_y;
     439           0 :             uint16_t  chromaStride = dst_picture_ptr->stride_cb;
     440           0 :             uint16_t  lumaWidth = (uint16_t)(dst_picture_ptr->width - sequence_control_set_ptr->max_input_pad_right);
     441           0 :             uint16_t  chromaWidth = (lumaWidth >> 1);
     442           0 :             uint16_t  lumaHeight = (uint16_t)(dst_picture_ptr->height - sequence_control_set_ptr->max_input_pad_bottom);
     443             : 
     444             :             // Y 8bit
     445           0 :             for (inputRowIndex = 0; inputRowIndex < lumaHeight; inputRowIndex++) {
     446           0 :                 EB_MEMCPY((dst_picture_ptr->buffer_y + lumaBufferOffset + lumaStride * inputRowIndex),
     447             :                     (src_picture_ptr->buffer_y + lumaBufferOffset + lumaStride * inputRowIndex),
     448             :                     lumaWidth);
     449             :             }
     450             : 
     451             :             // U 8bit
     452           0 :             for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     453           0 :                 EB_MEMCPY((dst_picture_ptr->buffer_cb + chromaBufferOffset + chromaStride * inputRowIndex),
     454             :                     (src_picture_ptr->buffer_cb + chromaBufferOffset + chromaStride * inputRowIndex),
     455             :                     chromaWidth);
     456             :             }
     457             : 
     458             :             // V 8bit
     459           0 :             for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     460           0 :                 EB_MEMCPY((dst_picture_ptr->buffer_cr + chromaBufferOffset + chromaStride * inputRowIndex),
     461             :                     (src_picture_ptr->buffer_cr + chromaBufferOffset + chromaStride * inputRowIndex),
     462             :                     chromaWidth);
     463             :             }
     464             :             // AMIR to update
     465             :             ////efficient copy - final
     466             :             ////compressed 2Bit in 1D format
     467             :             //{
     468             :             //    uint16_t luma2BitWidth = sequence_control_set_ptr->max_input_luma_width / 4;
     469             :             //    uint16_t lumaHeight = sequence_control_set_ptr->max_input_luma_height;
     470             : 
     471             :             //    uint16_t sourceLuma2BitStride = sourceLumaStride / 4;
     472             :             //    uint16_t sourceChroma2BitStride = sourceLuma2BitStride >> 1;
     473             : 
     474             :             //    for (inputRowIndex = 0; inputRowIndex < lumaHeight; inputRowIndex++) {
     475             :             //        EB_MEMCPY(input_picture_ptr->buffer_bit_inc_y + luma2BitWidth * inputRowIndex, inputPtr->luma_ext + sourceLuma2BitStride * inputRowIndex, luma2BitWidth);
     476             :             //    }
     477             :             //    for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     478             :             //        EB_MEMCPY(input_picture_ptr->buffer_bit_inc_cb + (luma2BitWidth >> 1)*inputRowIndex, inputPtr->cb_ext + sourceChroma2BitStride * inputRowIndex, luma2BitWidth >> 1);
     479             :             //    }
     480             :             //    for (inputRowIndex = 0; inputRowIndex < lumaHeight >> 1; inputRowIndex++) {
     481             :             //        EB_MEMCPY(input_picture_ptr->buffer_bit_inc_cr + (luma2BitWidth >> 1)*inputRowIndex, inputPtr->cr_ext + sourceChroma2BitStride * inputRowIndex, luma2BitWidth >> 1);
     482             :             //    }
     483             :             //}
     484             :         }
     485             :     }
     486             :     else { // 10bit packed
     487             : 
     488           0 :         EB_MEMCPY(dst_picture_ptr->buffer_y,
     489             :             src_picture_ptr->buffer_y ,
     490             :             src_picture_ptr->luma_size);
     491             : 
     492           0 :         EB_MEMCPY(dst_picture_ptr->buffer_cb,
     493             :             src_picture_ptr->buffer_cb,
     494             :             src_picture_ptr->chroma_size);
     495             : 
     496           0 :         EB_MEMCPY(dst_picture_ptr->buffer_cr,
     497             :             src_picture_ptr->buffer_cr,
     498             :             src_picture_ptr->chroma_size);
     499             : 
     500           0 :         EB_MEMCPY(dst_picture_ptr->buffer_bit_inc_y,
     501             :             src_picture_ptr->buffer_bit_inc_y,
     502             :             src_picture_ptr->luma_size);
     503             : 
     504           0 :         EB_MEMCPY(dst_picture_ptr->buffer_bit_inc_cb,
     505             :             src_picture_ptr->buffer_bit_inc_cb,
     506             :             src_picture_ptr->chroma_size);
     507             : 
     508           0 :         EB_MEMCPY(dst_picture_ptr->buffer_bit_inc_cr,
     509             :             src_picture_ptr->buffer_bit_inc_cr,
     510             :             src_picture_ptr->chroma_size);
     511             :     }
     512           0 :     return return_error;
     513             : }
     514           0 : static void CopyInputBuffer(
     515             :     SequenceControlSet*    sequenceControlSet,
     516             :     EbBufferHeaderType*     dst,
     517             :     EbBufferHeaderType*     src
     518             : )
     519             : {
     520             :     // Copy the higher level structure
     521           0 :     dst->n_alloc_len = src->n_alloc_len;
     522           0 :     dst->n_filled_len = src->n_filled_len;
     523           0 :     dst->flags = src->flags;
     524           0 :     dst->pts = src->pts;
     525           0 :     dst->n_tick_count = src->n_tick_count;
     526           0 :     dst->size = src->size;
     527           0 :     dst->qp = src->qp;
     528           0 :     dst->pic_type = src->pic_type;
     529             : 
     530             :     // Copy the picture buffer
     531           0 :     if (src->p_buffer != NULL)
     532           0 :         copy_frame_buffer(sequenceControlSet, dst->p_buffer, src->p_buffer);
     533           0 : }
     534             : 
     535             : #if TWO_PASS
     536             : /******************************************************
     537             :  * Read Stat from File
     538             :  * reads stat_struct_t per frame from the file and stores under picture_control_set_ptr
     539             :  ******************************************************/
     540           0 : static void read_stat_from_file(
     541             :     PictureParentControlSet  *picture_control_set_ptr,
     542             :     SequenceControlSet       *sequence_control_set_ptr)
     543             : {
     544           0 :     eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->stat_file_mutex);
     545             : 
     546           0 :     int32_t fseek_return_value = fseek(sequence_control_set_ptr->static_config.input_stat_file, (long)picture_control_set_ptr->picture_number * sizeof(stat_struct_t), SEEK_SET);
     547             : 
     548           0 :     if (fseek_return_value != 0) {
     549           0 :         printf("Error in fseek  returnVal %i\n", (int)fseek_return_value);
     550             :     }
     551           0 :     size_t fread_return_value = fread(&picture_control_set_ptr->stat_struct,
     552             :         (size_t)1,
     553             :         sizeof(stat_struct_t),
     554             :         sequence_control_set_ptr->static_config.input_stat_file);
     555           0 :     if (fread_return_value != sizeof(stat_struct_t)) {
     556           0 :         printf("Error in freed  returnVal %i\n", (int)fread_return_value);
     557             :     }
     558             : 
     559           0 :     uint64_t referenced_area_avg = 0;
     560           0 :     uint64_t referenced_area_has_non_zero = 0;
     561           0 :     for (int sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_total_count; ++sb_addr) {
     562           0 :         referenced_area_avg += (picture_control_set_ptr->stat_struct.referenced_area[sb_addr] / sequence_control_set_ptr->sb_params_array[sb_addr].width / sequence_control_set_ptr->sb_params_array[sb_addr].height);
     563           0 :         referenced_area_has_non_zero += picture_control_set_ptr->stat_struct.referenced_area[sb_addr];
     564             :     }
     565           0 :     referenced_area_avg /= sequence_control_set_ptr->sb_total_count;
     566             : #if TWO_PASS_IMPROVEMENT
     567             :     // adjust the reference area based on the intra refresh
     568           0 :     if (sequence_control_set_ptr->intra_period_length && sequence_control_set_ptr->intra_period_length < TWO_PASS_IR_THRSHLD)
     569           0 :         referenced_area_avg = referenced_area_avg * (sequence_control_set_ptr->intra_period_length + 1) / TWO_PASS_IR_THRSHLD;
     570             : #endif
     571           0 :     picture_control_set_ptr->referenced_area_avg = referenced_area_avg;
     572           0 :     picture_control_set_ptr->referenced_area_has_non_zero = referenced_area_has_non_zero ? 1 : 0;
     573             : 
     574           0 :     eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->stat_file_mutex);
     575           0 : }
     576             : #endif
     577             : /***************************************
     578             :  * ResourceCoordination Kernel
     579             :  ***************************************/
     580           2 : void* resource_coordination_kernel(void *input_ptr)
     581             : {
     582           2 :     ResourceCoordinationContext   *context_ptr = (ResourceCoordinationContext*)input_ptr;
     583             : 
     584             :     EbObjectWrapper               *picture_control_set_wrapper_ptr;
     585             : 
     586             :     PictureParentControlSet       *picture_control_set_ptr;
     587             : 
     588             :     EbObjectWrapper               *previousSequenceControlSetWrapperPtr;
     589             :     SequenceControlSet            *sequence_control_set_ptr;
     590             : 
     591             :     EbObjectWrapper               *ebInputWrapperPtr;
     592             :     EbBufferHeaderType              *ebInputPtr;
     593             :     EbObjectWrapper               *outputWrapperPtr;
     594             :     ResourceCoordinationResults   *outputResultsPtr;
     595             : 
     596             :     EbObjectWrapper               *input_picture_wrapper_ptr;
     597             :     EbObjectWrapper               *reference_picture_wrapper_ptr;
     598             : 
     599             :     uint32_t                         instance_index;
     600           2 :     EbBool                           end_of_sequence_flag = EB_FALSE;
     601             :     uint32_t                         aspectRatio;
     602             : 
     603           2 :     uint32_t                         input_size = 0;
     604           2 :     EbObjectWrapper               *prevPictureControlSetWrapperPtr = 0;
     605             : 
     606         122 :     for (;;) {
     607             :         // Tie instance_index to zero for now...
     608         124 :         instance_index = 0;
     609             : 
     610             :         // Get the Next svt Input Buffer [BLOCKING]
     611         124 :         eb_get_full_object(
     612             :             context_ptr->input_buffer_fifo_ptr,
     613             :             &ebInputWrapperPtr);
     614         122 :         ebInputPtr = (EbBufferHeaderType*)ebInputWrapperPtr->object_ptr;
     615         122 :         sequence_control_set_ptr = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr;
     616             : 
     617             :         // If config changes occured since the last picture began encoding, then
     618             :         //   prepare a new sequence_control_set_ptr containing the new changes and update the state
     619             :         //   of the previous Active SequenceControlSet
     620         122 :         eb_block_on_mutex(context_ptr->sequence_control_set_instance_array[instance_index]->config_mutex);
     621         122 :         if (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) {
     622             :             // Update picture width, picture height, cropping right offset, cropping bottom offset, and conformance windows
     623           2 :             if (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture)
     624             : 
     625             :             {
     626           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->seq_header.max_frame_width = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_luma_width;
     627           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->seq_header.max_frame_height = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_luma_height;
     628           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->chroma_width = (context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_luma_width >> 1);
     629           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->chroma_height = (context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_luma_height >> 1);
     630             : 
     631           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_right = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_pad_right;
     632           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->cropping_right_offset = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_right;
     633           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_bottom = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->max_input_pad_bottom;
     634           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->cropping_bottom_offset = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_bottom;
     635             : 
     636           2 :                 if (context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_right != 0 || context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->pad_bottom != 0)
     637           0 :                     context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->conformance_window_flag = 1;
     638             :                 else
     639           2 :                     context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->conformance_window_flag = 0;
     640           2 :                 input_size = context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->seq_header.max_frame_width * context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr->seq_header.max_frame_height;
     641             :             }
     642             : 
     643             :             // Copy previous Active SequenceControlSetPtr to a place holder
     644           2 :             previousSequenceControlSetWrapperPtr = context_ptr->sequenceControlSetActiveArray[instance_index];
     645             : 
     646             :             // Get empty SequenceControlSet [BLOCKING]
     647           2 :             eb_get_empty_object(
     648             :                 context_ptr->sequence_control_set_empty_fifo_ptr,
     649           2 :                 &context_ptr->sequenceControlSetActiveArray[instance_index]);
     650             : 
     651             :             // Copy the contents of the active SequenceControlSet into the new empty SequenceControlSet
     652           2 :             copy_sequence_control_set(
     653           2 :                 (SequenceControlSet*)context_ptr->sequenceControlSetActiveArray[instance_index]->object_ptr,
     654           2 :                 context_ptr->sequence_control_set_instance_array[instance_index]->sequence_control_set_ptr);
     655             : 
     656             :             // Disable releaseFlag of new SequenceControlSet
     657           2 :             eb_object_release_disable(
     658           2 :                 context_ptr->sequenceControlSetActiveArray[instance_index]);
     659             : 
     660           2 :             if (previousSequenceControlSetWrapperPtr != EB_NULL) {
     661             :                 // Enable releaseFlag of old SequenceControlSet
     662           0 :                 eb_object_release_enable(
     663             :                     previousSequenceControlSetWrapperPtr);
     664             : 
     665             :                 // Check to see if previous SequenceControlSet is already inactive, if TRUE then release the SequenceControlSet
     666           0 :                 if (previousSequenceControlSetWrapperPtr->live_count == 0) {
     667           0 :                     eb_release_object(
     668             :                         previousSequenceControlSetWrapperPtr);
     669             :                 }
     670             :             }
     671             :         }
     672         122 :         eb_release_mutex(context_ptr->sequence_control_set_instance_array[instance_index]->config_mutex);
     673             :         // Seque Control Set is released by Rate Control after passing through MDC->MD->ENCDEC->Packetization->RateControl,
     674             :         // in the PictureManager after receiving the reference and in PictureManager after receiving the feedback
     675         122 :         eb_object_inc_live_count(
     676         122 :             context_ptr->sequenceControlSetActiveArray[instance_index],
     677             :             3);
     678             : 
     679             :         // Set the current SequenceControlSet
     680         122 :         sequence_control_set_ptr = (SequenceControlSet*)context_ptr->sequenceControlSetActiveArray[instance_index]->object_ptr;
     681             : 
     682             :         // Init SB Params
     683         122 :         if (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) {
     684           2 :             derive_input_resolution(
     685             :                 sequence_control_set_ptr,
     686             :                 input_size);
     687             : 
     688           2 :             sb_params_init(sequence_control_set_ptr);
     689           2 :             sb_geom_init(sequence_control_set_ptr);
     690             : 
     691           6 :             sequence_control_set_ptr->enable_altrefs = sequence_control_set_ptr->static_config.enable_altrefs &&
     692           2 :                     sequence_control_set_ptr->static_config.altref_nframes > 1 &&
     693           2 :                     ((sequence_control_set_ptr->static_config.encoder_bit_depth >= 8 && sequence_control_set_ptr->static_config.enc_mode == ENC_M0) ||
     694           6 :                     sequence_control_set_ptr->static_config.encoder_bit_depth == 8) ? EB_TRUE : EB_FALSE;
     695             : 
     696             : #if II_COMP_FLAG
     697             : #if INTER_INTRA_HBD
     698             :             // Set inter-intra mode      Settings
     699             :             // 0                 OFF
     700             :             // 1                 ON
     701           4 :             sequence_control_set_ptr->seq_header.enable_interintra_compound =  (sequence_control_set_ptr->static_config.encoder_bit_depth == EB_10BIT &&
     702           4 :                                                                                 sequence_control_set_ptr->static_config.enable_hbd_mode_decision ) ? 0:
     703           2 :                                                                                 (sequence_control_set_ptr->static_config.enc_mode == ENC_M0) ? 1 : 0;
     704             : #else
     705             :             sequence_control_set_ptr->seq_header.enable_interintra_compound = (sequence_control_set_ptr->static_config.encoder_bit_depth == EB_10BIT ) ? 0 :
     706             :                                                                               (sequence_control_set_ptr->static_config.enc_mode == ENC_M0) ? 1 : 0;
     707             : #endif
     708             : #endif
     709             : #if FILTER_INTRA_FLAG
     710             :             // Set filter intra mode      Settings
     711             :             // 0                 OFF
     712             :             // 1                 ON
     713           2 :             if (sequence_control_set_ptr->static_config.enable_filter_intra)
     714           2 :                 sequence_control_set_ptr->seq_header.enable_filter_intra        = (sequence_control_set_ptr->static_config.enc_mode <= ENC_M2) ? 1 : 0;
     715             :             else
     716           0 :                 sequence_control_set_ptr->seq_header.enable_filter_intra        =  0;
     717             : #endif
     718             :             // Set compound mode      Settings
     719             :             // 0                 OFF: No compond mode search : AVG only
     720             :             // 1                 ON: full
     721             : #if INTER_INTER_HBD
     722           4 :             sequence_control_set_ptr->compound_mode = (sequence_control_set_ptr->static_config.encoder_bit_depth == EB_10BIT &&
     723           4 :                                                        sequence_control_set_ptr->static_config.enable_hbd_mode_decision ) ? 0:
     724           2 :                                                       (sequence_control_set_ptr->static_config.enc_mode <= ENC_M4) ? 1 : 0;
     725             : #else
     726             :             sequence_control_set_ptr->compound_mode = sequence_control_set_ptr->static_config.encoder_bit_depth == EB_10BIT ? 0 :
     727             :                 (sequence_control_set_ptr->static_config.enc_mode <= ENC_M4) ? 1 : 0;
     728             : #endif
     729           2 :             if (sequence_control_set_ptr->compound_mode)
     730             :             {
     731           1 :                 sequence_control_set_ptr->seq_header.order_hint_info.enable_jnt_comp = 1; //DISTANCE
     732           1 :                 sequence_control_set_ptr->seq_header.enable_masked_compound = 1; //DIFF+WEDGE
     733             :             }
     734             :             else {
     735           1 :                 sequence_control_set_ptr->seq_header.order_hint_info.enable_jnt_comp = 0;
     736           1 :                 sequence_control_set_ptr->seq_header.enable_masked_compound = 0;
     737             :             }
     738             :             // Sep PM mode
     739           2 :             sequence_control_set_ptr->pm_mode = sequence_control_set_ptr->input_resolution < INPUT_SIZE_4K_RANGE ?
     740             :                 PM_MODE_2 :
     741             :                 PM_MODE_1;
     742             : 
     743             :             // Construct PM Trans Coeff Shaping
     744           2 :             if (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) {
     745           2 :                 if (sequence_control_set_ptr->pm_mode == PM_MODE_0)
     746           0 :                     construct_pm_trans_coeff_shaping(sequence_control_set_ptr);
     747             :             }
     748             :         }
     749             :         // Since at this stage we do not know the prediction structure and the location of ALT_REF pictures,
     750             :         // for every picture (except first picture), we allocate two: 1. original picture, 2. potential Overlay picture.
     751             :         // In Picture Decision Process, where the overlay frames are known, they extra pictures are released
     752         244 :         uint8_t has_overlay = (sequence_control_set_ptr->static_config.enable_overlays == EB_FALSE ||
     753         122 :             context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) ? 0 : 1;
     754         244 :         for (uint8_t loop_index = 0; loop_index <= has_overlay && !end_of_sequence_flag; loop_index++) {
     755             :             //Get a New ParentPCS where we will hold the new inputPicture
     756         122 :             eb_get_empty_object(
     757         122 :                 context_ptr->picture_control_set_fifo_ptr_array[instance_index],
     758             :                 &picture_control_set_wrapper_ptr);
     759             : 
     760             :             // Parent PCS is released by the Rate Control after passing through MDC->MD->ENCDEC->Packetization
     761         122 :             eb_object_inc_live_count(
     762             :                 picture_control_set_wrapper_ptr,
     763             :                 1);
     764             : 
     765         122 :             picture_control_set_ptr = (PictureParentControlSet*)picture_control_set_wrapper_ptr->object_ptr;
     766             : 
     767         122 :             picture_control_set_ptr->p_pcs_wrapper_ptr = picture_control_set_wrapper_ptr;
     768             : 
     769         122 :             picture_control_set_ptr->overlay_ppcs_ptr = NULL;
     770         122 :             picture_control_set_ptr->is_alt_ref       = 0;
     771         122 :             if (loop_index) {
     772           0 :                 picture_control_set_ptr->is_overlay = 1;
     773             :                 // set the overlay_ppcs_ptr in the original (ALT_REF) ppcs to the current ppcs
     774           0 :                 EbObjectWrapper               *alt_ref_picture_control_set_wrapper_ptr = (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) ?
     775           0 :                     picture_control_set_wrapper_ptr :
     776           0 :                     sequence_control_set_ptr->encode_context_ptr->previous_picture_control_set_wrapper_ptr;
     777             : 
     778           0 :                 picture_control_set_ptr->alt_ref_ppcs_ptr = ((PictureParentControlSet*)alt_ref_picture_control_set_wrapper_ptr->object_ptr);
     779           0 :                 picture_control_set_ptr->alt_ref_ppcs_ptr->overlay_ppcs_ptr = picture_control_set_ptr;
     780             :             }
     781             :             else {
     782         122 :                 picture_control_set_ptr->is_overlay = 0;
     783         122 :                 picture_control_set_ptr->alt_ref_ppcs_ptr = NULL;
     784             :             }
     785             :             // Set the Encoder mode
     786         122 :             picture_control_set_ptr->enc_mode = sequence_control_set_ptr->static_config.enc_mode;
     787             : 
     788             :             // Keep track of the previous input for the ZZ SADs computation
     789         244 :             picture_control_set_ptr->previous_picture_control_set_wrapper_ptr = (context_ptr->sequence_control_set_instance_array[instance_index]->encode_context_ptr->initial_picture) ?
     790         122 :                 picture_control_set_wrapper_ptr :
     791         120 :                 sequence_control_set_ptr->encode_context_ptr->previous_picture_control_set_wrapper_ptr;
     792         122 :             if (loop_index == 0)
     793         122 :                 sequence_control_set_ptr->encode_context_ptr->previous_picture_control_set_wrapper_ptr = picture_control_set_wrapper_ptr;
     794             :             // Copy data from the svt buffer to the input frame
     795             :             // *Note - Assumes 4:2:0 planar
     796         122 :             input_picture_wrapper_ptr = ebInputWrapperPtr;
     797         122 :             picture_control_set_ptr->enhanced_picture_ptr = (EbPictureBufferDesc*)ebInputPtr->p_buffer;
     798         122 :             picture_control_set_ptr->input_ptr = ebInputPtr;
     799         122 :             end_of_sequence_flag = (picture_control_set_ptr->input_ptr->flags & EB_BUFFERFLAG_EOS) ? EB_TRUE : EB_FALSE;
     800         122 :             EbStartTime(&picture_control_set_ptr->start_time_seconds, &picture_control_set_ptr->start_time_u_seconds);
     801             : 
     802         122 :             picture_control_set_ptr->sequence_control_set_wrapper_ptr = context_ptr->sequenceControlSetActiveArray[instance_index];
     803         122 :             picture_control_set_ptr->sequence_control_set_ptr = sequence_control_set_ptr;
     804         122 :             picture_control_set_ptr->input_picture_wrapper_ptr = input_picture_wrapper_ptr;
     805         122 :             picture_control_set_ptr->end_of_sequence_flag = end_of_sequence_flag;
     806             : 
     807         122 :             if (loop_index == 1) {
     808             :                 // Get a new input picture for overlay.
     809             :                 EbObjectWrapper     *input_pic_wrapper_ptr;
     810             : 
     811             :                 // Get a new input picture for overlay.
     812           0 :                 eb_get_empty_object(
     813           0 :                     sequence_control_set_ptr->encode_context_ptr->overlay_input_picture_pool_fifo_ptr,
     814             :                     &input_pic_wrapper_ptr);
     815             : 
     816             :                 // Copy from original picture (picture_control_set_ptr->input_picture_wrapper_ptr), which is shared between overlay and alt_ref up to this point, to the new input picture.
     817           0 :                 if (picture_control_set_ptr->alt_ref_ppcs_ptr->input_picture_wrapper_ptr->object_ptr != NULL) {
     818           0 :                     CopyInputBuffer(
     819             :                         sequence_control_set_ptr,
     820           0 :                         (EbBufferHeaderType*)input_pic_wrapper_ptr->object_ptr,
     821           0 :                         (EbBufferHeaderType*)picture_control_set_ptr->alt_ref_ppcs_ptr->input_picture_wrapper_ptr->object_ptr);
     822             :                 }
     823             :                 // Assign the new picture to the new pointers
     824           0 :                 picture_control_set_ptr->input_ptr = (EbBufferHeaderType*)input_pic_wrapper_ptr->object_ptr;
     825           0 :                 picture_control_set_ptr->enhanced_picture_ptr = (EbPictureBufferDesc*)picture_control_set_ptr->input_ptr->p_buffer;
     826           0 :                 picture_control_set_ptr->input_picture_wrapper_ptr = input_pic_wrapper_ptr;
     827             :             }
     828             :             // Set Picture Control Flags
     829         122 :             picture_control_set_ptr->idr_flag = sequence_control_set_ptr->encode_context_ptr->initial_picture || (picture_control_set_ptr->input_ptr->pic_type == EB_AV1_KEY_PICTURE);
     830         122 :             picture_control_set_ptr->cra_flag = (picture_control_set_ptr->input_ptr->pic_type == EB_AV1_INTRA_ONLY_PICTURE) ? EB_TRUE : EB_FALSE;
     831         122 :             picture_control_set_ptr->scene_change_flag = EB_FALSE;
     832         122 :             picture_control_set_ptr->qp_on_the_fly = EB_FALSE;
     833         122 :             picture_control_set_ptr->sb_total_count = sequence_control_set_ptr->sb_total_count;
     834         122 :             picture_control_set_ptr->eos_coming = (ebInputPtr->flags & (EB_BUFFERFLAG_EOS << 1)) ? EB_TRUE : EB_FALSE;
     835             : 
     836         122 :             if (sequence_control_set_ptr->static_config.speed_control_flag) {
     837           0 :                 SpeedBufferControl(
     838             :                     context_ptr,
     839             :                     picture_control_set_ptr,
     840             :                     sequence_control_set_ptr);
     841             :             }
     842             :             else
     843         122 :                 picture_control_set_ptr->enc_mode = (EbEncMode)sequence_control_set_ptr->static_config.enc_mode;
     844             : #if TWO_PASS_USE_2NDP_ME_IN_1STP
     845             :             //  If the mode of the second pass is not set from CLI, it is set to enc_mode
     846         122 :             picture_control_set_ptr->snd_pass_enc_mode =
     847         122 :                 ( sequence_control_set_ptr->use_output_stat_file && sequence_control_set_ptr->static_config.snd_pass_enc_mode != MAX_ENC_PRESET + 1)?
     848             :                 (EbEncMode)sequence_control_set_ptr->static_config.snd_pass_enc_mode : picture_control_set_ptr->enc_mode;
     849             : #endif
     850         122 :             aspectRatio = (sequence_control_set_ptr->seq_header.max_frame_width * 10) / sequence_control_set_ptr->seq_header.max_frame_height;
     851         122 :             aspectRatio = (aspectRatio <= ASPECT_RATIO_4_3) ? ASPECT_RATIO_CLASS_0 : (aspectRatio <= ASPECT_RATIO_16_9) ? ASPECT_RATIO_CLASS_1 : ASPECT_RATIO_CLASS_2;
     852             : 
     853             :             // Set the SCD Mode
     854         122 :             sequence_control_set_ptr->scd_mode = sequence_control_set_ptr->static_config.scene_change_detection == 0 ?
     855         122 :                 SCD_MODE_0 :
     856             :                 SCD_MODE_1;
     857             : 
     858             :             // Set the block mean calculation prec
     859         122 :             sequence_control_set_ptr->block_mean_calc_prec = BLOCK_MEAN_PREC_SUB;
     860             : 
     861             :             // Pre-Analysis Signal(s) derivation
     862         122 :             signal_derivation_pre_analysis_oq(
     863             :                 sequence_control_set_ptr,
     864             :                 picture_control_set_ptr);
     865         122 :             picture_control_set_ptr->filtered_sse = 0;
     866         122 :             picture_control_set_ptr->filtered_sse_uv = 0;
     867             :             // Rate Control
     868             :             // Set the ME Distortion and OIS Historgrams to zero
     869         122 :             if (sequence_control_set_ptr->static_config.rate_control_mode) {
     870           0 :                 EB_MEMSET(picture_control_set_ptr->me_distortion_histogram, 0, NUMBER_OF_SAD_INTERVALS * sizeof(uint16_t));
     871           0 :                 EB_MEMSET(picture_control_set_ptr->ois_distortion_histogram, 0, NUMBER_OF_INTRA_SAD_INTERVALS * sizeof(uint16_t));
     872             :             }
     873         122 :             picture_control_set_ptr->full_sb_count = 0;
     874             : 
     875         122 :             if (sequence_control_set_ptr->static_config.use_qp_file == 1) {
     876           0 :                 picture_control_set_ptr->qp_on_the_fly = EB_TRUE;
     877           0 :                 if (picture_control_set_ptr->input_ptr->qp > MAX_QP_VALUE) {
     878           0 :                     SVT_LOG("SVT [WARNING]: INPUT QP OUTSIDE OF RANGE\n");
     879           0 :                     picture_control_set_ptr->qp_on_the_fly = EB_FALSE;
     880           0 :                     picture_control_set_ptr->picture_qp = (uint8_t)sequence_control_set_ptr->qp;
     881             :                 }
     882           0 :                 picture_control_set_ptr->picture_qp = (uint8_t)picture_control_set_ptr->input_ptr->qp;
     883             :             }
     884             :             else {
     885         122 :                 picture_control_set_ptr->qp_on_the_fly = EB_FALSE;
     886         122 :                 picture_control_set_ptr->picture_qp = (uint8_t)sequence_control_set_ptr->qp;
     887             :             }
     888             : 
     889             :             // Picture Stats
     890         122 :             if (loop_index == has_overlay || end_of_sequence_flag)
     891         122 :                 picture_control_set_ptr->picture_number = context_ptr->picture_number_array[instance_index]++;
     892             :             else
     893           0 :                 picture_control_set_ptr->picture_number = context_ptr->picture_number_array[instance_index];
     894         122 :             ResetPcsAv1(picture_control_set_ptr);
     895             : #if TWO_PASS
     896         122 :             if (sequence_control_set_ptr->use_input_stat_file && !end_of_sequence_flag)
     897           0 :                 read_stat_from_file(
     898             :                     picture_control_set_ptr,
     899             :                     sequence_control_set_ptr);
     900             :             else {
     901         122 :                 memset(&picture_control_set_ptr->stat_struct, 0, sizeof(stat_struct_t));
     902             :             }
     903             : #endif
     904         122 :             sequence_control_set_ptr->encode_context_ptr->initial_picture = EB_FALSE;
     905             : 
     906             :             // Get Empty Reference Picture Object
     907         122 :             eb_get_empty_object(
     908         122 :                 sequence_control_set_ptr->encode_context_ptr->pa_reference_picture_pool_fifo_ptr,
     909             :                 &reference_picture_wrapper_ptr);
     910             : 
     911         122 :             picture_control_set_ptr->pa_reference_picture_wrapper_ptr = reference_picture_wrapper_ptr;
     912             :             // Since overlay pictures are not added to PA_Reference queue in PD and not released there, the life count is only set to 1
     913         122 :             if (picture_control_set_ptr->is_overlay)
     914             :                 // Give the new Reference a nominal live_count of 1
     915           0 :                 eb_object_inc_live_count(
     916             :                     picture_control_set_ptr->pa_reference_picture_wrapper_ptr,
     917             :                     1);
     918             :             else
     919         122 :                 eb_object_inc_live_count(
     920             :                     picture_control_set_ptr->pa_reference_picture_wrapper_ptr,
     921             :                     2);
     922         122 :             ((EbPaReferenceObject*)picture_control_set_ptr->pa_reference_picture_wrapper_ptr->object_ptr)->input_padded_picture_ptr->buffer_y = picture_control_set_ptr->enhanced_picture_ptr->buffer_y;
     923             : 
     924             :             // Get Empty Output Results Object
     925         122 :             if (picture_control_set_ptr->picture_number > 0 && (prevPictureControlSetWrapperPtr != NULL))
     926             :             {
     927         120 :                 ((PictureParentControlSet       *)prevPictureControlSetWrapperPtr->object_ptr)->end_of_sequence_flag = end_of_sequence_flag;
     928         120 :                 eb_get_empty_object(
     929             :                     context_ptr->resource_coordination_results_output_fifo_ptr,
     930             :                     &outputWrapperPtr);
     931         120 :                 outputResultsPtr = (ResourceCoordinationResults*)outputWrapperPtr->object_ptr;
     932         120 :                 outputResultsPtr->picture_control_set_wrapper_ptr = prevPictureControlSetWrapperPtr;
     933             :                 // since overlay frame has the end of sequence set properly, set the end of sequence to true in the alt ref picture
     934         120 :                 if (((PictureParentControlSet       *)prevPictureControlSetWrapperPtr->object_ptr)->is_overlay && end_of_sequence_flag)
     935           0 :                     ((PictureParentControlSet       *)prevPictureControlSetWrapperPtr->object_ptr)->alt_ref_ppcs_ptr->end_of_sequence_flag = EB_TRUE;
     936             :                 // Post the finished Results Object
     937         120 :                 eb_post_full_object(outputWrapperPtr);
     938             :             }
     939         122 :             prevPictureControlSetWrapperPtr = picture_control_set_wrapper_ptr;
     940             : 
     941         122 :             set_tile_info(picture_control_set_ptr);
     942         122 :             if(sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0)
     943             :             {
     944           0 :                 struct PictureParentControlSet     *ppcs_ptr = picture_control_set_ptr;
     945           0 :                 Av1Common *const cm = ppcs_ptr->av1_cm;
     946           0 :                 uint8_t picture_width_in_sb = (uint8_t)((sequence_control_set_ptr->seq_header.max_frame_width + sequence_control_set_ptr->sb_size_pix - 1) / sequence_control_set_ptr->sb_size_pix);
     947             :                 int tile_row, tile_col;
     948             :                 uint32_t  x_lcu_index,  y_lcu_index;
     949           0 :                 const int tile_cols = cm->tiles_info.tile_cols;
     950           0 :                 const int tile_rows = cm->tiles_info.tile_rows;
     951             :                 TileInfo tile_info;
     952           0 :                 int sb_size_log2 = sequence_control_set_ptr->seq_header.sb_size_log2;
     953             :                 //Tile Loop
     954           0 :                 for (tile_row = 0; tile_row < tile_rows; tile_row++)
     955             :                 {
     956           0 :                     eb_av1_tile_set_row(&tile_info, &cm->tiles_info, cm->mi_rows, tile_row);
     957             : 
     958           0 :                     for (tile_col = 0; tile_col < tile_cols; tile_col++)
     959             :                     {
     960           0 :                         eb_av1_tile_set_col(&tile_info, &cm->tiles_info, cm->mi_cols, tile_col);
     961             : 
     962           0 :                         for ((y_lcu_index = cm->tiles_info.tile_row_start_mi[tile_row] >> sb_size_log2);
     963           0 :                              (y_lcu_index < (uint32_t)cm->tiles_info.tile_row_start_mi[tile_row + 1] >> sb_size_log2);
     964           0 :                              y_lcu_index++)
     965             :                         {
     966           0 :                             for ((x_lcu_index = cm->tiles_info.tile_col_start_mi[tile_col] >> sb_size_log2);
     967           0 :                                  (x_lcu_index < (uint32_t)cm->tiles_info.tile_col_start_mi[tile_col + 1] >> sb_size_log2);
     968           0 :                                  x_lcu_index++)
     969             :                             {
     970           0 :                                 int sb_index = (uint16_t)(x_lcu_index + y_lcu_index * picture_width_in_sb);
     971           0 :                                 sequence_control_set_ptr->sb_params_array[sb_index].tile_start_x = 4 * tile_info.mi_col_start;
     972           0 :                                 sequence_control_set_ptr->sb_params_array[sb_index].tile_end_x   = 4 * tile_info.mi_col_end;
     973           0 :                                 sequence_control_set_ptr->sb_params_array[sb_index].tile_start_y = 4 * tile_info.mi_row_start;
     974           0 :                                 sequence_control_set_ptr->sb_params_array[sb_index].tile_end_y   = 4 * tile_info.mi_row_end;
     975             :                             }
     976             :                         }
     977             :                     }
     978             :                 }
     979             :             }
     980             :         }
     981             :     }
     982             : 
     983             :     return EB_NULL;
     984             : }

Generated by: LCOV version 1.14