vf_w3fdif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
  3. * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
  4. * Based on the process described by Martin Weston for BBC R&D
  5. * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/common.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #include "w3fdif.h"
  32. typedef struct W3FDIFContext {
  33. const AVClass *class;
  34. int filter; ///< 0 is simple, 1 is more complex
  35. int deint; ///< which frames to deinterlace
  36. int linesize[4]; ///< bytes of pixel data per line for each plane
  37. int planeheight[4]; ///< height of each plane
  38. int field; ///< which field are we on, 0 or 1
  39. int eof;
  40. int nb_planes;
  41. AVFrame *prev, *cur, *next; ///< previous, current, next frames
  42. int32_t **work_line; ///< lines we are calculating
  43. int nb_threads;
  44. int max;
  45. W3FDIFDSPContext dsp;
  46. } W3FDIFContext;
  47. #define OFFSET(x) offsetof(W3FDIFContext, x)
  48. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  50. static const AVOption w3fdif_options[] = {
  51. { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
  52. CONST("simple", NULL, 0, "filter"),
  53. CONST("complex", NULL, 1, "filter"),
  54. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
  55. CONST("all", "deinterlace all frames", 0, "deint"),
  56. CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
  57. { NULL }
  58. };
  59. AVFILTER_DEFINE_CLASS(w3fdif);
  60. static int query_formats(AVFilterContext *ctx)
  61. {
  62. static const enum AVPixelFormat pix_fmts[] = {
  63. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  64. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  65. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  66. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  67. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  68. AV_PIX_FMT_YUVJ411P,
  69. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  70. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  71. AV_PIX_FMT_GRAY8,
  72. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  73. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  74. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  75. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  76. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  77. AV_PIX_FMT_NONE
  78. };
  79. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  80. if (!fmts_list)
  81. return AVERROR(ENOMEM);
  82. return ff_set_common_formats(ctx, fmts_list);
  83. }
  84. static void filter_simple_low(int32_t *work_line,
  85. uint8_t *in_lines_cur[2],
  86. const int16_t *coef, int linesize)
  87. {
  88. int i;
  89. for (i = 0; i < linesize; i++) {
  90. *work_line = *in_lines_cur[0]++ * coef[0];
  91. *work_line++ += *in_lines_cur[1]++ * coef[1];
  92. }
  93. }
  94. static void filter_complex_low(int32_t *work_line,
  95. uint8_t *in_lines_cur[4],
  96. const int16_t *coef, int linesize)
  97. {
  98. int i;
  99. for (i = 0; i < linesize; i++) {
  100. *work_line = *in_lines_cur[0]++ * coef[0];
  101. *work_line += *in_lines_cur[1]++ * coef[1];
  102. *work_line += *in_lines_cur[2]++ * coef[2];
  103. *work_line++ += *in_lines_cur[3]++ * coef[3];
  104. }
  105. }
  106. static void filter_simple_high(int32_t *work_line,
  107. uint8_t *in_lines_cur[3],
  108. uint8_t *in_lines_adj[3],
  109. const int16_t *coef, int linesize)
  110. {
  111. int i;
  112. for (i = 0; i < linesize; i++) {
  113. *work_line += *in_lines_cur[0]++ * coef[0];
  114. *work_line += *in_lines_adj[0]++ * coef[0];
  115. *work_line += *in_lines_cur[1]++ * coef[1];
  116. *work_line += *in_lines_adj[1]++ * coef[1];
  117. *work_line += *in_lines_cur[2]++ * coef[2];
  118. *work_line++ += *in_lines_adj[2]++ * coef[2];
  119. }
  120. }
  121. static void filter_complex_high(int32_t *work_line,
  122. uint8_t *in_lines_cur[5],
  123. uint8_t *in_lines_adj[5],
  124. const int16_t *coef, int linesize)
  125. {
  126. int i;
  127. for (i = 0; i < linesize; i++) {
  128. *work_line += *in_lines_cur[0]++ * coef[0];
  129. *work_line += *in_lines_adj[0]++ * coef[0];
  130. *work_line += *in_lines_cur[1]++ * coef[1];
  131. *work_line += *in_lines_adj[1]++ * coef[1];
  132. *work_line += *in_lines_cur[2]++ * coef[2];
  133. *work_line += *in_lines_adj[2]++ * coef[2];
  134. *work_line += *in_lines_cur[3]++ * coef[3];
  135. *work_line += *in_lines_adj[3]++ * coef[3];
  136. *work_line += *in_lines_cur[4]++ * coef[4];
  137. *work_line++ += *in_lines_adj[4]++ * coef[4];
  138. }
  139. }
  140. static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
  141. {
  142. int j;
  143. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  144. *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
  145. }
  146. static void filter16_simple_low(int32_t *work_line,
  147. uint8_t *in_lines_cur8[2],
  148. const int16_t *coef, int linesize)
  149. {
  150. uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
  151. int i;
  152. linesize /= 2;
  153. for (i = 0; i < linesize; i++) {
  154. *work_line = *in_lines_cur[0]++ * coef[0];
  155. *work_line++ += *in_lines_cur[1]++ * coef[1];
  156. }
  157. }
  158. static void filter16_complex_low(int32_t *work_line,
  159. uint8_t *in_lines_cur8[4],
  160. const int16_t *coef, int linesize)
  161. {
  162. uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
  163. (uint16_t *)in_lines_cur8[1],
  164. (uint16_t *)in_lines_cur8[2],
  165. (uint16_t *)in_lines_cur8[3] };
  166. int i;
  167. linesize /= 2;
  168. for (i = 0; i < linesize; i++) {
  169. *work_line = *in_lines_cur[0]++ * coef[0];
  170. *work_line += *in_lines_cur[1]++ * coef[1];
  171. *work_line += *in_lines_cur[2]++ * coef[2];
  172. *work_line++ += *in_lines_cur[3]++ * coef[3];
  173. }
  174. }
  175. static void filter16_simple_high(int32_t *work_line,
  176. uint8_t *in_lines_cur8[3],
  177. uint8_t *in_lines_adj8[3],
  178. const int16_t *coef, int linesize)
  179. {
  180. uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
  181. (uint16_t *)in_lines_cur8[1],
  182. (uint16_t *)in_lines_cur8[2] };
  183. uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
  184. (uint16_t *)in_lines_adj8[1],
  185. (uint16_t *)in_lines_adj8[2] };
  186. int i;
  187. linesize /= 2;
  188. for (i = 0; i < linesize; i++) {
  189. *work_line += *in_lines_cur[0]++ * coef[0];
  190. *work_line += *in_lines_adj[0]++ * coef[0];
  191. *work_line += *in_lines_cur[1]++ * coef[1];
  192. *work_line += *in_lines_adj[1]++ * coef[1];
  193. *work_line += *in_lines_cur[2]++ * coef[2];
  194. *work_line++ += *in_lines_adj[2]++ * coef[2];
  195. }
  196. }
  197. static void filter16_complex_high(int32_t *work_line,
  198. uint8_t *in_lines_cur8[5],
  199. uint8_t *in_lines_adj8[5],
  200. const int16_t *coef, int linesize)
  201. {
  202. uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
  203. (uint16_t *)in_lines_cur8[1],
  204. (uint16_t *)in_lines_cur8[2],
  205. (uint16_t *)in_lines_cur8[3],
  206. (uint16_t *)in_lines_cur8[4] };
  207. uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
  208. (uint16_t *)in_lines_adj8[1],
  209. (uint16_t *)in_lines_adj8[2],
  210. (uint16_t *)in_lines_adj8[3],
  211. (uint16_t *)in_lines_adj8[4] };
  212. int i;
  213. linesize /= 2;
  214. for (i = 0; i < linesize; i++) {
  215. *work_line += *in_lines_cur[0]++ * coef[0];
  216. *work_line += *in_lines_adj[0]++ * coef[0];
  217. *work_line += *in_lines_cur[1]++ * coef[1];
  218. *work_line += *in_lines_adj[1]++ * coef[1];
  219. *work_line += *in_lines_cur[2]++ * coef[2];
  220. *work_line += *in_lines_adj[2]++ * coef[2];
  221. *work_line += *in_lines_cur[3]++ * coef[3];
  222. *work_line += *in_lines_adj[3]++ * coef[3];
  223. *work_line += *in_lines_cur[4]++ * coef[4];
  224. *work_line++ += *in_lines_adj[4]++ * coef[4];
  225. }
  226. }
  227. static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
  228. {
  229. uint16_t *out_pixel = (uint16_t *)out_pixel8;
  230. int j;
  231. linesize /= 2;
  232. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  233. *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
  234. }
  235. static int config_input(AVFilterLink *inlink)
  236. {
  237. AVFilterContext *ctx = inlink->dst;
  238. W3FDIFContext *s = ctx->priv;
  239. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  240. int ret, i, depth;
  241. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  242. return ret;
  243. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  244. s->planeheight[0] = s->planeheight[3] = inlink->h;
  245. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  246. s->nb_threads = ff_filter_get_nb_threads(ctx);
  247. s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
  248. if (!s->work_line)
  249. return AVERROR(ENOMEM);
  250. for (i = 0; i < s->nb_threads; i++) {
  251. s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
  252. if (!s->work_line[i])
  253. return AVERROR(ENOMEM);
  254. }
  255. depth = desc->comp[0].depth;
  256. s->max = ((1 << depth) - 1) * 256 * 128;
  257. if (depth <= 8) {
  258. s->dsp.filter_simple_low = filter_simple_low;
  259. s->dsp.filter_complex_low = filter_complex_low;
  260. s->dsp.filter_simple_high = filter_simple_high;
  261. s->dsp.filter_complex_high = filter_complex_high;
  262. s->dsp.filter_scale = filter_scale;
  263. } else {
  264. s->dsp.filter_simple_low = filter16_simple_low;
  265. s->dsp.filter_complex_low = filter16_complex_low;
  266. s->dsp.filter_simple_high = filter16_simple_high;
  267. s->dsp.filter_complex_high = filter16_complex_high;
  268. s->dsp.filter_scale = filter16_scale;
  269. }
  270. if (ARCH_X86)
  271. ff_w3fdif_init_x86(&s->dsp, depth);
  272. return 0;
  273. }
  274. static int config_output(AVFilterLink *outlink)
  275. {
  276. AVFilterLink *inlink = outlink->src->inputs[0];
  277. outlink->time_base.num = inlink->time_base.num;
  278. outlink->time_base.den = inlink->time_base.den * 2;
  279. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  280. outlink->frame_rate.den = inlink->frame_rate.den;
  281. return 0;
  282. }
  283. /*
  284. * Filter coefficients from PH-2071, scaled by 256 * 128.
  285. * Each set of coefficients has a set for low-frequencies and high-frequencies.
  286. * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
  287. * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
  288. * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
  289. * and high-frequencies for simple and more-complex mode.
  290. */
  291. static const int8_t n_coef_lf[2] = { 2, 4 };
  292. static const int16_t coef_lf[2][4] = {{ 16384, 16384, 0, 0},
  293. { -852, 17236, 17236, -852}};
  294. static const int8_t n_coef_hf[2] = { 3, 5 };
  295. static const int16_t coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0},
  296. { 1016, -3801, 5570, -3801, 1016}};
  297. typedef struct ThreadData {
  298. AVFrame *out, *cur, *adj;
  299. int plane;
  300. } ThreadData;
  301. static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  302. {
  303. W3FDIFContext *s = ctx->priv;
  304. ThreadData *td = arg;
  305. AVFrame *out = td->out;
  306. AVFrame *cur = td->cur;
  307. AVFrame *adj = td->adj;
  308. const int plane = td->plane;
  309. const int filter = s->filter;
  310. uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
  311. uint8_t *out_line, *out_pixel;
  312. int32_t *work_line, *work_pixel;
  313. uint8_t *cur_data = cur->data[plane];
  314. uint8_t *adj_data = adj->data[plane];
  315. uint8_t *dst_data = out->data[plane];
  316. const int linesize = s->linesize[plane];
  317. const int height = s->planeheight[plane];
  318. const int cur_line_stride = cur->linesize[plane];
  319. const int adj_line_stride = adj->linesize[plane];
  320. const int dst_line_stride = out->linesize[plane];
  321. const int start = (height * jobnr) / nb_jobs;
  322. const int end = (height * (jobnr+1)) / nb_jobs;
  323. const int max = s->max;
  324. int j, y_in, y_out;
  325. /* copy unchanged the lines of the field */
  326. y_out = start + ((s->field == cur->top_field_first) ^ (start & 1));
  327. in_line = cur_data + (y_out * cur_line_stride);
  328. out_line = dst_data + (y_out * dst_line_stride);
  329. while (y_out < end) {
  330. memcpy(out_line, in_line, linesize);
  331. y_out += 2;
  332. in_line += cur_line_stride * 2;
  333. out_line += dst_line_stride * 2;
  334. }
  335. /* interpolate other lines of the field */
  336. y_out = start + ((s->field != cur->top_field_first) ^ (start & 1));
  337. out_line = dst_data + (y_out * dst_line_stride);
  338. while (y_out < end) {
  339. /* get low vertical frequencies from current field */
  340. for (j = 0; j < n_coef_lf[filter]; j++) {
  341. y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
  342. while (y_in < 0)
  343. y_in += 2;
  344. while (y_in >= height)
  345. y_in -= 2;
  346. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  347. }
  348. work_line = s->work_line[jobnr];
  349. switch (n_coef_lf[filter]) {
  350. case 2:
  351. s->dsp.filter_simple_low(work_line, in_lines_cur,
  352. coef_lf[filter], linesize);
  353. break;
  354. case 4:
  355. s->dsp.filter_complex_low(work_line, in_lines_cur,
  356. coef_lf[filter], linesize);
  357. }
  358. /* get high vertical frequencies from adjacent fields */
  359. for (j = 0; j < n_coef_hf[filter]; j++) {
  360. y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
  361. while (y_in < 0)
  362. y_in += 2;
  363. while (y_in >= height)
  364. y_in -= 2;
  365. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  366. in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
  367. }
  368. work_line = s->work_line[jobnr];
  369. switch (n_coef_hf[filter]) {
  370. case 3:
  371. s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
  372. coef_hf[filter], linesize);
  373. break;
  374. case 5:
  375. s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
  376. coef_hf[filter], linesize);
  377. }
  378. /* save scaled result to the output frame, scaling down by 256 * 128 */
  379. work_pixel = s->work_line[jobnr];
  380. out_pixel = out_line;
  381. s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
  382. /* move on to next line */
  383. y_out += 2;
  384. out_line += dst_line_stride * 2;
  385. }
  386. return 0;
  387. }
  388. static int filter(AVFilterContext *ctx, int is_second)
  389. {
  390. W3FDIFContext *s = ctx->priv;
  391. AVFilterLink *outlink = ctx->outputs[0];
  392. AVFrame *out, *adj;
  393. ThreadData td;
  394. int plane;
  395. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  396. if (!out)
  397. return AVERROR(ENOMEM);
  398. av_frame_copy_props(out, s->cur);
  399. out->interlaced_frame = 0;
  400. if (!is_second) {
  401. if (out->pts != AV_NOPTS_VALUE)
  402. out->pts *= 2;
  403. } else {
  404. int64_t cur_pts = s->cur->pts;
  405. int64_t next_pts = s->next->pts;
  406. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  407. out->pts = cur_pts + next_pts;
  408. } else {
  409. out->pts = AV_NOPTS_VALUE;
  410. }
  411. }
  412. adj = s->field ? s->next : s->prev;
  413. td.out = out; td.cur = s->cur; td.adj = adj;
  414. for (plane = 0; plane < s->nb_planes; plane++) {
  415. td.plane = plane;
  416. ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
  417. }
  418. s->field = !s->field;
  419. return ff_filter_frame(outlink, out);
  420. }
  421. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  422. {
  423. AVFilterContext *ctx = inlink->dst;
  424. W3FDIFContext *s = ctx->priv;
  425. int ret;
  426. av_frame_free(&s->prev);
  427. s->prev = s->cur;
  428. s->cur = s->next;
  429. s->next = frame;
  430. if (!s->cur) {
  431. s->cur = av_frame_clone(s->next);
  432. if (!s->cur)
  433. return AVERROR(ENOMEM);
  434. }
  435. if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
  436. AVFrame *out = av_frame_clone(s->cur);
  437. if (!out)
  438. return AVERROR(ENOMEM);
  439. av_frame_free(&s->prev);
  440. if (out->pts != AV_NOPTS_VALUE)
  441. out->pts *= 2;
  442. return ff_filter_frame(ctx->outputs[0], out);
  443. }
  444. if (!s->prev)
  445. return 0;
  446. ret = filter(ctx, 0);
  447. if (ret < 0)
  448. return ret;
  449. return filter(ctx, 1);
  450. }
  451. static int request_frame(AVFilterLink *outlink)
  452. {
  453. AVFilterContext *ctx = outlink->src;
  454. W3FDIFContext *s = ctx->priv;
  455. int ret;
  456. if (s->eof)
  457. return AVERROR_EOF;
  458. ret = ff_request_frame(ctx->inputs[0]);
  459. if (ret == AVERROR_EOF && s->cur) {
  460. AVFrame *next = av_frame_clone(s->next);
  461. if (!next)
  462. return AVERROR(ENOMEM);
  463. next->pts = s->next->pts * 2 - s->cur->pts;
  464. filter_frame(ctx->inputs[0], next);
  465. s->eof = 1;
  466. } else if (ret < 0) {
  467. return ret;
  468. }
  469. return 0;
  470. }
  471. static av_cold void uninit(AVFilterContext *ctx)
  472. {
  473. W3FDIFContext *s = ctx->priv;
  474. int i;
  475. av_frame_free(&s->prev);
  476. av_frame_free(&s->cur );
  477. av_frame_free(&s->next);
  478. for (i = 0; i < s->nb_threads; i++)
  479. av_freep(&s->work_line[i]);
  480. av_freep(&s->work_line);
  481. }
  482. static const AVFilterPad w3fdif_inputs[] = {
  483. {
  484. .name = "default",
  485. .type = AVMEDIA_TYPE_VIDEO,
  486. .filter_frame = filter_frame,
  487. .config_props = config_input,
  488. },
  489. { NULL }
  490. };
  491. static const AVFilterPad w3fdif_outputs[] = {
  492. {
  493. .name = "default",
  494. .type = AVMEDIA_TYPE_VIDEO,
  495. .config_props = config_output,
  496. .request_frame = request_frame,
  497. },
  498. { NULL }
  499. };
  500. AVFilter ff_vf_w3fdif = {
  501. .name = "w3fdif",
  502. .description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
  503. .priv_size = sizeof(W3FDIFContext),
  504. .priv_class = &w3fdif_class,
  505. .uninit = uninit,
  506. .query_formats = query_formats,
  507. .inputs = w3fdif_inputs,
  508. .outputs = w3fdif_outputs,
  509. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  510. };