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_SEI_PREFIX:
1059  case HEVC_NAL_SEI_SUFFIX:
1060  {
1061  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1062  unit->type == HEVC_NAL_SEI_PREFIX);
1063 
1064  if (err < 0)
1065  return err;
1066  }
1067  break;
1068 
1069  default:
1070  return AVERROR(ENOSYS);
1071  }
1072 
1073  return 0;
1074 }
1075 
1077  CodedBitstreamUnit *unit)
1078 {
1079  GetBitContext gbc;
1080  int err;
1081 
1082  err = init_get_bits8(&gbc, unit->data, unit->data_size);
1083  if (err < 0)
1084  return err;
1085 
1086  err = ff_cbs_alloc_unit_content(ctx, unit);
1087  if (err < 0)
1088  return err;
1089 
1090  switch (unit->type) {
1091  case VVC_DCI_NUT:
1092  {
1093  err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1094 
1095  if (err < 0)
1096  return err;
1097  }
1098  break;
1099  case VVC_OPI_NUT:
1100  {
1101  err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1102 
1103  if (err < 0)
1104  return err;
1105  }
1106  break;
1107  case VVC_VPS_NUT:
1108  {
1109  H266RawVPS *vps = unit->content;
1110 
1111  err = cbs_h266_read_vps(ctx, &gbc, vps);
1112  if (err < 0)
1113  return err;
1114 
1115  err = cbs_h266_replace_vps(ctx, unit);
1116  if (err < 0)
1117  return err;
1118  }
1119  break;
1120  case VVC_SPS_NUT:
1121  {
1122  H266RawSPS *sps = unit->content;
1123 
1124  err = cbs_h266_read_sps(ctx, &gbc, sps);
1125  if (err < 0)
1126  return err;
1127 
1128  err = cbs_h266_replace_sps(ctx, unit);
1129  if (err < 0)
1130  return err;
1131  }
1132  break;
1133 
1134  case VVC_PPS_NUT:
1135  {
1136  H266RawPPS *pps = unit->content;
1137 
1138  err = cbs_h266_read_pps(ctx, &gbc, pps);
1139  if (err < 0)
1140  return err;
1141 
1142  err = cbs_h266_replace_pps(ctx, unit);
1143  if (err < 0)
1144  return err;
1145  }
1146  break;
1147 
1148  case VVC_PREFIX_APS_NUT:
1149  case VVC_SUFFIX_APS_NUT:
1150  {
1151  err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1152  unit->type == VVC_PREFIX_APS_NUT);
1153 
1154  if (err < 0)
1155  return err;
1156  }
1157  break;
1158  case VVC_PH_NUT:
1159  {
1160  H266RawPH *ph = unit->content;
1161  err = cbs_h266_read_ph(ctx, &gbc, ph);
1162  if (err < 0)
1163  return err;
1164  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1165  if (err < 0)
1166  return err;
1167  }
1168  break;
1169 
1170  case VVC_TRAIL_NUT:
1171  case VVC_STSA_NUT:
1172  case VVC_RADL_NUT:
1173  case VVC_RASL_NUT:
1174  case VVC_IDR_W_RADL:
1175  case VVC_IDR_N_LP:
1176  case VVC_CRA_NUT:
1177  case VVC_GDR_NUT:
1178  {
1179  H266RawSlice *slice = unit->content;
1180  int pos, len;
1181 
1182  err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1183  if (err < 0)
1184  return err;
1185 
1186  if (!cbs_h2645_read_more_rbsp_data(&gbc))
1187  return AVERROR_INVALIDDATA;
1188 
1189  pos = get_bits_count(&gbc);
1190  len = unit->data_size;
1191 
1193  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1194  if (err < 0)
1195  return err;
1196  }
1197 
1198  slice->header_size = pos / 8;
1199  slice->data_size = len - pos / 8;
1200  slice->data_ref = av_buffer_ref(unit->data_ref);
1201  if (!slice->data_ref)
1202  return AVERROR(ENOMEM);
1203  slice->data = unit->data + pos / 8;
1204  slice->data_bit_start = pos % 8;
1205  }
1206  break;
1207 
1208  case VVC_AUD_NUT:
1209  {
1210  err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1211  if (err < 0)
1212  return err;
1213  }
1214  break;
1215 
1216  case VVC_PREFIX_SEI_NUT:
1217  case VVC_SUFFIX_SEI_NUT:
1218  {
1219  err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1220  unit->type == VVC_PREFIX_SEI_NUT);
1221 
1222  if (err < 0)
1223  return err;
1224  }
1225  break;
1226 
1227  default:
1228  return AVERROR(ENOSYS);
1229  }
1230  return 0;
1231 }
1232 
1234  PutBitContext *pbc, const uint8_t *data,
1235  size_t data_size, int data_bit_start)
1236 {
1237  size_t rest = data_size - (data_bit_start + 7) / 8;
1238  const uint8_t *pos = data + data_bit_start / 8;
1239 
1240  av_assert0(data_bit_start >= 0 &&
1241  data_size > data_bit_start / 8);
1242 
1243  if (data_size * 8 + 8 > put_bits_left(pbc))
1244  return AVERROR(ENOSPC);
1245 
1246  if (!rest)
1247  goto rbsp_stop_one_bit;
1248 
1249  // First copy the remaining bits of the first byte
1250  // The above check ensures that we do not accidentally
1251  // copy beyond the rbsp_stop_one_bit.
1252  if (data_bit_start % 8)
1253  put_bits(pbc, 8 - data_bit_start % 8,
1254  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1255 
1256  if (put_bits_count(pbc) % 8 == 0) {
1257  // If the writer is aligned at this point,
1258  // memcpy can be used to improve performance.
1259  // This happens normally for CABAC.
1260  flush_put_bits(pbc);
1261  memcpy(put_bits_ptr(pbc), pos, rest);
1262  skip_put_bytes(pbc, rest);
1263  } else {
1264  // If not, we have to copy manually.
1265  // rbsp_stop_one_bit forces us to special-case
1266  // the last byte.
1267  uint8_t temp;
1268  int i;
1269 
1270  for (; rest > 4; rest -= 4, pos += 4)
1271  put_bits32(pbc, AV_RB32(pos));
1272 
1273  for (; rest > 1; rest--, pos++)
1274  put_bits(pbc, 8, *pos);
1275 
1276  rbsp_stop_one_bit:
1277  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1278 
1279  av_assert0(temp);
1280  i = ff_ctz(*pos);
1281  temp = temp >> i;
1282  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1283  put_bits(pbc, i, temp);
1284  if (put_bits_count(pbc) % 8)
1285  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1286  }
1287 
1288  return 0;
1289 }
1290 
1292  CodedBitstreamUnit *unit,
1293  PutBitContext *pbc)
1294 {
1295  int err;
1296 
1297  switch (unit->type) {
1298  case H264_NAL_SPS:
1299  {
1300  H264RawSPS *sps = unit->content;
1301 
1302  err = cbs_h264_write_sps(ctx, pbc, sps);
1303  if (err < 0)
1304  return err;
1305 
1306  err = cbs_h264_replace_sps(ctx, unit);
1307  if (err < 0)
1308  return err;
1309  }
1310  break;
1311 
1312  case H264_NAL_SPS_EXT:
1313  {
1314  H264RawSPSExtension *sps_ext = unit->content;
1315 
1316  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1317  if (err < 0)
1318  return err;
1319  }
1320  break;
1321 
1322  case H264_NAL_PPS:
1323  {
1324  H264RawPPS *pps = unit->content;
1325 
1326  err = cbs_h264_write_pps(ctx, pbc, pps);
1327  if (err < 0)
1328  return err;
1329 
1330  err = cbs_h264_replace_pps(ctx, unit);
1331  if (err < 0)
1332  return err;
1333  }
1334  break;
1335 
1336  case H264_NAL_SLICE:
1337  case H264_NAL_IDR_SLICE:
1339  {
1340  H264RawSlice *slice = unit->content;
1341 
1342  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1343  if (err < 0)
1344  return err;
1345 
1346  if (slice->data) {
1347  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1348  slice->data_size,
1349  slice->data_bit_start);
1350  if (err < 0)
1351  return err;
1352  } else {
1353  // No slice data - that was just the header.
1354  // (Bitstream may be unaligned!)
1355  }
1356  }
1357  break;
1358 
1359  case H264_NAL_AUD:
1360  {
1361  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1362  if (err < 0)
1363  return err;
1364  }
1365  break;
1366 
1367  case H264_NAL_SEI:
1368  {
1369  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1370  if (err < 0)
1371  return err;
1372  }
1373  break;
1374 
1375  case H264_NAL_FILLER_DATA:
1376  {
1377  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1378  if (err < 0)
1379  return err;
1380  }
1381  break;
1382 
1383  case H264_NAL_END_SEQUENCE:
1384  {
1385  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1386  if (err < 0)
1387  return err;
1388  }
1389  break;
1390 
1391  case H264_NAL_END_STREAM:
1392  {
1393  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1394  if (err < 0)
1395  return err;
1396  }
1397  break;
1398 
1399  default:
1400  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1401  "NAL unit type %"PRIu32".\n", unit->type);
1402  return AVERROR_PATCHWELCOME;
1403  }
1404 
1405  return 0;
1406 }
1407 
1409  CodedBitstreamUnit *unit,
1410  PutBitContext *pbc)
1411 {
1412  int err;
1413 
1414  switch (unit->type) {
1415  case HEVC_NAL_VPS:
1416  {
1417  H265RawVPS *vps = unit->content;
1418 
1419  err = cbs_h265_write_vps(ctx, pbc, vps);
1420  if (err < 0)
1421  return err;
1422 
1423  err = cbs_h265_replace_vps(ctx, unit);
1424  if (err < 0)
1425  return err;
1426  }
1427  break;
1428 
1429  case HEVC_NAL_SPS:
1430  {
1431  H265RawSPS *sps = unit->content;
1432 
1433  err = cbs_h265_write_sps(ctx, pbc, sps);
1434  if (err < 0)
1435  return err;
1436 
1437  err = cbs_h265_replace_sps(ctx, unit);
1438  if (err < 0)
1439  return err;
1440  }
1441  break;
1442 
1443  case HEVC_NAL_PPS:
1444  {
1445  H265RawPPS *pps = unit->content;
1446 
1447  err = cbs_h265_write_pps(ctx, pbc, pps);
1448  if (err < 0)
1449  return err;
1450 
1451  err = cbs_h265_replace_pps(ctx, unit);
1452  if (err < 0)
1453  return err;
1454  }
1455  break;
1456 
1457  case HEVC_NAL_TRAIL_N:
1458  case HEVC_NAL_TRAIL_R:
1459  case HEVC_NAL_TSA_N:
1460  case HEVC_NAL_TSA_R:
1461  case HEVC_NAL_STSA_N:
1462  case HEVC_NAL_STSA_R:
1463  case HEVC_NAL_RADL_N:
1464  case HEVC_NAL_RADL_R:
1465  case HEVC_NAL_RASL_N:
1466  case HEVC_NAL_RASL_R:
1467  case HEVC_NAL_BLA_W_LP:
1468  case HEVC_NAL_BLA_W_RADL:
1469  case HEVC_NAL_BLA_N_LP:
1470  case HEVC_NAL_IDR_W_RADL:
1471  case HEVC_NAL_IDR_N_LP:
1472  case HEVC_NAL_CRA_NUT:
1473  {
1474  H265RawSlice *slice = unit->content;
1475 
1476  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1477  if (err < 0)
1478  return err;
1479 
1480  if (slice->data) {
1481  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1482  slice->data_size,
1483  slice->data_bit_start);
1484  if (err < 0)
1485  return err;
1486  } else {
1487  // No slice data - that was just the header.
1488  }
1489  }
1490  break;
1491 
1492  case HEVC_NAL_AUD:
1493  {
1494  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1495  if (err < 0)
1496  return err;
1497  }
1498  break;
1499 
1500  case HEVC_NAL_SEI_PREFIX:
1501  case HEVC_NAL_SEI_SUFFIX:
1502  {
1503  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1504  unit->type == HEVC_NAL_SEI_PREFIX);
1505 
1506  if (err < 0)
1507  return err;
1508  }
1509  break;
1510 
1511  default:
1512  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1513  "NAL unit type %"PRIu32".\n", unit->type);
1514  return AVERROR_PATCHWELCOME;
1515  }
1516 
1517  return 0;
1518 }
1519 
1521  const CodedBitstreamUnit *unit,
1522  enum AVDiscard skip)
1523 {
1525  H264RawSliceHeader *slice;
1526  int slice_type_i, slice_type_b, slice_type_si;
1527 
1528  if (skip <= AVDISCARD_DEFAULT)
1529  return 0;
1530 
1531  // keep non-VCL
1532  if (unit->type != H264_NAL_SLICE &&
1533  unit->type != H264_NAL_IDR_SLICE &&
1534  unit->type != H264_NAL_AUXILIARY_SLICE)
1535  return 0;
1536 
1537  if (skip >= AVDISCARD_ALL)
1538  return 1;
1539 
1540  if (skip >= AVDISCARD_NONKEY && unit->type != H264_NAL_IDR_SLICE)
1541  return 1;
1542 
1543  header = (H264RawNALUnitHeader *)unit->content;
1544  if (!header) {
1545  av_log(ctx->log_ctx, AV_LOG_WARNING,
1546  "h264 nal unit header is null, missing decompose?\n");
1547  return 0;
1548  }
1549 
1550  if (skip >= AVDISCARD_NONREF && !header->nal_ref_idc)
1551  return 1;
1552 
1553  slice = (H264RawSliceHeader *)unit->content;
1554  if (!slice) {
1555  av_log(ctx->log_ctx, AV_LOG_WARNING,
1556  "h264 slice header is null, missing decompose?\n");
1557  return 0;
1558  }
1559 
1560  slice_type_i = slice->slice_type % 5 == 2;
1561  slice_type_b = slice->slice_type % 5 == 1;
1562  slice_type_si = slice->slice_type % 5 == 4;
1563 
1564  if (skip >= AVDISCARD_BIDIR && slice_type_b)
1565  return 1;
1566  if (skip >= AVDISCARD_NONINTRA && !slice_type_i && !slice_type_si)
1567  return 1;
1568 
1569  return 0;
1570 }
1571 
1573  const CodedBitstreamUnit *unit,
1574  enum AVDiscard skip)
1575 {
1576  H265RawSliceHeader *slice;
1577 
1578  if (skip <= AVDISCARD_DEFAULT)
1579  return 0;
1580 
1581  switch (unit->type) {
1582  case HEVC_NAL_BLA_W_LP:
1583  case HEVC_NAL_BLA_W_RADL:
1584  case HEVC_NAL_BLA_N_LP:
1585  case HEVC_NAL_IDR_W_RADL:
1586  case HEVC_NAL_IDR_N_LP:
1587  case HEVC_NAL_CRA_NUT:
1588  // IRAP slice
1589  if (skip < AVDISCARD_ALL)
1590  return 0;
1591  break;
1592 
1593  case HEVC_NAL_TRAIL_R:
1594  case HEVC_NAL_TRAIL_N:
1595  case HEVC_NAL_TSA_N:
1596  case HEVC_NAL_TSA_R:
1597  case HEVC_NAL_STSA_N:
1598  case HEVC_NAL_STSA_R:
1599  case HEVC_NAL_RADL_N:
1600  case HEVC_NAL_RADL_R:
1601  case HEVC_NAL_RASL_N:
1602  case HEVC_NAL_RASL_R:
1603  // Slice
1604  break;
1605  default:
1606  // Don't discard non-slice nal.
1607  return 0;
1608  }
1609 
1610  if (skip >= AVDISCARD_NONKEY)
1611  return 1;
1612 
1613  slice = (H265RawSliceHeader *)unit->content;
1614  if (!slice) {
1615  av_log(ctx->log_ctx, AV_LOG_WARNING,
1616  "h265 slice header is null, missing decompose?\n");
1617  return 0;
1618  }
1619 
1620  if (skip >= AVDISCARD_NONINTRA && slice->slice_type != HEVC_SLICE_I)
1621  return 1;
1622  if (skip >= AVDISCARD_BIDIR && slice->slice_type == HEVC_SLICE_B)
1623  return 1;
1624 
1625  if (skip >= AVDISCARD_NONREF) {
1626  switch (unit->type) {
1627  case HEVC_NAL_TRAIL_N:
1628  case HEVC_NAL_TSA_N:
1629  case HEVC_NAL_STSA_N:
1630  case HEVC_NAL_RADL_N:
1631  case HEVC_NAL_RASL_N:
1632  case HEVC_NAL_VCL_N10:
1633  case HEVC_NAL_VCL_N12:
1634  case HEVC_NAL_VCL_N14:
1635  // non-ref
1636  return 1;
1637  default:
1638  break;
1639  }
1640  }
1641 
1642  return 0;
1643 }
1644 
1646  CodedBitstreamUnit *unit,
1647  PutBitContext *pbc)
1648 {
1649  int err;
1650 
1651  switch (unit->type) {
1652  case VVC_DCI_NUT:
1653  {
1654  H266RawDCI *dci = unit->content;
1655 
1656  err = cbs_h266_write_dci(ctx, pbc, dci);
1657  if (err < 0)
1658  return err;
1659  }
1660  break;
1661  case VVC_OPI_NUT:
1662  {
1663  H266RawOPI *opi = unit->content;
1664 
1665  err = cbs_h266_write_opi(ctx, pbc, opi);
1666  if (err < 0)
1667  return err;
1668  }
1669  break;
1670  case VVC_VPS_NUT:
1671  {
1672  H266RawVPS *vps = unit->content;
1673 
1674  err = cbs_h266_write_vps(ctx, pbc, vps);
1675  if (err < 0)
1676  return err;
1677 
1678  err = cbs_h266_replace_vps(ctx, unit);
1679  if (err < 0)
1680  return err;
1681  }
1682  break;
1683  case VVC_SPS_NUT:
1684  {
1685  H266RawSPS *sps = unit->content;
1686 
1687  err = cbs_h266_write_sps(ctx, pbc, sps);
1688  if (err < 0)
1689  return err;
1690 
1691  err = cbs_h266_replace_sps(ctx, unit);
1692  if (err < 0)
1693  return err;
1694  }
1695  break;
1696 
1697  case VVC_PPS_NUT:
1698  {
1699  H266RawPPS *pps = unit->content;
1700 
1701  err = cbs_h266_write_pps(ctx, pbc, pps);
1702  if (err < 0)
1703  return err;
1704 
1705  err = cbs_h266_replace_pps(ctx, unit);
1706  if (err < 0)
1707  return err;
1708  }
1709  break;
1710 
1711  case VVC_PREFIX_APS_NUT:
1712  case VVC_SUFFIX_APS_NUT:
1713  {
1714  err = cbs_h266_write_aps(ctx, pbc, unit->content,
1715  unit->type == VVC_PREFIX_APS_NUT);
1716  if (err < 0)
1717  return err;
1718  }
1719  break;
1720  case VVC_PH_NUT:
1721  {
1722  H266RawPH *ph = unit->content;
1723  err = cbs_h266_write_ph(ctx, pbc, ph);
1724  if (err < 0)
1725  return err;
1726 
1727  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1728  if (err < 0)
1729  return err;
1730  }
1731  break;
1732 
1733  case VVC_TRAIL_NUT:
1734  case VVC_STSA_NUT:
1735  case VVC_RADL_NUT:
1736  case VVC_RASL_NUT:
1737  case VVC_IDR_W_RADL:
1738  case VVC_IDR_N_LP:
1739  case VVC_CRA_NUT:
1740  case VVC_GDR_NUT:
1741  {
1742  H266RawSlice *slice = unit->content;
1743 
1744  err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1745  if (err < 0)
1746  return err;
1747 
1749  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1750  if (err < 0)
1751  return err;
1752  }
1753 
1754  if (slice->data) {
1755  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1756  slice->data_size,
1757  slice->data_bit_start);
1758  if (err < 0)
1759  return err;
1760  } else {
1761  // No slice data - that was just the header.
1762  }
1763  }
1764  break;
1765 
1766  case VVC_AUD_NUT:
1767  {
1768  err = cbs_h266_write_aud(ctx, pbc, unit->content);
1769  if (err < 0)
1770  return err;
1771  }
1772  break;
1773 
1774  case VVC_PREFIX_SEI_NUT:
1775  case VVC_SUFFIX_SEI_NUT:
1776  {
1777  err = cbs_h266_write_sei(ctx, pbc, unit->content,
1778  unit->type == VVC_PREFIX_SEI_NUT);
1779 
1780  if (err < 0)
1781  return err;
1782  }
1783  break;
1784 
1785  default:
1786  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1787  "NAL unit type %"PRIu32".\n", unit->type);
1788  return AVERROR_PATCHWELCOME;
1789  }
1790 
1791  return 0;
1792 }
1793 
1796  int nal_unit_index)
1797 {
1798  // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1799  if (nal_unit_index == 0) {
1800  // Assume that this is the first NAL unit in an access unit.
1801  return 1;
1802  }
1803  if (codec_id == AV_CODEC_ID_H264)
1804  return type == H264_NAL_SPS || type == H264_NAL_PPS;
1805  if (codec_id == AV_CODEC_ID_HEVC)
1806  return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1807  if (codec_id == AV_CODEC_ID_VVC)
1808  return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
1809  return 0;
1810 }
1811 
1813  CodedBitstreamFragment *frag)
1814 {
1815  uint8_t *data;
1816  size_t max_size, dp, sp;
1817  int err, i, zero_run;
1818 
1819  for (i = 0; i < frag->nb_units; i++) {
1820  // Data should already all have been written when we get here.
1821  av_assert0(frag->units[i].data);
1822  }
1823 
1824  max_size = 0;
1825  for (i = 0; i < frag->nb_units; i++) {
1826  // Start code + content with worst-case emulation prevention.
1827  max_size += 4 + frag->units[i].data_size * 3 / 2;
1828  }
1829 
1831  if (!data)
1832  return AVERROR(ENOMEM);
1833 
1834  dp = 0;
1835  for (i = 0; i < frag->nb_units; i++) {
1836  CodedBitstreamUnit *unit = &frag->units[i];
1837 
1838  if (unit->data_bit_padding > 0) {
1839  if (i < frag->nb_units - 1)
1840  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1841  "unaligned padding on non-final NAL unit.\n");
1842  else
1843  frag->data_bit_padding = unit->data_bit_padding;
1844  }
1845 
1846  if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1847  // zero_byte
1848  data[dp++] = 0;
1849  }
1850  // start_code_prefix_one_3bytes
1851  data[dp++] = 0;
1852  data[dp++] = 0;
1853  data[dp++] = 1;
1854 
1855  zero_run = 0;
1856  for (sp = 0; sp < unit->data_size; sp++) {
1857  if (zero_run < 2) {
1858  if (unit->data[sp] == 0)
1859  ++zero_run;
1860  else
1861  zero_run = 0;
1862  } else {
1863  if ((unit->data[sp] & ~3) == 0) {
1864  // emulation_prevention_three_byte
1865  data[dp++] = 3;
1866  }
1867  zero_run = unit->data[sp] == 0;
1868  }
1869  data[dp++] = unit->data[sp];
1870  }
1871  }
1872 
1873  av_assert0(dp <= max_size);
1875  if (err)
1876  return err;
1877  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1878 
1880  NULL, NULL, 0);
1881  if (!frag->data_ref) {
1882  av_freep(&data);
1883  return AVERROR(ENOMEM);
1884  }
1885 
1886  frag->data = data;
1887  frag->data_size = dp;
1888 
1889  return 0;
1890 }
1891 
1893 {
1895 
1896  for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1897  ff_refstruct_unref(&h264->sps[i]);
1898  for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1899  ff_refstruct_unref(&h264->pps[i]);
1900 
1901  h264->active_sps = NULL;
1902  h264->active_pps = NULL;
1903  h264->last_slice_nal_unit_type = 0;
1904 }
1905 
1907 {
1909  int i;
1910 
1912 
1913  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1914  ff_refstruct_unref(&h264->sps[i]);
1915  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1916  ff_refstruct_unref(&h264->pps[i]);
1917 }
1918 
1920 {
1922 
1923  for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1924  ff_refstruct_unref(&h265->vps[i]);
1925  for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1926  ff_refstruct_unref(&h265->sps[i]);
1927  for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1928  ff_refstruct_unref(&h265->pps[i]);
1929 
1930  h265->active_vps = NULL;
1931  h265->active_sps = NULL;
1932  h265->active_pps = NULL;
1933 }
1934 
1936 {
1938  int i;
1939 
1941 
1942  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1943  ff_refstruct_unref(&h265->vps[i]);
1944  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1945  ff_refstruct_unref(&h265->sps[i]);
1946  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1947  ff_refstruct_unref(&h265->pps[i]);
1948 }
1949 
1951 {
1953 
1954  for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1955  ff_refstruct_unref(&h266->vps[i]);
1956  for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1957  ff_refstruct_unref(&h266->sps[i]);
1958  for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1959  ff_refstruct_unref(&h266->pps[i]);
1960  ff_refstruct_unref(&h266->ph_ref);
1961 }
1962 
1964 {
1966 
1969  }
1970 
1971 static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
1972 {
1973  H264RawSEI *sei = content;
1974  ff_cbs_sei_free_message_list(&sei->message_list);
1975 }
1976 
1980 
1982 
1986 
1991 
1993 
1995 };
1996 
1997 static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
1998 {
1999  H265RawSEI *sei = content;
2000  ff_cbs_sei_free_message_list(&sei->message_list);
2001 }
2002 
2007 
2009 
2010  // Slices of non-IRAP pictures.
2012  H265RawSlice, data),
2013  // Slices of IRAP pictures.
2015  H265RawSlice, data),
2016 
2019 
2021 };
2022 
2023 static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
2024 {
2025  H266RawSEI *sei = content;
2026  ff_cbs_sei_free_message_list(&sei->message_list);
2027 }
2028 
2033  {
2034  .nb_unit_types = 1,
2035  .unit_type.list[0] = VVC_SPS_NUT,
2036  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2037  .content_size = sizeof(H266RawSPS),
2038  .type.ref = {
2039  .nb_offsets = 2,
2040  .offsets = { offsetof(H266RawSPS, extension_data.data),
2041  offsetof(H266RawSPS, vui.extension_data.data) }
2042  },
2043  },
2047 
2050 
2052  H266RawSlice, data),
2053 
2055  H266RawSlice, data),
2056 
2059 
2061 };
2062 
2065 
2066  .priv_data_size = sizeof(CodedBitstreamH264Context),
2067 
2068  .unit_types = cbs_h264_unit_types,
2069 
2070  .split_fragment = &cbs_h2645_split_fragment,
2071  .read_unit = &cbs_h264_read_nal_unit,
2072  .write_unit = &cbs_h264_write_nal_unit,
2073  .discarded_unit = &cbs_h264_discarded_nal_unit,
2074  .assemble_fragment = &cbs_h2645_assemble_fragment,
2075 
2076  .flush = &cbs_h264_flush,
2077  .close = &cbs_h264_close,
2078 };
2079 
2082 
2083  .priv_data_size = sizeof(CodedBitstreamH265Context),
2084 
2085  .unit_types = cbs_h265_unit_types,
2086 
2087  .split_fragment = &cbs_h2645_split_fragment,
2088  .read_unit = &cbs_h265_read_nal_unit,
2089  .write_unit = &cbs_h265_write_nal_unit,
2090  .discarded_unit = &cbs_h265_discarded_nal_unit,
2091  .assemble_fragment = &cbs_h2645_assemble_fragment,
2092 
2093  .flush = &cbs_h265_flush,
2094  .close = &cbs_h265_close,
2095 };
2096 
2099 
2100  .priv_data_size = sizeof(CodedBitstreamH266Context),
2101 
2102  .unit_types = cbs_h266_unit_types,
2103 
2104  .split_fragment = &cbs_h2645_split_fragment,
2105  .read_unit = &cbs_h266_read_nal_unit,
2106  .write_unit = &cbs_h266_write_nal_unit,
2107  .assemble_fragment = &cbs_h2645_assemble_fragment,
2108 
2109  .flush = &cbs_h266_flush,
2110  .close = &cbs_h266_close,
2111 };
2112 
2113 // Macro for the read/write pair.
2114 #define SEI_MESSAGE_RW(codec, name) \
2115  .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2116  .write = cbs_ ## codec ## _write_ ## name ## _internal
2117 
2119  {
2121  1, 1,
2122  sizeof(SEIRawFillerPayload),
2123  SEI_MESSAGE_RW(sei, filler_payload),
2124  },
2125  {
2127  1, 1,
2128  sizeof(SEIRawUserDataRegistered),
2129  SEI_MESSAGE_RW(sei, user_data_registered),
2130  },
2131  {
2133  1, 1,
2135  SEI_MESSAGE_RW(sei, user_data_unregistered),
2136  },
2137  {
2139  1, 0,
2141  SEI_MESSAGE_RW(sei, frame_packing_arrangement),
2142  },
2143  {
2145  0, 1,
2146  sizeof(SEIRawDecodedPictureHash),
2147  SEI_MESSAGE_RW(sei, decoded_picture_hash),
2148  },
2149  {
2151  1, 0,
2153  SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2154  },
2155  {
2157  1, 0,
2159  SEI_MESSAGE_RW(sei, content_light_level_info),
2160  },
2161  {
2163  1, 0,
2165  SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2166  },
2167  {
2169  1, 0,
2171  SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2172  },
2174 };
2175 
2177  {
2179  1, 0,
2180  sizeof(H264RawSEIBufferingPeriod),
2181  SEI_MESSAGE_RW(h264, sei_buffering_period),
2182  },
2183  {
2185  1, 0,
2186  sizeof(H264RawSEIPicTiming),
2187  SEI_MESSAGE_RW(h264, sei_pic_timing),
2188  },
2189  {
2191  1, 0,
2192  sizeof(H264RawSEIPanScanRect),
2193  SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2194  },
2195  {
2197  1, 0,
2198  sizeof(H264RawSEIRecoveryPoint),
2199  SEI_MESSAGE_RW(h264, sei_recovery_point),
2200  },
2201  {
2203  1, 0,
2205  SEI_MESSAGE_RW(h264, film_grain_characteristics),
2206  },
2207  {
2209  1, 0,
2211  SEI_MESSAGE_RW(h264, sei_frame_packing_arrangement),
2212  },
2213  {
2215  1, 0,
2217  SEI_MESSAGE_RW(h264, sei_display_orientation),
2218  },
2220 };
2221 
2223  {
2225  1, 0,
2226  sizeof(H265RawSEIBufferingPeriod),
2227  SEI_MESSAGE_RW(h265, sei_buffering_period),
2228  },
2229  {
2231  1, 0,
2232  sizeof(H265RawSEIPicTiming),
2233  SEI_MESSAGE_RW(h265, sei_pic_timing),
2234  },
2235  {
2237  1, 0,
2238  sizeof(H265RawSEIPanScanRect),
2239  SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2240  },
2241  {
2243  1, 0,
2244  sizeof(H265RawSEIRecoveryPoint),
2245  SEI_MESSAGE_RW(h265, sei_recovery_point),
2246  },
2247  {
2249  1, 0,
2251  SEI_MESSAGE_RW(h265, film_grain_characteristics),
2252  },
2253  {
2255  1, 0,
2257  SEI_MESSAGE_RW(h265, sei_display_orientation),
2258  },
2259  {
2261  1, 0,
2263  SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2264  },
2265  {
2267  0, 1,
2269  SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2270  },
2271  {
2273  1, 0,
2274  sizeof(H265RawSEITimeCode),
2275  SEI_MESSAGE_RW(h265, sei_time_code),
2276  },
2277  {
2279  1, 0,
2281  SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2282  },
2283  {
2285  1, 0,
2287  SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
2288  },
2290 };
2291 
2294 };
2295 
2297  int payload_type)
2298 {
2300  int i;
2301 
2302  switch (ctx->codec->codec_id) {
2303  case AV_CODEC_ID_H264:
2305  break;
2306  case AV_CODEC_ID_H265:
2308  break;
2309  case AV_CODEC_ID_H266:
2311  break;
2312  default:
2313  return NULL;
2314  }
2315 
2316  for (i = 0; codec_list[i].type >= 0; i++) {
2317  if (codec_list[i].type == payload_type)
2318  return &codec_list[i];
2319  }
2320 
2321  for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2322  if (cbs_sei_common_types[i].type == payload_type)
2323  return &cbs_sei_common_types[i];
2324  }
2325 
2326  return NULL;
2327 }
VVC_RADL_NUT
@ VVC_RADL_NUT
Definition: vvc.h:31
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
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
H2645_FLAG_USE_REF
@ H2645_FLAG_USE_REF
Definition: h2645_parse.h:99
CodedBitstreamH265Context::vps
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:750
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:186
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
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
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:3032
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_END_STREAM
@ H264_NAL_END_STREAM
Definition: h264.h:45
cbs_h266_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[]
Definition: cbs_h2645.c:2029
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:751
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:148
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:196
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
cbs.h
H265RawSEIDecodedPictureHash
Definition: cbs_h265.h:686
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
H264_NAL_SEI
@ H264_NAL_SEI
Definition: h264.h:40
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
CodedBitstreamH265Context::common
CodedBitstreamH2645Context common
Definition: cbs_h265.h:746
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
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
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:1794
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
H265RawSEI3DReferenceDisplaysInfo
Definition: cbs_h265.h:723
H265RawSPS
Definition: cbs_h265.h:245
H265RawVPS
Definition: cbs_h265.h:184
H264_NAL_AUD
@ H264_NAL_AUD
Definition: h264.h:43
H265RawPPS
Definition: cbs_h265.h:356
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:864
cbs_h265_flush
static void cbs_h265_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1919
CodedBitstreamH266Context::vps
H266RawVPS * vps[VVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:862
H264RawSEIPicTiming
Definition: cbs_h264.h:249
HEVC_NAL_VCL_N14
@ HEVC_NAL_VCL_N14
Definition: hevc.h:43
H266RawAUD
Definition: cbs_h266.h:644
cbs_sei_h265_types
static const SEIMessageTypeDescriptor cbs_sei_h265_types[]
Definition: cbs_h2645.c:2222
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:1892
H265RawSEIPicTiming
Definition: cbs_h265.h:614
GetBitContext
Definition: get_bits.h:108
H266RawAPS
Definition: cbs_h266.h:598
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
H266RawSliceHeader::sh_picture_header_in_slice_header_flag
uint8_t sh_picture_header_in_slice_header_flag
Definition: cbs_h266.h:771
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:2118
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
VVC_MAX_PPS_COUNT
@ VVC_MAX_PPS_COUNT
Definition: vvc.h:99
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:180
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:844
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:851
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
H264_NAL_SPS_EXT
@ H264_NAL_SPS_EXT
Definition: h264.h:47
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:2292
H264_NAL_FILLER_DATA
@ H264_NAL_FILLER_DATA
Definition: h264.h:46
cbs_h265_free_sei
static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:1997
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:394
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
H264_NAL_END_SEQUENCE
@ H264_NAL_END_SEQUENCE
Definition: h264.h:44
cbs_h266_read_nal_unit
static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1076
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:674
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:251
cbs_h265_write_nal_unit
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1408
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:1963
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:368
opi
static int FUNC() opi(CodedBitstreamContext *ctx, RWContext *rw, H266RawOPI *current)
Definition: cbs_h266_syntax_template.c:643
H264RawSEIPanScanRect
Definition: cbs_h264.h:257
CodedBitstreamH266Context::common
CodedBitstreamH2645Context common
Definition: cbs_h266.h:858
CodedBitstreamH2645Context::read_packet
H2645Packet read_packet
Definition: cbs_h2645.h:32
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:1977
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
H266RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h266.h:848
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:772
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:845
CodedBitstreamH266Context::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:865
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
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
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:856
AV_CODEC_ID_VVC
@ AV_CODEC_ID_VVC
Definition: codec_id.h:250
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:1520
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:1935
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:2296
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:1950
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:863
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:846
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
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:847
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:2114
CodedBitstreamH265Context::active_sps
const H265RawSPS * active_sps
Definition: cbs_h265.h:758
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:1906
cbs_h264_free_sei
static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:1971
CodedBitstreamH265Context::pps
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:752
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:842
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
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
CodedBitstreamH265Context::active_pps
const H265RawPPS * active_pps
Definition: cbs_h265.h:759
H264RawAUD
Definition: cbs_h264.h:218
VVC_PREFIX_APS_NUT
@ VVC_PREFIX_APS_NUT
Definition: vvc.h:46
H2645_FLAG_IS_NALFF
@ H2645_FLAG_IS_NALFF
Definition: h2645_parse.h:97
HEVC_NAL_CRA_NUT
@ HEVC_NAL_CRA_NUT
Definition: hevc.h:50
H266RawPH
Definition: cbs_h266.h:764
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:227
CodedBitstreamH265Context::active_vps
const H265RawVPS * active_vps
Definition: cbs_h265.h:757
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
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:2023
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:1572
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:2097
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:1233
cbs_h265_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[]
Definition: cbs_h2645.c:2003
CodedBitstreamH266Context::ph_ref
void * ph_ref
RefStruct reference backing ph above.
Definition: cbs_h266.h:866
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:2063
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
H264_NAL_AUXILIARY_SLICE
@ H264_NAL_AUXILIARY_SLICE
Definition: h264.h:53
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:2080
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:1288
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:1291
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
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:2176
H265RawSEIAlphaChannelInfo
Definition: cbs_h265.h:712
cbs_h2645_assemble_fragment
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:1812
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153
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:744
H266RawSlice
Definition: cbs_h266.h:841
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:1645
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
H264RawPPS
Definition: cbs_h264.h:171