FFmpeg
Loading...
Searching...
No Matches
f_metadata.c
Go to the documentation of this file.
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 "config_components.h"
27
28#include <float.h>
29
30#include "libavutil/avassert.h"
31#include "libavutil/avstring.h"
32#include "libavutil/eval.h"
33#include "libavutil/internal.h"
34#include "libavutil/opt.h"
35#include "libavutil/timestamp.h"
36#include "libavformat/avio.h"
37#include "avfilter.h"
38#include "audio.h"
39#include "filters.h"
40#include "video.h"
41
50
61
62static const char *const var_names[] = {
63 "VALUE1",
64 "VALUE2",
65 "FRAMEVAL",
66 "USERVAL",
67 NULL
68};
69
77
78typedef struct MetadataContext {
79 const AVClass *class;
80
81 int mode;
82 char *key;
83 char *value;
85
86 char *expr_str;
89
91 char *file_str;
92
93 int (*compare)(struct MetadataContext *s,
94 const char *value1, const char *value2);
95 void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
96
97 int direct; // reduces buffering when printing to user-supplied URL
99
100#define OFFSET(x) offsetof(MetadataContext, x)
101#define DEFINE_OPTIONS(filt_name, FLAGS) \
102static const AVOption filt_name##_options[] = { \
103 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, .unit = "mode" }, \
104 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_SELECT }, 0, 0, FLAGS, .unit = "mode" }, \
105 { "add", "add new metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_ADD }, 0, 0, FLAGS, .unit = "mode" }, \
106 { "modify", "modify metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, .unit = "mode" }, \
107 { "delete", "delete metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_DELETE }, 0, 0, FLAGS, .unit = "mode" }, \
108 { "print", "print metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_PRINT }, 0, 0, FLAGS, .unit = "mode" }, \
109 { "key", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
110 { "value", "set metadata value", OFFSET(value), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
111 { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, .unit = "function" }, \
112 { "same_str", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_SAME_STR }, 0, 3, FLAGS, .unit = "function" }, \
113 { "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, .unit = "function" }, \
114 { "less", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS }, 0, 3, FLAGS, .unit = "function" }, \
115 { "equal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL }, 0, 3, FLAGS, .unit = "function" }, \
116 { "greater", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER }, 0, 3, FLAGS, .unit = "function" }, \
117 { "expr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR }, 0, 3, FLAGS, .unit = "function" }, \
118 { "ends_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_ENDS_WITH }, 0, 0, FLAGS, .unit = "function" }, \
119 { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
120 { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
121 { "direct", "reduce buffering when printing to user-set file or pipe", OFFSET(direct), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS }, \
122 { NULL } \
123}
124
125static int same_str(MetadataContext *s, const char *value1, const char *value2)
126{
127 return !strcmp(value1, value2);
128}
129
130static int starts_with(MetadataContext *s, const char *value1, const char *value2)
131{
132 return !strncmp(value1, value2, strlen(value2));
133}
134
135static int ends_with(MetadataContext *s, const char *value1, const char *value2)
136{
137 const int len1 = strlen(value1);
138 const int len2 = strlen(value2);
139
140 return !strncmp(value1 + FFMAX(len1 - len2, 0), value2, len2);
141}
142
143static int equal(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 fabsf(f1 - f2) < FLT_EPSILON;
151}
152
153static int less(MetadataContext *s, const char *value1, const char *value2)
154{
155 float f1, f2;
156
157 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
158 return 0;
159
160 return (f1 - f2) < FLT_EPSILON;
161}
162
163static int greater(MetadataContext *s, const char *value1, const char *value2)
164{
165 float f1, f2;
166
167 if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
168 return 0;
169
170 return (f2 - f1) < FLT_EPSILON;
171}
172
173static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
174{
175 double f1, f2;
176
177 if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
178 return 0;
179
180 s->var_values[VAR_VALUE1] = s->var_values[VAR_FRAMEVAL] = f1;
181 s->var_values[VAR_VALUE2] = s->var_values[VAR_USERVAL] = f2;
182
183 return av_expr_eval(s->expr, s->var_values, NULL);
184}
185
186static void print_log(AVFilterContext *ctx, const char *msg, ...)
187{
188 va_list argument_list;
189
190 va_start(argument_list, msg);
191 if (msg)
192 av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
193 va_end(argument_list);
194}
195
196static void print_file(AVFilterContext *ctx, const char *msg, ...)
197{
198 MetadataContext *s = ctx->priv;
199 va_list argument_list;
200
201 va_start(argument_list, msg);
202 if (msg) {
203 char buf[128];
204 int ret = vsnprintf(buf, sizeof(buf), msg, argument_list);
205 avio_write(s->avio_context, buf, ret);
206 }
207 va_end(argument_list);
208}
209
211{
212 MetadataContext *s = ctx->priv;
213 int ret;
214
215 if (!s->key && s->mode != METADATA_PRINT && s->mode != METADATA_DELETE) {
216 av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
217 return AVERROR(EINVAL);
218 }
219
220 if ((s->mode == METADATA_MODIFY ||
221 s->mode == METADATA_ADD) && !s->value) {
222 av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
223 return AVERROR(EINVAL);
224 }
225
226 switch (s->function) {
228 s->compare = same_str;
229 break;
231 s->compare = starts_with;
232 break;
234 s->compare = ends_with;
235 break;
236 case METADATAF_LESS:
237 s->compare = less;
238 break;
239 case METADATAF_EQUAL:
240 s->compare = equal;
241 break;
243 s->compare = greater;
244 break;
245 case METADATAF_EXPR:
246 s->compare = parse_expr;
247 break;
248 default:
249 av_assert0(0);
250 };
251
252 if (s->function == METADATAF_EXPR) {
253 if (!s->expr_str) {
254 av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
255 return AVERROR(EINVAL);
256 }
257 if ((ret = av_expr_parse(&s->expr, s->expr_str,
258 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
259 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
260 return ret;
261 }
262 }
263
264 if (s->mode == METADATA_PRINT && s->file_str) {
265 s->print = print_file;
266 } else {
267 s->print = print_log;
268 }
269
270 s->avio_context = NULL;
271 if (s->file_str) {
272 if (!strcmp("-", s->file_str)) {
273 ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
274 } else {
275 ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
276 }
277
278 if (ret < 0) {
279 av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
280 s->file_str, av_err2str(ret));
281 return ret;
282 }
283
284 if (s->direct)
285 s->avio_context->direct = AVIO_FLAG_DIRECT;
286 }
287
288 return 0;
289}
290
292{
293 MetadataContext *s = ctx->priv;
294
295 av_expr_free(s->expr);
296 s->expr = NULL;
297 if (s->avio_context) {
298 avio_closep(&s->avio_context);
299 }
300}
301
303{
304 FilterLink *inl = ff_filter_link(inlink);
305 AVFilterContext *ctx = inlink->dst;
306 AVFilterLink *outlink = ctx->outputs[0];
307 MetadataContext *s = ctx->priv;
308 AVDictionary **metadata = &frame->metadata;
309 const AVDictionaryEntry *e;
310
311 e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
312 !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
313
314 switch (s->mode) {
315 case METADATA_SELECT:
316 if (!s->value && e && e->value) {
317 return ff_filter_frame(outlink, frame);
318 } else if (s->value && e && e->value &&
319 s->compare(s, e->value, s->value)) {
320 return ff_filter_frame(outlink, frame);
321 }
322 break;
323 case METADATA_ADD:
324 if (e && e->value) {
325 ;
326 } else {
327 av_dict_set(metadata, s->key, s->value, 0);
328 }
329 return ff_filter_frame(outlink, frame);
330 case METADATA_MODIFY:
331 if (e && e->value) {
332 av_dict_set(metadata, s->key, s->value, 0);
333 }
334 return ff_filter_frame(outlink, frame);
335 case METADATA_PRINT:
336 if (!s->key && e) {
337 s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
338 inl->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
339 s->print(ctx, "%s=%s\n", e->key, e->value);
340 while ((e = av_dict_iterate(*metadata, e)) != NULL) {
341 s->print(ctx, "%s=%s\n", e->key, e->value);
342 }
343 } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
344 s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
345 inl->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
346 s->print(ctx, "%s=%s\n", s->key, e->value);
347 }
348 return ff_filter_frame(outlink, frame);
349 case METADATA_DELETE:
350 if (!s->key) {
352 } else if (e && e->value && (!s->value || s->compare(s, e->value, s->value))) {
353 av_dict_set(metadata, s->key, NULL, 0);
354 }
355 return ff_filter_frame(outlink, frame);
356 default:
357 av_assert0(0);
358 };
359
361
362 return 0;
363}
364
365#if CONFIG_AMETADATA_FILTER
366
368AVFILTER_DEFINE_CLASS(ametadata);
369
370static const AVFilterPad ainputs[] = {
371 {
372 .name = "default",
373 .type = AVMEDIA_TYPE_AUDIO,
374 .filter_frame = filter_frame,
375 },
376};
377
379 .p.name = "ametadata",
380 .p.description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
381 .p.priv_class = &ametadata_class,
384 .priv_size = sizeof(MetadataContext),
385 .init = init,
386 .uninit = uninit,
387 FILTER_INPUTS(ainputs),
389};
390#endif /* CONFIG_AMETADATA_FILTER */
391
392#if CONFIG_METADATA_FILTER
393
396
397static const AVFilterPad inputs[] = {
398 {
399 .name = "default",
400 .type = AVMEDIA_TYPE_VIDEO,
401 .filter_frame = filter_frame,
402 },
403};
404
405const FFFilter ff_vf_metadata = {
406 .p.name = "metadata",
407 .p.description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
408 .p.priv_class = &metadata_class,
411 .priv_size = sizeof(MetadataContext),
412 .init = init,
413 .uninit = uninit,
416};
417#endif /* CONFIG_METADATA_FILTER */
static const AVFilterPad inputs[]
Definition af_aap.c:299
const FFFilter ff_af_ametadata
const FFFilter ff_vf_metadata
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:565
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition avio.c:717
Buffered I/O operations.
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition avio.h:644
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static int less(const void *a, const void *b)
Definition ctu.c:679
static __device__ float fabsf(float a)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition eval.c:368
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition eval.c:824
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition eval.c:735
simple arithmetic expression evaluator
static int equal(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:143
static void print_file(AVFilterContext *ctx, const char *msg,...)
Definition f_metadata.c:196
@ VAR_FRAMEVAL
Definition f_metadata.c:73
@ VAR_VALUE1
Definition f_metadata.c:71
@ VAR_VALUE2
Definition f_metadata.c:72
@ VAR_VARS_NB
Definition f_metadata.c:75
@ VAR_USERVAL
Definition f_metadata.c:74
static int less(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:153
static int greater(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:163
static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:173
static int starts_with(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:130
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition f_metadata.c:302
static int same_str(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:125
MetadataMode
Definition f_metadata.c:42
@ METADATA_ADD
Definition f_metadata.c:44
@ METADATA_PRINT
Definition f_metadata.c:47
@ METADATA_SELECT
Definition f_metadata.c:43
@ METADATA_MODIFY
Definition f_metadata.c:45
@ METADATA_NB
Definition f_metadata.c:48
@ METADATA_DELETE
Definition f_metadata.c:46
MetadataFunction
Definition f_metadata.c:51
@ METADATAF_LESS
Definition f_metadata.c:54
@ METADATAF_ENDS_WITH
Definition f_metadata.c:58
@ METADATAF_EQUAL
Definition f_metadata.c:55
@ METADATAF_SAME_STR
Definition f_metadata.c:52
@ METADATAF_EXPR
Definition f_metadata.c:57
@ METADATAF_STARTS_WITH
Definition f_metadata.c:53
@ METADATAF_NB
Definition f_metadata.c:59
@ METADATAF_GREATER
Definition f_metadata.c:56
static int ends_with(MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:135
static av_cold void uninit(AVFilterContext *ctx)
Definition f_metadata.c:291
#define DEFINE_OPTIONS(filt_name, FLAGS)
Definition f_metadata.c:101
static void print_log(AVFilterContext *ctx, const char *msg,...)
Definition f_metadata.c:186
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition opt.h:380
#define AV_OPT_FLAG_AUDIO_PARAM
Definition opt.h:356
#define AV_OPT_FLAG_VIDEO_PARAM
Definition opt.h:357
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition dict.h:75
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level.
Definition log.c:459
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_printf_format(fmtpos, attrpos)
Definition attributes.h:218
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMAX(a, b)
Definition macros.h:47
var_name
Definition noise.c:46
@ VAR_VARS_NB
Definition noise.c:59
static const char *const var_names[]
Definition noise.c:30
AVOptions.
#define vsnprintf
Definition snprintf.h:36
Describe the class of an AVClass context structure.
Definition log.h:76
char * key
Definition dict.h:91
char * value
Definition dict.h:92
Definition eval.c:171
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Bytestream IO Context.
Definition avio.h:160
void(* print)(AVFilterContext *ctx, const char *msg,...) av_printf_format(2
Definition f_metadata.c:95
void int direct
Definition f_metadata.c:97
double var_values[VAR_VARS_NB]
Definition f_metadata.c:88
int(* compare)(struct MetadataContext *s, const char *value1, const char *value2)
Definition f_metadata.c:93
AVExpr * expr
Definition f_metadata.c:87
AVIOContext * avio_context
Definition f_metadata.c:90
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:54
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:83
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37