Coverage Report

Created: 2026-07-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/ec_process.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 Intel Corporation
3
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
4
*
5
* This source code is subject to the terms of the BSD 2 Clause License and
6
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7
* was not distributed with this source code in the LICENSE file, you can
8
* obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9
* Media Patent License 1.0 was not distributed with this source code in the
10
* PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11
*/
12
13
#include <stdlib.h>
14
#include <stdio.h>
15
#include <string.h>
16
#include "enc_handle.h"
17
#include "entropy_coding.h"
18
#include "ec_process.h"
19
#include "enc_dec_results.h"
20
#include "ec_results.h"
21
#include "rc_tasks.h"
22
#include "cabac_context_model.h"
23
#include "svt_log.h"
24
#include "common_dsp_rtcd.h"
25
26
448
static void rest_context_dctor(EbPtr p) {
27
448
    EbThreadContext*      thread_ctx = (EbThreadContext*)p;
28
448
    EntropyCodingContext* obj        = (EntropyCodingContext*)thread_ctx->priv;
29
448
    EB_FREE_ARRAY(obj);
30
448
}
31
32
/******************************************************
33
 * Enc Dec Context Constructor
34
 ******************************************************/
35
EbErrorType svt_aom_entropy_coding_context_ctor(EbThreadContext* thread_ctx, const EbEncHandle* enc_handle_ptr,
36
448
                                                int index) {
37
448
    EntropyCodingContext* context_ptr;
38
448
    EB_CALLOC_ARRAY(context_ptr, 1);
39
448
    thread_ctx->priv  = context_ptr;
40
448
    thread_ctx->dctor = rest_context_dctor;
41
42
448
    context_ptr->is_16bit = enc_handle_ptr->scs_instance->scs->static_config.encoder_bit_depth > EB_EIGHT_BIT;
43
44
    // Zero levels_buf once; the tail (offset >= LEVELS_TAIL_OFFSET) serves as
45
    // bottom padding for all block sizes via offset-based placement in set_levels.
46
448
    memset(context_ptr->levels_buf + LEVELS_TAIL_OFFSET, 0, TX_PAD_2D - LEVELS_TAIL_OFFSET);
47
48
    // Input/Output System Resource Manager FIFOs
49
448
    context_ptr->enc_dec_input_fifo_ptr = svt_system_resource_get_consumer_fifo(
50
448
        enc_handle_ptr->rest_results_resource_ptr, index);
51
448
    context_ptr->entropy_coding_output_fifo_ptr = svt_system_resource_get_producer_fifo(
52
448
        enc_handle_ptr->entropy_coding_results_resource_ptr, index);
53
54
448
    return EB_ErrorNone;
55
448
}
56
57
/***********************************************
58
 * Entropy Coding Reset Neighbor Arrays
59
 ***********************************************/
60
4.76k
static void entropy_coding_reset_neighbor_arrays(PictureControlSet* pcs, uint16_t tile_idx) {
61
4.76k
    svt_aom_neighbor_array_unit_reset(pcs->partition_context_na[tile_idx]);
62
63
4.76k
    svt_aom_neighbor_array_unit_reset(pcs->luma_dc_sign_level_coeff_na[tile_idx]);
64
4.76k
    svt_aom_neighbor_array_unit_reset(pcs->cb_dc_sign_level_coeff_na[tile_idx]);
65
4.76k
    svt_aom_neighbor_array_unit_reset(pcs->cr_dc_sign_level_coeff_na[tile_idx]);
66
4.76k
    svt_aom_neighbor_array_unit_reset(pcs->txfm_context_array[tile_idx]);
67
4.76k
}
68
69
/**************************************************
70
 * Reset Entropy Coding Picture
71
 **************************************************/
