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"
33 #include "libavutil/timestamp.h"
34 #include "libavformat/avio.h"
50 enum MetadataFunction {
52 METADATAF_STARTS_WITH,
60 static const char *const var_names[] = {
72 typedef struct MetadataContext {
82 double var_values[VAR_VARS_NB];
84 AVIOContext* avio_context;
87 int (*compare)(struct MetadataContext *s,
88 const char *value1, const char *value2);
89 void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
92 #define OFFSET(x) offsetof(MetadataContext, x)
93 #define DEFINE_OPTIONS(filt_name, FLAGS) \
94 static const AVOption filt_name##_options[] = { \
95 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, "mode" }, \
96 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_SELECT }, 0, 0, FLAGS, "mode" }, \
97 { "add", "add new metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_ADD }, 0, 0, FLAGS, "mode" }, \
98 { "modify", "modify metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, "mode" }, \
99 { "delete", "delete metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_DELETE }, 0, 0, FLAGS, "mode" }, \
100 { "print", "print metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_PRINT }, 0, 0, FLAGS, "mode" }, \
101 { "key", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
102 { "value", "set metadata value", OFFSET(value), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
103 { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, "function" }, \
104 { "same_str", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_SAME_STR }, 0, 3, FLAGS, "function" }, \
105 { "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, "function" }, \
106 { "less", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS }, 0, 3, FLAGS, "function" }, \
107 { "equal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL }, 0, 3, FLAGS, "function" }, \
108 { "greater", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER }, 0, 3, FLAGS, "function" }, \
109 { "expr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR }, 0, 3, FLAGS, "function" }, \
110 { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
111 { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
115 static int same_str(MetadataContext *s, const char *value1, const char *value2)
117 return !strcmp(value1, value2);
120 static int starts_with(MetadataContext *s, const char *value1, const char *value2)
122 return !strncmp(value1, value2, strlen(value2));
125 static int equal(MetadataContext *s, const char *value1, const char *value2)
129 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
132 return fabsf(f1 - f2) < FLT_EPSILON;
135 static int less(MetadataContext *s, const char *value1, const char *value2)
139 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
142 return (f1 - f2) < FLT_EPSILON;
145 static int greater(MetadataContext *s, const char *value1, const char *value2)
149 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
152 return (f2 - f1) < FLT_EPSILON;
155 static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
159 if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
162 s->var_values[VAR_VALUE1] = f1;
163 s->var_values[VAR_VALUE2] = f2;
165 return av_expr_eval(s->expr, s->var_values, NULL);
168 static void print_log(AVFilterContext *ctx, const char *msg, ...)
170 va_list argument_list;
172 va_start(argument_list, msg);
174 av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
175 va_end(argument_list);
178 static void print_file(AVFilterContext *ctx, const char *msg, ...)
180 MetadataContext *s = ctx->priv;
181 va_list argument_list;
183 va_start(argument_list, msg);
186 vsnprintf(buf, sizeof(buf), msg, argument_list);
187 avio_write(s->avio_context, buf, av_strnlen(buf, sizeof(buf)));
189 va_end(argument_list);
192 static av_cold int init(AVFilterContext *ctx)
194 MetadataContext *s = ctx->priv;
197 if (!s->key && s->mode != METADATA_PRINT && s->mode != METADATA_DELETE) {
198 av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
199 return AVERROR(EINVAL);
202 if ((s->mode == METADATA_MODIFY ||
203 s->mode == METADATA_ADD) && !s->value) {
204 av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
205 return AVERROR(EINVAL);
208 switch (s->function) {
209 case METADATAF_SAME_STR:
210 s->compare = same_str;
212 case METADATAF_STARTS_WITH:
213 s->compare = starts_with;
218 case METADATAF_EQUAL:
221 case METADATAF_GREATER:
222 s->compare = greater;
225 s->compare = parse_expr;
231 if (s->function == METADATAF_EXPR) {
233 av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
234 return AVERROR(EINVAL);
236 if ((ret = av_expr_parse(&s->expr, s->expr_str,
237 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
238 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
243 if (s->mode == METADATA_PRINT && s->file_str) {
244 s->print = print_file;
246 s->print = print_log;
249 s->avio_context = NULL;
251 if (!strcmp("-", s->file_str)) {
252 ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
254 ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
259 av_strerror(ret, buf, sizeof(buf));
260 av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
269 static av_cold void uninit(AVFilterContext *ctx)
271 MetadataContext *s = ctx->priv;
273 if (s->avio_context) {
274 avio_closep(&s->avio_context);
278 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
280 AVFilterContext *ctx = inlink->dst;
281 AVFilterLink *outlink = ctx->outputs[0];
282 MetadataContext *s = ctx->priv;
283 AVDictionary **metadata = &frame->metadata;
284 AVDictionaryEntry *e;
287 return ff_filter_frame(outlink, frame);
289 e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
290 !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
293 case METADATA_SELECT:
294 if (!s->value && e && e->value) {
295 return ff_filter_frame(outlink, frame);
296 } else if (s->value && e && e->value &&
297 s->compare(s, e->value, s->value)) {
298 return ff_filter_frame(outlink, frame);
305 av_dict_set(metadata, s->key, s->value, 0);
307 return ff_filter_frame(outlink, frame);
309 case METADATA_MODIFY:
311 av_dict_set(metadata, s->key, s->value, 0);
313 return ff_filter_frame(outlink, frame);
317 s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
318 inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
319 s->print(ctx, "%s=%s\n", e->key, e->value);
320 while ((e = av_dict_get(*metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
321 s->print(ctx, "%s=%s\n", e->key, e->value);
323 } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
324 s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
325 inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
326 s->print(ctx, "%s=%s\n", s->key, e->value);
328 return ff_filter_frame(outlink, frame);
330 case METADATA_DELETE:
332 av_dict_free(metadata);
333 } else if (e && e->value && (!s->value || s->compare(s, e->value, s->value))) {
334 av_dict_set(metadata, s->key, NULL, 0);
336 return ff_filter_frame(outlink, frame);
342 av_frame_free(&frame);
347 #if CONFIG_AMETADATA_FILTER
349 DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
350 AVFILTER_DEFINE_CLASS(ametadata);
352 static const AVFilterPad ainputs[] = {
355 .type = AVMEDIA_TYPE_AUDIO,
356 .filter_frame = filter_frame,
361 static const AVFilterPad aoutputs[] = {
364 .type = AVMEDIA_TYPE_AUDIO,
369 AVFilter ff_af_ametadata = {
371 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
372 .priv_size = sizeof(MetadataContext),
373 .priv_class = &ametadata_class,
378 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
380 #endif /* CONFIG_AMETADATA_FILTER */
382 #if CONFIG_METADATA_FILTER
384 DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
385 AVFILTER_DEFINE_CLASS(metadata);
387 static const AVFilterPad inputs[] = {
390 .type = AVMEDIA_TYPE_VIDEO,
391 .filter_frame = filter_frame,
396 static const AVFilterPad outputs[] = {
399 .type = AVMEDIA_TYPE_VIDEO,
404 AVFilter ff_vf_metadata = {
406 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
407 .priv_size = sizeof(MetadataContext),
408 .priv_class = &metadata_class,
413 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
415 #endif /* CONFIG_METADATA_FILTER */