FFmpeg  4.1.5
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  av_assert0(size > 0);
548 
550  if (!data)
551  return AVERROR(ENOMEM);
552  memcpy(data, nal->data, size);
553  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
554 
555  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
556  data, size, NULL);
557  if (err < 0) {
558  av_freep(&data);
559  return err;
560  }
561  }
562 
563  return 0;
564 }
565 
568  int header)
569 {
570  enum AVCodecID codec_id = ctx->codec->codec_id;
572  GetByteContext gbc;
573  int err;
574 
575  av_assert0(frag->data && frag->nb_units == 0);
576  if (frag->data_size == 0)
577  return 0;
578 
579  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
580  // AVCC header.
581  size_t size, start, end;
582  int i, count, version;
583 
584  priv->mp4 = 1;
585 
586  bytestream2_init(&gbc, frag->data, frag->data_size);
587 
588  if (bytestream2_get_bytes_left(&gbc) < 6)
589  return AVERROR_INVALIDDATA;
590 
591  version = bytestream2_get_byte(&gbc);
592  if (version != 1) {
593  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
594  "first byte %u.", version);
595  return AVERROR_INVALIDDATA;
596  }
597 
598  bytestream2_skip(&gbc, 3);
599  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
600 
601  // SPS array.
602  count = bytestream2_get_byte(&gbc) & 0x1f;
603  start = bytestream2_tell(&gbc);
604  for (i = 0; i < count; i++) {
605  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
606  return AVERROR_INVALIDDATA;
607  size = bytestream2_get_be16(&gbc);
608  if (bytestream2_get_bytes_left(&gbc) < size)
609  return AVERROR_INVALIDDATA;
610  bytestream2_skip(&gbc, size);
611  }
612  end = bytestream2_tell(&gbc);
613 
614  err = ff_h2645_packet_split(&priv->read_packet,
615  frag->data + start, end - start,
616  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
617  if (err < 0) {
618  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
619  return err;
620  }
621  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
622  if (err < 0)
623  return err;
624 
625  // PPS array.
626  count = bytestream2_get_byte(&gbc);
627  start = bytestream2_tell(&gbc);
628  for (i = 0; i < count; i++) {
629  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
630  return AVERROR_INVALIDDATA;
631  size = bytestream2_get_be16(&gbc);
632  if (bytestream2_get_bytes_left(&gbc) < size)
633  return AVERROR_INVALIDDATA;
634  bytestream2_skip(&gbc, size);
635  }
636  end = bytestream2_tell(&gbc);
637 
638  err = ff_h2645_packet_split(&priv->read_packet,
639  frag->data + start, end - start,
640  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
641  if (err < 0) {
642  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
643  return err;
644  }
645  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
646  if (err < 0)
647  return err;
648 
649  if (bytestream2_get_bytes_left(&gbc) > 0) {
650  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
651  "header.\n", bytestream2_get_bytes_left(&gbc));
652  }
653 
654  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
655  // HVCC header.
656  size_t size, start, end;
657  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
658 
659  priv->mp4 = 1;
660 
661  bytestream2_init(&gbc, frag->data, frag->data_size);
662 
663  if (bytestream2_get_bytes_left(&gbc) < 23)
664  return AVERROR_INVALIDDATA;
665 
666  version = bytestream2_get_byte(&gbc);
667  if (version != 1) {
668  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
669  "first byte %u.", version);
670  return AVERROR_INVALIDDATA;
671  }
672 
673  bytestream2_skip(&gbc, 20);
674  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
675 
676  nb_arrays = bytestream2_get_byte(&gbc);
677  for (i = 0; i < nb_arrays; i++) {
678  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
679  nb_nals = bytestream2_get_be16(&gbc);
680 
681  start = bytestream2_tell(&gbc);
682  for (j = 0; j < nb_nals; j++) {
683  if (bytestream2_get_bytes_left(&gbc) < 2)
684  return AVERROR_INVALIDDATA;
685  size = bytestream2_get_be16(&gbc);
686  if (bytestream2_get_bytes_left(&gbc) < size)
687  return AVERROR_INVALIDDATA;
688  bytestream2_skip(&gbc, size);
689  }
690  end = bytestream2_tell(&gbc);
691 
692  err = ff_h2645_packet_split(&priv->read_packet,
693  frag->data + start, end - start,
694  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
695  if (err < 0) {
696  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
697  "HVCC array %d (%d NAL units of type %d).\n",
698  i, nb_nals, nal_unit_type);
699  return err;
700  }
701  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
702  if (err < 0)
703  return err;
704  }
705 
706  } else {
707  // Annex B, or later MP4 with already-known parameters.
708 
709  err = ff_h2645_packet_split(&priv->read_packet,
710  frag->data, frag->data_size,
711  ctx->log_ctx,
712  priv->mp4, priv->nal_length_size,
713  codec_id, 1);
714  if (err < 0)
715  return err;
716 
717  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
718  if (err < 0)
719  return err;
720  }
721 
722  return 0;
723 }
724 
725 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
726 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
727  CodedBitstreamUnit *unit) \
728 { \
729  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
730  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
731  unsigned int id = ps_var->id_element; \
732  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
733  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
734  " id : %d.\n", id); \
735  return AVERROR_INVALIDDATA; \
736  } \
737  if (priv->ps_var[id] == priv->active_ ## ps_var) \
738  priv->active_ ## ps_var = NULL ; \
739  av_buffer_unref(&priv->ps_var ## _ref[id]); \
740  if (unit->content_ref) \
741  priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
742  else \
743  priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
744  if (!priv->ps_var ## _ref[id]) \
745  return AVERROR(ENOMEM); \
746  priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
747  if (!unit->content_ref) \
748  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
749  return 0; \
750 }
751 
752 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
753 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
754 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
755 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
756 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
757 
758 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
759  CodedBitstreamUnit *unit)
760 {
761  GetBitContext gbc;
762  int err;
763 
764  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
765  if (err < 0)
766  return err;
767 
768  switch (unit->type) {
769  case H264_NAL_SPS:
770  {
771  H264RawSPS *sps;
772 
773  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
774  if (err < 0)
775  return err;
776  sps = unit->content;
777 
778  err = cbs_h264_read_sps(ctx, &gbc, sps);
779  if (err < 0)
780  return err;
781 
782  err = cbs_h264_replace_sps(ctx, unit);
783  if (err < 0)
784  return err;
785  }
786  break;
787 
788  case H264_NAL_SPS_EXT:
789  {
790  err = ff_cbs_alloc_unit_content(ctx, unit,
791  sizeof(H264RawSPSExtension),
792  NULL);
793  if (err < 0)
794  return err;
795 
796  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
797  if (err < 0)
798  return err;
799  }
800  break;
801 
802  case H264_NAL_PPS:
803  {
804  H264RawPPS *pps;
805 
806  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
808  if (err < 0)
809  return err;
810  pps = unit->content;
811 
812  err = cbs_h264_read_pps(ctx, &gbc, pps);
813  if (err < 0)
814  return err;
815 
816  err = cbs_h264_replace_pps(ctx, unit);
817  if (err < 0)
818  return err;
819  }
820  break;
821 
822  case H264_NAL_SLICE:
823  case H264_NAL_IDR_SLICE:
825  {
826  H264RawSlice *slice;
827  int pos, len;
828 
829  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
831  if (err < 0)
832  return err;
833  slice = unit->content;
834 
835  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
836  if (err < 0)
837  return err;
838 
839  pos = get_bits_count(&gbc);
840  len = unit->data_size;
841  if (!unit->data[len - 1]) {
842  int z;
843  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
844  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
845  "from slice data.\n", z);
846  len -= z;
847  }
848 
849  slice->data_size = len - pos / 8;
850  slice->data_ref = av_buffer_ref(unit->data_ref);
851  if (!slice->data_ref)
852  return AVERROR(ENOMEM);
853  slice->data = unit->data + pos / 8;
854  slice->data_bit_start = pos % 8;
855  }
856  break;
857 
858  case H264_NAL_AUD:
859  {
860  err = ff_cbs_alloc_unit_content(ctx, unit,
861  sizeof(H264RawAUD), NULL);
862  if (err < 0)
863  return err;
864 
865  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
866  if (err < 0)
867  return err;
868  }
869  break;
870 
871  case H264_NAL_SEI:
872  {
873  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
875  if (err < 0)
876  return err;
877 
878  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
879  if (err < 0)
880  return err;
881  }
882  break;
883 
885  {
886  err = ff_cbs_alloc_unit_content(ctx, unit,
887  sizeof(H264RawFiller), NULL);
888  if (err < 0)
889  return err;
890 
891  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
892  if (err < 0)
893  return err;
894  }
895  break;
896 
898  case H264_NAL_END_STREAM:
899  {
900  err = ff_cbs_alloc_unit_content(ctx, unit,
901  sizeof(H264RawNALUnitHeader),
902  NULL);
903  if (err < 0)
904  return err;
905 
906  err = (unit->type == H264_NAL_END_SEQUENCE ?
907  cbs_h264_read_end_of_sequence :
908  cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
909  if (err < 0)
910  return err;
911  }
912  break;
913 
914  default:
915  return AVERROR(ENOSYS);
916  }
917 
918  return 0;
919 }
920 
922  CodedBitstreamUnit *unit)
923 {
924  GetBitContext gbc;
925  int err;
926 
927  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
928  if (err < 0)
929  return err;
930 
931  switch (unit->type) {
932  case HEVC_NAL_VPS:
933  {
934  H265RawVPS *vps;
935 
936  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
938  if (err < 0)
939  return err;
940  vps = unit->content;
941 
942  err = cbs_h265_read_vps(ctx, &gbc, vps);
943  if (err < 0)
944  return err;
945 
946  err = cbs_h265_replace_vps(ctx, unit);
947  if (err < 0)
948  return err;
949  }
950  break;
951  case HEVC_NAL_SPS:
952  {
953  H265RawSPS *sps;
954 
955  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
957  if (err < 0)
958  return err;
959  sps = unit->content;
960 
961  err = cbs_h265_read_sps(ctx, &gbc, sps);
962  if (err < 0)
963  return err;
964 
965  err = cbs_h265_replace_sps(ctx, unit);
966  if (err < 0)
967  return err;
968  }
969  break;
970 
971  case HEVC_NAL_PPS:
972  {
973  H265RawPPS *pps;
974 
975  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
977  if (err < 0)
978  return err;
979  pps = unit->content;
980 
981  err = cbs_h265_read_pps(ctx, &gbc, pps);
982  if (err < 0)
983  return err;
984 
985  err = cbs_h265_replace_pps(ctx, unit);
986  if (err < 0)
987  return err;
988  }
989  break;
990 
991  case HEVC_NAL_TRAIL_N:
992  case HEVC_NAL_TRAIL_R:
993  case HEVC_NAL_TSA_N:
994  case HEVC_NAL_TSA_R:
995  case HEVC_NAL_STSA_N:
996  case HEVC_NAL_STSA_R:
997  case HEVC_NAL_RADL_N:
998  case HEVC_NAL_RADL_R:
999  case HEVC_NAL_RASL_N:
1000  case HEVC_NAL_RASL_R:
1001  case HEVC_NAL_BLA_W_LP:
1002  case HEVC_NAL_BLA_W_RADL:
1003  case HEVC_NAL_BLA_N_LP:
1004  case HEVC_NAL_IDR_W_RADL:
1005  case HEVC_NAL_IDR_N_LP:
1006  case HEVC_NAL_CRA_NUT:
1007  {
1008  H265RawSlice *slice;
1009  int pos, len;
1010 
1011  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1013  if (err < 0)
1014  return err;
1015  slice = unit->content;
1016 
1017  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1018  if (err < 0)
1019  return err;
1020 
1021  pos = get_bits_count(&gbc);
1022  len = unit->data_size;
1023  if (!unit->data[len - 1]) {
1024  int z;
1025  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1026  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1027  "from slice data.\n", z);
1028  len -= z;
1029  }
1030 
1031  slice->data_size = len - pos / 8;
1032  slice->data_ref = av_buffer_ref(unit->data_ref);
1033  if (!slice->data_ref)
1034  return AVERROR(ENOMEM);
1035  slice->data = unit->data + pos / 8;
1036  slice->data_bit_start = pos % 8;
1037  }
1038  break;
1039 
1040  case HEVC_NAL_AUD:
1041  {
1042  err = ff_cbs_alloc_unit_content(ctx, unit,
1043  sizeof(H265RawAUD), NULL);
1044  if (err < 0)
1045  return err;
1046 
1047  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1048  if (err < 0)
1049  return err;
1050  }
1051  break;
1052 
1053  case HEVC_NAL_SEI_PREFIX:
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 
1063  if (err < 0)
1064  return err;
1065  }
1066  break;
1067 
1068  default:
1069  return AVERROR(ENOSYS);
1070  }
1071 
1072  return 0;
1073 }
1074 
1076  CodedBitstreamUnit *unit,
1077  PutBitContext *pbc)
1078 {
1079  int err;
1080 
1081  switch (unit->type) {
1082  case H264_NAL_SPS:
1083  {
1084  H264RawSPS *sps = unit->content;
1085 
1086  err = cbs_h264_write_sps(ctx, pbc, sps);
1087  if (err < 0)
1088  return err;
1089 
1090  err = cbs_h264_replace_sps(ctx, unit);
1091  if (err < 0)
1092  return err;
1093  }
1094  break;
1095 
1096  case H264_NAL_SPS_EXT:
1097  {
1098  H264RawSPSExtension *sps_ext = unit->content;
1099 
1100  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1101  if (err < 0)
1102  return err;
1103  }
1104  break;
1105 
1106  case H264_NAL_PPS:
1107  {
1108  H264RawPPS *pps = unit->content;
1109 
1110  err = cbs_h264_write_pps(ctx, pbc, pps);
1111  if (err < 0)
1112  return err;
1113 
1114  err = cbs_h264_replace_pps(ctx, unit);
1115  if (err < 0)
1116  return err;
1117  }
1118  break;
1119 
1120  case H264_NAL_SLICE:
1121  case H264_NAL_IDR_SLICE:
1123  {
1124  H264RawSlice *slice = unit->content;
1125  GetBitContext gbc;
1126  int bits_left, end, zeroes;
1127 
1128  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1129  if (err < 0)
1130  return err;
1131 
1132  if (slice->data) {
1133  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1134  return AVERROR(ENOSPC);
1135 
1136  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1137  skip_bits_long(&gbc, slice->data_bit_start);
1138 
1139  // Copy in two-byte blocks, but stop before copying the
1140  // rbsp_stop_one_bit in the final byte.
1141  while (get_bits_left(&gbc) > 23)
1142  put_bits(pbc, 16, get_bits(&gbc, 16));
1143 
1144  bits_left = get_bits_left(&gbc);
1145  end = get_bits(&gbc, bits_left);
1146 
1147  // rbsp_stop_one_bit must be present here.
1148  av_assert0(end);
1149  zeroes = ff_ctz(end);
1150  if (bits_left > zeroes + 1)
1151  put_bits(pbc, bits_left - zeroes - 1,
1152  end >> (zeroes + 1));
1153  put_bits(pbc, 1, 1);
1154  while (put_bits_count(pbc) % 8 != 0)
1155  put_bits(pbc, 1, 0);
1156  } else {
1157  // No slice data - that was just the header.
1158  // (Bitstream may be unaligned!)
1159  }
1160  }
1161  break;
1162 
1163  case H264_NAL_AUD:
1164  {
1165  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1166  if (err < 0)
1167  return err;
1168  }
1169  break;
1170 
1171  case H264_NAL_SEI:
1172  {
1173  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1174  if (err < 0)
1175  return err;
1176  }
1177  break;
1178 
1179  case H264_NAL_FILLER_DATA:
1180  {
1181  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1182  if (err < 0)
1183  return err;
1184  }
1185  break;
1186 
1187  case H264_NAL_END_SEQUENCE:
1188  {
1189  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1190  if (err < 0)
1191  return err;
1192  }
1193  break;
1194 
1195  case H264_NAL_END_STREAM:
1196  {
1197  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1198  if (err < 0)
1199  return err;
1200  }
1201  break;
1202 
1203  default:
1204  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1205  "NAL unit type %"PRIu32".\n", unit->type);
1206  return AVERROR_PATCHWELCOME;
1207  }
1208 
1209  return 0;
1210 }
1211 
1213  CodedBitstreamUnit *unit,
1214  PutBitContext *pbc)
1215 {
1216  int err;
1217 
1218  switch (unit->type) {
1219  case HEVC_NAL_VPS:
1220  {
1221  H265RawVPS *vps = unit->content;
1222 
1223  err = cbs_h265_write_vps(ctx, pbc, vps);
1224  if (err < 0)
1225  return err;
1226 
1227  err = cbs_h265_replace_vps(ctx, unit);
1228  if (err < 0)
1229  return err;
1230  }
1231  break;
1232 
1233  case HEVC_NAL_SPS:
1234  {
1235  H265RawSPS *sps = unit->content;
1236 
1237  err = cbs_h265_write_sps(ctx, pbc, sps);
1238  if (err < 0)
1239  return err;
1240 
1241  err = cbs_h265_replace_sps(ctx, unit);
1242  if (err < 0)
1243  return err;
1244  }
1245  break;
1246 
1247  case HEVC_NAL_PPS:
1248  {
1249  H265RawPPS *pps = unit->content;
1250 
1251  err = cbs_h265_write_pps(ctx, pbc, pps);
1252  if (err < 0)
1253  return err;
1254 
1255  err = cbs_h265_replace_pps(ctx, unit);
1256  if (err < 0)
1257  return err;
1258  }
1259  break;
1260 
1261  case HEVC_NAL_TRAIL_N:
1262  case HEVC_NAL_TRAIL_R:
1263  case HEVC_NAL_TSA_N:
1264  case HEVC_NAL_TSA_R:
1265  case HEVC_NAL_STSA_N:
1266  case HEVC_NAL_STSA_R:
1267  case HEVC_NAL_RADL_N:
1268  case HEVC_NAL_RADL_R:
1269  case HEVC_NAL_RASL_N:
1270  case HEVC_NAL_RASL_R:
1271  case HEVC_NAL_BLA_W_LP:
1272  case HEVC_NAL_BLA_W_RADL:
1273  case HEVC_NAL_BLA_N_LP:
1274  case HEVC_NAL_IDR_W_RADL:
1275  case HEVC_NAL_IDR_N_LP:
1276  case HEVC_NAL_CRA_NUT:
1277  {
1278  H265RawSlice *slice = unit->content;
1279  GetBitContext gbc;
1280  int bits_left, end, zeroes;
1281 
1282  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1283  if (err < 0)
1284  return err;
1285 
1286  if (slice->data) {
1287  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1288  return AVERROR(ENOSPC);
1289 
1290  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1291  skip_bits_long(&gbc, slice->data_bit_start);
1292 
1293  // Copy in two-byte blocks, but stop before copying the
1294  // rbsp_stop_one_bit in the final byte.
1295  while (get_bits_left(&gbc) > 23)
1296  put_bits(pbc, 16, get_bits(&gbc, 16));
1297 
1298  bits_left = get_bits_left(&gbc);
1299  end = get_bits(&gbc, bits_left);
1300 
1301  // rbsp_stop_one_bit must be present here.
1302  av_assert0(end);
1303  zeroes = ff_ctz(end);
1304  if (bits_left > zeroes + 1)
1305  put_bits(pbc, bits_left - zeroes - 1,
1306  end >> (zeroes + 1));
1307  put_bits(pbc, 1, 1);
1308  while (put_bits_count(pbc) % 8 != 0)
1309  put_bits(pbc, 1, 0);
1310  } else {
1311  // No slice data - that was just the header.
1312  }
1313  }
1314  break;
1315 
1316  case HEVC_NAL_AUD:
1317  {
1318  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1319  if (err < 0)
1320  return err;
1321  }
1322  break;
1323 
1324  case HEVC_NAL_SEI_PREFIX:
1325  {
1326  err = cbs_h265_write_sei(ctx, pbc, unit->content);
1327 
1328  if (err < 0)
1329  return err;
1330  }
1331  break;
1332 
1333  default:
1334  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1335  "NAL unit type %"PRIu32".\n", unit->type);
1336  return AVERROR_PATCHWELCOME;
1337  }
1338 
1339  return 0;
1340 }
1341 
1343  CodedBitstreamUnit *unit)
1344 {
1346  enum AVCodecID codec_id = ctx->codec->codec_id;
1347  PutBitContext pbc;
1348  int err;
1349 
1350  if (!priv->write_buffer) {
1351  // Initial write buffer size is 1MB.
1352  priv->write_buffer_size = 1024 * 1024;
1353 
1354  reallocate_and_try_again:
1355  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1356  if (err < 0) {
1357  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1358  "sufficiently large write buffer (last attempt "
1359  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1360  return err;
1361  }
1362  }
1363 
1364  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1365 
1366  if (codec_id == AV_CODEC_ID_H264)
1367  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1368  else
1369  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1370 
1371  if (err == AVERROR(ENOSPC)) {
1372  // Overflow.
1373  priv->write_buffer_size *= 2;
1374  goto reallocate_and_try_again;
1375  }
1376  // Overflow but we didn't notice.
1377  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1378 
1379  if (err < 0) {
1380  // Write failed for some other reason.
1381  return err;
1382  }
1383 
1384  if (put_bits_count(&pbc) % 8)
1385  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1386  else
1387  unit->data_bit_padding = 0;
1388 
1389  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1390  flush_put_bits(&pbc);
1391 
1392  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1393  if (err < 0)
1394  return err;
1395 
1396  memcpy(unit->data, priv->write_buffer, unit->data_size);
1397 
1398  return 0;
1399 }
1400 
1402  CodedBitstreamFragment *frag)
1403 {
1404  uint8_t *data;
1405  size_t max_size, dp, sp;
1406  int err, i, zero_run;
1407 
1408  for (i = 0; i < frag->nb_units; i++) {
1409  // Data should already all have been written when we get here.
1410  av_assert0(frag->units[i].data);
1411  }
1412 
1413  max_size = 0;
1414  for (i = 0; i < frag->nb_units; i++) {
1415  // Start code + content with worst-case emulation prevention.
1416  max_size += 3 + frag->units[i].data_size * 3 / 2;
1417  }
1418 
1419  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1420  if (!data)
1421  return AVERROR(ENOMEM);
1422 
1423  dp = 0;
1424  for (i = 0; i < frag->nb_units; i++) {
1425  CodedBitstreamUnit *unit = &frag->units[i];
1426 
1427  if (unit->data_bit_padding > 0) {
1428  if (i < frag->nb_units - 1)
1429  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1430  "unaligned padding on non-final NAL unit.\n");
1431  else
1432  frag->data_bit_padding = unit->data_bit_padding;
1433  }
1434 
1435  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1436  (unit->type == H264_NAL_SPS ||
1437  unit->type == H264_NAL_PPS)) ||
1438  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1439  (unit->type == HEVC_NAL_VPS ||
1440  unit->type == HEVC_NAL_SPS ||
1441  unit->type == HEVC_NAL_PPS)) ||
1442  i == 0 /* (Assume this is the start of an access unit.) */) {
1443  // zero_byte
1444  data[dp++] = 0;
1445  }
1446  // start_code_prefix_one_3bytes
1447  data[dp++] = 0;
1448  data[dp++] = 0;
1449  data[dp++] = 1;
1450 
1451  zero_run = 0;
1452  for (sp = 0; sp < unit->data_size; sp++) {
1453  if (zero_run < 2) {
1454  if (unit->data[sp] == 0)
1455  ++zero_run;
1456  else
1457  zero_run = 0;
1458  } else {
1459  if ((unit->data[sp] & ~3) == 0) {
1460  // emulation_prevention_three_byte
1461  data[dp++] = 3;
1462  }
1463  zero_run = unit->data[sp] == 0;
1464  }
1465  data[dp++] = unit->data[sp];
1466  }
1467  }
1468 
1469  av_assert0(dp <= max_size);
1470  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1471  if (err)
1472  return err;
1473  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1474 
1476  NULL, NULL, 0);
1477  if (!frag->data_ref) {
1478  av_freep(&data);
1479  return AVERROR(ENOMEM);
1480  }
1481 
1482  frag->data = data;
1483  frag->data_size = dp;
1484 
1485  return 0;
1486 }
1487 
1489 {
1490  CodedBitstreamH264Context *h264 = ctx->priv_data;
1491  int i;
1492 
1494 
1495  av_freep(&h264->common.write_buffer);
1496 
1497  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1498  av_buffer_unref(&h264->sps_ref[i]);
1499  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1500  av_buffer_unref(&h264->pps_ref[i]);
1501 }
1502 
1504 {
1505  CodedBitstreamH265Context *h265 = ctx->priv_data;
1506  int i;
1507 
1509 
1510  av_freep(&h265->common.write_buffer);
1511 
1512  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1513  av_buffer_unref(&h265->vps_ref[i]);
1514  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1515  av_buffer_unref(&h265->sps_ref[i]);
1516  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1517  av_buffer_unref(&h265->pps_ref[i]);
1518 }
1519 
1522 
1523  .priv_data_size = sizeof(CodedBitstreamH264Context),
1524 
1525  .split_fragment = &cbs_h2645_split_fragment,
1526  .read_unit = &cbs_h264_read_nal_unit,
1527  .write_unit = &cbs_h2645_write_nal_unit,
1528  .assemble_fragment = &cbs_h2645_assemble_fragment,
1529 
1530  .close = &cbs_h264_close,
1531 };
1532 
1535 
1536  .priv_data_size = sizeof(CodedBitstreamH265Context),
1537 
1538  .split_fragment = &cbs_h2645_split_fragment,
1539  .read_unit = &cbs_h265_read_nal_unit,
1540  .write_unit = &cbs_h2645_write_nal_unit,
1541  .assemble_fragment = &cbs_h2645_assemble_fragment,
1542 
1543  .close = &cbs_h265_close,
1544 };
1545 
1548  const H264RawSEIPayload *payload)
1549 {
1550  H264RawSEI *sei;
1551  CodedBitstreamUnit *nal = NULL;
1552  int err, i;
1553 
1554  // Find an existing SEI NAL unit to add to.
1555  for (i = 0; i < au->nb_units; i++) {
1556  if (au->units[i].type == H264_NAL_SEI) {
1557  nal = &au->units[i];
1558  break;
1559  }
1560  }
1561  if (nal) {
1562  sei = nal->content;
1563 
1564  } else {
1565  // Need to make a new SEI NAL unit. Insert it before the first
1566  // slice data NAL unit; if no slice data, add at the end.
1567  AVBufferRef *sei_ref;
1568 
1569  sei = av_mallocz(sizeof(*sei));
1570  if (!sei)
1571  return AVERROR(ENOMEM);
1572 
1574  sei->nal_unit_header.nal_ref_idc = 0;
1575 
1576  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1577  &cbs_h264_free_sei, ctx, 0);
1578  if (!sei_ref) {
1579  av_freep(&sei);
1580  return AVERROR(ENOMEM);
1581  }
1582 
1583  for (i = 0; i < au->nb_units; i++) {
1584  if (au->units[i].type == H264_NAL_SLICE ||
1585  au->units[i].type == H264_NAL_IDR_SLICE)
1586  break;
1587  }
1588 
1589  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1590  sei, sei_ref);
1591  av_buffer_unref(&sei_ref);
1592  if (err < 0)
1593  return err;
1594  }
1595 
1596  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1597  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1598  "SEI NAL unit.\n");
1599  return AVERROR(EINVAL);
1600  }
1601 
1602  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1603  ++sei->payload_count;
1604 
1605  return 0;
1606 }
1607 
1610  CodedBitstreamUnit *nal,
1611  int position)
1612 {
1613  H264RawSEI *sei = nal->content;
1614 
1615  av_assert0(nal->type == H264_NAL_SEI);
1616  av_assert0(position >= 0 && position < sei->payload_count);
1617 
1618  if (position == 0 && sei->payload_count == 1) {
1619  // Deleting NAL unit entirely.
1620  int i;
1621 
1622  for (i = 0; i < au->nb_units; i++) {
1623  if (&au->units[i] == nal)
1624  break;
1625  }
1626  av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1627 
1628  return ff_cbs_delete_unit(ctx, au, i);
1629  } else {
1630  cbs_h264_free_sei_payload(&sei->payload[position]);
1631 
1632  --sei->payload_count;
1633  memmove(sei->payload + position,
1634  sei->payload + position + 1,
1635  (sei->payload_count - position) * sizeof(*sei->payload));
1636 
1637  return 0;
1638  }
1639 }
const char * name
Definition: avisynth_c.h:775
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1342
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:1212
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:921
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:1401
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:1546
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
H265RawSEIPayload payload[H265_MAX_SEI_PAYLOADS]
Definition: cbs_h265.h:558
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1488
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
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBufferRef * data_ref
Definition: cbs_h265.h:524
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
simple assert() macros that are a bit more flexible than ISO C assert().
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:1520
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1503
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:262
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:87
AVBufferRef * vps_ref[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:568
void * priv_data
Internal codec-specific data.
Definition: cbs.h:180
AVBufferRef * data_ref
Definition: cbs_h264.h:430
A reference to a data buffer.
Definition: buffer.h:81
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:305
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
struct H265RawSEIPayload::@55::@56 other
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1533
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:1608
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:1075
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:725
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:566
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