Browse code

lavfi: add field filter

The filter is a port of libmpcodecs/vf_field.c, since there is no common
code I relicensed it as LGPL, while keeping the original author
copyright.

Stefano Sabatini authored on 2012/10/30 20:15:28
Showing 9 changed files
... ...
@@ -18,6 +18,7 @@ version <next>:
18 18
 - LVF demuxer
19 19
 - ffescape tool
20 20
 - metadata (info chunk) support in CAF muxer
21
+- field filter ported from libmpcodecs
21 22
 
22 23
 
23 24
 version 1.0:
... ...
@@ -2128,6 +2128,31 @@ fade=in:5:20
2128 2128
 fade=in:0:25:alpha=1
2129 2129
 @end example
2130 2130
 
2131
+@section field
2132
+
2133
+Extract a single field from an interlaced image using stride
2134
+arithmetic to avoid wasting CPU time. The output frames are marked as
2135
+non-interlaced.
2136
+
2137
+This filter accepts the following named options:
2138
+@table @option
2139
+@item type
2140
+Specify whether to extract the top (if the value is @code{0} or
2141
+@code{top}) or the bottom field (if the value is @code{1} or
2142
+@code{bottom}).
2143
+@end table
2144
+
2145
+If the option key is not specified, the first value sets the @var{type}
2146
+option. For example:
2147
+@example
2148
+field=bottom
2149
+@end example
2150
+
2151
+is equivalent to:
2152
+@example
2153
+field=type=bottom
2154
+@end example
2155
+
2131 2156
 @section fieldorder
2132 2157
 
2133 2158
 Transform the field order of the input video.
... ...
@@ -98,6 +98,7 @@ OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
98 98
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
99 99
 OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
100 100
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
101
+OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
101 102
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
102 103
 OBJS-$(CONFIG_FIFO_FILTER)                   += fifo.o
103 104
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
... ...
@@ -90,6 +90,7 @@ void avfilter_register_all(void)
90 90
     REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
91 91
     REGISTER_FILTER (EDGEDETECT,  edgedetect,  vf);
92 92
     REGISTER_FILTER (FADE,        fade,        vf);
93
+    REGISTER_FILTER (FIELD,       field,       vf);
93 94
     REGISTER_FILTER (FIELDORDER,  fieldorder,  vf);
94 95
     REGISTER_FILTER (FIFO,        fifo,        vf);
95 96
     REGISTER_FILTER (FORMAT,      format,      vf);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  20
