vf_bwdif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * BobWeaver Deinterlacing Filter
  3. * Copyright (C) 2016 Thomas Mundt <loudmax@yahoo.de>
  4. *
  5. * Based on YADIF (Yet Another Deinterlacing Filter)
  6. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  7. * 2010 James Darnley <james.darnley@gmail.com>
  8. *
  9. * With use of Weston 3 Field Deinterlacing Filter algorithm
  10. * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
  11. * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
  12. * Based on the process described by Martin Weston for BBC R&D
  13. *
  14. * This file is part of FFmpeg.
  15. *
  16. * FFmpeg is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2.1 of the License, or (at your option) any later version.
  20. *
  21. * FFmpeg is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with FFmpeg; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "avfilter.h"
  36. #include "formats.h"
  37. #include "internal.h"
  38. #include "video.h"
  39. #include "bwdif.h"
  40. /*
  41. * Filter coefficients coef_lf and coef_hf taken from BBC PH-2071 (Weston 3 Field Deinterlacer).
  42. * Used when there is spatial and temporal interpolation.
  43. * Filter coefficients coef_sp are used when there is spatial interpolation only.
  44. * Adjusted for matching visual sharpness impression of spatial and temporal interpolation.
  45. */
  46. static const uint16_t coef_lf[2] = { 4309, 213 };
  47. static const uint16_t coef_hf[3] = { 5570, 3801, 1016 };
  48. static const uint16_t coef_sp[2] = { 5077, 981 };
  49. typedef struct ThreadData {
  50. AVFrame *frame;
  51. int plane;
  52. int w, h;
  53. int parity;
  54. int tff;
  55. } ThreadData;
  56. #define FILTER_INTRA() \
  57. for (x = 0; x < w; x++) { \
  58. interpol = (coef_sp[0] * (cur[mrefs] + cur[prefs]) - coef_sp[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
  59. dst[0] = av_clip(interpol, 0, clip_max); \
  60. \
  61. dst++; \
  62. cur++; \
  63. }
  64. #define FILTER1() \
  65. for (x = 0; x < w; x++) { \
  66. int c = cur[mrefs]; \
  67. int d = (prev2[0] + next2[0]) >> 1; \
  68. int e = cur[prefs]; \
  69. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  70. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e)) >> 1; \
  71. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e)) >> 1; \
  72. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  73. \
  74. if (!diff) { \
  75. dst[0] = d; \
  76. } else {
  77. #define SPAT_CHECK() \
  78. int b = ((prev2[mrefs2] + next2[mrefs2]) >> 1) - c; \
  79. int f = ((prev2[prefs2] + next2[prefs2]) >> 1) - e; \
  80. int dc = d - c; \
  81. int de = d - e; \
  82. int max = FFMAX3(de, dc, FFMIN(b, f)); \
  83. int min = FFMIN3(de, dc, FFMAX(b, f)); \
  84. diff = FFMAX3(diff, min, -max);
  85. #define FILTER_LINE() \
  86. SPAT_CHECK() \
  87. if (FFABS(c - e) > temporal_diff0) { \
  88. interpol = (((coef_hf[0] * (prev2[0] + next2[0]) \
  89. - coef_hf[1] * (prev2[mrefs2] + next2[mrefs2] + prev2[prefs2] + next2[prefs2]) \
  90. + coef_hf[2] * (prev2[mrefs4] + next2[mrefs4] + prev2[prefs4] + next2[prefs4])) >> 2) \
  91. + coef_lf[0] * (c + e) - coef_lf[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
  92. } else { \
  93. interpol = (coef_sp[0] * (c + e) - coef_sp[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
  94. }
  95. #define FILTER_EDGE() \
  96. if (spat) { \
  97. SPAT_CHECK() \
  98. } \
  99. interpol = (c + e) >> 1;
  100. #define FILTER2() \
  101. if (interpol > d + diff) \
  102. interpol = d + diff; \
  103. else if (interpol < d - diff) \
  104. interpol = d - diff; \
  105. \
  106. dst[0] = av_clip(interpol, 0, clip_max); \
  107. } \
  108. \
  109. dst++; \
  110. cur++; \
  111. prev++; \
  112. next++; \
  113. prev2++; \
  114. next2++; \
  115. }
  116. static void filter_intra(void *dst1, void *cur1, int w, int prefs, int mrefs,
  117. int prefs3, int mrefs3, int parity, int clip_max)
  118. {
  119. uint8_t *dst = dst1;
  120. uint8_t *cur = cur1;
  121. int interpol, x;
  122. FILTER_INTRA()
  123. }
  124. static void filter_line_c(void *dst1, void *prev1, void *cur1, void *next1,
  125. int w, int prefs, int mrefs, int prefs2, int mrefs2,
  126. int prefs3, int mrefs3, int prefs4, int mrefs4,
  127. int parity, int clip_max)
  128. {
  129. uint8_t *dst = dst1;
  130. uint8_t *prev = prev1;
  131. uint8_t *cur = cur1;
  132. uint8_t *next = next1;
  133. uint8_t *prev2 = parity ? prev : cur ;
  134. uint8_t *next2 = parity ? cur : next;
  135. int interpol, x;
  136. FILTER1()
  137. FILTER_LINE()
  138. FILTER2()
  139. }
  140. static void filter_edge(void *dst1, void *prev1, void *cur1, void *next1,
  141. int w, int prefs, int mrefs, int prefs2, int mrefs2,
  142. int parity, int clip_max, int spat)
  143. {
  144. uint8_t *dst = dst1;
  145. uint8_t *prev = prev1;
  146. uint8_t *cur = cur1;
  147. uint8_t *next = next1;
  148. uint8_t *prev2 = parity ? prev : cur ;
  149. uint8_t *next2 = parity ? cur : next;
  150. int interpol, x;
  151. FILTER1()
  152. FILTER_EDGE()
  153. FILTER2()
  154. }
  155. static void filter_intra_16bit(void *dst1, void *cur1, int w, int prefs, int mrefs,
  156. int prefs3, int mrefs3, int parity, int clip_max)
  157. {
  158. uint16_t *dst = dst1;
  159. uint16_t *cur = cur1;
  160. int interpol, x;
  161. FILTER_INTRA()
  162. }
  163. static void filter_line_c_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  164. int w, int prefs, int mrefs, int prefs2, int mrefs2,
  165. int prefs3, int mrefs3, int prefs4, int mrefs4,
  166. int parity, int clip_max)
  167. {
  168. uint16_t *dst = dst1;
  169. uint16_t *prev = prev1;
  170. uint16_t *cur = cur1;
  171. uint16_t *next = next1;
  172. uint16_t *prev2 = parity ? prev : cur ;
  173. uint16_t *next2 = parity ? cur : next;
  174. int interpol, x;
  175. FILTER1()
  176. FILTER_LINE()
  177. FILTER2()
  178. }
  179. static void filter_edge_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  180. int w, int prefs, int mrefs, int prefs2, int mrefs2,
  181. int parity, int clip_max, int spat)
  182. {
  183. uint16_t *dst = dst1;
  184. uint16_t *prev = prev1;
  185. uint16_t *cur = cur1;
  186. uint16_t *next = next1;
  187. uint16_t *prev2 = parity ? prev : cur ;
  188. uint16_t *next2 = parity ? cur : next;
  189. int interpol, x;
  190. FILTER1()
  191. FILTER_EDGE()
  192. FILTER2()
  193. }
  194. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  195. {
  196. BWDIFContext *s = ctx->priv;
  197. YADIFContext *yadif = &s->yadif;
  198. ThreadData *td = arg;
  199. int linesize = yadif->cur->linesize[td->plane];
  200. int clip_max = (1 << (yadif->csp->comp[td->plane].depth)) - 1;
  201. int df = (yadif->csp->comp[td->plane].depth + 7) / 8;
  202. int refs = linesize / df;
  203. int slice_start = (td->h * jobnr ) / nb_jobs;
  204. int slice_end = (td->h * (jobnr+1)) / nb_jobs;
  205. int y;
  206. for (y = slice_start; y < slice_end; y++) {
  207. if ((y ^ td->parity) & 1) {
  208. uint8_t *prev = &yadif->prev->data[td->plane][y * linesize];
  209. uint8_t *cur = &yadif->cur ->data[td->plane][y * linesize];
  210. uint8_t *next = &yadif->next->data[td->plane][y * linesize];
  211. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  212. if (yadif->current_field == YADIF_FIELD_END) {
  213. s->filter_intra(dst, cur, td->w, (y + df) < td->h ? refs : -refs,
  214. y > (df - 1) ? -refs : refs,
  215. (y + 3*df) < td->h ? 3 * refs : -refs,
  216. y > (3*df - 1) ? -3 * refs : refs,
  217. td->parity ^ td->tff, clip_max);
  218. } else if ((y < 4) || ((y + 5) > td->h)) {
  219. s->filter_edge(dst, prev, cur, next, td->w,
  220. (y + df) < td->h ? refs : -refs,
  221. y > (df - 1) ? -refs : refs,
  222. refs << 1, -(refs << 1),
  223. td->parity ^ td->tff, clip_max,
  224. (y < 2) || ((y + 3) > td->h) ? 0 : 1);
  225. } else {
  226. s->filter_line(dst, prev, cur, next, td->w,
  227. refs, -refs, refs << 1, -(refs << 1),
  228. 3 * refs, -3 * refs, refs << 2, -(refs << 2),
  229. td->parity ^ td->tff, clip_max);
  230. }
  231. } else {
  232. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  233. &yadif->cur->data[td->plane][y * linesize], td->w * df);
  234. }
  235. }
  236. return 0;
  237. }
  238. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  239. int parity, int tff)
  240. {
  241. BWDIFContext *bwdif = ctx->priv;
  242. YADIFContext *yadif = &bwdif->yadif;
  243. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  244. int i;
  245. for (i = 0; i < yadif->csp->nb_components; i++) {
  246. int w = dstpic->width;
  247. int h = dstpic->height;
  248. if (i == 1 || i == 2) {
  249. w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
  250. h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
  251. }
  252. td.w = w;
  253. td.h = h;
  254. td.plane = i;
  255. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
  256. }
  257. if (yadif->current_field == YADIF_FIELD_END) {
  258. yadif->current_field = YADIF_FIELD_NORMAL;
  259. }
  260. emms_c();
  261. }
  262. static av_cold void uninit(AVFilterContext *ctx)
  263. {
  264. BWDIFContext *bwdif = ctx->priv;
  265. YADIFContext *yadif = &bwdif->yadif;
  266. av_frame_free(&yadif->prev);
  267. av_frame_free(&yadif->cur );
  268. av_frame_free(&yadif->next);
  269. }
  270. static int query_formats(AVFilterContext *ctx)
  271. {
  272. static const enum AVPixelFormat pix_fmts[] = {
  273. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P,
  274. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  275. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
  276. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  277. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  278. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  279. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  280. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  281. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  282. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  283. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  284. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  285. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  286. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  287. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  288. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
  289. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  290. AV_PIX_FMT_NONE
  291. };
  292. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  293. if (!fmts_list)
  294. return AVERROR(ENOMEM);
  295. return ff_set_common_formats(ctx, fmts_list);
  296. }
  297. static int config_props(AVFilterLink *link)
  298. {
  299. AVFilterContext *ctx = link->src;
  300. BWDIFContext *s = link->src->priv;
  301. YADIFContext *yadif = &s->yadif;
  302. link->time_base.num = link->src->inputs[0]->time_base.num;
  303. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  304. link->w = link->src->inputs[0]->w;
  305. link->h = link->src->inputs[0]->h;
  306. if(yadif->mode&1)
  307. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  308. if (link->w < 3 || link->h < 3) {
  309. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  310. return AVERROR(EINVAL);
  311. }
  312. yadif->csp = av_pix_fmt_desc_get(link->format);
  313. yadif->filter = filter;
  314. if (yadif->csp->comp[0].depth > 8) {
  315. s->filter_intra = filter_intra_16bit;
  316. s->filter_line = filter_line_c_16bit;
  317. s->filter_edge = filter_edge_16bit;
  318. } else {
  319. s->filter_intra = filter_intra;
  320. s->filter_line = filter_line_c;
  321. s->filter_edge = filter_edge;
  322. }
  323. if (ARCH_X86)
  324. ff_bwdif_init_x86(s);
  325. return 0;
  326. }
  327. #define OFFSET(x) offsetof(YADIFContext, x)
  328. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  329. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  330. static const AVOption bwdif_options[] = {
  331. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FIELD}, 0, 1, FLAGS, "mode"},
  332. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  333. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  334. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  335. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  336. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  337. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  338. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  339. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  340. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  341. { NULL }
  342. };
  343. AVFILTER_DEFINE_CLASS(bwdif);
  344. static const AVFilterPad avfilter_vf_bwdif_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .filter_frame = ff_yadif_filter_frame,
  349. },
  350. { NULL }
  351. };
  352. static const AVFilterPad avfilter_vf_bwdif_outputs[] = {
  353. {
  354. .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .request_frame = ff_yadif_request_frame,
  357. .config_props = config_props,
  358. },
  359. { NULL }
  360. };
  361. AVFilter ff_vf_bwdif = {
  362. .name = "bwdif",
  363. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  364. .priv_size = sizeof(BWDIFContext),
  365. .priv_class = &bwdif_class,
  366. .uninit = uninit,
  367. .query_formats = query_formats,
  368. .inputs = avfilter_vf_bwdif_inputs,
  369. .outputs = avfilter_vf_bwdif_outputs,
  370. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  371. };