]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_metadata.c
Merge commit 'c5fd4b50610f62cbb3baa4f4108139363128dea1'
[ffmpeg] / libavfilter / f_metadata.c
1 /*
2  * Copyright (c) 2016 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * filter for manipulating frame metadata
24  */
25
26 #include <float.h>
27
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 "avfilter.h"
35 #include "audio.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39
40 enum MetadataMode {
41     METADATA_SELECT,
42     METADATA_ADD,
43     METADATA_MODIFY,
44     METADATA_DELETE,
45     METADATA_PRINT,
46     METADATA_NB
47 };
48
49 enum MetadataFunction {
50     METADATAF_SAME_STR,
51     METADATAF_STARTS_WITH,
52     METADATAF_LESS,
53     METADATAF_EQUAL,
54     METADATAF_GREATER,
55     METADATAF_EXPR,
56     METADATAF_NB
57 };
58
59 static const char *const var_names[] = {
60     "VALUE1",
61     "VALUE2",
62     NULL
63 };
64
65 enum var_name {
66     VAR_VALUE1,
67     VAR_VALUE2,
68     VAR_VARS_NB
69 };
70
71 typedef struct MetadataContext {
72     const AVClass *class;
73
74     int mode;
75     char *key;
76     char *value;
77     int function;
78
79     char *expr_str;
80     AVExpr *expr;
81     double var_values[VAR_VARS_NB];
82
83     FILE *file;
84     char *file_str;
85
86     int (*compare)(struct MetadataContext *s,
87                    const char *value1, const char *value2);
88     void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
89 } MetadataContext;
90
91 #define OFFSET(x) offsetof(MetadataContext, x)
92 #define DEFINE_OPTIONS(filt_name, FLAGS) \
93 static const AVOption filt_name##_options[] = { \
94     { "mode", "set a mode of operation", OFFSET(mode),   AV_OPT_TYPE_INT,    {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, "mode" }, \
95     {   "select", "select frame",        0,              AV_OPT_TYPE_CONST,  {.i64 = METADATA_SELECT }, 0, 0, FLAGS, "mode" }, \
96     {   "add",    "add new metadata",    0,              AV_OPT_TYPE_CONST,  {.i64 = METADATA_ADD },    0, 0, FLAGS, "mode" }, \
97     {   "modify", "modify metadata",     0,              AV_OPT_TYPE_CONST,  {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, "mode" }, \
98     {   "delete", "delete metadata",     0,              AV_OPT_TYPE_CONST,  {.i64 = METADATA_DELETE }, 0, 0, FLAGS, "mode" }, \
99     {   "print",  "print metadata",      0,              AV_OPT_TYPE_CONST,  {.i64 = METADATA_PRINT },  0, 0, FLAGS, "mode" }, \
100     { "key",   "set metadata key",       OFFSET(key),    AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
101     { "value", "set metadata value",     OFFSET(value),  AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
102     { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, "function" }, \
103     {   "same_str",    NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_SAME_STR },    0, 3, FLAGS, "function" }, \
104     {   "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, "function" }, \
105     {   "less",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS    },     0, 3, FLAGS, "function" }, \
106     {   "equal",       NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL   },     0, 3, FLAGS, "function" }, \
107     {   "greater",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER },     0, 3, FLAGS, "function" }, \
108     {   "expr",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR    },     0, 3, FLAGS, "function" }, \
109     { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
110     { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
111     { NULL } \
112 }
113
114 static int same_str(MetadataContext *s, const char *value1, const char *value2)
115 {
116     return !strcmp(value1, value2);
117 }
118
119 static int starts_with(MetadataContext *s, const char *value1, const char *value2)
120 {
121     return !strncmp(value1, value2, strlen(value2));
122 }
123
124 static int equal(MetadataContext *s, const char *value1, const char *value2)
125 {
126     float f1, f2;
127
128     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
129         return 0;
130
131     return fabsf(f1 - f2) < FLT_EPSILON;
132 }
133
134 static int less(MetadataContext *s, const char *value1, const char *value2)
135 {
136     float f1, f2;
137
138     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
139         return 0;
140
141     return (f1 - f2) < FLT_EPSILON;
142 }
143
144 static int greater(MetadataContext *s, const char *value1, const char *value2)
145 {
146     float f1, f2;
147
148     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
149         return 0;
150
151     return (f2 - f1) < FLT_EPSILON;
152 }
153
154 static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
155 {
156     double f1, f2;
157
158     if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
159         return 0;
160
161     s->var_values[VAR_VALUE1] = f1;
162     s->var_values[VAR_VALUE2] = f2;
163
164     return av_expr_eval(s->expr, s->var_values, NULL);
165 }
166
167 static void print_log(AVFilterContext *ctx, const char *msg, ...)
168 {
169     va_list argument_list;
170
171     va_start(argument_list, msg);
172     if (msg)
173         av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
174     va_end(argument_list);
175 }
176
177 static void print_file(AVFilterContext *ctx, const char *msg, ...)
178 {
179     MetadataContext *s = ctx->priv;
180     va_list argument_list;
181
182     va_start(argument_list, msg);
183     if (msg)
184         vfprintf(s->file, msg, argument_list);
185     va_end(argument_list);
186 }
187
188 static av_cold int init(AVFilterContext *ctx)
189 {
190     MetadataContext *s = ctx->priv;
191     int ret;
192
193     if (!s->key && s->mode != METADATA_PRINT) {
194         av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
195         return AVERROR(EINVAL);
196     }
197
198     if ((s->mode == METADATA_MODIFY ||
199         s->mode == METADATA_ADD) && !s->value) {
200         av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
201         return AVERROR(EINVAL);
202     }
203
204     switch (s->function) {
205     case METADATAF_SAME_STR:
206         s->compare = same_str;
207         break;
208     case METADATAF_STARTS_WITH:
209         s->compare = starts_with;
210         break;
211     case METADATAF_LESS:
212         s->compare = less;
213         break;
214     case METADATAF_EQUAL:
215         s->compare = equal;
216         break;
217     case METADATAF_GREATER:
218         s->compare = greater;
219         break;
220     case METADATAF_EXPR:
221         s->compare = parse_expr;
222         break;
223     default:
224         av_assert0(0);
225     };
226
227     if (s->function == METADATAF_EXPR) {
228         if (!s->expr_str) {
229             av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
230             return AVERROR(EINVAL);
231         }
232         if ((ret = av_expr_parse(&s->expr, s->expr_str,
233                                  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
234             av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
235             return ret;
236         }
237     }
238
239     if (s->file_str) {
240         if (!strcmp(s->file_str, "-")) {
241             s->file = stdout;
242         } else {
243             s->file = fopen(s->file_str, "w");
244             if (!s->file) {
245                 int err = AVERROR(errno);
246                 char buf[128];
247                 av_strerror(err, buf, sizeof(buf));
248                 av_log(ctx, AV_LOG_ERROR, "Could not open file %s: %s\n",
249                        s->file_str, buf);
250                 return err;
251             }
252         }
253         s->print = print_file;
254     } else {
255         s->print = print_log;
256     }
257
258     return 0;
259 }
260
261 static av_cold void uninit(AVFilterContext *ctx)
262 {
263     MetadataContext *s = ctx->priv;
264
265     if (s->file && s->file != stdout)
266         fclose(s->file);
267     s->file = NULL;
268 }
269
270 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
271 {
272     AVFilterContext *ctx = inlink->dst;
273     AVFilterLink *outlink = ctx->outputs[0];
274     MetadataContext *s = ctx->priv;
275     AVDictionary *metadata = av_frame_get_metadata(frame);
276     AVDictionaryEntry *e;
277
278     if (!metadata)
279         return ff_filter_frame(outlink, frame);
280
281     e = av_dict_get(metadata, !s->key ? "" : s->key, NULL,
282                     !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
283
284     switch (s->mode) {
285     case METADATA_SELECT:
286         if (!s->value && e && e->value) {
287             return ff_filter_frame(outlink, frame);
288         } else if (s->value && e && e->value &&
289                    s->compare(s, e->value, s->value)) {
290             return ff_filter_frame(outlink, frame);
291         }
292         break;
293     case METADATA_ADD:
294         if (e && e->value) {
295             ;
296         } else {
297             av_dict_set(&metadata, s->key, s->value, 0);
298         }
299         return ff_filter_frame(outlink, frame);
300         break;
301     case METADATA_MODIFY:
302         if (e && e->value) {
303             av_dict_set(&metadata, s->key, s->value, 0);
304         }
305         return ff_filter_frame(outlink, frame);
306         break;
307     case METADATA_PRINT:
308         if (!s->key && e) {
309             s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%-7s\n",
310                      inlink->frame_count, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
311             s->print(ctx, "%s=%s\n", e->key, e->value);
312             while ((e = av_dict_get(metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
313                 s->print(ctx, "%s=%s\n", e->key, e->value);
314             }
315         } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
316             s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%-7s\n",
317                      inlink->frame_count, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
318             s->print(ctx, "%s=%s\n", s->key, e->value);
319         }
320         return ff_filter_frame(outlink, frame);
321         break;
322     case METADATA_DELETE:
323         if (e && e->value && s->value && s->compare(s, e->value, s->value)) {
324             av_dict_set(&metadata, s->key, NULL, 0);
325         } else if (e && e->value) {
326             av_dict_set(&metadata, s->key, NULL, 0);
327         }
328         return ff_filter_frame(outlink, frame);
329         break;
330     default:
331         av_assert0(0);
332     };
333
334     av_frame_free(&frame);
335
336     return 0;
337 }
338
339 #if CONFIG_AMETADATA_FILTER
340
341 DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
342 AVFILTER_DEFINE_CLASS(ametadata);
343
344 static const AVFilterPad ainputs[] = {
345     {
346         .name         = "default",
347         .type         = AVMEDIA_TYPE_AUDIO,
348         .filter_frame = filter_frame,
349     },
350     { NULL }
351 };
352
353 static const AVFilterPad aoutputs[] = {
354     {
355         .name = "default",
356         .type = AVMEDIA_TYPE_AUDIO,
357     },
358     { NULL }
359 };
360
361 AVFilter ff_af_ametadata = {
362     .name          = "ametadata",
363     .description   = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
364     .priv_size     = sizeof(MetadataContext),
365     .priv_class    = &ametadata_class,
366     .init          = init,
367     .uninit        = uninit,
368     .query_formats = ff_query_formats_all,
369     .inputs        = ainputs,
370     .outputs       = aoutputs,
371     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
372 };
373 #endif /* CONFIG_AMETADATA_FILTER */
374
375 #if CONFIG_METADATA_FILTER
376
377 DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
378 AVFILTER_DEFINE_CLASS(metadata);
379
380 static const AVFilterPad inputs[] = {
381     {
382         .name         = "default",
383         .type         = AVMEDIA_TYPE_VIDEO,
384         .filter_frame = filter_frame,
385     },
386     { NULL }
387 };
388
389 static const AVFilterPad outputs[] = {
390     {
391         .name = "default",
392         .type = AVMEDIA_TYPE_VIDEO,
393     },
394     { NULL }
395 };
396
397 AVFilter ff_vf_metadata = {
398     .name        = "metadata",
399     .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
400     .priv_size   = sizeof(MetadataContext),
401     .priv_class  = &metadata_class,
402     .init        = init,
403     .uninit      = uninit,
404     .inputs      = inputs,
405     .outputs     = outputs,
406     .flags       = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
407 };
408 #endif /* CONFIG_METADATA_FILTER */