FFmpeg
cbs_h265.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/mem.h"
20 #include "libavutil/refstruct.h"
21 #include "bytestream.h"
22 #include "cbs.h"
23 #include "cbs_internal.h"
24 #include "cbs_h2645.h"
25 #include "cbs_h265.h"
26 #include "cbs_sei.h"
27 #include "get_bits.h"
28 
29 #define HEADER(name) do { \
30  ff_cbs_trace_header(ctx, name); \
31  } while (0)
32 
33 #define CHECK(call) do { \
34  err = (call); \
35  if (err < 0) \
36  return err; \
37  } while (0)
38 
39 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
40 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
41 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
42 #define FUNC_NAME2_EXPORT(rw, codec, name) ff_cbs_ ## codec ## _ ## rw ## _ ## name
43 #define FUNC_NAME1_EXPORT(rw, codec, name) FUNC_NAME2_EXPORT(rw, codec, name)
44 #define FUNC_SEI(name) FUNC_NAME1_EXPORT(READWRITE, sei, name)
45 
46 #define SEI_FUNC(name, args) \
47 static int FUNC_H265(name) args; \
48 static int FUNC_H265(name ## _internal)(CodedBitstreamContext *ctx, \
49  RWContext *rw, void *cur, \
50  SEIMessageState *state) \
51 { \
52  return FUNC_H265(name)(ctx, rw, cur, state); \
53 } \
54 static int FUNC_H265(name) args
55 
56 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
57 
58 #define u(width, name, range_min, range_max) \
59  xu(width, name, current->name, range_min, range_max, 0, )
60 #define flag(name) ub(1, name)
61 #define ue(name, range_min, range_max) \
62  xue(name, current->name, range_min, range_max, 0, )
63 #define i(width, name, range_min, range_max) \
64  xi(width, name, current->name, range_min, range_max, 0, )
65 #define ib(width, name) \
66  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
67 #define se(name, range_min, range_max) \
68  xse(name, current->name, range_min, range_max, 0, )
69 
70 #define us(width, name, range_min, range_max, subs, ...) \
71  xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
72 #define ubs(width, name, subs, ...) \
73  xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
74 #define flags(name, subs, ...) \
75  xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
76 #define ues(name, range_min, range_max, subs, ...) \
77  xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
78 #define is(width, name, range_min, range_max, subs, ...) \
79  xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
80 #define ibs(width, name, subs, ...) \
81  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
82 #define ses(name, range_min, range_max, subs, ...) \
83  xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
84 
85 #define fixed(width, name, value) do { \
86  av_unused uint32_t fixed_value = value; \
87  xu(width, name, fixed_value, value, value, 0, ); \
88  } while (0)
89 
90 
91 #define READ
92 #define READWRITE read
93 #define RWContext GetBitContext
94 
95 #define ub(width, name) do { \
96  uint32_t value; \
97  CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
98  &value)); \
99  current->name = value; \
100  } while (0)
101 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
102  uint32_t value; \
103  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
104  SUBSCRIPTS(subs, __VA_ARGS__), \
105  &value, range_min, range_max)); \
106  var = value; \
107  } while (0)
108 #define xue(name, var, range_min, range_max, subs, ...) do { \
109  uint32_t value; \
110  CHECK(ff_cbs_read_ue_golomb(ctx, rw, #name, \
111  SUBSCRIPTS(subs, __VA_ARGS__), \
112  &value, range_min, range_max)); \
113  var = value; \
114  } while (0)
115 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
116  int32_t value; \
117  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
118  SUBSCRIPTS(subs, __VA_ARGS__), \
119  &value, range_min, range_max)); \
120  var = value; \
121  } while (0)
122 #define xse(name, var, range_min, range_max, subs, ...) do { \
123  int32_t value; \
124  CHECK(ff_cbs_read_se_golomb(ctx, rw, #name, \
125  SUBSCRIPTS(subs, __VA_ARGS__), \
126  &value, range_min, range_max)); \
127  var = value; \
128  } while (0)
129 
130 
131 #define infer(name, value) do { \
132  current->name = value; \
133  } while (0)
134 
135 #define more_rbsp_data(var) ((var) = ff_cbs_h2645_read_more_rbsp_data(rw))
136 
137 #define bit_position(rw) (get_bits_count(rw))
138 #define byte_alignment(rw) (get_bits_count(rw) % 8)
139 
140 #define allocate(name, size) do { \
141  name ## _ref = av_buffer_allocz(size + \
142  AV_INPUT_BUFFER_PADDING_SIZE); \
143  if (!name ## _ref) \
144  return AVERROR(ENOMEM); \
145  name = name ## _ref->data; \
146  } while (0)
147 
148 #define FUNC(name) FUNC_H265(name)
150 #undef FUNC
151 
152 
153 #undef READ
154 #undef READWRITE
155 #undef RWContext
156 #undef ub
157 #undef xu
158 #undef xi
159 #undef xue
160 #undef xse
161 #undef infer
162 #undef more_rbsp_data
163 #undef bit_position
164 #undef byte_alignment
165 #undef allocate
166 #undef allocate_struct
167 
168 
169 #define WRITE
170 #define READWRITE write
171 #define RWContext PutBitContext
172 
173 #define ub(width, name) do { \
174  uint32_t value = current->name; \
175  CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
176  value)); \
177  } while (0)
178 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
179  uint32_t value = var; \
180  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
181  SUBSCRIPTS(subs, __VA_ARGS__), \
182  value, range_min, range_max)); \
183  } while (0)
184 #define xue(name, var, range_min, range_max, subs, ...) do { \
185  uint32_t value = var; \
186  CHECK(ff_cbs_write_ue_golomb(ctx, rw, #name, \
187  SUBSCRIPTS(subs, __VA_ARGS__), \
188  value, range_min, range_max)); \
189  } while (0)
190 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
191  int32_t value = var; \
192  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
193  SUBSCRIPTS(subs, __VA_ARGS__), \
194  value, range_min, range_max)); \
195  } while (0)
196 #define xse(name, var, range_min, range_max, subs, ...) do { \
197  int32_t value = var; \
198  CHECK(ff_cbs_write_se_golomb(ctx, rw, #name, \
199  SUBSCRIPTS(subs, __VA_ARGS__), \
200  value, range_min, range_max)); \
201  } while (0)
202 
203 #define infer(name, value) do { \
204  if (current->name != (value)) { \
205  av_log(ctx->log_ctx, AV_LOG_ERROR, \
206  "%s does not match inferred value: " \
207  "%"PRId64", but should be %"PRId64".\n", \
208  #name, (int64_t)current->name, (int64_t)(value)); \
209  return AVERROR_INVALIDDATA; \
210  } \
211  } while (0)
212 
213 #define more_rbsp_data(var) (var)
214 
215 #define bit_position(rw) (put_bits_count(rw))
216 #define byte_alignment(rw) (put_bits_count(rw) % 8)
217 
218 #define allocate(name, size) do { \
219  if (!name) { \
220  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
221  "for writing.\n", #name); \
222  return AVERROR_INVALIDDATA; \
223  } \
224  } while (0)
225 
226 #define FUNC(name) FUNC_H265(name)
228 #undef FUNC
229 
230 #undef WRITE
231 #undef READWRITE
232 #undef RWContext
233 #undef ub
234 #undef xu
235 #undef xi
236 #undef xue
237 #undef xse
238 #undef u
239 #undef i
240 #undef flag
241 #undef ue
242 #undef se
243 #undef infer
244 #undef more_rbsp_data
245 #undef bit_position
246 #undef byte_alignment
247 #undef allocate
248 
249 
250 
253  int header)
254 {
255  enum AVCodecID codec_id = ctx->codec->codec_id;
257  CodedBitstreamH2645Context *h2645 = &priv->common;
258  GetByteContext gbc;
259  int err;
260 
261  av_assert0(frag->data && frag->nb_units == 0);
262  if (frag->data_size == 0)
263  return 0;
264 
265  if (header && frag->data[0]) {
266  // HVCC header.
267  size_t size, start, end;
268  int i, j, nb_arrays, nal_unit_type, nb_nals, version;
269 
270  h2645->mp4 = 1;
271 
272  bytestream2_init(&gbc, frag->data, frag->data_size);
273 
274  if (bytestream2_get_bytes_left(&gbc) < 23)
275  return AVERROR_INVALIDDATA;
276 
277  version = bytestream2_get_byte(&gbc);
278  if (version != 1) {
279  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
280  "first byte %u.\n", version);
281  return AVERROR_INVALIDDATA;
282  }
283 
284  bytestream2_skip(&gbc, 20);
285  h2645->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
286 
287  nb_arrays = bytestream2_get_byte(&gbc);
288  for (i = 0; i < nb_arrays; i++) {
289  nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
290  nb_nals = bytestream2_get_be16(&gbc);
291 
292  start = bytestream2_tell(&gbc);
293  for (j = 0; j < nb_nals; j++) {
294  if (bytestream2_get_bytes_left(&gbc) < 2)
295  return AVERROR_INVALIDDATA;
296  size = bytestream2_get_be16(&gbc);
297  if (bytestream2_get_bytes_left(&gbc) < size)
298  return AVERROR_INVALIDDATA;
299  bytestream2_skip(&gbc, size);
300  }
301  end = bytestream2_tell(&gbc);
302 
303  err = ff_h2645_packet_split(&h2645->read_packet,
304  frag->data + start, end - start,
305  ctx->log_ctx, 2, AV_CODEC_ID_HEVC,
307  if (err < 0) {
308  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
309  "HVCC array %d (%d NAL units of type %d).\n",
310  i, nb_nals, nal_unit_type);
311  return err;
312  }
313  err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
314  if (err < 0)
315  return err;
316  }
317  } else {
319  // Annex B, or later MP4 with already-known parameters.
320 
321  err = ff_h2645_packet_split(&h2645->read_packet,
322  frag->data, frag->data_size,
323  ctx->log_ctx,
324  h2645->nal_length_size,
325  codec_id, flags);
326  if (err < 0)
327  return err;
328 
329  err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
330  if (err < 0)
331  return err;
332  }
333 
334  return 0;
335 }
336 
337 #define cbs_h2645_replace_ps(ps_name, ps_var, id_element) \
338 static int cbs_h265_replace_ ## ps_var(CodedBitstreamContext *ctx, \
339  CodedBitstreamUnit *unit) \
340 { \
341  CodedBitstreamH265Context *priv = ctx->priv_data; \
342  H265Raw## ps_name *ps_var = unit->content; \
343  unsigned int id = ps_var->id_element; \
344  int err = ff_cbs_make_unit_refcounted(ctx, unit); \
345  if (err < 0) \
346  return err; \
347  if (priv->ps_var[id] == priv->active_ ## ps_var) \
348  priv->active_ ## ps_var = NULL ; \
349  av_assert0(unit->content_ref); \
350  av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
351  return 0; \
352 }
353 
354 cbs_h2645_replace_ps(VPS, vps, vps_video_parameter_set_id)
355 cbs_h2645_replace_ps(SPS, sps, sps_seq_parameter_set_id)
356 cbs_h2645_replace_ps(PPS, pps, pps_pic_parameter_set_id)
357 
358 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
359  CodedBitstreamUnit *unit)
360 {
361  GetBitContext gbc;
362  int err;
363 
364  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
365  if (err < 0)
366  return err;
367 
368  err = ff_cbs_alloc_unit_content(ctx, unit);
369  if (err < 0)
370  return err;
371 
372  switch (unit->type) {
373  case HEVC_NAL_VPS:
374  {
375  H265RawVPS *vps = unit->content;
376 
377  err = cbs_h265_read_vps(ctx, &gbc, vps);
378  if (err < 0)
379  return err;
380 
381  err = cbs_h265_replace_vps(ctx, unit);
382  if (err < 0)
383  return err;
384  }
385  break;
386  case HEVC_NAL_SPS:
387  {
388  H265RawSPS *sps = unit->content;
389 
390  err = cbs_h265_read_sps(ctx, &gbc, sps);
391  if (err < 0)
392  return err;
393 
394  err = cbs_h265_replace_sps(ctx, unit);
395  if (err < 0)
396  return err;
397  }
398  break;
399 
400  case HEVC_NAL_PPS:
401  {
402  H265RawPPS *pps = unit->content;
403 
404  err = cbs_h265_read_pps(ctx, &gbc, pps);
405  if (err < 0)
406  return err;
407 
408  err = cbs_h265_replace_pps(ctx, unit);
409  if (err < 0)
410  return err;
411  }
412  break;
413 
414  case HEVC_NAL_TRAIL_N:
415  case HEVC_NAL_TRAIL_R:
416  case HEVC_NAL_TSA_N:
417  case HEVC_NAL_TSA_R:
418  case HEVC_NAL_STSA_N:
419  case HEVC_NAL_STSA_R:
420  case HEVC_NAL_RADL_N:
421  case HEVC_NAL_RADL_R:
422  case HEVC_NAL_RASL_N:
423  case HEVC_NAL_RASL_R:
424  case HEVC_NAL_BLA_W_LP:
425  case HEVC_NAL_BLA_W_RADL:
426  case HEVC_NAL_BLA_N_LP:
427  case HEVC_NAL_IDR_W_RADL:
428  case HEVC_NAL_IDR_N_LP:
429  case HEVC_NAL_CRA_NUT:
430  {
431  H265RawSlice *slice = unit->content;
432  int pos, len;
433 
434  err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
435  if (err < 0)
436  return err;
437 
439  return AVERROR_INVALIDDATA;
440 
441  pos = get_bits_count(&gbc);
442  len = unit->data_size;
443 
444  slice->data_size = len - pos / 8;
445  slice->data_ref = av_buffer_ref(unit->data_ref);
446  if (!slice->data_ref)
447  return AVERROR(ENOMEM);
448  slice->data = unit->data + pos / 8;
449  slice->data_bit_start = pos % 8;
450  }
451  break;
452 
453  case HEVC_NAL_AUD:
454  {
455  err = cbs_h265_read_aud(ctx, &gbc, unit->content);
456  if (err < 0)
457  return err;
458  }
459  break;
460 
461  case HEVC_NAL_FD_NUT:
462  {
463  err = cbs_h265_read_filler(ctx, &gbc, unit->content);
464  if (err < 0)
465  return err;
466  }
467  break;
468 
469  case HEVC_NAL_SEI_PREFIX:
470  case HEVC_NAL_SEI_SUFFIX:
471  {
472  err = cbs_h265_read_sei(ctx, &gbc, unit->content,
473  unit->type == HEVC_NAL_SEI_PREFIX);
474 
475  if (err < 0)
476  return err;
477  }
478  break;
479 
480  default:
481  return AVERROR(ENOSYS);
482  }
483 
484  return 0;
485 }
486 
488  CodedBitstreamUnit *unit,
489  PutBitContext *pbc)
490 {
491  int err;
492 
493  switch (unit->type) {
494  case HEVC_NAL_VPS:
495  {
496  H265RawVPS *vps = unit->content;
497 
498  err = cbs_h265_write_vps(ctx, pbc, vps);
499  if (err < 0)
500  return err;
501 
502  err = cbs_h265_replace_vps(ctx, unit);
503  if (err < 0)
504  return err;
505  }
506  break;
507 
508  case HEVC_NAL_SPS:
509  {
510  H265RawSPS *sps = unit->content;
511 
512  err = cbs_h265_write_sps(ctx, pbc, sps);
513  if (err < 0)
514  return err;
515 
516  err = cbs_h265_replace_sps(ctx, unit);
517  if (err < 0)
518  return err;
519  }
520  break;
521 
522  case HEVC_NAL_PPS:
523  {
524  H265RawPPS *pps = unit->content;
525 
526  err = cbs_h265_write_pps(ctx, pbc, pps);
527  if (err < 0)
528  return err;
529 
530  err = cbs_h265_replace_pps(ctx, unit);
531  if (err < 0)
532  return err;
533  }
534  break;
535 
536  case HEVC_NAL_TRAIL_N:
537  case HEVC_NAL_TRAIL_R:
538  case HEVC_NAL_TSA_N:
539  case HEVC_NAL_TSA_R:
540  case HEVC_NAL_STSA_N:
541  case HEVC_NAL_STSA_R:
542  case HEVC_NAL_RADL_N:
543  case HEVC_NAL_RADL_R:
544  case HEVC_NAL_RASL_N:
545  case HEVC_NAL_RASL_R:
546  case HEVC_NAL_BLA_W_LP:
547  case HEVC_NAL_BLA_W_RADL:
548  case HEVC_NAL_BLA_N_LP:
549  case HEVC_NAL_IDR_W_RADL:
550  case HEVC_NAL_IDR_N_LP:
551  case HEVC_NAL_CRA_NUT:
552  {
553  H265RawSlice *slice = unit->content;
554 
555  err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
556  if (err < 0)
557  return err;
558 
559  if (slice->data) {
560  err = ff_cbs_h2645_write_slice_data(ctx, pbc, slice->data,
561  slice->data_size,
562  slice->data_bit_start);
563  if (err < 0)
564  return err;
565  } else {
566  // No slice data - that was just the header.
567  }
568  }
569  break;
570 
571  case HEVC_NAL_AUD:
572  {
573  err = cbs_h265_write_aud(ctx, pbc, unit->content);
574  if (err < 0)
575  return err;
576  }
577  break;
578 
579  case HEVC_NAL_FD_NUT:
580  {
581  err = cbs_h265_write_filler(ctx, pbc, unit->content);
582  if (err < 0)
583  return err;
584  }
585  break;
586 
587  case HEVC_NAL_SEI_PREFIX:
588  case HEVC_NAL_SEI_SUFFIX:
589  {
590  err = cbs_h265_write_sei(ctx, pbc, unit->content,
591  unit->type == HEVC_NAL_SEI_PREFIX);
592 
593  if (err < 0)
594  return err;
595  }
596  break;
597 
598  default:
599  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
600  "NAL unit type %"PRIu32".\n", unit->type);
601  return AVERROR_PATCHWELCOME;
602  }
603 
604  return 0;
605 }
606 
608  const CodedBitstreamUnit *unit,
609  enum AVDiscard skip)
610 {
611  H265RawSliceHeader *slice;
612 
613  if (skip <= AVDISCARD_DEFAULT)
614  return 0;
615 
616  switch (unit->type) {
617  case HEVC_NAL_BLA_W_LP:
618  case HEVC_NAL_BLA_W_RADL:
619  case HEVC_NAL_BLA_N_LP:
620  case HEVC_NAL_IDR_W_RADL:
621  case HEVC_NAL_IDR_N_LP:
622  case HEVC_NAL_CRA_NUT:
623  // IRAP slice
624  if (skip < AVDISCARD_ALL)
625  return 0;
626  break;
627 
628  case HEVC_NAL_TRAIL_R:
629  case HEVC_NAL_TRAIL_N:
630  case HEVC_NAL_TSA_N:
631  case HEVC_NAL_TSA_R:
632  case HEVC_NAL_STSA_N:
633  case HEVC_NAL_STSA_R:
634  case HEVC_NAL_RADL_N:
635  case HEVC_NAL_RADL_R:
636  case HEVC_NAL_RASL_N:
637  case HEVC_NAL_RASL_R:
638  // Slice
639  break;
640  default:
641  // Don't discard non-slice nal.
642  return 0;
643  }
644 
645  if (skip >= AVDISCARD_NONKEY)
646  return 1;
647 
648  slice = (H265RawSliceHeader *)unit->content;
649  if (!slice) {
650  av_log(ctx->log_ctx, AV_LOG_WARNING,
651  "h265 slice header is null, missing decompose?\n");
652  return 0;
653  }
654 
655  if (skip >= AVDISCARD_NONINTRA && slice->slice_type != HEVC_SLICE_I)
656  return 1;
657  if (skip >= AVDISCARD_BIDIR && slice->slice_type == HEVC_SLICE_B)
658  return 1;
659 
660  if (skip >= AVDISCARD_NONREF) {
661  switch (unit->type) {
662  case HEVC_NAL_TRAIL_N:
663  case HEVC_NAL_TSA_N:
664  case HEVC_NAL_STSA_N:
665  case HEVC_NAL_RADL_N:
666  case HEVC_NAL_RASL_N:
667  case HEVC_NAL_VCL_N10:
668  case HEVC_NAL_VCL_N12:
669  case HEVC_NAL_VCL_N14:
670  // non-ref
671  return 1;
672  default:
673  break;
674  }
675  }
676 
677  return 0;
678 }
679 
681 {
683 
684  for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
685  av_refstruct_unref(&h265->vps[i]);
686  for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
687  av_refstruct_unref(&h265->sps[i]);
688  for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
689  av_refstruct_unref(&h265->pps[i]);
690 
691  h265->active_vps = NULL;
692  h265->active_sps = NULL;
693  h265->active_pps = NULL;
694 }
695 
697 {
699  int i;
700 
702 
703  for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
704  av_refstruct_unref(&h265->vps[i]);
705  for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
706  av_refstruct_unref(&h265->sps[i]);
707  for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
708  av_refstruct_unref(&h265->pps[i]);
709 }
710 
711 static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
712 {
713  H265RawSEI *sei = content;
714  ff_cbs_sei_free_message_list(&sei->message_list);
715 }
716 
721 
724 
725  // Slices of non-IRAP pictures.
727  H265RawSlice, data),
728  // Slices of IRAP pictures.
730  H265RawSlice, data),
731 
734 
736 };
737 
738 // Macro for the read/write pair.
739 #define SEI_MESSAGE_RW(codec, name) \
740  .read = cbs_ ## codec ## _read_ ## name ## _internal, \
741  .write = cbs_ ## codec ## _write_ ## name ## _internal
742 
744  {
746  1, 0,
748  SEI_MESSAGE_RW(h265, sei_buffering_period),
749  },
750  {
752  1, 0,
753  sizeof(H265RawSEIPicTiming),
754  SEI_MESSAGE_RW(h265, sei_pic_timing),
755  },
756  {
758  1, 0,
759  sizeof(H265RawSEIPanScanRect),
760  SEI_MESSAGE_RW(h265, sei_pan_scan_rect),
761  },
762  {
764  1, 0,
765  sizeof(H265RawSEIRecoveryPoint),
766  SEI_MESSAGE_RW(h265, sei_recovery_point),
767  },
768  {
770  1, 0,
772  SEI_MESSAGE_RW(h265, film_grain_characteristics),
773  },
774  {
776  1, 0,
778  SEI_MESSAGE_RW(h265, sei_display_orientation),
779  },
780  {
782  1, 0,
784  SEI_MESSAGE_RW(h265, sei_active_parameter_sets),
785  },
786  {
788  0, 1,
790  SEI_MESSAGE_RW(h265, sei_decoded_picture_hash),
791  },
792  {
794  1, 0,
795  sizeof(H265RawSEITimeCode),
796  SEI_MESSAGE_RW(h265, sei_time_code),
797  },
798  {
800  1, 0,
802  SEI_MESSAGE_RW(h265, sei_alpha_channel_info),
803  },
804  {
806  1, 0,
808  SEI_MESSAGE_RW(h265, sei_3d_reference_displays_info),
809  },
811 };
812 
815 
816  .priv_data_size = sizeof(CodedBitstreamH265Context),
817 
818  .unit_types = cbs_h265_unit_types,
819 
820  .split_fragment = &cbs_h265_split_fragment,
821  .read_unit = &cbs_h265_read_nal_unit,
822  .write_unit = &cbs_h265_write_nal_unit,
823  .discarded_unit = &cbs_h265_discarded_nal_unit,
824  .assemble_fragment = &ff_cbs_h2645_assemble_fragment,
825 
826  .flush = &cbs_h265_flush,
827  .close = &cbs_h265_close,
828 };
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
SEI_TYPE_ALPHA_CHANNEL_INFO
@ SEI_TYPE_ALPHA_CHANNEL_INFO
Definition: sei.h:123
cbs.h
CodedBitstreamH265Context::vps
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:756
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
H265RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h265.h:589
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
H265RawSEITimeCode
Definition: cbs_h265.h:693
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
GetByteContext
Definition: bytestream.h:33
bytestream2_tell
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition: bytestream.h:192
cbs_h265_syntax_template.c
H265RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h265.h:587
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:114
H265RawSEIActiveParameterSets
Definition: cbs_h265.h:677
H265RawSlice::header
H265RawSliceHeader header
Definition: cbs_h265.h:584
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
SEI_TYPE_PAN_SCAN_RECT
@ SEI_TYPE_PAN_SCAN_RECT
Definition: sei.h:32
ff_cbs_h2645_read_more_rbsp_data
int ff_cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:217
H265RawSEI
Definition: cbs_h265.h:739
HEVC_NAL_BLA_N_LP
@ HEVC_NAL_BLA_N_LP
Definition: hevc.h:47
CodedBitstreamH265Context::sps
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:757
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:226
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:604
data
const char data[16]
Definition: mxf.c:149
H265RawSEIPanScanRect
Definition: cbs_h265.h:630
HEVC_NAL_TSA_N
@ HEVC_NAL_TSA_N
Definition: hevc.h:31
HEVC_NAL_IDR_N_LP
@ HEVC_NAL_IDR_N_LP
Definition: hevc.h:49
ff_cbs_h2645_fragment_add_nals
int ff_cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition: cbs_h2645.c:229
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:81
H265RawSEIDecodedPictureHash
Definition: cbs_h265.h:686
cbs_h265.h
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
HEVC_NAL_IDR_W_RADL
@ HEVC_NAL_IDR_W_RADL
Definition: hevc.h:48
CodedBitstreamH265Context::common
CodedBitstreamH2645Context common
Definition: cbs_h265.h:752
cbs_h265_close
static av_cold void cbs_h265_close(CodedBitstreamContext *ctx)
Definition: cbs_h265.c:696
SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO
@ SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO
Definition: sei.h:127
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:77
ff_cbs_h2645_assemble_fragment
int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_h2645.c:341
H265RawSEI3DReferenceDisplaysInfo
Definition: cbs_h265.h:723
H265RawSPS
Definition: cbs_h265.h:245
H265RawVPS
Definition: cbs_h265.h:184
H265RawPPS
Definition: cbs_h265.h:356
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
cbs_h265_discarded_nal_unit
static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
Definition: cbs_h265.c:607
HEVC_NAL_VCL_N14
@ HEVC_NAL_VCL_N14
Definition: hevc.h:43
CBS_UNIT_TYPE_INTERNAL_REF
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
Definition: cbs_internal.h:363
H265RawSEIPicTiming
Definition: cbs_h265.h:614
GetBitContext
Definition: get_bits.h:109
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:88
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
ff_cbs_sei_h265_types
const SEIMessageTypeDescriptor ff_cbs_sei_h265_types[]
Definition: cbs_h265.c:743
refstruct.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:95
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
HEVC_SLICE_B
@ HEVC_SLICE_B
Definition: hevc.h:96
CodedBitstreamH2645Context
Definition: cbs_h2645.h:26
extension_data
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawExtensionData *current)
Definition: cbs_h265_syntax_template.c:61
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:106
CodedBitstreamH2645Context::nal_length_size
int nal_length_size
Definition: cbs_h2645.h:31
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
cbs_h265_write_nal_unit
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h265.c:487
HEVC_NAL_VCL_N10
@ HEVC_NAL_VCL_N10
Definition: hevc.h:39
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:142
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
SEI_MESSAGE_TYPE_END
#define SEI_MESSAGE_TYPE_END
Definition: cbs_sei.h:202
get_bits.h
CodedBitstreamH2645Context::mp4
int mp4
Definition: cbs_h2645.h:29
H265RawSEIDisplayOrientation
Definition: cbs_h265.h:668
cbs_internal.h
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
CodedBitstreamType::codec_id
enum AVCodecID codec_id
Definition: cbs_internal.h:142
AVDISCARD_BIDIR
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition: defs.h:229
PutBitContext
Definition: put_bits.h:50
H265RawSEIRecoveryPoint
Definition: cbs_h265.h:641
ff_cbs_type_h265
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h265.c:813
if
if(ret)
Definition: filter_design.txt:179
HEVC_NAL_VCL_N12
@ HEVC_NAL_VCL_N12
Definition: hevc.h:41
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:232
NULL
#define NULL
Definition: coverity.c:32
H265RawAUD
Definition: cbs_h265.h:487
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
SEI_TYPE_TIME_CODE
@ SEI_TYPE_TIME_CODE
Definition: sei.h:95
cbs_sei.h
cbs_h265_unit_types
static CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[]
Definition: cbs_h265.c:717
SPS
Sequence parameter set.
Definition: h264_ps.h:44
SEIMessageTypeDescriptor
Definition: cbs_sei.h:186
HEVC_SLICE_I
@ HEVC_SLICE_I
Definition: hevc.h:98
HEVC_NAL_STSA_N
@ HEVC_NAL_STSA_N
Definition: hevc.h:33
CodedBitstreamUnit::data_size
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:93
PPS
Picture parameter set.
Definition: h264_ps.h:110
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:552
CodedBitstreamH2645Context::read_packet
H2645Packet read_packet
Definition: cbs_h2645.h:33
CBS_UNIT_RANGE_INTERNAL_REF
#define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field)
Definition: cbs_internal.h:366
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
H265RawSliceHeader::slice_type
uint8_t slice_type
Definition: cbs_h265.h:504
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:858
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
HEVC_NAL_RASL_R
@ HEVC_NAL_RASL_R
Definition: hevc.h:38
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:231
AVDISCARD_DEFAULT
@ AVDISCARD_DEFAULT
discard useless packets like 0 size packets in avi
Definition: defs.h:227
HEVC_NAL_RASL_N
@ HEVC_NAL_RASL_N
Definition: hevc.h:37
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
size
int size
Definition: twinvq_data.h:10344
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:135
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
header
static const uint8_t header[24]
Definition: sdr2.c:68
HEVC_NAL_STSA_R
@ HEVC_NAL_STSA_R
Definition: hevc.h:34
CBS_UNIT_TYPE_POD
#define CBS_UNIT_TYPE_POD(type_, structure)
Definition: cbs_internal.h:339
CodedBitstreamType
Definition: cbs_internal.h:141
version
version
Definition: libkvazaar.c:313
cbs_h265_free_sei
static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
Definition: cbs_h265.c:711
H265RawFilmGrainCharacteristics
Definition: cbs_h265.h:647
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
SEI_MESSAGE_RW
#define SEI_MESSAGE_RW(codec, name)
Definition: cbs_h265.c:739
H265RawSliceHeader
Definition: cbs_h265.h:493
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
HEVC_NAL_TSA_R
@ HEVC_NAL_TSA_R
Definition: hevc.h:32
AVDISCARD_NONINTRA
@ AVDISCARD_NONINTRA
discard all non intra frames
Definition: defs.h:230
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:105
CBS_UNIT_TYPES_COMPLEX
#define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func)
Definition: cbs_internal.h:376
cbs_h2645.h
H265RawSlice::data_size
size_t data_size
Definition: cbs_h265.h:588
SEI_TYPE_PIC_TIMING
@ SEI_TYPE_PIC_TIMING
Definition: sei.h:31
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
flags
#define flags(name, subs,...)
Definition: cbs_h265.c:74
SEI_TYPE_DISPLAY_ORIENTATION
@ SEI_TYPE_DISPLAY_ORIENTATION
Definition: sei.h:77
len
int len
Definition: vorbis_enc_data.h:426
cbs_h2645_replace_ps
#define cbs_h2645_replace_ps(ps_name, ps_var, id_element)
Definition: cbs_h265.c:337
CodedBitstreamH265Context::active_sps
const H265RawSPS * active_sps
Definition: cbs_h265.h:764
CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_END_OF_LIST
Definition: cbs_internal.h:386
CodedBitstreamH265Context::pps
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h265.h:758
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
cbs_h265_split_fragment
static int cbs_h265_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h265.c:251
pos
unsigned int pos
Definition: spdifenc.c:414
ff_cbs_h2645_write_slice_data
int ff_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:265
CodedBitstreamH265Context::active_pps
const H265RawPPS * active_pps
Definition: cbs_h265.h:765
H2645_FLAG_USE_REF
@ H2645_FLAG_USE_REF
Definition: h2645_parse.h:99
HEVC_NAL_CRA_NUT
@ HEVC_NAL_CRA_NUT
Definition: hevc.h:50
SEI_TYPE_BUFFERING_PERIOD
@ SEI_TYPE_BUFFERING_PERIOD
Definition: sei.h:30
CodedBitstreamH265Context::active_vps
const H265RawVPS * active_vps
Definition: cbs_h265.h:763
pps
uint64_t pps
Definition: dovi_rpuenc.c:36
SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_RECOVERY_POINT
Definition: sei.h:36
SEI_TYPE_DECODED_PICTURE_HASH
@ SEI_TYPE_DECODED_PICTURE_HASH
Definition: sei.h:91
mem.h
HEVC_NAL_TRAIL_R
@ HEVC_NAL_TRAIL_R
Definition: hevc.h:30
HEVC_NAL_RADL_R
@ HEVC_NAL_RADL_R
Definition: hevc.h:36
H265RawSlice::data
uint8_t * data
Definition: cbs_h265.h:586
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
i
#define i(width, name, range_min, range_max)
Definition: cbs_h265.c:63
H2645_FLAG_IS_NALFF
@ H2645_FLAG_IS_NALFF
Definition: h2645_parse.h:97
ff_h2645_packet_split
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int nal_length_size, enum AVCodecID codec_id, int flags)
Split an input packet into NAL units.
Definition: h2645_parse.c:466
ff_cbs_sei_free_message_list
void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
Free all SEI messages in a message list.
Definition: cbs_sei.c:291
AVDiscard
AVDiscard
Definition: defs.h:223
AVDISCARD_NONREF
@ AVDISCARD_NONREF
discard all non reference
Definition: defs.h:228
HEVC_NAL_RADL_N
@ HEVC_NAL_RADL_N
Definition: hevc.h:35
SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
@ SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
Definition: sei.h:49
SEI_TYPE_ACTIVE_PARAMETER_SETS
@ SEI_TYPE_ACTIVE_PARAMETER_SETS
Definition: sei.h:87
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
cbs_h265_flush
static av_cold void cbs_h265_flush(CodedBitstreamContext *ctx)
Definition: cbs_h265.c:680
H265RawSEIBufferingPeriod
Definition: cbs_h265.h:593
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
H265RawSEIAlphaChannelInfo
Definition: cbs_h265.h:712
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160
HEVC_NAL_FD_NUT
@ HEVC_NAL_FD_NUT
Definition: hevc.h:67
HEVC_NAL_BLA_W_RADL
@ HEVC_NAL_BLA_W_RADL
Definition: hevc.h:46
CodedBitstreamH265Context
Definition: cbs_h265.h:750
HEVC_NAL_TRAIL_N
@ HEVC_NAL_TRAIL_N
Definition: hevc.h:29
H265RawSlice
Definition: cbs_h265.h:583
HEVC_NAL_BLA_W_LP
@ HEVC_NAL_BLA_W_LP
Definition: hevc.h:45
H265RawFiller
Definition: cbs_h265.h:744