Browse code

lavfi: add fieldorder filter

Signed-off-by: Stefano Sabatini <stefano.sabatini-lala@poste.it>

Mark Himsley authored on 2011/04/11 02:18:03
Showing 6 changed files
... ...
@@ -90,6 +90,7 @@ version <next>:
90 90
 - DPX image encoder
91 91
 - MicroDVD subtitle file muxer and demuxer
92 92
 - Playstation Portable PMP format demuxer
93
+- fieldorder video filter added
93 94
 
94 95
 
95 96
 version 0.6:
... ...
@@ -520,6 +520,39 @@ fade=in:0:25, fade=out:975:25
520 520
 fade=in:5:20
521 521
 @end example
522 522
 
523
+@section fieldorder
524
+
525
+Transform the field order of the input video.
526
+
527
+It accepts one parameter which specifies the required field order that
528
+the input interlaced video will be transformed to. The parameter can
529
+assume one of the following values:
530
+
531
+@table @option
532
+@item 0 or bff
533
+output bottom field first
534
+@item 1 or tff
535
+output top field first
536
+@end table
537
+
538
+Default value is "tff".
539
+
540
+Transformation is achieved by shifting the picture content up or down
541
+by one line, and filling the remaining line with appropriate picture content.
542
+This method is consistent with most broadcast field order converters.
543
+
544
+If the input video is not flagged as being interlaced, or it is already
545
+flagged as being of the required output field order then this filter does
546
+not alter the incoming video.
547
+
548
+This filter is very useful when converting to or from PAL DV material,
549
+which is bottom field first.
550
+
551
+For example:
552
+@example
553
+./ffmpeg -i in.vob -vf "fieldorder=bff" out.dv
554
+@end example
555
+
523 556
 @section fifo
524 557
 
525 558
 Buffer input images and send them when they are requested.
... ...
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
29 29
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
30 30
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
31 31
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
32
+OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
32 33
 OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
33 34
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
34 35
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
... ...
@@ -47,6 +47,7 @@ void avfilter_register_all(void)
47 47
     REGISTER_FILTER (DRAWBOX,     drawbox,     vf);
48 48
     REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
49 49
     REGISTER_FILTER (FADE,        fade,        vf);
50
+    REGISTER_FILTER (FIELDORDER,  fieldorder,  vf);
50 51
     REGISTER_FILTER (FIFO,        fifo,        vf);
51 52
     REGISTER_FILTER (FORMAT,      format,      vf);
52 53
     REGISTER_FILTER (FREI0R,      frei0r,      vf);
... ...
@@ -26,8 +26,8 @@
26 26
 #include "libavutil/samplefmt.h"
27 27
 
28 28
 #define LIBAVFILTER_VERSION_MAJOR  1
29
-#define LIBAVFILTER_VERSION_MINOR 77
30
-#define LIBAVFILTER_VERSION_MICRO  1
29
+#define LIBAVFILTER_VERSION_MINOR 78
30
+#define LIBAVFILTER_VERSION_MICRO  0
31 31
 
32 32
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
33 33
                                                LIBAVFILTER_VERSION_MINOR, \
