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  AV_INPUT_BUFFER_PADDING_SIZE); \
324  if (!name ## _ref) \
325  return AVERROR(ENOMEM); \
326  name = name ## _ref->data; \
327  } while (0)
328 
329 #define FUNC(name) FUNC_H264(READWRITE, name)
331 #undef FUNC
332 
333 #define FUNC(name) FUNC_H265(READWRITE, name)
335 #undef FUNC
336 
337 #undef READ
338 #undef READWRITE
339 #undef RWContext
340 #undef xu
341 #undef xue
342 #undef xse
343 #undef infer
344 #undef more_rbsp_data
345 #undef byte_alignment
346 #undef allocate
347 
348 
349 #define WRITE
350 #define READWRITE write
351 #define RWContext PutBitContext
352 
353 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
354  uint32_t value = var; \
355  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
356  SUBSCRIPTS(subs, __VA_ARGS__), \
357  value, range_min, range_max)); \
358  } while (0)
359 #define xue(name, var, range_min, range_max, subs, ...) do { \
360  uint32_t value = var; \
361  CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
362  SUBSCRIPTS(subs, __VA_ARGS__), \
363  value, range_min, range_max)); \
364  } while (0)
365 #define xse(name, var, range_min, range_max, subs, ...) do { \
366  int32_t value = var; \
367  CHECK(cbs_write_se_golomb(ctx, rw, #name, \
368  SUBSCRIPTS(subs, __VA_ARGS__), \
369  value, range_min, range_max)); \
370  } while (0)
371 
372 #define infer(name, value) do { \
373  if (current->name != (value)) { \
374  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
375  "%s does not match inferred value: " \
376  "%"PRId64", but should be %"PRId64".\n", \
377  #name, (int64_t)current->name, (int64_t)(value)); \
378  } \
379  } while (0)
380 
381 #define more_rbsp_data(var) (var)
382 
383 #define byte_alignment(rw) (put_bits_count(rw) % 8)
384 
385 #define allocate(name, size) do { \
386  if (!name) { \
387  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
388  "for writing.\n", #name); \
389  return AVERROR_INVALIDDATA; \
390  } \
391  } while (0)
392 
393 #define FUNC(name) FUNC_H264(READWRITE, name)
395 #undef FUNC
396 
397 #define FUNC(name) FUNC_H265(READWRITE, name)
399 #undef FUNC
400 
401 #undef WRITE
402 #undef READWRITE
403 #undef RWContext
404 #undef xu
405 #undef xue
406 #undef xse
407 #undef u
408 #undef flag
409 #undef ue
410 #undef se
411 #undef infer
412 #undef more_rbsp_data
413 #undef byte_alignment
414 #undef allocate
415 
416 
417 static void cbs_h264_free_pps(void *unit, uint8_t *content)
418 {
419  H264RawPPS *pps = (H264RawPPS*)content;
421  av_freep(&content);
422 }
423 
425 {
426  switch (payload->payload_type) {
433  break;
436  break;
439  break;
440  default:
441  av_buffer_unref(&payload->payload.other.data_ref);
442  break;
443  }
444 }
445 
446 static void cbs_h264_free_sei(void *unit, uint8_t *content)
447 {
448  H264RawSEI *sei = (H264RawSEI*)content;
449  int i;
450  for (i = 0; i < sei->payload_count; i++)
452  av_freep(&content);
453 }
454 
455 static void cbs_h264_free_slice(void *unit, uint8_t *content)
456 {
457  H264RawSlice *slice = (H264RawSlice*)content;
458  av_buffer_unref(&slice->data_ref);
459  av_freep(&content);
460 }
461 
462 static void cbs_h265_free_vps(void *unit, uint8_t *content)
463 {
464  H265RawVPS *vps = (H265RawVPS*)content;
466  av_freep(&content);
467 }
468 
469 static void cbs_h265_free_sps(void *unit, uint8_t *content)
470 {
471  H265RawSPS *sps = (H265RawSPS*)content;
473  av_freep(&content);
474 }
475 
476 static void cbs_h265_free_pps(void *unit, uint8_t *content)
477 {
478  H265RawPPS *pps = (H265RawPPS*)content;
480  av_freep(&content);
481 }
482 
483 static void cbs_h265_free_slice(void *unit, uint8_t *content)
484 {
485  H265RawSlice *slice = (H265RawSlice*)content;
486  av_buffer_unref(&slice->data_ref);
487  av_freep(&content);
488 }
489 
491 {
492  switch (payload->payload_type) {
495  break;
496  default:
497  av_buffer_unref(&payload->payload.other.data_ref);
498  break;
499  }
500 }
501 
502 static void cbs_h265_free_sei(void *unit, uint8_t *content)
503 {
504  H265RawSEI *sei = (H265RawSEI*)content;
505  int i;
506  for (i = 0; i < sei->payload_count; i++)
508  av_freep(&content);
509 }
510 
513  const H2645Packet *packet)
514 {
515  int err, i;
516 
517  for (i = 0; i < packet->nb_nals; i++) {
518  const H2645NAL *nal = &packet->nals[i];
519  size_t size = nal->size;
520  uint8_t *data;
521 
522  // Remove trailing zeroes.
523  while (size > 0 && nal->data[size - 1] == 0)
524  --size;
525  av_assert0(size > 0);
526 
528  if (!data)
529  return AVERROR(ENOMEM);
530  memcpy(data, nal->data, size);
531  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
532 
533  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
534  data, size, NULL);
535  if (err < 0) {
536  av_freep(&data);
537  return err;
538  }
539  }
540 
541  return 0;
542 }
543 
546  int header)
547 {
548  enum AVCodecID codec_id = ctx->codec->codec_id;
550  GetByteContext gbc;
551  int err;
552 
553  av_assert0(frag->data && frag->nb_units == 0);
554  if (frag->data_size == 0)
555  return 0;
556 
557  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
558  // AVCC header.
559  size_t size, start, end;
560  int i, count, version;
561 
562  priv->mp4 = 1;
563 
564  bytestream2_init(&gbc, frag->data, frag->data_size);
565 
566  if (bytestream2_get_bytes_left(&gbc) < 6)
567  return AVERROR_INVALIDDATA;
568 
569  version = bytestream2_get_byte(&gbc);
570  if (version != 1) {
571  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
572  "first byte %u.", version);
573  return AVERROR_INVALIDDATA;
574  }
575 
576  bytestream2_skip(&gbc, 3);
577  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
578 
579  // SPS array.
580  count = bytestream2_get_byte(&gbc) & 0x1f;
581  start = bytestream2_tell(&gbc);
582  for (i = 0; i < count; i++) {
583  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
584  return AVERROR_INVALIDDATA;
585  size = bytestream2_get_be16(&gbc);
586  if (bytestream2_get_bytes_left(&gbc) < size)
587  return AVERROR_INVALIDDATA;
588  bytestream2_skip(&gbc, size);
589  }
590  end = bytestream2_tell(&gbc);
591 
592  err = ff_h2645_packet_split(&priv->read_packet,
593  frag->data + start, end - start,
594  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
595  if (err < 0) {
596  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
597  return err;
598  }
599  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
600  if (err < 0)
601  return err;
602 
603  // PPS array.
604  count = bytestream2_get_byte(&gbc);
605  start = bytestream2_tell(&gbc);
606  for (i = 0; i < count; i++) {
607  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
608  return AVERROR_INVALIDDATA;
609  size = bytestream2_get_be16(&gbc);
610  if (bytestream2_get_bytes_left(&gbc) < size)
611  return AVERROR_INVALIDDATA;
612  bytestream2_skip(&gbc, size);
613  }
614  end = bytestream2_tell(&gbc);
615 
616  err = ff_h2645_packet_split(&priv->read_packet,
617  frag->data + start, end - start,
618  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
619  if (err < 0) {
620  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
621  return err;
622  }
623  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
624  if (err < 0)
625  return err;
626 
627  if (bytestream2_get_bytes_left(&gbc) > 0) {
628  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
629  "header.\n", bytestream2_get_bytes_left(&gbc));
630  }
631 
632  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
633  // HVCC header.
634  size_t size, start, end;
635  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
636 
637  priv->mp4 = 1;
638 
639  bytestream2_init(&gbc, frag->data, frag->data_size);
640 
641  if (bytestream2_get_bytes_left(&gbc) < 23)
642  return AVERROR_INVALIDDATA;
643 
644  version = bytestream2_get_byte(&gbc);
645  if (version != 1) {
646  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
647  "first byte %u.", version);
648  return AVERROR_INVALIDDATA;
649  }
650 
651  bytestream2_skip(&gbc, 20);
652  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
653 
654  nb_arrays = bytestream2_get_byte(&gbc);
655  for (i = 0; i < nb_arrays; i++) {
656  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
657  nb_nals = bytestream2_get_be16(&gbc);
658 
659  start = bytestream2_tell(&gbc);
660  for (j = 0; j < nb_nals; j++) {
661  if (bytestream2_get_bytes_left(&gbc) < 2)
662  return AVERROR_INVALIDDATA;
663  size = bytestream2_get_be16(&gbc);
664  if (bytestream2_get_bytes_left(&gbc) < size)
665  return AVERROR_INVALIDDATA;
666  bytestream2_skip(&gbc, size);
667  }
668  end = bytestream2_tell(&gbc);
669 
670  err = ff_h2645_packet_split(&priv->read_packet,
671  frag->data + start, end - start,
672  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
673  if (err < 0) {
674  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
675  "HVCC array %d (%d NAL units of type %d).\n",
676  i, nb_nals, nal_unit_type);
677  return err;
678  }
679  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
680  if (err < 0)
681  return err;
682  }
683 
684  } else {
685  // Annex B, or later MP4 with already-known parameters.
686 
687  err = ff_h2645_packet_split(&priv->read_packet,
688  frag->data, frag->data_size,
689  ctx->log_ctx,
690  priv->mp4, priv->nal_length_size,
691  codec_id, 1);
692  if (err < 0)
693  return err;
694 
695  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
696  if (err < 0)
697  return err;
698  }
699 
700  return 0;
701 }
702 
703 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
704 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
705  CodedBitstreamUnit *unit) \
706 { \
707  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
708  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
709  unsigned int id = ps_var->id_element; \
710  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
711  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
712  " id : %d.\n", id); \
713  return AVERROR_INVALIDDATA; \
714  } \
715  if (priv->ps_var[id] == priv->active_ ## ps_var) \
716  priv->active_ ## ps_var = NULL ; \
717  av_buffer_unref(&priv->ps_var ## _ref[id]); \
718  if (unit->content_ref) \
719  priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
720  else \
721  priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
722  if (!priv->ps_var ## _ref[id]) \
723  return AVERROR(ENOMEM); \
724  priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
725  if (!unit->content_ref) \
726  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
727  return 0; \
728 }
729 
730 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
731 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
732 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
733 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
734 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
735 
736 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
737  CodedBitstreamUnit *unit)
738 {
739  GetBitContext gbc;
740  int err;
741 
742  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
743  if (err < 0)
744  return err;
745 
746  switch (unit->type) {
747  case H264_NAL_SPS:
748  {
749  H264RawSPS *sps;
750 
751  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
752  if (err < 0)
753  return err;
754  sps = unit->content;
755 
756  err = cbs_h264_read_sps(ctx, &gbc, sps);
757  if (err < 0)
758  return err;
759 
760  err = cbs_h264_replace_sps(ctx, unit);
761  if (err < 0)
762  return err;
763  }
764  break;
765 
766  case H264_NAL_SPS_EXT:
767  {
768  err = ff_cbs_alloc_unit_content(ctx, unit,
769  sizeof(H264RawSPSExtension),
770  NULL);
771  if (err < 0)
772  return err;
773 
774  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
775  if (err < 0)
776  return err;
777  }
778  break;
779 
780  case H264_NAL_PPS:
781  {
782  H264RawPPS *pps;
783 
784  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
786  if (err < 0)
787  return err;
788  pps = unit->content;
789 
790  err = cbs_h264_read_pps(ctx, &gbc, pps);
791  if (err < 0)
792  return err;
793 
794  err = cbs_h264_replace_pps(ctx, unit);
795  if (err < 0)
796  return err;
797  }
798  break;
799 
800  case H264_NAL_SLICE:
801  case H264_NAL_IDR_SLICE:
803  {
804  H264RawSlice *slice;
805  int pos, len;
806 
807  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
809  if (err < 0)
810  return err;
811  slice = unit->content;
812 
813  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
814  if (err < 0)
815  return err;
816 
817  pos = get_bits_count(&gbc);
818  len = unit->data_size;
819  if (!unit->data[len - 1]) {
820  int z;
821  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
822  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
823  "from slice data.\n", z);
824  len -= z;
825  }
826 
827  slice->data_size = len - pos / 8;
828  slice->data_ref = av_buffer_ref(unit->data_ref);
829  if (!slice->data_ref)
830  return AVERROR(ENOMEM);
831  slice->data = unit->data + pos / 8;
832  slice->data_bit_start = pos % 8;
833  }
834  break;
835 
836  case H264_NAL_AUD:
837  {
838  err = ff_cbs_alloc_unit_content(ctx, unit,
839  sizeof(H264RawAUD), NULL);
840  if (err < 0)
841  return err;
842 
843  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
844  if (err < 0)
845  return err;
846  }
847  break;
848 
849  case H264_NAL_SEI:
850  {
851  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
853  if (err < 0)
854  return err;
855 
856  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
857  if (err < 0)
858  return err;
859  }
860  break;
861 
863  {
864  err = ff_cbs_alloc_unit_content(ctx, unit,
865  sizeof(H264RawFiller), NULL);
866  if (err < 0)
867  return err;
868 
869  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
870  if (err < 0)
871  return err;
872  }
873  break;
874 
876  case H264_NAL_END_STREAM:
877  {
878  err = ff_cbs_alloc_unit_content(ctx, unit,
879  sizeof(H264RawNALUnitHeader),
880  NULL);
881  if (err < 0)
882  return err;
883 
884  err = (unit->type == H264_NAL_END_SEQUENCE ?
885  cbs_h264_read_end_of_sequence :
886  cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
887  if (err < 0)
888  return err;
889  }
890  break;
891 
892  default:
893  return AVERROR(ENOSYS);
894  }
895 
896  return 0;
897 }
898 
900  CodedBitstreamUnit *unit)
901 {
902  GetBitContext gbc;
903  int err;
904 
905  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
906  if (err < 0)
907  return err;
908 
909  switch (unit->type) {
910  case HEVC_NAL_VPS:
911  {
912  H265RawVPS *vps;
913 
914  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
916  if (err < 0)
917  return err;
918  vps = unit->content;
919 
920  err = cbs_h265_read_vps(ctx, &gbc, vps);
921  if (err < 0)
922  return err;
923 
924  err = cbs_h265_replace_vps(ctx, unit);
925  if (err < 0)
926  return err;
927  }
928  break;
929  case HEVC_NAL_SPS:
930  {
931  H265RawSPS *sps;
932 
933  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
935  if (err < 0)
936  return err;
937  sps = unit->content;
938 
939  err = cbs_h265_read_sps(ctx, &gbc, sps);
940  if (err < 0)
941  return err;
942 
943  err = cbs_h265_replace_sps(ctx, unit);
944  if (err < 0)
945  return err;
946  }
947  break;
948 
949  case HEVC_NAL_PPS:
950  {
951  H265RawPPS *pps;
952 
953  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
955  if (err < 0)
956  return err;
957  pps = unit->content;
958 
959  err = cbs_h265_read_pps(ctx, &gbc, pps);
960  if (err < 0)
961  return err;
962 
963  err = cbs_h265_replace_pps(ctx, unit);
964  if (err < 0)
965  return err;
966  }
967  break;
968 
969  case HEVC_NAL_TRAIL_N:
970  case HEVC_NAL_TRAIL_R:
971  case HEVC_NAL_TSA_N:
972  case HEVC_NAL_TSA_R:
973  case HEVC_NAL_STSA_N:
974  case HEVC_NAL_STSA_R:
975  case HEVC_NAL_RADL_N:
976  case HEVC_NAL_RADL_R:
977  case HEVC_NAL_RASL_N:
978  case HEVC_NAL_RASL_R:
979  case HEVC_NAL_BLA_W_LP:
980  case HEVC_NAL_BLA_W_RADL:
981  case HEVC_NAL_BLA_N_LP:
982  case HEVC_NAL_IDR_W_RADL:
983  case HEVC_NAL_IDR_N_LP:
984  case HEVC_NAL_CRA_NUT:
985  {
986  H265RawSlice *slice;
987  int pos, len;
988 
989  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
991  if (err < 0)
992  return err;
993  slice = unit->content;
994 
995  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
996  if (err < 0)
997  return err;
998 
999  pos = get_bits_count(&gbc);
1000  len = unit->data_size;
1001  if (!unit->data[len - 1]) {
1002  int z;
1003  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1004  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1005  "from slice data.\n", z);
1006  len -= z;
1007  }
1008 
1009  slice->data_size = len - pos / 8;
1010  slice->data_ref = av_buffer_ref(unit->data_ref);
1011  if (!slice->data_ref)
1012  return AVERROR(ENOMEM);
1013  slice->data = unit->data + pos / 8;
1014  slice->data_bit_start = pos % 8;
1015  }
1016  break;
1017 
1018  case HEVC_NAL_AUD:
1019  {
1020  err = ff_cbs_alloc_unit_content(ctx, unit,
1021  sizeof(H265RawAUD), NULL);
1022  if (err < 0)
1023  return err;
1024 
1025  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1026  if (err < 0)
1027  return err;
1028  }
1029  break;
1030 
1031  case HEVC_NAL_SEI_PREFIX:
1032  {
1033  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1035 
1036  if (err < 0)
1037  return err;
1038 
1039  err = cbs_h265_read_sei(ctx, &gbc, unit->content);
1040 
1041  if (err < 0)
1042  return err;
1043  }
1044  break;
1045 
1046  default:
1047  return AVERROR(ENOSYS);
1048  }
1049 
1050  return 0;
1051 }
1052 
1054  CodedBitstreamUnit *unit,
1055  PutBitContext *pbc)
1056 {
1057  int err;
1058 
1059  switch (unit->type) {
1060  case H264_NAL_SPS:
1061  {
1062  H264RawSPS *sps = unit->content;
1063 
1064  err = cbs_h264_write_sps(ctx, pbc, sps);
1065  if (err < 0)
1066  return err;
1067 
1068  err = cbs_h264_replace_sps(ctx, unit);
1069  if (err < 0)
1070  return err;
1071  }
1072  break;
1073 
1074  case H264_NAL_SPS_EXT:
1075  {
1076  H264RawSPSExtension *sps_ext = unit->content;
1077 
1078  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1079  if (err < 0)
1080  return err;
1081  }
1082  break;
1083 
1084  case H264_NAL_PPS:
1085  {
1086  H264RawPPS *pps = unit->content;
1087 
1088  err = cbs_h264_write_pps(ctx, pbc, pps);
1089  if (err < 0)
1090  return err;
1091 
1092  err = cbs_h264_replace_pps(ctx, unit);
1093  if (err < 0)
1094  return err;
1095  }
1096  break;
1097 
1098  case H264_NAL_SLICE:
1099  case H264_NAL_IDR_SLICE:
1101  {
1102  H264RawSlice *slice = unit->content;
1103  GetBitContext gbc;
1104  int bits_left, end, zeroes;
1105 
1106  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1107  if (err < 0)
1108  return err;
1109 
1110  if (slice->data) {
1111  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1112  return AVERROR(ENOSPC);
1113 
1114  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1115  skip_bits_long(&gbc, slice->data_bit_start);
1116 
1117  // Copy in two-byte blocks, but stop before copying the
1118  // rbsp_stop_one_bit in the final byte.
1119  while (get_bits_left(&gbc) > 23)
1120  put_bits(pbc, 16, get_bits(&gbc, 16));
1121 
1122  bits_left = get_bits_left(&gbc);
1123  end = get_bits(&gbc, bits_left);
1124 
1125  // rbsp_stop_one_bit must be present here.
1126  av_assert0(end);
1127  zeroes = ff_ctz(end);
1128  if (bits_left > zeroes + 1)
1129  put_bits(pbc, bits_left - zeroes - 1,
1130  end >> (zeroes + 1));
1131  put_bits(pbc, 1, 1);
1132  while (put_bits_count(pbc) % 8 != 0)
1133  put_bits(pbc, 1, 0);
1134  } else {
1135  // No slice data - that was just the header.
1136  // (Bitstream may be unaligned!)
1137  }
1138  }
1139  break;
1140 
1141  case H264_NAL_AUD:
1142  {
1143  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1144  if (err < 0)
1145  return err;
1146  }
1147  break;
1148 
1149  case H264_NAL_SEI:
1150  {
1151  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1152  if (err < 0)
1153  return err;
1154  }
1155  break;
1156 
1157  case H264_NAL_FILLER_DATA:
1158  {
1159  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1160  if (err < 0)
1161  return err;
1162  }
1163  break;
1164 
1165  case H264_NAL_END_SEQUENCE:
1166  {
1167  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1168  if (err < 0)
1169  return err;
1170  }
1171  break;
1172 
1173  case H264_NAL_END_STREAM:
1174  {
1175  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1176  if (err < 0)
1177  return err;
1178  }
1179  break;
1180 
1181  default:
1182  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1183  "NAL unit type %"PRIu32".\n", unit->type);
1184  return AVERROR_PATCHWELCOME;
1185  }
1186 
1187  return 0;
1188 }
1189 
1191  CodedBitstreamUnit *unit,
1192  PutBitContext *pbc)
1193 {
1194  int err;
1195 
1196  switch (unit->type) {
1197  case HEVC_NAL_VPS:
1198  {
1199  H265RawVPS *vps = unit->content;
1200 
1201  err = cbs_h265_write_vps(ctx, pbc, vps);
1202  if (err < 0)
1203  return err;
1204 
1205  err = cbs_h265_replace_vps(ctx, unit);
1206  if (err < 0)
1207  return err;
1208  }
1209  break;
1210 
1211  case HEVC_NAL_SPS:
1212  {
1213  H265RawSPS *sps = unit->content;
1214 
1215  err = cbs_h265_write_sps(ctx, pbc, sps);
1216  if (err < 0)
1217  return err;
1218 
1219  err = cbs_h265_replace_sps(ctx, unit);
1220  if (err < 0)
1221  return err;
1222  }
1223  break;
1224 
1225  case HEVC_NAL_PPS:
1226  {
1227  H265RawPPS *pps = unit->content;
1228 
1229  err = cbs_h265_write_pps(ctx, pbc, pps);
1230  if (err < 0)
1231  return err;
1232 
1233  err = cbs_h265_replace_pps(ctx, unit);
1234  if (err < 0)
1235  return err;
1236  }
1237  break;
1238 
1239  case HEVC_NAL_TRAIL_N:
1240  case HEVC_NAL_TRAIL_R:
1241  case HEVC_NAL_TSA_N:
1242  case HEVC_NAL_TSA_R:
1243  case HEVC_NAL_STSA_N:
1244  case HEVC_NAL_STSA_R:
1245  case HEVC_NAL_RADL_N:
1246  case HEVC_NAL_RADL_R:
1247  case HEVC_NAL_RASL_N:
1248  case HEVC_NAL_RASL_R:
1249  case HEVC_NAL_BLA_W_LP:
1250  case HEVC_NAL_BLA_W_RADL:
1251  case HEVC_NAL_BLA_N_LP:
1252  case HEVC_NAL_IDR_W_RADL:
1253  case HEVC_NAL_IDR_N_LP:
1254  case HEVC_NAL_CRA_NUT:
1255  {
1256  H265RawSlice *slice = unit->content;
1257  GetBitContext gbc;
1258  int bits_left, end, zeroes;
1259 
1260  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1261  if (err < 0)
1262  return err;
1263 
1264  if (slice->data) {
1265  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1266  return AVERROR(ENOSPC);
1267 
1268  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1269  skip_bits_long(&gbc, slice->data_bit_start);
1270 
1271  // Copy in two-byte blocks, but stop before copying the
1272  // rbsp_stop_one_bit in the final byte.
1273  while (get_bits_left(&gbc) > 23)
1274  put_bits(pbc, 16, get_bits(&gbc, 16));
1275 
1276  bits_left = get_bits_left(&gbc);
1277  end = get_bits(&gbc, bits_left);
1278 
1279  // rbsp_stop_one_bit must be present here.
1280  av_assert0(end);
1281  zeroes = ff_ctz(end);
1282  if (bits_left > zeroes + 1)
1283  put_bits(pbc, bits_left - zeroes - 1,
1284  end >> (zeroes + 1));
1285  put_bits(pbc, 1, 1);
1286  while (put_bits_count(pbc) % 8 != 0)
1287  put_bits(pbc, 1, 0);
1288  } else {
1289  // No slice data - that was just the header.
1290  }
1291  }
1292  break;
1293 
1294  case HEVC_NAL_AUD:
1295  {
1296  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1297  if (err < 0)
1298  return err;
1299  }
1300  break;
1301 
1302  case HEVC_NAL_SEI_PREFIX:
1303  {
1304  err = cbs_h265_write_sei(ctx, pbc, unit->content);
1305 
1306  if (err < 0)
1307  return err;
1308  }
1309  break;
1310 
1311  default:
1312  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1313  "NAL unit type %"PRIu32".\n", unit->type);
1314  return AVERROR_PATCHWELCOME;
1315  }
1316 
1317  return 0;
1318 }
1319 
1321  CodedBitstreamUnit *unit)
1322 {
1324  enum AVCodecID codec_id = ctx->codec->codec_id;
1325  PutBitContext pbc;
1326  int err;
1327 
1328  if (!priv->write_buffer) {
1329  // Initial write buffer size is 1MB.
1330  priv->write_buffer_size = 1024 * 1024;
1331 
1332  reallocate_and_try_again:
1333  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1334  if (err < 0) {
1335  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1336  "sufficiently large write buffer (last attempt "
1337  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1338  return err;
1339  }
1340  }
1341 
1342  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1343 
1344  if (codec_id == AV_CODEC_ID_H264)
1345  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1346  else
1347  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1348 
1349  if (err == AVERROR(ENOSPC)) {
1350  // Overflow.
1351  priv->write_buffer_size *= 2;
1352  goto reallocate_and_try_again;
1353  }
1354  // Overflow but we didn't notice.
1355  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1356 
1357  if (err < 0) {
1358  // Write failed for some other reason.
1359  return err;
1360  }
1361 
1362  if (put_bits_count(&pbc) % 8)
1363  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1364  else
1365  unit->data_bit_padding = 0;
1366 
1367  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1368  flush_put_bits(&pbc);
1369 
1370  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1371  if (err < 0)
1372  return err;
1373 
1374  memcpy(unit->data, priv->write_buffer, unit->data_size);
1375 
1376  return 0;
1377 }
1378 
1380  CodedBitstreamFragment *frag)
1381 {
1382  uint8_t *data;
1383  size_t max_size, dp, sp;
1384  int err, i, zero_run;
1385 
1386  for (i = 0; i < frag->nb_units; i++) {
1387  // Data should already all have been written when we get here.
1388  av_assert0(frag->units[i].data);
1389  }
1390 
1391  max_size = 0;
1392  for (i = 0; i < frag->nb_units; i++) {
1393  // Start code + content with worst-case emulation prevention.
1394  max_size += 3 + frag->units[i].data_size * 3 / 2;
1395  }
1396 
1397  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1398  if (!data)
1399  return AVERROR(ENOMEM);
1400 
1401  dp = 0;
1402  for (i = 0; i < frag->nb_units; i++) {
1403  CodedBitstreamUnit *unit = &frag->units[i];
1404 
1405  if (unit->data_bit_padding > 0) {
1406  if (i < frag->nb_units - 1)
1407  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1408  "unaligned padding on non-final NAL unit.\n");
1409  else
1410  frag->data_bit_padding = unit->data_bit_padding;
1411  }
1412 
1413  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1414  (unit->type == H264_NAL_SPS ||
1415  unit->type == H264_NAL_PPS)) ||
1416  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1417  (unit->type == HEVC_NAL_VPS ||
1418  unit->type == HEVC_NAL_SPS ||
1419  unit->type == HEVC_NAL_PPS)) ||
1420  i == 0 /* (Assume this is the start of an access unit.) */) {
1421  // zero_byte
1422  data[dp++] = 0;
1423  }
1424  // start_code_prefix_one_3bytes
1425  data[dp++] = 0;
1426  data[dp++] = 0;
1427  data[dp++] = 1;
1428 
1429  zero_run = 0;
1430  for (sp = 0; sp < unit->data_size; sp++) {
1431  if (zero_run < 2) {
1432  if (unit->data[sp] == 0)
1433  ++zero_run;
1434  else
1435  zero_run = 0;
1436  } else {
1437  if ((unit->data[sp] & ~3) == 0) {
1438  // emulation_prevention_three_byte
1439  data[dp++] = 3;
1440  }
1441  zero_run = unit->data[sp] == 0;
1442  }
1443  data[dp++] = unit->data[sp];
1444  }
1445  }
1446 
1447  av_assert0(dp <= max_size);
1448  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1449  if (err)
1450  return err;
1451  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1452 
1454  NULL, NULL, 0);
1455  if (!frag->data_ref) {
1456  av_freep(&data);
1457  return AVERROR(ENOMEM);
1458  }
1459 
1460  frag->data = data;
1461  frag->data_size = dp;
1462 
1463  return 0;
1464 }
1465 
1467 {
1468  CodedBitstreamH264Context *h264 = ctx->priv_data;
1469  int i;
1470 
1472 
1473  av_freep(&h264->common.write_buffer);
1474 
1475  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1476  av_buffer_unref(&h264->sps_ref[i]);
1477  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1478  av_buffer_unref(&h264->pps_ref[i]);
1479 }
1480 
1482 {
1483  CodedBitstreamH265Context *h265 = ctx->priv_data;
1484  int i;
1485 
1487 
1488  av_freep(&h265->common.write_buffer);
1489 
1490  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1491  av_buffer_unref(&h265->vps_ref[i]);
1492  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1493  av_buffer_unref(&h265->sps_ref[i]);
1494  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1495  av_buffer_unref(&h265->pps_ref[i]);
1496 }
1497 
1500 
1501  .priv_data_size = sizeof(CodedBitstreamH264Context),
1502 
1503  .split_fragment = &cbs_h2645_split_fragment,
1504  .read_unit = &cbs_h264_read_nal_unit,
1505  .write_unit = &cbs_h2645_write_nal_unit,
1506  .assemble_fragment = &cbs_h2645_assemble_fragment,
1507 
1508  .close = &cbs_h264_close,
1509 };
1510 
1513 
1514  .priv_data_size = sizeof(CodedBitstreamH265Context),
1515 
1516  .split_fragment = &cbs_h2645_split_fragment,
1517  .read_unit = &cbs_h265_read_nal_unit,
1518  .write_unit = &cbs_h2645_write_nal_unit,
1519  .assemble_fragment = &cbs_h2645_assemble_fragment,
1520 
1521  .close = &cbs_h265_close,
1522 };
1523 
1526  const H264RawSEIPayload *payload)
1527 {
1528  H264RawSEI *sei;
1529  CodedBitstreamUnit *nal = NULL;
1530  int err, i;
1531 
1532  // Find an existing SEI NAL unit to add to.
1533  for (i = 0; i < au->nb_units; i++) {
1534  if (au->units[i].type == H264_NAL_SEI) {
1535  nal = &au->units[i];
1536  break;
1537  }
1538  }
1539  if (nal) {
1540  sei = nal->content;
1541 
1542  } else {
1543  // Need to make a new SEI NAL unit. Insert it before the first
1544  // slice data NAL unit; if no slice data, add at the end.
1545  AVBufferRef *sei_ref;
1546 
1547  sei = av_mallocz(sizeof(*sei));
1548  if (!sei)
1549  return AVERROR(ENOMEM);
1550 
1552  sei->nal_unit_header.nal_ref_idc = 0;
1553 
1554  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1555  &cbs_h264_free_sei, ctx, 0);
1556  if (!sei_ref) {
1557  av_freep(&sei);
1558  return AVERROR(ENOMEM);
1559  }
1560 
1561  for (i = 0; i < au->nb_units; i++) {
1562  if (au->units[i].type == H264_NAL_SLICE ||
1563  au->units[i].type == H264_NAL_IDR_SLICE)
1564  break;
1565  }
1566 
1567  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1568  sei, sei_ref);
1569  av_buffer_unref(&sei_ref);
1570  if (err < 0)
1571  return err;
1572  }
1573 
1574  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1575  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1576  "SEI NAL unit.\n");
1577  return AVERROR(EINVAL);
1578  }
1579 
1580  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1581  ++sei->payload_count;
1582 
1583  return 0;
1584 }
1585 
1588  CodedBitstreamUnit *nal,
1589  int position)
1590 {
1591  H264RawSEI *sei = nal->content;
1592 
1593  av_assert0(nal->type == H264_NAL_SEI);
1594  av_assert0(position >= 0 && position < sei->payload_count);
1595 
1596  if (position == 0 && sei->payload_count == 1) {
1597  // Deleting NAL unit entirely.
1598  int i;
1599 
1600  for (i = 0; i < au->nb_units; i++) {
1601  if (&au->units[i] == nal)
1602  break;
1603  }
1604  av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1605 
1606  return ff_cbs_delete_unit(ctx, au, i);
1607  } else {
1608  cbs_h264_free_sei_payload(&sei->payload[position]);
1609 
1610  --sei->payload_count;
1611  memmove(sei->payload + position,
1612  sei->payload + position + 1,
1613  (sei->payload_count - position) * sizeof(*sei->payload));
1614 
1615  return 0;
1616  }
1617 }
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1320
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
union H264RawSEIPayload::@50 payload
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:1190
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:899
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:506
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:570
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:1379
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:490
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:1524
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:417
struct H264RawSEIPayload::@50::@51 other
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:483
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:1466
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:527
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:607
#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
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:476
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
union H265RawSEIPayload::@55 payload
#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:424
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1498
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1481
uint32_t payload_type
Definition: cbs_h265.h:542
picture timing
Definition: h264_sei.h:29
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:201
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:644
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
static void cbs_h265_free_vps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:462
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:455
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
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:502
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
struct H265RawSEIPayload::@55::@56 other
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:1511
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:782
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:1586
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:1053
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:703
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:544
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:376
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:446
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:511
static void cbs_h265_free_sps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:469