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 "libavutil/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 = av_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  av_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  av_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  av_refstruct_unref(&priv->pps[i]);
814  }
815  }
816  av_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  av_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;
1090 
1091  err = init_get_bits8(&gbc, unit->data, unit->data_size);
1092  if (err < 0)
1093  return err;
1094 
1095  err = ff_cbs_alloc_unit_content(ctx, unit);
1096  if (err < 0)
1097  return err;
1098 
1099  switch (unit->type) {
1100  case VVC_DCI_NUT:
1101  {
1102  err = cbs_h266_read_dci(ctx, &gbc, unit->content);
1103 
1104  if (err < 0)
1105  return err;
1106  }
1107  break;
1108  case VVC_OPI_NUT:
1109  {
1110  err = cbs_h266_read_opi(ctx, &gbc, unit->content);
1111 
1112  if (err < 0)
1113  return err;
1114  }
1115  break;
1116  case VVC_VPS_NUT:
1117  {
1118  H266RawVPS *vps = unit->content;
1119 
1120  err = cbs_h266_read_vps(ctx, &gbc, vps);
1121  if (err < 0)
1122  return err;
1123 
1124  err = cbs_h266_replace_vps(ctx, unit);
1125  if (err < 0)
1126  return err;
1127  }
1128  break;
1129  case VVC_SPS_NUT:
1130  {
1131  H266RawSPS *sps = unit->content;
1132 
1133  err = cbs_h266_read_sps(ctx, &gbc, sps);
1134  if (err < 0)
1135  return err;
1136 
1137  err = cbs_h266_replace_sps(ctx, unit);
1138  if (err < 0)
1139  return err;
1140  }
1141  break;
1142 
1143  case VVC_PPS_NUT:
1144  {
1145  H266RawPPS *pps = unit->content;
1146 
1147  err = cbs_h266_read_pps(ctx, &gbc, pps);
1148  if (err < 0)
1149  return err;
1150 
1151  err = cbs_h266_replace_pps(ctx, unit);
1152  if (err < 0)
1153  return err;
1154  }
1155  break;
1156 
1157  case VVC_PREFIX_APS_NUT:
1158  case VVC_SUFFIX_APS_NUT:
1159  {
1160  err = cbs_h266_read_aps(ctx, &gbc, unit->content,
1161  unit->type == VVC_PREFIX_APS_NUT);
1162 
1163  if (err < 0)
1164  return err;
1165  }
1166  break;
1167  case VVC_PH_NUT:
1168  {
1169  H266RawPH *ph = unit->content;
1170  err = cbs_h266_read_ph(ctx, &gbc, ph);
1171  if (err < 0)
1172  return err;
1173  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1174  if (err < 0)
1175  return err;
1176  }
1177  break;
1178 
1179  case VVC_TRAIL_NUT:
1180  case VVC_STSA_NUT:
1181  case VVC_RADL_NUT:
1182  case VVC_RASL_NUT:
1183  case VVC_IDR_W_RADL:
1184  case VVC_IDR_N_LP:
1185  case VVC_CRA_NUT:
1186  case VVC_GDR_NUT:
1187  {
1188  H266RawSlice *slice = unit->content;
1189  int pos, len;
1190 
1191  err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1192  if (err < 0)
1193  return err;
1194 
1195  if (!cbs_h2645_read_more_rbsp_data(&gbc))
1196  return AVERROR_INVALIDDATA;
1197 
1198  pos = get_bits_count(&gbc);
1199  len = unit->data_size;
1200 
1202  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1203  if (err < 0)
1204  return err;
1205  slice->ph_ref = NULL;
1206  } else {
1207  slice->ph_ref = av_refstruct_ref(h266->ph_ref);
1208  }
1209  slice->ph = h266->ph;
1210  slice->pps = av_refstruct_ref(h266->pps[slice->ph->ph_pic_parameter_set_id]);
1211  slice->sps = av_refstruct_ref(h266->sps[slice->pps->pps_seq_parameter_set_id]);
1212 
1213  slice->header_size = pos / 8;
1214  slice->data_size = len - pos / 8;
1215  slice->data_ref = av_buffer_ref(unit->data_ref);
1216  if (!slice->data_ref)
1217  return AVERROR(ENOMEM);
1218  slice->data = unit->data + pos / 8;
1219  slice->data_bit_start = pos % 8;
1220  }
1221  break;
1222 
1223  case VVC_AUD_NUT:
1224  {
1225  err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1226  if (err < 0)
1227  return err;
1228  }
1229  break;
1230 
1231  case VVC_PREFIX_SEI_NUT:
1232  case VVC_SUFFIX_SEI_NUT:
1233  {
1234  err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1235  unit->type == VVC_PREFIX_SEI_NUT);
1236 
1237  if (err < 0)
1238  return err;
1239  }
1240  break;
1241 
1242  default:
1243  return AVERROR(ENOSYS);
1244  }
1245  return 0;
1246 }
1247 
1249  PutBitContext *pbc, const uint8_t *data,
1250  size_t data_size, int data_bit_start)
1251 {
1252  size_t rest = data_size - (data_bit_start + 7) / 8;
1253  const uint8_t *pos = data + data_bit_start / 8;
1254 
1255  av_assert0(data_bit_start >= 0 &&
1256  data_size > data_bit_start / 8);
1257 
1258  if (data_size * 8 + 8 > put_bits_left(pbc))
1259  return AVERROR(ENOSPC);
1260 
1261  if (!rest)
1262  goto rbsp_stop_one_bit;
1263 
1264  // First copy the remaining bits of the first byte
1265  // The above check ensures that we do not accidentally
1266  // copy beyond the rbsp_stop_one_bit.
1267  if (data_bit_start % 8)
1268  put_bits(pbc, 8 - data_bit_start % 8,
1269  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1270 
1271  if (put_bits_count(pbc) % 8 == 0) {
1272  // If the writer is aligned at this point,
1273  // memcpy can be used to improve performance.
1274  // This happens normally for CABAC.
1275  flush_put_bits(pbc);
1276  memcpy(put_bits_ptr(pbc), pos, rest);
1277  skip_put_bytes(pbc, rest);
1278  } else {
1279  // If not, we have to copy manually.
1280  // rbsp_stop_one_bit forces us to special-case
1281  // the last byte.
1282  uint8_t temp;
1283  int i;
1284 
1285  for (; rest > 4; rest -= 4, pos += 4)
1286  put_bits32(pbc, AV_RB32(pos));
1287 
1288  for (; rest > 1; rest--, pos++)
1289  put_bits(pbc, 8, *pos);
1290 
1291  rbsp_stop_one_bit:
1292  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1293 
1294  av_assert0(temp);
1295  i = ff_ctz(*pos);
1296  temp = temp >> i;
1297  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1298  put_bits(pbc, i, temp);
1299  if (put_bits_count(pbc) % 8)
1300  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1301  }
1302 
1303  return 0;
1304 }
1305 
1307  CodedBitstreamUnit *unit,
1308  PutBitContext *pbc)
1309 {
1310  int err;
1311 
1312  switch (unit->type) {
1313  case H264_NAL_SPS:
1314  {
1315  H264RawSPS *sps = unit->content;
1316 
1317  err = cbs_h264_write_sps(ctx, pbc, sps);
1318  if (err < 0)
1319  return err;
1320 
1321  err = cbs_h264_replace_sps(ctx, unit);
1322  if (err < 0)
1323  return err;
1324  }
1325  break;
1326 
1327  case H264_NAL_SPS_EXT:
1328  {
1329  H264RawSPSExtension *sps_ext = unit->content;
1330 
1331  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1332  if (err < 0)
1333  return err;
1334  }
1335  break;
1336 
1337  case H264_NAL_PPS:
1338  {
1339  H264RawPPS *pps = unit->content;
1340 
1341  err = cbs_h264_write_pps(ctx, pbc, pps);
1342  if (err < 0)
1343  return err;
1344 
1345  err = cbs_h264_replace_pps(ctx, unit);
1346  if (err < 0)
1347  return err;
1348  }
1349  break;
1350 
1351  case H264_NAL_SLICE:
1352  case H264_NAL_IDR_SLICE:
1354  {
1355  H264RawSlice *slice = unit->content;
1356 
1357  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1358  if (err < 0)
1359  return err;
1360 
1361  if (slice->data) {
1362  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1363  slice->data_size,
1364  slice->data_bit_start);
1365  if (err < 0)
1366  return err;
1367  } else {
1368  // No slice data - that was just the header.
1369  // (Bitstream may be unaligned!)
1370  }
1371  }
1372  break;
1373 
1374  case H264_NAL_AUD:
1375  {
1376  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1377  if (err < 0)
1378  return err;
1379  }
1380  break;
1381 
1382  case H264_NAL_SEI:
1383  {
1384  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1385  if (err < 0)
1386  return err;
1387  }
1388  break;
1389 
1390  case H264_NAL_FILLER_DATA:
1391  {
1392  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1393  if (err < 0)
1394  return err;
1395  }
1396  break;
1397 
1398  case H264_NAL_END_SEQUENCE:
1399  {
1400  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1401  if (err < 0)
1402  return err;
1403  }
1404  break;
1405 
1406  case H264_NAL_END_STREAM:
1407  {
1408  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1409  if (err < 0)
1410  return err;
1411  }
1412  break;
1413 
1414  default:
1415  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1416  "NAL unit type %"PRIu32".\n", unit->type);
1417  return AVERROR_PATCHWELCOME;
1418  }
1419 
1420  return 0;
1421 }
1422 
1424  CodedBitstreamUnit *unit,
1425  PutBitContext *pbc)
1426 {
1427  int err;
1428 
1429  switch (unit->type) {
1430  case HEVC_NAL_VPS:
1431  {
1432  H265RawVPS *vps = unit->content;
1433 
1434  err = cbs_h265_write_vps(ctx, pbc, vps);
1435  if (err < 0)
1436  return err;
1437 
1438  err = cbs_h265_replace_vps(ctx, unit);
1439  if (err < 0)
1440  return err;
1441  }
1442  break;
1443 
1444  case HEVC_NAL_SPS:
1445  {
1446  H265RawSPS *sps = unit->content;
1447 
1448  err = cbs_h265_write_sps(ctx, pbc, sps);
1449  if (err < 0)
1450  return err;
1451 
1452  err = cbs_h265_replace_sps(ctx, unit);
1453  if (err < 0)
1454  return err;
1455  }
1456  break;
1457 
1458  case HEVC_NAL_PPS:
1459  {
1460  H265RawPPS *pps = unit->content;
1461 
1462  err = cbs_h265_write_pps(ctx, pbc, pps);
1463  if (err < 0)
1464  return err;
1465 
1466  err = cbs_h265_replace_pps(ctx, unit);
1467  if (err < 0)
1468  return err;
1469  }
1470  break;
1471 
1472  case HEVC_NAL_TRAIL_N:
1473  case HEVC_NAL_TRAIL_R:
1474  case HEVC_NAL_TSA_N:
1475  case HEVC_NAL_TSA_R:
1476  case HEVC_NAL_STSA_N:
1477  case HEVC_NAL_STSA_R:
1478  case HEVC_NAL_RADL_N:
1479  case HEVC_NAL_RADL_R:
1480  case HEVC_NAL_RASL_N:
1481  case HEVC_NAL_RASL_R:
1482  case HEVC_NAL_BLA_W_LP:
1483  case HEVC_NAL_BLA_W_RADL:
1484  case HEVC_NAL_BLA_N_LP:
1485  case HEVC_NAL_IDR_W_RADL:
1486  case HEVC_NAL_IDR_N_LP:
1487  case HEVC_NAL_CRA_NUT:
1488  {
1489  H265RawSlice *slice = unit->content;
1490 
1491  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1492  if (err < 0)
1493  return err;
1494 
1495  if (slice->data) {
1496  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1497  slice->data_size,
1498  slice->data_bit_start);
1499  if (err < 0)
1500  return err;
1501  } else {
1502  // No slice data - that was just the header.
1503  }
1504  }
1505  break;
1506 
1507  case HEVC_NAL_AUD:
1508  {
1509  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1510  if (err < 0)
1511  return err;
1512  }
1513  break;
1514 
1515  case HEVC_NAL_FD_NUT:
1516  {
1517  err = cbs_h265_write_filler(ctx, pbc, unit->content);
1518  if (err < 0)
1519  return err;
1520  }
1521  break;
1522 
1523  case HEVC_NAL_SEI_PREFIX:
1524  case HEVC_NAL_SEI_SUFFIX:
1525  {
1526  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1527  unit->type == HEVC_NAL_SEI_PREFIX);
1528 
1529  if (err < 0)
1530  return err;
1531  }
1532  break;
1533 
1534  default:
1535  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1536  "NAL unit type %"PRIu32".\n", unit->type);
1537  return AVERROR_PATCHWELCOME;
1538  }
1539 
1540  return 0;
1541 }
1542 
1544  const CodedBitstreamUnit *unit,
1545  enum AVDiscard skip)
1546 {
1548  H264RawSliceHeader *slice;
1549  int slice_type_i, slice_type_b, slice_type_si;
1550 
1551  if (skip <= AVDISCARD_DEFAULT)
1552  return 0;
1553 
1554  // keep non-VCL
1555  if (unit->type != H264_NAL_SLICE &&
1556  unit->type != H264_NAL_IDR_SLICE &&
1557  unit->type != H264_NAL_AUXILIARY_SLICE)
1558  return 0;
1559 
1560  if (skip >= AVDISCARD_ALL)
1561  return 1;
1562 
1563  if (skip >= AVDISCARD_NONKEY && unit->type != H264_NAL_IDR_SLICE)
1564  return 1;
1565 
1566  header = (H264RawNALUnitHeader *)unit->content;
1567  if (!header) {
1568  av_log(ctx->log_ctx, AV_LOG_WARNING,
1569  "h264 nal unit header is null, missing decompose?\n");
1570  return 0;
1571  }
1572 
1573  if (skip >= AVDISCARD_NONREF && !header->nal_ref_idc)
1574  return 1;
1575 
1576  slice = (H264RawSliceHeader *)unit->content;
1577  if (!slice) {
1578  av_log(ctx->log_ctx, AV_LOG_WARNING,
1579  "h264 slice header is null, missing decompose?\n");
1580  return 0;
1581  }
1582 
1583  slice_type_i = slice->slice_type % 5 == 2;
1584  slice_type_b = slice->slice_type % 5 == 1;
1585  slice_type_si = slice->slice_type % 5 == 4;
1586 
1587  if (skip >= AVDISCARD_BIDIR && slice_type_b)
1588  return 1;
1589  if (skip >= AVDISCARD_NONINTRA && !slice_type_i && !slice_type_si)
1590  return 1;
1591 
1592  return 0;
1593 }
1594 
1596  const CodedBitstreamUnit *unit,
1597  enum AVDiscard skip)
1598 {
1599  H265RawSliceHeader *slice;
1600 
1601  if (skip <= AVDISCARD_DEFAULT)
1602  return 0;
1603 
1604  switch (unit->type) {
1605  case HEVC_NAL_BLA_W_LP:
1606  case HEVC_NAL_BLA_W_RADL:
1607  case HEVC_NAL_BLA_N_LP:
1608  case HEVC_NAL_IDR_W_RADL:
1609  case HEVC_NAL_IDR_N_LP:
1610  case HEVC_NAL_CRA_NUT:
1611  // IRAP slice
1612  if (skip < AVDISCARD_ALL)
1613  return 0;
1614  break;
1615 
1616  case HEVC_NAL_TRAIL_R:
1617  case HEVC_NAL_TRAIL_N:
1618  case HEVC_NAL_TSA_N:
1619  case HEVC_NAL_TSA_R:
1620  case HEVC_NAL_STSA_N:
1621  case HEVC_NAL_STSA_R:
1622  case HEVC_NAL_RADL_N:
1623  case HEVC_NAL_RADL_R:
1624  case HEVC_NAL_RASL_N:
1625  case HEVC_NAL_RASL_R:
1626  // Slice
1627  break;
1628  default:
1629  // Don't discard non-slice nal.
1630  return 0;
1631  }
1632 
1633  if (skip >= AVDISCARD_NONKEY)
1634  return 1;
1635 
1636  slice = (H265RawSliceHeader *)unit->content;
1637  if (!slice) {
1638  av_log(ctx->log_ctx, AV_LOG_WARNING,
1639  "h265 slice header is null, missing decompose?\n");
1640  return 0;
1641  }
1642 
1643  if (skip >= AVDISCARD_NONINTRA && slice->slice_type != HEVC_SLICE_I)
1644  return 1;
1645  if (skip >= AVDISCARD_BIDIR && slice->slice_type == HEVC_SLICE_B)
1646  return 1;
1647 
1648  if (skip >= AVDISCARD_NONREF) {
1649  switch (unit->type) {
1650  case HEVC_NAL_TRAIL_N:
1651  case HEVC_NAL_TSA_N:
1652  case HEVC_NAL_STSA_N:
1653  case HEVC_NAL_RADL_N:
1654  case HEVC_NAL_RASL_N:
1655  case HEVC_NAL_VCL_N10:
1656  case HEVC_NAL_VCL_N12:
1657  case HEVC_NAL_VCL_N14:
1658  // non-ref
1659  return 1;
1660  default:
1661  break;
1662  }
1663  }
1664 
1665  return 0;
1666 }
1667 
1669  CodedBitstreamUnit *unit,
1670  PutBitContext *pbc)
1671 {
1672  int err;
1673 
1674  switch (unit->type) {
1675  case VVC_DCI_NUT:
1676  {
1677  H266RawDCI *dci = unit->content;
1678 
1679  err = cbs_h266_write_dci(ctx, pbc, dci);
1680  if (err < 0)
1681  return err;
1682  }
1683  break;
1684  case VVC_OPI_NUT:
1685  {
1686  H266RawOPI *opi = unit->content;
1687 
1688  err = cbs_h266_write_opi(ctx, pbc, opi);
1689  if (err < 0)
1690  return err;
1691  }
1692  break;
1693  case VVC_VPS_NUT:
1694  {
1695  H266RawVPS *vps = unit->content;
1696 
1697  err = cbs_h266_write_vps(ctx, pbc, vps);
1698  if (err < 0)
1699  return err;
1700 
1701  err = cbs_h266_replace_vps(ctx, unit);
1702  if (err < 0)
1703  return err;
1704  }
1705  break;
1706  case VVC_SPS_NUT:
1707  {
1708  H266RawSPS *sps = unit->content;
1709 
1710  err = cbs_h266_write_sps(ctx, pbc, sps);
1711  if (err < 0)
1712  return err;
1713 
1714  err = cbs_h266_replace_sps(ctx, unit);
1715  if (err < 0)
1716  return err;
1717  }
1718  break;
1719 
1720  case VVC_PPS_NUT:
1721  {
1722  H266RawPPS *pps = unit->content;
1723 
1724  err = cbs_h266_write_pps(ctx, pbc, pps);
1725  if (err < 0)
1726  return err;
1727 
1728  err = cbs_h266_replace_pps(ctx, unit);
1729  if (err < 0)
1730  return err;
1731  }
1732  break;
1733 
1734  case VVC_PREFIX_APS_NUT:
1735  case VVC_SUFFIX_APS_NUT:
1736  {
1737  err = cbs_h266_write_aps(ctx, pbc, unit->content,
1738  unit->type == VVC_PREFIX_APS_NUT);
1739  if (err < 0)
1740  return err;
1741  }
1742  break;
1743  case VVC_PH_NUT:
1744  {
1745  H266RawPH *ph = unit->content;
1746  err = cbs_h266_write_ph(ctx, pbc, ph);
1747  if (err < 0)
1748  return err;
1749 
1750  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
1751  if (err < 0)
1752  return err;
1753  }
1754  break;
1755 
1756  case VVC_TRAIL_NUT:
1757  case VVC_STSA_NUT:
1758  case VVC_RADL_NUT:
1759  case VVC_RASL_NUT:
1760  case VVC_IDR_W_RADL:
1761  case VVC_IDR_N_LP:
1762  case VVC_CRA_NUT:
1763  case VVC_GDR_NUT:
1764  {
1765  H266RawSlice *slice = unit->content;
1766 
1767  err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1768  if (err < 0)
1769  return err;
1770 
1772  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
1773  if (err < 0)
1774  return err;
1775  }
1776 
1777  if (slice->data) {
1778  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1779  slice->data_size,
1780  slice->data_bit_start);
1781  if (err < 0)
1782  return err;
1783  } else {
1784  // No slice data - that was just the header.
1785  }
1786  }
1787  break;
1788 
1789  case VVC_AUD_NUT:
1790  {
1791  err = cbs_h266_write_aud(ctx, pbc, unit->content);
1792  if (err < 0)
1793  return err;
1794  }
1795  break;
1796 
1797  case VVC_PREFIX_SEI_NUT:
1798  case VVC_SUFFIX_SEI_NUT:
1799  {
1800  err = cbs_h266_write_sei(ctx, pbc, unit->content,
1801  unit->type == VVC_PREFIX_SEI_NUT);
1802 
1803  if (err < 0)
1804  return err;
1805  }
1806  break;
1807 
1808  default:
1809  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1810  "NAL unit type %"PRIu32".\n", unit->type);
1811  return AVERROR_PATCHWELCOME;
1812  }
1813 
1814  return 0;
1815 }
1816 
1819  int nal_unit_index)
1820 {
1821  // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1822  if (nal_unit_index == 0) {
1823  // Assume that this is the first NAL unit in an access unit.
1824  return 1;
1825  }
1826  if (codec_id == AV_CODEC_ID_H264)
1827  return type == H264_NAL_SPS || type == H264_NAL_PPS;
1828  if (codec_id == AV_CODEC_ID_HEVC)
1829  return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1830  if (codec_id == AV_CODEC_ID_VVC)
1831  return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
1832  return 0;
1833 }
1834 
1836  CodedBitstreamFragment *frag)
1837 {
1838  uint8_t *data;
1839  size_t max_size, dp, sp;
1840  int err, i, zero_run;
1841 
1842  for (i = 0; i < frag->nb_units; i++) {
1843  // Data should already all have been written when we get here.
1844  av_assert0(frag->units[i].data);
1845  }
1846 
1847  max_size = 0;
1848  for (i = 0; i < frag->nb_units; i++) {
1849  // Start code + content with worst-case emulation prevention.
1850  max_size += 4 + frag->units[i].data_size * 3 / 2;
1851  }
1852 
1854  if (!data)
1855  return AVERROR(ENOMEM);
1856 
1857  dp = 0;
1858  for (i = 0; i < frag->nb_units; i++) {
1859  CodedBitstreamUnit *unit = &frag->units[i];
1860 
1861  if (unit->data_bit_padding > 0) {
1862  if (i < frag->nb_units - 1)
1863  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1864  "unaligned padding on non-final NAL unit.\n");
1865  else
1866  frag->data_bit_padding = unit->data_bit_padding;
1867  }
1868 
1869  if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1870  // zero_byte
1871  data[dp++] = 0;
1872  }
1873  // start_code_prefix_one_3bytes
1874  data[dp++] = 0;
1875  data[dp++] = 0;
1876  data[dp++] = 1;
1877 
1878  zero_run = 0;
1879  for (sp = 0; sp < unit->data_size; sp++) {
1880  if (zero_run < 2) {
1881  if (unit->data[sp] == 0)
1882  ++zero_run;
1883  else
1884  zero_run = 0;
1885  } else {
1886  if ((unit->data[sp] & ~3) == 0) {
1887  // emulation_prevention_three_byte
1888  data[dp++] = 3;
1889  }
1890  zero_run = unit->data[sp] == 0;
1891  }
1892  data[dp++] = unit->data[sp];
1893  }
1894  }
1895 
1896  av_assert0(dp <= max_size);
1898  if (err)
1899  return err;
1900  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1901 
1903  NULL, NULL, 0);
1904  if (!frag->data_ref) {
1905  av_freep(&data);
1906  return AVERROR(ENOMEM);
1907  }
1908 
1909  frag->data = data;
1910  frag->data_size = dp;
1911 
1912  return 0;
1913 }
1914 
1916 {
1918 
1919  for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1920  av_refstruct_unref(&h264->sps[i]);
1921  for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1922  av_refstruct_unref(&h264->pps[i]);
1923 
1924  h264->active_sps = NULL;
1925  h264->active_pps = NULL;
1926  h264->last_slice_nal_unit_type = 0;
1927 }
1928 
1930 {
1932  int i;
1933 
1935 
1936  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1937  av_refstruct_unref(&h264->sps[i]);
1938  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1939  av_refstruct_unref(&h264->pps[i]);
1940 }
1941 
1943 {
1945 
1946  for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1947  av_refstruct_unref(&h265->vps[i]);
1948  for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1949  av_refstruct_unref(&h265->sps[i]);
1950  for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1951  av_refstruct_unref(&h265->pps[i]);
1952 
1953  h265->active_vps = NULL;
1954  h265->active_sps = NULL;
1955  h265->active_pps = NULL;
1956 }
1957 
1959 {
1961  int i;
1962 
1964 
1965  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1966  av_refstruct_unref(&h265->vps[i]);
1967  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1968  av_refstruct_unref(&h265->sps[i]);
1969  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1970  av_refstruct_unref(&h265->pps[i]);
1971 }
1972 
1974 {
1976 
1977  for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
1978  av_refstruct_unref(&h266->vps[i]);
1979  for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
1980  av_refstruct_unref(&h266->sps[i]);
1981  for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
1982  av_refstruct_unref(&h266->pps[i]);
1983  av_refstruct_unref(&h266->ph_ref);
1984 }
1985 
1987 {
1989 
1992  }
1993 
1994 static void cbs_h264_free_sei(AVRefStructOpaque unused, void *content)
1995 {
1996  H264RawSEI *sei = content;
1997  ff_cbs_sei_free_message_list(&sei->message_list);
1998 }
1999 
2003 
2005 
2009 
2014 
2016 
2018 };
2019 
2020 static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
2021 {
2022  H265RawSEI *sei = content;
2023  ff_cbs_sei_free_message_list(&sei->message_list);
2024 }
2025 
2030 
2033 
2034  // Slices of non-IRAP pictures.
2036  H265RawSlice, data),
2037  // Slices of IRAP pictures.
2039  H265RawSlice, data),
2040 
2043 
2045 };
2046 
2047 static void cbs_h266_free_slice(AVRefStructOpaque unused, void *content)
2048 {
2049  H266RawSlice *slice = content;
2050  av_buffer_unref(&slice->data_ref);
2051  av_refstruct_unref(&slice->sps);
2052  av_refstruct_unref(&slice->pps);
2053  av_refstruct_unref(&slice->ph_ref);
2054 }
2055 
2056 
2057 static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
2058 {
2059  H266RawSEI *sei = content;
2060  ff_cbs_sei_free_message_list(&sei->message_list);
2061 }
2062 
2067  {
2068  .nb_unit_types = 1,
2069  .unit_type.list[0] = VVC_SPS_NUT,
2070  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
2071  .content_size = sizeof(H266RawSPS),
2072  .type.ref = {
2073  .nb_offsets = 2,
2074  .offsets = { offsetof(H266RawSPS, extension_data.data),
2075  offsetof(H266RawSPS, vui.extension_data.data) }
2076  },
2077  },
2081 
2084 
2091 
2094 
2096 };
2097 
2100 
2101  .priv_data_size = sizeof(CodedBitstreamH264Context),
2102 
2103  .unit_types = cbs_h264_unit_types,
2104 
2105  .split_fragment = &cbs_h2645_split_fragment,
2106  .read_unit = &cbs_h264_read_nal_unit,
2107  .write_unit = &cbs_h264_write_nal_unit,
2108  .discarded_unit = &cbs_h264_discarded_nal_unit,
2109  .assemble_fragment = &cbs_h2645_assemble_fragment,
2110 
2111  .flush = &cbs_h264_flush,
2112  .close = &cbs_h264_close,
2113 };
2114 
2117 
2118  .priv_data_size = sizeof(CodedBitstreamH265Context),
2119 
2120  .unit_types = cbs_h265_unit_types,
2121 
2122  .split_fragment = &cbs_h2645_split_fragment,
2123  .read_unit = &cbs_h265_read_nal_unit,
2124  .write_unit = &cbs_h265_write_nal_unit,
2125  .discarded_unit = &cbs_h265_discarded_nal_unit,
2126  .assemble_fragment = &cbs_h2645_assemble_fragment,
2127 
2128  .flush = &cbs_h265_flush,
2129  .close = &cbs_h265_close,
2130 };
2131 
2134 
2135  .priv_data_size = sizeof(CodedBitstreamH266Context),
2136 
2137  .unit_types = cbs_h266_unit_types,
2138 
2139  .split_fragment = &cbs_h2645_split_fragment,
2140  .read_unit = &cbs_h266_read_nal_unit,
2141  .write_unit = &cbs_h266_write_nal_unit,
2142  .assemble_fragment = &cbs_h2645_assemble_fragment,
2143 
2144  .flush = &cbs_h266_flush,
2145  .close = &cbs_h266_close,
2146 };
2147 
2148 // Macro for the read/write pair.
2149 #define SEI_MESSAGE_RW(codec, name) \
2150  .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2151  .write = cbs_ ## codec ## _write_ ## name ## _internal
2152 
2154  {
2156  1, 1,
2157  sizeof(SEIRawFillerPayload),
2158  SEI_MESSAGE_RW(sei, filler_payload),
2159  },
2160  {
2162  1, 1,
2163  sizeof(SEIRawUserDataRegistered),
2164  SEI_MESSAGE_RW(sei, user_data_registered),
2165  },
2166  {
2168  1, 1,
2170  SEI_MESSAGE_RW(sei, user_data_unregistered),
2171  },
2172  {
2174  1, 0,
2176  SEI_MESSAGE_RW(sei, frame_packing_arrangement),
2177  },
2178  {
2180  0, 1,
2181  sizeof(SEIRawDecodedPictureHash),
2182  SEI_MESSAGE_RW(sei, decoded_picture_hash),
2183  },
2184  {
2186  1, 0,
2188  SEI_MESSAGE_RW(sei, mastering_display_colour_volume),
2189  },
2190  {
2192  1, 0,
2194  SEI_MESSAGE_RW(sei, content_light_level_info),
2195  },
2196  {
2198  1, 0,
2200  SEI_MESSAGE_RW(sei, alternative_transfer_characteristics),
2201  },
2202  {
2204  1, 0,
2206  SEI_MESSAGE_RW(sei, ambient_viewing_environment),
2207  },
2209 };
2210 
2212  {
2214  1, 0,
2215  sizeof(H264RawSEIBufferingPeriod),
2216  SEI_MESSAGE_RW(h264, sei_buffering_period),
2217  },
2218  {
2220  1, 0,
2221  sizeof(H264RawSEIPicTiming),
2222  SEI_MESSAGE_RW(h264, sei_pic_timing),
2223  },
2224  {
2226  1, 0,
2227  sizeof(H264RawSEIPanScanRect),
2228  SEI_MESSAGE_RW(h264, sei_pan_scan_rect),
2229  },
2230  {
2232  1, 0,
2233  sizeof(H264RawSEIRecoveryPoint),
2234  SEI_MESSAGE_RW(h264, sei_recovery_point),
2235  },
2236  {
2238  1, 0,
2240  SEI_MESSAGE_RW(h264, film_grain_characteristics),
2241  },
2242  {
2244  1, 0,
2246  SEI_MESSAGE_RW(h264, sei_frame_packing_arrangement),
2247  },
2248  {
2250  1, 0,
2252  SEI_MESSAGE_RW(h264, sei_display_orientation),
2253  },
2255 };
2256 
2258  {
2260  1, 0,
2261  sizeof(H265RawSEIBufferingPeriod),
2262  SEI_MESSAGE_RW(h265, sei_buffering_period),
2263  },
2264  {
2266  1, 0,
2267  sizeof(H265RawSEIPicTiming),
2268  SEI_MESSAGE_RW(h265, sei_pic_timing),
2269  },
2270  {
2272  1, 0,
2273  sizeof(H265RawSEIPanScanRect),
2274  SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
2275  },
2276  {
2278  1, 0,
2279  sizeof(H265RawSEIRecoveryPoint),
2280  SEI_MESSAGE_RW(h265, sei_recovery_point),
2281  },
2282  {
2284  1, 0,
2286  SEI_MESSAGE_RW(h265, film_grain_characteristics),
2287  },
2288  {
2290  1, 0,
2292  SEI_MESSAGE_RW(h265, sei_display_orientation),
2293  },
2294  {
2296  1, 0,
2298  SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
2299  },
2300  {
2302  0, 1,
2304  SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
2305  },
2306  {
2308  1, 0,
2309  sizeof(H265RawSEITimeCode),
2310  SEI_MESSAGE_RW(h265, sei_time_code),
2311  },
2312  {
2314  1, 0,
2316  SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
2317  },
2318  {
2320  1, 0,
2322  SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
2323  },
2325 };
2326 
2329 };
2330 
2332  {
2334  1, 0,
2336  SEI_MESSAGE_RW(sei, film_grain_characteristics),
2337  },
2338  {
2340  1, 0,
2341  sizeof(SEIRawDisplayOrientation),
2342  SEI_MESSAGE_RW(sei, display_orientation)
2343  },
2344  {
2346  1, 0,
2348  SEI_MESSAGE_RW(sei, frame_field_information)
2349  },
2351 };
2352 
2354  int payload_type)
2355 {
2357  int i;
2358 
2359  switch (ctx->codec->codec_id) {
2360  case AV_CODEC_ID_H264:
2362  break;
2363  case AV_CODEC_ID_H265:
2365  break;
2366  case AV_CODEC_ID_H266:
2368  break;
2369  default:
2370  return NULL;
2371  }
2372 
2373  for (i = 0; codec_list[i].type >= 0; i++) {
2374  if (codec_list[i].type == payload_type)
2375  return &codec_list[i];
2376  }
2377 
2378  if (ctx->codec->codec_id == AV_CODEC_ID_H266) {
2379  for (i = 0; cbs_sei_h274_types[i].type >= 0; i++) {
2380  if (cbs_sei_h274_types[i].type == payload_type)
2381  return &cbs_sei_h274_types[i];
2382  }
2383  }
2384 
2385  for (i = 0; cbs_sei_common_types[i].type >= 0; i++) {
2386  if (cbs_sei_common_types[i].type == payload_type)
2387  return &cbs_sei_common_types[i];
2388  }
2389 
2390  return NULL;
2391 }
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
cbs.h
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:276
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
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:119
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
H264_NAL_END_STREAM
@ H264_NAL_END_STREAM
Definition: h264.h:45
CBS_UNIT_TYPE_COMPLEX
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
Definition: cbs_internal.h:383
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:493
h2645_parse.h
cbs_h266.h
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:689
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
cbs_h264_syntax_template.c
H265RawSEITimeCode
Definition: cbs_h265.h:693
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
SEIRawUserDataRegistered
Definition: cbs_sei.h:33
H266RawDCI
Definition: cbs_h266.h:252
ff_ctz
#define ff_ctz
Definition: intmath.h:105
SEIRawAmbientViewingEnvironment
Definition: cbs_sei.h:94
GetByteContext
Definition: bytestream.h:33
cbs_h265_close
static av_cold void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1958
bytestream2_tell
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition: bytestream.h:192
SEIRawFrameFieldInformation
Definition: cbs_sei.h:128
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
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:419
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:114
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:250
SEI_TYPE_PAN_SCAN_RECT
@ SEI_TYPE_PAN_SCAN_RECT
Definition: sei.h:32
cbs_h264_close
static av_cold void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1929
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:3037
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:224
H265RawSEI
Definition: cbs_h265.h:739
cbs_h266_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[]
Definition: cbs_h2645.c:2063
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:226
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:604
b
#define b
Definition: input.c:42
data
const char data[16]
Definition: mxf.c:149
H265RawSEIPanScanRect
Definition: cbs_h265.h:630
cbs_h266_flush
static av_cold void cbs_h266_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1973
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:226
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:81
put_bits32
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:301
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
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
CodedBitstreamH265Context::common
CodedBitstreamH2645Context common
Definition: cbs_h265.h:752
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
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:512
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:1817
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:77
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
cbs_h264_flush
static av_cold void cbs_h264_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1915
H265RawPPS
Definition: cbs_h265.h:356
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:135
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:871
CodedBitstreamH266Context::vps
H266RawVPS * vps[VVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:869
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_h264_free_sei
static void cbs_h264_free_sei(AVRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:1994
cbs_sei_h265_types
static const SEIMessageTypeDescriptor cbs_sei_h265_types[]
Definition: cbs_h2645.c:2257
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:363
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
H265RawSEIPicTiming
Definition: cbs_h265.h:614
GetBitContext
Definition: get_bits.h:109
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:77
H2645_FLAG_IS_NALFF
@ H2645_FLAG_IS_NALFF
Definition: h2645_parse.h:97
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:135
cbs_sei_h274_types
static const SEIMessageTypeDescriptor cbs_sei_h274_types[]
Definition: cbs_h2645.c:2331
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: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:88
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
VVC_IDR_W_RADL
@ VVC_IDR_W_RADL
Definition: vvc.h:36
cbs_h265_flush
static av_cold void cbs_h265_flush(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1942
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:2153
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:175
avassert.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:95
H266RawSlice::ph_ref
void * ph_ref
RefStruct reference backing referred-to PH above.
Definition: cbs_h266.h:855
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
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
av_cold
#define av_cold
Definition: attributes.h:106
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:539
CodedBitstreamH2645Context::nal_length_size
int nal_length_size
Definition: cbs_h2645.h:30
H266RawSlice::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:854
H266RawSEI
Definition: cbs_h266.h:858
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:129
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:142
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:2327
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:41
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:54
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SEI_MESSAGE_TYPE_END
#define SEI_MESSAGE_TYPE_END
Definition: cbs_sei.h:200
SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
Definition: sei.h:34
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:146
CodedBitstreamType::codec_id
enum AVCodecID codec_id
Definition: cbs_internal.h:142
MAX_UINT_BITS
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:235
AVDISCARD_BIDIR
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition: defs.h:229
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
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
CBS_TRACE_WRITE_END
#define CBS_TRACE_WRITE_END()
Definition: cbs_internal.h:294
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:232
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:116
H265RawAUD
Definition: cbs_h265.h:487
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
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:1423
SPS
Sequence parameter set.
Definition: h264_ps.h:44
SEIMessageTypeDescriptor
Definition: cbs_sei.h:184
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
H264_NAL_AUD
@ H264_NAL_AUD
Definition: h264.h:43
H264RawNALUnitHeader
Definition: cbs_h264.h:31
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:93
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
CodedBitstreamH266Context::common
CodedBitstreamH2645Context common
Definition: cbs_h266.h:865
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:366
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:2000
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
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
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:231
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:227
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[]
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
H266RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h266.h:847
CodedBitstreamH266Context::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:872
cbs_h266_free_sei
static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:2057
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:186
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:135
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:99
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
av_refstruct_ref
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
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:339
CodedBitstreamH266Context
Definition: cbs_h266.h:863
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:141
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:1543
version
version
Definition: libkvazaar.c:315
H265RawFilmGrainCharacteristics
Definition: cbs_h265.h:647
VVC_SPS_NUT
@ VVC_SPS_NUT
Definition: vvc.h:44
vvc.h
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
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
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:2353
H264RawSlice::header
H264RawSliceHeader header
Definition: cbs_h264.h:409
HEVC_NAL_TSA_R
@ HEVC_NAL_TSA_R
Definition: hevc.h:32
H266RawSlice::pps
H266RawPPS * pps
RefStruct reference to referred-to PPS.
Definition: cbs_h266.h:853
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:230
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:105
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:90
CBS_UNIT_TYPES_COMPLEX
#define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func)
Definition: cbs_internal.h:376
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:369
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:870
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
H264_NAL_FILLER_DATA
@ H264_NAL_FILLER_DATA
Definition: h264.h:46
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
SEI_MESSAGE_RW
#define SEI_MESSAGE_RW(codec, name)
Definition: cbs_h2645.c:2149
CodedBitstreamH265Context::active_sps
const H265RawSPS * active_sps
Definition: cbs_h265.h:764
H266RawPictureHeader::ph_pic_parameter_set_id
uint8_t ph_pic_parameter_set_id
Definition: cbs_h266.h:682
CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_END_OF_LIST
Definition: cbs_internal.h:386
cbs_h266_free_slice
static void cbs_h266_free_slice(AVRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:2047
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
H264_NAL_SEI
@ H264_NAL_SEI
Definition: h264.h:40
H2645_FLAG_USE_REF
@ H2645_FLAG_USE_REF
Definition: h2645_parse.h:99
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
H264_NAL_SPS_EXT
@ H264_NAL_SPS_EXT
Definition: h264.h:47
flags
#define flags(name, subs,...)
Definition: cbs_h2645.c:267
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
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:402
SEI_TYPE_BUFFERING_PERIOD
@ SEI_TYPE_BUFFERING_PERIOD
Definition: sei.h:30
SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
av_refstruct_replace
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
H264_NAL_AUXILIARY_SLICE
@ H264_NAL_AUXILIARY_SLICE
Definition: h264.h:53
VVC_SUFFIX_APS_NUT
@ VVC_SUFFIX_APS_NUT
Definition: vvc.h:47
SEIRawDisplayOrientation
Definition: cbs_sei.h:121
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:411
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:271
pps
uint64_t pps
Definition: dovi_rpuenc.c:36
VVC_CRA_NUT
@ VVC_CRA_NUT
Definition: vvc.h:38
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
H266RawSlice::sps
H266RawSPS * sps
RefStruct reference to referred-to SPS.
Definition: cbs_h266.h:852
cbs_h265_discarded_nal_unit
static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
Definition: cbs_h2645.c:1595
H264RawSEIRecoveryPoint
Definition: cbs_h264.h:268
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
cbs_h265_free_sei
static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
Definition: cbs_h2645.c:2020
H264_NAL_END_SEQUENCE
@ H264_NAL_END_SEQUENCE
Definition: h264.h:44
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:153
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:2132
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:355
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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:1248
cbs_h265_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[]
Definition: cbs_h2645.c:2026
CodedBitstreamH266Context::ph_ref
void * ph_ref
RefStruct reference backing ph above.
Definition: cbs_h266.h:873
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:2098
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
SEI_TYPE_FRAME_FIELD_INFO
@ SEI_TYPE_FRAME_FIELD_INFO
Definition: sei.h:126
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:152
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:466
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:223
AVDISCARD_NONREF
@ AVDISCARD_NONREF
discard all non reference
Definition: defs.h:228
HEVC_NAL_RADL_N
@ HEVC_NAL_RADL_N
Definition: hevc.h:35
SEIRawFilmGrainCharacteristics
Definition: cbs_sei.h:100
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:2115
CBS_TRACE_READ_START
#define CBS_TRACE_READ_START()
Definition: cbs_internal.h:248
H2645Packet
Definition: h2645_parse.h:82
SEI_TYPE_ACTIVE_PARAMETER_SETS
@ SEI_TYPE_ACTIVE_PARAMETER_SETS
Definition: sei.h:87
cbs_h266_close
static av_cold void cbs_h266_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1986
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
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:1306
H264RawSlice::data_size
size_t data_size
Definition: cbs_h264.h:413
H265RawSEIBufferingPeriod
Definition: cbs_h265.h:593
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
cbs_sei_h264_types
static const SEIMessageTypeDescriptor cbs_sei_h264_types[]
Definition: cbs_h2645.c:2211
H265RawSEIAlphaChannelInfo
Definition: cbs_h265.h:712
cbs_h2645_assemble_fragment
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:1835
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160
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:1668
CBS_TRACE_READ_END
#define CBS_TRACE_READ_END()
Definition: cbs_internal.h:256
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:286
H265RawFiller
Definition: cbs_h265.h:744
H264RawPPS
Definition: cbs_h264.h:171