33
-#define LIBAVFILTER_VERSION_MICRO 113
32
+#define LIBAVFILTER_VERSION_MINOR  21
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,145 @@
0
+/*
1
+ * Copyright (c) 2003 Rich Felker
2
+ * Copyright (c) 2012 Stefano Sabatini
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
+ * field filter, based on libmpcodecs/vf_field.c by Rich Felker
24
+ */
25
+
26
+#include "libavutil/opt.h"
27
+#include "libavutil/pixdesc.h"
28
+#include "avfilter.h"
29
+#include "internal.h"
30
+
31
+enum FieldType { FIELD_TYPE_TOP = 0, FIELD_TYPE_BOTTOM };
32
+
33
+typedef struct {
34
+    const AVClass *class;
35
+    enum FieldType type;
36
+    int nb_planes;              ///< number of planes of the current format
37
+} FieldContext;
38
+
39
+#define OFFSET(x) offsetof(FieldContext, x)
40
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
41
+
42
+static const AVOption field_options[] = {
43
+    {"type", "set field type (top or bottom)", OFFSET(type), AV_OPT_TYPE_INT, {.i64=FIELD_TYPE_TOP}, 0, 1, FLAGS, "field_type" },
44
+    {"top",    "select top field",    0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_TOP},    INT_MIN, INT_MAX, FLAGS, "field_type"},
45
+    {"bottom", "select bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field_type"},
46
+
47
+    {NULL}
48
+};
49
+
50
+AVFILTER_DEFINE_CLASS(field);
51
+
52
+static av_cold int init(AVFilterContext *ctx, const char *args)
53
+{
54
+    FieldContext *field = ctx->priv;
55
+    static const char *shorthand[] = { "type", NULL };
56
+
57
+    field->class = &field_class;
58
+    av_opt_set_defaults(field);
59
+
60
+    return av_opt_set_from_string(field, args, shorthand, "=", ":");
61
+}
62
+
63
+static int config_props_output(AVFilterLink *outlink)
64
+{
65
+    AVFilterContext *ctx = outlink->src;
66
+    FieldContext *field = ctx->priv;
67
+    AVFilterLink *inlink = ctx->inputs[0];
68
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
69
+    int i;
70
+
71
+    for (i = 0; i < desc->nb_components; i++)
72
+        field->nb_planes = FFMAX(field->nb_planes, desc->comp[i].plane);
73
+    field->nb_planes++;
74
+
75
+    outlink->w = inlink->w;
76
+    outlink->h = (inlink->h + (field->type == FIELD_TYPE_TOP)) / 2;
77
+
78
+    av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d type:%s -> w:%d h:%d\n",
79
+           inlink->w, inlink->h, field->type == FIELD_TYPE_BOTTOM ? "bottom" : "top",
80
+           outlink->w, outlink->h);
81
+    return 0;
82
+}
83
+
84
+static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
85
+{
86
+    FieldContext *field = inlink->dst->priv;
87
+    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
88
+    AVFilterLink *outlink = inlink->dst->outputs[0];
89
+    int i;
90
+
91
+    if (!outpicref)
92
+        return AVERROR(ENOMEM);
93
+
94
+    outpicref->video->h = outlink->h;
95
+    outpicref->video->interlaced = 0;
96
+
97
+    for (i = 0; i < field->nb_planes; i++) {
98
+        if (field->type == FIELD_TYPE_BOTTOM)
99
+            outpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
100
+        outpicref->linesize[i] = 2 * inpicref->linesize[i];
101
+    }
102
+    return ff_start_frame(outlink, outpicref);
103
+}
104
+
105
+static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
106
+{
107
+    FieldContext *field = inlink->dst->priv;
108
+    int y1 = (y + (field->type == FIELD_TYPE_TOP)) / 2;
109
+    int h1 = (h + (field->type == FIELD_TYPE_TOP)) / 2;
110
+    return ff_draw_slice(inlink->dst->outputs[0], y1, h1, slice_dir);
111
+}
112
+
113
+static const AVFilterPad field_inputs[] = {
114
+    {
115
+        .name             = "default",
116
+        .type             = AVMEDIA_TYPE_VIDEO,
117
+        .get_video_buffer = ff_null_get_video_buffer,
118
+        .start_frame      = start_frame,
119
+        .draw_slice       = draw_slice,
120
+        .end_frame        = ff_null_end_frame,
121
+    },
122
+    { NULL }
123
+};
124
+
125
+static const AVFilterPad field_outputs[] = {
126
+    {
127
+        .name          = "default",
128
+        .type          = AVMEDIA_TYPE_VIDEO,
129
+        .config_props  = config_props_output,
130
+    },
131
+    { NULL }
132
+};
133
+
134
+AVFilter avfilter_vf_field = {
135
+    .name          = "field",
136
+    .description   = NULL_IF_CONFIG_SMALL("Extract a field from the input video."),
137
+
138
+    .priv_size     = sizeof(FieldContext),
139
+    .init          = init,
140
+
141
+    .inputs        = field_inputs,
142
+    .outputs       = field_outputs,
143
+    .priv_class    = &field_class,
144
+};
... ...
@@ -11,6 +11,7 @@ FATE_LAVFI = fate-lavfi-alphaextract_rgb                                \
11 11
              fate-lavfi-drawbox                                         \
12 12
              fate-lavfi-edgedetect                                      \
13 13
              fate-lavfi-fade                                            \
14
+             fate-lavfi-field                                           \
14 15
              fate-lavfi-idet                                            \
15 16
              fate-lavfi-life                                            \
16 17
              fate-lavfi-null                                            \
