blob: efa31287274a5ed0077e4b488b42aa85e2dcbeb1
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 "interlace.h" |
37 | #include "internal.h" |
38 | #include "video.h" |
39 | |
40 | #define OFFSET(x) offsetof(InterlaceContext, x) |
41 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
42 | static const AVOption interlace_options[] = { |
43 | { "scan", "scanning mode", OFFSET(scan), |
44 | AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" }, |
45 | { "tff", "top field first", 0, |
46 | AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" }, |
47 | { "bff", "bottom field first", 0, |
48 | AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" }, |
49 | { "lowpass", "set vertical low-pass filter", OFFSET(lowpass), |
50 | AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = FLAGS }, |
51 | { NULL } |
52 | }; |
53 | |
54 | AVFILTER_DEFINE_CLASS(interlace); |
55 | |
56 | static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize, |
57 | const uint8_t *srcp, |
58 | const uint8_t *srcp_above, |
59 | const uint8_t *srcp_below) |
60 | { |
61 | int i; |
62 | for (i = 0; i < linesize; i++) { |
63 | // this calculation is an integer representation of |
64 | // '0.5 * current + 0.25 * above + 0.25 * below' |
65 | // '1 +' is for rounding. |
66 | dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2; |
67 | } |
68 | } |
69 | |
70 | static const enum AVPixelFormat formats_supported[] = { |
71 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, |
72 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P, |
73 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
74 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE |
75 | }; |
76 | |
77 | static int query_formats(AVFilterContext *ctx) |
78 | { |
79 | AVFilterFormats *fmts_list = ff_make_format_list(formats_supported); |
80 | if (!fmts_list) |
81 | return AVERROR(ENOMEM); |
82 | return ff_set_common_formats(ctx, fmts_list); |
83 | } |
84 | |
85 | static av_cold void uninit(AVFilterContext *ctx) |
86 | { |
87 | InterlaceContext *s = ctx->priv; |
88 | |
89 | av_frame_free(&s->cur); |
90 | av_frame_free(&s->next); |
91 | } |
92 | |
93 | static int config_out_props(AVFilterLink *outlink) |
94 | { |
95 | AVFilterContext *ctx = outlink->src; |
96 | AVFilterLink *inlink = outlink->src->inputs[0]; |
97 | InterlaceContext *s = ctx->priv; |
98 | |
99 | if (inlink->h < 2) { |
100 | av_log(ctx, AV_LOG_ERROR, "input video height is too small\n"); |
101 | return AVERROR_INVALIDDATA; |
102 | } |
103 | |
104 | if (!s->lowpass) |
105 | av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, " |
106 | "the resulting video will be aliased rather than interlaced.\n"); |
107 | |
108 | // same input size |
109 | outlink->w = inlink->w; |
110 | outlink->h = inlink->h; |
111 | outlink->time_base = inlink->time_base; |
112 | outlink->frame_rate = inlink->frame_rate; |
113 | // half framerate |
114 | outlink->time_base.num *= 2; |
115 | outlink->frame_rate.den *= 2; |
116 | |
117 | |
118 | if (s->lowpass) { |
119 | s->lowpass_line = lowpass_line_c; |
120 | if (ARCH_X86) |
121 | ff_interlace_init_x86(s); |
122 | } |
123 | |
124 | av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n", |
125 | s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without"); |
126 | |
127 | return 0; |
128 | } |
129 | |
130 | static void copy_picture_field(InterlaceContext *s, |
131 | AVFrame *src_frame, AVFrame *dst_frame, |
132 | AVFilterLink *inlink, enum FieldType field_type, |
133 | int lowpass) |
134 | { |
135 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
136 | int hsub = desc->log2_chroma_w; |
137 | int vsub = desc->log2_chroma_h; |
138 | int plane, j; |
139 | |
140 | for (plane = 0; plane < desc->nb_components; plane++) { |
141 | int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w; |
142 | int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h; |
143 | uint8_t *dstp = dst_frame->data[plane]; |
144 | const uint8_t *srcp = src_frame->data[plane]; |
145 | |
146 | av_assert0(cols >= 0 || lines >= 0); |
147 | |
148 | lines = (lines + (field_type == FIELD_UPPER)) / 2; |
149 | if (field_type == FIELD_LOWER) { |
150 | srcp += src_frame->linesize[plane]; |
151 | dstp += dst_frame->linesize[plane]; |
152 | } |
153 | if (lowpass) { |
154 | int srcp_linesize = src_frame->linesize[plane] * 2; |
155 | int dstp_linesize = dst_frame->linesize[plane] * 2; |
156 | for (j = lines; j > 0; j--) { |
157 | const uint8_t *srcp_above = srcp - src_frame->linesize[plane]; |
158 | const uint8_t *srcp_below = srcp + src_frame->linesize[plane]; |
159 | if (j == lines) |
160 | srcp_above = srcp; // there is no line above |
161 | if (j == 1) |
162 | srcp_below = srcp; // there is no line below |
163 | s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below); |
164 | dstp += dstp_linesize; |
165 | srcp += srcp_linesize; |
166 | } |
167 | } else { |
168 | av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2, |
169 | srcp, src_frame->linesize[plane] * 2, |
170 | cols, lines); |
171 | } |
172 | } |
173 | } |
174 | |
175 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
176 | { |
177 | AVFilterContext *ctx = inlink->dst; |
178 | AVFilterLink *outlink = ctx->outputs[0]; |
179 | InterlaceContext *s = ctx->priv; |
180 | AVFrame *out; |
181 | int tff, ret; |
182 | |
183 | av_frame_free(&s->cur); |
184 | s->cur = s->next; |
185 | s->next = buf; |
186 | |
187 | /* we need at least two frames */ |
188 | if (!s->cur || !s->next) |
189 | return 0; |
190 | |
191 | if (s->cur->interlaced_frame) { |
192 | av_log(ctx, AV_LOG_WARNING, |
193 | "video is already interlaced, adjusting framerate only\n"); |
194 | out = av_frame_clone(s->cur); |
195 | if (!out) |
196 | return AVERROR(ENOMEM); |
197 | out->pts /= 2; // adjust pts to new framerate |
198 | ret = ff_filter_frame(outlink, out); |
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, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass); |
214 | av_frame_free(&s->cur); |
215 | |
216 | /* copy lower/upper field from next */ |
217 | copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass); |
218 | av_frame_free(&s->next); |
219 | |
220 | ret = ff_filter_frame(outlink, out); |
221 | |
222 | return ret; |
223 | } |
224 | |
225 | static const AVFilterPad inputs[] = { |
226 | { |
227 | .name = "default", |
228 | .type = AVMEDIA_TYPE_VIDEO, |
229 | .filter_frame = filter_frame, |
230 | }, |
231 | { NULL } |
232 | }; |
233 | |
234 | static const AVFilterPad outputs[] = { |
235 | { |
236 | .name = "default", |
237 | .type = AVMEDIA_TYPE_VIDEO, |
238 | .config_props = config_out_props, |
239 | }, |
240 | { NULL } |
241 | }; |
242 | |
243 | AVFilter ff_vf_interlace = { |
244 | .name = "interlace", |
245 | .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."), |
246 | .uninit = uninit, |
247 | .priv_class = &interlace_class, |
248 | .priv_size = sizeof(InterlaceContext), |
249 | .query_formats = query_formats, |
250 | .inputs = inputs, |
251 | .outputs = outputs, |
252 | }; |
253 |