LCOV - code coverage report
Current view: top level - Codec - EbPacketizationProcess.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 148 248 59.7 %
Date: 2019-11-25 17:38:06 Functions: 6 7 85.7 %

          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             : 
       8             : #include "EbDefinitions.h"
       9             : #include "EbPacketizationProcess.h"
      10             : #include "EbEntropyCodingResults.h"
      11             : #include "EbSystemResourceManager.h"
      12             : #include "EbSequenceControlSet.h"
      13             : #include "EbPictureControlSet.h"
      14             : #include "EbEntropyCoding.h"
      15             : #include "EbRateControlTasks.h"
      16             : #include "EbTime.h"
      17             : #include "EbModeDecisionProcess.h"
      18             : #include "EbPictureDemuxResults.h"
      19             : #define DETAILED_FRAME_OUTPUT 0
      20             : 
      21           0 : static EbBool IsPassthroughData(EbLinkedListNode* dataNode)
      22             : {
      23           0 :     return dataNode->passthrough;
      24             : }
      25             : 
      26             : // Extracts passthrough data from a linked list. The extracted data nodes are removed from the original linked list and
      27             : // returned as a linked list. Does not gaurantee the original order of the nodes.
      28         120 : static EbLinkedListNode* ExtractPassthroughData(EbLinkedListNode** llPtrPtr)
      29             : {
      30         120 :     EbLinkedListNode* llRestPtr = NULL;
      31         120 :     EbLinkedListNode* llPassPtr = split_eb_linked_list(*llPtrPtr, &llRestPtr, IsPassthroughData);
      32         120 :     *llPtrPtr = llRestPtr;
      33         120 :     return llPassPtr;
      34             : }
      35             : 
      36           2 : static void packetization_context_dctor(EbPtr p)
      37             : {
      38           2 :     PacketizationContext *obj = (PacketizationContext*)p;
      39           2 :     EB_FREE_ARRAY(obj->pps_config);
      40           2 : }
      41             : 
      42           2 : EbErrorType packetization_context_ctor(
      43             :     PacketizationContext  *context_ptr,
      44             :     EbFifo                *entropy_coding_input_fifo_ptr,
      45             :     EbFifo                *rate_control_tasks_output_fifo_ptr,
      46             :     EbFifo              *picture_manager_input_fifo_ptr
      47             : )
      48             : {
      49           2 :     context_ptr->dctor = packetization_context_dctor;
      50           2 :     context_ptr->entropy_coding_input_fifo_ptr = entropy_coding_input_fifo_ptr;
      51           2 :     context_ptr->rate_control_tasks_output_fifo_ptr = rate_control_tasks_output_fifo_ptr;
      52           2 :     context_ptr->picture_manager_input_fifo_ptr = picture_manager_input_fifo_ptr;
      53           2 :     EB_MALLOC_ARRAY(context_ptr->pps_config, 1);
      54             : 
      55           2 :     return EB_ErrorNone;
      56             : }
      57             : #define TD_SIZE                     2
      58             : #define OBU_FRAME_HEADER_SIZE       3
      59             : #define TILES_GROUP_SIZE            1
      60             : 
      61             : // Write TD after offsetting the stream buffer
      62         120 : static void write_td (
      63             :     EbBufferHeaderType  *out_str_ptr,
      64             :     EbBool               show_ex,
      65             :     EbBool               has_tiles){
      66         120 :     uint8_t  td_buff[TD_SIZE] = { 0,0 };
      67         120 :     uint8_t  obu_frame_header_size = has_tiles ? OBU_FRAME_HEADER_SIZE + TILES_GROUP_SIZE : OBU_FRAME_HEADER_SIZE;
      68         120 :     if (out_str_ptr &&
      69         120 :         (out_str_ptr->n_alloc_len > (out_str_ptr->n_filled_len + 2))) {
      70         120 :         uint8_t *src_address = (show_ex == EB_FALSE) ?  out_str_ptr->p_buffer :
      71          56 :                 out_str_ptr->p_buffer + out_str_ptr->n_filled_len - (obu_frame_header_size);
      72             : 
      73         120 :         uint8_t *dst_address = src_address + TD_SIZE;
      74             : 
      75         120 :         uint32_t move_size   = (show_ex == EB_FALSE) ? out_str_ptr->n_filled_len :
      76             :                                (obu_frame_header_size);
      77             : 
      78         120 :         memmove(dst_address,
      79             :                 src_address,
      80             :                 move_size);
      81             : 
      82         120 :         encode_td_av1((uint8_t*)(&td_buff));
      83             : 
      84         120 :         EB_MEMCPY(src_address,
      85             :                   &td_buff,
      86             :                   TD_SIZE);
      87             :     }
      88         120 : }
      89             : 
      90         120 : void update_rc_rate_tables(
      91             :     PictureControlSet            *picture_control_set_ptr,
      92             :     SequenceControlSet           *sequence_control_set_ptr) {
      93         120 :     EncodeContext* encode_context_ptr = (EncodeContext*)sequence_control_set_ptr->encode_context_ptr;
      94             : 
      95             :     uint32_t  intra_sad_interval_index;
      96             :     uint32_t  sad_interval_index;
      97             : 
      98             :     LargestCodingUnit   *sb_ptr;
      99             :     SbParams *sb_params_ptr;
     100             : 
     101             :     uint32_t  sb_index;
     102             :     int32_t   qp_index;
     103         120 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
     104             : 
     105             :     // LCU Loop
     106         120 :     if (sequence_control_set_ptr->static_config.rate_control_mode > 0) {
     107           0 :         uint64_t  sadBits[NUMBER_OF_SAD_INTERVALS] = { 0 };
     108           0 :         uint32_t  count[NUMBER_OF_SAD_INTERVALS] = { 0 };
     109             : 
     110           0 :         encode_context_ptr->rate_control_tables_array_updated = EB_TRUE;
     111             : 
     112           0 :         for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
     113           0 :             sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
     114           0 :             sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
     115             : 
     116           0 :             if (sb_params_ptr->is_complete_sb) {
     117           0 :                 if (picture_control_set_ptr->slice_type == I_SLICE) {
     118           0 :                     intra_sad_interval_index = picture_control_set_ptr->parent_pcs_ptr->intra_sad_interval_index[sb_index];
     119             : 
     120           0 :                     sadBits[intra_sad_interval_index] += sb_ptr->total_bits;
     121           0 :                     count[intra_sad_interval_index] ++;
     122             :                 }
     123             :                 else {
     124           0 :                     sad_interval_index = picture_control_set_ptr->parent_pcs_ptr->inter_sad_interval_index[sb_index];
     125             : 
     126           0 :                     sadBits[sad_interval_index] += sb_ptr->total_bits;
     127           0 :                     count[sad_interval_index] ++;
     128             :                 }
     129             :             }
     130             :         }
     131             :         {
     132           0 :             eb_block_on_mutex(encode_context_ptr->rate_table_update_mutex);
     133             : 
     134           0 :             uint64_t ref_qindex_dequant = (uint64_t)picture_control_set_ptr->parent_pcs_ptr->deq.y_dequant_QTX[frm_hdr->quantization_params.base_q_idx][1];
     135           0 :             uint64_t sad_bits_ref_dequant = 0;
     136           0 :             uint64_t weight = 0;
     137             :             {
     138           0 :                 if (picture_control_set_ptr->slice_type == I_SLICE) {
     139           0 :                     if (sequence_control_set_ptr->input_resolution < INPUT_SIZE_4K_RANGE) {
     140           0 :                         for (sad_interval_index = 0; sad_interval_index < NUMBER_OF_INTRA_SAD_INTERVALS; sad_interval_index++) {
     141           0 :                             if (count[sad_interval_index] > 5)
     142           0 :                                 weight = 8;
     143           0 :                             else if (count[sad_interval_index] > 1)
     144           0 :                                 weight = 5;
     145           0 :                             else if (count[sad_interval_index] == 1)
     146           0 :                                 weight = 2;
     147           0 :                             if (count[sad_interval_index] > 0) {
     148           0 :                                 sadBits[sad_interval_index] /= count[sad_interval_index];
     149           0 :                                 sad_bits_ref_dequant = sadBits[sad_interval_index] * ref_qindex_dequant;
     150           0 :                                 for (qp_index = sequence_control_set_ptr->static_config.min_qp_allowed; qp_index <= (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed; qp_index++) {
     151           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     152           0 :                                         (EbBitNumber)(((weight * sad_bits_ref_dequant / picture_control_set_ptr->parent_pcs_ptr->deq.y_dequant_QTX[quantizer_to_qindex[qp_index]][1])
     153           0 :                                             + (10 - weight) * (uint32_t)encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] + 5) / 10);
     154             : 
     155           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     156           0 :                                         MIN((uint16_t)encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index], (uint16_t)((1 << 15) - 1));
     157             :                                 }
     158             :                             }
     159             :                         }
     160             :                     }
     161             :                     else {
     162           0 :                         for (sad_interval_index = 0; sad_interval_index < NUMBER_OF_INTRA_SAD_INTERVALS; sad_interval_index++) {
     163           0 :                             if (count[sad_interval_index] > 10)
     164           0 :                                 weight = 8;
     165           0 :                             else if (count[sad_interval_index] > 5)
     166           0 :                                 weight = 5;
     167           0 :                             else if (count[sad_interval_index] >= 1)
     168           0 :                                 weight = 1;
     169           0 :                             if (count[sad_interval_index] > 0) {
     170           0 :                                 sadBits[sad_interval_index] /= count[sad_interval_index];
     171           0 :                                 sad_bits_ref_dequant = sadBits[sad_interval_index] * ref_qindex_dequant;
     172           0 :                                 for (qp_index = sequence_control_set_ptr->static_config.min_qp_allowed; qp_index <= (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed; qp_index++) {
     173           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     174           0 :                                         (EbBitNumber)(((weight * sad_bits_ref_dequant / picture_control_set_ptr->parent_pcs_ptr->deq.y_dequant_QTX[quantizer_to_qindex[qp_index]][1])
     175           0 :                                             + (10 - weight) * (uint32_t)encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] + 5) / 10);
     176             : 
     177           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     178           0 :                                         MIN((uint16_t)encode_context_ptr->rate_control_tables_array[qp_index].intra_sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index], (uint16_t)((1 << 15) - 1));
     179             :                                 }
     180             :                             }
     181             :                         }
     182             :                     }
     183             :                 }
     184             :                 else {
     185           0 :                     if (sequence_control_set_ptr->input_resolution < INPUT_SIZE_4K_RANGE) {
     186           0 :                         for (sad_interval_index = 0; sad_interval_index < NUMBER_OF_SAD_INTERVALS; sad_interval_index++) {
     187           0 :                             if (count[sad_interval_index] > 5)
     188           0 :                                 weight = 8;
     189           0 :                             else if (count[sad_interval_index] > 1)
     190           0 :                                 weight = 5;
     191           0 :                             else if (count[sad_interval_index] == 1)
     192           0 :                                 weight = 1;
     193           0 :                             if (count[sad_interval_index] > 0) {
     194           0 :                                 sadBits[sad_interval_index] /= count[sad_interval_index];
     195           0 :                                 sad_bits_ref_dequant = sadBits[sad_interval_index] * ref_qindex_dequant;
     196           0 :                                 for (qp_index = sequence_control_set_ptr->static_config.min_qp_allowed; qp_index <= (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed; qp_index++) {
     197           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     198           0 :                                         (EbBitNumber)(((weight * sad_bits_ref_dequant / picture_control_set_ptr->parent_pcs_ptr->deq.y_dequant_QTX[quantizer_to_qindex[qp_index]][1])
     199           0 :                                             + (10 - weight) * (uint32_t)encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] + 5) / 10);
     200           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     201           0 :                                         MIN((uint16_t)encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index], (uint16_t)((1 << 15) - 1));
     202             :                                 }
     203             :                             }
     204             :                         }
     205             :                     }
     206             :                     else {
     207           0 :                         for (sad_interval_index = 0; sad_interval_index < NUMBER_OF_SAD_INTERVALS; sad_interval_index++) {
     208           0 :                             if (count[sad_interval_index] > 10)
     209           0 :                                 weight = 7;
     210           0 :                             else if (count[sad_interval_index] > 5)
     211           0 :                                 weight = 5;
     212           0 :                             else if (sad_interval_index > ((NUMBER_OF_SAD_INTERVALS >> 1) - 1) && count[sad_interval_index] > 1)
     213           0 :                                 weight = 1;
     214           0 :                             if (count[sad_interval_index] > 0) {
     215           0 :                                 sadBits[sad_interval_index] /= count[sad_interval_index];
     216           0 :                                 sad_bits_ref_dequant = sadBits[sad_interval_index] * ref_qindex_dequant;
     217           0 :                                 for (qp_index = sequence_control_set_ptr->static_config.min_qp_allowed; qp_index <= (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed; qp_index++) {
     218           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     219           0 :                                         (EbBitNumber)(((weight * sad_bits_ref_dequant / picture_control_set_ptr->parent_pcs_ptr->deq.y_dequant_QTX[quantizer_to_qindex[qp_index]][1])
     220           0 :                                             + (10 - weight) * (uint32_t)encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] + 5) / 10);
     221           0 :                                     encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index] =
     222           0 :                                         MIN((uint16_t)encode_context_ptr->rate_control_tables_array[qp_index].sad_bits_array[picture_control_set_ptr->temporal_layer_index][sad_interval_index], (uint16_t)((1 << 15) - 1));
     223             :                                 }
     224             :                             }
     225             :                         }
     226             :                     }
     227             :                 }
     228             :             }
     229           0 :             eb_release_mutex(encode_context_ptr->rate_table_update_mutex);
     230             :         }
     231             :     }
     232         120 : }
     233           2 : void* packetization_kernel(void *input_ptr)
     234             : {
     235             :     // Context
     236           2 :     PacketizationContext         *context_ptr = (PacketizationContext*)input_ptr;
     237             : 
     238             :     PictureControlSet            *picture_control_set_ptr;
     239             : 
     240             :     // Config
     241             :     SequenceControlSet           *sequence_control_set_ptr;
     242             : 
     243             :     FrameHeader                  *frm_hdr;
     244             : 
     245             :     // Encoding Context
     246             :     EncodeContext                *encode_context_ptr;
     247             : 
     248             :     // Input
     249             :     EbObjectWrapper              *entropyCodingResultsWrapperPtr;
     250             :     EntropyCodingResults         *entropyCodingResultsPtr;
     251             : 
     252             :     // Output
     253             :     EbObjectWrapper              *output_stream_wrapper_ptr;
     254             :     EbBufferHeaderType             *output_stream_ptr;
     255             :     EbObjectWrapper              *rateControlTasksWrapperPtr;
     256             :     RateControlTasks             *rateControlTasksPtr;
     257             :     EbObjectWrapper               *picture_manager_results_wrapper_ptr;
     258             :     PictureDemuxResults           *picture_manager_results_ptr;
     259             :     // Queue variables
     260             :     int32_t                         queueEntryIndex;
     261             :     PacketizationReorderEntry    *queueEntryPtr;
     262             :     EbLinkedListNode               *appDataLLHeadTempPtr;
     263             : 
     264           2 :     context_ptr->tot_shown_frames = 0;
     265           2 :     context_ptr->disp_order_continuity_count = 0;
     266             : 
     267             :     for (;;) {
     268             :         // Get EntropyCoding Results
     269         122 :         eb_get_full_object(
     270             :             context_ptr->entropy_coding_input_fifo_ptr,
     271             :             &entropyCodingResultsWrapperPtr);
     272         120 :         entropyCodingResultsPtr = (EntropyCodingResults*)entropyCodingResultsWrapperPtr->object_ptr;
     273         120 :         picture_control_set_ptr = (PictureControlSet*)entropyCodingResultsPtr->picture_control_set_wrapper_ptr->object_ptr;
     274         120 :         sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
     275         120 :         encode_context_ptr = (EncodeContext*)sequence_control_set_ptr->encode_context_ptr;
     276         120 :         frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
     277             :         //****************************************************
     278             :         // Input Entropy Results into Reordering Queue
     279             :         //****************************************************
     280             :         //get a new entry spot
     281         120 :         queueEntryIndex = picture_control_set_ptr->parent_pcs_ptr->decode_order % PACKETIZATION_REORDER_QUEUE_MAX_DEPTH;
     282         120 :         queueEntryPtr = encode_context_ptr->packetization_reorder_queue[queueEntryIndex];
     283         120 :         queueEntryPtr->start_time_seconds = picture_control_set_ptr->parent_pcs_ptr->start_time_seconds;
     284         120 :         queueEntryPtr->start_time_u_seconds = picture_control_set_ptr->parent_pcs_ptr->start_time_u_seconds;
     285         120 :         queueEntryPtr->is_alt_ref = picture_control_set_ptr->parent_pcs_ptr->is_alt_ref;
     286             : 
     287             :         //TODO: The output buffer should be big enough to avoid a deadlock here. Add an assert that make the warning
     288             :         // Get  Output Bitstream buffer
     289         120 :         output_stream_wrapper_ptr = picture_control_set_ptr->parent_pcs_ptr->output_stream_wrapper_ptr;
     290         120 :         output_stream_ptr = (EbBufferHeaderType*)output_stream_wrapper_ptr->object_ptr;
     291         120 :         output_stream_ptr->flags = 0;
     292         120 :         output_stream_ptr->flags |= (encode_context_ptr->terminating_sequence_flag_received == EB_TRUE && picture_control_set_ptr->parent_pcs_ptr->decode_order == encode_context_ptr->terminating_picture_number) ? EB_BUFFERFLAG_EOS : 0;
     293         120 :         output_stream_ptr->n_filled_len = 0;
     294         120 :         output_stream_ptr->pts = picture_control_set_ptr->parent_pcs_ptr->input_ptr->pts;
     295         120 :         output_stream_ptr->dts = picture_control_set_ptr->parent_pcs_ptr->decode_order - (uint64_t)(1 << picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels) + 1;
     296         240 :         output_stream_ptr->pic_type = picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ?
     297          68 :             picture_control_set_ptr->parent_pcs_ptr->idr_flag ? EB_AV1_KEY_PICTURE :
     298         120 :             picture_control_set_ptr->slice_type : EB_AV1_NON_REF_PICTURE;
     299         120 :         output_stream_ptr->p_app_private = picture_control_set_ptr->parent_pcs_ptr->input_ptr->p_app_private;
     300         120 :         output_stream_ptr->qp            = picture_control_set_ptr->parent_pcs_ptr->picture_qp;
     301             : 
     302         120 :         if (sequence_control_set_ptr->static_config.stat_report){
     303           0 :             output_stream_ptr->luma_sse = picture_control_set_ptr->parent_pcs_ptr->luma_sse;
     304           0 :             output_stream_ptr->cr_sse   = picture_control_set_ptr->parent_pcs_ptr->cr_sse;
     305           0 :             output_stream_ptr->cb_sse   = picture_control_set_ptr->parent_pcs_ptr->cb_sse;
     306             :         } else {
     307         120 :             output_stream_ptr->luma_sse = 0;
     308         120 :             output_stream_ptr->cr_sse   = 0;
     309         120 :             output_stream_ptr->cb_sse   = 0;
     310             :         }
     311             : 
     312             :         // Get Empty Rate Control Input Tasks
     313         120 :         eb_get_empty_object(
     314             :             context_ptr->rate_control_tasks_output_fifo_ptr,
     315             :             &rateControlTasksWrapperPtr);
     316         120 :         rateControlTasksPtr = (RateControlTasks*)rateControlTasksWrapperPtr->object_ptr;
     317         120 :         rateControlTasksPtr->picture_control_set_wrapper_ptr = picture_control_set_ptr->picture_parent_control_set_wrapper_ptr;
     318         120 :         rateControlTasksPtr->task_type = RC_PACKETIZATION_FEEDBACK_RESULT;
     319         120 :         if (picture_control_set_ptr->parent_pcs_ptr->frame_end_cdf_update_mode &&
     320          60 :             picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE &&
     321          38 :             picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr) {
     322             : 
     323          38 :             eb_av1_reset_cdf_symbol_counters(picture_control_set_ptr->entropy_coder_ptr->fc);
     324          38 :             ((EbReferenceObject*)picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr)->frame_context
     325          38 :                 = (*picture_control_set_ptr->entropy_coder_ptr->fc);
     326             : 
     327             :             // Get Empty Results Object
     328          38 :             eb_get_empty_object(
     329             :                 context_ptr->picture_manager_input_fifo_ptr,
     330             :                 &picture_manager_results_wrapper_ptr);
     331             : 
     332          38 :             picture_manager_results_ptr = (PictureDemuxResults*)picture_manager_results_wrapper_ptr->object_ptr;
     333          38 :             picture_manager_results_ptr->picture_number = picture_control_set_ptr->picture_number;
     334          38 :             picture_manager_results_ptr->picture_type = EB_PIC_FEEDBACK;
     335          38 :             picture_manager_results_ptr->sequence_control_set_wrapper_ptr = picture_control_set_ptr->sequence_control_set_wrapper_ptr;
     336             :         }
     337             :         else {
     338          82 :             picture_manager_results_wrapper_ptr = EB_NULL;
     339             :             (void)picture_manager_results_ptr;
     340             :             (void)picture_manager_results_wrapper_ptr;
     341             :         }
     342             :         // Reset the bitstream before writing to it
     343         120 :         reset_bitstream(
     344         120 :             picture_control_set_ptr->bitstream_ptr->output_bitstream_ptr);
     345             : 
     346             :         // Code the SPS
     347         120 :         if (frm_hdr->frame_type == KEY_FRAME) {
     348           2 :             encode_sps_av1(
     349             :                 picture_control_set_ptr->bitstream_ptr,
     350             :                 sequence_control_set_ptr);
     351             :         }
     352             : 
     353         120 :         write_frame_header_av1(
     354             :             picture_control_set_ptr->bitstream_ptr,
     355             :             sequence_control_set_ptr,
     356             :             picture_control_set_ptr,
     357             :             0);
     358             : 
     359             :         // Copy Slice Header to the Output Bitstream
     360         120 :         copy_rbsp_bitstream_to_payload(
     361             :             picture_control_set_ptr->bitstream_ptr,
     362             :             output_stream_ptr->p_buffer,
     363             :             (uint32_t*) &(output_stream_ptr->n_filled_len),
     364             :             (uint32_t*) &(output_stream_ptr->n_alloc_len),
     365             :             encode_context_ptr);
     366         120 :         if (picture_control_set_ptr->parent_pcs_ptr->has_show_existing) {
     367             :             // Reset the bitstream before writing to it
     368          56 :             reset_bitstream(
     369          56 :                 picture_control_set_ptr->bitstream_ptr->output_bitstream_ptr);
     370          56 :             write_frame_header_av1(
     371             :                 picture_control_set_ptr->bitstream_ptr,
     372             :                 sequence_control_set_ptr,
     373             :                 picture_control_set_ptr,
     374             :                 1);
     375             : 
     376             :             // Copy Slice Header to the Output Bitstream
     377          56 :             copy_rbsp_bitstream_to_payload(
     378             :                 picture_control_set_ptr->bitstream_ptr,
     379             :                 output_stream_ptr->p_buffer,
     380             :                 (uint32_t*)&(output_stream_ptr->n_filled_len),
     381             :                 (uint32_t*)&(output_stream_ptr->n_alloc_len),
     382             :                 encode_context_ptr);
     383             : 
     384          56 :             output_stream_ptr->flags |= EB_BUFFERFLAG_SHOW_EXT;
     385             :         }
     386             : 
     387             :         // Send the number of bytes per frame to RC
     388         120 :         picture_control_set_ptr->parent_pcs_ptr->total_num_bits = output_stream_ptr->n_filled_len << 3;
     389         120 :         queueEntryPtr->total_num_bits = picture_control_set_ptr->parent_pcs_ptr->total_num_bits;
     390             :         // update the rate tables used in RC based on the encoded bits of each sb
     391         120 :         update_rc_rate_tables(
     392             :             picture_control_set_ptr,
     393             :             sequence_control_set_ptr);
     394         120 :         queueEntryPtr->frame_type = frm_hdr->frame_type;
     395         120 :         queueEntryPtr->poc = picture_control_set_ptr->picture_number;
     396         120 :         memcpy(&queueEntryPtr->av1_ref_signal, &picture_control_set_ptr->parent_pcs_ptr->av1_ref_signal, sizeof(Av1RpsNode));
     397             : 
     398         120 :         queueEntryPtr->slice_type = picture_control_set_ptr->slice_type;
     399             : #if DETAILED_FRAME_OUTPUT
     400             :         queueEntryPtr->ref_poc_list0 = picture_control_set_ptr->parent_pcs_ptr->ref_pic_poc_array[REF_LIST_0][0];
     401             :         queueEntryPtr->ref_poc_list1 = picture_control_set_ptr->parent_pcs_ptr->ref_pic_poc_array[REF_LIST_1][0];
     402             :         memcpy(queueEntryPtr->ref_poc_array, picture_control_set_ptr->parent_pcs_ptr->av1RefSignal.ref_poc_array, 7 * sizeof(uint64_t));
     403             : #endif
     404         120 :         queueEntryPtr->show_frame = frm_hdr->show_frame;
     405         120 :         queueEntryPtr->has_show_existing = picture_control_set_ptr->parent_pcs_ptr->has_show_existing;
     406         120 :         queueEntryPtr->show_existing_frame = frm_hdr->show_existing_frame;
     407             : 
     408             :         //Store the output buffer in the Queue
     409         120 :         queueEntryPtr->output_stream_wrapper_ptr = output_stream_wrapper_ptr;
     410             : 
     411             :         // Note: last chance here to add more output meta data for an encoded picture -->
     412             : 
     413             :         // collect output meta data
     414         120 :         queueEntryPtr->out_meta_data = concat_eb_linked_list(ExtractPassthroughData(&(picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr)),
     415         120 :             picture_control_set_ptr->parent_pcs_ptr->app_out_data_ll_head_ptr);
     416         120 :         picture_control_set_ptr->parent_pcs_ptr->app_out_data_ll_head_ptr = (EbLinkedListNode *)EB_NULL;
     417             : 
     418             :         // Calling callback functions to release the memory allocated for data linked list in the application
     419         120 :         while (picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr != EB_NULL) {
     420           0 :             appDataLLHeadTempPtr = picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr->next;
     421           0 :             if (picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr->release_cb_fnc_ptr != EB_NULL)
     422           0 :                 picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr->release_cb_fnc_ptr(picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr);
     423           0 :             picture_control_set_ptr->parent_pcs_ptr->data_ll_head_ptr = appDataLLHeadTempPtr;
     424             :         }
     425             : 
     426         120 :         if (sequence_control_set_ptr->static_config.speed_control_flag) {
     427             :             // update speed control variables
     428           0 :             eb_block_on_mutex(encode_context_ptr->sc_buffer_mutex);
     429           0 :             encode_context_ptr->sc_frame_out++;
     430           0 :             eb_release_mutex(encode_context_ptr->sc_buffer_mutex);
     431             :         }
     432             : 
     433             :         // Post Rate Control Taks
     434         120 :         eb_post_full_object(rateControlTasksWrapperPtr);
     435         120 :         if (picture_control_set_ptr->parent_pcs_ptr->frame_end_cdf_update_mode &&
     436          60 :             picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE &&
     437          38 :             picture_control_set_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr)
     438             :             // Post the Full Results Object
     439          38 :             eb_post_full_object(picture_manager_results_wrapper_ptr);
     440             :         else
     441             :             // Since feedback is not set to PM, life count of is reduced here instead of PM
     442          82 :             eb_release_object(picture_control_set_ptr->sequence_control_set_wrapper_ptr);
     443             :         //Release the Parent PCS then the Child PCS
     444         120 :         eb_release_object(entropyCodingResultsPtr->picture_control_set_wrapper_ptr);//Child
     445             : 
     446             :         // Release the Entropy Coding Result
     447         120 :         eb_release_object(entropyCodingResultsWrapperPtr);
     448             : 
     449             :         //****************************************************
     450             :         // Process the head of the queue
     451             :         //****************************************************
     452             :         // Look at head of queue and see if any picture is ready to go
     453         120 :         queueEntryPtr = encode_context_ptr->packetization_reorder_queue[encode_context_ptr->packetization_reorder_queue_head_index];
     454             : 
     455         240 :         while (queueEntryPtr->output_stream_wrapper_ptr != EB_NULL) {
     456         120 :             EbBool has_tiles = (EbBool)(sequence_control_set_ptr->static_config.tile_columns || sequence_control_set_ptr->static_config.tile_rows);
     457         120 :             output_stream_wrapper_ptr = queueEntryPtr->output_stream_wrapper_ptr;
     458         120 :             output_stream_ptr = (EbBufferHeaderType*)output_stream_wrapper_ptr->object_ptr;
     459             : 
     460         120 :             if (queueEntryPtr->has_show_existing) {
     461          56 :                 write_td(output_stream_ptr, EB_TRUE, has_tiles);
     462          56 :                 output_stream_ptr->n_filled_len += TD_SIZE;
     463             :             }
     464             : 
     465         120 :             if (encode_context_ptr->td_needed == EB_TRUE){
     466          64 :                 output_stream_ptr->flags |= (uint32_t)EB_BUFFERFLAG_HAS_TD;
     467          64 :                 write_td(output_stream_ptr, EB_FALSE, has_tiles);
     468          64 :                 encode_context_ptr->td_needed = EB_FALSE;
     469          64 :                 output_stream_ptr->n_filled_len += TD_SIZE;
     470             :             }
     471             : 
     472         120 :             if (queueEntryPtr->has_show_existing || queueEntryPtr->show_frame)
     473          64 :                 encode_context_ptr->td_needed = EB_TRUE;
     474             : 
     475             : #if DETAILED_FRAME_OUTPUT
     476             :             {
     477             :                 int32_t i;
     478             :                 uint8_t  showTab[] = { 'H', 'V' };
     479             : 
     480             :                 //Countinuity count check of visible frames
     481             :                 if (queueEntryPtr->show_frame) {
     482             :                     if (context_ptr->disp_order_continuity_count == queueEntryPtr->poc)
     483             :                         context_ptr->disp_order_continuity_count++;
     484             :                     else
     485             :                     {
     486             :                         SVT_LOG("SVT [ERROR]: disp_order_continuity_count Error1 POC:%i\n", (int32_t)queueEntryPtr->poc);
     487             :                         exit(0);
     488             :                     }
     489             :                 }
     490             : 
     491             :                 if (queueEntryPtr->has_show_existing) {
     492             :                     if (context_ptr->disp_order_continuity_count == context_ptr->dpb_disp_order[queueEntryPtr->show_existing_frame])
     493             :                         context_ptr->disp_order_continuity_count++;
     494             :                     else
     495             :                     {
     496             :                         SVT_LOG("SVT [ERROR]: disp_order_continuity_count Error2 POC:%i\n", (int32_t)queueEntryPtr->poc);
     497             :                         exit(0);
     498             :                     }
     499             :                 }
     500             : 
     501             :                 //update total number of shown frames
     502             :                 if (queueEntryPtr->show_frame)
     503             :                     context_ptr->tot_shown_frames++;
     504             :                 if (queueEntryPtr->has_show_existing)
     505             :                     context_ptr->tot_shown_frames++;
     506             : 
     507             :                 //implement the GOP here - Serial dec order
     508             :                 if (queueEntryPtr->frame_type == KEY_FRAME)
     509             :                 {
     510             :                     //reset the DPB on a Key frame
     511             :                     for (i = 0; i < 8; i++)
     512             :                     {
     513             :                         context_ptr->dpb_dec_order[i] = queueEntryPtr->picture_number;
     514             :                         context_ptr->dpb_disp_order[i] = queueEntryPtr->poc;
     515             :                     }
     516             :                     SVT_LOG("%i  %i  %c ****KEY***** %i frames\n", (int32_t)queueEntryPtr->picture_number, (int32_t)queueEntryPtr->poc, showTab[queueEntryPtr->show_frame], (int32_t)context_ptr->tot_shown_frames);
     517             :                 }
     518             :                 else
     519             :                 {
     520             :                     int32_t LASTrefIdx = queueEntryPtr->av1_ref_signal.ref_dpb_index[0];
     521             :                     int32_t BWDrefIdx = queueEntryPtr->av1_ref_signal.ref_dpb_index[4];
     522             : 
     523             :                     if (queueEntryPtr->frame_type == INTER_FRAME)
     524             :                     {
     525             :                         if (queueEntryPtr->has_show_existing)
     526             :                             SVT_LOG("%i (%i  %i)    %i  (%i  %i)   %c  showEx: %i   %i frames\n",
     527             :                             (int32_t)queueEntryPtr->picture_number, (int32_t)context_ptr->dpb_dec_order[LASTrefIdx], (int32_t)context_ptr->dpb_dec_order[BWDrefIdx],
     528             :                                 (int32_t)queueEntryPtr->poc, (int32_t)context_ptr->dpb_disp_order[LASTrefIdx], (int32_t)context_ptr->dpb_disp_order[BWDrefIdx],
     529             :                                 showTab[queueEntryPtr->show_frame], (int32_t)context_ptr->dpb_disp_order[queueEntryPtr->show_existing_frame], (int32_t)context_ptr->tot_shown_frames);
     530             :                         else
     531             :                             SVT_LOG("%i (%i  %i)    %i  (%i  %i)   %c  %i frames\n",
     532             :                             (int32_t)queueEntryPtr->picture_number, (int32_t)context_ptr->dpb_dec_order[LASTrefIdx], (int32_t)context_ptr->dpb_dec_order[BWDrefIdx],
     533             :                                 (int32_t)queueEntryPtr->poc, (int32_t)context_ptr->dpb_disp_order[LASTrefIdx], (int32_t)context_ptr->dpb_disp_order[BWDrefIdx],
     534             :                                 showTab[queueEntryPtr->show_frame], (int32_t)context_ptr->tot_shown_frames);
     535             : 
     536             :                         if (queueEntryPtr->ref_poc_list0 != context_ptr->dpb_disp_order[LASTrefIdx])
     537             :                         {
     538             :                             SVT_LOG("L0 MISMATCH POC:%i\n", (int32_t)queueEntryPtr->poc);
     539             :                             exit(0);
     540             :                         }
     541             :                         if (sequence_control_set_ptr->static_config.hierarchical_levels == 3 && queueEntryPtr->slice_type == B_SLICE && queueEntryPtr->ref_poc_list1 != context_ptr->dpb_disp_order[BWDrefIdx])
     542             :                         {
     543             :                             SVT_LOG("L1 MISMATCH POC:%i\n", (int32_t)queueEntryPtr->poc);
     544             :                             exit(0);
     545             :                         }
     546             : 
     547             :                         for (int rr = 0; rr < 7; rr++)
     548             :                         {
     549             :                             uint8_t dpb_spot = queueEntryPtr->av1RefSignal.refDpbIndex[rr];
     550             : 
     551             :                             if (queueEntryPtr->ref_poc_array[rr] != context_ptr->dpbDispOrder[dpb_spot])
     552             :                                 printf("REF_POC MISMATCH POC:%i  ref:%i\n", (int32_t)queueEntryPtr->poc, rr);
     553             :                         }
     554             :                     }
     555             :                     else
     556             :                     {
     557             :                         if (queueEntryPtr->has_show_existing)
     558             :                             SVT_LOG("%i  %i  %c   showEx: %i ----INTRA---- %i frames \n", (int32_t)queueEntryPtr->picture_number, (int32_t)queueEntryPtr->poc,
     559             :                                 showTab[queueEntryPtr->show_frame], (int32_t)context_ptr->dpb_disp_order[queueEntryPtr->show_existing_frame], (int32_t)context_ptr->tot_shown_frames);
     560             :                         else
     561             :                             printf("%i  %i  %c   ----INTRA---- %i frames\n", (int32_t)queueEntryPtr->picture_number, (int32_t)queueEntryPtr->poc,
     562             :                             (int32_t)showTab[queueEntryPtr->show_frame], (int32_t)context_ptr->tot_shown_frames);
     563             :                     }
     564             : 
     565             :                     //Update the DPB
     566             :                     for (i = 0; i < 8; i++)
     567             :                     {
     568             :                         if ((queueEntryPtr->av1_ref_signal.refresh_frame_mask >> i) & 1)
     569             :                         {
     570             :                             context_ptr->dpb_dec_order[i] = queueEntryPtr->picture_number;
     571             :                             context_ptr->dpb_disp_order[i] = queueEntryPtr->poc;
     572             :                         }
     573             :                     }
     574             :                 }
     575             :             }
     576             : #endif
     577             : #if ADP_STATS_PER_LAYER
     578             :             if (queueEntryPtr->picture_number == sequence_control_set_ptr->static_config.frames_to_be_encoded - 1) {
     579             :                 uint8_t layerIndex;
     580             :                 SVT_LOG("\nsq_search_count\tsq_non4_search_count\tmdc_count\tpred_count\tpred1_nfl_count");
     581             :                 for (layerIndex = 0; layerIndex < 5; layerIndex++) {
     582             :                     SVT_LOG("\n/***************************Layer %d Stats ********************************/\n", layerIndex);
     583             :                     if (sequence_control_set_ptr->total_count[layerIndex]) {
     584             :                         SVT_LOG("%d\t", ((sequence_control_set_ptr->sq_search_count[layerIndex] * 100) / sequence_control_set_ptr->total_count[layerIndex]));
     585             :                         SVT_LOG("%d\t", ((sequence_control_set_ptr->sq_non4_search_count[layerIndex] * 100) / sequence_control_set_ptr->total_count[layerIndex]));
     586             :                         SVT_LOG("%d\t", ((sequence_control_set_ptr->mdc_count[layerIndex] * 100) / sequence_control_set_ptr->total_count[layerIndex]));
     587             :                         SVT_LOG("%d\t", ((sequence_control_set_ptr->pred_count[layerIndex] * 100) / sequence_control_set_ptr->total_count[layerIndex]));
     588             :                         SVT_LOG("%d\t", ((sequence_control_set_ptr->pred1_nfl_count[layerIndex] * 100) / sequence_control_set_ptr->total_count[layerIndex]));
     589             :                     }
     590             :                 }
     591             :                 SVT_LOG("\n");
     592             :             }
     593             : #endif
     594             :             // Calculate frame latency in milliseconds
     595         120 :             double latency = 0.0;
     596         120 :             uint64_t finishTimeSeconds = 0;
     597         120 :             uint64_t finishTimeuSeconds = 0;
     598         120 :             EbFinishTime(&finishTimeSeconds, &finishTimeuSeconds);
     599             : 
     600         120 :             EbComputeOverallElapsedTimeMs(
     601             :                 queueEntryPtr->start_time_seconds,
     602             :                 queueEntryPtr->start_time_u_seconds,
     603             :                 finishTimeSeconds,
     604             :                 finishTimeuSeconds,
     605             :                 &latency);
     606             : 
     607         120 :             output_stream_ptr->n_tick_count = (uint32_t)latency;
     608         120 :             output_stream_ptr->p_app_private = queueEntryPtr->out_meta_data;
     609         120 :             if (queueEntryPtr->is_alt_ref)
     610           0 :                 output_stream_ptr->flags |= (uint32_t)EB_BUFFERFLAG_IS_ALT_REF;
     611             : 
     612         120 :             eb_post_full_object(output_stream_wrapper_ptr);
     613         120 :             queueEntryPtr->out_meta_data = (EbLinkedListNode *)EB_NULL;
     614             : 
     615             :             // Reset the Reorder Queue Entry
     616         120 :             queueEntryPtr->picture_number += PACKETIZATION_REORDER_QUEUE_MAX_DEPTH;
     617         120 :             queueEntryPtr->output_stream_wrapper_ptr = (EbObjectWrapper *)EB_NULL;
     618             : 
     619         120 :             if (encode_context_ptr->statistics_port_active)
     620           0 :                 queueEntryPtr->outputStatisticsWrapperPtr = (EbObjectWrapper *)EB_NULL;
     621             :             // Increment the Reorder Queue head Ptr
     622         120 :             encode_context_ptr->packetization_reorder_queue_head_index =
     623         120 :                 (encode_context_ptr->packetization_reorder_queue_head_index == PACKETIZATION_REORDER_QUEUE_MAX_DEPTH - 1) ? 0 : encode_context_ptr->packetization_reorder_queue_head_index + 1;
     624             : 
     625         120 :             queueEntryPtr = encode_context_ptr->packetization_reorder_queue[encode_context_ptr->packetization_reorder_queue_head_index];
     626             :         }
     627             :     }
     628             :     return EB_NULL;
     629             : }

Generated by: LCOV version 1.14