blob: d45f703e1cff48416d53c9c4b276f1781f577599 [file] [log] [blame]
Dawid Kozinski34e4f182023-06-15 11:46:431/*
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 "parser.h"
21#include "evc.h"
22#include "evc_parse.h"
23
24#define EXTENDED_SAR 255
25
26#define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
27
28static const enum AVPixelFormat pix_fmts_8bit[NUM_CHROMA_FORMATS] = {
29 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
30};
31
32static const enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS] = {
33 AV_PIX_FMT_GRAY9, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9
34};
35
36static const enum AVPixelFormat pix_fmts_10bit[NUM_CHROMA_FORMATS] = {
37 AV_PIX_FMT_GRAY10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10
38};
39
40static const enum AVPixelFormat pix_fmts_12bit[NUM_CHROMA_FORMATS] = {
41 AV_PIX_FMT_GRAY12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12
42};
43
44static const enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS] = {
45 AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14
46};
47
48static const enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS] = {
49 AV_PIX_FMT_GRAY16, AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16
50};
51
52// nuh_temporal_id specifies a temporal identifier for the NAL unit
53int ff_evc_get_temporal_id(const uint8_t *bits, int bits_size, void *logctx)
54{
55 int temporal_id = 0;
56 uint16_t t = 0;
57
58 if (bits_size < EVC_NALU_HEADER_SIZE) {
59 av_log(logctx, AV_LOG_ERROR, "Can't read NAL unit header\n");
60 return 0;
61 }
62
63 // forbidden_zero_bit
64 if ((bits[0] & 0x80) != 0)
65 return -1;
66
67 t = AV_RB16(bits);
68
69 temporal_id = (t >> 6) & 0x0007;
70
71 return temporal_id;
72}
73
74// @see ISO_IEC_23094-1 (7.3.7 Reference picture list structure syntax)
75static int ref_pic_list_struct(GetBitContext *gb, RefPicListStruct *rpl)
76{
77 uint32_t delta_poc_st, strp_entry_sign_flag = 0;
78 rpl->ref_pic_num = get_ue_golomb(gb);
79 if (rpl->ref_pic_num > 0) {
80 delta_poc_st = get_ue_golomb(gb);
81
82 rpl->ref_pics[0] = delta_poc_st;
83 if (rpl->ref_pics[0] != 0) {
84 strp_entry_sign_flag = get_bits(gb, 1);
85
86 rpl->ref_pics[0] *= 1 - (strp_entry_sign_flag << 1);
87 }
88 }
89
90 for (int i = 1; i < rpl->ref_pic_num; ++i) {
91 delta_poc_st = get_ue_golomb(gb);
92 if (delta_poc_st != 0)
93 strp_entry_sign_flag = get_bits(gb, 1);
94 rpl->ref_pics[i] = rpl->ref_pics[i - 1] + delta_poc_st * (1 - (strp_entry_sign_flag << 1));
95 }
96
97 return 0;
98}
99
100// @see ISO_IEC_23094-1 (E.2.2 HRD parameters syntax)
101static int hrd_parameters(GetBitContext *gb, HRDParameters *hrd)
102{
103 hrd->cpb_cnt_minus1 = get_ue_golomb(gb);
104 hrd->bit_rate_scale = get_bits(gb, 4);
105 hrd->cpb_size_scale = get_bits(gb, 4);
106 for (int SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
107 hrd->bit_rate_value_minus1[SchedSelIdx] = get_ue_golomb(gb);
108 hrd->cpb_size_value_minus1[SchedSelIdx] = get_ue_golomb(gb);
109 hrd->cbr_flag[SchedSelIdx] = get_bits(gb, 1);
110 }
111 hrd->initial_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
112 hrd->cpb_removal_delay_length_minus1 = get_bits(gb, 5);
113 hrd->cpb_removal_delay_length_minus1 = get_bits(gb, 5);
114 hrd->time_offset_length = get_bits(gb, 5);
115
116 return 0;
117}
118
119// @see ISO_IEC_23094-1 (E.2.1 VUI parameters syntax)
120static int vui_parameters(GetBitContext *gb, VUIParameters *vui)
121{
122 vui->aspect_ratio_info_present_flag = get_bits(gb, 1);
123 if (vui->aspect_ratio_info_present_flag) {
124 vui->aspect_ratio_idc = get_bits(gb, 8);
125 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
126 vui->sar_width = get_bits(gb, 16);
127 vui->sar_height = get_bits(gb, 16);
128 }
129 }
130 vui->overscan_info_present_flag = get_bits(gb, 1);
131 if (vui->overscan_info_present_flag)
132 vui->overscan_appropriate_flag = get_bits(gb, 1);
133 vui->video_signal_type_present_flag = get_bits(gb, 1);
134 if (vui->video_signal_type_present_flag) {
135 vui->video_format = get_bits(gb, 3);
136 vui->video_full_range_flag = get_bits(gb, 1);
137 vui->colour_description_present_flag = get_bits(gb, 1);
138 if (vui->colour_description_present_flag) {
139 vui->colour_primaries = get_bits(gb, 8);
140 vui->transfer_characteristics = get_bits(gb, 8);
141 vui->matrix_coefficients = get_bits(gb, 8);
142 }
143 }
144 vui->chroma_loc_info_present_flag = get_bits(gb, 1);
145 if (vui->chroma_loc_info_present_flag) {
146 vui->chroma_sample_loc_type_top_field = get_ue_golomb(gb);
147 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb(gb);
148 }
149 vui->neutral_chroma_indication_flag = get_bits(gb, 1);
150
151 vui->field_seq_flag = get_bits(gb, 1);
152
153 vui->timing_info_present_flag = get_bits(gb, 1);
154 if (vui->timing_info_present_flag) {
155 vui->num_units_in_tick = get_bits(gb, 32);
156 vui->time_scale = get_bits(gb, 32);
157 vui->fixed_pic_rate_flag = get_bits(gb, 1);
158 }
159 vui->nal_hrd_parameters_present_flag = get_bits(gb, 1);
160 if (vui->nal_hrd_parameters_present_flag)
161 hrd_parameters(gb, &vui->hrd_parameters);
162 vui->vcl_hrd_parameters_present_flag = get_bits(gb, 1);
163 if (vui->vcl_hrd_parameters_present_flag)
164 hrd_parameters(gb, &vui->hrd_parameters);
165 if (vui->nal_hrd_parameters_present_flag || vui->vcl_hrd_parameters_present_flag)
166 vui->low_delay_hrd_flag = get_bits(gb, 1);
167 vui->pic_struct_present_flag = get_bits(gb, 1);
168 vui->bitstream_restriction_flag = get_bits(gb, 1);
169 if (vui->bitstream_restriction_flag) {
170 vui->motion_vectors_over_pic_boundaries_flag = get_bits(gb, 1);
171 vui->max_bytes_per_pic_denom = get_ue_golomb(gb);
172 vui->max_bits_per_mb_denom = get_ue_golomb(gb);
173 vui->log2_max_mv_length_horizontal = get_ue_golomb(gb);
174 vui->log2_max_mv_length_vertical = get_ue_golomb(gb);
175 vui->num_reorder_pics = get_ue_golomb(gb);
176 vui->max_dec_pic_buffering = get_ue_golomb(gb);
177 }
178
179 return 0;
180}
181
182// @see ISO_IEC_23094-1 (7.3.2.1 SPS RBSP syntax)
183EVCParserSPS *ff_evc_parse_sps(EVCParserContext *ctx, const uint8_t *bs, int bs_size)
184{
185 GetBitContext gb;
186 EVCParserSPS *sps;
187 int sps_seq_parameter_set_id;
188
189 if (init_get_bits8(&gb, bs, bs_size) < 0)
190 return NULL;
191
192 sps_seq_parameter_set_id = get_ue_golomb(&gb);
193
194 if (sps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
195 return NULL;
196
197 if(!ctx->sps[sps_seq_parameter_set_id]) {
198 if((ctx->sps[sps_seq_parameter_set_id] = av_malloc(sizeof(EVCParserSPS))) == NULL)
199 return NULL;
200 }
201
202 sps = ctx->sps[sps_seq_parameter_set_id];
James Almer57879b22023-06-15 13:33:25203 memset(sps, 0, sizeof(*sps));
204
Dawid Kozinski34e4f182023-06-15 11:46:43205 sps->sps_seq_parameter_set_id = sps_seq_parameter_set_id;
206
207 // the Baseline profile is indicated by profile_idc eqal to 0
208 // the Main profile is indicated by profile_idc eqal to 1
209 sps->profile_idc = get_bits(&gb, 8);
210
211 sps->level_idc = get_bits(&gb, 8);
212
213 skip_bits_long(&gb, 32); /* skip toolset_idc_h */
214 skip_bits_long(&gb, 32); /* skip toolset_idc_l */
215
216 // 0 - monochrome
217 // 1 - 4:2:0
218 // 2 - 4:2:2
219 // 3 - 4:4:4
220 sps->chroma_format_idc = get_ue_golomb(&gb);
221
222 sps->pic_width_in_luma_samples = get_ue_golomb(&gb);
223 sps->pic_height_in_luma_samples = get_ue_golomb(&gb);
224
225 sps->bit_depth_luma_minus8 = get_ue_golomb(&gb);
226 sps->bit_depth_chroma_minus8 = get_ue_golomb(&gb);
227
228 sps->sps_btt_flag = get_bits(&gb, 1);
229 if (sps->sps_btt_flag) {
230 sps->log2_ctu_size_minus5 = get_ue_golomb(&gb);
231 sps->log2_min_cb_size_minus2 = get_ue_golomb(&gb);
232 sps->log2_diff_ctu_max_14_cb_size = get_ue_golomb(&gb);
233 sps->log2_diff_ctu_max_tt_cb_size = get_ue_golomb(&gb);
234 sps->log2_diff_min_cb_min_tt_cb_size_minus2 = get_ue_golomb(&gb);
235 }
236
237 sps->sps_suco_flag = get_bits(&gb, 1);
238 if (sps->sps_suco_flag) {
239 sps->log2_diff_ctu_size_max_suco_cb_size = get_ue_golomb(&gb);
240 sps->log2_diff_max_suco_min_suco_cb_size = get_ue_golomb(&gb);
241 }
242
243 sps->sps_admvp_flag = get_bits(&gb, 1);
244 if (sps->sps_admvp_flag) {
245 sps->sps_affine_flag = get_bits(&gb, 1);
246 sps->sps_amvr_flag = get_bits(&gb, 1);
247 sps->sps_dmvr_flag = get_bits(&gb, 1);
248 sps->sps_mmvd_flag = get_bits(&gb, 1);
249 sps->sps_hmvp_flag = get_bits(&gb, 1);
250 }
251
252 sps->sps_eipd_flag = get_bits(&gb, 1);
253 if (sps->sps_eipd_flag) {
254 sps->sps_ibc_flag = get_bits(&gb, 1);
255 if (sps->sps_ibc_flag)
256 sps->log2_max_ibc_cand_size_minus2 = get_ue_golomb(&gb);
257 }
258
259 sps->sps_cm_init_flag = get_bits(&gb, 1);
260 if (sps->sps_cm_init_flag)
261 sps->sps_adcc_flag = get_bits(&gb, 1);
262
263 sps->sps_iqt_flag = get_bits(&gb, 1);
264 if (sps->sps_iqt_flag)
265 sps->sps_ats_flag = get_bits(&gb, 1);
266
267 sps->sps_addb_flag = get_bits(&gb, 1);
268 sps->sps_alf_flag = get_bits(&gb, 1);
269 sps->sps_htdf_flag = get_bits(&gb, 1);
270 sps->sps_rpl_flag = get_bits(&gb, 1);
271 sps->sps_pocs_flag = get_bits(&gb, 1);
272 sps->sps_dquant_flag = get_bits(&gb, 1);
273 sps->sps_dra_flag = get_bits(&gb, 1);
274
275 if (sps->sps_pocs_flag)
276 sps->log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb(&gb);
277
278 if (!sps->sps_pocs_flag || !sps->sps_rpl_flag) {
279 sps->log2_sub_gop_length = get_ue_golomb(&gb);
280 if (sps->log2_sub_gop_length == 0)
281 sps->log2_ref_pic_gap_length = get_ue_golomb(&gb);
282 }
283
284 if (!sps->sps_rpl_flag)
285 sps->max_num_tid0_ref_pics = get_ue_golomb(&gb);
286 else {
287 sps->sps_max_dec_pic_buffering_minus1 = get_ue_golomb(&gb);
288 sps->long_term_ref_pic_flag = get_bits(&gb, 1);
289 sps->rpl1_same_as_rpl0_flag = get_bits(&gb, 1);
290 sps->num_ref_pic_list_in_sps[0] = get_ue_golomb(&gb);
291
292 for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i)
293 ref_pic_list_struct(&gb, &sps->rpls[0][i]);
294
295 if (!sps->rpl1_same_as_rpl0_flag) {
296 sps->num_ref_pic_list_in_sps[1] = get_ue_golomb(&gb);
297 for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i)
298 ref_pic_list_struct(&gb, &sps->rpls[1][i]);
299 }
300 }
301
302 sps->picture_cropping_flag = get_bits(&gb, 1);
303
304 if (sps->picture_cropping_flag) {
305 sps->picture_crop_left_offset = get_ue_golomb(&gb);
306 sps->picture_crop_right_offset = get_ue_golomb(&gb);
307 sps->picture_crop_top_offset = get_ue_golomb(&gb);
308 sps->picture_crop_bottom_offset = get_ue_golomb(&gb);
309 }
310
311 if (sps->chroma_format_idc != 0) {
312 sps->chroma_qp_table_struct.chroma_qp_table_present_flag = get_bits(&gb, 1);
313
314 if (sps->chroma_qp_table_struct.chroma_qp_table_present_flag) {
315 sps->chroma_qp_table_struct.same_qp_table_for_chroma = get_bits(&gb, 1);
316 sps->chroma_qp_table_struct.global_offset_flag = get_bits(&gb, 1);
317 for (int i = 0; i < (sps->chroma_qp_table_struct.same_qp_table_for_chroma ? 1 : 2); i++) {
318 sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i] = get_ue_golomb(&gb);;
319 for (int j = 0; j <= sps->chroma_qp_table_struct.num_points_in_qp_table_minus1[i]; j++) {
320 sps->chroma_qp_table_struct.delta_qp_in_val_minus1[i][j] = get_bits(&gb, 6);
321 sps->chroma_qp_table_struct.delta_qp_out_val[i][j] = get_se_golomb(&gb);
322 }
323 }
324 }
325 }
326
327 sps->vui_parameters_present_flag = get_bits(&gb, 1);
328 if (sps->vui_parameters_present_flag)
329 vui_parameters(&gb, &(sps->vui_parameters));
330
331 // @note
332 // If necessary, add the missing fields to the EVCParserSPS structure
333 // and then extend parser implementation
334
335 return sps;
336}
337
338// @see ISO_IEC_23094-1 (7.3.2.2 SPS RBSP syntax)
339//
340// @note
341// The current implementation of parse_sps function doesn't handle VUI parameters parsing.
342// If it will be needed, parse_sps function could be extended to handle VUI parameters parsing
343// to initialize fields of the AVCodecContex i.e. color_primaries, color_trc,color_range
344//
345EVCParserPPS *ff_evc_parse_pps(EVCParserContext *ctx, const uint8_t *bs, int bs_size)
346{
347 GetBitContext gb;
348 EVCParserPPS *pps;
349
350 int pps_pic_parameter_set_id;
351
352 if (init_get_bits8(&gb, bs, bs_size) < 0)
353 return NULL;
354
355 pps_pic_parameter_set_id = get_ue_golomb(&gb);
356 if (pps_pic_parameter_set_id > EVC_MAX_PPS_COUNT)
357 return NULL;
358
359 if(!ctx->pps[pps_pic_parameter_set_id]) {
James Almerf42df832023-06-15 13:35:23360 if ((ctx->pps[pps_pic_parameter_set_id] = av_malloc(sizeof(EVCParserPPS))) == NULL)
Dawid Kozinski34e4f182023-06-15 11:46:43361 return NULL;
362 }
363
364 pps = ctx->pps[pps_pic_parameter_set_id];
James Almer57879b22023-06-15 13:33:25365 memset(pps, 0, sizeof(*pps));
Dawid Kozinski34e4f182023-06-15 11:46:43366
367 pps->pps_pic_parameter_set_id = pps_pic_parameter_set_id;
368
369 pps->pps_seq_parameter_set_id = get_ue_golomb(&gb);
370 if (pps->pps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
371 return NULL;
372
373 pps->num_ref_idx_default_active_minus1[0] = get_ue_golomb(&gb);
374 pps->num_ref_idx_default_active_minus1[1] = get_ue_golomb(&gb);
375 pps->additional_lt_poc_lsb_len = get_ue_golomb(&gb);
376 pps->rpl1_idx_present_flag = get_bits(&gb, 1);
377 pps->single_tile_in_pic_flag = get_bits(&gb, 1);
378
379 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 pps->uniform_tile_spacing_flag = get_bits(&gb, 1);
383
384 if (!pps->uniform_tile_spacing_flag) {
385 for (int i = 0; i < pps->num_tile_columns_minus1; i++)
386 pps->tile_column_width_minus1[i] = get_ue_golomb(&gb);
387
388 for (int i = 0; i < pps->num_tile_rows_minus1; i++)
389 pps->tile_row_height_minus1[i] = get_ue_golomb(&gb);
390 }
391 pps->loop_filter_across_tiles_enabled_flag = get_bits(&gb, 1);
392 pps->tile_offset_len_minus1 = get_ue_golomb(&gb);
393 }
394
395 pps->tile_id_len_minus1 = get_ue_golomb(&gb);
396 pps->explicit_tile_id_flag = get_bits(&gb, 1);
397
398 if (pps->explicit_tile_id_flag) {
399 for (int i = 0; i <= pps->num_tile_rows_minus1; i++) {
400 for (int j = 0; j <= pps->num_tile_columns_minus1; j++)
401 pps->tile_id_val[i][j] = get_bits(&gb, pps->tile_id_len_minus1 + 1);
402 }
403 }
404
405 pps->pic_dra_enabled_flag = 0;
406 pps->pic_dra_enabled_flag = get_bits(&gb, 1);
407
408 if (pps->pic_dra_enabled_flag)
409 pps->pic_dra_aps_id = get_bits(&gb, 5);
410
411 pps->arbitrary_slice_present_flag = get_bits(&gb, 1);
412 pps->constrained_intra_pred_flag = get_bits(&gb, 1);
413 pps->cu_qp_delta_enabled_flag = get_bits(&gb, 1);
414
415 if (pps->cu_qp_delta_enabled_flag)
416 pps->log2_cu_qp_delta_area_minus6 = get_ue_golomb(&gb);
417
418 return pps;
419}
420
421// @see ISO_IEC_23094-1 (7.3.2.6 Slice layer RBSP syntax)
James Almer44f26312023-06-15 14:03:51422static int evc_parse_slice_header(EVCParserContext *ctx, EVCParserSliceHeader *sh, const uint8_t *bs, int bs_size)
Dawid Kozinski34e4f182023-06-15 11:46:43423{
424 GetBitContext gb;
Dawid Kozinski34e4f182023-06-15 11:46:43425 EVCParserPPS *pps;
426 EVCParserSPS *sps;
427
428 int num_tiles_in_slice = 0;
429 int slice_pic_parameter_set_id;
James Almer44f26312023-06-15 14:03:51430 int ret;
Dawid Kozinski34e4f182023-06-15 11:46:43431
James Almer44f26312023-06-15 14:03:51432 if ((ret = init_get_bits8(&gb, bs, bs_size)) < 0)
433 return ret;
Dawid Kozinski34e4f182023-06-15 11:46:43434
435 slice_pic_parameter_set_id = get_ue_golomb(&gb);
436
437 if (slice_pic_parameter_set_id < 0 || slice_pic_parameter_set_id >= EVC_MAX_PPS_COUNT)
James Almer44f26312023-06-15 14:03:51438 return AVERROR_INVALIDDATA;
Dawid Kozinski34e4f182023-06-15 11:46:43439
440 pps = ctx->pps[slice_pic_parameter_set_id];
441 if(!pps)
James Almer44f26312023-06-15 14:03:51442 return AVERROR_INVALIDDATA;
Dawid Kozinski34e4f182023-06-15 11:46:43443
444 sps = ctx->sps[slice_pic_parameter_set_id];
445 if(!sps)
James Almer44f26312023-06-15 14:03:51446 return AVERROR_INVALIDDATA;
Dawid Kozinski34e4f182023-06-15 11:46:43447
James Almer44f26312023-06-15 14:03:51448 memset(sh, 0, sizeof(*sh));
Dawid Kozinski34e4f182023-06-15 11:46:43449 sh->slice_pic_parameter_set_id = slice_pic_parameter_set_id;
450
451 if (!pps->single_tile_in_pic_flag) {
452 sh->single_tile_in_slice_flag = get_bits(&gb, 1);
453 sh->first_tile_id = get_bits(&gb, pps->tile_id_len_minus1 + 1);
454 } else
455 sh->single_tile_in_slice_flag = 1;
456
457 if (!sh->single_tile_in_slice_flag) {
458 if (pps->arbitrary_slice_present_flag)
459 sh->arbitrary_slice_flag = get_bits(&gb, 1);
460
461 if (!sh->arbitrary_slice_flag)
462 sh->last_tile_id = get_bits(&gb, pps->tile_id_len_minus1 + 1);
463 else {
464 sh->num_remaining_tiles_in_slice_minus1 = get_ue_golomb(&gb);
465 num_tiles_in_slice = sh->num_remaining_tiles_in_slice_minus1 + 2;
466 for (int i = 0; i < num_tiles_in_slice - 1; ++i)
467 sh->delta_tile_id_minus1[i] = get_ue_golomb(&gb);
468 }
469 }
470
471 sh->slice_type = get_ue_golomb(&gb);
472
473 if (ctx->nalu_type == EVC_IDR_NUT)
474 sh->no_output_of_prior_pics_flag = get_bits(&gb, 1);
475
476 if (sps->sps_mmvd_flag && ((sh->slice_type == EVC_SLICE_TYPE_B) || (sh->slice_type == EVC_SLICE_TYPE_P)))
477 sh->mmvd_group_enable_flag = get_bits(&gb, 1);
478 else
479 sh->mmvd_group_enable_flag = 0;
480
481 if (sps->sps_alf_flag) {
482 int ChromaArrayType = sps->chroma_format_idc;
483
484 sh->slice_alf_enabled_flag = get_bits(&gb, 1);
485
486 if (sh->slice_alf_enabled_flag) {
487 sh->slice_alf_luma_aps_id = get_bits(&gb, 5);
488 sh->slice_alf_map_flag = get_bits(&gb, 1);
489 sh->slice_alf_chroma_idc = get_bits(&gb, 2);
490
491 if ((ChromaArrayType == 1 || ChromaArrayType == 2) && sh->slice_alf_chroma_idc > 0)
492 sh->slice_alf_chroma_aps_id = get_bits(&gb, 5);
493 }
494 if (ChromaArrayType == 3) {
495 int sliceChromaAlfEnabledFlag = 0;
496 int sliceChroma2AlfEnabledFlag = 0;
497
498 if (sh->slice_alf_chroma_idc == 1) { // @see ISO_IEC_23094-1 (7.4.5)
499 sliceChromaAlfEnabledFlag = 1;
500 sliceChroma2AlfEnabledFlag = 0;
501 } else if (sh->slice_alf_chroma_idc == 2) {
502 sliceChromaAlfEnabledFlag = 0;
503 sliceChroma2AlfEnabledFlag = 1;
504 } else if (sh->slice_alf_chroma_idc == 3) {
505 sliceChromaAlfEnabledFlag = 1;
506 sliceChroma2AlfEnabledFlag = 1;
507 } else {
508 sliceChromaAlfEnabledFlag = 0;
509 sliceChroma2AlfEnabledFlag = 0;
510 }
511
512 if (!sh->slice_alf_enabled_flag)
513 sh->slice_alf_chroma_idc = get_bits(&gb, 2);
514
515 if (sliceChromaAlfEnabledFlag) {
516 sh->slice_alf_chroma_aps_id = get_bits(&gb, 5);
517 sh->slice_alf_chroma_map_flag = get_bits(&gb, 1);
518 }
519
520 if (sliceChroma2AlfEnabledFlag) {
521 sh->slice_alf_chroma2_aps_id = get_bits(&gb, 5);
522 sh->slice_alf_chroma2_map_flag = get_bits(&gb, 1);
523 }
524 }
525 }
526
527 if (ctx->nalu_type != EVC_IDR_NUT) {
528 if (sps->sps_pocs_flag)
529 sh->slice_pic_order_cnt_lsb = get_bits(&gb, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
530 }
531
532 // @note
533 // If necessary, add the missing fields to the EVCParserSliceHeader structure
534 // and then extend parser implementation
535
James Almer44f26312023-06-15 14:03:51536 return 0;
Dawid Kozinski34e4f182023-06-15 11:46:43537}
538
539int ff_evc_parse_nal_unit(EVCParserContext *ctx, const uint8_t *buf, int buf_size, void *logctx)
540{
541 int nalu_type, nalu_size;
542 int tid;
543 const uint8_t *data = buf;
544 int data_size = buf_size;
545
546 // ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
547 ctx->key_frame = -1;
548
549 nalu_size = buf_size;
550 if (nalu_size <= 0) {
551 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", nalu_size);
552 return AVERROR_INVALIDDATA;
553 }
554
555 // @see ISO_IEC_23094-1_2020, 7.4.2.2 NAL unit header semantic (Table 4 - NAL unit type codes and NAL unit type classes)
556 // @see enum EVCNALUnitType in evc.h
557 nalu_type = evc_get_nalu_type(data, data_size, logctx);
558 if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
559 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type);
560 return AVERROR_INVALIDDATA;
561 }
562 ctx->nalu_type = nalu_type;
563
564 tid = ff_evc_get_temporal_id(data, data_size, logctx);
565 if (tid < 0) {
566 av_log(logctx, AV_LOG_ERROR, "Invalid temporial id: (%d)\n", tid);
567 return AVERROR_INVALIDDATA;
568 }
569 ctx->nuh_temporal_id = tid;
570
571 data += EVC_NALU_HEADER_SIZE;
572 data_size -= EVC_NALU_HEADER_SIZE;
573
574 switch(nalu_type) {
575 case EVC_SPS_NUT: {
576 EVCParserSPS *sps;
577 int SubGopLength;
578 int bit_depth;
579
580 sps = ff_evc_parse_sps(ctx, data, nalu_size);
581 if (!sps) {
582 av_log(logctx, AV_LOG_ERROR, "SPS parsing error\n");
583 return AVERROR_INVALIDDATA;
584 }
585
586 ctx->coded_width = sps->pic_width_in_luma_samples;
587 ctx->coded_height = sps->pic_height_in_luma_samples;
588
589 if(sps->picture_cropping_flag) {
590 ctx->width = sps->pic_width_in_luma_samples - sps->picture_crop_left_offset - sps->picture_crop_right_offset;
591 ctx->height = sps->pic_height_in_luma_samples - sps->picture_crop_top_offset - sps->picture_crop_bottom_offset;
592 } else {
593 ctx->width = sps->pic_width_in_luma_samples;
594 ctx->height = sps->pic_height_in_luma_samples;
595 }
596
597 SubGopLength = (int)pow(2.0, sps->log2_sub_gop_length);
598 ctx->gop_size = SubGopLength;
599
600 ctx->delay = (sps->sps_max_dec_pic_buffering_minus1) ? sps->sps_max_dec_pic_buffering_minus1 - 1 : SubGopLength + sps->max_num_tid0_ref_pics - 1;
601
602 if (sps->profile_idc == 1) ctx->profile = FF_PROFILE_EVC_MAIN;
603 else ctx->profile = FF_PROFILE_EVC_BASELINE;
604
James Almerda2af702023-06-15 13:10:14605 if (sps->vui_parameters_present_flag && sps->vui_parameters.timing_info_present_flag) {
606 int64_t num = sps->vui_parameters.num_units_in_tick;
607 int64_t den = sps->vui_parameters.time_scale;
608 if (num != 0 && den != 0)
609 av_reduce(&ctx->framerate.den, &ctx->framerate.num, num, den, 1 << 30);
610 } else
611 ctx->framerate = (AVRational) { 0, 1 };
Dawid Kozinski34e4f182023-06-15 11:46:43612
613 bit_depth = sps->bit_depth_chroma_minus8 + 8;
614 ctx->format = AV_PIX_FMT_NONE;
615
616 switch (bit_depth) {
617 case 8:
618 ctx->format = pix_fmts_8bit[sps->chroma_format_idc];
619 break;
620 case 9:
621 ctx->format = pix_fmts_9bit[sps->chroma_format_idc];
622 break;
623 case 10:
624 ctx->format = pix_fmts_10bit[sps->chroma_format_idc];
625 break;
626 case 12:
627 ctx->format = pix_fmts_12bit[sps->chroma_format_idc];
628 break;
629 case 14:
630 ctx->format = pix_fmts_14bit[sps->chroma_format_idc];
631 break;
632 case 16:
633 ctx->format = pix_fmts_16bit[sps->chroma_format_idc];
634 break;
635 }
636 av_assert0(ctx->format != AV_PIX_FMT_NONE);
637
638 break;
639 }
640 case EVC_PPS_NUT: {
641 EVCParserPPS *pps;
642
643 pps = ff_evc_parse_pps(ctx, data, nalu_size);
644 if (!pps) {
645 av_log(logctx, AV_LOG_ERROR, "PPS parsing error\n");
646 return AVERROR_INVALIDDATA;
647 }
648 break;
649 }
650 case EVC_SEI_NUT: // Supplemental Enhancement Information
651 case EVC_APS_NUT: // Adaptation parameter set
652 case EVC_FD_NUT: // Filler data
653 break;
654 case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture
655 case EVC_NOIDR_NUT: {
James Almer44f26312023-06-15 14:03:51656 EVCParserSliceHeader sh;
Dawid Kozinski34e4f182023-06-15 11:46:43657 EVCParserSPS *sps;
James Almer44f26312023-06-15 14:03:51658 int ret;
Dawid Kozinski34e4f182023-06-15 11:46:43659
James Almer44f26312023-06-15 14:03:51660 ret = evc_parse_slice_header(ctx, &sh, data, nalu_size);
661 if (ret < 0) {
Dawid Kozinski34e4f182023-06-15 11:46:43662 av_log(logctx, AV_LOG_ERROR, "Slice header parsing error\n");
James Almer44f26312023-06-15 14:03:51663 return ret;
Dawid Kozinski34e4f182023-06-15 11:46:43664 }
665
James Almer44f26312023-06-15 14:03:51666 switch (sh.slice_type) {
Dawid Kozinski34e4f182023-06-15 11:46:43667 case EVC_SLICE_TYPE_B: {
668 ctx->pict_type = AV_PICTURE_TYPE_B;
669 break;
670 }
671 case EVC_SLICE_TYPE_P: {
672 ctx->pict_type = AV_PICTURE_TYPE_P;
673 break;
674 }
675 case EVC_SLICE_TYPE_I: {
676 ctx->pict_type = AV_PICTURE_TYPE_I;
677 break;
678 }
679 default: {
680 ctx->pict_type = AV_PICTURE_TYPE_NONE;
681 }
682 }
683
684 ctx->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0;
685
686 // POC (picture order count of the current picture) derivation
687 // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
James Almer44f26312023-06-15 14:03:51688 sps = ctx->sps[sh.slice_pic_parameter_set_id];
Dawid Kozinski34e4f182023-06-15 11:46:43689
690 if (sps && sps->sps_pocs_flag) {
691
692 int PicOrderCntMsb = 0;
693 ctx->poc.prevPicOrderCntVal = ctx->poc.PicOrderCntVal;
694
695 if (nalu_type == EVC_IDR_NUT)
696 PicOrderCntMsb = 0;
697 else {
698 int MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
699
700 int prevPicOrderCntLsb = ctx->poc.PicOrderCntVal & (MaxPicOrderCntLsb - 1);
701 int prevPicOrderCntMsb = ctx->poc.PicOrderCntVal - prevPicOrderCntLsb;
702
703
James Almer44f26312023-06-15 14:03:51704 if ((sh.slice_pic_order_cnt_lsb < prevPicOrderCntLsb) &&
705 ((prevPicOrderCntLsb - sh.slice_pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)))
Dawid Kozinski34e4f182023-06-15 11:46:43706
707 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
708
James Almer44f26312023-06-15 14:03:51709 else if ((sh.slice_pic_order_cnt_lsb > prevPicOrderCntLsb) &&
710 ((sh.slice_pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)))
Dawid Kozinski34e4f182023-06-15 11:46:43711
712 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
713
714 else
715 PicOrderCntMsb = prevPicOrderCntMsb;
716 }
James Almer44f26312023-06-15 14:03:51717 ctx->poc.PicOrderCntVal = PicOrderCntMsb + sh.slice_pic_order_cnt_lsb;
Dawid Kozinski34e4f182023-06-15 11:46:43718
719 } else {
720 if (nalu_type == EVC_IDR_NUT) {
721 ctx->poc.PicOrderCntVal = 0;
722 ctx->poc.DocOffset = -1;
723 } else {
724 int SubGopLength = (int)pow(2.0, sps->log2_sub_gop_length);
725 if (tid == 0) {
726 ctx->poc.PicOrderCntVal = ctx->poc.prevPicOrderCntVal + SubGopLength;
727 ctx->poc.DocOffset = 0;
728 ctx->poc.prevPicOrderCntVal = ctx->poc.PicOrderCntVal;
729 } else {
730 int ExpectedTemporalId;
731 int PocOffset;
732 int prevDocOffset = ctx->poc.DocOffset;
733
734 ctx->poc.DocOffset = (prevDocOffset + 1) % SubGopLength;
735 if (ctx->poc.DocOffset == 0) {
736 ctx->poc.prevPicOrderCntVal += SubGopLength;
737 ExpectedTemporalId = 0;
738 } else
739 ExpectedTemporalId = 1 + (int)log2(ctx->poc.DocOffset);
740 while (tid != ExpectedTemporalId) {
741 ctx->poc.DocOffset = (ctx->poc.DocOffset + 1) % SubGopLength;
742 if (ctx->poc.DocOffset == 0)
743 ExpectedTemporalId = 0;
744 else
745 ExpectedTemporalId = 1 + (int)log2(ctx->poc.DocOffset);
746 }
747 PocOffset = (int)(SubGopLength * ((2.0 * ctx->poc.DocOffset + 1) / (int)pow(2.0, tid) - 2));
748 ctx->poc.PicOrderCntVal = ctx->poc.prevPicOrderCntVal + PocOffset;
749 }
750 }
751 }
752
753 ctx->output_picture_number = ctx->poc.PicOrderCntVal;
754 ctx->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0;
755
756 break;
757 }
758 }
759
760 return 0;
761}
762