FFmpeg
 All Data Structures 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 int init(AVFilterContext *ctx, const char *args)
101 {
102  TInterlaceContext *tinterlace = ctx->priv;
103  static const char *shorthand[] = { "mode", NULL };
104 
105  tinterlace->class = &tinterlace_class;
106  av_opt_set_defaults(tinterlace);
107 
108  return av_opt_set_from_string(tinterlace, args, shorthand, "=", ":");
109 }
110 
111 static av_cold void uninit(AVFilterContext *ctx)
112 {
113  TInterlaceContext *tinterlace = ctx->priv;
114 
115  avfilter_unref_bufferp(&tinterlace->cur );
116  avfilter_unref_bufferp(&tinterlace->next);
117 
118  av_opt_free(tinterlace);
119  av_freep(&tinterlace->black_data[0]);
120 }
121 
122 static int config_out_props(AVFilterLink *outlink)
123 {
124  AVFilterContext *ctx = outlink->src;
125  AVFilterLink *inlink = outlink->src->inputs[0];
126  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
127  TInterlaceContext *tinterlace = ctx->priv;
128 
129  tinterlace->vsub = desc->log2_chroma_h;
130  outlink->w = inlink->w;
131  outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
132  inlink->h*2 : inlink->h;
133 
134  if (tinterlace->mode == MODE_PAD) {
135  uint8_t black[4] = { 16, 128, 128, 16 };
136  int i, ret;
138  black[0] = black[3] = 0;
139  ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
140  outlink->w, outlink->h, outlink->format, 1);
141  if (ret < 0)
142  return ret;
143 
144  /* fill black picture with black */
145  for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
146  int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
147  memset(tinterlace->black_data[i], black[i],
148  tinterlace->black_linesize[i] * h);
149  }
150  }
151  if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
152  && !(tinterlace->mode == MODE_INTERLEAVE_TOP
153  || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
154  av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
155  tinterlace->mode);
156  tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
157  }
158  av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
159  tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
160  inlink->h, outlink->h);
161 
162  return 0;
163 }
164 
165 #define FIELD_UPPER 0
166 #define FIELD_LOWER 1
167 #define FIELD_UPPER_AND_LOWER 2
168 
169 /**
170  * Copy picture field from src to dst.
171  *
172  * @param src_field copy from upper, lower field or both
173  * @param interleave leave a padding line between each copied line
174  * @param dst_field copy to upper or lower field,
175  * only meaningful when interleave is selected
176  * @param flags context flags
177  */
178 static inline
179 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
180  const uint8_t *src[4], int src_linesize[4],
181  enum AVPixelFormat format, int w, int src_h,
182  int src_field, int interleave, int dst_field,
183  int flags)
184 {
185  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
186  int plane, vsub = desc->log2_chroma_h;
187  int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
188  int h, i;
189 
190  for (plane = 0; plane < desc->nb_components; plane++) {
191  int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
192  int linesize = av_image_get_linesize(format, w, plane);
193  uint8_t *dstp = dst[plane];
194  const uint8_t *srcp = src[plane];
195 
196  if (linesize < 0)
197  return;
198 
199  lines /= k;
200  if (src_field == FIELD_LOWER)
201  srcp += src_linesize[plane];
202  if (interleave && dst_field == FIELD_LOWER)
203  dstp += dst_linesize[plane];
204  if (flags & TINTERLACE_FLAG_VLPF) {
205  // Low-pass filtering is required when creating an interlaced destination from
206  // a progressive source which contains high-frequency vertical detail.
207  // Filtering will reduce interlace 'twitter' and Moire patterning.
208  int srcp_linesize = src_linesize[plane] * k;
209  int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
210  for (h = lines; h > 0; h--) {
211  const uint8_t *srcp_above = srcp - src_linesize[plane];
212  const uint8_t *srcp_below = srcp + src_linesize[plane];
213  if (h == lines) srcp_above = srcp; // there is no line above
214  if (h == 1) srcp_below = srcp; // there is no line below
215  for (i = 0; i < linesize; i++) {
216  // this calculation is an integer representation of
217  // '0.5 * current + 0.25 * above + 0.25 + below'
218  // '1 +' is for rounding. */
219  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
220  }
221  dstp += dstp_linesize;
222  srcp += srcp_linesize;
223  }
224  } else {
225  av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
226  srcp, src_linesize[plane]*k, linesize, lines);
227  }
228  }
229 }
230 
231 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
232 {
233  AVFilterContext *ctx = inlink->dst;
234  AVFilterLink *outlink = ctx->outputs[0];
235  TInterlaceContext *tinterlace = ctx->priv;
236  AVFilterBufferRef *cur, *next, *out;
237  int field, tff, ret;
238 
239  avfilter_unref_buffer(tinterlace->cur);
240  tinterlace->cur = tinterlace->next;
241  tinterlace->next = picref;
242 
243  cur = tinterlace->cur;
244  next = tinterlace->next;
245  /* we need at least two frames */
246  if (!tinterlace->cur)
247  return 0;
248 
249  switch (tinterlace->mode) {
250  case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
251  * the lower field, generating a double-height video at half framerate */
252  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
253  if (!out)
254  return AVERROR(ENOMEM);
256  out->video->h = outlink->h;
257  out->video->interlaced = 1;
258  out->video->top_field_first = 1;
259 
260  /* write odd frame lines into the upper field of the new frame */
261  copy_picture_field(out->data, out->linesize,
262  (const uint8_t **)cur->data, cur->linesize,
263  inlink->format, inlink->w, inlink->h,
264  FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
265  /* write even frame lines into the lower field of the new frame */
266  copy_picture_field(out->data, out->linesize,
267  (const uint8_t **)next->data, next->linesize,
268  inlink->format, inlink->w, inlink->h,
269  FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
270  avfilter_unref_bufferp(&tinterlace->next);
271  break;
272 
273  case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
274  case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
275  out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
276  avfilter_unref_bufferp(&tinterlace->next);
277  break;
278 
279  case MODE_PAD: /* expand each frame to double height, but pad alternate
280  * lines with black; framerate unchanged */
281  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
283  out->video->h = outlink->h;
284 
285  field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
286  /* copy upper and lower fields */
287  copy_picture_field(out->data, out->linesize,
288  (const uint8_t **)cur->data, cur->linesize,
289  inlink->format, inlink->w, inlink->h,
290  FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
291  /* pad with black the other field */
292  copy_picture_field(out->data, out->linesize,
293  (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
294  inlink->format, inlink->w, inlink->h,
295  FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
296  break;
297 
298  /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
299  * halving the frame rate and preserving image height */
300  case MODE_INTERLEAVE_TOP: /* top field first */
301  case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
302  tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
303  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
304  if (!out)
305  return AVERROR(ENOMEM);
307  out->video->interlaced = 1;
308  out->video->top_field_first = tff;
309 
310  /* copy upper/lower field from cur */
311  copy_picture_field(out->data, out->linesize,
312  (const uint8_t **)cur->data, cur->linesize,
313  inlink->format, inlink->w, inlink->h,
314  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
315  tinterlace->flags);
316  /* copy lower/upper field from next */
317  copy_picture_field(out->data, out->linesize,
318  (const uint8_t **)next->data, next->linesize,
319  inlink->format, inlink->w, inlink->h,
320  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
321  tinterlace->flags);
322  avfilter_unref_bufferp(&tinterlace->next);
323  break;
324  case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
325  /* output current frame first */
326  out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
327  if (!out)
328  return AVERROR(ENOMEM);
329  out->video->interlaced = 1;
330 
331  if ((ret = ff_filter_frame(outlink, out)) < 0)
332  return ret;
333 
334  /* output mix of current and next frame */
335  tff = next->video->top_field_first;
336  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
337  if (!out)
338  return AVERROR(ENOMEM);
340  out->video->interlaced = 1;
341 
342  /* write current frame second field lines into the second field of the new frame */
343  copy_picture_field(out->data, out->linesize,
344  (const uint8_t **)cur->data, cur->linesize,
345  inlink->format, inlink->w, inlink->h,
346  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
347  tinterlace->flags);
348  /* write next frame first field lines into the first field of the new frame */
349  copy_picture_field(out->data, out->linesize,
350  (const uint8_t **)next->data, next->linesize,
351  inlink->format, inlink->w, inlink->h,
352  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
353  tinterlace->flags);
354  break;
355  default:
356  av_assert0(0);
357  }
358 
359  ret = ff_filter_frame(outlink, out);
360  tinterlace->frame++;
361 
362  return ret;
363 }
364 
365 static int request_frame(AVFilterLink *outlink)
366 {
367  TInterlaceContext *tinterlace = outlink->src->priv;
368  AVFilterLink *inlink = outlink->src->inputs[0];
369 
370  do {
371  int ret;
372 
373  if ((ret = ff_request_frame(inlink)) < 0)
374  return ret;
375  } while (!tinterlace->cur);
376 
377  return 0;
378 }
379 
380 static const AVFilterPad tinterlace_inputs[] = {
381  {
382  .name = "default",
383  .type = AVMEDIA_TYPE_VIDEO,
384  .filter_frame = filter_frame,
385  },
386  { NULL }
387 };
388 
389 static const AVFilterPad tinterlace_outputs[] = {
390  {
391  .name = "default",
392  .type = AVMEDIA_TYPE_VIDEO,
393  .config_props = config_out_props,
394  .request_frame = request_frame,
395  },
396  { NULL }
397 };
398 
400  .name = "tinterlace",
401  .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
402  .priv_size = sizeof(TInterlaceContext),
403  .init = init,
404  .uninit = uninit,
406  .inputs = tinterlace_inputs,
407  .outputs = tinterlace_outputs,
408  .priv_class = &tinterlace_class,
409 };