summaryrefslogtreecommitdiff
path: root/libavfilter/f_metadata.c (plain)
blob: 1f613ecb56f36dd7b7a797d9de21c7d6868acca2
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 "libavformat/avio.h"
35#include "avfilter.h"
36#include "audio.h"
37#include "formats.h"
38#include "internal.h"
39#include "video.h"
40
41enum MetadataMode {
42 METADATA_SELECT,
43 METADATA_ADD,
44 METADATA_MODIFY,
45 METADATA_DELETE,
46 METADATA_PRINT,
47 METADATA_NB
48};
49
50enum MetadataFunction {
51 METADATAF_SAME_STR,
52 METADATAF_STARTS_WITH,
53 METADATAF_LESS,
54 METADATAF_EQUAL,
55 METADATAF_GREATER,
56 METADATAF_EXPR,
57 METADATAF_NB
58};
59
60static const char *const var_names[] = {
61 "VALUE1",
62 "VALUE2",
63 NULL
64};
65
66enum var_name {
67 VAR_VALUE1,
68 VAR_VALUE2,
69 VAR_VARS_NB
70};
71
72typedef struct MetadataContext {
73 const AVClass *class;
74
75 int mode;
76 char *key;
77 char *value;
78 int function;
79
80 char *expr_str;
81 AVExpr *expr;
82 double var_values[VAR_VARS_NB];
83
84 AVIOContext* avio_context;
85 char *file_str;
86
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);
90} MetadataContext;
91
92#define OFFSET(x) offsetof(MetadataContext, x)
93#define DEFINE_OPTIONS(filt_name, FLAGS) \
94static 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 }, \
112 { NULL } \
113}
114
115static int same_str(MetadataContext *s, const char *value1, const char *value2)
116{
117 return !strcmp(value1, value2);
118}
119
120static int starts_with(MetadataContext *s, const char *value1, const char *value2)
121{
122 return !strncmp(value1, value2, strlen(value2));
123}
124
125static int equal(MetadataContext *s, const char *value1, const char *value2)
126{
127 float f1, f2;
128
129 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
130 return 0;
131
132 return fabsf(f1 - f2) < FLT_EPSILON;
133}
134
135static int less(MetadataContext *s, const char *value1, const char *value2)
136{
137 float f1, f2;
138
139 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
140 return 0;
141
142 return (f1 - f2) < FLT_EPSILON;
143}
144
145static int greater(MetadataContext *s, const char *value1, const char *value2)
146{
147 float f1, f2;
148
149 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
150 return 0;
151
152 return (f2 - f1) < FLT_EPSILON;
153}
154
155static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
156{
157 double f1, f2;
158
159 if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
160 return 0;
161
162 s->var_values[VAR_VALUE1] = f1;
163 s->var_values[VAR_VALUE2] = f2;
164
165 return av_expr_eval(s->expr, s->var_values, NULL);
166}
167
168static void print_log(AVFilterContext *ctx, const char *msg, ...)
169{
170 va_list argument_list;
171
172 va_start(argument_list, msg);
173 if (msg)
174 av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
175 va_end(argument_list);
176}
177
178static void print_file(AVFilterContext *ctx, const char *msg, ...)
179{
180 MetadataContext *s = ctx->priv;
181 va_list argument_list;
182
183 va_start(argument_list, msg);
184 if (msg) {
185 char buf[128];
186 vsnprintf(buf, sizeof(buf), msg, argument_list);
187 avio_write(s->avio_context, buf, av_strnlen(buf, sizeof(buf)));
188 }
189 va_end(argument_list);
190}
191
192static av_cold int init(AVFilterContext *ctx)
193{
194 MetadataContext *s = ctx->priv;
195 int ret;
196
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);
200 }
201
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);
206 }
207
208 switch (s->function) {
209 case METADATAF_SAME_STR:
210 s->compare = same_str;
211 break;
212 case METADATAF_STARTS_WITH:
213 s->compare = starts_with;
214 break;
215 case METADATAF_LESS:
216 s->compare = less;
217 break;
218 case METADATAF_EQUAL:
219 s->compare = equal;
220 break;
221 case METADATAF_GREATER:
222 s->compare = greater;
223 break;
224 case METADATAF_EXPR:
225 s->compare = parse_expr;
226 break;
227 default:
228 av_assert0(0);
229 };
230
231 if (s->function == METADATAF_EXPR) {
232 if (!s->expr_str) {
233 av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
234 return AVERROR(EINVAL);
235 }
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);
239 return ret;
240 }
241 }
242
243 if (s->mode == METADATA_PRINT && s->file_str) {
244 s->print = print_file;
245 } else {
246 s->print = print_log;
247 }
248
249 s->avio_context = NULL;
250 if (s->file_str) {
251 if (!strcmp("-", s->file_str)) {
252 ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
253 } else {
254 ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
255 }
256
257 if (ret < 0) {
258 char buf[128];
259 av_strerror(ret, buf, sizeof(buf));
260 av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
261 s->file_str, buf);
262 return ret;
263 }
264 }
265
266 return 0;
267}
268
269static av_cold void uninit(AVFilterContext *ctx)
270{
271 MetadataContext *s = ctx->priv;
272
273 if (s->avio_context) {
274 avio_closep(&s->avio_context);
275 }
276}
277
278static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
279{
280 AVFilterContext *ctx = inlink->dst;
281 AVFilterLink *outlink = ctx->outputs[0];
282 MetadataContext *s = ctx->priv;
283 AVDictionary **metadata = avpriv_frame_get_metadatap(frame);
284 AVDictionaryEntry *e;
285
286 if (!*metadata)
287 return ff_filter_frame(outlink, frame);
288
289 e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
290 !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
291
292 switch (s->mode) {
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);
299 }
300 break;
301 case METADATA_ADD:
302 if (e && e->value) {
303 ;
304 } else {
305 av_dict_set(metadata, s->key, s->value, 0);
306 }
307 return ff_filter_frame(outlink, frame);
308 break;
309 case METADATA_MODIFY:
310 if (e && e->value) {
311 av_dict_set(metadata, s->key, s->value, 0);
312 }
313 return ff_filter_frame(outlink, frame);
314 break;
315 case METADATA_PRINT:
316 if (!s->key && e) {
317 s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%-7s\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);
322 }
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:%-7s\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);
327 }
328 return ff_filter_frame(outlink, frame);
329 break;
330 case METADATA_DELETE:
331 if (!s->key) {
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);
335 }
336 return ff_filter_frame(outlink, frame);
337 break;
338 default:
339 av_assert0(0);
340 };
341
342 av_frame_free(&frame);
343
344 return 0;
345}
346
347#if CONFIG_AMETADATA_FILTER
348
349DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
350AVFILTER_DEFINE_CLASS(ametadata);
351
352static const AVFilterPad ainputs[] = {
353 {
354 .name = "default",
355 .type = AVMEDIA_TYPE_AUDIO,
356 .filter_frame = filter_frame,
357 },
358 { NULL }
359};
360
361static const AVFilterPad aoutputs[] = {
362 {
363 .name = "default",
364 .type = AVMEDIA_TYPE_AUDIO,
365 },
366 { NULL }
367};
368
369AVFilter ff_af_ametadata = {
370 .name = "ametadata",
371 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
372 .priv_size = sizeof(MetadataContext),
373 .priv_class = &ametadata_class,
374 .init = init,
375 .uninit = uninit,
376 .inputs = ainputs,
377 .outputs = aoutputs,
378 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
379};
380#endif /* CONFIG_AMETADATA_FILTER */
381
382#if CONFIG_METADATA_FILTER
383
384DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
385AVFILTER_DEFINE_CLASS(metadata);
386
387static const AVFilterPad inputs[] = {
388 {
389 .name = "default",
390 .type = AVMEDIA_TYPE_VIDEO,
391 .filter_frame = filter_frame,
392 },
393 { NULL }
394};
395
396static const AVFilterPad outputs[] = {
397 {
398 .name = "default",
399 .type = AVMEDIA_TYPE_VIDEO,
400 },
401 { NULL }
402};
403
404AVFilter ff_vf_metadata = {
405 .name = "metadata",
406 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
407 .priv_size = sizeof(MetadataContext),
408 .priv_class = &metadata_class,
409 .init = init,
410 .uninit = uninit,
411 .inputs = inputs,
412 .outputs = outputs,
413 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
414};
415#endif /* CONFIG_METADATA_FILTER */
416