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 "bsf_internal.h"
24 #include "cbs.h"
25 #include "cbs_av1.h"
26 
27 enum {
31 };
32 
33 typedef struct AV1MetadataContext {
34  const AVClass *class;
35 
39 
40  int td;
41 
45 
48 
51 
54 
55 
58 {
60  AV1RawColorConfig *clc = &seq->color_config;
61  AV1RawTimingInfo *tim = &seq->timing_info;
62 
63  if (ctx->color_primaries >= 0 ||
64  ctx->transfer_characteristics >= 0 ||
65  ctx->matrix_coefficients >= 0) {
67 
68  if (ctx->color_primaries >= 0)
69  clc->color_primaries = ctx->color_primaries;
70  if (ctx->transfer_characteristics >= 0)
72  if (ctx->matrix_coefficients >= 0)
74  }
75 
76  if (ctx->color_range >= 0) {
77  if (clc->color_primaries == AVCOL_PRI_BT709 &&
80  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
81  "on RGB streams encoded in BT.709 sRGB.\n");
82  } else {
83  clc->color_range = ctx->color_range;
84  }
85  }
86 
87  if (ctx->chroma_sample_position >= 0) {
88  if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
89  av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
90  "can only be set for 4:2:0 streams.\n");
91  } else {
93  }
94  }
95 
96  if (ctx->tick_rate.num && ctx->tick_rate.den) {
97  int num, den;
98 
99  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
100  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
101 
102  tim->time_scale = num;
103  tim->num_units_in_display_tick = den;
104  seq->timing_info_present_flag = 1;
105 
106  if (ctx->num_ticks_per_picture > 0) {
107  tim->equal_picture_interval = 1;
109  ctx->num_ticks_per_picture - 1;
110  }
111  }
112 
113  return 0;
114 }
115 
117 {
119  CodedBitstreamFragment *frag = &ctx->access_unit;
120  uint8_t *side_data;
121  int side_data_size;
122  int err, i;
123 
125  &side_data_size);
126  if (!side_data_size)
127  return 0;
128 
129  err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
130  if (err < 0) {
131  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
132  return err;
133  }
134 
135  for (i = 0; i < frag->nb_units; i++) {
136  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
137  AV1RawOBU *obu = frag->units[i].content;
139  if (err < 0)
140  return err;
141  }
142  }
143 
144  err = ff_cbs_write_fragment_data(ctx->output, frag);
145  if (err < 0) {
146  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
147  return err;
148  }
149 
151  if (!side_data)
152  return AVERROR(ENOMEM);
153  memcpy(side_data, frag->data, frag->data_size);
154 
155  ff_cbs_fragment_reset(frag);
156 
157  return 0;
158 }
159 
161 {
163  CodedBitstreamFragment *frag = &ctx->access_unit;
164  AV1RawOBU td, *obu;
165  int err, i;
166 
167  err = ff_bsf_get_packet_ref(bsf, pkt);
168  if (err < 0)
169  return err;
170 
171  err = av1_metadata_update_side_data(bsf, pkt);
172  if (err < 0)
173  goto fail;
174 
175  err = ff_cbs_read_packet(ctx->input, frag, pkt);
176  if (err < 0) {
177  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
178  goto fail;
179  }
180 
181  if (frag->nb_units == 0) {
182  av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
183  err = AVERROR_INVALIDDATA;
184  goto fail;
185  }
186 
187  for (i = 0; i < frag->nb_units; i++) {
188  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
189  obu = frag->units[i].content;
191  if (err < 0)
192  goto fail;
193  }
194  }
195 
196  // If a Temporal Delimiter is present, it must be the first OBU.
197  if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
198  if (ctx->td == REMOVE)
199  ff_cbs_delete_unit(frag, 0);
200  } else if (ctx->td == INSERT) {
201  td = (AV1RawOBU) {
203  };
204 
206  &td, NULL);
207  if (err < 0) {
208  av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
209  goto fail;
210  }
211  }
212 
213  if (ctx->delete_padding) {
214  for (i = frag->nb_units - 1; i >= 0; i--) {
215  if (frag->units[i].type == AV1_OBU_PADDING)
216  ff_cbs_delete_unit(frag, i);
217  }
218  }
219 
220  err = ff_cbs_write_packet(ctx->output, pkt, frag);
221  if (err < 0) {
222  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
223  goto fail;
224  }
225 
226  err = 0;
227 fail:
228  ff_cbs_fragment_reset(frag);
229 
230  if (err < 0)
231  av_packet_unref(pkt);
232 
233  return err;
234 }
235 
237 {
239  CodedBitstreamFragment *frag = &ctx->access_unit;
240  AV1RawOBU *obu;
241  int err, i;
242 
243  err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
244  if (err < 0)
245  return err;
246  err = ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
247  if (err < 0)
248  return err;
249 
250  if (bsf->par_in->extradata) {
251  err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
252  if (err < 0) {
253  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
254  goto fail;
255  }
256 
257  for (i = 0; i < frag->nb_units; i++) {
258  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
259  obu = frag->units[i].content;
261  if (err < 0)
262  goto fail;
263  }
264  }
265 
266  err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
267  if (err < 0) {
268  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
269  goto fail;
270  }
271  }
272 
273  err = 0;
274 fail:
275  ff_cbs_fragment_reset(frag);
276  return err;
277 }
278 
280 {
282 
284  ff_cbs_close(&ctx->input);
285  ff_cbs_close(&ctx->output);
286 }
287 
288 #define OFFSET(x) offsetof(AV1MetadataContext, x)
289 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
290 static const AVOption av1_metadata_options[] = {
291  { "td", "Temporal Delimiter OBU",
293  { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
294  { "pass", NULL, 0, AV_OPT_TYPE_CONST,
295  { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
296  { "insert", NULL, 0, AV_OPT_TYPE_CONST,
297  { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
298  { "remove", NULL, 0, AV_OPT_TYPE_CONST,
299  { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
300 
301  { "color_primaries", "Set color primaries (section 6.4.2)",
303  { .i64 = -1 }, -1, 255, FLAGS },
304  { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
306  { .i64 = -1 }, -1, 255, FLAGS },
307  { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
309  { .i64 = -1 }, -1, 255, FLAGS },
310 
311  { "color_range", "Set color range flag (section 6.4.2)",
313  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
314  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
315  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
316  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
317  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
318 
319  { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
321  { .i64 = -1 }, -1, 3, FLAGS, "csp" },
322  { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
323  { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
324  { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
325  { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
326  { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
327  { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
328 
329  { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
331  { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
332  { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
334  { .i64 = -1 }, -1, INT_MAX, FLAGS },
335 
336  { "delete_padding", "Delete all Padding OBUs",
338  { .i64 = 0 }, 0, 1, FLAGS},
339 
340  { NULL }
341 };
342 
343 static const AVClass av1_metadata_class = {
344  .class_name = "av1_metadata_bsf",
345  .item_name = av_default_item_name,
346  .option = av1_metadata_options,
347  .version = LIBAVUTIL_VERSION_INT,
348 };
349 
350 static const enum AVCodecID av1_metadata_codec_ids[] = {
352 };
353 
355  .name = "av1_metadata",
356  .priv_data_size = sizeof(AV1MetadataContext),
357  .priv_class = &av1_metadata_class,
359  .close = &av1_metadata_close,
362 };
#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: bsf.h:83
AVOption.
Definition: opt.h:248
CodedBitstreamContext * input
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#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:397
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
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: bsf.h:49
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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: bsf.h:70
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:513
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
uint8_t
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:238
const char * name
Definition: bsf.h:99
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
CodedBitstreamContext * output
#define av_log(a,...)
void ff_cbs_fragment_reset(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:141
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:353
static int av1_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:460
union AV1RawOBU::@25 obu
#define fail()
Definition: checkasm.h:123
#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:372
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 * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:336
uint8_t subsampling_y
Definition: cbs_av1.h:53
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
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:606
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:168
Rational number (pair of numerator and denominator).
Definition: rational.h:58
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:790
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:715
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:115
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:220
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:497
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: codec_par.h:74
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:155
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
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:265
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
This structure stores compressed data.
Definition: packet.h:340
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:252
uint8_t transfer_characteristics
Definition: cbs_av1.h:48
int i
Definition: input.c:407