FFmpeg  4.4.9
vf_fieldmatch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Fredrik Mellbin
3  * Copyright (c) 2013 Clément Bœsch
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Fieldmatching filter, ported from VFM filter (VapourSynth) by Clément.
25  * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
26  * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone
27  * (tritical), the original author.
28  *
29  * @see http://bengal.missouri.edu/~kes25c/
30  * @see http://www.vapoursynth.com/about/
31  */
32 
33 #include <inttypes.h>
34 
35 #include "libavutil/avassert.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/timestamp.h"
40 #include "avfilter.h"
41 #include "filters.h"
42 #include "internal.h"
43 
44 #define INPUT_MAIN 0
45 #define INPUT_CLEANSRC 1
46 
51 };
52 
60  NB_MODE
61 };
62 
68 };
69 
70 enum comb_dbg {
75 };
76 
77 typedef struct FieldMatchContext {
78  const AVClass *class;
79 
80  AVFrame *prv, *src, *nxt; ///< main sliding window of 3 frames
81  AVFrame *prv2, *src2, *nxt2; ///< sliding window of the optional second stream
82  int got_frame[2]; ///< frame request flag for each input stream
83  int hsub, vsub; ///< chroma subsampling values
84  int bpc; ///< bytes per component
85  uint32_t eof; ///< bitmask for end of stream
88 
89  /* options */
90  int order;
91  int ppsrc;
92  int mode; ///< matching_mode
93  int field;
94  int mchroma;
95  int y0, y1;
97  double scthresh_flt;
98  int combmatch; ///< comb_matching_mode
99  int combdbg;
100  int cthresh;
101  int chroma;
103  int combpel;
104 
105  /* misc buffers */
107  int map_linesize[4];
110  int *c_array;
114 
115 #define OFFSET(x) offsetof(FieldMatchContext, x)
116 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
117 
118 static const AVOption fieldmatch_options[] = {
119  { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" },
120  { "auto", "auto detect parity", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "order" },
121  { "bff", "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "order" },
122  { "tff", "assume top field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "order" },
123  { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC_N}, MODE_PC, NB_MODE-1, FLAGS, "mode" },
124  { "pc", "2-way match (p/c)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC}, INT_MIN, INT_MAX, FLAGS, "mode" },
125  { "pc_n", "2-way match + 3rd match on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N}, INT_MIN, INT_MAX, FLAGS, "mode" },
126  { "pc_u", "2-way match + 3rd match (same order) on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U}, INT_MIN, INT_MAX, FLAGS, "mode" },
127  { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, "mode" },
128  { "pcn", "3-way match (p/c/n)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN}, INT_MIN, INT_MAX, FLAGS, "mode" },
129  { "pcn_ub", "3-way match + 4th/5th matches on combed (p/c/n + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB}, INT_MIN, INT_MAX, FLAGS, "mode" },
130  { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
131  { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" },
132  { "auto", "automatic (same value as 'order')", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "field" },
133  { "bottom", "bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field" },
134  { "top", "top field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "field" },
135  { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
136  { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
137  { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
138  { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS },
139  { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, "combmatching" },
140  { "none", "disable combmatching", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
141  { "sc", "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
142  { "full", "enable combmatching all the time", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
143  { "combdbg", "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, "dbglvl" },
144  { "none", "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
145  { "pcn", "calculate p/c/n", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
146  { "pcnub", "calculate p/c/n/u/b", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
147  { "cthresh", "set the area combing threshold used for combed frame detection", OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS },
148  { "chroma", "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma), AV_OPT_TYPE_BOOL,{.i64= 0}, 0, 1, FLAGS },
149  { "blockx", "set the x-axis size of the window used during combed frame detection", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
150  { "blocky", "set the y-axis size of the window used during combed frame detection", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
151  { "combpel", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(combpel), AV_OPT_TYPE_INT, {.i64=80}, 0, INT_MAX, FLAGS },
152  { NULL }
153 };
154 
156 
157 static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
158 {
159  return plane ? AV_CEIL_RSHIFT(f->width, fm->hsub) : f->width;
160 }
161 
162 static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
163 {
164  return plane ? AV_CEIL_RSHIFT(f->height, fm->vsub) : f->height;
165 }
166 
167 static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
168 {
169  int x, y;
170  const uint8_t *srcp1 = f1->data[0];
171  const uint8_t *srcp2 = f2->data[0];
172  const int src1_linesize = f1->linesize[0];
173  const int src2_linesize = f2->linesize[0];
174  const int width = f1->width;
175  const int height = f1->height;
176  int64_t acc = 0;
177 
178  for (y = 0; y < height; y++) {
179  for (x = 0; x < width; x++)
180  acc += abs(srcp1[x] - srcp2[x]);
181  srcp1 += src1_linesize;
182  srcp2 += src2_linesize;
183  }
184  return acc;
185 }
186 
187 static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
188 {
189  int y;
190 
191  for (y = 0; y < h; y++) {
192  memset(data, v, w);
193  data += linesize;
194  }
195 }
196 
198  int hsub, int vsub)
199 {
200  const uint8_t *cmkpU = fm->cmask_data[1];
201  const uint8_t *cmkpV = fm->cmask_data[2];
202  const int width = AV_CEIL_RSHIFT(src->width, hsub);
203  const int height = AV_CEIL_RSHIFT(src->height, vsub);
204  const ptrdiff_t cmk_linesize = fm->cmask_linesize[0];
205  const ptrdiff_t cmk_linesizeUV = fm->cmask_linesize[2];
206 
207  for (int y = 1; y < height - 1; y++) {
208  uint8_t *cmkp_row = fm->cmask_data[0] + ((y << vsub) - (vsub ? y & 1 : 0)) * cmk_linesize;
209  cmkpV += cmk_linesizeUV;
210  cmkpU += cmk_linesizeUV;
211  for (int x = 1; x < width - 1; x++) {
212 #define HAS_FF_AROUND(p, lz) (p[(x)-1 - (lz)] == 0xff || p[(x) - (lz)] == 0xff || p[(x)+1 - (lz)] == 0xff || \
213  p[(x)-1 ] == 0xff || p[(x)+1 ] == 0xff || \
214  p[(x)-1 + (lz)] == 0xff || p[(x) + (lz)] == 0xff || p[(x)+1 + (lz)] == 0xff)
215  if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
216  (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
217  uint8_t *cmkp = cmkp_row + (x << hsub);
218  for (int i = 0; i < (1 << vsub) + !!vsub; i++, cmkp += cmk_linesize) {
219  if (hsub == 2)
220  AV_WN32A(cmkp, 0xffffffff);
221  else if (hsub)
222  AV_WN16A(cmkp, 0xffff);
223  else
224  *cmkp = 0xff;
225  }
226  }
227  }
228  }
229 }
230 
231 static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
232 {
233  int x, y, plane, max_v = 0;
234  const int cthresh = fm->cthresh;
235  const int cthresh6 = cthresh * 6;
236 
237  for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
238  const uint8_t *srcp = src->data[plane];
239  const int src_linesize = src->linesize[plane];
240  const int width = get_width (fm, src, plane);
241  const int height = get_height(fm, src, plane);
242  uint8_t *cmkp = fm->cmask_data[plane];
243  const int cmk_linesize = fm->cmask_linesize[plane];
244 
245  if (cthresh < 0) {
246  fill_buf(cmkp, width, height, cmk_linesize, 0xff);
247  continue;
248  }
249  fill_buf(cmkp, width, height, cmk_linesize, 0);
250 
251  /* [1 -3 4 -3 1] vertical filter */
252 #define FILTER(xm2, xm1, xp1, xp2) \
253  abs( 4 * srcp[x] \
254  -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
255  + (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
256 
257  /* first line */
258  for (x = 0; x < width; x++) {
259  const int s1 = abs(srcp[x] - srcp[x + src_linesize]);
260  if (s1 > cthresh && FILTER(2, 1, 1, 2))
261  cmkp[x] = 0xff;
262  }
263  srcp += src_linesize;
264  cmkp += cmk_linesize;
265 
266  /* second line */
267  for (x = 0; x < width; x++) {
268  const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
269  const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
270  if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2))
271  cmkp[x] = 0xff;
272  }
273  srcp += src_linesize;
274  cmkp += cmk_linesize;
275 
276  /* all lines minus first two and last two */
277  for (y = 2; y < height-2; y++) {
278  for (x = 0; x < width; x++) {
279  const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
280  const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
281  if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2))
282  cmkp[x] = 0xff;
283  }
284  srcp += src_linesize;
285  cmkp += cmk_linesize;
286  }
287 
288  /* before-last line */
289  for (x = 0; x < width; x++) {
290  const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
291  const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
292  if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2))
293  cmkp[x] = 0xff;
294  }
295  srcp += src_linesize;
296  cmkp += cmk_linesize;
297 
298  /* last line */
299  for (x = 0; x < width; x++) {
300  const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
301  if (s1 > cthresh && FILTER(-2, -1, -1, -2))
302  cmkp[x] = 0xff;
303  }
304  }
305 
306  if (fm->chroma) {
307  const int hsub = fm->hsub;
308  const int vsub = fm->vsub;
309  if (hsub == 0 && vsub == 0) mark_combed_chroma(fm, src, 0, 0);
310  else if (hsub == 1 && vsub == 0) mark_combed_chroma(fm, src, 1, 0);
311  else if (hsub == 1 && vsub == 1) mark_combed_chroma(fm, src, 1, 1);
312  else if (hsub == 2 && vsub == 0) mark_combed_chroma(fm, src, 2, 0);
313  else mark_combed_chroma(fm, src, hsub, vsub);
314  }
315 
316  {
317  const int blockx = fm->blockx;
318  const int blocky = fm->blocky;
319  const int xhalf = blockx/2;
320  const int yhalf = blocky/2;
321  const int cmk_linesize = fm->cmask_linesize[0];
322  const uint8_t *cmkp = fm->cmask_data[0] + cmk_linesize;
323  const int width = src->width;
324  const int height = src->height;
325  const int xblocks = ((width+xhalf)/blockx) + 1;
326  const int xblocks4 = xblocks<<2;
327  const int yblocks = ((height+yhalf)/blocky) + 1;
328  int *c_array = fm->c_array;
329  const int arraysize = (xblocks*yblocks)<<2;
330  int heighta = (height/(blocky/2))*(blocky/2);
331  const int widtha = (width /(blockx/2))*(blockx/2);
332  if (heighta == height)
333  heighta = height - yhalf;
334  memset(c_array, 0, arraysize * sizeof(*c_array));
335 
336 #define C_ARRAY_ADD(v) do { \
337  const int box1 = (x / blockx) * 4; \
338  const int box2 = ((x + xhalf) / blockx) * 4; \
339  c_array[temp1 + box1 ] += v; \
340  c_array[temp1 + box2 + 1] += v; \
341  c_array[temp2 + box1 + 2] += v; \
342  c_array[temp2 + box2 + 3] += v; \
343 } while (0)
344 
345 #define VERTICAL_HALF(y_start, y_end) do { \
346  for (y = y_start; y < y_end; y++) { \
347  const int temp1 = (y / blocky) * xblocks4; \
348  const int temp2 = ((y + yhalf) / blocky) * xblocks4; \
349  for (x = 0; x < width; x++) \
350  if (cmkp[x - cmk_linesize] == 0xff && \
351  cmkp[x ] == 0xff && \
352  cmkp[x + cmk_linesize] == 0xff) \
353  C_ARRAY_ADD(1); \
354  cmkp += cmk_linesize; \
355  } \
356 } while (0)
357 
358  VERTICAL_HALF(1, yhalf);
359 
360  for (y = yhalf; y < heighta; y += yhalf) {
361  const int temp1 = (y / blocky) * xblocks4;
362  const int temp2 = ((y + yhalf) / blocky) * xblocks4;
363 
364  for (x = 0; x < widtha; x += xhalf) {
365  const uint8_t *cmkp_tmp = cmkp + x;
366  int u, v, sum = 0;
367  for (u = 0; u < yhalf; u++) {
368  for (v = 0; v < xhalf; v++)
369  if (cmkp_tmp[v - cmk_linesize] == 0xff &&
370  cmkp_tmp[v ] == 0xff &&
371  cmkp_tmp[v + cmk_linesize] == 0xff)
372  sum++;
373  cmkp_tmp += cmk_linesize;
374  }
375  if (sum)
376  C_ARRAY_ADD(sum);
377  }
378 
379  for (x = widtha; x < width; x++) {
380  const uint8_t *cmkp_tmp = cmkp + x;
381  int u, sum = 0;
382  for (u = 0; u < yhalf; u++) {
383  if (cmkp_tmp[-cmk_linesize] == 0xff &&
384  cmkp_tmp[ 0] == 0xff &&
385  cmkp_tmp[ cmk_linesize] == 0xff)
386  sum++;
387  cmkp_tmp += cmk_linesize;
388  }
389  if (sum)
390  C_ARRAY_ADD(sum);
391  }
392 
393  cmkp += cmk_linesize * yhalf;
394  }
395 
396  VERTICAL_HALF(heighta, height - 1);
397 
398  for (x = 0; x < arraysize; x++)
399  if (c_array[x] > max_v)
400  max_v = c_array[x];
401  }
402  return max_v;
403 }
404 
405 // the secret is that tbuffer is an interlaced, offset subset of all the lines
406 static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
407  const uint8_t *nxtp, int nxt_linesize,
408  uint8_t *tbuffer, int tbuf_linesize,
409  int width, int height)
410 {
411  int y, x;
412 
413  prvp -= prv_linesize;
414  nxtp -= nxt_linesize;
415  for (y = 0; y < height; y++) {
416  for (x = 0; x < width; x++)
417  tbuffer[x] = FFABS(prvp[x] - nxtp[x]);
418  prvp += prv_linesize;
419  nxtp += nxt_linesize;
420  tbuffer += tbuf_linesize;
421  }
422 }
423 
424 /**
425  * Build a map over which pixels differ a lot/a little
426  */
428  const uint8_t *prvp, int prv_linesize,
429  const uint8_t *nxtp, int nxt_linesize,
430  uint8_t *dstp, int dst_linesize, int height,
431  int width, int plane)
432 {
433  int x, y, u, diff, count;
434  int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
435  const uint8_t *dp = fm->tbuffer + tpitch;
436 
437  build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
438  fm->tbuffer, tpitch, width, height>>1);
439 
440  for (y = 2; y < height - 2; y += 2) {
441  for (x = 1; x < width - 1; x++) {
442  diff = dp[x];
443  if (diff > 3) {
444  for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
445  count += dp[u-tpitch] > 3;
446  count += dp[u ] > 3;
447  count += dp[u+tpitch] > 3;
448  }
449  if (count > 1) {
450  dstp[x] = 1;
451  if (diff > 19) {
452  int upper = 0, lower = 0;
453  for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
454  if (dp[u-tpitch] > 19) { count++; upper = 1; }
455  if (dp[u ] > 19) count++;
456  if (dp[u+tpitch] > 19) { count++; lower = 1; }
457  }
458  if (count > 3) {
459  if (upper && lower) {
460  dstp[x] |= 1<<1;
461  } else {
462  int upper2 = 0, lower2 = 0;
463  for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
464  if (y != 2 && dp[u-2*tpitch] > 19) upper2 = 1;
465  if ( dp[u- tpitch] > 19) upper = 1;
466  if ( dp[u+ tpitch] > 19) lower = 1;
467  if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
468  }
469  if ((upper && (lower || upper2)) ||
470  (lower && (upper || lower2)))
471  dstp[x] |= 1<<1;
472  else if (count > 5)
473  dstp[x] |= 1<<2;
474  }
475  }
476  }
477  }
478  }
479  }
480  dp += tpitch;
481  dstp += dst_linesize;
482  }
483 }
484 
485 enum { mP, mC, mN, mB, mU };
486 
487 static int get_field_base(int match, int field)
488 {
489  return match < 3 ? 2 - field : 1 + field;
490 }
491 
492 static AVFrame *select_frame(FieldMatchContext *fm, int match)
493 {
494  if (match == mP || match == mB) return fm->prv;
495  else if (match == mN || match == mU) return fm->nxt;
496  else /* match == mC */ return fm->src;
497 }
498 
499 static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
500 {
501  int plane, ret;
502  uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
503  uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
504  int norm1, norm2, mtn1, mtn2;
505  float c1, c2, mr;
506  const AVFrame *src = fm->src;
507 
508  for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
509  int x, y, temp1, temp2, fbase, stopy;
510  const AVFrame *prev, *next;
511  uint8_t *mapp = fm->map_data[plane];
512  int map_linesize = fm->map_linesize[plane];
513  const uint8_t *srcp = src->data[plane];
514  const int src_linesize = src->linesize[plane];
515  const int srcf_linesize = src_linesize << 1;
516  int prv_linesize, nxt_linesize;
517  int prvf_linesize, nxtf_linesize;
518  const int width = get_width (fm, src, plane);
519  const int height = get_height(fm, src, plane);
520  const int y0a = fm->y0 >> (plane ? fm->vsub : 0);
521  const int y1a = fm->y1 >> (plane ? fm->vsub : 0);
522  const int startx = (plane == 0 ? 8 : 8 >> fm->hsub);
523  const int stopx = width - startx;
524  const uint8_t *srcpf, *srcf, *srcnf;
525  const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
526 
527  fill_buf(mapp, width, height, map_linesize, 0);
528 
529  /* match1 */
530  fbase = get_field_base(match1, field);
531  srcf = srcp + (fbase + 1) * src_linesize;
532  stopy = height - 1 - fbase;
533  srcpf = srcf - srcf_linesize;
534  srcnf = srcf + srcf_linesize;
535  mapp = mapp + fbase * map_linesize;
536  prev = select_frame(fm, match1);
537  prv_linesize = prev->linesize[plane];
538  prvf_linesize = prv_linesize << 1;
539  prvpf = prev->data[plane] + fbase * prv_linesize; // previous frame, previous field
540  prvnf = prvpf + prvf_linesize; // previous frame, next field
541 
542  /* match2 */
543  fbase = get_field_base(match2, field);
544  next = select_frame(fm, match2);
545  nxt_linesize = next->linesize[plane];
546  nxtf_linesize = nxt_linesize << 1;
547  nxtpf = next->data[plane] + fbase * nxt_linesize; // next frame, previous field
548  nxtnf = nxtpf + nxtf_linesize; // next frame, next field
549 
550  map_linesize <<= 1;
551  if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
552  build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
553  mapp, map_linesize, height, width, plane);
554  else
555  build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
556  mapp + map_linesize, map_linesize, height, width, plane);
557 
558  for (y = 2; y < stopy; y += 2) {
559  if (y0a == y1a || y < y0a || y > y1a) {
560  for (x = startx; x < stopx; x++) {
561  if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
562  temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
563 
564  temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
565  if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
566  accumPc += temp2;
567  if (temp2 > 42) {
568  if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
569  accumPm += temp2;
570  if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
571  accumPml += temp2;
572  }
573 
574  temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
575  if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
576  accumNc += temp2;
577  if (temp2 > 42) {
578  if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
579  accumNm += temp2;
580  if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
581  accumNml += temp2;
582  }
583  }
584  }
585  }
586  prvpf += prvf_linesize;
587  prvnf += prvf_linesize;
588  srcpf += srcf_linesize;
589  srcf += srcf_linesize;
590  srcnf += srcf_linesize;
591  nxtpf += nxtf_linesize;
592  nxtnf += nxtf_linesize;
593  mapp += map_linesize;
594  }
595  }
596 
597  if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
598  FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
599  accumPm = accumPml;
600  accumNm = accumNml;
601  }
602 
603  norm1 = (int)((accumPc / 6.0f) + 0.5f);
604  norm2 = (int)((accumNc / 6.0f) + 0.5f);
605  mtn1 = (int)((accumPm / 6.0f) + 0.5f);
606  mtn2 = (int)((accumNm / 6.0f) + 0.5f);
607  c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
608  c2 = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
609  mr = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMAX(norm1,norm2),1));
610  if (((mtn1 >= 500 || mtn2 >= 500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
611  ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
612  ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
613  ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
614  ret = mtn1 > mtn2 ? match2 : match1;
615  else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
616  ret = mtn1 > mtn2 ? match2 : match1;
617  else
618  ret = norm1 > norm2 ? match2 : match1;
619  return ret;
620 }
621 
622 static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
623  const AVFrame *src, int field)
624 {
625  int plane;
626  for (plane = 0; plane < 4 && src->data[plane] && src->linesize[plane]; plane++) {
627  const int plane_h = get_height(fm, src, plane);
628  const int nb_copy_fields = (plane_h >> 1) + (field ? 0 : (plane_h & 1));
629  av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1,
630  src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1,
631  get_width(fm, src, plane) * fm->bpc, nb_copy_fields);
632  }
633 }
634 
635 static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
636  const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
637 {
638  AVFrame *dst;
639  FieldMatchContext *fm = ctx->priv;
640 
641  if (match == mC) {
642  dst = av_frame_clone(src);
643  } else {
644  AVFilterLink *outlink = ctx->outputs[0];
645 
646  dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
647  if (!dst)
648  return NULL;
649  av_frame_copy_props(dst, src);
650 
651  switch (match) {
652  case mP: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv, field); break;
653  case mN: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt, field); break;
654  case mB: copy_fields(fm, dst, src, field); copy_fields(fm, dst, prv, 1-field); break;
655  case mU: copy_fields(fm, dst, src, field); copy_fields(fm, dst, nxt, 1-field); break;
656  default: av_assert0(0);
657  }
658  }
659  return dst;
660 }
661 
662 static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2,
663  AVFrame **gen_frames, int field)
664 {
665  const FieldMatchContext *fm = ctx->priv;
666 
667 #define LOAD_COMB(mid) do { \
668  if (combs[mid] < 0) { \
669  if (!gen_frames[mid]) \
670  gen_frames[mid] = create_weave_frame(ctx, mid, field, \
671  fm->prv, fm->src, fm->nxt); \
672  combs[mid] = calc_combed_score(fm, gen_frames[mid]); \
673  } \
674 } while (0)
675 
676  LOAD_COMB(m1);
677  LOAD_COMB(m2);
678 
679  if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) &&
680  abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel)
681  return m2;
682  else
683  return m1;
684 }
685 
686 static const int fxo0m[] = { mP, mC, mN, mB, mU };
687 static const int fxo1m[] = { mN, mC, mP, mU, mB };
688 
689 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
690 {
691  AVFilterContext *ctx = inlink->dst;
692  AVFilterLink *outlink = ctx->outputs[0];
693  FieldMatchContext *fm = ctx->priv;
694  int combs[] = { -1, -1, -1, -1, -1 };
695  int order, field, i, match, sc = 0, ret = 0;
696  const int *fxo;
697  AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
698  AVFrame *dst = NULL;
699 
700  /* update frames queue(s) */
701 #define SLIDING_FRAME_WINDOW(prv, src, nxt) do { \
702  if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
703  av_frame_free(&prv); \
704  prv = src; \
705  src = nxt; \
706  if (in) \
707  nxt = in; \
708  if (!prv) \
709  prv = src; \
710  if (!prv) /* received only one frame at that point */ \
711  return 0; \
712  av_assert0(prv && src && nxt); \
713 } while (0)
714  if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
715  av_assert0(fm->got_frame[INPUT_MAIN] == 0);
716  SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
717  fm->got_frame[INPUT_MAIN] = 1;
718  } else {
720  SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
721  fm->got_frame[INPUT_CLEANSRC] = 1;
722  }
723  if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
724  return 0;
726  in = fm->src;
727 
728  /* parity */
729  order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1);
730  field = fm->field != FM_PARITY_AUTO ? fm->field : order;
731  av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
732  fxo = field ^ order ? fxo1m : fxo0m;
733 
734  /* debug mode: we generate all the fields combinations and their associated
735  * combed score. XXX: inject as frame metadata? */
736  if (fm->combdbg) {
737  for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) {
738  if (i > mN && fm->combdbg == COMBDBG_PCN)
739  break;
740  gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt);
741  if (!gen_frames[i]) {
742  ret = AVERROR(ENOMEM);
743  goto fail;
744  }
745  combs[i] = calc_combed_score(fm, gen_frames[i]);
746  }
747  av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n",
748  combs[0], combs[1], combs[2], combs[3], combs[4]);
749  } else {
750  gen_frames[mC] = av_frame_clone(fm->src);
751  if (!gen_frames[mC]) {
752  ret = AVERROR(ENOMEM);
753  goto fail;
754  }
755  }
756 
757  /* p/c selection and optional 3-way p/c/n matches */
758  match = compare_fields(fm, fxo[mC], fxo[mP], field);
759  if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
760  match = compare_fields(fm, match, fxo[mN], field);
761 
762  /* scene change check */
763  if (fm->combmatch == COMBMATCH_SC) {
764  if (fm->lastn == outlink->frame_count_in - 1) {
765  if (fm->lastscdiff > fm->scthresh)
766  sc = 1;
767  } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) {
768  sc = 1;
769  }
770 
771  if (!sc) {
772  fm->lastn = outlink->frame_count_in;
773  fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt);
774  sc = fm->lastscdiff > fm->scthresh;
775  }
776  }
777 
778  if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) {
779  switch (fm->mode) {
780  /* 2-way p/c matches */
781  case MODE_PC:
782  match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
783  break;
784  case MODE_PC_N:
785  match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
786  break;
787  case MODE_PC_U:
788  match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
789  break;
790  case MODE_PC_N_UB:
791  match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
792  match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
793  match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
794  break;
795  /* 3-way p/c/n matches */
796  case MODE_PCN:
797  match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
798  break;
799  case MODE_PCN_UB:
800  match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
801  match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
802  break;
803  default:
804  av_assert0(0);
805  }
806  }
807 
808  /* get output frame and drop the others */
809  if (fm->ppsrc) {
810  /* field matching was based on a filtered/post-processed input, we now
811  * pick the untouched fields from the clean source */
812  dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2);
813  } else {
814  if (!gen_frames[match]) { // XXX: is that possible?
815  dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt);
816  } else {
817  dst = gen_frames[match];
818  gen_frames[match] = NULL;
819  }
820  }
821  if (!dst) {
822  ret = AVERROR(ENOMEM);
823  goto fail;
824  }
825 
826  /* mark the frame we are unable to match properly as interlaced so a proper
827  * de-interlacer can take the relay */
828  dst->interlaced_frame = combs[match] >= fm->combpel;
829  if (dst->interlaced_frame) {
830  av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
831  outlink->frame_count_in, av_ts2timestr(in->pts, &inlink->time_base));
832  dst->top_field_first = field;
833  }
834 
835  av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)"
836  " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
837  fm->combpel, match, dst->interlaced_frame ? "YES" : "NO");
838 
839 fail:
840  for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
841  av_frame_free(&gen_frames[i]);
842 
843  if (ret >= 0)
844  return ff_filter_frame(outlink, dst);
845  return ret;
846 }
847 
849 {
850  FieldMatchContext *fm = ctx->priv;
851  AVFrame *frame = NULL;
852  int ret = 0, status;
853  int64_t pts;
854 
856 
857  if ((fm->got_frame[INPUT_MAIN] == 0) &&
858  (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_MAIN], &frame)) > 0) {
859  ret = filter_frame(ctx->inputs[INPUT_MAIN], frame);
860  if (ret < 0)
861  return ret;
862  }
863  if (ret < 0)
864  return ret;
865  if (fm->ppsrc &&
866  (fm->got_frame[INPUT_CLEANSRC] == 0) &&
867  (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_CLEANSRC], &frame)) > 0) {
868  ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], frame);
869  if (ret < 0)
870  return ret;
871  }
872  if (ret < 0) {
873  return ret;
874  } else if (ff_inlink_acknowledge_status(ctx->inputs[INPUT_MAIN], &status, &pts)) {
875  if (status == AVERROR_EOF) { // flushing
876  fm->eof |= 1 << INPUT_MAIN;
877  ret = filter_frame(ctx->inputs[INPUT_MAIN], NULL);
878  }
879  ff_outlink_set_status(ctx->outputs[0], status, pts);
880  return ret;
881  } else if (fm->ppsrc && ff_inlink_acknowledge_status(ctx->inputs[INPUT_CLEANSRC], &status, &pts)) {
882  if (status == AVERROR_EOF) { // flushing
883  fm->eof |= 1 << INPUT_CLEANSRC;
884  ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], NULL);
885  }
886  ff_outlink_set_status(ctx->outputs[0], status, pts);
887  return ret;
888  } else {
889  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
890  if (fm->got_frame[INPUT_MAIN] == 0)
892  if (fm->ppsrc && (fm->got_frame[INPUT_CLEANSRC] == 0))
894  }
895  return 0;
896  }
897 }
898 
900 {
901  FieldMatchContext *fm = ctx->priv;
902 
903  static const enum AVPixelFormat pix_fmts[] = {
907  };
908  static const enum AVPixelFormat unproc_pix_fmts[] = {
923  };
924  int ret;
925 
927  if (!fmts_list)
928  return AVERROR(ENOMEM);
929  if (!fm->ppsrc) {
930  return ff_set_common_formats(ctx, fmts_list);
931  }
932 
933  if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_MAIN]->outcfg.formats)) < 0)
934  return ret;
935  fmts_list = ff_make_format_list(unproc_pix_fmts);
936  if (!fmts_list)
937  return AVERROR(ENOMEM);
938  if ((ret = ff_formats_ref(fmts_list, &ctx->outputs[0]->incfg.formats)) < 0)
939  return ret;
940  if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_CLEANSRC]->outcfg.formats)) < 0)
941  return ret;
942  return 0;
943 }
944 
945 static int config_input(AVFilterLink *inlink)
946 {
947  int ret;
948  AVFilterContext *ctx = inlink->dst;
949  FieldMatchContext *fm = ctx->priv;
950  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
951  const int w = inlink->w;
952  const int h = inlink->h;
953 
954  if (h <= fm->blocky / 2 || AV_CEIL_RSHIFT(h, pix_desc->log2_chroma_h) < 4) {
955  av_log(ctx, AV_LOG_ERROR, "Height %d is too small\n", h);
956  return AVERROR(EINVAL);
957  }
958 
959  fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0);
960 
961  if ((ret = av_image_alloc(fm->map_data, fm->map_linesize, w, h, inlink->format, 32)) < 0 ||
962  (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
963  return ret;
964 
965  fm->hsub = pix_desc->log2_chroma_w;
966  fm->vsub = pix_desc->log2_chroma_h;
967 
968  fm->tpitchy = FFALIGN(w, 16);
969  fm->tpitchuv = FFALIGN(w >> 1, 16);
970 
971  fm->tbuffer = av_calloc((h/2 + 4) * fm->tpitchy, sizeof(*fm->tbuffer));
972  fm->c_array = av_malloc_array((((w + fm->blockx/2)/fm->blockx)+1) *
973  (((h + fm->blocky/2)/fm->blocky)+1),
974  4 * sizeof(*fm->c_array));
975  if (!fm->tbuffer || !fm->c_array)
976  return AVERROR(ENOMEM);
977 
978  return 0;
979 }
980 
982 {
983  const FieldMatchContext *fm = ctx->priv;
984  AVFilterPad pad = {
985  .name = "main",
986  .type = AVMEDIA_TYPE_VIDEO,
987  .config_props = config_input,
988  };
989  int ret;
990 
991  if ((ret = ff_insert_inpad(ctx, INPUT_MAIN, &pad)) < 0)
992  return ret;
993 
994  if (fm->ppsrc) {
995  pad.name = "clean_src";
996  pad.config_props = NULL;
997  if ((ret = ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad)) < 0)
998  return ret;
999  }
1000 
1001  if ((fm->blockx & (fm->blockx - 1)) ||
1002  (fm->blocky & (fm->blocky - 1))) {
1003  av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
1004  return AVERROR(EINVAL);
1005  }
1006 
1007  if (fm->combpel > fm->blockx * fm->blocky) {
1008  av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n");
1009  return AVERROR(EINVAL);
1010  }
1011 
1012  return 0;
1013 }
1014 
1016 {
1017  FieldMatchContext *fm = ctx->priv;
1018 
1019  if (fm->prv != fm->src)
1020  av_frame_free(&fm->prv);
1021  if (fm->nxt != fm->src)
1022  av_frame_free(&fm->nxt);
1023  if (fm->prv2 != fm->src2)
1024  av_frame_free(&fm->prv2);
1025  if (fm->nxt2 != fm->src2)
1026  av_frame_free(&fm->nxt2);
1027  av_frame_free(&fm->src);
1028  av_frame_free(&fm->src2);
1029  av_freep(&fm->map_data[0]);
1030  av_freep(&fm->cmask_data[0]);
1031  av_freep(&fm->tbuffer);
1032  av_freep(&fm->c_array);
1033 }
1034 
1035 static int config_output(AVFilterLink *outlink)
1036 {
1037  AVFilterContext *ctx = outlink->src;
1038  FieldMatchContext *fm = ctx->priv;
1039  const AVFilterLink *inlink =
1040  ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
1042 
1043  fm->bpc = (desc->comp[0].depth + 7) / 8;
1044  outlink->time_base = inlink->time_base;
1045  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
1046  outlink->frame_rate = inlink->frame_rate;
1047  outlink->w = inlink->w;
1048  outlink->h = inlink->h;
1049  return 0;
1050 }
1051 
1053  {
1054  .name = "default",
1055  .type = AVMEDIA_TYPE_VIDEO,
1056  .config_props = config_output,
1057  },
1058  { NULL }
1059 };
1060 
1062  .name = "fieldmatch",
1063  .description = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine."),
1064  .query_formats = query_formats,
1065  .priv_size = sizeof(FieldMatchContext),
1066  .init = fieldmatch_init,
1067  .activate = activate,
1069  .inputs = NULL,
1071  .priv_class = &fieldmatch_class,
1073 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
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
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1449
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1494
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1636
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:274
#define f(width, name)
Definition: cbs_vp9.c:255
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
#define abs(x)
Definition: cuda_runtime.h:35
static AVFrame * frame
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:200
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:223
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:183
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition: formats.c:466
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_INT
Definition: opt.h:225
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:106
#define AVERROR_EOF
End of file.
Definition: error.h:55
#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_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ 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
misc image utilities
int i
Definition: input.c:407
#define AV_WN16A(p, v)
Definition: intreadwrite.h:534
#define AV_WN32A(p, v)
Definition: intreadwrite.h:538
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:240
#define FF_INLINK_IDX(link)
Find the index of a link.
Definition: internal.h:302
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
#define FFALIGN(x, a)
Definition: macros.h:48
static const uint64_t c2
Definition: murmur3.c:52
static const uint64_t c1
Definition: murmur3.c:51
const char data[16]
Definition: mxf.c:142
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
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_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ 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_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:401
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
#define s1
Definition: regdef.h:38
#define s2
Definition: regdef.h:39
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
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
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:118
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:470
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
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
uint8_t * cmask_data[4]
int vsub
chroma subsampling values
Definition: vf_fieldmatch.c:83
uint8_t * map_data[4]
int mode
matching_mode
Definition: vf_fieldmatch.c:92
int combmatch
comb_matching_mode
Definition: vf_fieldmatch.c:98
AVFrame * nxt2
sliding window of the optional second stream
Definition: vf_fieldmatch.c:81
int got_frame[2]
frame request flag for each input stream
Definition: vf_fieldmatch.c:82
uint32_t eof
bitmask for end of stream
Definition: vf_fieldmatch.c:85
AVFrame * nxt
main sliding window of 3 frames
Definition: vf_fieldmatch.c:80
int bpc
bytes per component
Definition: vf_fieldmatch.c:84
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
static int64_t pts
matching_mode
Definition: vf_fieldmatch.c:53
@ MODE_PCN
Definition: vf_fieldmatch.c:58
@ MODE_PCN_UB
Definition: vf_fieldmatch.c:59
@ MODE_PC_U
Definition: vf_fieldmatch.c:56
@ MODE_PC_N_UB
Definition: vf_fieldmatch.c:57
@ MODE_PC_N
Definition: vf_fieldmatch.c:55
@ MODE_PC
Definition: vf_fieldmatch.c:54
@ NB_MODE
Definition: vf_fieldmatch.c:60
static int get_field_base(int match, int field)
static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize, const uint8_t *nxtp, int nxt_linesize, uint8_t *tbuffer, int tbuf_linesize, int width, int height)
#define INPUT_MAIN
Definition: vf_fieldmatch.c:44
#define C_ARRAY_ADD(v)
static AVFrame * create_weave_frame(AVFilterContext *ctx, int match, int field, const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
comb_matching_mode
Definition: vf_fieldmatch.c:63
@ COMBMATCH_NONE
Definition: vf_fieldmatch.c:64
@ NB_COMBMATCH
Definition: vf_fieldmatch.c:67
@ COMBMATCH_FULL
Definition: vf_fieldmatch.c:66
@ COMBMATCH_SC
Definition: vf_fieldmatch.c:65
#define FILTER(xm2, xm1, xp1, xp2)
static AVFrame * select_frame(FieldMatchContext *fm, int match)
static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
static av_always_inline void mark_combed_chroma(const FieldMatchContext *fm, const AVFrame *src, int hsub, int vsub)
fieldmatch_parity
Definition: vf_fieldmatch.c:47
@ FM_PARITY_BOTTOM
Definition: vf_fieldmatch.c:49
@ FM_PARITY_TOP
Definition: vf_fieldmatch.c:50
@ FM_PARITY_AUTO
Definition: vf_fieldmatch.c:48
static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
static int query_formats(AVFilterContext *ctx)
AVFILTER_DEFINE_CLASS(fieldmatch)
#define LOAD_COMB(mid)
static int config_input(AVFilterLink *inlink)
static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
#define FLAGS
static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2, AVFrame **gen_frames, int field)
#define VERTICAL_HALF(y_start, y_end)
static const int fxo0m[]
static void build_diff_map(FieldMatchContext *fm, const uint8_t *prvp, int prv_linesize, const uint8_t *nxtp, int nxt_linesize, uint8_t *dstp, int dst_linesize, int height, int width, int plane)
Build a map over which pixels differ a lot/a little.
static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilter ff_vf_fieldmatch
static const AVFilterPad fieldmatch_outputs[]
comb_dbg
Definition: vf_fieldmatch.c:70
@ COMBDBG_PCN
Definition: vf_fieldmatch.c:72
@ COMBDBG_NONE
Definition: vf_fieldmatch.c:71
@ NB_COMBDBG
Definition: vf_fieldmatch.c:74
@ COMBDBG_PCNUB
Definition: vf_fieldmatch.c:73
static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
#define INPUT_CLEANSRC
Definition: vf_fieldmatch.c:45
@ mB
@ mU
@ mN
@ mP
@ mC
static int activate(AVFilterContext *ctx)
static const int fxo1m[]
static av_cold int fieldmatch_init(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
#define HAS_FF_AROUND(p, lz)
static const AVOption fieldmatch_options[]
static void copy_fields(const FieldMatchContext *fm, AVFrame *dst, const AVFrame *src, int field)
#define SLIDING_FRAME_WINDOW(prv, src, nxt)
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1625
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
int acc
Definition: yuv2rgb.c:721