Browse code

lavfi/sidedata: add filter for manipulating frame side data

This is a similar filter to f_metadata, only it works on side data. Since
adding side data from a user provided arbitrary binary string is unsafe,
because current code assumes that a side data of a certain kind has the proper
size, this filter only implements selection and deletion. Also, no value
matching support is implemented yet, because there is no uniform way to specify
a side data textually.

Signed-off-by: Marton Balint <cus@passwd.hu>

Marton Balint authored on 2016/10/05 07:32:48
Showing 6 changed files
... ...
@@ -36,6 +36,7 @@ version <next>:
36 36
 - extended mov edit list support
37 37
 - libfaac encoder removed
38 38
 - Matroska muxer now writes CRC32 elements by default in all Level 1 elements
39
+- sidedata video and asidedata audio filter
39 40
 
40 41
 
41 42
 version 3.1:
... ...
@@ -17568,6 +17568,36 @@ ffmpeg -i audio.flac -lavfi showwavespic=split_channels=1:s=1024x800 waveform.pn
17568 17568
 @end example
17569 17569
 @end itemize
17570 17570
 
17571
+@section sidedata, asidedata
17572
+
17573
+Delete frame side data, or select frames based on it.
17574
+
17575
+This filter accepts the following options:
17576
+
17577
+@table @option
17578
+@item mode
17579
+Set mode of operation of the filter.
17580
+
17581
+Can be one of the following:
17582
+
17583
+@table @samp
17584
+@item select
17585
+Select every frame with side data of @code{type}.
17586
+
17587
+@item delete
17588
+Delete side data of @code{type}. If @code{type} is not set, delete all side
17589
+data in the frame.
17590
+
17591
+@end table
17592
+
17593
+@item type
17594
+Set side data type used with all modes. Must be set for @code{select} mode. For
17595
+the list of frame side data types, refer to the @code{AVFrameSideDataType} enum
17596
+in @file{libavutil/frame.h}. For example, to choose
17597
+@code{AV_FRAME_DATA_PANSCAN} side data, you must specify @code{PANSCAN}.
17598
+
17599
+@end table
17600
+
17571 17601
 @section spectrumsynth
17572 17602
 
17573 17603
 Sythesize audio from 2 input video spectrums, first input stream represents
... ...
@@ -62,6 +62,7 @@ OBJS-$(CONFIG_ASETPTS_FILTER)                += setpts.o
62 62
 OBJS-$(CONFIG_ASETRATE_FILTER)               += af_asetrate.o
63 63
 OBJS-$(CONFIG_ASETTB_FILTER)                 += settb.o
64 64
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
65
+OBJS-$(CONFIG_ASIDEDATA_FILTER)              += f_sidedata.o
65 66
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
66 67
 OBJS-$(CONFIG_ASTATS_FILTER)                 += af_astats.o
67 68
 OBJS-$(CONFIG_ASTREAMSELECT_FILTER)          += f_streamselect.o
... ...
@@ -270,6 +271,7 @@ OBJS-$(CONFIG_SHOWINFO_FILTER)               += vf_showinfo.o
270 270
 OBJS-$(CONFIG_SHOWPALETTE_FILTER)            += vf_showpalette.o
271 271
 OBJS-$(CONFIG_SHUFFLEFRAMES_FILTER)          += vf_shuffleframes.o
272 272
 OBJS-$(CONFIG_SHUFFLEPLANES_FILTER)          += vf_shuffleplanes.o
273
+OBJS-$(CONFIG_SIDEDATA_FILTER)               += f_sidedata.o
273 274
 OBJS-$(CONFIG_SIGNALSTATS_FILTER)            += vf_signalstats.o
274 275
 OBJS-$(CONFIG_SMARTBLUR_FILTER)              += vf_smartblur.o
275 276
 OBJS-$(CONFIG_SOBEL_FILTER)                  += vf_convolution.o
... ...
@@ -80,6 +80,7 @@ void avfilter_register_all(void)
80 80
     REGISTER_FILTER(ASETRATE,       asetrate,       af);
81 81
     REGISTER_FILTER(ASETTB,         asettb,         af);
82 82
     REGISTER_FILTER(ASHOWINFO,      ashowinfo,      af);
83
+    REGISTER_FILTER(ASIDEDATA,      asidedata,      af);
83 84
     REGISTER_FILTER(ASPLIT,         asplit,         af);
