FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2024-09-07 18:49:03
Exec Total Coverage
Lines: 701 948 73.9%
Functions: 36 38 94.7%
Branches: 418 666 62.8%

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 301599 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 301599 times.
301599 CBS_TRACE_READ_START();
45
46
2/2
✓ Branch 1 taken 10804 times.
✓ Branch 2 taken 290795 times.
301599 max_length = FFMIN(get_bits_left(gbc), 32);
47
48
1/2
✓ Branch 0 taken 301599 times.
✗ Branch 1 not taken.
301599 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 301599 times.
301599 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 301599 leading_zeroes = max_length - 1 - av_log2(leading_bits);
62 301599 skip_bits_long(gbc, leading_zeroes);
63
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 301599 times.
301599 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 301599 value = get_bits_long(gbc, leading_zeroes + 1) - 1;
71
72
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 301599 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
301599 CBS_TRACE_READ_END();
73
74
2/4
✓ Branch 0 taken 301599 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 301599 times.
301599 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 301599 *write_to = value;
82 301599 return 0;
83 }
84
85 97980 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 97980 times.
97980 CBS_TRACE_READ_START();
95
96
2/2
✓ Branch 1 taken 8452 times.
✓ Branch 2 taken 89528 times.
97980 max_length = FFMIN(get_bits_left(gbc), 32);
97
98
1/2
✓ Branch 0 taken 97980 times.
✗ Branch 1 not taken.
97980 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97980 times.
97980 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 97980 leading_zeroes = max_length - 1 - av_log2(leading_bits);
112 97980 skip_bits_long(gbc, leading_zeroes);
113
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 97980 times.
97980 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 97980 unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
121
122
2/2
✓ Branch 0 taken 53740 times.
✓ Branch 1 taken 44240 times.
97980 if (unsigned_value & 1)
123 53740 value = -(int32_t)(unsigned_value / 2);
124 else
125 44240 value = unsigned_value / 2;
126
127
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 97980 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
97980 CBS_TRACE_READ_END();
128
129
2/4
✓ Branch 0 taken 97980 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97980 times.
97980 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 97980 *write_to = value;
137 97980 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 28840 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
329 {
330 28840 int bits_left = get_bits_left(gbc);
331
2/2
✓ Branch 0 taken 21537 times.
✓ Branch 1 taken 7303 times.
28840 if (bits_left > 8)
332 21537 return 1;
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7303 times.
7303 if (bits_left == 0)
334 return 0;
335
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 7292 times.
7303 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
336 11 return 1;
337 7292 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 11805 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 36489 times.
✓ Branch 1 taken 11805 times.
48294 for (i = 0; i < packet->nb_nals; i++) {
497 36489 const H2645NAL *nal = &packet->nals[i];
498 AVBufferRef *ref;
499 36489 size_t size = nal->size;
500 36489 enum AVCodecID codec_id = ctx->codec->codec_id;
501
502
3/4
✓ Branch 0 taken 9372 times.
✓ Branch 1 taken 27117 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9372 times.
36489 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 44431 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7942 times.
✓ Branch 3 taken 36489 times.
44431 while (size > 0 && nal->data[size - 1] == 0)
508 7942 --size;
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36489 times.
36489 if (size == 0) {
510 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
511 continue;
512 }
513
514 72978 ref = (nal->data == nal->raw_data) ? frag->data_ref
515
2/2
✓ Branch 0 taken 35195 times.
✓ Branch 1 taken 1294 times.
36489 : packet->rbsp.rbsp_buffer_ref;
516
517 36489 err = ff_cbs_append_unit_data(frag, nal->type,
518 36489 (uint8_t*)nal->data, size, ref);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36489 times.
36489 if (err < 0)
520 return err;
521 }
522
523 11805 return 0;
524 }
525
526 11794 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
527 CodedBitstreamFragment *frag,
528 int header)
529 {
530 11794 enum AVCodecID codec_id = ctx->codec->codec_id;
531 11794 CodedBitstreamH2645Context *priv = ctx->priv_data;
532 GetByteContext gbc;
533 int err;
534
535
2/4
✓ Branch 0 taken 11794 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11794 times.
11794 av_assert0(frag->data && frag->nb_units == 0);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11794 times.
11794 if (frag->data_size == 0)
537 return 0;
538
539
6/6
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 11638 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 146 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 1 times.
11803 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 147 times.
✓ Branch 1 taken 11638 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 146 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
11785 } 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 147 times.
✓ Branch 1 taken 11638 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 146 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
11786 } 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 11154 times.
11784 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 11784 err = ff_h2645_packet_split(&priv->read_packet,
744 11784 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 11784 times.
11784 if (err < 0)
749 return err;
750
751 11784 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11784 times.
11784 if (err < 0)
753 return err;
754 }
755
756 11794 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 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)
777
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)
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 1501 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1501 times.
1501 cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
799
800 1223 static int cbs_h266_replace_sps(CodedBitstreamContext *ctx,
801 CodedBitstreamUnit *unit)
802 {
803 1223 CodedBitstreamH266Context *priv = ctx->priv_data;
804 1223 H266RawSPS *sps = unit->content;
805 1223 unsigned int id = sps->sps_seq_parameter_set_id;
806 1223 int err = ff_cbs_make_unit_refcounted(ctx, unit);
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1223 times.
1223 if (err < 0)
808 return err;
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1223 times.
1223 av_assert0(unit->content_ref);
810
4/4
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 206 times.
✓ Branch 2 taken 780 times.
✓ Branch 3 taken 237 times.
1223 if (priv->sps[id] && memcmp(priv->sps[id], unit->content_ref, sizeof(*priv->sps[id]))) {
811
2/2
✓ Branch 0 taken 49920 times.
✓ Branch 1 taken 780 times.
50700 for (unsigned int i = 0; i < VVC_MAX_PPS_COUNT; i++) {
812
3/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 49140 times.
✓ Branch 2 taken 780 times.
✗ Branch 3 not taken.
49920 if (priv->pps[i] && priv->pps[i]->pps_seq_parameter_set_id == id)
813 780 ff_refstruct_unref(&priv->pps[i]);
814 }
815 }
816 1223 ff_refstruct_replace(&priv->sps[id], unit->content_ref);
817 1223 return 0;
818 }
819
820 8164 static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
821 CodedBitstreamUnit *unit,
822 H266RawPictureHeader *ph)
823 {
824 8164 CodedBitstreamH266Context *h266 = ctx->priv_data;
825 int err;
826
827 8164 err = ff_cbs_make_unit_refcounted(ctx, unit);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8164 times.
8164 if (err < 0)
829 return err;
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8164 times.
8164 av_assert0(unit->content_ref);
831 8164 ff_refstruct_replace(&h266->ph_ref, unit->content_ref);
832 8164 h266->ph = ph;
833 8164 return 0;
834 }
835
836 9230 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
837 CodedBitstreamUnit *unit)
838 {
839 GetBitContext gbc;
840 int err;
841
842 9230 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9230 times.
9230 if (err < 0)
844 return err;
845
846 9230 err = ff_cbs_alloc_unit_content(ctx, unit);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9230 times.
9230 if (err < 0)
848 return err;
849
850
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) {
851 73 case H264_NAL_SPS:
852 {
853 73 H264RawSPS *sps = unit->content;
854
855 73 err = cbs_h264_read_sps(ctx, &gbc, sps);
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
857 return err;
858
859 73 err = cbs_h264_replace_sps(ctx, unit);
860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
861 return err;
862 }
863 73 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 1495 case H264_NAL_PPS:
874 {
875 1495 H264RawPPS *pps = unit->content;
876
877 1495 err = cbs_h264_read_pps(ctx, &gbc, pps);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
879 return err;
880
881 1495 err = cbs_h264_replace_pps(ctx, unit);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
883 return err;
884 }
885 1495 break;
886
887 5973 case H264_NAL_SLICE:
888 case H264_NAL_IDR_SLICE:
889 case H264_NAL_AUXILIARY_SLICE:
890 {
891 5973 H264RawSlice *slice = unit->content;
892 int pos, len;
893
894 5973 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
5973 if (err < 0)
896 return err;
897
898
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5973 times.
5973 if (!cbs_h2645_read_more_rbsp_data(&gbc))
899 return AVERROR_INVALIDDATA;
900
901 5973 pos = get_bits_count(&gbc);
902 5973 len = unit->data_size;
903
904 5973 slice->data_size = len - pos / 8;
905 5973 slice->data_ref = av_buffer_ref(unit->data_ref);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
5973 if (!slice->data_ref)
907 return AVERROR(ENOMEM);
908 5973 slice->data = unit->data + pos / 8;
909 5973 slice->data_bit_start = pos % 8;
910 }
911 5973 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 9229 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/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) {
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 1727 case HEVC_NAL_SEI_PREFIX:
1059 case HEVC_NAL_SEI_SUFFIX:
1060 {
1061 1727 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1062 1727 unit->type == HEVC_NAL_SEI_PREFIX);
1063
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1727 times.
1727 if (err < 0)
1065 return err;
1066 }
1067 1727 break;
1068
1069 default:
1070 return AVERROR(ENOSYS);
1071 }
1072
1073 9372 return 0;
1074 }
1075
1076 14302 static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
1077 CodedBitstreamUnit *unit)
1078 {
1079 GetBitContext gbc;
1080 int err;
1081
1082 14302 err = init_get_bits8(&gbc, unit->data, unit->data_size);
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14302 times.
14302 if (err < 0)
1084 return err;
1085
1086 14302 err = ff_cbs_alloc_unit_content(ctx, unit);
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14302 times.
14302 if (err < 0)
1088 return err;
1089
1090
10/11
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 888 times.
✓ Branch 4 taken 1110 times.
✓ Branch 5 taken 543 times.
✓ Branch 6 taken 177 times.
✓ Branch 7 taken 8355 times.
✓ Branch 8 taken 49 times.
✓ Branch 9 taken 3159 times.
✗ Branch 10 not taken.
14302 switch (unit->type) {
1091 4 case VVC_DCI_NUT:
1092 {
1093 4 err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1094
1095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1096 return err;
1097 }
1098 4 break;
1099 2 case VVC_OPI_NUT:
1100 {
1101 2 err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1102
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1104 return err;
1105 }
1106 2 break;
1107 15 case VVC_VPS_NUT:
1108 {
1109 15 H266RawVPS *vps = unit->content;
1110
1111 15 err = cbs_h266_read_vps(ctx, &gbc, vps);
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (err < 0)
1113 return err;
1114
1115 15 err = cbs_h266_replace_vps(ctx, unit);
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (err < 0)
1117 return err;
1118 }
1119 15 break;
1120 888 case VVC_SPS_NUT:
1121 {
1122 888 H266RawSPS *sps = unit->content;
1123
1124 888 err = cbs_h266_read_sps(ctx, &gbc, sps);
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 888 times.
888 if (err < 0)
1126 return err;
1127
1128 888 err = cbs_h266_replace_sps(ctx, unit);
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 888 times.
888 if (err < 0)
1130 return err;
1131 }
1132 888 break;
1133
1134 1110 case VVC_PPS_NUT:
1135 {
1136 1110 H266RawPPS *pps = unit->content;
1137
1138 1110 err = cbs_h266_read_pps(ctx, &gbc, pps);
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1110 times.
1110 if (err < 0)
1140 return err;
1141
1142 1110 err = cbs_h266_replace_pps(ctx, unit);
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1110 times.
1110 if (err < 0)
1144 return err;
1145 }
1146 1110 break;
1147
1148 543 case VVC_PREFIX_APS_NUT:
1149 case VVC_SUFFIX_APS_NUT:
1150 {
1151 543 err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1152 543 unit->type == VVC_PREFIX_APS_NUT);
1153
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 if (err < 0)
1155 return err;
1156 }
1157 543 break;
1158 177 case VVC_PH_NUT:
1159 {
1160 177 H266RawPH *ph = unit->content;
1161 177 err = cbs_h266_read_ph(ctx, &gbc, ph);
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 if (err < 0)
1163 return err;
1164 177 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 if (err < 0)
1166 return err;
1167 }
1168 177 break;
1169
1170 8355 case VVC_TRAIL_NUT:
1171 case VVC_STSA_NUT:
1172 case VVC_RADL_NUT:
1173 case VVC_RASL_NUT:
1174 case VVC_IDR_W_RADL:
1175 case VVC_IDR_N_LP:
1176 case VVC_CRA_NUT:
1177 case VVC_GDR_NUT:
1178 {
1179 8355 H266RawSlice *slice = unit->content;
1180 int pos, len;
1181
1182 8355 err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8355 times.
8355 if (err < 0)
1184 return err;
1185
1186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8355 times.
8355 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1187 return AVERROR_INVALIDDATA;
1188
1189 8355 pos = get_bits_count(&gbc);
1190 8355 len = unit->data_size;
1191
1192
2/2
✓ Branch 0 taken 5857 times.
✓ Branch 1 taken 2498 times.
8355 if (slice->header.sh_picture_header_in_slice_header_flag) {
1193 5857 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5857 times.
5857 if (err < 0)
1195 return err;
1196 }
1197
1198 8355 slice->header_size = pos / 8;
1199 8355 slice->data_size = len - pos / 8;
1200 8355 slice->data_ref = av_buffer_ref(unit->data_ref);
1201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8355 times.
8355 if (!slice->data_ref)
1202 return AVERROR(ENOMEM);
1203 8355 slice->data = unit->data + pos / 8;
1204 8355 slice->data_bit_start = pos % 8;
1205 }
1206 8355 break;
1207
1208 49 case VVC_AUD_NUT:
1209 {
1210 49 err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (err < 0)
1212 return err;
1213 }
1214 49 break;
1215
1216 3159 case VVC_PREFIX_SEI_NUT:
1217 case VVC_SUFFIX_SEI_NUT:
1218 {
1219 3159 err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1220 3159 unit->type == VVC_PREFIX_SEI_NUT);
1221
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3159 times.
3159 if (err < 0)
1223 return err;
1224 }
1225 3159 break;
1226
1227 default:
1228 return AVERROR(ENOSYS);
1229 }
1230 14302 return 0;
1231 }
1232
1233 14194 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1234 PutBitContext *pbc, const uint8_t *data,
1235 size_t data_size, int data_bit_start)
1236 {
1237 14194 size_t rest = data_size - (data_bit_start + 7) / 8;
1238 14194 const uint8_t *pos = data + data_bit_start / 8;
1239
1240
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 &&
1241 data_size > data_bit_start / 8);
1242
1243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14194 times.
14194 if (data_size * 8 + 8 > put_bits_left(pbc))
1244 return AVERROR(ENOSPC);
1245
1246
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14193 times.
14194 if (!rest)
1247 1 goto rbsp_stop_one_bit;
1248
1249 // First copy the remaining bits of the first byte
1250 // The above check ensures that we do not accidentally
1251 // copy beyond the rbsp_stop_one_bit.
1252
2/2
✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 10464 times.
14193 if (data_bit_start % 8)
1253 3729 put_bits(pbc, 8 - data_bit_start % 8,
1254 3729 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1255
1256
2/2
✓ Branch 1 taken 14084 times.
✓ Branch 2 taken 109 times.
14193 if (put_bits_count(pbc) % 8 == 0) {
1257 // If the writer is aligned at this point,
1258 // memcpy can be used to improve performance.
1259 // This happens normally for CABAC.
1260 14084 flush_put_bits(pbc);
1261 14084 memcpy(put_bits_ptr(pbc), pos, rest);
1262 14084 skip_put_bytes(pbc, rest);
1263 } else {
1264 // If not, we have to copy manually.
1265 // rbsp_stop_one_bit forces us to special-case
1266 // the last byte.
1267 uint8_t temp;
1268 int i;
1269
1270
2/2
✓ Branch 0 taken 98762 times.
✓ Branch 1 taken 109 times.
98871 for (; rest > 4; rest -= 4, pos += 4)
1271 98762 put_bits32(pbc, AV_RB32(pos));
1272
1273
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 109 times.
274 for (; rest > 1; rest--, pos++)
1274 165 put_bits(pbc, 8, *pos);
1275
1276 109 rbsp_stop_one_bit:
1277
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);
1278
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 av_assert0(temp);
1280 110 i = ff_ctz(*pos);
1281 110 temp = temp >> i;
1282
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1283 110 put_bits(pbc, i, temp);
1284
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 18 times.
110 if (put_bits_count(pbc) % 8)
1285 92 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1286 }
1287
1288 14194 return 0;
1289 }
1290
1291 7953 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1292 CodedBitstreamUnit *unit,
1293 PutBitContext *pbc)
1294 {
1295 int err;
1296
1297
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) {
1298 73 case H264_NAL_SPS:
1299 {
1300 73 H264RawSPS *sps = unit->content;
1301
1302 73 err = cbs_h264_write_sps(ctx, pbc, sps);
1303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
1304 return err;
1305
1306 73 err = cbs_h264_replace_sps(ctx, unit);
1307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
1308 return err;
1309 }
1310 73 break;
1311
1312 case H264_NAL_SPS_EXT:
1313 {
1314 H264RawSPSExtension *sps_ext = unit->content;
1315
1316 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1317 if (err < 0)
1318 return err;
1319 }
1320 break;
1321
1322 1495 case H264_NAL_PPS:
1323 {
1324 1495 H264RawPPS *pps = unit->content;
1325
1326 1495 err = cbs_h264_write_pps(ctx, pbc, pps);
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
1328 return err;
1329
1330 1495 err = cbs_h264_replace_pps(ctx, unit);
1331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (err < 0)
1332 return err;
1333 }
1334 1495 break;
1335
1336 5575 case H264_NAL_SLICE:
1337 case H264_NAL_IDR_SLICE:
1338 case H264_NAL_AUXILIARY_SLICE:
1339 {
1340 5575 H264RawSlice *slice = unit->content;
1341
1342 5575 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5575 times.
5575 if (err < 0)
1344 return err;
1345
1346
1/2
✓ Branch 0 taken 5575 times.
✗ Branch 1 not taken.
5575 if (slice->data) {
1347 5575 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1348 slice->data_size,
1349 slice->data_bit_start);
1350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5575 times.
5575 if (err < 0)
1351 return err;
1352 } else {
1353 // No slice data - that was just the header.
1354 // (Bitstream may be unaligned!)
1355 }
1356 }
1357 5575 break;
1358
1359 302 case H264_NAL_AUD:
1360 {
1361 302 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (err < 0)
1363 return err;
1364 }
1365 302 break;
1366
1367 505 case H264_NAL_SEI:
1368 {
1369 505 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505 times.
505 if (err < 0)
1371 return err;
1372 }
1373 505 break;
1374
1375 case H264_NAL_FILLER_DATA:
1376 {
1377 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1378 if (err < 0)
1379 return err;
1380 }
1381 break;
1382
1383 1 case H264_NAL_END_SEQUENCE:
1384 {
1385 1 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1387 return err;
1388 }
1389 1 break;
1390
1391 2 case H264_NAL_END_STREAM:
1392 {
1393 2 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1395 return err;
1396 }
1397 2 break;
1398
1399 default:
1400 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1401 "NAL unit type %"PRIu32".\n", unit->type);
1402 return AVERROR_PATCHWELCOME;
1403 }
1404
1405 7953 return 0;
1406 }
1407
1408 8050 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1409 CodedBitstreamUnit *unit,
1410 PutBitContext *pbc)
1411 {
1412 int err;
1413
1414
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) {
1415 56 case HEVC_NAL_VPS:
1416 {
1417 56 H265RawVPS *vps = unit->content;
1418
1419 56 err = cbs_h265_write_vps(ctx, pbc, vps);
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
1421 return err;
1422
1423 56 err = cbs_h265_replace_vps(ctx, unit);
1424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (err < 0)
1425 return err;
1426 }
1427 56 break;
1428
1429 58 case HEVC_NAL_SPS:
1430 {
1431 58 H265RawSPS *sps = unit->content;
1432
1433 58 err = cbs_h265_write_sps(ctx, pbc, sps);
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
1435 return err;
1436
1437 58 err = cbs_h265_replace_sps(ctx, unit);
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
1439 return err;
1440 }
1441 58 break;
1442
1443 283 case HEVC_NAL_PPS:
1444 {
1445 283 H265RawPPS *pps = unit->content;
1446
1447 283 err = cbs_h265_write_pps(ctx, pbc, pps);
1448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
1449 return err;
1450
1451 283 err = cbs_h265_replace_pps(ctx, unit);
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (err < 0)
1453 return err;
1454 }
1455 283 break;
1456
1457 6031 case HEVC_NAL_TRAIL_N:
1458 case HEVC_NAL_TRAIL_R:
1459 case HEVC_NAL_TSA_N:
1460 case HEVC_NAL_TSA_R:
1461 case HEVC_NAL_STSA_N:
1462 case HEVC_NAL_STSA_R:
1463 case HEVC_NAL_RADL_N:
1464 case HEVC_NAL_RADL_R:
1465 case HEVC_NAL_RASL_N:
1466 case HEVC_NAL_RASL_R:
1467 case HEVC_NAL_BLA_W_LP:
1468 case HEVC_NAL_BLA_W_RADL:
1469 case HEVC_NAL_BLA_N_LP:
1470 case HEVC_NAL_IDR_W_RADL:
1471 case HEVC_NAL_IDR_N_LP:
1472 case HEVC_NAL_CRA_NUT:
1473 {
1474 6031 H265RawSlice *slice = unit->content;
1475
1476 6031 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6031 times.
6031 if (err < 0)
1478 return err;
1479
1480
1/2
✓ Branch 0 taken 6031 times.
✗ Branch 1 not taken.
6031 if (slice->data) {
1481 6031 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1482 slice->data_size,
1483 slice->data_bit_start);
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6031 times.
6031 if (err < 0)
1485 return err;
1486 } else {
1487 // No slice data - that was just the header.
1488 }
1489 }
1490 6031 break;
1491
1492 60 case HEVC_NAL_AUD:
1493 {
1494 60 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1496 return err;
1497 }
1498 60 break;
1499
1500 1562 case HEVC_NAL_SEI_PREFIX:
1501 case HEVC_NAL_SEI_SUFFIX:
1502 {
1503 1562 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1504 1562 unit->type == HEVC_NAL_SEI_PREFIX);
1505
1506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1562 times.
1562 if (err < 0)
1507 return err;
1508 }
1509 1562 break;
1510
1511 default:
1512 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1513 "NAL unit type %"PRIu32".\n", unit->type);
1514 return AVERROR_PATCHWELCOME;
1515 }
1516
1517 8050 return 0;
1518 }
1519
1520 756 static int cbs_h264_discarded_nal_unit(CodedBitstreamContext *ctx,
1521 const CodedBitstreamUnit *unit,
1522 enum AVDiscard skip)
1523 {
1524 H264RawNALUnitHeader *header;
1525 H264RawSliceHeader *slice;
1526 int slice_type_i, slice_type_b, slice_type_si;
1527
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 756 times.
756 if (skip <= AVDISCARD_DEFAULT)
1529 return 0;
1530
1531 // keep non-VCL
1532
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 500 times.
756 if (unit->type != H264_NAL_SLICE &&
1533
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 4 times.
256 unit->type != H264_NAL_IDR_SLICE &&
1534
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 unit->type != H264_NAL_AUXILIARY_SLICE)
1535 252 return 0;
1536
1537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
504 if (skip >= AVDISCARD_ALL)
1538 return 1;
1539
1540
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)
1541 125 return 1;
1542
1543 379 header = (H264RawNALUnitHeader *)unit->content;
1544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379 times.
379 if (!header) {
1545 av_log(ctx->log_ctx, AV_LOG_WARNING,
1546 "h264 nal unit header is null, missing decompose?\n");
1547 return 0;
1548 }
1549
1550
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)
1551 183 return 1;
1552
1553 196 slice = (H264RawSliceHeader *)unit->content;
1554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (!slice) {
1555 av_log(ctx->log_ctx, AV_LOG_WARNING,
1556 "h264 slice header is null, missing decompose?\n");
1557 return 0;
1558 }
1559
1560 196 slice_type_i = slice->slice_type % 5 == 2;
1561 196 slice_type_b = slice->slice_type % 5 == 1;
1562 196 slice_type_si = slice->slice_type % 5 == 4;
1563
1564
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)
1565 60 return 1;
1566
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)
1567 29 return 1;
1568
1569 107 return 0;
1570 }
1571
1572 536 static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx,
1573 const CodedBitstreamUnit *unit,
1574 enum AVDiscard skip)
1575 {
1576 H265RawSliceHeader *slice;
1577
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536 times.
536 if (skip <= AVDISCARD_DEFAULT)
1579 return 0;
1580
1581
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 204 times.
536 switch (unit->type) {
1582 4 case HEVC_NAL_BLA_W_LP:
1583 case HEVC_NAL_BLA_W_RADL:
1584 case HEVC_NAL_BLA_N_LP:
1585 case HEVC_NAL_IDR_W_RADL:
1586 case HEVC_NAL_IDR_N_LP:
1587 case HEVC_NAL_CRA_NUT:
1588 // IRAP slice
1589
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (skip < AVDISCARD_ALL)
1590 4 return 0;
1591 break;
1592
1593 328 case HEVC_NAL_TRAIL_R:
1594 case HEVC_NAL_TRAIL_N:
1595 case HEVC_NAL_TSA_N:
1596 case HEVC_NAL_TSA_R:
1597 case HEVC_NAL_STSA_N:
1598 case HEVC_NAL_STSA_R:
1599 case HEVC_NAL_RADL_N:
1600 case HEVC_NAL_RADL_R:
1601 case HEVC_NAL_RASL_N:
1602 case HEVC_NAL_RASL_R:
1603 // Slice
1604 328 break;
1605 204 default:
1606 // Don't discard non-slice nal.
1607 204 return 0;
1608 }
1609
1610
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 281 times.
328 if (skip >= AVDISCARD_NONKEY)
1611 47 return 1;
1612
1613 281 slice = (H265RawSliceHeader *)unit->content;
1614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 if (!slice) {
1615 av_log(ctx->log_ctx, AV_LOG_WARNING,
1616 "h265 slice header is null, missing decompose?\n");
1617 return 0;
1618 }
1619
1620
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)
1621 47 return 1;
1622
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)
1623 47 return 1;
1624
1625
1/2
✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
187 if (skip >= AVDISCARD_NONREF) {
1626
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 163 times.
187 switch (unit->type) {
1627 24 case HEVC_NAL_TRAIL_N:
1628 case HEVC_NAL_TSA_N:
1629 case HEVC_NAL_STSA_N:
1630 case HEVC_NAL_RADL_N:
1631 case HEVC_NAL_RASL_N:
1632 case HEVC_NAL_VCL_N10:
1633 case HEVC_NAL_VCL_N12:
1634 case HEVC_NAL_VCL_N14:
1635 // non-ref
1636 24 return 1;
1637 163 default:
1638 163 break;
1639 }
1640 }
1641
1642 163 return 0;
1643 }
1644
1645 5777 static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
1646 CodedBitstreamUnit *unit,
1647 PutBitContext *pbc)
1648 {
1649 int err;
1650
1651
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) {
1652 1 case VVC_DCI_NUT:
1653 {
1654 1 H266RawDCI *dci = unit->content;
1655
1656 1 err = cbs_h266_write_dci(ctx, pbc, dci);
1657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1658 return err;
1659 }
1660 1 break;
1661 1 case VVC_OPI_NUT:
1662 {
1663 1 H266RawOPI *opi = unit->content;
1664
1665 1 err = cbs_h266_write_opi(ctx, pbc, opi);
1666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1667 return err;
1668 }
1669 1 break;
1670 4 case VVC_VPS_NUT:
1671 {
1672 4 H266RawVPS *vps = unit->content;
1673
1674 4 err = cbs_h266_write_vps(ctx, pbc, vps);
1675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1676 return err;
1677
1678 4 err = cbs_h266_replace_vps(ctx, unit);
1679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (err < 0)
1680 return err;
1681 }
1682 4 break;
1683 335 case VVC_SPS_NUT:
1684 {
1685 335 H266RawSPS *sps = unit->content;
1686
1687 335 err = cbs_h266_write_sps(ctx, pbc, sps);
1688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1689 return err;
1690
1691 335 err = cbs_h266_replace_sps(ctx, unit);
1692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (err < 0)
1693 return err;
1694 }
1695 335 break;
1696
1697 391 case VVC_PPS_NUT:
1698 {
1699 391 H266RawPPS *pps = unit->content;
1700
1701 391 err = cbs_h266_write_pps(ctx, pbc, pps);
1702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1703 return err;
1704
1705 391 err = cbs_h266_replace_pps(ctx, unit);
1706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (err < 0)
1707 return err;
1708 }
1709 391 break;
1710
1711 228 case VVC_PREFIX_APS_NUT:
1712 case VVC_SUFFIX_APS_NUT:
1713 {
1714 228 err = cbs_h266_write_aps(ctx, pbc, unit->content,
1715 228 unit->type == VVC_PREFIX_APS_NUT);
1716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 if (err < 0)
1717 return err;
1718 }
1719 228 break;
1720 27 case VVC_PH_NUT:
1721 {
1722 27 H266RawPH *ph = unit->content;
1723 27 err = cbs_h266_write_ph(ctx, pbc, ph);
1724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1725 return err;
1726
1727 27 err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
1729 return err;
1730 }
1731 27 break;
1732
1733 2588 case VVC_TRAIL_NUT:
1734 case VVC_STSA_NUT:
1735 case VVC_RADL_NUT:
1736 case VVC_RASL_NUT:
1737 case VVC_IDR_W_RADL:
1738 case VVC_IDR_N_LP:
1739 case VVC_CRA_NUT:
1740 case VVC_GDR_NUT:
1741 {
1742 2588 H266RawSlice *slice = unit->content;
1743
1744 2588 err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1746 return err;
1747
1748
2/2
✓ Branch 0 taken 2103 times.
✓ Branch 1 taken 485 times.
2588 if (slice->header.sh_picture_header_in_slice_header_flag) {
1749 2103 err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2103 times.
2103 if (err < 0)
1751 return err;
1752 }
1753
1754
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (slice->data) {
1755 2588 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1756 slice->data_size,
1757 slice->data_bit_start);
1758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 if (err < 0)
1759 return err;
1760 } else {
1761 // No slice data - that was just the header.
1762 }
1763 }
1764 2588 break;
1765
1766 12 case VVC_AUD_NUT:
1767 {
1768 12 err = cbs_h266_write_aud(ctx, pbc, unit->content);
1769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (err < 0)
1770 return err;
1771 }
1772 12 break;
1773
1774 2190 case VVC_PREFIX_SEI_NUT:
1775 case VVC_SUFFIX_SEI_NUT:
1776 {
1777 2190 err = cbs_h266_write_sei(ctx, pbc, unit->content,
1778 2190 unit->type == VVC_PREFIX_SEI_NUT);
1779
1780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2190 times.
2190 if (err < 0)
1781 return err;
1782 }
1783 2190 break;
1784
1785 default:
1786 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1787 "NAL unit type %"PRIu32".\n", unit->type);
1788 return AVERROR_PATCHWELCOME;
1789 }
1790
1791 5777 return 0;
1792 }
1793
1794 21780 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
1795 CodedBitstreamUnitType type,
1796 int nal_unit_index)
1797 {
1798 // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1799
2/2
✓ Branch 0 taken 7246 times.
✓ Branch 1 taken 14534 times.
21780 if (nal_unit_index == 0) {
1800 // Assume that this is the first NAL unit in an access unit.
1801 7246 return 1;
1802 }
1803
2/2
✓ Branch 0 taken 5022 times.
✓ Branch 1 taken 9512 times.
14534 if (codec_id == AV_CODEC_ID_H264)
1804
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;
1805
2/2
✓ Branch 0 taken 5867 times.
✓ Branch 1 taken 3645 times.
9512 if (codec_id == AV_CODEC_ID_HEVC)
1806
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;
1807
1/2
✓ Branch 0 taken 3645 times.
✗ Branch 1 not taken.
3645 if (codec_id == AV_CODEC_ID_VVC)
1808
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;
1809 return 0;
1810 }
1811
1812 7247 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1813 CodedBitstreamFragment *frag)
1814 {
1815 uint8_t *data;
1816 size_t max_size, dp, sp;
1817 int err, i, zero_run;
1818
1819
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1820 // Data should already all have been written when we get here.
1821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21780 times.
21780 av_assert0(frag->units[i].data);
1822 }
1823
1824 7247 max_size = 0;
1825
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1826 // Start code + content with worst-case emulation prevention.
1827 21780 max_size += 4 + frag->units[i].data_size * 3 / 2;
1828 }
1829
1830 7247 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (!data)
1832 return AVERROR(ENOMEM);
1833
1834 7247 dp = 0;
1835
2/2
✓ Branch 0 taken 21780 times.
✓ Branch 1 taken 7247 times.
29027 for (i = 0; i < frag->nb_units; i++) {
1836 21780 CodedBitstreamUnit *unit = &frag->units[i];
1837
1838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21780 times.
21780 if (unit->data_bit_padding > 0) {
1839 if (i < frag->nb_units - 1)
1840 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1841 "unaligned padding on non-final NAL unit.\n");
1842 else
1843 frag->data_bit_padding = unit->data_bit_padding;
1844 }
1845
1846
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)) {
1847 // zero_byte
1848 8257 data[dp++] = 0;
1849 }
1850 // start_code_prefix_one_3bytes
1851 21780 data[dp++] = 0;
1852 21780 data[dp++] = 0;
1853 21780 data[dp++] = 1;
1854
1855 21780 zero_run = 0;
1856
2/2
✓ Branch 0 taken 16462517 times.
✓ Branch 1 taken 21780 times.
16484297 for (sp = 0; sp < unit->data_size; sp++) {
1857
2/2
✓ Branch 0 taken 16455111 times.
✓ Branch 1 taken 7406 times.
16462517 if (zero_run < 2) {
1858
2/2
✓ Branch 0 taken 147107 times.
✓ Branch 1 taken 16308004 times.
16455111 if (unit->data[sp] == 0)
1859 147107 ++zero_run;
1860 else
1861 16308004 zero_run = 0;
1862 } else {
1863
2/2
✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 6119 times.
7406 if ((unit->data[sp] & ~3) == 0) {
1864 // emulation_prevention_three_byte
1865 1287 data[dp++] = 3;
1866 }
1867 7406 zero_run = unit->data[sp] == 0;
1868 }
1869 16462517 data[dp++] = unit->data[sp];
1870 }
1871 }
1872
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 av_assert0(dp <= max_size);
1874 7247 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (err)
1876 return err;
1877 7247 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1878
1879 7247 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1880 NULL, NULL, 0);
1881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7247 times.
7247 if (!frag->data_ref) {
1882 av_freep(&data);
1883 return AVERROR(ENOMEM);
1884 }
1885
1886 7247 frag->data = data;
1887 7247 frag->data_size = dp;
1888
1889 7247 return 0;
1890 }
1891
1892 static void cbs_h264_flush(CodedBitstreamContext *ctx)
1893 {
1894 CodedBitstreamH264Context *h264 = ctx->priv_data;
1895
1896 for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1897 ff_refstruct_unref(&h264->sps[i]);
1898 for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1899 ff_refstruct_unref(&h264->pps[i]);
1900
1901 h264->active_sps = NULL;
1902 h264->active_pps = NULL;
1903 h264->last_slice_nal_unit_type = 0;
1904 }
1905
1906 44 static void cbs_h264_close(CodedBitstreamContext *ctx)
1907 {
1908 44 CodedBitstreamH264Context *h264 = ctx->priv_data;
1909 int i;
1910
1911 44 ff_h2645_packet_uninit(&h264->common.read_packet);
1912
1913
2/2
✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 44 times.
1452 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1914 1408 ff_refstruct_unref(&h264->sps[i]);
1915
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 44 times.
11308 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1916 11264 ff_refstruct_unref(&h264->pps[i]);
1917 44 }
1918
1919 static void cbs_h265_flush(CodedBitstreamContext *ctx)
1920 {
1921 CodedBitstreamH265Context *h265 = ctx->priv_data;
1922
1923 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1924 ff_refstruct_unref(&h265->vps[i]);
1925 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1926 ff_refstruct_unref(&h265->sps[i]);
1927 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1928 ff_refstruct_unref(&h265->pps[i]);
1929
1930 h265->active_vps = NULL;
1931 h265->active_sps = NULL;
1932 h265->active_pps = NULL;
1933 }
1934
1935 44 static void cbs_h265_close(CodedBitstreamContext *ctx)
1936 {
1937 44 CodedBitstreamH265Context *h265 = ctx->priv_data;
1938 int i;
1939
1940 44 ff_h2645_packet_uninit(&h265->common.read_packet);
1941
1942
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 44 times.
748 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1943 704 ff_refstruct_unref(&h265->vps[i]);
1944
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 44 times.
748 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1945 704 ff_refstruct_unref(&h265->sps[i]);
1946
2/2
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 44 times.
2860 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1947 2816 ff_refstruct_unref(&h265->pps[i]);
1948 44 }
1949
1950 186 static void cbs_h266_flush(CodedBitstreamContext *ctx)
1951 {
1952 186 CodedBitstreamH266Context *h266 = ctx->priv_data;
1953
1954
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 186 times.
3162 for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1955 2976 ff_refstruct_unref(&h266->vps[i]);
1956
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 186 times.
3162 for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1957 2976 ff_refstruct_unref(&h266->sps[i]);
1958
2/2
✓ Branch 0 taken 11904 times.
✓ Branch 1 taken 186 times.
12090 for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1959 11904 ff_refstruct_unref(&h266->pps[i]);
1960 186 ff_refstruct_unref(&h266->ph_ref);
1961 186 }
1962
1963 186 static void cbs_h266_close(CodedBitstreamContext *ctx)
1964 {
1965 186 CodedBitstreamH266Context *h266 = ctx->priv_data;
1966
1967 186 cbs_h266_flush(ctx);
1968 186 ff_h2645_packet_uninit(&h266->common.read_packet);
1969 186 }
1970
1971 913 static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
1972 {
1973 913 H264RawSEI *sei = content;
1974 913 ff_cbs_sei_free_message_list(&sei->message_list);
1975 913 }
1976
1977 static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
1978 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
1979 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
1980
1981 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
1982
1983 CBS_UNIT_TYPES_INTERNAL_REF((H264_NAL_IDR_SLICE,
1984 H264_NAL_SLICE,
1985 H264_NAL_AUXILIARY_SLICE), H264RawSlice, data),
1986
1987 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
1988 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
1989 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
1990 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
1991
1992 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
1993
1994 CBS_UNIT_TYPE_END_OF_LIST
1995 };
1996
1997 1727 static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
1998 {
1999 1727 H265RawSEI *sei = content;
2000 1727 ff_cbs_sei_free_message_list(&sei->message_list);
2001 1727 }
2002
2003 static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
2004 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_VPS, H265RawVPS, extension_data.data),
2005 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
2006 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
2007
2008 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
2009
2010 // Slices of non-IRAP pictures.
2011 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_TRAIL_N, HEVC_NAL_RASL_R,
2012 H265RawSlice, data),
2013 // Slices of IRAP pictures.
2014 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_BLA_W_LP, HEVC_NAL_CRA_NUT,
2015 H265RawSlice, data),
2016
2017 CBS_UNIT_TYPES_COMPLEX((HEVC_NAL_SEI_PREFIX, HEVC_NAL_SEI_SUFFIX),
2018 H265RawSEI, cbs_h265_free_sei),
2019
2020 CBS_UNIT_TYPE_END_OF_LIST
2021 };
2022
2023 3159 static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
2024 {
2025 3159 H266RawSEI *sei = content;
2026 3159 ff_cbs_sei_free_message_list(&sei->message_list);
2027 3159 }
2028
2029 static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[] = {
2030 CBS_UNIT_TYPE_INTERNAL_REF(VVC_DCI_NUT, H266RawDCI, extension_data.data),
2031 CBS_UNIT_TYPE_INTERNAL_REF(VVC_OPI_NUT, H266RawOPI, extension_data.data),
2032 CBS_UNIT_TYPE_INTERNAL_REF(VVC_VPS_NUT, H266RawVPS, extension_data.data),
2033 {
2034 .nb_unit_types = 1,
2035 .unit_type.list[0] = VVC_SPS_NUT,
2036 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2037 .content_size = sizeof(H266RawSPS),
2038 .type.ref = {
2039 .nb_offsets = 2,
2040 .offsets = { offsetof(H266RawSPS, extension_data.data),
2041 offsetof(H266RawSPS, vui.extension_data.data) }
2042 },
2043 },
2044 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PPS_NUT, H266RawPPS, extension_data.data),
2045 CBS_UNIT_TYPE_INTERNAL_REF(VVC_PREFIX_APS_NUT, H266RawAPS, extension_data.data),
2046 CBS_UNIT_TYPE_INTERNAL_REF(VVC_SUFFIX_APS_NUT, H266RawAPS, extension_data.data),
2047
2048 CBS_UNIT_TYPE_POD(VVC_PH_NUT , H266RawPH),
2049 CBS_UNIT_TYPE_POD(VVC_AUD_NUT, H266RawAUD),
2050
2051 CBS_UNIT_RANGE_INTERNAL_REF(VVC_TRAIL_NUT, VVC_RASL_NUT,
2052 H266RawSlice, data),
2053
2054 CBS_UNIT_RANGE_INTERNAL_REF(VVC_IDR_W_RADL, VVC_GDR_NUT,
2055 H266RawSlice, data),
2056
2057 CBS_UNIT_TYPES_COMPLEX((VVC_PREFIX_SEI_NUT, VVC_SUFFIX_SEI_NUT),
2058 H266RawSEI, cbs_h266_free_sei),
2059
2060 CBS_UNIT_TYPE_END_OF_LIST
2061 };
2062
2063 const CodedBitstreamType ff_cbs_type_h264 = {
2064 .codec_id = AV_CODEC_ID_H264,
2065
2066 .priv_data_size = sizeof(CodedBitstreamH264Context),
2067
2068 .unit_types = cbs_h264_unit_types,
2069
2070 .split_fragment = &cbs_h2645_split_fragment,
2071 .read_unit = &cbs_h264_read_nal_unit,
2072 .write_unit = &cbs_h264_write_nal_unit,
2073 .discarded_unit = &cbs_h264_discarded_nal_unit,
2074 .assemble_fragment = &cbs_h2645_assemble_fragment,
2075
2076 .flush = &cbs_h264_flush,
2077 .close = &cbs_h264_close,
2078 };
2079
2080 const CodedBitstreamType ff_cbs_type_h265 = {
2081 .codec_id = AV_CODEC_ID_HEVC,
2082
2083 .priv_data_size = sizeof(CodedBitstreamH265Context),
2084
2085 .unit_types = cbs_h265_unit_types,
2086
2087 .split_fragment = &cbs_h2645_split_fragment,
2088 .read_unit = &cbs_h265_read_nal_unit,
2089 .write_unit = &cbs_h265_write_nal_unit,
2090 .discarded_unit = &cbs_h265_discarded_nal_unit,
2091 .assemble_fragment = &cbs_h2645_assemble_fragment,
2092
2093 .flush = &cbs_h265_flush,
2094 .close = &cbs_h265_close,
2095 };
2096
2097 const CodedBitstreamType ff_cbs_type_h266 = {
2098 .codec_id = AV_CODEC_ID_VVC,
2099
2100 .priv_data_size = sizeof(CodedBitstreamH266Context),
2101
2102 .unit_types = cbs_h266_unit_types,
2103
2104 .split_fragment = &cbs_h2645_split_fragment,
2105 .read_unit = &cbs_h266_read_nal_unit,
2106 .write_unit = &cbs_h266_write_nal_unit,
2107 .assemble_fragment = &cbs_h2645_assemble_fragment,
2108
2109 .flush = &cbs_h266_flush,
2110 .close = &cbs_h266_close,
2111 };
2112
2113 // Macro for the read/write pair.
2114 #define SEI_MESSAGE_RW(codec, name) \
2115 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2116 .write = cbs_ ## codec ## _write_ ## name ## _internal
2117
2118 static const SEIMessageTypeDescriptor cbs_sei_common_types[] = {
2119 {
2120 SEI_TYPE_FILLER_PAYLOAD,
2121 1, 1,
2122 sizeof(SEIRawFillerPayload),
2123 SEI_MESSAGE_RW(sei, filler_payload),
2124 },
2125 {
2126 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
2127 1, 1,
2128 sizeof(SEIRawUserDataRegistered),
2129 SEI_MESSAGE_RW(sei, user_data_registered),
2130 },
2131 {
2132 SEI_TYPE_USER_DATA_UNREGISTERED,
2133 1, 1,
2134 sizeof(SEIRawUserDataUnregistered),
2135 SEI_MESSAGE_RW(sei, user_data_unregistered),
2136 },
2137 {
2138 SEI_TYPE_FRAME_PACKING_ARRANGEMENT,
2139 1, 0,
2140 sizeof(SEIRawFramePackingArrangement),
2141 SEI_MESSAGE_RW(sei, frame_packing_arrangement),
2142 },
2143 {
2144 SEI_TYPE_DECODED_PICTURE_HASH,
2145 0, 1,
2146 sizeof(SEIRawDecodedPictureHash),
2147 SEI_MESSAGE_RW(sei, decoded_picture_hash),
2148 },
2149 {
2150 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
2151 1, 0,
2152 sizeof(SEIRawMasteringDisplayColourVolume),
2153 SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2154 },
2155 {
2156 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
2157 1, 0,
2158 sizeof(SEIRawContentLightLevelInfo),
2159 SEI_MESSAGE_RW(sei, content_light_level_info),
2160 },
2161 {
2162 SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS,
2163 1, 0,
2164 sizeof(SEIRawAlternativeTransferCharacteristics),
2165 SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2166 },
2167 {
2168 SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT,
2169 1, 0,
2170 sizeof(SEIRawAmbientViewingEnvironment),
2171 SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2172 },
2173 SEI_MESSAGE_TYPE_END,
2174 };
2175
2176 static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = {
2177 {
2178 SEI_TYPE_BUFFERING_PERIOD,
2179 1, 0,
2180 sizeof(H264RawSEIBufferingPeriod),
2181 SEI_MESSAGE_RW(h264, sei_buffering_period),
2182 },
2183 {
2184 SEI_TYPE_PIC_TIMING,
2185 1, 0,
2186 sizeof(H264RawSEIPicTiming),
2187 SEI_MESSAGE_RW(h264, sei_pic_timing),
2188 },
2189 {
2190 SEI_TYPE_PAN_SCAN_RECT,
2191 1, 0,
2192 sizeof(H264RawSEIPanScanRect),
2193 SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2194 },
2195 {
2196 SEI_TYPE_RECOVERY_POINT,
2197 1, 0,
2198 sizeof(H264RawSEIRecoveryPoint),
2199 SEI_MESSAGE_RW(h264, sei_recovery_point),
2200 },
2201 {
2202 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2203 1, 0,
2204 sizeof(H264RawFilmGrainCharacteristics),
2205 SEI_MESSAGE_RW(h264, film_grain_characteristics),
2206 },
2207 {
2208 SEI_TYPE_FRAME_PACKING_ARRANGEMENT,
2209 1, 0,
2210 sizeof(H264RawSEIFramePackingArrangement),
2211 SEI_MESSAGE_RW(h264, sei_frame_packing_arrangement),
2212 },
2213 {
2214 SEI_TYPE_DISPLAY_ORIENTATION,
2215 1, 0,
2216 sizeof(H264RawSEIDisplayOrientation),
2217 SEI_MESSAGE_RW(h264, sei_display_orientation),
2218 },
2219 SEI_MESSAGE_TYPE_END
2220 };
2221
2222 static const SEIMessageTypeDescriptor cbs_sei_h265_types[] = {
2223 {
2224 SEI_TYPE_BUFFERING_PERIOD,
2225 1, 0,
2226 sizeof(H265RawSEIBufferingPeriod),
2227 SEI_MESSAGE_RW(h265, sei_buffering_period),
2228 },
2229 {
2230 SEI_TYPE_PIC_TIMING,
2231 1, 0,
2232 sizeof(H265RawSEIPicTiming),
2233 SEI_MESSAGE_RW(h265, sei_pic_timing),
2234 },
2235 {
2236 SEI_TYPE_PAN_SCAN_RECT,
2237 1, 0,
2238 sizeof(H265RawSEIPanScanRect),
2239 SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2240 },
2241 {
2242 SEI_TYPE_RECOVERY_POINT,
2243 1, 0,
2244 sizeof(H265RawSEIRecoveryPoint),
2245 SEI_MESSAGE_RW(h265, sei_recovery_point),
2246 },
2247 {
2248 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
2249 1, 0,
2250 sizeof(H265RawFilmGrainCharacteristics),
2251 SEI_MESSAGE_RW(h265, film_grain_characteristics),
2252 },
2253 {
2254 SEI_TYPE_DISPLAY_ORIENTATION,
2255 1, 0,
2256 sizeof(H265RawSEIDisplayOrientation),
2257 SEI_MESSAGE_RW(h265, sei_display_orientation),
2258 },
2259 {
2260 SEI_TYPE_ACTIVE_PARAMETER_SETS,
2261 1, 0,
2262 sizeof(H265RawSEIActiveParameterSets),
2263 SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2264 },
2265 {
2266 SEI_TYPE_DECODED_PICTURE_HASH,
2267 0, 1,
2268 sizeof(H265RawSEIDecodedPictureHash),
2269 SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2270 },
2271 {
2272 SEI_TYPE_TIME_CODE,
2273 1, 0,
2274 sizeof(H265RawSEITimeCode),
2275 SEI_MESSAGE_RW(h265, sei_time_code),
2276 },
2277 {
2278 SEI_TYPE_ALPHA_CHANNEL_INFO,
2279 1, 0,
2280 sizeof(H265RawSEIAlphaChannelInfo),
2281 SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2282 },
2283 {
2284 SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO,
2285 1, 0,
2286 sizeof(H265RawSEI3DReferenceDisplaysInfo),
2287 SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
2288 },
2289 SEI_MESSAGE_TYPE_END
2290 };
2291
2292 static const SEIMessageTypeDescriptor cbs_sei_h266_types[] = {
2293 SEI_MESSAGE_TYPE_END
2294 };
2295
2296 14373 const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx,
2297 int payload_type)
2298 {
2299 const SEIMessageTypeDescriptor *codec_list;
2300 int i;
2301
2302
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) {
2303 1983 case AV_CODEC_ID_H264:
2304 1983 codec_list = cbs_sei_h264_types;
2305 1983 break;
2306 4851 case AV_CODEC_ID_H265:
2307 4851 codec_list = cbs_sei_h265_types;
2308 4851 break;
2309 7539 case AV_CODEC_ID_H266:
2310 7539 codec_list = cbs_sei_h266_types;
2311 7539 break;
2312 default:
2313 return NULL;
2314 }
2315
2316
2/2
✓ Branch 0 taken 40628 times.
✓ Branch 1 taken 7692 times.
48320 for (i = 0; codec_list[i].type >= 0; i++) {
2317
2/2
✓ Branch 0 taken 6681 times.
✓ Branch 1 taken 33947 times.
40628 if (codec_list[i].type == payload_type)
2318 6681 return &codec_list[i];
2319 }
2320
2321
2/2
✓ Branch 0 taken 39194 times.
✓ Branch 1 taken 269 times.
39463 for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2322
2/2
✓ Branch 0 taken 7423 times.
✓ Branch 1 taken 31771 times.
39194 if (cbs_sei_common_types[i].type == payload_type)
2323 7423 return &cbs_sei_common_types[i];
2324 }
2325
2326 269 return NULL;
2327 }
2328