Coverage Report

Created: 2026-06-15 06:25

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
431
static void rest_context_dctor(EbPtr p) {
27
431
    EbThreadContext*      thread_ctx = (EbThreadContext*)p;
28
431
    EntropyCodingContext* obj        = (EntropyCodingContext*)thread_ctx->priv;
29
431
    EB_FREE_ARRAY(obj);
30
431
}
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
431
                                                int index) {
37
431
    EntropyCodingContext* context_ptr;
38
431
    EB_CALLOC_ARRAY(context_ptr, 1);
39
431
    thread_ctx->priv  = context_ptr;
40
431
    thread_ctx->dctor = rest_context_dctor;
41
42
431
    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
431
    memset(context_ptr->levels_buf + LEVELS_TAIL_OFFSET, 0, TX_PAD_2D - LEVELS_TAIL_OFFSET);
47
48
    // Input/Output System Resource Manager FIFOs
49
431
    context_ptr->enc_dec_input_fifo_ptr = svt_system_resource_get_consumer_fifo(
50
431
        enc_handle_ptr->rest_results_resource_ptr, index);
51
431
    context_ptr->entropy_coding_output_fifo_ptr = svt_system_resource_get_producer_fifo(
52
431
        enc_handle_ptr->entropy_coding_results_resource_ptr, index);
53
54
431
    return EB_ErrorNone;
55
431
}
56
57
/***********************************************
58
 * Entropy Coding Reset Neighbor Arrays
59
 ***********************************************/
60
4.65k
static void entropy_coding_reset_neighbor_arrays(PictureControlSet* pcs, uint16_t tile_idx) {
61
4.65k
    svt_aom_neighbor_array_unit_reset(pcs->partition_context_na[tile_idx]);
62
63
4.65k
    svt_aom_neighbor_array_unit_reset(pcs->luma_dc_sign_level_coeff_na[tile_idx]);
64
4.65k
    svt_aom_neighbor_array_unit_reset(pcs->cb_dc_sign_level_coeff_na[tile_idx]);
65
4.65k
    svt_aom_neighbor_array_unit_reset(pcs->cr_dc_sign_level_coeff_na[tile_idx]);
66
4.65k
    svt_aom_neighbor_array_unit_reset(pcs->txfm_context_array[tile_idx]);
67
4.65k
}
68
69
/**************************************************
70
 * Reset Entropy Coding Picture
71
 **************************************************/
72
431
static void reset_entropy_coding_picture(EntropyCodingContext* ctx, PictureControlSet* pcs, SequenceControlSet* scs) {
73
431
    PictureParentControlSet* ppcs     = pcs->ppcs;
74
431
    const uint16_t           tile_cnt = ppcs->av1_cm->tiles_info.tile_rows * ppcs->av1_cm->tiles_info.tile_cols;
75
431
    ctx->is_16bit                     = scs->static_config.encoder_bit_depth > EB_EIGHT_BIT;
76
431
    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
431
    const uint32_t entropy_coding_qp = frm_hdr->quantization_params.base_q_idx;
79
80
5.08k
    for (uint16_t tile_idx = 0; tile_idx < tile_cnt; tile_idx++) {
81
4.65k
        ppcs->prev_qindex[tile_idx] = entropy_coding_qp;
82
4.65k
    }
83
431
    if (frm_hdr->allow_intrabc) {
84
0
        assert(frm_hdr->delta_lf_params.delta_lf_present == 0);
85
0
    }
86
431
    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.08k
    for (uint16_t tile_idx = 0; tile_idx < tile_cnt; tile_idx++) {
97
4.65k
        EntropyCoder*        ec                   = pcs->ec_info[tile_idx]->ec;
98
4.65k
        OutputBitstreamUnit* output_bitstream_ptr = ec->ec_output_bitstream_ptr;
99
        //****************************************************************//
100
4.65k
        ec->ec_writer.allow_update_cdf = !ppcs->large_scale_tile && !frm_hdr->disable_cdf_update;
101
4.65k
        aom_start_encode(&ec->ec_writer, output_bitstream_ptr);
102
        // ADD Reset here
103
4.65k
        const uint8_t primary_ref_frame = frm_hdr->primary_ref_frame;
104
4.65k
        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.65k
        } else {
112
4.65k
            svt_aom_reset_entropy_coder(scs->enc_ctx, ec, entropy_coding_qp, pcs->slice_type);
113
4.65k
        }
114
115
4.65k
        entropy_coding_reset_neighbor_arrays(pcs, tile_idx);
116
4.65k
    }
