FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h265.c
Date: 2026-09-26 05:01:43
Exec Total Coverage
Lines: 165 241 68.5%
Functions: 11 11 100.0%
Branches: 85 153 55.6%

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/mem.h"
20 #include "libavutil/refstruct.h"
21 #include "bytestream.h"
22 #include "cbs.h"
23 #include "cbs_internal.h"
24 #include "cbs_h2645.h"
25 #include "cbs_h265.h"
26 #include "cbs_sei.h"
27 #include "get_bits.h"
28
29 // PicWidthInCtbsY and PicHeightInCtbsY (7.4.3.2.1).
30 13876 static void cbs_h265_pic_size_in_ctbs(const H265RawSPS *sps,
31 unsigned int *width, unsigned int *height)
32 {
33 13876 unsigned int ctb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3 +
34 13876 sps->log2_diff_max_min_luma_coding_block_size;
35 13876 *width = AV_CEIL_RSHIFT(sps->pic_width_in_luma_samples, ctb_log2_size_y);
36 13876 *height = AV_CEIL_RSHIFT(sps->pic_height_in_luma_samples, ctb_log2_size_y);
37 13876 }
38
39 #define HEADER(name) do { \
40 ff_cbs_trace_header(ctx, name); \
41 } while (0)
42
43 #define CHECK(call) do { \
44 err = (call); \
45 if (err < 0) \
46 return err; \
47 } while (0)
48
49 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
50 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
51 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
52 #define FUNC_NAME2_EXPORT(rw, codec, name) ff_cbs_ ## codec ## _ ## rw ## _ ## name
53 #define FUNC_NAME1_EXPORT(rw, codec, name) FUNC_NAME2_EXPORT(rw, codec, name)
54 #define FUNC_SEI(name) FUNC_NAME1_EXPORT(READWRITE, sei, name)
55
56 #define SEI_FUNC(name, args) \
57 static int FUNC_H265(name) args; \
58 static int FUNC_H265(name ## _internal)(CodedBitstreamContext *ctx, \
59 RWContext *rw, void *cur, \
60 SEIMessageState *state) \
61 { \
62 return FUNC_H265(name)(ctx, rw, cur, state); \
63 } \
64 static int FUNC_H265(name) args
65
66 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
67
68 #define u(width, name, range_min, range_max) \
69 xu(width, name, current->name, range_min, range_max, 0, )
70 #define flag(name) ub(1, name)
71 #define ue(name, range_min, range_max) \
72 xue(name, current->name, range_min, range_max, 0, )
73 #define i(width, name, range_min, range_max) \
74 xi(width, name, current->name, range_min, range_max, 0, )
75 #define ib(width, name) \
76 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
77 #define se(name, range_min, range_max) \
78 xse(name, current->name, range_min, range_max, 0, )
79
80 #define us(width, name, range_min, range_max, subs, ...) \
81 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
82 #define ubs(width, name, subs, ...) \
83 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
84 #define flags(name, subs, ...) \
85 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
86 #define ues(name, range_min, range_max, subs, ...) \
87 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
88 #define is(width, name, range_min, range_max, subs, ...) \
89 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
90 #define ibs(width, name, subs, ...) \
91 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
92 #define ses(name, range_min, range_max, subs, ...) \
93 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
94
95 #define fixed(width, name, value) do { \
96 av_unused uint32_t fixed_value = value; \
97 xu(width, name, fixed_value, value, value, 0, ); \
98 } while (0)
99
100
101 #define READ
102 #define READWRITE read
103 #define RWContext GetBitContext
104
105 #define ub(width, name) do { \
106 uint32_t value; \
107 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
108 &value)); \
109 current->name = value; \
110 } while (0)
111 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
112 uint32_t value; \
113 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
114 SUBSCRIPTS(subs, __VA_ARGS__), \
115 &value, range_min, range_max)); \
116 var = value; \
117 } while (0)
118 #define xue(name, var, range_min, range_max, subs, ...) do { \
119 uint32_t value; \
120 CHECK(ff_cbs_read_ue_golomb(ctx, rw, #name, \
121 SUBSCRIPTS(subs, __VA_ARGS__), \
122 &value, range_min, range_max)); \
123 var = value; \
124 } while (0)
125 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
126 int32_t value; \
127 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
128 SUBSCRIPTS(subs, __VA_ARGS__), \
129 &value, range_min, range_max)); \
130 var = value; \
131 } while (0)
132 #define xse(name, var, range_min, range_max, subs, ...) do { \
133 int32_t value; \
134 CHECK(ff_cbs_read_se_golomb(ctx, rw, #name, \
135 SUBSCRIPTS(subs, __VA_ARGS__), \
136 &value, range_min, range_max)); \
137 var = value; \
138 } while (0)
139
140
141 #define infer(name, value) do { \
142 current->name = value; \
143 } while (0)
144
145 #define more_rbsp_data(var) ((var) = ff_cbs_h2645_read_more_rbsp_data(rw))
146
147 #define bit_position(rw) (get_bits_count(rw))
148 #define byte_alignment(rw) (get_bits_count(rw) % 8)
149
150 #define allocate(name, size) do { \
151 name ## _ref = av_buffer_allocz(size + \
152 AV_INPUT_BUFFER_PADDING_SIZE); \
153 if (!name ## _ref) \
154 return AVERROR(ENOMEM); \
155 name = (void *)name ## _ref->data; \
156 } while (0)
157
158 #define FUNC(name) FUNC_H265(name)
159 #include "cbs_h265_syntax_template.c"
160 #undef FUNC
161
162
163 #undef READ
164 #undef READWRITE
165 #undef RWContext
166 #undef ub
167 #undef xu
168 #undef xi
169 #undef xue
170 #undef xse
171 #undef infer
172 #undef more_rbsp_data
173 #undef bit_position
174 #undef byte_alignment
175 #undef allocate
176 #undef allocate_struct
177
178
179 #define WRITE
180 #define READWRITE write
181 #define RWContext PutBitContext
182
183 #define ub(width, name) do { \
184 uint32_t value = current->name; \
185 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
186 value)); \
187 } while (0)
188 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
189 uint32_t value = var; \
190 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
191 SUBSCRIPTS(subs, __VA_ARGS__), \
192 value, range_min, range_max)); \
193 } while (0)
194 #define xue(name, var, range_min, range_max, subs, ...) do { \
195 uint32_t value = var; \
196 CHECK(ff_cbs_write_ue_golomb(ctx, rw, #name, \
197 SUBSCRIPTS(subs, __VA_ARGS__), \
198 value, range_min, range_max)); \
199 } while (0)
200 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
201 int32_t value = var; \
202 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
203 SUBSCRIPTS(subs, __VA_ARGS__), \
204 value, range_min, range_max)); \
205 } while (0)
206 #define xse(name, var, range_min, range_max, subs, ...) do { \
207 int32_t value = var; \
208 CHECK(ff_cbs_write_se_golomb(ctx, rw, #name, \
209 SUBSCRIPTS(subs, __VA_ARGS__), \
210 value, range_min, range_max)); \
211 } while (0)
212
213 #define infer(name, value) do { \
214 if (current->name != (value)) { \
215 av_log(ctx->log_ctx, AV_LOG_ERROR, \
216 "%s does not match inferred value: " \
217 "%"PRId64", but should be %"PRId64".\n", \
218 #name, (int64_t)current->name, (int64_t)(value)); \
219 return AVERROR_INVALIDDATA; \
220 } \
221 } while (0)
222
223 #define more_rbsp_data(var) (var)
224
225 #define bit_position(rw) (put_bits_count(rw))
226 #define byte_alignment(rw) (put_bits_count(rw) % 8)
227
228 #define allocate(name, size) do { \
229 if (!name) { \
230 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
231 "for writing.\n", #name); \
232 return AVERROR_INVALIDDATA; \
233 } \
234 } while (0)
235
236 #define FUNC(name) FUNC_H265(name)
237 #include "cbs_h265_syntax_template.c"
238 #undef FUNC
239
240 #undef WRITE
241 #undef READWRITE
242 #undef RWContext
243 #undef ub
244 #undef xu
245 #undef xi
246 #undef xue
247 #undef xse
248 #undef u
249 #undef i
250 #undef flag
251 #undef ue
252 #undef se
253 #undef infer
254 #undef more_rbsp_data
255 #undef bit_position
256 #undef byte_alignment
257 #undef allocate
258
259
260
261 2650 static int cbs_h265_split_fragment(CodedBitstreamContext *ctx,
262 CodedBitstreamFragment *frag,
263 int header)
264 {
265 2650 enum AVCodecID codec_id = ctx->codec->codec_id;
266 2650 CodedBitstreamH265Context *priv = ctx->priv_data;
267 2650 CodedBitstreamH2645Context *h2645 = &priv->common;
268 GetByteContext gbc;
269 int err;
270
271
2/4
✓ Branch 0 taken 2650 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2650 times.
2650 av_assert0(frag->data && frag->nb_units == 0);
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2650 times.
2650 if (frag->data_size == 0)
273 ✗ return 0;
274
275
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2620 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
2650 if (header && frag->data[0]) {
276 // HVCC header.
277 size_t size, start, end;
278 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
279
280 ✗ h2645->mp4 = 1;
281
282 ✗ bytestream2_init(&gbc, frag->data, frag->data_size);
283
284 ✗ if (bytestream2_get_bytes_left(&gbc) < 23)
285 ✗ return AVERROR_INVALIDDATA;
286
287 ✗ version = bytestream2_get_byte(&gbc);
288 ✗ if (version != 1) {
289 ✗ av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
290 "first byte %u.\n", version);
291 ✗ return AVERROR_INVALIDDATA;
292 }
293
294 ✗ bytestream2_skip(&gbc, 20);
295 ✗ h2645->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
296
297 ✗ nb_arrays = bytestream2_get_byte(&gbc);
298 ✗ for (i = 0; i < nb_arrays; i++) {
299 ✗ nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
300 ✗ nb_nals = bytestream2_get_be16(&gbc);
301
302 ✗ start = bytestream2_tell(&gbc);
303 ✗ for (j = 0; j < nb_nals; j++) {
304 ✗ if (bytestream2_get_bytes_left(&gbc) < 2)
305 ✗ return AVERROR_INVALIDDATA;
306 ✗ size = bytestream2_get_be16(&gbc);
307 ✗ if (bytestream2_get_bytes_left(&gbc) < size)
308 ✗ return AVERROR_INVALIDDATA;
309 ✗ bytestream2_skip(&gbc, size);
310 }
311 ✗ end = bytestream2_tell(&gbc);
312
313 ✗ err = ff_h2645_packet_split(&h2645->read_packet,
314 ✗ frag->data + start, end - start,
315 ctx->log_ctx, 2, AV_CODEC_ID_HEVC,
316 H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
317 ✗ if (err < 0) {
318 ✗ av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
319 "HVCC array %d (%d NAL units of type %d).\n",
320 i, nb_nals, nal_unit_type);
321 ✗ return err;
322 }
323 ✗ err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
324 ✗ if (err < 0)
325 ✗ return err;
326 }
327 } else {
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2650 times.
2650 int flags = (H2645_FLAG_IS_NALFF * !!h2645->mp4) | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF;
329 // Annex B, or later MP4 with already-known parameters.
330
331 2650 err = ff_h2645_packet_split(&h2645->read_packet,
332 2650 frag->data, frag->data_size,
333 ctx->log_ctx,
334 h2645->nal_length_size,
335 codec_id, flags);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2650 times.
2650 if (err < 0)
337 ✗ return err;
338
339 2650 err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2650 times.
2650 if (err < 0)
341 ✗ return err;
342 }
343
344 2650 return 0;
345 }
346
347 #define cbs_h2645_replace_ps(ps_name, ps_var, id_element) \
348 static int cbs_h265_replace_ ## ps_var(CodedBitstreamContext *ctx, \
349 CodedBitstreamUnit *unit) \
350 { \
351 CodedBitstreamH265Context *priv = ctx->priv_data; \
352 H265Raw## ps_name *ps_var = unit->content; \
353 unsigned int id = ps_var->id_element; \
354 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
355 if (err < 0) \
356 return err; \
357 if (priv->ps_var[id] == priv->active_ ## ps_var) \
358 priv->active_ ## ps_var = NULL ; \
359 av_assert0(unit->content_ref); \
360 av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
361 return 0; \
362 }
363
364
3/6
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 132 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 132 times.
132 cbs_h2645_replace_ps(VPS, vps, vps_video_parameter_set_id)
365
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✓ Branch 3 taken 135 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 138 times.
138 cbs_h2645_replace_ps(SPS, sps, sps_seq_parameter_set_id)
366
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 714 times.
✓ Branch 3 taken 461 times.
✓ Branch 4 taken 253 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 714 times.
714 cbs_h2645_replace_ps(PPS, pps, pps_pic_parameter_set_id)
367
368 9969 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
369 CodedBitstreamUnit *unit)
370 {
371 GetBitContext gbc;
372 int err;
373
374 9969 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9969 times.
9969 if (err < 0)
376 ✗ return err;
377
378 9969 err = ff_cbs_alloc_unit_content(ctx, unit);
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9969 times.
9969 if (err < 0)
380 ✗ return err;
381
382
6/8
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 424 times.
✓ Branch 3 taken 7484 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1859 times.
✗ Branch 7 not taken.
9969 switch (unit->type) {
383 69 case HEVC_NAL_VPS:
384 {
385 69 H265RawVPS *vps = unit->content;
386
387 69 err = cbs_h265_read_vps(ctx, &gbc, vps);
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (err < 0)
389 ✗ return err;
390
391 69 err = cbs_h265_replace_vps(ctx, unit);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (err < 0)
393 ✗ return err;
394 }
395 69 break;
396 73 case HEVC_NAL_SPS:
397 {
398 73 H265RawSPS *sps = unit->content;
399
400 73 err = cbs_h265_read_sps(ctx, &gbc, sps);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
402 ✗ return err;
403
404 73 err = cbs_h265_replace_sps(ctx, unit);
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (err < 0)
406 ✗ return err;
407 }
408 73 break;
409
410 424 case HEVC_NAL_PPS:
411 {
412 424 H265RawPPS *pps = unit->content;
413
414 424 err = cbs_h265_read_pps(ctx, &gbc, pps);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424 times.
424 if (err < 0)
416 ✗ return err;
417
418 424 err = cbs_h265_replace_pps(ctx, unit);
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424 times.
424 if (err < 0)
420 ✗ return err;
421 }
422 424 break;
423
424 7484 case HEVC_NAL_TRAIL_N:
425 case HEVC_NAL_TRAIL_R:
426 case HEVC_NAL_TSA_N:
427 case HEVC_NAL_TSA_R:
428 case HEVC_NAL_STSA_N:
429 case HEVC_NAL_STSA_R:
430 case HEVC_NAL_RADL_N:
431 case HEVC_NAL_RADL_R:
432 case HEVC_NAL_RASL_N:
433 case HEVC_NAL_RASL_R:
434 case HEVC_NAL_BLA_W_LP:
435 case HEVC_NAL_BLA_W_RADL:
436 case HEVC_NAL_BLA_N_LP:
437 case HEVC_NAL_IDR_W_RADL:
438 case HEVC_NAL_IDR_N_LP:
439 case HEVC_NAL_CRA_NUT:
440 {
441 7484 H265RawSlice *slice = unit->content;
442 int pos, len;
443
444 7484 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7484 times.
7484 if (err < 0)
446 ✗ return err;
447
448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7484 times.
7484 if (!ff_cbs_h2645_read_more_rbsp_data(&gbc))
449 ✗ return AVERROR_INVALIDDATA;
450
451 7484 pos = get_bits_count(&gbc);
452 7484 len = unit->data_size;
453
454 7484 slice->data_size = len - pos / 8;
455 7484 slice->data_ref = av_buffer_ref(unit->data_ref);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7484 times.
7484 if (!slice->data_ref)
457 ✗ return AVERROR(ENOMEM);
458 7484 slice->data = unit->data + pos / 8;
459 7484 slice->data_bit_start = pos % 8;
460 }
461 7484 break;
462
463 60 case HEVC_NAL_AUD:
464 {
465 60 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
467 ✗ return err;
468 }
469 60 break;
470
471 ✗ case HEVC_NAL_FD_NUT:
472 {
473 ✗ err = cbs_h265_read_filler(ctx, &gbc, unit->content);
474 ✗ if (err < 0)
475 ✗ return err;
476 }
477 ✗ break;
478
479 1859 case HEVC_NAL_SEI_PREFIX:
480 case HEVC_NAL_SEI_SUFFIX:
481 {
482 1859 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
483 1859 unit->type == HEVC_NAL_SEI_PREFIX);
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1859 times.
1859 if (err < 0)
486 ✗ return err;
487 }
488 1859 break;
489
490 ✗ default:
491 ✗ return AVERROR(ENOSYS);
492 }
493
494 9969 return 0;
495 }
496
497 8146 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
498 CodedBitstreamUnit *unit,
499 PutBitContext *pbc)
500 {
501 int err;
502
503
6/8
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 290 times.
✓ Branch 3 taken 6104 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
8146 switch (unit->type) {
504 63 case HEVC_NAL_VPS:
505 {
506 63 H265RawVPS *vps = unit->content;
507
508 63 err = cbs_h265_write_vps(ctx, pbc, vps);
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (err < 0)
510 ✗ return err;
511
512 63 err = cbs_h265_replace_vps(ctx, unit);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (err < 0)
514 ✗ return err;
515 }
516 63 break;
517
518 65 case HEVC_NAL_SPS:
519 {
520 65 H265RawSPS *sps = unit->content;
521
522 65 err = cbs_h265_write_sps(ctx, pbc, sps);
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (err < 0)
524 ✗ return err;
525
526 65 err = cbs_h265_replace_sps(ctx, unit);
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (err < 0)
528 ✗ return err;
529 }
530 65 break;
531
532 290 case HEVC_NAL_PPS:
533 {
534 290 H265RawPPS *pps = unit->content;
535
536 290 err = cbs_h265_write_pps(ctx, pbc, pps);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (err < 0)
538 ✗ return err;
539
540 290 err = cbs_h265_replace_pps(ctx, unit);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (err < 0)
542 ✗ return err;
543 }
544 290 break;
545
546 6104 case HEVC_NAL_TRAIL_N:
547 case HEVC_NAL_TRAIL_R:
548 case HEVC_NAL_TSA_N:
549 case HEVC_NAL_TSA_R:
550 case HEVC_NAL_STSA_N:
551 case HEVC_NAL_STSA_R:
552 case HEVC_NAL_RADL_N:
553 case HEVC_NAL_RADL_R:
554 case HEVC_NAL_RASL_N:
555 case HEVC_NAL_RASL_R:
556 case HEVC_NAL_BLA_W_LP:
557 case HEVC_NAL_BLA_W_RADL:
558 case HEVC_NAL_BLA_N_LP:
559 case HEVC_NAL_IDR_W_RADL:
560 case HEVC_NAL_IDR_N_LP:
561 case HEVC_NAL_CRA_NUT:
562 {
563 6104 H265RawSlice *slice = unit->content;
564
565 6104 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6104 times.
6104 if (err < 0)
567 ✗ return err;
568
569
1/2
✓ Branch 0 taken 6104 times.
✗ Branch 1 not taken.
6104 if (slice->data) {
570 6104 err = ff_cbs_h2645_write_slice_data(ctx, pbc, slice->data,
571 slice->data_size,
572 slice->data_bit_start);
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6104 times.
6104 if (err < 0)
574 ✗ return err;
575 } else {
576 // No slice data - that was just the header.
577 }
578 }
579 6104 break;
580
581 60 case HEVC_NAL_AUD:
582 {
583 60 err = cbs_h265_write_aud(ctx, pbc, unit->content);
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
585 ✗ return err;
586 }
587 60 break;
588
589 ✗ case HEVC_NAL_FD_NUT:
590 {
591 ✗ err = cbs_h265_write_filler(ctx, pbc, unit->content);
592 ✗ if (err < 0)
593 ✗ return err;
594 }
595 ✗ break;
596
597 1564 case HEVC_NAL_SEI_PREFIX:
598 case HEVC_NAL_SEI_SUFFIX:
599 {
600 1564 err = cbs_h265_write_sei(ctx, pbc, unit->content,
601 1564 unit->type == HEVC_NAL_SEI_PREFIX);
602
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1564 times.
1564 if (err < 0)
604 ✗ return err;
605 }
606 1564 break;
607
608 ✗ default:
609 ✗ av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
610 "NAL unit type %"PRIu32".\n", unit->type);
611 ✗ return AVERROR_PATCHWELCOME;
612 }
613
614 8146 return 0;
615 }
616
617 536 static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx,
618 const CodedBitstreamUnit *unit,
619 enum AVDiscard skip)
620 {
621 H265RawSliceHeader *slice;
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536 times.
536 if (skip <= AVDISCARD_DEFAULT)
624 ✗ return 0;
625
626
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 204 times.
536 switch (unit->type) {
627 4 case HEVC_NAL_BLA_W_LP:
628 case HEVC_NAL_BLA_W_RADL:
629 case HEVC_NAL_BLA_N_LP:
630 case HEVC_NAL_IDR_W_RADL:
631 case HEVC_NAL_IDR_N_LP:
632 case HEVC_NAL_CRA_NUT:
633 // IRAP slice
634
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (skip < AVDISCARD_ALL)
635 4 return 0;
636 ✗ break;
637
638 328 case HEVC_NAL_TRAIL_R:
639 case HEVC_NAL_TRAIL_N:
640 case HEVC_NAL_TSA_N:
641 case HEVC_NAL_TSA_R:
642 case HEVC_NAL_STSA_N:
643 case HEVC_NAL_STSA_R:
644 case HEVC_NAL_RADL_N:
645 case HEVC_NAL_RADL_R:
646 case HEVC_NAL_RASL_N:
647 case HEVC_NAL_RASL_R:
648 // Slice
649 328 break;
650 204 default:
651 // Don't discard non-slice nal.
652 204 return 0;
653 }
654
655
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 281 times.
328 if (skip >= AVDISCARD_NONKEY)
656 47 return 1;
657
658 281 slice = (H265RawSliceHeader *)unit->content;
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 if (!slice) {
660 ✗ av_log(ctx->log_ctx, AV_LOG_WARNING,
661 "h265 slice header is null, missing decompose?\n");
662 ✗ return 0;
663 }
664
665
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)
666 47 return 1;
667
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)
668 47 return 1;
669
670
1/2
✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
187 if (skip >= AVDISCARD_NONREF) {
671
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 163 times.
187 switch (unit->type) {
672 24 case HEVC_NAL_TRAIL_N:
673 case HEVC_NAL_TSA_N:
674 case HEVC_NAL_STSA_N:
675 case HEVC_NAL_RADL_N:
676 case HEVC_NAL_RASL_N:
677 case HEVC_NAL_VCL_N10:
678 case HEVC_NAL_VCL_N12:
679 case HEVC_NAL_VCL_N14:
680 // non-ref
681 24 return 1;
682 163 default:
683 163 break;
684 }
685 }
686
687 163 return 0;
688 }
689
690 3 static av_cold void cbs_h265_flush(CodedBitstreamContext *ctx)
691 {
692 3 CodedBitstreamH265Context *h265 = ctx->priv_data;
693
694
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
695 48 av_refstruct_unref(&h265->vps[i]);
696
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
697 48 av_refstruct_unref(&h265->sps[i]);
698
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
195 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
699 192 av_refstruct_unref(&h265->pps[i]);
700
701 3 h265->active_vps = NULL;
702 3 h265->active_sps = NULL;
703 3 h265->active_pps = NULL;
704 3 }
705
706 53 static av_cold void cbs_h265_close(CodedBitstreamContext *ctx)
707 {
708 53 CodedBitstreamH265Context *h265 = ctx->priv_data;
709 int i;
710
711 53 ff_h2645_packet_uninit(&h265->common.read_packet);
712
713
2/2
✓ Branch 0 taken 848 times.
✓ Branch 1 taken 53 times.
901 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
714 848 av_refstruct_unref(&h265->vps[i]);
715
2/2
✓ Branch 0 taken 848 times.
✓ Branch 1 taken 53 times.
901 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
716 848 av_refstruct_unref(&h265->sps[i]);
717
2/2
✓ Branch 0 taken 3392 times.
✓ Branch 1 taken 53 times.
3445 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
718 3392 av_refstruct_unref(&h265->pps[i]);
719 53 }
720
721 1859 static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
722 {
723 1859 H265RawSEI *sei = content;
724 1859 ff_cbs_sei_free_message_list(&sei->message_list);
725 1859 }
726
727 static CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
728 {
729 .nb_unit_types = 1,
730 .unit_type.list = { HEVC_NAL_VPS },
731 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
732 .content_size = sizeof(H265RawVPS),
733 .type.ref = {
734 .nb_offsets = 2,
735 .offsets = { offsetof(H265RawVPS, extension_data.data),
736 offsetof(H265RawVPS, hrd_parameters) }
737 },
738 },
739
740 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
741 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
742
743 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
744 CBS_UNIT_TYPE_POD(HEVC_NAL_FD_NUT, H265RawFiller),
745
746 // Slices of non-IRAP pictures.
747 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_TRAIL_N, HEVC_NAL_RASL_R,
748 H265RawSlice, data),
749 // Slices of IRAP pictures.
750 CBS_UNIT_RANGE_INTERNAL_REF(HEVC_NAL_BLA_W_LP, HEVC_NAL_CRA_NUT,
751 H265RawSlice, data),
752
753 CBS_UNIT_TYPES_COMPLEX((HEVC_NAL_SEI_PREFIX, HEVC_NAL_SEI_SUFFIX),
754 H265RawSEI, cbs_h265_free_sei),
755
756 CBS_UNIT_TYPE_END_OF_LIST
757 };
758
759 // Macro for the read/write pair.
760 #define SEI_MESSAGE_RW(codec, name) \
761 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
762 .write = cbs_ ## codec ## _write_ ## name ## _internal
763
764 const SEIMessageTypeDescriptor ff_cbs_sei_h265_types[] = {
765 {
766 SEI_TYPE_BUFFERING_PERIOD,
767 1, 0,
768 sizeof(H265RawSEIBufferingPeriod),
769 SEI_MESSAGE_RW(h265, sei_buffering_period),
770 },
771 {
772 SEI_TYPE_PIC_TIMING,
773 1, 0,
774 sizeof(H265RawSEIPicTiming),
775 SEI_MESSAGE_RW(h265, sei_pic_timing),
776 },
777 {
778 SEI_TYPE_PAN_SCAN_RECT,
779 1, 0,
780 sizeof(H265RawSEIPanScanRect),
781 SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
782 },
783 {
784 SEI_TYPE_RECOVERY_POINT,
785 1, 0,
786 sizeof(H265RawSEIRecoveryPoint),
787 SEI_MESSAGE_RW(h265, sei_recovery_point),
788 },
789 {
790 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
791 1, 0,
792 sizeof(H265RawFilmGrainCharacteristics),
793 SEI_MESSAGE_RW(h265, film_grain_characteristics),
794 },
795 {
796 SEI_TYPE_DISPLAY_ORIENTATION,
797 1, 0,
798 sizeof(H265RawSEIDisplayOrientation),
799 SEI_MESSAGE_RW(h265, sei_display_orientation),
800 },
801 {
802 SEI_TYPE_ACTIVE_PARAMETER_SETS,
803 1, 0,
804 sizeof(H265RawSEIActiveParameterSets),
805 SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
806 },
807 {
808 SEI_TYPE_DECODED_PICTURE_HASH,
809 0, 1,
810 sizeof(H265RawSEIDecodedPictureHash),
811 SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
812 },
813 {
814 SEI_TYPE_TIME_CODE,
815 1, 0,
816 sizeof(H265RawSEITimeCode),
817 SEI_MESSAGE_RW(h265, sei_time_code),
818 },
819 {
820 SEI_TYPE_ALPHA_CHANNEL_INFO,
821 1, 0,
822 sizeof(H265RawSEIAlphaChannelInfo),
823 SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
824 },
825 {
826 SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO,
827 1, 0,
828 sizeof(H265RawSEI3DReferenceDisplaysInfo),
829 SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
830 },
831 SEI_MESSAGE_TYPE_END
832 };
833
834 const CodedBitstreamType ff_cbs_type_h265 = {
835 .codec_id = AV_CODEC_ID_HEVC,
836
837 .priv_data_size = sizeof(CodedBitstreamH265Context),
838
839 .unit_types = cbs_h265_unit_types,
840
841 .split_fragment = &cbs_h265_split_fragment,
842 .read_unit = &cbs_h265_read_nal_unit,
843 .write_unit = &cbs_h265_write_nal_unit,
844 .discarded_unit = &cbs_h265_discarded_nal_unit,
845 .assemble_fragment = &ff_cbs_h2645_assemble_fragment,
846
847 .flush = &cbs_h265_flush,
848 .close = &cbs_h265_close,
849 };
850