FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_tinterlace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with FFmpeg if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 /**
24  * @file
25  * temporal field interlace filter, ported from MPlayer/libmpcodecs
26  */
27 
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avassert.h"
31 #include "avfilter.h"
32 #include "internal.h"
33 
43 };
44 
45 typedef struct {
46  const AVClass *class;
47  enum TInterlaceMode mode; ///< interlace mode selected
48  int flags; ///< flags affecting interlacing algorithm
49  int frame; ///< number of the output frame
50  int vsub; ///< chroma vertical subsampling
53  uint8_t *black_data[4]; ///< buffer used to fill padded lines
54  int black_linesize[4];
56 
57 #define OFFSET(x) offsetof(TInterlaceContext, x)
58 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59 #define TINTERLACE_FLAG_VLPF 01
60 
61 static const AVOption tinterlace_options[] = {
62  {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
63  {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
64  {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
65  {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
66  {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
67  {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
68  {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
69  {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
70 
71  {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
72  {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
73  {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
74 
75  {NULL}
76 };
77 
78 AVFILTER_DEFINE_CLASS(tinterlace);
79 
80 #define FULL_SCALE_YUVJ_FORMATS \
81  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
82 
85 };
86 
88 {
89  static const enum AVPixelFormat pix_fmts[] = {
94  };
95 
97  return 0;
98 }
99 
100 static av_cold void uninit(AVFilterContext *ctx)
101 {
102  TInterlaceContext *tinterlace = ctx->priv;
103 
104  av_frame_free(&tinterlace->cur );
105  av_frame_free(&tinterlace->next);
106  av_freep(&tinterlace->black_data[0]);
107 }
108 
109 static int config_out_props(AVFilterLink *outlink)
110 {
111  AVFilterContext *ctx = outlink->src;
112  AVFilterLink *inlink = outlink->src->inputs[0];
113  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
114  TInterlaceContext *tinterlace = ctx->priv;
115 
116  tinterlace->vsub = desc->log2_chroma_h;
117  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
118  outlink->w = inlink->w;
119  outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
120  inlink->h*2 : inlink->h;
121 
122  if (tinterlace->mode == MODE_PAD) {
123  uint8_t black[4] = { 16, 128, 128, 16 };
124  int i, ret;
126  black[0] = black[3] = 0;
127  ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
128  outlink->w, outlink->h, outlink->format, 1);
129  if (ret < 0)
130  return ret;
131 
132  /* fill black picture with black */
133  for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
134  int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
135  memset(tinterlace->black_data[i], black[i],
136  tinterlace->black_linesize[i] * h);
137  }
138  }
139  if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
140  && !(tinterlace->mode == MODE_INTERLEAVE_TOP
141  || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
142  av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
143  tinterlace->mode);
144  tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
145  }
146  av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
147  tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
148  inlink->h, outlink->h);
149 
150  return 0;
151 }
152 
153 #define FIELD_UPPER 0
154 #define FIELD_LOWER 1
155 #define FIELD_UPPER_AND_LOWER 2
156 
157 /**
158  * Copy picture field from src to dst.
159  *
160  * @param src_field copy from upper, lower field or both
161  * @param interleave leave a padding line between each copied line
162  * @param dst_field copy to upper or lower field,
163  * only meaningful when interleave is selected
164  * @param flags context flags
165  */
166 static inline
167 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
168  const uint8_t *src[4], int src_linesize[4],
169  enum AVPixelFormat format, int w, int src_h,
170  int src_field, int interleave, int dst_field,
171  int flags)
172 {
173  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
174  int plane, vsub = desc->log2_chroma_h;
175  int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
176  int h, i;
177 
178  for (plane = 0; plane < desc->nb_components; plane++) {
179  int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
180  int linesize = av_image_get_linesize(format, w, plane);
181  uint8_t *dstp = dst[plane];
182  const uint8_t *srcp = src[plane];
183 
184  if (linesize < 0)
185  return;
186 
187  lines = (lines + (src_field == FIELD_UPPER)) / k;
188  if (src_field == FIELD_LOWER)
189  srcp += src_linesize[plane];
190  if (interleave && dst_field == FIELD_LOWER)
191  dstp += dst_linesize[plane];
192  if (flags & TINTERLACE_FLAG_VLPF) {
193  // Low-pass filtering is required when creating an interlaced destination from
194  // a progressive source which contains high-frequency vertical detail.
195  // Filtering will reduce interlace 'twitter' and Moire patterning.
196  int srcp_linesize = src_linesize[plane] * k;
197  int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
198  for (h = lines; h > 0; h--) {
199  const uint8_t *srcp_above = srcp - src_linesize[plane];
200  const uint8_t *srcp_below = srcp + src_linesize[plane];
201  if (h == lines) srcp_above = srcp; // there is no line above
202  if (h == 1) srcp_below = srcp; // there is no line below
203  for (i = 0; i < linesize; i++) {
204  // this calculation is an integer representation of
205  // '0.5 * current + 0.25 * above + 0.25 * below'
206  // '1 +' is for rounding. */
207  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
208  }
209  dstp += dstp_linesize;
210  srcp += srcp_linesize;
211  }
212  } else {
213  av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
214  srcp, src_linesize[plane]*k, linesize, lines);
215  }
216  }
217 }
218 
219 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
220 {
221  AVFilterContext *ctx = inlink->dst;
222  AVFilterLink *outlink = ctx->outputs[0];
223  TInterlaceContext *tinterlace = ctx->priv;
224  AVFrame *cur, *next, *out;
225  int field, tff, ret;
226 
227  av_frame_free(&tinterlace->cur);
228  tinterlace->cur = tinterlace->next;
229  tinterlace->next = picref;
230 
231  cur = tinterlace->cur;
232  next = tinterlace->next;
233  /* we need at least two frames */
234  if (!tinterlace->cur)
235  return 0;
236 
237  switch (tinterlace->mode) {
238  case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
239  * the lower field, generating a double-height video at half framerate */
240  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
241  if (!out)
242  return AVERROR(ENOMEM);
243  av_frame_copy_props(out, cur);
244  out->height = outlink->h;
245  out->interlaced_frame = 1;
246  out->top_field_first = 1;
247 
248  /* write odd frame lines into the upper field of the new frame */
249  copy_picture_field(out->data, out->linesize,
250  (const uint8_t **)cur->data, cur->linesize,
251  inlink->format, inlink->w, inlink->h,
252  FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
253  /* write even frame lines into the lower field of the new frame */
254  copy_picture_field(out->data, out->linesize,
255  (const uint8_t **)next->data, next->linesize,
256  inlink->format, inlink->w, inlink->h,
257  FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
258  av_frame_free(&tinterlace->next);
259  break;
260 
261  case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
262  case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
263  out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
264  av_frame_free(&tinterlace->next);
265  break;
266 
267  case MODE_PAD: /* expand each frame to double height, but pad alternate
268  * lines with black; framerate unchanged */
269  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
270  if (!out)
271  return AVERROR(ENOMEM);
272  av_frame_copy_props(out, cur);
273  out->height = outlink->h;
274 
275  field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
276  /* copy upper and lower fields */
277  copy_picture_field(out->data, out->linesize,
278  (const uint8_t **)cur->data, cur->linesize,
279  inlink->format, inlink->w, inlink->h,
280  FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
281  /* pad with black the other field */
282  copy_picture_field(out->data, out->linesize,
283  (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
284  inlink->format, inlink->w, inlink->h,
285  FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
286  break;
287 
288  /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
289  * halving the frame rate and preserving image height */
290  case MODE_INTERLEAVE_TOP: /* top field first */
291  case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
292  tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
293  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
294  if (!out)
295  return AVERROR(ENOMEM);
296  av_frame_copy_props(out, cur);
297  out->interlaced_frame = 1;
298  out->top_field_first = tff;
299 
300  /* copy upper/lower field from cur */
301  copy_picture_field(out->data, out->linesize,
302  (const uint8_t **)cur->data, cur->linesize,
303  inlink->format, inlink->w, inlink->h,
304  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
305  tinterlace->flags);
306  /* copy lower/upper field from next */
307  copy_picture_field(out->data, out->linesize,
308  (const uint8_t **)next->data, next->linesize,
309  inlink->format, inlink->w, inlink->h,
310  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
311  tinterlace->flags);
312  av_frame_free(&tinterlace->next);
313  break;
314  case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
315  /* output current frame first */
316  out = av_frame_clone(cur);
317  if (!out)
318  return AVERROR(ENOMEM);
319  out->interlaced_frame = 1;
320 
321  if ((ret = ff_filter_frame(outlink, out)) < 0)
322  return ret;
323 
324  /* output mix of current and next frame */
325  tff = next->top_field_first;
326  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
327  if (!out)
328  return AVERROR(ENOMEM);
329  av_frame_copy_props(out, next);
330  out->interlaced_frame = 1;
331 
332  /* write current frame second field lines into the second field of the new frame */
333  copy_picture_field(out->data, out->linesize,
334  (const uint8_t **)cur->data, cur->linesize,
335  inlink->format, inlink->w, inlink->h,
336  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
337  tinterlace->flags);
338  /* write next frame first field lines into the first field of the new frame */
339  copy_picture_field(out->data, out->linesize,
340  (const uint8_t **)next->data, next->linesize,
341  inlink->format, inlink->w, inlink->h,
342  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
343  tinterlace->flags);
344  break;
345  default:
346  av_assert0(0);
347  }
348 
349  ret = ff_filter_frame(outlink, out);
350  tinterlace->frame++;
351 
352  return ret;
353 }
354 
355 static const AVFilterPad tinterlace_inputs[] = {
356  {
357  .name = "default",
358  .type = AVMEDIA_TYPE_VIDEO,
359  .filter_frame = filter_frame,
360  },
361  { NULL }
362 };
363 
364 static const AVFilterPad tinterlace_outputs[] = {
365  {
366  .name = "default",
367  .type = AVMEDIA_TYPE_VIDEO,
368  .config_props = config_out_props,
369  },
370  { NULL }
371 };
372 
374  .name = "tinterlace",
375  .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
376  .priv_size = sizeof(TInterlaceContext),
377  .uninit = uninit,
379  .inputs = tinterlace_inputs,
380  .outputs = tinterlace_outputs,
381  .priv_class = &tinterlace_class,
382 };