2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * filter for manipulating frame metadata
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/fifo.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixelutils.h"
50 enum MetadataFunction {
52 METADATAF_STARTS_WITH,
60 static const char *const var_names[] = {
72 typedef struct MetadataContext {
83 double var_values[VAR_VARS_NB];
88 int (*compare)(struct MetadataContext *s,
89 const char *value1, const char *value2, size_t length);
90 void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
93 #define OFFSET(x) offsetof(MetadataContext, x)
94 #define DEFINE_OPTIONS(filt_name, FLAGS) \
95 static const AVOption filt_name##_options[] = { \
96 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, "mode" }, \
97 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_SELECT }, 0, 0, FLAGS, "mode" }, \
98 { "add", "add new metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_ADD }, 0, 0, FLAGS, "mode" }, \
99 { "modify", "modify metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, "mode" }, \
100 { "delete", "delete metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_DELETE }, 0, 0, FLAGS, "mode" }, \
101 { "print", "print metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_PRINT }, 0, 0, FLAGS, "mode" }, \
102 { "key", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
103 { "value", "set metadata value", OFFSET(value), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
104 { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, "function" }, \
105 { "string", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STRING }, 0, 3, FLAGS, "function" }, \
106 { "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, "function" }, \
107 { "less", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS }, 0, 3, FLAGS, "function" }, \
108 { "equal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL }, 0, 3, FLAGS, "function" }, \
109 { "greater", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER }, 0, 3, FLAGS, "function" }, \
110 { "expr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR }, 0, 3, FLAGS, "function" }, \
111 { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
112 { "length", "compare up to N chars for string function", OFFSET(length), AV_OPT_TYPE_INT, {.i64 = INT_MAX }, 1, INT_MAX, FLAGS }, \
113 { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
117 static int string(MetadataContext *s, const char *value1, const char *value2, size_t length)
119 return !strncmp(value1, value2, length);
122 static int starts_with(MetadataContext *s, const char *value1, const char *value2, size_t length)
124 return !strncmp(value1, value2, strlen(value2));
127 static int equal(MetadataContext *s, const char *value1, const char *value2, size_t length)
131 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
134 return fabsf(f1 - f2) < FLT_EPSILON;
137 static int less(MetadataContext *s, const char *value1, const char *value2, size_t length)
141 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
144 return (f1 - f2) < FLT_EPSILON;
147 static int greater(MetadataContext *s, const char *value1, const char *value2, size_t length)
151 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
154 return (f2 - f1) < FLT_EPSILON;
157 static int parse_expr(MetadataContext *s, const char *value1, const char *value2, size_t length)
161 if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
164 s->var_values[VAR_VALUE1] = f1;
165 s->var_values[VAR_VALUE2] = f2;
167 return av_expr_eval(s->expr, s->var_values, NULL);
170 static void print_log(AVFilterContext *ctx, const char *msg, ...)
172 va_list argument_list;
174 va_start(argument_list, msg);
176 av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
177 va_end(argument_list);
180 static void print_file(AVFilterContext *ctx, const char *msg, ...)
182 MetadataContext *s = ctx->priv;
183 va_list argument_list;
185 va_start(argument_list, msg);
187 vfprintf(s->file, msg, argument_list);
188 va_end(argument_list);
191 static av_cold int init(AVFilterContext *ctx)
193 MetadataContext *s = ctx->priv;
196 if (!s->key && s->mode != METADATA_PRINT) {
197 av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
198 return AVERROR(EINVAL);
201 if ((s->mode == METADATA_MODIFY ||
202 s->mode == METADATA_ADD) && !s->value) {
203 av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
204 return AVERROR(EINVAL);
207 switch (s->function) {
208 case METADATAF_STRING:
211 case METADATAF_STARTS_WITH:
212 s->compare = starts_with;
217 case METADATAF_EQUAL:
220 case METADATAF_GREATER:
221 s->compare = greater;
224 s->compare = parse_expr;
230 if (s->function == METADATAF_EXPR) {
232 av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
233 return AVERROR(EINVAL);
235 if ((ret = av_expr_parse(&s->expr, s->expr_str,
236 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
237 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
243 if (!strcmp(s->file_str, "-")) {
246 s->file = fopen(s->file_str, "w");
248 int err = AVERROR(errno);
250 av_strerror(err, buf, sizeof(buf));
251 av_log(ctx, AV_LOG_ERROR, "Could not open file %s: %s\n",
256 s->print = print_file;
258 s->print = print_log;
264 static av_cold void uninit(AVFilterContext *ctx)
266 MetadataContext *s = ctx->priv;
268 if (s->file && s->file != stdout)
273 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
275 AVFilterContext *ctx = inlink->dst;
276 AVFilterLink *outlink = ctx->outputs[0];
277 MetadataContext *s = ctx->priv;
278 AVDictionary *metadata = av_frame_get_metadata(frame);
279 AVDictionaryEntry *e;
282 return ff_filter_frame(outlink, frame);
284 e = av_dict_get(metadata, !s->key ? "" : s->key, NULL,
285 !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
288 case METADATA_SELECT:
289 if (!s->value && e && e->value) {
290 return ff_filter_frame(outlink, frame);
291 } else if (s->value && e && e->value &&
292 s->compare(s, e->value, s->value, s->length)) {
293 return ff_filter_frame(outlink, frame);
300 av_dict_set(&metadata, s->key, s->value, 0);
302 return ff_filter_frame(outlink, frame);
304 case METADATA_MODIFY:
306 av_dict_set(&metadata, s->key, s->value, 0);
308 return ff_filter_frame(outlink, frame);
312 s->print(ctx, "frame %"PRId64" pts %"PRId64"\n", inlink->frame_count, frame->pts);
313 s->print(ctx, "%s=%s\n", e->key, e->value);
314 while ((e = av_dict_get(metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
315 s->print(ctx, "%s=%s\n", e->key, e->value);
317 } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value, s->length)))) {
318 s->print(ctx, "frame %"PRId64" pts %"PRId64"\n", inlink->frame_count, frame->pts);
319 s->print(ctx, "%s=%s\n", s->key, e->value);
321 return ff_filter_frame(outlink, frame);
323 case METADATA_DELETE:
324 if (e && e->value && s->value && s->compare(s, e->value, s->value, s->length)) {
325 av_dict_set(&metadata, s->key, NULL, 0);
326 } else if (e && e->value) {
327 av_dict_set(&metadata, s->key, NULL, 0);
329 return ff_filter_frame(outlink, frame);
335 av_frame_free(&frame);
340 #if CONFIG_AMETADATA_FILTER
342 DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
343 AVFILTER_DEFINE_CLASS(ametadata);
345 static const AVFilterPad ainputs[] = {
348 .type = AVMEDIA_TYPE_AUDIO,
349 .filter_frame = filter_frame,
354 static const AVFilterPad aoutputs[] = {
357 .type = AVMEDIA_TYPE_AUDIO,
362 AVFilter ff_af_ametadata = {
364 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
365 .priv_size = sizeof(MetadataContext),
366 .priv_class = &ametadata_class,
369 .query_formats = ff_query_formats_all,
372 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
374 #endif /* CONFIG_AMETADATA_FILTER */
376 #if CONFIG_METADATA_FILTER
378 DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
379 AVFILTER_DEFINE_CLASS(metadata);
381 static const AVFilterPad inputs[] = {
384 .type = AVMEDIA_TYPE_VIDEO,
385 .filter_frame = filter_frame,
390 static const AVFilterPad outputs[] = {
393 .type = AVMEDIA_TYPE_VIDEO,
398 AVFilter ff_vf_metadata = {
400 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
401 .priv_size = sizeof(MetadataContext),
402 .priv_class = &metadata_class,
407 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
409 #endif /* CONFIG_METADATA_FILTER */