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