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