FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 656 929 70.6%
Functions: 36 38 94.7%
Branches: 390 646 60.4%

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