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