]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_interlace.c
Merge commit 'e926b5ceb1962833f0c884a328382bc2eca67aff'
[ffmpeg] / libavfilter / vf_interlace.c
1 /*
2  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2011 Stefano Sabatini
5  * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * @file
26  * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
27  */
28
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/avassert.h"
33
34 #include "formats.h"
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "video.h"
38
39 enum ScanMode {
40     MODE_TFF = 0,
41     MODE_BFF = 1,
42 };
43
44 enum FieldType {
45     FIELD_UPPER = 0,
46     FIELD_LOWER = 1,
47 };
48
49 typedef struct {
50     const AVClass *class;
51     enum ScanMode scan;    // top or bottom field first scanning
52     int lowpass;           // enable or disable low pass filterning
53     AVFrame *cur, *next;   // the two frames from which the new one is obtained
54     int got_output;        // signal an output frame is reday to request_frame()
55 } InterlaceContext;
56
57 #define OFFSET(x) offsetof(InterlaceContext, x)
58 #define V AV_OPT_FLAG_VIDEO_PARAM
59 static const AVOption options[] = {
60     { "scan", "scanning mode", OFFSET(scan),
61         AV_OPT_TYPE_INT,   {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
62     { "tff", "top field first", 0,
63         AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
64     { "bff", "bottom field first", 0,
65         AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
66     { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
67         AV_OPT_TYPE_INT,   {.i64 = 1 },        0, 1, .flags = V },
68     { NULL }
69 };
70
71 static const AVClass class = {
72     .class_name = "interlace filter",
73     .item_name  = av_default_item_name,
74     .option     = options,
75     .version    = LIBAVUTIL_VERSION_INT,
76 };
77
78
79 static const enum AVPixelFormat formats_supported[] = {
80     AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
81     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
82     AV_PIX_FMT_GRAY8,    AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
83     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
84 };
85
86 static int query_formats(AVFilterContext *ctx)
87 {
88     ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
89     return 0;
90 }
91
92 static av_cold void uninit(AVFilterContext *ctx)
93 {
94     InterlaceContext *s = ctx->priv;
95
96     av_frame_free(&s->cur);
97     av_frame_free(&s->next);
98 }
99
100 static int config_out_props(AVFilterLink *outlink)
101 {
102     AVFilterContext *ctx = outlink->src;
103     AVFilterLink *inlink = outlink->src->inputs[0];
104     InterlaceContext *s = ctx->priv;
105
106     if (inlink->h < 2) {
107         av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
108         return AVERROR_INVALIDDATA;
109     }
110     // same input size
111     outlink->w = inlink->w;
112     outlink->h = inlink->h;
113     outlink->time_base = inlink->time_base;
114     outlink->frame_rate = inlink->frame_rate;
115     // half framerate
116     outlink->time_base.num *= 2;
117     outlink->frame_rate.den *= 2;
118
119     av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
120            s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
121
122     return 0;
123 }
124
125 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
126                                AVFilterLink *inlink, enum FieldType field_type,
127                                int lowpass)
128 {
129     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
130     int vsub = desc->log2_chroma_h;
131     int plane, i, j;
132
133     for (plane = 0; plane < desc->nb_components; plane++) {
134         int lines = (plane == 1 || plane == 2) ? inlink->h >> vsub : inlink->h;
135         int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
136         uint8_t *dstp = dst_frame->data[plane];
137         const uint8_t *srcp = src_frame->data[plane];
138
139         av_assert0(linesize >= 0);
140
141         lines /= 2;
142         if (field_type == FIELD_LOWER)
143             srcp += src_frame->linesize[plane];
144         if (field_type == FIELD_LOWER)
145             dstp += dst_frame->linesize[plane];
146         if (lowpass) {
147             int srcp_linesize = src_frame->linesize[plane] * 2;
148             int dstp_linesize = dst_frame->linesize[plane] * 2;
149             for (j = lines; j > 0; j--) {
150                 const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
151                 const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
152                 if (j == lines)
153                     srcp_above = srcp; // there is no line above
154                 if (j == 1)
155                     srcp_below = srcp; // there is no line below
156                 for (i = 0; i < linesize; i++) {
157                     // this calculation is an integer representation of
158                     // '0.5 * current + 0.25 * above + 0.25 + below'
159                     // '1 +' is for rounding.
160                     dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
161                 }
162                 dstp += dstp_linesize;
163                 srcp += srcp_linesize;
164             }
165         } else {
166             av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
167                                 srcp, src_frame->linesize[plane] * 2,
168                                 linesize, lines);
169         }
170     }
171 }
172
173 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
174 {
175     AVFilterContext *ctx = inlink->dst;
176     AVFilterLink *outlink = ctx->outputs[0];
177     InterlaceContext *s = ctx->priv;
178     AVFrame *out;
179     int tff, ret;
180
181     av_frame_free(&s->cur);
182     s->cur  = s->next;
183     s->next = buf;
184
185     /* we need at least two frames */
186     if (!s->cur || !s->next)
187         return 0;
188
189     tff = (s->scan == MODE_TFF);
190     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
191     if (!out)
192         return AVERROR(ENOMEM);
193
194     av_frame_copy_props(out, s->cur);
195     out->interlaced_frame = 1;
196     out->top_field_first  = tff;
197     out->pts             /= 2;  // adjust pts to new framerate
198
199     /* copy upper/lower field from cur */
200     copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
201     av_frame_free(&s->cur);
202
203     /* copy lower/upper field from next */
204     copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
205     av_frame_free(&s->next);
206
207     ret = ff_filter_frame(outlink, out);
208     s->got_output = 1;
209
210     return ret;
211 }
212
213 static int request_frame(AVFilterLink *outlink)
214 {
215     AVFilterContext *ctx = outlink->src;
216     InterlaceContext *s  = ctx->priv;
217     int ret = 0;
218
219     s->got_output = 0;
220     while (ret >= 0 && !s->got_output)
221         ret = ff_request_frame(ctx->inputs[0]);
222
223     return ret;
224 }
225
226 static const AVFilterPad inputs[] = {
227     {
228         .name         = "default",
229         .type         = AVMEDIA_TYPE_VIDEO,
230         .filter_frame = filter_frame,
231     },
232     { NULL }
233 };
234
235 static const AVFilterPad outputs[] = {
236     {
237         .name          = "default",
238         .type          = AVMEDIA_TYPE_VIDEO,
239         .config_props  = config_out_props,
240         .request_frame = request_frame,
241     },
242     { NULL }
243 };
244
245 AVFilter avfilter_vf_interlace = {
246     .name          = "interlace",
247     .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
248     .uninit        = uninit,
249
250     .priv_class    = &class,
251     .priv_size     = sizeof(InterlaceContext),
252     .query_formats = query_formats,
253
254     .inputs        = inputs,
255     .outputs       = outputs,
256 };
257