Browse code

lavfi: port MP il filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/02/09 00:48:59
Showing 6 changed files
... ...
@@ -15,6 +15,7 @@ version <next>:
15 15
 - improved showspectrum filter, with multichannel support and sox-like colors
16 16
 - histogram filter
17 17
 - tee muxer
18
+- il filter ported from libmpcodecs
18 19
 
19 20
 
20 21
 version 1.1:
... ...
@@ -4648,6 +4648,43 @@ Useful for enlarging pixel art images without reducing sharpness.
4648 4648
 @section swapuv
4649 4649
 Swap U & V plane.
4650 4650
 
4651
+@section il
4652
+Deinterleave or interleave fields.
4653
+
4654
+This filter allows to process interlaced images fields without
4655
+deinterlacing them. Deinterleaving splits the input frame into 2
4656
+fields (so called half pictures). Odd lines are moved to the top
4657
+half of the output image, even lines to the bottom half.
4658
+You can process (filter) them independently and then re-interleave them.
4659
+
4660
+It accepts a list of options in the form of @var{key}=@var{value} pairs
4661
+separated by ":". A description of the accepted options follows.
4662
+
4663
+@table @option
4664
+@item luma_mode, l
4665
+@item chroma_mode, s
4666
+@item alpha_mode, a
4667
+Available values for @var{luma_mode}, @var{chroma_mode} and
4668
+@var{alpha_mode} are:
4669
+
4670
+@table @samp
4671
+@item none
4672
+Do nothing.
4673
+
4674
+@item deinterleave, d
4675
+Deinterleave fields, placing one above the other.
4676
+
4677
+@item interleave, i
4678
+Interleave fields. Reversing effect of deinterleave.
4679
+@end table
4680
+Default value is @code{none}.
4681
+
4682
+@item luma_swap, ls
4683
+@item chroma_swap, cs
4684
+@item alpha_swap, as
4685
+Swap luma/chroma/alpha fields. Exchange even & odd lines. Default value is @code{0}.
4686
+@end table
4687
+
4651 4688
 @section thumbnail
4652 4689
 Select the most representative frame in a given sequence of consecutive frames.
4653 4690
 
... ...
@@ -126,6 +126,7 @@ OBJS-$(CONFIG_HISTOGRAM_FILTER)              += vf_histogram.o
126 126
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
127 127
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
128 128
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
129
+OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
129 130
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
130 131
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
131 132
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
... ...
@@ -120,6 +120,7 @@ void avfilter_register_all(void)
120 120
     REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
121 121
     REGISTER_FILTER(HUE,            hue,            vf);
122 122
     REGISTER_FILTER(IDET,           idet,           vf);
123
+    REGISTER_FILTER(IL,             il,             vf);
123 124
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
124 125
     REGISTER_FILTER(LUT,            lut,            vf);
125 126
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  36
32
+#define LIBAVFILTER_VERSION_MINOR  37
33 33
 #define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
