FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_interlace.c
Go to the documentation of this file.
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 {
47 };
48 
49 typedef struct InterlaceContext {
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
55 
56 #define OFFSET(x) offsetof(InterlaceContext, x)
57 #define V AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption interlace_options[] = {
59  { "scan", "scanning mode", OFFSET(scan),
60  AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
61  { "tff", "top field first", 0,
62  AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
63  { "bff", "bottom field first", 0,
64  AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
65  { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
66  AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
67  { NULL }
68 };
69 
70 AVFILTER_DEFINE_CLASS(interlace);
71 
72 static const enum AVPixelFormat formats_supported[] = {
77 };
78 
80 {
82  return 0;
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, "***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  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
117 
118  av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
119  s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
120 
121  return 0;
122 }
123 
124 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
125  AVFilterLink *inlink, enum FieldType field_type,
126  int lowpass)
127 {
128  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
129  int vsub = desc->log2_chroma_h;
130  int plane, i, j;
131 
132  for (plane = 0; plane < desc->nb_components; plane++) {
133  int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
134  int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
135  uint8_t *dstp = dst_frame->data[plane];
136  const uint8_t *srcp = src_frame->data[plane];
137 
138  av_assert0(linesize >= 0);
139 
140  lines = (lines + (field_type == FIELD_UPPER)) / 2;
141  if (field_type == FIELD_LOWER)
142  srcp += src_frame->linesize[plane];
143  if (field_type == FIELD_LOWER)
144  dstp += dst_frame->linesize[plane];
145  if (lowpass) {
146  int srcp_linesize = src_frame->linesize[plane] * 2;
147  int dstp_linesize = dst_frame->linesize[plane] * 2;
148  for (j = lines; j > 0; j--) {
149  const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
150  const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
151  if (j == lines)
152  srcp_above = srcp; // there is no line above
153  if (j == 1)
154  srcp_below = srcp; // there is no line below
155  for (i = 0; i < linesize; i++) {
156  // this calculation is an integer representation of
157  // '0.5 * current + 0.25 * above + 0.25 * below'
158  // '1 +' is for rounding.
159  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
160  }
161  dstp += dstp_linesize;
162  srcp += srcp_linesize;
163  }
164  } else {
165  av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
166  srcp, src_frame->linesize[plane] * 2,
167  linesize, lines);
168  }
169  }
170 }
171 
172 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
173 {
174  AVFilterContext *ctx = inlink->dst;
175  AVFilterLink *outlink = ctx->outputs[0];
176  InterlaceContext *s = ctx->priv;
177  AVFrame *out;
178  int tff, ret;
179 
180  av_frame_free(&s->cur);
181  s->cur = s->next;
182  s->next = buf;
183 
184  /* we need at least two frames */
185  if (!s->cur || !s->next)
186  return 0;
187 
188  if (s->cur->interlaced_frame) {
189  av_log(ctx, AV_LOG_WARNING,
190  "video is already interlaced, adjusting framerate only\n");
191  out = av_frame_clone(s->cur);
192  if (!out)
193  return AVERROR(ENOMEM);
194  out->pts /= 2; // adjust pts to new framerate
195  ret = ff_filter_frame(outlink, out);
196  return ret;
197  }
198 
199  tff = (s->scan == MODE_TFF);
200  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
201  if (!out)
202  return AVERROR(ENOMEM);
203 
204  av_frame_copy_props(out, s->cur);
205  out->interlaced_frame = 1;
206  out->top_field_first = tff;
207  out->pts /= 2; // adjust pts to new framerate
208 
209  /* copy upper/lower field from cur */
210  copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
211  av_frame_free(&s->cur);
212 
213  /* copy lower/upper field from next */
214  copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
215  av_frame_free(&s->next);
216 
217  ret = ff_filter_frame(outlink, out);
218 
219  return ret;
220 }
221 
222 static const AVFilterPad inputs[] = {
223  {
224  .name = "default",
225  .type = AVMEDIA_TYPE_VIDEO,
226  .filter_frame = filter_frame,
227  },
228  { NULL }
229 };
230 
231 static const AVFilterPad outputs[] = {
232  {
233  .name = "default",
234  .type = AVMEDIA_TYPE_VIDEO,
235  .config_props = config_out_props,
236  },
237  { NULL }
238 };
239 
241  .name = "interlace",
242  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
243  .uninit = uninit,
244  .priv_class = &interlace_class,
245  .priv_size = sizeof(InterlaceContext),
247  .inputs = inputs,
248  .outputs = outputs,
249 };