34 34
new file mode 100644
... ...
@@ -0,0 +1,235 @@
0
+/*
1
+ * Copyright (c) 2011 Mark Himsley
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 Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 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 GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along 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
+/**
21
+ * @file
22
+ * video field order filter, heavily influenced by vf_pad.c
23
+ */
24
+
25
+/* #define DEBUG */
26
+
27
+#include "libavutil/imgutils.h"
28
+#include "libavutil/pixdesc.h"
29
+#include "avfilter.h"
30
+
31
+typedef struct
32
+{
33
+    unsigned int dst_tff;      ///< output bff/tff
34
+    int          line_size[4]; ///< bytes of pixel data per line for each plane
35
+} FieldOrderContext;
36
+
37
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
38
+{
39
+    FieldOrderContext *fieldorder = ctx->priv;
40
+
41
+    const char *tff = "tff";
42
+    const char *bff = "bff";
43
+
44
+    if (!args) {
45
+        fieldorder->dst_tff = 1;
46
+    } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
47
+        fieldorder->dst_tff = !!fieldorder->dst_tff;
48
+    } else if (!strcmp(tff, args)) {
49
+        fieldorder->dst_tff = 1;
50
+    } else if (!strcmp(bff, args)) {
51
+        fieldorder->dst_tff = 0;
52
+    } else {
53
+        av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
54
+        return AVERROR(EINVAL);
55
+    }
56
+
57
+    av_log(ctx, AV_LOG_INFO, "output field order: %s\n",
58
+            fieldorder->dst_tff ? tff : bff);
59
+
60
+    return 0;
61
+}
62
+
63
+static int query_formats(AVFilterContext *ctx)
64
+{
65
+    AVFilterFormats  *formats;
66
+    enum PixelFormat pix_fmt;
67
+    int              ret;
68
+
69
+    /** accept any input pixel format that is not hardware accelerated, not
70
+     *  a bitstream format, and does not have vertically sub-sampled chroma */
71
+    if (ctx->inputs[0]) {
72
+        formats = NULL;
73
+        for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
74
+            if (!(  av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
75
+                 || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
76
+                && av_pix_fmt_descriptors[pix_fmt].nb_components
77
+                && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
78
+                && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
79
+                avfilter_formats_unref(&formats);
80
+                return ret;
81
+            }
82
+        avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
83
+        avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
84
+    }
85
+
86
+    return 0;
87
+}
88
+
89
+static int config_input(AVFilterLink *inlink)
90
+{
91
+    AVFilterContext   *ctx        = inlink->dst;
92
+    FieldOrderContext *fieldorder = ctx->priv;
93
+    int               plane;
94
+
95
+    /** full an array with the number of bytes that the video
96
+     *  data occupies per line for each plane of the input video */
97
+    for (plane = 0; plane < 4; plane++) {
98
+        fieldorder->line_size[plane] = av_image_get_linesize(
99
+                inlink->format,
100
+                inlink->w,
101
+                plane);
102
+    }
103
+
104
+    return 0;
105
+}
106
+
107
+static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
108
+{
109
+    AVFilterContext   *ctx        = inlink->dst;
110
+    AVFilterLink      *outlink    = ctx->outputs[0];
111
+
112
+    return avfilter_get_video_buffer(outlink, perms, w, h);
113
+}
114
+
115
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
116
+{
117
+    AVFilterContext   *ctx        = inlink->dst;
118
+    AVFilterLink      *outlink    = ctx->outputs[0];
119
+
120
+    AVFilterBufferRef *outpicref;
121
+
122
+    outpicref = avfilter_ref_buffer(inpicref, ~0);
123
+    outlink->out_buf = outpicref;
124
+
125
+    avfilter_start_frame(outlink, outpicref);
126
+}
127
+
128
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
129
+{
130
+    AVFilterContext   *ctx        = inlink->dst;
131
+    FieldOrderContext *fieldorder = ctx->priv;
132
+    AVFilterLink      *outlink    = ctx->outputs[0];
133
+
134
+    AVFilterBufferRef *inpicref   = inlink->cur_buf;
135
+
136
+    /** can only currently do slices if this filter is doing nothing
137
+     *  because this filter is moving picture content, the output
138
+     *  slice will contain different video lines than the input slice
139
+     *  and that complexity will be added later */
140
+    if (  !inpicref->video->interlaced
141
+        || inpicref->video->top_field_first == fieldorder->dst_tff) {
142
+        avfilter_draw_slice(outlink, y, h, slice_dir);
143
+    }
144
+}
145
+
146
+static void end_frame(AVFilterLink *inlink)
147
+{
148
+    AVFilterContext   *ctx        = inlink->dst;
149
+    FieldOrderContext *fieldorder = ctx->priv;
150
+    AVFilterLink      *outlink    = ctx->outputs[0];
151
+
152
+    AVFilterBufferRef *inpicref   = inlink->cur_buf;
153
+    AVFilterBufferRef *outpicref  = outlink->out_buf;
154
+
155
+    int               h, w, plane, line_step, line_size, line;
156
+    uint8_t           *cpy_src, *cpy_dst;
157
+
158
+    if (    inpicref->video->interlaced
159
+         && inpicref->video->top_field_first != fieldorder->dst_tff) {
160
+        av_dlog(ctx,
161
+                "picture will move %s one line\n",
162
+                fieldorder->dst_tff ? "up" : "down");
163
+        h = inpicref->video->h;
164
+        w = inpicref->video->w;
165
+        for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
166
+            line_step = inpicref->linesize[plane];
167
+            line_size = fieldorder->line_size[plane];
168
+            cpy_src = inpicref->data[plane];
169
+            cpy_dst = outpicref->data[plane];
170
+            if (fieldorder->dst_tff) {
171
+                /** Move every line up one line, working from
172
+                 *  the top to the bottom of the frame.
173
+                 *  The original top line is lost.
174
+                 *  The new last line is created as a copy of the
175
+                 *  penultimate line from that field. */
176
+                for (line = 0; line < h; line++) {
177
+                    if (1 + line < outpicref->video->h) {
178
+                        memcpy(cpy_dst, cpy_src + line_step, line_size);
179
+                    } else {
180
+                        memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
181
+                    }
182
+                    cpy_src += line_step;
183
+                    cpy_dst += line_step;
184
+                }
185
+            } else {
186
+                /** Move every line down one line, working from
187
+                 *  the bottom to the top of the frame.
188
+                 *  The original bottom line is lost.
189
+                 *  The new first line is created as a copy of the
190
+                 *  second line from that field. */
191
+                cpy_src += (h - 1) * line_step;
192
+                cpy_dst += (h - 1) * line_step;
193
+                for (line = h - 1; line >= 0 ; line--) {
194
+                    if (line > 0) {
195
+                        memcpy(cpy_dst, cpy_src - line_step, line_size);
196
+                    } else {
197
+                        memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
198
+                    }
199
+                    cpy_src -= line_step;
200
+                    cpy_dst -= line_step;
201
+                }
202
+            }
203
+        }
204
+        outpicref->video->top_field_first = fieldorder->dst_tff;
205
+        avfilter_draw_slice(outlink, 0, h, 1);
206
+    } else {
207
+        av_dlog(ctx,
208
+                "not interlaced or field order already correct\n");
209
+    }
210
+
211
+    avfilter_end_frame(outlink);
212
+    avfilter_unref_buffer(inpicref);
213
+}
214
+
215
+AVFilter avfilter_vf_fieldorder = {
216
+    .name          = "fieldorder",
217
+    .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
218
+    .init          = init,
219
+    .priv_size     = sizeof(FieldOrderContext),
220
+    .query_formats = query_formats,
221
+    .inputs        = (AVFilterPad[]) {{ .name             = "default",
222
+                                        .type             = AVMEDIA_TYPE_VIDEO,
223
+                                        .config_props     = config_input,
224
+                                        .start_frame      = start_frame,
225
+                                        .get_video_buffer = get_video_buffer,
226
+                                        .draw_slice       = draw_slice,
227
+                                        .end_frame        = end_frame,
228
+                                        .min_perms        = AV_PERM_READ,
229
+                                        .rej_perms        = AV_PERM_REUSE2|AV_PERM_PRESERVE,},
230
+                                      { .name = NULL}},
231
+    .outputs       = (AVFilterPad[]) {{ .name             = "default",
232
+                                        .type             = AVMEDIA_TYPE_VIDEO, },
233
+                                      { .name = NULL}},
234
+};