FFmpeg
av1_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 "cbs.h"
24 #include "cbs_bsf.h"
25 #include "cbs_av1.h"
26 
27 typedef struct AV1MetadataContext {
29 
30  int td;
32 
36 
39 
42 
45 
46 
49 {
51  AV1RawColorConfig *clc = &seq->color_config;
52  AV1RawTimingInfo *tim = &seq->timing_info;
53 
54  if (ctx->color_primaries >= 0 ||
55  ctx->transfer_characteristics >= 0 ||
56  ctx->matrix_coefficients >= 0) {
58 
59  if (ctx->color_primaries >= 0)
60  clc->color_primaries = ctx->color_primaries;
61  if (ctx->transfer_characteristics >= 0)
62  clc->transfer_characteristics = ctx->transfer_characteristics;
63  if (ctx->matrix_coefficients >= 0)
64  clc->matrix_coefficients = ctx->matrix_coefficients;
65  }
66 
67  if (ctx->color_range >= 0) {
68  if (clc->color_primaries == AVCOL_PRI_BT709 &&
71  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
72  "on RGB streams encoded in BT.709 sRGB.\n");
73  } else {
74  clc->color_range = ctx->color_range;
75  }
76  }
77 
78  if (ctx->chroma_sample_position >= 0) {
79  if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
80  av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
81  "can only be set for 4:2:0 streams.\n");
82  } else {
83  clc->chroma_sample_position = ctx->chroma_sample_position;
84  }
85  }
86 
87  if (ctx->tick_rate.num && ctx->tick_rate.den) {
88  int num, den;
89 
90  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
91  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
92 
93  tim->time_scale = num;
94  tim->num_units_in_display_tick = den;
95  seq->timing_info_present_flag = 1;
96 
97  if (ctx->num_ticks_per_picture > 0) {
98  tim->equal_picture_interval = 1;
100  ctx->num_ticks_per_picture - 1;
101  }
102  }
103 
104  return 0;
105 }
106 
109 {
111  int err, i;
112 
113  for (i = 0; i < frag->nb_units; i++) {
114  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
115  AV1RawOBU *obu = frag->units[i].content;
117  if (err < 0)
118  return err;
119  }
120  }
121 
122  // If a Temporal Delimiter is present, it must be the first OBU.
123  if (frag->nb_units && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
124  if (ctx->td == BSF_ELEMENT_REMOVE)
125  ff_cbs_delete_unit(frag, 0);
126  } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
128  &ctx->td_obu, NULL);
129  if (err < 0) {
130  av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
131  return err;
132  }
133  }
134 
135  if (ctx->delete_padding) {
136  for (i = frag->nb_units - 1; i >= 0; i--) {
137  if (frag->units[i].type == AV1_OBU_PADDING)
138  ff_cbs_delete_unit(frag, i);
139  }
140  }
141 
142  return 0;
143 }
144 
147  .fragment_name = "temporal unit",
148  .unit_name = "OBU",
149  .update_fragment = &av1_metadata_update_fragment,
150 };
151 
153 {
155 
156  ctx->td_obu = (AV1RawOBU) {
157  .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
158  };
159 
161 }
162 
163 #define OFFSET(x) offsetof(AV1MetadataContext, x)
164 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
165 static const AVOption av1_metadata_options[] = {
166  BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
167  td, FLAGS),
168 
169  { "color_primaries", "Set color primaries (section 6.4.2)",
171  { .i64 = -1 }, -1, 255, FLAGS },
172  { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
174  { .i64 = -1 }, -1, 255, FLAGS },
175  { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
176  OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
177  { .i64 = -1 }, -1, 255, FLAGS },
178 
179  { "color_range", "Set color range flag (section 6.4.2)",
181  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
182  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
183  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
184  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
185  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
186 
187  { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
188  OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
189  { .i64 = -1 }, -1, 3, FLAGS, "csp" },
190  { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
191  { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
192  { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
193  { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
194  { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
195  { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
196 
197  { "tick_rate", "Set display tick rate (time_scale / num_units_in_display_tick)",
198  OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
199  { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
200  { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
201  OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
202  { .i64 = -1 }, -1, INT_MAX, FLAGS },
203 
204  { "delete_padding", "Delete all Padding OBUs",
205  OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
206  { .i64 = 0 }, 0, 1, FLAGS},
207 
208  { NULL }
209 };
210 
211 static const AVClass av1_metadata_class = {
212  .class_name = "av1_metadata_bsf",
213  .item_name = av_default_item_name,
214  .option = av1_metadata_options,
215  .version = LIBAVUTIL_VERSION_INT,
216 };
217 
218 static const enum AVCodecID av1_metadata_codec_ids[] = {
220 };
221 
223  .name = "av1_metadata",
224  .priv_data_size = sizeof(AV1MetadataContext),
225  .priv_class = &av1_metadata_class,
227  .close = &ff_cbs_bsf_generic_close,
230 };
AV1RawTimingInfo::num_units_in_display_tick
uint32_t num_units_in_display_tick
Definition: cbs_av1.h:59
AV1RawTimingInfo::time_scale
uint32_t time_scale
Definition: cbs_av1.h:60
av1_metadata_type
static const CBSBSFType av1_metadata_type
Definition: av1_metadata_bsf.c:145
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
td
#define td
Definition: regdef.h:70
AV1RawColorConfig::color_primaries
uint8_t color_primaries
Definition: cbs_av1.h:47
ff_cbs_bsf_generic_init
int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
Initialise generic CBS BSF setup.
Definition: cbs_bsf.c:110
opt.h
av1_metadata_class
static const AVClass av1_metadata_class
Definition: av1_metadata_bsf.c:211
AV1RawSequenceHeader::timing_info_present_flag
uint8_t timing_info_present_flag
Definition: cbs_av1.h:78
AV1RawSequenceHeader
Definition: cbs_av1.h:73
CBSBSFType::codec_id
enum AVCodecID codec_id
Definition: cbs_bsf.h:32
AV1RawColorConfig::color_range
uint8_t color_range
Definition: cbs_av1.h:51
BSF_ELEMENT_INSERT
@ BSF_ELEMENT_INSERT
Definition: cbs_bsf.h:104
AV1MetadataContext::td
int td
Definition: av1_metadata_bsf.c:30
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:106
AVBitStreamFilter::name
const char * name
Definition: bsf.h:91
AVOption
AVOption.
Definition: opt.h:247
AV1RawSequenceHeader::timing_info
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
CBSBSFContext
Definition: cbs_bsf.h:53
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:524
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:73
cbs.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
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:229
AV1MetadataContext::delete_padding
int delete_padding
Definition: av1_metadata_bsf.c:43
AVBSFContext
The bitstream filter state.
Definition: bsf.h:47
AV1RawColorConfig::subsampling_y
uint8_t subsampling_y
Definition: cbs_av1.h:53
init
static int init
Definition: av_tx.c:47
AV1MetadataContext::color_primaries
int color_primaries
Definition: av1_metadata_bsf.c:33
av1_metadata_init
static int av1_metadata_init(AVBSFContext *bsf)
Definition: av1_metadata_bsf.c:152
AV1RawColorConfig
Definition: cbs_av1.h:41
av1_metadata_codec_ids
static enum AVCodecID av1_metadata_codec_ids[]
Definition: av1_metadata_bsf.c:218
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:508
bsf.h
cbs_bsf.h
av1_metadata_update_fragment
static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, CodedBitstreamFragment *frag)
Definition: av1_metadata_bsf.c:107
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
av1_metadata_update_sequence_header
static int av1_metadata_update_sequence_header(AVBSFContext *bsf, AV1RawSequenceHeader *seq)
Definition: av1_metadata_bsf.c:47
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:167
cbs_av1.h
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV1_CSP_UNKNOWN
@ AV1_CSP_UNKNOWN
Definition: av1.h:132
AV1_CSP_VERTICAL
@ AV1_CSP_VERTICAL
Definition: av1.h:133
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
BSF_ELEMENT_REMOVE
@ BSF_ELEMENT_REMOVE
Definition: cbs_bsf.h:106
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV1RawColorConfig::transfer_characteristics
uint8_t transfer_characteristics
Definition: cbs_av1.h:48
color_range
color_range
Definition: vf_selectivecolor.c:44
AV1MetadataContext::common
CBSBSFContext common
Definition: av1_metadata_bsf.c:28
ff_av1_metadata_bsf
const AVBitStreamFilter ff_av1_metadata_bsf
Definition: av1_metadata_bsf.c:222
FLAGS
#define FLAGS
Definition: av1_metadata_bsf.c:164
AV1RawOBU::obu
union AV1RawOBU::@25 obu
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV1RawOBU
Definition: cbs_av1.h:391
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AV1RawTimingInfo
Definition: cbs_av1.h:58
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(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:756
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:471
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV1RawColorConfig::chroma_sample_position
uint8_t chroma_sample_position
Definition: cbs_av1.h:54
AV1MetadataContext::transfer_characteristics
int transfer_characteristics
Definition: av1_metadata_bsf.c:34
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AV1MetadataContext::chroma_sample_position
int chroma_sample_position
Definition: av1_metadata_bsf.c:38
AV1RawTimingInfo::num_ticks_per_picture_minus_1
uint32_t num_ticks_per_picture_minus_1
Definition: cbs_av1.h:63
CBSBSFType
Definition: cbs_bsf.h:31
AV1RawOBU::sequence_header
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:397
ff_cbs_bsf_generic_close
void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
Close a generic CBS BSF instance.
Definition: cbs_bsf.c:150
transfer_characteristics
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
Definition: vf_colorspace.c:177
AV1_OBU_SEQUENCE_HEADER
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
OFFSET
#define OFFSET(x)
Definition: av1_metadata_bsf.c:163
AV1RawColorConfig::color_description_present_flag
uint8_t color_description_present_flag
Definition: cbs_av1.h:46
AV1_OBU_PADDING
@ AV1_OBU_PADDING
Definition: av1.h:39
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
common.h
AV1RawTimingInfo::equal_picture_interval
uint8_t equal_picture_interval
Definition: cbs_av1.h:62
AV1RawSequenceHeader::color_config
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:62
ff_cbs_bsf_generic_filter
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
Filter operation for CBS BSF.
Definition: cbs_bsf.c:61
AV1RawColorConfig::subsampling_x
uint8_t subsampling_x
Definition: cbs_av1.h:52
av1_metadata_options
static const AVOption av1_metadata_options[]
Definition: av1_metadata_bsf.c:165
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:71
AV1MetadataContext::tick_rate
AVRational tick_rate
Definition: av1_metadata_bsf.c:40
BSF_ELEMENT_OPTIONS_PIR
#define BSF_ELEMENT_OPTIONS_PIR(name, help, field, opt_flags)
Definition: cbs_bsf.h:112
AV1MetadataContext::matrix_coefficients
int matrix_coefficients
Definition: av1_metadata_bsf.c:35
AV1MetadataContext::td_obu
AV1RawOBU td_obu
Definition: av1_metadata_bsf.c:31
AV1MetadataContext::color_range
int color_range
Definition: av1_metadata_bsf.c:37
AVBitStreamFilter
Definition: bsf.h:90
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AV1RawColorConfig::mono_chrome
uint8_t mono_chrome
Definition: cbs_av1.h:44
AV1RawColorConfig::matrix_coefficients
uint8_t matrix_coefficients
Definition: cbs_av1.h:49
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV1MetadataContext
Definition: av1_metadata_bsf.c:27
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
color_primaries
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
Definition: vf_colorspace.c:211
AV1MetadataContext::num_ticks_per_picture
int num_ticks_per_picture
Definition: av1_metadata_bsf.c:41
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:152
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:831
AV1_CSP_COLOCATED
@ AV1_CSP_COLOCATED
Definition: av1.h:134