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