FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_lcevc_syntax_template.c
Date: 2026-03-13 22:30:28
Exec Total Coverage
Lines: 0 352 0.0%
Functions: 0 38 0.0%
Branches: 0 465 0.0%

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 static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
20 {
21 int err;
22
23 // ISO/IEC 23094-2:2021/FDAM 1:2023(E) 7.3.12
24 while (byte_alignment(rw) != 0)
25 fixed(1, alignment_bit_equal_to_zero, 0);
26
27 return 0;
28 }
29
30 static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
31 {
32 int err;
33
34 fixed(1, rbsp_stop_one_bit, 1);
35 while (byte_alignment(rw) != 0)
36 fixed(1, rbsp_alignment_zero_bit, 0);
37
38 return 0;
39 }
40
41 static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
42 LCEVCRawNALUnitHeader *current,
43 uint32_t valid_type_mask)
44 {
45 int err;
46
47 fixed(1, forbidden_zero_bit, 0);
48 fixed(1, forbidden_one_bit, 1);
49 ub(5, nal_unit_type);
50
51 if (!(1 << current->nal_unit_type & valid_type_mask)) {
52 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
53 current->nal_unit_type);
54 return AVERROR_INVALIDDATA;
55 }
56
57 ub(9, reserved_flag);
58
59 return 0;
60 }
61
62 LCEVC_BLOCK_FUNC(global_config, (CodedBitstreamContext *ctx, RWContext *rw,
63 LCEVCRawGlobalConfig *current,
64 LCEVCProcessBlockState *state,
65 int nal_unit_type))
66 {
67 CodedBitstreamLCEVCContext *priv = ctx->priv_data;
68 int err;
69
70 HEADER("Global Config");
71
72 flag(processed_planes_type_flag);
73 ub(6, resolution_type);
74 ub(1, transform_type);
75 ub(2, chroma_sampling_type);
76 ub(2, base_depth_type);
77 ub(2, enhancement_depth_type);
78 flag(temporal_step_width_modifier_signalled_flag);
79 flag(predicted_residual_mode_flag);
80 flag(temporal_tile_intra_signalling_enabled_flag);
81 flag(temporal_enabled_flag);
82 ub(3, upsample_type);
83 flag(level1_filtering_signalled_flag);
84 ub(2, scaling_mode_level1);
85 ub(2, scaling_mode_level2);
86 ub(2, tile_dimensions_type);
87 ub(2, user_data_enabled);
88 flag(level1_depth_flag);
89 flag(chroma_step_width_flag);
90
91 if (current->processed_planes_type_flag) {
92 ub(4, planes_type);
93 ub(4, reserved_zeros_4bit);
94 } else
95 infer(planes_type, 0);
96
97 if (current->temporal_step_width_modifier_signalled_flag) {
98 ub(8, temporal_step_width_modifier);
99 }
100
101 if (current->upsample_type == 4) {
102 ub(16, upsampler_coeff1);
103 ub(16, upsampler_coeff2);
104 ub(16, upsampler_coeff3);
105 ub(16, upsampler_coeff4);
106 }
107
108 if (current->level1_filtering_signalled_flag) {
109 ub(4, level1_filtering_first_coefficient);
110 ub(4, level1_filtering_second_coefficient);
111 }
112
113 if (current->tile_dimensions_type > 0) {
114 if (current->tile_dimensions_type == 3) {
115 ub(16, custom_tile_width);
116 ub(16, custom_tile_height);
117 }
118 ub(5, reserved_zeros_5bit);
119 flag(compression_type_entropy_enabled_per_tile_flag);
120 ub(2, compression_type_size_per_tile);
121 }
122
123 if (current->resolution_type == 63) {
124 ub(16, custom_resolution_width);
125 ub(16, custom_resolution_height);
126 }
127 if (current->chroma_step_width_flag) {
128 ub(8, chroma_step_width_multiplier);
129 } else {
130 infer(chroma_step_width_multiplier, 64);
131 }
132
133 av_refstruct_replace(&priv->gc, current);
134
135 return 0;
136 }
137
138 LCEVC_BLOCK_FUNC(sequence_config, (CodedBitstreamContext *ctx, RWContext *rw,
139 LCEVCRawSequenceConfig *current,
140 LCEVCProcessBlockState *state,
141 int nal_unit_type))
142 {
143 CodedBitstreamLCEVCContext *priv = ctx->priv_data;
144 int err;
145
146 HEADER("Sequence Config");
147
148 ub(4, profile_idc);
149 ub(4, level_idc);
150 ub(2, sublevel_idc);
151 flag(conformance_window_flag);
152 ub(5, reserved_zeros_5bit);
153
154 if (current->profile_idc == 15 || current->level_idc == 15) {
155 ub(3, profile_idc);
156 ub(4, level_idc);
157 ub(1, reserved_zeros_1bit);
158 }
159 if (current->conformance_window_flag == 1) {
160 mb(conf_win_left_offset);
161 mb(conf_win_right_offset);
162 mb(conf_win_top_offset);
163 mb(conf_win_bottom_offset);
164 }
165
166 av_refstruct_replace(&priv->sc, current);
167
168 return 0;
169 }
170
171 LCEVC_BLOCK_FUNC(picture_config, (CodedBitstreamContext *ctx, RWContext *rw,
172 LCEVCRawPictureConfig *current,
173 LCEVCProcessBlockState *state,
174 int nal_unit_type))
175 {
176 CodedBitstreamLCEVCContext *priv = ctx->priv_data;
177 int nlayers, err;
178
179 HEADER("Picture Config");
180
181 if (!priv->gc)
182 return AVERROR_INVALIDDATA;
183
184 flag(no_enhancement_bit_flag);
185 if (current->no_enhancement_bit_flag == 0) {
186 ub(3, quant_matrix_mode);
187 flag(dequant_offset_signalled_flag);
188 flag(picture_type_bit_flag);
189 flag(temporal_refresh_bit_flag);
190 flag(step_width_sublayer1_enabled_flag);
191 ub(15, step_width_sublayer2);
192 flag(dithering_control_flag);
193 priv->dithering_control_flag = current->dithering_control_flag;
194 infer(temporal_signalling_present_flag, priv->gc->temporal_enabled_flag &&
195 !current->temporal_refresh_bit_flag);
196 } else {
197 infer(quant_matrix_mode, 0);
198 infer(dequant_offset_signalled_flag, 0);
199 ub(4, reserved_zeros_4bit);
200 flag(picture_type_bit_flag);
201 flag(temporal_refresh_bit_flag);
202 infer(step_width_sublayer1_enabled_flag, 0);
203 infer(dithering_control_flag, nal_unit_type == LCEVC_IDR_NUT
204 ? 0
205 : priv->dithering_control_flag);
206 priv->dithering_control_flag = current->dithering_control_flag;
207 flag(temporal_signalling_present_flag);
208 }
209
210 if (current->picture_type_bit_flag == 1) {
211 flag(field_type_bit_flag);
212 ub(7, reserved_zeros_7bit);
213 }
214
215 if (current->step_width_sublayer1_enabled_flag == 1) {
216 ub(15, step_width_sublayer1);
217 flag(level1_filtering_enabled_flag);
218 } else
219 infer(level1_filtering_enabled_flag, 0);
220
221 nlayers = priv->gc->transform_type ? 16 : 4;
222 if (current->quant_matrix_mode == 2 ||
223 current->quant_matrix_mode == 3 ||
224 current->quant_matrix_mode == 5) {
225 for (int layer_idx = 0; layer_idx < nlayers; layer_idx++)
226 ubs(8, qm_coefficient_0[layer_idx], 1, layer_idx);
227 }
228
229 if (current->quant_matrix_mode == 4 || current->quant_matrix_mode == 5) {
230 for (int layer_idx = 0; layer_idx < nlayers; layer_idx++)
231 ubs(8, qm_coefficient_1[layer_idx], 1, layer_idx);
232 }
233
234 if (current->dequant_offset_signalled_flag) {
235 flag(dequant_offset_mode_flag);
236 ub(7, dequant_offset);
237 }
238
239 if (current->dithering_control_flag == 1) {
240 ub(2, dithering_type);
241 ub(1, reserverd_zero);
242 if (current->dithering_type != 0) {
243 ub(5, dithering_strength);
244 } else {
245 ub(5, reserved_zeros_5bit);
246 }
247 }
248
249 av_refstruct_replace(&priv->pc, current);
250 av_refstruct_replace(&current->gc, priv->gc);
251
252 return 0;
253 }
254
255 LCEVC_BLOCK_FUNC(encoded_data, (CodedBitstreamContext *ctx, RWContext *rw,
256 LCEVCRawEncodedData *current,
257 LCEVCProcessBlockState *state,
258 int nal_unit_type))
259 {
260 CodedBitstreamLCEVCContext *priv = ctx->priv_data;
261 int nplanes, nlayers, err;
262 #ifdef READ
263 int start = get_bits_count(rw);
264 #endif
265
266 HEADER("Encoded Data");
267
268 if (!priv->gc || !priv->pc)
269 return AVERROR_INVALIDDATA;
270
271 nplanes = priv->gc->planes_type ? 3 : 1;
272 nlayers = priv->gc->transform_type ? 16 : 4;
273 for (int plane_idx = 0; plane_idx < nplanes; plane_idx++) {
274 if (priv->pc->no_enhancement_bit_flag == 0) {
275 for (int level_idx = 1; level_idx <= 2; level_idx++) {
276 for (int layer_idx = 0; layer_idx < nlayers; layer_idx++) {
277 ubs(1, surfaces_entropy_enabled_flag[plane_idx][level_idx][layer_idx], 3, plane_idx, level_idx, layer_idx);
278 ubs(1, surfaces_rle_only_flag[plane_idx][level_idx][layer_idx], 3, plane_idx, level_idx, layer_idx);
279 }
280 }
281 }
282 if (priv->pc->temporal_signalling_present_flag == 1) {
283 ubs(1, temporal_surfaces_entropy_enabled_flag[plane_idx], 1, plane_idx);
284 ubs(1, temporal_surfaces_rle_only_flag[plane_idx], 1, plane_idx);
285 }
286 }
287
288 CHECK(FUNC(byte_alignment)(ctx, rw));
289
290 #ifdef READ
291 if (!ff_cbs_h2645_read_more_rbsp_data(rw))
292 return AVERROR_INVALIDDATA;
293
294 int pos = get_bits_count(rw) - start;
295 int len = state->payload_size;
296
297 current->header_size = pos / 8;
298 current->data_size = len - pos / 8;
299 skip_bits_long(rw, current->data_size * 8);
300 #else
301 err = ff_cbs_h2645_write_slice_data(ctx, rw, current->data,
302 current->data_size, 0);
303 if (err < 0)
304 return err;
305 #endif
306
307 av_refstruct_replace(&current->sc, priv->sc);
308 av_refstruct_replace(&current->gc, priv->gc);
309 av_refstruct_replace(&current->pc, priv->pc);
310
311 return 0;
312 }
313
314 static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
315 LCEVCRawSEI *current, int payload_size)
316 {
317 SEIRawMessage *message = &current->message;
318 int sei_type;
319 int i, err;
320
321 ub(8, payload_type);
322
323 if (current->payload_type == 1)
324 sei_type = SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME;
325 else if (current->payload_type == 2)
326 sei_type = SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
327 else if (current->payload_type == 4)
328 sei_type = SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35;
329 else if (current->payload_type == 5)
330 sei_type = SEI_TYPE_USER_DATA_UNREGISTERED;
331 else {
332 uint8_t *data;
333
334 #ifdef READ
335 current->payload_size = payload_size;
336 allocate(current->payload_ref, current->payload_size);
337 current->payload = current->payload_ref;
338 #else
339 allocate(current->payload, current->payload_size);
340 #endif
341 data = current->payload;
342
343 for (i = 0; i < current->payload_size; i++)
344 xu(8, reserved_sei_message_payload_byte[i], data[i], 0, 255, 1, i);
345
346 return 0;
347 }
348
349 message->payload_type = sei_type;
350 message->payload_size = payload_size;
351
352 CHECK(FUNC_SEI(message)(ctx, rw, message));
353
354 return 0;
355 }
356
357 static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
358 LCEVCRawVUI *current)
359 {
360 int err;
361
362 HEADER("VUI Parameters");
363
364 flag(aspect_ratio_info_present_flag);
365 if (current->aspect_ratio_info_present_flag) {
366 ub(8, aspect_ratio_idc);
367
368 if (current->aspect_ratio_idc == 255) {
369 ub(16, sar_width);
370 ub(16, sar_height);
371 }
372 } else {
373 infer(aspect_ratio_idc, 0);
374 }
375
376 flag(overscan_info_present_flag);
377 if (current->overscan_info_present_flag)
378 flag(overscan_appropriate_flag);
379 else
380 infer(overscan_appropriate_flag, 0);
381
382 flag(video_signal_type_present_flag);
383 if (current->video_signal_type_present_flag) {
384 u(3, video_format, 0, 5);
385 flag(video_full_range_flag);
386 flag(colour_description_present_flag);
387 if (current->colour_description_present_flag) {
388 ub(8, colour_primaries);
389 ub(8, transfer_characteristics);
390 ub(8, matrix_coefficients);
391 } else {
392 infer(colour_primaries, 2);
393 infer(transfer_characteristics, 2);
394 infer(matrix_coefficients, 2);
395 }
396 } else {
397 infer(video_format, 5);
398 infer(video_full_range_flag, 0);
399 infer(colour_description_present_flag, 0);
400 infer(colour_primaries, 2);
401 infer(transfer_characteristics, 2);
402 infer(matrix_coefficients, 2);
403 }
404 flag(chroma_loc_info_present_flag);
405 if (current->chroma_loc_info_present_flag) {
406 ue(chroma_sample_loc_type_top_field, 0, 5);
407 ue(chroma_sample_loc_type_bottom_field, 0, 5);
408 } else {
409 infer(chroma_sample_loc_type_top_field, 0);
410 infer(chroma_sample_loc_type_bottom_field, 0);
411 }
412
413 return 0;
414 }
415
416 LCEVC_BLOCK_FUNC(additional_info, (CodedBitstreamContext *ctx, RWContext *rw,
417 LCEVCRawAdditionalInfo *current,
418 LCEVCProcessBlockState *state,
419 int nal_unit_type))
420 {
421 int i, err;
422
423 HEADER("Additional Info");
424
425 ub(8, additional_info_type);
426
427 if (current->additional_info_type == LCEVC_ADDITIONAL_INFO_TYPE_SEI) {
428 CHECK(FUNC(sei_payload)(ctx, rw, &current->sei, state->payload_size - 2));
429 } else if (current->additional_info_type == LCEVC_ADDITIONAL_INFO_TYPE_VUI)
430 CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui));
431 else {
432 uint8_t *data;
433
434 #ifdef READ
435 current->payload_size = state->payload_size - 1;
436 allocate(current->payload_ref, current->payload_size);
437 current->payload = current->payload_ref;
438 #else
439 allocate(current->payload, current->payload_size);
440 #endif
441 data = current->payload;
442
443 for (i = 0; i < current->payload_size; i++)
444 xu(8, additional_info_byte[i], data[i], 0, 255, 1, i);
445 }
446
447 return 0;
448 }
449
450 LCEVC_BLOCK_FUNC(filler, (CodedBitstreamContext *ctx, RWContext *rw,
451 LCEVCRawFiller *current,
452 LCEVCProcessBlockState *state,
453 int nal_unit_type))
454 {
455 int err;
456
457 HEADER("Filler");
458
459
460 #ifdef READ
461 while (show_bits(rw, 8) == 0xaa) {
462 fixed(8, filler_byte, 0xaa);
463 ++current->filler_size;
464 }
465 if (state->payload_size != current->filler_size)
466 return AVERROR_INVALIDDATA;
467
468 #else
469 for (int i = 0; i < current->filler_size; i++)
470 fixed(8, filler_byte, 0xaa);
471 #endif
472
473 return 0;
474 }
475
476 static int FUNC(process_block)(CodedBitstreamContext *ctx, RWContext *rw,
477 LCEVCRawProcessBlock *current,
478 int nal_unit_type)
479 {
480 const LCEVCProcessBlockTypeDescriptor *desc;
481 int err, i;
482
483 desc = ff_cbs_lcevc_process_block_find_type(ctx, current->payload_type);
484 if (desc) {
485 LCEVCProcessBlockState state = {
486 .payload_type = current->payload_type,
487 .payload_size = current->payload_size,
488 .extension_present = current->extension_bit_length > 0,
489 };
490 int start_position, current_position, bits_written;
491
492 #ifdef READ
493 CHECK(ff_cbs_lcevc_alloc_process_block_payload(current, desc));
494 #endif
495
496 start_position = bit_position(rw);
497
498 CHECK(desc->READWRITE(ctx, rw, current->payload, &state, nal_unit_type));
499
500 current_position = bit_position(rw);
501 bits_written = current_position - start_position;
502
503 if (byte_alignment(rw) || state.extension_present ||
504 bits_written < 8 * current->payload_size) {
505 size_t bits_left;
506
507 #ifdef READ
508 GetBitContext tmp = *rw;
509 int trailing_bits, trailing_zero_bits;
510
511 bits_left = 8 * current->payload_size - bits_written;
512 if (bits_left > 8)
513 skip_bits_long(&tmp, bits_left - 8);
514 trailing_bits = get_bits(&tmp, FFMIN(bits_left, 8));
515 if (trailing_bits == 0) {
516 // The trailing bits must contain a payload_bit_equal_to_one, so
517 // they can't all be zero.
518 return AVERROR_INVALIDDATA;
519 }
520 trailing_zero_bits = ff_ctz(trailing_bits);
521 current->extension_bit_length =
522 bits_left - 1 - trailing_zero_bits;
523 #endif
524
525 if (current->extension_bit_length > 0) {
526 allocate(current->extension_data,
527 (current->extension_bit_length + 7) / 8);
528
529 bits_left = current->extension_bit_length;
530 for (i = 0; bits_left > 0; i++) {
531 int length = FFMIN(bits_left, 8);
532 xu(length, reserved_payload_extension_data,
533 current->extension_data[i],
534 0, MAX_UINT_BITS(length), 0);
535 bits_left -= length;
536 }
537 }
538
539 fixed(1, payload_bit_equal_to_one, 1);
540 while (byte_alignment(rw))
541 fixed(1, payload_bit_equal_to_zero, 0);
542 }
543
544 #ifdef WRITE
545 current->payload_size = (put_bits_count(rw) - start_position) / 8;
546 #endif
547 } else {
548 uint8_t *data;
549
550 #ifdef READ
551 allocate(current->payload_ref, current->payload_size);
552 current->payload = current->payload_ref;
553 #else
554 allocate(current->payload, current->payload_size);
555 #endif
556 data = current->payload;
557
558 for (i = 0; i < current->payload_size; i++)
559 xu(8, payload_byte[i], data[i], 0, 255, 1, i);
560 }
561
562 return 0;
563 }
564
565 static int FUNC(process_block_list)(CodedBitstreamContext *ctx, RWContext *rw,
566 LCEVCRawProcessBlockList *current,
567 int nal_unit_type)
568 {
569 LCEVCRawProcessBlock *block;
570 int err, k;
571
572 #ifdef READ
573 for (k = 0;; k++) {
574 int payload_size_type;
575 int payload_type;
576 uint32_t payload_size;
577 uint32_t tmp;
578 GetBitContext payload_gbc;
579
580 HEADER("Process Block");
581
582 xu(3, payload_size_type, tmp, 0, MAX_UINT_BITS(3), 0);
583 payload_size_type = tmp;
584 xu(5, payload_type, tmp, 0, MAX_UINT_BITS(5), 0);
585 payload_type = tmp;
586
587 if (payload_size_type == 6) {
588 av_log(ctx->log_ctx, AV_LOG_ERROR, "payload_size_type == 6\n");
589 return AVERROR_INVALIDDATA;
590 }
591
592 payload_size = payload_size_type;
593 if (payload_size_type == 7)
594 xmb(custom_byte_size, payload_size);
595
596 // There must be space remaining for the payload
597 if (payload_size > get_bits_left(rw) / 8) {
598 av_log(ctx->log_ctx, AV_LOG_ERROR,
599 "Invalid process block: payload_size too large "
600 "(%"PRIu32" bytes).\n", payload_size);
601 return AVERROR_INVALIDDATA;
602 }
603 CHECK(init_get_bits(&payload_gbc, rw->buffer,
604 get_bits_count(rw) + 8 * payload_size));
605 skip_bits_long(&payload_gbc, get_bits_count(rw));
606
607 CHECK(ff_cbs_lcevc_list_add(current, -1));
608 block = &current->blocks[k];
609
610 block->payload_type = payload_type;
611 block->payload_size = payload_size;
612
613 CHECK(FUNC(process_block)(ctx, &payload_gbc, block, nal_unit_type));
614
615 skip_bits_long(rw, 8 * payload_size);
616
617 if (!ff_cbs_h2645_read_more_rbsp_data(rw))
618 break;
619 }
620 #else
621 for (k = 0; k < current->nb_blocks; k++) {
622 PutBitContext start_state;
623 uint32_t tmp;
624 int trace, i;
625
626 block = &current->blocks[k];
627
628 // We write the payload twice in order to find the size. Trace
629 // output is switched off for the first write.
630 trace = ctx->trace_enable;
631 ctx->trace_enable = 0;
632
633 start_state = *rw;
634 for (i = 0; i < 2; i++) {
635 *rw = start_state;
636
637 tmp = FFMIN(block->payload_size, 7);
638 xu(3, payload_size_type, tmp, 0, 7, 0);
639 xu(5, payload_type, block->payload_type, 0, MAX_UINT_BITS(5), 0);
640
641 if (tmp == 7)
642 xmb(custom_byte_size, block->payload_size);
643
644 err = FUNC(process_block)(ctx, rw, block, nal_unit_type);
645 ctx->trace_enable = trace;
646 if (err < 0)
647 return err;
648 }
649 }
650 #endif
651
652 return 0;
653 }
654
655 static int FUNC(nal)(CodedBitstreamContext *ctx, RWContext *rw,
656 LCEVCRawNAL *current, int nal_unit_type)
657 {
658 int err;
659
660 if (nal_unit_type == LCEVC_NON_IDR_NUT)
661 HEADER("Non IDR");
662 else
663 HEADER("IDR");
664
665 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
666 (1 << LCEVC_IDR_NUT) | (1 << LCEVC_NON_IDR_NUT)));
667
668 CHECK(FUNC(process_block_list) (ctx, rw, &current->process_block_list,
669 current->nal_unit_header.nal_unit_type));
670
671 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
672
673 return 0;
674 }
675