FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cbs_h2645.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21 
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h264.h"
26 #include "cbs_h265.h"
27 #include "golomb.h"
28 #include "h264.h"
29 #include "h264_sei.h"
30 #include "h2645_parse.h"
31 #include "hevc.h"
32 #include "hevc_sei.h"
33 
34 
36  const char *name, const int *subscripts,
37  uint32_t *write_to,
38  uint32_t range_min, uint32_t range_max)
39 {
40  uint32_t value;
41  int position, i, j;
42  unsigned int k;
43  char bits[65];
44 
45  position = get_bits_count(gbc);
46 
47  for (i = 0; i < 32; i++) {
48  if (get_bits_left(gbc) < i + 1) {
49  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
50  "%s: bitstream ended.\n", name);
51  return AVERROR_INVALIDDATA;
52  }
53  k = get_bits1(gbc);
54  bits[i] = k ? '1' : '0';
55  if (k)
56  break;
57  }
58  if (i >= 32) {
59  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
60  "%s: more than 31 zeroes.\n", name);
61  return AVERROR_INVALIDDATA;
62  }
63  value = 1;
64  for (j = 0; j < i; j++) {
65  k = get_bits1(gbc);
66  bits[i + j + 1] = k ? '1' : '0';
67  value = value << 1 | k;
68  }
69  bits[i + j + 1] = 0;
70  --value;
71 
72  if (ctx->trace_enable)
73  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
74  bits, value);
75 
76  if (value < range_min || value > range_max) {
77  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
78  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
79  name, value, range_min, range_max);
80  return AVERROR_INVALIDDATA;
81  }
82 
83  *write_to = value;
84  return 0;
85 }
86 
88  const char *name, const int *subscripts,
89  int32_t *write_to,
90  int32_t range_min, int32_t range_max)
91 {
92  int32_t value;
93  int position, i, j;
94  unsigned int k;
95  uint32_t v;
96  char bits[65];
97 
98  position = get_bits_count(gbc);
99 
100  for (i = 0; i < 32; i++) {
101  if (get_bits_left(gbc) < i + 1) {
102  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
103  "%s: bitstream ended.\n", name);
104  return AVERROR_INVALIDDATA;
105  }
106  k = get_bits1(gbc);
107  bits[i] = k ? '1' : '0';
108  if (k)
109  break;
110  }
111  if (i >= 32) {
112  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
113  "%s: more than 31 zeroes.\n", name);
114  return AVERROR_INVALIDDATA;
115  }
116  v = 1;
117  for (j = 0; j < i; j++) {
118  k = get_bits1(gbc);
119  bits[i + j + 1] = k ? '1' : '0';
120  v = v << 1 | k;
121  }
122  bits[i + j + 1] = 0;
123  if (v & 1)
124  value = -(int32_t)(v / 2);
125  else
126  value = v / 2;
127 
128  if (ctx->trace_enable)
129  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
130  bits, value);
131 
132  if (value < range_min || value > range_max) {
133  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
134  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
135  name, value, range_min, range_max);
136  return AVERROR_INVALIDDATA;
137  }
138 
139  *write_to = value;
140  return 0;
141 }
142 
144  const char *name, const int *subscripts,
145  uint32_t value,
146  uint32_t range_min, uint32_t range_max)
147 {
148  int len;
149 
150  if (value < range_min || value > range_max) {
151  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
152  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
153  name, value, range_min, range_max);
154  return AVERROR_INVALIDDATA;
155  }
156  av_assert0(value != UINT32_MAX);
157 
158  len = av_log2(value + 1);
159  if (put_bits_left(pbc) < 2 * len + 1)
160  return AVERROR(ENOSPC);
161 
162  if (ctx->trace_enable) {
163  char bits[65];
164  int i;
165 
166  for (i = 0; i < len; i++)
167  bits[i] = '0';
168  bits[len] = '1';
169  for (i = 0; i < len; i++)
170  bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
171  bits[len + len + 1] = 0;
172 
174  name, subscripts, bits, value);
175  }
176 
177  put_bits(pbc, len, 0);
178  if (len + 1 < 32)
179  put_bits(pbc, len + 1, value + 1);
180  else
181  put_bits32(pbc, value + 1);
182 
183  return 0;
184 }
185 
187  const char *name, const int *subscripts,
188  int32_t value,
189  int32_t range_min, int32_t range_max)
190 {
191  int len;
192  uint32_t uvalue;
193 
194  if (value < range_min || value > range_max) {
195  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
196  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
197  name, value, range_min, range_max);
198  return AVERROR_INVALIDDATA;
199  }
200  av_assert0(value != INT32_MIN);
201 
202  if (value == 0)
203  uvalue = 0;
204  else if (value > 0)
205  uvalue = 2 * (uint32_t)value - 1;
206  else
207  uvalue = 2 * (uint32_t)-value;
208 
209  len = av_log2(uvalue + 1);
210  if (put_bits_left(pbc) < 2 * len + 1)
211  return AVERROR(ENOSPC);
212 
213  if (ctx->trace_enable) {
214  char bits[65];
215  int i;
216 
217  for (i = 0; i < len; i++)
218  bits[i] = '0';
219  bits[len] = '1';
220  for (i = 0; i < len; i++)
221  bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
222  bits[len + len + 1] = 0;
223 
225  name, subscripts, bits, value);
226  }
227 
228  put_bits(pbc, len, 0);
229  if (len + 1 < 32)
230  put_bits(pbc, len + 1, uvalue + 1);
231  else
232  put_bits32(pbc, uvalue + 1);
233 
234  return 0;
235 }
236 
237 #define HEADER(name) do { \
238  ff_cbs_trace_header(ctx, name); \
239  } while (0)
240 
241 #define CHECK(call) do { \
242  err = (call); \
243  if (err < 0) \
244  return err; \
245  } while (0)
246 
247 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
248 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
249 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
250 
251 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
252 
253 #define u(width, name, range_min, range_max) \
254  xu(width, name, current->name, range_min, range_max, 0)
255 #define flag(name) u(1, name, 0, 1)
256 #define ue(name, range_min, range_max) \
257  xue(name, current->name, range_min, range_max, 0)
258 #define se(name, range_min, range_max) \
259  xse(name, current->name, range_min, range_max, 0)
260 
261 #define us(width, name, range_min, range_max, subs, ...) \
262  xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
263 #define flags(name, subs, ...) \
264  xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
265 #define ues(name, range_min, range_max, subs, ...) \
266  xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
267 #define ses(name, range_min, range_max, subs, ...) \
268  xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
269 
270 #define fixed(width, name, value) do { \
271  av_unused uint32_t fixed_value = value; \
272  xu(width, name, fixed_value, value, value, 0); \
273  } while (0)
274 
275 
276 #define READ
277 #define READWRITE read
278 #define RWContext GetBitContext
279 
280 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
281  uint32_t value = range_min; \
282  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
283  SUBSCRIPTS(subs, __VA_ARGS__), \
284  &value, range_min, range_max)); \
285  var = value; \
286  } while (0)
287 #define xue(name, var, range_min, range_max, subs, ...) do { \
288  uint32_t value = range_min; \
289  CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
290  SUBSCRIPTS(subs, __VA_ARGS__), \
291  &value, range_min, range_max)); \
292  var = value; \
293  } while (0)
294 #define xse(name, var, range_min, range_max, subs, ...) do { \
295  int32_t value = range_min; \
296  CHECK(cbs_read_se_golomb(ctx, rw, #name, \
297  SUBSCRIPTS(subs, __VA_ARGS__), \
298  &value, range_min, range_max)); \
299  var = value; \
300  } while (0)
301 
302 
303 #define infer(name, value) do { \
304  current->name = value; \
305  } while (0)
306 
308 {
309  int bits_left = get_bits_left(gbc);
310  if (bits_left > 8)
311  return 1;
312  if (show_bits(gbc, bits_left) == 1 << (bits_left - 1))
313  return 0;
314  return 1;
315 }
316 
317 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
318 
319 #define byte_alignment(rw) (get_bits_count(rw) % 8)
320 
321 #define allocate(name, size) do { \
322  name ## _ref = av_buffer_allocz(size + \
323  AV_INPUT_BUFFER_PADDING_SIZE); \
324  if (!name ## _ref) \
325  return AVERROR(ENOMEM); \
326  name = name ## _ref->data; \
327  } while (0)
328 
329 #define FUNC(name) FUNC_H264(READWRITE, name)
331 #undef FUNC
332 
333 #define FUNC(name) FUNC_H265(READWRITE, name)
335 #undef FUNC
336 
337 #undef READ
338 #undef READWRITE
339 #undef RWContext
340 #undef xu
341 #undef xue
342 #undef xse
343 #undef infer
344 #undef more_rbsp_data
345 #undef byte_alignment
346 #undef allocate
347 
348 
349 #define WRITE
350 #define READWRITE write
351 #define RWContext PutBitContext
352 
353 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
354  uint32_t value = var; \
355  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
356  SUBSCRIPTS(subs, __VA_ARGS__), \
357  value, range_min, range_max)); \
358  } while (0)
359 #define xue(name, var, range_min, range_max, subs, ...) do { \
360  uint32_t value = var; \
361  CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
362  SUBSCRIPTS(subs, __VA_ARGS__), \
363  value, range_min, range_max)); \
364  } while (0)
365 #define xse(name, var, range_min, range_max, subs, ...) do { \
366  int32_t value = var; \
367  CHECK(cbs_write_se_golomb(ctx, rw, #name, \
368  SUBSCRIPTS(subs, __VA_ARGS__), \
369  value, range_min, range_max)); \
370  } while (0)
371 
372 #define infer(name, value) do { \
373  if (current->name != (value)) { \
374  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
375  "%s does not match inferred value: " \
376  "%"PRId64", but should be %"PRId64".\n", \
377  #name, (int64_t)current->name, (int64_t)(value)); \
378  } \
379  } while (0)
380 
381 #define more_rbsp_data(var) (var)
382 
383 #define byte_alignment(rw) (put_bits_count(rw) % 8)
384 
385 #define allocate(name, size) do { \
386  if (!name) { \
387  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
388  "for writing.\n", #name); \
389  return AVERROR_INVALIDDATA; \
390  } \
391  } while (0)
392 
393 #define FUNC(name) FUNC_H264(READWRITE, name)
395 #undef FUNC
396 
397 #define FUNC(name) FUNC_H265(READWRITE, name)
399 #undef FUNC
400 
401 #undef WRITE
402 #undef READWRITE
403 #undef RWContext
404 #undef xu
405 #undef xue
406 #undef xse
407 #undef u
408 #undef flag
409 #undef ue
410 #undef se
411 #undef infer
412 #undef more_rbsp_data
413 #undef byte_alignment
414 #undef allocate
415 
416 
417 static void cbs_h264_free_pps(void *unit, uint8_t *content)
418 {
419  H264RawPPS *pps = (H264RawPPS*)content;
421  av_freep(&content);
422 }
423 
425 {
426  switch (payload->payload_type) {
433  break;
436  break;
439  break;
440  default:
441  av_buffer_unref(&payload->payload.other.data_ref);
442  break;
443  }
444 }
445 
446 static void cbs_h264_free_sei(void *unit, uint8_t *content)
447 {
448  H264RawSEI *sei = (H264RawSEI*)content;
449  int i;
450  for (i = 0; i < sei->payload_count; i++)
452  av_freep(&content);
453 }
454 
455 static void cbs_h264_free_slice(void *unit, uint8_t *content)
456 {
457  H264RawSlice *slice = (H264RawSlice*)content;
458  av_buffer_unref(&slice->data_ref);
459  av_freep(&content);
460 }
461 
462 static void cbs_h265_free_vps(void *unit, uint8_t *content)
463 {
464  H265RawVPS *vps = (H265RawVPS*)content;
466  av_freep(&content);
467 }
468 
469 static void cbs_h265_free_sps(void *unit, uint8_t *content)
470 {
471  H265RawSPS *sps = (H265RawSPS*)content;
473  av_freep(&content);
474 }
475 
476 static void cbs_h265_free_pps(void *unit, uint8_t *content)
477 {
478  H265RawPPS *pps = (H265RawPPS*)content;
480  av_freep(&content);
481 }
482 
483 static void cbs_h265_free_slice(void *unit, uint8_t *content)
484 {
485  H265RawSlice *slice = (H265RawSlice*)content;
486  av_buffer_unref(&slice->data_ref);
487  av_freep(&content);
488 }
489 
491 {
492  switch (payload->payload_type) {
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  size_t size = nal->size;
535 
536  // Remove trailing zeroes.
537  while (size > 0 && nal->data[size - 1] == 0)
538  --size;
539  av_assert0(size > 0);
540 
541  if (nal->data == nal->raw_data) {
542  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
543  (uint8_t*)nal->data, size, frag->data_ref);
544  if (err < 0)
545  return err;
546  } else {
548  if (!data)
549  return AVERROR(ENOMEM);
550  memcpy(data, nal->data, size);
551  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
552 
553  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
554  data, size, NULL);
555  if (err < 0) {
556  av_freep(&data);
557  return err;
558  }
559  }
560  }
561 
562  return 0;
563 }
564 
567  int header)
568 {
569  enum AVCodecID codec_id = ctx->codec->codec_id;
571  GetByteContext gbc;
572  int err;
573 
574  av_assert0(frag->data && frag->nb_units == 0);
575  if (frag->data_size == 0)
576  return 0;
577 
578  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
579  // AVCC header.
580  size_t size, start, end;
581  int i, count, version;
582 
583  priv->mp4 = 1;
584 
585  bytestream2_init(&gbc, frag->data, frag->data_size);
586 
587  if (bytestream2_get_bytes_left(&gbc) < 6)
588  return AVERROR_INVALIDDATA;
589 
590  version = bytestream2_get_byte(&gbc);
591  if (version != 1) {
592  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
593  "first byte %u.", version);
594  return AVERROR_INVALIDDATA;
595  }
596 
597  bytestream2_skip(&gbc, 3);
598  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
599 
600  // SPS array.
601  count = bytestream2_get_byte(&gbc) & 0x1f;
602  start = bytestream2_tell(&gbc);
603  for (i = 0; i < count; i++) {
604  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
605  return AVERROR_INVALIDDATA;
606  size = bytestream2_get_be16(&gbc);
607  if (bytestream2_get_bytes_left(&gbc) < size)
608  return AVERROR_INVALIDDATA;
609  bytestream2_skip(&gbc, size);
610  }
611  end = bytestream2_tell(&gbc);
612 
613  err = ff_h2645_packet_split(&priv->read_packet,
614  frag->data + start, end - start,
615  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
616  if (err < 0) {
617  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
618  return err;
619  }
620  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
621  if (err < 0)
622  return err;
623 
624  // PPS array.
625  count = bytestream2_get_byte(&gbc);
626  start = bytestream2_tell(&gbc);
627  for (i = 0; i < count; i++) {
628  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
629  return AVERROR_INVALIDDATA;
630  size = bytestream2_get_be16(&gbc);
631  if (bytestream2_get_bytes_left(&gbc) < size)
632  return AVERROR_INVALIDDATA;
633  bytestream2_skip(&gbc, size);
634  }
635  end = bytestream2_tell(&gbc);
636 
637  err = ff_h2645_packet_split(&priv->read_packet,
638  frag->data + start, end - start,
639  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
640  if (err < 0) {
641  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
642  return err;
643  }
644  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
645  if (err < 0)
646  return err;
647 
648  if (bytestream2_get_bytes_left(&gbc) > 0) {
649  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
650  "header.\n", bytestream2_get_bytes_left(&gbc));
651  }
652 
653  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
654  // HVCC header.
655  size_t size, start, end;
656  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
657 
658  priv->mp4 = 1;
659 
660  bytestream2_init(&gbc, frag->data, frag->data_size);
661 
662  if (bytestream2_get_bytes_left(&gbc) < 23)
663  return AVERROR_INVALIDDATA;
664 
665  version = bytestream2_get_byte(&gbc);
666  if (version != 1) {
667  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
668  "first byte %u.", version);
669  return AVERROR_INVALIDDATA;
670  }
671 
672  bytestream2_skip(&gbc, 20);
673  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
674 
675  nb_arrays = bytestream2_get_byte(&gbc);
676  for (i = 0; i < nb_arrays; i++) {
677  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
678  nb_nals = bytestream2_get_be16(&gbc);
679 
680  start = bytestream2_tell(&gbc);
681  for (j = 0; j < nb_nals; j++) {
682  if (bytestream2_get_bytes_left(&gbc) < 2)
683  return AVERROR_INVALIDDATA;
684  size = bytestream2_get_be16(&gbc);
685  if (bytestream2_get_bytes_left(&gbc) < size)
686  return AVERROR_INVALIDDATA;
687  bytestream2_skip(&gbc, size);
688  }
689  end = bytestream2_tell(&gbc);
690 
691  err = ff_h2645_packet_split(&priv->read_packet,
692  frag->data + start, end - start,
693  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
694  if (err < 0) {
695  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
696  "HVCC array %d (%d NAL units of type %d).\n",
697  i, nb_nals, nal_unit_type);
698  return err;
699  }
700  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
701  if (err < 0)
702  return err;
703  }
704 
705  } else {
706  // Annex B, or later MP4 with already-known parameters.
707 
708  err = ff_h2645_packet_split(&priv->read_packet,
709  frag->data, frag->data_size,
710  ctx->log_ctx,
711  priv->mp4, priv->nal_length_size,
712  codec_id, 1);
713  if (err < 0)
714  return err;
715 
716  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
717  if (err < 0)
718  return err;
719  }
720 
721  return 0;
722 }
723 
724 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
725 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
726  CodedBitstreamUnit *unit) \
727 { \
728  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
729  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
730  unsigned int id = ps_var->id_element; \
731  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
732  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
733  " id : %d.\n", id); \
734  return AVERROR_INVALIDDATA; \
735  } \
736  if (priv->ps_var[id] == priv->active_ ## ps_var) \
737  priv->active_ ## ps_var = NULL ; \
738  av_buffer_unref(&priv->ps_var ## _ref[id]); \
739  if (unit->content_ref) \
740  priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
741  else \
742  priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
743  if (!priv->ps_var ## _ref[id]) \
744  return AVERROR(ENOMEM); \
745  priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
746  if (!unit->content_ref) \
747  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
748  return 0; \
749 }
750 
751 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
752 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
753 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
754 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
755 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
756 
757 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
758  CodedBitstreamUnit *unit)
759 {
760  GetBitContext gbc;
761  int err;
762 
763  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
764  if (err < 0)
765  return err;
766 
767  switch (unit->type) {
768  case H264_NAL_SPS:
769  {
770  H264RawSPS *sps;
771 
772  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
773  if (err < 0)
774  return err;
775  sps = unit->content;
776 
777  err = cbs_h264_read_sps(ctx, &gbc, sps);
778  if (err < 0)
779  return err;
780 
781  err = cbs_h264_replace_sps(ctx, unit);
782  if (err < 0)
783  return err;
784  }
785  break;
786 
787  case H264_NAL_SPS_EXT:
788  {
789  err = ff_cbs_alloc_unit_content(ctx, unit,
790  sizeof(H264RawSPSExtension),
791  NULL);
792  if (err < 0)
793  return err;
794 
795  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
796  if (err < 0)
797  return err;
798  }
799  break;
800 
801  case H264_NAL_PPS:
802  {
803  H264RawPPS *pps;
804 
805  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
807  if (err < 0)
808  return err;
809  pps = unit->content;
810 
811  err = cbs_h264_read_pps(ctx, &gbc, pps);
812  if (err < 0)
813  return err;
814 
815  err = cbs_h264_replace_pps(ctx, unit);
816  if (err < 0)
817  return err;
818  }
819  break;
820 
821  case H264_NAL_SLICE:
822  case H264_NAL_IDR_SLICE:
824  {
825  H264RawSlice *slice;
826  int pos, len;
827 
828  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
830  if (err < 0)
831  return err;
832  slice = unit->content;
833 
834  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
835  if (err < 0)
836  return err;
837 
838  pos = get_bits_count(&gbc);
839  len = unit->data_size;
840  if (!unit->data[len - 1]) {
841  int z;
842  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
843  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
844  "from slice data.\n", z);
845  len -= z;
846  }
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  pos = get_bits_count(&gbc);
1021  len = unit->data_size;
1022  if (!unit->data[len - 1]) {
1023  int z;
1024  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1025  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1026  "from slice data.\n", z);
1027  len -= z;
1028  }
1029 
1030  slice->data_size = len - pos / 8;
1031  slice->data_ref = av_buffer_ref(unit->data_ref);
1032  if (!slice->data_ref)
1033  return AVERROR(ENOMEM);
1034  slice->data = unit->data + pos / 8;
1035  slice->data_bit_start = pos % 8;
1036  }
1037  break;
1038 
1039  case HEVC_NAL_AUD:
1040  {
1041  err = ff_cbs_alloc_unit_content(ctx, unit,
1042  sizeof(H265RawAUD), NULL);
1043  if (err < 0)
1044  return err;
1045 
1046  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1047  if (err < 0)
1048  return err;
1049  }
1050  break;
1051 
1052  case HEVC_NAL_SEI_PREFIX:
1053  case HEVC_NAL_SEI_SUFFIX:
1054  {
1055  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1057 
1058  if (err < 0)
1059  return err;
1060 
1061  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1062  unit->type == HEVC_NAL_SEI_PREFIX);
1063 
1064  if (err < 0)
1065  return err;
1066  }
1067  break;
1068 
1069  default:
1070  return AVERROR(ENOSYS);
1071  }
1072 
1073  return 0;
1074 }
1075 
1077  PutBitContext *pbc, const uint8_t *data,
1078  size_t data_size, int data_bit_start)
1079 {
1080  size_t rest = data_size - (data_bit_start + 7) / 8;
1081  const uint8_t *pos = data + data_bit_start / 8;
1082 
1083  av_assert0(data_bit_start >= 0 &&
1084  8 * data_size > data_bit_start);
1085 
1086  if (data_size * 8 + 8 > put_bits_left(pbc))
1087  return AVERROR(ENOSPC);
1088 
1089  if (!rest)
1090  goto rbsp_stop_one_bit;
1091 
1092  // First copy the remaining bits of the first byte
1093  // The above check ensures that we do not accidentally
1094  // copy beyond the rbsp_stop_one_bit.
1095  if (data_bit_start % 8)
1096  put_bits(pbc, 8 - data_bit_start % 8,
1097  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1098 
1099  if (put_bits_count(pbc) % 8 == 0) {
1100  // If the writer is aligned at this point,
1101  // memcpy can be used to improve performance.
1102  // This happens normally for CABAC.
1103  flush_put_bits(pbc);
1104  memcpy(put_bits_ptr(pbc), pos, rest);
1105  skip_put_bytes(pbc, rest);
1106  } else {
1107  // If not, we have to copy manually.
1108  // rbsp_stop_one_bit forces us to special-case
1109  // the last byte.
1110  uint8_t temp;
1111  int i;
1112 
1113  for (; rest > 4; rest -= 4, pos += 4)
1114  put_bits32(pbc, AV_RB32(pos));
1115 
1116  for (; rest > 1; rest--, pos++)
1117  put_bits(pbc, 8, *pos);
1118 
1119  rbsp_stop_one_bit:
1120  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1121 
1122  av_assert0(temp);
1123  i = ff_ctz(*pos);
1124  temp = temp >> i;
1125  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1126  put_bits(pbc, i, temp);
1127  if (put_bits_count(pbc) % 8)
1128  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1129  }
1130 
1131  return 0;
1132 }
1133 
1135  CodedBitstreamUnit *unit,
1136  PutBitContext *pbc)
1137 {
1138  int err;
1139 
1140  switch (unit->type) {
1141  case H264_NAL_SPS:
1142  {
1143  H264RawSPS *sps = unit->content;
1144 
1145  err = cbs_h264_write_sps(ctx, pbc, sps);
1146  if (err < 0)
1147  return err;
1148 
1149  err = cbs_h264_replace_sps(ctx, unit);
1150  if (err < 0)
1151  return err;
1152  }
1153  break;
1154 
1155  case H264_NAL_SPS_EXT:
1156  {
1157  H264RawSPSExtension *sps_ext = unit->content;
1158 
1159  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1160  if (err < 0)
1161  return err;
1162  }
1163  break;
1164 
1165  case H264_NAL_PPS:
1166  {
1167  H264RawPPS *pps = unit->content;
1168 
1169  err = cbs_h264_write_pps(ctx, pbc, pps);
1170  if (err < 0)
1171  return err;
1172 
1173  err = cbs_h264_replace_pps(ctx, unit);
1174  if (err < 0)
1175  return err;
1176  }
1177  break;
1178 
1179  case H264_NAL_SLICE:
1180  case H264_NAL_IDR_SLICE:
1182  {
1183  H264RawSlice *slice = unit->content;
1184 
1185  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1186  if (err < 0)
1187  return err;
1188 
1189  if (slice->data) {
1190  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1191  slice->data_size,
1192  slice->data_bit_start);
1193  if (err < 0)
1194  return err;
1195  } else {
1196  // No slice data - that was just the header.
1197  // (Bitstream may be unaligned!)
1198  }
1199  }
1200  break;
1201 
1202  case H264_NAL_AUD:
1203  {
1204  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1205  if (err < 0)
1206  return err;
1207  }
1208  break;
1209 
1210  case H264_NAL_SEI:
1211  {
1212  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1213  if (err < 0)
1214  return err;
1215  }
1216  break;
1217 
1218  case H264_NAL_FILLER_DATA:
1219  {
1220  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1221  if (err < 0)
1222  return err;
1223  }
1224  break;
1225 
1226  case H264_NAL_END_SEQUENCE:
1227  {
1228  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1229  if (err < 0)
1230  return err;
1231  }
1232  break;
1233 
1234  case H264_NAL_END_STREAM:
1235  {
1236  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1237  if (err < 0)
1238  return err;
1239  }
1240  break;
1241 
1242  default:
1243  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1244  "NAL unit type %"PRIu32".\n", unit->type);
1245  return AVERROR_PATCHWELCOME;
1246  }
1247 
1248  return 0;
1249 }
1250 
1252  CodedBitstreamUnit *unit,
1253  PutBitContext *pbc)
1254 {
1255  int err;
1256 
1257  switch (unit->type) {
1258  case HEVC_NAL_VPS:
1259  {
1260  H265RawVPS *vps = unit->content;
1261 
1262  err = cbs_h265_write_vps(ctx, pbc, vps);
1263  if (err < 0)
1264  return err;
1265 
1266  err = cbs_h265_replace_vps(ctx, unit);
1267  if (err < 0)
1268  return err;
1269  }
1270  break;
1271 
1272  case HEVC_NAL_SPS:
1273  {
1274  H265RawSPS *sps = unit->content;
1275 
1276  err = cbs_h265_write_sps(ctx, pbc, sps);
1277  if (err < 0)
1278  return err;
1279 
1280  err = cbs_h265_replace_sps(ctx, unit);
1281  if (err < 0)
1282  return err;
1283  }
1284  break;
1285 
1286  case HEVC_NAL_PPS:
1287  {
1288  H265RawPPS *pps = unit->content;
1289 
1290  err = cbs_h265_write_pps(ctx, pbc, pps);
1291  if (err < 0)
1292  return err;
1293 
1294  err = cbs_h265_replace_pps(ctx, unit);
1295  if (err < 0)
1296  return err;
1297  }
1298  break;
1299 
1300  case HEVC_NAL_TRAIL_N:
1301  case HEVC_NAL_TRAIL_R:
1302  case HEVC_NAL_TSA_N:
1303  case HEVC_NAL_TSA_R:
1304  case HEVC_NAL_STSA_N:
1305  case HEVC_NAL_STSA_R:
1306  case HEVC_NAL_RADL_N:
1307  case HEVC_NAL_RADL_R:
1308  case HEVC_NAL_RASL_N:
1309  case HEVC_NAL_RASL_R:
1310  case HEVC_NAL_BLA_W_LP:
1311  case HEVC_NAL_BLA_W_RADL:
1312  case HEVC_NAL_BLA_N_LP:
1313  case HEVC_NAL_IDR_W_RADL:
1314  case HEVC_NAL_IDR_N_LP:
1315  case HEVC_NAL_CRA_NUT:
1316  {
1317  H265RawSlice *slice = unit->content;
1318 
1319  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1320  if (err < 0)
1321  return err;
1322 
1323  if (slice->data) {
1324  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1325  slice->data_size,
1326  slice->data_bit_start);
1327  if (err < 0)
1328  return err;
1329  } else {
1330  // No slice data - that was just the header.
1331  }
1332  }
1333  break;
1334 
1335  case HEVC_NAL_AUD:
1336  {
1337  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1338  if (err < 0)
1339  return err;
1340  }
1341  break;
1342 
1343  case HEVC_NAL_SEI_PREFIX:
1344  case HEVC_NAL_SEI_SUFFIX:
1345  {
1346  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1347  unit->type == HEVC_NAL_SEI_PREFIX);
1348 
1349  if (err < 0)
1350  return err;
1351  }
1352  break;
1353 
1354  default:
1355  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1356  "NAL unit type %"PRIu32".\n", unit->type);
1357  return AVERROR_PATCHWELCOME;
1358  }
1359 
1360  return 0;
1361 }
1362 
1364  CodedBitstreamUnit *unit)
1365 {
1367  enum AVCodecID codec_id = ctx->codec->codec_id;
1368  PutBitContext pbc;
1369  int err;
1370 
1371  if (!priv->write_buffer) {
1372  // Initial write buffer size is 1MB.
1373  priv->write_buffer_size = 1024 * 1024;
1374 
1375  reallocate_and_try_again:
1376  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1377  if (err < 0) {
1378  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1379  "sufficiently large write buffer (last attempt "
1380  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1381  return err;
1382  }
1383  }
1384 
1385  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1386 
1387  if (codec_id == AV_CODEC_ID_H264)
1388  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1389  else
1390  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1391 
1392  if (err == AVERROR(ENOSPC)) {
1393  // Overflow.
1394  priv->write_buffer_size *= 2;
1395  goto reallocate_and_try_again;
1396  }
1397  // Overflow but we didn't notice.
1398  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1399 
1400  if (err < 0) {
1401  // Write failed for some other reason.
1402  return err;
1403  }
1404 
1405  if (put_bits_count(&pbc) % 8)
1406  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1407  else
1408  unit->data_bit_padding = 0;
1409 
1410  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1411  flush_put_bits(&pbc);
1412 
1413  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1414  if (err < 0)
1415  return err;
1416 
1417  memcpy(unit->data, priv->write_buffer, unit->data_size);
1418 
1419  return 0;
1420 }
1421 
1423  CodedBitstreamFragment *frag)
1424 {
1425  uint8_t *data;
1426  size_t max_size, dp, sp;
1427  int err, i, zero_run;
1428 
1429  for (i = 0; i < frag->nb_units; i++) {
1430  // Data should already all have been written when we get here.
1431  av_assert0(frag->units[i].data);
1432  }
1433 
1434  max_size = 0;
1435  for (i = 0; i < frag->nb_units; i++) {
1436  // Start code + content with worst-case emulation prevention.
1437  max_size += 3 + frag->units[i].data_size * 3 / 2;
1438  }
1439 
1440  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1441  if (!data)
1442  return AVERROR(ENOMEM);
1443 
1444  dp = 0;
1445  for (i = 0; i < frag->nb_units; i++) {
1446  CodedBitstreamUnit *unit = &frag->units[i];
1447 
1448  if (unit->data_bit_padding > 0) {
1449  if (i < frag->nb_units - 1)
1450  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1451  "unaligned padding on non-final NAL unit.\n");
1452  else
1453  frag->data_bit_padding = unit->data_bit_padding;
1454  }
1455 
1456  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1457  (unit->type == H264_NAL_SPS ||
1458  unit->type == H264_NAL_PPS)) ||
1459  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1460  (unit->type == HEVC_NAL_VPS ||
1461  unit->type == HEVC_NAL_SPS ||
1462  unit->type == HEVC_NAL_PPS)) ||
1463  i == 0 /* (Assume this is the start of an access unit.) */) {
1464  // zero_byte
1465  data[dp++] = 0;
1466  }
1467  // start_code_prefix_one_3bytes
1468  data[dp++] = 0;
1469  data[dp++] = 0;
1470  data[dp++] = 1;
1471 
1472  zero_run = 0;
1473  for (sp = 0; sp < unit->data_size; sp++) {
1474  if (zero_run < 2) {
1475  if (unit->data[sp] == 0)
1476  ++zero_run;
1477  else
1478  zero_run = 0;
1479  } else {
1480  if ((unit->data[sp] & ~3) == 0) {
1481  // emulation_prevention_three_byte
1482  data[dp++] = 3;
1483  }
1484  zero_run = unit->data[sp] == 0;
1485  }
1486  data[dp++] = unit->data[sp];
1487  }
1488  }
1489 
1490  av_assert0(dp <= max_size);
1491  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1492  if (err)
1493  return err;
1494  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1495 
1497  NULL, NULL, 0);
1498  if (!frag->data_ref) {
1499  av_freep(&data);
1500  return AVERROR(ENOMEM);
1501  }
1502 
1503  frag->data = data;
1504  frag->data_size = dp;
1505 
1506  return 0;
1507 }
1508 
1510 {
1511  CodedBitstreamH264Context *h264 = ctx->priv_data;
1512  int i;
1513 
1515 
1516  av_freep(&h264->common.write_buffer);
1517 
1518  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1519  av_buffer_unref(&h264->sps_ref[i]);
1520  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1521  av_buffer_unref(&h264->pps_ref[i]);
1522 }
1523 
1525 {
1526  CodedBitstreamH265Context *h265 = ctx->priv_data;
1527  int i;
1528 
1530 
1531  av_freep(&h265->common.write_buffer);
1532 
1533  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1534  av_buffer_unref(&h265->vps_ref[i]);
1535  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1536  av_buffer_unref(&h265->sps_ref[i]);
1537  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1538  av_buffer_unref(&h265->pps_ref[i]);
1539 }
1540 
1543 
1544  .priv_data_size = sizeof(CodedBitstreamH264Context),
1545 
1546  .split_fragment = &cbs_h2645_split_fragment,
1547  .read_unit = &cbs_h264_read_nal_unit,
1548  .write_unit = &cbs_h2645_write_nal_unit,
1549  .assemble_fragment = &cbs_h2645_assemble_fragment,
1550 
1551  .close = &cbs_h264_close,
1552 };
1553 
1556 
1557  .priv_data_size = sizeof(CodedBitstreamH265Context),
1558 
1559  .split_fragment = &cbs_h2645_split_fragment,
1560  .read_unit = &cbs_h265_read_nal_unit,
1561  .write_unit = &cbs_h2645_write_nal_unit,
1562  .assemble_fragment = &cbs_h2645_assemble_fragment,
1563 
1564  .close = &cbs_h265_close,
1565 };
1566 
1569  const H264RawSEIPayload *payload)
1570 {
1571  H264RawSEI *sei;
1572  CodedBitstreamUnit *nal = NULL;
1573  int err, i;
1574 
1575  // Find an existing SEI NAL unit to add to.
1576  for (i = 0; i < au->nb_units; i++) {
1577  if (au->units[i].type == H264_NAL_SEI) {
1578  nal = &au->units[i];
1579  break;
1580  }
1581  }
1582  if (nal) {
1583  sei = nal->content;
1584 
1585  } else {
1586  // Need to make a new SEI NAL unit. Insert it before the first
1587  // slice data NAL unit; if no slice data, add at the end.
1588  AVBufferRef *sei_ref;
1589 
1590  sei = av_mallocz(sizeof(*sei));
1591  if (!sei)
1592  return AVERROR(ENOMEM);
1593 
1595  sei->nal_unit_header.nal_ref_idc = 0;
1596 
1597  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1598  &cbs_h264_free_sei, ctx, 0);
1599  if (!sei_ref) {
1600  av_freep(&sei);
1601  return AVERROR(ENOMEM);
1602  }
1603 
1604  for (i = 0; i < au->nb_units; i++) {
1605  if (au->units[i].type == H264_NAL_SLICE ||
1606  au->units[i].type == H264_NAL_IDR_SLICE)
1607  break;
1608  }
1609 
1610  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1611  sei, sei_ref);
1612  av_buffer_unref(&sei_ref);
1613  if (err < 0)
1614  return err;
1615  }
1616 
1617  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1618  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1619  "SEI NAL unit.\n");
1620  return AVERROR(EINVAL);
1621  }
1622 
1623  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1624  ++sei->payload_count;
1625 
1626  return 0;
1627 }
1628 
1631  CodedBitstreamUnit *nal,
1632  int position)
1633 {
1634  H264RawSEI *sei = nal->content;
1635 
1636  av_assert0(nal->type == H264_NAL_SEI);
1637  av_assert0(position >= 0 && position < sei->payload_count);
1638 
1639  if (position == 0 && sei->payload_count == 1) {
1640  // Deleting NAL unit entirely.
1641  int i;
1642 
1643  for (i = 0; i < au->nb_units; i++) {
1644  if (&au->units[i] == nal)
1645  break;
1646  }
1647  av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1648 
1649  return ff_cbs_delete_unit(ctx, au, i);
1650  } else {
1651  cbs_h264_free_sei_payload(&sei->payload[position]);
1652 
1653  --sei->payload_count;
1654  memmove(sei->payload + position,
1655  sei->payload + position + 1,
1656  (sei->payload_count - position) * sizeof(*sei->payload));
1657 
1658  return 0;
1659  }
1660 }
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1363
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: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:1076
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
AVBufferRef * sps_ref[H264_MAX_SPS_COUNT]
Definition: cbs_h264.h:446
int size
Definition: h2645_parse.h:34
union H264RawSEIPayload::@50 payload
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#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:1251
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:506
Picture parameter set.
Definition: h264_ps.h:109
int av_log2(unsigned v)
Definition: intmath.c:26
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:570
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:1422
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:1567
Macro definitions for various function/variable attributes.
pan-scan rectangle
Definition: h264_sei.h:30
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:197
static void cbs_h264_free_pps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:417
struct H264RawSEIPayload::@50::@51 other
uint8_t
#define av_malloc(s)
AVBufferRef * pps_ref[HEVC_MAX_PPS_COUNT]
Definition: cbs_h265.h: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
unregistered user data
Definition: h264_sei.h:33
static void cbs_h265_free_slice(void *unit, uint8_t *content)
Definition: cbs_h2645.c:483
display orientation
Definition: h264_sei.h:36
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:86
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:469
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
H264RawSPS * sps[H264_MAX_SPS_COUNT]
Definition: cbs_h264.h:448
#define sp
Definition: regdef.h:63
H265RawSEIPayload payload[H265_MAX_SEI_PAYLOADS]
Definition: cbs_h265.h:712
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1509
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:527
Coded bitstream unit structure.
Definition: cbs.h:64
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:153
H265RawPSExtensionData extension_data
Definition: cbs_h265.h: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:607
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
H.264 common definitions.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:814
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
buffering period (H.264, D.1.1)
Definition: h264_sei.h:28
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static 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
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
union H265RawSEIPayload::@55 payload
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBufferRef * data_ref
Definition: cbs_h265.h: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:164
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
Definition: cbs_h2645.c:424
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1541
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1524
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
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:443
int ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:644
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
static void cbs_h265_free_vps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:462
size_t data_size
Definition: cbs_h264.h:428
int type
NAL unit type.
Definition: h2645_parse.h:51
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
#define FF_ARRAY_ELEMS(a)
AVBufferRef * pps_ref[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:447
H264RawSEIUserDataRegistered user_data_registered
Definition: cbs_h264.h:326
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
uint8_t payload_count
Definition: cbs_h265.h: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
const uint8_t * data
Definition: h2645_parse.h:35
AVBufferRef * slice_group_id_ref
Definition: cbs_h264.h:200
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:159
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:615
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
recovery point (frame # to decoder sync)
Definition: h264_sei.h:34
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:307
enum AVCodecID codec_id
Definition: cbs_internal.h:29
registered user data as specified by Rec. ITU-T T.35
Definition: h264_sei.h:32
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
Definition: cbs_h264.h:342
static void cbs_h265_free_sei(void *unit, uint8_t *content)
Definition: cbs_h2645.c:517
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
#define SIZE_SPECIFIER
Definition: internal.h:262
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:87
AVBufferRef * vps_ref[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:722
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:329
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:1554
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
const uint8_t * raw_data
Definition: h2645_parse.h:44
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: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:1629
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:1134
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:724
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:565
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:376
uint8_t * data
Definition: cbs_h264.h:427
uint8_t payload_count
Definition: cbs_h264.h:343
static void cbs_h264_free_sei(void *unit, uint8_t *content)
Definition: cbs_h2645.c:446
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80
const char * name
Definition: opengl_enc.c:103
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition: cbs_h2645.c:526
static void cbs_h265_free_sps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:469