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