Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/evc_parse.c
Line
Count
Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include "golomb.h"
20
#include "evc.h"
21
#include "evc_parse.h"
22
23
// @see ISO_IEC_23094-1 (7.3.2.6 Slice layer RBSP syntax)
24
int ff_evc_parse_slice_header(GetBitContext *gb, EVCParserSliceHeader *sh,
25
                              const EVCParamSets *ps, enum EVCNALUnitType nalu_type)
26
179k
{
27
179k
    const EVCParserPPS *pps;
28
179k
    const EVCParserSPS *sps;
29
179k
    int num_tiles_in_slice = 0;
30
179k
    unsigned slice_pic_parameter_set_id;
31
32
179k
    slice_pic_parameter_set_id = get_ue_golomb_31(gb);
33
34
179k
    if (slice_pic_parameter_set_id >= EVC_MAX_PPS_COUNT)
35
0
        return AVERROR_INVALIDDATA;
36
37
179k
    pps = ps->pps[slice_pic_parameter_set_id];
38
179k
    if(!pps)
39
18.8k
        return AVERROR_INVALIDDATA;
40
41
160k
    sps = ps->sps[pps->pps_seq_parameter_set_id];
42
160k
    if(!sps)
43
1.53k
        return AVERROR_INVALIDDATA;
44
45
159k
    memset(sh, 0, sizeof(*sh));
46
159k
    sh->slice_pic_parameter_set_id = slice_pic_parameter_set_id;
47
48
159k
    if (!pps->single_tile_in_pic_flag) {
49
70.9k
        sh->single_tile_in_slice_flag = get_bits1(gb);
50
70.9k
        sh->first_tile_id = get_bits(gb, pps->tile_id_len_minus1 + 1);
51
70.9k
    } else
52
88.2k
        sh->single_tile_in_slice_flag = 1;
53
54
159k
    if (!sh->single_tile_in_slice_flag) {
55
54.8k
        if (pps->arbitrary_slice_present_flag)
56
35.7k
            sh->arbitrary_slice_flag = get_bits1(gb);
57
58
54.8k
        if (!sh->arbitrary_slice_flag)
59
36.6k
            sh->last_tile_id = get_bits(gb, pps->tile_id_len_minus1 + 1);
60
18.2k
        else {
61
18.2k
            unsigned num_remaining_tiles_in_slice_minus1 = get_ue_golomb_long(gb);
62
18.2k
            if (num_remaining_tiles_in_slice_minus1 > EVC_MAX_TILE_ROWS * EVC_MAX_TILE_COLUMNS - 2)
63
708
                return AVERROR_INVALIDDATA;
64
65
17.5k
            num_tiles_in_slice = num_remaining_tiles_in_slice_minus1 + 2;
66
17.5k
            sh->num_remaining_tiles_in_slice_minus1 = num_remaining_tiles_in_slice_minus1;
67
408k
            for (int i = 0; i < num_tiles_in_slice - 1; ++i)
68
390k
                sh->delta_tile_id_minus1[i] = get_ue_golomb_long(gb);
69
17.5k
        }
70
54.8k
    }
71
72
158k
    sh->slice_type = get_ue_golomb_31(gb);
73
74
158k
    if (nalu_type == EVC_IDR_NUT)
75
31.6k
        sh->no_output_of_prior_pics_flag = get_bits1(gb);
76
77
158k
    if (sps->sps_mmvd_flag && ((sh->slice_type == EVC_SLICE_TYPE_B) || (sh->slice_type == EVC_SLICE_TYPE_P)))
78
31.8k
        sh->mmvd_group_enable_flag = get_bits1(gb);
79
126k
    else
80
126k
        sh->mmvd_group_enable_flag = 0;
81
82
158k
    if (sps->sps_alf_flag) {
83
123k
        int ChromaArrayType = sps->chroma_format_idc;
84
85
123k
        sh->slice_alf_enabled_flag = get_bits1(gb);
86
87
123k
        if (sh->slice_alf_enabled_flag) {
88
41.7k
            sh->slice_alf_luma_aps_id = get_bits(gb, 5);
89
41.7k
            sh->slice_alf_map_flag = get_bits1(gb);
90
41.7k
            sh->slice_alf_chroma_idc = get_bits(gb, 2);
91
92
41.7k
            if ((ChromaArrayType == 1 || ChromaArrayType == 2) && sh->slice_alf_chroma_idc > 0)
93
6.37k
                sh->slice_alf_chroma_aps_id =  get_bits(gb, 5);
94
41.7k
        }
95
123k
        if (ChromaArrayType == 3) {
96
28.2k
            int sliceChromaAlfEnabledFlag = 0;
97
28.2k
            int sliceChroma2AlfEnabledFlag = 0;
98
99
28.2k
            if (sh->slice_alf_chroma_idc == 1) { // @see ISO_IEC_23094-1 (7.4.5)
100
2.83k
                sliceChromaAlfEnabledFlag = 1;
101
2.83k
                sliceChroma2AlfEnabledFlag = 0;
102
25.3k
            } else if (sh->slice_alf_chroma_idc == 2) {
103
2.40k
                sliceChromaAlfEnabledFlag = 0;
104
2.40k
                sliceChroma2AlfEnabledFlag = 1;
105
22.9k
            } else if (sh->slice_alf_chroma_idc == 3) {
106
4.68k
                sliceChromaAlfEnabledFlag = 1;
107
4.68k
                sliceChroma2AlfEnabledFlag = 1;
108
18.2k
            } else {
109
18.2k
                sliceChromaAlfEnabledFlag = 0;
110
18.2k
                sliceChroma2AlfEnabledFlag = 0;
111
18.2k
            }
112
113
28.2k
            if (!sh->slice_alf_enabled_flag)
114
14.4k
                sh->slice_alf_chroma_idc = get_bits(gb, 2);
115
116
28.2k
            if (sliceChromaAlfEnabledFlag) {
117
7.52k
                sh->slice_alf_chroma_aps_id = get_bits(gb, 5);
118
7.52k
                sh->slice_alf_chroma_map_flag = get_bits1(gb);
119
7.52k
            }
120
121
28.2k
            if (sliceChroma2AlfEnabledFlag) {
122
7.09k
                sh->slice_alf_chroma2_aps_id = get_bits(gb, 5);
123
7.09k
                sh->slice_alf_chroma2_map_flag = get_bits1(gb);
124
7.09k
            }
125
28.2k
        }
126
123k
    }
127
128
158k
    if (nalu_type != EVC_IDR_NUT) {
129
126k
        if (sps->sps_pocs_flag)
130
101k
            sh->slice_pic_order_cnt_lsb = get_bits(gb, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
131
126k
    }
132
133
    // @note
134
    // If necessary, add the missing fields to the EVCParserSliceHeader structure
135
    // and then extend parser implementation
136
137
158k
    return 0;
138
159k
}
139
140
int ff_evc_derive_poc(const EVCParamSets *ps, const EVCParserSliceHeader *sh,
141
                      EVCParserPoc *poc, enum EVCNALUnitType nalu_type, int tid)
142
158k
{
143
158k
    const EVCParserPPS *pps = ps->pps[sh->slice_pic_parameter_set_id];
144
158k
    const EVCParserSPS *sps;
145
146
158k
    if (!pps)
147
0
        return AVERROR_INVALIDDATA;
148
149
158k
    sps = ps->sps[pps->pps_seq_parameter_set_id];
150
158k
    if (!sps)
151
0
        return AVERROR_INVALIDDATA;
152
153
158k
    if (sps->sps_pocs_flag) {
154
123k
        int PicOrderCntMsb = 0;
155
123k
        poc->prevPicOrderCntVal = poc->PicOrderCntVal;
156
157
123k
        if (nalu_type == EVC_IDR_NUT)
158
22.4k
            PicOrderCntMsb = 0;
159
101k
        else {
160
101k
            int MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
161
101k
            int prevPicOrderCntLsb = poc->PicOrderCntVal & (MaxPicOrderCntLsb - 1);
162
101k
            int prevPicOrderCntMsb = poc->PicOrderCntVal - prevPicOrderCntLsb;
163
164
101k
            if ((sh->slice_pic_order_cnt_lsb < prevPicOrderCntLsb) &&
165
9.02k
                ((prevPicOrderCntLsb - sh->slice_pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)))
166
4.79k
                PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
167
96.3k
            else if ((sh->slice_pic_order_cnt_lsb > prevPicOrderCntLsb) &&
168
11.8k
                     ((sh->slice_pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)))
169
5.84k
                PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
170
90.5k
            else
171
90.5k
                PicOrderCntMsb = prevPicOrderCntMsb;
172
101k
        }
173
123k
        poc->PicOrderCntVal = PicOrderCntMsb + sh->slice_pic_order_cnt_lsb;
174
123k
    } else {
175
34.7k
        if (nalu_type == EVC_IDR_NUT) {
176
9.22k
            poc->PicOrderCntVal = 0;
177
9.22k
            poc->DocOffset = -1;
178
25.5k
        } else {
179
25.5k
            int SubGopLength = 1 << sps->log2_sub_gop_length;
180
181
25.5k
            if (tid > (SubGopLength > 1 ? 1 + av_log2(SubGopLength - 1) : 0))
182
4.70k
                return AVERROR_INVALIDDATA;
183
184
20.8k
            if (tid == 0) {
185
10.6k
                poc->PicOrderCntVal = poc->prevPicOrderCntVal + SubGopLength;
186
10.6k
                poc->DocOffset = 0;
187
10.6k
                poc->prevPicOrderCntVal = poc->PicOrderCntVal;
188
10.6k
            } else {
189
10.1k
                int ExpectedTemporalId;
190
10.1k
                int PocOffset;
191
10.1k
                int prevDocOffset = poc->DocOffset;
192
193
10.1k
                poc->DocOffset = (prevDocOffset + 1) % SubGopLength;
194
10.1k
                if (poc->DocOffset == 0) {
195
2.03k
                    poc->prevPicOrderCntVal += SubGopLength;
196
2.03k
                    ExpectedTemporalId = 0;
197
2.03k
                } else
198
8.11k
                    ExpectedTemporalId = 1 + av_log2(poc->DocOffset);
199
200
53.8k
                while (tid != ExpectedTemporalId) {
201
43.7k
                    poc->DocOffset = (poc->DocOffset + 1) % SubGopLength;
202
43.7k
                    if (poc->DocOffset == 0)
203
3.57k
                        ExpectedTemporalId = 0;
204
40.1k
                    else
205
40.1k
                        ExpectedTemporalId = 1 + av_log2(poc->DocOffset);
206
43.7k
                }
207
10.1k
                PocOffset = (int)(SubGopLength * ((2.0 * poc->DocOffset + 1) / (1 << tid) - 2));
208
10.1k
                poc->PicOrderCntVal = poc->prevPicOrderCntVal + PocOffset;
209
10.1k
            }
210
20.8k
        }
211
34.7k
    }
212
213
153k
    return 0;
214
158k
}