LCOV - code coverage report
Current view: top level - libavfilter - f_metadata.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 48 138 34.8 %
Date: 2017-12-13 18:07:29 Functions: 4 11 36.4 %

          Line data    Source code
       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             : 
      41             : enum MetadataMode {
      42             :     METADATA_SELECT,
      43             :     METADATA_ADD,
      44             :     METADATA_MODIFY,
      45             :     METADATA_DELETE,
      46             :     METADATA_PRINT,
      47             :     METADATA_NB
      48             : };
      49             : 
      50             : enum 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             : 
      60             : static const char *const var_names[] = {
      61             :     "VALUE1",
      62             :     "VALUE2",
      63             :     NULL
      64             : };
      65             : 
      66             : enum var_name {
      67             :     VAR_VALUE1,
      68             :     VAR_VALUE2,
      69             :     VAR_VARS_NB
      70             : };
      71             : 
      72             : typedef 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) \
      94             : static 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             : 
     115           0 : static int same_str(MetadataContext *s, const char *value1, const char *value2)
     116             : {
     117           0 :     return !strcmp(value1, value2);
     118             : }
     119             : 
     120           0 : static int starts_with(MetadataContext *s, const char *value1, const char *value2)
     121             : {
     122           0 :     return !strncmp(value1, value2, strlen(value2));
     123             : }
     124             : 
     125           0 : static int equal(MetadataContext *s, const char *value1, const char *value2)
     126             : {
     127             :     float f1, f2;
     128             : 
     129           0 :     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
     130           0 :         return 0;
     131             : 
     132           0 :     return fabsf(f1 - f2) < FLT_EPSILON;
     133             : }
     134             : 
     135           0 : static int less(MetadataContext *s, const char *value1, const char *value2)
     136             : {
     137             :     float f1, f2;
     138             : 
     139           0 :     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
     140           0 :         return 0;
     141             : 
     142           0 :     return (f1 - f2) < FLT_EPSILON;
     143             : }
     144             : 
     145           0 : static int greater(MetadataContext *s, const char *value1, const char *value2)
     146             : {
     147             :     float f1, f2;
     148             : 
     149           0 :     if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
     150           0 :         return 0;
     151             : 
     152           0 :     return (f2 - f1) < FLT_EPSILON;
     153             : }
     154             : 
     155           0 : static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
     156             : {
     157             :     double f1, f2;
     158             : 
     159           0 :     if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
     160           0 :         return 0;
     161             : 
     162           0 :     s->var_values[VAR_VALUE1] = f1;
     163           0 :     s->var_values[VAR_VALUE2] = f2;
     164             : 
     165           0 :     return av_expr_eval(s->expr, s->var_values, NULL);
     166             : }
     167             : 
     168           0 : static void print_log(AVFilterContext *ctx, const char *msg, ...)
     169             : {
     170             :     va_list argument_list;
     171             : 
     172           0 :     va_start(argument_list, msg);
     173           0 :     if (msg)
     174           0 :         av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
     175           0 :     va_end(argument_list);
     176           0 : }
     177             : 
     178         150 : static void print_file(AVFilterContext *ctx, const char *msg, ...)
     179             : {
     180         150 :     MetadataContext *s = ctx->priv;
     181             :     va_list argument_list;
     182             : 
     183         150 :     va_start(argument_list, msg);
     184         150 :     if (msg) {
     185             :         char buf[128];
     186         150 :         vsnprintf(buf, sizeof(buf), msg, argument_list);
     187         150 :         avio_write(s->avio_context, buf, av_strnlen(buf, sizeof(buf)));
     188             :     }
     189         150 :     va_end(argument_list);
     190         150 : }
     191             : 
     192           8 : static av_cold int init(AVFilterContext *ctx)
     193             : {
     194           8 :     MetadataContext *s = ctx->priv;
     195             :     int ret;
     196             : 
     197           8 :     if (!s->key && s->mode != METADATA_PRINT && s->mode != METADATA_DELETE) {
     198           0 :         av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
     199           0 :         return AVERROR(EINVAL);
     200             :     }
     201             : 
     202          16 :     if ((s->mode == METADATA_MODIFY ||
     203           8 :         s->mode == METADATA_ADD) && !s->value) {
     204           0 :         av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
     205           0 :         return AVERROR(EINVAL);
     206             :     }
     207             : 
     208           8 :     switch (s->function) {
     209           8 :     case METADATAF_SAME_STR:
     210           8 :         s->compare = same_str;
     211           8 :         break;
     212           0 :     case METADATAF_STARTS_WITH:
     213           0 :         s->compare = starts_with;
     214           0 :         break;
     215           0 :     case METADATAF_LESS:
     216           0 :         s->compare = less;
     217           0 :         break;
     218           0 :     case METADATAF_EQUAL:
     219           0 :         s->compare = equal;
     220           0 :         break;
     221           0 :     case METADATAF_GREATER:
     222           0 :         s->compare = greater;
     223           0 :         break;
     224           0 :     case METADATAF_EXPR:
     225           0 :         s->compare = parse_expr;
     226           0 :         break;
     227           0 :     default:
     228           0 :         av_assert0(0);
     229             :     };
     230             : 
     231           8 :     if (s->function == METADATAF_EXPR) {
     232           0 :         if (!s->expr_str) {
     233           0 :             av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
     234           0 :             return AVERROR(EINVAL);
     235             :         }
     236           0 :         if ((ret = av_expr_parse(&s->expr, s->expr_str,
     237             :                                  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
     238           0 :             av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
     239           0 :             return ret;
     240             :         }
     241             :     }
     242             : 
     243           8 :     if (s->mode == METADATA_PRINT && s->file_str) {
     244           8 :         s->print = print_file;
     245             :     } else {
     246           0 :         s->print = print_log;
     247             :     }
     248             : 
     249           8 :     s->avio_context = NULL;
     250           8 :     if (s->file_str) {
     251           8 :         if (!strcmp("-", s->file_str)) {
     252           8 :             ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
     253             :         } else {
     254           0 :             ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
     255             :         }
     256             : 
     257           8 :         if (ret < 0) {
     258             :             char buf[128];
     259           0 :             av_strerror(ret, buf, sizeof(buf));
     260           0 :             av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
     261             :                    s->file_str, buf);
     262           0 :             return ret;
     263             :         }
     264             :     }
     265             : 
     266           8 :     return 0;
     267             : }
     268             : 
     269           8 : static av_cold void uninit(AVFilterContext *ctx)
     270             : {
     271           8 :     MetadataContext *s = ctx->priv;
     272             : 
     273           8 :     if (s->avio_context) {
     274           8 :         avio_closep(&s->avio_context);
     275             :     }
     276           8 : }
     277             : 
     278          20 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     279             : {
     280          20 :     AVFilterContext *ctx = inlink->dst;
     281          20 :     AVFilterLink *outlink = ctx->outputs[0];
     282          20 :     MetadataContext *s = ctx->priv;
     283          20 :     AVDictionary **metadata = &frame->metadata;
     284             :     AVDictionaryEntry *e;
     285             : 
     286          20 :     if (!*metadata)
     287           0 :         return ff_filter_frame(outlink, frame);
     288             : 
     289          20 :     e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
     290          20 :                     !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
     291             : 
     292          20 :     switch (s->mode) {
     293           0 :     case METADATA_SELECT:
     294           0 :         if (!s->value && e && e->value) {
     295           0 :             return ff_filter_frame(outlink, frame);
     296           0 :         } else if (s->value && e && e->value &&
     297           0 :                    s->compare(s, e->value, s->value)) {
     298           0 :             return ff_filter_frame(outlink, frame);
     299             :         }
     300           0 :         break;
     301           0 :     case METADATA_ADD:
     302           0 :         if (e && e->value) {
     303             :             ;
     304             :         } else {
     305           0 :             av_dict_set(metadata, s->key, s->value, 0);
     306             :         }
     307           0 :         return ff_filter_frame(outlink, frame);
     308             :         break;
     309           0 :     case METADATA_MODIFY:
     310           0 :         if (e && e->value) {
     311           0 :             av_dict_set(metadata, s->key, s->value, 0);
     312             :         }
     313           0 :         return ff_filter_frame(outlink, frame);
     314             :         break;
     315          20 :     case METADATA_PRINT:
     316          40 :         if (!s->key && e) {
     317         100 :             s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
     318          80 :                      inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
     319          20 :             s->print(ctx, "%s=%s\n", e->key, e->value);
     320         150 :             while ((e = av_dict_get(*metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
     321         110 :                 s->print(ctx, "%s=%s\n", e->key, e->value);
     322             :             }
     323           0 :         } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
     324           0 :             s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
     325           0 :                      inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
     326           0 :             s->print(ctx, "%s=%s\n", s->key, e->value);
     327             :         }
     328          20 :         return ff_filter_frame(outlink, frame);
     329             :         break;
     330           0 :     case METADATA_DELETE:
     331           0 :         if (!s->key) {
     332           0 :             av_dict_free(metadata);
     333           0 :         } else if (e && e->value && (!s->value || s->compare(s, e->value, s->value))) {
     334           0 :             av_dict_set(metadata, s->key, NULL, 0);
     335             :         }
     336           0 :         return ff_filter_frame(outlink, frame);
     337             :         break;
     338           0 :     default:
     339           0 :         av_assert0(0);
     340             :     };
     341             : 
     342           0 :     av_frame_free(&frame);
     343             : 
     344           0 :     return 0;
     345             : }
     346             : 
     347             : #if CONFIG_AMETADATA_FILTER
     348             : 
     349             : DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
     350             : AVFILTER_DEFINE_CLASS(ametadata);
     351             : 
     352             : static const AVFilterPad ainputs[] = {
     353             :     {
     354             :         .name         = "default",
     355             :         .type         = AVMEDIA_TYPE_AUDIO,
     356             :         .filter_frame = filter_frame,
     357             :     },
     358             :     { NULL }
     359             : };
     360             : 
     361             : static const AVFilterPad aoutputs[] = {
     362             :     {
     363             :         .name = "default",
     364             :         .type = AVMEDIA_TYPE_AUDIO,
     365             :     },
     366             :     { NULL }
     367             : };
     368             : 
     369             : AVFilter 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             : 
     384             : DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
     385             : AVFILTER_DEFINE_CLASS(metadata);
     386             : 
     387             : static const AVFilterPad inputs[] = {
     388             :     {
     389             :         .name         = "default",
     390             :         .type         = AVMEDIA_TYPE_VIDEO,
     391             :         .filter_frame = filter_frame,
     392             :     },
     393             :     { NULL }
     394             : };
     395             : 
     396             : static const AVFilterPad outputs[] = {
     397             :     {
     398             :         .name = "default",
     399             :         .type = AVMEDIA_TYPE_VIDEO,
     400             :     },
     401             :     { NULL }
     402             : };
     403             : 
     404             : AVFilter 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 */

Generated by: LCOV version 1.13