117
431
}
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.08k
EbErrorType svt_aom_entropy_coding_kernel_iter(void* context) {
140
5.08k
    EntropyCodingContext* context_ptr = (EntropyCodingContext*)context;
141
142
    // Input
143
5.08k
    EbObjectWrapper* rest_results_wrapper;
144
145
    // Output
146
5.08k
    EbObjectWrapper* entropy_coding_results_wrapper_ptr;
147
148
    // Get Mode Decision Results
149
5.08k
    EB_GET_FULL_OBJECT(context_ptr->enc_dec_input_fifo_ptr, &rest_results_wrapper);
150
151
4.65k
    RestResults*        rest_results = (RestResults*)rest_results_wrapper->object_ptr;
152
4.65k
    PictureControlSet*  pcs          = (PictureControlSet*)rest_results->pcs_wrapper->object_ptr;
153
4.65k
    SequenceControlSet* scs          = pcs->scs;
154
    // SB Constants
155
156
4.65k
    uint32_t sb_size = scs->sb_size;
157
158
4.65k
    uint8_t          sb_size_log2    = (uint8_t)svt_log2f(sb_size);
159
4.65k
    uint32_t         pic_width_in_sb = (pcs->ppcs->aligned_width + sb_size - 1) >> sb_size_log2;
160
4.65k
    uint16_t         tile_idx        = rest_results->tile_index;
161
4.65k
    Av1Common* const cm              = pcs->ppcs->av1_cm;
162
4.65k
    const uint16_t   tile_cnt        = cm->tiles_info.tile_rows * cm->tiles_info.tile_cols;
163
4.65k
    const uint16_t   tile_col        = tile_idx % cm->tiles_info.tile_cols;
164
4.65k
    const uint16_t   tile_row        = tile_idx / cm->tiles_info.tile_cols;
165
4.65k
    const uint16_t   tile_sb_start_x = cm->tiles_info.tile_col_start_mi[tile_col] >> scs->seq_header.sb_size_log2;
166
4.65k
    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.65k
    uint16_t tile_width_in_sb = (cm->tiles_info.tile_col_start_mi[tile_col + 1] -
169
4.65k
                                 cm->tiles_info.tile_col_start_mi[tile_col]) >>
170
4.65k
        scs->seq_header.sb_size_log2;
171
4.65k
    uint16_t tile_height_in_sb = (cm->tiles_info.tile_row_start_mi[tile_row + 1] -
172
4.65k
                                  cm->tiles_info.tile_row_start_mi[tile_row]) >>
173
4.65k
        scs->seq_header.sb_size_log2;
174
175
4.65k
    bool frame_entropy_done = false;
176
177
4.65k
    svt_block_on_mutex(pcs->entropy_coding_pic_mutex);
178
4.65k
    if (pcs->entropy_coding_pic_reset_flag) {
179
431
        pcs->entropy_coding_pic_reset_flag = false;
180
181
431
        reset_entropy_coding_picture(context_ptr, pcs, scs);
182
431
    }
183
4.65k
    svt_release_mutex(pcs->entropy_coding_pic_mutex);
184
185
4.65k
    if (!svt_aom_is_pic_skipped(pcs->ppcs)) {
186
4.65k
#if OPT_STATS_MUTEX
187
4.65k
        context_ptr->tot_qindex = 0;
188
4.65k
        context_ptr->valid_area = 0;
189
4.65k
#endif
190
9.61k
        for (uint32_t y_sb_index = 0; y_sb_index < tile_height_in_sb; ++y_sb_index) {
191
10.6k
            for (uint32_t x_sb_index = 0; x_sb_index < tile_width_in_sb; ++x_sb_index) {
192
5.73k
                uint16_t    sb_index = (uint16_t)((x_sb_index + tile_sb_start_x) +
193
5.73k
                                               (y_sb_index + tile_sb_start_y) * pic_width_in_sb);
194
5.73k
                SuperBlock* sb_ptr   = pcs->sb_ptr_array[sb_index];
195
196
5.73k
                const uint32_t sb_origin_x = (x_sb_index + tile_sb_start_x) << sb_size_log2;
197
5.73k
                const uint32_t sb_origin_y = (y_sb_index + tile_sb_start_y) << sb_size_log2;
198
5.73k
                if (x_sb_index == 0 && y_sb_index == 0) {
199
4.65k
                    svt_av1_reset_loop_restoration(context_ptr);
200
4.65k
                    context_ptr->tok = pcs->tile_tok[tile_row][tile_col];
201
4.65k
                }
202
203
5.73k
                EbPictureBufferDesc* coeff_picture_ptr = pcs->ppcs->enc_dec_ptr->quantized_coeff[sb_index];
204
5.73k
                context_ptr->coded_area_sb             = 0;
205
5.73k
                context_ptr->coded_area_sb_uv          = 0;
206
                // Ensure EC buffer has room for worst-case SB output (4 bytes/pixel)
207
5.73k
                EbErrorType ret = svt_aom_ec_ensure_capacity(&pcs->ec_info[tile_idx]->ec->ec_writer,
208
5.73k
                                                             sb_size * sb_size * 4);
209
5.73k
                if (ret != EB_ErrorNone) {
210
0
                    return ret;
211
0
                }
212
5.73k
                svt_aom_write_modes_sb(context_ptr,
213
5.73k
                                       sb_ptr,
214
5.73k
                                       pcs,
215
5.73k
                                       tile_idx,
216
5.73k
                                       pcs->ec_info[tile_idx]->ec,
217
5.73k
                                       coeff_picture_ptr,
218
5.73k
                                       sb_ptr->ptree,
219
5.73k
                                       sb_origin_y >> MI_SIZE_LOG2,
220
5.73k
                                       sb_origin_x >> MI_SIZE_LOG2);
221
5.73k
            }
222
4.96k
        }
223
4.65k
    }
224
4.65k
    bool pic_ready = true;
225
226
    // Current tile ready
227
4.65k
    svt_aom_encode_slice_finish(pcs->ec_info[tile_idx]->ec);
228
229
4.65k
    svt_block_on_mutex(pcs->entropy_coding_pic_mutex);
230
4.65k
#if OPT_STATS_MUTEX
231
    // Flush locally-accumulated qindex stats (avoids per-block mutex)
232
4.65k
    pcs->ppcs->tot_qindex += context_ptr->tot_qindex;
233
4.65k
    pcs->ppcs->valid_qindex_area += context_ptr->valid_area;
234
4.65k
#endif
235
4.65k
    pcs->ec_info[tile_idx]->entropy_coding_tile_done = true;
236
36.4k
    for (uint16_t i = 0; i < tile_cnt; i++) {
237
36.0k
        if (pcs->ec_info[i]->entropy_coding_tile_done == false) {
238
4.21k
            pic_ready = false;
239
4.21k
            break;
240
4.21k
        }
241
36.0k
    }
242
4.65k
    svt_release_mutex(pcs->entropy_coding_pic_mutex);
243
4.65k
    if (pic_ready) {
244
431
        if (pcs->ppcs->superres_total_recode_loop == 0) {
245
            // Release the reference Pictures from both lists
246
3.44k
            for (REF_FRAME_MINUS1 ref = LAST; ref < ALT + 1; ref++) {
247
3.01k
                const uint8_t list_idx = get_list_idx(ref + 1);
248
3.01k
                const uint8_t ref_idx  = get_ref_frame_idx(ref + 1);
249
3.01k
                if (pcs->ref_pic_ptr_array[list_idx][ref_idx] != NULL) {
250
0
                    svt_release_object(pcs->ref_pic_ptr_array[list_idx][ref_idx]);
251
0
                }
252
3.01k
            }
253
254
            //free palette data
255
431
            if (pcs->tile_tok[0][0]) {
256
0
                EB_FREE_ARRAY(pcs->tile_tok[0][0]);
257
0
            }
258
431
        }
259
431
        frame_entropy_done = true;
260
431
    }
261
262
4.65k
    if (frame_entropy_done) {
263
431
        if (pcs->ppcs->valid_qindex_area) {
264
431
            pcs->ppcs->avg_qp = ((pcs->ppcs->tot_qindex / pcs->ppcs->valid_qindex_area) + 2) >> 2;
265
431
        }
266
        // Get Empty Entropy Coding Results
267
431
        svt_get_empty_object(context_ptr->entropy_coding_output_fifo_ptr, &entropy_coding_results_wrapper_ptr);
268
431
        EntropyCodingResults* entropy_coding_results_ptr = (EntropyCodingResults*)
269
431
                                                               entropy_coding_results_wrapper_ptr->object_ptr;
270
431
        entropy_coding_results_ptr->pcs_wrapper = rest_results->pcs_wrapper;
271
272
        // Post EntropyCoding Results
273
431
        svt_post_full_object(entropy_coding_results_wrapper_ptr);
274
431
    }
275
276
    // Release Mode Decision Results
277
4.65k
    svt_release_object(rest_results_wrapper);
278
4.65k
    return EB_ErrorNone;
279
4.65k
}
280
281
431
void* svt_aom_entropy_coding_kernel(void* input_ptr) {
282
431
    EbThreadContext* thread_ctx = (EbThreadContext*)input_ptr;
283
5.08k
    for (;;) {
284
5.08k
        EbErrorType err = svt_aom_entropy_coding_kernel_iter(thread_ctx->priv);
285
5.08k
        if (err == EB_NoErrorFifoShutdown) {
286
431
            return NULL;
287
431
        }
288
5.08k
    }
289
0
    return NULL;
290
431
}