FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/evc_ps.c
Date: 2023-10-02 11:06:47
Exec Total Coverage
Lines: 85 267 31.8%
Functions: 3 6 50.0%
Branches: 29 142 20.4%

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
183 4 sps->pic_width_in_luma_samples = get_ue_golomb_long(gb);
184 4 sps->pic_height_in_luma_samples = get_ue_golomb_long(gb);
185
186 4 sps->bit_depth_luma_minus8 = get_ue_golomb_31(gb);
187 4 sps->bit_depth_chroma_minus8 = get_ue_golomb_31(gb);
188
189 4 sps->sps_btt_flag = get_bits1(gb);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_btt_flag) {
191 sps->log2_ctu_size_minus2 = get_ue_golomb_long(gb);
192 sps->log2_min_cb_size_minus2 = get_ue_golomb_long(gb);
193 sps->log2_diff_ctu_max_14_cb_size = get_ue_golomb_long(gb);
194 sps->log2_diff_ctu_max_tt_cb_size = get_ue_golomb_long(gb);
195 sps->log2_diff_min_cb_min_tt_cb_size_minus2 = get_ue_golomb_long(gb);
196 }
197
198 4 sps->sps_suco_flag = get_bits1(gb);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_suco_flag) {
200 sps->log2_diff_ctu_size_max_suco_cb_size = get_ue_golomb_long(gb);
201 sps->log2_diff_max_suco_min_suco_cb_size = get_ue_golomb_long(gb);
202 }
203
204 4 sps->sps_admvp_flag = get_bits1(gb);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_admvp_flag) {
206 sps->sps_affine_flag = get_bits1(gb);
207 sps->sps_amvr_flag = get_bits1(gb);
208 sps->sps_dmvr_flag = get_bits1(gb);
209 sps->sps_mmvd_flag = get_bits1(gb);
210 sps->sps_hmvp_flag = get_bits1(gb);
211 }
212
213 4 sps->sps_eipd_flag = get_bits1(gb);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_eipd_flag) {
215 sps->sps_ibc_flag = get_bits1(gb);
216 if (sps->sps_ibc_flag)
217 sps->log2_max_ibc_cand_size_minus2 = get_ue_golomb(gb);
218 }
219
220 4 sps->sps_cm_init_flag = get_bits1(gb);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_cm_init_flag)
222 sps->sps_adcc_flag = get_bits1(gb);
223
224 4 sps->sps_iqt_flag = get_bits1(gb);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_iqt_flag)
226 sps->sps_ats_flag = get_bits1(gb);
227
228 4 sps->sps_addb_flag = get_bits1(gb);
229 4 sps->sps_alf_flag = get_bits1(gb);
230 4 sps->sps_htdf_flag = get_bits1(gb);
231 4 sps->sps_rpl_flag = get_bits1(gb);
232 4 sps->sps_pocs_flag = get_bits1(gb);
233 4 sps->sps_dquant_flag = get_bits1(gb);
234 4 sps->sps_dra_flag = get_bits1(gb);
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->sps_pocs_flag) {
237 sps->log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb(gb);
238 if (sps->log2_max_pic_order_cnt_lsb_minus4 > 12U) {
239 ret = AVERROR_INVALIDDATA;
240 goto fail;
241 }
242 }
243
244
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) {
245 4 sps->log2_sub_gop_length = get_ue_golomb(gb);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->log2_sub_gop_length > 5U) {
247 ret = AVERROR_INVALIDDATA;
248 goto fail;
249 }
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->log2_sub_gop_length == 0)
251 sps->log2_ref_pic_gap_length = get_ue_golomb(gb);
252 }
253
254
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!sps->sps_rpl_flag)
255 4 sps->max_num_tid0_ref_pics = get_ue_golomb_31(gb);
256 else {
257 sps->sps_max_dec_pic_buffering_minus1 = get_ue_golomb_long(gb);
258 if ((unsigned)sps->sps_max_dec_pic_buffering_minus1 > 16 - 1) {
259 ret = AVERROR_INVALIDDATA;
260 goto fail;
261 }
262 sps->long_term_ref_pic_flag = get_bits1(gb);
263 sps->rpl1_same_as_rpl0_flag = get_bits1(gb);
264 sps->num_ref_pic_list_in_sps[0] = get_ue_golomb(gb);
265
266 if ((unsigned)sps->num_ref_pic_list_in_sps[0] >= EVC_MAX_NUM_RPLS) {
267 ret = AVERROR_INVALIDDATA;
268 goto fail;
269 }
270
271 for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i) {
272 ret = ref_pic_list_struct(sps, gb, &sps->rpls[0][i]);
273 if (ret < 0)
274 goto fail;
275 }
276
277 if (!sps->rpl1_same_as_rpl0_flag) {
278 sps->num_ref_pic_list_in_sps[1] = get_ue_golomb(gb);
279 if ((unsigned)sps->num_ref_pic_list_in_sps[1] >= EVC_MAX_NUM_RPLS) {
280 ret = AVERROR_INVALIDDATA;
281 goto fail;
282 }
283 for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i) {
284 ret = ref_pic_list_struct(sps, gb, &sps->rpls[1][i]);
285 if (ret < 0)
286 goto fail;
287 }
288 }
289 }
290
291 4 sps->picture_cropping_flag = get_bits1(gb);
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->picture_cropping_flag) {
294 sps->picture_crop_left_offset = get_ue_golomb_long(gb);
295 sps->picture_crop_right_offset = get_ue_golomb_long(gb);
296 sps->picture_crop_top_offset = get_ue_golomb_long(gb);
297 sps->picture_crop_bottom_offset = get_ue_golomb_long(gb);
298 }
299
300
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (sps->chroma_format_idc != 0) {
301 4 sps->chroma_qp_table_struct.chroma_qp_table_present_flag = get_bits1(gb);
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->chroma_qp_table_struct.chroma_qp_table_present_flag) {
304 sps->chroma_qp_table_struct.same_qp_table_for_chroma = get_bits1(gb);
305 sps->chroma_qp_table_struct.global_offset_flag = get_bits1(gb);
306 for (int i = 0; i < (sps->chroma_qp_table_struct.same_qp_table_for_chroma ? 1 : 2); i++) {
307 sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i] = get_ue_golomb(gb);
308 if (sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i] >= EVC_MAX_QP_TABLE_SIZE) {
309 ret = AVERROR_INVALIDDATA;
310 goto fail;
311 }
312 for (int j = 0; j <= sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i]; j++) {
313 sps->chroma_qp_table_struct.delta_qp_in_val_minus1[i][j] = get_bits(gb, 6);
314 sps->chroma_qp_table_struct.delta_qp_out_val[i][j] = get_se_golomb_long(gb);
315 }
316 }
317 }
318 }
319
320 4 sps->vui_parameters_present_flag = get_bits1(gb);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (sps->vui_parameters_present_flag) {
322 ret = vui_parameters(gb, &(sps->vui_parameters));
323 if (ret < 0)
324 goto fail;
325 }
326
327 // @note
328 // If necessary, add the missing fields to the EVCParserSPS structure
329 // and then extend parser implementation
330
331 4 av_freep(&ps->sps[sps_seq_parameter_set_id]);
332 4 ps->sps[sps_seq_parameter_set_id] = sps;
333
334 4 return 0;
335 fail:
336 av_free(sps);
337 return ret;
338 }
339
340 // @see ISO_IEC_23094-1 (7.3.2.2 SPS RBSP syntax)
341 //
342 // @note
343 // The current implementation of parse_sps function doesn't handle VUI parameters parsing.
344 // If it will be needed, parse_sps function could be extended to handle VUI parameters parsing
345 // to initialize fields of the AVCodecContex i.e. color_primaries, color_trc,color_range
346 //
347 4 int ff_evc_parse_pps(GetBitContext *gb, EVCParamSets *ps)
348 {
349 EVCParserPPS *pps;
350 unsigned pps_pic_parameter_set_id;
351 int ret;
352
353 4 pps_pic_parameter_set_id = get_ue_golomb(gb);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps_pic_parameter_set_id >= EVC_MAX_PPS_COUNT)
355 return AVERROR_INVALIDDATA;
356
357 4 pps = av_mallocz(sizeof(*pps));
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pps)
359 return AVERROR(ENOMEM);
360
361 4 pps->pps_pic_parameter_set_id = pps_pic_parameter_set_id;
362
363 4 pps->pps_seq_parameter_set_id = get_ue_golomb(gb);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->pps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT) {
365 ret = AVERROR_INVALIDDATA;
366 goto fail;
367 }
368
369 4 pps->num_ref_idx_default_active_minus1[0] = get_ue_golomb(gb);
370 4 pps->num_ref_idx_default_active_minus1[1] = get_ue_golomb(gb);
371 4 pps->additional_lt_poc_lsb_len = get_ue_golomb(gb);
372 4 pps->rpl1_idx_present_flag = get_bits1(gb);
373 4 pps->single_tile_in_pic_flag = get_bits1(gb);
374
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pps->single_tile_in_pic_flag) {
376 pps->num_tile_columns_minus1 = get_ue_golomb(gb);
377 pps->num_tile_rows_minus1 = get_ue_golomb(gb);
378 if (pps->num_tile_columns_minus1 >= EVC_MAX_TILE_COLUMNS ||
379 pps->num_tile_rows_minus1 >= EVC_MAX_TILE_ROWS) {
380 ret = AVERROR_INVALIDDATA;
381 goto fail;
382 }
383 pps->uniform_tile_spacing_flag = get_bits1(gb);
384
385 if (!pps->uniform_tile_spacing_flag) {
386 for (int i = 0; i < pps->num_tile_columns_minus1; i++)
387 pps->tile_column_width_minus1[i] = get_ue_golomb(gb);
388
389 for (int i = 0; i < pps->num_tile_rows_minus1; i++)
390 pps->tile_row_height_minus1[i] = get_ue_golomb(gb);
391 }
392 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
393 pps->tile_offset_len_minus1 = get_ue_golomb(gb);
394 }
395
396 4 pps->tile_id_len_minus1 = get_ue_golomb(gb);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->tile_id_len_minus1 > 15U) {
398 ret = AVERROR_INVALIDDATA;
399 goto fail;
400 }
401 4 pps->explicit_tile_id_flag = get_bits1(gb);
402
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->explicit_tile_id_flag) {
404 for (int i = 0; i <= pps->num_tile_rows_minus1; i++) {
405 for (int j = 0; j <= pps->num_tile_columns_minus1; j++)
406 pps->tile_id_val[i][j] = get_bits(gb, pps->tile_id_len_minus1 + 1);
407 }
408 }
409
410 4 pps->pic_dra_enabled_flag = 0;
411 4 pps->pic_dra_enabled_flag = get_bits1(gb);
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->pic_dra_enabled_flag)
414 pps->pic_dra_aps_id = get_bits(gb, 5);
415
416 4 pps->arbitrary_slice_present_flag = get_bits1(gb);
417 4 pps->constrained_intra_pred_flag = get_bits1(gb);
418 4 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
419
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (pps->cu_qp_delta_enabled_flag)
421 pps->log2_cu_qp_delta_area_minus6 = get_ue_golomb(gb);
422
423 4 av_freep(&ps->pps[pps_pic_parameter_set_id]);
424 4 ps->pps[pps_pic_parameter_set_id] = pps;
425
426 4 return 0;
427 fail:
428 av_free(pps);
429 return ret;
430 }
431
432 3 void ff_evc_ps_free(EVCParamSets *ps) {
433
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < EVC_MAX_SPS_COUNT; i++)
434 48 av_freep(&ps->sps[i]);
435
436
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
195 for (int i = 0; i < EVC_MAX_PPS_COUNT; i++)
437 192 av_freep(&ps->pps[i]);
438 3 }
439