FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 403 640 63.0%
Branches: 248 460 53.9%

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
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h264.h"
26 #include "cbs_h265.h"
27 #include "h264.h"
28 #include "h2645_parse.h"
29 #include "hevc.h"
30
31
32 69410 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
33 const char *name, const int *subscripts,
34 uint32_t *write_to,
35 uint32_t range_min, uint32_t range_max)
36 {
37 uint32_t value;
38 int position, i, j;
39 unsigned int k;
40 char bits[65];
41
42 69410 position = get_bits_count(gbc);
43
44
1/2
✓ Branch 0 taken 117634 times.
✗ Branch 1 not taken.
117634 for (i = 0; i < 32; i++) {
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117634 times.
117634 if (get_bits_left(gbc) < i + 1) {
46 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
47 "%s: bitstream ended.\n", name);
48 return AVERROR_INVALIDDATA;
49 }
50 117634 k = get_bits1(gbc);
51
2/2
✓ Branch 0 taken 69410 times.
✓ Branch 1 taken 48224 times.
117634 bits[i] = k ? '1' : '0';
52
2/2
✓ Branch 0 taken 69410 times.
✓ Branch 1 taken 48224 times.
117634 if (k)
53 69410 break;
54 }
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69410 times.
69410 if (i >= 32) {
56 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
57 "%s: more than 31 zeroes.\n", name);
58 return AVERROR_INVALIDDATA;
59 }
60 69410 value = 1;
61
2/2
✓ Branch 0 taken 48224 times.
✓ Branch 1 taken 69410 times.
117634 for (j = 0; j < i; j++) {
62 48224 k = get_bits1(gbc);
63
2/2
✓ Branch 0 taken 16795 times.
✓ Branch 1 taken 31429 times.
48224 bits[i + j + 1] = k ? '1' : '0';
64 48224 value = value << 1 | k;
65 }
66 69410 bits[i + j + 1] = 0;
67 69410 --value;
68
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69410 times.
69410 if (ctx->trace_enable)
70 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
71 bits, value);
72
73
2/4
✓ Branch 0 taken 69410 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 69410 times.
69410 if (value < range_min || value > range_max) {
74 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
75 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
76 name, value, range_min, range_max);
77 return AVERROR_INVALIDDATA;
78 }
79
80 69410 *write_to = value;
81 69410 return 0;
82 }
83
84 75720 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
85 const char *name, const int *subscripts,
86 int32_t *write_to,
87 int32_t range_min, int32_t range_max)
88 {
89 int32_t value;
90 int position, i, j;
91 unsigned int k;
92 uint32_t v;
93 char bits[65];
94
95 75720 position = get_bits_count(gbc);
96
97
1/2
✓ Branch 0 taken 268098 times.
✗ Branch 1 not taken.
268098 for (i = 0; i < 32; i++) {
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 268098 times.
268098 if (get_bits_left(gbc) < i + 1) {
99 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
100 "%s: bitstream ended.\n", name);
101 return AVERROR_INVALIDDATA;
102 }
103 268098 k = get_bits1(gbc);
104
2/2
✓ Branch 0 taken 75720 times.
✓ Branch 1 taken 192378 times.
268098 bits[i] = k ? '1' : '0';
105
2/2
✓ Branch 0 taken 75720 times.
✓ Branch 1 taken 192378 times.
268098 if (k)
106 75720 break;
107 }
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75720 times.
75720 if (i >= 32) {
109 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
110 "%s: more than 31 zeroes.\n", name);
111 return AVERROR_INVALIDDATA;
112 }
113 75720 v = 1;
114
2/2
✓ Branch 0 taken 192378 times.
✓ Branch 1 taken 75720 times.
268098 for (j = 0; j < i; j++) {
115 192378 k = get_bits1(gbc);
116
2/2
✓ Branch 0 taken 85639 times.
✓ Branch 1 taken 106739 times.
192378 bits[i + j + 1] = k ? '1' : '0';
117 192378 v = v << 1 | k;
118 }
119 75720 bits[i + j + 1] = 0;
120
2/2
✓ Branch 0 taken 43802 times.
✓ Branch 1 taken 31918 times.
75720 if (v & 1)
121 43802 value = -(int32_t)(v / 2);
122 else
123 31918 value = v / 2;
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75720 times.
75720 if (ctx->trace_enable)
126 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
127 bits, value);
128
129
2/4
✓ Branch 0 taken 75720 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 75720 times.
75720 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 75720 *write_to = value;
137 75720 return 0;
138 }
139
140 69918 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/4
✓ Branch 0 taken 69918 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 69918 times.
69918 if (value < range_min || value > range_max) {
148 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
149 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
150 name, value, range_min, range_max);
151 return AVERROR_INVALIDDATA;
152 }
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69918 times.
69918 av_assert0(value != UINT32_MAX);
154
155 69918 len = av_log2(value + 1);
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 69918 times.
69918 if (put_bits_left(pbc) < 2 * len + 1)
157 return AVERROR(ENOSPC);
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69918 times.
69918 if (ctx->trace_enable) {
160 char bits[65];
161 int i;
162
163 for (i = 0; i < len; i++)
164 bits[i] = '0';
165 bits[len] = '1';
166 for (i = 0; i < len; i++)
167 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
168 bits[len + len + 1] = 0;
169
170 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
171 name, subscripts, bits, value);
172 }
173
174 69918 put_bits(pbc, len, 0);
175
1/2
✓ Branch 0 taken 69918 times.
✗ Branch 1 not taken.
69918 if (len + 1 < 32)
176 69918 put_bits(pbc, len + 1, value + 1);
177 else
178 put_bits32(pbc, value + 1);
179
180 69918 return 0;
181 }
182
183 75744 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
184 const char *name, const int *subscripts,
185 int32_t value,
186 int32_t range_min, int32_t range_max)
187 {
188 int len;
189 uint32_t uvalue;
190
191
2/4
✓ Branch 0 taken 75744 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 75744 times.
75744 if (value < range_min || value > range_max) {
192 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
193 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
194 name, value, range_min, range_max);
195 return AVERROR_INVALIDDATA;
196 }
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75744 times.
75744 av_assert0(value != INT32_MIN);
198
199
2/2
✓ Branch 0 taken 17169 times.
✓ Branch 1 taken 58575 times.
75744 if (value == 0)
200 17169 uvalue = 0;
201
2/2
✓ Branch 0 taken 31928 times.
✓ Branch 1 taken 26647 times.
58575 else if (value > 0)
202 31928 uvalue = 2 * (uint32_t)value - 1;
203 else
204 26647 uvalue = 2 * (uint32_t)-value;
205
206 75744 len = av_log2(uvalue + 1);
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 75744 times.
75744 if (put_bits_left(pbc) < 2 * len + 1)
208 return AVERROR(ENOSPC);
209
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75744 times.
75744 if (ctx->trace_enable) {
211 char bits[65];
212 int i;
213
214 for (i = 0; i < len; i++)
215 bits[i] = '0';
216 bits[len] = '1';
217 for (i = 0; i < len; i++)
218 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
219 bits[len + len + 1] = 0;
220
221 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
222 name, subscripts, bits, value);
223 }
224
225 75744 put_bits(pbc, len, 0);
226
1/2
✓ Branch 0 taken 75744 times.
✗ Branch 1 not taken.
75744 if (len + 1 < 32)
227 75744 put_bits(pbc, len + 1, uvalue + 1);
228 else
229 put_bits32(pbc, uvalue + 1);
230
231 75744 return 0;
232 }
233
234 // payload_extension_present() - true if we are before the last 1-bit
235 // in the payload structure, which must be in the last byte.
236 5 static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
237 int cur_pos)
238 {
239 5 int bits_left = payload_size * 8 - cur_pos;
240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 return (bits_left > 0 &&
241 (bits_left > 7 || show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1)));
242 }
243
244 #define HEADER(name) do { \
245 ff_cbs_trace_header(ctx, name); \
246 } while (0)
247
248 #define CHECK(call) do { \
249 err = (call); \
250 if (err < 0) \
251 return err; \
252 } while (0)
253
254 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
255 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
256 #define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
257 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
258 #define FUNC_SEI(name) FUNC_NAME1(READWRITE, sei, name)
259
260 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
261
262 #define u(width, name, range_min, range_max) \
263 xu(width, name, current->name, range_min, range_max, 0, )
264 #define ub(width, name) \
265 xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
266 #define flag(name) ub(1, name)
267 #define ue(name, range_min, range_max) \
268 xue(name, current->name, range_min, range_max, 0, )
269 #define i(width, name, range_min, range_max) \
270 xi(width, name, current->name, range_min, range_max, 0, )
271 #define ib(width, name) \
272 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
273 #define se(name, range_min, range_max) \
274 xse(name, current->name, range_min, range_max, 0, )
275
276 #define us(width, name, range_min, range_max, subs, ...) \
277 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
278 #define ubs(width, name, subs, ...) \
279 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
280 #define flags(name, subs, ...) \
281 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
282 #define ues(name, range_min, range_max, subs, ...) \
283 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
284 #define is(width, name, range_min, range_max, subs, ...) \
285 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
286 #define ibs(width, name, subs, ...) \
287 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
288 #define ses(name, range_min, range_max, subs, ...) \
289 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
290
291 #define fixed(width, name, value) do { \
292 av_unused uint32_t fixed_value = value; \
293 xu(width, name, fixed_value, value, value, 0, ); \
294 } while (0)
295
296
297 #define READ
298 #define READWRITE read
299 #define RWContext GetBitContext
300
301 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
302 uint32_t value; \
303 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
304 SUBSCRIPTS(subs, __VA_ARGS__), \
305 &value, range_min, range_max)); \
306 var = value; \
307 } while (0)
308 #define xue(name, var, range_min, range_max, subs, ...) do { \
309 uint32_t value; \
310 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
311 SUBSCRIPTS(subs, __VA_ARGS__), \
312 &value, range_min, range_max)); \
313 var = value; \
314 } while (0)
315 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
316 int32_t value; \
317 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
318 SUBSCRIPTS(subs, __VA_ARGS__), \
319 &value, range_min, range_max)); \
320 var = value; \
321 } while (0)
322 #define xse(name, var, range_min, range_max, subs, ...) do { \
323 int32_t value; \
324 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
325 SUBSCRIPTS(subs, __VA_ARGS__), \
326 &value, range_min, range_max)); \
327 var = value; \
328 } while (0)
329
330
331 #define infer(name, value) do { \
332 current->name = value; \
333 } while (0)
334
335 13901 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
336 {
337 13901 int bits_left = get_bits_left(gbc);
338
2/2
✓ Branch 0 taken 10836 times.
✓ Branch 1 taken 3065 times.
13901 if (bits_left > 8)
339 10836 return 1;
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3065 times.
3065 if (bits_left == 0)
341 return 0;
342
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3062 times.
3065 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
343 3 return 1;
344 3062 return 0;
345 }
346
347 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
348
349 #define bit_position(rw) (get_bits_count(rw))
350 #define byte_alignment(rw) (get_bits_count(rw) % 8)
351
352 #define allocate(name, size) do { \
353 name ## _ref = av_buffer_allocz(size + \
354 AV_INPUT_BUFFER_PADDING_SIZE); \
355 if (!name ## _ref) \
356 return AVERROR(ENOMEM); \
357 name = name ## _ref->data; \
358 } while (0)
359
360 #define FUNC(name) FUNC_SEI(name)
361 #include "cbs_sei_syntax_template.c"
362 #undef FUNC
363
364 #define FUNC(name) FUNC_H264(name)
365 #include "cbs_h264_syntax_template.c"
366 #undef FUNC
367
368 #define FUNC(name) FUNC_H265(name)
369 #include "cbs_h265_syntax_template.c"
370 #undef FUNC
371
372 #undef READ
373 #undef READWRITE
374 #undef RWContext
375 #undef xu
376 #undef xi
377 #undef xue
378 #undef xse
379 #undef infer
380 #undef more_rbsp_data
381 #undef bit_position
382 #undef byte_alignment
383 #undef allocate
384
385
386 #define WRITE
387 #define READWRITE write
388 #define RWContext PutBitContext
389
390 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
391 uint32_t value = var; \
392 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
393 SUBSCRIPTS(subs, __VA_ARGS__), \
394 value, range_min, range_max)); \
395 } while (0)
396 #define xue(name, var, range_min, range_max, subs, ...) do { \
397 uint32_t value = var; \
398 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
399 SUBSCRIPTS(subs, __VA_ARGS__), \
400 value, range_min, range_max)); \
401 } while (0)
402 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
403 int32_t value = var; \
404 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
405 SUBSCRIPTS(subs, __VA_ARGS__), \
406 value, range_min, range_max)); \
407 } while (0)
408 #define xse(name, var, range_min, range_max, subs, ...) do { \
409 int32_t value = var; \
410 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
411 SUBSCRIPTS(subs, __VA_ARGS__), \
412 value, range_min, range_max)); \
413 } while (0)
414
415 #define infer(name, value) do { \
416 if (current->name != (value)) { \
417 av_log(ctx->log_ctx, AV_LOG_ERROR, \
418 "%s does not match inferred value: " \
419 "%"PRId64", but should be %"PRId64".\n", \
420 #name, (int64_t)current->name, (int64_t)(value)); \
421 return AVERROR_INVALIDDATA; \
422 } \
423 } while (0)
424
425 #define more_rbsp_data(var) (var)
426
427 #define bit_position(rw) (put_bits_count(rw))
428 #define byte_alignment(rw) (put_bits_count(rw) % 8)
429
430 #define allocate(name, size) do { \
431 if (!name) { \
432 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
433 "for writing.\n", #name); \
434 return AVERROR_INVALIDDATA; \
435 } \
436 } while (0)
437
438 #define FUNC(name) FUNC_SEI(name)
439 #include "cbs_sei_syntax_template.c"
440 #undef FUNC
441
442 #define FUNC(name) FUNC_H264(name)
443 #include "cbs_h264_syntax_template.c"
444 #undef FUNC
445
446 #define FUNC(name) FUNC_H265(name)
447 #include "cbs_h265_syntax_template.c"
448 #undef FUNC
449
450 #undef WRITE
451 #undef READWRITE
452 #undef RWContext
453 #undef xu
454 #undef xi
455 #undef xue
456 #undef xse
457 #undef u
458 #undef i
459 #undef flag
460 #undef ue
461 #undef se
462 #undef infer
463 #undef more_rbsp_data
464 #undef bit_position
465 #undef byte_alignment
466 #undef allocate
467
468
469 4543 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
470 CodedBitstreamFragment *frag,
471 const H2645Packet *packet)
472 {
473 int err, i;
474
475
2/2
✓ Branch 0 taken 14628 times.
✓ Branch 1 taken 4543 times.
19171 for (i = 0; i < packet->nb_nals; i++) {
476 14628 const H2645NAL *nal = &packet->nals[i];
477 AVBufferRef *ref;
478 14628 size_t size = nal->size;
479
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14628 times.
14628 if (nal->nuh_layer_id > 0)
481 continue;
482
483 // Remove trailing zeroes.
484
3/4
✓ Branch 0 taken 22602 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7974 times.
✓ Branch 3 taken 14628 times.
22602 while (size > 0 && nal->data[size - 1] == 0)
485 7974 --size;
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14628 times.
14628 if (size == 0) {
487 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
488 continue;
489 }
490
491 29256 ref = (nal->data == nal->raw_data) ? frag->data_ref
492
2/2
✓ Branch 0 taken 8737 times.
✓ Branch 1 taken 5891 times.
14628 : packet->rbsp.rbsp_buffer_ref;
493
494 14628 err = ff_cbs_append_unit_data(frag, nal->type,
495 14628 (uint8_t*)nal->data, size, ref);
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14628 times.
14628 if (err < 0)
497 return err;
498 }
499
500 4543 return 0;
501 }
502
503 4543 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
504 CodedBitstreamFragment *frag,
505 int header)
506 {
507 4543 enum AVCodecID codec_id = ctx->codec->codec_id;
508 4543 CodedBitstreamH2645Context *priv = ctx->priv_data;
509 GetByteContext gbc;
510 int err;
511
512
2/4
✓ Branch 0 taken 4543 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4543 times.
4543 av_assert0(frag->data && frag->nb_units == 0);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4543 times.
4543 if (frag->data_size == 0)
514 return 0;
515
516
3/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4507 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4543 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
517 // AVCC header.
518 size_t size, start, end;
519 int i, count, version;
520
521 priv->mp4 = 1;
522
523 bytestream2_init(&gbc, frag->data, frag->data_size);
524
525 if (bytestream2_get_bytes_left(&gbc) < 6)
526 return AVERROR_INVALIDDATA;
527
528 version = bytestream2_get_byte(&gbc);
529 if (version != 1) {
530 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
531 "first byte %u.\n", version);
532 return AVERROR_INVALIDDATA;
533 }
534
535 bytestream2_skip(&gbc, 3);
536 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
537
538 // SPS array.
539 count = bytestream2_get_byte(&gbc) & 0x1f;
540 start = bytestream2_tell(&gbc);
541 for (i = 0; i < count; i++) {
542 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
543 return AVERROR_INVALIDDATA;
544 size = bytestream2_get_be16(&gbc);
545 if (bytestream2_get_bytes_left(&gbc) < size)
546 return AVERROR_INVALIDDATA;
547 bytestream2_skip(&gbc, size);
548 }
549 end = bytestream2_tell(&gbc);
550
551 err = ff_h2645_packet_split(&priv->read_packet,
552 frag->data + start, end - start,
553 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
554 if (err < 0) {
555 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
556 return err;
557 }
558 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
559 if (err < 0)
560 return err;
561
562 // PPS array.
563 count = bytestream2_get_byte(&gbc);
564 start = bytestream2_tell(&gbc);
565 for (i = 0; i < count; i++) {
566 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
567 return AVERROR_INVALIDDATA;
568 size = bytestream2_get_be16(&gbc);
569 if (bytestream2_get_bytes_left(&gbc) < size)
570 return AVERROR_INVALIDDATA;
571 bytestream2_skip(&gbc, size);
572 }
573 end = bytestream2_tell(&gbc);
574
575 err = ff_h2645_packet_split(&priv->read_packet,
576 frag->data + start, end - start,
577 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
578 if (err < 0) {
579 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
580 return err;
581 }
582 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
583 if (err < 0)
584 return err;
585
586 if (bytestream2_get_bytes_left(&gbc) > 0) {
587 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
588 "header.\n", bytestream2_get_bytes_left(&gbc));
589 }
590
591
3/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4507 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4543 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
592 // HVCC header.
593 size_t size, start, end;
594 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
595
596 priv->mp4 = 1;
597
598 bytestream2_init(&gbc, frag->data, frag->data_size);
599
600 if (bytestream2_get_bytes_left(&gbc) < 23)
601 return AVERROR_INVALIDDATA;
602
603 version = bytestream2_get_byte(&gbc);
604 if (version != 1) {
605 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
606 "first byte %u.\n", version);
607 return AVERROR_INVALIDDATA;
608 }
609
610 bytestream2_skip(&gbc, 20);
611 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
612
613 nb_arrays = bytestream2_get_byte(&gbc);
614 for (i = 0; i < nb_arrays; i++) {
615 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
616 nb_nals = bytestream2_get_be16(&gbc);
617
618 start = bytestream2_tell(&gbc);
619 for (j = 0; j < nb_nals; j++) {
620 if (bytestream2_get_bytes_left(&gbc) < 2)
621 return AVERROR_INVALIDDATA;
622 size = bytestream2_get_be16(&gbc);
623 if (bytestream2_get_bytes_left(&gbc) < size)
624 return AVERROR_INVALIDDATA;
625 bytestream2_skip(&gbc, size);
626 }
627 end = bytestream2_tell(&gbc);
628
629 err = ff_h2645_packet_split(&priv->read_packet,
630 frag->data + start, end - start,
631 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
632 if (err < 0) {
633 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
634 "HVCC array %d (%d NAL units of type %d).\n",
635 i, nb_nals, nal_unit_type);
636 return err;
637 }
638 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
639 if (err < 0)
640 return err;
641 }
642
643 } else {
644 // Annex B, or later MP4 with already-known parameters.
645
646 4543 err = ff_h2645_packet_split(&priv->read_packet,
647 4543 frag->data, frag->data_size,
648 ctx->log_ctx,
649 priv->mp4, priv->nal_length_size,
650 codec_id, 1, 1);
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4543 times.
4543 if (err < 0)
652 return err;
653
654 4543 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4543 times.
4543 if (err < 0)
656 return err;
657 }
658
659 4543 return 0;
660 }
661
662 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
663 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
664 CodedBitstreamUnit *unit) \
665 { \
666 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
667 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
668 unsigned int id = ps_var->id_element; \
669 int err; \
670 if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
671 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
672 " id : %d.\n", id); \
673 return AVERROR_INVALIDDATA; \
674 } \
675 err = ff_cbs_make_unit_refcounted(ctx, unit); \
676 if (err < 0) \
677 return err; \
678 if (priv->ps_var[id] == priv->active_ ## ps_var) \
679 priv->active_ ## ps_var = NULL ; \
680 av_buffer_unref(&priv->ps_var ## _ref[id]); \
681 av_assert0(unit->content_ref); \
682 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
683 if (!priv->ps_var ## _ref[id]) \
684 return AVERROR(ENOMEM); \
685 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
686 return 0; \
687 }
688
689
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 52 times.
✓ Branch 7 taken 32 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 84 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 84 times.
84 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
690
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 2368 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2368 times.
✓ Branch 6 taken 2322 times.
✓ Branch 7 taken 46 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2368 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2368 times.
2368 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
691
5/10
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 96 times.
✓ Branch 6 taken 96 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 96 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 96 times.
96 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
692
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 100 times.
✓ Branch 6 taken 98 times.
✓ Branch 7 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 100 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 100 times.
100 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
693
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 550 times.
✓ Branch 6 taken 378 times.
✓ Branch 7 taken 172 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 550 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 550 times.
550 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
694
695 6796 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
696 CodedBitstreamUnit *unit)
697 {
698 GetBitContext gbc;
699 int err;
700
701 6796 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6796 times.
6796 if (err < 0)
703 return err;
704
705 6796 err = ff_cbs_alloc_unit_content2(ctx, unit);
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6796 times.
6796 if (err < 0)
707 return err;
708
709
7/9
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1184 times.
✓ Branch 3 taken 4953 times.
✓ Branch 4 taken 235 times.
✓ Branch 5 taken 345 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
6796 switch (unit->type) {
710 42 case H264_NAL_SPS:
711 {
712 42 H264RawSPS *sps = unit->content;
713
714 42 err = cbs_h264_read_sps(ctx, &gbc, sps);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
716 return err;
717
718 42 err = cbs_h264_replace_sps(ctx, unit);
719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
720 return err;
721 }
722 42 break;
723
724 case H264_NAL_SPS_EXT:
725 {
726 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
727 if (err < 0)
728 return err;
729 }
730 break;
731
732 1184 case H264_NAL_PPS:
733 {
734 1184 H264RawPPS *pps = unit->content;
735
736 1184 err = cbs_h264_read_pps(ctx, &gbc, pps);
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 if (err < 0)
738 return err;
739
740 1184 err = cbs_h264_replace_pps(ctx, unit);
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 if (err < 0)
742 return err;
743 }
744 1184 break;
745
746 4953 case H264_NAL_SLICE:
747 case H264_NAL_IDR_SLICE:
748 case H264_NAL_AUXILIARY_SLICE:
749 {
750 4953 H264RawSlice *slice = unit->content;
751 int pos, len;
752
753 4953 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4953 times.
4953 if (err < 0)
755 return err;
756
757
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4953 times.
4953 if (!cbs_h2645_read_more_rbsp_data(&gbc))
758 return AVERROR_INVALIDDATA;
759
760 4953 pos = get_bits_count(&gbc);
761 4953 len = unit->data_size;
762
763 4953 slice->data_size = len - pos / 8;
764 4953 slice->data_ref = av_buffer_ref(unit->data_ref);
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4953 times.
4953 if (!slice->data_ref)
766 return AVERROR(ENOMEM);
767 4953 slice->data = unit->data + pos / 8;
768 4953 slice->data_bit_start = pos % 8;
769 }
770 4953 break;
771
772 235 case H264_NAL_AUD:
773 {
774 235 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 235 times.
235 if (err < 0)
776 return err;
777 }
778 235 break;
779
780 345 case H264_NAL_SEI:
781 {
782 345 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 345 times.
345 if (err < 0)
784 return err;
785 }
786 345 break;
787
788 34 case H264_NAL_FILLER_DATA:
789 {
790 34 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
791
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
34 if (err < 0)
792 1 return err;
793 }
794 33 break;
795
796 3 case H264_NAL_END_SEQUENCE:
797 case H264_NAL_END_STREAM:
798 {
799 3 err = (unit->type == H264_NAL_END_SEQUENCE ?
800
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 cbs_h264_read_end_of_sequence :
801 3 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0)
803 return err;
804 }
805 3 break;
806
807 default:
808 return AVERROR(ENOSYS);
809 }
810
811 6795 return 0;
812 }
813
814 7832 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
815 CodedBitstreamUnit *unit)
816 {
817 GetBitContext gbc;
818 int err;
819
820 7832 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7832 times.
7832 if (err < 0)
822 return err;
823
824 7832 err = ff_cbs_alloc_unit_content2(ctx, unit);
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7832 times.
7832 if (err < 0)
826 return err;
827
828
6/7
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 5864 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 1535 times.
✗ Branch 6 not taken.
7832 switch (unit->type) {
829 48 case HEVC_NAL_VPS:
830 {
831 48 H265RawVPS *vps = unit->content;
832
833 48 err = cbs_h265_read_vps(ctx, &gbc, vps);
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (err < 0)
835 return err;
836
837 48 err = cbs_h265_replace_vps(ctx, unit);
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (err < 0)
839 return err;
840 }
841 48 break;
842 50 case HEVC_NAL_SPS:
843 {
844 50 H265RawSPS *sps = unit->content;
845
846 50 err = cbs_h265_read_sps(ctx, &gbc, sps);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (err < 0)
848 return err;
849
850 50 err = cbs_h265_replace_sps(ctx, unit);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (err < 0)
852 return err;
853 }
854 50 break;
855
856 275 case HEVC_NAL_PPS:
857 {
858 275 H265RawPPS *pps = unit->content;
859
860 275 err = cbs_h265_read_pps(ctx, &gbc, pps);
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 if (err < 0)
862 return err;
863
864 275 err = cbs_h265_replace_pps(ctx, unit);
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 if (err < 0)
866 return err;
867 }
868 275 break;
869
870 5864 case HEVC_NAL_TRAIL_N:
871 case HEVC_NAL_TRAIL_R:
872 case HEVC_NAL_TSA_N:
873 case HEVC_NAL_TSA_R:
874 case HEVC_NAL_STSA_N:
875 case HEVC_NAL_STSA_R:
876 case HEVC_NAL_RADL_N:
877 case HEVC_NAL_RADL_R:
878 case HEVC_NAL_RASL_N:
879 case HEVC_NAL_RASL_R:
880 case HEVC_NAL_BLA_W_LP:
881 case HEVC_NAL_BLA_W_RADL:
882 case HEVC_NAL_BLA_N_LP:
883 case HEVC_NAL_IDR_W_RADL:
884 case HEVC_NAL_IDR_N_LP:
885 case HEVC_NAL_CRA_NUT:
886 {
887 5864 H265RawSlice *slice = unit->content;
888 int pos, len;
889
890 5864 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5864 times.
5864 if (err < 0)
892 return err;
893
894
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5864 times.
5864 if (!cbs_h2645_read_more_rbsp_data(&gbc))
895 return AVERROR_INVALIDDATA;
896
897 5864 pos = get_bits_count(&gbc);
898 5864 len = unit->data_size;
899
900 5864 slice->data_size = len - pos / 8;
901 5864 slice->data_ref = av_buffer_ref(unit->data_ref);
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5864 times.
5864 if (!slice->data_ref)
903 return AVERROR(ENOMEM);
904 5864 slice->data = unit->data + pos / 8;
905 5864 slice->data_bit_start = pos % 8;
906 }
907 5864 break;
908
909 60 case HEVC_NAL_AUD:
910 {
911 60 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
913 return err;
914 }
915 60 break;
916
917 1535 case HEVC_NAL_SEI_PREFIX:
918 case HEVC_NAL_SEI_SUFFIX:
919 {
920 1535 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
921 1535 unit->type == HEVC_NAL_SEI_PREFIX);
922
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1535 times.
1535 if (err < 0)
924 return err;
925 }
926 1535 break;
927
928 default:
929 return AVERROR(ENOSYS);
930 }
931
932 7832 return 0;
933 }
934
935 10816 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
936 PutBitContext *pbc, const uint8_t *data,
937 size_t data_size, int data_bit_start)
938 {
939 10816 size_t rest = data_size - (data_bit_start + 7) / 8;
940 10816 const uint8_t *pos = data + data_bit_start / 8;
941
942
2/4
✓ Branch 0 taken 10816 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10816 times.
10816 av_assert0(data_bit_start >= 0 &&
943 data_size > data_bit_start / 8);
944
945
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10816 times.
10816 if (data_size * 8 + 8 > put_bits_left(pbc))
946 return AVERROR(ENOSPC);
947
948
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10815 times.
10816 if (!rest)
949 1 goto rbsp_stop_one_bit;
950
951 // First copy the remaining bits of the first byte
952 // The above check ensures that we do not accidentally
953 // copy beyond the rbsp_stop_one_bit.
954
2/2
✓ Branch 0 taken 3547 times.
✓ Branch 1 taken 7268 times.
10815 if (data_bit_start % 8)
955 3547 put_bits(pbc, 8 - data_bit_start % 8,
956 3547 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
957
958
1/2
✓ Branch 1 taken 10815 times.
✗ Branch 2 not taken.
10815 if (put_bits_count(pbc) % 8 == 0) {
959 // If the writer is aligned at this point,
960 // memcpy can be used to improve performance.
961 // This happens normally for CABAC.
962 10815 flush_put_bits(pbc);
963 10815 memcpy(put_bits_ptr(pbc), pos, rest);
964 10815 skip_put_bytes(pbc, rest);
965 } else {
966 // If not, we have to copy manually.
967 // rbsp_stop_one_bit forces us to special-case
968 // the last byte.
969 uint8_t temp;
970 int i;
971
972 for (; rest > 4; rest -= 4, pos += 4)
973 put_bits32(pbc, AV_RB32(pos));
974
975 for (; rest > 1; rest--, pos++)
976 put_bits(pbc, 8, *pos);
977
978 rbsp_stop_one_bit:
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
980
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(temp);
982 1 i = ff_ctz(*pos);
983 1 temp = temp >> i;
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
985 1 put_bits(pbc, i, temp);
986
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (put_bits_count(pbc) % 8)
987 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
988 }
989
990 10816 return 0;
991 }
992
993 6720 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
994 CodedBitstreamUnit *unit,
995 PutBitContext *pbc)
996 {
997 int err;
998
999
7/10
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1184 times.
✓ Branch 3 taken 4952 times.
✓ Branch 4 taken 195 times.
✓ Branch 5 taken 344 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
6720 switch (unit->type) {
1000 42 case H264_NAL_SPS:
1001 {
1002 42 H264RawSPS *sps = unit->content;
1003
1004 42 err = cbs_h264_write_sps(ctx, pbc, sps);
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
1006 return err;
1007
1008 42 err = cbs_h264_replace_sps(ctx, unit);
1009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
1010 return err;
1011 }
1012 42 break;
1013
1014 case H264_NAL_SPS_EXT:
1015 {
1016 H264RawSPSExtension *sps_ext = unit->content;
1017
1018 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1019 if (err < 0)
1020 return err;
1021 }
1022 break;
1023
1024 1184 case H264_NAL_PPS:
1025 {
1026 1184 H264RawPPS *pps = unit->content;
1027
1028 1184 err = cbs_h264_write_pps(ctx, pbc, pps);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 if (err < 0)
1030 return err;
1031
1032 1184 err = cbs_h264_replace_pps(ctx, unit);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 if (err < 0)
1034 return err;
1035 }
1036 1184 break;
1037
1038 4952 case H264_NAL_SLICE:
1039 case H264_NAL_IDR_SLICE:
1040 case H264_NAL_AUXILIARY_SLICE:
1041 {
1042 4952 H264RawSlice *slice = unit->content;
1043
1044 4952 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4952 times.
4952 if (err < 0)
1046 return err;
1047
1048
1/2
✓ Branch 0 taken 4952 times.
✗ Branch 1 not taken.
4952 if (slice->data) {
1049 4952 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1050 slice->data_size,
1051 slice->data_bit_start);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4952 times.
4952 if (err < 0)
1053 return err;
1054 } else {
1055 // No slice data - that was just the header.
1056 // (Bitstream may be unaligned!)
1057 }
1058 }
1059 4952 break;
1060
1061 195 case H264_NAL_AUD:
1062 {
1063 195 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (err < 0)
1065 return err;
1066 }
1067 195 break;
1068
1069 344 case H264_NAL_SEI:
1070 {
1071 344 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 344 times.
344 if (err < 0)
1073 return err;
1074 }
1075 344 break;
1076
1077 case H264_NAL_FILLER_DATA:
1078 {
1079 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1080 if (err < 0)
1081 return err;
1082 }
1083 break;
1084
1085 1 case H264_NAL_END_SEQUENCE:
1086 {
1087 1 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1089 return err;
1090 }
1091 1 break;
1092
1093 2 case H264_NAL_END_STREAM:
1094 {
1095 2 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err < 0)
1097 return err;
1098 }
1099 2 break;
1100
1101 default:
1102 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1103 "NAL unit type %"PRIu32".\n", unit->type);
1104 return AVERROR_PATCHWELCOME;
1105 }
1106
1107 6720 return 0;
1108 }
1109
1110 7832 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1111 CodedBitstreamUnit *unit,
1112 PutBitContext *pbc)
1113 {
1114 int err;
1115
1116
6/7
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 5864 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 1535 times.
✗ Branch 6 not taken.
7832 switch (unit->type) {
1117 48 case HEVC_NAL_VPS:
1118 {
1119 48 H265RawVPS *vps = unit->content;
1120
1121 48 err = cbs_h265_write_vps(ctx, pbc, vps);
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (err < 0)
1123 return err;
1124
1125 48 err = cbs_h265_replace_vps(ctx, unit);
1126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (err < 0)
1127 return err;
1128 }
1129 48 break;
1130
1131 50 case HEVC_NAL_SPS:
1132 {
1133 50 H265RawSPS *sps = unit->content;
1134
1135 50 err = cbs_h265_write_sps(ctx, pbc, sps);
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (err < 0)
1137 return err;
1138
1139 50 err = cbs_h265_replace_sps(ctx, unit);
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (err < 0)
1141 return err;
1142 }
1143 50 break;
1144
1145 275 case HEVC_NAL_PPS:
1146 {
1147 275 H265RawPPS *pps = unit->content;
1148
1149 275 err = cbs_h265_write_pps(ctx, pbc, pps);
1150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 if (err < 0)
1151 return err;
1152
1153 275 err = cbs_h265_replace_pps(ctx, unit);
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 if (err < 0)
1155 return err;
1156 }
1157 275 break;
1158
1159 5864 case HEVC_NAL_TRAIL_N:
1160 case HEVC_NAL_TRAIL_R:
1161 case HEVC_NAL_TSA_N:
1162 case HEVC_NAL_TSA_R:
1163 case HEVC_NAL_STSA_N:
1164 case HEVC_NAL_STSA_R:
1165 case HEVC_NAL_RADL_N:
1166 case HEVC_NAL_RADL_R:
1167 case HEVC_NAL_RASL_N:
1168 case HEVC_NAL_RASL_R:
1169 case HEVC_NAL_BLA_W_LP:
1170 case HEVC_NAL_BLA_W_RADL:
1171 case HEVC_NAL_BLA_N_LP:
1172 case HEVC_NAL_IDR_W_RADL:
1173 case HEVC_NAL_IDR_N_LP:
1174 case HEVC_NAL_CRA_NUT:
1175 {
1176 5864 H265RawSlice *slice = unit->content;
1177
1178 5864 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5864 times.
5864 if (err < 0)
1180 return err;
1181
1182
1/2
✓ Branch 0 taken 5864 times.
✗ Branch 1 not taken.
5864 if (slice->data) {
1183 5864 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1184 slice->data_size,
1185 slice->data_bit_start);
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5864 times.
5864 if (err < 0)
1187 return err;
1188 } else {
1189 // No slice data - that was just the header.
1190 }
1191 }
1192 5864 break;
1193
1194 60 case HEVC_NAL_AUD:
1195 {
1196 60 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
1198 return err;
1199 }
1200 60 break;
1201
1202 1535 case HEVC_NAL_SEI_PREFIX:
1203 case HEVC_NAL_SEI_SUFFIX:
1204 {
1205 1535 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1206 1535 unit->type == HEVC_NAL_SEI_PREFIX);
1207
1208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1535 times.
1535 if (err < 0)
1209 return err;
1210 }
1211 1535 break;
1212
1213 default:
1214 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1215 "NAL unit type %"PRIu32".\n", unit->type);
1216 return AVERROR_PATCHWELCOME;
1217 }
1218
1219 7832 return 0;
1220 }
1221
1222 14552 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
1223 CodedBitstreamUnitType type,
1224 int nal_unit_index)
1225 {
1226 // Section B.1.2 in H.264, section B.2.2 in H.265.
1227
2/2
✓ Branch 0 taken 4542 times.
✓ Branch 1 taken 10010 times.
14552 if (nal_unit_index == 0) {
1228 // Assume that this is the first NAL unit in an access unit.
1229 4542 return 1;
1230 }
1231
2/2
✓ Branch 0 taken 4330 times.
✓ Branch 1 taken 5680 times.
10010 if (codec_id == AV_CODEC_ID_H264)
1232
4/4
✓ Branch 0 taken 4322 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 417 times.
✓ Branch 3 taken 3905 times.
4330 return type == H264_NAL_SPS || type == H264_NAL_PPS;
1233
1/2
✓ Branch 0 taken 5680 times.
✗ Branch 1 not taken.
5680 if (codec_id == AV_CODEC_ID_HEVC)
1234
6/6
✓ Branch 0 taken 5677 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5627 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 5579 times.
5680 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1235 return 0;
1236 }
1237
1238 4542 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1239 CodedBitstreamFragment *frag)
1240 {
1241 uint8_t *data;
1242 size_t max_size, dp, sp;
1243 int err, i, zero_run;
1244
1245
2/2
✓ Branch 0 taken 14552 times.
✓ Branch 1 taken 4542 times.
19094 for (i = 0; i < frag->nb_units; i++) {
1246 // Data should already all have been written when we get here.
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14552 times.
14552 av_assert0(frag->units[i].data);
1248 }
1249
1250 4542 max_size = 0;
1251
2/2
✓ Branch 0 taken 14552 times.
✓ Branch 1 taken 4542 times.
19094 for (i = 0; i < frag->nb_units; i++) {
1252 // Start code + content with worst-case emulation prevention.
1253 14552 max_size += 4 + frag->units[i].data_size * 3 / 2;
1254 }
1255
1256 4542 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4542 times.
4542 if (!data)
1258 return AVERROR(ENOMEM);
1259
1260 4542 dp = 0;
1261
2/2
✓ Branch 0 taken 14552 times.
✓ Branch 1 taken 4542 times.
19094 for (i = 0; i < frag->nb_units; i++) {
1262 14552 CodedBitstreamUnit *unit = &frag->units[i];
1263
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14552 times.
14552 if (unit->data_bit_padding > 0) {
1265 if (i < frag->nb_units - 1)
1266 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1267 "unaligned padding on non-final NAL unit.\n");
1268 else
1269 frag->data_bit_padding = unit->data_bit_padding;
1270 }
1271
1272
2/2
✓ Branch 1 taken 5068 times.
✓ Branch 2 taken 9484 times.
14552 if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1273 // zero_byte
1274 5068 data[dp++] = 0;
1275 }
1276 // start_code_prefix_one_3bytes
1277 14552 data[dp++] = 0;
1278 14552 data[dp++] = 0;
1279 14552 data[dp++] = 1;
1280
1281 14552 zero_run = 0;
1282
2/2
✓ Branch 0 taken 10831841 times.
✓ Branch 1 taken 14552 times.
10846393 for (sp = 0; sp < unit->data_size; sp++) {
1283
2/2
✓ Branch 0 taken 10827719 times.
✓ Branch 1 taken 4122 times.
10831841 if (zero_run < 2) {
1284
2/2
✓ Branch 0 taken 107168 times.
✓ Branch 1 taken 10720551 times.
10827719 if (unit->data[sp] == 0)
1285 107168 ++zero_run;
1286 else
1287 10720551 zero_run = 0;
1288 } else {
1289
2/2
✓ Branch 0 taken 797 times.
✓ Branch 1 taken 3325 times.
4122 if ((unit->data[sp] & ~3) == 0) {
1290 // emulation_prevention_three_byte
1291 797 data[dp++] = 3;
1292 }
1293 4122 zero_run = unit->data[sp] == 0;
1294 }
1295 10831841 data[dp++] = unit->data[sp];
1296 }
1297 }
1298
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4542 times.
4542 av_assert0(dp <= max_size);
1300 4542 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4542 times.
4542 if (err)
1302 return err;
1303 4542 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1304
1305 4542 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1306 NULL, NULL, 0);
1307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4542 times.
4542 if (!frag->data_ref) {
1308 av_freep(&data);
1309 return AVERROR(ENOMEM);
1310 }
1311
1312 4542 frag->data = data;
1313 4542 frag->data_size = dp;
1314
1315 4542 return 0;
1316 }
1317
1318 static void cbs_h264_flush(CodedBitstreamContext *ctx)
1319 {
1320 CodedBitstreamH264Context *h264 = ctx->priv_data;
1321
1322 for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++) {
1323 av_buffer_unref(&h264->sps_ref[i]);
1324 h264->sps[i] = NULL;
1325 }
1326 for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++) {
1327 av_buffer_unref(&h264->pps_ref[i]);
1328 h264->pps[i] = NULL;
1329 }
1330
1331 h264->active_sps = NULL;
1332 h264->active_pps = NULL;
1333 h264->last_slice_nal_unit_type = 0;
1334 }
1335
1336 32 static void cbs_h264_close(CodedBitstreamContext *ctx)
1337 {
1338 32 CodedBitstreamH264Context *h264 = ctx->priv_data;
1339 int i;
1340
1341 32 ff_h2645_packet_uninit(&h264->common.read_packet);
1342
1343
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 32 times.
1056 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1344 1024 av_buffer_unref(&h264->sps_ref[i]);
1345
2/2
✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 32 times.
8224 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1346 8192 av_buffer_unref(&h264->pps_ref[i]);
1347 32 }
1348
1349 static void cbs_h265_flush(CodedBitstreamContext *ctx)
1350 {
1351 CodedBitstreamH265Context *h265 = ctx->priv_data;
1352
1353 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++) {
1354 av_buffer_unref(&h265->vps_ref[i]);
1355 h265->vps[i] = NULL;
1356 }
1357 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++) {
1358 av_buffer_unref(&h265->sps_ref[i]);
1359 h265->sps[i] = NULL;
1360 }
1361 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++) {
1362 av_buffer_unref(&h265->pps_ref[i]);
1363 h265->pps[i] = NULL;
1364 }
1365
1366 h265->active_vps = NULL;
1367 h265->active_sps = NULL;
1368 h265->active_pps = NULL;
1369 }
1370
1371 40 static void cbs_h265_close(CodedBitstreamContext *ctx)
1372 {
1373 40 CodedBitstreamH265Context *h265 = ctx->priv_data;
1374 int i;
1375
1376 40 ff_h2645_packet_uninit(&h265->common.read_packet);
1377
1378
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 40 times.
680 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1379 640 av_buffer_unref(&h265->vps_ref[i]);
1380
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 40 times.
680 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1381 640 av_buffer_unref(&h265->sps_ref[i]);
1382
2/2
✓ Branch 0 taken 2560 times.
✓ Branch 1 taken 40 times.
2600 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1383 2560 av_buffer_unref(&h265->pps_ref[i]);
1384 40 }
1385
1386 345 static void cbs_h264_free_sei(void *opaque, uint8_t *content)
1387 {
1388 345 H264RawSEI *sei = (H264RawSEI*)content;
1389 345 ff_cbs_sei_free_message_list(&sei->message_list);
1390 345 av_free(content);
1391 345 }
1392
1393 static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
1394 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
1395 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
1396
1397 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
1398
1399 {
1400 .nb_unit_types = 3,
1401 .unit_types = {
1402 H264_NAL_IDR_SLICE,
1403 H264_NAL_SLICE,
1404 H264_NAL_AUXILIARY_SLICE,
1405 },
1406 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1407 .content_size = sizeof(H264RawSlice),
1408 .nb_ref_offsets = 1,
1409 .ref_offsets = { offsetof(H264RawSlice, data) },
1410 },
1411
1412 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
1413 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
1414 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
1415 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
1416
1417 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
1418
1419 CBS_UNIT_TYPE_END_OF_LIST
1420 };
1421
1422 1535 static void cbs_h265_free_sei(void *opaque, uint8_t *content)
1423 {
1424 1535 H265RawSEI *sei = (H265RawSEI*)content;
1425 1535 ff_cbs_sei_free_message_list(&sei->message_list);
1426 1535 av_free(content);
1427 1535 }
1428
1429 static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
1430 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_VPS, H265RawVPS, extension_data.data),
1431 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
1432 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
1433
1434 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
1435
1436 {
1437 // Slices of non-IRAP pictures.
1438 .nb_unit_types = CBS_UNIT_TYPE_RANGE,
1439 .unit_type_range_start = HEVC_NAL_TRAIL_N,
1440 .unit_type_range_end = HEVC_NAL_RASL_R,
1441
1442 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1443 .content_size = sizeof(H265RawSlice),
1444 .nb_ref_offsets = 1,
1445 .ref_offsets = { offsetof(H265RawSlice, data) },
1446 },
1447
1448 {
1449 // Slices of IRAP pictures.
1450 .nb_unit_types = CBS_UNIT_TYPE_RANGE,
1451 .unit_type_range_start = HEVC_NAL_BLA_W_LP,
1452 .unit_type_range_end = HEVC_NAL_CRA_NUT,
1453
1454 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1455 .content_size = sizeof(H265RawSlice),
1456 .nb_ref_offsets = 1,
1457 .ref_offsets = { offsetof(H265RawSlice, data) },
1458 },
1459
1460 {
1461 .nb_unit_types = 2,
1462 .unit_types = {
1463 HEVC_NAL_SEI_PREFIX,
1464 HEVC_NAL_SEI_SUFFIX
1465 },
1466 .content_type = CBS_CONTENT_TYPE_COMPLEX,
1467 .content_size = sizeof(H265RawSEI),
1468 .content_free = &cbs_h265_free_sei,
1469 },
1470
1471 CBS_UNIT_TYPE_END_OF_LIST
1472 };
1473
1474 const CodedBitstreamType ff_cbs_type_h264 = {
1475 .codec_id = AV_CODEC_ID_H264,
1476
1477 .priv_data_size = sizeof(CodedBitstreamH264Context),
1478
1479 .unit_types = cbs_h264_unit_types,
1480
1481 .split_fragment = &cbs_h2645_split_fragment,
1482 .read_unit = &cbs_h264_read_nal_unit,
1483 .write_unit = &cbs_h264_write_nal_unit,
1484 .assemble_fragment = &cbs_h2645_assemble_fragment,
1485
1486 .flush = &cbs_h264_flush,
1487 .close = &cbs_h264_close,
1488 };
1489
1490 const CodedBitstreamType ff_cbs_type_h265 = {
1491 .codec_id = AV_CODEC_ID_HEVC,
1492
1493 .priv_data_size = sizeof(CodedBitstreamH265Context),
1494
1495 .unit_types = cbs_h265_unit_types,
1496
1497 .split_fragment = &cbs_h2645_split_fragment,
1498 .read_unit = &cbs_h265_read_nal_unit,
1499 .write_unit = &cbs_h265_write_nal_unit,
1500 .assemble_fragment = &cbs_h2645_assemble_fragment,
1501
1502 .flush = &cbs_h265_flush,
1503 .close = &cbs_h265_close,
1504 };
1505
1506 static const SEIMessageTypeDescriptor cbs_sei_common_types[] = {
1507 {
1508 SEI_TYPE_FILLER_PAYLOAD,
1509 1, 1,
1510 sizeof(SEIRawFillerPayload),
1511 SEI_MESSAGE_RW(sei, filler_payload),
1512 },
1513 {
1514 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
1515 1, 1,
1516 sizeof(SEIRawUserDataRegistered),
1517 SEI_MESSAGE_RW(sei, user_data_registered),
1518 },
1519 {
1520 SEI_TYPE_USER_DATA_UNREGISTERED,
1521 1, 1,
1522 sizeof(SEIRawUserDataUnregistered),
1523 SEI_MESSAGE_RW(sei, user_data_unregistered),
1524 },
1525 {
1526 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
1527 1, 0,
1528 sizeof(SEIRawMasteringDisplayColourVolume),
1529 SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
1530 },
1531 {
1532 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
1533 1, 0,
1534 sizeof(SEIRawContentLightLevelInfo),
1535 SEI_MESSAGE_RW(sei, content_light_level_info),
1536 },
1537 {
1538 SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS,
1539 1, 0,
1540 sizeof(SEIRawAlternativeTransferCharacteristics),
1541 SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
1542 },
1543 SEI_MESSAGE_TYPE_END,
1544 };
1545
1546 static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = {
1547 {
1548 SEI_TYPE_BUFFERING_PERIOD,
1549 1, 0,
1550 sizeof(H264RawSEIBufferingPeriod),
1551 SEI_MESSAGE_RW(h264, sei_buffering_period),
1552 },
1553 {
1554 SEI_TYPE_PIC_TIMING,
1555 1, 0,
1556 sizeof(H264RawSEIPicTiming),
1557 SEI_MESSAGE_RW(h264, sei_pic_timing),
1558 },
1559 {
1560 SEI_TYPE_PAN_SCAN_RECT,
1561 1, 0,
1562 sizeof(H264RawSEIPanScanRect),
1563 SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
1564 },
1565 {
1566 SEI_TYPE_RECOVERY_POINT,
1567 1, 0,
1568 sizeof(H264RawSEIRecoveryPoint),
1569 SEI_MESSAGE_RW(h264, sei_recovery_point),
1570 },
1571 {
1572 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
1573 1, 0,
1574 sizeof(H264RawFilmGrainCharacteristics),
1575 SEI_MESSAGE_RW(h264, film_grain_characteristics),
1576 },
1577 {
1578 SEI_TYPE_DISPLAY_ORIENTATION,
1579 1, 0,
1580 sizeof(H264RawSEIDisplayOrientation),
1581 SEI_MESSAGE_RW(h264, sei_display_orientation),
1582 },
1583 SEI_MESSAGE_TYPE_END
1584 };
1585
1586 static const SEIMessageTypeDescriptor cbs_sei_h265_types[] = {
1587 {
1588 SEI_TYPE_BUFFERING_PERIOD,
1589 1, 0,
1590 sizeof(H265RawSEIBufferingPeriod),
1591 SEI_MESSAGE_RW(h265, sei_buffering_period),
1592 },
1593 {
1594 SEI_TYPE_PIC_TIMING,
1595 1, 0,
1596 sizeof(H265RawSEIPicTiming),
1597 SEI_MESSAGE_RW(h265, sei_pic_timing),
1598 },
1599 {
1600 SEI_TYPE_PAN_SCAN_RECT,
1601 1, 0,
1602 sizeof(H265RawSEIPanScanRect),
1603 SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
1604 },
1605 {
1606 SEI_TYPE_RECOVERY_POINT,
1607 1, 0,
1608 sizeof(H265RawSEIRecoveryPoint),
1609 SEI_MESSAGE_RW(h265, sei_recovery_point),
1610 },
1611 {
1612 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
1613 1, 0,
1614 sizeof(H265RawFilmGrainCharacteristics),
1615 SEI_MESSAGE_RW(h265, film_grain_characteristics),
1616 },
1617 {
1618 SEI_TYPE_DISPLAY_ORIENTATION,
1619 1, 0,
1620 sizeof(H265RawSEIDisplayOrientation),
1621 SEI_MESSAGE_RW(h265, sei_display_orientation),
1622 },
1623 {
1624 SEI_TYPE_ACTIVE_PARAMETER_SETS,
1625 1, 0,
1626 sizeof(H265RawSEIActiveParameterSets),
1627 SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
1628 },
1629 {
1630 SEI_TYPE_DECODED_PICTURE_HASH,
1631 0, 1,
1632 sizeof(H265RawSEIDecodedPictureHash),
1633 SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
1634 },
1635 {
1636 SEI_TYPE_TIME_CODE,
1637 1, 0,
1638 sizeof(H265RawSEITimeCode),
1639 SEI_MESSAGE_RW(h265, sei_time_code),
1640 },
1641 {
1642 SEI_TYPE_ALPHA_CHANNEL_INFO,
1643 1, 0,
1644 sizeof(H265RawSEIAlphaChannelInfo),
1645 SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
1646 },
1647 SEI_MESSAGE_TYPE_END
1648 };
1649
1650 5698 const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx,
1651 int payload_type)
1652 {
1653 const SEIMessageTypeDescriptor *codec_list;
1654 int i;
1655
1656
2/2
✓ Branch 0 taken 33882 times.
✓ Branch 1 taken 5620 times.
39502 for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
1657
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 33804 times.
33882 if (cbs_sei_common_types[i].type == payload_type)
1658 78 return &cbs_sei_common_types[i];
1659 }
1660
1661
2/3
✓ Branch 0 taken 1015 times.
✓ Branch 1 taken 4605 times.
✗ Branch 2 not taken.
5620 switch (ctx->codec->codec_id) {
1662 1015 case AV_CODEC_ID_H264:
1663 1015 codec_list = cbs_sei_h264_types;
1664 1015 break;
1665 4605 case AV_CODEC_ID_H265:
1666 4605 codec_list = cbs_sei_h265_types;
1667 4605 break;
1668 default:
1669 return NULL;
1670 }
1671
1672
2/2
✓ Branch 0 taken 35978 times.
✓ Branch 1 taken 15 times.
35993 for (i = 0; codec_list[i].type >= 0; i++) {
1673
2/2
✓ Branch 0 taken 5605 times.
✓ Branch 1 taken 30373 times.
35978 if (codec_list[i].type == payload_type)
1674 5605 return &codec_list[i];
1675 }
1676
1677 15 return NULL;
1678 }
1679