]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_metadata.c
avfilter/vf_drawbox: add alpha pixel formats support
[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 "avfilter.h"
34 #include "audio.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38
39 enum MetadataMode {
40     METADATA_SELECT,
41     METADATA_ADD,
42     METADATA_MODIFY,
43     METADATA_DELETE,
44     METADATA_PRINT,
45     METADATA_NB
46 };
47
48 enum MetadataFunction {
49     METADATAF_SAME_STR,
50     METADATAF_STARTS_WITH,
51     METADATAF_LESS,
52     METADATAF_EQUAL,
53     METADATAF_GREATER,
54     METADATAF_EXPR,
55     METADATAF_NB
56 };
57
58 static const char *const var_names[] = {
59     "VALUE1",
60     "VALUE2",
61     NULL
62 };
63
64 enum var_name {
65     VAR_VALUE1,
66     VAR_VALUE2,
67     VAR_VARS_NB
68 };
69
70 typedef struct MetadataContext {
71     const AVClass *class;
72
73     int mode;
74     char *key;
75     char *value;
76     int function;
77
78     char *expr_str;
79     AVExpr *expr;
80     double var_values[VAR_VARS_NB];
81
82     FILE *file;
83     char *file_str;
84
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);
88 } MetadataContext;
89
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 }, \
110     { NULL } \
111 }
112
113 static int same_str(MetadataContext *s, const char *value1, const char *value2)
114 {
115     return !strcmp(value1, value2);
116 }
117
118 static int starts_with(MetadataContext *s, const char *value1, const char *value2)
119 {
120     return !strncmp(value1, value2, strlen(value2));
121 }
122
123 static int equal(MetadataContext *s, const char *value1, const char *value2)
124 {
125     float f1, f2;
126
127     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
128         return 0;
129
130     return fabsf(f1 - f2) < FLT_EPSILON;
131 }
132
133 static int less(MetadataContext *s, const char *value1, const char *value2)
134 {
135     float f1, f2;
136
137     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
138         return 0;
139
140     return (f1 - f2) < FLT_EPSILON;
141 }
142
143 static int greater(MetadataContext *s, const char *value1, const char *value2)
144 {
145     float f1, f2;
146
147     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
148         return 0;
149
150     return (f2 - f1) < FLT_EPSILON;
151 }
152
153 static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
154 {
155     double f1, f2;
156
157     if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
158         return 0;
159
160     s->var_values[VAR_VALUE1] = f1;
161     s->var_values[VAR_VALUE2] = f2;
162
163     return av_expr_eval(s->expr, s->var_values, NULL);
164 }
165
166 static void print_log(AVFilterContext *ctx, const char *msg, ...)
167 {
168     va_list argument_list;
169
170     va_start(argument_list, msg);
171     if (msg)
172         av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
173     va_end(argument_list);
174 }
175
176 static void print_file(AVFilterContext *ctx, const char *msg, ...)
177 {
178     MetadataContext *s = ctx->priv;
179     va_list argument_list;
180
181     va_start(argument_list, msg);
182     if (msg)
183         vfprintf(s->file, msg, argument_list);
184     va_end(argument_list);
185 }
186
187 static av_cold int init(AVFilterContext *ctx)
188 {
189     MetadataContext *s = ctx->priv;
190     int ret;
191
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);
195     }
196
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);
201     }
202
203     switch (s->function) {
204     case METADATAF_SAME_STR:
205         s->compare = same_str;
206         break;
207     case METADATAF_STARTS_WITH:
208         s->compare = starts_with;
209         break;
210     case METADATAF_LESS:
211         s->compare = less;
212         break;
213     case METADATAF_EQUAL:
214         s->compare = equal;
215         break;
216     case METADATAF_GREATER:
217         s->compare = greater;
218         break;
219     case METADATAF_EXPR:
220         s->compare = parse_expr;
221         break;
222     default:
223         av_assert0(0);
224     };
225
226     if (s->function == METADATAF_EXPR) {
227         if (!s->expr_str) {
228             av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
229             return AVERROR(EINVAL);
230         }
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);
234             return ret;
235         }
236     }
237
238     if (s->file_str) {
239         if (!strcmp(s->file_str, "-")) {
240             s->file = stdout;
241         } else {
242             s->file = fopen(s->file_str, "w");
243             if (!s->file) {
244                 int err = AVERROR(errno);
245                 char buf[128];
246                 av_strerror(err, buf, sizeof(buf));
247                 av_log(ctx, AV_LOG_ERROR, "Could not open file %s: %s\n",
248                        s->file_str, buf);
249                 return err;
250             }
251         }
252         s->print = print_file;
253     } else {
254         s->print = print_log;
255     }
256
257     return 0;
258 }
259
260 static av_cold void uninit(AVFilterContext *ctx)
261 {
262     MetadataContext *s = ctx->priv;
263
264     if (s->file && s->file != stdout)
265         fclose(s->file);
266     s->file = NULL;
267 }
268
269 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
270 {
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;
276
277     if (!metadata)
278         return ff_filter_frame(outlink, frame);
279
280     e = av_dict_get(metadata, !s->key ? "" : s->key, NULL,
281                     !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
282
283     switch (s->mode) {
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);
290         }
291         break;
292     case METADATA_ADD:
293         if (e && e->value) {
294             ;
295         } else {
296             av_dict_set(&metadata, s->key, s->value, 0);
297         }
298         return ff_filter_frame(outlink, frame);
299         break;
300     case METADATA_MODIFY:
301         if (e && e->value) {
302             av_dict_set(&metadata, s->key, s->value, 0);
303         }
304         return ff_filter_frame(outlink, frame);
305         break;
306     case METADATA_PRINT:
307         if (!s->key && e) {
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);
312             }
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);
316         }
317         return ff_filter_frame(outlink, frame);
318         break;
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);
324         }
325         return ff_filter_frame(outlink, frame);
326         break;
327     default:
328         av_assert0(0);
329     };
330
331     av_frame_free(&frame);
332
333     return 0;
334 }
335
336 #if CONFIG_AMETADATA_FILTER
337
338 DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
339 AVFILTER_DEFINE_CLASS(ametadata);
340
341 static const AVFilterPad ainputs[] = {
342     {
343         .name         = "default",
344         .type         = AVMEDIA_TYPE_AUDIO,
345         .filter_frame = filter_frame,
346     },
347     { NULL }
348 };
349
350 static const AVFilterPad aoutputs[] = {
351     {
352         .name = "default",
353         .type = AVMEDIA_TYPE_AUDIO,
354     },
355     { NULL }
356 };
357
358 AVFilter ff_af_ametadata = {
359     .name          = "ametadata",
360     .description   = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
361     .priv_size     = sizeof(MetadataContext),
362     .priv_class    = &ametadata_class,
363     .init          = init,
364     .uninit        = uninit,
365     .query_formats = ff_query_formats_all,
366     .inputs        = ainputs,
367     .outputs       = aoutputs,
368     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
369 };
370 #endif /* CONFIG_AMETADATA_FILTER */
371
372 #if CONFIG_METADATA_FILTER
373
374 DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
375 AVFILTER_DEFINE_CLASS(metadata);
376
377 static const AVFilterPad inputs[] = {
378     {
379         .name         = "default",
380         .type         = AVMEDIA_TYPE_VIDEO,
381         .filter_frame = filter_frame,
382     },
383     { NULL }
384 };
385
386 static const AVFilterPad outputs[] = {
387     {
388         .name = "default",
389         .type = AVMEDIA_TYPE_VIDEO,
390     },
391     { NULL }
392 };
393
394 AVFilter ff_vf_metadata = {
395     .name        = "metadata",
396     .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
397     .priv_size   = sizeof(MetadataContext),
398     .priv_class  = &metadata_class,
399     .init        = init,
400     .uninit      = uninit,
401     .inputs      = inputs,
402     .outputs     = outputs,
403     .flags       = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
404 };
405 #endif /* CONFIG_METADATA_FILTER */