FFmpeg
h265_metadata_bsf.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/common.h"
20 #include "libavutil/opt.h"
21 
22 #include "bsf.h"
23 #include "bsf_internal.h"
24 #include "cbs.h"
25 #include "cbs_h265.h"
26 #include "hevc.h"
27 #include "h265_profile_level.h"
28 
29 enum {
33 };
34 
35 enum {
37  LEVEL_AUTO = -1,
38 };
39 
40 typedef struct H265MetadataContext {
41  const AVClass *class;
42 
45 
47 
48  int aud;
49 
51 
57 
59 
63 
64  int crop_left;
66  int crop_top;
68 
69  int level;
73 
74 
76  const CodedBitstreamFragment *au)
77 {
81  const H265RawHRDParameters *hrd = NULL;
82  int64_t bit_rate = 0;
83  int width = 0, height = 0;
84  int tile_cols = 0, tile_rows = 0;
85  int max_dec_pic_buffering = 0;
86  int i;
87 
88  for (i = 0; i < au->nb_units; i++) {
89  const CodedBitstreamUnit *unit = &au->units[i];
90 
91  if (unit->type == HEVC_NAL_VPS) {
92  const H265RawVPS *vps = unit->content;
93 
94  ptl = &vps->profile_tier_level;
95  max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
96 
97  if (vps->vps_num_hrd_parameters > 0)
98  hrd = &vps->hrd_parameters[0];
99 
100  } else if (unit->type == HEVC_NAL_SPS) {
101  const H265RawSPS *sps = unit->content;
102 
103  ptl = &sps->profile_tier_level;
104  max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
105 
106  width = sps->pic_width_in_luma_samples;
107  height = sps->pic_height_in_luma_samples;
108 
109  if (sps->vui.vui_hrd_parameters_present_flag)
110  hrd = &sps->vui.hrd_parameters;
111 
112  } else if (unit->type == HEVC_NAL_PPS) {
113  const H265RawPPS *pps = unit->content;
114 
115  if (pps->tiles_enabled_flag) {
116  tile_cols = pps->num_tile_columns_minus1 + 1;
117  tile_rows = pps->num_tile_rows_minus1 + 1;
118  }
119  }
120  }
121 
122  if (hrd) {
124  bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
125  (INT64_C(1) << hrd->bit_rate_scale + 6);
126  } else if (hrd->vcl_hrd_parameters_present_flag) {
127  bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
128  (INT64_C(1) << hrd->bit_rate_scale + 6);
129  // Adjust for VCL vs. NAL limits.
130  bit_rate = bit_rate * 11 / 10;
131  }
132  }
133 
134  desc = ff_h265_guess_level(ptl, bit_rate, width, height,
135  0, tile_rows, tile_cols,
136  max_dec_pic_buffering);
137  if (desc) {
138  av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
139  "level %s.\n", desc->name);
140  ctx->level_guess = desc->level_idc;
141  }
142 }
143 
146 {
148 
149  if (ctx->level != LEVEL_UNSET) {
150  if (ctx->level == LEVEL_AUTO) {
151  if (ctx->level_guess) {
152  *level_idc = ctx->level_guess;
153  } else {
154  if (!ctx->level_warned) {
155  av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
156  "of stream: using level 8.5.\n");
157  ctx->level_warned = 1;
158  }
159  *level_idc = 255;
160  }
161  } else {
162  *level_idc = ctx->level;
163  }
164  }
165 }
166 
168  H265RawVPS *vps)
169 {
171 
172  if (ctx->tick_rate.num && ctx->tick_rate.den) {
173  int num, den;
174 
175  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
176  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
177 
178  vps->vps_time_scale = num;
179  vps->vps_num_units_in_tick = den;
180 
181  vps->vps_timing_info_present_flag = 1;
182 
183  if (ctx->num_ticks_poc_diff_one > 0) {
184  vps->vps_num_ticks_poc_diff_one_minus1 =
185  ctx->num_ticks_poc_diff_one - 1;
186  vps->vps_poc_proportional_to_timing_flag = 1;
187  } else if (ctx->num_ticks_poc_diff_one == 0) {
188  vps->vps_poc_proportional_to_timing_flag = 0;
189  }
190  }
191 
192  h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
193 
194  return 0;
195 }
196 
198  H265RawSPS *sps)
199 {
201  int need_vui = 0;
202  int crop_unit_x, crop_unit_y;
203 
204  if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
205  // Table E-1.
206  static const AVRational sar_idc[] = {
207  { 0, 0 }, // Unspecified (never written here).
208  { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
209  { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
210  { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
211  { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
212  };
213  int num, den, i;
214 
215  av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
216  ctx->sample_aspect_ratio.den, 65535);
217 
218  for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
219  if (num == sar_idc[i].num &&
220  den == sar_idc[i].den)
221  break;
222  }
223  if (i == FF_ARRAY_ELEMS(sar_idc)) {
224  sps->vui.aspect_ratio_idc = 255;
225  sps->vui.sar_width = num;
226  sps->vui.sar_height = den;
227  } else {
228  sps->vui.aspect_ratio_idc = i;
229  }
230  sps->vui.aspect_ratio_info_present_flag = 1;
231  need_vui = 1;
232  }
233 
234 #define SET_OR_INFER(field, value, present_flag, infer) do { \
235  if (value >= 0) { \
236  field = value; \
237  need_vui = 1; \
238  } else if (!present_flag) \
239  field = infer; \
240  } while (0)
241 
242  if (ctx->video_format >= 0 ||
243  ctx->video_full_range_flag >= 0 ||
244  ctx->colour_primaries >= 0 ||
245  ctx->transfer_characteristics >= 0 ||
246  ctx->matrix_coefficients >= 0) {
247 
248  SET_OR_INFER(sps->vui.video_format, ctx->video_format,
249  sps->vui.video_signal_type_present_flag, 5);
250 
251  SET_OR_INFER(sps->vui.video_full_range_flag,
252  ctx->video_full_range_flag,
253  sps->vui.video_signal_type_present_flag, 0);
254 
255  if (ctx->colour_primaries >= 0 ||
256  ctx->transfer_characteristics >= 0 ||
257  ctx->matrix_coefficients >= 0) {
258 
259  SET_OR_INFER(sps->vui.colour_primaries,
260  ctx->colour_primaries,
261  sps->vui.colour_description_present_flag, 2);
262 
263  SET_OR_INFER(sps->vui.transfer_characteristics,
264  ctx->transfer_characteristics,
265  sps->vui.colour_description_present_flag, 2);
266 
267  SET_OR_INFER(sps->vui.matrix_coefficients,
268  ctx->matrix_coefficients,
269  sps->vui.colour_description_present_flag, 2);
270 
271  sps->vui.colour_description_present_flag = 1;
272  }
273  sps->vui.video_signal_type_present_flag = 1;
274  need_vui = 1;
275  }
276 
277  if (ctx->chroma_sample_loc_type >= 0) {
278  sps->vui.chroma_sample_loc_type_top_field =
279  ctx->chroma_sample_loc_type;
280  sps->vui.chroma_sample_loc_type_bottom_field =
281  ctx->chroma_sample_loc_type;
282  sps->vui.chroma_loc_info_present_flag = 1;
283  need_vui = 1;
284  }
285 
286  if (ctx->tick_rate.num && ctx->tick_rate.den) {
287  int num, den;
288 
289  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
290  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
291 
292  sps->vui.vui_time_scale = num;
293  sps->vui.vui_num_units_in_tick = den;
294 
295  sps->vui.vui_timing_info_present_flag = 1;
296  need_vui = 1;
297 
298  if (ctx->num_ticks_poc_diff_one > 0) {
299  sps->vui.vui_num_ticks_poc_diff_one_minus1 =
300  ctx->num_ticks_poc_diff_one - 1;
301  sps->vui.vui_poc_proportional_to_timing_flag = 1;
302  } else if (ctx->num_ticks_poc_diff_one == 0) {
303  sps->vui.vui_poc_proportional_to_timing_flag = 0;
304  }
305  }
306 
307  if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
308  crop_unit_x = 1;
309  crop_unit_y = 1;
310  } else {
311  crop_unit_x = 1 + (sps->chroma_format_idc < 3);
312  crop_unit_y = 1 + (sps->chroma_format_idc < 2);
313  }
314 #define CROP(border, unit) do { \
315  if (ctx->crop_ ## border >= 0) { \
316  if (ctx->crop_ ## border % unit != 0) { \
317  av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
318  "must be a multiple of %d.\n", #border, unit); \
319  return AVERROR(EINVAL); \
320  } \
321  sps->conf_win_ ## border ## _offset = \
322  ctx->crop_ ## border / unit; \
323  sps->conformance_window_flag = 1; \
324  } \
325  } while (0)
326  CROP(left, crop_unit_x);
327  CROP(right, crop_unit_x);
328  CROP(top, crop_unit_y);
329  CROP(bottom, crop_unit_y);
330 #undef CROP
331 
332  if (need_vui)
333  sps->vui_parameters_present_flag = 1;
334 
335  h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
336 
337  return 0;
338 }
339 
341 {
343  CodedBitstreamFragment *au = &ctx->access_unit;
344  uint8_t *side_data;
345  int side_data_size;
346  int err, i;
347 
349  &side_data_size);
350  if (!side_data_size)
351  return 0;
352 
353  err = ff_cbs_read(ctx->cbc, au, side_data, side_data_size);
354  if (err < 0) {
355  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
356  return err;
357  }
358 
359  if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
360  h265_metadata_guess_level(bsf, au);
361 
362  for (i = 0; i < au->nb_units; i++) {
363  if (au->units[i].type == HEVC_NAL_VPS) {
364  err = h265_metadata_update_vps(bsf, au->units[i].content);
365  if (err < 0)
366  return err;
367  }
368  if (au->units[i].type == HEVC_NAL_SPS) {
369  err = h265_metadata_update_sps(bsf, au->units[i].content);
370  if (err < 0)
371  return err;
372  }
373  }
374 
375  err = ff_cbs_write_fragment_data(ctx->cbc, au);
376  if (err < 0) {
377  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
378  return err;
379  }
380 
382  if (!side_data)
383  return AVERROR(ENOMEM);
384  memcpy(side_data, au->data, au->data_size);
385 
386  ff_cbs_fragment_reset(ctx->cbc, au);
387 
388  return 0;
389 }
390 
392 {
394  CodedBitstreamFragment *au = &ctx->access_unit;
395  int err, i;
396 
397  err = ff_bsf_get_packet_ref(bsf, pkt);
398  if (err < 0)
399  return err;
400 
402  if (err < 0)
403  goto fail;
404 
405  err = ff_cbs_read_packet(ctx->cbc, au, pkt);
406  if (err < 0) {
407  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
408  goto fail;
409  }
410 
411  if (au->nb_units == 0) {
412  av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
413  err = AVERROR_INVALIDDATA;
414  goto fail;
415  }
416 
417  // If an AUD is present, it must be the first NAL unit.
418  if (au->nb_units && au->units[0].type == HEVC_NAL_AUD) {
419  if (ctx->aud == REMOVE)
420  ff_cbs_delete_unit(ctx->cbc, au, 0);
421  } else {
422  if (ctx->aud == INSERT) {
423  H265RawAUD *aud = &ctx->aud_nal;
424  int pic_type = 0, temporal_id = 8, layer_id = 0;
425 
426  for (i = 0; i < au->nb_units; i++) {
427  const H265RawNALUnitHeader *nal = au->units[i].content;
428  if (!nal)
429  continue;
430  if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
431  temporal_id = nal->nuh_temporal_id_plus1 - 1;
432 
433  if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
434  const H265RawSlice *slice = au->units[i].content;
435  layer_id = nal->nuh_layer_id;
436  if (slice->header.slice_type == HEVC_SLICE_B &&
437  pic_type < 2)
438  pic_type = 2;
439  if (slice->header.slice_type == HEVC_SLICE_P &&
440  pic_type < 1)
441  pic_type = 1;
442  }
443  }
444 
445  aud->nal_unit_header = (H265RawNALUnitHeader) {
446  .nal_unit_type = HEVC_NAL_AUD,
447  .nuh_layer_id = layer_id,
448  .nuh_temporal_id_plus1 = temporal_id + 1,
449  };
450  aud->pic_type = pic_type;
451 
452  err = ff_cbs_insert_unit_content(ctx->cbc, au,
453  0, HEVC_NAL_AUD, aud, NULL);
454  if (err < 0) {
455  av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
456  goto fail;
457  }
458  }
459  }
460 
461  if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
462  h265_metadata_guess_level(bsf, au);
463 
464  for (i = 0; i < au->nb_units; i++) {
465  if (au->units[i].type == HEVC_NAL_VPS) {
466  err = h265_metadata_update_vps(bsf, au->units[i].content);
467  if (err < 0)
468  goto fail;
469  }
470  if (au->units[i].type == HEVC_NAL_SPS) {
471  err = h265_metadata_update_sps(bsf, au->units[i].content);
472  if (err < 0)
473  goto fail;
474  }
475  }
476 
477  err = ff_cbs_write_packet(ctx->cbc, pkt, au);
478  if (err < 0) {
479  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
480  goto fail;
481  }
482 
483  err = 0;
484 fail:
485  ff_cbs_fragment_reset(ctx->cbc, au);
486 
487  if (err < 0)
489 
490  return err;
491 }
492 
494 {
496  CodedBitstreamFragment *au = &ctx->access_unit;
497  int err, i;
498 
499  err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_HEVC, bsf);
500  if (err < 0)
501  return err;
502 
503  if (bsf->par_in->extradata) {
504  err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
505  if (err < 0) {
506  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
507  goto fail;
508  }
509 
510  if (ctx->level == LEVEL_AUTO)
511  h265_metadata_guess_level(bsf, au);
512 
513  for (i = 0; i < au->nb_units; i++) {
514  if (au->units[i].type == HEVC_NAL_VPS) {
515  err = h265_metadata_update_vps(bsf, au->units[i].content);
516  if (err < 0)
517  goto fail;
518  }
519  if (au->units[i].type == HEVC_NAL_SPS) {
520  err = h265_metadata_update_sps(bsf, au->units[i].content);
521  if (err < 0)
522  goto fail;
523  }
524  }
525 
526  err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
527  if (err < 0) {
528  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
529  goto fail;
530  }
531  }
532 
533  err = 0;
534 fail:
535  ff_cbs_fragment_reset(ctx->cbc, au);
536  return err;
537 }
538 
540 {
542 
543  ff_cbs_fragment_free(ctx->cbc, &ctx->access_unit);
544  ff_cbs_close(&ctx->cbc);
545 }
546 
547 #define OFFSET(x) offsetof(H265MetadataContext, x)
548 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
549 static const AVOption h265_metadata_options[] = {
550  { "aud", "Access Unit Delimiter NAL units",
552  { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" },
553  { "pass", NULL, 0, AV_OPT_TYPE_CONST,
554  { .i64 = PASS }, .flags = FLAGS, .unit = "aud" },
555  { "insert", NULL, 0, AV_OPT_TYPE_CONST,
556  { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" },
557  { "remove", NULL, 0, AV_OPT_TYPE_CONST,
558  { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" },
559 
560  { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
561  OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
562  { .dbl = 0.0 }, 0, 65535, FLAGS },
563 
564  { "video_format", "Set video format (table E-2)",
565  OFFSET(video_format), AV_OPT_TYPE_INT,
566  { .i64 = -1 }, -1, 7, FLAGS },
567  { "video_full_range_flag", "Set video full range flag",
568  OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
569  { .i64 = -1 }, -1, 1, FLAGS },
570  { "colour_primaries", "Set colour primaries (table E-3)",
571  OFFSET(colour_primaries), AV_OPT_TYPE_INT,
572  { .i64 = -1 }, -1, 255, FLAGS },
573  { "transfer_characteristics", "Set transfer characteristics (table E-4)",
575  { .i64 = -1 }, -1, 255, FLAGS },
576  { "matrix_coefficients", "Set matrix coefficients (table E-5)",
577  OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
578  { .i64 = -1 }, -1, 255, FLAGS },
579 
580  { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
581  OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
582  { .i64 = -1 }, -1, 6, FLAGS },
583 
584  { "tick_rate",
585  "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
586  OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
587  { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
588  { "num_ticks_poc_diff_one",
589  "Set VPS and VUI number of ticks per POC increment",
590  OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
591  { .i64 = -1 }, -1, INT_MAX, FLAGS },
592 
593  { "crop_left", "Set left border crop offset",
594  OFFSET(crop_left), AV_OPT_TYPE_INT,
595  { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
596  { "crop_right", "Set right border crop offset",
597  OFFSET(crop_right), AV_OPT_TYPE_INT,
598  { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
599  { "crop_top", "Set top border crop offset",
600  OFFSET(crop_top), AV_OPT_TYPE_INT,
601  { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
602  { "crop_bottom", "Set bottom border crop offset",
603  OFFSET(crop_bottom), AV_OPT_TYPE_INT,
604  { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
605 
606  { "level", "Set level (tables A.6 and A.7)",
608  { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
609  { "auto", "Attempt to guess level from stream properties",
611  { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
612 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
613  { .i64 = value }, .flags = FLAGS, .unit = "level"
614  { LEVEL("1", 30) },
615  { LEVEL("2", 60) },
616  { LEVEL("2.1", 63) },
617  { LEVEL("3", 90) },
618  { LEVEL("3.1", 93) },
619  { LEVEL("4", 120) },
620  { LEVEL("4.1", 123) },
621  { LEVEL("5", 150) },
622  { LEVEL("5.1", 153) },
623  { LEVEL("5.2", 156) },
624  { LEVEL("6", 180) },
625  { LEVEL("6.1", 183) },
626  { LEVEL("6.2", 186) },
627  { LEVEL("8.5", 255) },
628 #undef LEVEL
629 
630  { NULL }
631 };
632 
633 static const AVClass h265_metadata_class = {
634  .class_name = "h265_metadata_bsf",
635  .item_name = av_default_item_name,
636  .option = h265_metadata_options,
637  .version = LIBAVUTIL_VERSION_INT,
638 };
639 
640 static const enum AVCodecID h265_metadata_codec_ids[] = {
642 };
643 
645  .name = "hevc_metadata",
646  .priv_data_size = sizeof(H265MetadataContext),
647  .priv_class = &h265_metadata_class,
649  .close = &h265_metadata_close,
652 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
H265MetadataContext::crop_bottom
int crop_bottom
Definition: h265_metadata_bsf.c:67
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
level
uint8_t level
Definition: svq3.c:210
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
bsf_internal.h
opt.h
H265MetadataContext::chroma_sample_loc_type
int chroma_sample_loc_type
Definition: h265_metadata_bsf.c:58
H265RawSubLayerHRDParameters::bit_rate_value_minus1
uint32_t bit_rate_value_minus1[HEVC_MAX_CPB_CNT]
Definition: cbs_h265.h:101
OFFSET
#define OFFSET(x)
Definition: h265_metadata_bsf.c:547
H265MetadataContext::transfer_characteristics
int transfer_characteristics
Definition: h265_metadata_bsf.c:55
ff_cbs_read_extradata
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:224
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
H265RawSlice::header
H265RawSliceHeader header
Definition: cbs_h265.h:542
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
level_idc
int level_idc
Definition: h264_levels.c:25
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
AVOption
AVOption.
Definition: opt.h:246
H265RawNALUnitHeader::nuh_temporal_id_plus1
uint8_t nuh_temporal_id_plus1
Definition: cbs_h265.h:40
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
REMOVE
@ REMOVE
Definition: h265_metadata_bsf.c:32
cbs.h
cbs_h265.h
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
LEVEL_UNSET
@ LEVEL_UNSET
Definition: h265_metadata_bsf.c:36
H265MetadataContext::matrix_coefficients
int matrix_coefficients
Definition: h265_metadata_bsf.c:56
PASS
@ PASS
Definition: h265_metadata_bsf.c:30
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:228
h265_metadata_options
static const AVOption h265_metadata_options[]
Definition: h265_metadata_bsf.c:549
H265MetadataContext::aud_nal
H265RawAUD aud_nal
Definition: h265_metadata_bsf.c:46
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:64
H265LevelDescriptor
Definition: h265_profile_level.h:27
H265RawSPS
Definition: cbs_h265.h:252
h265_metadata_update_level
static void h265_metadata_update_level(AVBSFContext *bsf, uint8_t *level_idc)
Definition: h265_metadata_bsf.c:144
H265RawVPS
Definition: cbs_h265.h:191
H265RawPPS
Definition: cbs_h265.h:354
LEVEL_AUTO
@ LEVEL_AUTO
Definition: h265_metadata_bsf.c:37
bsf.h
fail
#define fail()
Definition: checkasm.h:123
H265RawHRDParameters::vcl_hrd_parameters_present_flag
uint8_t vcl_hrd_parameters_present_flag
Definition: cbs_h265.h:110
ff_cbs_read
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:269
h265_metadata_update_side_data
static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
Definition: h265_metadata_bsf.c:340
H265MetadataContext::level
int level
Definition: h265_metadata_bsf.c:69
H265MetadataContext::crop_left
int crop_left
Definition: h265_metadata_bsf.c:64
ff_cbs_write_extradata
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:376
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:83
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
LEVEL
#define LEVEL(name, value)
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
h265_metadata_close
static void h265_metadata_close(AVBSFContext *bsf)
Definition: h265_metadata_bsf.c:539
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
ptl
const H265RawProfileTierLevel * ptl
Definition: h265_levels.c:170
ff_h265_guess_level
const H265LevelDescriptor * ff_h265_guess_level(const H265RawProfileTierLevel *ptl, int64_t bitrate, int width, int height, int slice_segments, int tile_rows, int tile_cols, int max_dec_pic_buffering)
Guess the level of a stream from some parameters.
Definition: h265_profile_level.c:174
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
width
#define width
HEVC_NAL_RSV_VCL31
@ HEVC_NAL_RSV_VCL31
Definition: hevc.h:60
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:401
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
HEVC_MAX_HEIGHT
@ HEVC_MAX_HEIGHT
Definition: hevc.h:139
H265MetadataContext::video_full_range_flag
int video_full_range_flag
Definition: h265_metadata_bsf.c:53
h265_metadata_update_sps
static int h265_metadata_update_sps(AVBSFContext *bsf, H265RawSPS *sps)
Definition: h265_metadata_bsf.c:197
tile_rows
int tile_rows
Definition: h265_levels.c:217
H265RawHRDParameters::bit_rate_scale
uint8_t bit_rate_scale
Definition: cbs_h265.h:118
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ctx
AVFormatContext * ctx
Definition: movenc.c:48
tile_cols
int tile_cols
Definition: h265_levels.c:218
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:799
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:353
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
HEVC_SLICE_B
@ HEVC_SLICE_B
Definition: hevc.h:96
NULL
#define NULL
Definition: coverity.c:32
H265RawAUD
Definition: cbs_h265.h:445
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
H265MetadataContext::level_warned
int level_warned
Definition: h265_metadata_bsf.c:71
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:420
H265RawSliceHeader::slice_type
uint8_t slice_type
Definition: cbs_h265.h:462
H265RawNALUnitHeader
Definition: cbs_h265.h:37
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
H265MetadataContext::level_guess
int level_guess
Definition: h265_metadata_bsf.c:70
aud
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
Definition: cbs_h264_syntax_template.c:1006
h265_metadata_guess_level
static void h265_metadata_guess_level(AVBSFContext *bsf, const CodedBitstreamFragment *au)
Definition: h265_metadata_bsf.c:75
desc
const char * desc
Definition: nvenc.c:79
pps
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Definition: cbs_h264_syntax_template.c:404
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
transfer_characteristics
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
Definition: vf_colorspace.c:176
h265_profile_level.h
H265RawHRDParameters
Definition: cbs_h265.h:108
CROP
#define CROP(border, unit)
height
#define height
H265MetadataContext
Definition: h265_metadata_bsf.c:40
H265MetadataContext::tick_rate
AVRational tick_rate
Definition: h265_metadata_bsf.c:60
H265RawNALUnitHeader::nuh_layer_id
uint8_t nuh_layer_id
Definition: cbs_h265.h:39
H265MetadataContext::poc_proportional_to_timing_flag
int poc_proportional_to_timing_flag
Definition: h265_metadata_bsf.c:61
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
H265MetadataContext::video_format
int video_format
Definition: h265_metadata_bsf.c:52
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
HEVC_SLICE_P
@ HEVC_SLICE_P
Definition: hevc.h:97
h265_metadata_init
static int h265_metadata_init(AVBSFContext *bsf)
Definition: h265_metadata_bsf.c:493
uint8_t
uint8_t
Definition: audio_convert.c:194
H265MetadataContext::cbc
CodedBitstreamContext * cbc
Definition: h265_metadata_bsf.c:43
H265RawProfileTierLevel
Definition: cbs_h265.h:43
h265_metadata_filter
static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
Definition: h265_metadata_bsf.c:391
H265RawHRDParameters::nal_hrd_parameters_present_flag
uint8_t nal_hrd_parameters_present_flag
Definition: cbs_h265.h:109
ff_cbs_write_fragment_data
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:340
hevc.h
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
H265MetadataContext::colour_primaries
int colour_primaries
Definition: h265_metadata_bsf.c:54
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
ff_cbs_init
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
h265_metadata_class
static const AVClass h265_metadata_class
Definition: h265_metadata_bsf.c:633
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
FLAGS
#define FLAGS
Definition: h265_metadata_bsf.c:548
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
HEVC_MAX_WIDTH
@ HEVC_MAX_WIDTH
Definition: hevc.h:138
AVBitStreamFilter
Definition: bsf.h:98
h265_metadata_update_vps
static int h265_metadata_update_vps(AVBSFContext *bsf, H265RawVPS *vps)
Definition: h265_metadata_bsf.c:167
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
H265MetadataContext::crop_top
int crop_top
Definition: h265_metadata_bsf.c:66
H265RawHRDParameters::vcl_sub_layer_hrd_parameters
H265RawSubLayerHRDParameters vcl_sub_layer_hrd_parameters[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:132
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:142
H265MetadataContext::crop_right
int crop_right
Definition: h265_metadata_bsf.c:65
ff_cbs_read_packet
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:242
ff_hevc_metadata_bsf
const AVBitStreamFilter ff_hevc_metadata_bsf
Definition: h265_metadata_bsf.c:644
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:722
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
AVPacket
This structure stores compressed data.
Definition: packet.h:332
INSERT
@ INSERT
Definition: h265_metadata_bsf.c:31
H265RawHRDParameters::nal_sub_layer_hrd_parameters
H265RawSubLayerHRDParameters nal_sub_layer_hrd_parameters[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:131
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:249
H265MetadataContext::num_ticks_poc_diff_one
int num_ticks_poc_diff_one
Definition: h265_metadata_bsf.c:62
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
H265MetadataContext::access_unit
CodedBitstreamFragment access_unit
Definition: h265_metadata_bsf.c:44
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
H265MetadataContext::sample_aspect_ratio
AVRational sample_aspect_ratio
Definition: h265_metadata_bsf.c:50
h265_metadata_codec_ids
static enum AVCodecID h265_metadata_codec_ids[]
Definition: h265_metadata_bsf.c:640
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
H265RawSlice
Definition: cbs_h265.h:541
SET_OR_INFER
#define SET_OR_INFER(field, value, present_flag, infer)
H265MetadataContext::aud
int aud
Definition: h265_metadata_bsf.c:48