new file mode 100644
... ...
@@ -0,0 +1,236 @@
0
+/*
1
+ * Copyright (c) 2002 Michael Niedmayer
2
+ * Copyright (c) 2013 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
+ * (de)interleave fields filter
24
+ */
25
+
26
+#include "libavutil/opt.h"
27
+#include "libavutil/pixdesc.h"
28
+#include "avfilter.h"
29
+#include "internal.h"
30
+
31
+enum FilterMode {
32
+    MODE_NONE,
33
+    MODE_INTERLEAVE,
34
+    MODE_DEINTERLEAVE
35
+};
36
+
37
+typedef struct {
38
+    const AVClass *class;
39
+    enum FilterMode luma_mode, chroma_mode, alpha_mode;
40
+    int luma_swap, chroma_swap, alpha_swap;
41
+    int nb_planes;
42
+    int chroma_width, chroma_height;
43
+    int width;
44
+} IlContext;
45
+
46
+#define OFFSET(x) offsetof(IlContext, x)
47
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48
+
49
+static const AVOption il_options[] = {
50
+    {"luma_mode",   "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
51
+    {"l",           "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
52
+    {"none",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE},         0, 0, FLAGS, "luma_mode"},
53
+    {"interleave",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "luma_mode"},
54
+    {"i",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "luma_mode"},
55
+    {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
56
+    {"d",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
57
+    {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
58
+    {"c",           "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
59
+    {"none",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE},         0, 0, FLAGS, "chroma_mode"},
60
+    {"interleave",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "chroma_mode"},
61
+    {"i",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "chroma_mode"},
62
+    {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
63
+    {"d",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
64
+    {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
65
+    {"a",          "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
66
+    {"none",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE},         0, 0, FLAGS, "alpha_mode"},
67
+    {"interleave",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "alpha_mode"},
68
+    {"i",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE},   0, 0, FLAGS, "alpha_mode"},
69
+    {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
70
+    {"d",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
71
+    {"luma_swap",   "swap luma fields",   OFFSET(luma_swap),   AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
72
+    {"ls",          "swap luma fields",   OFFSET(luma_swap),   AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
73
+    {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
74
+    {"cs",          "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
75
+    {"alpha_swap",  "swap alpha fields",  OFFSET(alpha_swap),  AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
76
+    {"as",          "swap alpha fields",  OFFSET(alpha_swap),  AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
77
+    {NULL}
78
+};
79
+
80
+AVFILTER_DEFINE_CLASS(il);
81
+
82
+static av_cold int init(AVFilterContext *ctx, const char *args)
83
+{
84
+    IlContext *il = ctx->priv;
85
+    int ret;
86
+
87
+    il->class = &il_class;
88
+    av_opt_set_defaults(il);
89
+
90
+    if ((ret = av_set_options_string(il, args, "=", ":")) < 0)
91
+        return ret;
92
+
93
+    return 0;
94
+}
95
+
96
+static int config_input(AVFilterLink *inlink)
97
+{
98
+    IlContext *il = inlink->dst->priv;
99
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
100
+    int bpp = av_get_padded_bits_per_pixel(desc) >> 3;
101
+    int bytes = FFALIGN(desc->comp[0].depth_minus1 + 1, 8) / 8;
102
+    int i;
103
+
104
+    for (i = 0; i < desc->nb_components; i++)
105
+        il->nb_planes = FFMAX(il->nb_planes, desc->comp[i].plane);
106
+    il->nb_planes++;
107
+
108
+    il->chroma_height = inlink->h >> desc->log2_chroma_h;
109
+    il->chroma_width  = (inlink->w >> desc->log2_chroma_w) * bytes;
110
+    il->width         = inlink->w * bytes;
111
+    if (!(desc->flags & PIX_FMT_PLANAR))
112
+        il->width *= bpp;
113
+
114
+    return 0;
115
+}
116
+
117
+static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
118
+                       int dst_linesize, int src_linesize,
119
+                       enum FilterMode mode, int swap)
120
+{
121
+    const int a = swap;
122
+    const int b = 1 - a;
123
+    const int m = h >> 1;
124
+    int y;
125
+
126
+    switch (mode) {
127
+    case MODE_DEINTERLEAVE:
128
+        for (y = 0; y < m; y++) {
129
+            memcpy(dst + dst_linesize *  y     , src + src_linesize * (y * 2 + a), w);
130
+            memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
131
+        }
132
+        break;
133
+    case MODE_NONE:
134
+        for (y = 0; y < m; y++) {
135
+            memcpy(dst + dst_linesize *  y * 2     , src + src_linesize * (y * 2 + a), w);
136
+            memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
137
+        }
138
+        break;
139
+    case MODE_INTERLEAVE:
140
+        for (y = 0; y < m; y++) {
141
+            memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize *  y     , w);
142
+            memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
143
+        }
144
+        break;
145
+    }
146
+}
147
+
148
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
149
+{
150
+    IlContext *il = inlink->dst->priv;
151
+    AVFilterLink *outlink = inlink->dst->outputs[0];
152
+    AVFilterBufferRef *out;
153
+    int ret;
154
+
155
+    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
156
+    if (!out) {
157
+        avfilter_unref_bufferp(&inpicref);
158
+        return AVERROR(ENOMEM);
159
+    }
160
+    avfilter_copy_buffer_ref_props(out, inpicref);
161
+
162
+    interleave(out->data[0], inpicref->data[0],
163
+               il->width, inlink->h,
164
+               out->linesize[0], inpicref->linesize[0],
165
+               il->luma_mode, il->luma_swap);
166
+
167
+    if (il->nb_planes > 2) {
168
+        interleave(out->data[1], inpicref->data[1],
169
+                   il->chroma_width, il->chroma_height,
170
+                   out->linesize[1], inpicref->linesize[1],
171
+                   il->chroma_mode, il->chroma_swap);
172
+        interleave(out->data[2], inpicref->data[2],
173
+                   il->chroma_width, il->chroma_height,
174
+                   out->linesize[2], inpicref->linesize[2],
175
+                   il->chroma_mode, il->chroma_swap);
176
+    }
177
+    if (il->nb_planes == 2 && il->nb_planes == 4) {
178
+        int comp = il->nb_planes - 1;
179
+        interleave(out->data[comp], inpicref->data[comp],
180
+                   il->width, inlink->h,
181
+                   out->linesize[comp], inpicref->linesize[comp],
182
+                   il->alpha_mode, il->alpha_swap);
183
+    }
184
+
185
+    ret = ff_filter_frame(outlink, out);
186
+    avfilter_unref_bufferp(&inpicref);
187
+    return ret;
188
+}
189
+
190
+static int query_formats(AVFilterContext *ctx)
191
+{
192
+    AVFilterFormats *formats = NULL;
193
+    int fmt;
194
+
195
+    for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
196
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
197
+        if (!(desc->flags & PIX_FMT_PAL ||
198
+            fmt == AV_PIX_FMT_NV21 ||
199
+            fmt == AV_PIX_FMT_NV12))
200
+            ff_add_format(&formats, fmt);
201
+    }
202
+
203
+    ff_set_common_formats(ctx, formats);
204
+    return 0;
205
+}
206
+
207
+static const AVFilterPad inputs[] = {
208
+    {
209
+        .name             = "default",
210
+        .type             = AVMEDIA_TYPE_VIDEO,
211
+        .get_video_buffer = ff_null_get_video_buffer,
212
+        .filter_frame     = filter_frame,
213
+        .config_props     = config_input,
214
+    },
215
+    { NULL }
216
+};
217
+
218
+static const AVFilterPad outputs[] = {
219
+    {
220
+        .name          = "default",
221
+        .type          = AVMEDIA_TYPE_VIDEO,
222
+    },
223
+    { NULL }
224
+};
225
+
226
+AVFilter avfilter_vf_il = {
227
+    .name          = "il",
228
+    .description   = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
229
+    .priv_size     = sizeof(IlContext),
230
+    .init          = init,
231
+    .query_formats = query_formats,
232
+    .inputs        = inputs,
233
+    .outputs       = outputs,
234
+    .priv_class    = &il_class,
235
+};