FFmpeg  4.4.9
vf_tinterlace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2010 Baptiste Coudurier
5  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.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  * temporal field interlace filter, ported from MPlayer/libmpcodecs
27  */
28 
29 #include "libavutil/opt.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/avassert.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "tinterlace.h"
35 
36 #define OFFSET(x) offsetof(TInterlaceContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38 
39 static const AVOption tinterlace_options[] = {
40  {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
41  {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
42  {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
43  {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
44  {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
45  {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
46  {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
47  {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
48  {"mergex2", "merge fields keeping same frame rate", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
49 
50  {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
51  {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
52  {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
53  {"complex_filter", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
54  {"cvlpf", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
55  {"exact_tb", "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
56  {"bypass_il", "bypass already interlaced frames", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_BYPASS_IL}, INT_MIN, INT_MAX, FLAGS, "flags" },
57 
58  {NULL}
59 };
60 
62 
63 static const AVOption interlace_options[] = {
64  { "scan", "scanning mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_TFF}, 0, 1, FLAGS, "mode"},
65  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
66  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_BFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
67  { "lowpass", "set vertical low-pass filter", OFFSET(lowpass), AV_OPT_TYPE_INT, {.i64 = VLPF_LIN}, 0, 2, FLAGS, "lowpass" },
68  { "off", "disable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
69  { "linear", "linear vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
70  { "complex", "complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
71 
72  { NULL }
73 };
74 
76 
77 #define FULL_SCALE_YUVJ_FORMATS \
78  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
79 
80 static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
82 };
83 
84 static const AVRational standard_tbs[] = {
85  {1, 25},
86  {1, 30},
87  {1001, 30000},
88 };
89 
91 {
92  static const enum AVPixelFormat pix_fmts[] = {
104  };
105 
107  if (!fmts_list)
108  return AVERROR(ENOMEM);
109  return ff_set_common_formats(ctx, fmts_list);
110 }
111 
112 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
113  ptrdiff_t mref, ptrdiff_t pref, int clip_max)
114 {
115  const uint8_t *srcp_above = srcp + mref;
116  const uint8_t *srcp_below = srcp + pref;
117  int i;
118  for (i = 0; i < width; i++) {
119  // this calculation is an integer representation of
120  // '0.5 * current + 0.25 * above + 0.25 * below'
121  // '1 +' is for rounding.
122  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
123  }
124 }
125 
126 static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
127  ptrdiff_t mref, ptrdiff_t pref, int clip_max)
128 {
129  uint16_t *dstp = (uint16_t *)dst8;
130  const uint16_t *srcp = (const uint16_t *)src8;
131  const uint16_t *srcp_above = srcp + mref / 2;
132  const uint16_t *srcp_below = srcp + pref / 2;
133  int i, src_x;
134  for (i = 0; i < width; i++) {
135  // this calculation is an integer representation of
136  // '0.5 * current + 0.25 * above + 0.25 * below'
137  // '1 +' is for rounding.
138  src_x = av_le2ne16(srcp[i]) << 1;
139  dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
140  + av_le2ne16(srcp_below[i])) >> 2);
141  }
142 }
143 
144 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
145  ptrdiff_t mref, ptrdiff_t pref, int clip_max)
146 {
147  const uint8_t *srcp_above = srcp + mref;
148  const uint8_t *srcp_below = srcp + pref;
149  const uint8_t *srcp_above2 = srcp + mref * 2;
150  const uint8_t *srcp_below2 = srcp + pref * 2;
151  int i, src_x, src_ab;
152  for (i = 0; i < width; i++) {
153  // this calculation is an integer representation of
154  // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
155  // '4 +' is for rounding.
156  src_x = srcp[i] << 1;
157  src_ab = srcp_above[i] + srcp_below[i];
158  dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
159  - srcp_above2[i] - srcp_below2[i]) >> 3);
160  // Prevent over-sharpening:
161  // dst must not exceed src when the average of above and below
162  // is less than src. And the other way around.
163  if (src_ab > src_x) {
164  if (dstp[i] < srcp[i])
165  dstp[i] = srcp[i];
166  } else if (dstp[i] > srcp[i])
167  dstp[i] = srcp[i];
168  }
169 }
170 
171 static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
172  ptrdiff_t mref, ptrdiff_t pref, int clip_max)
173 {
174  uint16_t *dstp = (uint16_t *)dst8;
175  const uint16_t *srcp = (const uint16_t *)src8;
176  const uint16_t *srcp_above = srcp + mref / 2;
177  const uint16_t *srcp_below = srcp + pref / 2;
178  const uint16_t *srcp_above2 = srcp + mref;
179  const uint16_t *srcp_below2 = srcp + pref;
180  int i, dst_le, src_le, src_x, src_ab;
181  for (i = 0; i < width; i++) {
182  // this calculation is an integer representation of
183  // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
184  // '4 +' is for rounding.
185  src_le = av_le2ne16(srcp[i]);
186  src_x = src_le << 1;
187  src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
188  dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
189  - av_le2ne16(srcp_above2[i])
190  - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
191  // Prevent over-sharpening:
192  // dst must not exceed src when the average of above and below
193  // is less than src. And the other way around.
194  if (src_ab > src_x) {
195  if (dst_le < src_le)
196  dstp[i] = av_le2ne16(src_le);
197  else
198  dstp[i] = av_le2ne16(dst_le);
199  } else if (dst_le > src_le) {
200  dstp[i] = av_le2ne16(src_le);
201  } else
202  dstp[i] = av_le2ne16(dst_le);
203  }
204 }
205 
207 {
208  TInterlaceContext *tinterlace = ctx->priv;
209 
210  av_frame_free(&tinterlace->cur );
211  av_frame_free(&tinterlace->next);
212  av_freep(&tinterlace->black_data[0]);
213 }
214 
215 static int config_out_props(AVFilterLink *outlink)
216 {
217  AVFilterContext *ctx = outlink->src;
218  AVFilterLink *inlink = outlink->src->inputs[0];
220  TInterlaceContext *tinterlace = ctx->priv;
221  int i;
222 
223  tinterlace->vsub = desc->log2_chroma_h;
224  outlink->w = inlink->w;
225  outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2?
226  inlink->h*2 : inlink->h;
227  if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2)
229  av_make_q(2, 1));
230 
231  if (tinterlace->mode == MODE_PAD) {
232  uint8_t black[4] = { 0, 0, 0, 16 };
233  int ret;
234  ret = ff_draw_init(&tinterlace->draw, outlink->format, 0);
235  if (ret < 0) {
236  av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
237  return ret;
238  }
239  ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
241  tinterlace->color.comp[0].u8[0] = 0;
242  ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
243  outlink->w, outlink->h, outlink->format, 16);
244  if (ret < 0)
245  return ret;
246 
247  ff_fill_rectangle(&tinterlace->draw, &tinterlace->color, tinterlace->black_data,
248  tinterlace->black_linesize, 0, 0, outlink->w, outlink->h);
249  }
250  if (tinterlace->flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)
251  && !(tinterlace->mode == MODE_INTERLEAVE_TOP
252  || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
253  av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n",
254  tinterlace->mode);
256  }
257  tinterlace->preout_time_base = inlink->time_base;
258  if (tinterlace->mode == MODE_INTERLACEX2) {
259  tinterlace->preout_time_base.den *= 2;
260  outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
261  outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
262  } else if (tinterlace->mode == MODE_MERGEX2) {
263  outlink->frame_rate = inlink->frame_rate;
264  outlink->time_base = inlink->time_base;
265  } else if (tinterlace->mode != MODE_PAD) {
266  outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
267  outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
268  }
269 
270  for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
271  if (!av_cmp_q(standard_tbs[i], outlink->time_base))
272  break;
273  }
274  if (i == FF_ARRAY_ELEMS(standard_tbs) ||
275  (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
276  outlink->time_base = tinterlace->preout_time_base;
277 
278  tinterlace->csp = av_pix_fmt_desc_get(outlink->format);
279  if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) {
280  if (tinterlace->csp->comp[0].depth > 8)
282  else
283  tinterlace->lowpass_line = lowpass_line_complex_c;
284  if (ARCH_X86)
285  ff_tinterlace_init_x86(tinterlace);
286  } else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
287  if (tinterlace->csp->comp[0].depth > 8)
288  tinterlace->lowpass_line = lowpass_line_c_16;
289  else
290  tinterlace->lowpass_line = lowpass_line_c;
291  if (ARCH_X86)
292  ff_tinterlace_init_x86(tinterlace);
293  }
294 
295  av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
296  (tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
297  (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off",
298  inlink->h, outlink->h);
299 
300  return 0;
301 }
302 
303 #define FIELD_UPPER 0
304 #define FIELD_LOWER 1
305 #define FIELD_UPPER_AND_LOWER 2
306 
307 /**
308  * Copy picture field from src to dst.
309  *
310  * @param src_field copy from upper, lower field or both
311  * @param interleave leave a padding line between each copied line
312  * @param dst_field copy to upper or lower field,
313  * only meaningful when interleave is selected
314  * @param flags context flags
315  */
316 static inline
318  uint8_t *dst[4], int dst_linesize[4],
319  const uint8_t *src[4], int src_linesize[4],
320  enum AVPixelFormat format, int w, int src_h,
321  int src_field, int interleave, int dst_field,
322  int flags)
323 {
325  int hsub = desc->log2_chroma_w;
326  int plane, vsub = desc->log2_chroma_h;
327  int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
328  int h;
329 
330  for (plane = 0; plane < desc->nb_components; plane++) {
331  int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
332  int cols = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT( w, hsub) : w;
333  uint8_t *dstp = dst[plane];
334  const uint8_t *srcp = src[plane];
335  int srcp_linesize = src_linesize[plane] * k;
336  int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
337  int clip_max = (1 << tinterlace->csp->comp[plane].depth) - 1;
338 
339  lines = (lines + (src_field == FIELD_UPPER)) / k;
340  if (src_field == FIELD_LOWER)
341  srcp += src_linesize[plane];
342  if (interleave && dst_field == FIELD_LOWER)
343  dstp += dst_linesize[plane];
344  // Low-pass filtering is required when creating an interlaced destination from
345  // a progressive source which contains high-frequency vertical detail.
346  // Filtering will reduce interlace 'twitter' and Moire patterning.
348  int x = !!(flags & TINTERLACE_FLAG_CVLPF);
349  for (h = lines; h > 0; h--) {
350  ptrdiff_t pref = src_linesize[plane];
351  ptrdiff_t mref = -pref;
352  if (h >= (lines - x)) mref = 0; // there is no line above
353  else if (h <= (1 + x)) pref = 0; // there is no line below
354 
355  tinterlace->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
356  dstp += dstp_linesize;
357  srcp += srcp_linesize;
358  }
359  } else {
360  if (tinterlace->csp->comp[plane].depth > 8)
361  cols *= 2;
362  av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
363  }
364  }
365 }
366 
367 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
368 {
369  AVFilterContext *ctx = inlink->dst;
370  AVFilterLink *outlink = ctx->outputs[0];
371  TInterlaceContext *tinterlace = ctx->priv;
372  AVFrame *cur, *next, *out;
373  int field, tff, ret;
374 
375  av_frame_free(&tinterlace->cur);
376  tinterlace->cur = tinterlace->next;
377  tinterlace->next = picref;
378 
379  cur = tinterlace->cur;
380  next = tinterlace->next;
381  /* we need at least two frames */
382  if (!tinterlace->cur)
383  return 0;
384 
385  switch (tinterlace->mode) {
386  case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
387  * the lower field, generating a double-height video at same framerate */
388  case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
389  * the lower field, generating a double-height video at half framerate */
390  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
391  if (!out)
392  return AVERROR(ENOMEM);
393  av_frame_copy_props(out, cur);
394  out->height = outlink->h;
395  out->interlaced_frame = 1;
396  out->top_field_first = 1;
397  out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
398 
399  /* write odd frame lines into the upper field of the new frame */
400  copy_picture_field(tinterlace, out->data, out->linesize,
401  (const uint8_t **)cur->data, cur->linesize,
402  inlink->format, inlink->w, inlink->h,
403  FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags);
404  /* write even frame lines into the lower field of the new frame */
405  copy_picture_field(tinterlace, out->data, out->linesize,
406  (const uint8_t **)next->data, next->linesize,
407  inlink->format, inlink->w, inlink->h,
408  FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags);
409  if (tinterlace->mode != MODE_MERGEX2)
410  av_frame_free(&tinterlace->next);
411  break;
412 
413  case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
414  case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
415  out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
416  if (!out)
417  return AVERROR(ENOMEM);
418  av_frame_free(&tinterlace->next);
419  break;
420 
421  case MODE_PAD: /* expand each frame to double height, but pad alternate
422  * lines with black; framerate unchanged */
423  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
424  if (!out)
425  return AVERROR(ENOMEM);
426  av_frame_copy_props(out, cur);
427  out->height = outlink->h;
428  out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
429 
430  field = (1 + outlink->frame_count_in) & 1 ? FIELD_UPPER : FIELD_LOWER;
431  /* copy upper and lower fields */
432  copy_picture_field(tinterlace, out->data, out->linesize,
433  (const uint8_t **)cur->data, cur->linesize,
434  inlink->format, inlink->w, inlink->h,
435  FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
436  /* pad with black the other field */
437  copy_picture_field(tinterlace, out->data, out->linesize,
438  (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
439  inlink->format, inlink->w, inlink->h,
440  FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
441  break;
442 
443  /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
444  * halving the frame rate and preserving image height */
445  case MODE_INTERLEAVE_TOP: /* top field first */
446  case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
447  if ((tinterlace->flags & TINTERLACE_FLAG_BYPASS_IL) && cur->interlaced_frame) {
449  "video is already interlaced, adjusting framerate only\n");
450  out = av_frame_clone(cur);
451  if (!out)
452  return AVERROR(ENOMEM);
453  out->pts /= 2; // adjust pts to new framerate
454  ret = ff_filter_frame(outlink, out);
455  return ret;
456  }
457  tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
458  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
459  if (!out)
460  return AVERROR(ENOMEM);
461  av_frame_copy_props(out, cur);
462  out->interlaced_frame = 1;
463  out->top_field_first = tff;
464 
465  /* copy upper/lower field from cur */
466  copy_picture_field(tinterlace, out->data, out->linesize,
467  (const uint8_t **)cur->data, cur->linesize,
468  inlink->format, inlink->w, inlink->h,
469  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
470  tinterlace->flags);
471  /* copy lower/upper field from next */
472  copy_picture_field(tinterlace, out->data, out->linesize,
473  (const uint8_t **)next->data, next->linesize,
474  inlink->format, inlink->w, inlink->h,
475  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
476  tinterlace->flags);
477  av_frame_free(&tinterlace->next);
478  break;
479  case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
480  /* output current frame first */
481  out = av_frame_clone(cur);
482  if (!out)
483  return AVERROR(ENOMEM);
484  out->interlaced_frame = 1;
485  if (cur->pts != AV_NOPTS_VALUE)
486  out->pts = cur->pts*2;
487 
488  out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
489  if ((ret = ff_filter_frame(outlink, out)) < 0)
490  return ret;
491 
492  /* output mix of current and next frame */
493  tff = next->top_field_first;
494  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
495  if (!out)
496  return AVERROR(ENOMEM);
497  av_frame_copy_props(out, next);
498  out->interlaced_frame = 1;
499  out->top_field_first = !tff;
500 
501  if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
502  out->pts = cur->pts + next->pts;
503  else
504  out->pts = AV_NOPTS_VALUE;
505  /* write current frame second field lines into the second field of the new frame */
506  copy_picture_field(tinterlace, out->data, out->linesize,
507  (const uint8_t **)cur->data, cur->linesize,
508  inlink->format, inlink->w, inlink->h,
509  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
510  tinterlace->flags);
511  /* write next frame first field lines into the first field of the new frame */
512  copy_picture_field(tinterlace, out->data, out->linesize,
513  (const uint8_t **)next->data, next->linesize,
514  inlink->format, inlink->w, inlink->h,
515  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
516  tinterlace->flags);
517  break;
518  default:
519  av_assert0(0);
520  }
521 
522  out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
523  ret = ff_filter_frame(outlink, out);
524 
525  return ret;
526 }
527 
529 {
530  TInterlaceContext *tinterlace = ctx->priv;
531 
532  if (tinterlace->mode <= MODE_BFF)
533  tinterlace->mode += MODE_INTERLEAVE_TOP;
534 
535  tinterlace->flags |= TINTERLACE_FLAG_BYPASS_IL;
536  if (tinterlace->lowpass == VLPF_LIN)
537  tinterlace->flags |= TINTERLACE_FLAG_VLPF;
538  if (tinterlace->lowpass == VLPF_CMP)
539  tinterlace->flags |= TINTERLACE_FLAG_CVLPF;
540 
541  return 0;
542 }
543 
544 static const AVFilterPad tinterlace_inputs[] = {
545  {
546  .name = "default",
547  .type = AVMEDIA_TYPE_VIDEO,
548  .filter_frame = filter_frame,
549  },
550  { NULL }
551 };
552 
553 static const AVFilterPad tinterlace_outputs[] = {
554  {
555  .name = "default",
556  .type = AVMEDIA_TYPE_VIDEO,
557  .config_props = config_out_props,
558  },
559  { NULL }
560 };
561 
563  .name = "tinterlace",
564  .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
565  .priv_size = sizeof(TInterlaceContext),
566  .uninit = uninit,
570  .priv_class = &tinterlace_class,
571 };
572 
573 
575  .name = "interlace",
576  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
577  .priv_size = sizeof(TInterlaceContext),
578  .init = init_interlace,
579  .uninit = uninit,
583  .priv_class = &interlace_class,
584 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static const char *const format[]
Definition: af_aiir.c:457
@ lowpass
Definition: af_biquads.c:81
#define av_cold
Definition: attributes.h:88
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
@ MODE_NB
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define av_le2ne16(x)
Definition: bswap.h:95
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip
Definition: common.h:122
#define av_clip_uint8
Definition: common.h:128
#define ARCH_X86
Definition: config.h:39
#define NULL
Definition: coverity.c:32
static av_always_inline void RENAME() interleave(TYPE *dst, TYPE *src0, TYPE *src1, int w2, int add, int shift)
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:84
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:144
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_x, int dst_y, int w, int h)
Fill a rectangle with an uniform color.
Definition: drawutils.c:231
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int ff_fmt_is_in(int fmt, const int *fmts)
Tell if an integer is contained in the provided -1-terminated list of integers.
Definition: formats.c:257
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:224
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:376
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:219
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
misc image utilities
int i
Definition: input.c:407
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUVA444P10LE
planar YUV 4:4:4 40bpp, (1 Cr & Cb sample per 1x1 Y & A samples, little-endian)
Definition: pixfmt.h:189
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVA420P10LE
planar YUV 4:2:0 25bpp, (1 Cr & Cb sample per 2x2 Y & A samples, little-endian)
Definition: pixfmt.h:185
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_YUVA422P10LE
planar YUV 4:2:2 30bpp, (1 Cr & Cb sample per 2x1 Y & A samples, little-endian)
Definition: pixfmt.h:187
@ AV_PIX_FMT_YUV420P10LE
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:159
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV422P12LE
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:247
@ AV_PIX_FMT_YUV440P12LE
planar YUV 4:4:0,24bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian
Definition: pixfmt.h:277
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_YUV420P12LE
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:243
@ AV_PIX_FMT_YUV422P10LE
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:161
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_YUV440P10LE
planar YUV 4:4:0,20bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian
Definition: pixfmt.h:275
@ AV_PIX_FMT_YUV444P10LE
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:165
@ AV_PIX_FMT_YUV444P12LE
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:251
#define FF_ARRAY_ELEMS(a)
int depth
Number of bits in the component.
Definition: pixdesc.h:58
An instance of a filter.
Definition: avfilter.h:341
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:406
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int den
Denominator.
Definition: rational.h:60
union FFDrawColor::@204 comp[MAX_PLANES]
uint8_t u8[16]
Definition: drawutils.h:54
AVFrame * cur
Definition: tinterlace.h:71
AVRational preout_time_base
Definition: tinterlace.h:67
int black_linesize[4]
Definition: tinterlace.h:74
const AVPixFmtDescriptor * csp
Definition: tinterlace.h:77
FFDrawColor color
Definition: tinterlace.h:76
void(* lowpass_line)(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp, ptrdiff_t mref, ptrdiff_t pref, int clip_max)
Definition: tinterlace.h:78
int flags
flags affecting interlacing algorithm
Definition: tinterlace.h:68
int lowpass
legacy interlace filter lowpass mode
Definition: tinterlace.h:69
AVFrame * next
Definition: tinterlace.h:72
int vsub
chroma vertical subsampling
Definition: tinterlace.h:70
int mode
TInterlaceMode, interlace mode selected.
Definition: tinterlace.h:66
FFDrawContext draw
Definition: tinterlace.h:75
uint8_t * black_data[4]
buffer used to fill padded lines
Definition: tinterlace.h:73
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define width
temporal field interlace filter, ported from MPlayer/libmpcodecs
#define TINTERLACE_FLAG_BYPASS_IL
Definition: tinterlace.h:39
#define TINTERLACE_FLAG_EXACT_TB
Definition: tinterlace.h:38
void ff_tinterlace_init_x86(TInterlaceContext *interlace)
#define TINTERLACE_FLAG_VLPF
Definition: tinterlace.h:36
@ MODE_MERGEX2
Definition: tinterlace.h:55
@ MODE_DROP_EVEN
Definition: tinterlace.h:49
@ MODE_INTERLEAVE_BOTTOM
Definition: tinterlace.h:53
@ MODE_INTERLACEX2
Definition: tinterlace.h:54
@ MODE_MERGE
Definition: tinterlace.h:48
@ MODE_DROP_ODD
Definition: tinterlace.h:50
@ MODE_PAD
Definition: tinterlace.h:51
@ MODE_INTERLEAVE_TOP
Definition: tinterlace.h:52
@ VLPF_OFF
Definition: tinterlace.h:42
@ VLPF_LIN
Definition: tinterlace.h:43
@ VLPF_CMP
Definition: tinterlace.h:44
#define TINTERLACE_FLAG_CVLPF
Definition: tinterlace.h:37
@ MODE_BFF
Definition: tinterlace.h:61
@ MODE_TFF
Definition: tinterlace.h:60
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
static enum AVPixelFormat full_scale_yuvj_pix_fmts[]
Definition: vf_tinterlace.c:80
static int init_interlace(AVFilterContext *ctx)
#define FULL_SCALE_YUVJ_FORMATS
Definition: vf_tinterlace.c:77
AVFilter ff_vf_tinterlace
static void copy_picture_field(TInterlaceContext *tinterlace, uint8_t *dst[4], int dst_linesize[4], const uint8_t *src[4], int src_linesize[4], enum AVPixelFormat format, int w, int src_h, int src_field, int interleave, int dst_field, int flags)
Copy picture field from src to dst.
#define FIELD_UPPER_AND_LOWER
static const AVOption tinterlace_options[]
Definition: vf_tinterlace.c:39
static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp, ptrdiff_t mref, ptrdiff_t pref, int clip_max)
static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp, ptrdiff_t mref, ptrdiff_t pref, int clip_max)
static const AVRational standard_tbs[]
Definition: vf_tinterlace.c:84
static const AVOption interlace_options[]
Definition: vf_tinterlace.c:63
static int query_formats(AVFilterContext *ctx)
Definition: vf_tinterlace.c:90
#define FLAGS
Definition: vf_tinterlace.c:37
static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8, ptrdiff_t mref, ptrdiff_t pref, int clip_max)
#define FIELD_LOWER
AVFilter ff_vf_interlace
#define FIELD_UPPER
static av_cold void uninit(AVFilterContext *ctx)
static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8, ptrdiff_t mref, ptrdiff_t pref, int clip_max)
static const AVFilterPad tinterlace_inputs[]
static const AVFilterPad tinterlace_outputs[]
#define OFFSET(x)
Definition: vf_tinterlace.c:36
AVFILTER_DEFINE_CLASS(tinterlace)
static int config_out_props(AVFilterLink *outlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104