72
448
static void reset_entropy_coding_picture(EntropyCodingContext* ctx, PictureControlSet* pcs, SequenceControlSet* scs) {
73
448
    PictureParentControlSet* ppcs     = pcs->ppcs;
74
448
    const uint16_t           tile_cnt = ppcs->av1_cm->tiles_info.tile_rows * ppcs->av1_cm->tiles_info.tile_cols;
75
448
    ctx->is_16bit                     = scs->static_config.encoder_bit_depth > EB_EIGHT_BIT;
76
448
    const FrameHeader* frm_hdr        = &ppcs->frm_hdr;
77
    // Asuming cb and cr offset to be the same for chroma QP in both slice and pps for lambda computation
78
448
    const uint32_t entropy_coding_qp = frm_hdr->quantization_params.base_q_idx;
79
80
5.21k
    for (uint16_t tile_idx = 0; tile_idx < tile_cnt; tile_idx++) {
81
4.76k
        ppcs->prev_qindex[tile_idx] = entropy_coding_qp;
82
4.76k
    }
83
448
    if (frm_hdr->allow_intrabc) {
84
0
        assert(frm_hdr->delta_lf_params.delta_lf_present == 0);
85
0
    }
86
448
    if (frm_hdr->delta_lf_params.delta_lf_present) {
87
0
        ppcs->prev_delta_lf_from_base = 0;
88
89
0
        const int frame_lf_count = ppcs->monochrome == 0 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
90
0
        for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
91
0
            ppcs->prev_delta_lf[lf_id] = 0;
92
0
        }
93
0
    }
94
95
    // pass the ent
96
5.21k
    for (uint16_t tile_idx = 0; tile_idx < tile_cnt; tile_idx++) {
97
4.76k
        EntropyCoder*        ec                   = pcs->ec_info[tile_idx]->ec;
98
4.76k
        OutputBitstreamUnit* output_bitstream_ptr = ec->ec_output_bitstream_ptr;
99
        //****************************************************************//
100
4.76k
        ec->ec_writer.allow_update_cdf = !ppcs->large_scale_tile && !frm_hdr->disable_cdf_update;
101
4.76k
        aom_start_encode(&ec->ec_writer, output_bitstream_ptr);
102
        // ADD Reset here
103
4.76k
        const uint8_t primary_ref_frame = frm_hdr->primary_ref_frame;
104
4.76k
        if (primary_ref_frame != PRIMARY_REF_NONE) {
105
            // primary ref stored as REF_FRAME_MINUS1, while get_list_idx/get_ref_frame_idx take arg of ref frame
106
            // Therefore, add 1 to the primary ref frame (e.g. LAST --> LAST_FRAME)
107
0
            const uint8_t      list_idx = get_list_idx(primary_ref_frame + 1);
108
0
            const uint8_t      ref_idx  = get_ref_frame_idx(primary_ref_frame + 1);
109
0
            EbReferenceObject* ref      = (EbReferenceObject*)pcs->ref_pic_ptr_array[list_idx][ref_idx]->object_ptr;
110
0
            svt_memcpy(ec->fc, &ref->frame_context, sizeof(FRAME_CONTEXT));
111
4.76k
        } else {
112
4.76k
            svt_aom_reset_entropy_coder(scs->enc_ctx, ec, entropy_coding_qp, pcs->slice_type);
113
4.76k
        }
114
115
4.76k
        entropy_coding_reset_neighbor_arrays(pcs, tile_idx);
116
4.76k
    }
117
448
}
118
119
/* Entropy Coding */
120
121
/*********************************************************************************
122
 *
123
 * @brief
124
 *  The Entropy Coding process is responsible for producing an AV1 conformant bitstream for each
125
 *frame.
126
 *
127
 * @par Description:
128
 *  The entropy coder is a frame-based process and is based on multi-symbol arithmetic range coding.
129
 *  It takes as input the coding decisions and information for each block and produces as output the
130
 *bitstream for each frame.
131
 *
132
 * @param[in] Coding Decisions
133
 *  Coding decisions and information for each block.
134
 *
135
 * @param[out] bitstream
136
 *  Bitstream for each block
137
 *
138
 ********************************************************************************/
