FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
50 
51 
54 {
56  AV1RawColorConfig *clc = &seq->color_config;
57  AV1RawTimingInfo *tim = &seq->timing_info;
58 
59  if (ctx->color_primaries >= 0 ||
60  ctx->transfer_characteristics >= 0 ||
61  ctx->matrix_coefficients >= 0) {
67  }
68 
69  if (ctx->color_primaries >= 0)
70  clc->color_primaries = ctx->color_primaries;
71  if (ctx->transfer_characteristics >= 0)
73  if (ctx->matrix_coefficients >= 0)
75  }
76 
77  if (ctx->color_range >= 0) {
78  if (clc->color_primaries == AVCOL_PRI_BT709 &&
81  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
82  "on RGB streams encoded in BT.709 sRGB.\n");
83  } else {
84  clc->color_range = ctx->color_range;
85  }
86  }
87 
88  if (ctx->chroma_sample_position >= 0) {
89  if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
90  av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
91  "can only be set for 4:2:0 streams.\n");
92  } else {
94  }
95  }
96 
97  if (ctx->tick_rate.num && ctx->tick_rate.den) {
98  int num, den;
99 
100  av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
101  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
102 
103  tim->time_scale = num;
104  tim->num_units_in_display_tick = den;
105  seq->timing_info_present_flag = 1;
106 
107  if (ctx->num_ticks_per_picture > 0) {
108  tim->equal_picture_interval = 1;
110  ctx->num_ticks_per_picture - 1;
111  }
112  }
113 
114  return 0;
115 }
116 
118 {
120  AVPacket *in = NULL;
121  CodedBitstreamFragment *frag = &ctx->access_unit;
122  AV1RawOBU td, *obu;
123  int err, i;
124 
125  err = ff_bsf_get_packet(bsf, &in);
126  if (err < 0)
127  return err;
128 
129  err = ff_cbs_read_packet(ctx->cbc, frag, in);
130  if (err < 0) {
131  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
132  goto fail;
133  }
134 
135  for (i = 0; i < frag->nb_units; i++) {
136  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
137  obu = frag->units[i].content;
139  if (err < 0)
140  goto fail;
141  }
142  }
143 
144  // If a Temporal Delimiter is present, it must be the first OBU.
145  if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
146  if (ctx->td == REMOVE)
147  ff_cbs_delete_unit(ctx->cbc, frag, 0);
148  } else if (ctx->td == INSERT) {
149  td = (AV1RawOBU) {
151  };
152 
154  &td, NULL);
155  if (err < 0) {
156  av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
157  goto fail;
158  }
159  }
160 
161  err = ff_cbs_write_packet(ctx->cbc, out, frag);
162  if (err < 0) {
163  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
164  goto fail;
165  }
166 
167  err = av_packet_copy_props(out, in);
168  if (err < 0)
169  goto fail;
170 
171  err = 0;
172 fail:
173  ff_cbs_fragment_uninit(ctx->cbc, frag);
174 
175  if (err < 0)
176  av_packet_unref(out);
177  av_packet_free(&in);
178 
179  return err;
180 }
181 
183 {
185  CodedBitstreamFragment *frag = &ctx->access_unit;
186  AV1RawOBU *obu;
187  int err, i;
188 
189  err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
190  if (err < 0)
191  return err;
192 
193  if (bsf->par_in->extradata) {
194  err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
195  if (err < 0) {
196  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
197  goto fail;
198  }
199 
200  for (i = 0; i < frag->nb_units; i++) {
201  if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
202  obu = frag->units[i].content;
204  if (err < 0)
205  goto fail;
206  }
207  }
208 
209  err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
210  if (err < 0) {
211  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
212  goto fail;
213  }
214  }
215 
216  err = 0;
217 fail:
218  ff_cbs_fragment_uninit(ctx->cbc, frag);
219  return err;
220 }
221 
223 {
225  ff_cbs_close(&ctx->cbc);
226 }
227 
228 #define OFFSET(x) offsetof(AV1MetadataContext, x)
229 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
230 static const AVOption av1_metadata_options[] = {
231  { "td", "Temporal Delimiter OBU",
233  { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
234  { "pass", NULL, 0, AV_OPT_TYPE_CONST,
235  { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
236  { "insert", NULL, 0, AV_OPT_TYPE_CONST,
237  { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
238  { "remove", NULL, 0, AV_OPT_TYPE_CONST,
239  { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
240 
241  { "color_primaries", "Set color primaries (section 6.4.2)",
243  { .i64 = -1 }, -1, 255, FLAGS },
244  { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
246  { .i64 = -1 }, -1, 255, FLAGS },
247  { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
248  OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
249  { .i64 = -1 }, -1, 255, FLAGS },
250 
251  { "color_range", "Set color range flag (section 6.4.2)",
253  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
254  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
255  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
256  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
257  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
258 
259  { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
260  OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
261  { .i64 = -1 }, -1, 3, FLAGS, "csp" },
262  { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
263  { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
264  { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
265  { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
266  { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
267  { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
268 
269  { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
270  OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
271  { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
272  { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
273  OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
274  { .i64 = -1 }, -1, INT_MAX, FLAGS },
275 
276  { NULL }
277 };
278 
279 static const AVClass av1_metadata_class = {
280  .class_name = "av1_metadata_bsf",
281  .item_name = av_default_item_name,
282  .option = av1_metadata_options,
283  .version = LIBAVUTIL_VERSION_INT,
284 };
285 
286 static const enum AVCodecID av1_metadata_codec_ids[] = {
288 };
289 
291  .name = "av1_metadata",
292  .priv_data_size = sizeof(AV1MetadataContext),
293  .priv_class = &av1_metadata_class,
295  .close = &av1_metadata_close,
298 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5737
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:343
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: avcodec.h:5703
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:570
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
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:5724
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:487
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
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:153
uint8_t matrix_coefficients
Definition: cbs_av1.h:49
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
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:233
const char * name
Definition: avcodec.h:5753
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.
Definition: cbs.h:153
#define av_log(a,...)
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
#define td
Definition: regdef.h:70
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
CodedBitstreamContext * cbc
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:435
#define fail()
Definition: checkasm.h:117
#define FLAGS
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:564
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:318
static const AVOption av1_metadata_options[]
AV1RawOBUHeader header
Definition: cbs_av1.h:369
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
AVFormatContext * ctx
Definition: movenc.c:48
int 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:644
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:598
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:159
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:213
uint32_t num_ticks_per_picture_minus_1
Definition: cbs_av1.h:63
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:374
uint8_t equal_picture_interval
Definition: cbs_av1.h:62
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:471
common internal and external API header
static enum AVCodecID codec_ids[]
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:216
int den
Denominator.
Definition: rational.h:60
CodedBitstreamFragment access_unit
static void av1_metadata_close(AVBSFContext *bsf)
static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out)
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:3914
FILE * out
Definition: movenc.c:54
union AV1RawOBU::@47 obu
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
This structure stores compressed data.
Definition: avcodec.h:1422
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5731
uint8_t transfer_characteristics
Definition: cbs_av1.h:48