FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2025-12-07 22:02:57
Exec Total Coverage
Lines: 741 975 76.0%
Functions: 39 39 100.0%
Branches: 438 678 64.6%

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/attributes.h"
20 #include "libavutil/avassert.h"
21 #include "libavutil/mem.h"
22
23 #include "bytestream.h"
24 #include "cbs.h"
25 #include "cbs_internal.h"
26 #include "cbs_h264.h"
27 #include "cbs_h265.h"
28 #include "cbs_h266.h"
29 #include "h264.h"
30 #include "h2645_parse.h"
31 #include "libavutil/refstruct.h"
32 #include "vvc.h"
33
34 #include "hevc/hevc.h"
35
36 346884 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
37 const char *name, const int *subscripts,
38 uint32_t *write_to,
39 uint32_t range_min, uint32_t range_max)
40 {
41 uint32_t leading_bits, value;
42 int max_length, leading_zeroes;
43
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346884 times.
346884 CBS_TRACE_READ_START();
45
46
2/2
✓ Branch 1 taken 11541 times.
✓ Branch 2 taken 335343 times.
346884 max_length = FFMIN(get_bits_left(gbc), 32);
47
48
1/2
✓ Branch 0 taken 346884 times.
✗ Branch 1 not taken.
346884 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346884 times.
346884 if (leading_bits == 0) {
50 if (max_length >= 32) {
51 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
52 "%s: more than 31 zeroes.\n", name);
53 return AVERROR_INVALIDDATA;
54 } else {
55 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
56 "%s: bitstream ended.\n", name);
57 return AVERROR_INVALIDDATA;
58 }
59 }
60
61 346884 leading_zeroes = max_length - 1 - av_log2(leading_bits);
62 346884 skip_bits_long(gbc, leading_zeroes);
63
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 346884 times.
346884 if (get_bits_left(gbc) < leading_zeroes + 1) {
65 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
66 "%s: bitstream ended.\n", name);
67 return AVERROR_INVALIDDATA;
68 }
69
70 346884 value = get_bits_long(gbc, leading_zeroes + 1) - 1;
71
72
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 346884 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
346884 CBS_TRACE_READ_END();
73
74
2/4
✓ Branch 0 taken 346884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 346884 times.
346884 if (value < range_min || value > range_max) {
75 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
76 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
77 name, value, range_min, range_max);
78 return AVERROR_INVALIDDATA;
79 }
80
81 346884 *write_to = value;
82 346884 return 0;
83 }
84
85 167287 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
86 const char *name, const int *subscripts,
87 int32_t *write_to,
88 int32_t range_min, int32_t range_max)
89 {
90 uint32_t leading_bits, unsigned_value;
91 int max_length, leading_zeroes;
92 int32_t value;
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167287 times.
167287 CBS_TRACE_READ_START();
95
96
2/2
✓ Branch 1 taken 9952 times.
✓ Branch 2 taken 157335 times.
167287 max_length = FFMIN(get_bits_left(gbc), 32);
97
98
1/2
✓ Branch 0 taken 167287 times.
✗ Branch 1 not taken.
167287 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167287 times.
167287 if (leading_bits == 0) {
100 if (max_length >= 32) {
101 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
102 "%s: more than 31 zeroes.\n", name);
103 return AVERROR_INVALIDDATA;
104 } else {
105 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
106 "%s: bitstream ended.\n", name);
107 return AVERROR_INVALIDDATA;
108 }
109 }
110
111 167287 leading_zeroes = max_length - 1 - av_log2(leading_bits);
112 167287 skip_bits_long(gbc, leading_zeroes);
113
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 167287 times.
167287 if (get_bits_left(gbc) < leading_zeroes + 1) {
115 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
116 "%s: bitstream ended.\n", name);
117 return AVERROR_INVALIDDATA;
118 }
119
120 167287 unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
121
122
2/2
✓ Branch 0 taken 89809 times.
✓ Branch 1 taken 77478 times.
167287 if (unsigned_value & 1)
123 89809 value = -(int32_t)(unsigned_value / 2);
124 else
125 77478 value = unsigned_value / 2;
126
127
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 167287 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
167287 CBS_TRACE_READ_END();
128
129
2/4
✓ Branch 0 taken 167287 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 167287 times.
167287 if (value < range_min || value > range_max) {
130 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
131 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
132 name, value, range_min, range_max);
133 return AVERROR_INVALIDDATA;
134 }
135
136 167287 *write_to = value;
137 167287 return 0;
138 }
139
140 155423 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
141 const char *name, const int *subscripts,
142 uint32_t value,
143 uint32_t range_min, uint32_t range_max)
144 {
145 int len;
146
147
2/2
✓ Branch 0 taken 152354 times.
✓ Branch 1 taken 3069 times.
155423 CBS_TRACE_WRITE_START();
148
149
2/4
✓ Branch 0 taken 155423 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 155423 times.
155423 if (value < range_min || value > range_max) {
150 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
151 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
152 name, value, range_min, range_max);
153 return AVERROR_INVALIDDATA;
154 }
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155423 times.
155423 av_assert0(value != UINT32_MAX);
156
157 155423 len = av_log2(value + 1);
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 155423 times.
155423 if (put_bits_left(pbc) < 2 * len + 1)
159 return AVERROR(ENOSPC);
160
161 155423 put_bits(pbc, len, 0);
162
1/2
✓ Branch 0 taken 155423 times.
✗ Branch 1 not taken.
155423 if (len + 1 < 32)
163 155423 put_bits(pbc, len + 1, value + 1);
164 else
165 put_bits32(pbc, value + 1);
166
167
3/4
✓ Branch 0 taken 152354 times.
✓ Branch 1 taken 3069 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 152354 times.
155423 CBS_TRACE_WRITE_END();
168
169 155423 return 0;
170 }
171
172 84109 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
173 const char *name, const int *subscripts,
174 int32_t value,
175 int32_t range_min, int32_t range_max)
176 {
177 int len;
178 uint32_t uvalue;
179
180
2/2
✓ Branch 0 taken 83539 times.
✓ Branch 1 taken 570 times.
84109 CBS_TRACE_WRITE_START();
181
182
2/4
✓ Branch 0 taken 84109 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84109 times.
84109 if (value < range_min || value > range_max) {
183 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
184 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
185 name, value, range_min, range_max);
186 return AVERROR_INVALIDDATA;
187 }
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84109 times.
84109 av_assert0(value != INT32_MIN);
189
190
2/2
✓ Branch 0 taken 19190 times.
✓ Branch 1 taken 64919 times.
84109 if (value == 0)
191 19190 uvalue = 0;
192
2/2
✓ Branch 0 taken 36580 times.
✓ Branch 1 taken 28339 times.
64919 else if (value > 0)
193 36580 uvalue = 2 * (uint32_t)value - 1;
194 else
195 28339 uvalue = 2 * (uint32_t)-value;
196
197 84109 len = av_log2(uvalue + 1);
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84109 times.
84109 if (put_bits_left(pbc) < 2 * len + 1)
199 return AVERROR(ENOSPC);
200
201 84109 put_bits(pbc, len, 0);
202
1/2
✓ Branch 0 taken 84109 times.
✗ Branch 1 not taken.
84109 if (len + 1 < 32)
203 84109 put_bits(pbc, len + 1, uvalue + 1);
204 else
205 put_bits32(pbc, uvalue + 1);
206
207
3/4
✓ Branch 0 taken 83539 times.
✓ Branch 1 taken 570 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83539 times.
84109 CBS_TRACE_WRITE_END();
208
209 84109 return 0;
210 }
211
212 // payload_extension_present() - true if we are before the last 1-bit
213 // in the payload structure, which must be in the last byte.
214 19 static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
215 int cur_pos)
216 {
217 19 int bits_left = payload_size * 8 - cur_pos;
218
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 9 times.
24 return (bits_left > 0 &&
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 (bits_left > 7 || show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1)));
220 }
221
222 #define HEADER(name) do { \
223 ff_cbs_trace_header(ctx, name); \
224 } while (0)
225
226 #define CHECK(call) do { \
227 err = (call); \
228 if (err < 0) \
229 return err; \
230 } while (0)
231
232 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
233 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
234 #define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
235 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
236 #define FUNC_H266(name) FUNC_NAME1(READWRITE, h266, name)
237 #define FUNC_SEI(name) FUNC_NAME1(READWRITE, sei, name)
238
239 #define SEI_FUNC(name, args) \
240 static int FUNC(name) args; \
241 static int FUNC(name ## _internal)(CodedBitstreamContext *ctx, \
242 RWContext *rw, void *cur, \
243 SEIMessageState *state) \
244 { \
245 return FUNC(name)(ctx, rw, cur, state); \
246 } \
247 static int FUNC(name) args
248
249 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
250
251 #define u(width, name, range_min, range_max) \
252 xu(width, name, current->name, range_min, range_max, 0, )
253 #define flag(name) ub(1, name)
254 #define ue(name, range_min, range_max) \
255 xue(name, current->name, range_min, range_max, 0, )
256 #define i(width, name, range_min, range_max) \
257 xi(width, name, current->name, range_min, range_max, 0, )
258 #define ib(width, name) \
259 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
260 #define se(name, range_min, range_max) \
261 xse(name, current->name, range_min, range_max, 0, )
262
263 #define us(width, name, range_min, range_max, subs, ...) \
264 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
265 #define ubs(width, name, subs, ...) \
266 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
267 #define flags(name, subs, ...) \
268 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
269 #define ues(name, range_min, range_max, subs, ...) \
270 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
271 #define is(width, name, range_min, range_max, subs, ...) \
272 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
273 #define ibs(width, name, subs, ...) \
274 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
275 #define ses(name, range_min, range_max, subs, ...) \
276 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
277
278 #define fixed(width, name, value) do { \
279 av_unused uint32_t fixed_value = value; \
280 xu(width, name, fixed_value, value, value, 0, ); \
281 } while (0)
282
283
284 #define READ
285 #define READWRITE read
286 #define RWContext GetBitContext
287
288 #define ub(width, name) do { \
289 uint32_t value; \
290 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
291 &value)); \
292 current->name = value; \
293 } while (0)
294 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
295 uint32_t value; \
296 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
297 SUBSCRIPTS(subs, __VA_ARGS__), \
298 &value, range_min, range_max)); \
299 var = value; \
300 } while (0)
301 #define xue(name, var, range_min, range_max, subs, ...) do { \
302 uint32_t value; \
303 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
304 SUBSCRIPTS(subs, __VA_ARGS__), \
305 &value, range_min, range_max)); \
306 var = value; \
307 } while (0)
308 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
309 int32_t value; \
310 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
311 SUBSCRIPTS(subs, __VA_ARGS__), \
312 &value, range_min, range_max)); \
313 var = value; \
314 } while (0)
315 #define xse(name, var, range_min, range_max, subs, ...) do { \
316 int32_t value; \
317 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
318 SUBSCRIPTS(subs, __VA_ARGS__), \
319 &value, range_min, range_max)); \
320 var = value; \
321 } while (0)
322
323
324 #define infer(name, value) do { \
325 current->name = value; \
326 } while (0)
327
328 29918 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
329 {
330 29918 int bits_left = get_bits_left(gbc);
331
2/2
✓ Branch 0 taken 22353 times.
✓ Branch 1 taken 7565 times.
29918 if (bits_left > 8)
332 22353 return 1;
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7565 times.
7565 if (bits_left == 0)
334 return 0;
335
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 7554 times.
7565 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
336 11 return 1;
337 7554 return 0;
338 }
339
340 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
341
342 #define bit_position(rw) (get_bits_count(rw))
343 #define byte_alignment(rw) (get_bits_count(rw) % 8)
344
345 /* The CBS SEI code uses the refstruct API for the allocation
346 * of its child buffers. */
347 #define allocate(name, size) do { \
348 name = av_refstruct_allocz(size + \
349 AV_INPUT_BUFFER_PADDING_SIZE); \
350 if (!name) \
351 return AVERROR(ENOMEM); \
352 } while (0)
353
354 #define FUNC(name) FUNC_SEI(name)
355 #include "cbs_sei_syntax_template.c"
356 #undef FUNC
357
358 #undef allocate
359
360 /* The other code uses the refstruct API for the allocation
361 * of its child buffers. */
362 #define allocate(name, size) do { \
363 name ## _ref = av_buffer_allocz(size + \
364 AV_INPUT_BUFFER_PADDING_SIZE); \
365 if (!name ## _ref) \
366 return AVERROR(ENOMEM); \
367 name = name ## _ref->data; \
368 } while (0)
369
370 #define FUNC(name) FUNC_H264(name)
371 #include "cbs_h264_syntax_template.c"
372 #undef FUNC
373
374 #define FUNC(name) FUNC_H265(name)
375 #include "cbs_h265_syntax_template.c"
376 #undef FUNC
377
378 #define FUNC(name) FUNC_H266(name)
379 #include "cbs_h266_syntax_template.c"
380 #undef FUNC
381
382 #undef READ
383 #undef READWRITE
384 #undef RWContext
385 #undef ub
386 #undef xu
387 #undef xi
388 #undef xue
389 #undef xse
390 #undef infer
391 #undef more_rbsp_data
392 #undef bit_position
393 #undef byte_alignment
394 #undef allocate
395
396
397 #define WRITE
398 #define READWRITE write
399 #define RWContext PutBitContext
400
401 #define ub(width, name) do { \
402 uint32_t value = current->name; \
403 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
404 value)); \
405 } while (0)
406 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
407 uint32_t value = var; \
408 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
409 SUBSCRIPTS(subs, __VA_ARGS__), \
410 value, range_min, range_max)); \
411 } while (0)
412 #define xue(name, var, range_min, range_max, subs, ...) do { \
413 uint32_t value = var; \
414 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
415 SUBSCRIPTS(subs, __VA_ARGS__), \
416 value, range_min, range_max)); \
417 } while (0)
418 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
419 int32_t value = var; \
420 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
421 SUBSCRIPTS(subs, __VA_ARGS__), \
422 value, range_min, range_max)); \
423 } while (0)
424 #define xse(name, var, range_min, range_max, subs, ...) do { \
425 int32_t value = var; \
426 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
427 SUBSCRIPTS(subs, __VA_ARGS__), \
428 value, range_min, range_max)); \
429 } while (0)
430
431 #define infer(name, value) do { \
432 if (current->name != (value)) { \
433 av_log(ctx->log_ctx, AV_LOG_ERROR, \
434 "%s does not match inferred value: " \
435 "%"PRId64", but should be %"PRId64".\n", \
436 #name, (int64_t)current->name, (int64_t)(value)); \
437 return AVERROR_INVALIDDATA; \
438 } \
439 } while (0)
440
441 #define more_rbsp_data(var) (var)
442
443 #define bit_position(rw) (put_bits_count(rw))
444 #define byte_alignment(rw) (put_bits_count(rw) % 8)
445
446 #define allocate(name, size) do { \
447 if (!name) { \
448 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
449 "for writing.\n", #name); \
450 return AVERROR_INVALIDDATA; \
451 } \
452 } while (0)
453
454 #define FUNC(name) FUNC_SEI(name)
455 #include "cbs_sei_syntax_template.c"
456 #undef FUNC
457
458 #define FUNC(name) FUNC_H264(name)
459 #include "cbs_h264_syntax_template.c"
460 #undef FUNC
461
462 #define FUNC(name) FUNC_H265(name)
463 #include "cbs_h265_syntax_template.c"
464 #undef FUNC
465
466 #define FUNC(name) FUNC_H266(name)
467 #include "cbs_h266_syntax_template.c"
468 #undef FUNC
469
470 #undef WRITE
471 #undef READWRITE
472 #undef RWContext
473 #undef ub
474 #undef xu
475 #undef xi
476 #undef xue
477 #undef xse
478 #undef u
479 #undef i
480 #undef flag
481 #undef ue
482 #undef se
483 #undef infer
484 #undef more_rbsp_data
485 #undef bit_position
486 #undef byte_alignment
487 #undef allocate
488
489
490 12551 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
491 CodedBitstreamFragment *frag,
492 const H2645Packet *packet)
493 {
494 int err, i;
495
496
2/2
✓ Branch 0 taken 38608 times.
✓ Branch 1 taken 12551 times.
51159 for (i = 0; i < packet->nb_nals; i++) {
497 38608 const H2645NAL *nal = &packet->nals[i];
498 AVBufferRef *ref;
499 38608 size_t size = nal->size;
500 38608 enum AVCodecID codec_id = ctx->codec->codec_id;
501
502
3/4
✓ Branch 0 taken 9960 times.
✓ Branch 1 taken 28648 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9960 times.
38608 if (codec_id == AV_CODEC_ID_HEVC && nal->nuh_layer_id > 0 &&
503 (nal->type < HEVC_NAL_VPS || nal->type > HEVC_NAL_PPS))
504 continue;
505
506 // Remove trailing zeroes.
507
3/4
✓ Branch 0 taken 47177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8569 times.
✓ Branch 3 taken 38608 times.
47177 while (size > 0 && nal->data[size - 1] == 0)
508 8569 --size;
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38608 times.
38608 if (size == 0) {
510 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
511 continue;
512 }
513
514 77216 ref = (nal->data == nal->raw_data) ? frag->data_ref
515
2/2
✓ Branch 0 taken 37163 times.
✓ Branch 1 taken 1445 times.
38608 : packet->rbsp.rbsp_buffer_ref;
516
517 38608 err = ff_cbs_append_unit_data(frag, nal->type,
518 38608 (uint8_t*)nal->data, size, ref);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38608 times.
38608 if (err < 0)
520 return err;
521 }
522
523 12551 return 0;
524 }
525
526 12537 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
527 CodedBitstreamFragment *frag,
528 int header)
529 {
530 12537 enum AVCodecID codec_id = ctx->codec->codec_id;
531 12537 CodedBitstreamH2645Context *priv = ctx->priv_data;
532 GetByteContext gbc;
533 int err;
534
535
2/4
✓ Branch 0 taken 12537 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12537 times.
12537 av_assert0(frag->data && frag->nb_units == 0);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12537 times.
12537 if (frag->data_size == 0)
537 return 0;
538
539
6/6
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 12361 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 165 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 3 times.
12545 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
540 // AVCC header.
541 size_t size, start, end;
542 int i, count, version;
543
544 8 priv->mp4 = 1;
545
546 8 bytestream2_init(&gbc, frag->data, frag->data_size);
547
548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(&gbc) < 6)
549 return AVERROR_INVALIDDATA;
550
551 8 version = bytestream2_get_byte(&gbc);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (version != 1) {
553 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
554 "first byte %u.\n", version);
555 return AVERROR_INVALIDDATA;
556 }
557
558 8 bytestream2_skip(&gbc, 3);
559 8 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
560
561 // SPS array.
562 8 count = bytestream2_get_byte(&gbc) & 0x1f;
563 8 start = bytestream2_tell(&gbc);
564
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
15 for (i = 0; i < count; i++) {
565
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
566 return AVERROR_INVALIDDATA;
567 7 size = bytestream2_get_be16(&gbc);
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (bytestream2_get_bytes_left(&gbc) < size)
569 return AVERROR_INVALIDDATA;
570 7 bytestream2_skip(&gbc, size);
571 }
572 8 end = bytestream2_tell(&gbc);
573
574 8 err = ff_h2645_packet_split(&priv->read_packet,
575 8 frag->data + start, end - start,
576 ctx->log_ctx, 2, AV_CODEC_ID_H264,
577 H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0) {
579 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
580 return err;
581 }
582 8 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0)
584 return err;
585
586 // PPS array.
587 8 count = bytestream2_get_byte(&gbc);
588 8 start = bytestream2_tell(&gbc);
589
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
15 for (i = 0; i < count; i++) {
590
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
591 return AVERROR_INVALIDDATA;
592 7 size = bytestream2_get_be16(&gbc);
593
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (bytestream2_get_bytes_left(&gbc) < size)
594 return AVERROR_INVALIDDATA;
595 7 bytestream2_skip(&gbc, size);
596 }
597 8 end = bytestream2_tell(&gbc);
598
599 8 err = ff_h2645_packet_split(&priv->read_packet,
600 8 frag->data + start, end - start,
601 ctx->log_ctx, 2, AV_CODEC_ID_H264,
602 H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0) {
604 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
605 return err;
606 }
607 8 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0)
609 return err;
610
611
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
8 if (bytestream2_get_bytes_left(&gbc) > 0) {
612 2 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
613 "header.\n", bytestream2_get_bytes_left(&gbc));
614 }
615
616
5/6
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 12361 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 165 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
12529 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
617 // HVCC header.
618 size_t size, start, end;
619 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
620
621 priv->mp4 = 1;
622
623 bytestream2_init(&gbc, frag->data, frag->data_size);
624
625 if (bytestream2_get_bytes_left(&gbc) < 23)
626 return AVERROR_INVALIDDATA;
627
628 version = bytestream2_get_byte(&gbc);
629 if (version != 1) {
630 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
631 "first byte %u.\n", version);
632 return AVERROR_INVALIDDATA;
633 }
634
635 bytestream2_skip(&gbc, 20);
636 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
637
638 nb_arrays = bytestream2_get_byte(&gbc);
639 for (i = 0; i < nb_arrays; i++) {
640 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
641 nb_nals = bytestream2_get_be16(&gbc);
642
643 start = bytestream2_tell(&gbc);
644 for (j = 0; j < nb_nals; j++) {
645 if (bytestream2_get_bytes_left(&gbc) < 2)
646 return AVERROR_INVALIDDATA;
647 size = bytestream2_get_be16(&gbc);
648 if (bytestream2_get_bytes_left(&gbc) < size)
649 return AVERROR_INVALIDDATA;
650 bytestream2_skip(&gbc, size);
651 }
652 end = bytestream2_tell(&gbc);
653
654 err = ff_h2645_packet_split(&priv->read_packet,
655 frag->data + start, end - start,
656 ctx->log_ctx, 2, AV_CODEC_ID_HEVC,
657 H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
658 if (err < 0) {
659 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
660 "HVCC array %d (%d NAL units of type %d).\n",
661 i, nb_nals, nal_unit_type);
662 return err;
663 }
664 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
665 if (err < 0)
666 return err;
667 }
668
669
5/6
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 12361 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 165 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
12532 } else if(header && frag->data[0] && codec_id == AV_CODEC_ID_VVC) {
670 // VVCC header.
671 int ptl_present_flag, num_arrays;
672 int b, i, j;
673
674 3 priv->mp4 = 1;
675
676 3 bytestream2_init(&gbc, frag->data, frag->data_size);
677
678 3 b = bytestream2_get_byte(&gbc);
679 3 priv->nal_length_size = ((b >> 1) & 3) + 1;
680 3 ptl_present_flag = b & 1;
681
682
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(ptl_present_flag) {
683 int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
684 3 num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
685 3 bytestream2_skip(&gbc, 1);
686
687 // begin VvcPTLRecord(num_sublayers);
688 3 num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
689 3 bytestream2_skip(&gbc, 2 + num_bytes_constraint_info);
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if(num_sublayers > 1) {
691 int count_present_flags = 0;
692 b = bytestream2_get_byte(&gbc);
693 for(i = num_sublayers - 2; i >= 0; i--) {
694 if((b >> (7 - (num_sublayers - 2 - i))) & 0x01)
695 count_present_flags++;
696 }
697 bytestream2_skip(&gbc, count_present_flags);
698 }
699 3 num_sub_profiles = bytestream2_get_byte(&gbc);
700 3 bytestream2_skip(&gbc, num_sub_profiles * 4);
701 // end VvcPTLRecord(num_sublayers);
702
703 3 bytestream2_skip(&gbc, 3 * 2);
704 }
705
706 3 num_arrays = bytestream2_get_byte(&gbc);
707
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for(j = 0; j < num_arrays; j++) {
708 size_t start, end, size;
709 9 int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
710 9 unsigned int num_nalus = 1;
711
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 if(nal_unit_type != VVC_DCI_NUT && nal_unit_type != VVC_OPI_NUT)
712 9 num_nalus = bytestream2_get_be16(&gbc);
713
714 9 start = bytestream2_tell(&gbc);
715
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for(i = 0; i < num_nalus; i++) {
716
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(&gbc) < 2)
717 return AVERROR_INVALIDDATA;
718 9 size = bytestream2_get_be16(&gbc);
719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(&gbc) < size)
720 return AVERROR_INVALIDDATA;
721 9 bytestream2_skip(&gbc, size);
722 }
723 9 end = bytestream2_tell(&gbc);
724
725 9 err = ff_h2645_packet_split(&priv->read_packet,
726 9 frag->data + start, end - start,
727 ctx->log_ctx, 2, AV_CODEC_ID_VVC,
728 H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0) {
730 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
731 "VVCC array %d (%d NAL units of type %d).\n",
732 i, num_nalus, nal_unit_type);
733 return err;
734 }
735 9 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0)
737 return err;
738 }
739 } else {
740
2/2
✓ Branch 0 taken 641 times.
✓ Branch 1 taken 11885 times.
12526 int flags = (H2645_FLAG_IS_NALFF * !!priv->mp4) | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF;
741 // Annex B, or later MP4 with already-known parameters.
742
743 12526 err = ff_h2645_packet_split(&priv->read_packet,
744 12526 frag->data, frag->data_size,
745 ctx->log_ctx,
746 priv->nal_length_size,
747 codec_id, flags);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12526 times.
12526 if (err < 0)
749 return err;
750
751 12526 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12526 times.
12526 if (err < 0)
753 return err;
754 }
755
756 12537 return 0;
757 }
758
759 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
760 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
761 CodedBitstreamUnit *unit) \
762 { \
763 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
764 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
765 unsigned int id = ps_var->id_element; \
766 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
767 if (err < 0) \
768 return err; \
769 if (priv->ps_var[id] == priv->active_ ## ps_var) \
770 priv->active_ ## ps_var = NULL ; \
771 av_assert0(unit->content_ref); \
772 av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
773 return 0; \
774 }
775
776
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
✓ Branch 3 taken 111 times.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 152 times.
152 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
777
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2996 times.
✓ Branch 3 taken 2941 times.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2996 times.
2996 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
778
3/6
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 126 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 126 times.
126 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
779
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 129 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 132 times.
132 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
780
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 708 times.
✓ Branch 3 taken 457 times.
✓ Branch 4 taken 251 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 708 times.
708 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
781
782 #define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
783 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
784 CodedBitstreamUnit *unit) \
785 { \
786 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
787 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
788 unsigned int id = ps_var->id_element; \
789 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
790 if (err < 0) \
791 return err; \
792 av_assert0(unit->content_ref); \
793 av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
794 return 0; \
795 }
796
797
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
26 cbs_h266_replace_ps(6, VPS, vps, vps_video_parameter_set_id)
798
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1898 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1898 times.
1898 cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
799
800 1267 static int cbs_h266_replace_sps(CodedBitstreamContext *ctx,
801 CodedBitstreamUnit *unit)
802 {
803 1267 CodedBitstreamH266Context *priv = ctx->priv_data;
804 1267 H266RawSPS *sps = unit->content;
805 1267 unsigned int id = sps->sps_seq_parameter_set_id;
806 1267 int err = ff_cbs_make_unit_refcounted(ctx, unit);
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 if (err < 0)
808 return err;
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 av_assert0(unit->content_ref);
810
4/4
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 230 times.
✓ Branch 2 taken 786 times.
✓ Branch 3 taken 251 times.
1267 if (priv->sps[id] && memcmp(priv->sps[id], unit->content_ref, sizeof(*priv->sps[id]))) {
811
2/2
✓ Branch 0 taken 50304 times.
✓ Branch 1 taken 786 times.
51090 for (unsigned int i = 0; i < VVC_MAX_PPS_COUNT; i++) {
812
3/4
✓ Branch 0 taken 786 times.
✓ Branch 1 taken 49518 times.
✓ Branch 2 taken 786 times.
✗ Branch 3 not taken.
50304 if (priv->pps[i] && priv->pps[i]->pps_seq_parameter_set_id == id)
813 786 av_refstruct_unref(&priv->pps[i]);
814 }
815 }
816 1267 av_refstruct_replace(&priv->sps[id], unit->content_ref);
817 1267 return 0;
818 }
819
820 8514 static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
821 CodedBitstreamUnit *unit,
822 H266RawPictureHeader *ph)
823 {
824 8514 CodedBitstreamH266Context *h266 = ctx->priv_data;
825 int err;
826
827 8514 err = ff_cbs_make_unit_refcounted(ctx, unit);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8514 times.
8514 if (err < 0)
829 return err;
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8514 times.
8514 av_assert0(unit->content_ref);
831 8514 av_refstruct_replace(&h266->ph_ref, unit->content_ref);
832 8514 h266->ph = ph;
833 8514 return 0;
834 }
835
836 9323 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
837 CodedBitstreamUnit *unit)
838 {
839 GetBitContext gbc;
840 int err;
841
842 9323 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9323 times.
9323 if (err < 0)
844 return err;
845
846 9323 err = ff_cbs_alloc_unit_content(ctx, unit);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9323 times.
9323 if (err < 0)
848 return err;
849
850
7/9
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1499 times.
✓ Branch 3 taken 6056 times.
✓ Branch 4 taken 739 times.
✓ Branch 5 taken 915 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
9323 switch (unit->type) {
851 77 case H264_NAL_SPS:
852 {
853 77 H264RawSPS *sps = unit->content;
854
855 77 err = cbs_h264_read_sps(ctx, &gbc, sps);
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (err < 0)
857 return err;
858
859 77 err = cbs_h264_replace_sps(ctx, unit);
860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (err < 0)
861 return err;
862 }
863 77 break;
864
865 case H264_NAL_SPS_EXT:
866 {
867 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
868 if (err < 0)
869 return err;
870 }
871 break;
872
873 1499 case H264_NAL_PPS:
874 {
875 1499 H264RawPPS *pps = unit->content;
876
877 1499 err = cbs_h264_read_pps(ctx, &gbc, pps);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1499 times.
1499 if (err < 0)
879 return err;
880
881 1499 err = cbs_h264_replace_pps(ctx, unit);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1499 times.
1499 if (err < 0)
883 return err;
884 }
885 1499 break;
886
887 6056 case H264_NAL_SLICE:
888 case H264_NAL_IDR_SLICE:
889 case H264_NAL_AUXILIARY_SLICE:
890 {
891 6056 H264RawSlice *slice = unit->content;
892 int pos, len;
893
894 6056 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6056 times.
6056 if (err < 0)
896 return err;
897
898
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6056 times.
6056 if (!cbs_h2645_read_more_rbsp_data(&gbc))
899 return AVERROR_INVALIDDATA;
900
901 6056 pos = get_bits_count(&gbc);
902 6056 len = unit->data_size;
903
904 6056 slice->data_size = len - pos / 8;
905 6056 slice->data_ref = av_buffer_ref(unit->data_ref);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6056 times.
6056 if (!slice->data_ref)
907 return AVERROR(ENOMEM);
908 6056 slice->data = unit->data + pos / 8;
909 6056 slice->data_bit_start = pos % 8;
910 }
911 6056 break;
912
913 739 case H264_NAL_AUD:
914 {
915 739 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (err < 0)
917 return err;
918 }
919 739 break;
920
921 915 case H264_NAL_SEI:
922 {
923 915 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 915 times.
915 if (err < 0)
925 return err;
926 }
927 915 break;
928
929 34 case H264_NAL_FILLER_DATA:
930 {
931 34 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
932
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
34 if (err < 0)
933 1 return err;
934 }
935 33 break;
936
937 3 case H264_NAL_END_SEQUENCE:
938 case H264_NAL_END_STREAM:
939 {
940 3 err = (unit->type == H264_NAL_END_SEQUENCE ?
941
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 cbs_h264_read_end_of_sequence :
942 3 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0)
944 return err;
945 }
946 3 break;
947
948 default:
949 return AVERROR(ENOSYS);
950 }
951
952 9322 return 0;
953 }
954
955 9960 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
956 CodedBitstreamUnit *unit)
957 {
958 GetBitContext gbc;
959 int err;
960
961 9960 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9960 times.
9960 if (err < 0)
963 return err;
964
965 9960 err = ff_cbs_alloc_unit_content(ctx, unit);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9960 times.
9960 if (err < 0)
967 return err;
968
969
6/8
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 421 times.
✓ Branch 3 taken 7486 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1857 times.
✗ Branch 7 not taken.
9960 switch (unit->type) {
970 66 case HEVC_NAL_VPS:
971 {
972 66 H265RawVPS *vps = unit->content;
973
974 66 err = cbs_h265_read_vps(ctx, &gbc, vps);
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (err < 0)
976 return err;
977
978 66 err = cbs_h265_replace_vps(ctx, unit);
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (err < 0)
980 return err;
981 }
982 66 break;
983 70 case HEVC_NAL_SPS:
984 {
985 70 H265RawSPS *sps = unit->content;
986
987 70 err = cbs_h265_read_sps(ctx, &gbc, sps);
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (err < 0)
989 return err;
990
991 70 err = cbs_h265_replace_sps(ctx, unit);
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (err < 0)
993 return err;
994 }
995 70 break;
996
997 421 case HEVC_NAL_PPS:
998 {
999 421 H265RawPPS *pps = unit->content;
1000
1001 421 err = cbs_h265_read_pps(ctx, &gbc, pps);
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (err < 0)
1003 return err;
1004
1005 421 err = cbs_h265_replace_pps(ctx, unit);
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (err < 0)
1007 return err;
1008 }
1009 421 break;
1010
1011 7486 case HEVC_NAL_TRAIL_N:
1012 case HEVC_NAL_TRAIL_R:
1013 case HEVC_NAL_TSA_N:
1014 case HEVC_NAL_TSA_R:
1015 case HEVC_NAL_STSA_N:
1016 case HEVC_NAL_STSA_R:
1017 case HEVC_NAL_RADL_N:
1018 case HEVC_NAL_RADL_R:
1019 case HEVC_NAL_RASL_N:
1020 case HEVC_NAL_RASL_R:
1021 case HEVC_NAL_BLA_W_LP:
1022 case HEVC_NAL_BLA_W_RADL:
1023 case HEVC_NAL_BLA_N_LP:
1024 case HEVC_NAL_IDR_W_RADL:
1025 case HEVC_NAL_IDR_N_LP:
1026 case HEVC_NAL_CRA_NUT:
1027 {
1028 7486 H265RawSlice *slice = unit->content;
1029 int pos, len;
1030
1031 7486 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7486 times.
7486 if (err < 0)
1033 return err;
1034
1035
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7486 times.
7486 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1036 return AVERROR_INVALIDDATA;
1037
1038 7486 pos = get_bits_count(&gbc);
1039 7486 len = unit->data_size;
1040
1041 7486 slice->data_size = len - pos / 8;
1042 7486 slice->data_ref = av_buffer_ref(unit->data_ref);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7486 times.
7486 if (!slice->data_ref)
1044 return AVERROR(ENOMEM);
1045 7486 slice->data = unit->data + pos / 8;
1046 7486 slice->data_bit_start = pos % 8;
1047 }
1048 7486 break;
1049
1050 60 case HEVC_NAL_AUD:
1051 {
1052 60 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1054 return err;
1055 }
1056 60 break;
1057
1058 case HEVC_NAL_FD_NUT:
1059 {
1060 err = cbs_h265_read_filler(ctx, &gbc, unit->content);
1061 if (err < 0)
1062 return err;
1063 }
1064 break;
1065
1066 1857 case HEVC_NAL_SEI_PREFIX:
1067 case HEVC_NAL_SEI_SUFFIX:
1068 {
1069 1857 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1070 1857 unit->type == HEVC_NAL_SEI_PREFIX);
1071
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1857 times.
1857 if (err < 0)
1073 return err;
1074 }
1075 1857 break;
1076
1077 default:
1078 return AVERROR(ENOSYS);
1079 }
1080
1081 9960 return 0;
1082 }
1083
1084 15468 static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
1085 CodedBitstreamUnit *unit)
1086 {
1087 GetBitContext gbc;
1088 int err;
1089 15468 CodedBitstreamH266Context *h266 = ctx->priv_data;
1090
1091 15468 err = init_get_bits8(&gbc, unit->data, unit->data_size);
1092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15468 times.
15468 if (err < 0)
1093 return err;
1094
1095 15468 err = ff_cbs_alloc_unit_content(ctx, unit);
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15468 times.
15468 if (err < 0)
1097 return err;
1098
1099
10/11
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 932 times.
✓ Branch 4 taken 1507 times.
✓ Branch 5 taken 655 times.
✓ Branch 6 taken 218 times.
✓ Branch 7 taken 8787 times.
✓ Branch 8 taken 53 times.
✓ Branch 9 taken 3288 times.
✗ Branch 10 not taken.
15468 switch (unit->type) {
1100 4 case VVC_DCI_NUT:
1101 {
1102 4 err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1105 return err;
1106 }
1107 4 break;
1108 2 case VVC_OPI_NUT:
1109 {
1110 2 err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1111
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1113 return err;
1114 }
1115 2 break;
1116 22 case VVC_VPS_NUT:
1117 {
1118 22 H266RawVPS *vps = unit->content;
1119
1120 22 err = cbs_h266_read_vps(ctx, &gbc, vps);
1121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (err < 0)
1122 return err;
1123
1124 22 err = cbs_h266_replace_vps(ctx, unit);
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (err < 0)
1126 return err;
1127 }
1128 22 break;
1129 932 case VVC_SPS_NUT:
1130 {
1131 932 H266RawSPS *sps = unit->content;
1132
1133 932 err = cbs_h266_read_sps(ctx, &gbc, sps);
1134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 932 times.
932 if (err < 0)
1135 return err;
1136
1137 932 err = cbs_h266_replace_sps(ctx, unit);
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 932 times.
932 if (err < 0)
1139 return err;
1140 }
1141 932 break;
1142
1143 1507 case VVC_PPS_NUT:
1144 {
1145 1507 H266RawPPS *pps = unit->content;
1146
1147 1507 err = cbs_h266_read_pps(ctx, &gbc, pps);
1148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1507 times.
1507 if (err < 0)
1149 return err;
1150
1151 1507 err = cbs_h266_replace_pps(ctx, unit);
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1507 times.
1507 if (err < 0)
1153 return err;
1154 }
1155 1507 break;
1156
1157 655 case VVC_PREFIX_APS_NUT:
1158 case VVC_SUFFIX_APS_NUT:
1159 {
1160 655 err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1161 655 unit->type == VVC_PREFIX_APS_NUT);
1162
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 655 times.
655 if (err < 0)
1164 return err;
1165 }
1166 655 break;
1167 218 case VVC_PH_NUT:
1168 {
1169 218 H266RawPH *ph = unit->content;
1170 218 err = cbs_h266_read_ph(ctx, &gbc, ph);
1171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (err < 0)
1172 return err;
1173 218 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (err < 0)
1175 return err;
1176 }
1177 218 break;
1178
1179 8787 case VVC_TRAIL_NUT:
1180 case VVC_STSA_NUT:
1181 case VVC_RADL_NUT:
1182 case VVC_RASL_NUT:
1183 case VVC_IDR_W_RADL:
1184 case VVC_IDR_N_LP:
1185 case VVC_CRA_NUT:
1186 case VVC_GDR_NUT:
1187 {
1188 8787 H266RawSlice *slice = unit->content;
1189 int pos, len;
1190
1191 8787 err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8787 times.
8787 if (err < 0)
1193 return err;
1194
1195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8787 times.
8787 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1196 return AVERROR_INVALIDDATA;
1197
1198 8787 pos = get_bits_count(&gbc);
1199 8787 len = unit->data_size;
1200
1201
2/2
✓ Branch 0 taken 6166 times.
✓ Branch 1 taken 2621 times.
8787 if (slice->header.sh_picture_header_in_slice_header_flag) {
1202 6166 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6166 times.
6166 if (err < 0)
1204 return err;
1205 6166 slice->ph_ref = NULL;
1206 } else {
1207 2621 slice->ph_ref = av_refstruct_ref(h266->ph_ref);
1208 }
1209 8787 slice->ph = h266->ph;
1210 8787 slice->pps = av_refstruct_ref(h266->pps[slice->ph->ph_pic_parameter_set_id]);
1211 8787 slice->sps = av_refstruct_ref(h266->sps[slice->pps->pps_seq_parameter_set_id]);
1212
1213 8787 slice->header_size = pos / 8;
1214 8787 slice->data_size = len - pos / 8;
1215 8787 slice->data_ref = av_buffer_ref(unit->data_ref);
1216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8787 times.
8787 if (!slice->data_ref)
1217 return AVERROR(ENOMEM);
1218 8787 slice->data = unit->data + pos / 8;
1219 8787 slice->data_bit_start = pos % 8;
1220 }
1221 8787 break;
1222
1223 53 case VVC_AUD_NUT:
1224 {
1225 53 err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (err < 0)
1227 return err;
1228 }
1229 53 break;
1230
1231 3288 case VVC_PREFIX_SEI_NUT:
1232 case VVC_SUFFIX_SEI_NUT:
1233 {
1234 3288 err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1235 3288 unit->type == VVC_PREFIX_SEI_NUT);
1236
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3288 times.
3288 if (err < 0)
1238 return err;
1239 }
1240 3288 break;
1241
1242 default:
1243 return AVERROR(ENOSYS);
1244 }
1245 15468 return 0;
1246 }
1247
1248 14273 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1249 PutBitContext *pbc, const uint8_t *data,
1250 size_t data_size, int data_bit_start)
1251 {
1252 14273 size_t rest = data_size - (data_bit_start + 7) / 8;
1253 14273 const uint8_t *pos = data + data_bit_start / 8;
1254
1255
2/4
✓ Branch 0 taken 14273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14273 times.
14273 av_assert0(data_bit_start >= 0 &&
1256 data_size > data_bit_start / 8);
1257
1258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14273 times.
14273 if (data_size * 8 + 8 > put_bits_left(pbc))
1259 return AVERROR(ENOSPC);
1260
1261
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14272 times.
14273 if (!rest)
1262 1 goto rbsp_stop_one_bit;
1263
1264 // First copy the remaining bits of the first byte
1265 // The above check ensures that we do not accidentally
1266 // copy beyond the rbsp_stop_one_bit.
1267
2/2
✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 10543 times.
14272 if (data_bit_start % 8)
1268 3729 put_bits(pbc, 8 - data_bit_start % 8,
1269 3729 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1270
1271
2/2
✓ Branch 1 taken 14163 times.
✓ Branch 2 taken 109 times.
14272 if (put_bits_count(pbc) % 8 == 0) {
1272 // If the writer is aligned at this point,
1273 // memcpy can be used to improve performance.
1274 // This happens normally for CABAC.
1275 14163 flush_put_bits(pbc);
1276 14163 memcpy(put_bits_ptr(pbc), pos, rest);
1277 14163 skip_put_bytes(pbc, rest);
1278 } else {
1279 // If not, we have to copy manually.
1280 // rbsp_stop_one_bit forces us to special-case
1281 // the last byte.
1282 uint8_t temp;
1283 int i;
1284
1285
2/2
✓ Branch 0 taken 98762 times.
✓ Branch 1 taken 109 times.
98871 for (; rest > 4; rest -= 4, pos += 4)
1286 98762 put_bits32(pbc, AV_RB32(pos));
1287
1288
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 109 times.
274 for (; rest > 1; rest--, pos++)
1289 165 put_bits(pbc, 8, *pos);
1290
1291 109 rbsp_stop_one_bit:
1292
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1293
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 av_assert0(temp);
1295 110 i = ff_ctz(*pos);
1296 110 temp = temp >> i;
1297
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1298 110 put_bits(pbc, i, temp);
1299
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 18 times.
110 if (put_bits_count(pbc) % 8)
1300 92 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1301 }
1302
1303 14273 return 0;
1304 }
1305
1306 7963 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1307 CodedBitstreamUnit *unit,
1308 PutBitContext *pbc)
1309 {
1310 int err;
1311
1312
7/10
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1497 times.
✓ Branch 3 taken 5579 times.
✓ Branch 4 taken 302 times.
✓ Branch 5 taken 507 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
7963 switch (unit->type) {
1313 75 case H264_NAL_SPS:
1314 {
1315 75 H264RawSPS *sps = unit->content;
1316
1317 75 err = cbs_h264_write_sps(ctx, pbc, sps);
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (err < 0)
1319 return err;
1320
1321 75 err = cbs_h264_replace_sps(ctx, unit);
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (err < 0)
1323 return err;
1324 }
1325 75 break;
1326
1327 case H264_NAL_SPS_EXT:
1328 {
1329 H264RawSPSExtension *sps_ext = unit->content;
1330
1331 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1332 if (err < 0)
1333 return err;
1334 }
1335 break;
1336
1337 1497 case H264_NAL_PPS:
1338 {
1339 1497 H264RawPPS *pps = unit->content;
1340
1341 1497 err = cbs_h264_write_pps(ctx, pbc, pps);
1342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (err < 0)
1343 return err;
1344
1345 1497 err = cbs_h264_replace_pps(ctx, unit);
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (err < 0)
1347 return err;
1348 }
1349 1497 break;
1350
1351 5579 case H264_NAL_SLICE:
1352 case H264_NAL_IDR_SLICE:
1353 case H264_NAL_AUXILIARY_SLICE:
1354 {
1355 5579 H264RawSlice *slice = unit->content;
1356
1357 5579 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5579 times.
5579 if (err < 0)
1359 return err;
1360
1361
1/2
✓ Branch 0 taken 5579 times.
✗ Branch 1 not taken.
5579 if (slice->data) {
1362 5579 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1363 slice->data_size,
1364 slice->data_bit_start);
1365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5579 times.
5579 if (err < 0)
1366 return err;
1367 } else {
1368 // No slice data - that was just the header.
1369 // (Bitstream may be unaligned!)
1370 }
1371 }
1372 5579 break;
1373
1374 302 case H264_NAL_AUD:
1375 {
1376 302 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (err < 0)
1378 return err;
1379 }
1380 302 break;
1381
1382 507 case H264_NAL_SEI:
1383 {
1384 507 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
507 if (err < 0)
1386 return err;
1387 }
1388 507 break;
1389
1390 case H264_NAL_FILLER_DATA:
1391 {
1392 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1393 if (err < 0)
1394 return err;
1395 }
1396 break;
1397
1398 1 case H264_NAL_END_SEQUENCE:
1399 {
1400 1 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1402 return err;
1403 }
1404 1 break;
1405
1406 2 case H264_NAL_END_STREAM:
1407 {
1408 2 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1410 return err;
1411 }
1412 2 break;
1413
1414 default:
1415 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1416 "NAL unit type %"PRIu32".\n", unit->type);
1417 return AVERROR_PATCHWELCOME;
1418 }
1419
1420 7963 return 0;
1421 }
1422
1423 8137 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1424 CodedBitstreamUnit *unit,
1425 PutBitContext *pbc)
1426 {
1427 int err;
1428
1429
6/8
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 287 times.
✓ Branch 3 taken 6106 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1562 times.
✗ Branch 7 not taken.
8137 switch (unit->type) {
1430 60 case HEVC_NAL_VPS:
1431 {
1432 60 H265RawVPS *vps = unit->content;
1433
1434 60 err = cbs_h265_write_vps(ctx, pbc, vps);
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1436 return err;
1437
1438 60 err = cbs_h265_replace_vps(ctx, unit);
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1440 return err;
1441 }
1442 60 break;
1443
1444 62 case HEVC_NAL_SPS:
1445 {
1446 62 H265RawSPS *sps = unit->content;
1447
1448 62 err = cbs_h265_write_sps(ctx, pbc, sps);
1449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (err < 0)
1450 return err;
1451
1452 62 err = cbs_h265_replace_sps(ctx, unit);
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (err < 0)
1454 return err;
1455 }
1456 62 break;
1457
1458 287 case HEVC_NAL_PPS:
1459 {
1460 287 H265RawPPS *pps = unit->content;
1461
1462 287 err = cbs_h265_write_pps(ctx, pbc, pps);
1463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (err < 0)
1464 return err;
1465
1466 287 err = cbs_h265_replace_pps(ctx, unit);
1467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (err < 0)
1468 return err;
1469 }
1470 287 break;
1471
1472 6106 case HEVC_NAL_TRAIL_N:
1473 case HEVC_NAL_TRAIL_R:
1474 case HEVC_NAL_TSA_N:
1475 case HEVC_NAL_TSA_R:
1476 case HEVC_NAL_STSA_N:
1477 case HEVC_NAL_STSA_R:
1478 case HEVC_NAL_RADL_N:
1479 case HEVC_NAL_RADL_R:
1480 case HEVC_NAL_RASL_N:
1481 case HEVC_NAL_RASL_R:
1482 case HEVC_NAL_BLA_W_LP:
1483 case HEVC_NAL_BLA_W_RADL:
1484 case HEVC_NAL_BLA_N_LP:
1485 case HEVC_NAL_IDR_W_RADL:
1486 case HEVC_NAL_IDR_N_LP:
1487 case HEVC_NAL_CRA_NUT:
1488 {
1489 6106 H265RawSlice *slice = unit->content;
1490
1491 6106 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6106 times.
6106 if (err < 0)
1493 return err;
1494
1495
1/2
✓ Branch 0 taken 6106 times.
✗ Branch 1 not taken.
6106 if (slice->data) {
1496 6106 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1497 slice->data_size,
1498 slice->data_bit_start);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6106 times.
6106 if (err < 0)
1500 return err;
1501 } else {
1502 // No slice data - that was just the header.
1503 }
1504 }
1505 6106 break;
1506
1507 60 case HEVC_NAL_AUD:
1508 {
1509 60 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1511 return err;
1512 }
1513 60 break;
1514
1515 case HEVC_NAL_FD_NUT:
1516 {
1517 err = cbs_h265_write_filler(ctx, pbc, unit->content);
1518 if (err < 0)
1519 return err;
1520 }
1521 break;
1522
1523 1562 case HEVC_NAL_SEI_PREFIX:
1524 case HEVC_NAL_SEI_SUFFIX:
1525 {
1526 1562 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1527 1562 unit->type == HEVC_NAL_SEI_PREFIX);
1528
1529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1562 times.
1562 if (err < 0)
1530 return err;
1531 }
1532 1562 break;
1533
1534 default:
1535 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1536 "NAL unit type %"PRIu32".\n", unit->type);
1537 return AVERROR_PATCHWELCOME;
1538 }
1539
1540 8137 return 0;
1541 }
1542
1543 756 static int cbs_h264_discarded_nal_unit(CodedBitstreamContext *ctx,
1544 const CodedBitstreamUnit *unit,
1545 enum AVDiscard skip)
1546 {
1547 H264RawNALUnitHeader *header;
1548 H264RawSliceHeader *slice;
1549 int slice_type_i, slice_type_b, slice_type_si;
1550
1551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 756 times.
756 if (skip <= AVDISCARD_DEFAULT)
1552 return 0;
1553
1554 // keep non-VCL
1555
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 500 times.
756 if (unit->type != H264_NAL_SLICE &&
1556
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 4 times.
256 unit->type != H264_NAL_IDR_SLICE &&
1557
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 unit->type != H264_NAL_AUXILIARY_SLICE)
1558 252 return 0;
1559
1560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
504 if (skip >= AVDISCARD_ALL)
1561 return 1;
1562
1563
4/4
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 378 times.
✓ Branch 2 taken 125 times.
✓ Branch 3 taken 1 times.
504 if (skip >= AVDISCARD_NONKEY && unit->type != H264_NAL_IDR_SLICE)
1564 125 return 1;
1565
1566 379 header = (H264RawNALUnitHeader *)unit->content;
1567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379 times.
379 if (!header) {
1568 av_log(ctx->log_ctx, AV_LOG_WARNING,
1569 "h264 nal unit header is null, missing decompose?\n");
1570 return 0;
1571 }
1572
1573
3/4
✓ Branch 0 taken 379 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 196 times.
379 if (skip >= AVDISCARD_NONREF && !header->nal_ref_idc)
1574 183 return 1;
1575
1576 196 slice = (H264RawSliceHeader *)unit->content;
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (!slice) {
1578 av_log(ctx->log_ctx, AV_LOG_WARNING,
1579 "h264 slice header is null, missing decompose?\n");
1580 return 0;
1581 }
1582
1583 196 slice_type_i = slice->slice_type % 5 == 2;
1584 196 slice_type_b = slice->slice_type % 5 == 1;
1585 196 slice_type_si = slice->slice_type % 5 == 4;
1586
1587
4/4
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 71 times.
196 if (skip >= AVDISCARD_BIDIR && slice_type_b)
1588 60 return 1;
1589
5/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
136 if (skip >= AVDISCARD_NONINTRA && !slice_type_i && !slice_type_si)
1590 29 return 1;
1591
1592 107 return 0;
1593 }
1594
1595 536 static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx,
1596 const CodedBitstreamUnit *unit,
1597 enum AVDiscard skip)
1598 {
1599 H265RawSliceHeader *slice;
1600
1601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536 times.
536 if (skip <= AVDISCARD_DEFAULT)
1602 return 0;
1603
1604
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 204 times.
536 switch (unit->type) {
1605 4 case HEVC_NAL_BLA_W_LP:
1606 case HEVC_NAL_BLA_W_RADL:
1607 case HEVC_NAL_BLA_N_LP:
1608 case HEVC_NAL_IDR_W_RADL:
1609 case HEVC_NAL_IDR_N_LP:
1610 case HEVC_NAL_CRA_NUT:
1611 // IRAP slice
1612
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (skip < AVDISCARD_ALL)
1613 4 return 0;
1614 break;
1615
1616 328 case HEVC_NAL_TRAIL_R:
1617 case HEVC_NAL_TRAIL_N:
1618 case HEVC_NAL_TSA_N:
1619 case HEVC_NAL_TSA_R:
1620 case HEVC_NAL_STSA_N:
1621 case HEVC_NAL_STSA_R:
1622 case HEVC_NAL_RADL_N:
1623 case HEVC_NAL_RADL_R:
1624 case HEVC_NAL_RASL_N:
1625 case HEVC_NAL_RASL_R:
1626 // Slice
1627 328 break;
1628 204 default:
1629 // Don't discard non-slice nal.
1630 204 return 0;
1631 }
1632
1633
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 281 times.
328 if (skip >= AVDISCARD_NONKEY)
1634 47 return 1;
1635
1636 281 slice = (H265RawSliceHeader *)unit->content;
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 if (!slice) {
1638 av_log(ctx->log_ctx, AV_LOG_WARNING,
1639 "h265 slice header is null, missing decompose?\n");
1640 return 0;
1641 }
1642
1643
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 234 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
281 if (skip >= AVDISCARD_NONINTRA && slice->slice_type != HEVC_SLICE_I)
1644 47 return 1;
1645
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
234 if (skip >= AVDISCARD_BIDIR && slice->slice_type == HEVC_SLICE_B)
1646 47 return 1;
1647
1648
1/2
✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
187 if (skip >= AVDISCARD_NONREF) {
1649
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 163 times.
187 switch (unit->type) {
1650 24 case HEVC_NAL_TRAIL_N:
1651 case HEVC_NAL_TSA_N:
1652 case HEVC_NAL_STSA_N:
1653 case HEVC_NAL_RADL_N:
1654 case HEVC_NAL_RASL_N:
1655 case HEVC_NAL_VCL_N10:
1656 case HEVC_NAL_VCL_N12:
1657 case HEVC_NAL_VCL_N14:
1658 // non-ref
1659 24 return 1;
1660 163 default:
1661 163 break;
1662 }
1663 }
1664
1665 163 return 0;
1666 }
1667
1668 5777 static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
1669 CodedBitstreamUnit *unit,
1670 PutBitContext *pbc)
1671 {
1672 int err;
1673
1674
10/11
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 335 times.
✓ Branch 4 taken 391 times.
✓ Branch 5 taken 228 times.
✓ Branch 6 taken 27 times.
✓ Branch 7 taken 2588 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 2190 times.
✗ Branch 10 not taken.
5777 switch (unit->type) {
1675 1 case VVC_DCI_NUT:
1676 {
1677 1 H266RawDCI *dci = unit->content;
1678
1679 1 err = cbs_h266_write_dci(ctx, pbc, dci);
1680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1681 return err;
1682 }
1683 1 break;
1684 1 case VVC_OPI_NUT:
1685 {
1686 1 H266RawOPI *opi = unit->content;
1687
1688 1 err = cbs_h266_write_opi(ctx, pbc, opi);
1689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1690 return err;
1691 }
1692 1 break;
1693 4 case VVC_VPS_NUT:
1694 {
1695 4 H266RawVPS *vps = unit->content;
1696
1697 4 err = cbs_h266_write_vps(ctx, pbc, vps);
1698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1699 return err;
1700
1701 4 err = cbs_h266_replace_vps(ctx, unit);
1702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1703 return err;
1704 }
1705 4 break;
1706 335 case VVC_SPS_NUT:
1707 {
1708 335 H266RawSPS *sps = unit->content;
1709
1710 335 err = cbs_h266_write_sps(ctx, pbc, sps);
1711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1712 return err;
1713
1714 335 err = cbs_h266_replace_sps(ctx, unit);
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1716 return err;
1717 }
1718 335 break;
1719
1720 391 case VVC_PPS_NUT:
1721 {
1722 391 H266RawPPS *pps = unit->content;
1723
1724 391 err = cbs_h266_write_pps(ctx, pbc, pps);
1725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1726 return err;
1727
1728 391 err = cbs_h266_replace_pps(ctx, unit);
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1730 return err;
1731 }
1732 391 break;
1733
1734 228 case VVC_PREFIX_APS_NUT:
1735 case VVC_SUFFIX_APS_NUT:
1736 {
1737 228 err = cbs_h266_write_aps(ctx, pbc, unit->content,
1738 228 unit->type == VVC_PREFIX_APS_NUT);
1739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 if (err < 0)
1740 return err;
1741 }
1742 228 break;
1743 27 case VVC_PH_NUT:
1744 {
1745 27 H266RawPH *ph = unit->content;
1746 27 err = cbs_h266_write_ph(ctx, pbc, ph);
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1748 return err;
1749
1750 27 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1752 return err;
1753 }
1754 27 break;
1755
1756 2588 case VVC_TRAIL_NUT:
1757 case VVC_STSA_NUT:
1758 case VVC_RADL_NUT:
1759 case VVC_RASL_NUT:
1760 case VVC_IDR_W_RADL:
1761 case VVC_IDR_N_LP:
1762 case VVC_CRA_NUT:
1763 case VVC_GDR_NUT:
1764 {
1765 2588 H266RawSlice *slice = unit->content;
1766
1767 2588 err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1769 return err;
1770
1771
2/2
✓ Branch 0 taken 2103 times.
✓ Branch 1 taken 485 times.
2588 if (slice->header.sh_picture_header_in_slice_header_flag) {
1772 2103 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2103 times.
2103 if (err < 0)
1774 return err;
1775 }
1776
1777
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (slice->data) {
1778 2588 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1779 slice->data_size,
1780 slice->data_bit_start);
1781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1782 return err;
1783 } else {
1784 // No slice data - that was just the header.
1785 }
1786 }
1787 2588 break;
1788
1789 12 case VVC_AUD_NUT:
1790 {
1791 12 err = cbs_h266_write_aud(ctx, pbc, unit->content);
1792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (err < 0)
1793 return err;
1794 }
1795 12 break;
1796
1797 2190 case VVC_PREFIX_SEI_NUT:
1798 case VVC_SUFFIX_SEI_NUT:
1799 {
1800 2190 err = cbs_h266_write_sei(ctx, pbc, unit->content,
1801 2190 unit->type == VVC_PREFIX_SEI_NUT);
1802
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2190 times.
2190 if (err < 0)
1804 return err;
1805 }
1806 2190 break;
1807
1808 default:
1809 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1810 "NAL unit type %"PRIu32".\n", unit->type);
1811 return AVERROR_PATCHWELCOME;
1812 }
1813
1814 5777 return 0;
1815 }
1816
1817 21877 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
1818 CodedBitstreamUnitType type,
1819 int nal_unit_index)
1820 {
1821 // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1822
2/2
✓ Branch 0 taken 7327 times.
✓ Branch 1 taken 14550 times.
21877 if (nal_unit_index == 0) {
1823 // Assume that this is the first NAL unit in an access unit.
1824 7327 return 1;
1825 }
1826
2/2
✓ Branch 0 taken 5028 times.
✓ Branch 1 taken 9522 times.
14550 if (codec_id == AV_CODEC_ID_H264)
1827
4/4
✓ Branch 0 taken 5018 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 450 times.
✓ Branch 3 taken 4568 times.
5028 return type == H264_NAL_SPS || type == H264_NAL_PPS;
1828
2/2
✓ Branch 0 taken 5877 times.
✓ Branch 1 taken 3645 times.
9522 if (codec_id == AV_CODEC_ID_HEVC)
1829
6/6
✓ Branch 0 taken 5874 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5812 times.
✓ Branch 3 taken 62 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 5752 times.
5877 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1830
1/2
✓ Branch 0 taken 3645 times.
✗ Branch 1 not taken.
3645 if (codec_id == AV_CODEC_ID_VVC)
1831
4/4
✓ Branch 0 taken 2642 times.
✓ Branch 1 taken 1003 times.
✓ Branch 2 taken 438 times.
✓ Branch 3 taken 2204 times.
3645 return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
1832 return 0;
1833 }
1834
1835 7328 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1836 CodedBitstreamFragment *frag)
1837 {
1838 uint8_t *data;
1839 size_t max_size, dp, sp;
1840 int err, i, zero_run;
1841
1842
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
1843 // Data should already all have been written when we get here.
1844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21877 times.
21877 av_assert0(frag->units[i].data);
1845 }
1846
1847 7328 max_size = 0;
1848
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
1849 // Start code + content with worst-case emulation prevention.
1850 21877 max_size += 4 + frag->units[i].data_size * 3 / 2;
1851 }
1852
1853 7328 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!data)
1855 return AVERROR(ENOMEM);
1856
1857 7328 dp = 0;
1858
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
1859 21877 CodedBitstreamUnit *unit = &frag->units[i];
1860
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21877 times.
21877 if (unit->data_bit_padding > 0) {
1862 if (i < frag->nb_units - 1)
1863 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1864 "unaligned padding on non-final NAL unit.\n");
1865 else
1866 frag->data_bit_padding = unit->data_bit_padding;
1867 }
1868
1869
2/2
✓ Branch 1 taken 8350 times.
✓ Branch 2 taken 13527 times.
21877 if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1870 // zero_byte
1871 8350 data[dp++] = 0;
1872 }
1873 // start_code_prefix_one_3bytes
1874 21877 data[dp++] = 0;
1875 21877 data[dp++] = 0;
1876 21877 data[dp++] = 1;
1877
1878 21877 zero_run = 0;
1879
2/2
✓ Branch 0 taken 19230787 times.
✓ Branch 1 taken 21877 times.
19252664 for (sp = 0; sp < unit->data_size; sp++) {
1880
2/2
✓ Branch 0 taken 19223304 times.
✓ Branch 1 taken 7483 times.
19230787 if (zero_run < 2) {
1881
2/2
✓ Branch 0 taken 157971 times.
✓ Branch 1 taken 19065333 times.
19223304 if (unit->data[sp] == 0)
1882 157971 ++zero_run;
1883 else
1884 19065333 zero_run = 0;
1885 } else {
1886
2/2
✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 6158 times.
7483 if ((unit->data[sp] & ~3) == 0) {
1887 // emulation_prevention_three_byte
1888 1325 data[dp++] = 3;
1889 }
1890 7483 zero_run = unit->data[sp] == 0;
1891 }
1892 19230787 data[dp++] = unit->data[sp];
1893 }
1894 }
1895
1896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 av_assert0(dp <= max_size);
1897 7328 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (err)
1899 return err;
1900 7328 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1901
1902 7328 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1903 NULL, NULL, 0);
1904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!frag->data_ref) {
1905 av_freep(&data);
1906 return AVERROR(ENOMEM);
1907 }
1908
1909 7328 frag->data = data;
1910 7328 frag->data_size = dp;
1911
1912 7328 return 0;
1913 }
1914
1915 1 static av_cold void cbs_h264_flush(CodedBitstreamContext *ctx)
1916 {
1917 1 CodedBitstreamH264Context *h264 = ctx->priv_data;
1918
1919
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
33 for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1920 32 av_refstruct_unref(&h264->sps[i]);
1921
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1 times.
257 for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1922 256 av_refstruct_unref(&h264->pps[i]);
1923
1924 1 h264->active_sps = NULL;
1925 1 h264->active_pps = NULL;
1926 1 h264->last_slice_nal_unit_type = 0;
1927 1 }
1928
1929 47 static av_cold void cbs_h264_close(CodedBitstreamContext *ctx)
1930 {
1931 47 CodedBitstreamH264Context *h264 = ctx->priv_data;
1932 int i;
1933
1934 47 ff_h2645_packet_uninit(&h264->common.read_packet);
1935
1936
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 47 times.
1551 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1937 1504 av_refstruct_unref(&h264->sps[i]);
1938
2/2
✓ Branch 0 taken 12032 times.
✓ Branch 1 taken 47 times.
12079 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1939 12032 av_refstruct_unref(&h264->pps[i]);
1940 47 }
1941
1942 3 static av_cold void cbs_h265_flush(CodedBitstreamContext *ctx)
1943 {
1944 3 CodedBitstreamH265Context *h265 = ctx->priv_data;
1945
1946
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1947 48 av_refstruct_unref(&h265->vps[i]);
1948
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1949 48 av_refstruct_unref(&h265->sps[i]);
1950
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
195 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1951 192 av_refstruct_unref(&h265->pps[i]);
1952
1953 3 h265->active_vps = NULL;
1954 3 h265->active_sps = NULL;
1955 3 h265->active_pps = NULL;
1956 3 }
1957
1958 51 static av_cold void cbs_h265_close(CodedBitstreamContext *ctx)
1959 {
1960 51 CodedBitstreamH265Context *h265 = ctx->priv_data;
1961 int i;
1962
1963 51 ff_h2645_packet_uninit(&h265->common.read_packet);
1964
1965
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 51 times.
867 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1966 816 av_refstruct_unref(&h265->vps[i]);
1967
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 51 times.
867 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1968 816 av_refstruct_unref(&h265->sps[i]);
1969
2/2
✓ Branch 0 taken 3264 times.
✓ Branch 1 taken 51 times.
3315 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1970 3264 av_refstruct_unref(&h265->pps[i]);
1971 51 }
1972
1973 208 static av_cold void cbs_h266_flush(CodedBitstreamContext *ctx)
1974 {
1975 208 CodedBitstreamH266Context *h266 = ctx->priv_data;
1976
1977
2/2
✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 208 times.
3536 for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1978 3328 av_refstruct_unref(&h266->vps[i]);
1979
2/2
✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 208 times.
3536 for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1980 3328 av_refstruct_unref(&h266->sps[i]);
1981
2/2
✓ Branch 0 taken 13312 times.
✓ Branch 1 taken 208 times.
13520 for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1982 13312 av_refstruct_unref(&h266->pps[i]);
1983 208 av_refstruct_unref(&h266->ph_ref);
1984 208 }
1985
1986 208 static av_cold void cbs_h266_close(CodedBitstreamContext *ctx)
1987 {
1988 208 CodedBitstreamH266Context *h266 = ctx->priv_data;
1989
1990 208 cbs_h266_flush(ctx);
1991 208 ff_h2645_packet_uninit(&h266->common.read_packet);
1992 208 }
1993
1994 915 static void cbs_h264_free_sei(AVRefStructOpaque unused, void *content)
1995 {
1996 915 H264RawSEI *sei = content;
1997 915 ff_cbs_sei_free_message_list(&sei->message_list);
1998 915 }
1999
2000 static CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
2001 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
2002 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
2003
2004 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
2005
2006 CBS_UNIT_TYPES_INTERNAL_REF((H264_NAL_IDR_SLICE,
2007 H264_NAL_SLICE,
2008 H264_NAL_AUXILIARY_SLICE), H264RawSlice, data),
2009
2010 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
2011 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
2012 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
2013 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
2014
2015 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
2016
2017 CBS_UNIT_TYPE_END_OF_LIST
2018 };
2019
2020 1857 static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
2021 {
2022 1857 H265RawSEI *sei = content;
2023 1857 ff_cbs_sei_free_message_list(&sei->message_list);
2024 1857 }
2025
2026 static CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
2027 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_VPS, H265RawVPS, extension_data.data),
2028 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
2029 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
2030
2031 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
2032 CBS_UNIT_TYPE_POD(HEVC_NAL_FD_NUT, H265RawFiller),
2033
2034 // Slices of non-IRAP pictures.
2035 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_TRAIL_N, HEVC_NAL_RASL_R,
2036 H265RawSlice, data),
2037 // Slices of IRAP pictures.
2038 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_BLA_W_LP, HEVC_NAL_CRA_NUT,
2039 H265RawSlice, data),
2040
2041 CBS_UNIT_TYPES_COMPLEX((HEVC_NAL_SEI_PREFIX, HEVC_NAL_SEI_SUFFIX),
2042 H265RawSEI, cbs_h265_free_sei),
2043
2044 CBS_UNIT_TYPE_END_OF_LIST
2045 };
2046
2047 8787 static void cbs_h266_free_slice(AVRefStructOpaque unused, void *content)
2048 {
2049 8787 H266RawSlice *slice = content;
2050 8787 av_buffer_unref(&slice->data_ref);
2051 8787 av_refstruct_unref(&slice->sps);
2052 8787 av_refstruct_unref(&slice->pps);
2053 8787 av_refstruct_unref(&slice->ph_ref);
2054 8787 }
2055
2056
2057 3288 static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
2058 {
2059 3288 H266RawSEI *sei = content;
2060 3288 ff_cbs_sei_free_message_list(&sei->message_list);
2061 3288 }
2062
2063 static CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[] = {
2064 CBS_UNIT_TYPE_INTERNAL_REF(VVC_DCI_NUT, H266RawDCI, extension_data.data),
2065 CBS_UNIT_TYPE_INTERNAL_REF(VVC_OPI_NUT, H266RawOPI, extension_data.data),
2066 CBS_UNIT_TYPE_INTERNAL_REF(VVC_VPS_NUT, H266RawVPS, extension_data.data),
2067 {
2068 .nb_unit_types = 1,
2069 .unit_type.list[0] = VVC_SPS_NUT,
2070 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2071 .content_size = sizeof(H266RawSPS),
2072 .type.ref = {
2073 .nb_offsets = 2,
2074 .offsets = { offsetof(H266RawSPS, extension_data.data),
2075 offsetof(H266RawSPS, vui.extension_data.data) }
2076 },
2077 },
2078 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PPS_NUT, H266RawPPS, extension_data.data),
2079 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PREFIX_APS_NUT, H266RawAPS, extension_data.data),
2080 CBS_UNIT_TYPE_INTERNAL_REF(VVC_SUFFIX_APS_NUT, H266RawAPS, extension_data.data),
2081
2082 CBS_UNIT_TYPE_POD(VVC_PH_NUT , H266RawPH),
2083 CBS_UNIT_TYPE_POD(VVC_AUD_NUT, H266RawAUD),
2084
2085 CBS_UNIT_TYPES_COMPLEX((VVC_TRAIL_NUT, VVC_STSA_NUT, VVC_RADL_NUT),
2086 H266RawSlice, cbs_h266_free_slice),
2087 CBS_UNIT_TYPES_COMPLEX((VVC_RASL_NUT, VVC_IDR_W_RADL, VVC_IDR_N_LP),
2088 H266RawSlice, cbs_h266_free_slice),
2089 CBS_UNIT_TYPES_COMPLEX((VVC_CRA_NUT, VVC_GDR_NUT),
2090 H266RawSlice, cbs_h266_free_slice),
2091
2092 CBS_UNIT_TYPES_COMPLEX((VVC_PREFIX_SEI_NUT, VVC_SUFFIX_SEI_NUT),
2093 H266RawSEI, cbs_h266_free_sei),
2094
2095 CBS_UNIT_TYPE_END_OF_LIST
2096 };
2097
2098 const CodedBitstreamType ff_cbs_type_h264 = {
2099 .codec_id = AV_CODEC_ID_H264,
2100
2101 .priv_data_size = sizeof(CodedBitstreamH264Context),
2102
2103 .unit_types = cbs_h264_unit_types,
2104
2105 .split_fragment = &cbs_h2645_split_fragment,
2106 .read_unit = &cbs_h264_read_nal_unit,
2107 .write_unit = &cbs_h264_write_nal_unit,
2108 .discarded_unit = &cbs_h264_discarded_nal_unit,
2109 .assemble_fragment = &cbs_h2645_assemble_fragment,
2110
2111 .flush = &cbs_h264_flush,
2112 .close = &cbs_h264_close,
2113 };
2114
2115 const CodedBitstreamType ff_cbs_type_h265 = {
2116 .codec_id = AV_CODEC_ID_HEVC,
2117
2118 .priv_data_size = sizeof(CodedBitstreamH265Context),
2119
2120 .unit_types = cbs_h265_unit_types,
2121
2122 .split_fragment = &cbs_h2645_split_fragment,
2123 .read_unit = &cbs_h265_read_nal_unit,
2124 .write_unit = &cbs_h265_write_nal_unit,
2125 .discarded_unit = &cbs_h265_discarded_nal_unit,
2126 .assemble_fragment = &cbs_h2645_assemble_fragment,
2127
2128 .flush = &cbs_h265_flush,
2129 .close = &cbs_h265_close,
2130 };
2131
2132 const CodedBitstreamType ff_cbs_type_h266 = {
2133 .codec_id = AV_CODEC_ID_VVC,
2134
2135 .priv_data_size = sizeof(CodedBitstreamH266Context),
2136
2137 .unit_types = cbs_h266_unit_types,
2138
2139 .split_fragment = &cbs_h2645_split_fragment,
2140 .read_unit = &cbs_h266_read_nal_unit,
2141 .write_unit = &cbs_h266_write_nal_unit,
2142 .assemble_fragment = &cbs_h2645_assemble_fragment,
2143
2144 .flush = &cbs_h266_flush,
2145 .close = &cbs_h266_close,
2146 };
2147
2148 // Macro for the read/write pair.
2149 #define SEI_MESSAGE_RW(codec, name) \
2150 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2151 .write = cbs_ ## codec ## _write_ ## name ## _internal
2152
2153 static const SEIMessageTypeDescriptor cbs_sei_common_types[] = {
2154 {
2155 SEI_TYPE_FILLER_PAYLOAD,
2156 1, 1,
2157 sizeof(SEIRawFillerPayload),
2158 SEI_MESSAGE_RW(sei, filler_payload),
2159 },
2160 {
2161 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
2162 1, 1,
2163 sizeof(SEIRawUserDataRegistered),
2164 SEI_MESSAGE_RW(sei, user_data_registered),
2165 },
2166 {
2167 SEI_TYPE_USER_DATA_UNREGISTERED,
2168 1, 1,
2169 sizeof(SEIRawUserDataUnregistered),
2170 SEI_MESSAGE_RW(sei, user_data_unregistered),
2171 },
2172 {
2173 SEI_TYPE_FRAME_PACKING_ARRANGEMENT,
2174 1, 0,
2175 sizeof(SEIRawFramePackingArrangement),
2176 SEI_MESSAGE_RW(sei, frame_packing_arrangement),
2177 },
2178 {
2179 SEI_TYPE_DECODED_PICTURE_HASH,
2180 0, 1,
2181 sizeof(SEIRawDecodedPictureHash),
2182 SEI_MESSAGE_RW(sei, decoded_picture_hash),
2183 },
2184 {
2185 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
2186 1, 0,
2187 sizeof(SEIRawMasteringDisplayColourVolume),
2188 SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2189 },
2190 {
2191 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
2192 1, 0,
2193 sizeof(SEIRawContentLightLevelInfo),
2194 SEI_MESSAGE_RW(sei, content_light_level_info),
2195 },
2196 {
2197 SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS,
2198 1, 0,
2199 sizeof(SEIRawAlternativeTransferCharacteristics),
2200 SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2201 },
2202 {
2203 SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT,
2204 1, 0,
2205 sizeof(SEIRawAmbientViewingEnvironment),
2206 SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2207 },
2208 SEI_MESSAGE_TYPE_END,
2209 };
2210
2211 static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = {
2212 {
2213 SEI_TYPE_BUFFERING_PERIOD,
2214 1, 0,
2215 sizeof(H264RawSEIBufferingPeriod),
2216 SEI_MESSAGE_RW(h264, sei_buffering_period),
2217 },
2218 {
2219 SEI_TYPE_PIC_TIMING,
2220 1, 0,
2221 sizeof(H264RawSEIPicTiming),
2222 SEI_MESSAGE_RW(h264, sei_pic_timing),
2223 },
2224 {
2225 SEI_TYPE_PAN_SCAN_RECT,
2226 1, 0,
2227 sizeof(H264RawSEIPanScanRect),
2228 SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2229 },
2230 {
2231 SEI_TYPE_RECOVERY_POINT,
2232 1, 0,
2233 sizeof(H264RawSEIRecoveryPoint),
2234 SEI_MESSAGE_RW(h264, sei_recovery_point),
2235 },
2236 {
2237 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2238 1, 0,
2239 sizeof(H264RawFilmGrainCharacteristics),
2240 SEI_MESSAGE_RW(h264, film_grain_characteristics),
2241 },
2242 {
2243 SEI_TYPE_FRAME_PACKING_ARRANGEMENT,
2244 1, 0,
2245 sizeof(H264RawSEIFramePackingArrangement),
2246 SEI_MESSAGE_RW(h264, sei_frame_packing_arrangement),
2247 },
2248 {
2249 SEI_TYPE_DISPLAY_ORIENTATION,
2250 1, 0,
2251 sizeof(H264RawSEIDisplayOrientation),
2252 SEI_MESSAGE_RW(h264, sei_display_orientation),
2253 },
2254 SEI_MESSAGE_TYPE_END
2255 };
2256
2257 static const SEIMessageTypeDescriptor cbs_sei_h265_types[] = {
2258 {
2259 SEI_TYPE_BUFFERING_PERIOD,
2260 1, 0,
2261 sizeof(H265RawSEIBufferingPeriod),
2262 SEI_MESSAGE_RW(h265, sei_buffering_period),
2263 },
2264 {
2265 SEI_TYPE_PIC_TIMING,
2266 1, 0,
2267 sizeof(H265RawSEIPicTiming),
2268 SEI_MESSAGE_RW(h265, sei_pic_timing),
2269 },
2270 {
2271 SEI_TYPE_PAN_SCAN_RECT,
2272 1, 0,
2273 sizeof(H265RawSEIPanScanRect),
2274 SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2275 },
2276 {
2277 SEI_TYPE_RECOVERY_POINT,
2278 1, 0,
2279 sizeof(H265RawSEIRecoveryPoint),
2280 SEI_MESSAGE_RW(h265, sei_recovery_point),
2281 },
2282 {
2283 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2284 1, 0,
2285 sizeof(H265RawFilmGrainCharacteristics),
2286 SEI_MESSAGE_RW(h265, film_grain_characteristics),
2287 },
2288 {
2289 SEI_TYPE_DISPLAY_ORIENTATION,
2290 1, 0,
2291 sizeof(H265RawSEIDisplayOrientation),
2292 SEI_MESSAGE_RW(h265, sei_display_orientation),
2293 },
2294 {
2295 SEI_TYPE_ACTIVE_PARAMETER_SETS,
2296 1, 0,
2297 sizeof(H265RawSEIActiveParameterSets),
2298 SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2299 },
2300 {
2301 SEI_TYPE_DECODED_PICTURE_HASH,
2302 0, 1,
2303 sizeof(H265RawSEIDecodedPictureHash),
2304 SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2305 },
2306 {
2307 SEI_TYPE_TIME_CODE,
2308 1, 0,
2309 sizeof(H265RawSEITimeCode),
2310 SEI_MESSAGE_RW(h265, sei_time_code),
2311 },
2312 {
2313 SEI_TYPE_ALPHA_CHANNEL_INFO,
2314 1, 0,
2315 sizeof(H265RawSEIAlphaChannelInfo),
2316 SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2317 },
2318 {
2319 SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO,
2320 1, 0,
2321 sizeof(H265RawSEI3DReferenceDisplaysInfo),
2322 SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
2323 },
2324 SEI_MESSAGE_TYPE_END
2325 };
2326
2327 static const SEIMessageTypeDescriptor cbs_sei_h266_types[] = {
2328 SEI_MESSAGE_TYPE_END
2329 };
2330
2331 static const SEIMessageTypeDescriptor cbs_sei_h274_types[] = {
2332 {
2333 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2334 1, 0,
2335 sizeof(SEIRawFilmGrainCharacteristics),
2336 SEI_MESSAGE_RW(sei, film_grain_characteristics),
2337 },
2338 {
2339 SEI_TYPE_DISPLAY_ORIENTATION,
2340 1, 0,
2341 sizeof(SEIRawDisplayOrientation),
2342 SEI_MESSAGE_RW(sei, display_orientation)
2343 },
2344 {
2345 SEI_TYPE_FRAME_FIELD_INFO,
2346 1, 0,
2347 sizeof(SEIRawFrameFieldInformation),
2348 SEI_MESSAGE_RW(sei, frame_field_information)
2349 },
2350 SEI_MESSAGE_TYPE_END,
2351 };
2352
2353 14638 const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx,
2354 int payload_type)
2355 {
2356 const SEIMessageTypeDescriptor *codec_list;
2357 int i;
2358
2359
3/4
✓ Branch 0 taken 1989 times.
✓ Branch 1 taken 4981 times.
✓ Branch 2 taken 7668 times.
✗ Branch 3 not taken.
14638 switch (ctx->codec->codec_id) {
2360 1989 case AV_CODEC_ID_H264:
2361 1989 codec_list = cbs_sei_h264_types;
2362 1989 break;
2363 4981 case AV_CODEC_ID_H265:
2364 4981 codec_list = cbs_sei_h265_types;
2365 4981 break;
2366 7668 case AV_CODEC_ID_H266:
2367 7668 codec_list = cbs_sei_h266_types;
2368 7668 break;
2369 default:
2370 return NULL;
2371 }
2372
2373
2/2
✓ Branch 0 taken 41710 times.
✓ Branch 1 taken 7827 times.
49537 for (i = 0; codec_list[i].type >= 0; i++) {
2374
2/2
✓ Branch 0 taken 6811 times.
✓ Branch 1 taken 34899 times.
41710 if (codec_list[i].type == payload_type)
2375 6811 return &codec_list[i];
2376 }
2377
2378
2/2
✓ Branch 0 taken 7668 times.
✓ Branch 1 taken 159 times.
7827 if (ctx->codec->codec_id == AV_CODEC_ID_H266) {
2379
2/2
✓ Branch 0 taken 23004 times.
✓ Branch 1 taken 7646 times.
30650 for (i = 0; cbs_sei_h274_types[i].type >= 0; i++) {
2380
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22982 times.
23004 if (cbs_sei_h274_types[i].type == payload_type)
2381 22 return &cbs_sei_h274_types[i];
2382 }
2383 }
2384
2385
2/2
✓ Branch 0 taken 39747 times.
✓ Branch 1 taken 269 times.
40016 for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2386
2/2
✓ Branch 0 taken 7536 times.
✓ Branch 1 taken 32211 times.
39747 if (cbs_sei_common_types[i].type == payload_type)
2387 7536 return &cbs_sei_common_types[i];
2388 }
2389
2390 269 return NULL;
2391 }
2392