FFmpeg
cbs_h2645.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21 
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h264.h"
26 #include "cbs_h265.h"
27 #include "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) {
504  break;
507  break;
510  break;
511  default:
512  av_buffer_unref(&payload->payload.other.data_ref);
513  break;
514  }
515 }
516 
517 static void cbs_h265_free_sei(void *unit, uint8_t *content)
518 {
519  H265RawSEI *sei = (H265RawSEI*)content;
520  int i;
521  for (i = 0; i < sei->payload_count; i++)
523  av_freep(&content);
524 }
525 
528  const H2645Packet *packet)
529 {
530  int err, i;
531 
532  for (i = 0; i < packet->nb_nals; i++) {
533  const H2645NAL *nal = &packet->nals[i];
534  AVBufferRef *ref;
535  size_t size = nal->size;
536 
537  // Remove trailing zeroes.
538  while (size > 0 && nal->data[size - 1] == 0)
539  --size;
540  av_assert0(size > 0);
541 
542  ref = (nal->data == nal->raw_data) ? frag->data_ref
543  : packet->rbsp.rbsp_buffer_ref;
544 
545  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
546  (uint8_t*)nal->data, size, ref);
547  if (err < 0)
548  return err;
549  }
550 
551  return 0;
552 }
553 
556  int header)
557 {
558  enum AVCodecID codec_id = ctx->codec->codec_id;
560  GetByteContext gbc;
561  int err;
562 
563  av_assert0(frag->data && frag->nb_units == 0);
564  if (frag->data_size == 0)
565  return 0;
566 
567  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
568  // AVCC header.
569  size_t size, start, end;
570  int i, count, version;
571 
572  priv->mp4 = 1;
573 
574  bytestream2_init(&gbc, frag->data, frag->data_size);
575 
576  if (bytestream2_get_bytes_left(&gbc) < 6)
577  return AVERROR_INVALIDDATA;
578 
579  version = bytestream2_get_byte(&gbc);
580  if (version != 1) {
581  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
582  "first byte %u.", version);
583  return AVERROR_INVALIDDATA;
584  }
585 
586  bytestream2_skip(&gbc, 3);
587  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
588 
589  // SPS array.
590  count = bytestream2_get_byte(&gbc) & 0x1f;
591  start = bytestream2_tell(&gbc);
592  for (i = 0; i < count; i++) {
593  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
594  return AVERROR_INVALIDDATA;
595  size = bytestream2_get_be16(&gbc);
596  if (bytestream2_get_bytes_left(&gbc) < size)
597  return AVERROR_INVALIDDATA;
598  bytestream2_skip(&gbc, size);
599  }
600  end = bytestream2_tell(&gbc);
601 
602  err = ff_h2645_packet_split(&priv->read_packet,
603  frag->data + start, end - start,
604  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
605  if (err < 0) {
606  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
607  return err;
608  }
609  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
610  if (err < 0)
611  return err;
612 
613  // PPS array.
614  count = bytestream2_get_byte(&gbc);
615  start = bytestream2_tell(&gbc);
616  for (i = 0; i < count; i++) {
617  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
618  return AVERROR_INVALIDDATA;
619  size = bytestream2_get_be16(&gbc);
620  if (bytestream2_get_bytes_left(&gbc) < size)
621  return AVERROR_INVALIDDATA;
622  bytestream2_skip(&gbc, size);
623  }
624  end = bytestream2_tell(&gbc);
625 
626  err = ff_h2645_packet_split(&priv->read_packet,
627  frag->data + start, end - start,
628  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
629  if (err < 0) {
630  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
631  return err;
632  }
633  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
634  if (err < 0)
635  return err;
636 
637  if (bytestream2_get_bytes_left(&gbc) > 0) {
638  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
639  "header.\n", bytestream2_get_bytes_left(&gbc));
640  }
641 
642  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
643  // HVCC header.
644  size_t size, start, end;
645  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
646 
647  priv->mp4 = 1;
648 
649  bytestream2_init(&gbc, frag->data, frag->data_size);
650 
651  if (bytestream2_get_bytes_left(&gbc) < 23)
652  return AVERROR_INVALIDDATA;
653 
654  version = bytestream2_get_byte(&gbc);
655  if (version != 1) {
656  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
657  "first byte %u.", version);
658  return AVERROR_INVALIDDATA;
659  }
660 
661  bytestream2_skip(&gbc, 20);
662  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
663 
664  nb_arrays = bytestream2_get_byte(&gbc);
665  for (i = 0; i < nb_arrays; i++) {
666  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
667  nb_nals = bytestream2_get_be16(&gbc);
668 
669  start = bytestream2_tell(&gbc);
670  for (j = 0; j < nb_nals; j++) {
671  if (bytestream2_get_bytes_left(&gbc) < 2)
672  return AVERROR_INVALIDDATA;
673  size = bytestream2_get_be16(&gbc);
674  if (bytestream2_get_bytes_left(&gbc) < size)
675  return AVERROR_INVALIDDATA;
676  bytestream2_skip(&gbc, size);
677  }
678  end = bytestream2_tell(&gbc);
679 
680  err = ff_h2645_packet_split(&priv->read_packet,
681  frag->data + start, end - start,
682  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
683  if (err < 0) {
684  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
685  "HVCC array %d (%d NAL units of type %d).\n",
686  i, nb_nals, nal_unit_type);
687  return err;
688  }
689  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
690  if (err < 0)
691  return err;
692  }
693 
694  } else {
695  // Annex B, or later MP4 with already-known parameters.
696 
697  err = ff_h2645_packet_split(&priv->read_packet,
698  frag->data, frag->data_size,
699  ctx->log_ctx,
700  priv->mp4, priv->nal_length_size,
701  codec_id, 1, 1);
702  if (err < 0)
703  return err;
704 
705  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
706  if (err < 0)
707  return err;
708  }
709 
710  return 0;
711 }
712 
713 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
714 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
715  CodedBitstreamUnit *unit) \
716 { \
717  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
718  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
719  unsigned int id = ps_var->id_element; \
720  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
721  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
722  " id : %d.\n", id); \
723  return AVERROR_INVALIDDATA; \
724  } \
725  if (priv->ps_var[id] == priv->active_ ## ps_var) \
726  priv->active_ ## ps_var = NULL ; \
727  av_buffer_unref(&priv->ps_var ## _ref[id]); \
728  if (unit->content_ref) \
729  priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
730  else \
731  priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
732  if (!priv->ps_var ## _ref[id]) \
733  return AVERROR(ENOMEM); \
734  priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
735  if (!unit->content_ref) \
736  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
737  return 0; \
738 }
739 
740 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
741 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
742 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
743 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
744 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
745 
746 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
747  CodedBitstreamUnit *unit)
748 {
749  GetBitContext gbc;
750  int err;
751 
752  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
753  if (err < 0)
754  return err;
755 
756  switch (unit->type) {
757  case H264_NAL_SPS:
758  {
759  H264RawSPS *sps;
760 
761  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
762  if (err < 0)
763  return err;
764  sps = unit->content;
765 
766  err = cbs_h264_read_sps(ctx, &gbc, sps);
767  if (err < 0)
768  return err;
769 
770  err = cbs_h264_replace_sps(ctx, unit);
771  if (err < 0)
772  return err;
773  }
774  break;
775 
776  case H264_NAL_SPS_EXT:
777  {
778  err = ff_cbs_alloc_unit_content(ctx, unit,
779  sizeof(H264RawSPSExtension),
780  NULL);
781  if (err < 0)
782  return err;
783 
784  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
785  if (err < 0)
786  return err;
787  }
788  break;
789 
790  case H264_NAL_PPS:
791  {
792  H264RawPPS *pps;
793 
794  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
796  if (err < 0)
797  return err;
798  pps = unit->content;
799 
800  err = cbs_h264_read_pps(ctx, &gbc, pps);
801  if (err < 0)
802  return err;
803 
804  err = cbs_h264_replace_pps(ctx, unit);
805  if (err < 0)
806  return err;
807  }
808  break;
809 
810  case H264_NAL_SLICE:
811  case H264_NAL_IDR_SLICE:
813  {
814  H264RawSlice *slice;
815  int pos, len;
816 
817  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
819  if (err < 0)
820  return err;
821  slice = unit->content;
822 
823  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
824  if (err < 0)
825  return err;
826 
827  pos = get_bits_count(&gbc);
828  len = unit->data_size;
829  if (!unit->data[len - 1]) {
830  int z;
831  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
832  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
833  "from slice data.\n", z);
834  len -= z;
835  }
836 
837  slice->data_size = len - pos / 8;
838  slice->data_ref = av_buffer_ref(unit->data_ref);
839  if (!slice->data_ref)
840  return AVERROR(ENOMEM);
841  slice->data = unit->data + pos / 8;
842  slice->data_bit_start = pos % 8;
843  }
844  break;
845 
846  case H264_NAL_AUD:
847  {
848  err = ff_cbs_alloc_unit_content(ctx, unit,
849  sizeof(H264RawAUD), NULL);
850  if (err < 0)
851  return err;
852 
853  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
854  if (err < 0)
855  return err;
856  }
857  break;
858 
859  case H264_NAL_SEI:
860  {
861  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
863  if (err < 0)
864  return err;
865 
866  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
867  if (err < 0)
868  return err;
869  }
870  break;
871 
873  {
874  err = ff_cbs_alloc_unit_content(ctx, unit,
875  sizeof(H264RawFiller), NULL);
876  if (err < 0)
877  return err;
878 
879  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
880  if (err < 0)
881  return err;
882  }
883  break;
884 
886  case H264_NAL_END_STREAM:
887  {
888  err = ff_cbs_alloc_unit_content(ctx, unit,
889  sizeof(H264RawNALUnitHeader),
890  NULL);
891  if (err < 0)
892  return err;
893 
894  err = (unit->type == H264_NAL_END_SEQUENCE ?
895  cbs_h264_read_end_of_sequence :
896  cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
897  if (err < 0)
898  return err;
899  }
900  break;
901 
902  default:
903  return AVERROR(ENOSYS);
904  }
905 
906  return 0;
907 }
908 
910  CodedBitstreamUnit *unit)
911 {
912  GetBitContext gbc;
913  int err;
914 
915  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
916  if (err < 0)
917  return err;
918 
919  switch (unit->type) {
920  case HEVC_NAL_VPS:
921  {
922  H265RawVPS *vps;
923 
924  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
926  if (err < 0)
927  return err;
928  vps = unit->content;
929 
930  err = cbs_h265_read_vps(ctx, &gbc, vps);
931  if (err < 0)
932  return err;
933 
934  err = cbs_h265_replace_vps(ctx, unit);
935  if (err < 0)
936  return err;
937  }
938  break;
939  case HEVC_NAL_SPS:
940  {
941  H265RawSPS *sps;
942 
943  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
945  if (err < 0)
946  return err;
947  sps = unit->content;
948 
949  err = cbs_h265_read_sps(ctx, &gbc, sps);
950  if (err < 0)
951  return err;
952 
953  err = cbs_h265_replace_sps(ctx, unit);
954  if (err < 0)
955  return err;
956  }
957  break;
958 
959  case HEVC_NAL_PPS:
960  {
961  H265RawPPS *pps;
962 
963  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
965  if (err < 0)
966  return err;
967  pps = unit->content;
968 
969  err = cbs_h265_read_pps(ctx, &gbc, pps);
970  if (err < 0)
971  return err;
972 
973  err = cbs_h265_replace_pps(ctx, unit);
974  if (err < 0)
975  return err;
976  }
977  break;
978 
979  case HEVC_NAL_TRAIL_N:
980  case HEVC_NAL_TRAIL_R:
981  case HEVC_NAL_TSA_N:
982  case HEVC_NAL_TSA_R:
983  case HEVC_NAL_STSA_N:
984  case HEVC_NAL_STSA_R:
985  case HEVC_NAL_RADL_N:
986  case HEVC_NAL_RADL_R:
987  case HEVC_NAL_RASL_N:
988  case HEVC_NAL_RASL_R:
989  case HEVC_NAL_BLA_W_LP:
990  case HEVC_NAL_BLA_W_RADL:
991  case HEVC_NAL_BLA_N_LP:
992  case HEVC_NAL_IDR_W_RADL:
993  case HEVC_NAL_IDR_N_LP:
994  case HEVC_NAL_CRA_NUT:
995  {
996  H265RawSlice *slice;
997  int pos, len;
998 
999  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1001  if (err < 0)
1002  return err;
1003  slice = unit->content;
1004 
1005  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1006  if (err < 0)
1007  return err;
1008 
1009  pos = get_bits_count(&gbc);
1010  len = unit->data_size;
1011  if (!unit->data[len - 1]) {
1012  int z;
1013  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1014  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1015  "from slice data.\n", z);
1016  len -= z;
1017  }
1018 
1019  slice->data_size = len - pos / 8;
1020  slice->data_ref = av_buffer_ref(unit->data_ref);
1021  if (!slice->data_ref)
1022  return AVERROR(ENOMEM);
1023  slice->data = unit->data + pos / 8;
1024  slice->data_bit_start = pos % 8;
1025  }
1026  break;
1027 
1028  case HEVC_NAL_AUD:
1029  {
1030  err = ff_cbs_alloc_unit_content(ctx, unit,
1031  sizeof(H265RawAUD), NULL);
1032  if (err < 0)
1033  return err;
1034 
1035  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1036  if (err < 0)
1037  return err;
1038  }
1039  break;
1040 
1041  case HEVC_NAL_SEI_PREFIX:
1042  case HEVC_NAL_SEI_SUFFIX:
1043  {
1044  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1046 
1047  if (err < 0)
1048  return err;
1049 
1050  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1051  unit->type == HEVC_NAL_SEI_PREFIX);
1052 
1053  if (err < 0)
1054  return err;
1055  }
1056  break;
1057 
1058  default:
1059  return AVERROR(ENOSYS);
1060  }
1061 
1062  return 0;
1063 }
1064 
1066  PutBitContext *pbc, const uint8_t *data,
1067  size_t data_size, int data_bit_start)
1068 {
1069  size_t rest = data_size - (data_bit_start + 7) / 8;
1070  const uint8_t *pos = data + data_bit_start / 8;
1071 
1072  av_assert0(data_bit_start >= 0 &&
1073  8 * data_size > data_bit_start);
1074 
1075  if (data_size * 8 + 8 > put_bits_left(pbc))
1076  return AVERROR(ENOSPC);
1077 
1078  if (!rest)
1079  goto rbsp_stop_one_bit;
1080 
1081  // First copy the remaining bits of the first byte
1082  // The above check ensures that we do not accidentally
1083  // copy beyond the rbsp_stop_one_bit.
1084  if (data_bit_start % 8)
1085  put_bits(pbc, 8 - data_bit_start % 8,
1086  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1087 
1088  if (put_bits_count(pbc) % 8 == 0) {
1089  // If the writer is aligned at this point,
1090  // memcpy can be used to improve performance.
1091  // This happens normally for CABAC.
1092  flush_put_bits(pbc);
1093  memcpy(put_bits_ptr(pbc), pos, rest);
1094  skip_put_bytes(pbc, rest);
1095  } else {
1096  // If not, we have to copy manually.
1097  // rbsp_stop_one_bit forces us to special-case
1098  // the last byte.
1099  uint8_t temp;
1100  int i;
1101 
1102  for (; rest > 4; rest -= 4, pos += 4)
1103  put_bits32(pbc, AV_RB32(pos));
1104 
1105  for (; rest > 1; rest--, pos++)
1106  put_bits(pbc, 8, *pos);
1107 
1108  rbsp_stop_one_bit:
1109  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1110 
1111  av_assert0(temp);
1112  i = ff_ctz(*pos);
1113  temp = temp >> i;
1114  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1115  put_bits(pbc, i, temp);
1116  if (put_bits_count(pbc) % 8)
1117  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1118  }
1119 
1120  return 0;
1121 }
1122 
1124  CodedBitstreamUnit *unit,
1125  PutBitContext *pbc)
1126 {
1127  int err;
1128 
1129  switch (unit->type) {
1130  case H264_NAL_SPS:
1131  {
1132  H264RawSPS *sps = unit->content;
1133 
1134  err = cbs_h264_write_sps(ctx, pbc, sps);
1135  if (err < 0)
1136  return err;
1137 
1138  err = cbs_h264_replace_sps(ctx, unit);
1139  if (err < 0)
1140  return err;
1141  }
1142  break;
1143 
1144  case H264_NAL_SPS_EXT:
1145  {
1146  H264RawSPSExtension *sps_ext = unit->content;
1147 
1148  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1149  if (err < 0)
1150  return err;
1151  }
1152  break;
1153 
1154  case H264_NAL_PPS:
1155  {
1156  H264RawPPS *pps = unit->content;
1157 
1158  err = cbs_h264_write_pps(ctx, pbc, pps);
1159  if (err < 0)
1160  return err;
1161 
1162  err = cbs_h264_replace_pps(ctx, unit);
1163  if (err < 0)
1164  return err;
1165  }
1166  break;
1167 
1168  case H264_NAL_SLICE:
1169  case H264_NAL_IDR_SLICE:
1171  {
1172  H264RawSlice *slice = unit->content;
1173 
1174  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1175  if (err < 0)
1176  return err;
1177 
1178  if (slice->data) {
1179  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1180  slice->data_size,
1181  slice->data_bit_start);
1182  if (err < 0)
1183  return err;
1184  } else {
1185  // No slice data - that was just the header.
1186  // (Bitstream may be unaligned!)
1187  }
1188  }
1189  break;
1190 
1191  case H264_NAL_AUD:
1192  {
1193  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1194  if (err < 0)
1195  return err;
1196  }
1197  break;
1198 
1199  case H264_NAL_SEI:
1200  {
1201  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1202  if (err < 0)
1203  return err;
1204  }
1205  break;
1206 
1207  case H264_NAL_FILLER_DATA:
1208  {
1209  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1210  if (err < 0)
1211  return err;
1212  }
1213  break;
1214 
1215  case H264_NAL_END_SEQUENCE:
1216  {
1217  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1218  if (err < 0)
1219  return err;
1220  }
1221  break;
1222 
1223  case H264_NAL_END_STREAM:
1224  {
1225  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1226  if (err < 0)
1227  return err;
1228  }
1229  break;
1230 
1231  default:
1232  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1233  "NAL unit type %"PRIu32".\n", unit->type);
1234  return AVERROR_PATCHWELCOME;
1235  }
1236 
1237  return 0;
1238 }
1239 
1241  CodedBitstreamUnit *unit,
1242  PutBitContext *pbc)
1243 {
1244  int err;
1245 
1246  switch (unit->type) {
1247  case HEVC_NAL_VPS:
1248  {
1249  H265RawVPS *vps = unit->content;
1250 
1251  err = cbs_h265_write_vps(ctx, pbc, vps);
1252  if (err < 0)
1253  return err;
1254 
1255  err = cbs_h265_replace_vps(ctx, unit);
1256  if (err < 0)
1257  return err;
1258  }
1259  break;
1260 
1261  case HEVC_NAL_SPS:
1262  {
1263  H265RawSPS *sps = unit->content;
1264 
1265  err = cbs_h265_write_sps(ctx, pbc, sps);
1266  if (err < 0)
1267  return err;
1268 
1269  err = cbs_h265_replace_sps(ctx, unit);
1270  if (err < 0)
1271  return err;
1272  }
1273  break;
1274 
1275  case HEVC_NAL_PPS:
1276  {
1277  H265RawPPS *pps = unit->content;
1278 
1279  err = cbs_h265_write_pps(ctx, pbc, pps);
1280  if (err < 0)
1281  return err;
1282 
1283  err = cbs_h265_replace_pps(ctx, unit);
1284  if (err < 0)
1285  return err;
1286  }
1287  break;
1288 
1289  case HEVC_NAL_TRAIL_N:
1290  case HEVC_NAL_TRAIL_R:
1291  case HEVC_NAL_TSA_N:
1292  case HEVC_NAL_TSA_R:
1293  case HEVC_NAL_STSA_N:
1294  case HEVC_NAL_STSA_R:
1295  case HEVC_NAL_RADL_N:
1296  case HEVC_NAL_RADL_R:
1297  case HEVC_NAL_RASL_N:
1298  case HEVC_NAL_RASL_R:
1299  case HEVC_NAL_BLA_W_LP:
1300  case HEVC_NAL_BLA_W_RADL:
1301  case HEVC_NAL_BLA_N_LP:
1302  case HEVC_NAL_IDR_W_RADL:
1303  case HEVC_NAL_IDR_N_LP:
1304  case HEVC_NAL_CRA_NUT:
1305  {
1306  H265RawSlice *slice = unit->content;
1307 
1308  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1309  if (err < 0)
1310  return err;
1311 
1312  if (slice->data) {
1313  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1314  slice->data_size,
1315  slice->data_bit_start);
1316  if (err < 0)
1317  return err;
1318  } else {
1319  // No slice data - that was just the header.
1320  }
1321  }
1322  break;
1323 
1324  case HEVC_NAL_AUD:
1325  {
1326  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1327  if (err < 0)
1328  return err;
1329  }
1330  break;
1331 
1332  case HEVC_NAL_SEI_PREFIX:
1333  case HEVC_NAL_SEI_SUFFIX:
1334  {
1335  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1336  unit->type == HEVC_NAL_SEI_PREFIX);
1337 
1338  if (err < 0)
1339  return err;
1340  }
1341  break;
1342 
1343  default:
1344  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1345  "NAL unit type %"PRIu32".\n", unit->type);
1346  return AVERROR_PATCHWELCOME;
1347  }
1348 
1349  return 0;
1350 }
1351 
1353  CodedBitstreamUnit *unit)
1354 {
1356  enum AVCodecID codec_id = ctx->codec->codec_id;
1357  PutBitContext pbc;
1358  int err;
1359 
1360  if (!priv->write_buffer) {
1361  // Initial write buffer size is 1MB.
1362  priv->write_buffer_size = 1024 * 1024;
1363 
1364  reallocate_and_try_again:
1365  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1366  if (err < 0) {
1367  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1368  "sufficiently large write buffer (last attempt "
1369  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1370  return err;
1371  }
1372  }
1373 
1374  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1375 
1376  if (codec_id == AV_CODEC_ID_H264)
1377  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1378  else
1379  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1380 
1381  if (err == AVERROR(ENOSPC)) {
1382  // Overflow.
1383  priv->write_buffer_size *= 2;
1384  goto reallocate_and_try_again;
1385  }
1386  // Overflow but we didn't notice.
1387  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1388 
1389  if (err < 0) {
1390  // Write failed for some other reason.
1391  return err;
1392  }
1393 
1394  if (put_bits_count(&pbc) % 8)
1395  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1396  else
1397  unit->data_bit_padding = 0;
1398 
1399  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1400  flush_put_bits(&pbc);
1401 
1402  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1403  if (err < 0)
1404  return err;
1405 
1406  memcpy(unit->data, priv->write_buffer, unit->data_size);
1407 
1408  return 0;
1409 }
1410 
1412  CodedBitstreamFragment *frag)
1413 {
1414  uint8_t *data;
1415  size_t max_size, dp, sp;
1416  int err, i, zero_run;
1417 
1418  for (i = 0; i < frag->nb_units; i++) {
1419  // Data should already all have been written when we get here.
1420  av_assert0(frag->units[i].data);
1421  }
1422 
1423  max_size = 0;
1424  for (i = 0; i < frag->nb_units; i++) {
1425  // Start code + content with worst-case emulation prevention.
1426  max_size += 3 + frag->units[i].data_size * 3 / 2;
1427  }
1428 
1429  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1430  if (!data)
1431  return AVERROR(ENOMEM);
1432 
1433  dp = 0;
1434  for (i = 0; i < frag->nb_units; i++) {
1435  CodedBitstreamUnit *unit = &frag->units[i];
1436 
1437  if (unit->data_bit_padding > 0) {
1438  if (i < frag->nb_units - 1)
1439  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1440  "unaligned padding on non-final NAL unit.\n");
1441  else
1442  frag->data_bit_padding = unit->data_bit_padding;
1443  }
1444 
1445  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1446  (unit->type == H264_NAL_SPS ||
1447  unit->type == H264_NAL_PPS)) ||
1448  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1449  (unit->type == HEVC_NAL_VPS ||
1450  unit->type == HEVC_NAL_SPS ||
1451  unit->type == HEVC_NAL_PPS)) ||
1452  i == 0 /* (Assume this is the start of an access unit.) */) {
1453  // zero_byte
1454  data[dp++] = 0;
1455  }
1456  // start_code_prefix_one_3bytes
1457  data[dp++] = 0;
1458  data[dp++] = 0;
1459  data[dp++] = 1;
1460 
1461  zero_run = 0;
1462  for (sp = 0; sp < unit->data_size; sp++) {
1463  if (zero_run < 2) {
1464  if (unit->data[sp] == 0)
1465  ++zero_run;
1466  else
1467  zero_run = 0;
1468  } else {
1469  if ((unit->data[sp] & ~3) == 0) {
1470  // emulation_prevention_three_byte
1471  data[dp++] = 3;
1472  }
1473  zero_run = unit->data[sp] == 0;
1474  }
1475  data[dp++] = unit->data[sp];
1476  }
1477  }
1478 
1479  av_assert0(dp <= max_size);
1480  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1481  if (err)
1482  return err;
1483  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1484 
1486  NULL, NULL, 0);
1487  if (!frag->data_ref) {
1488  av_freep(&data);
1489  return AVERROR(ENOMEM);
1490  }
1491 
1492  frag->data = data;
1493  frag->data_size = dp;
1494 
1495  return 0;
1496 }
1497 
1499 {
1500  CodedBitstreamH264Context *h264 = ctx->priv_data;
1501  int i;
1502 
1504 
1505  av_freep(&h264->common.write_buffer);
1506 
1507  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1508  av_buffer_unref(&h264->sps_ref[i]);
1509  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1510  av_buffer_unref(&h264->pps_ref[i]);
1511 }
1512 
1514 {
1515  CodedBitstreamH265Context *h265 = ctx->priv_data;
1516  int i;
1517 
1519 
1520  av_freep(&h265->common.write_buffer);
1521 
1522  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1523  av_buffer_unref(&h265->vps_ref[i]);
1524  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1525  av_buffer_unref(&h265->sps_ref[i]);
1526  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1527  av_buffer_unref(&h265->pps_ref[i]);
1528 }
1529 
1532 
1533  .priv_data_size = sizeof(CodedBitstreamH264Context),
1534 
1535  .split_fragment = &cbs_h2645_split_fragment,
1536  .read_unit = &cbs_h264_read_nal_unit,
1537  .write_unit = &cbs_h2645_write_nal_unit,
1538  .assemble_fragment = &cbs_h2645_assemble_fragment,
1539 
1540  .close = &cbs_h264_close,
1541 };
1542 
1545 
1546  .priv_data_size = sizeof(CodedBitstreamH265Context),
1547 
1548  .split_fragment = &cbs_h2645_split_fragment,
1549  .read_unit = &cbs_h265_read_nal_unit,
1550  .write_unit = &cbs_h2645_write_nal_unit,
1551  .assemble_fragment = &cbs_h2645_assemble_fragment,
1552 
1553  .close = &cbs_h265_close,
1554 };
1555 
1558  const H264RawSEIPayload *payload)
1559 {
1560  H264RawSEI *sei;
1561  CodedBitstreamUnit *nal = NULL;
1562  int err, i;
1563 
1564  // Find an existing SEI NAL unit to add to.
1565  for (i = 0; i < au->nb_units; i++) {
1566  if (au->units[i].type == H264_NAL_SEI) {
1567  nal = &au->units[i];
1568  break;
1569  }
1570  }
1571  if (nal) {
1572  sei = nal->content;
1573 
1574  } else {
1575  // Need to make a new SEI NAL unit. Insert it before the first
1576  // slice data NAL unit; if no slice data, add at the end.
1577  AVBufferRef *sei_ref;
1578 
1579  sei = av_mallocz(sizeof(*sei));
1580  if (!sei)
1581  return AVERROR(ENOMEM);
1582 
1584  sei->nal_unit_header.nal_ref_idc = 0;
1585 
1586  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1587  &cbs_h264_free_sei, ctx, 0);
1588  if (!sei_ref) {
1589  av_freep(&sei);
1590  return AVERROR(ENOMEM);
1591  }
1592 
1593  for (i = 0; i < au->nb_units; i++) {
1594  if (au->units[i].type == H264_NAL_SLICE ||
1595  au->units[i].type == H264_NAL_IDR_SLICE)
1596  break;
1597  }
1598 
1599  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1600  sei, sei_ref);
1601  av_buffer_unref(&sei_ref);
1602  if (err < 0)
1603  return err;
1604  }
1605 
1606  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1607  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1608  "SEI NAL unit.\n");
1609  return AVERROR(EINVAL);
1610  }
1611 
1612  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1613  ++sei->payload_count;
1614 
1615  return 0;
1616 }
1617 
1620  CodedBitstreamUnit *nal,
1621  int position)
1622 {
1623  H264RawSEI *sei = nal->content;
1624 
1625  av_assert0(nal->type == H264_NAL_SEI);
1626  av_assert0(position >= 0 && position < sei->payload_count);
1627 
1628  if (position == 0 && sei->payload_count == 1) {
1629  // Deleting NAL unit entirely.
1630  int i;
1631 
1632  for (i = 0; i < au->nb_units; i++) {
1633  if (&au->units[i] == nal)
1634  break;
1635  }
1636  av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1637 
1638  return ff_cbs_delete_unit(ctx, au, i);
1639  } else {
1640  cbs_h264_free_sei_payload(&sei->payload[position]);
1641 
1642  --sei->payload_count;
1643  memmove(sei->payload + position,
1644  sei->payload + position + 1,
1645  (sei->payload_count - position) * sizeof(*sei->payload));
1646 
1647  return 0;
1648  }
1649 }
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1352
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:727
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
H265RawSEIUserDataUnregistered user_data_unregistered
Definition: cbs_h265.h:691
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
static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx, PutBitContext *pbc, const uint8_t *data, size_t data_size, int data_bit_start)
Definition: cbs_h2645.c:1065
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:35
union H264RawSEIPayload::@50 payload
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
else temp
Definition: vf_mcdeint.c:256
AVBufferRef * data_ref
Definition: cbs_h265.h:189
H264RawPPS * pps[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:449
AVBufferRef * sps_ref[HEVC_MAX_SPS_COUNT]
Definition: cbs_h265.h:723
uint8_t * data
Definition: cbs_h265.h:545
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:1240
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:909
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:508
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:586
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:1411
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:718
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:1556
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:206
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:724
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:86
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int data_bit_start
Definition: cbs_h265.h:547
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
unregistered user data
Definition: h264_sei.h:33
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding, int use_ref)
Split an input packet into NAL units.
Definition: h2645_parse.c:388
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
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
Definition: cbs_h265.h:726
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:512
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:712
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1498
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:529
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_allocated.
Definition: cbs.h:162
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:417
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:623
#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:815
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 uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:324
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
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:548
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:173
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
Definition: cbs_h2645.c:424
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1530
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1513
uint32_t payload_type
Definition: cbs_h265.h:684
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:333
picture timing
Definition: h264_sei.h:29
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:225
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:444
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:660
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:52
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:713
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
H265RawSEIUserDataRegistered user_data_registered
Definition: cbs_h265.h:690
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
AVBufferRef * rbsp_buffer_ref
Definition: h2645_parse.h:70
const uint8_t * data
Definition: h2645_parse.h:36
AVBufferRef * slice_group_id_ref
Definition: cbs_h264.h:200
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:488
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:168
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:616
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:517
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
H2645RBSP rbsp
Definition: h2645_parse.h:78
#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:722
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
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:329
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
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:1543
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:786
const uint8_t * raw_data
Definition: h2645_parse.h:45
int data_bit_start
Definition: cbs_h264.h:429
H265RawSliceHeader header
Definition: cbs_h265.h:543
H264RawSliceHeader header
Definition: cbs_h264.h:425
int len
H2645NAL * nals
Definition: h2645_parse.h:77
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:1618
mastering display properties
Definition: h264_sei.h:38
size_t data_size
Definition: cbs_h265.h:546
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:1123
void INT64 start
Definition: avisynth_c.h:690
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:178
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
Definition: cbs_h2645.c:713
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:554
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:725
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:378
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:526
static void cbs_h265_free_sps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:469