FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/evc_ps.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 86 270 31.9%
Functions: 3 6 50.0%
Branches: 30 144 20.8%

Line Branch Exec 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 "libavutil/mem.h"
20 #include "get_bits.h"
21 #include "golomb.h"
22 #include "evc.h"
23 #include "evc_ps.h"
24
25 #define EXTENDED_SAR 255
26
27 // @see ISO_IEC_23094-1 (7.3.7 Reference picture list structure syntax)
28 static int ref_pic_list_struct(const EVCParserSPS *sps, GetBitContext *gb, RefPicListStruct *rpl)
29 {
30 uint32_t delta_poc_st, strp_entry_sign_flag = 0;
31 rpl->ref_pic_num = get_ue_golomb_long(gb);
32
33 if ((unsigned)rpl->ref_pic_num > sps->sps_max_dec_pic_buffering_minus1)
34 return AVERROR_INVALIDDATA;
35
36 if (rpl->ref_pic_num > 0) {
37 delta_poc_st = get_ue_golomb_long(gb);
38
39 rpl->ref_pics[0] = delta_poc_st;
40 if (rpl->ref_pics[0] != 0) {
41 strp_entry_sign_flag = get_bits(gb, 1);
42
43 rpl->ref_pics[0] *= 1 - (strp_entry_sign_flag << 1);
44 }
45 }
46
47 for (int i = 1; i < rpl->ref_pic_num; ++i) {
48 delta_poc_st = get_ue_golomb_long(gb);
49 if (delta_poc_st != 0)
50 strp_entry_sign_flag = get_bits(gb, 1);
51 rpl->ref_pics[i] = rpl->ref_pics[i - 1] + delta_poc_st * (1 - (strp_entry_sign_flag << 1));
52 }
53
54 return 0;
55 }
56
57 // @see ISO_IEC_23094-1 (E.2.2 HRD parameters syntax)
58 static int hrd_parameters(GetBitContext *gb, HRDParameters *hrd)
59 {
60 hrd->cpb_cnt_minus1 = get_ue_golomb_31(gb);
61 if (hrd->cpb_cnt_minus1 >= FF_ARRAY_ELEMS(hrd->cpb_size_value_minus1))
62 return AVERROR_INVALIDDATA;
63
64 hrd->bit_rate_scale = get_bits(gb, 4);
65 hrd->cpb_size_scale = get_bits(gb, 4);
66 for (int SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
67 hrd->bit_rate_value_minus1[SchedSelIdx] = get_ue_golomb_long(gb);
68 hrd->cpb_size_value_minus1[SchedSelIdx] = get_ue_golomb_long(gb);
69 hrd->cbr_flag[SchedSelIdx] = get_bits(gb, 1);
70 }
71 hrd->initial_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
72 hrd->cpb_removal_delay_length_minus1 = get_bits(gb, 5);
73 hrd->cpb_removal_delay_length_minus1 = get_bits(gb, 5);
74 hrd->time_offset_length = get_bits(gb, 5);
75
76 return 0;
77 }
78
79 // @see ISO_IEC_23094-1 (E.2.1 VUI parameters syntax)
80 static int vui_parameters(GetBitContext *gb, VUIParameters *vui)
81 {
82 int ret;
83
84 vui->aspect_ratio_info_present_flag = get_bits(gb, 1);
85 if (vui->aspect_ratio_info_present_flag) {
86 vui->aspect_ratio_idc = get_bits(gb, 8);
87 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
88 vui->sar_width = get_bits(gb, 16);
89 vui->sar_height = get_bits(gb, 16);
90 }
91 }
92 vui->overscan_info_present_flag = get_bits(gb, 1);
93 if (vui->overscan_info_present_flag)
94 vui->overscan_appropriate_flag = get_bits(gb, 1);
95 vui->video_signal_type_present_flag = get_bits(gb, 1);
96 if (vui->video_signal_type_present_flag) {
97 vui->video_format = get_bits(gb, 3);
98 vui->video_full_range_flag = get_bits(gb, 1);
99 vui->colour_description_present_flag = get_bits(gb, 1);
100 if (vui->colour_description_present_flag) {
101 vui->colour_primaries = get_bits(gb, 8);
102 vui->transfer_characteristics = get_bits(gb, 8);
103 vui->matrix_coefficients = get_bits(gb, 8);
104 }
105 }
106 vui->chroma_loc_info_present_flag = get_bits(gb, 1);
107 if (vui->chroma_loc_info_present_flag) {
108 vui->chroma_sample_loc_type_top_field = get_ue_golomb_31(gb);
109 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_31(gb);
110 }
111 vui->neutral_chroma_indication_flag = get_bits(gb, 1);
112
113 vui->field_seq_flag = get_bits(gb, 1);
114
115 vui->timing_info_present_flag = get_bits(gb, 1);
116 if (vui->timing_info_present_flag) {
117 vui->num_units_in_tick = get_bits_long(gb, 32);
118 vui->time_scale = get_bits_long(gb, 32);
119 vui->fixed_pic_rate_flag = get_bits(gb, 1);
120 }
121 vui->nal_hrd_parameters_present_flag = get_bits(gb, 1);
122 if (vui->nal_hrd_parameters_present_flag) {
123 ret = hrd_parameters(gb, &vui->hrd_parameters);
124 if (ret < 0)
125 return ret;
126 }
127
128 vui->vcl_hrd_parameters_present_flag = get_bits(gb, 1);
129 if (vui->vcl_hrd_parameters_present_flag) {
130 ret = hrd_parameters(gb, &vui->hrd_parameters);
131 if (ret < 0)
132 return ret;
133 }
134 if (vui->nal_hrd_parameters_present_flag || vui->vcl_hrd_parameters_present_flag)
135 vui->low_delay_hrd_flag = get_bits(gb, 1);
136 vui->pic_struct_present_flag = get_bits(gb, 1);
137 vui->bitstream_restriction_flag = get_bits(gb, 1);
138 if (vui->bitstream_restriction_flag) {
139 vui->motion_vectors_over_pic_boundaries_flag = get_bits(gb, 1);
140 vui->max_bytes_per_pic_denom = get_ue_golomb_31(gb);
141 vui->max_bits_per_mb_denom = get_ue_golomb_31(gb);
142 vui->log2_max_mv_length_horizontal = get_ue_golomb_31(gb);
143 vui->log2_max_mv_length_vertical = get_ue_golomb_31(gb);
144 vui->num_reorder_pics = get_ue_golomb_long(gb);
145 vui->max_dec_pic_buffering = get_ue_golomb_long(gb);
146 }
147
148 return 0;
149 }
150
151 // @see ISO_IEC_23094-1 (7.3.2.1 SPS RBSP syntax)
152 4 int ff_evc_parse_sps(GetBitContext *gb, EVCParamSets *ps)
153 {
154 EVCParserSPS *sps;
155 unsigned sps_seq_parameter_set_id;
156 int ret;
157
158 4 sps_seq_parameter_set_id = get_ue_golomb(gb);
159
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
161 return AVERROR_INVALIDDATA;
162
163 4 sps = av_mallocz(sizeof(*sps));
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!sps)
165 return AVERROR(ENOMEM);
166
167 4 sps->sps_seq_parameter_set_id = sps_seq_parameter_set_id;
168
169 // the Baseline profile is indicated by profile_idc eqal to 0
170 // the Main profile is indicated by profile_idc eqal to 1
171 4 sps->profile_idc = get_bits(gb, 8);
172
173 4 sps->level_idc = get_bits(gb, 8);
174
175 4 skip_bits_long(gb, 32); /* skip toolset_idc_h */
176 4 skip_bits_long(gb, 32); /* skip toolset_idc_l */
177
178 // 0 - monochrome
179 // 1 - 4:2:0
180 // 2 - 4:2:2
181 // 3 - 4:4:4
182 4 sps->chroma_format_idc = get_ue_golomb_31(gb);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->chroma_format_idc > 3) {
184 ret = AVERROR_INVALIDDATA;
185 goto fail;
186 }
187
188 4 sps->pic_width_in_luma_samples = get_ue_golomb_long(gb);
189 4 sps->pic_height_in_luma_samples = get_ue_golomb_long(gb);
190
191 4 sps->bit_depth_luma_minus8 = get_ue_golomb_31(gb);
192 4 sps->bit_depth_chroma_minus8 = get_ue_golomb_31(gb);
193
194 4 sps->sps_btt_flag = get_bits1(gb);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_btt_flag) {
196 sps->log2_ctu_size_minus2 = get_ue_golomb_long(gb);
197 sps->log2_min_cb_size_minus2 = get_ue_golomb_long(gb);
198 sps->log2_diff_ctu_max_14_cb_size = get_ue_golomb_long(gb);
199 sps->log2_diff_ctu_max_tt_cb_size = get_ue_golomb_long(gb);
200 sps->log2_diff_min_cb_min_tt_cb_size_minus2 = get_ue_golomb_long(gb);
201 }
202
203 4 sps->sps_suco_flag = get_bits1(gb);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_suco_flag) {
205 sps->log2_diff_ctu_size_max_suco_cb_size = get_ue_golomb_long(gb);
206 sps->log2_diff_max_suco_min_suco_cb_size = get_ue_golomb_long(gb);
207 }
208
209 4 sps->sps_admvp_flag = get_bits1(gb);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_admvp_flag) {
211 sps->sps_affine_flag = get_bits1(gb);
212 sps->sps_amvr_flag = get_bits1(gb);
213 sps->sps_dmvr_flag = get_bits1(gb);
214 sps->sps_mmvd_flag = get_bits1(gb);
215 sps->sps_hmvp_flag = get_bits1(gb);
216 }
217
218 4 sps->sps_eipd_flag = get_bits1(gb);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_eipd_flag) {
220 sps->sps_ibc_flag = get_bits1(gb);
221 if (sps->sps_ibc_flag)
222 sps->log2_max_ibc_cand_size_minus2 = get_ue_golomb(gb);
223 }
224
225 4 sps->sps_cm_init_flag = get_bits1(gb);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_cm_init_flag)
227 sps->sps_adcc_flag = get_bits1(gb);
228
229 4 sps->sps_iqt_flag = get_bits1(gb);
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_iqt_flag)
231 sps->sps_ats_flag = get_bits1(gb);
232
233 4 sps->sps_addb_flag = get_bits1(gb);
234 4 sps->sps_alf_flag = get_bits1(gb);
235 4 sps->sps_htdf_flag = get_bits1(gb);
236 4 sps->sps_rpl_flag = get_bits1(gb);
237 4 sps->sps_pocs_flag = get_bits1(gb);
238 4 sps->sps_dquant_flag = get_bits1(gb);
239 4 sps->sps_dra_flag = get_bits1(gb);
240
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_pocs_flag) {
242 sps->log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb(gb);
243 if (sps->log2_max_pic_order_cnt_lsb_minus4 > 12U) {
244 ret = AVERROR_INVALIDDATA;
245 goto fail;
246 }
247 }
248
249
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (!sps->sps_pocs_flag || !sps->sps_rpl_flag) {
250 4 sps->log2_sub_gop_length = get_ue_golomb(gb);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->log2_sub_gop_length > 5U) {
252 ret = AVERROR_INVALIDDATA;
253 goto fail;
254 }
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->log2_sub_gop_length == 0)
256 sps->log2_ref_pic_gap_length = get_ue_golomb(gb);
257 }
258
259
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!sps->sps_rpl_flag)
260 4 sps->max_num_tid0_ref_pics = get_ue_golomb_31(gb);
261 else {
262 sps->sps_max_dec_pic_buffering_minus1 = get_ue_golomb_long(gb);
263 if ((unsigned)sps->sps_max_dec_pic_buffering_minus1 > 16 - 1) {
264 ret = AVERROR_INVALIDDATA;
265 goto fail;
266 }
267 sps->long_term_ref_pic_flag = get_bits1(gb);
268 sps->rpl1_same_as_rpl0_flag = get_bits1(gb);
269 sps->num_ref_pic_list_in_sps[0] = get_ue_golomb(gb);
270
271 if ((unsigned)sps->num_ref_pic_list_in_sps[0] >= EVC_MAX_NUM_RPLS) {
272 ret = AVERROR_INVALIDDATA;
273 goto fail;
274 }
275
276 for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i) {
277 ret = ref_pic_list_struct(sps, gb, &sps->rpls[0][i]);
278 if (ret < 0)
279 goto fail;
280 }
281
282 if (!sps->rpl1_same_as_rpl0_flag) {
283 sps->num_ref_pic_list_in_sps[1] = get_ue_golomb(gb);
284 if ((unsigned)sps->num_ref_pic_list_in_sps[1] >= EVC_MAX_NUM_RPLS) {
285 ret = AVERROR_INVALIDDATA;
286 goto fail;
287 }
288 for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i) {
289 ret = ref_pic_list_struct(sps, gb, &sps->rpls[1][i]);
290 if (ret < 0)
291 goto fail;
292 }
293 }
294 }
295
296 4 sps->picture_cropping_flag = get_bits1(gb);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->picture_cropping_flag) {
299 sps->picture_crop_left_offset = get_ue_golomb_long(gb);
300 sps->picture_crop_right_offset = get_ue_golomb_long(gb);
301 sps->picture_crop_top_offset = get_ue_golomb_long(gb);
302 sps->picture_crop_bottom_offset = get_ue_golomb_long(gb);
303 }
304
305
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (sps->chroma_format_idc != 0) {
306 4 sps->chroma_qp_table_struct.chroma_qp_table_present_flag = get_bits1(gb);
307
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->chroma_qp_table_struct.chroma_qp_table_present_flag) {
309 sps->chroma_qp_table_struct.same_qp_table_for_chroma = get_bits1(gb);
310 sps->chroma_qp_table_struct.global_offset_flag = get_bits1(gb);
311 for (int i = 0; i < (sps->chroma_qp_table_struct.same_qp_table_for_chroma ? 1 : 2); i++) {
312 sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i] = get_ue_golomb(gb);
313 if (sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i] >= EVC_MAX_QP_TABLE_SIZE) {
314 ret = AVERROR_INVALIDDATA;
315 goto fail;
316 }
317 for (int j = 0; j <= sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i]; j++) {
318 sps->chroma_qp_table_struct.delta_qp_in_val_minus1[i][j] = get_bits(gb, 6);
319 sps->chroma_qp_table_struct.delta_qp_out_val[i][j] = get_se_golomb_long(gb);
320 }
321 }
322 }
323 }
324
325 4 sps->vui_parameters_present_flag = get_bits1(gb);
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->vui_parameters_present_flag) {
327 ret = vui_parameters(gb, &(sps->vui_parameters));
328 if (ret < 0)
329 goto fail;
330 }
331
332 // @note
333 // If necessary, add the missing fields to the EVCParserSPS structure
334 // and then extend parser implementation
335
336 4 av_freep(&ps->sps[sps_seq_parameter_set_id]);
337 4 ps->sps[sps_seq_parameter_set_id] = sps;
338
339 4 return 0;
340 fail:
341 av_free(sps);
342 return ret;
343 }
344
345 // @see ISO_IEC_23094-1 (7.3.2.2 SPS RBSP syntax)
346 //
347 // @note
348 // The current implementation of parse_sps function doesn't handle VUI parameters parsing.
349 // If it will be needed, parse_sps function could be extended to handle VUI parameters parsing
350 // to initialize fields of the AVCodecContex i.e. color_primaries, color_trc,color_range
351 //
352 4 int ff_evc_parse_pps(GetBitContext *gb, EVCParamSets *ps)
353 {
354 EVCParserPPS *pps;
355 unsigned pps_pic_parameter_set_id;
356 int ret;
357
358 4 pps_pic_parameter_set_id = get_ue_golomb(gb);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps_pic_parameter_set_id >= EVC_MAX_PPS_COUNT)
360 return AVERROR_INVALIDDATA;
361
362 4 pps = av_mallocz(sizeof(*pps));
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pps)
364 return AVERROR(ENOMEM);
365
366 4 pps->pps_pic_parameter_set_id = pps_pic_parameter_set_id;
367
368 4 pps->pps_seq_parameter_set_id = get_ue_golomb(gb);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->pps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT) {
370 ret = AVERROR_INVALIDDATA;
371 goto fail;
372 }
373
374 4 pps->num_ref_idx_default_active_minus1[0] = get_ue_golomb(gb);
375 4 pps->num_ref_idx_default_active_minus1[1] = get_ue_golomb(gb);
376 4 pps->additional_lt_poc_lsb_len = get_ue_golomb(gb);
377 4 pps->rpl1_idx_present_flag = get_bits1(gb);
378 4 pps->single_tile_in_pic_flag = get_bits1(gb);
379
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pps->single_tile_in_pic_flag) {
381 pps->num_tile_columns_minus1 = get_ue_golomb(gb);
382 pps->num_tile_rows_minus1 = get_ue_golomb(gb);
383 if (pps->num_tile_columns_minus1 >= EVC_MAX_TILE_COLUMNS ||
384 pps->num_tile_rows_minus1 >= EVC_MAX_TILE_ROWS) {
385 ret = AVERROR_INVALIDDATA;
386 goto fail;
387 }
388 pps->uniform_tile_spacing_flag = get_bits1(gb);
389
390 if (!pps->uniform_tile_spacing_flag) {
391 for (int i = 0; i < pps->num_tile_columns_minus1; i++)
392 pps->tile_column_width_minus1[i] = get_ue_golomb(gb);
393
394 for (int i = 0; i < pps->num_tile_rows_minus1; i++)
395 pps->tile_row_height_minus1[i] = get_ue_golomb(gb);
396 }
397 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
398 pps->tile_offset_len_minus1 = get_ue_golomb(gb);
399 }
400
401 4 pps->tile_id_len_minus1 = get_ue_golomb(gb);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->tile_id_len_minus1 > 15U) {
403 ret = AVERROR_INVALIDDATA;
404 goto fail;
405 }
406 4 pps->explicit_tile_id_flag = get_bits1(gb);
407
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->explicit_tile_id_flag) {
409 for (int i = 0; i <= pps->num_tile_rows_minus1; i++) {
410 for (int j = 0; j <= pps->num_tile_columns_minus1; j++)
411 pps->tile_id_val[i][j] = get_bits(gb, pps->tile_id_len_minus1 + 1);
412 }
413 }
414
415 4 pps->pic_dra_enabled_flag = 0;
416 4 pps->pic_dra_enabled_flag = get_bits1(gb);
417
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->pic_dra_enabled_flag)
419 pps->pic_dra_aps_id = get_bits(gb, 5);
420
421 4 pps->arbitrary_slice_present_flag = get_bits1(gb);
422 4 pps->constrained_intra_pred_flag = get_bits1(gb);
423 4 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
424
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->cu_qp_delta_enabled_flag)
426 pps->log2_cu_qp_delta_area_minus6 = get_ue_golomb(gb);
427
428 4 av_freep(&ps->pps[pps_pic_parameter_set_id]);
429 4 ps->pps[pps_pic_parameter_set_id] = pps;
430
431 4 return 0;
432 fail:
433 av_free(pps);
434 return ret;
435 }
436
437 3 void ff_evc_ps_free(EVCParamSets *ps) {
438
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < EVC_MAX_SPS_COUNT; i++)
439 48 av_freep(&ps->sps[i]);
440
441
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
195 for (int i = 0; i < EVC_MAX_PPS_COUNT; i++)
442 192 av_freep(&ps->pps[i]);
443 3 }
444