Browse code

lavfi: port phase filter from libmpcodecs

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

Paul B Mahol authored on 2013/09/01 03:46:17
Showing 8 changed files
... ...
@@ -16,6 +16,7 @@ version <next>
16 16
 - incomplete Voxware MetaSound decoder
17 17
 - read EXIF metadata from JPEG
18 18
 - DVB teletext decoder
19
+- phase filter ported from libmpcodecs
19 20
 
20 21
 
21 22
 version 2.0:
... ...
@@ -40,6 +40,7 @@ Specifically, the GPL parts of FFmpeg are
40 40
     - vf_noise.c
41 41
     - vf_owdenoise.c
42 42
     - vf_perspective.c
43
+    - vf_phase.c
43 44
     - vf_pp.c
44 45
     - vf_sab.c
45 46
     - vf_smartblur.c
... ...
@@ -2220,6 +2220,7 @@ resample_filter_deps="avresample"
2220 2220
 ocv_filter_deps="libopencv"
2221 2221
 owdenoise_filter_deps="gpl"
2222 2222
 pan_filter_deps="swresample"
2223
+phase_filter_deps="gpl"
2223 2224
 pp_filter_deps="gpl postproc"
2224 2225
 removelogo_filter_deps="avcodec avformat swscale"
2225 2226
 sab_filter_deps="gpl swscale"
... ...
@@ -5854,6 +5854,67 @@ It accepts the following values:
5854 5854
 Default value is @samp{linear}.
5855 5855
 @end table
5856 5856
 
5857
+@section phase
5858
+
5859
+Delay interlaced video by one field time so that the field order changes.
5860
+
5861
+The intended use is to fix PAL movies that have been captured with the
5862
+opposite field order to the film-to-video transfer.
5863
+
5864
+A description of the accepted parameters follows.
5865
+
5866
+@table @option
5867
+@item mode
5868
+Set phase mode.
5869
+
5870
+It accepts the following values:
5871
+@table @samp
5872
+@item t
5873
+Capture field order top-first, transfer bottom-first.
5874
+Filter will delay the bottom field.
5875
+
5876
+@item b
5877
+Capture field order bottom-first, transfer top-first.
5878
+Filter will delay the top field.
5879
+
5880
+@item p
5881
+Capture and transfer with the same field order. This mode only exists
5882
+for the documentation of the other options to refer to, but if you
5883
+actually select it, the filter will faithfully do nothing.
5884
+
5885
+@item a
5886
+Capture field order determined automatically by field flags, transfer
5887
+opposite.
5888
+Filter selects among @samp{t} and @samp{b} modes on a frame by frame
5889
+basis using field flags. If no field information is available,
5890
+then this works just like @samp{u}.
5891
+
5892
+@item u
5893
+Capture unknown or varying, transfer opposite.
5894
+Filter selects among @samp{t} and @samp{b} on a frame by frame basis by
5895
+analyzing the images and selecting the alternative that produces best
5896
+match between the fields.
5897
+
5898
+@item T
5899
+Capture top-first, transfer unknown or varying.
5900
+Filter selects among @samp{t} and @samp{p} using image analysis.
5901
+
5902
+@item B
5903
+Capture bottom-first, transfer unknown or varying.
5904
+Filter selects among @samp{b} and @samp{p} using image analysis.
5905
+
5906
+@item A
5907
+Capture determined by field flags, transfer unknown or varying.
5908
+Filter selects among @samp{t}, @samp{b} and @samp{p} using field flags and
5909
+image analysis. If no field information is available, then this works just
5910
+like @samp{U}. This is the default mode.
5911
+
5912
+@item U
5913
+Both capture and transfer unknown or varying.
5914
+Filter selects among @samp{t}, @samp{b} and @samp{p} using image analysis only.
5915
+@end table
5916
+@end table
5917
+
5857 5918
 @section pixdesctest
5858 5919
 
5859 5920
 Pixel format descriptor test filter, mainly useful for internal
