FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cbs_h2645.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21 
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h264.h"
26 #include "cbs_h265.h"
27 #include "golomb.h"
28 #include "h264.h"
29 #include "h264_sei.h"
30 #include "h2645_parse.h"
31 #include "hevc.h"
32 
33 
35  const char *name, uint32_t *write_to,
36  uint32_t range_min, uint32_t range_max)
37 {
38  uint32_t value;
39  int position, i, j;
40  unsigned int k;
41  char bits[65];
42 
43  position = get_bits_count(gbc);
44 
45  for (i = 0; i < 32; i++) {
46  if (get_bits_left(gbc) < i + 1) {
47  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
48  "%s: bitstream ended.\n", name);
49  return AVERROR_INVALIDDATA;
50  }
51  k = get_bits1(gbc);
52  bits[i] = k ? '1' : '0';
53  if (k)
54  break;
55  }
56  if (i >= 32) {
57  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
58  "%s: more than 31 zeroes.\n", name);
59  return AVERROR_INVALIDDATA;
60  }
61  value = 1;
62  for (j = 0; j < i; j++) {
63  k = get_bits1(gbc);
64  bits[i + j + 1] = k ? '1' : '0';
65  value = value << 1 | k;
66  }
67  bits[i + j + 1] = 0;
68  --value;
69 
70  if (ctx->trace_enable)
71  ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
72 
73  if (value < range_min || value > range_max) {
74  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
75  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
76  name, value, range_min, range_max);
77  return AVERROR_INVALIDDATA;
78  }
79 
80  *write_to = value;
81  return 0;
82 }
83 
85  const char *name, int32_t *write_to,
86  int32_t range_min, int32_t range_max)
87 {
88  int32_t value;
89  int position, i, j;
90  unsigned int k;
91  uint32_t v;
92  char bits[65];
93 
94  position = get_bits_count(gbc);
95 
96  for (i = 0; i < 32; i++) {
97  if (get_bits_left(gbc) < i + 1) {
98  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
99  "%s: bitstream ended.\n", name);
100  return AVERROR_INVALIDDATA;
101  }
102  k = get_bits1(gbc);
103  bits[i] = k ? '1' : '0';
104  if (k)
105  break;
106  }
107  if (i >= 32) {
108  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
109  "%s: more than 31 zeroes.\n", name);
110  return AVERROR_INVALIDDATA;
111  }
112  v = 1;
113  for (j = 0; j < i; j++) {
114  k = get_bits1(gbc);
115  bits[i + j + 1] = k ? '1' : '0';
116  v = v << 1 | k;
117  }
118  bits[i + j + 1] = 0;
119  if (v & 1)
120  value = -(int32_t)(v / 2);
121  else
122  value = v / 2;
123 
124  if (ctx->trace_enable)
125  ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
126 
127  if (value < range_min || value > range_max) {
128  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
129  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
130  name, value, range_min, range_max);
131  return AVERROR_INVALIDDATA;
132  }
133 
134  *write_to = value;
135  return 0;
136 }
137 
139  const char *name, uint32_t value,
140  uint32_t range_min, uint32_t range_max)
141 {
142  int len;
143 
144  if (value < range_min || value > range_max) {
145  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
146  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
147  name, value, range_min, range_max);
148  return AVERROR_INVALIDDATA;
149  }
150  av_assert0(value != UINT32_MAX);
151 
152  len = av_log2(value + 1);
153  if (put_bits_left(pbc) < 2 * len + 1)
154  return AVERROR(ENOSPC);
155 
156  if (ctx->trace_enable) {
157  char bits[65];
158  int i;
159 
160  for (i = 0; i < len; i++)
161  bits[i] = '0';
162  bits[len] = '1';
163  for (i = 0; i < len; i++)
164  bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
165  bits[len + len + 1] = 0;
166 
167  ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
168  }
169 
170  put_bits(pbc, len, 0);
171  if (len + 1 < 32)
172  put_bits(pbc, len + 1, value + 1);
173  else
174  put_bits32(pbc, value + 1);
175 
176  return 0;
177 }
178 
180  const char *name, int32_t value,
181  int32_t range_min, int32_t range_max)
182 {
183  int len;
184  uint32_t uvalue;
185 
186  if (value < range_min || value > range_max) {
187  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
188  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
189  name, value, range_min, range_max);
190  return AVERROR_INVALIDDATA;
191  }
192  av_assert0(value != INT32_MIN);
193 
194  if (value == 0)
195  uvalue = 0;
196  else if (value > 0)
197  uvalue = 2 * (uint32_t)value - 1;
198  else
199  uvalue = 2 * (uint32_t)-value;
200 
201  len = av_log2(uvalue + 1);
202  if (put_bits_left(pbc) < 2 * len + 1)
203  return AVERROR(ENOSPC);
204 
205  if (ctx->trace_enable) {
206  char bits[65];
207  int i;
208 
209  for (i = 0; i < len; i++)
210  bits[i] = '0';
211  bits[len] = '1';
212  for (i = 0; i < len; i++)
213  bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
214  bits[len + len + 1] = 0;
215 
216  ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
217  }
218 
219  put_bits(pbc, len, 0);
220  if (len + 1 < 32)
221  put_bits(pbc, len + 1, uvalue + 1);
222  else
223  put_bits32(pbc, uvalue + 1);
224 
225  return 0;
226 }
227 
228 #define HEADER(name) do { \
229  ff_cbs_trace_header(ctx, name); \
230  } while (0)
231 
232 #define CHECK(call) do { \
233  err = (call); \
234  if (err < 0) \
235  return err; \
236  } while (0)
237 
238 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
239 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
240 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
241 
242 
243 #define READ
244 #define READWRITE read
245 #define RWContext GetBitContext
246 
247 #define xu(width, name, var, range_min, range_max) do { \
248  uint32_t value = range_min; \
249  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
250  &value, range_min, range_max)); \
251  var = value; \
252  } while (0)
253 #define xue(name, var, range_min, range_max) do { \
254  uint32_t value = range_min; \
255  CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
256  &value, range_min, range_max)); \
257  var = value; \
258  } while (0)
259 #define xse(name, var, range_min, range_max) do { \
260  int32_t value = range_min; \
261  CHECK(cbs_read_se_golomb(ctx, rw, #name, \
262  &value, range_min, range_max)); \
263  var = value; \
264  } while (0)
265 
266 
267 #define u(width, name, range_min, range_max) \
268  xu(width, name, current->name, range_min, range_max)
269 #define flag(name) u(1, name, 0, 1)
270 #define ue(name, range_min, range_max) \
271  xue(name, current->name, range_min, range_max)
272 #define se(name, range_min, range_max) \
273  xse(name, current->name, range_min, range_max)
274 
275 #define infer(name, value) do { \
276  current->name = value; \
277  } while (0)
278 
280 {
281  int bits_left = get_bits_left(gbc);
282  if (bits_left > 8)
283  return 1;
284  if (show_bits(gbc, bits_left) == 1 << (bits_left - 1))
285  return 0;
286  return 1;
287 }
288 
289 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
290 
291 #define byte_alignment(rw) (get_bits_count(rw) % 8)
292 
293 #define allocate(name, size) do { \
294  name = av_mallocz(size); \
295  if (!name) \
296  return AVERROR(ENOMEM); \
297  } while (0)
298 
299 #define FUNC(name) FUNC_H264(READWRITE, name)
301 #undef FUNC
302 
303 #define FUNC(name) FUNC_H265(READWRITE, name)
305 #undef FUNC
306 
307 #undef READ
308 #undef READWRITE
309 #undef RWContext
310 #undef xu
311 #undef xue
312 #undef xse
313 #undef u
314 #undef flag
315 #undef ue
316 #undef se
317 #undef infer
318 #undef more_rbsp_data
319 #undef byte_alignment
320 #undef allocate
321 
322 
323 #define WRITE
324 #define READWRITE write
325 #define RWContext PutBitContext
326 
327 #define xu(width, name, var, range_min, range_max) do { \
328  uint32_t value = var; \
329  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
330  value, range_min, range_max)); \
331  } while (0)
332 #define xue(name, var, range_min, range_max) do { \
333  uint32_t value = var; \
334  CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
335  value, range_min, range_max)); \
336  } while (0)
337 #define xse(name, var, range_min, range_max) do { \
338  int32_t value = var; \
339  CHECK(cbs_write_se_golomb(ctx, rw, #name, \
340  value, range_min, range_max)); \
341  } while (0)
342 
343 #define u(width, name, range_min, range_max) \
344  xu(width, name, current->name, range_min, range_max)
345 #define flag(name) u(1, name, 0, 1)
346 #define ue(name, range_min, range_max) \
347  xue(name, current->name, range_min, range_max)
348 #define se(name, range_min, range_max) \
349  xse(name, current->name, range_min, range_max)
350 
351 #define infer(name, value) do { \
352  if (current->name != (value)) { \
353  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
354  "%s does not match inferred value: " \
355  "%"PRId64", but should be %"PRId64".\n", \
356  #name, (int64_t)current->name, (int64_t)(value)); \
357  } \
358  } while (0)
359 
360 #define more_rbsp_data(var) (var)
361 
362 #define byte_alignment(rw) (put_bits_count(rw) % 8)
363 
364 #define allocate(name, size) do { \
365  if (!name) { \
366  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
367  "for writing.\n", #name); \
368  return AVERROR_INVALIDDATA; \
369  } \
370  } while (0)
371 
372 #define FUNC(name) FUNC_H264(READWRITE, name)
374 #undef FUNC
375 
376 #define FUNC(name) FUNC_H265(READWRITE, name)
378 #undef FUNC
379 
380 #undef WRITE
381 #undef READWRITE
382 #undef RWContext
383 #undef xu
384 #undef xue
385 #undef xse
386 #undef u
387 #undef flag
388 #undef ue
389 #undef se
390 #undef infer
391 #undef more_rbsp_data
392 #undef byte_alignment
393 #undef allocate
394 
395 
397 {
398  int i;
399  for (i = 0; i < sei->payload_count; i++) {
400  H264RawSEIPayload *payload = &sei->payload[i];
401 
402  switch (payload->payload_type) {
407  break;
410  break;
413  break;
414  default:
415  av_freep(&payload->payload.other.data);
416  break;
417  }
418  }
419 }
420 
422 {
423  av_freep(&slice->data);
424 }
425 
427 {
428  switch (unit->type) {
429  case H264_NAL_SEI:
430  cbs_h264_free_sei(unit->content);
431  break;
432  case H264_NAL_IDR_SLICE:
433  case H264_NAL_SLICE:
435  break;
436  }
437  av_freep(&unit->content);
438 }
439 
441 {
442  switch (unit->type) {
443  case HEVC_NAL_VPS:
444  av_freep(&((H265RawVPS*)unit->content)->extension_data.data);
445  break;
446  case HEVC_NAL_SPS:
447  av_freep(&((H265RawSPS*)unit->content)->extension_data.data);
448  break;
449  case HEVC_NAL_PPS:
450  av_freep(&((H265RawPPS*)unit->content)->extension_data.data);
451  break;
452  case HEVC_NAL_TRAIL_N:
453  case HEVC_NAL_TRAIL_R:
454  case HEVC_NAL_TSA_N:
455  case HEVC_NAL_TSA_R:
456  case HEVC_NAL_STSA_N:
457  case HEVC_NAL_STSA_R:
458  case HEVC_NAL_RADL_N:
459  case HEVC_NAL_RADL_R:
460  case HEVC_NAL_RASL_N:
461  case HEVC_NAL_RASL_R:
462  case HEVC_NAL_BLA_W_LP:
463  case HEVC_NAL_BLA_W_RADL:
464  case HEVC_NAL_BLA_N_LP:
465  case HEVC_NAL_IDR_W_RADL:
466  case HEVC_NAL_IDR_N_LP:
467  case HEVC_NAL_CRA_NUT:
468  av_freep(&((H265RawSlice*)unit->content)->data);
469  break;
470  }
471  av_freep(&unit->content);
472 }
473 
476  const H2645Packet *packet)
477 {
478  int err, i;
479 
480  for (i = 0; i < packet->nb_nals; i++) {
481  const H2645NAL *nal = &packet->nals[i];
482  size_t size = nal->size;
483  uint8_t *data;
484 
485  // Remove trailing zeroes.
486  while (size > 0 && nal->data[size - 1] == 0)
487  --size;
488  av_assert0(size > 0);
489 
491  if (!data)
492  return AVERROR(ENOMEM);
493  memcpy(data, nal->data, size);
494  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
495 
496  err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
497  data, size);
498  if (err < 0) {
499  av_freep(&data);
500  return err;
501  }
502  }
503 
504  return 0;
505 }
506 
509  int header)
510 {
511  enum AVCodecID codec_id = ctx->codec->codec_id;
513  GetByteContext gbc;
514  int err;
515 
516  av_assert0(frag->data && frag->nb_units == 0);
517  if (frag->data_size == 0)
518  return 0;
519 
520  if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
521  // AVCC header.
522  size_t size, start, end;
523  int i, count, version;
524 
525  priv->mp4 = 1;
526 
527  bytestream2_init(&gbc, frag->data, frag->data_size);
528 
529  if (bytestream2_get_bytes_left(&gbc) < 6)
530  return AVERROR_INVALIDDATA;
531 
532  version = bytestream2_get_byte(&gbc);
533  if (version != 1) {
534  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
535  "first byte %u.", version);
536  return AVERROR_INVALIDDATA;
537  }
538 
539  bytestream2_skip(&gbc, 3);
540  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
541 
542  // SPS array.
543  count = bytestream2_get_byte(&gbc) & 0x1f;
544  start = bytestream2_tell(&gbc);
545  for (i = 0; i < count; i++) {
546  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
547  return AVERROR_INVALIDDATA;
548  size = bytestream2_get_be16(&gbc);
549  if (bytestream2_get_bytes_left(&gbc) < size)
550  return AVERROR_INVALIDDATA;
551  bytestream2_skip(&gbc, size);
552  }
553  end = bytestream2_tell(&gbc);
554 
555  err = ff_h2645_packet_split(&priv->read_packet,
556  frag->data + start, end - start,
557  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
558  if (err < 0) {
559  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
560  return err;
561  }
562  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
563  if (err < 0)
564  return err;
565 
566  // PPS array.
567  count = bytestream2_get_byte(&gbc);
568  start = bytestream2_tell(&gbc);
569  for (i = 0; i < count; i++) {
570  if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
571  return AVERROR_INVALIDDATA;
572  size = bytestream2_get_be16(&gbc);
573  if (bytestream2_get_bytes_left(&gbc) < size)
574  return AVERROR_INVALIDDATA;
575  bytestream2_skip(&gbc, size);
576  }
577  end = bytestream2_tell(&gbc);
578 
579  err = ff_h2645_packet_split(&priv->read_packet,
580  frag->data + start, end - start,
581  ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
582  if (err < 0) {
583  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
584  return err;
585  }
586  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
587  if (err < 0)
588  return err;
589 
590  if (bytestream2_get_bytes_left(&gbc) > 0) {
591  av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
592  "header.\n", bytestream2_get_bytes_left(&gbc));
593  }
594 
595  } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
596  // HVCC header.
597  size_t size, start, end;
598  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
599 
600  priv->mp4 = 1;
601 
602  bytestream2_init(&gbc, frag->data, frag->data_size);
603 
604  if (bytestream2_get_bytes_left(&gbc) < 23)
605  return AVERROR_INVALIDDATA;
606 
607  version = bytestream2_get_byte(&gbc);
608  if (version != 1) {
609  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
610  "first byte %u.", version);
611  return AVERROR_INVALIDDATA;
612  }
613 
614  bytestream2_skip(&gbc, 20);
615  priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
616 
617  nb_arrays = bytestream2_get_byte(&gbc);
618  for (i = 0; i < nb_arrays; i++) {
619  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
620  nb_nals = bytestream2_get_be16(&gbc);
621 
622  start = bytestream2_tell(&gbc);
623  for (j = 0; j < nb_nals; j++) {
624  if (bytestream2_get_bytes_left(&gbc) < 2)
625  return AVERROR_INVALIDDATA;
626  size = bytestream2_get_be16(&gbc);
627  if (bytestream2_get_bytes_left(&gbc) < size)
628  return AVERROR_INVALIDDATA;
629  bytestream2_skip(&gbc, size);
630  }
631  end = bytestream2_tell(&gbc);
632 
633  err = ff_h2645_packet_split(&priv->read_packet,
634  frag->data + start, end - start,
635  ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
636  if (err < 0) {
637  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
638  "HVCC array %d (%d NAL units of type %d).\n",
639  i, nb_nals, nal_unit_type);
640  return err;
641  }
642  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
643  if (err < 0)
644  return err;
645  }
646 
647  } else {
648  // Annex B, or later MP4 with already-known parameters.
649 
650  err = ff_h2645_packet_split(&priv->read_packet,
651  frag->data, frag->data_size,
652  ctx->log_ctx,
653  priv->mp4, priv->nal_length_size,
654  codec_id, 1);
655  if (err < 0)
656  return err;
657 
658  err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
659  if (err < 0)
660  return err;
661  }
662 
663  return 0;
664 }
665 
666 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
667 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
668  const H26 ## h26n ## Raw ## ps_name *ps_var) \
669 { \
670  CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
671  unsigned int id = ps_var->id_element; \
672  if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
673  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
674  " id : %d.\n", id); \
675  return AVERROR_INVALIDDATA; \
676  } \
677  av_freep(&priv->ps_var[id]); \
678  priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
679  if (!priv->ps_var[id]) \
680  return AVERROR(ENOMEM); \
681  memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
682  return 0; \
683 }
684 
685 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
686 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
687 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
688 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
689 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
690 
691 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
692  CodedBitstreamUnit *unit)
693 {
694  GetBitContext gbc;
695  int err;
696 
697  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
698  if (err < 0)
699  return err;
700 
701  switch (unit->type) {
702  case H264_NAL_SPS:
703  {
704  H264RawSPS *sps;
705 
706  sps = av_mallocz(sizeof(*sps));
707  if (!sps)
708  return AVERROR(ENOMEM);
709  err = cbs_h264_read_sps(ctx, &gbc, sps);
710  if (err >= 0)
711  err = cbs_h264_replace_sps(ctx, sps);
712  if (err < 0) {
713  av_free(sps);
714  return err;
715  }
716 
717  unit->content = sps;
718  }
719  break;
720 
721  case H264_NAL_SPS_EXT:
722  {
723  H264RawSPSExtension *sps_ext;
724 
725  sps_ext = av_mallocz(sizeof(*sps_ext));
726  if (!sps_ext)
727  return AVERROR(ENOMEM);
728  err = cbs_h264_read_sps_extension(ctx, &gbc, sps_ext);
729  if (err < 0) {
730  av_free(sps_ext);
731  return err;
732  }
733 
734  unit->content = sps_ext;
735  }
736  break;
737 
738  case H264_NAL_PPS:
739  {
740  H264RawPPS *pps;
741 
742  pps = av_mallocz(sizeof(*pps));
743  if (!pps)
744  return AVERROR(ENOMEM);
745  err = cbs_h264_read_pps(ctx, &gbc, pps);
746  if (err >= 0)
747  err = cbs_h264_replace_pps(ctx, pps);
748  if (err < 0) {
749  av_free(pps);
750  return err;
751  }
752 
753  unit->content = pps;
754  }
755  break;
756 
757  case H264_NAL_SLICE:
758  case H264_NAL_IDR_SLICE:
760  {
761  H264RawSlice *slice;
762  int pos, len;
763 
764  slice = av_mallocz(sizeof(*slice));
765  if (!slice)
766  return AVERROR(ENOMEM);
767  err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
768  if (err < 0) {
769  av_free(slice);
770  return err;
771  }
772 
773  pos = get_bits_count(&gbc);
774  len = unit->data_size;
775  if (!unit->data[len - 1]) {
776  int z;
777  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
778  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
779  "from slice data.\n", z);
780  len -= z;
781  }
782 
783  slice->data_size = len - pos / 8;
784  slice->data = av_malloc(slice->data_size +
786  if (!slice->data) {
787  av_free(slice);
788  return AVERROR(ENOMEM);
789  }
790  memcpy(slice->data,
791  unit->data + pos / 8, slice->data_size);
792  memset(slice->data + slice->data_size, 0,
794  slice->data_bit_start = pos % 8;
795 
796  unit->content = slice;
797  }
798  break;
799 
800  case H264_NAL_AUD:
801  {
802  H264RawAUD *aud;
803 
804  aud = av_mallocz(sizeof(*aud));
805  if (!aud)
806  return AVERROR(ENOMEM);
807  err = cbs_h264_read_aud(ctx, &gbc, aud);
808  if (err < 0) {
809  av_free(aud);
810  return err;
811  }
812 
813  unit->content = aud;
814  }
815  break;
816 
817  case H264_NAL_SEI:
818  {
819  H264RawSEI *sei;
820 
821  sei = av_mallocz(sizeof(*sei));
822  if (!sei)
823  return AVERROR(ENOMEM);
824  err = cbs_h264_read_sei(ctx, &gbc, sei);
825  if (err < 0) {
826  cbs_h264_free_sei(sei);
827  av_free(sei);
828  return err;
829  }
830 
831  unit->content = sei;
832  }
833  break;
834 
835  default:
836  return AVERROR(ENOSYS);
837  }
838 
839  return 0;
840 }
841 
843  CodedBitstreamUnit *unit)
844 {
845  GetBitContext gbc;
846  int err;
847 
848  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
849  if (err < 0)
850  return err;
851 
852  switch (unit->type) {
853  case HEVC_NAL_VPS:
854  {
855  H265RawVPS *vps;
856 
857  vps = av_mallocz(sizeof(*vps));
858  if (!vps)
859  return AVERROR(ENOMEM);
860  err = cbs_h265_read_vps(ctx, &gbc, vps);
861  if (err >= 0)
862  err = cbs_h265_replace_vps(ctx, vps);
863  if (err < 0) {
864  av_free(vps);
865  return err;
866  }
867 
868  unit->content = vps;
869  }
870  break;
871  case HEVC_NAL_SPS:
872  {
873  H265RawSPS *sps;
874 
875  sps = av_mallocz(sizeof(*sps));
876  if (!sps)
877  return AVERROR(ENOMEM);
878  err = cbs_h265_read_sps(ctx, &gbc, sps);
879  if (err >= 0)
880  err = cbs_h265_replace_sps(ctx, sps);
881  if (err < 0) {
882  av_free(sps);
883  return err;
884  }
885 
886  unit->content = sps;
887  }
888  break;
889 
890  case HEVC_NAL_PPS:
891  {
892  H265RawPPS *pps;
893 
894  pps = av_mallocz(sizeof(*pps));
895  if (!pps)
896  return AVERROR(ENOMEM);
897  err = cbs_h265_read_pps(ctx, &gbc, pps);
898  if (err >= 0)
899  err = cbs_h265_replace_pps(ctx, pps);
900  if (err < 0) {
901  av_free(pps);
902  return err;
903  }
904 
905  unit->content = pps;
906  }
907  break;
908 
909  case HEVC_NAL_TRAIL_N:
910  case HEVC_NAL_TRAIL_R:
911  case HEVC_NAL_TSA_N:
912  case HEVC_NAL_TSA_R:
913  case HEVC_NAL_STSA_N:
914  case HEVC_NAL_STSA_R:
915  case HEVC_NAL_RADL_N:
916  case HEVC_NAL_RADL_R:
917  case HEVC_NAL_RASL_N:
918  case HEVC_NAL_RASL_R:
919  case HEVC_NAL_BLA_W_LP:
920  case HEVC_NAL_BLA_W_RADL:
921  case HEVC_NAL_BLA_N_LP:
922  case HEVC_NAL_IDR_W_RADL:
923  case HEVC_NAL_IDR_N_LP:
924  case HEVC_NAL_CRA_NUT:
925  {
926  H265RawSlice *slice;
927  int pos, len;
928 
929  slice = av_mallocz(sizeof(*slice));
930  if (!slice)
931  return AVERROR(ENOMEM);
932  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
933  if (err < 0) {
934  av_free(slice);
935  return err;
936  }
937 
938  pos = get_bits_count(&gbc);
939  len = unit->data_size;
940  if (!unit->data[len - 1]) {
941  int z;
942  for (z = 0; z < len && !unit->data[len - z - 1]; z++);
943  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
944  "from slice data.\n", z);
945  len -= z;
946  }
947 
948  slice->data_size = len - pos / 8;
949  slice->data = av_malloc(slice->data_size +
951  if (!slice->data) {
952  av_free(slice);
953  return AVERROR(ENOMEM);
954  }
955  memcpy(slice->data,
956  unit->data + pos / 8, slice->data_size);
957  memset(slice->data + slice->data_size, 0,
959  slice->data_bit_start = pos % 8;
960 
961  unit->content = slice;
962  }
963  break;
964 
965  case HEVC_NAL_AUD:
966  {
967  H265RawAUD *aud;
968 
969  aud = av_mallocz(sizeof(*aud));
970  if (!aud)
971  return AVERROR(ENOMEM);
972  err = cbs_h265_read_aud(ctx, &gbc, aud);
973  if (err < 0) {
974  av_free(aud);
975  return err;
976  }
977 
978  unit->content = aud;
979  }
980  break;
981 
982  default:
983  return AVERROR(ENOSYS);
984  }
985 
986  return 0;
987 }
988 
990  CodedBitstreamUnit *unit,
991  PutBitContext *pbc)
992 {
993  int err;
994 
995  switch (unit->type) {
996  case H264_NAL_SPS:
997  {
998  H264RawSPS *sps = unit->content;
999 
1000  err = cbs_h264_write_sps(ctx, pbc, sps);
1001  if (err < 0)
1002  return err;
1003 
1004  err = cbs_h264_replace_sps(ctx, sps);
1005  if (err < 0)
1006  return err;
1007  }
1008  break;
1009 
1010  case H264_NAL_SPS_EXT:
1011  {
1012  H264RawSPSExtension *sps_ext = unit->content;
1013 
1014  err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1015  if (err < 0)
1016  return err;
1017  }
1018  break;
1019 
1020  case H264_NAL_PPS:
1021  {
1022  H264RawPPS *pps = unit->content;
1023 
1024  err = cbs_h264_write_pps(ctx, pbc, pps);
1025  if (err < 0)
1026  return err;
1027 
1028  err = cbs_h264_replace_pps(ctx, pps);
1029  if (err < 0)
1030  return err;
1031  }
1032  break;
1033 
1034  case H264_NAL_SLICE:
1035  case H264_NAL_IDR_SLICE:
1037  {
1038  H264RawSlice *slice = unit->content;
1039  GetBitContext gbc;
1040  int bits_left, end, zeroes;
1041 
1042  err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1043  if (err < 0)
1044  return err;
1045 
1046  if (slice->data) {
1047  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1048  return AVERROR(ENOSPC);
1049 
1050  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1051  skip_bits_long(&gbc, slice->data_bit_start);
1052 
1053  // Copy in two-byte blocks, but stop before copying the
1054  // rbsp_stop_one_bit in the final byte.
1055  while (get_bits_left(&gbc) > 23)
1056  put_bits(pbc, 16, get_bits(&gbc, 16));
1057 
1058  bits_left = get_bits_left(&gbc);
1059  end = get_bits(&gbc, bits_left);
1060 
1061  // rbsp_stop_one_bit must be present here.
1062  av_assert0(end);
1063  zeroes = ff_ctz(end);
1064  if (bits_left > zeroes + 1)
1065  put_bits(pbc, bits_left - zeroes - 1,
1066  end >> (zeroes + 1));
1067  put_bits(pbc, 1, 1);
1068  while (put_bits_count(pbc) % 8 != 0)
1069  put_bits(pbc, 1, 0);
1070  } else {
1071  // No slice data - that was just the header.
1072  // (Bitstream may be unaligned!)
1073  }
1074  }
1075  break;
1076 
1077  case H264_NAL_AUD:
1078  {
1079  err = cbs_h264_write_aud(ctx, pbc, unit->content);
1080  if (err < 0)
1081  return err;
1082  }
1083  break;
1084 
1085  case H264_NAL_SEI:
1086  {
1087  err = cbs_h264_write_sei(ctx, pbc, unit->content);
1088  if (err < 0)
1089  return err;
1090  }
1091  break;
1092 
1093  default:
1094  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1095  "NAL unit type %"PRIu32".\n", unit->type);
1096  return AVERROR_PATCHWELCOME;
1097  }
1098 
1099  return 0;
1100 }
1101 
1103  CodedBitstreamUnit *unit,
1104  PutBitContext *pbc)
1105 {
1106  int err;
1107 
1108  switch (unit->type) {
1109  case HEVC_NAL_VPS:
1110  {
1111  H265RawVPS *vps = unit->content;
1112 
1113  err = cbs_h265_write_vps(ctx, pbc, vps);
1114  if (err < 0)
1115  return err;
1116 
1117  err = cbs_h265_replace_vps(ctx, vps);
1118  if (err < 0)
1119  return err;
1120  }
1121  break;
1122 
1123  case HEVC_NAL_SPS:
1124  {
1125  H265RawSPS *sps = unit->content;
1126 
1127  err = cbs_h265_write_sps(ctx, pbc, sps);
1128  if (err < 0)
1129  return err;
1130 
1131  err = cbs_h265_replace_sps(ctx, sps);
1132  if (err < 0)
1133  return err;
1134  }
1135  break;
1136 
1137  case HEVC_NAL_PPS:
1138  {
1139  H265RawPPS *pps = unit->content;
1140 
1141  err = cbs_h265_write_pps(ctx, pbc, pps);
1142  if (err < 0)
1143  return err;
1144 
1145  err = cbs_h265_replace_pps(ctx, pps);
1146  if (err < 0)
1147  return err;
1148  }
1149  break;
1150 
1151  case HEVC_NAL_TRAIL_N:
1152  case HEVC_NAL_TRAIL_R:
1153  case HEVC_NAL_TSA_N:
1154  case HEVC_NAL_TSA_R:
1155  case HEVC_NAL_STSA_N:
1156  case HEVC_NAL_STSA_R:
1157  case HEVC_NAL_RADL_N:
1158  case HEVC_NAL_RADL_R:
1159  case HEVC_NAL_RASL_N:
1160  case HEVC_NAL_RASL_R:
1161  case HEVC_NAL_BLA_W_LP:
1162  case HEVC_NAL_BLA_W_RADL:
1163  case HEVC_NAL_BLA_N_LP:
1164  case HEVC_NAL_IDR_W_RADL:
1165  case HEVC_NAL_IDR_N_LP:
1166  case HEVC_NAL_CRA_NUT:
1167  {
1168  H265RawSlice *slice = unit->content;
1169  GetBitContext gbc;
1170  int bits_left, end, zeroes;
1171 
1172  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1173  if (err < 0)
1174  return err;
1175 
1176  if (slice->data) {
1177  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1178  return AVERROR(ENOSPC);
1179 
1180  init_get_bits(&gbc, slice->data, slice->data_size * 8);
1181  skip_bits_long(&gbc, slice->data_bit_start);
1182 
1183  // Copy in two-byte blocks, but stop before copying the
1184  // rbsp_stop_one_bit in the final byte.
1185  while (get_bits_left(&gbc) > 23)
1186  put_bits(pbc, 16, get_bits(&gbc, 16));
1187 
1188  bits_left = get_bits_left(&gbc);
1189  end = get_bits(&gbc, bits_left);
1190 
1191  // rbsp_stop_one_bit must be present here.
1192  av_assert0(end);
1193  zeroes = ff_ctz(end);
1194  if (bits_left > zeroes + 1)
1195  put_bits(pbc, bits_left - zeroes - 1,
1196  end >> (zeroes + 1));
1197  put_bits(pbc, 1, 1);
1198  while (put_bits_count(pbc) % 8 != 0)
1199  put_bits(pbc, 1, 0);
1200  } else {
1201  // No slice data - that was just the header.
1202  }
1203  }
1204  break;
1205 
1206  case HEVC_NAL_AUD:
1207  {
1208  err = cbs_h265_write_aud(ctx, pbc, unit->content);
1209  if (err < 0)
1210  return err;
1211  }
1212  break;
1213 
1214  default:
1215  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1216  "NAL unit type %"PRIu32".\n", unit->type);
1217  return AVERROR_PATCHWELCOME;
1218  }
1219 
1220  return 0;
1221 }
1222 
1224  CodedBitstreamUnit *unit)
1225 {
1227  enum AVCodecID codec_id = ctx->codec->codec_id;
1228  PutBitContext pbc;
1229  int err;
1230 
1231  if (!priv->write_buffer) {
1232  // Initial write buffer size is 1MB.
1233  priv->write_buffer_size = 1024 * 1024;
1234 
1235  reallocate_and_try_again:
1236  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1237  if (err < 0) {
1238  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1239  "sufficiently large write buffer (last attempt "
1240  "%zu bytes).\n", priv->write_buffer_size);
1241  return err;
1242  }
1243  }
1244 
1245  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1246 
1247  if (codec_id == AV_CODEC_ID_H264)
1248  err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1249  else
1250  err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1251 
1252  if (err == AVERROR(ENOSPC)) {
1253  // Overflow.
1254  priv->write_buffer_size *= 2;
1255  goto reallocate_and_try_again;
1256  }
1257  // Overflow but we didn't notice.
1258  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1259 
1260  if (err < 0) {
1261  // Write failed for some other reason.
1262  return err;
1263  }
1264 
1265  if (put_bits_count(&pbc) % 8)
1266  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1267  else
1268  unit->data_bit_padding = 0;
1269 
1270  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1271  flush_put_bits(&pbc);
1272 
1273  err = av_reallocp(&unit->data, unit->data_size);
1274  if (err < 0)
1275  return err;
1276 
1277  memcpy(unit->data, priv->write_buffer, unit->data_size);
1278 
1279  return 0;
1280 }
1281 
1283  CodedBitstreamFragment *frag)
1284 {
1285  uint8_t *data;
1286  size_t max_size, dp, sp;
1287  int err, i, zero_run;
1288 
1289  for (i = 0; i < frag->nb_units; i++) {
1290  // Data should already all have been written when we get here.
1291  av_assert0(frag->units[i].data);
1292  }
1293 
1294  max_size = 0;
1295  for (i = 0; i < frag->nb_units; i++) {
1296  // Start code + content with worst-case emulation prevention.
1297  max_size += 3 + frag->units[i].data_size * 3 / 2;
1298  }
1299 
1300  data = av_malloc(max_size);
1301  if (!data)
1302  return AVERROR(ENOMEM);
1303 
1304  dp = 0;
1305  for (i = 0; i < frag->nb_units; i++) {
1306  CodedBitstreamUnit *unit = &frag->units[i];
1307 
1308  if (unit->data_bit_padding > 0) {
1309  if (i < frag->nb_units - 1)
1310  av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1311  "unaligned padding on non-final NAL unit.\n");
1312  else
1313  frag->data_bit_padding = unit->data_bit_padding;
1314  }
1315 
1316  if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1317  (unit->type == H264_NAL_SPS ||
1318  unit->type == H264_NAL_PPS)) ||
1319  (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1320  (unit->type == HEVC_NAL_VPS ||
1321  unit->type == HEVC_NAL_SPS ||
1322  unit->type == HEVC_NAL_PPS)) ||
1323  i == 0 /* (Assume this is the start of an access unit.) */) {
1324  // zero_byte
1325  data[dp++] = 0;
1326  }
1327  // start_code_prefix_one_3bytes
1328  data[dp++] = 0;
1329  data[dp++] = 0;
1330  data[dp++] = 1;
1331 
1332  zero_run = 0;
1333  for (sp = 0; sp < unit->data_size; sp++) {
1334  if (zero_run < 2) {
1335  if (unit->data[sp] == 0)
1336  ++zero_run;
1337  else
1338  zero_run = 0;
1339  } else {
1340  if ((unit->data[sp] & ~3) == 0) {
1341  // emulation_prevention_three_byte
1342  data[dp++] = 3;
1343  }
1344  zero_run = unit->data[sp] == 0;
1345  }
1346  data[dp++] = unit->data[sp];
1347  }
1348  }
1349 
1350  av_assert0(dp <= max_size);
1351  err = av_reallocp(&data, dp);
1352  if (err)
1353  return err;
1354 
1355  frag->data = data;
1356  frag->data_size = dp;
1357 
1358  return 0;
1359 }
1360 
1362 {
1363  CodedBitstreamH264Context *h264 = ctx->priv_data;
1364  int i;
1365 
1367 
1368  av_freep(&h264->common.write_buffer);
1369 
1370  for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1371  av_freep(&h264->sps[i]);
1372  for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1373  av_freep(&h264->pps[i]);
1374 }
1375 
1377 {
1378  CodedBitstreamH265Context *h265 = ctx->priv_data;
1379  int i;
1380 
1382 
1383  av_freep(&h265->common.write_buffer);
1384 
1385  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1386  av_freep(&h265->vps[i]);
1387  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1388  av_freep(&h265->sps[i]);
1389  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1390  av_freep(&h265->pps[i]);
1391 }
1392 
1395 
1396  .priv_data_size = sizeof(CodedBitstreamH264Context),
1397 
1398  .split_fragment = &cbs_h2645_split_fragment,
1399  .read_unit = &cbs_h264_read_nal_unit,
1400  .write_unit = &cbs_h2645_write_nal_unit,
1401  .assemble_fragment = &cbs_h2645_assemble_fragment,
1402 
1403  .free_unit = &cbs_h264_free_nal_unit,
1404  .close = &cbs_h264_close,
1405 };
1406 
1409 
1410  .priv_data_size = sizeof(CodedBitstreamH265Context),
1411 
1412  .split_fragment = &cbs_h2645_split_fragment,
1413  .read_unit = &cbs_h265_read_nal_unit,
1414  .write_unit = &cbs_h2645_write_nal_unit,
1415  .assemble_fragment = &cbs_h2645_assemble_fragment,
1416 
1417  .free_unit = &cbs_h265_free_nal_unit,
1418  .close = &cbs_h265_close,
1419 };
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:1223
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:263
uint32_t payload_type
Definition: cbs_h264.h:294
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:34
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define ff_ctz
Definition: intmath.h:106
int nb_units
Number of units in this fragment.
Definition: cbs.h:133
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
Definition: cbs_h265.h:526
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: h2645_parse.h:34
struct H264RawSEIPayload::@56::@57 other
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
H264RawPPS * pps[H264_MAX_PPS_COUNT]
Definition: cbs_h264.h:412
uint8_t * data
Definition: cbs_h265.h:512
Sequence parameter set.
Definition: h264_ps.h:43
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:65
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:1102
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:842
Picture parameter set.
Definition: h264_ps.h:108
int av_log2(unsigned v)
Definition: intmath.c:26
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:1282
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
CodedBitstreamH2645Context common
Definition: cbs_h265.h:520
static void cbs_h264_free_slice(H264RawSlice *slice)
Definition: cbs_h2645.c:421
Macro definitions for various function/variable attributes.
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:445
#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:183
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int data_bit_start
Definition: cbs_h265.h:514
unregistered user data
Definition: h264_sei.h:32
display orientation
Definition: h264_sei.h:35
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:83
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
Definition: cbs_h265.h:525
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:386
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
H264RawSPS * sps[H264_MAX_SPS_COUNT]
Definition: cbs_h264.h:411
#define sp
Definition: regdef.h:63
static void cbs_h264_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1361
Coded bitstream unit structure.
Definition: cbs.h:61
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:92
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:139
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:72
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:121
static void cbs_h264_free_sei(H264RawSEI *sei)
Definition: cbs_h2645.c:396
H.264 common definitions.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
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 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
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
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().
GLsizei count
Definition: opengl_enc.c:109
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:125
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
H264RawSEIUserDataUnregistered user_data_unregistered
Definition: cbs_h264.h:301
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:150
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1393
static void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h2645.c:1376
picture timing
Definition: h264_sei.h:29
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, int32_t value, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:179
AVFormatContext * ctx
Definition: movenc.c:48
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:296
enum AVCodecID codec_id
Definition: vaapi_decode.c:236
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs_h2645.c:138
union H264RawSEIPayload::@56 payload
size_t data_size
Definition: cbs_h264.h:400
int type
NAL unit type.
Definition: h2645_parse.h:51
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:114
#define FF_ARRAY_ELEMS(a)
static void cbs_h264_free_nal_unit(CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:426
H264RawSEIUserDataRegistered user_data_registered
Definition: cbs_h264.h:300
static void cbs_h265_free_nal_unit(CodedBitstreamUnit *unit)
Definition: cbs_h2645.c:440
#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
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
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:108
const uint8_t * data
Definition: h2645_parse.h:35
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs_h2645.c:84
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:145
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
recovery point (frame # to decoder sync)
Definition: h264_sei.h:33
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:279
enum AVCodecID codec_id
Definition: cbs_internal.h:29
registered user data as specified by Rec. ITU-T T.35
Definition: h264_sei.h:31
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
Definition: cbs_h264.h:314
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *name, const char *bits, int64_t value)
Definition: cbs.c:297
void * priv_data
Internal codec-specific data.
Definition: cbs.h:166
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1407
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:770
int data_bit_start
Definition: cbs_h264.h:401
#define av_free(p)
H265RawSliceHeader header
Definition: cbs_h265.h:510
H264RawSliceHeader header
Definition: cbs_h264.h:397
int len
H2645NAL * nals
Definition: h2645_parse.h:75
size_t data_size
Definition: cbs_h265.h:513
CodedBitstreamH2645Context common
Definition: cbs_h264.h:407
#define av_freep(p)
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h2645.c:989
void INT64 start
Definition: avisynth_c.h:690
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:155
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
Definition: cbs_h2645.c:666
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h2645.c:507
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
Definition: cbs_h265.h:524
exp golomb vlc stuff
uint8_t * data
Definition: cbs_h264.h:399
uint8_t payload_count
Definition: cbs_h264.h:315
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:77
const char * name
Definition: opengl_enc.c:103
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition: cbs_h2645.c:474