You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
22KB

  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 mode; ///< 0 is frame, 1 is field
  36. int parity; ///< frame field parity
  37. int deint; ///< which frames to deinterlace
  38. int linesize[4]; ///< bytes of pixel data per line for each plane
  39. int planeheight[4]; ///< height of each plane
  40. int field; ///< which field are we on, 0 or 1
  41. int eof;
  42. int nb_planes;
  43. AVFrame *prev, *cur, *next; ///< previous, current, next frames
  44. int32_t **work_line; ///< lines we are calculating
  45. int nb_threads;
  46. int max;
  47. W3FDIFDSPContext dsp;
  48. } W3FDIFContext;
  49. #define OFFSET(x) offsetof(W3FDIFContext, x)
  50. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  51. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  52. static const AVOption w3fdif_options[] = {
  53. { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
  54. CONST("simple", NULL, 0, "filter"),
  55. CONST("complex", NULL, 1, "filter"),
  56. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode"},
  57. CONST("frame", "send one frame for each frame", 0, "mode"),
  58. CONST("field", "send one frame for each field", 1, "mode"),
  59. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
  60. CONST("tff", "assume top field first", 0, "parity"),
  61. CONST("bff", "assume bottom field first", 1, "parity"),
  62. CONST("auto", "auto detect parity", -1, "parity"),
  63. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
  64. CONST("all", "deinterlace all frames", 0, "deint"),
  65. CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(w3fdif);
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. static const enum AVPixelFormat pix_fmts[] = {
  72. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  73. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  74. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  75. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  76. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  77. AV_PIX_FMT_YUVJ411P,
  78. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  79. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  80. AV_PIX_FMT_GRAY8,
  81. AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  82. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  83. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  84. AV_PIX_FMT_YUV440P10,
  85. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  86. AV_PIX_FMT_YUV440P12,
  87. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  88. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  89. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  90. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  91. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  92. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  93. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  94. AV_PIX_FMT_NONE
  95. };
  96. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  97. if (!fmts_list)
  98. return AVERROR(ENOMEM);
  99. return ff_set_common_formats(ctx, fmts_list);
  100. }
  101. static void filter_simple_low(int32_t *work_line,
  102. uint8_t *in_lines_cur[2],
  103. const int16_t *coef, int linesize)
  104. {
  105. int i;
  106. for (i = 0; i < linesize; i++) {
  107. *work_line = *in_lines_cur[0]++ * coef[0];
  108. *work_line++ += *in_lines_cur[1]++ * coef[1];
  109. }
  110. }
  111. static void filter_complex_low(int32_t *work_line,
  112. uint8_t *in_lines_cur[4],
  113. const int16_t *coef, int linesize)
  114. {
  115. int i;
  116. for (i = 0; i < linesize; i++) {
  117. *work_line = *in_lines_cur[0]++ * coef[0];
  118. *work_line += *in_lines_cur[1]++ * coef[1];
  119. *work_line += *in_lines_cur[2]++ * coef[2];
  120. *work_line++ += *in_lines_cur[3]++ * coef[3];
  121. }
  122. }
  123. static void filter_simple_high(int32_t *work_line,
  124. uint8_t *in_lines_cur[3],
  125. uint8_t *in_lines_adj[3],
  126. const int16_t *coef, int linesize)
  127. {
  128. int i;
  129. for (i = 0; i < linesize; i++) {
  130. *work_line += *in_lines_cur[0]++ * coef[0];
  131. *work_line += *in_lines_adj[0]++ * coef[0];
  132. *work_line += *in_lines_cur[1]++ * coef[1];
  133. *work_line += *in_lines_adj[1]++ * coef[1];
  134. *work_line += *in_lines_cur[2]++ * coef[2];
  135. *work_line++ += *in_lines_adj[2]++ * coef[2];
  136. }
  137. }
  138. static void filter_complex_high(int32_t *work_line,
  139. uint8_t *in_lines_cur[5],
  140. uint8_t *in_lines_adj[5],
  141. const int16_t *coef, int linesize)
  142. {
  143. int i;
  144. for (i = 0; i < linesize; i++) {
  145. *work_line += *in_lines_cur[0]++ * coef[0];
  146. *work_line += *in_lines_adj[0]++ * coef[0];
  147. *work_line += *in_lines_cur[1]++ * coef[1];
  148. *work_line += *in_lines_adj[1]++ * coef[1];
  149. *work_line += *in_lines_cur[2]++ * coef[2];
  150. *work_line += *in_lines_adj[2]++ * coef[2];
  151. *work_line += *in_lines_cur[3]++ * coef[3];
  152. *work_line += *in_lines_adj[3]++ * coef[3];
  153. *work_line += *in_lines_cur[4]++ * coef[4];
  154. *work_line++ += *in_lines_adj[4]++ * coef[4];
  155. }
  156. }
  157. static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
  158. {
  159. int j;
  160. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  161. *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
  162. }
  163. static void filter16_simple_low(int32_t *work_line,
  164. uint8_t *in_lines_cur8[2],
  165. const int16_t *coef, int linesize)
  166. {
  167. uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
  168. int i;
  169. linesize /= 2;
  170. for (i = 0; i < linesize; i++) {
  171. *work_line = *in_lines_cur[0]++ * coef[0];
  172. *work_line++ += *in_lines_cur[1]++ * coef[1];
  173. }
  174. }
  175. static void filter16_complex_low(int32_t *work_line,
  176. uint8_t *in_lines_cur8[4],
  177. const int16_t *coef, int linesize)
  178. {
  179. uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
  180. (uint16_t *)in_lines_cur8[1],
  181. (uint16_t *)in_lines_cur8[2],
  182. (uint16_t *)in_lines_cur8[3] };
  183. int i;
  184. linesize /= 2;
  185. for (i = 0; i < linesize; i++) {
  186. *work_line = *in_lines_cur[0]++ * coef[0];
  187. *work_line += *in_lines_cur[1]++ * coef[1];
  188. *work_line += *in_lines_cur[2]++ * coef[2];
  189. *work_line++ += *in_lines_cur[3]++ * coef[3];
  190. }
  191. }
  192. static void filter16_simple_high(int32_t *work_line,
  193. uint8_t *in_lines_cur8[3],
  194. uint8_t *in_lines_adj8[3],
  195. const int16_t *coef, int linesize)
  196. {
  197. uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
  198. (uint16_t *)in_lines_cur8[1],
  199. (uint16_t *)in_lines_cur8[2] };
  200. uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
  201. (uint16_t *)in_lines_adj8[1],
  202. (uint16_t *)in_lines_adj8[2] };
  203. int i;
  204. linesize /= 2;
  205. for (i = 0; i < linesize; i++) {
  206. *work_line += *in_lines_cur[0]++ * coef[0];
  207. *work_line += *in_lines_adj[0]++ * coef[0];
  208. *work_line += *in_lines_cur[1]++ * coef[1];
  209. *work_line += *in_lines_adj[1]++ * coef[1];
  210. *work_line += *in_lines_cur[2]++ * coef[2];
  211. *work_line++ += *in_lines_adj[2]++ * coef[2];
  212. }
  213. }
  214. static void filter16_complex_high(int32_t *work_line,
  215. uint8_t *in_lines_cur8[5],
  216. uint8_t *in_lines_adj8[5],
  217. const int16_t *coef, int linesize)
  218. {
  219. uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
  220. (uint16_t *)in_lines_cur8[1],
  221. (uint16_t *)in_lines_cur8[2],
  222. (uint16_t *)in_lines_cur8[3],
  223. (uint16_t *)in_lines_cur8[4] };
  224. uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
  225. (uint16_t *)in_lines_adj8[1],
  226. (uint16_t *)in_lines_adj8[2],
  227. (uint16_t *)in_lines_adj8[3],
  228. (uint16_t *)in_lines_adj8[4] };
  229. int i;
  230. linesize /= 2;
  231. for (i = 0; i < linesize; i++) {
  232. *work_line += *in_lines_cur[0]++ * coef[0];
  233. *work_line += *in_lines_adj[0]++ * coef[0];
  234. *work_line += *in_lines_cur[1]++ * coef[1];
  235. *work_line += *in_lines_adj[1]++ * coef[1];
  236. *work_line += *in_lines_cur[2]++ * coef[2];
  237. *work_line += *in_lines_adj[2]++ * coef[2];
  238. *work_line += *in_lines_cur[3]++ * coef[3];
  239. *work_line += *in_lines_adj[3]++ * coef[3];
  240. *work_line += *in_lines_cur[4]++ * coef[4];
  241. *work_line++ += *in_lines_adj[4]++ * coef[4];
  242. }
  243. }
  244. static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
  245. {
  246. uint16_t *out_pixel = (uint16_t *)out_pixel8;
  247. int j;
  248. linesize /= 2;
  249. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  250. *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
  251. }
  252. static int config_input(AVFilterLink *inlink)
  253. {
  254. AVFilterContext *ctx = inlink->dst;
  255. W3FDIFContext *s = ctx->priv;
  256. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  257. int ret, i, depth;
  258. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  259. return ret;
  260. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  261. s->planeheight[0] = s->planeheight[3] = inlink->h;
  262. if (inlink->h < 3) {
  263. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
  264. return AVERROR(EINVAL);
  265. }
  266. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  267. s->nb_threads = ff_filter_get_nb_threads(ctx);
  268. s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
  269. if (!s->work_line)
  270. return AVERROR(ENOMEM);
  271. for (i = 0; i < s->nb_threads; i++) {
  272. s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
  273. if (!s->work_line[i])
  274. return AVERROR(ENOMEM);
  275. }
  276. depth = desc->comp[0].depth;
  277. s->max = ((1 << depth) - 1) * 256 * 128;
  278. if (depth <= 8) {
  279. s->dsp.filter_simple_low = filter_simple_low;
  280. s->dsp.filter_complex_low = filter_complex_low;
  281. s->dsp.filter_simple_high = filter_simple_high;
  282. s->dsp.filter_complex_high = filter_complex_high;
  283. s->dsp.filter_scale = filter_scale;
  284. } else {
  285. s->dsp.filter_simple_low = filter16_simple_low;
  286. s->dsp.filter_complex_low = filter16_complex_low;
  287. s->dsp.filter_simple_high = filter16_simple_high;
  288. s->dsp.filter_complex_high = filter16_complex_high;
  289. s->dsp.filter_scale = filter16_scale;
  290. }
  291. if (ARCH_X86)
  292. ff_w3fdif_init_x86(&s->dsp, depth);
  293. return 0;
  294. }
  295. static int config_output(AVFilterLink *outlink)
  296. {
  297. AVFilterLink *inlink = outlink->src->inputs[0];
  298. outlink->time_base.num = inlink->time_base.num;
  299. outlink->time_base.den = inlink->time_base.den * 2;
  300. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  301. outlink->frame_rate.den = inlink->frame_rate.den;
  302. return 0;
  303. }
  304. /*
  305. * Filter coefficients from PH-2071, scaled by 256 * 128.
  306. * Each set of coefficients has a set for low-frequencies and high-frequencies.
  307. * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
  308. * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
  309. * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
  310. * and high-frequencies for simple and more-complex mode.
  311. */
  312. static const int8_t n_coef_lf[2] = { 2, 4 };
  313. static const int16_t coef_lf[2][4] = {{ 16384, 16384, 0, 0},
  314. { -852, 17236, 17236, -852}};
  315. static const int8_t n_coef_hf[2] = { 3, 5 };
  316. static const int16_t coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0},
  317. { 1016, -3801, 5570, -3801, 1016}};
  318. typedef struct ThreadData {
  319. AVFrame *out, *cur, *adj;
  320. int plane;
  321. } ThreadData;
  322. static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  323. {
  324. W3FDIFContext *s = ctx->priv;
  325. ThreadData *td = arg;
  326. AVFrame *out = td->out;
  327. AVFrame *cur = td->cur;
  328. AVFrame *adj = td->adj;
  329. const int plane = td->plane;
  330. const int filter = s->filter;
  331. uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
  332. uint8_t *out_line, *out_pixel;
  333. int32_t *work_line, *work_pixel;
  334. uint8_t *cur_data = cur->data[plane];
  335. uint8_t *adj_data = adj->data[plane];
  336. uint8_t *dst_data = out->data[plane];
  337. const int linesize = s->linesize[plane];
  338. const int height = s->planeheight[plane];
  339. const int cur_line_stride = cur->linesize[plane];
  340. const int adj_line_stride = adj->linesize[plane];
  341. const int dst_line_stride = out->linesize[plane];
  342. const int start = (height * jobnr) / nb_jobs;
  343. const int end = (height * (jobnr+1)) / nb_jobs;
  344. const int max = s->max;
  345. const int interlaced = cur->interlaced_frame;
  346. const int tff = s->field == (s->parity == -1 ? interlaced ? cur->top_field_first : 1 :
  347. s->parity ^ 1);
  348. int j, y_in, y_out;
  349. /* copy unchanged the lines of the field */
  350. y_out = start + (tff ^ (start & 1));
  351. in_line = cur_data + (y_out * cur_line_stride);
  352. out_line = dst_data + (y_out * dst_line_stride);
  353. while (y_out < end) {
  354. memcpy(out_line, in_line, linesize);
  355. y_out += 2;
  356. in_line += cur_line_stride * 2;
  357. out_line += dst_line_stride * 2;
  358. }
  359. /* interpolate other lines of the field */
  360. y_out = start + ((!tff) ^ (start & 1));
  361. out_line = dst_data + (y_out * dst_line_stride);
  362. while (y_out < end) {
  363. /* get low vertical frequencies from current field */
  364. for (j = 0; j < n_coef_lf[filter]; j++) {
  365. y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
  366. while (y_in < 0)
  367. y_in += 2;
  368. while (y_in >= height)
  369. y_in -= 2;
  370. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  371. }
  372. work_line = s->work_line[jobnr];
  373. switch (n_coef_lf[filter]) {
  374. case 2:
  375. s->dsp.filter_simple_low(work_line, in_lines_cur,
  376. coef_lf[filter], linesize);
  377. break;
  378. case 4:
  379. s->dsp.filter_complex_low(work_line, in_lines_cur,
  380. coef_lf[filter], linesize);
  381. }
  382. /* get high vertical frequencies from adjacent fields */
  383. for (j = 0; j < n_coef_hf[filter]; j++) {
  384. y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
  385. while (y_in < 0)
  386. y_in += 2;
  387. while (y_in >= height)
  388. y_in -= 2;
  389. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  390. in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
  391. }
  392. work_line = s->work_line[jobnr];
  393. switch (n_coef_hf[filter]) {
  394. case 3:
  395. s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
  396. coef_hf[filter], linesize);
  397. break;
  398. case 5:
  399. s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
  400. coef_hf[filter], linesize);
  401. }
  402. /* save scaled result to the output frame, scaling down by 256 * 128 */
  403. work_pixel = s->work_line[jobnr];
  404. out_pixel = out_line;
  405. s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
  406. /* move on to next line */
  407. y_out += 2;
  408. out_line += dst_line_stride * 2;
  409. }
  410. return 0;
  411. }
  412. static int filter(AVFilterContext *ctx, int is_second)
  413. {
  414. W3FDIFContext *s = ctx->priv;
  415. AVFilterLink *outlink = ctx->outputs[0];
  416. AVFrame *out, *adj;
  417. ThreadData td;
  418. int plane;
  419. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  420. if (!out)
  421. return AVERROR(ENOMEM);
  422. av_frame_copy_props(out, s->cur);
  423. out->interlaced_frame = 0;
  424. if (!is_second) {
  425. if (out->pts != AV_NOPTS_VALUE)
  426. out->pts *= 2;
  427. } else {
  428. int64_t cur_pts = s->cur->pts;
  429. int64_t next_pts = s->next->pts;
  430. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  431. out->pts = cur_pts + next_pts;
  432. } else {
  433. out->pts = AV_NOPTS_VALUE;
  434. }
  435. }
  436. adj = s->field ? s->next : s->prev;
  437. td.out = out; td.cur = s->cur; td.adj = adj;
  438. for (plane = 0; plane < s->nb_planes; plane++) {
  439. td.plane = plane;
  440. ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
  441. }
  442. if (s->mode)
  443. s->field = !s->field;
  444. return ff_filter_frame(outlink, out);
  445. }
  446. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  447. {
  448. AVFilterContext *ctx = inlink->dst;
  449. W3FDIFContext *s = ctx->priv;
  450. int ret;
  451. av_frame_free(&s->prev);
  452. s->prev = s->cur;
  453. s->cur = s->next;
  454. s->next = frame;
  455. if (!s->cur) {
  456. s->cur = av_frame_clone(s->next);
  457. if (!s->cur)
  458. return AVERROR(ENOMEM);
  459. }
  460. if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
  461. AVFrame *out = av_frame_clone(s->cur);
  462. if (!out)
  463. return AVERROR(ENOMEM);
  464. av_frame_free(&s->prev);
  465. if (out->pts != AV_NOPTS_VALUE)
  466. out->pts *= 2;
  467. return ff_filter_frame(ctx->outputs[0], out);
  468. }
  469. if (!s->prev)
  470. return 0;
  471. ret = filter(ctx, 0);
  472. if (ret < 0 || s->mode == 0)
  473. return ret;
  474. return filter(ctx, 1);
  475. }
  476. static int request_frame(AVFilterLink *outlink)
  477. {
  478. AVFilterContext *ctx = outlink->src;
  479. W3FDIFContext *s = ctx->priv;
  480. int ret;
  481. if (s->eof)
  482. return AVERROR_EOF;
  483. ret = ff_request_frame(ctx->inputs[0]);
  484. if (ret == AVERROR_EOF && s->cur) {
  485. AVFrame *next = av_frame_clone(s->next);
  486. if (!next)
  487. return AVERROR(ENOMEM);
  488. next->pts = s->next->pts * 2 - s->cur->pts;
  489. filter_frame(ctx->inputs[0], next);
  490. s->eof = 1;
  491. } else if (ret < 0) {
  492. return ret;
  493. }
  494. return 0;
  495. }
  496. static av_cold void uninit(AVFilterContext *ctx)
  497. {
  498. W3FDIFContext *s = ctx->priv;
  499. int i;
  500. av_frame_free(&s->prev);
  501. av_frame_free(&s->cur );
  502. av_frame_free(&s->next);
  503. for (i = 0; i < s->nb_threads; i++)
  504. av_freep(&s->work_line[i]);
  505. av_freep(&s->work_line);
  506. }
  507. static const AVFilterPad w3fdif_inputs[] = {
  508. {
  509. .name = "default",
  510. .type = AVMEDIA_TYPE_VIDEO,
  511. .filter_frame = filter_frame,
  512. .config_props = config_input,
  513. },
  514. { NULL }
  515. };
  516. static const AVFilterPad w3fdif_outputs[] = {
  517. {
  518. .name = "default",
  519. .type = AVMEDIA_TYPE_VIDEO,
  520. .config_props = config_output,
  521. .request_frame = request_frame,
  522. },
  523. { NULL }
  524. };
  525. AVFilter ff_vf_w3fdif = {
  526. .name = "w3fdif",
  527. .description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
  528. .priv_size = sizeof(W3FDIFContext),
  529. .priv_class = &w3fdif_class,
  530. .uninit = uninit,
  531. .query_formats = query_formats,
  532. .inputs = w3fdif_inputs,
  533. .outputs = w3fdif_outputs,
  534. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  535. .process_command = ff_filter_process_command,
  536. };