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_av1.h"
25 
26 enum {
30 };
31 
32 typedef struct AV1MetadataContext {
33  const AVClass *class;
34 
37 
38  int td;
39 
43 
46 
49 
52 
53 
56 {
58  AV1RawColorConfig *clc = &seq->color_config;
59  AV1RawTimingInfo *tim = &seq->timing_info;
60 
61  if (ctx->color_primaries >= 0 ||
62  ctx->transfer_characteristics >= 0 ||
63  ctx->matrix_coefficients >= 0) {
69  }
70 
71  if (ctx->color_primaries >= 0)
72  clc->color_primaries = ctx->color_primaries;
73  if (ctx->transfer_characteristics >= 0)
75  if (ctx->matrix_coefficients >= 0)
77  }
78 
79  if (ctx->color_range >= 0) {
80  if (clc->color_primaries == AVCOL_PRI_BT709 &&
83  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
84  "on RGB streams encoded in BT.709 sRGB.\n");
85  } else {
86  clc->color_range = ctx->color_range;
87  }
88  }
89 
90  if (ctx->chroma_sample_position >= 0) {
91  if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
92  av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
93  "can only be set for 4:2:0 streams.\n");
94  } else {
96  }
97  }
98 
99  if (ctx->tick_rate.num && ctx->tick_rate.den) {
100  int num, den;
101 
102  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
103  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
104 
105  tim->time_scale = num;
106  tim->num_units_in_display_tick = den;
107  seq->timing_info_present_flag = 1;
108 
109  if (ctx->num_ticks_per_picture > 0) {
110  tim->equal_picture_interval = 1;
112  ctx->num_ticks_per_picture - 1;
113  }
114  }
115 
116  return 0;
117 }
118 
120 {
122  CodedBitstreamFragment *frag = &ctx->access_unit;
123  AV1RawOBU td, *obu;
124  int err, i;
125 
126  err = ff_bsf_get_packet_ref(bsf, pkt);
127  if (err < 0)
128  return err;
129 
130  err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
131  if (err < 0) {
132  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
133  goto fail;
134  }
135 
136  if (frag->nb_units == 0) {
137  av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
138  err = AVERROR_INVALIDDATA;
139  goto fail;
140  }
141 
142  for (i = 0; i < frag->nb_units; i++) {
143  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
144  obu = frag->units[i].content;
146  if (err < 0)
147  goto fail;
148  }
149  }
150 
151  // If a Temporal Delimiter is present, it must be the first OBU.
152  if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
153  if (ctx->td == REMOVE)
154  ff_cbs_delete_unit(ctx->cbc, frag, 0);
155  } else if (ctx->td == INSERT) {
156  td = (AV1RawOBU) {
158  };
159 
161  &td, NULL);
162  if (err < 0) {
163  av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
164  goto fail;
165  }
166  }
167 
168  if (ctx->delete_padding) {
169  for (i = frag->nb_units - 1; i >= 0; i--) {
170  if (frag->units[i].type == AV1_OBU_PADDING)
171  ff_cbs_delete_unit(ctx->cbc, frag, i);
172  }
173  }
174 
175  err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
176  if (err < 0) {
177  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
178  goto fail;
179  }
180 
181  err = 0;
182 fail:
183  ff_cbs_fragment_reset(ctx->cbc, frag);
184 
185  if (err < 0)
186  av_packet_unref(pkt);
187 
188  return err;
189 }
190 
192 {
194  CodedBitstreamFragment *frag = &ctx->access_unit;
195  AV1RawOBU *obu;
196  int err, i;
197 
198  err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
199  if (err < 0)
200  return err;
201 
202  if (bsf->par_in->extradata) {
203  err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
204  if (err < 0) {
205  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
206  goto fail;
207  }
208 
209  for (i = 0; i < frag->nb_units; i++) {
210  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
211  obu = frag->units[i].content;
213  if (err < 0)
214  goto fail;
215  }
216  }
217 
218  err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
219  if (err < 0) {
220  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
221  goto fail;
222  }
223  }
224 
225  err = 0;
226 fail:
227  ff_cbs_fragment_reset(ctx->cbc, frag);
228  return err;
229 }
230 
232 {
234 
235  ff_cbs_fragment_free(ctx->cbc, &ctx->access_unit);
236  ff_cbs_close(&ctx->cbc);
237 }
238 
239 #define OFFSET(x) offsetof(AV1MetadataContext, x)
240 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
241 static const AVOption av1_metadata_options[] = {
242  { "td", "Temporal Delimiter OBU",
244  { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
245  { "pass", NULL, 0, AV_OPT_TYPE_CONST,
246  { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
247  { "insert", NULL, 0, AV_OPT_TYPE_CONST,
248  { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
249  { "remove", NULL, 0, AV_OPT_TYPE_CONST,
250  { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
251 
252  { "color_primaries", "Set color primaries (section 6.4.2)",
254  { .i64 = -1 }, -1, 255, FLAGS },
255  { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
257  { .i64 = -1 }, -1, 255, FLAGS },
258  { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
260  { .i64 = -1 }, -1, 255, FLAGS },
261 
262  { "color_range", "Set color range flag (section 6.4.2)",
264  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
265  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
266  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
267  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
268  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
269 
270  { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
272  { .i64 = -1 }, -1, 3, FLAGS, "csp" },
273  { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
274  { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
275  { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
276  { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
277  { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
278  { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
279 
280  { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
282  { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
283  { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
285  { .i64 = -1 }, -1, INT_MAX, FLAGS },
286 
287  { "delete_padding", "Delete all Padding OBUs",
289  { .i64 = 0 }, 0, 1, FLAGS},
290 
291  { NULL }
292 };
293 
294 static const AVClass av1_metadata_class = {
295  .class_name = "av1_metadata_bsf",
296  .item_name = av_default_item_name,
297  .option = av1_metadata_options,
298  .version = LIBAVUTIL_VERSION_INT,
299 };
300 
301 static const enum AVCodecID av1_metadata_codec_ids[] = {
303 };
304 
306  .name = "av1_metadata",
307  .priv_data_size = sizeof(AV1MetadataContext),
308  .priv_class = &av1_metadata_class,
310  .close = &av1_metadata_close,
313 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5797
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint32_t time_scale
Definition: cbs_av1.h:60
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:345
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t mono_chrome
Definition: cbs_av1.h:44
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
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:740
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5763
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:666
color_range
uint8_t timing_info_present_flag
Definition: cbs_av1.h:78
uint32_t num_units_in_display_tick
Definition: cbs_av1.h:59
static AVPacket pkt
uint8_t color_range
Definition: cbs_av1.h:51
static const AVClass av1_metadata_class
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5784
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:497
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
uint8_t matrix_coefficients
Definition: cbs_av1.h:49
AVOptions.
static enum AVCodecID av1_metadata_codec_ids[]
const AVBitStreamFilter ff_av1_metadata_bsf
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:239
const char * name
Definition: avcodec.h:5813
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
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
#define av_log(a,...)
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:154
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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
CodedBitstreamContext * cbc
union AV1RawOBU::@48 obu
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:445
#define fail()
Definition: checkasm.h:120
#define FLAGS
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
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:320
static const AVOption av1_metadata_options[]
AV1RawOBUHeader header
Definition: cbs_av1.h:388
AVFormatContext * ctx
Definition: movenc.c:48
static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
uint8_t color_primaries
Definition: cbs_av1.h:47
static int av1_metadata_update_sequence_header(AVBSFContext *bsf, AV1RawSequenceHeader *seq)
static int av1_metadata_init(AVBSFContext *bsf)
uint8_t subsampling_y
Definition: cbs_av1.h:53
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t subsampling_x
Definition: cbs_av1.h:52
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
Describe the class of an AVClass context structure.
Definition: log.h:67
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:139
Context structure for coded bitstream operations.
Definition: cbs.h:168
Rational number (pair of numerator and denominator).
Definition: rational.h:58
uint8_t color_description_present_flag
Definition: cbs_av1.h:46
#define OFFSET(x)
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
uint8_t obu_type
Definition: cbs_av1.h:31
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:221
uint32_t num_ticks_per_picture_minus_1
Definition: cbs_av1.h:63
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:393
uint8_t equal_picture_interval
Definition: cbs_av1.h:62
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:481
common internal and external API header
static enum AVCodecID codec_ids[]
int den
Denominator.
Definition: rational.h:60
CodedBitstreamFragment access_unit
static void av1_metadata_close(AVBSFContext *bsf)
uint8_t chroma_sample_position
Definition: cbs_av1.h:54
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5791
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:238
uint8_t transfer_characteristics
Definition: cbs_av1.h:48