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 "avfilter.h"
34 #include "audio.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 
46 };
47 
56 };
57 
58 static const char *const var_names[] = {
59  "VALUE1",
60  "VALUE2",
61  NULL
62 };
63 
64 enum var_name {
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;
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);
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 
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;
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 
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);
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 
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,
366  .inputs = ainputs,
367  .outputs = aoutputs,
369 };
370 #endif /* CONFIG_AMETADATA_FILTER */
371 
372 #if CONFIG_METADATA_FILTER
373 
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,
404 };
405 #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:118
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:153
AVFormatContext * ctx
Definition: movenc-test.c:48
#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:58
static av_cold int init(AVFilterContext *ctx)
Definition: f_metadata.c:187
#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:85
#define DEFINE_OPTIONS(filt_name, FLAGS)
Definition: f_metadata.c:91
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:657
#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:122
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:1163
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
Definition: eval.c:148
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:78
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:113
double var_values[VAR_VARS_NB]
Definition: f_metadata.c:80
char * file_str
Definition: f_metadata.c:83
#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:176
#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:166
#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:154
#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:319
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:260
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_metadata.c:269
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
static int greater(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:143
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
MetadataFunction
Definition: f_metadata.c:48
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:141
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:370
const char * name
Filter name.
Definition: avfilter.h:145
static int equal(MetadataContext *s, const char *value1, const char *value2)
Definition: f_metadata.c:123
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
static int flags
Definition: cpu.c:47
MetadataMode
Definition: f_metadata.c:39
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:133
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
char * key
Definition: dict.h:87
AVExpr * expr
Definition: f_metadata.c:79
char * value
Definition: dict.h:88
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:712
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:339
An instance of a filter.
Definition: avfilter.h:304
#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:72
internal API functions
void(* print)(AVFilterContext *ctx, const char *msg,...) av_printf_format(2
Definition: f_metadata.c:87
simple arithmetic expression evaluator