FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <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 
47 };
48 
57 };
58 
59 static const char *const var_names[] = {
60  "VALUE1",
61  "VALUE2",
62  NULL
63 };
64 
65 enum var_name {
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;
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);
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 
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;
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 
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);
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 
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,
369  .inputs = ainputs,
370  .outputs = aoutputs,
372 };
373 #endif /* CONFIG_AMETADATA_FILTER */
374 
375 #if CONFIG_METADATA_FILTER
376 
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,
407 };
408 #endif /* CONFIG_METADATA_FILTER */
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
static int starts_with(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:119
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:154
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
static const char *const var_names[]
Definition: f_metadata.c:59
static av_cold int init(AVFilterContext *ctx)
Definition: f_metadata.c:188
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
int(* compare)(struct MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:86
#define DEFINE_OPTIONS(filt_name, FLAGS)
Definition: f_metadata.c:92
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:658
#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:123
const char * name
Pad name.
Definition: internal.h:59
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
#define av_cold
Definition: attributes.h:82
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
Definition: eval.c:149
static AVFrame * frame
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:39
#define av_log(a,...)
char * expr_str
Definition: f_metadata.c:79
A filter pad used for either input or output.
Definition: internal.h:53
static int same_str(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:114
double var_values[VAR_VARS_NB]
Definition: f_metadata.c:81
char * file_str
Definition: f_metadata.c:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void print_file(AVFilterContext *ctx, const char *msg,...)
Definition: f_metadata.c:177
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
Definition: opt.h:292
static void print_log(AVFilterContext *ctx, const char *msg,...)
Definition: f_metadata.c:167
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:320
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_printf_format(fmtpos, attrpos)
Definition: attributes.h:156
var_name
Definition: aeval.c:46
common internal API header
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_metadata.c:261
AVFormatContext * ctx
Definition: movenc.c:48
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_metadata.c:270
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
static int greater(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:144
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
MetadataFunction
Definition: f_metadata.c:49
void * buf
Definition: avisynth_c.h:553
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:69
AVDictionary * av_frame_get_metadata(const AVFrame *frame)
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:142
int ff_query_formats_all(AVFilterContext *ctx)
Set the formats list to all existing formats.
Definition: formats.c:602
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:379
const char * name
Filter name.
Definition: avfilter.h:146
static int equal(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:124
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
static int flags
Definition: cpu.c:47
MetadataMode
Definition: f_metadata.c:40
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:105
static int less(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:134
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
char * key
Definition: dict.h:86
AVExpr * expr
Definition: f_metadata.c:80
char * value
Definition: dict.h:87
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:713
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:339
An instance of a filter.
Definition: avfilter.h:305
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
internal API functions
void(* print)(AVFilterContext *ctx, const char *msg,...) av_printf_format(2
Definition: f_metadata.c:88
simple arithmetic expression evaluator