FFmpeg
cbs_h266.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/intmath.h"
20 #include "libavutil/mem.h"
21 #include "libavutil/refstruct.h"
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h2645.h"
26 #include "cbs_h266.h"
27 #include "cbs_sei.h"
28 #include "get_bits.h"
29 
30 #define HEADER(name) do { \
31  ff_cbs_trace_header(ctx, name); \
32  } while (0)
33 
34 #define CHECK(call) do { \
35  err = (call); \
36  if (err < 0) \
37  return err; \
38  } while (0)
39 
40 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
41 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
42 #define FUNC_H266(name) FUNC_NAME1(READWRITE, h266, name)
43 #define FUNC_NAME2_EXPORT(rw, codec, name) ff_cbs_ ## codec ## _ ## rw ## _ ## name
44 #define FUNC_NAME1_EXPORT(rw, codec, name) FUNC_NAME2_EXPORT(rw, codec, name)
45 #define FUNC_SEI(name) FUNC_NAME1_EXPORT(READWRITE, sei, name)
46 
47 #define SEI_FUNC(name, args) \
48 static int FUNC_H266(name) args; \
49 static int FUNC_H266(name ## _internal)(CodedBitstreamContext *ctx, \
50  RWContext *rw, void *cur, \
51  SEIMessageState *state) \
52 { \
53  return FUNC_H266(name)(ctx, rw, cur, state); \
54 } \
55 static int FUNC_H266(name) args
56 
57 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
58 
59 #define u(width, name, range_min, range_max) \
60  xu(width, name, current->name, range_min, range_max, 0, )
61 #define flag(name) ub(1, name)
62 #define ue(name, range_min, range_max) \
63  xue(name, current->name, range_min, range_max, 0, )
64 #define i(width, name, range_min, range_max) \
65  xi(width, name, current->name, range_min, range_max, 0, )
66 #define ib(width, name) \
67  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
68 #define se(name, range_min, range_max) \
69  xse(name, current->name, range_min, range_max, 0, )
70 
71 #define us(width, name, range_min, range_max, subs, ...) \
72  xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
73 #define ubs(width, name, subs, ...) \
74  xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
75 #define flags(name, subs, ...) \
76  xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
77 #define ues(name, range_min, range_max, subs, ...) \
78  xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
79 #define is(width, name, range_min, range_max, subs, ...) \
80  xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
81 #define ibs(width, name, subs, ...) \
82  xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
83 #define ses(name, range_min, range_max, subs, ...) \
84  xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
85 
86 #define fixed(width, name, value) do { \
87  av_unused uint32_t fixed_value = value; \
88  xu(width, name, fixed_value, value, value, 0, ); \
89  } while (0)
90 
91 
92 #define READ
93 #define READWRITE read
94 #define RWContext GetBitContext
95 
96 #define ub(width, name) do { \
97  uint32_t value; \
98  CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
99  &value)); \
100  current->name = value; \
101  } while (0)
102 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
103  uint32_t value; \
104  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
105  SUBSCRIPTS(subs, __VA_ARGS__), \
106  &value, range_min, range_max)); \
107  var = value; \
108  } while (0)
109 #define xue(name, var, range_min, range_max, subs, ...) do { \
110  uint32_t value; \
111  CHECK(ff_cbs_read_ue_golomb(ctx, rw, #name, \
112  SUBSCRIPTS(subs, __VA_ARGS__), \
113  &value, range_min, range_max)); \
114  var = value; \
115  } while (0)
116 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
117  int32_t value; \
118  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
119  SUBSCRIPTS(subs, __VA_ARGS__), \
120  &value, range_min, range_max)); \
121  var = value; \
122  } while (0)
123 #define xse(name, var, range_min, range_max, subs, ...) do { \
124  int32_t value; \
125  CHECK(ff_cbs_read_se_golomb(ctx, rw, #name, \
126  SUBSCRIPTS(subs, __VA_ARGS__), \
127  &value, range_min, range_max)); \
128  var = value; \
129  } while (0)
130 
131 
132 #define infer(name, value) do { \
133  current->name = value; \
134  } while (0)
135 
136 #define more_rbsp_data(var) ((var) = ff_cbs_h2645_read_more_rbsp_data(rw))
137 
138 #define bit_position(rw) (get_bits_count(rw))
139 #define byte_alignment(rw) (get_bits_count(rw) % 8)
140 
141 #define allocate(name, size) do { \
142  name ## _ref = av_buffer_allocz(size + \
143  AV_INPUT_BUFFER_PADDING_SIZE); \
144  if (!name ## _ref) \
145  return AVERROR(ENOMEM); \
146  name = name ## _ref->data; \
147  } while (0)
148 
149 #define FUNC(name) FUNC_H266(name)
151 #undef FUNC
152 
153 
154 #undef READ
155 #undef READWRITE
156 #undef RWContext
157 #undef ub
158 #undef xu
159 #undef xi
160 #undef xue
161 #undef xse
162 #undef infer
163 #undef more_rbsp_data
164 #undef bit_position
165 #undef byte_alignment
166 #undef allocate
167 #undef allocate_struct
168 
169 
170 #define WRITE
171 #define READWRITE write
172 #define RWContext PutBitContext
173 
174 #define ub(width, name) do { \
175  uint32_t value = current->name; \
176  CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
177  value)); \
178  } while (0)
179 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
180  uint32_t value = var; \
181  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
182  SUBSCRIPTS(subs, __VA_ARGS__), \
183  value, range_min, range_max)); \
184  } while (0)
185 #define xue(name, var, range_min, range_max, subs, ...) do { \
186  uint32_t value = var; \
187  CHECK(ff_cbs_write_ue_golomb(ctx, rw, #name, \
188  SUBSCRIPTS(subs, __VA_ARGS__), \
189  value, range_min, range_max)); \
190  } while (0)
191 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
192  int32_t value = var; \
193  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
194  SUBSCRIPTS(subs, __VA_ARGS__), \
195  value, range_min, range_max)); \
196  } while (0)
197 #define xse(name, var, range_min, range_max, subs, ...) do { \
198  int32_t value = var; \
199  CHECK(ff_cbs_write_se_golomb(ctx, rw, #name, \
200  SUBSCRIPTS(subs, __VA_ARGS__), \
201  value, range_min, range_max)); \
202  } while (0)
203 
204 #define infer(name, value) do { \
205  if (current->name != (value)) { \
206  av_log(ctx->log_ctx, AV_LOG_ERROR, \
207  "%s does not match inferred value: " \
208  "%"PRId64", but should be %"PRId64".\n", \
209  #name, (int64_t)current->name, (int64_t)(value)); \
210  return AVERROR_INVALIDDATA; \
211  } \
212  } while (0)
213 
214 #define more_rbsp_data(var) (var)
215 
216 #define bit_position(rw) (put_bits_count(rw))
217 #define byte_alignment(rw) (put_bits_count(rw) % 8)
218 
219 #define allocate(name, size) do { \
220  if (!name) { \
221  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
222  "for writing.\n", #name); \
223  return AVERROR_INVALIDDATA; \
224  } \
225  } while (0)
226 
227 #define FUNC(name) FUNC_H266(name)
229 #undef FUNC
230 
231 #undef WRITE
232 #undef READWRITE
233 #undef RWContext
234 #undef ub
235 #undef xu
236 #undef xi
237 #undef xue
238 #undef xse
239 #undef u
240 #undef i
241 #undef flag
242 #undef ue
243 #undef se
244 #undef infer
245 #undef more_rbsp_data
246 #undef bit_position
247 #undef byte_alignment
248 #undef allocate
249 
250 
251 
254  int header)
255 {
256  enum AVCodecID codec_id = ctx->codec->codec_id;
258  CodedBitstreamH2645Context *h2645 = &priv->common;
259  GetByteContext gbc;
260  int err;
261 
262  av_assert0(frag->data && frag->nb_units == 0);
263  if (frag->data_size == 0)
264  return 0;
265 
266  if(header && frag->data[0]) {
267  // VVCC header.
268  int ptl_present_flag, num_arrays;
269  int b, i, j;
270 
271  h2645->mp4 = 1;
272 
273  bytestream2_init(&gbc, frag->data, frag->data_size);
274 
275  b = bytestream2_get_byte(&gbc);
276  h2645->nal_length_size = ((b >> 1) & 3) + 1;
277  ptl_present_flag = b & 1;
278 
279  if(ptl_present_flag) {
280  int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
281  num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
282  bytestream2_skip(&gbc, 1);
283 
284  // begin VvcPTLRecord(num_sublayers);
285  num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
286  bytestream2_skip(&gbc, 2 + num_bytes_constraint_info);
287  if(num_sublayers > 1) {
288  int count_present_flags = 0;
289  b = bytestream2_get_byte(&gbc);
290  for(i = num_sublayers - 2; i >= 0; i--) {
291  if((b >> (7 - (num_sublayers - 2 - i))) & 0x01)
292  count_present_flags++;
293  }
294  bytestream2_skip(&gbc, count_present_flags);
295  }
296  num_sub_profiles = bytestream2_get_byte(&gbc);
297  bytestream2_skip(&gbc, num_sub_profiles * 4);
298  // end VvcPTLRecord(num_sublayers);
299 
300  bytestream2_skip(&gbc, 3 * 2);
301  }
302 
303  num_arrays = bytestream2_get_byte(&gbc);
304  for(j = 0; j < num_arrays; j++) {
305  size_t start, end, size;
306  int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
307  unsigned int num_nalus = 1;
308  if(nal_unit_type != VVC_DCI_NUT && nal_unit_type != VVC_OPI_NUT)
309  num_nalus = bytestream2_get_be16(&gbc);
310 
311  start = bytestream2_tell(&gbc);
312  for(i = 0; i < num_nalus; i++) {
313  if (bytestream2_get_bytes_left(&gbc) < 2)
314  return AVERROR_INVALIDDATA;
315  size = bytestream2_get_be16(&gbc);
316  if (bytestream2_get_bytes_left(&gbc) < size)
317  return AVERROR_INVALIDDATA;
318  bytestream2_skip(&gbc, size);
319  }
320  end = bytestream2_tell(&gbc);
321 
322  err = ff_h2645_packet_split(&h2645->read_packet,
323  frag->data + start, end - start,
324  ctx->log_ctx, 2, AV_CODEC_ID_VVC,
326  if (err < 0) {
327  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
328  "VVCC array %d (%d NAL units of type %d).\n",
329  i, num_nalus, nal_unit_type);
330  return err;
331  }
332  err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
333  if (err < 0)
334  return err;
335  }
336  } else {
338  // Annex B, or later MP4 with already-known parameters.
339 
340  err = ff_h2645_packet_split(&h2645->read_packet,
341  frag->data, frag->data_size,
342  ctx->log_ctx,
343  h2645->nal_length_size,
344  codec_id, flags);
345  if (err < 0)
346  return err;
347 
348  err = ff_cbs_h2645_fragment_add_nals(ctx, frag, &h2645->read_packet);
349  if (err < 0)
350  return err;
351  }
352 
353  return 0;
354 }
355 
356 #define cbs_h266_replace_ps(ps_name, ps_var, id_element) \
357 static int cbs_h266_replace_ ## ps_var(CodedBitstreamContext *ctx, \
358  CodedBitstreamUnit *unit) \
359 { \
360  CodedBitstreamH266Context *priv = ctx->priv_data; \
361  H266Raw ## ps_name *ps_var = unit->content; \
362  unsigned int id = ps_var->id_element; \
363  int err = ff_cbs_make_unit_refcounted(ctx, unit); \
364  if (err < 0) \
365  return err; \
366  av_assert0(unit->content_ref); \
367  av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
368  return 0; \
369 }
370 
371 cbs_h266_replace_ps(VPS, vps, vps_video_parameter_set_id)
372 cbs_h266_replace_ps(PPS, pps, pps_pic_parameter_set_id)
373 
374 static int cbs_h266_replace_sps(CodedBitstreamContext *ctx,
375  CodedBitstreamUnit *unit)
376 {
378  H266RawSPS *sps = unit->content;
379  unsigned int id = sps->sps_seq_parameter_set_id;
380  int err = ff_cbs_make_unit_refcounted(ctx, unit);
381  if (err < 0)
382  return err;
383  av_assert0(unit->content_ref);
384  if (priv->sps[id] && memcmp(priv->sps[id], unit->content_ref, sizeof(*priv->sps[id]))) {
385  for (unsigned int i = 0; i < VVC_MAX_PPS_COUNT; i++) {
386  if (priv->pps[i] && priv->pps[i]->pps_seq_parameter_set_id == id)
387  av_refstruct_unref(&priv->pps[i]);
388  }
389  }
390  av_refstruct_replace(&priv->sps[id], unit->content_ref);
391  return 0;
392 }
393 
395  CodedBitstreamUnit *unit,
397 {
399  int err;
400 
401  err = ff_cbs_make_unit_refcounted(ctx, unit);
402  if (err < 0)
403  return err;
404  av_assert0(unit->content_ref);
405  av_refstruct_replace(&h266->ph_ref, unit->content_ref);
406  h266->ph = ph;
407  return 0;
408 }
409 
411  CodedBitstreamUnit *unit)
412 {
413  GetBitContext gbc;
414  int err;
416 
417  err = init_get_bits8(&gbc, unit->data, unit->data_size);
418  if (err < 0)
419  return err;
420 
421  err = ff_cbs_alloc_unit_content(ctx, unit);
422  if (err < 0)
423  return err;
424 
425  switch (unit->type) {
426  case VVC_DCI_NUT:
427  {
428  err = cbs_h266_read_dci(ctx, &gbc, unit->content);
429 
430  if (err < 0)
431  return err;
432  }
433  break;
434  case VVC_OPI_NUT:
435  {
436  err = cbs_h266_read_opi(ctx, &gbc, unit->content);
437 
438  if (err < 0)
439  return err;
440  }
441  break;
442  case VVC_VPS_NUT:
443  {
444  H266RawVPS *vps = unit->content;
445 
446  err = cbs_h266_read_vps(ctx, &gbc, vps);
447  if (err < 0)
448  return err;
449 
450  err = cbs_h266_replace_vps(ctx, unit);
451  if (err < 0)
452  return err;
453  }
454  break;
455  case VVC_SPS_NUT:
456  {
457  H266RawSPS *sps = unit->content;
458 
459  err = cbs_h266_read_sps(ctx, &gbc, sps);
460  if (err < 0)
461  return err;
462 
463  err = cbs_h266_replace_sps(ctx, unit);
464  if (err < 0)
465  return err;
466  }
467  break;
468 
469  case VVC_PPS_NUT:
470  {
471  H266RawPPS *pps = unit->content;
472 
473  err = cbs_h266_read_pps(ctx, &gbc, pps);
474  if (err < 0)
475  return err;
476 
477  err = cbs_h266_replace_pps(ctx, unit);
478  if (err < 0)
479  return err;
480  }
481  break;
482 
483  case VVC_PREFIX_APS_NUT:
484  case VVC_SUFFIX_APS_NUT:
485  {
486  err = cbs_h266_read_aps(ctx, &gbc, unit->content,
487  unit->type == VVC_PREFIX_APS_NUT);
488 
489  if (err < 0)
490  return err;
491  }
492  break;
493  case VVC_PH_NUT:
494  {
495  H266RawPH *ph = unit->content;
496  err = cbs_h266_read_ph(ctx, &gbc, ph);
497  if (err < 0)
498  return err;
499  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
500  if (err < 0)
501  return err;
502  }
503  break;
504 
505  case VVC_TRAIL_NUT:
506  case VVC_STSA_NUT:
507  case VVC_RADL_NUT:
508  case VVC_RASL_NUT:
509  case VVC_IDR_W_RADL:
510  case VVC_IDR_N_LP:
511  case VVC_CRA_NUT:
512  case VVC_GDR_NUT:
513  {
514  H266RawSlice *slice = unit->content;
515  int pos, len;
516 
517  err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
518  if (err < 0)
519  return err;
520 
522  return AVERROR_INVALIDDATA;
523 
524  pos = get_bits_count(&gbc);
525  len = unit->data_size;
526 
528  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
529  if (err < 0)
530  return err;
531  slice->ph_ref = NULL;
532  } else {
533  slice->ph_ref = av_refstruct_ref(h266->ph_ref);
534  }
535  slice->ph = h266->ph;
536  slice->pps = av_refstruct_ref(h266->pps[slice->ph->ph_pic_parameter_set_id]);
537  slice->sps = av_refstruct_ref(h266->sps[slice->pps->pps_seq_parameter_set_id]);
538 
539  slice->header_size = pos / 8;
540  slice->data_size = len - pos / 8;
541  slice->data_ref = av_buffer_ref(unit->data_ref);
542  if (!slice->data_ref)
543  return AVERROR(ENOMEM);
544  slice->data = unit->data + pos / 8;
545  slice->data_bit_start = pos % 8;
546  }
547  break;
548 
549  case VVC_AUD_NUT:
550  {
551  err = cbs_h266_read_aud(ctx, &gbc, unit->content);
552  if (err < 0)
553  return err;
554  }
555  break;
556 
557  case VVC_PREFIX_SEI_NUT:
558  case VVC_SUFFIX_SEI_NUT:
559  {
560  err = cbs_h266_read_sei(ctx, &gbc, unit->content,
561  unit->type == VVC_PREFIX_SEI_NUT);
562 
563  if (err < 0)
564  return err;
565  }
566  break;
567 
568  default:
569  return AVERROR(ENOSYS);
570  }
571  return 0;
572 }
573 
575  CodedBitstreamUnit *unit,
576  PutBitContext *pbc)
577 {
578  int err;
579 
580  switch (unit->type) {
581  case VVC_DCI_NUT:
582  {
583  H266RawDCI *dci = unit->content;
584 
585  err = cbs_h266_write_dci(ctx, pbc, dci);
586  if (err < 0)
587  return err;
588  }
589  break;
590  case VVC_OPI_NUT:
591  {
592  H266RawOPI *opi = unit->content;
593 
594  err = cbs_h266_write_opi(ctx, pbc, opi);
595  if (err < 0)
596  return err;
597  }
598  break;
599  case VVC_VPS_NUT:
600  {
601  H266RawVPS *vps = unit->content;
602 
603  err = cbs_h266_write_vps(ctx, pbc, vps);
604  if (err < 0)
605  return err;
606 
607  err = cbs_h266_replace_vps(ctx, unit);
608  if (err < 0)
609  return err;
610  }
611  break;
612  case VVC_SPS_NUT:
613  {
614  H266RawSPS *sps = unit->content;
615 
616  err = cbs_h266_write_sps(ctx, pbc, sps);
617  if (err < 0)
618  return err;
619 
620  err = cbs_h266_replace_sps(ctx, unit);
621  if (err < 0)
622  return err;
623  }
624  break;
625 
626  case VVC_PPS_NUT:
627  {
628  H266RawPPS *pps = unit->content;
629 
630  err = cbs_h266_write_pps(ctx, pbc, pps);
631  if (err < 0)
632  return err;
633 
634  err = cbs_h266_replace_pps(ctx, unit);
635  if (err < 0)
636  return err;
637  }
638  break;
639 
640  case VVC_PREFIX_APS_NUT:
641  case VVC_SUFFIX_APS_NUT:
642  {
643  err = cbs_h266_write_aps(ctx, pbc, unit->content,
644  unit->type == VVC_PREFIX_APS_NUT);
645  if (err < 0)
646  return err;
647  }
648  break;
649  case VVC_PH_NUT:
650  {
651  H266RawPH *ph = unit->content;
652  err = cbs_h266_write_ph(ctx, pbc, ph);
653  if (err < 0)
654  return err;
655 
656  err = cbs_h266_replace_ph(ctx, unit, &ph->ph_picture_header);
657  if (err < 0)
658  return err;
659  }
660  break;
661 
662  case VVC_TRAIL_NUT:
663  case VVC_STSA_NUT:
664  case VVC_RADL_NUT:
665  case VVC_RASL_NUT:
666  case VVC_IDR_W_RADL:
667  case VVC_IDR_N_LP:
668  case VVC_CRA_NUT:
669  case VVC_GDR_NUT:
670  {
671  H266RawSlice *slice = unit->content;
672 
673  err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
674  if (err < 0)
675  return err;
676 
678  err = cbs_h266_replace_ph(ctx, unit, &slice->header.sh_picture_header);
679  if (err < 0)
680  return err;
681  }
682 
683  if (slice->data) {
684  err = ff_cbs_h2645_write_slice_data(ctx, pbc, slice->data,
685  slice->data_size,
686  slice->data_bit_start);
687  if (err < 0)
688  return err;
689  } else {
690  // No slice data - that was just the header.
691  }
692  }
693  break;
694 
695  case VVC_AUD_NUT:
696  {
697  err = cbs_h266_write_aud(ctx, pbc, unit->content);
698  if (err < 0)
699  return err;
700  }
701  break;
702 
703  case VVC_PREFIX_SEI_NUT:
704  case VVC_SUFFIX_SEI_NUT:
705  {
706  err = cbs_h266_write_sei(ctx, pbc, unit->content,
707  unit->type == VVC_PREFIX_SEI_NUT);
708 
709  if (err < 0)
710  return err;
711  }
712  break;
713 
714  default:
715  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
716  "NAL unit type %"PRIu32".\n", unit->type);
717  return AVERROR_PATCHWELCOME;
718  }
719 
720  return 0;
721 }
722 
724 {
726 
727  for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
728  av_refstruct_unref(&h266->vps[i]);
729  for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
730  av_refstruct_unref(&h266->sps[i]);
731  for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
732  av_refstruct_unref(&h266->pps[i]);
733  av_refstruct_unref(&h266->ph_ref);
734 }
735 
737 {
739 
742 }
743 
744 static void cbs_h266_free_slice(AVRefStructOpaque unused, void *content)
745 {
746  H266RawSlice *slice = content;
747  av_buffer_unref(&slice->data_ref);
748  av_refstruct_unref(&slice->sps);
749  av_refstruct_unref(&slice->pps);
750  av_refstruct_unref(&slice->ph_ref);
751 }
752 
753 
754 static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
755 {
756  H266RawSEI *sei = content;
757  ff_cbs_sei_free_message_list(&sei->message_list);
758 }
759 
764  {
765  .nb_unit_types = 1,
766  .unit_type.list[0] = VVC_SPS_NUT,
767  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
768  .content_size = sizeof(H266RawSPS),
769  .type.ref = {
770  .nb_offsets = 2,
771  .offsets = { offsetof(H266RawSPS, extension_data.data),
772  offsetof(H266RawSPS, vui.extension_data.data) }
773  },
774  },
778 
781 
788 
791 
793 };
794 
797 
798  .priv_data_size = sizeof(CodedBitstreamH266Context),
799 
800  .unit_types = cbs_h266_unit_types,
801 
802  .split_fragment = &cbs_h266_split_fragment,
803  .read_unit = &cbs_h266_read_nal_unit,
804  .write_unit = &cbs_h266_write_nal_unit,
805  .assemble_fragment = &ff_cbs_h2645_assemble_fragment,
806 
807  .flush = &cbs_h266_flush,
808  .close = &cbs_h266_close,
809 };
VVC_RADL_NUT
@ VVC_RADL_NUT
Definition: vvc.h:31
VVC_RASL_NUT
@ VVC_RASL_NUT
Definition: vvc.h:32
VVC_GDR_NUT
@ VVC_GDR_NUT
Definition: vvc.h:39
VVC_STSA_NUT
@ VVC_STSA_NUT
Definition: vvc.h:30
cbs.h
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:119
cbs_h266.h
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
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
H266RawDCI
Definition: cbs_h266.h:252
GetByteContext
Definition: bytestream.h:33
bytestream2_tell
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition: bytestream.h:192
VVC_DCI_NUT
@ VVC_DCI_NUT
Definition: vvc.h:42
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
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
ff_cbs_h2645_read_more_rbsp_data
int ff_cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:217
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3050
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
b
#define b
Definition: input.c:42
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
H266RawPPS::pps_seq_parameter_set_id
uint8_t pps_seq_parameter_set_id
Definition: cbs_h266.h:500
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
cbs_h266_write_nal_unit
static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_h266.c:574
VVC_AUD_NUT
@ VVC_AUD_NUT
Definition: vvc.h:49
cbs_h266_free_slice
static void cbs_h266_free_slice(AVRefStructOpaque unused, void *content)
Definition: cbs_h266.c:744
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
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
CodedBitstreamH266Context::pps
H266RawPPS * pps[VVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:871
CodedBitstreamH266Context::vps
H266RawVPS * vps[VVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:869
H266RawAUD
Definition: cbs_h266.h:646
CBS_UNIT_TYPE_INTERNAL_REF
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
Definition: cbs_internal.h:363
GetBitContext
Definition: get_bits.h:109
H266RawAPS
Definition: cbs_h266.h:600
CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_INTERNAL_REFS
Definition: cbs_internal.h:77
H266RawSliceHeader::sh_picture_header_in_slice_header_flag
uint8_t sh_picture_header_in_slice_header_flag
Definition: cbs_h266.h:773
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:88
i
#define i(width, name, range_min, range_max)
Definition: cbs_h266.c:64
VVC_IDR_W_RADL
@ VVC_IDR_W_RADL
Definition: vvc.h:36
refstruct.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:95
H266RawSlice::ph_ref
void * ph_ref
RefStruct reference backing referred-to PH above.
Definition: cbs_h266.h:855
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
CodedBitstreamH2645Context
Definition: cbs_h2645.h:26
H266RawSlice::data
uint8_t * data
Definition: cbs_h266.h:846
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
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
CodedBitstreamH2645Context::nal_length_size
int nal_length_size
Definition: cbs_h2645.h:31
H266RawSlice::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:854
H266RawSEI
Definition: cbs_h266.h:858
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
cbs_h266_unit_types
static CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[]
Definition: cbs_h266.c:760
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:142
flags
#define flags(name, subs,...)
Definition: cbs_h266.c:75
cbs_h266_split_fragment
static int cbs_h266_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_h266.c:252
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
cbs_h266_replace_ps
#define cbs_h266_replace_ps(ps_name, ps_var, id_element)
Definition: cbs_h266.c:356
get_bits.h
CodedBitstreamH2645Context::mp4
int mp4
Definition: cbs_h2645.h:29
cbs_h266_close
static av_cold void cbs_h266_close(CodedBitstreamContext *ctx)
Definition: cbs_h266.c:736
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
cbs_h266_read_nal_unit
static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_h266.c:410
PutBitContext
Definition: put_bits.h:50
H266RawSPS
Definition: cbs_h266.h:308
H266RawVPS
Definition: cbs_h266.h:262
H266RawPPS
Definition: cbs_h266.h:496
H266RawOPI
Definition: cbs_h266.h:241
H266RawSPS::extension_data
H266RawExtensionData extension_data
Definition: cbs_h266.h:492
NULL
#define NULL
Definition: coverity.c:32
H266RawPictureHeader
Definition: cbs_h266.h:676
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
av_buffer_unref
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:139
cbs_sei.h
VVC_PREFIX_SEI_NUT
@ VVC_PREFIX_SEI_NUT
Definition: vvc.h:52
cbs_h266_replace_ph
static int cbs_h266_replace_ph(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, H266RawPictureHeader *ph)
Definition: cbs_h266.c:394
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
opi
static int FUNC() opi(CodedBitstreamContext *ctx, RWContext *rw, H266RawOPI *current)
Definition: cbs_h266_syntax_template.c:643
cbs_h266_flush
static av_cold void cbs_h266_flush(CodedBitstreamContext *ctx)
Definition: cbs_h266.c:723
CodedBitstreamH266Context::common
CodedBitstreamH2645Context common
Definition: cbs_h266.h:865
CodedBitstreamH2645Context::read_packet
H2645Packet read_packet
Definition: cbs_h2645.h:33
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
VVC_PH_NUT
@ VVC_PH_NUT
Definition: vvc.h:48
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
H266RawSlice::data_bit_start
int data_bit_start
Definition: cbs_h266.h:850
H266RawSliceHeader::sh_picture_header
H266RawPictureHeader sh_picture_header
Definition: cbs_h266.h:774
VVC_VPS_NUT
@ VVC_VPS_NUT
Definition: vvc.h:43
H266RawSlice::data_ref
AVBufferRef * data_ref
Definition: cbs_h266.h:847
CodedBitstreamH266Context::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:872
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
header
static const uint8_t header[24]
Definition: sdr2.c:68
av_refstruct_ref
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
CBS_UNIT_TYPE_POD
#define CBS_UNIT_TYPE_POD(type_, structure)
Definition: cbs_internal.h:339
CodedBitstreamH266Context
Definition: cbs_h266.h:863
AV_CODEC_ID_VVC
@ AV_CODEC_ID_VVC
Definition: codec_id.h:252
VVC_TRAIL_NUT
@ VVC_TRAIL_NUT
Definition: vvc.h:29
CodedBitstreamType
Definition: cbs_internal.h:141
VVC_SPS_NUT
@ VVC_SPS_NUT
Definition: vvc.h:44
ff_cbs_type_h266
const CodedBitstreamType ff_cbs_type_h266
Definition: cbs_h266.c:795
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
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
H266RawSlice::pps
H266RawPPS * pps
RefStruct reference to referred-to PPS.
Definition: cbs_h266.h:853
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
CodedBitstreamH266Context::sps
H266RawSPS * sps[VVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:870
VVC_SUFFIX_SEI_NUT
@ VVC_SUFFIX_SEI_NUT
Definition: vvc.h:53
H266RawSlice::header_size
size_t header_size
Definition: cbs_h266.h:848
VVC_IDR_N_LP
@ VVC_IDR_N_LP
Definition: vvc.h:37
VVC_MAX_PPS_COUNT
@ VVC_MAX_PPS_COUNT
Definition: vvc.h:99
H266RawSlice::data_size
size_t data_size
Definition: cbs_h266.h:849
len
int len
Definition: vorbis_enc_data.h:426
H266RawPictureHeader::ph_pic_parameter_set_id
uint8_t ph_pic_parameter_set_id
Definition: cbs_h266.h:682
CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_END_OF_LIST
Definition: cbs_internal.h:386
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
H266RawSlice::header
H266RawSliceHeader header
Definition: cbs_h266.h:844
pos
unsigned int pos
Definition: spdifenc.c:414
VVC_PPS_NUT
@ VVC_PPS_NUT
Definition: vvc.h:45
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
H2645_FLAG_USE_REF
@ H2645_FLAG_USE_REF
Definition: h2645_parse.h:99
VVC_PREFIX_APS_NUT
@ VVC_PREFIX_APS_NUT
Definition: vvc.h:46
cbs_h266_free_sei
static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
Definition: cbs_h266.c:754
H266RawPH
Definition: cbs_h266.h:766
av_refstruct_replace
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
VVC_SUFFIX_APS_NUT
@ VVC_SUFFIX_APS_NUT
Definition: vvc.h:47
pps
uint64_t pps
Definition: dovi_rpuenc.c:36
VVC_CRA_NUT
@ VVC_CRA_NUT
Definition: vvc.h:38
H266RawExtensionData::data
uint8_t * data
Definition: cbs_h266.h:149
dci
static int FUNC() dci(CodedBitstreamContext *ctx, RWContext *rw, H266RawDCI *current)
Definition: cbs_h266_syntax_template.c:670
VVC_OPI_NUT
@ VVC_OPI_NUT
Definition: vvc.h:41
H266RawSlice::sps
H266RawSPS * sps
RefStruct reference to referred-to SPS.
Definition: cbs_h266.h:852
mem.h
cbs_h266_syntax_template.c
CodedBitstreamH266Context::ph_ref
void * ph_ref
RefStruct reference backing ph above.
Definition: cbs_h266.h:873
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
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
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160
H266RawSlice
Definition: cbs_h266.h:843
intmath.h