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