... ...
@@ -168,6 +168,7 @@ OBJS-$(CONFIG_OWDENOISE_FILTER)              += vf_owdenoise.o
168 168
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
169 169
 OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
170 170
 OBJS-$(CONFIG_PERSPECTIVE_FILTER)            += vf_perspective.o
171
+OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
171 172
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
172 173
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
173 174
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o
... ...
@@ -163,6 +163,7 @@ void avfilter_register_all(void)
163 163
     REGISTER_FILTER(PAD,            pad,            vf);
164 164
     REGISTER_FILTER(PERMS,          perms,          vf);
165 165
     REGISTER_FILTER(PERSPECTIVE,    perspective,    vf);
166
+    REGISTER_FILTER(PHASE,          phase,          vf);
166 167
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
167 168
     REGISTER_FILTER(PP,             pp,             vf);
168 169
     REGISTER_FILTER(PSNR,           psnr,           vf);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  82
34
-#define LIBAVFILTER_VERSION_MICRO 102
33
+#define LIBAVFILTER_VERSION_MINOR  83
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,320 @@
0
+/*
1
+ * Copyright (c) 2004 Ville Saari
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License along
16
+ * with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/avassert.h"
21
+#include "libavutil/imgutils.h"
22
+#include "libavutil/pixdesc.h"
23
+#include "libavutil/opt.h"
24
+#include "avfilter.h"
25
+#include "formats.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+enum PhaseMode {
30
+    PROGRESSIVE,
31
+    TOP_FIRST,
32
+    BOTTOM_FIRST,
33
+    TOP_FIRST_ANALYZE,
34
+    BOTTOM_FIRST_ANALYZE,
35
+    ANALYZE,
36
+    FULL_ANALYZE,
37
+    AUTO,
38
+    AUTO_ANALYZE
39
+};
40
+
41
+typedef struct PhaseContext {
42
+    const AVClass *class;
43
+    enum PhaseMode mode;
44
+    AVFrame *frame;
45
+    int nb_planes;
46
+    int planeheight[4];
47
+    int linesize[4];
48
+} PhaseContext;
49
+
50
+#define OFFSET(x) offsetof(PhaseContext, x)
51
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
53
+
54
+static const AVOption phase_options[] = {
55
+    { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
56
+    CONST("p", "progressive",          PROGRESSIVE,          "mode"),
57
+    CONST("t", "top first",            TOP_FIRST,            "mode"),
58
+    CONST("b", "bottom first",         BOTTOM_FIRST,         "mode"),
59
+    CONST("T", "top first analyze",    TOP_FIRST_ANALYZE,    "mode"),
60
+    CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
61
+    CONST("u", "analyze",              ANALYZE,              "mode"),
62
+    CONST("U", "full analyze",         FULL_ANALYZE,         "mode"),
63
+    CONST("a", "auto",                 AUTO,                 "mode"),
64
+    CONST("A", "auto analyze",         AUTO_ANALYZE,         "mode"),
65
+    { NULL }
66
+};
67
+
68
+AVFILTER_DEFINE_CLASS(phase);
69
+
70
+static int query_formats(AVFilterContext *ctx)
71
+{
72
+    static const enum AVPixelFormat pix_fmts[] = {
73
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
74
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
75
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
76
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
77
+    };
78
+
79
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
80
+    return 0;
81
+}
82
+
83
+static int config_input(AVFilterLink *inlink)
84
+{
85
+    PhaseContext *s = inlink->dst->priv;
86
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
87
+    int ret;
88
+
89
+    if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
90
+        return ret;
91
+
92
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
93
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
94
+
95
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
96
+
97
+    return 0;
98
+}
99
+
100
+/*
101
+ * This macro interpolates the value of both fields at a point halfway
102
+ * between lines and takes the squared difference. In field resolution
103
+ * the point is a quarter pixel below a line in one field and a quarter
104
+ * pixel above a line in other.
105
+ *
106
+ * (The result is actually multiplied by 25)
107
+ */
108
+#define DIFF(a, as, b, bs) (t = ((*a - b[bs]) << 2) + a[as << 1] - b[-bs], t * t)
109
+
110
+/*
111
+ * Find which field combination has the smallest average squared difference
112
+ * between the fields.
113
+ */
114
+static enum PhaseMode analyze_plane(AVFilterContext *ctx, PhaseContext *s,
115
+                                    AVFrame *old, AVFrame *new)
116
+{
117
+    double bdiff, tdiff, pdiff, scale;
118
+    const int ns = new->linesize[0];
119
+    const int os = old->linesize[0];
120
+    uint8_t *nptr = new->data[0];
121
+    uint8_t *optr = old->data[0];
122
+    const int h = new->height;
123
+    const int w = new->width;
124
+    int bdif, tdif, pdif;
125
+    enum PhaseMode mode = s->mode;
126
+    uint8_t *end, *rend;
127
+    int top, t;
128
+
129
+    if (mode == AUTO) {
130
+        mode = new->interlaced_frame ? new->top_field_first ?
131
+               TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
132
+    } else if (mode == AUTO_ANALYZE) {
133
+        mode = new->interlaced_frame ? new->top_field_first ?
134
+               TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
135
+    }
136
+
137
+    if (mode <= BOTTOM_FIRST) {
138
+        bdiff = pdiff = tdiff = 65536.0;
139
+    } else {
140
+        bdiff = pdiff = tdiff = 0.0;
141
+
142
+        for (end = nptr + (h - 2) * ns, nptr += ns, optr += os, top = 0;
143
+             nptr < end; nptr += ns - w, optr += os - w, top ^= 1) {
144
+            pdif = tdif = bdif = 0;
145
+
146
+            switch (mode) {
147
+            case TOP_FIRST_ANALYZE:
148
+                if (top) {
149
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
150
+                        pdif += DIFF(nptr, ns, nptr, ns);
151
+                        tdif += DIFF(nptr, ns, optr, os);
152
+                    }
153
+                } else {
154
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
155
+                        pdif += DIFF(nptr, ns, nptr, ns);
156
+                        tdif += DIFF(optr, os, nptr, ns);
157
+                    }
158
+                }
159
+                break;
160
+            case BOTTOM_FIRST_ANALYZE:
161
+                if (top) {
162
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
163
+                        pdif += DIFF(nptr, ns, nptr, ns);
164
+                        bdif += DIFF(optr, os, nptr, ns);
165
+                    }
166
+                } else {
167
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
168
+                        pdif += DIFF(nptr, ns, nptr, ns);
169
+                        bdif += DIFF(nptr, ns, optr, os);
170
+                    }
171
+                }
172
+                break;
173
+            case ANALYZE:
174
+                if (top) {
175
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
176
+                        tdif += DIFF(nptr, ns, optr, os);
177
+                        bdif += DIFF(optr, os, nptr, ns);
178
+                    }
179
+                } else {
180
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
181
+                        bdif += DIFF(nptr, ns, optr, os);
182
+                        tdif += DIFF(optr, os, nptr, ns);
183
+                    }
184
+                }
185
+                break;
186
+            case FULL_ANALYZE:
187
+                if (top) {
188
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
189
+                        pdif += DIFF(nptr, ns, nptr, ns);
190
+                        tdif += DIFF(nptr, ns, optr, os);
191
+                        bdif += DIFF(optr, os, nptr, ns);
192
+                    }
193
+                } else {
194
+                    for (rend = nptr + w; nptr < rend; nptr++, optr++) {
195
+                        pdif += DIFF(nptr, ns, nptr, ns);
196
+                        bdif += DIFF(nptr, ns, optr, os);
197
+                        tdif += DIFF(optr, os, nptr, ns);
198
+                    }
199
+                }
200
+                break;
201
+            default:
202
+                av_assert0(0);
203
+            }
204
+
205
+            pdiff += (double)pdif;
206
+            tdiff += (double)tdif;
207
+            bdiff += (double)bdif;
208
+        }
209
+
210
+        scale = 1.0 / (w * (h - 3)) / 25.0;
211
+        pdiff *= scale;
212
+        tdiff *= scale;
213
+        bdiff *= scale;
214
+
215
+        if (mode == TOP_FIRST_ANALYZE) {
216
+            bdiff = 65536.0;
217
+        } else if (mode == BOTTOM_FIRST_ANALYZE) {
218
+            tdiff = 65536.0;
219
+        } else if (mode == ANALYZE) {
220
+            pdiff = 65536.0;
221
+        }
222
+
223
+        if (bdiff < pdiff && bdiff < tdiff) {
224
+            mode = BOTTOM_FIRST;
225
+        } else if (tdiff < pdiff && tdiff < bdiff) {
226
+            mode = TOP_FIRST;
227
+        } else {
228
+            mode = PROGRESSIVE;
229
+        }
230
+    }
231
+
232
+    av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
233
+           mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
234
+           tdiff, bdiff, pdiff);
235
+    return mode;
236
+}
237
+
238
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
239
+{
240
+    AVFilterContext *ctx = inlink->dst;
241
+    AVFilterLink *outlink = ctx->outputs[0];
242
+    PhaseContext *s = ctx->priv;
243
+    enum PhaseMode mode = s->mode;
244
+    int plane, top, y;
245
+    AVFrame *out;
246
+
247
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
248
+    if (!out) {
249
+        av_frame_free(&in);
250
+        return AVERROR(ENOMEM);
251
+    }
252
+    av_frame_copy_props(out, in);
253
+
254
+    if (!s->frame) {
255
+        mode = PROGRESSIVE;
256
+        s->frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
257
+        if (!s->frame) {
258
+            av_frame_free(&in);
259
+            av_frame_free(&out);
260
+            return AVERROR(ENOMEM);
261
+        }
262
+    } else {
263
+        mode = analyze_plane(ctx, s, s->frame, in);
264
+    }
265
+
266
+    for (plane = 0; plane < s->nb_planes; plane++) {
267
+        uint8_t *buf = s->frame->data[plane];
268
+        uint8_t *from = in->data[plane];
269
+        uint8_t *to = out->data[plane];
270
+
271
+        for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
272
+            memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
273
+            memcpy(buf, from, s->linesize[plane]);
274
+
275
+            buf += s->frame->linesize[plane];
276
+            from += in->linesize[plane];
277
+            to += out->linesize[plane];
278
+        }
279
+    }
280
+
281
+    av_frame_free(&in);
282
+    return ff_filter_frame(outlink, out);
283
+}
284
+
285
+static av_cold void uninit(AVFilterContext *ctx)
286
+{
287
+    PhaseContext *s = ctx->priv;
288
+
289
+    av_frame_free(&s->frame);
290
+}
291
+
292
+static const AVFilterPad phase_inputs[] = {
293
+    {
294
+        .name          = "default",
295
+        .type          = AVMEDIA_TYPE_VIDEO,
296
+        .filter_frame  = filter_frame,
297
+        .config_props  = config_input,
298
+    },
299
+    { NULL }
300
+};
301
+
302
+static const AVFilterPad phase_outputs[] = {
303
+    {
304
+        .name = "default",
305
+        .type = AVMEDIA_TYPE_VIDEO,
306
+    },
307
+    { NULL }
308
+};
309
+
310
+AVFilter avfilter_vf_phase = {
311
+    .name          = "phase",
312
+    .description   = NULL_IF_CONFIG_SMALL("Phase shift fields."),
313
+    .priv_size     = sizeof(PhaseContext),
314
+    .priv_class    = &phase_class,
315
+    .uninit        = uninit,
316
+    .query_formats = query_formats,
317
+    .inputs        = phase_inputs,
318
+    .outputs       = phase_outputs,
319
+};