84 85
     REGISTER_FILTER(ASTATS,         astats,         af);
85 86
     REGISTER_FILTER(ASTREAMSELECT,  astreamselect,  af);
... ...
@@ -286,6 +287,7 @@ void avfilter_register_all(void)
286 286
     REGISTER_FILTER(SHOWPALETTE,    showpalette,    vf);
287 287
     REGISTER_FILTER(SHUFFLEFRAMES,  shuffleframes,  vf);
288 288
     REGISTER_FILTER(SHUFFLEPLANES,  shuffleplanes,  vf);
289
+    REGISTER_FILTER(SIDEDATA,       sidedata,       vf);
289 290
     REGISTER_FILTER(SIGNALSTATS,    signalstats,    vf);
290 291
     REGISTER_FILTER(SMARTBLUR,      smartblur,      vf);
291 292
     REGISTER_FILTER(SOBEL,          sobel,          vf);
292 293
new file mode 100644
... ...
@@ -0,0 +1,181 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+/**
19
+ * @file
20
+ * filter for manipulating frame side data
21
+ */
22
+
23
+#include "libavutil/avassert.h"
24
+#include "libavutil/internal.h"
25
+#include "libavutil/frame.h"
26
+#include "libavutil/opt.h"
27
+#include "avfilter.h"
28
+#include "formats.h"
29
+#include "internal.h"
30
+
31
+enum SideDataMode {
32
+    SIDEDATA_SELECT,
33
+    SIDEDATA_DELETE,
34
+    SIDEDATA_NB
35
+};
36
+
37
+typedef struct SideDataContext {
38
+    const AVClass *class;
39
+
40
+    int mode;
41
+    enum AVFrameSideDataType type;
42
+} SideDataContext;
43
+
44
+#define OFFSET(x) offsetof(SideDataContext, x)
45
+#define DEFINE_OPTIONS(filt_name, FLAGS) \
46
+static const AVOption filt_name##_options[] = { \
47
+    { "mode", "set a mode of operation", OFFSET(mode),   AV_OPT_TYPE_INT,    {.i64 = 0 }, 0, SIDEDATA_NB-1, FLAGS, "mode" }, \
48
+    {   "select", "select frame",        0,              AV_OPT_TYPE_CONST,  {.i64 = SIDEDATA_SELECT }, 0, 0, FLAGS, "mode" }, \
49
+    {   "delete", "delete side data",    0,              AV_OPT_TYPE_CONST,  {.i64 = SIDEDATA_DELETE }, 0, 0, FLAGS, "mode" }, \
50
+    { "type",   "set side data type",    OFFSET(type),   AV_OPT_TYPE_INT,    {.i64 = -1 }, -1, INT_MAX, FLAGS, "type" }, \
51
+    {   "PANSCAN",                    "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_PANSCAN                    }, 0, 0, FLAGS, "type" }, \
52
+    {   "A53_CC",                     "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_A53_CC                     }, 0, 0, FLAGS, "type" }, \
53
+    {   "STEREO3D",                   "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_STEREO3D                   }, 0, 0, FLAGS, "type" }, \
54
+    {   "MATRIXENCODING",             "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_MATRIXENCODING             }, 0, 0, FLAGS, "type" }, \
55
+    {   "DOWNMIX_INFO",               "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_DOWNMIX_INFO               }, 0, 0, FLAGS, "type" }, \
56
+    {   "REPLAYGAIN",                 "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_REPLAYGAIN                 }, 0, 0, FLAGS, "type" }, \
57
+    {   "DISPLAYMATRIX",              "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_DISPLAYMATRIX              }, 0, 0, FLAGS, "type" }, \
58
+    {   "AFD",                        "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_AFD                        }, 0, 0, FLAGS, "type" }, \
59
+    {   "MOTION_VECTORS",             "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_MOTION_VECTORS             }, 0, 0, FLAGS, "type" }, \
60
+    {   "SKIP_SAMPLES",               "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_SKIP_SAMPLES               }, 0, 0, FLAGS, "type" }, \
61
+    {   "AUDIO_SERVICE_TYPE",         "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_AUDIO_SERVICE_TYPE         }, 0, 0, FLAGS, "type" }, \
62
+    {   "MASTERING_DISPLAY_METADATA", "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, 0, 0, FLAGS, "type" }, \
63
+    {   "GOP_TIMECODE",               "", 0,             AV_OPT_TYPE_CONST,  {.i64 = AV_FRAME_DATA_GOP_TIMECODE               }, 0, 0, FLAGS, "type" }, \
64
+    { NULL } \
65
+}
66
+
67
+static av_cold int init(AVFilterContext *ctx)
68
+{
69
+    SideDataContext *s = ctx->priv;
70
+
71
+    if (s->type == -1 && s->mode != SIDEDATA_DELETE) {
72
+        av_log(ctx, AV_LOG_ERROR, "Side data type must be set\n");
73
+        return AVERROR(EINVAL);
74
+    }
75
+
76
+    return 0;
77
+}
78
+
79
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
80
+{
81
+    AVFilterContext *ctx = inlink->dst;
82
+    AVFilterLink *outlink = ctx->outputs[0];
83
+    SideDataContext *s = ctx->priv;
84
+    AVFrameSideData *sd = NULL;
85
+
86
+    if (s->type != -1)
87
+       sd = av_frame_get_side_data(frame, s->type);
88
+
89
+    switch (s->mode) {
90
+    case SIDEDATA_SELECT:
91
+        if (sd) {
92
+            return ff_filter_frame(outlink, frame);
93
+        }
94
+        break;
95
+    case SIDEDATA_DELETE:
96
+        if (s->type == -1) {
97
+            while (frame->nb_side_data)
98
+                av_frame_remove_side_data(frame, frame->side_data[0]->type);
99
+        } else if (sd) {
100
+            av_frame_remove_side_data(frame, s->type);
101
+        }
102
+        return ff_filter_frame(outlink, frame);
103
+        break;
104
+    default:
105
+        av_assert0(0);
106
+    };
107
+
108
+    av_frame_free(&frame);
109
+
110
+    return 0;
111
+}
112
+
113
+#if CONFIG_ASIDEDATA_FILTER
114
+
115
+DEFINE_OPTIONS(asidedata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
116
+AVFILTER_DEFINE_CLASS(asidedata);
117
+
118
+static const AVFilterPad ainputs[] = {
119
+    {
120
+        .name         = "default",
121
+        .type         = AVMEDIA_TYPE_AUDIO,
122
+        .filter_frame = filter_frame,
123
+    },
124
+    { NULL }
125
+};
126
+
127
+static const AVFilterPad aoutputs[] = {
128
+    {
129
+        .name = "default",
130
+        .type = AVMEDIA_TYPE_AUDIO,
131
+    },
132
+    { NULL }
133
+};
134
+
135
+AVFilter ff_af_asidedata = {
136
+    .name          = "asidedata",
137
+    .description   = NULL_IF_CONFIG_SMALL("Manipulate audio frame side data."),
138
+    .priv_size     = sizeof(SideDataContext),
139
+    .priv_class    = &asidedata_class,
140
+    .init          = init,
141
+    .query_formats = ff_query_formats_all,
142
+    .inputs        = ainputs,
143
+    .outputs       = aoutputs,
144
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
145
+};
146
+#endif /* CONFIG_ASIDEDATA_FILTER */
147
+
148
+#if CONFIG_SIDEDATA_FILTER
149
+
150
+DEFINE_OPTIONS(sidedata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
151
+AVFILTER_DEFINE_CLASS(sidedata);
152
+
153
+static const AVFilterPad inputs[] = {
154
+    {
155
+        .name         = "default",
156
+        .type         = AVMEDIA_TYPE_VIDEO,
157
+        .filter_frame = filter_frame,
158
+    },
159
+    { NULL }
160
+};
161
+
162
+static const AVFilterPad outputs[] = {
163
+    {
164
+        .name = "default",
165
+        .type = AVMEDIA_TYPE_VIDEO,
166
+    },
167
+    { NULL }
168
+};
169
+
170
+AVFilter ff_vf_sidedata = {
171
+    .name        = "sidedata",
172
+    .description = NULL_IF_CONFIG_SMALL("Manipulate video frame side data."),
173
+    .priv_size   = sizeof(SideDataContext),
174
+    .priv_class  = &sidedata_class,
175
+    .init        = init,
176
+    .inputs      = inputs,
177
+    .outputs     = outputs,
178
+    .flags       = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
179
+};
180
+#endif /* CONFIG_SIDEDATA_FILTER */
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  63
33
+#define LIBAVFILTER_VERSION_MINOR  64
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \