]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/f_metadata.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / f_metadata.c
index 523a94d38cb20844088ae0099ca26b79cb52e7c5..5fec7c3c56606fe89e48b72ce4b6bd60f909f1fb 100644 (file)
@@ -54,6 +54,7 @@ enum MetadataFunction {
     METADATAF_EQUAL,
     METADATAF_GREATER,
     METADATAF_EXPR,
+    METADATAF_ENDS_WITH,
     METADATAF_NB
 };
 
@@ -87,6 +88,8 @@ typedef struct MetadataContext {
     int (*compare)(struct MetadataContext *s,
                    const char *value1, const char *value2);
     void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
+
+    int direct;    // reduces buffering when printing to user-supplied URL
 } MetadataContext;
 
 #define OFFSET(x) offsetof(MetadataContext, x)
@@ -107,8 +110,10 @@ static const AVOption filt_name##_options[] = { \
     {   "equal",       NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL   },     0, 3, FLAGS, "function" }, \
     {   "greater",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER },     0, 3, FLAGS, "function" }, \
     {   "expr",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR    },     0, 3, FLAGS, "function" }, \
+    {   "ends_with",   NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_ENDS_WITH },   0, 0, FLAGS, "function" }, \
     { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
     { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
+    { "direct", "reduce buffering when printing to user-set file or pipe", OFFSET(direct), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS }, \
     { NULL } \
 }
 
@@ -122,6 +127,14 @@ static int starts_with(MetadataContext *s, const char *value1, const char *value
     return !strncmp(value1, value2, strlen(value2));
 }
 
+static int ends_with(MetadataContext *s, const char *value1, const char *value2)
+{
+    const int len1 = strlen(value1);
+    const int len2 = strlen(value2);
+
+    return !strncmp(value1 + FFMAX(len1 - len2, 0), value2, len2);
+}
+
 static int equal(MetadataContext *s, const char *value1, const char *value2)
 {
     float f1, f2;
@@ -212,6 +225,9 @@ static av_cold int init(AVFilterContext *ctx)
     case METADATAF_STARTS_WITH:
         s->compare = starts_with;
         break;
+    case METADATAF_ENDS_WITH:
+        s->compare = ends_with;
+        break;
     case METADATAF_LESS:
         s->compare = less;
         break;
@@ -261,6 +277,9 @@ static av_cold int init(AVFilterContext *ctx)
                    s->file_str, buf);
             return ret;
         }
+
+        if (s->direct)
+            s->avio_context->direct = AVIO_FLAG_DIRECT;
     }
 
     return 0;
@@ -270,6 +289,8 @@ static av_cold void uninit(AVFilterContext *ctx)
 {
     MetadataContext *s = ctx->priv;
 
+    av_expr_free(s->expr);
+    s->expr = NULL;
     if (s->avio_context) {
         avio_closep(&s->avio_context);
     }
@@ -283,7 +304,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     AVDictionary **metadata = &frame->metadata;
     AVDictionaryEntry *e;
 
-    if (!*metadata)
+    if (!*metadata && s->mode != METADATA_ADD)
         return ff_filter_frame(outlink, frame);
 
     e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
@@ -305,13 +326,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
             av_dict_set(metadata, s->key, s->value, 0);
         }
         return ff_filter_frame(outlink, frame);
-        break;
     case METADATA_MODIFY:
         if (e && e->value) {
             av_dict_set(metadata, s->key, s->value, 0);
         }
         return ff_filter_frame(outlink, frame);
-        break;
     case METADATA_PRINT:
         if (!s->key && e) {
             s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
@@ -326,7 +345,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
             s->print(ctx, "%s=%s\n", s->key, e->value);
         }
         return ff_filter_frame(outlink, frame);
-        break;
     case METADATA_DELETE:
         if (!s->key) {
             av_dict_free(metadata);
@@ -334,7 +352,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
             av_dict_set(metadata, s->key, NULL, 0);
         }
         return ff_filter_frame(outlink, frame);
-        break;
     default:
         av_assert0(0);
     };
@@ -366,7 +383,7 @@ static const AVFilterPad aoutputs[] = {
     { NULL }
 };
 
-AVFilter ff_af_ametadata = {
+const AVFilter ff_af_ametadata = {
     .name          = "ametadata",
     .description   = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
     .priv_size     = sizeof(MetadataContext),
@@ -401,7 +418,7 @@ static const AVFilterPad outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_metadata = {
+const AVFilter ff_vf_metadata = {
     .name        = "metadata",
     .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
     .priv_size   = sizeof(MetadataContext),