FFmpeg coverage


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