Coverage Report

Created: 2026-07-30 06:27

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