FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2024-06-18 05:56:57
Exec Total Coverage
Lines: 656 933 70.3%
Functions: 36 38 94.7%
Branches: 390 650 60.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 #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 "refstruct.h"
32 #include "vvc.h"
33
34 #include "hevc/hevc.h"
35
36 293449 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 293449 times.
293449 CBS_TRACE_READ_START();
45
46
2/2
✓ Branch 1 taken 10652 times.
✓ Branch 2 taken 282797 times.
293449 max_length = FFMIN(get_bits_left(gbc), 32);
47
48
1/2
✓ Branch 0 taken 293449 times.
✗ Branch 1 not taken.
293449 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293449 times.
293449 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 293449 leading_zeroes = max_length - 1 - av_log2(leading_bits);
62 293449 skip_bits_long(gbc, leading_zeroes);
63
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 293449 times.
293449 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 293449 value = get_bits_long(gbc, leading_zeroes + 1) - 1;
71
72
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 293449 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
293449 CBS_TRACE_READ_END();
73
74
2/4
✓ Branch 0 taken 293449 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 293449 times.
293449 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 293449 *write_to = value;
82 293449 return 0;
83 }
84
85 97422 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 97422 times.
97422 CBS_TRACE_READ_START();
95
96
2/2
✓ Branch 1 taken 8315 times.
✓ Branch 2 taken 89107 times.
97422 max_length = FFMIN(get_bits_left(gbc), 32);
97
98
1/2
✓ Branch 0 taken 97422 times.
✗ Branch 1 not taken.
97422 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97422 times.
97422 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 97422 leading_zeroes = max_length - 1 - av_log2(leading_bits);
112 97422 skip_bits_long(gbc, leading_zeroes);
113
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 97422 times.
97422 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 97422 unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
121
122
2/2
✓ Branch 0 taken 53275 times.
✓ Branch 1 taken 44147 times.
97422 if (unsigned_value & 1)
123 53275 value = -(int32_t)(unsigned_value / 2);
124 else
125 44147 value = unsigned_value / 2;
126
127
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 97422 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
97422 CBS_TRACE_READ_END();
128
129
2/4
✓ Branch 0 taken 97422 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97422 times.
97422 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 97422 *write_to = value;
137 97422 return 0;
138 }
139
140 154844 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 151775 times.
✓ Branch 1 taken 3069 times.
154844 CBS_TRACE_WRITE_START();
148
149
2/4
✓ Branch 0 taken 154844 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 154844 times.
154844 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 154844 times.
154844 av_assert0(value != UINT32_MAX);
156
157 154844 len = av_log2(value + 1);
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154844 times.
154844 if (put_bits_left(pbc) < 2 * len + 1)
159 return AVERROR(ENOSPC);
160
161 154844 put_bits(pbc, len, 0);
162
1/2
✓ Branch 0 taken 154844 times.
✗ Branch 1 not taken.
154844 if (len + 1 < 32)
163 154844 put_bits(pbc, len + 1, value + 1);
164 else
165 put_bits32(pbc, value + 1);
166
167
3/4
✓ Branch 0 taken 151775 times.
✓ Branch 1 taken 3069 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 151775 times.
154844 CBS_TRACE_WRITE_END();
168
169 154844 return 0;
170 }
171
172 84001 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 83431 times.
✓ Branch 1 taken 570 times.
84001 CBS_TRACE_WRITE_START();
181
182
2/4
✓ Branch 0 taken 84001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84001 times.
84001 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 84001 times.
84001 av_assert0(value != INT32_MIN);
189
190
2/2
✓ Branch 0 taken 19170 times.
✓ Branch 1 taken 64831 times.
84001 if (value == 0)
191 19170 uvalue = 0;
192
2/2
✓ Branch 0 taken 36505 times.
✓ Branch 1 taken 28326 times.
64831 else if (value > 0)
193 36505 uvalue = 2 * (uint32_t)value - 1;
194 else
195 28326 uvalue = 2 * (uint32_t)-value;
196
197 84001 len = av_log2(uvalue + 1);
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84001 times.
84001 if (put_bits_left(pbc) < 2 * len + 1)
199 return AVERROR(ENOSPC);
200
201 84001 put_bits(pbc, len, 0);
202
1/2
✓ Branch 0 taken 84001 times.
✗ Branch 1 not taken.
84001 if (len + 1 < 32)
203 84001 put_bits(pbc, len + 1, uvalue + 1);
204 else
205 put_bits32(pbc, uvalue + 1);
206
207
3/4
✓ Branch 0 taken 83431 times.
✓ Branch 1 taken 570 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83431 times.
84001 CBS_TRACE_WRITE_END();
208
209 84001 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 5 static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
215 int cur_pos)
216 {
217 5 int bits_left = payload_size * 8 - cur_pos;
218
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 return (bits_left > 0 &&
219 (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 28746 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
329 {
330 28746 int bits_left = get_bits_left(gbc);
331
2/2
✓ Branch 0 taken 21483 times.
✓ Branch 1 taken 7263 times.
28746 if (bits_left > 8)
332 21483 return 1;
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7263 times.
7263 if (bits_left == 0)
334 return 0;
335
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7256 times.
7263 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
336 7 return 1;
337 7256 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 = ff_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 11763 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 36259 times.
✓ Branch 1 taken 11763 times.
48022 for (i = 0; i < packet->nb_nals; i++) {
497 36259 const H2645NAL *nal = &packet->nals[i];
498 AVBufferRef *ref;
499 36259 size_t size = nal->size;
500 36259 enum AVCodecID codec_id = ctx->codec->codec_id;
501
502
3/4
✓ Branch 0 taken 18602 times.
✓ Branch 1 taken 17657 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18602 times.
36259 if (codec_id != AV_CODEC_ID_VVC && nal->nuh_layer_id > 0)
503 continue;
504
505 // Remove trailing zeroes.
506
3/4
✓ Branch 0 taken 44106 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7847 times.
✓ Branch 3 taken 36259 times.
44106 while (size > 0 && nal->data[size - 1] == 0)
507 7847 --size;
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36259 times.
36259 if (size == 0) {
509 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
510 continue;
511 }
512
513 72518 ref = (nal->data == nal->raw_data) ? frag->data_ref
514
2/2
✓ Branch 0 taken 34968 times.
✓ Branch 1 taken 1291 times.
36259 : packet->rbsp.rbsp_buffer_ref;
515
516 36259 err = ff_cbs_append_unit_data(frag, nal->type,
517 36259 (uint8_t*)nal->data, size, ref);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36259 times.
36259 if (err < 0)
519 return err;
520 }
521
522 11763 return 0;
523 }
524
525 11754 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
526 CodedBitstreamFragment *frag,
527 int header)
528 {
529 11754 enum AVCodecID codec_id = ctx->codec->codec_id;
530 11754 CodedBitstreamH2645Context *priv = ctx->priv_data;
531 GetByteContext gbc;
532 int err;
533
534
2/4
✓ Branch 0 taken 11754 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11754 times.
11754 av_assert0(frag->data && frag->nb_units == 0);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11754 times.
11754 if (frag->data_size == 0)
536 return 0;
537
538
5/6
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 11601 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
11763 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
539 // AVCC header.
540 size_t size, start, end;
541 int i, count, version;
542
543 9 priv->mp4 = 1;
544
545 9 bytestream2_init(&gbc, frag->data, frag->data_size);
546
547
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(&gbc) < 6)
548 return AVERROR_INVALIDDATA;
549
550 9 version = bytestream2_get_byte(&gbc);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (version != 1) {
552 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
553 "first byte %u.\n", version);
554 return AVERROR_INVALIDDATA;
555 }
556
557 9 bytestream2_skip(&gbc, 3);
558 9 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
559
560 // SPS array.
561 9 count = bytestream2_get_byte(&gbc) & 0x1f;
562 9 start = bytestream2_tell(&gbc);
563
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 for (i = 0; i < count; i++) {
564
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
565 return AVERROR_INVALIDDATA;
566 8 size = bytestream2_get_be16(&gbc);
567
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(&gbc) < size)
568 return AVERROR_INVALIDDATA;
569 8 bytestream2_skip(&gbc, size);
570 }
571 9 end = bytestream2_tell(&gbc);
572
573 9 err = ff_h2645_packet_split(&priv->read_packet,
574 9 frag->data + start, end - start,
575 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0) {
577 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
578 return err;
579 }
580 9 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0)
582 return err;
583
584 // PPS array.
585 9 count = bytestream2_get_byte(&gbc);
586 9 start = bytestream2_tell(&gbc);
587
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 for (i = 0; i < count; i++) {
588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
589 return AVERROR_INVALIDDATA;
590 8 size = bytestream2_get_be16(&gbc);
591
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(&gbc) < size)
592 return AVERROR_INVALIDDATA;
593 8 bytestream2_skip(&gbc, size);
594 }
595 9 end = bytestream2_tell(&gbc);
596
597 9 err = ff_h2645_packet_split(&priv->read_packet,
598 9 frag->data + start, end - start,
599 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0) {
601 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
602 return err;
603 }
604 9 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (err < 0)
606 return err;
607
608
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
9 if (bytestream2_get_bytes_left(&gbc) > 0) {
609 2 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
610 "header.\n", bytestream2_get_bytes_left(&gbc));
611 }
612
613
3/6
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 11601 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 144 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11745 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
614 // HVCC header.
615 size_t size, start, end;
616 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
617
618 priv->mp4 = 1;
619
620 bytestream2_init(&gbc, frag->data, frag->data_size);
621
622 if (bytestream2_get_bytes_left(&gbc) < 23)
623 return AVERROR_INVALIDDATA;
624
625 version = bytestream2_get_byte(&gbc);
626 if (version != 1) {
627 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
628 "first byte %u.\n", version);
629 return AVERROR_INVALIDDATA;
630 }
631
632 bytestream2_skip(&gbc, 20);
633 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
634
635 nb_arrays = bytestream2_get_byte(&gbc);
636 for (i = 0; i < nb_arrays; i++) {
637 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
638 nb_nals = bytestream2_get_be16(&gbc);
639
640 start = bytestream2_tell(&gbc);
641 for (j = 0; j < nb_nals; j++) {
642 if (bytestream2_get_bytes_left(&gbc) < 2)
643 return AVERROR_INVALIDDATA;
644 size = bytestream2_get_be16(&gbc);
645 if (bytestream2_get_bytes_left(&gbc) < size)
646 return AVERROR_INVALIDDATA;
647 bytestream2_skip(&gbc, size);
648 }
649 end = bytestream2_tell(&gbc);
650
651 err = ff_h2645_packet_split(&priv->read_packet,
652 frag->data + start, end - start,
653 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
654 if (err < 0) {
655 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
656 "HVCC array %d (%d NAL units of type %d).\n",
657 i, nb_nals, nal_unit_type);
658 return err;
659 }
660 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
661 if (err < 0)
662 return err;
663 }
664
665
3/6
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 11601 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 144 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11745 } else if(header && frag->data[0] && codec_id == AV_CODEC_ID_VVC) {
666 // VVCC header.
667 int ptl_present_flag, num_arrays;
668 int b, i, j;
669
670 priv->mp4 = 1;
671
672 bytestream2_init(&gbc, frag->data, frag->data_size);
673
674 b = bytestream2_get_byte(&gbc);
675 priv->nal_length_size = ((b >> 1) & 3) + 1;
676 ptl_present_flag = b & 1;
677
678 if(ptl_present_flag) {
679 int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
680 num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
681 bytestream2_skip(&gbc, 1);
682
683 // begin VvcPTLRecord(num_sublayers);
684 num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
685 bytestream2_skip(&gbc, 2 + num_bytes_constraint_info);
686 if(num_sublayers > 1) {
687 int count_present_flags = 0;
688 b = bytestream2_get_byte(&gbc);
689 for(i = num_sublayers - 2; i >= 0; i--) {
690 if((b >> (7 - (num_sublayers - 2 - i))) & 0x01)
691 count_present_flags++;
692 }
693 bytestream2_skip(&gbc, count_present_flags);
694 }
695 num_sub_profiles = bytestream2_get_byte(&gbc);
696 bytestream2_skip(&gbc, num_sub_profiles * 4);
697 // end VvcPTLRecord(num_sublayers);
698
699 bytestream2_skip(&gbc, 3 * 2);
700 }
701
702 num_arrays = bytestream2_get_byte(&gbc);
703 for(j = 0; j < num_arrays; j++) {
704 size_t start, end, size;
705 int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
706 unsigned int num_nalus = 1;
707 if(nal_unit_type != VVC_DCI_NUT && nal_unit_type != VVC_OPI_NUT)
708 num_nalus = bytestream2_get_be16(&gbc);
709
710 start = bytestream2_tell(&gbc);
711 for(i = 0; i < num_nalus; i++) {
712 if (bytestream2_get_bytes_left(&gbc) < 2)
713 return AVERROR_INVALIDDATA;
714 size = bytestream2_get_be16(&gbc);
715 if (bytestream2_get_bytes_left(&gbc) < size)
716 return AVERROR_INVALIDDATA;
717 bytestream2_skip(&gbc, size);
718 }
719 end = bytestream2_tell(&gbc);
720
721 err = ff_h2645_packet_split(&priv->read_packet,
722 frag->data + start, end - start,
723 ctx->log_ctx, 1, 2, AV_CODEC_ID_VVC, 1, 1);
724 if (err < 0) {
725 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
726 "VVCC array %d (%d NAL units of type %d).\n",
727 i, num_nalus, nal_unit_type);
728 return err;
729 }
730 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
731 if (err < 0)
732 return err;
733 }
734 } else {
735 // Annex B, or later MP4 with already-known parameters.
736
737 11745 err = ff_h2645_packet_split(&priv->read_packet,
738 11745 frag->data, frag->data_size,
739 ctx->log_ctx,
740 priv->mp4, priv->nal_length_size,
741 codec_id, 1, 1);
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11745 times.
11745 if (err < 0)
743 return err;
744
745 11745 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11745 times.
11745 if (err < 0)
747 return err;
748 }
749
750 11754 return 0;
751 }
752
753 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
754 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
755 CodedBitstreamUnit *unit) \
756 { \
757 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
758 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
759 unsigned int id = ps_var->id_element; \
760 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
761 if (err < 0) \
762 return err; \
763 if (priv->ps_var[id] == priv->active_ ## ps_var) \
764 priv->active_ ## ps_var = NULL ; \
765 av_assert0(unit->content_ref); \
766 ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
767 return 0; \
768 }
769
770
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 146 times.
✓ Branch 3 taken 106 times.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 146 times.
146 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
771
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2990 times.
✓ Branch 3 taken 2936 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2990 times.
2990 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
772
3/6
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 112 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 112 times.
112 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
773
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 116 times.
116 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
774
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 566 times.
✓ Branch 3 taken 386 times.
✓ Branch 4 taken 180 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 566 times.
566 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
775
776 #define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
777 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
778 CodedBitstreamUnit *unit) \
779 { \
780 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
781 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
782 unsigned int id = ps_var->id_element; \
783 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
784 if (err < 0) \
785 return err; \
786 av_assert0(unit->content_ref); \
787 ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
788 return 0; \
789 }
790
791
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
14 cbs_h266_replace_ps(6, VPS, vps, vps_video_parameter_set_id)
792
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1187 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1187 times.
1187 cbs_h266_replace_ps(6, SPS, sps, sps_seq_parameter_set_id)
793
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1465 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1465 times.
1465 cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
794
795 8127 static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
796 CodedBitstreamUnit *unit,
797 H266RawPictureHeader *ph)
798 {
799 8127 CodedBitstreamH266Context *h266 = ctx->priv_data;
800 int err;
801
802 8127 err = ff_cbs_make_unit_refcounted(ctx, unit);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8127 times.
8127 if (err < 0)
804 return err;
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8127 times.
8127 av_assert0(unit->content_ref);
806 8127 ff_refstruct_replace(&h266->ph_ref, unit->content_ref);
807 8127 h266->ph = ph;
808 8127 return 0;
809 }
810
811 9230 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
812 CodedBitstreamUnit *unit)
813 {
814 GetBitContext gbc;
815 int err;
816
817 9230 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9230 times.
9230 if (err < 0)
819 return err;
820
821 9230 err = ff_cbs_alloc_unit_content(ctx, unit);
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9230 times.
9230 if (err < 0)
823 return err;
824
825
7/9
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1495 times.
✓ Branch 3 taken 5973 times.
✓ Branch 4 taken 739 times.
✓ Branch 5 taken 913 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
9230 switch (unit->type) {
826 73 case H264_NAL_SPS:
827 {
828 73 H264RawSPS *sps = unit->content;
829
830 73 err = cbs_h264_read_sps(ctx, &gbc, sps);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
832 return err;
833
834 73 err = cbs_h264_replace_sps(ctx, unit);
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
836 return err;
837 }
838 73 break;
839
840 case H264_NAL_SPS_EXT:
841 {
842 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
843 if (err < 0)
844 return err;
845 }
846 break;
847
848 1495 case H264_NAL_PPS:
849 {
850 1495 H264RawPPS *pps = unit->content;
851
852 1495 err = cbs_h264_read_pps(ctx, &gbc, pps);
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
854 return err;
855
856 1495 err = cbs_h264_replace_pps(ctx, unit);
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
858 return err;
859 }
860 1495 break;
861
862 5973 case H264_NAL_SLICE:
863 case H264_NAL_IDR_SLICE:
864 case H264_NAL_AUXILIARY_SLICE:
865 {
866 5973 H264RawSlice *slice = unit->content;
867 int pos, len;
868
869 5973 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
5973 if (err < 0)
871 return err;
872
873
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5973 times.
5973 if (!cbs_h2645_read_more_rbsp_data(&gbc))
874 return AVERROR_INVALIDDATA;
875
876 5973 pos = get_bits_count(&gbc);
877 5973 len = unit->data_size;
878
879 5973 slice->data_size = len - pos / 8;
880 5973 slice->data_ref = av_buffer_ref(unit->data_ref);
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
5973 if (!slice->data_ref)
882 return AVERROR(ENOMEM);
883 5973 slice->data = unit->data + pos / 8;
884 5973 slice->data_bit_start = pos % 8;
885 }
886 5973 break;
887
888 739 case H264_NAL_AUD:
889 {
890 739 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (err < 0)
892 return err;
893 }
894 739 break;
895
896 913 case H264_NAL_SEI:
897 {
898 913 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 913 times.
913 if (err < 0)
900 return err;
901 }
902 913 break;
903
904 34 case H264_NAL_FILLER_DATA:
905 {
906 34 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
907
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
34 if (err < 0)
908 1 return err;
909 }
910 33 break;
911
912 3 case H264_NAL_END_SEQUENCE:
913 case H264_NAL_END_STREAM:
914 {
915 3 err = (unit->type == H264_NAL_END_SEQUENCE ?
916
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 cbs_h264_read_end_of_sequence :
917 3 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0)
919 return err;
920 }
921 3 break;
922
923 default:
924 return AVERROR(ENOSYS);
925 }
926
927 9229 return 0;
928 }
929
930 9372 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
931 CodedBitstreamUnit *unit)
932 {
933 GetBitContext gbc;
934 int err;
935
936 9372 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9372 times.
9372 if (err < 0)
938 return err;
939
940 9372 err = ff_cbs_alloc_unit_content(ctx, unit);
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9372 times.
9372 if (err < 0)
942 return err;
943
944
6/7
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 283 times.
✓ Branch 3 taken 7188 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 1727 times.
✗ Branch 6 not taken.
9372 switch (unit->type) {
945 56 case HEVC_NAL_VPS:
946 {
947 56 H265RawVPS *vps = unit->content;
948
949 56 err = cbs_h265_read_vps(ctx, &gbc, vps);
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
951 return err;
952
953 56 err = cbs_h265_replace_vps(ctx, unit);
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
955 return err;
956 }
957 56 break;
958 58 case HEVC_NAL_SPS:
959 {
960 58 H265RawSPS *sps = unit->content;
961
962 58 err = cbs_h265_read_sps(ctx, &gbc, sps);
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
964 return err;
965
966 58 err = cbs_h265_replace_sps(ctx, unit);
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
968 return err;
969 }
970 58 break;
971
972 283 case HEVC_NAL_PPS:
973 {
974 283 H265RawPPS *pps = unit->content;
975
976 283 err = cbs_h265_read_pps(ctx, &gbc, pps);
977
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
978 return err;
979
980 283 err = cbs_h265_replace_pps(ctx, unit);
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
982 return err;
983 }
984 283 break;
985
986 7188 case HEVC_NAL_TRAIL_N:
987 case HEVC_NAL_TRAIL_R:
988 case HEVC_NAL_TSA_N:
989 case HEVC_NAL_TSA_R:
990 case HEVC_NAL_STSA_N:
991 case HEVC_NAL_STSA_R:
992 case HEVC_NAL_RADL_N:
993 case HEVC_NAL_RADL_R:
994 case HEVC_NAL_RASL_N:
995 case HEVC_NAL_RASL_R:
996 case HEVC_NAL_BLA_W_LP:
997 case HEVC_NAL_BLA_W_RADL:
998 case HEVC_NAL_BLA_N_LP:
999 case HEVC_NAL_IDR_W_RADL:
1000 case HEVC_NAL_IDR_N_LP:
1001 case HEVC_NAL_CRA_NUT:
1002 {
1003 7188 H265RawSlice *slice = unit->content;
1004 int pos, len;
1005
1006 7188 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7188 times.
7188 if (err < 0)
1008 return err;
1009
1010
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7188 times.
7188 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1011 return AVERROR_INVALIDDATA;
1012
1013 7188 pos = get_bits_count(&gbc);
1014 7188 len = unit->data_size;
1015
1016 7188 slice->data_size = len - pos / 8;
1017 7188 slice->data_ref = av_buffer_ref(unit->data_ref);
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7188 times.
7188 if (!slice->data_ref)
1019 return AVERROR(ENOMEM);
1020 7188 slice->data = unit->data + pos / 8;
1021 7188 slice->data_bit_start = pos % 8;
1022 }
1023 7188 break;
1024
1025 60 case HEVC_NAL_AUD:
1026 {
1027 60 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1029 return err;
1030 }
1031 60 break;
1032
1033 1727 case HEVC_NAL_SEI_PREFIX:
1034 case HEVC_NAL_SEI_SUFFIX:
1035 {
1036 1727 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1037 1727 unit->type == HEVC_NAL_SEI_PREFIX);
1038
1039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1727 times.
1727 if (err < 0)
1040 return err;
1041 }
1042 1727 break;
1043
1044 default:
1045 return AVERROR(ENOSYS);
1046 }
1047
1048 9372 return 0;
1049 }
1050
1051 14080 static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
1052 CodedBitstreamUnit *unit)
1053 {
1054 GetBitContext gbc;
1055 int err;
1056
1057 14080 err = init_get_bits8(&gbc, unit->data, unit->data_size);
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14080 times.
14080 if (err < 0)
1059 return err;
1060
1061 14080 err = ff_cbs_alloc_unit_content(ctx, unit);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14080 times.
14080 if (err < 0)
1063 return err;
1064
1065
10/11
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 852 times.
✓ Branch 4 taken 1074 times.
✓ Branch 5 taken 495 times.
✓ Branch 6 taken 175 times.
✓ Branch 7 taken 8301 times.
✓ Branch 8 taken 45 times.
✓ Branch 9 taken 3124 times.
✗ Branch 10 not taken.
14080 switch (unit->type) {
1066 3 case VVC_DCI_NUT:
1067 {
1068 3 err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1069
1070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0)
1071 return err;
1072 }
1073 3 break;
1074 1 case VVC_OPI_NUT:
1075 {
1076 1 err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1077
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1079 return err;
1080 }
1081 1 break;
1082 10 case VVC_VPS_NUT:
1083 {
1084 10 H266RawVPS *vps = unit->content;
1085
1086 10 err = cbs_h266_read_vps(ctx, &gbc, vps);
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (err < 0)
1088 return err;
1089
1090 10 err = cbs_h266_replace_vps(ctx, unit);
1091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (err < 0)
1092 return err;
1093 }
1094 10 break;
1095 852 case VVC_SPS_NUT:
1096 {
1097 852 H266RawSPS *sps = unit->content;
1098
1099 852 err = cbs_h266_read_sps(ctx, &gbc, sps);
1100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 852 times.
852 if (err < 0)
1101 return err;
1102
1103 852 err = cbs_h266_replace_sps(ctx, unit);
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 852 times.
852 if (err < 0)
1105 return err;
1106 }
1107 852 break;
1108
1109 1074 case VVC_PPS_NUT:
1110 {
1111 1074 H266RawPPS *pps = unit->content;
1112
1113 1074 err = cbs_h266_read_pps(ctx, &gbc, pps);
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1074 times.
1074 if (err < 0)
1115 return err;
1116
1117 1074 err = cbs_h266_replace_pps(ctx, unit);
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1074 times.
1074 if (err < 0)
1119 return err;
1120 }
1121 1074 break;
1122
1123 495 case VVC_PREFIX_APS_NUT:
1124 case VVC_SUFFIX_APS_NUT:
1125 {
1126 495 err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1127 495 unit->type == VVC_PREFIX_APS_NUT);
1128
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495 times.
495 if (err < 0)
1130 return err;
1131 }
1132 495 break;
1133 175 case VVC_PH_NUT:
1134 {
1135 175 H266RawPH *ph = unit->content;
1136 175 err = cbs_h266_read_ph(ctx, &gbc, ph);
1137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (err < 0)
1138 return err;
1139 175 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (err < 0)
1141 return err;
1142 }
1143 175 break;
1144
1145 8301 case VVC_TRAIL_NUT:
1146 case VVC_STSA_NUT:
1147 case VVC_RADL_NUT:
1148 case VVC_RASL_NUT:
1149 case VVC_IDR_W_RADL:
1150 case VVC_IDR_N_LP:
1151 case VVC_CRA_NUT:
1152 case VVC_GDR_NUT:
1153 {
1154 8301 H266RawSlice *slice = unit->content;
1155 int pos, len;
1156
1157 8301 err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8301 times.
8301 if (err < 0)
1159 return err;
1160
1161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8301 times.
8301 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1162 return AVERROR_INVALIDDATA;
1163
1164 8301 pos = get_bits_count(&gbc);
1165 8301 len = unit->data_size;
1166
1167
2/2
✓ Branch 0 taken 5822 times.
✓ Branch 1 taken 2479 times.
8301 if (slice->header.sh_picture_header_in_slice_header_flag) {
1168 5822 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5822 times.
5822 if (err < 0)
1170 return err;
1171 }
1172
1173 8301 slice->header_size = pos / 8;
1174 8301 slice->data_size = len - pos / 8;
1175 8301 slice->data_ref = av_buffer_ref(unit->data_ref);
1176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8301 times.
8301 if (!slice->data_ref)
1177 return AVERROR(ENOMEM);
1178 8301 slice->data = unit->data + pos / 8;
1179 8301 slice->data_bit_start = pos % 8;
1180 }
1181 8301 break;
1182
1183 45 case VVC_AUD_NUT:
1184 {
1185 45 err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (err < 0)
1187 return err;
1188 }
1189 45 break;
1190
1191 3124 case VVC_PREFIX_SEI_NUT:
1192 case VVC_SUFFIX_SEI_NUT:
1193 {
1194 3124 err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1195 3124 unit->type == VVC_PREFIX_SEI_NUT);
1196
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3124 times.
3124 if (err < 0)
1198 return err;
1199 }
1200 3124 break;
1201
1202 default:
1203 return AVERROR(ENOSYS);
1204 }
1205 14080 return 0;
1206 }
1207
1208 14194 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1209 PutBitContext *pbc, const uint8_t *data,
1210 size_t data_size, int data_bit_start)
1211 {
1212 14194 size_t rest = data_size - (data_bit_start + 7) / 8;
1213 14194 const uint8_t *pos = data + data_bit_start / 8;
1214
1215
2/4
✓ Branch 0 taken 14194 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14194 times.
14194 av_assert0(data_bit_start >= 0 &&
1216 data_size > data_bit_start / 8);
1217
1218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14194 times.
14194 if (data_size * 8 + 8 > put_bits_left(pbc))
1219 return AVERROR(ENOSPC);
1220
1221
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14193 times.
14194 if (!rest)
1222 1 goto rbsp_stop_one_bit;
1223
1224 // First copy the remaining bits of the first byte
1225 // The above check ensures that we do not accidentally
1226 // copy beyond the rbsp_stop_one_bit.
1227
2/2
✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 10464 times.
14193 if (data_bit_start % 8)
1228 3729 put_bits(pbc, 8 - data_bit_start % 8,
1229 3729 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1230
1231
2/2
✓ Branch 1 taken 14084 times.
✓ Branch 2 taken 109 times.
14193 if (put_bits_count(pbc) % 8 == 0) {
1232 // If the writer is aligned at this point,
1233 // memcpy can be used to improve performance.
1234 // This happens normally for CABAC.
1235 14084 flush_put_bits(pbc);
1236 14084 memcpy(put_bits_ptr(pbc), pos, rest);
1237 14084 skip_put_bytes(pbc, rest);
1238 } else {
1239 // If not, we have to copy manually.
1240 // rbsp_stop_one_bit forces us to special-case
1241 // the last byte.
1242 uint8_t temp;
1243 int i;
1244
1245
2/2
✓ Branch 0 taken 98762 times.
✓ Branch 1 taken 109 times.
98871 for (; rest > 4; rest -= 4, pos += 4)
1246 98762 put_bits32(pbc, AV_RB32(pos));
1247
1248
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 109 times.
274 for (; rest > 1; rest--, pos++)
1249 165 put_bits(pbc, 8, *pos);
1250
1251 109 rbsp_stop_one_bit:
1252
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);
1253
1254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 av_assert0(temp);
1255 110 i = ff_ctz(*pos);
1256 110 temp = temp >> i;
1257
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1258 110 put_bits(pbc, i, temp);
1259
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 18 times.
110 if (put_bits_count(pbc) % 8)
1260 92 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1261 }
1262
1263 14194 return 0;
1264 }
1265
1266 7953 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1267 CodedBitstreamUnit *unit,
1268 PutBitContext *pbc)
1269 {
1270 int err;
1271
1272
7/10
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1495 times.
✓ Branch 3 taken 5575 times.
✓ Branch 4 taken 302 times.
✓ Branch 5 taken 505 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
7953 switch (unit->type) {
1273 73 case H264_NAL_SPS:
1274 {
1275 73 H264RawSPS *sps = unit->content;
1276
1277 73 err = cbs_h264_write_sps(ctx, pbc, sps);
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
1279 return err;
1280
1281 73 err = cbs_h264_replace_sps(ctx, unit);
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
1283 return err;
1284 }
1285 73 break;
1286
1287 case H264_NAL_SPS_EXT:
1288 {
1289 H264RawSPSExtension *sps_ext = unit->content;
1290
1291 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1292 if (err < 0)
1293 return err;
1294 }
1295 break;
1296
1297 1495 case H264_NAL_PPS:
1298 {
1299 1495 H264RawPPS *pps = unit->content;
1300
1301 1495 err = cbs_h264_write_pps(ctx, pbc, pps);
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
1303 return err;
1304
1305 1495 err = cbs_h264_replace_pps(ctx, unit);
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
1307 return err;
1308 }
1309 1495 break;
1310
1311 5575 case H264_NAL_SLICE:
1312 case H264_NAL_IDR_SLICE:
1313 case H264_NAL_AUXILIARY_SLICE:
1314 {
1315 5575 H264RawSlice *slice = unit->content;
1316
1317 5575 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5575 times.
5575 if (err < 0)
1319 return err;
1320
1321
1/2
✓ Branch 0 taken 5575 times.
✗ Branch 1 not taken.
5575 if (slice->data) {
1322 5575 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1323 slice->data_size,
1324 slice->data_bit_start);
1325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5575 times.
5575 if (err < 0)
1326 return err;
1327 } else {
1328 // No slice data - that was just the header.
1329 // (Bitstream may be unaligned!)
1330 }
1331 }
1332 5575 break;
1333
1334 302 case H264_NAL_AUD:
1335 {
1336 302 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (err < 0)
1338 return err;
1339 }
1340 302 break;
1341
1342 505 case H264_NAL_SEI:
1343 {
1344 505 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505 times.
505 if (err < 0)
1346 return err;
1347 }
1348 505 break;
1349
1350 case H264_NAL_FILLER_DATA:
1351 {
1352 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1353 if (err < 0)
1354 return err;
1355 }
1356 break;
1357
1358 1 case H264_NAL_END_SEQUENCE:
1359 {
1360 1 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1362 return err;
1363 }
1364 1 break;
1365
1366 2 case H264_NAL_END_STREAM:
1367 {
1368 2 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1370 return err;
1371 }
1372 2 break;
1373
1374 default:
1375 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1376 "NAL unit type %"PRIu32".\n", unit->type);
1377 return AVERROR_PATCHWELCOME;
1378 }
1379
1380 7953 return 0;
1381 }
1382
1383 8050 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1384 CodedBitstreamUnit *unit,
1385 PutBitContext *pbc)
1386 {
1387 int err;
1388
1389
6/7
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 283 times.
✓ Branch 3 taken 6031 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 1562 times.
✗ Branch 6 not taken.
8050 switch (unit->type) {
1390 56 case HEVC_NAL_VPS:
1391 {
1392 56 H265RawVPS *vps = unit->content;
1393
1394 56 err = cbs_h265_write_vps(ctx, pbc, vps);
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
1396 return err;
1397
1398 56 err = cbs_h265_replace_vps(ctx, unit);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
1400 return err;
1401 }
1402 56 break;
1403
1404 58 case HEVC_NAL_SPS:
1405 {
1406 58 H265RawSPS *sps = unit->content;
1407
1408 58 err = cbs_h265_write_sps(ctx, pbc, sps);
1409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
1410 return err;
1411
1412 58 err = cbs_h265_replace_sps(ctx, unit);
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
1414 return err;
1415 }
1416 58 break;
1417
1418 283 case HEVC_NAL_PPS:
1419 {
1420 283 H265RawPPS *pps = unit->content;
1421
1422 283 err = cbs_h265_write_pps(ctx, pbc, pps);
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
1424 return err;
1425
1426 283 err = cbs_h265_replace_pps(ctx, unit);
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
1428 return err;
1429 }
1430 283 break;
1431
1432 6031 case HEVC_NAL_TRAIL_N:
1433 case HEVC_NAL_TRAIL_R:
1434 case HEVC_NAL_TSA_N:
1435 case HEVC_NAL_TSA_R:
1436 case HEVC_NAL_STSA_N:
1437 case HEVC_NAL_STSA_R:
1438 case HEVC_NAL_RADL_N:
1439 case HEVC_NAL_RADL_R:
1440 case HEVC_NAL_RASL_N:
1441 case HEVC_NAL_RASL_R:
1442 case HEVC_NAL_BLA_W_LP:
1443 case HEVC_NAL_BLA_W_RADL:
1444 case HEVC_NAL_BLA_N_LP:
1445 case HEVC_NAL_IDR_W_RADL:
1446 case HEVC_NAL_IDR_N_LP:
1447 case HEVC_NAL_CRA_NUT:
1448 {
1449 6031 H265RawSlice *slice = unit->content;
1450
1451 6031 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6031 times.
6031 if (err < 0)
1453 return err;
1454
1455
1/2
✓ Branch 0 taken 6031 times.
✗ Branch 1 not taken.
6031 if (slice->data) {
1456 6031 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1457 slice->data_size,
1458 slice->data_bit_start);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6031 times.
6031 if (err < 0)
1460 return err;
1461 } else {
1462 // No slice data - that was just the header.
1463 }
1464 }
1465 6031 break;
1466
1467 60 case HEVC_NAL_AUD:
1468 {
1469 60 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1471 return err;
1472 }
1473 60 break;
1474
1475 1562 case HEVC_NAL_SEI_PREFIX:
1476 case HEVC_NAL_SEI_SUFFIX:
1477 {
1478 1562 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1479 1562 unit->type == HEVC_NAL_SEI_PREFIX);
1480
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1562 times.
1562 if (err < 0)
1482 return err;
1483 }
1484 1562 break;
1485
1486 default:
1487 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1488 "NAL unit type %"PRIu32".\n", unit->type);
1489 return AVERROR_PATCHWELCOME;
1490 }
1491
1492 8050 return 0;
1493 }
1494
1495 756 static int cbs_h264_discarded_nal_unit(CodedBitstreamContext *ctx,
1496 const CodedBitstreamUnit *unit,
1497 enum AVDiscard skip)
1498 {
1499 H264RawNALUnitHeader *header;
1500 H264RawSliceHeader *slice;
1501 int slice_type_i, slice_type_b, slice_type_si;
1502
1503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 756 times.
756 if (skip <= AVDISCARD_DEFAULT)
1504 return 0;
1505
1506 // keep non-VCL
1507
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 500 times.
756 if (unit->type != H264_NAL_SLICE &&
1508
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 4 times.
256 unit->type != H264_NAL_IDR_SLICE &&
1509
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 unit->type != H264_NAL_AUXILIARY_SLICE)
1510 252 return 0;
1511
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
504 if (skip >= AVDISCARD_ALL)
1513 return 1;
1514
1515
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)
1516 125 return 1;
1517
1518 379 header = (H264RawNALUnitHeader *)unit->content;
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379 times.
379 if (!header) {
1520 av_log(ctx->log_ctx, AV_LOG_WARNING,
1521 "h264 nal unit header is null, missing decompose?\n");
1522 return 0;
1523 }
1524
1525
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)
1526 183 return 1;
1527
1528 196 slice = (H264RawSliceHeader *)unit->content;
1529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (!slice) {
1530 av_log(ctx->log_ctx, AV_LOG_WARNING,
1531 "h264 slice header is null, missing decompose?\n");
1532 return 0;
1533 }
1534
1535 196 slice_type_i = slice->slice_type % 5 == 2;
1536 196 slice_type_b = slice->slice_type % 5 == 1;
1537 196 slice_type_si = slice->slice_type % 5 == 4;
1538
1539
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)
1540 60 return 1;
1541
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)
1542 29 return 1;
1543
1544 107 return 0;
1545 }
1546
1547 536 static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx,
1548 const CodedBitstreamUnit *unit,
1549 enum AVDiscard skip)
1550 {
1551 H265RawSliceHeader *slice;
1552
1553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536 times.
536 if (skip <= AVDISCARD_DEFAULT)
1554 return 0;
1555
1556
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 204 times.
536 switch (unit->type) {
1557 4 case HEVC_NAL_BLA_W_LP:
1558 case HEVC_NAL_BLA_W_RADL:
1559 case HEVC_NAL_BLA_N_LP:
1560 case HEVC_NAL_IDR_W_RADL:
1561 case HEVC_NAL_IDR_N_LP:
1562 case HEVC_NAL_CRA_NUT:
1563 // IRAP slice
1564
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (skip < AVDISCARD_ALL)
1565 4 return 0;
1566 break;
1567
1568 328 case HEVC_NAL_TRAIL_R:
1569 case HEVC_NAL_TRAIL_N:
1570 case HEVC_NAL_TSA_N:
1571 case HEVC_NAL_TSA_R:
1572 case HEVC_NAL_STSA_N:
1573 case HEVC_NAL_STSA_R:
1574 case HEVC_NAL_RADL_N:
1575 case HEVC_NAL_RADL_R:
1576 case HEVC_NAL_RASL_N:
1577 case HEVC_NAL_RASL_R:
1578 // Slice
1579 328 break;
1580 204 default:
1581 // Don't discard non-slice nal.
1582 204 return 0;
1583 }
1584
1585
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 281 times.
328 if (skip >= AVDISCARD_NONKEY)
1586 47 return 1;
1587
1588 281 slice = (H265RawSliceHeader *)unit->content;
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 if (!slice) {
1590 av_log(ctx->log_ctx, AV_LOG_WARNING,
1591 "h265 slice header is null, missing decompose?\n");
1592 return 0;
1593 }
1594
1595
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)
1596 47 return 1;
1597
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)
1598 47 return 1;
1599
1600
1/2
✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
187 if (skip >= AVDISCARD_NONREF) {
1601
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 163 times.
187 switch (unit->type) {
1602 24 case HEVC_NAL_TRAIL_N:
1603 case HEVC_NAL_TSA_N:
1604 case HEVC_NAL_STSA_N:
1605 case HEVC_NAL_RADL_N:
1606 case HEVC_NAL_RASL_N:
1607 case HEVC_NAL_VCL_N10:
1608 case HEVC_NAL_VCL_N12:
1609 case HEVC_NAL_VCL_N14:
1610 // non-ref
1611 24 return 1;
1612 163 default:
1613 163 break;
1614 }
1615 }
1616
1617 163 return 0;
1618 }
1619
1620 5777 static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
1621 CodedBitstreamUnit *unit,
1622 PutBitContext *pbc)
1623 {
1624 int err;
1625
1626
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) {
1627 1 case VVC_DCI_NUT:
1628 {
1629 1 H266RawDCI *dci = unit->content;
1630
1631 1 err = cbs_h266_write_dci(ctx, pbc, dci);
1632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1633 return err;
1634 }
1635 1 break;
1636 1 case VVC_OPI_NUT:
1637 {
1638 1 H266RawOPI *opi = unit->content;
1639
1640 1 err = cbs_h266_write_opi(ctx, pbc, opi);
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1642 return err;
1643 }
1644 1 break;
1645 4 case VVC_VPS_NUT:
1646 {
1647 4 H266RawVPS *vps = unit->content;
1648
1649 4 err = cbs_h266_write_vps(ctx, pbc, vps);
1650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1651 return err;
1652
1653 4 err = cbs_h266_replace_vps(ctx, unit);
1654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1655 return err;
1656 }
1657 4 break;
1658 335 case VVC_SPS_NUT:
1659 {
1660 335 H266RawSPS *sps = unit->content;
1661
1662 335 err = cbs_h266_write_sps(ctx, pbc, sps);
1663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1664 return err;
1665
1666 335 err = cbs_h266_replace_sps(ctx, unit);
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1668 return err;
1669 }
1670 335 break;
1671
1672 391 case VVC_PPS_NUT:
1673 {
1674 391 H266RawPPS *pps = unit->content;
1675
1676 391 err = cbs_h266_write_pps(ctx, pbc, pps);
1677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1678 return err;
1679
1680 391 err = cbs_h266_replace_pps(ctx, unit);
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1682 return err;
1683 }
1684 391 break;
1685
1686 228 case VVC_PREFIX_APS_NUT:
1687 case VVC_SUFFIX_APS_NUT:
1688 {
1689 228 err = cbs_h266_write_aps(ctx, pbc, unit->content,
1690 228 unit->type == VVC_PREFIX_APS_NUT);
1691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 if (err < 0)
1692 return err;
1693 }
1694 228 break;
1695 27 case VVC_PH_NUT:
1696 {
1697 27 H266RawPH *ph = unit->content;
1698 27 err = cbs_h266_write_ph(ctx, pbc, ph);
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1700 return err;
1701
1702 27 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1704 return err;
1705 }
1706 27 break;
1707
1708 2588 case VVC_TRAIL_NUT:
1709 case VVC_STSA_NUT:
1710 case VVC_RADL_NUT:
1711 case VVC_RASL_NUT:
1712 case VVC_IDR_W_RADL:
1713 case VVC_IDR_N_LP:
1714 case VVC_CRA_NUT:
1715 case VVC_GDR_NUT:
1716 {
1717 2588 H266RawSlice *slice = unit->content;
1718
1719 2588 err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1721 return err;
1722
1723
2/2
✓ Branch 0 taken 2103 times.
✓ Branch 1 taken 485 times.
2588 if (slice->header.sh_picture_header_in_slice_header_flag) {
1724 2103 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2103 times.
2103 if (err < 0)
1726 return err;
1727 }
1728
1729
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (slice->data) {
1730 2588 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1731 slice->data_size,
1732 slice->data_bit_start);
1733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1734 return err;
1735 } else {
1736 // No slice data - that was just the header.
1737 }
1738 }
1739 2588 break;
1740
1741 12 case VVC_AUD_NUT:
1742 {
1743 12 err = cbs_h266_write_aud(ctx, pbc, unit->content);
1744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (err < 0)
1745 return err;
1746 }
1747 12 break;
1748
1749 2190 case VVC_PREFIX_SEI_NUT:
1750 case VVC_SUFFIX_SEI_NUT:
1751 {
1752 2190 err = cbs_h266_write_sei(ctx, pbc, unit->content,
1753 2190 unit->type == VVC_PREFIX_SEI_NUT);
1754
1755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2190 times.
2190 if (err < 0)
1756 return err;
1757 }
1758 2190 break;
1759
1760 default:
1761 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1762 "NAL unit type %"PRIu32".\n", unit->type);
1763 return AVERROR_PATCHWELCOME;
1764 }
1765
1766 5777 return 0;
1767 }
1768
1769 21780 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
1770 CodedBitstreamUnitType type,
1771 int nal_unit_index)
1772 {
1773 // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1774
2/2
✓ Branch 0 taken 7246 times.
✓ Branch 1 taken 14534 times.
21780 if (nal_unit_index == 0) {
1775 // Assume that this is the first NAL unit in an access unit.
1776 7246 return 1;
1777 }
1778
2/2
✓ Branch 0 taken 5022 times.
✓ Branch 1 taken 9512 times.
14534 if (codec_id == AV_CODEC_ID_H264)
1779
4/4
✓ Branch 0 taken 5014 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 448 times.
✓ Branch 3 taken 4566 times.
5022 return type == H264_NAL_SPS || type == H264_NAL_PPS;
1780
2/2
✓ Branch 0 taken 5867 times.
✓ Branch 1 taken 3645 times.
9512 if (codec_id == AV_CODEC_ID_HEVC)
1781
6/6
✓ Branch 0 taken 5864 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5806 times.
✓ Branch 3 taken 58 times.
✓ Branch 4 taken 56 times.
✓ Branch 5 taken 5750 times.
5867 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1782
1/2
✓ Branch 0 taken 3645 times.
✗ Branch 1 not taken.
3645 if (codec_id == AV_CODEC_ID_VVC)
1783
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;
1784 return 0;
1785 }
1786
1787 7247 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1788 CodedBitstreamFragment *frag)
1789 {
1790 uint8_t *data;
1791 size_t max_size, dp, sp;
1792 int err, i, zero_run;
1793
1794
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1795 // Data should already all have been written when we get here.
1796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21780 times.
21780 av_assert0(frag->units[i].data);
1797 }
1798
1799 7247 max_size = 0;
1800
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1801 // Start code + content with worst-case emulation prevention.
1802 21780 max_size += 4 + frag->units[i].data_size * 3 / 2;
1803 }
1804
1805 7247 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (!data)
1807 return AVERROR(ENOMEM);
1808
1809 7247 dp = 0;
1810
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1811 21780 CodedBitstreamUnit *unit = &frag->units[i];
1812
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21780 times.
21780 if (unit->data_bit_padding > 0) {
1814 if (i < frag->nb_units - 1)
1815 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1816 "unaligned padding on non-final NAL unit.\n");
1817 else
1818 frag->data_bit_padding = unit->data_bit_padding;
1819 }
1820
1821
2/2
✓ Branch 1 taken 8257 times.
✓ Branch 2 taken 13523 times.
21780 if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1822 // zero_byte
1823 8257 data[dp++] = 0;
1824 }
1825 // start_code_prefix_one_3bytes
1826 21780 data[dp++] = 0;
1827 21780 data[dp++] = 0;
1828 21780 data[dp++] = 1;
1829
1830 21780 zero_run = 0;
1831
2/2
✓ Branch 0 taken 16462517 times.
✓ Branch 1 taken 21780 times.
16484297 for (sp = 0; sp < unit->data_size; sp++) {
1832
2/2
✓ Branch 0 taken 16455111 times.
✓ Branch 1 taken 7406 times.
16462517 if (zero_run < 2) {
1833
2/2
✓ Branch 0 taken 147107 times.
✓ Branch 1 taken 16308004 times.
16455111 if (unit->data[sp] == 0)
1834 147107 ++zero_run;
1835 else
1836 16308004 zero_run = 0;
1837 } else {
1838
2/2
✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 6119 times.
7406 if ((unit->data[sp] & ~3) == 0) {
1839 // emulation_prevention_three_byte
1840 1287 data[dp++] = 3;
1841 }
1842 7406 zero_run = unit->data[sp] == 0;
1843 }
1844 16462517 data[dp++] = unit->data[sp];
1845 }
1846 }
1847
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 av_assert0(dp <= max_size);
1849 7247 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (err)
1851 return err;
1852 7247 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1853
1854 7247 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1855 NULL, NULL, 0);
1856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (!frag->data_ref) {
1857 av_freep(&data);
1858 return AVERROR(ENOMEM);
1859 }
1860
1861 7247 frag->data = data;
1862 7247 frag->data_size = dp;
1863
1864 7247 return 0;
1865 }
1866
1867 static void cbs_h264_flush(CodedBitstreamContext *ctx)
1868 {
1869 CodedBitstreamH264Context *h264 = ctx->priv_data;
1870
1871 for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1872 ff_refstruct_unref(&h264->sps[i]);
1873 for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1874 ff_refstruct_unref(&h264->pps[i]);
1875
1876 h264->active_sps = NULL;
1877 h264->active_pps = NULL;
1878 h264->last_slice_nal_unit_type = 0;
1879 }
1880
1881 44 static void cbs_h264_close(CodedBitstreamContext *ctx)
1882 {
1883 44 CodedBitstreamH264Context *h264 = ctx->priv_data;
1884 int i;
1885
1886 44 ff_h2645_packet_uninit(&h264->common.read_packet);
1887
1888
2/2
✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 44 times.
1452 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1889 1408 ff_refstruct_unref(&h264->sps[i]);
1890
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 44 times.
11308 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1891 11264 ff_refstruct_unref(&h264->pps[i]);
1892 44 }
1893
1894 static void cbs_h265_flush(CodedBitstreamContext *ctx)
1895 {
1896 CodedBitstreamH265Context *h265 = ctx->priv_data;
1897
1898 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1899 ff_refstruct_unref(&h265->vps[i]);
1900 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1901 ff_refstruct_unref(&h265->sps[i]);
1902 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1903 ff_refstruct_unref(&h265->pps[i]);
1904
1905 h265->active_vps = NULL;
1906 h265->active_sps = NULL;
1907 h265->active_pps = NULL;
1908 }
1909
1910 44 static void cbs_h265_close(CodedBitstreamContext *ctx)
1911 {
1912 44 CodedBitstreamH265Context *h265 = ctx->priv_data;
1913 int i;
1914
1915 44 ff_h2645_packet_uninit(&h265->common.read_packet);
1916
1917
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 44 times.
748 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1918 704 ff_refstruct_unref(&h265->vps[i]);
1919
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 44 times.
748 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1920 704 ff_refstruct_unref(&h265->sps[i]);
1921
2/2
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 44 times.
2860 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1922 2816 ff_refstruct_unref(&h265->pps[i]);
1923 44 }
1924
1925 155 static void cbs_h266_flush(CodedBitstreamContext *ctx)
1926 {
1927 155 CodedBitstreamH266Context *h266 = ctx->priv_data;
1928
1929
2/2
✓ Branch 0 taken 2480 times.
✓ Branch 1 taken 155 times.
2635 for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1930 2480 ff_refstruct_unref(&h266->vps[i]);
1931
2/2
✓ Branch 0 taken 2480 times.
✓ Branch 1 taken 155 times.
2635 for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1932 2480 ff_refstruct_unref(&h266->sps[i]);
1933
2/2
✓ Branch 0 taken 9920 times.
✓ Branch 1 taken 155 times.
10075 for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1934 9920 ff_refstruct_unref(&h266->pps[i]);
1935 155 ff_refstruct_unref(&h266->ph_ref);
1936 155 }
1937
1938 155 static void cbs_h266_close(CodedBitstreamContext *ctx)
1939 {
1940 155 CodedBitstreamH266Context *h266 = ctx->priv_data;
1941
1942 155 cbs_h266_flush(ctx);
1943 155 ff_h2645_packet_uninit(&h266->common.read_packet);
1944 155 }
1945
1946 913 static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
1947 {
1948 913 H264RawSEI *sei = content;
1949 913 ff_cbs_sei_free_message_list(&sei->message_list);
1950 913 }
1951
1952 static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
1953 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
1954 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
1955
1956 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
1957
1958 CBS_UNIT_TYPES_INTERNAL_REF((H264_NAL_IDR_SLICE,
1959 H264_NAL_SLICE,
1960 H264_NAL_AUXILIARY_SLICE), H264RawSlice, data),
1961
1962 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
1963 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
1964 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
1965 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
1966
1967 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
1968
1969 CBS_UNIT_TYPE_END_OF_LIST
1970 };
1971
1972 1727 static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
1973 {
1974 1727 H265RawSEI *sei = content;
1975 1727 ff_cbs_sei_free_message_list(&sei->message_list);
1976 1727 }
1977
1978 static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
1979 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_VPS, H265RawVPS, extension_data.data),
1980 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
1981 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
1982
1983 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
1984
1985 // Slices of non-IRAP pictures.
1986 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_TRAIL_N, HEVC_NAL_RASL_R,
1987 H265RawSlice, data),
1988 // Slices of IRAP pictures.
1989 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_BLA_W_LP, HEVC_NAL_CRA_NUT,
1990 H265RawSlice, data),
1991
1992 CBS_UNIT_TYPES_COMPLEX((HEVC_NAL_SEI_PREFIX, HEVC_NAL_SEI_SUFFIX),
1993 H265RawSEI, cbs_h265_free_sei),
1994
1995 CBS_UNIT_TYPE_END_OF_LIST
1996 };
1997
1998 3124 static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
1999 {
2000 3124 H266RawSEI *sei = content;
2001 3124 ff_cbs_sei_free_message_list(&sei->message_list);
2002 3124 }
2003
2004 static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[] = {
2005 CBS_UNIT_TYPE_INTERNAL_REF(VVC_DCI_NUT, H266RawDCI, extension_data.data),
2006 CBS_UNIT_TYPE_INTERNAL_REF(VVC_OPI_NUT, H266RawOPI, extension_data.data),
2007 CBS_UNIT_TYPE_INTERNAL_REF(VVC_VPS_NUT, H266RawVPS, extension_data.data),
2008 {
2009 .nb_unit_types = 1,
2010 .unit_type.list[0] = VVC_SPS_NUT,
2011 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2012 .content_size = sizeof(H266RawSPS),
2013 .type.ref = {
2014 .nb_offsets = 2,
2015 .offsets = { offsetof(H266RawSPS, extension_data.data),
2016 offsetof(H266RawSPS, vui.extension_data.data) }
2017 },
2018 },
2019 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PPS_NUT, H266RawPPS, extension_data.data),
2020 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PREFIX_APS_NUT, H266RawAPS, extension_data.data),
2021 CBS_UNIT_TYPE_INTERNAL_REF(VVC_SUFFIX_APS_NUT, H266RawAPS, extension_data.data),
2022
2023 CBS_UNIT_TYPE_POD(VVC_PH_NUT , H266RawPH),
2024 CBS_UNIT_TYPE_POD(VVC_AUD_NUT, H266RawAUD),
2025
2026 CBS_UNIT_RANGE_INTERNAL_REF(VVC_TRAIL_NUT, VVC_RASL_NUT,
2027 H266RawSlice, data),
2028
2029 CBS_UNIT_RANGE_INTERNAL_REF(VVC_IDR_W_RADL, VVC_GDR_NUT,
2030 H266RawSlice, data),
2031
2032 CBS_UNIT_TYPES_COMPLEX((VVC_PREFIX_SEI_NUT, VVC_SUFFIX_SEI_NUT),
2033 H266RawSEI, cbs_h266_free_sei),
2034
2035 CBS_UNIT_TYPE_END_OF_LIST
2036 };
2037
2038 const CodedBitstreamType ff_cbs_type_h264 = {
2039 .codec_id = AV_CODEC_ID_H264,
2040
2041 .priv_data_size = sizeof(CodedBitstreamH264Context),
2042
2043 .unit_types = cbs_h264_unit_types,
2044
2045 .split_fragment = &cbs_h2645_split_fragment,
2046 .read_unit = &cbs_h264_read_nal_unit,
2047 .write_unit = &cbs_h264_write_nal_unit,
2048 .discarded_unit = &cbs_h264_discarded_nal_unit,
2049 .assemble_fragment = &cbs_h2645_assemble_fragment,
2050
2051 .flush = &cbs_h264_flush,
2052 .close = &cbs_h264_close,
2053 };
2054
2055 const CodedBitstreamType ff_cbs_type_h265 = {
2056 .codec_id = AV_CODEC_ID_HEVC,
2057
2058 .priv_data_size = sizeof(CodedBitstreamH265Context),
2059
2060 .unit_types = cbs_h265_unit_types,
2061
2062 .split_fragment = &cbs_h2645_split_fragment,
2063 .read_unit = &cbs_h265_read_nal_unit,
2064 .write_unit = &cbs_h265_write_nal_unit,
2065 .discarded_unit = &cbs_h265_discarded_nal_unit,
2066 .assemble_fragment = &cbs_h2645_assemble_fragment,
2067
2068 .flush = &cbs_h265_flush,
2069 .close = &cbs_h265_close,
2070 };
2071
2072 const CodedBitstreamType ff_cbs_type_h266 = {
2073 .codec_id = AV_CODEC_ID_VVC,
2074
2075 .priv_data_size = sizeof(CodedBitstreamH266Context),
2076
2077 .unit_types = cbs_h266_unit_types,
2078
2079 .split_fragment = &cbs_h2645_split_fragment,
2080 .read_unit = &cbs_h266_read_nal_unit,
2081 .write_unit = &cbs_h266_write_nal_unit,
2082 .assemble_fragment = &cbs_h2645_assemble_fragment,
2083
2084 .flush = &cbs_h266_flush,
2085 .close = &cbs_h266_close,
2086 };
2087
2088 // Macro for the read/write pair.
2089 #define SEI_MESSAGE_RW(codec, name) \
2090 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2091 .write = cbs_ ## codec ## _write_ ## name ## _internal
2092
2093 static const SEIMessageTypeDescriptor cbs_sei_common_types[] = {
2094 {
2095 SEI_TYPE_FILLER_PAYLOAD,
2096 1, 1,
2097 sizeof(SEIRawFillerPayload),
2098 SEI_MESSAGE_RW(sei, filler_payload),
2099 },
2100 {
2101 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
2102 1, 1,
2103 sizeof(SEIRawUserDataRegistered),
2104 SEI_MESSAGE_RW(sei, user_data_registered),
2105 },
2106 {
2107 SEI_TYPE_USER_DATA_UNREGISTERED,
2108 1, 1,
2109 sizeof(SEIRawUserDataUnregistered),
2110 SEI_MESSAGE_RW(sei, user_data_unregistered),
2111 },
2112 {
2113 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
2114 1, 0,
2115 sizeof(SEIRawMasteringDisplayColourVolume),
2116 SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2117 },
2118 {
2119 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
2120 1, 0,
2121 sizeof(SEIRawContentLightLevelInfo),
2122 SEI_MESSAGE_RW(sei, content_light_level_info),
2123 },
2124 {
2125 SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS,
2126 1, 0,
2127 sizeof(SEIRawAlternativeTransferCharacteristics),
2128 SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2129 },
2130 {
2131 SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT,
2132 1, 0,
2133 sizeof(SEIRawAmbientViewingEnvironment),
2134 SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2135 },
2136 SEI_MESSAGE_TYPE_END,
2137 };
2138
2139 static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = {
2140 {
2141 SEI_TYPE_BUFFERING_PERIOD,
2142 1, 0,
2143 sizeof(H264RawSEIBufferingPeriod),
2144 SEI_MESSAGE_RW(h264, sei_buffering_period),
2145 },
2146 {
2147 SEI_TYPE_PIC_TIMING,
2148 1, 0,
2149 sizeof(H264RawSEIPicTiming),
2150 SEI_MESSAGE_RW(h264, sei_pic_timing),
2151 },
2152 {
2153 SEI_TYPE_PAN_SCAN_RECT,
2154 1, 0,
2155 sizeof(H264RawSEIPanScanRect),
2156 SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2157 },
2158 {
2159 SEI_TYPE_RECOVERY_POINT,
2160 1, 0,
2161 sizeof(H264RawSEIRecoveryPoint),
2162 SEI_MESSAGE_RW(h264, sei_recovery_point),
2163 },
2164 {
2165 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2166 1, 0,
2167 sizeof(H264RawFilmGrainCharacteristics),
2168 SEI_MESSAGE_RW(h264, film_grain_characteristics),
2169 },
2170 {
2171 SEI_TYPE_DISPLAY_ORIENTATION,
2172 1, 0,
2173 sizeof(H264RawSEIDisplayOrientation),
2174 SEI_MESSAGE_RW(h264, sei_display_orientation),
2175 },
2176 SEI_MESSAGE_TYPE_END
2177 };
2178
2179 static const SEIMessageTypeDescriptor cbs_sei_h265_types[] = {
2180 {
2181 SEI_TYPE_BUFFERING_PERIOD,
2182 1, 0,
2183 sizeof(H265RawSEIBufferingPeriod),
2184 SEI_MESSAGE_RW(h265, sei_buffering_period),
2185 },
2186 {
2187 SEI_TYPE_PIC_TIMING,
2188 1, 0,
2189 sizeof(H265RawSEIPicTiming),
2190 SEI_MESSAGE_RW(h265, sei_pic_timing),
2191 },
2192 {
2193 SEI_TYPE_PAN_SCAN_RECT,
2194 1, 0,
2195 sizeof(H265RawSEIPanScanRect),
2196 SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2197 },
2198 {
2199 SEI_TYPE_RECOVERY_POINT,
2200 1, 0,
2201 sizeof(H265RawSEIRecoveryPoint),
2202 SEI_MESSAGE_RW(h265, sei_recovery_point),
2203 },
2204 {
2205 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2206 1, 0,
2207 sizeof(H265RawFilmGrainCharacteristics),
2208 SEI_MESSAGE_RW(h265, film_grain_characteristics),
2209 },
2210 {
2211 SEI_TYPE_DISPLAY_ORIENTATION,
2212 1, 0,
2213 sizeof(H265RawSEIDisplayOrientation),
2214 SEI_MESSAGE_RW(h265, sei_display_orientation),
2215 },
2216 {
2217 SEI_TYPE_ACTIVE_PARAMETER_SETS,
2218 1, 0,
2219 sizeof(H265RawSEIActiveParameterSets),
2220 SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2221 },
2222 {
2223 SEI_TYPE_DECODED_PICTURE_HASH,
2224 0, 1,
2225 sizeof(H265RawSEIDecodedPictureHash),
2226 SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2227 },
2228 {
2229 SEI_TYPE_TIME_CODE,
2230 1, 0,
2231 sizeof(H265RawSEITimeCode),
2232 SEI_MESSAGE_RW(h265, sei_time_code),
2233 },
2234 {
2235 SEI_TYPE_ALPHA_CHANNEL_INFO,
2236 1, 0,
2237 sizeof(H265RawSEIAlphaChannelInfo),
2238 SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2239 },
2240 SEI_MESSAGE_TYPE_END
2241 };
2242
2243 static const SEIMessageTypeDescriptor cbs_sei_h266_types[] = {
2244 {
2245 SEI_TYPE_DECODED_PICTURE_HASH,
2246 0, 1,
2247 sizeof(H266RawSEIDecodedPictureHash),
2248 SEI_MESSAGE_RW(h266, sei_decoded_picture_hash),
2249 },
2250 SEI_MESSAGE_TYPE_END
2251 };
2252
2253 14338 const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx,
2254 int payload_type)
2255 {
2256 const SEIMessageTypeDescriptor *codec_list;
2257 int i;
2258
2259
2/2
✓ Branch 0 taken 99742 times.
✓ Branch 1 taken 14200 times.
113942 for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2260
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 99604 times.
99742 if (cbs_sei_common_types[i].type == payload_type)
2261 138 return &cbs_sei_common_types[i];
2262 }
2263
2264
3/4
✓ Branch 0 taken 1845 times.
✓ Branch 1 taken 4851 times.
✓ Branch 2 taken 7504 times.
✗ Branch 3 not taken.
14200 switch (ctx->codec->codec_id) {
2265 1845 case AV_CODEC_ID_H264:
2266 1845 codec_list = cbs_sei_h264_types;
2267 1845 break;
2268 4851 case AV_CODEC_ID_H265:
2269 4851 codec_list = cbs_sei_h265_types;
2270 4851 break;
2271 7504 case AV_CODEC_ID_H266:
2272 7504 codec_list = cbs_sei_h266_types;
2273 7504 break;
2274 default:
2275 return NULL;
2276 }
2277
2278
2/2
✓ Branch 0 taken 47148 times.
✓ Branch 1 taken 266 times.
47414 for (i = 0; codec_list[i].type >= 0; i++) {
2279
2/2
✓ Branch 0 taken 13934 times.
✓ Branch 1 taken 33214 times.
47148 if (codec_list[i].type == payload_type)
2280 13934 return &codec_list[i];
2281 }
2282
2283 266 return NULL;
2284 }
2285