FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "golomb.h"
28 #include "h264.h"
29 #include "h264_sei.h"
30 #include "h2645_parse.h"
31 #include "hevc.h"
32 
33 
35  const char *name, uint32_t *write_to,
36  uint32_t range_min, uint32_t range_max)
37 {
38  uint32_t value;
39  int position, i, j;
40  unsigned int k;
41  char bits[65];
42 
43  position = get_bits_count(gbc);
44 
45  for (i = 0; i < 32; i++) {
46  if (get_bits_left(gbc) < i + 1) {
47  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
48  "%s: bitstream ended.\n", name);
49  return AVERROR_INVALIDDATA;
50  }
51  k = get_bits1(gbc);
52  bits[i] = k ? '1' : '0';
53  if (k)
54  break;
55  }
56  if (i >= 32) {
57  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
58  "%s: more than 31 zeroes.\n", name);
59  return AVERROR_INVALIDDATA;
60  }
61  value = 1;
62  for (j = 0; j < i; j++) {
63  k = get_bits1(gbc);
64  bits[i + j + 1] = k ? '1' : '0';
65  value = value << 1 | k;
66  }
67  bits[i + j + 1] = 0;
68  --value;
69 
70  if (ctx->trace_enable)
71  ff_cbs_trace_syntax_element(ctx, position, name, 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, int32_t *write_to,
86  int32_t range_min, int32_t range_max)
87 {
88  int32_t value;
89  int position, i, j;
90  unsigned int k;
91  uint32_t v;
92  char bits[65];
93 
94  position = get_bits_count(gbc);
95 
96  for (i = 0; i < 32; i++) {
97  if (get_bits_left(gbc) < i + 1) {
98  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
99  "%s: bitstream ended.\n", name);
100  return AVERROR_INVALIDDATA;
101  }
102  k = get_bits1(gbc);
103  bits[i] = k ? '1' : '0';
104  if (k)
105  break;
106  }
107  if (i >= 32) {
108  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
109  "%s: more than 31 zeroes.\n", name);
110  return AVERROR_INVALIDDATA;
111  }
112  v = 1;
113  for (j = 0; j < i; j++) {
114  k = get_bits1(gbc);
115  bits[i + j + 1] = k ? '1' : '0';
116  v = v << 1 | k;
117  }
118  bits[i + j + 1] = 0;
119  if (v & 1)
120  value = -(int32_t)(v / 2);
121  else
122  value = v / 2;
123 
124  if (ctx->trace_enable)
125  ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
126 
127  if (value < range_min || value > range_max) {
128  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
129  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
130  name, value, range_min, range_max);
131  return AVERROR_INVALIDDATA;
132  }
133 
134  *write_to = value;
135  return 0;
136 }
137 
139  const char *name, uint32_t value,
140  uint32_t range_min, uint32_t range_max)
141 {
142  int len;
143 
144  if (value < range_min || value > range_max) {
145  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
146  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
147  name, value, range_min, range_max);
148  return AVERROR_INVALIDDATA;
149  }
150  av_assert0(value != UINT32_MAX);
151 
152  len = av_log2(value + 1);
153  if (put_bits_left(pbc) < 2 * len + 1)
154  return AVERROR(ENOSPC);
155 
156  if (ctx->trace_enable) {
157  char bits[65];
158  int i;
159 
160  for (i = 0; i < len; i++)
161  bits[i] = '0';
162  bits[len] = '1';
163  for (i = 0; i < len; i++)
164  bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
165  bits[len + len + 1] = 0;
166 
167  ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
168  }
169 
170  put_bits(pbc, len, 0);
171  if (len + 1 < 32)
172  put_bits(pbc, len + 1, value + 1);
173  else
174  put_bits32(pbc, value + 1);
175 
176  return 0;
177 }
178 
180  const char *name, int32_t value,
181  int32_t range_min, int32_t range_max)
182 {
183  int len;
184  uint32_t uvalue;
185 
186  if (value < range_min || value > range_max) {
187  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
188  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
189  name, value, range_min, range_max);
190  return AVERROR_INVALIDDATA;
191  }
192  av_assert0(value != INT32_MIN);
193 
194  if (value == 0)
195  uvalue = 0;
196  else if (value > 0)
197  uvalue = 2 * (uint32_t)value - 1;
198  else
199  uvalue = 2 * (uint32_t)-value;
200 
201  len = av_log2(uvalue + 1);
202  if (put_bits_left(pbc) < 2 * len + 1)
203  return AVERROR(ENOSPC);
204 
205  if (ctx->trace_enable) {
206  char bits[65];
207  int i;
208 
209  for (i = 0; i < len; i++)
210  bits[i] = '0';
211  bits[len] = '1';
212  for (i = 0; i < len; i++)
213  bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
214  bits[len + len + 1] = 0;
215 
216  ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
217  }
218 
219  put_bits(pbc, len, 0);
220  if (len + 1 < 32)
221  put_bits(pbc, len + 1, uvalue + 1);
222  else
223  put_bits32(pbc, uvalue + 1);
224 
225  return 0;
226 }
227 
228 #define HEADER(name) do { \
229  ff_cbs_trace_header(ctx, name); \
230  } while (0)
231 
232 #define CHECK(call) do { \
233  err = (call); \
234  if (err < 0) \
235  return err; \
236  } while (0)
237 
238 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
239 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
240 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
241 
242 
243 #define READ
244 #define READWRITE read
245 #define RWContext GetBitContext
246 
247 #define xu(width, name, var, range_min, range_max) do { \
248  uint32_t value = range_min; \
249  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
250  &value, range_min, range_max)); \
251  var = value; \
252  } while (0)
253 #define xue(name, var, range_min, range_max) do { \
254  uint32_t value = range_min; \
255  CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
256  &value, range_min, range_max)); \
257  var = value; \
258  } while (0)
259 #define xse(name, var, range_min, range_max) do { \
260  int32_t value = range_min; \
261  CHECK(cbs_read_se_golomb(ctx, rw, #name, \
262  &value, range_min, range_max)); \
263  var = value; \
264  } while (0)
265 
266 
267 #define u(width, name, range_min, range_max) \
268  xu(width, name, current->name, range_min, range_max)
269 #define flag(name) u(1, name, 0, 1)
270 #define ue(name, range_min, range_max) \
271  xue(name, current->name, range_min, range_max)
272 #define se(name, range_min, range_max) \
273  xse(name, current->name, range_min, range_max)
274 
275 #define infer(name, value) do { \
276  current->name = value; \
277  } while (0)
278 
280 {
281  int bits_left = get_bits_left(gbc);
282  if (bits_left > 8)
283  return 1;
284  if (show_bits(gbc, bits_left) == 1 << (bits_left - 1))
285  return 0;
286  return 1;
287 }
288 
289 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
290 
291 #define byte_alignment(rw) (get_bits_count(rw) % 8)
292 
293 #define allocate(name, size) do { \
294  name ## _ref = av_buffer_allocz(size); \
295  if (!name ## _ref) \
296  return AVERROR(ENOMEM); \
297  name = name ## _ref->data; \
298  } while (0)
299 
300 #define FUNC(name) FUNC_H264(READWRITE, name)
302 #undef FUNC
303 
304 #define FUNC(name) FUNC_H265(READWRITE, name)
306 #undef FUNC
307 
308 #undef READ
309 #undef READWRITE
310 #undef RWContext
311 #undef xu
312 #undef xue
313 #undef xse
314 #undef u
315 #undef flag
316 #undef ue
317 #undef se
318 #undef infer
319 #undef more_rbsp_data
320 #undef byte_alignment
321 #undef allocate
322 
323 
324 #define WRITE
325 #define READWRITE write
326 #define RWContext PutBitContext
327 
328 #define xu(width, name, var, range_min, range_max) do { \
329  uint32_t value = var; \
330  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
331  value, range_min, range_max)); \
332  } while (0)
333 #define xue(name, var, range_min, range_max) do { \
334  uint32_t value = var; \
335  CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
336  value, range_min, range_max)); \
337  } while (0)
338 #define xse(name, var, range_min, range_max) do { \
339  int32_t value = var; \
340  CHECK(cbs_write_se_golomb(ctx, rw, #name, \
341  value, range_min, range_max)); \
342  } while (0)
343 
344 #define u(width, name, range_min, range_max) \
345  xu(width, name, current->name, range_min, range_max)
346 #define flag(name) u(1, name, 0, 1)
347 #define ue(name, range_min, range_max) \
348  xue(name, current->name, range_min, range_max)
349 #define se(name, range_min, range_max) \
350  xse(name, current->name, range_min, range_max)
351 
352 #define infer(name, value) do { \
353  if (current->name != (value)) { \
354  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
355  "%s does not match inferred value: " \
356  "%"PRId64", but should be %"PRId64".\n", \
357  #name, (int64_t)current->name, (int64_t)(value)); \
358  } \
359  } while (0)
360 
361 #define more_rbsp_data(var) (var)
362 
363 #define byte_alignment(rw) (put_bits_count(rw) % 8)
364 
365 #define allocate(name, size) do { \
366  if (!name) { \
367  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
368  "for writing.\n", #name); \
369  return AVERROR_INVALIDDATA; \
370  } \
371  } while (0)
372 
373 #define FUNC(name) FUNC_H264(READWRITE, name)
375 #undef FUNC
376 
377 #define FUNC(name) FUNC_H265(READWRITE, name)
379 #undef FUNC
380 
381 #undef WRITE
382 #undef READWRITE
383 #undef RWContext
384 #undef xu
385 #undef xue
386 #undef xse
387 #undef u
388 #undef flag
389 #undef ue
390 #undef se
391 #undef infer
392 #undef more_rbsp_data
393 #undef byte_alignment
394 #undef allocate
395 
396 
397 static void cbs_h264_free_pps(void *unit, uint8_t *content)
398 {
399  H264RawPPS *pps = (H264RawPPS*)content;
401  av_freep(&content);
402 }
403 
405 {
406  switch (payload->payload_type) {
411  break;
414  break;
417  break;
418  default:
419  av_buffer_unref(&payload->payload.other.data_ref);
420  break;
421  }
422 }
423 
424 static void cbs_h264_free_sei(void *unit, uint8_t *content)
425 {
426  H264RawSEI *sei = (H264RawSEI*)content;
427  int i;
428  for (i = 0; i < sei->payload_count; i++)
430  av_freep(&content);
431 }
432 
433 static void cbs_h264_free_slice(void *unit, uint8_t *content)
434 {
435  H264RawSlice *slice = (H264RawSlice*)content;
436  av_buffer_unref(&slice->data_ref);
437  av_freep(&content);
438 }
439 
440 static void cbs_h265_free_vps(void *unit, uint8_t *content)
441 {
442  H265RawVPS *vps = (H265RawVPS*)content;
444  av_freep(&content);
445 }
446 
447 static void cbs_h265_free_sps(void *unit, uint8_t *content)
448 {
449  H265RawSPS *sps = (H265RawSPS*)content;
451  av_freep(&content);
452 }
453 
454 static void cbs_h265_free_pps(void *unit, uint8_t *content)
455 {
456  H265RawPPS *pps = (H265RawPPS*)content;
458  av_freep(&content);
459 }
460 
461 static void cbs_h265_free_slice(void *unit, uint8_t *content)
462 {
463  H265RawSlice *slice = (H265RawSlice*)content;
464  av_buffer_unref(&slice->data_ref);
465  av_freep(&content);
466 }
467 
470  const H2645Packet *packet)
471 {
472  int err, i;
473 
474  for (i = 0; i < packet->nb_nals; i++) {
475  const H2645NAL *nal = &packet->nals[i];
476  size_t size = nal->size;
477  uint8_t *data;
478 
479  // Remove trailing zeroes.
480  while (size > 0 && nal->data[size - 1] == 0)
481  --size;
482  av_assert0(size > 0);
483 
485  if (!data)
486  return AVERROR(ENOMEM);
487  memcpy(data, nal->data, size);
488  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
489 
490  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
491  data, size, NULL);
492  if (err < 0) {
493  av_freep(&data);
494  return err;
495  }
496  }
497 
498  return 0;
499 }
500 
503  int header)
504 {
505  enum AVCodecID codec_id = ctx->codec->codec_id;
507  GetByteContext gbc;
508  int err;
509 
510  av_assert0(frag->data && frag->nb_units == 0);
511  if (frag->data_size == 0)
512  return 0;
513 
514  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
515  // AVCC header.
516  size_t size, start, end;
517  int i, count, version;
518 
519  priv->mp4 = 1;
520 
521  bytestream2_init(&gbc, frag->data, frag->data_size);
522 
523  if (bytestream2_get_bytes_left(&gbc) < 6)
524  return AVERROR_INVALIDDATA;
525 
526  version = bytestream2_get_byte(&gbc);
527  if (version != 1) {
528  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
529  "first byte %u.", version);
530  return AVERROR_INVALIDDATA;
531  }
532 
533  bytestream2_skip(&gbc, 3);
534  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
535 
536  // SPS array.
537  count = bytestream2_get_byte(&gbc) & 0x1f;
538  start = bytestream2_tell(&gbc);
539  for (i = 0; i < count; i++) {
540  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
541  return AVERROR_INVALIDDATA;
542  size = bytestream2_get_be16(&gbc);
543  if (bytestream2_get_bytes_left(&gbc) < size)
544  return AVERROR_INVALIDDATA;
545  bytestream2_skip(&gbc, size);
546  }
547  end = bytestream2_tell(&gbc);
548 
549  err = ff_h2645_packet_split(&priv->read_packet,
550  frag->data + start, end - start,
551  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
552  if (err < 0) {
553  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
554  return err;
555  }
556  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
557  if (err < 0)
558  return err;
559 
560  // PPS array.
561  count = bytestream2_get_byte(&gbc);
562  start = bytestream2_tell(&gbc);
563  for (i = 0; i < count; i++) {
564  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
565  return AVERROR_INVALIDDATA;
566  size = bytestream2_get_be16(&gbc);
567  if (bytestream2_get_bytes_left(&gbc) < size)
568  return AVERROR_INVALIDDATA;
569  bytestream2_skip(&gbc, size);
570  }
571  end = bytestream2_tell(&gbc);
572 
573  err = ff_h2645_packet_split(&priv->read_packet,
574  frag->data + start, end - start,
575  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
576  if (err < 0) {
577  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
578  return err;
579  }
580  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
581  if (err < 0)
582  return err;
583 
584  if (bytestream2_get_bytes_left(&gbc) > 0) {
585  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
586  "header.\n", bytestream2_get_bytes_left(&gbc));
587  }
588 
589  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
590  // HVCC header.
591  size_t size, start, end;
592  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
593 
594  priv->mp4 = 1;
595 
596  bytestream2_init(&gbc, frag->data, frag->data_size);
597 
598  if (bytestream2_get_bytes_left(&gbc) < 23)
599  return AVERROR_INVALIDDATA;
600 
601  version = bytestream2_get_byte(&gbc);
602  if (version != 1) {
603  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
604  "first byte %u.", version);
605  return AVERROR_INVALIDDATA;
606  }
607 
608  bytestream2_skip(&gbc, 20);
609  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
610 
611  nb_arrays = bytestream2_get_byte(&gbc);
612  for (i = 0; i < nb_arrays; i++) {
613  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
614  nb_nals = bytestream2_get_be16(&gbc);
615 
616  start = bytestream2_tell(&gbc);
617  for (j = 0; j < nb_nals; j++) {
618  if (bytestream2_get_bytes_left(&gbc) < 2)
619  return AVERROR_INVALIDDATA;
620  size = bytestream2_get_be16(&gbc);
621  if (bytestream2_get_bytes_left(&gbc) < size)
622  return AVERROR_INVALIDDATA;
623  bytestream2_skip(&gbc, size);
624  }
625  end = bytestream2_tell(&gbc);
626 
627  err = ff_h2645_packet_split(&priv->read_packet,
628  frag->data + start, end - start,
629  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
630  if (err < 0) {
631  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
632  "HVCC array %d (%d NAL units of type %d).\n",
633  i, nb_nals, nal_unit_type);
634  return err;
635  }
636  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
637  if (err < 0)
638  return err;
639  }
640 
641  } else {
642  // Annex B, or later MP4 with already-known parameters.
643 
644  err = ff_h2645_packet_split(&priv->read_packet,
645  frag->data, frag->data_size,
646  ctx->log_ctx,
647  priv->mp4, priv->nal_length_size,
648  codec_id, 1);
649  if (err < 0)
650  return err;
651 
652  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
653  if (err < 0)
654  return err;
655  }
656 
657  return 0;
658 }
659 
660 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
661 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
662  const H26 ## h26n ## Raw ## ps_name *ps_var) \
663 { \
664  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
665  unsigned int id = ps_var->id_element; \
666  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
667  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
668  " id : %d.\n", id); \
669  return AVERROR_INVALIDDATA; \
670  } \
671  if (priv->ps_var[id] == priv->active_ ## ps_var) \
672  priv->active_ ## ps_var = NULL ; \
673  av_freep(&priv->ps_var[id]); \
674  priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
675  if (!priv->ps_var[id]) \
676  return AVERROR(ENOMEM); \
677  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
678  return 0; \
679 }
680 
681 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
682 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
683 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
684 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
685 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
686 
687 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
688  CodedBitstreamUnit *unit)
689 {
690  GetBitContext gbc;
691  int err;
692 
693  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
694  if (err < 0)
695  return err;
696 
697  switch (unit->type) {
698  case H264_NAL_SPS:
699  {
700  H264RawSPS *sps;
701 
702  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
703  if (err < 0)
704  return err;
705  sps = unit->content;
706 
707  err = cbs_h264_read_sps(ctx, &gbc, sps);
708  if (err < 0)
709  return err;
710 
711  err = cbs_h264_replace_sps(ctx, sps);
712  if (err < 0)
713  return err;
714  }
715  break;
716 
717  case H264_NAL_SPS_EXT:
718  {
719  err = ff_cbs_alloc_unit_content(ctx, unit,
720  sizeof(H264RawSPSExtension),
721  NULL);
722  if (err < 0)
723  return err;
724 
725  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
726  if (err < 0)
727  return err;
728  }
729  break;
730 
731  case H264_NAL_PPS:
732  {
733  H264RawPPS *pps;
734 
735  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
737  if (err < 0)
738  return err;
739  pps = unit->content;
740 
741  err = cbs_h264_read_pps(ctx, &gbc, pps);
742  if (err < 0)
743  return err;
744 
745  err = cbs_h264_replace_pps(ctx, pps);
746  if (err < 0)
747  return err;
748  }
749  break;
750 
751  case H264_NAL_SLICE:
752  case H264_NAL_IDR_SLICE:
754  {
755  H264RawSlice *slice;
756  int pos, len;
757 
758  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
760  if (err < 0)
761  return err;
762  slice = unit->content;
763 
764  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
765  if (err < 0)
766  return err;
767 
768  pos = get_bits_count(&gbc);
769  len = unit->data_size;
770  if (!unit->data[len - 1]) {
771  int z;
772  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
773  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
774  "from slice data.\n", z);
775  len -= z;
776  }
777 
778  slice->data_size = len - pos / 8;
779  slice->data_ref = av_buffer_alloc(slice->data_size +
781  if (!slice->data_ref)
782  return AVERROR(ENOMEM);
783  slice->data = slice->data_ref->data;
784  memcpy(slice->data,
785  unit->data + pos / 8, slice->data_size);
786  memset(slice->data + slice->data_size, 0,
788  slice->data_bit_start = pos % 8;
789  }
790  break;
791 
792  case H264_NAL_AUD:
793  {
794  err = ff_cbs_alloc_unit_content(ctx, unit,
795  sizeof(H264RawAUD), NULL);
796  if (err < 0)
797  return err;
798 
799  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
800  if (err < 0)
801  return err;
802  }
803  break;
804 
805  case H264_NAL_SEI:
806  {
807  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
809  if (err < 0)
810  return err;
811 
812  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
813  if (err < 0)
814  return err;
815  }
816  break;
817 
819  {
820  err = ff_cbs_alloc_unit_content(ctx, unit,
821  sizeof(H264RawFiller), NULL);
822  if (err < 0)
823  return err;
824 
825  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
826  if (err < 0)
827  return err;
828  }
829  break;
830 
831  default:
832  return AVERROR(ENOSYS);
833  }
834 
835  return 0;
836 }
837 
839  CodedBitstreamUnit *unit)
840 {
841  GetBitContext gbc;
842  int err;
843 
844  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
845  if (err < 0)
846  return err;
847 
848  switch (unit->type) {
849  case HEVC_NAL_VPS:
850  {
851  H265RawVPS *vps;
852 
853  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
855  if (err < 0)
856  return err;
857  vps = unit->content;
858 
859  err = cbs_h265_read_vps(ctx, &gbc, vps);
860  if (err < 0)
861  return err;
862 
863  err = cbs_h265_replace_vps(ctx, vps);
864  if (err < 0)
865  return err;
866  }
867  break;
868  case HEVC_NAL_SPS:
869  {
870  H265RawSPS *sps;
871 
872  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
874  if (err < 0)
875  return err;
876  sps = unit->content;
877 
878  err = cbs_h265_read_sps(ctx, &gbc, sps);
879  if (err < 0)
880  return err;
881 
882  err = cbs_h265_replace_sps(ctx, sps);
883  if (err < 0)
884  return err;
885  }
886  break;
887 
888  case HEVC_NAL_PPS:
889  {
890  H265RawPPS *pps;
891 
892  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
894  if (err < 0)
895  return err;
896  pps = unit->content;
897 
898  err = cbs_h265_read_pps(ctx, &gbc, pps);
899  if (err < 0)
900  return err;
901 
902  err = cbs_h265_replace_pps(ctx, pps);
903  if (err < 0)
904  return err;
905  }
906  break;
907 
908  case HEVC_NAL_TRAIL_N:
909  case HEVC_NAL_TRAIL_R:
910  case HEVC_NAL_TSA_N:
911  case HEVC_NAL_TSA_R:
912  case HEVC_NAL_STSA_N:
913  case HEVC_NAL_STSA_R:
914  case HEVC_NAL_RADL_N:
915  case HEVC_NAL_RADL_R:
916  case HEVC_NAL_RASL_N:
917  case HEVC_NAL_RASL_R:
918  case HEVC_NAL_BLA_W_LP:
919  case HEVC_NAL_BLA_W_RADL:
920  case HEVC_NAL_BLA_N_LP:
921  case HEVC_NAL_IDR_W_RADL:
922  case HEVC_NAL_IDR_N_LP:
923  case HEVC_NAL_CRA_NUT:
924  {
925  H265RawSlice *slice;
926  int pos, len;
927 
928  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
930  if (err < 0)
931  return err;
932  slice = unit->content;
933 
934  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
935  if (err < 0)
936  return err;
937 
938  pos = get_bits_count(&gbc);
939  len = unit->data_size;
940  if (!unit->data[len - 1]) {
941  int z;
942  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
943  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
944  "from slice data.\n", z);
945  len -= z;
946  }
947 
948  slice->data_size = len - pos / 8;
949  slice->data_ref = av_buffer_alloc(slice->data_size +
951  if (!slice->data_ref)
952  return AVERROR(ENOMEM);
953  slice->data = slice->data_ref->data;
954  memcpy(slice->data,
955  unit->data + pos / 8, slice->data_size);
956  memset(slice->data + slice->data_size, 0,
958  slice->data_bit_start = pos % 8;
959  }
960  break;
961 
962  case HEVC_NAL_AUD:
963  {
964  err = ff_cbs_alloc_unit_content(ctx, unit,
965  sizeof(H265RawAUD), NULL);
966  if (err < 0)
967  return err;
968 
969  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
970  if (err < 0)
971  return err;
972  }
973  break;
974 
975  default:
976  return AVERROR(ENOSYS);
977  }
978 
979  return 0;
980 }
981 
983  CodedBitstreamUnit *unit,
984  PutBitContext *pbc)
985 {
986  int err;
987 
988  switch (unit->type) {
989  case H264_NAL_SPS:
990  {
991  H264RawSPS *sps = unit->content;
992 
993  err = cbs_h264_write_sps(ctx, pbc, sps);
994  if (err < 0)
995  return err;
996 
997  err = cbs_h264_replace_sps(ctx, sps);
998  if (err < 0)
999  return err;
1000  }
1001  break;
1002 
1003  case H264_NAL_SPS_EXT:
1004  {
1005  H264RawSPSExtension *sps_ext = unit->content;
1006 
1007  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1008  if (err < 0)
1009  return err;
1010  }
1011  break;
1012 
1013  case H264_NAL_PPS:
1014  {
1015  H264RawPPS *pps = unit->content;
1016 
1017  err = cbs_h264_write_pps(ctx, pbc, pps);
1018  if (err < 0)
1019  return err;
1020 
1021  err = cbs_h264_replace_pps(ctx, pps);
1022  if (err < 0)
1023  return err;
1024  }
1025  break;
1026 
1027  case H264_NAL_SLICE:
1028  case H264_NAL_IDR_SLICE:
1030  {
1031  H264RawSlice *slice = unit->content;
1032  GetBitContext gbc;
1033  int bits_left, end, zeroes;
1034 
1035  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1036  if (err < 0)
1037  return err;
1038 
1039  if (slice->data) {
1040  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1041  return AVERROR(ENOSPC);
1042 
1043  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1044  skip_bits_long(&gbc, slice->data_bit_start);
1045 
1046  // Copy in two-byte blocks, but stop before copying the
1047  // rbsp_stop_one_bit in the final byte.
1048  while (get_bits_left(&gbc) > 23)
1049  put_bits(pbc, 16, get_bits(&gbc, 16));
1050 
1051  bits_left = get_bits_left(&gbc);
1052  end = get_bits(&gbc, bits_left);
1053 
1054  // rbsp_stop_one_bit must be present here.
1055  av_assert0(end);
1056  zeroes = ff_ctz(end);
1057  if (bits_left > zeroes + 1)
1058  put_bits(pbc, bits_left - zeroes - 1,
1059  end >> (zeroes + 1));
1060  put_bits(pbc, 1, 1);
1061  while (put_bits_count(pbc) % 8 != 0)
1062  put_bits(pbc, 1, 0);
1063  } else {
1064  // No slice data - that was just the header.
1065  // (Bitstream may be unaligned!)
1066  }
1067  }
1068  break;
1069 
1070  case H264_NAL_AUD:
1071  {
1072  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1073  if (err < 0)
1074  return err;
1075  }
1076  break;
1077 
1078  case H264_NAL_SEI:
1079  {
1080  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1081  if (err < 0)
1082  return err;
1083  }
1084  break;
1085 
1086  case H264_NAL_FILLER_DATA:
1087  {
1088  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1089  if (err < 0)
1090  return err;
1091  }
1092  break;
1093 
1094  default:
1095  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1096  "NAL unit type %"PRIu32".\n", unit->type);
1097  return AVERROR_PATCHWELCOME;
1098  }
1099 
1100  return 0;
1101 }
1102 
1104  CodedBitstreamUnit *unit,
1105  PutBitContext *pbc)
1106 {
1107  int err;
1108 
1109  switch (unit->type) {
1110  case HEVC_NAL_VPS:
1111  {
1112  H265RawVPS *vps = unit->content;
1113 
1114  err = cbs_h265_write_vps(ctx, pbc, vps);
1115  if (err < 0)
1116  return err;
1117 
1118  err = cbs_h265_replace_vps(ctx, vps);
1119  if (err < 0)
1120  return err;
1121  }
1122  break;
1123 
1124  case HEVC_NAL_SPS:
1125  {
1126  H265RawSPS *sps = unit->content;
1127 
1128  err = cbs_h265_write_sps(ctx, pbc, sps);
1129  if (err < 0)
1130  return err;
1131 
1132  err = cbs_h265_replace_sps(ctx, sps);
1133  if (err < 0)
1134  return err;
1135  }
1136  break;
1137 
1138  case HEVC_NAL_PPS:
1139  {
1140  H265RawPPS *pps = unit->content;
1141 
1142  err = cbs_h265_write_pps(ctx, pbc, pps);
1143  if (err < 0)
1144  return err;
1145 
1146  err = cbs_h265_replace_pps(ctx, pps);
1147  if (err < 0)
1148  return err;
1149  }
1150  break;
1151 
1152  case HEVC_NAL_TRAIL_N:
1153  case HEVC_NAL_TRAIL_R:
1154  case HEVC_NAL_TSA_N:
1155  case HEVC_NAL_TSA_R:
1156  case HEVC_NAL_STSA_N:
1157  case HEVC_NAL_STSA_R:
1158  case HEVC_NAL_RADL_N:
1159  case HEVC_NAL_RADL_R:
1160  case HEVC_NAL_RASL_N:
1161  case HEVC_NAL_RASL_R:
1162  case HEVC_NAL_BLA_W_LP:
1163  case HEVC_NAL_BLA_W_RADL:
1164  case HEVC_NAL_BLA_N_LP:
1165  case HEVC_NAL_IDR_W_RADL:
1166  case HEVC_NAL_IDR_N_LP:
1167  case HEVC_NAL_CRA_NUT:
1168  {
1169  H265RawSlice *slice = unit->content;
1170  GetBitContext gbc;
1171  int bits_left, end, zeroes;
1172 
1173  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1174  if (err < 0)
1175  return err;
1176 
1177  if (slice->data) {
1178  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1179  return AVERROR(ENOSPC);
1180 
1181  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1182  skip_bits_long(&gbc, slice->data_bit_start);
1183 
1184  // Copy in two-byte blocks, but stop before copying the
1185  // rbsp_stop_one_bit in the final byte.
1186  while (get_bits_left(&gbc) > 23)
1187  put_bits(pbc, 16, get_bits(&gbc, 16));
1188 
1189  bits_left = get_bits_left(&gbc);
1190  end = get_bits(&gbc, bits_left);
1191 
1192  // rbsp_stop_one_bit must be present here.
1193  av_assert0(end);
1194  zeroes = ff_ctz(end);
1195  if (bits_left > zeroes + 1)
1196  put_bits(pbc, bits_left - zeroes - 1,
1197  end >> (zeroes + 1));
1198  put_bits(pbc, 1, 1);
1199  while (put_bits_count(pbc) % 8 != 0)
1200  put_bits(pbc, 1, 0);
1201  } else {
1202  // No slice data - that was just the header.
1203  }
1204  }
1205  break;
1206 
1207  case HEVC_NAL_AUD:
1208  {
1209  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1210  if (err < 0)
1211  return err;
1212  }
1213  break;
1214 
1215  default:
1216  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1217  "NAL unit type %"PRIu32".\n", unit->type);
1218  return AVERROR_PATCHWELCOME;
1219  }
1220 
1221  return 0;
1222 }
1223 
1225  CodedBitstreamUnit *unit)
1226 {
1228  enum AVCodecID codec_id = ctx->codec->codec_id;
1229  PutBitContext pbc;
1230  int err;
1231 
1232  if (!priv->write_buffer) {
1233  // Initial write buffer size is 1MB.
1234  priv->write_buffer_size = 1024 * 1024;
1235 
1236  reallocate_and_try_again:
1237  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1238  if (err < 0) {
1239  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1240  "sufficiently large write buffer (last attempt "
1241  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1242  return err;
1243  }
1244  }
1245 
1246  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1247 
1248  if (codec_id == AV_CODEC_ID_H264)
1249  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1250  else
1251  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1252 
1253  if (err == AVERROR(ENOSPC)) {
1254  // Overflow.
1255  priv->write_buffer_size *= 2;
1256  goto reallocate_and_try_again;
1257  }
1258  // Overflow but we didn't notice.
1259  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1260 
1261  if (err < 0) {
1262  // Write failed for some other reason.
1263  return err;
1264  }
1265 
1266  if (put_bits_count(&pbc) % 8)
1267  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1268  else
1269  unit->data_bit_padding = 0;
1270 
1271  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1272  flush_put_bits(&pbc);
1273 
1274  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1275  if (err < 0)
1276  return err;
1277 
1278  memcpy(unit->data, priv->write_buffer, unit->data_size);
1279 
1280  return 0;
1281 }
1282 
1284  CodedBitstreamFragment *frag)
1285 {
1286  uint8_t *data;
1287  size_t max_size, dp, sp;
1288  int err, i, zero_run;
1289 
1290  for (i = 0; i < frag->nb_units; i++) {
1291  // Data should already all have been written when we get here.
1292  av_assert0(frag->units[i].data);
1293  }
1294 
1295  max_size = 0;
1296  for (i = 0; i < frag->nb_units; i++) {
1297  // Start code + content with worst-case emulation prevention.
1298  max_size += 3 + frag->units[i].data_size * 3 / 2;
1299  }
1300 
1301  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1302  if (!data)
1303  return AVERROR(ENOMEM);
1304 
1305  dp = 0;
1306  for (i = 0; i < frag->nb_units; i++) {
1307  CodedBitstreamUnit *unit = &frag->units[i];
1308 
1309  if (unit->data_bit_padding > 0) {
1310  if (i < frag->nb_units - 1)
1311  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1312  "unaligned padding on non-final NAL unit.\n");
1313  else
1314  frag->data_bit_padding = unit->data_bit_padding;
1315  }
1316 
1317  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1318  (unit->type == H264_NAL_SPS ||
1319  unit->type == H264_NAL_PPS)) ||
1320  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1321  (unit->type == HEVC_NAL_VPS ||
1322  unit->type == HEVC_NAL_SPS ||
1323  unit->type == HEVC_NAL_PPS)) ||
1324  i == 0 /* (Assume this is the start of an access unit.) */) {
1325  // zero_byte
1326  data[dp++] = 0;
1327  }
1328  // start_code_prefix_one_3bytes
1329  data[dp++] = 0;
1330  data[dp++] = 0;
1331  data[dp++] = 1;
1332 
1333  zero_run = 0;
1334  for (sp = 0; sp < unit->data_size; sp++) {
1335  if (zero_run < 2) {
1336  if (unit->data[sp] == 0)
1337  ++zero_run;
1338  else
1339  zero_run = 0;
1340  } else {
1341  if ((unit->data[sp] & ~3) == 0) {
1342  // emulation_prevention_three_byte
1343  data[dp++] = 3;
1344  }
1345  zero_run = unit->data[sp] == 0;
1346  }
1347  data[dp++] = unit->data[sp];
1348  }
1349  }
1350 
1351  av_assert0(dp <= max_size);
1352  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1353  if (err)
1354  return err;
1355  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1356 
1358  NULL, NULL, 0);
1359  if (!frag->data_ref) {
1360  av_freep(&data);
1361  return AVERROR(ENOMEM);
1362  }
1363 
1364  frag->data = data;
1365  frag->data_size = dp;
1366 
1367  return 0;
1368 }
1369 
1371 {
1372  CodedBitstreamH264Context *h264 = ctx->priv_data;
1373  int i;
1374 
1376 
1377  av_freep(&h264->common.write_buffer);
1378 
1379  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1380  av_freep(&h264->sps[i]);
1381  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1382  av_freep(&h264->pps[i]);
1383 }
1384 
1386 {
1387  CodedBitstreamH265Context *h265 = ctx->priv_data;
1388  int i;
1389 
1391 
1392  av_freep(&h265->common.write_buffer);
1393 
1394  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1395  av_freep(&h265->vps[i]);
1396  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1397  av_freep(&h265->sps[i]);
1398  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1399  av_freep(&h265->pps[i]);
1400 }
1401 
1404 
1405  .priv_data_size = sizeof(CodedBitstreamH264Context),
1406 
1407  .split_fragment = &cbs_h2645_split_fragment,
1408  .read_unit = &cbs_h264_read_nal_unit,
1409  .write_unit = &cbs_h2645_write_nal_unit,
1410  .assemble_fragment = &cbs_h2645_assemble_fragment,
1411 
1412  .close = &cbs_h264_close,
1413 };
1414 
1417 
1418  .priv_data_size = sizeof(CodedBitstreamH265Context),
1419 
1420  .split_fragment = &cbs_h2645_split_fragment,
1421  .read_unit = &cbs_h265_read_nal_unit,
1422  .write_unit = &cbs_h2645_write_nal_unit,
1423  .assemble_fragment = &cbs_h2645_assemble_fragment,
1424 
1425  .close = &cbs_h265_close,
1426 };
1427 
1430  const H264RawSEIPayload *payload)
1431 {
1432  H264RawSEI *sei;
1433  CodedBitstreamUnit *nal = NULL;
1434  int err, i;
1435 
1436  // Find an existing SEI NAL unit to add to.
1437  for (i = 0; i < au->nb_units; i++) {
1438  if (au->units[i].type == H264_NAL_SEI) {
1439  nal = &au->units[i];
1440  break;
1441  }
1442  }
1443  if (nal) {
1444  sei = nal->content;
1445 
1446  } else {
1447  // Need to make a new SEI NAL unit. Insert it before the first
1448  // slice data NAL unit; if no slice data, add at the end.
1449  AVBufferRef *sei_ref;
1450 
1451  sei = av_mallocz(sizeof(*sei));
1452  if (!sei)
1453  return AVERROR(ENOMEM);
1454 
1456  sei->nal_unit_header.nal_ref_idc = 0;
1457 
1458  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1459  &cbs_h264_free_sei, ctx, 0);
1460  if (!sei_ref) {
1461  av_freep(&sei);
1462  return AVERROR(ENOMEM);
1463  }
1464 
1465  for (i = 0; i < au->nb_units; i++) {
1466  if (au->units[i].type == H264_NAL_SLICE ||
1467  au->units[i].type == H264_NAL_IDR_SLICE)
1468  break;
1469  }
1470 
1471  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1472  sei, sei_ref);
1473  av_buffer_unref(&sei_ref);
1474  if (err < 0)
1475  return err;
1476  }
1477 
1478  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1479  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1480  "SEI NAL unit.\n");
1481  return AVERROR(EINVAL);
1482  }
1483 
1484  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1485  ++sei->payload_count;
1486 
1487  return 0;
1488 }
1489 
1492  CodedBitstreamUnit *nal,
1493  int position)
1494 {
1495  H264RawSEI *sei = nal->content;
1496 
1497  av_assert0(nal->type == H264_NAL_SEI);
1498  av_assert0(position >= 0 && position < sei->payload_count);
1499 
1500  if (position == 0 && sei->payload_count == 1) {
1501  // Deleting NAL unit entirely.
1502  int i;
1503 
1504  for (i = 0; i < au->nb_units; i++) {
1505  if (&au->units[i] == nal)
1506  break;
1507  }
1508  av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1509 
1510  return ff_cbs_delete_unit(ctx, au, i);
1511  } else {
1512  cbs_h264_free_sei_payload(&sei->payload[position]);
1513 
1514  --sei->payload_count;
1515  memmove(sei->payload + position,
1516  sei->payload + position + 1,
1517  (sei->payload_count - position) * sizeof(*sei->payload));
1518 
1519  return 0;
1520  }
1521 }
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1224
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)
Split an input packet into NAL units.
Definition: h2645_parse.c:263
uint32_t payload_type
Definition: cbs_h264.h:299
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:34
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define NULL
Definition: coverity.c:32
#define ff_ctz
Definition: intmath.h:106
int nb_units
Number of units in this fragment.
Definition: cbs.h:144
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
Definition: cbs_h265.h:528
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:125
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: h2645_parse.h:34
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:212
AVBufferRef * data_ref
Definition: cbs_h265.h:157
H264RawPPS * pps[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:425
uint8_t * data
Definition: cbs_h265.h:513
Sequence parameter set.
Definition: h264_ps.h:43
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:318
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:67
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1103
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:838
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:454
Picture parameter set.
Definition: h264_ps.h:108
int av_log2(unsigned v)
Definition: intmath.c:26
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:518
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:1283
struct H264RawSEIPayload::@42::@43 other
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
CodedBitstreamH2645Context common
Definition: cbs_h265.h:522
int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, const H264RawSEIPayload *payload)
Add an SEI message to an access unit.
Definition: cbs_h2645.c:1428
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:194
static void cbs_h264_free_pps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:397
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int data_bit_start
Definition: cbs_h265.h:515
unregistered user data
Definition: h264_sei.h:32
static void cbs_h265_free_slice(void *unit, uint8_t *content)
Definition: cbs_h2645.c:461
display orientation
Definition: h264_sei.h:35
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:85
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
Definition: cbs_h265.h:527
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:386
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
H264RawSPS * sps[H264_MAX_SPS_COUNT]
Definition: cbs_h264.h:424
#define sp
Definition: regdef.h:63
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1370
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:475
Coded bitstream unit structure.
Definition: cbs.h:63
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:99
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:150
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:385
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:74
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:555
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:127
H.264 common definitions.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:596
union H264RawSEIPayload::@42 payload
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
buffering period (H.264, D.1.1)
Definition: h264_sei.h:28
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void cbs_h265_free_pps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:454
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBufferRef * data_ref
Definition: cbs_h265.h:516
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
GLsizei count
Definition: opengl_enc.c:109
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:131
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
H264RawSEIUserDataUnregistered user_data_unregistered
Definition: cbs_h264.h:306
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:161
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
Definition: cbs_h2645.c:404
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1402
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1385
picture timing
Definition: h264_sei.h:29
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:193
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, int32_t value, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:179
AVFormatContext * ctx
Definition: movenc.c:48
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:304
int ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:592
enum AVCodecID codec_id
Definition: vaapi_decode.c:362
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:138
static void cbs_h265_free_vps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:440
size_t data_size
Definition: cbs_h264.h:406
int type
NAL unit type.
Definition: h2645_parse.h:51
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:120
#define FF_ARRAY_ELEMS(a)
H264RawSEIUserDataRegistered user_data_registered
Definition: cbs_h264.h:305
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
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:163
static void cbs_h264_free_slice(void *unit, uint8_t *content)
Definition: cbs_h2645.c:433
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:114
uint8_t * data
The data buffer.
Definition: buffer.h:89
const uint8_t * data
Definition: h2645_parse.h:35
AVBufferRef * slice_group_id_ref
Definition: cbs_h264.h:200
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:84
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:156
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:433
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
recovery point (frame # to decoder sync)
Definition: h264_sei.h:33
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:279
enum AVCodecID codec_id
Definition: cbs_internal.h:29
registered user data as specified by Rec. ITU-T T.35
Definition: h264_sei.h:31
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
Definition: cbs_h264.h:320
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *name, const char *bits, int64_t value)
Definition: cbs.c:354
#define SIZE_SPECIFIER
Definition: internal.h:262
void * priv_data
Internal codec-specific data.
Definition: cbs.h:177
AVBufferRef * data_ref
Definition: cbs_h264.h:408
A reference to a data buffer.
Definition: buffer.h:81
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:297
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1415
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
int data_bit_start
Definition: cbs_h264.h:407
H265RawSliceHeader header
Definition: cbs_h265.h:511
H264RawSliceHeader header
Definition: cbs_h264.h:403
int len
H2645NAL * nals
Definition: h2645_parse.h:75
int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, CodedBitstreamUnit *nal, int position)
Delete an SEI message from an access unit.
Definition: cbs_h2645.c:1490
size_t data_size
Definition: cbs_h265.h:514
CodedBitstreamH2645Context common
Definition: cbs_h264.h:420
uint8_t nal_ref_idc
Definition: cbs_h264.h:42
#define av_freep(p)
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:982
void INT64 start
Definition: avisynth_c.h:690
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:166
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
Definition: cbs_h2645.c:660
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:501
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:526
AVBufferRef * data_ref
If data is reference counted, a reference to the buffer containing data.
Definition: cbs.h:136
exp golomb vlc stuff
uint8_t nal_unit_type
Definition: cbs_h264.h:43
uint8_t * data
Definition: cbs_h264.h:405
uint8_t payload_count
Definition: cbs_h264.h:321
static void cbs_h264_free_sei(void *unit, uint8_t *content)
Definition: cbs_h2645.c:424
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:79
const char * name
Definition: opengl_enc.c:103
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition: cbs_h2645.c:468
static void cbs_h265_free_sps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:447