139
5.21k
EbErrorType svt_aom_entropy_coding_kernel_iter(void* context) {
140
5.21k
    EntropyCodingContext* context_ptr = (EntropyCodingContext*)context;
141
142
    // Input
143
5.21k
    EbObjectWrapper* rest_results_wrapper;
144
145
    // Output
146
5.21k
    EbObjectWrapper* entropy_coding_results_wrapper_ptr;
147
148
    // Get Mode Decision Results
149
5.21k
    EB_GET_FULL_OBJECT(context_ptr->enc_dec_input_fifo_ptr, &rest_results_wrapper);
150
151
4.76k
    RestResults*        rest_results = (RestResults*)rest_results_wrapper->object_ptr;
152
4.76k
    PictureControlSet*  pcs          = (PictureControlSet*)rest_results->pcs_wrapper->object_ptr;
153
4.76k
    SequenceControlSet* scs          = pcs->scs;
154
    // SB Constants
155
156
4.76k
    uint32_t sb_size = scs->sb_size;
157
158
4.76k
    uint8_t          sb_size_log2    = (uint8_t)svt_log2f(sb_size);
159
4.76k
    uint32_t         pic_width_in_sb = (pcs->ppcs->aligned_width + sb_size - 1) >> sb_size_log2;
160
4.76k
    uint16_t         tile_idx        = rest_results->tile_index;
161
4.76k
    Av1Common* const cm              = pcs->ppcs->av1_cm;
162
4.76k
    const uint16_t   tile_cnt        = cm->tiles_info.tile_rows * cm->tiles_info.tile_cols;
163
4.76k
    const uint16_t   tile_col        = tile_idx % cm->tiles_info.tile_cols;
164
4.76k
    const uint16_t   tile_row        = tile_idx / cm->tiles_info.tile_cols;
165
4.76k
    const uint16_t   tile_sb_start_x = cm->tiles_info.tile_col_start_mi[tile_col] >> scs->seq_header.sb_size_log2;
166
4.76k
    const uint16_t   tile_sb_start_y = cm->tiles_info.tile_row_start_mi[tile_row] >> scs->seq_header.sb_size_log2;
167
168
4.76k
    uint16_t tile_width_in_sb = (cm->tiles_info.tile_col_start_mi[tile_col + 1] -
169
4.76k
                                 cm->tiles_info.tile_col_start_mi[tile_col]) >>
170
4.76k
        scs->seq_header.sb_size_log2;
171
4.76k
    uint16_t tile_height_in_sb = (cm->tiles_info.tile_row_start_mi[tile_row + 1] -
172
4.76k
                                  cm->tiles_info.tile_row_start_mi[tile_row]) >>
173
4.76k
        scs->seq_header.sb_size_log2;
174
175
4.76k
    bool frame_entropy_done = false;
176
177
4.76k
    svt_block_on_mutex(pcs->entropy_coding_pic_mutex);
178
4.76k
    if (pcs->entropy_coding_pic_reset_flag) {
179
448
        pcs->entropy_coding_pic_reset_flag = false;
180
181
448
        reset_entropy_coding_picture(context_ptr, pcs, scs);
182
448
    }
183
4.76k
    svt_release_mutex(pcs->entropy_coding_pic_mutex);
184
185
4.76k
    if (!svt_aom_is_pic_skipped(pcs->ppcs)) {
186
4.76k
        context_ptr->tot_qindex = 0;
187
4.76k
        context_ptr->valid_area = 0;
188
9.93k
        for (uint32_t y_sb_index = 0; y_sb_index < tile_height_in_sb; ++y_sb_index) {
189
11.1k
            for (uint32_t x_sb_index = 0; x_sb_index < tile_width_in_sb; ++x_sb_index) {
190
5.93k
                uint16_t    sb_index = (uint16_t)((x_sb_index + tile_sb_start_x) +
191
5.93k
                                               (y_sb_index + tile_sb_start_y) * pic_width_in_sb);
192
5.93k
                SuperBlock* sb_ptr   = pcs->sb_ptr_array[sb_index];
193
194
5.93k
                const uint32_t sb_origin_x = (x_sb_index + tile_sb_start_x) << sb_size_log2;
195
5.93k
                const uint32_t sb_origin_y = (y_sb_index + tile_sb_start_y) << sb_size_log2;
196
5.93k
                if (x_sb_index == 0 && y_sb_index == 0) {
197
4.76k
                    svt_av1_reset_loop_restoration(context_ptr);
198
4.76k
                    context_ptr->tok = pcs->tile_tok[tile_row][tile_col];
199
4.76k
                }
200
201
5.93k
                EbPictureBufferDesc* coeff_picture_ptr = pcs->ppcs->enc_dec_ptr->quantized_coeff[sb_index];
202
5.93k
                context_ptr->coded_area_sb             = 0;
203
5.93k
                context_ptr->coded_area_sb_uv          = 0;
204
                // Ensure EC buffer has room for worst-case SB output (4 bytes/pixel)
205
5.93k
                EbErrorType ret = svt_aom_ec_ensure_capacity(&pcs->ec_info[tile_idx]->ec->ec_writer,
206
5.93k
                                                             sb_size * sb_size * 4);
207
5.93k
                if (ret != EB_ErrorNone) {
208
0
                    return ret;
209
0
                }
210
5.93k
                svt_aom_write_modes_sb(context_ptr,
211
5.93k
                                       sb_ptr,
212
5.93k
                                       pcs,
213
5.93k
                                       tile_idx,
214
5.93k
                                       pcs->ec_info[tile_idx]->ec,
215
5.93k
                                       coeff_picture_ptr,
216
5.93k
                                       sb_ptr->ptree,
217
5.93k
                                       sb_origin_y >> MI_SIZE_LOG2,
218
5.93k
                                       sb_origin_x >> MI_SIZE_LOG2);
219
5.93k
            }
220
5.16k
        }
221
4.76k
    }
222
4.76k
    bool pic_ready = true;
223
224
    // Current tile ready
225
4.76k
    svt_aom_encode_slice_finish(pcs->ec_info[tile_idx]->ec);
226
227
4.76k
    svt_block_on_mutex(pcs->entropy_coding_pic_mutex);
228
    // Flush locally-accumulated qindex stats (avoids per-block mutex)
229
4.76k
    pcs->ppcs->tot_qindex += context_ptr->tot_qindex;
230
4.76k
    pcs->ppcs->valid_qindex_area += context_ptr->valid_area;
231
4.76k
    pcs->ec_info[tile_idx]->entropy_coding_tile_done = true;
232
37.1k
    for (uint16_t i = 0; i < tile_cnt; i++) {
233
36.6k
        if (pcs->ec_info[i]->entropy_coding_tile_done == false) {
234
4.32k
            pic_ready = false;
235
4.32k
            break;
236
4.32k
        }
237
36.6k
    }
238
4.76k
    svt_release_mutex(pcs->entropy_coding_pic_mutex);
239
4.76k
    if (pic_ready) {
240
448
        if (pcs->ppcs->superres_total_recode_loop == 0) {
241
            // Release the reference Pictures from both lists
242
3.58k
            for (REF_FRAME_MINUS1 ref = LAST; ref < ALT + 1; ref++) {
243
3.13k
                const uint8_t list_idx = get_list_idx(ref + 1);
244
3.13k
                const uint8_t ref_idx  = get_ref_frame_idx(ref + 1);
245
3.13k
                if (pcs->ref_pic_ptr_array[list_idx][ref_idx] != NULL) {
246
0
                    svt_release_object(pcs->ref_pic_ptr_array[list_idx][ref_idx]);
247
0
                }
248
3.13k
            }
249
250
            //free palette data
251
448
            if (pcs->tile_tok[0][0]) {
252
0
                EB_FREE_ARRAY(pcs->tile_tok[0][0]);
253
0
            }
254
448
        }
255
448
        frame_entropy_done = true;
256
448
    }
257
258
4.76k
    if (frame_entropy_done) {
259
448
        if (pcs->ppcs->valid_qindex_area) {
260
448
            pcs->ppcs->avg_qp = ((pcs->ppcs->tot_qindex / pcs->ppcs->valid_qindex_area) + 2) >> 2;
261
448
        }
262
        // Get Empty Entropy Coding Results
263
448
        svt_get_empty_object(context_ptr->entropy_coding_output_fifo_ptr, &entropy_coding_results_wrapper_ptr);
264
448
        EntropyCodingResults* entropy_coding_results_ptr = (EntropyCodingResults*)
265
448
                                                               entropy_coding_results_wrapper_ptr->object_ptr;
266
448
        entropy_coding_results_ptr->pcs_wrapper = rest_results->pcs_wrapper;
267
268
        // Post EntropyCoding Results
269
448
        svt_post_full_object(entropy_coding_results_wrapper_ptr);
270
448
    }
271
272
    // Release Mode Decision Results
273
4.76k
    svt_release_object(rest_results_wrapper);
274
4.76k
    return EB_ErrorNone;
275
4.76k
}
276
277
448
void* svt_aom_entropy_coding_kernel(void* input_ptr) {
278
448
    EbThreadContext* thread_ctx = (EbThreadContext*)input_ptr;
279
5.21k
    for (;;) {
280
5.21k
        EbErrorType err = svt_aom_entropy_coding_kernel_iter(thread_ctx->priv);
281
5.21k
        if (err == EB_NoErrorFifoShutdown) {
282
448
            return NULL;
283
448
        }
284
5.21k
    }
285
0
    return NULL;
286
448
}