FFmpeg  4.2.3
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 "h264.h"
28 #include "h264_sei.h"
29 #include "h2645_parse.h"
30 #include "hevc.h"
31 #include "hevc_sei.h"
32 
33 
35  const char *name, const int *subscripts,
36  uint32_t *write_to,
37  uint32_t range_min, uint32_t range_max)
38 {
39  uint32_t value;
40  int position, i, j;
41  unsigned int k;
42  char bits[65];
43 
44  position = get_bits_count(gbc);
45 
46  for (i = 0; i < 32; i++) {
47  if (get_bits_left(gbc) < i + 1) {
48  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
49  "%s: bitstream ended.\n", name);
50  return AVERROR_INVALIDDATA;
51  }
52  k = get_bits1(gbc);
53  bits[i] = k ? '1' : '0';
54  if (k)
55  break;
56  }
57  if (i >= 32) {
58  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
59  "%s: more than 31 zeroes.\n", name);
60  return AVERROR_INVALIDDATA;
61  }
62  value = 1;
63  for (j = 0; j < i; j++) {
64  k = get_bits1(gbc);
65  bits[i + j + 1] = k ? '1' : '0';
66  value = value << 1 | k;
67  }
68  bits[i + j + 1] = 0;
69  --value;
70 
71  if (ctx->trace_enable)
72  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
73  bits, value);
74 
75  if (value < range_min || value > range_max) {
76  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
77  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
78  name, value, range_min, range_max);
79  return AVERROR_INVALIDDATA;
80  }
81 
82  *write_to = value;
83  return 0;
84 }
85 
87  const char *name, const int *subscripts,
88  int32_t *write_to,
89  int32_t range_min, int32_t range_max)
90 {
91  int32_t value;
92  int position, i, j;
93  unsigned int k;
94  uint32_t v;
95  char bits[65];
96 
97  position = get_bits_count(gbc);
98 
99  for (i = 0; i < 32; i++) {
100  if (get_bits_left(gbc) < i + 1) {
101  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
102  "%s: bitstream ended.\n", name);
103  return AVERROR_INVALIDDATA;
104  }
105  k = get_bits1(gbc);
106  bits[i] = k ? '1' : '0';
107  if (k)
108  break;
109  }
110  if (i >= 32) {
111  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
112  "%s: more than 31 zeroes.\n", name);
113  return AVERROR_INVALIDDATA;
114  }
115  v = 1;
116  for (j = 0; j < i; j++) {
117  k = get_bits1(gbc);
118  bits[i + j + 1] = k ? '1' : '0';
119  v = v << 1 | k;
120  }
121  bits[i + j + 1] = 0;
122  if (v & 1)
123  value = -(int32_t)(v / 2);
124  else
125  value = v / 2;
126 
127  if (ctx->trace_enable)
128  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
129  bits, value);
130 
131  if (value < range_min || value > range_max) {
132  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
133  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
134  name, value, range_min, range_max);
135  return AVERROR_INVALIDDATA;
136  }
137 
138  *write_to = value;
139  return 0;
140 }
141 
143  const char *name, const int *subscripts,
144  uint32_t value,
145  uint32_t range_min, uint32_t range_max)
146 {
147  int len;
148 
149  if (value < range_min || value > range_max) {
150  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
151  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
152  name, value, range_min, range_max);
153  return AVERROR_INVALIDDATA;
154  }
155  av_assert0(value != UINT32_MAX);
156 
157  len = av_log2(value + 1);
158  if (put_bits_left(pbc) < 2 * len + 1)
159  return AVERROR(ENOSPC);
160 
161  if (ctx->trace_enable) {
162  char bits[65];
163  int i;
164 
165  for (i = 0; i < len; i++)
166  bits[i] = '0';
167  bits[len] = '1';
168  for (i = 0; i < len; i++)
169  bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
170  bits[len + len + 1] = 0;
171 
173  name, subscripts, bits, value);
174  }
175 
176  put_bits(pbc, len, 0);
177  if (len + 1 < 32)
178  put_bits(pbc, len + 1, value + 1);
179  else
180  put_bits32(pbc, value + 1);
181 
182  return 0;
183 }
184 
186  const char *name, const int *subscripts,
187  int32_t value,
188  int32_t range_min, int32_t range_max)
189 {
190  int len;
191  uint32_t uvalue;
192 
193  if (value < range_min || value > range_max) {
194  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
195  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
196  name, value, range_min, range_max);
197  return AVERROR_INVALIDDATA;
198  }
199  av_assert0(value != INT32_MIN);
200 
201  if (value == 0)
202  uvalue = 0;
203  else if (value > 0)
204  uvalue = 2 * (uint32_t)value - 1;
205  else
206  uvalue = 2 * (uint32_t)-value;
207 
208  len = av_log2(uvalue + 1);
209  if (put_bits_left(pbc) < 2 * len + 1)
210  return AVERROR(ENOSPC);
211 
212  if (ctx->trace_enable) {
213  char bits[65];
214  int i;
215 
216  for (i = 0; i < len; i++)
217  bits[i] = '0';
218  bits[len] = '1';
219  for (i = 0; i < len; i++)
220  bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
221  bits[len + len + 1] = 0;
222 
224  name, subscripts, bits, value);
225  }
226 
227  put_bits(pbc, len, 0);
228  if (len + 1 < 32)
229  put_bits(pbc, len + 1, uvalue + 1);
230  else
231  put_bits32(pbc, uvalue + 1);
232 
233  return 0;
234 }
235 
236 #define HEADER(name) do { \
237  ff_cbs_trace_header(ctx, name); \
238  } while (0)
239 
240 #define CHECK(call) do { \
241  err = (call); \
242  if (err < 0) \
243  return err; \
244  } while (0)
245 
246 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
247 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
248 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
249 
250 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
251 
252 #define u(width, name, range_min, range_max) \
253  xu(width, name, current->name, range_min, range_max, 0)
254 #define ub(width, name) \
255  xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
256 #define flag(name) ub(1, name)
257 #define ue(name, range_min, range_max) \
258  xue(name, current->name, range_min, range_max, 0)
259 #define i(width, name, range_min, range_max) \
260  xi(width, name, current->name, range_min, range_max, 0)
261 #define ib(width, name) \
262  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0)
263 #define se(name, range_min, range_max) \
264  xse(name, current->name, range_min, range_max, 0)
265 
266 #define us(width, name, range_min, range_max, subs, ...) \
267  xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
268 #define ubs(width, name, subs, ...) \
269  xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
270 #define flags(name, subs, ...) \
271  xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
272 #define ues(name, range_min, range_max, subs, ...) \
273  xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
274 #define is(width, name, range_min, range_max, subs, ...) \
275  xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
276 #define ibs(width, name, subs, ...) \
277  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
278 #define ses(name, range_min, range_max, subs, ...) \
279  xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
280 
281 #define fixed(width, name, value) do { \
282  av_unused uint32_t fixed_value = value; \
283  xu(width, name, fixed_value, value, value, 0); \
284  } while (0)
285 
286 
287 #define READ
288 #define READWRITE read
289 #define RWContext GetBitContext
290 
291 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
292  uint32_t value; \
293  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
294  SUBSCRIPTS(subs, __VA_ARGS__), \
295  &value, range_min, range_max)); \
296  var = value; \
297  } while (0)
298 #define xue(name, var, range_min, range_max, subs, ...) do { \
299  uint32_t value; \
300  CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
301  SUBSCRIPTS(subs, __VA_ARGS__), \
302  &value, range_min, range_max)); \
303  var = value; \
304  } while (0)
305 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
306  int32_t value; \
307  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
308  SUBSCRIPTS(subs, __VA_ARGS__), \
309  &value, range_min, range_max)); \
310  var = value; \
311  } while (0)
312 #define xse(name, var, range_min, range_max, subs, ...) do { \
313  int32_t value; \
314  CHECK(cbs_read_se_golomb(ctx, rw, #name, \
315  SUBSCRIPTS(subs, __VA_ARGS__), \
316  &value, range_min, range_max)); \
317  var = value; \
318  } while (0)
319 
320 
321 #define infer(name, value) do { \
322  current->name = value; \
323  } while (0)
324 
326 {
327  int bits_left = get_bits_left(gbc);
328  if (bits_left > 8)
329  return 1;
330  if (bits_left == 0)
331  return 0;
332  if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
333  return 1;
334  return 0;
335 }
336 
337 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
338 
339 #define byte_alignment(rw) (get_bits_count(rw) % 8)
340 
341 #define allocate(name, size) do { \
342  name ## _ref = av_buffer_allocz(size + \
343  AV_INPUT_BUFFER_PADDING_SIZE); \
344  if (!name ## _ref) \
345  return AVERROR(ENOMEM); \
346  name = name ## _ref->data; \
347  } while (0)
348 
349 #define FUNC(name) FUNC_H264(READWRITE, name)
351 #undef FUNC
352 
353 #define FUNC(name) FUNC_H265(READWRITE, name)
355 #undef FUNC
356 
357 #undef READ
358 #undef READWRITE
359 #undef RWContext
360 #undef xu
361 #undef xi
362 #undef xue
363 #undef xse
364 #undef infer
365 #undef more_rbsp_data
366 #undef byte_alignment
367 #undef allocate
368 
369 
370 #define WRITE
371 #define READWRITE write
372 #define RWContext PutBitContext
373 
374 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
375  uint32_t value = var; \
376  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
377  SUBSCRIPTS(subs, __VA_ARGS__), \
378  value, range_min, range_max)); \
379  } while (0)
380 #define xue(name, var, range_min, range_max, subs, ...) do { \
381  uint32_t value = var; \
382  CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
383  SUBSCRIPTS(subs, __VA_ARGS__), \
384  value, range_min, range_max)); \
385  } while (0)
386 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
387  int32_t value = var; \
388  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
389  SUBSCRIPTS(subs, __VA_ARGS__), \
390  value, range_min, range_max)); \
391  } while (0)
392 #define xse(name, var, range_min, range_max, subs, ...) do { \
393  int32_t value = var; \
394  CHECK(cbs_write_se_golomb(ctx, rw, #name, \
395  SUBSCRIPTS(subs, __VA_ARGS__), \
396  value, range_min, range_max)); \
397  } while (0)
398 
399 #define infer(name, value) do { \
400  if (current->name != (value)) { \
401  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
402  "%s does not match inferred value: " \
403  "%"PRId64", but should be %"PRId64".\n", \
404  #name, (int64_t)current->name, (int64_t)(value)); \
405  } \
406  } while (0)
407 
408 #define more_rbsp_data(var) (var)
409 
410 #define byte_alignment(rw) (put_bits_count(rw) % 8)
411 
412 #define allocate(name, size) do { \
413  if (!name) { \
414  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
415  "for writing.\n", #name); \
416  return AVERROR_INVALIDDATA; \
417  } \
418  } while (0)
419 
420 #define FUNC(name) FUNC_H264(READWRITE, name)
422 #undef FUNC
423 
424 #define FUNC(name) FUNC_H265(READWRITE, name)
426 #undef FUNC
427 
428 #undef WRITE
429 #undef READWRITE
430 #undef RWContext
431 #undef xu
432 #undef xi
433 #undef xue
434 #undef xse
435 #undef u
436 #undef i
437 #undef flag
438 #undef ue
439 #undef se
440 #undef infer
441 #undef more_rbsp_data
442 #undef byte_alignment
443 #undef allocate
444 
445 
446 static void cbs_h264_free_pps(void *unit, uint8_t *content)
447 {
448  H264RawPPS *pps = (H264RawPPS*)content;
450  av_freep(&content);
451 }
452 
454 {
455  switch (payload->payload_type) {
463  break;
466  break;
469  break;
470  default:
471  av_buffer_unref(&payload->payload.other.data_ref);
472  break;
473  }
474 }
475 
476 static void cbs_h264_free_sei(void *unit, uint8_t *content)
477 {
478  H264RawSEI *sei = (H264RawSEI*)content;
479  int i;
480  for (i = 0; i < sei->payload_count; i++)
482  av_freep(&content);
483 }
484 
485 static void cbs_h264_free_slice(void *unit, uint8_t *content)
486 {
487  H264RawSlice *slice = (H264RawSlice*)content;
488  av_buffer_unref(&slice->data_ref);
489  av_freep(&content);
490 }
491 
492 static void cbs_h265_free_vps(void *unit, uint8_t *content)
493 {
494  H265RawVPS *vps = (H265RawVPS*)content;
496  av_freep(&content);
497 }
498 
499 static void cbs_h265_free_sps(void *unit, uint8_t *content)
500 {
501  H265RawSPS *sps = (H265RawSPS*)content;
503  av_freep(&content);
504 }
505 
506 static void cbs_h265_free_pps(void *unit, uint8_t *content)
507 {
508  H265RawPPS *pps = (H265RawPPS*)content;
510  av_freep(&content);
511 }
512 
513 static void cbs_h265_free_slice(void *unit, uint8_t *content)
514 {
515  H265RawSlice *slice = (H265RawSlice*)content;
516  av_buffer_unref(&slice->data_ref);
517  av_freep(&content);
518 }
519 
521 {
522  switch (payload->payload_type) {
535  break;
538  break;
541  break;
542  default:
543  av_buffer_unref(&payload->payload.other.data_ref);
544  break;
545  }
546 }
547 
548 static void cbs_h265_free_sei(void *unit, uint8_t *content)
549 {
550  H265RawSEI *sei = (H265RawSEI*)content;
551  int i;
552  for (i = 0; i < sei->payload_count; i++)
554  av_freep(&content);
555 }
556 
559  const H2645Packet *packet)
560 {
561  int err, i;
562 
563  for (i = 0; i < packet->nb_nals; i++) {
564  const H2645NAL *nal = &packet->nals[i];
565  AVBufferRef *ref;
566  size_t size = nal->size;
567 
568  // Remove trailing zeroes.
569  while (size > 0 && nal->data[size - 1] == 0)
570  --size;
571  if (size == 0) {
572  av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
573  continue;
574  }
575 
576  ref = (nal->data == nal->raw_data) ? frag->data_ref
577  : packet->rbsp.rbsp_buffer_ref;
578 
579  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
580  (uint8_t*)nal->data, size, ref);
581  if (err < 0)
582  return err;
583  }
584 
585  return 0;
586 }
587 
590  int header)
591 {
592  enum AVCodecID codec_id = ctx->codec->codec_id;
594  GetByteContext gbc;
595  int err;
596 
597  av_assert0(frag->data && frag->nb_units == 0);
598  if (frag->data_size == 0)
599  return 0;
600 
601  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
602  // AVCC header.
603  size_t size, start, end;
604  int i, count, version;
605 
606  priv->mp4 = 1;
607 
608  bytestream2_init(&gbc, frag->data, frag->data_size);
609 
610  if (bytestream2_get_bytes_left(&gbc) < 6)
611  return AVERROR_INVALIDDATA;
612 
613  version = bytestream2_get_byte(&gbc);
614  if (version != 1) {
615  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
616  "first byte %u.", version);
617  return AVERROR_INVALIDDATA;
618  }
619 
620  bytestream2_skip(&gbc, 3);
621  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
622 
623  // SPS array.
624  count = bytestream2_get_byte(&gbc) & 0x1f;
625  start = bytestream2_tell(&gbc);
626  for (i = 0; i < count; i++) {
627  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
628  return AVERROR_INVALIDDATA;
629  size = bytestream2_get_be16(&gbc);
630  if (bytestream2_get_bytes_left(&gbc) < size)
631  return AVERROR_INVALIDDATA;
632  bytestream2_skip(&gbc, size);
633  }
634  end = bytestream2_tell(&gbc);
635 
636  err = ff_h2645_packet_split(&priv->read_packet,
637  frag->data + start, end - start,
638  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
639  if (err < 0) {
640  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
641  return err;
642  }
643  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
644  if (err < 0)
645  return err;
646 
647  // PPS array.
648  count = bytestream2_get_byte(&gbc);
649  start = bytestream2_tell(&gbc);
650  for (i = 0; i < count; i++) {
651  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
652  return AVERROR_INVALIDDATA;
653  size = bytestream2_get_be16(&gbc);
654  if (bytestream2_get_bytes_left(&gbc) < size)
655  return AVERROR_INVALIDDATA;
656  bytestream2_skip(&gbc, size);
657  }
658  end = bytestream2_tell(&gbc);
659 
660  err = ff_h2645_packet_split(&priv->read_packet,
661  frag->data + start, end - start,
662  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
663  if (err < 0) {
664  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
665  return err;
666  }
667  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
668  if (err < 0)
669  return err;
670 
671  if (bytestream2_get_bytes_left(&gbc) > 0) {
672  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
673  "header.\n", bytestream2_get_bytes_left(&gbc));
674  }
675 
676  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
677  // HVCC header.
678  size_t size, start, end;
679  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
680 
681  priv->mp4 = 1;
682 
683  bytestream2_init(&gbc, frag->data, frag->data_size);
684 
685  if (bytestream2_get_bytes_left(&gbc) < 23)
686  return AVERROR_INVALIDDATA;
687 
688  version = bytestream2_get_byte(&gbc);
689  if (version != 1) {
690  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
691  "first byte %u.", version);
692  return AVERROR_INVALIDDATA;
693  }
694 
695  bytestream2_skip(&gbc, 20);
696  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
697 
698  nb_arrays = bytestream2_get_byte(&gbc);
699  for (i = 0; i < nb_arrays; i++) {
700  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
701  nb_nals = bytestream2_get_be16(&gbc);
702 
703  start = bytestream2_tell(&gbc);
704  for (j = 0; j < nb_nals; j++) {
705  if (bytestream2_get_bytes_left(&gbc) < 2)
706  return AVERROR_INVALIDDATA;
707  size = bytestream2_get_be16(&gbc);
708  if (bytestream2_get_bytes_left(&gbc) < size)
709  return AVERROR_INVALIDDATA;
710  bytestream2_skip(&gbc, size);
711  }
712  end = bytestream2_tell(&gbc);
713 
714  err = ff_h2645_packet_split(&priv->read_packet,
715  frag->data + start, end - start,
716  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
717  if (err < 0) {
718  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
719  "HVCC array %d (%d NAL units of type %d).\n",
720  i, nb_nals, nal_unit_type);
721  return err;
722  }
723  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
724  if (err < 0)
725  return err;
726  }
727 
728  } else {
729  // Annex B, or later MP4 with already-known parameters.
730 
731  err = ff_h2645_packet_split(&priv->read_packet,
732  frag->data, frag->data_size,
733  ctx->log_ctx,
734  priv->mp4, priv->nal_length_size,
735  codec_id, 1, 1);
736  if (err < 0)
737  return err;
738 
739  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
740  if (err < 0)
741  return err;
742  }
743 
744  return 0;
745 }
746 
747 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
748 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
749  CodedBitstreamUnit *unit) \
750 { \
751  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
752  H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
753  unsigned int id = ps_var->id_element; \
754  if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
755  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
756  " id : %d.\n", id); \
757  return AVERROR_INVALIDDATA; \
758  } \
759  if (priv->ps_var[id] == priv->active_ ## ps_var) \
760  priv->active_ ## ps_var = NULL ; \
761  av_buffer_unref(&priv->ps_var ## _ref[id]); \
762  if (unit->content_ref) \
763  priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
764  else \
765  priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
766  if (!priv->ps_var ## _ref[id]) \
767  return AVERROR(ENOMEM); \
768  priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
769  if (!unit->content_ref) \
770  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
771  return 0; \
772 }
773 
774 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
775 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
776 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
777 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
778 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
779 
780 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
781  CodedBitstreamUnit *unit)
782 {
783  GetBitContext gbc;
784  int err;
785 
786  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
787  if (err < 0)
788  return err;
789 
790  switch (unit->type) {
791  case H264_NAL_SPS:
792  {
793  H264RawSPS *sps;
794 
795  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
796  if (err < 0)
797  return err;
798  sps = unit->content;
799 
800  err = cbs_h264_read_sps(ctx, &gbc, sps);
801  if (err < 0)
802  return err;
803 
804  err = cbs_h264_replace_sps(ctx, unit);
805  if (err < 0)
806  return err;
807  }
808  break;
809 
810  case H264_NAL_SPS_EXT:
811  {
812  err = ff_cbs_alloc_unit_content(ctx, unit,
813  sizeof(H264RawSPSExtension),
814  NULL);
815  if (err < 0)
816  return err;
817 
818  err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
819  if (err < 0)
820  return err;
821  }
822  break;
823 
824  case H264_NAL_PPS:
825  {
826  H264RawPPS *pps;
827 
828  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
830  if (err < 0)
831  return err;
832  pps = unit->content;
833 
834  err = cbs_h264_read_pps(ctx, &gbc, pps);
835  if (err < 0)
836  return err;
837 
838  err = cbs_h264_replace_pps(ctx, unit);
839  if (err < 0)
840  return err;
841  }
842  break;
843 
844  case H264_NAL_SLICE:
845  case H264_NAL_IDR_SLICE:
847  {
848  H264RawSlice *slice;
849  int pos, len;
850 
851  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
853  if (err < 0)
854  return err;
855  slice = unit->content;
856 
857  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
858  if (err < 0)
859  return err;
860 
862  return AVERROR_INVALIDDATA;
863 
864  pos = get_bits_count(&gbc);
865  len = unit->data_size;
866 
867  slice->data_size = len - pos / 8;
868  slice->data_ref = av_buffer_ref(unit->data_ref);
869  if (!slice->data_ref)
870  return AVERROR(ENOMEM);
871  slice->data = unit->data + pos / 8;
872  slice->data_bit_start = pos % 8;
873  }
874  break;
875 
876  case H264_NAL_AUD:
877  {
878  err = ff_cbs_alloc_unit_content(ctx, unit,
879  sizeof(H264RawAUD), NULL);
880  if (err < 0)
881  return err;
882 
883  err = cbs_h264_read_aud(ctx, &gbc, unit->content);
884  if (err < 0)
885  return err;
886  }
887  break;
888 
889  case H264_NAL_SEI:
890  {
891  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
893  if (err < 0)
894  return err;
895 
896  err = cbs_h264_read_sei(ctx, &gbc, unit->content);
897  if (err < 0)
898  return err;
899  }
900  break;
901 
903  {
904  err = ff_cbs_alloc_unit_content(ctx, unit,
905  sizeof(H264RawFiller), NULL);
906  if (err < 0)
907  return err;
908 
909  err = cbs_h264_read_filler(ctx, &gbc, unit->content);
910  if (err < 0)
911  return err;
912  }
913  break;
914 
916  case H264_NAL_END_STREAM:
917  {
918  err = ff_cbs_alloc_unit_content(ctx, unit,
919  sizeof(H264RawNALUnitHeader),
920  NULL);
921  if (err < 0)
922  return err;
923 
924  err = (unit->type == H264_NAL_END_SEQUENCE ?
925  cbs_h264_read_end_of_sequence :
926  cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
927  if (err < 0)
928  return err;
929  }
930  break;
931 
932  default:
933  return AVERROR(ENOSYS);
934  }
935 
936  return 0;
937 }
938 
940  CodedBitstreamUnit *unit)
941 {
942  GetBitContext gbc;
943  int err;
944 
945  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
946  if (err < 0)
947  return err;
948 
949  switch (unit->type) {
950  case HEVC_NAL_VPS:
951  {
952  H265RawVPS *vps;
953 
954  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
956  if (err < 0)
957  return err;
958  vps = unit->content;
959 
960  err = cbs_h265_read_vps(ctx, &gbc, vps);
961  if (err < 0)
962  return err;
963 
964  err = cbs_h265_replace_vps(ctx, unit);
965  if (err < 0)
966  return err;
967  }
968  break;
969  case HEVC_NAL_SPS:
970  {
971  H265RawSPS *sps;
972 
973  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
975  if (err < 0)
976  return err;
977  sps = unit->content;
978 
979  err = cbs_h265_read_sps(ctx, &gbc, sps);
980  if (err < 0)
981  return err;
982 
983  err = cbs_h265_replace_sps(ctx, unit);
984  if (err < 0)
985  return err;
986  }
987  break;
988 
989  case HEVC_NAL_PPS:
990  {
991  H265RawPPS *pps;
992 
993  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
995  if (err < 0)
996  return err;
997  pps = unit->content;
998 
999  err = cbs_h265_read_pps(ctx, &gbc, pps);
1000  if (err < 0)
1001  return err;
1002 
1003  err = cbs_h265_replace_pps(ctx, unit);
1004  if (err < 0)
1005  return err;
1006  }
1007  break;
1008 
1009  case HEVC_NAL_TRAIL_N:
1010  case HEVC_NAL_TRAIL_R:
1011  case HEVC_NAL_TSA_N:
1012  case HEVC_NAL_TSA_R:
1013  case HEVC_NAL_STSA_N:
1014  case HEVC_NAL_STSA_R:
1015  case HEVC_NAL_RADL_N:
1016  case HEVC_NAL_RADL_R:
1017  case HEVC_NAL_RASL_N:
1018  case HEVC_NAL_RASL_R:
1019  case HEVC_NAL_BLA_W_LP:
1020  case HEVC_NAL_BLA_W_RADL:
1021  case HEVC_NAL_BLA_N_LP:
1022  case HEVC_NAL_IDR_W_RADL:
1023  case HEVC_NAL_IDR_N_LP:
1024  case HEVC_NAL_CRA_NUT:
1025  {
1026  H265RawSlice *slice;
1027  int pos, len;
1028 
1029  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1031  if (err < 0)
1032  return err;
1033  slice = unit->content;
1034 
1035  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1036  if (err < 0)
1037  return err;
1038 
1039  if (!cbs_h2645_read_more_rbsp_data(&gbc))
1040  return AVERROR_INVALIDDATA;
1041 
1042  pos = get_bits_count(&gbc);
1043  len = unit->data_size;
1044 
1045  slice->data_size = len - pos / 8;
1046  slice->data_ref = av_buffer_ref(unit->data_ref);
1047  if (!slice->data_ref)
1048  return AVERROR(ENOMEM);
1049  slice->data = unit->data + pos / 8;
1050  slice->data_bit_start = pos % 8;
1051  }
1052  break;
1053 
1054  case HEVC_NAL_AUD:
1055  {
1056  err = ff_cbs_alloc_unit_content(ctx, unit,
1057  sizeof(H265RawAUD), NULL);
1058  if (err < 0)
1059  return err;
1060 
1061  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1062  if (err < 0)
1063  return err;
1064  }
1065  break;
1066 
1067  case HEVC_NAL_SEI_PREFIX:
1068  case HEVC_NAL_SEI_SUFFIX:
1069  {
1070  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1072 
1073  if (err < 0)
1074  return err;
1075 
1076  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1077  unit->type == HEVC_NAL_SEI_PREFIX);
1078 
1079  if (err < 0)
1080  return err;
1081  }
1082  break;
1083 
1084  default:
1085  return AVERROR(ENOSYS);
1086  }
1087 
1088  return 0;
1089 }
1090 
1092  PutBitContext *pbc, const uint8_t *data,
1093  size_t data_size, int data_bit_start)
1094 {
1095  size_t rest = data_size - (data_bit_start + 7) / 8;
1096  const uint8_t *pos = data + data_bit_start / 8;
1097 
1098  av_assert0(data_bit_start >= 0 &&
1099  data_size > data_bit_start / 8);
1100 
1101  if (data_size * 8 + 8 > put_bits_left(pbc))
1102  return AVERROR(ENOSPC);
1103 
1104  if (!rest)
1105  goto rbsp_stop_one_bit;
1106 
1107  // First copy the remaining bits of the first byte
1108  // The above check ensures that we do not accidentally
1109  // copy beyond the rbsp_stop_one_bit.
1110  if (data_bit_start % 8)
1111  put_bits(pbc, 8 - data_bit_start % 8,
1112  *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1113 
1114  if (put_bits_count(pbc) % 8 == 0) {
1115  // If the writer is aligned at this point,
1116  // memcpy can be used to improve performance.
1117  // This happens normally for CABAC.
1118  flush_put_bits(pbc);
1119  memcpy(put_bits_ptr(pbc), pos, rest);
1120  skip_put_bytes(pbc, rest);
1121  } else {
1122  // If not, we have to copy manually.
1123  // rbsp_stop_one_bit forces us to special-case
1124  // the last byte.
1125  uint8_t temp;
1126  int i;
1127 
1128  for (; rest > 4; rest -= 4, pos += 4)
1129  put_bits32(pbc, AV_RB32(pos));
1130 
1131  for (; rest > 1; rest--, pos++)
1132  put_bits(pbc, 8, *pos);
1133 
1134  rbsp_stop_one_bit:
1135  temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1136 
1137  av_assert0(temp);
1138  i = ff_ctz(*pos);
1139  temp = temp >> i;
1140  i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1141  put_bits(pbc, i, temp);
1142  if (put_bits_count(pbc) % 8)
1143  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1144  }
1145 
1146  return 0;
1147 }
1148 
1150  CodedBitstreamUnit *unit,
1151  PutBitContext *pbc)
1152 {
1153  int err;
1154 
1155  switch (unit->type) {
1156  case H264_NAL_SPS:
1157  {
1158  H264RawSPS *sps = unit->content;
1159 
1160  err = cbs_h264_write_sps(ctx, pbc, sps);
1161  if (err < 0)
1162  return err;
1163 
1164  err = cbs_h264_replace_sps(ctx, unit);
1165  if (err < 0)
1166  return err;
1167  }
1168  break;
1169 
1170  case H264_NAL_SPS_EXT:
1171  {
1172  H264RawSPSExtension *sps_ext = unit->content;
1173 
1174  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1175  if (err < 0)
1176  return err;
1177  }
1178  break;
1179 
1180  case H264_NAL_PPS:
1181  {
1182  H264RawPPS *pps = unit->content;
1183 
1184  err = cbs_h264_write_pps(ctx, pbc, pps);
1185  if (err < 0)
1186  return err;
1187 
1188  err = cbs_h264_replace_pps(ctx, unit);
1189  if (err < 0)
1190  return err;
1191  }
1192  break;
1193 
1194  case H264_NAL_SLICE:
1195  case H264_NAL_IDR_SLICE:
1197  {
1198  H264RawSlice *slice = unit->content;
1199 
1200  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1201  if (err < 0)
1202  return err;
1203 
1204  if (slice->data) {
1205  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1206  slice->data_size,
1207  slice->data_bit_start);
1208  if (err < 0)
1209  return err;
1210  } else {
1211  // No slice data - that was just the header.
1212  // (Bitstream may be unaligned!)
1213  }
1214  }
1215  break;
1216 
1217  case H264_NAL_AUD:
1218  {
1219  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1220  if (err < 0)
1221  return err;
1222  }
1223  break;
1224 
1225  case H264_NAL_SEI:
1226  {
1227  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1228  if (err < 0)
1229  return err;
1230  }
1231  break;
1232 
1233  case H264_NAL_FILLER_DATA:
1234  {
1235  err = cbs_h264_write_filler(ctx, pbc, unit->content);
1236  if (err < 0)
1237  return err;
1238  }
1239  break;
1240 
1241  case H264_NAL_END_SEQUENCE:
1242  {
1243  err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1244  if (err < 0)
1245  return err;
1246  }
1247  break;
1248 
1249  case H264_NAL_END_STREAM:
1250  {
1251  err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1252  if (err < 0)
1253  return err;
1254  }
1255  break;
1256 
1257  default:
1258  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1259  "NAL unit type %"PRIu32".\n", unit->type);
1260  return AVERROR_PATCHWELCOME;
1261  }
1262 
1263  return 0;
1264 }
1265 
1267  CodedBitstreamUnit *unit,
1268  PutBitContext *pbc)
1269 {
1270  int err;
1271 
1272  switch (unit->type) {
1273  case HEVC_NAL_VPS:
1274  {
1275  H265RawVPS *vps = unit->content;
1276 
1277  err = cbs_h265_write_vps(ctx, pbc, vps);
1278  if (err < 0)
1279  return err;
1280 
1281  err = cbs_h265_replace_vps(ctx, unit);
1282  if (err < 0)
1283  return err;
1284  }
1285  break;
1286 
1287  case HEVC_NAL_SPS:
1288  {
1289  H265RawSPS *sps = unit->content;
1290 
1291  err = cbs_h265_write_sps(ctx, pbc, sps);
1292  if (err < 0)
1293  return err;
1294 
1295  err = cbs_h265_replace_sps(ctx, unit);
1296  if (err < 0)
1297  return err;
1298  }
1299  break;
1300 
1301  case HEVC_NAL_PPS:
1302  {
1303  H265RawPPS *pps = unit->content;
1304 
1305  err = cbs_h265_write_pps(ctx, pbc, pps);
1306  if (err < 0)
1307  return err;
1308 
1309  err = cbs_h265_replace_pps(ctx, unit);
1310  if (err < 0)
1311  return err;
1312  }
1313  break;
1314 
1315  case HEVC_NAL_TRAIL_N:
1316  case HEVC_NAL_TRAIL_R:
1317  case HEVC_NAL_TSA_N:
1318  case HEVC_NAL_TSA_R:
1319  case HEVC_NAL_STSA_N:
1320  case HEVC_NAL_STSA_R:
1321  case HEVC_NAL_RADL_N:
1322  case HEVC_NAL_RADL_R:
1323  case HEVC_NAL_RASL_N:
1324  case HEVC_NAL_RASL_R:
1325  case HEVC_NAL_BLA_W_LP:
1326  case HEVC_NAL_BLA_W_RADL:
1327  case HEVC_NAL_BLA_N_LP:
1328  case HEVC_NAL_IDR_W_RADL:
1329  case HEVC_NAL_IDR_N_LP:
1330  case HEVC_NAL_CRA_NUT:
1331  {
1332  H265RawSlice *slice = unit->content;
1333 
1334  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1335  if (err < 0)
1336  return err;
1337 
1338  if (slice->data) {
1339  err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1340  slice->data_size,
1341  slice->data_bit_start);
1342  if (err < 0)
1343  return err;
1344  } else {
1345  // No slice data - that was just the header.
1346  }
1347  }
1348  break;
1349 
1350  case HEVC_NAL_AUD:
1351  {
1352  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1353  if (err < 0)
1354  return err;
1355  }
1356  break;
1357 
1358  case HEVC_NAL_SEI_PREFIX:
1359  case HEVC_NAL_SEI_SUFFIX:
1360  {
1361  err = cbs_h265_write_sei(ctx, pbc, unit->content,
1362  unit->type == HEVC_NAL_SEI_PREFIX);
1363 
1364  if (err < 0)
1365  return err;
1366  }
1367  break;
1368 
1369  default:
1370  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1371  "NAL unit type %"PRIu32".\n", unit->type);
1372  return AVERROR_PATCHWELCOME;
1373  }
1374 
1375  return 0;
1376 }
1377 
1379  CodedBitstreamFragment *frag)
1380 {
1381  uint8_t *data;
1382  size_t max_size, dp, sp;
1383  int err, i, zero_run;
1384 
1385  for (i = 0; i < frag->nb_units; i++) {
1386  // Data should already all have been written when we get here.
1387  av_assert0(frag->units[i].data);
1388  }
1389 
1390  max_size = 0;
1391  for (i = 0; i < frag->nb_units; i++) {
1392  // Start code + content with worst-case emulation prevention.
1393  max_size += 4 + frag->units[i].data_size * 3 / 2;
1394  }
1395 
1396  data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1397  if (!data)
1398  return AVERROR(ENOMEM);
1399 
1400  dp = 0;
1401  for (i = 0; i < frag->nb_units; i++) {
1402  CodedBitstreamUnit *unit = &frag->units[i];
1403 
1404  if (unit->data_bit_padding > 0) {
1405  if (i < frag->nb_units - 1)
1406  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1407  "unaligned padding on non-final NAL unit.\n");
1408  else
1409  frag->data_bit_padding = unit->data_bit_padding;
1410  }
1411 
1412  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1413  (unit->type == H264_NAL_SPS ||
1414  unit->type == H264_NAL_PPS)) ||
1415  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1416  (unit->type == HEVC_NAL_VPS ||
1417  unit->type == HEVC_NAL_SPS ||
1418  unit->type == HEVC_NAL_PPS)) ||
1419  i == 0 /* (Assume this is the start of an access unit.) */) {
1420  // zero_byte
1421  data[dp++] = 0;
1422  }
1423  // start_code_prefix_one_3bytes
1424  data[dp++] = 0;
1425  data[dp++] = 0;
1426  data[dp++] = 1;
1427 
1428  zero_run = 0;
1429  for (sp = 0; sp < unit->data_size; sp++) {
1430  if (zero_run < 2) {
1431  if (unit->data[sp] == 0)
1432  ++zero_run;
1433  else
1434  zero_run = 0;
1435  } else {
1436  if ((unit->data[sp] & ~3) == 0) {
1437  // emulation_prevention_three_byte
1438  data[dp++] = 3;
1439  }
1440  zero_run = unit->data[sp] == 0;
1441  }
1442  data[dp++] = unit->data[sp];
1443  }
1444  }
1445 
1446  av_assert0(dp <= max_size);
1447  err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1448  if (err)
1449  return err;
1450  memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1451 
1453  NULL, NULL, 0);
1454  if (!frag->data_ref) {
1455  av_freep(&data);
1456  return AVERROR(ENOMEM);
1457  }
1458 
1459  frag->data = data;
1460  frag->data_size = dp;
1461 
1462  return 0;
1463 }
1464 
1466 {
1467  CodedBitstreamH264Context *h264 = ctx->priv_data;
1468  int i;
1469 
1471 
1472  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1473  av_buffer_unref(&h264->sps_ref[i]);
1474  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1475  av_buffer_unref(&h264->pps_ref[i]);
1476 }
1477 
1479 {
1480  CodedBitstreamH265Context *h265 = ctx->priv_data;
1481  int i;
1482 
1484 
1485  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1486  av_buffer_unref(&h265->vps_ref[i]);
1487  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1488  av_buffer_unref(&h265->sps_ref[i]);
1489  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1490  av_buffer_unref(&h265->pps_ref[i]);
1491 }
1492 
1495 
1496  .priv_data_size = sizeof(CodedBitstreamH264Context),
1497 
1498  .split_fragment = &cbs_h2645_split_fragment,
1499  .read_unit = &cbs_h264_read_nal_unit,
1500  .write_unit = &cbs_h264_write_nal_unit,
1501  .assemble_fragment = &cbs_h2645_assemble_fragment,
1502 
1503  .close = &cbs_h264_close,
1504 };
1505 
1508 
1509  .priv_data_size = sizeof(CodedBitstreamH265Context),
1510 
1511  .split_fragment = &cbs_h2645_split_fragment,
1512  .read_unit = &cbs_h265_read_nal_unit,
1513  .write_unit = &cbs_h265_write_nal_unit,
1514  .assemble_fragment = &cbs_h2645_assemble_fragment,
1515 
1516  .close = &cbs_h265_close,
1517 };
1518 
1521  const H264RawSEIPayload *payload)
1522 {
1523  H264RawSEI *sei;
1524  CodedBitstreamUnit *nal = NULL;
1525  int err, i;
1526 
1527  // Find an existing SEI NAL unit to add to.
1528  for (i = 0; i < au->nb_units; i++) {
1529  if (au->units[i].type == H264_NAL_SEI) {
1530  nal = &au->units[i];
1531  break;
1532  }
1533  }
1534  if (nal) {
1535  sei = nal->content;
1536 
1537  } else {
1538  // Need to make a new SEI NAL unit. Insert it before the first
1539  // slice data NAL unit; if no slice data, add at the end.
1540  AVBufferRef *sei_ref;
1541 
1542  sei = av_mallocz(sizeof(*sei));
1543  if (!sei)
1544  return AVERROR(ENOMEM);
1545 
1547  sei->nal_unit_header.nal_ref_idc = 0;
1548 
1549  sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1550  &cbs_h264_free_sei, ctx, 0);
1551  if (!sei_ref) {
1552  av_freep(&sei);
1553  return AVERROR(ENOMEM);
1554  }
1555 
1556  for (i = 0; i < au->nb_units; i++) {
1557  if (au->units[i].type == H264_NAL_SLICE ||
1558  au->units[i].type == H264_NAL_IDR_SLICE)
1559  break;
1560  }
1561 
1562  err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1563  sei, sei_ref);
1564  av_buffer_unref(&sei_ref);
1565  if (err < 0)
1566  return err;
1567  }
1568 
1569  if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1570  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1571  "SEI NAL unit.\n");
1572  return AVERROR(EINVAL);
1573  }
1574 
1575  memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1576  ++sei->payload_count;
1577 
1578  return 0;
1579 }
1580 
1583  CodedBitstreamUnit *nal,
1584  int position)
1585 {
1586  H264RawSEI *sei = nal->content;
1587 
1588  av_assert0(nal->type == H264_NAL_SEI);
1589  av_assert0(position >= 0 && position < sei->payload_count);
1590 
1591  if (position == 0 && sei->payload_count == 1) {
1592  // Deleting NAL unit entirely.
1593  int i;
1594 
1595  for (i = 0; i < au->nb_units; i++) {
1596  if (&au->units[i] == nal)
1597  break;
1598  }
1599 
1600  ff_cbs_delete_unit(ctx, au, i);
1601  } else {
1602  cbs_h264_free_sei_payload(&sei->payload[position]);
1603 
1604  --sei->payload_count;
1605  memmove(sei->payload + position,
1606  sei->payload + position + 1,
1607  (sei->payload_count - position) * sizeof(*sei->payload));
1608  }
1609 }
const char * name
Definition: avisynth_c.h:867
uint32_t payload_type
Definition: cbs_h264.h:322
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
struct H265RawSEIPayload::@56::@57 other
#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:738
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
H265RawSEIUserDataUnregistered user_data_unregistered
Definition: cbs_h265.h:701
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
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:1091
AVBufferRef * sps_ref[H264_MAX_SPS_COUNT]
Definition: cbs_h264.h:451
int size
Definition: h2645_parse.h:35
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:188
H264RawPPS * pps[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:454
AVBufferRef * sps_ref[HEVC_MAX_SPS_COUNT]
Definition: cbs_h265.h:734
void 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:796
uint8_t * data
Definition: cbs_h265.h:544
Sequence parameter set.
Definition: h264_ps.h:44
enum AVCodecID codec_id
Definition: qsv.c:72
H264RawNALUnitHeader nal_unit_header
Definition: cbs_h264.h:345
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:1266
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:939
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:34
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:644
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:722
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:1378
union H265RawSEIPayload::@56 payload
int version
Definition: avisynth_c.h:858
static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
Definition: cbs_h2645.c:520
CodedBitstreamH2645Context common
Definition: cbs_h265.h:729
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:185
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:1519
Macro definitions for various function/variable attributes.
pan-scan rectangle
Definition: h264_sei.h:30
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:206
static void cbs_h264_free_pps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:446
uint8_t
#define av_malloc(s)
AVBufferRef * pps_ref[HEVC_MAX_PPS_COUNT]
Definition: cbs_h265.h:735
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:98
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int data_bit_start
Definition: cbs_h265.h:546
unregistered user data
Definition: h264_sei.h:33
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding, int use_ref)
Split an input packet into NAL units.
Definition: h2645_parse.c:393
static void cbs_h265_free_slice(void *unit, uint8_t *content)
Definition: cbs_h2645.c:513
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:737
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:517
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:453
#define sp
Definition: regdef.h:63
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
H265RawSEIPayload payload[H265_MAX_SEI_PAYLOADS]
Definition: cbs_h265.h:723
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1465
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_allocated.
Definition: cbs.h:162
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:416
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:759
alternative transfer
Definition: h264_sei.h:39
#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:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
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:506
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
AVBufferRef * data_ref
Definition: cbs_h265.h:547
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
uint8_t bits
Definition: vp3data.h:202
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:330
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
Definition: cbs_h2645.c:453
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1493
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1478
uint32_t payload_type
Definition: cbs_h265.h:694
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:224
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:446
static void cbs_h265_free_vps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:492
size_t data_size
Definition: cbs_h264.h:433
int type
NAL unit type.
Definition: h2645_parse.h:52
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
#define FF_ARRAY_ELEMS(a)
#define av_log2
Definition: intmath.h:83
AVBufferRef * pps_ref[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:452
H264RawSEIUserDataRegistered user_data_registered
Definition: cbs_h264.h:329
#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:724
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:485
H265RawSEIUserDataRegistered user_data_registered
Definition: cbs_h265.h:700
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
AVBufferRef * rbsp_buffer_ref
Definition: h2645_parse.h:70
const uint8_t * data
Definition: h2645_parse.h:36
AVBufferRef * slice_group_id_ref
Definition: cbs_h264.h:199
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
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:168
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
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:325
struct H264RawSEIPayload::@51::@52 other
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:347
static void cbs_h265_free_sei(void *unit, uint8_t *content)
Definition: cbs_h2645.c:548
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
H2645RBSP rbsp
Definition: h2645_parse.h:78
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:86
AVBufferRef * vps_ref[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:733
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
AVBufferRef * data_ref
Definition: cbs_h264.h:435
A reference to a data buffer.
Definition: buffer.h:81
H265RawPSExtensionData extension_data
Definition: cbs_h265.h:328
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
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:1506
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:790
const uint8_t * raw_data
Definition: h2645_parse.h:45
int data_bit_start
Definition: cbs_h264.h:434
H265RawSliceHeader header
Definition: cbs_h265.h:542
H264RawSliceHeader header
Definition: cbs_h264.h:430
void 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:1581
int len
union H264RawSEIPayload::@51 payload
H2645NAL * nals
Definition: h2645_parse.h:77
mastering display properties
Definition: h264_sei.h:38
size_t data_size
Definition: cbs_h265.h:545
CodedBitstreamH2645Context common
Definition: cbs_h264.h:447
uint8_t nal_ref_idc
Definition: cbs_h264.h:41
#define av_freep(p)
void INT64 INT64 count
Definition: avisynth_c.h:766
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1149
void INT64 start
Definition: avisynth_c.h:766
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:178
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
Definition: cbs_h2645.c:747
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:588
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:736
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
uint8_t nal_unit_type
Definition: cbs_h264.h:42
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:142
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:435
uint8_t * data
Definition: cbs_h264.h:432
uint8_t payload_count
Definition: cbs_h264.h:348
static void cbs_h264_free_sei(void *unit, uint8_t *content)
Definition: cbs_h2645.c:476
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:557
static void cbs_h265_free_sps(void *unit, uint8_t *content)
Definition: cbs_h2645.c:499