FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 711 958 74.2%
Functions: 37 38 97.4%
Branches: 424 672 63.1%

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