FFmpeg coverage


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