FFmpeg
cbs_h2645.c
Go to the documentation of this file.
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 
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 
45 
46  max_length = FFMIN(get_bits_left(gbc), 32);
47 
48  leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
49  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  leading_zeroes = max_length - 1 - av_log2(leading_bits);
62  skip_bits_long(gbc, leading_zeroes);
63 
64  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  value = get_bits_long(gbc, leading_zeroes + 1) - 1;
71 
73 
74  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  *write_to = value;
82  return 0;
83 }
84 
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 
95 
96  max_length = FFMIN(get_bits_left(gbc), 32);
97 
98  leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
99  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  leading_zeroes = max_length - 1 - av_log2(leading_bits);
112  skip_bits_long(gbc, leading_zeroes);
113 
114  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  unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
121 
122  if (unsigned_value & 1)
123  value = -(int32_t)(unsigned_value / 2);
124  else
125  value = unsigned_value / 2;
126 
128 
129  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  *write_to = value;
137  return 0;
138 }
139 
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 
148 
149  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  av_assert0(value != UINT32_MAX);
156 
157  len = av_log2(value + 1);
158  if (put_bits_left(pbc) < 2 * len + 1)
159  return AVERROR(ENOSPC);
160 
161  put_bits(pbc, len, 0);
162  if (len + 1 < 32)
163  put_bits(pbc, len + 1, value + 1);
164  else
165  put_bits32(pbc, value + 1);
166 
168 
169  return 0;
170 }
171 
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 
181 
182  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  av_assert0(value != INT32_MIN);
189 
190  if (value == 0)
191  uvalue = 0;
192  else if (value > 0)
193  uvalue = 2 * (uint32_t)value - 1;
194  else
195  uvalue = 2 * (uint32_t)-value;
196 
197  len = av_log2(uvalue + 1);
198  if (put_bits_left(pbc) < 2 * len + 1)
199  return AVERROR(ENOSPC);
200 
201  put_bits(pbc, len, 0);
202  if (len + 1 < 32)
203  put_bits(pbc, len + 1, uvalue + 1);
204  else
205  put_bits32(pbc, uvalue + 1);
206 
208 
209  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 static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
215  int cur_pos)
216 {
217  int bits_left = payload_size * 8 - cur_pos;
218  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 
329 {
330  int bits_left = get_bits_left(gbc);
331  if (bits_left > 8)
332  return 1;
333  if (bits_left == 0)
334  return 0;
335  if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
336  return 1;
337  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)
372 #undef FUNC
373 
374 #define FUNC(name) FUNC_H265(name)
376 #undef FUNC
377 
378 #define FUNC(name) FUNC_H266(name)
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)
460 #undef FUNC
461 
462 #define FUNC(name) FUNC_H265(name)
464 #undef FUNC
465 
466 #define FUNC(name) FUNC_H266(name)
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 
492  const H2645Packet *packet)
493 {
494  int err, i;
495 
496  for (i = 0; i < packet->nb_nals; i++) {
497  const H2645NAL *nal = &packet->nals[i];
498  AVBufferRef *ref;
499  size_t size = nal->size;
500  enum AVCodecID codec_id = ctx->codec->codec_id;
501 
502  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  while (size > 0 && nal->data[size - 1] == 0)
508  --size;
509  if (size == 0) {
510  av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
511  continue;
512  }
513 
514  ref = (nal->data == nal->raw_data) ? frag->data_ref
515  : packet->rbsp.rbsp_buffer_ref;
516 
517  err = ff_cbs_append_unit_data(frag, nal->type,
518  (uint8_t*)nal->data, size, ref);
519  if (err < 0)
520  return err;
521  }
522 
523  return 0;
524 }
525 
528  int header)
529 {
530  enum AVCodecID codec_id = ctx->codec->codec_id;
532  GetByteContext gbc;
533  int err;
534 
535  av_assert0(frag->data && frag->nb_units == 0);
536  if (frag->data_size == 0)
537  return 0;
538 
539  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  priv->mp4 = 1;
545 
546  bytestream2_init(&gbc, frag->data, frag->data_size);
547 
548  if (bytestream2_get_bytes_left(&gbc) < 6)
549  return AVERROR_INVALIDDATA;
550 
551  version = bytestream2_get_byte(&gbc);
552  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  bytestream2_skip(&gbc, 3);
559  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
560 
561  // SPS array.
562  count = bytestream2_get_byte(&gbc) & 0x1f;
563  start = bytestream2_tell(&gbc);
564  for (i = 0; i < count; i++) {
565  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
566  return AVERROR_INVALIDDATA;
567  size = bytestream2_get_be16(&gbc);
568  if (bytestream2_get_bytes_left(&gbc) < size)
569  return AVERROR_INVALIDDATA;
570  bytestream2_skip(&gbc, size);
571  }
572  end = bytestream2_tell(&gbc);
573 
574  err = ff_h2645_packet_split(&priv->read_packet,
575  frag->data + start, end - start,
576  ctx->log_ctx, 2, AV_CODEC_ID_H264,
578  if (err < 0) {
579  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS 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  // PPS array.
587  count = bytestream2_get_byte(&gbc);
588  start = bytestream2_tell(&gbc);
589  for (i = 0; i < count; i++) {
590  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
591  return AVERROR_INVALIDDATA;
592  size = bytestream2_get_be16(&gbc);
593  if (bytestream2_get_bytes_left(&gbc) < size)
594  return AVERROR_INVALIDDATA;
595  bytestream2_skip(&gbc, size);
596  }
597  end = bytestream2_tell(&gbc);
598 
599  err = ff_h2645_packet_split(&priv->read_packet,
600  frag->data + start, end - start,
601  ctx->log_ctx, 2, AV_CODEC_ID_H264,
603  if (err < 0) {
604  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
605  return err;
606  }
607  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
608  if (err < 0)
609  return err;
610 
611  if (bytestream2_get_bytes_left(&gbc) > 0) {
612  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
613  "header.\n", bytestream2_get_bytes_left(&gbc));
614  }
615 
616  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
617  // HVCC header.
618  size_t size, start, end;
619  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
620 
621  priv->mp4 = 1;
622 
623  bytestream2_init(&gbc, frag->data, frag->data_size);
624 
625  if (bytestream2_get_bytes_left(&gbc) < 23)
626  return AVERROR_INVALIDDATA;
627 
628  version = bytestream2_get_byte(&gbc);
629  if (version != 1) {
630  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
631  "first byte %u.\n", version);
632  return AVERROR_INVALIDDATA;
633  }
634 
635  bytestream2_skip(&gbc, 20);
636  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
637 
638  nb_arrays = bytestream2_get_byte(&gbc);
639  for (i = 0; i < nb_arrays; i++) {
640  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
641  nb_nals = bytestream2_get_be16(&gbc);
642 
643  start = bytestream2_tell(&gbc);
644  for (j = 0; j < nb_nals; j++) {
645  if (bytestream2_get_bytes_left(&gbc) < 2)
646  return AVERROR_INVALIDDATA;
647  size = bytestream2_get_be16(&gbc);
648  if (bytestream2_get_bytes_left(&gbc) < size)
649  return AVERROR_INVALIDDATA;
650  bytestream2_skip(&gbc, size);
651  }
652  end = bytestream2_tell(&gbc);
653 
654  err = ff_h2645_packet_split(&priv->read_packet,
655  frag->data + start, end - start,
656  ctx->log_ctx, 2, AV_CODEC_ID_HEVC,
658  if (err < 0) {
659  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
660  "HVCC array %d (%d NAL units of type %d).\n",
661  i, nb_nals, nal_unit_type);
662  return err;
663  }
664  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
665  if (err < 0)
666  return err;
667  }
668 
669  } else if(header && frag->data[0] && codec_id == AV_CODEC_ID_VVC) {
670  // VVCC header.
671  int ptl_present_flag, num_arrays;
672  int b, i, j;
673 
674  priv->mp4 = 1;
675 
676  bytestream2_init(&gbc, frag->data, frag->data_size);
677 
678  b = bytestream2_get_byte(&gbc);
679  priv->nal_length_size = ((b >> 1) & 3) + 1;
680  ptl_present_flag = b & 1;
681 
682  if(ptl_present_flag) {
683  int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
684  num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
685  bytestream2_skip(&gbc, 1);
686 
687  // begin VvcPTLRecord(num_sublayers);
688  num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
689  bytestream2_skip(&gbc, 2 + num_bytes_constraint_info);
690  if(num_sublayers > 1) {
691  int count_present_flags = 0;
692  b = bytestream2_get_byte(&gbc);
693  for(i = num_sublayers - 2; i >= 0; i--) {
694  if((b >> (7 - (num_sublayers - 2 - i))) & 0x01)
695  count_present_flags++;
696  }
697  bytestream2_skip(&gbc, count_present_flags);
698  }
699  num_sub_profiles = bytestream2_get_byte(&gbc);
700  bytestream2_skip(&gbc, num_sub_profiles * 4);
701  // end VvcPTLRecord(num_sublayers);
702 
703  bytestream2_skip(&gbc, 3 * 2);
704  }
705 
706  num_arrays = bytestream2_get_byte(&gbc);
707  for(j = 0; j < num_arrays; j++) {
708  size_t start, end, size;
709  int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
710  unsigned int num_nalus = 1;
711  if(nal_unit_type != VVC_DCI_NUT && nal_unit_type != VVC_OPI_NUT)
712  num_nalus = bytestream2_get_be16(&gbc);
713 
714  start = bytestream2_tell(&gbc);
715  for(i = 0; i < num_nalus; i++) {
716  if (bytestream2_get_bytes_left(&gbc) < 2)
717  return AVERROR_INVALIDDATA;
718  size = bytestream2_get_be16(&gbc);
719  if (bytestream2_get_bytes_left(&gbc) < size)
720  return AVERROR_INVALIDDATA;
721  bytestream2_skip(&gbc, size);
722  }
723  end = bytestream2_tell(&gbc);
724 
725  err = ff_h2645_packet_split(&priv->read_packet,
726  frag->data + start, end - start,
727  ctx->log_ctx, 2, AV_CODEC_ID_VVC,
729  if (err < 0) {
730  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
731  "VVCC array %d (%d NAL units of type %d).\n",
732  i, num_nalus, nal_unit_type);
733  return err;
734  }
735  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
736  if (err < 0)
737  return err;
738  }
739  } else {
741  // Annex B, or later MP4 with already-known parameters.
742 
743  err = ff_h2645_packet_split(&priv->read_packet,
744  frag->data, frag->data_size,
745  ctx->log_ctx,
746  priv->nal_length_size,
747  codec_id, flags);
748  if (err < 0)
749  return err;
750 
751  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
752  if (err < 0)
753  return err;
754  }
755 
756  return 0;
757 }
758 
759 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
760 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
761  CodedBitstreamUnit *unit) \
762 { \
763  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
764  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
765  unsigned int id = ps_var->id_element; \
766  int err = ff_cbs_make_unit_refcounted(ctx, unit); \
767  if (err < 0) \
768  return err; \
769  if (priv->ps_var[id] == priv->active_ ## ps_var) \
770  priv->active_ ## ps_var = NULL ; \
771  av_assert0(unit->content_ref); \
772  ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
773  return 0; \
774 }
775 
776 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
777 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
778 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
779 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
780 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
781 
782 #define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
783 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
784  CodedBitstreamUnit *unit) \
785 { \
786  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
787  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
788  unsigned int id = ps_var->id_element; \
789  int err = ff_cbs_make_unit_refcounted(ctx, unit); \
790  if (err < 0) \
791  return err; \
792  av_assert0(unit->content_ref); \
793  ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
794  return 0; \
795 }
796 
797 cbs_h266_replace_ps(6, VPS, vps, vps_video_parameter_set_id)
798 cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
799 
800 static int cbs_h266_replace_sps(CodedBitstreamContext *ctx,
801  CodedBitstreamUnit *unit)
802 {
804  H266RawSPS *sps = unit->content;
805  unsigned int id = sps->sps_seq_parameter_set_id;
806  int err = ff_cbs_make_unit_refcounted(ctx, unit);
807  if (err < 0)
808  return err;
809  av_assert0(unit->content_ref);
810  if (priv->sps[id] && memcmp(priv->sps[id], unit->content_ref, sizeof(*priv->sps[id]))) {
811  for (unsigned int i = 0; i < VVC_MAX_PPS_COUNT; i++) {
812  if (priv->pps[i] && priv->pps[i]->pps_seq_parameter_set_id == id)
813  ff_refstruct_unref(&priv->pps[i]);
814  }
815  }
816  ff_refstruct_replace(&priv->sps[id], unit->content_ref);
817  return 0;
818 }
819 
821  CodedBitstreamUnit *unit,
823 {
825  int err;
826 
827  err = ff_cbs_make_unit_refcounted(ctx, unit);
828  if (err < 0)
829  return err;
830  av_assert0(unit->content_ref);
831  ff_refstruct_replace(&h266->ph_ref, unit->content_ref);
832  h266->ph = ph;
833  return 0;
834 }
835 
837  CodedBitstreamUnit *unit)
838 {
839  GetBitContext gbc;
840  int err;
841 
842  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
843  if (err < 0)
844  return err;
845 
846  err = ff_cbs_alloc_unit_content(ctx, unit);
847  if (err < 0)
848  return err;
849 
850  switch (unit->type) {
851  case H264_NAL_SPS:
852  {
853  H264RawSPS *sps = unit->content;
854 
855  err = cbs_h264_read_sps(ctx, &gbc, sps);
856  if (err < 0)
857  return err;
858 
859  err = cbs_h264_replace_sps(ctx, unit);
860  if (err < 0)
861  return err;
862  }
863  break;
864 
865  case H264_NAL_SPS_EXT:
866  {
867  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
868  if (err < 0)
869  return err;
870  }
871  break;
872 
873  case H264_NAL_PPS:
874  {
875  H264RawPPS *pps = unit->content;
876 
877  err = cbs_h264_read_pps(ctx, &gbc, pps);
878  if (err < 0)
879  return err;
880 
881  err = cbs_h264_replace_pps(ctx, unit);
882  if (err < 0)
883  return err;
884  }
885  break;
886 
887  case H264_NAL_SLICE:
888  case H264_NAL_IDR_SLICE:
890  {
891  H264RawSlice *slice = unit->content;
892  int pos, len;
893 
894  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
895  if (err < 0)
896  return err;
897 
899  return AVERROR_INVALIDDATA;
900 
901  pos = get_bits_count(&gbc);
902  len = unit->data_size;
903 
904  slice->data_size = len - pos / 8;
905  slice->data_ref = av_buffer_ref(unit->data_ref);
906  if (!slice->data_ref)
907  return AVERROR(ENOMEM);
908  slice->data = unit->data + pos / 8;
909  slice->data_bit_start = pos % 8;
910  }
911  break;
912 
913  case H264_NAL_AUD:
914  {
915  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
916  if (err < 0)
917  return err;
918  }
919  break;
920 
921  case H264_NAL_SEI:
922  {
923  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
924  if (err < 0)
925  return err;
926  }
927  break;
928 
930  {
931  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
932  if (err < 0)
933  return err;
934  }
935  break;
936 
938  case H264_NAL_END_STREAM:
939  {
940  err = (unit->type == H264_NAL_END_SEQUENCE ?
941  cbs_h264_read_end_of_sequence :
942  cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
943  if (err < 0)
944  return err;
945  }
946  break;
947 
948  default:
949  return AVERROR(ENOSYS);
950  }
951 
952  return 0;
953 }
954 
956  CodedBitstreamUnit *unit)
957 {
958  GetBitContext gbc;
959  int err;
960 
961  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
962  if (err < 0)
963  return err;
964 
965  err = ff_cbs_alloc_unit_content(ctx, unit);
966  if (err < 0)
967  return err;
968 
969  switch (unit->type) {
970  case HEVC_NAL_VPS:
971  {
972  H265RawVPS *vps = unit->content;
973 
974  err = cbs_h265_read_vps(ctx, &gbc, vps);
975  if (err < 0)
976  return err;
977 
978  err = cbs_h265_replace_vps(ctx, unit);
979  if (err < 0)
980  return err;
981  }
982  break;
983  case HEVC_NAL_SPS:
984  {
985  H265RawSPS *sps = unit->content;
986 
987  err = cbs_h265_read_sps(ctx, &gbc, sps);
988  if (err < 0)
989  return err;
990 
991  err = cbs_h265_replace_sps(ctx, unit);
992  if (err < 0)
993  return err;
994  }
995  break;
996 
997  case HEVC_NAL_PPS:
998  {
999  H265RawPPS *pps = unit->content;
1000 
1001  err = cbs_h265_read_pps(ctx, &gbc, pps);
1002  if (err < 0)
1003  return err;
1004 
1005  err = cbs_h265_replace_pps(ctx, unit);
1006  if (err < 0)
1007  return err;
1008  }
1009  break;
1010 
1011  case HEVC_NAL_TRAIL_N:
1012  case HEVC_NAL_TRAIL_R:
1013  case HEVC_NAL_TSA_N:
1014  case HEVC_NAL_TSA_R:
1015  case HEVC_NAL_STSA_N:
1016  case HEVC_NAL_STSA_R:
1017  case HEVC_NAL_RADL_N:
1018  case HEVC_NAL_RADL_R:
1019  case HEVC_NAL_RASL_N:
1020  case HEVC_NAL_RASL_R:
1021  case HEVC_NAL_BLA_W_LP:
1022  case HEVC_NAL_BLA_W_RADL:
1023  case HEVC_NAL_BLA_N_LP:
1024  case HEVC_NAL_IDR_W_RADL:
1025  case HEVC_NAL_IDR_N_LP:
1026  case HEVC_NAL_CRA_NUT:
1027  {
1028  H265RawSlice *slice = unit->content;
1029  int pos, len;
1030 
1031  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1032  if (err < 0)
1033  return err;
1034 
1035  if (!cbs_h2645_read_more_rbsp_data(&gbc))
1036  return AVERROR_INVALIDDATA;
1037 
1038  pos = get_bits_count(&gbc);
1039  len = unit->data_size;
1040 
1041  slice->data_size = len - pos / 8;
1042  slice->data_ref = av_buffer_ref(unit->data_ref);
1043  if (!slice->data_ref)
1044  return AVERROR(ENOMEM);
1045  slice->data = unit->data + pos / 8;
1046  slice->data_bit_start = pos % 8;
1047  }
1048  break;
1049 
1050  case HEVC_NAL_AUD:
1051  {
1052  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1053  if (err < 0)
1054  return err;
1055  }
1056  break;
1057 
1058  case HEVC_NAL_FD_NUT:
1059  {
1060  err = cbs_h265_read_filler(ctx, &gbc, unit->content);
1061  if (err < 0)
1062  return err;
1063  }
1064  break;
1065 
1066  case HEVC_NAL_SEI_PREFIX:
1067  case HEVC_NAL_SEI_SUFFIX:
1068  {
1069  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1070  unit->type == HEVC_NAL_SEI_PREFIX);
1071 
1072  if (err < 0)
1073  return err;
1074  }
1075  break;
1076 
1077  default:
1078  return AVERROR(ENOSYS);
1079  }
1080 
1081  return 0;
1082 }
1083 
1085  CodedBitstreamUnit *unit)
1086 {
1087  GetBitContext gbc;
1088  int err;
1089 
1090  err = init_get_bits8(&gbc, unit->data, unit->data_size);
1091  if (err < 0)
1092  return err;
1093 
1094  err = ff_cbs_alloc_unit_content(ctx, unit);
1095  if (err < 0)
1096  return err;
1097 
1098  switch (unit->type) {
1099  case VVC_DCI_NUT:
1100  {
1101  err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1102 
1103  if (err < 0)
1104  return err;
1105  }
1106  break;
1107  case VVC_OPI_NUT:
1108  {
1109  err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1110 
1111  if (err < 0)
1112  return err;
1113  }
1114  break;
1115  case VVC_VPS_NUT:
1116  {
1117  H266RawVPS *vps = unit->content;
1118 
1119  err = cbs_h266_read_vps(ctx, &gbc, vps);
1120  if (err < 0)
1121  return err;
1122 
1123  err = cbs_h266_replace_vps(ctx, unit);
1124  if (err < 0)
1125  return err;
1126  }
1127  break;
1128  case VVC_SPS_NUT:
1129  {
1130  H266RawSPS *sps = unit->content;
1131 
1132  err = cbs_h266_read_sps(ctx, &gbc, sps);
1133  if (err < 0)
1134  return err;
1135 
1136  err = cbs_h266_replace_sps(ctx, unit);
1137  if (err < 0)
1138  return err;
1139  }
1140  break;
1141 
1142  case VVC_PPS_NUT:
1143  {
1144  H266RawPPS *pps = unit->content;
1145 
1146  err = cbs_h266_read_pps(ctx, &gbc, pps);
1147  if (err < 0)
1148  return err;
1149 
1150  err = cbs_h266_replace_pps(ctx, unit);
1151  if (err < 0)
1152  return err;
1153  }
1154  break;
1155 
1156  case VVC_PREFIX_APS_NUT:
1157  case VVC_SUFFIX_APS_NUT:
1158  {
1159  err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1160  unit->type == VVC_PREFIX_APS_NUT);
1161 
1162  if (err < 0)
1163  return err;
1164  }
1165  break;
1166  case VVC_PH_NUT:
1167  {
1168  H266RawPH *ph = unit->content;
1169  err = cbs_h266_read_ph(ctx, &gbc, ph);
1170  if (err < 0)
1171  return err;
1172  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1173  if (err < 0)
1174  return err;
1175  }
1176  break;
1177 
1178  case VVC_TRAIL_NUT:
1179  case VVC_STSA_NUT:
1180  case VVC_RADL_NUT:
1181  case VVC_RASL_NUT:
1182  case VVC_IDR_W_RADL:
1183  case VVC_IDR_N_LP:
1184  case VVC_CRA_NUT:
1185  case VVC_GDR_NUT:
1186  {
1187  H266RawSlice *slice = unit->content;
1188  int pos, len;
1189 
1190  err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1191  if (err < 0)
1192  return err;
1193 
1194  if (!cbs_h2645_read_more_rbsp_data(&gbc))
1195  return AVERROR_INVALIDDATA;
1196 
1197  pos = get_bits_count(&gbc);
1198  len = unit->data_size;
1199 
1201  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1202  if (err < 0)
1203  return err;
1204  }
1205 
1206  slice->header_size = pos / 8;
1207  slice->data_size = len - pos / 8;
1208  slice->data_ref = av_buffer_ref(unit->data_ref);
1209  if (!slice->data_ref)
1210  return AVERROR(ENOMEM);
1211  slice->data = unit->data + pos / 8;
1212  slice->data_bit_start = pos % 8;
1213  }
1214  break;
1215 
1216  case VVC_AUD_NUT:
1217  {
1218  err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1219  if (err < 0)
1220  return err;
1221  }
1222  break;
1223 
1224  case VVC_PREFIX_SEI_NUT:
1225  case VVC_SUFFIX_SEI_NUT:
1226  {
1227  err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1228  unit->type == VVC_PREFIX_SEI_NUT);
1229 
1230  if (err < 0)
1231  return err;
1232  }
1233  break;
1234 
1235  default:
1236  return AVERROR(ENOSYS);
1237  }
1238  return 0;
1239 }
1240 
1242  PutBitContext *pbc, const uint8_t *data,
1243  size_t data_size, int data_bit_start)
1244 {
1245  size_t rest = data_size - (data_bit_start + 7) / 8;
1246  const uint8_t *pos = data + data_bit_start / 8;
1247 
1248  av_assert0(data_bit_start >= 0 &&
1249  data_size > data_bit_start / 8);
1250 
1251  if (data_size * 8 + 8 > put_bits_left(pbc))
1252  return AVERROR(ENOSPC);
1253 
1254  if (!rest)
1255  goto rbsp_stop_one_bit;
1256 
1257  // First copy the remaining bits of the first byte
1258  // The above check ensures that we do not accidentally
1259  // copy beyond the rbsp_stop_one_bit.
1260  if (data_bit_start % 8)
1261  put_bits(pbc, 8 - data_bit_start % 8,
1262  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1263 
1264  if (put_bits_count(pbc) % 8 == 0) {
1265  // If the writer is aligned at this point,
1266  // memcpy can be used to improve performance.
1267  // This happens normally for CABAC.
1268  flush_put_bits(pbc);
1269  memcpy(put_bits_ptr(pbc), pos, rest);
1270  skip_put_bytes(pbc, rest);
1271  } else {
1272  // If not, we have to copy manually.
1273  // rbsp_stop_one_bit forces us to special-case
1274  // the last byte.
1275  uint8_t temp;
1276  int i;
1277 
1278  for (; rest > 4; rest -= 4, pos += 4)
1279  put_bits32(pbc, AV_RB32(pos));
1280 
1281  for (; rest > 1; rest--, pos++)
1282  put_bits(pbc, 8, *pos);
1283 
1284  rbsp_stop_one_bit:
1285  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1286 
1287  av_assert0(temp);
1288  i = ff_ctz(*pos);
1289  temp = temp >> i;
1290  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1291  put_bits(pbc, i, temp);
1292  if (put_bits_count(pbc) % 8)
1293  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1294  }
1295 
1296  return 0;
1297 }
1298 
1300  CodedBitstreamUnit *unit,
1301  PutBitContext *pbc)
1302 {
1303  int err;
1304 
1305  switch (unit->type) {
1306  case H264_NAL_SPS:
1307  {
1308  H264RawSPS *sps = unit->content;
1309 
1310  err = cbs_h264_write_sps(ctx, pbc, sps);
1311  if (err < 0)
1312  return err;
1313 
1314  err = cbs_h264_replace_sps(ctx, unit);
1315  if (err < 0)
1316  return err;
1317  }
1318  break;
1319 
1320  case H264_NAL_SPS_EXT:
1321  {
1322  H264RawSPSExtension *sps_ext = unit->content;
1323 
1324  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1325  if (err < 0)
1326  return err;
1327  }
1328  break;
1329 
1330  case H264_NAL_PPS:
1331  {
1332  H264RawPPS *pps = unit->content;
1333 
1334  err = cbs_h264_write_pps(ctx, pbc, pps);
1335  if (err < 0)
1336  return err;
1337 
1338  err = cbs_h264_replace_pps(ctx, unit);
1339  if (err < 0)
1340  return err;
1341  }
1342  break;
1343 
1344  case H264_NAL_SLICE:
1345  case H264_NAL_IDR_SLICE:
1347  {
1348  H264RawSlice *slice = unit->content;
1349 
1350  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1351  if (err < 0)
1352  return err;
1353 
1354  if (slice->data) {
1355  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1356  slice->data_size,
1357  slice->data_bit_start);
1358  if (err < 0)
1359  return err;
1360  } else {
1361  // No slice data - that was just the header.
1362  // (Bitstream may be unaligned!)
1363  }
1364  }
1365  break;
1366 
1367  case H264_NAL_AUD:
1368  {
1369  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1370  if (err < 0)
1371  return err;
1372  }
1373  break;
1374 
1375  case H264_NAL_SEI:
1376  {
1377  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1378  if (err < 0)
1379  return err;
1380  }
1381  break;
1382 
1383  case H264_NAL_FILLER_DATA:
1384  {
1385  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1386  if (err < 0)
1387  return err;
1388  }
1389  break;
1390 
1391  case H264_NAL_END_SEQUENCE:
1392  {
1393  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1394  if (err < 0)
1395  return err;
1396  }
1397  break;
1398 
1399  case H264_NAL_END_STREAM:
1400  {
1401  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1402  if (err < 0)
1403  return err;
1404  }
1405  break;
1406 
1407  default:
1408  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1409  "NAL unit type %"PRIu32".\n", unit->type);
1410  return AVERROR_PATCHWELCOME;
1411  }
1412 
1413  return 0;
1414 }
1415 
1417  CodedBitstreamUnit *unit,
1418  PutBitContext *pbc)
1419 {
1420  int err;
1421 
1422  switch (unit->type) {
1423  case HEVC_NAL_VPS:
1424  {
1425  H265RawVPS *vps = unit->content;
1426 
1427  err = cbs_h265_write_vps(ctx, pbc, vps);
1428  if (err < 0)
1429  return err;
1430 
1431  err = cbs_h265_replace_vps(ctx, unit);
1432  if (err < 0)
1433  return err;
1434  }
1435  break;
1436 
1437  case HEVC_NAL_SPS:
1438  {
1439  H265RawSPS *sps = unit->content;
1440 
1441  err = cbs_h265_write_sps(ctx, pbc, sps);
1442  if (err < 0)
1443  return err;
1444 
1445  err = cbs_h265_replace_sps(ctx, unit);
1446  if (err < 0)
1447  return err;
1448  }
1449  break;
1450 
1451  case HEVC_NAL_PPS:
1452  {
1453  H265RawPPS *pps = unit->content;
1454 
1455  err = cbs_h265_write_pps(ctx, pbc, pps);
1456  if (err < 0)
1457  return err;
1458 
1459  err = cbs_h265_replace_pps(ctx, unit);
1460  if (err < 0)
1461  return err;
1462  }
1463  break;
1464 
1465  case HEVC_NAL_TRAIL_N:
1466  case HEVC_NAL_TRAIL_R:
1467  case HEVC_NAL_TSA_N:
1468  case HEVC_NAL_TSA_R:
1469  case HEVC_NAL_STSA_N:
1470  case HEVC_NAL_STSA_R:
1471  case HEVC_NAL_RADL_N:
1472  case HEVC_NAL_RADL_R:
1473  case HEVC_NAL_RASL_N:
1474  case HEVC_NAL_RASL_R:
1475  case HEVC_NAL_BLA_W_LP:
1476  case HEVC_NAL_BLA_W_RADL:
1477  case HEVC_NAL_BLA_N_LP:
1478  case HEVC_NAL_IDR_W_RADL:
1479  case HEVC_NAL_IDR_N_LP:
1480  case HEVC_NAL_CRA_NUT:
1481  {
1482  H265RawSlice *slice = unit->content;
1483 
1484  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1485  if (err < 0)
1486  return err;
1487 
1488  if (slice->data) {
1489  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1490  slice->data_size,
1491  slice->data_bit_start);
1492  if (err < 0)
1493  return err;
1494  } else {
1495  // No slice data - that was just the header.
1496  }
1497  }
1498  break;
1499 
1500  case HEVC_NAL_AUD:
1501  {
1502  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1503  if (err < 0)
1504  return err;
1505  }
1506  break;
1507 
1508  case HEVC_NAL_FD_NUT:
1509  {
1510  err = cbs_h265_write_filler(ctx, pbc, unit->content);
1511  if (err < 0)
1512  return err;
1513  }
1514  break;
1515 
1516  case HEVC_NAL_SEI_PREFIX:
1517  case HEVC_NAL_SEI_SUFFIX:
1518  {
1519  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1520  unit->type == HEVC_NAL_SEI_PREFIX);
1521 
1522  if (err < 0)
1523  return err;
1524  }
1525  break;
1526 
1527  default:
1528  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1529  "NAL unit type %"PRIu32".\n", unit->type);
1530  return AVERROR_PATCHWELCOME;
1531  }
1532 
1533  return 0;
1534 }
1535 
1537  const CodedBitstreamUnit *unit,
1538  enum AVDiscard skip)
1539 {
1541  H264RawSliceHeader *slice;
1542  int slice_type_i, slice_type_b, slice_type_si;
1543 
1544  if (skip <= AVDISCARD_DEFAULT)
1545  return 0;
1546 
1547  // keep non-VCL
1548  if (unit->type != H264_NAL_SLICE &&
1549  unit->type != H264_NAL_IDR_SLICE &&
1550  unit->type != H264_NAL_AUXILIARY_SLICE)
1551  return 0;
1552 
1553  if (skip >= AVDISCARD_ALL)
1554  return 1;
1555 
1556  if (skip >= AVDISCARD_NONKEY && unit->type != H264_NAL_IDR_SLICE)
1557  return 1;
1558 
1559  header = (H264RawNALUnitHeader *)unit->content;
1560  if (!header) {
1561  av_log(ctx->log_ctx, AV_LOG_WARNING,
1562  "h264 nal unit header is null, missing decompose?\n");
1563  return 0;
1564  }
1565 
1566  if (skip >= AVDISCARD_NONREF && !header->nal_ref_idc)
1567  return 1;
1568 
1569  slice = (H264RawSliceHeader *)unit->content;
1570  if (!slice) {
1571  av_log(ctx->log_ctx, AV_LOG_WARNING,
1572  "h264 slice header is null, missing decompose?\n");
1573  return 0;
1574  }
1575 
1576  slice_type_i = slice->slice_type % 5 == 2;
1577  slice_type_b = slice->slice_type % 5 == 1;
1578  slice_type_si = slice->slice_type % 5 == 4;
1579 
1580  if (skip >= AVDISCARD_BIDIR && slice_type_b)
1581  return 1;
1582  if (skip >= AVDISCARD_NONINTRA && !slice_type_i && !slice_type_si)
1583  return 1;
1584 
1585  return 0;
1586 }
1587 
1589  const CodedBitstreamUnit *unit,
1590  enum AVDiscard skip)
1591 {
1592  H265RawSliceHeader *slice;
1593 
1594  if (skip <= AVDISCARD_DEFAULT)
1595  return 0;
1596 
1597  switch (unit->type) {
1598  case HEVC_NAL_BLA_W_LP:
1599  case HEVC_NAL_BLA_W_RADL:
1600  case HEVC_NAL_BLA_N_LP:
1601  case HEVC_NAL_IDR_W_RADL:
1602  case HEVC_NAL_IDR_N_LP:
1603  case HEVC_NAL_CRA_NUT:
1604  // IRAP slice
1605  if (skip < AVDISCARD_ALL)
1606  return 0;
1607  break;
1608 
1609  case HEVC_NAL_TRAIL_R:
1610  case HEVC_NAL_TRAIL_N:
1611  case HEVC_NAL_TSA_N:
1612  case HEVC_NAL_TSA_R:
1613  case HEVC_NAL_STSA_N:
1614  case HEVC_NAL_STSA_R:
1615  case HEVC_NAL_RADL_N:
1616  case HEVC_NAL_RADL_R:
1617  case HEVC_NAL_RASL_N:
1618  case HEVC_NAL_RASL_R:
1619  // Slice
1620  break;
1621  default:
1622  // Don't discard non-slice nal.
1623  return 0;
1624  }
1625 
1626  if (skip >= AVDISCARD_NONKEY)
1627  return 1;
1628 
1629  slice = (H265RawSliceHeader *)unit->content;
1630  if (!slice) {
1631  av_log(ctx->log_ctx, AV_LOG_WARNING,
1632  "h265 slice header is null, missing decompose?\n");
1633  return 0;
1634  }
1635 
1636  if (skip >= AVDISCARD_NONINTRA && slice->slice_type != HEVC_SLICE_I)
1637  return 1;
1638  if (skip >= AVDISCARD_BIDIR && slice->slice_type == HEVC_SLICE_B)
1639  return 1;
1640 
1641  if (skip >= AVDISCARD_NONREF) {
1642  switch (unit->type) {
1643  case HEVC_NAL_TRAIL_N:
1644  case HEVC_NAL_TSA_N:
1645  case HEVC_NAL_STSA_N:
1646  case HEVC_NAL_RADL_N:
1647  case HEVC_NAL_RASL_N:
1648  case HEVC_NAL_VCL_N10:
1649  case HEVC_NAL_VCL_N12:
1650  case HEVC_NAL_VCL_N14:
1651  // non-ref
1652  return 1;
1653  default:
1654  break;
1655  }
1656  }
1657 
1658  return 0;
1659 }
1660 
1662  CodedBitstreamUnit *unit,
1663  PutBitContext *pbc)
1664 {
1665  int err;
1666 
1667  switch (unit->type) {
1668  case VVC_DCI_NUT:
1669  {
1670  H266RawDCI *dci = unit->content;
1671 
1672  err = cbs_h266_write_dci(ctx, pbc, dci);
1673  if (err < 0)
1674  return err;
1675  }
1676  break;
1677  case VVC_OPI_NUT:
1678  {
1679  H266RawOPI *opi = unit->content;
1680 
1681  err = cbs_h266_write_opi(ctx, pbc, opi);
1682  if (err < 0)
1683  return err;
1684  }
1685  break;
1686  case VVC_VPS_NUT:
1687  {
1688  H266RawVPS *vps = unit->content;
1689 
1690  err = cbs_h266_write_vps(ctx, pbc, vps);
1691  if (err < 0)
1692  return err;
1693 
1694  err = cbs_h266_replace_vps(ctx, unit);
1695  if (err < 0)
1696  return err;
1697  }
1698  break;
1699  case VVC_SPS_NUT:
1700  {
1701  H266RawSPS *sps = unit->content;
1702 
1703  err = cbs_h266_write_sps(ctx, pbc, sps);
1704  if (err < 0)
1705  return err;
1706 
1707  err = cbs_h266_replace_sps(ctx, unit);
1708  if (err < 0)
1709  return err;
1710  }
1711  break;
1712 
1713  case VVC_PPS_NUT:
1714  {
1715  H266RawPPS *pps = unit->content;
1716 
1717  err = cbs_h266_write_pps(ctx, pbc, pps);
1718  if (err < 0)
1719  return err;
1720 
1721  err = cbs_h266_replace_pps(ctx, unit);
1722  if (err < 0)
1723  return err;
1724  }
1725  break;
1726 
1727  case VVC_PREFIX_APS_NUT:
1728  case VVC_SUFFIX_APS_NUT:
1729  {
1730  err = cbs_h266_write_aps(ctx, pbc, unit->content,
1731  unit->type == VVC_PREFIX_APS_NUT);
1732  if (err < 0)
1733  return err;
1734  }
1735  break;
1736  case VVC_PH_NUT:
1737  {
1738  H266RawPH *ph = unit->content;
1739  err = cbs_h266_write_ph(ctx, pbc, ph);
1740  if (err < 0)
1741  return err;
1742 
1743  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1744  if (err < 0)
1745  return err;
1746  }
1747  break;
1748 
1749  case VVC_TRAIL_NUT:
1750  case VVC_STSA_NUT:
1751  case VVC_RADL_NUT:
1752  case VVC_RASL_NUT:
1753  case VVC_IDR_W_RADL:
1754  case VVC_IDR_N_LP:
1755  case VVC_CRA_NUT:
1756  case VVC_GDR_NUT:
1757  {
1758  H266RawSlice *slice = unit->content;
1759 
1760  err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1761  if (err < 0)
1762  return err;
1763 
1765  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1766  if (err < 0)
1767  return err;
1768  }
1769 
1770  if (slice->data) {
1771  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1772  slice->data_size,
1773  slice->data_bit_start);
1774  if (err < 0)
1775  return err;
1776  } else {
1777  // No slice data - that was just the header.
1778  }
1779  }
1780  break;
1781 
1782  case VVC_AUD_NUT:
1783  {
1784  err = cbs_h266_write_aud(ctx, pbc, unit->content);
1785  if (err < 0)
1786  return err;
1787  }
1788  break;
1789 
1790  case VVC_PREFIX_SEI_NUT:
1791  case VVC_SUFFIX_SEI_NUT:
1792  {
1793  err = cbs_h266_write_sei(ctx, pbc, unit->content,
1794  unit->type == VVC_PREFIX_SEI_NUT);
1795 
1796  if (err < 0)
1797  return err;
1798  }
1799  break;
1800 
1801  default:
1802  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1803  "NAL unit type %"PRIu32".\n", unit->type);
1804  return AVERROR_PATCHWELCOME;
1805  }
1806 
1807  return 0;
1808 }
1809 
1812  int nal_unit_index)
1813 {
1814  // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1815  if (nal_unit_index == 0) {
1816  // Assume that this is the first NAL unit in an access unit.
1817  return 1;
1818  }
1819  if (codec_id == AV_CODEC_ID_H264)
1820  return type == H264_NAL_SPS || type == H264_NAL_PPS;
1821  if (codec_id == AV_CODEC_ID_HEVC)
1822  return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1823  if (codec_id == AV_CODEC_ID_VVC)
1824  return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
1825  return 0;
1826 }
1827 
1829  CodedBitstreamFragment *frag)
1830 {
1831  uint8_t *data;
1832  size_t max_size, dp, sp;
1833  int err, i, zero_run;
1834 
1835  for (i = 0; i < frag->nb_units; i++) {
1836  // Data should already all have been written when we get here.
1837  av_assert0(frag->units[i].data);
1838  }
1839 
1840  max_size = 0;
1841  for (i = 0; i < frag->nb_units; i++) {
1842  // Start code + content with worst-case emulation prevention.
1843  max_size += 4 + frag->units[i].data_size * 3 / 2;
1844  }
1845 
1847  if (!data)
1848  return AVERROR(ENOMEM);
1849 
1850  dp = 0;
1851  for (i = 0; i < frag->nb_units; i++) {
1852  CodedBitstreamUnit *unit = &frag->units[i];
1853 
1854  if (unit->data_bit_padding > 0) {
1855  if (i < frag->nb_units - 1)
1856  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1857  "unaligned padding on non-final NAL unit.\n");
1858  else
1859  frag->data_bit_padding = unit->data_bit_padding;
1860  }
1861 
1862  if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1863  // zero_byte
1864  data[dp++] = 0;
1865  }
1866  // start_code_prefix_one_3bytes
1867  data[dp++] = 0;
1868  data[dp++] = 0;
1869  data[dp++] = 1;
1870 
1871  zero_run = 0;
1872  for (sp = 0; sp < unit->data_size; sp++) {
1873  if (zero_run < 2) {
1874  if (unit->data[sp] == 0)
1875  ++zero_run;
1876  else
1877  zero_run = 0;
1878  } else {
1879  if ((unit->data[sp] & ~3) == 0) {
1880  // emulation_prevention_three_byte
1881  data[dp++] = 3;
1882  }
1883  zero_run = unit->data[sp] == 0;
1884  }
1885  data[dp++] = unit->data[sp];
1886  }
1887  }
1888 
1889  av_assert0(dp <= max_size);
1891  if (err)
1892  return err;
1893  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1894 
1896  NULL, NULL, 0);
1897  if (!frag->data_ref) {
1898  av_freep(&data);
1899  return AVERROR(ENOMEM);
1900  }
1901 
1902  frag->data = data;
1903  frag->data_size = dp;
1904 
1905  return 0;
1906 }
1907 
1909 {
1911 
1912  for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1913  ff_refstruct_unref(&h264->sps[i]);
1914  for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1915  ff_refstruct_unref(&h264->pps[i]);
1916 
1917  h264->active_sps = NULL;
1918  h264->active_pps = NULL;
1919  h264->last_slice_nal_unit_type = 0;
1920 }
1921 
1923 {
1925  int i;
1926 
1928 
1929  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1930  ff_refstruct_unref(&h264->sps[i]);
1931  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1932  ff_refstruct_unref(&h264->pps[i]);
1933 }
1934 
1936 {
1938 
1939  for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1940  ff_refstruct_unref(&h265->vps[i]);
1941  for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1942  ff_refstruct_unref(&h265->sps[i]);
1943  for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1944  ff_refstruct_unref(&h265->pps[i]);
1945 
1946  h265->active_vps = NULL;
1947  h265->active_sps = NULL;
1948  h265->active_pps = NULL;
1949 }
1950 
1952 {
1954  int i;
1955 
1957 
1958  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1959  ff_refstruct_unref(&h265->vps[i]);
1960  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1961  ff_refstruct_unref(&h265->sps[i]);
1962  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1963  ff_refstruct_unref(&h265->pps[i]);
1964 }
1965 
1967 {
1969 
1970  for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1971  ff_refstruct_unref(&h266->vps[i]);
1972  for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1973  ff_refstruct_unref(&h266->sps[i]);
1974  for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1975  ff_refstruct_unref(&h266->pps[i]);
1976  ff_refstruct_unref(&h266->ph_ref);
1977 }
1978 
1980 {
1982 
1985  }
1986 
1987 static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
1988 {
1989  H264RawSEI *sei = content;
1990  ff_cbs_sei_free_message_list(&sei->message_list);
1991 }
1992 
1996 
1998 
2002 
2007 
2009 
2011 };
2012 
2013 static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
2014 {
2015  H265RawSEI *sei = content;
2016  ff_cbs_sei_free_message_list(&sei->message_list);
2017 }
2018 
2023 
2026 
2027  // Slices of non-IRAP pictures.
2029  H265RawSlice, data),
2030  // Slices of IRAP pictures.
2032  H265RawSlice, data),
2033 
2036 
2038 };
2039 
2040 static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
2041 {
2042  H266RawSEI *sei = content;
2043  ff_cbs_sei_free_message_list(&sei->message_list);
2044 }
2045 
2050  {
2051  .nb_unit_types = 1,
2052  .unit_type.list[0] = VVC_SPS_NUT,
2053  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2054  .content_size = sizeof(H266RawSPS),
2055  .type.ref = {
2056  .nb_offsets = 2,
2057  .offsets = { offsetof(H266RawSPS, extension_data.data),
2058  offsetof(H266RawSPS, vui.extension_data.data) }
2059  },
2060  },
2064 
2067 
2069  H266RawSlice, data),
2070 
2072  H266RawSlice, data),
2073 
2076 
2078 };
2079 
2082 
2083  .priv_data_size = sizeof(CodedBitstreamH264Context),
2084 
2085  .unit_types = cbs_h264_unit_types,
2086 
2087  .split_fragment = &cbs_h2645_split_fragment,
2088  .read_unit = &cbs_h264_read_nal_unit,
2089  .write_unit = &cbs_h264_write_nal_unit,
2090  .discarded_unit = &cbs_h264_discarded_nal_unit,
2091  .assemble_fragment = &cbs_h2645_assemble_fragment,
2092 
2093  .flush = &cbs_h264_flush,
2094  .close = &cbs_h264_close,
2095 };
2096 
2099 
2100  .priv_data_size = sizeof(CodedBitstreamH265Context),
2101 
2102  .unit_types = cbs_h265_unit_types,
2103 
2104  .split_fragment = &cbs_h2645_split_fragment,
2105  .read_unit = &cbs_h265_read_nal_unit,
2106  .write_unit = &cbs_h265_write_nal_unit,
2107  .discarded_unit = &cbs_h265_discarded_nal_unit,
2108  .assemble_fragment = &cbs_h2645_assemble_fragment,
2109 
2110  .flush = &cbs_h265_flush,
2111  .close = &cbs_h265_close,
2112 };
2113 
2116 
2117  .priv_data_size = sizeof(CodedBitstreamH266Context),
2118 
2119  .unit_types = cbs_h266_unit_types,
2120 
2121  .split_fragment = &cbs_h2645_split_fragment,
2122  .read_unit = &cbs_h266_read_nal_unit,
2123  .write_unit = &cbs_h266_write_nal_unit,
2124  .assemble_fragment = &cbs_h2645_assemble_fragment,
2125 
2126  .flush = &cbs_h266_flush,
2127  .close = &cbs_h266_close,
2128 };
2129 
2130 // Macro for the read/write pair.
2131 #define SEI_MESSAGE_RW(codec, name) \
2132  .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2133  .write = cbs_ ## codec ## _write_ ## name ## _internal
2134 
2136  {
2138  1, 1,
2139  sizeof(SEIRawFillerPayload),
2140  SEI_MESSAGE_RW(sei, filler_payload),
2141  },
2142  {
2144  1, 1,
2145  sizeof(SEIRawUserDataRegistered),
2146  SEI_MESSAGE_RW(sei, user_data_registered),
2147  },
2148  {
2150  1, 1,
2152  SEI_MESSAGE_RW(sei, user_data_unregistered),
2153  },
2154  {
2156  1, 0,
2158  SEI_MESSAGE_RW(sei, frame_packing_arrangement),
2159  },
2160  {
2162  0, 1,
2163  sizeof(SEIRawDecodedPictureHash),
2164  SEI_MESSAGE_RW(sei, decoded_picture_hash),
2165  },
2166  {
2168  1, 0,
2170  SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2171  },
2172  {
2174  1, 0,
2176  SEI_MESSAGE_RW(sei, content_light_level_info),
2177  },
2178  {
2180  1, 0,
2182  SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2183  },
2184  {
2186  1, 0,
2188  SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2189  },
2191 };
2192 
2194  {
2196  1, 0,
2197  sizeof(H264RawSEIBufferingPeriod),
2198  SEI_MESSAGE_RW(h264, sei_buffering_period),
2199  },
2200  {
2202  1, 0,
2203  sizeof(H264RawSEIPicTiming),
2204  SEI_MESSAGE_RW(h264, sei_pic_timing),
2205  },
2206  {
2208  1, 0,
2209  sizeof(H264RawSEIPanScanRect),
2210  SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2211  },
2212  {
2214  1, 0,
2215  sizeof(H264RawSEIRecoveryPoint),
2216  SEI_MESSAGE_RW(h264, sei_recovery_point),
2217  },
2218  {
2220  1, 0,
2222  SEI_MESSAGE_RW(h264, film_grain_characteristics),
2223  },
2224  {
2226  1, 0,
2228  SEI_MESSAGE_RW(h264, sei_frame_packing_arrangement),
2229  },
2230  {
2232  1, 0,
2234  SEI_MESSAGE_RW(h264, sei_display_orientation),
2235  },
2237 };
2238 
2240  {
2242  1, 0,
2243  sizeof(H265RawSEIBufferingPeriod),
2244  SEI_MESSAGE_RW(h265, sei_buffering_period),
2245  },
2246  {
2248  1, 0,
2249  sizeof(H265RawSEIPicTiming),
2250  SEI_MESSAGE_RW(h265, sei_pic_timing),
2251  },
2252  {
2254  1, 0,
2255  sizeof(H265RawSEIPanScanRect),
2256  SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2257  },
2258  {
2260  1, 0,
2261  sizeof(H265RawSEIRecoveryPoint),
2262  SEI_MESSAGE_RW(h265, sei_recovery_point),
2263  },
2264  {
2266  1, 0,
2268  SEI_MESSAGE_RW(h265, film_grain_characteristics),
2269  },
2270  {
2272  1, 0,
2274  SEI_MESSAGE_RW(h265, sei_display_orientation),
2275  },
2276  {
2278  1, 0,
2280  SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2281  },
2282  {
2284  0, 1,
2286  SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2287  },
2288  {
2290  1, 0,
2291  sizeof(H265RawSEITimeCode),
2292  SEI_MESSAGE_RW(h265, sei_time_code),
2293  },
2294  {
2296  1, 0,
2298  SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2299  },
2300  {
2302  1, 0,
2304  SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
2305  },
2307 };
2308 
2311 };
2312 
2314  int payload_type)
2315 {
2317  int i;
2318 
2319  switch (ctx->codec->codec_id) {
2320  case AV_CODEC_ID_H264:
2322  break;
2323  case AV_CODEC_ID_H265:
2325  break;
2326  case AV_CODEC_ID_H266:
2328  break;
2329  default:
2330  return NULL;
2331  }
2332 
2333  for (i = 0; codec_list[i].type >= 0; i++) {
2334  if (codec_list[i].type == payload_type)
2335  return &codec_list[i];
2336  }
2337 
2338  for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2339  if (cbs_sei_common_types[i].type == payload_type)
2340  return &cbs_sei_common_types[i];
2341  }
2342 
2343  return NULL;
2344 }
VVC_RADL_NUT
@ VVC_RADL_NUT
Definition: vvc.h:31
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
VVC_RASL_NUT
@ VVC_RASL_NUT
Definition: vvc.h:32
SEI_TYPE_ALPHA_CHANNEL_INFO
@ SEI_TYPE_ALPHA_CHANNEL_INFO
Definition: sei.h:123
VVC_GDR_NUT
@ VVC_GDR_NUT
Definition: vvc.h:39
VVC_STSA_NUT
@ VVC_STSA_NUT
Definition: vvc.h:30
CodedBitstreamH265Context::vps
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:756
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
H265RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h265.h:589
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:112
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
CBS_UNIT_TYPE_COMPLEX
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
Definition: cbs_internal.h:332
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:495
h2645_parse.h
cbs_h266.h
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
H2645_FLAG_IS_NALFF
@ H2645_FLAG_IS_NALFF
Definition: h2645_parse.h:97
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:291
cbs_h264_syntax_template.c
H265RawSEITimeCode
Definition: cbs_h265.h:693
SEIRawUserDataRegistered
Definition: cbs_sei.h:33
H266RawDCI
Definition: cbs_h266.h:252
H2645_FLAG_USE_REF
@ H2645_FLAG_USE_REF
Definition: h2645_parse.h:99
ff_ctz
#define ff_ctz
Definition: intmath.h:107
SEIRawAmbientViewingEnvironment
Definition: cbs_sei.h:94
GetByteContext
Definition: bytestream.h:33
CodedBitstreamH264Context::common
CodedBitstreamH2645Context common
Definition: cbs_h264.h:426
cbs_h265_syntax_template.c
VVC_DCI_NUT
@ VVC_DCI_NUT
Definition: vvc.h:42
cbs_h266_replace_ps
#define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element)
H265RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h265.h:587
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
cbs_h264.h
cbs_h266_replace_ph
static int cbs_h266_replace_ph(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, H266RawPictureHeader *ph)
Definition: cbs_h2645.c:820
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:107
H265RawSEIActiveParameterSets
Definition: cbs_h265.h:677
H265RawSlice::header
H265RawSliceHeader header
Definition: cbs_h265.h:584
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
SEI_TYPE_PAN_SCAN_RECT
@ SEI_TYPE_PAN_SCAN_RECT
Definition: sei.h:32
CodedBitstreamH264Context::pps
H264RawPPS * pps[H264_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h264.h:431
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3042
H2645NAL::nuh_layer_id
int nuh_layer_id
Definition: h2645_parse.h:67
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
H265RawSEI
Definition: cbs_h265.h:739
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
cbs_h266_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[]
Definition: cbs_h2645.c:2046
HEVC_NAL_BLA_N_LP
@ HEVC_NAL_BLA_N_LP
Definition: hevc.h:47
CodedBitstreamH265Context::sps
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:757
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:219
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:600
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:149
H265RawSEIPanScanRect
Definition: cbs_h265.h:630
HEVC_NAL_TSA_N
@ HEVC_NAL_TSA_N
Definition: hevc.h:31
HEVC_NAL_IDR_N_LP
@ HEVC_NAL_IDR_N_LP
Definition: hevc.h:49
SEIRawAlternativeTransferCharacteristics
Definition: cbs_sei.h:90
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
cbs.h
H265RawSEIDecodedPictureHash
Definition: cbs_h265.h:686
H264_NAL_SEI
@ H264_NAL_SEI
Definition: h264.h:40
cbs_h2645_split_fragment
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:526
cbs_h265.h
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
H266RawPPS::pps_seq_parameter_set_id
uint8_t pps_seq_parameter_set_id
Definition: cbs_h266.h:500
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
HEVC_NAL_IDR_W_RADL
@ HEVC_NAL_IDR_W_RADL
Definition: hevc.h:48
H264_NAL_END_SEQUENCE
@ H264_NAL_END_SEQUENCE
Definition: h264.h:44
CodedBitstreamH265Context::common
CodedBitstreamH2645Context common
Definition: cbs_h265.h:752
VVC_AUD_NUT
@ VVC_AUD_NUT
Definition: vvc.h:49
SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO
@ SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO
Definition: sei.h:127
H2645Packet::nb_nals
int nb_nals
Definition: h2645_parse.h:85
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
cbs_h2645_unit_requires_zero_byte
static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id, CodedBitstreamUnitType type, int nal_unit_index)
Definition: cbs_h2645.c:1810
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
H265RawSEI3DReferenceDisplaysInfo
Definition: cbs_h265.h:723
VVC_MAX_PPS_COUNT
@ VVC_MAX_PPS_COUNT
Definition: vvc.h:99
H265RawSPS
Definition: cbs_h265.h:245
H265RawVPS
Definition: cbs_h265.h:184
H264_NAL_SPS_EXT
@ H264_NAL_SPS_EXT
Definition: h264.h:47
H265RawPPS
Definition: cbs_h265.h:356
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
SEIRawContentLightLevelInfo
Definition: cbs_sei.h:85
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
CodedBitstreamH266Context::pps
H266RawPPS * pps[VVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:866
cbs_h265_flush
static void cbs_h265_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1935
CodedBitstreamH266Context::vps
H266RawVPS * vps[VVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:864
H264RawSEIPicTiming
Definition: cbs_h264.h:249
HEVC_NAL_VCL_N14
@ HEVC_NAL_VCL_N14
Definition: hevc.h:43
H266RawAUD
Definition: cbs_h266.h:646
cbs_sei_h265_types
static const SEIMessageTypeDescriptor cbs_sei_h265_types[]
Definition: cbs_h2645.c:2239
CodedBitstreamH264Context::active_sps
const H264RawSPS * active_sps
Definition: cbs_h264.h:436
cbs_h2645_fragment_add_nals
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition: cbs_h2645.c:490
CBS_UNIT_TYPE_INTERNAL_REF
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
Definition: cbs_internal.h:312
cbs_h264_flush
static void cbs_h264_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1908
H265RawSEIPicTiming
Definition: cbs_h265.h:614
GetBitContext
Definition: get_bits.h:108
H266RawAPS
Definition: cbs_h266.h:600
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_INTERNAL_REFS
Definition: cbs_internal.h:38
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:125
SEIRawUserDataUnregistered
Definition: cbs_sei.h:40
H264_NAL_END_STREAM
@ H264_NAL_END_STREAM
Definition: h264.h:45
H266RawSliceHeader::sh_picture_header_in_slice_header_flag
uint8_t sh_picture_header_in_slice_header_flag
Definition: cbs_h266.h:773
H2645Packet::rbsp
H2645RBSP rbsp
Definition: h2645_parse.h:84
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT
@ SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT
Definition: sei.h:107
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:81
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
VVC_IDR_W_RADL
@ VVC_IDR_W_RADL
Definition: vvc.h:36
refstruct.h
SEI_TYPE_FILLER_PAYLOAD
@ SEI_TYPE_FILLER_PAYLOAD
Definition: sei.h:33
cbs_sei_common_types
static const SEIMessageTypeDescriptor cbs_sei_common_types[]
Definition: cbs_h2645.c:2135
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
avassert.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:56
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
HEVC_SLICE_B
@ HEVC_SLICE_B
Definition: hevc.h:96
CodedBitstreamH2645Context
Definition: cbs_h2645.h:25
H266RawSlice::data
uint8_t * data
Definition: cbs_h266.h:846
extension_data
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawExtensionData *current)
Definition: cbs_h265_syntax_template.c:61
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
CodedBitstreamH2645Context::nal_length_size
int nal_length_size
Definition: cbs_h2645.h:30
H266RawSEI
Definition: cbs_h266.h:853
H2645NAL::size
int size
Definition: h2645_parse.h:36
cbs_read_ue_golomb
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:36
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
SEIRawFillerPayload
Definition: cbs_sei.h:29
HEVC_NAL_VCL_N10
@ HEVC_NAL_VCL_N10
Definition: hevc.h:39
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:135
SEI_TYPE_FRAME_PACKING_ARRANGEMENT
@ SEI_TYPE_FRAME_PACKING_ARRANGEMENT
Definition: sei.h:75
cbs_sei_h266_types
static const SEIMessageTypeDescriptor cbs_sei_h266_types[]
Definition: cbs_h2645.c:2309
cbs_h265_free_sei
static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:2013
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SEI_MESSAGE_TYPE_END
#define SEI_MESSAGE_TYPE_END
Definition: cbs_sei.h:160
SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
Definition: sei.h:34
ff_cbs_append_unit_data
int ff_cbs_append_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Add a new unit to a fragment with the given data bitstream.
Definition: cbs.c:850
hevc.h
H2645NAL::data
const uint8_t * data
Definition: h2645_parse.h:35
CodedBitstreamH2645Context::mp4
int mp4
Definition: cbs_h2645.h:28
H265RawSEIDisplayOrientation
Definition: cbs_h265.h:668
cbs_internal.h
cbs_h2645_replace_ps
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
Definition: cbs_h2645.c:759
H264RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h264.h:412
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
CodedBitstreamFragment::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:139
CodedBitstreamType::codec_id
enum AVCodecID codec_id
Definition: cbs_internal.h:103
MAX_UINT_BITS
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:196
AVDISCARD_BIDIR
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition: defs.h:218
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
H2645NAL::type
int type
NAL unit type.
Definition: h2645_parse.h:52
PutBitContext
Definition: put_bits.h:50
CBS_TRACE_WRITE_END
#define CBS_TRACE_WRITE_END()
Definition: cbs_internal.h:254
H265RawSEIRecoveryPoint
Definition: cbs_h265.h:641
if
if(ret)
Definition: filter_design.txt:179
cbs_h265_payload_extension_present
static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size, int cur_pos)
Definition: cbs_h2645.c:214
cbs_sei_syntax_template.c
H266RawSPS
Definition: cbs_h266.h:308
HEVC_NAL_VCL_N12
@ HEVC_NAL_VCL_N12
Definition: hevc.h:41
H266RawVPS
Definition: cbs_h266.h:262
H266RawPPS
Definition: cbs_h266.h:496
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:221
cbs_h266_read_nal_unit
static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1084
H266RawOPI
Definition: cbs_h266.h:241
H266RawSPS::extension_data
H266RawExtensionData extension_data
Definition: cbs_h266.h:492
NULL
#define NULL
Definition: coverity.c:32
H266RawPictureHeader
Definition: cbs_h266.h:676
bits_left
#define bits_left
Definition: bitstream.h:114
H265RawAUD
Definition: cbs_h265.h:487
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
SEI_TYPE_TIME_CODE
@ SEI_TYPE_TIME_CODE
Definition: sei.h:95
AV_CODEC_ID_H266
#define AV_CODEC_ID_H266
Definition: codec_id.h:253
cbs_h265_write_nal_unit
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1416
SPS
Sequence parameter set.
Definition: h264_ps.h:44
SEIMessageTypeDescriptor
Definition: cbs_sei.h:144
HEVC_SLICE_I
@ HEVC_SLICE_I
Definition: hevc.h:98
SEIRawMasteringDisplayColourVolume
Definition: cbs_sei.h:76
VVC_PREFIX_SEI_NUT
@ VVC_PREFIX_SEI_NUT
Definition: vvc.h:52
H264RawNALUnitHeader
Definition: cbs_h264.h:31
cbs_h266_close
static void cbs_h266_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1979
HEVC_NAL_STSA_N
@ HEVC_NAL_STSA_N
Definition: hevc.h:33
CodedBitstreamUnit::data_size
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:86
PPS
Picture parameter set.
Definition: h264_ps.h:110
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:370
opi
static int FUNC() opi(CodedBitstreamContext *ctx, RWContext *rw, H266RawOPI *current)
Definition: cbs_h266_syntax_template.c:643
H264RawSEIPanScanRect
Definition: cbs_h264.h:257
H264_NAL_AUXILIARY_SLICE
@ H264_NAL_AUXILIARY_SLICE
Definition: h264.h:53
CodedBitstreamH266Context::common
CodedBitstreamH2645Context common
Definition: cbs_h266.h:860
CodedBitstreamH2645Context::read_packet
H2645Packet read_packet
Definition: cbs_h2645.h:32
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
CBS_UNIT_RANGE_INTERNAL_REF
#define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field)
Definition: cbs_internal.h:315
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
VVC_PH_NUT
@ VVC_PH_NUT
Definition: vvc.h:48
H265RawSliceHeader::slice_type
uint8_t slice_type
Definition: cbs_h265.h:504
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:858
cbs_h264_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[]
Definition: cbs_h2645.c:1993
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
CodedBitstreamH264Context
Definition: cbs_h264.h:424
HEVC_NAL_RASL_R
@ HEVC_NAL_RASL_R
Definition: hevc.h:38
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:220
H264_NAL_AUD
@ H264_NAL_AUD
Definition: h264.h:43
H266RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h266.h:850
AVDISCARD_DEFAULT
@ AVDISCARD_DEFAULT
discard useless packets like 0 size packets in avi
Definition: defs.h:216
H266RawSliceHeader::sh_picture_header
H266RawPictureHeader sh_picture_header
Definition: cbs_h266.h:774
H264RawFilmGrainCharacteristics
Definition: cbs_h264.h:275
H264RawSliceHeader::slice_type
uint8_t slice_type
Definition: cbs_h264.h:334
VVC_VPS_NUT
@ VVC_VPS_NUT
Definition: vvc.h:43
HEVC_NAL_RASL_N
@ HEVC_NAL_RASL_N
Definition: hevc.h:37
codec_list
const FFCodec * codec_list[]
H266RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h266.h:847
CodedBitstreamH266Context::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:867
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
size
int size
Definition: twinvq_data.h:10344
SEIMessageTypeDescriptor::type
int type
Definition: cbs_sei.h:146
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:128
H264_NAL_FILLER_DATA
@ H264_NAL_FILLER_DATA
Definition: h264.h:46
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:188
CodedBitstreamUnit::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:92
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
H2645NAL
Definition: h2645_parse.h:34
H264RawSEIDisplayOrientation
Definition: cbs_h264.h:316
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
header
static const uint8_t header[24]
Definition: sdr2.c:68
HEVC_NAL_STSA_R
@ HEVC_NAL_STSA_R
Definition: hevc.h:34
SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
@ SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
Definition: sei.h:96
CBS_UNIT_TYPE_POD
#define CBS_UNIT_TYPE_POD(type_, structure)
Definition: cbs_internal.h:288
CodedBitstreamH266Context
Definition: cbs_h266.h:858
AV_CODEC_ID_VVC
@ AV_CODEC_ID_VVC
Definition: codec_id.h:252
VVC_TRAIL_NUT
@ VVC_TRAIL_NUT
Definition: vvc.h:29
CodedBitstreamType
Definition: cbs_internal.h:102
SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS
@ SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS
Definition: sei.h:106
attributes.h
cbs_h264_discarded_nal_unit
static int cbs_h264_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
Definition: cbs_h2645.c:1536
version
version
Definition: libkvazaar.c:321
H265RawFilmGrainCharacteristics
Definition: cbs_h265.h:647
VVC_SPS_NUT
@ VVC_SPS_NUT
Definition: vvc.h:44
vvc.h
cbs_h265_close
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1951
CodedBitstreamH264Context::active_pps
const H264RawPPS * active_pps
Definition: cbs_h264.h:437
H264RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h264.h:414
H264RawSEIFramePackingArrangement
Definition: cbs_h264.h:296
H264RawSliceHeader
Definition: cbs_h264.h:330
H2645Packet::nals
H2645NAL * nals
Definition: h2645_parse.h:83
H265RawSliceHeader
Definition: cbs_h265.h:493
ff_cbs_sei_find_type
const SEIMessageTypeDescriptor * ff_cbs_sei_find_type(CodedBitstreamContext *ctx, int payload_type)
Find the type descriptor for the given payload type.
Definition: cbs_h2645.c:2313
H264RawSlice::header
H264RawSliceHeader header
Definition: cbs_h264.h:409
HEVC_NAL_TSA_R
@ HEVC_NAL_TSA_R
Definition: hevc.h:32
CodedBitstreamH264Context::last_slice_nal_unit_type
uint8_t last_slice_nal_unit_type
Definition: cbs_h264.h:442
AVDISCARD_NONINTRA
@ AVDISCARD_NONINTRA
discard all non intra frames
Definition: defs.h:219
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:98
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:80
CBS_UNIT_TYPES_COMPLEX
#define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func)
Definition: cbs_internal.h:325
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:371
cbs_h266_flush
static void cbs_h266_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1966
H265RawSlice::data_size
size_t data_size
Definition: cbs_h265.h:588
CodedBitstreamH266Context::sps
H266RawSPS * sps[VVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:865
VVC_SUFFIX_SEI_NUT
@ VVC_SUFFIX_SEI_NUT
Definition: vvc.h:53
SEI_TYPE_PIC_TIMING
@ SEI_TYPE_PIC_TIMING
Definition: sei.h:31
H266RawSlice::header_size
size_t header_size
Definition: cbs_h266.h:848
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
VVC_IDR_N_LP
@ VVC_IDR_N_LP
Definition: vvc.h:37
H266RawSlice::data_size
size_t data_size
Definition: cbs_h266.h:849
SEI_TYPE_DISPLAY_ORIENTATION
@ SEI_TYPE_DISPLAY_ORIENTATION
Definition: sei.h:77
H264RawSlice::data
uint8_t * data
Definition: cbs_h264.h:411
H2645RBSP::rbsp_buffer_ref
AVBufferRef * rbsp_buffer_ref
Definition: h2645_parse.h:76
cbs_write_se_golomb
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, const int *subscripts, int32_t value, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:172
len
int len
Definition: vorbis_enc_data.h:426
ff_cbs_make_unit_refcounted
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition: cbs.c:1035
SEI_MESSAGE_RW
#define SEI_MESSAGE_RW(codec, name)
Definition: cbs_h2645.c:2131
CodedBitstreamH265Context::active_sps
const H265RawSPS * active_sps
Definition: cbs_h265.h:764
CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_END_OF_LIST
Definition: cbs_internal.h:335
cbs_read_se_golomb
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:85
H2645NAL::raw_data
const uint8_t * raw_data
Definition: h2645_parse.h:45
H264RawSEI
Definition: cbs_h264.h:325
cbs_h264_close
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1922
cbs_h264_free_sei
static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:1987
CodedBitstreamH265Context::pps
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:758
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
flags
#define flags(name, subs,...)
Definition: cbs_h2645.c:267
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
H266RawSlice::header
H266RawSliceHeader header
Definition: cbs_h266.h:844
pos
unsigned int pos
Definition: spdifenc.c:414
VVC_PPS_NUT
@ VVC_PPS_NUT
Definition: vvc.h:45
ff_cbs_alloc_unit_content
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:923
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
CodedBitstreamH265Context::active_pps
const H265RawPPS * active_pps
Definition: cbs_h265.h:765
H264RawAUD
Definition: cbs_h264.h:218
VVC_PREFIX_APS_NUT
@ VVC_PREFIX_APS_NUT
Definition: vvc.h:46
HEVC_NAL_CRA_NUT
@ HEVC_NAL_CRA_NUT
Definition: hevc.h:50
H266RawPH
Definition: cbs_h266.h:766
put_bits_ptr
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:377
SEI_TYPE_BUFFERING_PERIOD
@ SEI_TYPE_BUFFERING_PERIOD
Definition: sei.h:30
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:47
SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
VVC_SUFFIX_APS_NUT
@ VVC_SUFFIX_APS_NUT
Definition: vvc.h:47
SEIRawDecodedPictureHash
Definition: cbs_sei.h:66
skip_put_bytes
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:386
AV_CODEC_ID_H265
#define AV_CODEC_ID_H265
Definition: codec_id.h:229
CodedBitstreamH265Context::active_vps
const H265RawVPS * active_vps
Definition: cbs_h265.h:763
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
temp
else temp
Definition: vf_mcdeint.c:263
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
VVC_CRA_NUT
@ VVC_CRA_NUT
Definition: vvc.h:38
cbs_h266_free_sei
static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:2040
cbs_write_ue_golomb
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:140
H266RawExtensionData::data
uint8_t * data
Definition: cbs_h266.h:149
SEIRawFramePackingArrangement
Definition: cbs_sei.h:46
SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_RECOVERY_POINT
Definition: sei.h:36
dci
static int FUNC() dci(CodedBitstreamContext *ctx, RWContext *rw, H266RawDCI *current)
Definition: cbs_h266_syntax_template.c:670
VVC_OPI_NUT
@ VVC_OPI_NUT
Definition: vvc.h:41
SEI_TYPE_DECODED_PICTURE_HASH
@ SEI_TYPE_DECODED_PICTURE_HASH
Definition: sei.h:91
cbs_h265_discarded_nal_unit
static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
Definition: cbs_h2645.c:1588
H264RawSEIRecoveryPoint
Definition: cbs_h264.h:268
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
HEVC_NAL_TRAIL_R
@ HEVC_NAL_TRAIL_R
Definition: hevc.h:30
HEVC_NAL_RADL_R
@ HEVC_NAL_RADL_R
Definition: hevc.h:36
cbs_h266_syntax_template.c
ff_cbs_type_h266
const CodedBitstreamType ff_cbs_type_h266
Definition: cbs_h2645.c:2114
cbs_h265_read_nal_unit
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:955
H265RawSlice::data
uint8_t * data
Definition: cbs_h265.h:586
CBS_UNIT_TYPES_INTERNAL_REF
#define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field)
Definition: cbs_internal.h:304
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
cbs_h2645_write_slice_data
static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx, PutBitContext *pbc, const uint8_t *data, size_t data_size, int data_bit_start)
Definition: cbs_h2645.c:1241
cbs_h265_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[]
Definition: cbs_h2645.c:2019
CodedBitstreamH266Context::ph_ref
void * ph_ref
RefStruct reference backing ph above.
Definition: cbs_h266.h:868
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
h264.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
H264RawFiller
Definition: cbs_h264.h:417
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_cbs_type_h264
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:2080
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
@ SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
Definition: sei.h:103
CodedBitstreamFragment::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:145
ff_h2645_packet_split
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int nal_length_size, enum AVCodecID codec_id, int flags)
Split an input packet into NAL units.
Definition: h2645_parse.c:465
cbs_h264_read_nal_unit
static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:836
H264RawSPSExtension
Definition: cbs_h264.h:157
H264RawSEIBufferingPeriod
Definition: cbs_h264.h:224
ff_cbs_sei_free_message_list
void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
Free all SEI messages in a message list.
Definition: cbs_sei.c:91
AVDiscard
AVDiscard
Definition: defs.h:212
AVDISCARD_NONREF
@ AVDISCARD_NONREF
discard all non reference
Definition: defs.h:217
HEVC_NAL_RADL_N
@ HEVC_NAL_RADL_N
Definition: hevc.h:35
CodedBitstreamH264Context::sps
H264RawSPS * sps[H264_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h264.h:430
SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
@ SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
Definition: sei.h:49
ff_cbs_type_h265
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:2097
CBS_TRACE_READ_START
#define CBS_TRACE_READ_START()
Definition: cbs_internal.h:208
H2645Packet
Definition: h2645_parse.h:82
SEI_TYPE_ACTIVE_PARAMETER_SETS
@ SEI_TYPE_ACTIVE_PARAMETER_SETS
Definition: sei.h:87
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
cbs_h264_write_nal_unit
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1299
H264RawSlice::data_size
size_t data_size
Definition: cbs_h264.h:413
H265RawSEIBufferingPeriod
Definition: cbs_h265.h:593
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
cbs_sei_h264_types
static const SEIMessageTypeDescriptor cbs_sei_h264_types[]
Definition: cbs_h2645.c:2193
H265RawSEIAlphaChannelInfo
Definition: cbs_h265.h:712
cbs_h2645_assemble_fragment
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:1828
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153
HEVC_NAL_FD_NUT
@ HEVC_NAL_FD_NUT
Definition: hevc.h:67
HEVC_NAL_BLA_W_RADL
@ HEVC_NAL_BLA_W_RADL
Definition: hevc.h:46
cbs_h2645_read_more_rbsp_data
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:328
CodedBitstreamH265Context
Definition: cbs_h265.h:750
H266RawSlice
Definition: cbs_h266.h:843
HEVC_NAL_TRAIL_N
@ HEVC_NAL_TRAIL_N
Definition: hevc.h:29
H265RawSlice
Definition: cbs_h265.h:583
H264RawSlice
Definition: cbs_h264.h:408
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155
cbs_h266_write_nal_unit
static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1661
CBS_TRACE_READ_END
#define CBS_TRACE_READ_END()
Definition: cbs_internal.h:216
HEVC_NAL_BLA_W_LP
@ HEVC_NAL_BLA_W_LP
Definition: hevc.h:45
H264RawSPS
Definition: cbs_h264.h:102
CBS_TRACE_WRITE_START
#define CBS_TRACE_WRITE_START()
Definition: cbs_internal.h:246
H265RawFiller
Definition: cbs_h265.h:744
H264RawPPS
Definition: cbs_h264.h:171