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/internal.h"
32 #include "libavutil/opt.h"
48 enum MetadataFunction {
50 METADATAF_STARTS_WITH,
58 static const char *const var_names[] = {
70 typedef struct MetadataContext {
80 double var_values[VAR_VARS_NB];
85 int (*compare)(struct MetadataContext *s,
86 const char *value1, const char *value2);
87 void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
90 #define OFFSET(x) offsetof(MetadataContext, x)
91 #define DEFINE_OPTIONS(filt_name, FLAGS) \
92 static const AVOption filt_name##_options[] = { \
93 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, "mode" }, \
94 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_SELECT }, 0, 0, FLAGS, "mode" }, \
95 { "add", "add new metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_ADD }, 0, 0, FLAGS, "mode" }, \
96 { "modify", "modify metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, "mode" }, \
97 { "delete", "delete metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_DELETE }, 0, 0, FLAGS, "mode" }, \
98 { "print", "print metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_PRINT }, 0, 0, FLAGS, "mode" }, \
99 { "key", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
100 { "value", "set metadata value", OFFSET(value), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
101 { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, "function" }, \
102 { "same_str", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_SAME_STR }, 0, 3, FLAGS, "function" }, \
103 { "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, "function" }, \
104 { "less", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS }, 0, 3, FLAGS, "function" }, \
105 { "equal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL }, 0, 3, FLAGS, "function" }, \
106 { "greater", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER }, 0, 3, FLAGS, "function" }, \
107 { "expr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR }, 0, 3, FLAGS, "function" }, \
108 { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
109 { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
113 static int same_str(MetadataContext *s, const char *value1, const char *value2)
115 return !strcmp(value1, value2);
118 static int starts_with(MetadataContext *s, const char *value1, const char *value2)
120 return !strncmp(value1, value2, strlen(value2));
123 static int equal(MetadataContext *s, const char *value1, const char *value2)
127 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
130 return fabsf(f1 - f2) < FLT_EPSILON;
133 static int less(MetadataContext *s, const char *value1, const char *value2)
137 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
140 return (f1 - f2) < FLT_EPSILON;
143 static int greater(MetadataContext *s, const char *value1, const char *value2)
147 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
150 return (f2 - f1) < FLT_EPSILON;
153 static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
157 if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
160 s->var_values[VAR_VALUE1] = f1;
161 s->var_values[VAR_VALUE2] = f2;
163 return av_expr_eval(s->expr, s->var_values, NULL);
166 static void print_log(AVFilterContext *ctx, const char *msg, ...)
168 va_list argument_list;
170 va_start(argument_list, msg);
172 av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
173 va_end(argument_list);
176 static void print_file(AVFilterContext *ctx, const char *msg, ...)
178 MetadataContext *s = ctx->priv;
179 va_list argument_list;
181 va_start(argument_list, msg);
183 vfprintf(s->file, msg, argument_list);
184 va_end(argument_list);
187 static av_cold int init(AVFilterContext *ctx)
189 MetadataContext *s = ctx->priv;
192 if (!s->key && s->mode != METADATA_PRINT) {
193 av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
194 return AVERROR(EINVAL);
197 if ((s->mode == METADATA_MODIFY ||
198 s->mode == METADATA_ADD) && !s->value) {
199 av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
200 return AVERROR(EINVAL);
203 switch (s->function) {
204 case METADATAF_SAME_STR:
205 s->compare = same_str;
207 case METADATAF_STARTS_WITH:
208 s->compare = starts_with;
213 case METADATAF_EQUAL:
216 case METADATAF_GREATER:
217 s->compare = greater;
220 s->compare = parse_expr;
226 if (s->function == METADATAF_EXPR) {
228 av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
229 return AVERROR(EINVAL);
231 if ((ret = av_expr_parse(&s->expr, s->expr_str,
232 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
233 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
239 if (!strcmp(s->file_str, "-")) {
242 s->file = fopen(s->file_str, "w");
244 int err = AVERROR(errno);
246 av_strerror(err, buf, sizeof(buf));
247 av_log(ctx, AV_LOG_ERROR, "Could not open file %s: %s\n",
252 s->print = print_file;
254 s->print = print_log;
260 static av_cold void uninit(AVFilterContext *ctx)
262 MetadataContext *s = ctx->priv;
264 if (s->file && s->file != stdout)
269 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
271 AVFilterContext *ctx = inlink->dst;
272 AVFilterLink *outlink = ctx->outputs[0];
273 MetadataContext *s = ctx->priv;
274 AVDictionary *metadata = av_frame_get_metadata(frame);
275 AVDictionaryEntry *e;
278 return ff_filter_frame(outlink, frame);
280 e = av_dict_get(metadata, !s->key ? "" : s->key, NULL,
281 !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
284 case METADATA_SELECT:
285 if (!s->value && e && e->value) {
286 return ff_filter_frame(outlink, frame);
287 } else if (s->value && e && e->value &&
288 s->compare(s, e->value, s->value)) {
289 return ff_filter_frame(outlink, frame);
296 av_dict_set(&metadata, s->key, s->value, 0);
298 return ff_filter_frame(outlink, frame);
300 case METADATA_MODIFY:
302 av_dict_set(&metadata, s->key, s->value, 0);
304 return ff_filter_frame(outlink, frame);
308 s->print(ctx, "frame %"PRId64" pts %"PRId64"\n", inlink->frame_count, frame->pts);
309 s->print(ctx, "%s=%s\n", e->key, e->value);
310 while ((e = av_dict_get(metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
311 s->print(ctx, "%s=%s\n", e->key, e->value);
313 } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
314 s->print(ctx, "frame %"PRId64" pts %"PRId64"\n", inlink->frame_count, frame->pts);
315 s->print(ctx, "%s=%s\n", s->key, e->value);
317 return ff_filter_frame(outlink, frame);
319 case METADATA_DELETE:
320 if (e && e->value && s->value && s->compare(s, e->value, s->value)) {
321 av_dict_set(&metadata, s->key, NULL, 0);
322 } else if (e && e->value) {
323 av_dict_set(&metadata, s->key, NULL, 0);
325 return ff_filter_frame(outlink, frame);
331 av_frame_free(&frame);
336 #if CONFIG_AMETADATA_FILTER
338 DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
339 AVFILTER_DEFINE_CLASS(ametadata);
341 static const AVFilterPad ainputs[] = {
344 .type = AVMEDIA_TYPE_AUDIO,
345 .filter_frame = filter_frame,
350 static const AVFilterPad aoutputs[] = {
353 .type = AVMEDIA_TYPE_AUDIO,
358 AVFilter ff_af_ametadata = {
360 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
361 .priv_size = sizeof(MetadataContext),
362 .priv_class = &ametadata_class,
365 .query_formats = ff_query_formats_all,
368 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
370 #endif /* CONFIG_AMETADATA_FILTER */
372 #if CONFIG_METADATA_FILTER
374 DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
375 AVFILTER_DEFINE_CLASS(metadata);
377 static const AVFilterPad inputs[] = {
380 .type = AVMEDIA_TYPE_VIDEO,
381 .filter_frame = filter_frame,
386 static const AVFilterPad outputs[] = {
389 .type = AVMEDIA_TYPE_VIDEO,
394 AVFilter ff_vf_metadata = {
396 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
397 .priv_size = sizeof(MetadataContext),
398 .priv_class = &metadata_class,
403 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
405 #endif /* CONFIG_METADATA_FILTER */