... ...
@@ -111,6 +111,7 @@ do_lavfi_pixfmts(){
111 111
 do_lavfi_pixfmts "copy"    ""
112 112
 do_lavfi_pixfmts "crop"    "100:100:100:100"
113 113
 do_lavfi_pixfmts "hflip"   ""
114
+do_lavfi_pixfmts "field"   "field" "bottom"
114 115
 do_lavfi_pixfmts "null"    ""
115 116
 do_lavfi_pixfmts "pad"     "500:400:20:20"
116 117
 do_lavfi_pixfmts "pixdesctest" ""
117 118
new file mode 100644
... ...
@@ -0,0 +1,101 @@
0
+0bgr                57434af4bddb691877f2400c704604eb
1
+0rgb                fc2ba950163aeee98590181e31fcd202
2
+abgr                3c78d0a72484a1ecd3cae245b9fa988c
3
+argb                d5057a2be412864719ffb8ba129c1f2c
4
+bgr0                b33c6b58b0d7bf6ce07d5a2d7c267040
5
+bgr24               bd6620738df19410d5df5f31e7451709
6
+bgr444be            219e318b7e0e05050181e71df6b9539d
7
+bgr444le            8354f2cf5b30de0233d302a74816649d
8
+bgr48be             22be50bd0aa39f07ad1b1aa57cb741ce
9
+bgr48le             c4b0f8057b3eac237e9228e83bdc4c66
10
+bgr4_byte           d4c3304b4b823a130c335379e4d3444d
11
+bgr555be            c3072da465233dbfc8f61dc7a9766d2c
12
+bgr555le            be83adcf0b802b061442f0c564fd5987
13
+bgr565be            bf955b9a035af0e613cf1de249f55f9d
14
+bgr565le            6dd85cd5e19266c53a54cbcf06d396a7
15
+bgr8                9669f6974f0fc1c0afa1c7d4df093c0b
16
+bgra                f7cabae31dd7465dab2203f45db646f8
17
+gray                66a09b53f7d3f79dcb6096f3ec3740c5
18
+gray16be            a447af6482b922c9997ac02e5d3535f1
19
+gray16le            c1dd0db327295898ff282d07f48c105d
20
+monob               1b7fb7e69a913a0a1c0dffc54e5899ea
21
+monow               b5d3778a054fc73d515d36f8a6bc693b
22
+nv12                b3829e9ae2a15349432b7efac4236068
23
+nv21                963cf5780e07301ff2906bf345b6d0ff
24
+pal8                bfedafc3bf19c2c12eeb87125833142a
25
+rgb0                d7481143742ff68abfbac4195edbede0
26
+rgb24               908d5494062c617bc87149c9daaf2167
27
+rgb444be            281a8f186a2726c2b31aa7e09e21c865
28
+rgb444le            93f9ee6265d8ad5e744ab652563f9b78
29
+rgb48be             0d1d60e1639edb2758ad776cb5583970
30
+rgb48le             c958b5e98324263e97de2bb528f5bda4
31
+rgb4_byte           2ec97bf65649e3d47eb6812701544593
32
+rgb555be            21b9138b229d4065b02d38b5b62f18d1
33
+rgb555le            0307ee34e562b2fb2b1c6988ae18b2b2
34
+rgb565be            e8f3ebcbb9a5fff000eca8a312f89782
35
+rgb565le            53bbd558fb0dcd82f1fad83ea855c3ad
36
+rgb8                67bfdd4fa88b1ab9be876f42dfc75683
37
+rgba                d0ebdf1495bc6b7e9d3bfbe2813a9d16
38
+uyvy422             a6a52504a16f09b8f2ec2405bc8190b5
39
+yuv410p             3feb55b1e2a385b488c82e808a12587b
40
+yuv411p             ab3dd8e6cf1452afe2d2e976e4726370
41
+yuv420p             52e26ad198368e2326bef97cdfa6a2bb
42
+yuv420p10be         04353bfc21e9b88cd7776e83be756742
43
+yuv420p10le         3f8e7167dbd12976c6ee516b8c952363
44
+yuv420p12be         b058ac076c8a5fe522b9fd9b8422054e
45
+yuv420p12le         ad0bf28e69eeb14eac5d8f9ea8b801f1
46
+yuv420p14be         c7a435d42f07928332ecb21a7d96ad7a
47
+yuv420p14le         5507e8db4e58c9517012686a7408996b
48
+yuv420p16be         5241d64e9fa2fd6590fd23ea0e8a6f90
49
+yuv420p16le         78da606f761a4fb62fdac05aa5092742
50
+yuv420p9be          e4bcaf5d6a7030f950b08501327f6175
51
+yuv420p9le          bbf80e57389578be66d4a7a12335a613
52
+yuv422p             e461a21995da361b88202339a2ebb879
53
+yuv422p10be         a3e13070215f5a016ac9bae7e7115417
54
+yuv422p10le         8e9e3d9adc8fdb8a0a03d79bdc31eefe
55
+yuv422p12be         4c339f71d79d2dac1dabc6121e1cf021
56
+yuv422p12le         69d336fccbe1ffa88106ea5bde0c8743
57
+yuv422p14be         f20c21dbfda632d26816fce27c1cb6e9
58
+yuv422p14le         70dddb8bdad188079a05113059d139f8
59
+yuv422p16be         55cfed8fa610f82b6625e16871dab235
60
+yuv422p16le         e2488df0f22987fe7ed12a5ef2adf835
61
+yuv422p9be          80fcdd7fd9cdd79632104dcc32f78b4b
62
+yuv422p9le          e0ec9f94c875297ee5d0546274df40e9
63
+yuv440p             f8e80596babcdb94378ec8bebf2dd46d
64
+yuv444p             572bad9e12ed53e242658fa613412279
65
+yuv444p10be         c5304f086afc4624d4fffb66a3cf3cb8
66
+yuv444p10le         d1754974b936f74028752d49413d30aa
67
+yuv444p12be         206d6b0fbd84d4e013b9b074cbd65135
68
+yuv444p12le         dc2b1bfbecba71eba50e7e4da470a8d0
69
+yuv444p14be         19cd2ef75ed5698898c55040e51def88
70
+yuv444p14le         8d47c9575d1355572ee9bfc873d46753
71
+yuv444p16be         3a67c28325978db734ba03b1828c15da
72
+yuv444p16le         c6dc275a4277fd3c65535253bb298263
73
+yuv444p9be          f47357cdd775fc399aeab3ae58712fb9
74
+yuv444p9le          e29799ecb6fac9f5b6d85bc34d248d4b
75
+yuva420p            82ab09bb7a3a24bf95aeb3fa9d939847
76
+yuva420p10be        f4559039e99ecf74a58e7063b1e7c5d3
77
+yuva420p10le        b1eb7df29134936450c2c312ae23a1a4
78
+yuva420p16be        c580495d34c6ac6e3e3b81772a0f3070
79
+yuva420p16le        1c49deeafb8f81e9186f3906c9b1a670
80
+yuva420p9be         a788d7cc6ad67ad52619a08da126569a
81
+yuva420p9le         c1579a5c015908f26b76480d82f6a648
82
+yuva422p            c162b37ce05360c47b2a2224ea0748ad
83
+yuva422p10be        6df70d1018e8c0c9fa377f72f49bf81b
84
+yuva422p10le        2eb54f20d3e5f180c539d92a75fe66e9
85
+yuva422p16be        1c61492076be33404894c1d3ec578d87
86
+yuva422p16le        1c085c9479a57eea35e74c264c947d73
87
+yuva422p9be         271c6cc091052731373ef5313bc76435
88
+yuva422p9le         b8c2e963ac35371e8aa6a05d5c252b37
89
+yuva444p            4a85f1f17e95829cd53c9a28928fd8eb
90
+yuva444p10be        d312f0d30a88fdd18e992362ea3b5f81
91
+yuva444p10le        d12aed62a367bc7735e59503a3cf8cc6
92
+yuva444p16be        ac5c17adeb0ef6730a0de1dbd1d14a1a
93
+yuva444p16le        41f1a82eb686d7191bdb00206f723247
94
+yuva444p9be         413d01385a8b008031b2ab3ef0b9eff4
95
+yuva444p9le         33ede0bd20bfd85489d266ac81d035d6
96
+yuvj420p            762dc6a157d0bee72da3e3d852668aef
97
+yuvj422p            8cec955c1c62b00b6798361ef82962b7
98
+yuvj440p            7b469444994d8b52766ee461bcb795ea
99
+yuvj444p            b395162325af489c465a3e6a31fbb0e7
100
+yuyv422             1efb17cd0a48d2e956fd574ea6f412e7