vf_fieldmatch.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. * @file
  23. * Fieldmatching filter, ported from VFM filter (VapouSsynth) by Clément.
  24. * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
  25. * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone
  26. * (tritical), the original author.
  27. *
  28. * @see http://bengal.missouri.edu/~kes25c/
  29. * @see http://www.vapoursynth.com/about/
  30. */
  31. #include <inttypes.h>
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/timestamp.h"
  36. #include "avfilter.h"
  37. #include "internal.h"
  38. #define INPUT_MAIN 0
  39. #define INPUT_CLEANSRC 1
  40. enum fieldmatch_parity {
  41. FM_PARITY_AUTO = -1,
  42. FM_PARITY_BOTTOM = 0,
  43. FM_PARITY_TOP = 1,
  44. };
  45. enum matching_mode {
  46. MODE_PC,
  47. MODE_PC_N,
  48. MODE_PC_U,
  49. MODE_PC_N_UB,
  50. MODE_PCN,
  51. MODE_PCN_UB,
  52. NB_MODE
  53. };
  54. enum comb_matching_mode {
  55. COMBMATCH_NONE,
  56. COMBMATCH_SC,
  57. COMBMATCH_FULL,
  58. NB_COMBMATCH
  59. };
  60. enum comb_dbg {
  61. COMBDBG_NONE,
  62. COMBDBG_PCN,
  63. COMBDBG_PCNUB,
  64. NB_COMBDBG
  65. };
  66. typedef struct {
  67. const AVClass *class;
  68. AVFrame *prv, *src, *nxt; ///< main sliding window of 3 frames
  69. AVFrame *prv2, *src2, *nxt2; ///< sliding window of the optional second stream
  70. int got_frame[2]; ///< frame request flag for each input stream
  71. int hsub, vsub; ///< chroma subsampling values
  72. uint32_t eof; ///< bitmask for end of stream
  73. int64_t lastscdiff;
  74. int64_t lastn;
  75. /* options */
  76. int order;
  77. int ppsrc;
  78. enum matching_mode mode;
  79. int field;
  80. int mchroma;
  81. int y0, y1;
  82. int64_t scthresh;
  83. double scthresh_flt;
  84. enum comb_matching_mode combmatch;
  85. int combdbg;
  86. int cthresh;
  87. int chroma;
  88. int blockx, blocky;
  89. int combpel;
  90. /* misc buffers */
  91. uint8_t *map_data[4];
  92. int map_linesize[4];
  93. uint8_t *cmask_data[4];
  94. int cmask_linesize[4];
  95. int *c_array;
  96. int tpitchy, tpitchuv;
  97. uint8_t *tbuffer;
  98. } FieldMatchContext;
  99. #define OFFSET(x) offsetof(FieldMatchContext, x)
  100. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  101. static const AVOption fieldmatch_options[] = {
  102. { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" },
  103. { "auto", "auto detect parity", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "order" },
  104. { "bff", "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "order" },
  105. { "tff", "assume top field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "order" },
  106. { "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" },
  107. { "pc", "2-way match (p/c)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC}, INT_MIN, INT_MAX, FLAGS, "mode" },
  108. { "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" },
  109. { "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" },
  110. { "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" },
  111. { "pcn", "3-way match (p/c/n)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN}, INT_MIN, INT_MAX, FLAGS, "mode" },
  112. { "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" },
  113. { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  114. { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" },
  115. { "auto", "automatic (same value as 'order')", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "field" },
  116. { "bottom", "bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field" },
  117. { "top", "top field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "field" },
  118. { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  119. { "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 },
  120. { "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 },
  121. { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS },
  122. { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, "combmatching" },
  123. { "none", "disable combmatching", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
  124. { "sc", "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
  125. { "full", "enable combmatching all the time", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
  126. { "combdbg", "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, "dbglvl" },
  127. { "none", "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
  128. { "pcn", "calculate p/c/n", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
  129. { "pcnub", "calculate p/c/n/u/b", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
  130. { "cthresh", "set the area combing threshold used for combed frame detection", OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS },
  131. { "chroma", "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64= 0}, 0, 1, FLAGS },
  132. { "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 },
  133. { "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 },
  134. { "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 },
  135. { NULL }
  136. };
  137. AVFILTER_DEFINE_CLASS(fieldmatch);
  138. static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
  139. {
  140. return plane ? FF_CEIL_RSHIFT(f->width, fm->hsub) : f->width;
  141. }
  142. static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
  143. {
  144. return plane ? FF_CEIL_RSHIFT(f->height, fm->vsub) : f->height;
  145. }
  146. static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
  147. {
  148. int x, y;
  149. const uint8_t *srcp1 = f1->data[0];
  150. const uint8_t *srcp2 = f2->data[0];
  151. const int src1_linesize = f1->linesize[0];
  152. const int src2_linesize = f2->linesize[0];
  153. const int width = f1->width;
  154. const int height = f1->height;
  155. int64_t acc = 0;
  156. for (y = 0; y < height; y++) {
  157. for (x = 0; x < width; x++)
  158. acc += abs(srcp1[x] - srcp2[x]);
  159. srcp1 += src1_linesize;
  160. srcp2 += src2_linesize;
  161. }
  162. return acc;
  163. }
  164. static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
  165. {
  166. int y;
  167. for (y = 0; y < h; y++) {
  168. memset(data, v, w);
  169. data += linesize;
  170. }
  171. }
  172. static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
  173. {
  174. int x, y, plane, max_v = 0;
  175. const int cthresh = fm->cthresh;
  176. const int cthresh6 = cthresh * 6;
  177. for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
  178. const uint8_t *srcp = src->data[plane];
  179. const int src_linesize = src->linesize[plane];
  180. const int width = get_width (fm, src, plane);
  181. const int height = get_height(fm, src, plane);
  182. uint8_t *cmkp = fm->cmask_data[plane];
  183. const int cmk_linesize = fm->cmask_linesize[plane];
  184. if (cthresh < 0) {
  185. fill_buf(cmkp, width, height, cmk_linesize, 0xff);
  186. continue;
  187. }
  188. fill_buf(cmkp, width, height, cmk_linesize, 0);
  189. /* [1 -3 4 -3 1] vertical filter */
  190. #define FILTER(xm2, xm1, xp1, xp2) \
  191. abs( 4 * srcp[x] \
  192. -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
  193. + (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
  194. /* first line */
  195. for (x = 0; x < width; x++) {
  196. const int s1 = abs(srcp[x] - srcp[x + src_linesize]);
  197. if (s1 > cthresh && FILTER(2, 1, 1, 2))
  198. cmkp[x] = 0xff;
  199. }
  200. srcp += src_linesize;
  201. cmkp += cmk_linesize;
  202. /* second line */
  203. for (x = 0; x < width; x++) {
  204. const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
  205. const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
  206. if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2))
  207. cmkp[x] = 0xff;
  208. }
  209. srcp += src_linesize;
  210. cmkp += cmk_linesize;
  211. /* all lines minus first two and last two */
  212. for (y = 2; y < height-2; y++) {
  213. for (x = 0; x < width; x++) {
  214. const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
  215. const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
  216. if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2))
  217. cmkp[x] = 0xff;
  218. }
  219. srcp += src_linesize;
  220. cmkp += cmk_linesize;
  221. }
  222. /* before-last line */
  223. for (x = 0; x < width; x++) {
  224. const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
  225. const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
  226. if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2))
  227. cmkp[x] = 0xff;
  228. }
  229. srcp += src_linesize;
  230. cmkp += cmk_linesize;
  231. /* last line */
  232. for (x = 0; x < width; x++) {
  233. const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
  234. if (s1 > cthresh && FILTER(-2, -1, -1, -2))
  235. cmkp[x] = 0xff;
  236. }
  237. }
  238. if (fm->chroma) {
  239. uint8_t *cmkp = fm->cmask_data[0];
  240. uint8_t *cmkpU = fm->cmask_data[1];
  241. uint8_t *cmkpV = fm->cmask_data[2];
  242. const int width = FF_CEIL_RSHIFT(src->width, fm->hsub);
  243. const int height = FF_CEIL_RSHIFT(src->height, fm->vsub);
  244. const int cmk_linesize = fm->cmask_linesize[0] << 1;
  245. const int cmk_linesizeUV = fm->cmask_linesize[2];
  246. uint8_t *cmkpp = cmkp - (cmk_linesize>>1);
  247. uint8_t *cmkpn = cmkp + (cmk_linesize>>1);
  248. uint8_t *cmkpnn = cmkp + cmk_linesize;
  249. for (y = 1; y < height - 1; y++) {
  250. cmkpp += cmk_linesize;
  251. cmkp += cmk_linesize;
  252. cmkpn += cmk_linesize;
  253. cmkpnn += cmk_linesize;
  254. cmkpV += cmk_linesizeUV;
  255. cmkpU += cmk_linesizeUV;
  256. for (x = 1; x < width - 1; x++) {
  257. #define HAS_FF_AROUND(p, lz) (p[x-1 - lz] == 0xff || p[x - lz] == 0xff || p[x+1 - lz] == 0xff || \
  258. p[x-1 ] == 0xff || p[x+1 ] == 0xff || \
  259. p[x-1 + lz] == 0xff || p[x + lz] == 0xff || p[x+1 + lz] == 0xff)
  260. if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
  261. (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
  262. ((uint16_t*)cmkp)[x] = 0xffff;
  263. ((uint16_t*)cmkpn)[x] = 0xffff;
  264. if (y&1) ((uint16_t*)cmkpp)[x] = 0xffff;
  265. else ((uint16_t*)cmkpnn)[x] = 0xffff;
  266. }
  267. }
  268. }
  269. }
  270. {
  271. const int blockx = fm->blockx;
  272. const int blocky = fm->blocky;
  273. const int xhalf = blockx/2;
  274. const int yhalf = blocky/2;
  275. const int cmk_linesize = fm->cmask_linesize[0];
  276. const uint8_t *cmkp = fm->cmask_data[0] + cmk_linesize;
  277. const int width = src->width;
  278. const int height = src->height;
  279. const int xblocks = ((width+xhalf)/blockx) + 1;
  280. const int xblocks4 = xblocks<<2;
  281. const int yblocks = ((height+yhalf)/blocky) + 1;
  282. int *c_array = fm->c_array;
  283. const int arraysize = (xblocks*yblocks)<<2;
  284. int heighta = (height/(blocky/2))*(blocky/2);
  285. const int widtha = (width /(blockx/2))*(blockx/2);
  286. if (heighta == height)
  287. heighta = height - yhalf;
  288. memset(c_array, 0, arraysize * sizeof(*c_array));
  289. #define C_ARRAY_ADD(v) do { \
  290. const int box1 = (x / blockx) * 4; \
  291. const int box2 = ((x + xhalf) / blockx) * 4; \
  292. c_array[temp1 + box1 ] += v; \
  293. c_array[temp1 + box2 + 1] += v; \
  294. c_array[temp2 + box1 + 2] += v; \
  295. c_array[temp2 + box2 + 3] += v; \
  296. } while (0)
  297. #define VERTICAL_HALF(y_start, y_end) do { \
  298. for (y = y_start; y < y_end; y++) { \
  299. const int temp1 = (y / blocky) * xblocks4; \
  300. const int temp2 = ((y + yhalf) / blocky) * xblocks4; \
  301. for (x = 0; x < width; x++) \
  302. if (cmkp[x - cmk_linesize] == 0xff && \
  303. cmkp[x ] == 0xff && \
  304. cmkp[x + cmk_linesize] == 0xff) \
  305. C_ARRAY_ADD(1); \
  306. cmkp += cmk_linesize; \
  307. } \
  308. } while (0)
  309. VERTICAL_HALF(1, yhalf);
  310. for (y = yhalf; y < heighta; y += yhalf) {
  311. const int temp1 = (y / blocky) * xblocks4;
  312. const int temp2 = ((y + yhalf) / blocky) * xblocks4;
  313. for (x = 0; x < widtha; x += xhalf) {
  314. const uint8_t *cmkp_tmp = cmkp + x;
  315. int u, v, sum = 0;
  316. for (u = 0; u < yhalf; u++) {
  317. for (v = 0; v < xhalf; v++)
  318. if (cmkp_tmp[v - cmk_linesize] == 0xff &&
  319. cmkp_tmp[v ] == 0xff &&
  320. cmkp_tmp[v + cmk_linesize] == 0xff)
  321. sum++;
  322. cmkp_tmp += cmk_linesize;
  323. }
  324. if (sum)
  325. C_ARRAY_ADD(sum);
  326. }
  327. for (x = widtha; x < width; x++) {
  328. const uint8_t *cmkp_tmp = cmkp + x;
  329. int u, sum = 0;
  330. for (u = 0; u < yhalf; u++) {
  331. if (cmkp_tmp[-cmk_linesize] == 0xff &&
  332. cmkp_tmp[ 0] == 0xff &&
  333. cmkp_tmp[ cmk_linesize] == 0xff)
  334. sum++;
  335. cmkp_tmp += cmk_linesize;
  336. }
  337. if (sum)
  338. C_ARRAY_ADD(sum);
  339. }
  340. cmkp += cmk_linesize * yhalf;
  341. }
  342. VERTICAL_HALF(heighta, height - 1);
  343. for (x = 0; x < arraysize; x++)
  344. if (c_array[x] > max_v)
  345. max_v = c_array[x];
  346. }
  347. return max_v;
  348. }
  349. // the secret is that tbuffer is an interlaced, offset subset of all the lines
  350. static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
  351. const uint8_t *nxtp, int nxt_linesize,
  352. uint8_t *tbuffer, int tbuf_linesize,
  353. int width, int height)
  354. {
  355. int y, x;
  356. prvp -= prv_linesize;
  357. nxtp -= nxt_linesize;
  358. for (y = 0; y < height; y++) {
  359. for (x = 0; x < width; x++)
  360. tbuffer[x] = FFABS(prvp[x] - nxtp[x]);
  361. prvp += prv_linesize;
  362. nxtp += nxt_linesize;
  363. tbuffer += tbuf_linesize;
  364. }
  365. }
  366. /**
  367. * Build a map over which pixels differ a lot/a little
  368. */
  369. static void build_diff_map(FieldMatchContext *fm,
  370. const uint8_t *prvp, int prv_linesize,
  371. const uint8_t *nxtp, int nxt_linesize,
  372. uint8_t *dstp, int dst_linesize, int height,
  373. int width, int plane)
  374. {
  375. int x, y, u, diff, count;
  376. int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
  377. const uint8_t *dp = fm->tbuffer + tpitch;
  378. build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
  379. fm->tbuffer, tpitch, width, height>>1);
  380. for (y = 2; y < height - 2; y += 2) {
  381. for (x = 1; x < width - 1; x++) {
  382. diff = dp[x];
  383. if (diff > 3) {
  384. for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
  385. count += dp[u-tpitch] > 3;
  386. count += dp[u ] > 3;
  387. count += dp[u+tpitch] > 3;
  388. }
  389. if (count > 1) {
  390. dstp[x] = 1;
  391. if (diff > 19) {
  392. int upper = 0, lower = 0;
  393. for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
  394. if (dp[u-tpitch] > 19) { count++; upper = 1; }
  395. if (dp[u ] > 19) count++;
  396. if (dp[u+tpitch] > 19) { count++; lower = 1; }
  397. }
  398. if (count > 3) {
  399. if (upper && lower) {
  400. dstp[x] |= 1<<1;
  401. } else {
  402. int upper2 = 0, lower2 = 0;
  403. for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
  404. if (y != 2 && dp[u-2*tpitch] > 19) upper2 = 1;
  405. if ( dp[u- tpitch] > 19) upper = 1;
  406. if ( dp[u+ tpitch] > 19) lower = 1;
  407. if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
  408. }
  409. if ((upper && (lower || upper2)) ||
  410. (lower && (upper || lower2)))
  411. dstp[x] |= 1<<1;
  412. else if (count > 5)
  413. dstp[x] |= 1<<2;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. dp += tpitch;
  421. dstp += dst_linesize;
  422. }
  423. }
  424. enum { mP, mC, mN, mB, mU };
  425. static int get_field_base(int match, int field)
  426. {
  427. return match < 3 ? 2 - field : 1 + field;
  428. }
  429. static AVFrame *select_frame(FieldMatchContext *fm, int match)
  430. {
  431. if (match == mP || match == mB) return fm->prv;
  432. else if (match == mN || match == mU) return fm->nxt;
  433. else /* match == mC */ return fm->src;
  434. }
  435. static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
  436. {
  437. int plane, ret;
  438. uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
  439. uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
  440. int norm1, norm2, mtn1, mtn2;
  441. float c1, c2, mr;
  442. const AVFrame *src = fm->src;
  443. for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
  444. int x, y, temp1, temp2, fbase;
  445. const AVFrame *prev, *next;
  446. uint8_t *mapp = fm->map_data[plane];
  447. int map_linesize = fm->map_linesize[plane];
  448. const uint8_t *srcp = src->data[plane];
  449. const int src_linesize = src->linesize[plane];
  450. const int srcf_linesize = src_linesize << 1;
  451. int prv_linesize, nxt_linesize;
  452. int prvf_linesize, nxtf_linesize;
  453. const int width = get_width (fm, src, plane);
  454. const int height = get_height(fm, src, plane);
  455. const int y0a = fm->y0 >> (plane != 0);
  456. const int y1a = fm->y1 >> (plane != 0);
  457. const int startx = (plane == 0 ? 8 : 4);
  458. const int stopx = width - startx;
  459. const uint8_t *srcpf, *srcf, *srcnf;
  460. const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
  461. fill_buf(mapp, width, height, map_linesize, 0);
  462. /* match1 */
  463. fbase = get_field_base(match1, field);
  464. srcf = srcp + (fbase + 1) * src_linesize;
  465. srcpf = srcf - srcf_linesize;
  466. srcnf = srcf + srcf_linesize;
  467. mapp = mapp + fbase * map_linesize;
  468. prev = select_frame(fm, match1);
  469. prv_linesize = prev->linesize[plane];
  470. prvf_linesize = prv_linesize << 1;
  471. prvpf = prev->data[plane] + fbase * prv_linesize; // previous frame, previous field
  472. prvnf = prvpf + prvf_linesize; // previous frame, next field
  473. /* match2 */
  474. fbase = get_field_base(match2, field);
  475. next = select_frame(fm, match2);
  476. nxt_linesize = next->linesize[plane];
  477. nxtf_linesize = nxt_linesize << 1;
  478. nxtpf = next->data[plane] + fbase * nxt_linesize; // next frame, previous field
  479. nxtnf = nxtpf + nxtf_linesize; // next frame, next field
  480. map_linesize <<= 1;
  481. if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
  482. build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
  483. mapp, map_linesize, height, width, plane);
  484. else
  485. build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
  486. mapp + map_linesize, map_linesize, height, width, plane);
  487. for (y = 2; y < height - 2; y += 2) {
  488. if (y0a == y1a || y < y0a || y > y1a) {
  489. for (x = startx; x < stopx; x++) {
  490. if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
  491. temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
  492. temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
  493. if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
  494. accumPc += temp2;
  495. if (temp2 > 42) {
  496. if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
  497. accumPm += temp2;
  498. if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
  499. accumPml += temp2;
  500. }
  501. temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
  502. if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
  503. accumNc += temp2;
  504. if (temp2 > 42) {
  505. if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
  506. accumNm += temp2;
  507. if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
  508. accumNml += temp2;
  509. }
  510. }
  511. }
  512. }
  513. prvpf += prvf_linesize;
  514. prvnf += prvf_linesize;
  515. srcpf += srcf_linesize;
  516. srcf += srcf_linesize;
  517. srcnf += srcf_linesize;
  518. nxtpf += nxtf_linesize;
  519. nxtnf += nxtf_linesize;
  520. mapp += map_linesize;
  521. }
  522. }
  523. if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
  524. FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
  525. accumPm = accumPml;
  526. accumNm = accumNml;
  527. }
  528. norm1 = (int)((accumPc / 6.0f) + 0.5f);
  529. norm2 = (int)((accumNc / 6.0f) + 0.5f);
  530. mtn1 = (int)((accumPm / 6.0f) + 0.5f);
  531. mtn2 = (int)((accumNm / 6.0f) + 0.5f);
  532. c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
  533. c2 = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
  534. mr = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMAX(norm1,norm2),1));
  535. if (((mtn1 >= 500 || mtn2 >= 500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
  536. ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
  537. ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
  538. ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
  539. ret = mtn1 > mtn2 ? match2 : match1;
  540. else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
  541. ret = mtn1 > mtn2 ? match2 : match1;
  542. else
  543. ret = norm1 > norm2 ? match2 : match1;
  544. return ret;
  545. }
  546. static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
  547. const AVFrame *src, int field)
  548. {
  549. int plane;
  550. for (plane = 0; plane < 4 && src->data[plane] && src->linesize[plane]; plane++)
  551. av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1,
  552. src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1,
  553. get_width(fm, src, plane), get_height(fm, src, plane) / 2);
  554. }
  555. static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
  556. const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
  557. {
  558. AVFrame *dst;
  559. FieldMatchContext *fm = ctx->priv;
  560. if (match == mC) {
  561. dst = av_frame_clone(src);
  562. } else {
  563. AVFilterLink *outlink = ctx->outputs[0];
  564. dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  565. if (!dst)
  566. return NULL;
  567. av_frame_copy_props(dst, src);
  568. switch (match) {
  569. case mP: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv, field); break;
  570. case mN: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt, field); break;
  571. case mB: copy_fields(fm, dst, src, field); copy_fields(fm, dst, prv, 1-field); break;
  572. case mU: copy_fields(fm, dst, src, field); copy_fields(fm, dst, nxt, 1-field); break;
  573. default: av_assert0(0);
  574. }
  575. }
  576. return dst;
  577. }
  578. static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2,
  579. AVFrame **gen_frames, int field)
  580. {
  581. const FieldMatchContext *fm = ctx->priv;
  582. #define LOAD_COMB(mid) do { \
  583. if (combs[mid] < 0) { \
  584. if (!gen_frames[mid]) \
  585. gen_frames[mid] = create_weave_frame(ctx, mid, field, \
  586. fm->prv, fm->src, fm->nxt); \
  587. combs[mid] = calc_combed_score(fm, gen_frames[mid]); \
  588. } \
  589. } while (0)
  590. LOAD_COMB(m1);
  591. LOAD_COMB(m2);
  592. if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) &&
  593. abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel)
  594. return m2;
  595. else
  596. return m1;
  597. }
  598. static const int fxo0m[] = { mP, mC, mN, mB, mU };
  599. static const int fxo1m[] = { mN, mC, mP, mU, mB };
  600. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  601. {
  602. AVFilterContext *ctx = inlink->dst;
  603. AVFilterLink *outlink = ctx->outputs[0];
  604. FieldMatchContext *fm = ctx->priv;
  605. int combs[] = { -1, -1, -1, -1, -1 };
  606. int order, field, i, match, sc = 0;
  607. const int *fxo;
  608. AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
  609. AVFrame *dst;
  610. /* update frames queue(s) */
  611. #define SLIDING_FRAME_WINDOW(prv, src, nxt) do { \
  612. if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
  613. av_frame_free(&prv); \
  614. prv = src; \
  615. src = nxt; \
  616. if (in) \
  617. nxt = in; \
  618. if (!prv) \
  619. prv = src; \
  620. if (!prv) /* received only one frame at that point */ \
  621. return 0; \
  622. av_assert0(prv && src && nxt); \
  623. } while (0)
  624. if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
  625. SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
  626. fm->got_frame[INPUT_MAIN] = 1;
  627. } else {
  628. SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
  629. fm->got_frame[INPUT_CLEANSRC] = 1;
  630. }
  631. if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
  632. return 0;
  633. fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0;
  634. in = fm->src;
  635. /* parity */
  636. order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1);
  637. field = fm->field != FM_PARITY_AUTO ? fm->field : order;
  638. av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
  639. fxo = field ^ order ? fxo1m : fxo0m;
  640. /* debug mode: we generate all the fields combinations and their associated
  641. * combed score. XXX: inject as frame metadata? */
  642. if (fm->combdbg) {
  643. for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) {
  644. if (i > mN && fm->combdbg == COMBDBG_PCN)
  645. break;
  646. gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt);
  647. if (!gen_frames[i])
  648. return AVERROR(ENOMEM);
  649. combs[i] = calc_combed_score(fm, gen_frames[i]);
  650. }
  651. av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n",
  652. combs[0], combs[1], combs[2], combs[3], combs[4]);
  653. } else {
  654. gen_frames[mC] = av_frame_clone(fm->src);
  655. if (!gen_frames[mC])
  656. return AVERROR(ENOMEM);
  657. }
  658. /* p/c selection and optional 3-way p/c/n matches */
  659. match = compare_fields(fm, fxo[mC], fxo[mP], field);
  660. if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
  661. match = compare_fields(fm, match, fxo[mN], field);
  662. /* scene change check */
  663. if (fm->combmatch == COMBMATCH_SC) {
  664. if (fm->lastn == outlink->frame_count - 1) {
  665. if (fm->lastscdiff > fm->scthresh)
  666. sc = 1;
  667. } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) {
  668. sc = 1;
  669. }
  670. if (!sc) {
  671. fm->lastn = outlink->frame_count;
  672. fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt);
  673. sc = fm->lastscdiff > fm->scthresh;
  674. }
  675. }
  676. if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) {
  677. switch (fm->mode) {
  678. /* 2-way p/c matches */
  679. case MODE_PC:
  680. match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
  681. break;
  682. case MODE_PC_N:
  683. match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
  684. break;
  685. case MODE_PC_U:
  686. match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
  687. break;
  688. case MODE_PC_N_UB:
  689. match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
  690. match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
  691. match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
  692. break;
  693. /* 3-way p/c/n matches */
  694. case MODE_PCN:
  695. match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
  696. break;
  697. case MODE_PCN_UB:
  698. match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
  699. match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
  700. break;
  701. default:
  702. av_assert0(0);
  703. }
  704. }
  705. /* get output frame and drop the others */
  706. if (fm->ppsrc) {
  707. /* field matching was based on a filtered/post-processed input, we now
  708. * pick the untouched fields from the clean source */
  709. dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2);
  710. } else {
  711. if (!gen_frames[match]) { // XXX: is that possible?
  712. dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt);
  713. } else {
  714. dst = gen_frames[match];
  715. gen_frames[match] = NULL;
  716. }
  717. }
  718. if (!dst)
  719. return AVERROR(ENOMEM);
  720. for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
  721. av_frame_free(&gen_frames[i]);
  722. /* mark the frame we are unable to match properly as interlaced so a proper
  723. * de-interlacer can take the relay */
  724. dst->interlaced_frame = combs[match] >= fm->combpel;
  725. if (dst->interlaced_frame) {
  726. av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
  727. outlink->frame_count, av_ts2timestr(in->pts, &inlink->time_base));
  728. dst->top_field_first = field;
  729. }
  730. av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)"
  731. " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
  732. fm->combpel, match, dst->interlaced_frame ? "YES" : "NO");
  733. return ff_filter_frame(outlink, dst);
  734. }
  735. static int request_inlink(AVFilterContext *ctx, int lid)
  736. {
  737. int ret = 0;
  738. FieldMatchContext *fm = ctx->priv;
  739. if (!fm->got_frame[lid]) {
  740. AVFilterLink *inlink = ctx->inputs[lid];
  741. ret = ff_request_frame(inlink);
  742. if (ret == AVERROR_EOF) { // flushing
  743. fm->eof |= 1 << lid;
  744. ret = filter_frame(inlink, NULL);
  745. }
  746. }
  747. return ret;
  748. }
  749. static int request_frame(AVFilterLink *outlink)
  750. {
  751. int ret;
  752. AVFilterContext *ctx = outlink->src;
  753. FieldMatchContext *fm = ctx->priv;
  754. const uint32_t eof_mask = 1<<INPUT_MAIN | fm->ppsrc<<INPUT_CLEANSRC;
  755. if ((fm->eof & eof_mask) == eof_mask) // flush done?
  756. return AVERROR_EOF;
  757. if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
  758. return ret;
  759. if (fm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
  760. return ret;
  761. return 0;
  762. }
  763. static int query_formats(AVFilterContext *ctx)
  764. {
  765. // TODO: second input source can support >8bit depth
  766. static const enum AVPixelFormat pix_fmts[] = {
  767. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  768. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  769. AV_PIX_FMT_NONE
  770. };
  771. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  772. return 0;
  773. }
  774. static int config_input(AVFilterLink *inlink)
  775. {
  776. int ret;
  777. AVFilterContext *ctx = inlink->dst;
  778. FieldMatchContext *fm = ctx->priv;
  779. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  780. const int w = inlink->w;
  781. const int h = inlink->h;
  782. fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0);
  783. if ((ret = av_image_alloc(fm->map_data, fm->map_linesize, w, h, inlink->format, 32)) < 0 ||
  784. (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
  785. return ret;
  786. fm->hsub = pix_desc->log2_chroma_w;
  787. fm->vsub = pix_desc->log2_chroma_h;
  788. fm->tpitchy = FFALIGN(w, 16);
  789. fm->tpitchuv = FFALIGN(w >> 1, 16);
  790. fm->tbuffer = av_malloc(h/2 * fm->tpitchy);
  791. fm->c_array = av_malloc((((w + fm->blockx/2)/fm->blockx)+1) *
  792. (((h + fm->blocky/2)/fm->blocky)+1) *
  793. 4 * sizeof(*fm->c_array));
  794. if (!fm->tbuffer || !fm->c_array)
  795. return AVERROR(ENOMEM);
  796. return 0;
  797. }
  798. static av_cold int fieldmatch_init(AVFilterContext *ctx)
  799. {
  800. const FieldMatchContext *fm = ctx->priv;
  801. AVFilterPad pad = {
  802. .name = av_strdup("main"),
  803. .type = AVMEDIA_TYPE_VIDEO,
  804. .filter_frame = filter_frame,
  805. .config_props = config_input,
  806. };
  807. if (!pad.name)
  808. return AVERROR(ENOMEM);
  809. ff_insert_inpad(ctx, INPUT_MAIN, &pad);
  810. if (fm->ppsrc) {
  811. pad.name = av_strdup("clean_src");
  812. pad.config_props = NULL;
  813. if (!pad.name)
  814. return AVERROR(ENOMEM);
  815. ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
  816. }
  817. if ((fm->blockx & (fm->blockx - 1)) ||
  818. (fm->blocky & (fm->blocky - 1))) {
  819. av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
  820. return AVERROR(EINVAL);
  821. }
  822. if (fm->combpel > fm->blockx * fm->blocky) {
  823. av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n");
  824. return AVERROR(EINVAL);
  825. }
  826. return 0;
  827. }
  828. static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
  829. {
  830. int i;
  831. FieldMatchContext *fm = ctx->priv;
  832. if (fm->prv != fm->src)
  833. av_frame_free(&fm->prv);
  834. if (fm->nxt != fm->src)
  835. av_frame_free(&fm->nxt);
  836. av_frame_free(&fm->src);
  837. av_freep(&fm->map_data[0]);
  838. av_freep(&fm->cmask_data[0]);
  839. av_freep(&fm->tbuffer);
  840. av_freep(&fm->c_array);
  841. for (i = 0; i < ctx->nb_inputs; i++)
  842. av_freep(&ctx->input_pads[i].name);
  843. }
  844. static int config_output(AVFilterLink *outlink)
  845. {
  846. AVFilterContext *ctx = outlink->src;
  847. const FieldMatchContext *fm = ctx->priv;
  848. const AVFilterLink *inlink =
  849. ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
  850. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  851. outlink->time_base = inlink->time_base;
  852. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  853. outlink->frame_rate = inlink->frame_rate;
  854. outlink->w = inlink->w;
  855. outlink->h = inlink->h;
  856. return 0;
  857. }
  858. static const AVFilterPad fieldmatch_outputs[] = {
  859. {
  860. .name = "default",
  861. .type = AVMEDIA_TYPE_VIDEO,
  862. .request_frame = request_frame,
  863. .config_props = config_output,
  864. },
  865. { NULL }
  866. };
  867. AVFilter ff_vf_fieldmatch = {
  868. .name = "fieldmatch",
  869. .description = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine."),
  870. .query_formats = query_formats,
  871. .priv_size = sizeof(FieldMatchContext),
  872. .init = fieldmatch_init,
  873. .uninit = fieldmatch_uninit,
  874. .inputs = NULL,
  875. .outputs = fieldmatch_outputs,
  876. .priv_class = &fieldmatch_class,
  877. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  878. };