vf_kerndeint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2012 Jeremy Tran
  3. * Copyright (c) 2004 Tobias Diedrich
  4. * Copyright (c) 2003 Donald A. Graft
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * Kernel Deinterlacer
  25. * Ported from MPlayer libmpcodecs/vf_kerndeint.c.
  26. */
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. typedef struct KerndeintContext {
  35. const AVClass *class;
  36. int frame; ///< frame count, starting from 0
  37. int thresh, map, order, sharp, twoway;
  38. int vsub;
  39. int is_packed_rgb;
  40. uint8_t *tmp_data [4]; ///< temporary plane data buffer
  41. int tmp_linesize[4]; ///< temporary plane byte linesize
  42. int tmp_bwidth [4]; ///< temporary plane byte width
  43. } KerndeintContext;
  44. #define OFFSET(x) offsetof(KerndeintContext, x)
  45. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption kerndeint_options[] = {
  47. { "thresh", "set the threshold", OFFSET(thresh), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
  48. { "map", "set the map", OFFSET(map), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  49. { "order", "set the order", OFFSET(order), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  50. { "sharp", "set sharpening", OFFSET(sharp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  51. { "twoway", "set twoway", OFFSET(twoway), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  52. { NULL }
  53. };
  54. AVFILTER_DEFINE_CLASS(kerndeint);
  55. static av_cold void uninit(AVFilterContext *ctx)
  56. {
  57. KerndeintContext *kerndeint = ctx->priv;
  58. av_freep(&kerndeint->tmp_data[0]);
  59. }
  60. static int query_formats(AVFilterContext *ctx)
  61. {
  62. static const enum AVPixelFormat pix_fmts[] = {
  63. AV_PIX_FMT_YUV420P,
  64. AV_PIX_FMT_YUYV422,
  65. AV_PIX_FMT_ARGB, AV_PIX_FMT_0RGB,
  66. AV_PIX_FMT_ABGR, AV_PIX_FMT_0BGR,
  67. AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB0,
  68. AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,
  69. AV_PIX_FMT_NONE
  70. };
  71. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  72. if (!fmts_list)
  73. return AVERROR(ENOMEM);
  74. return ff_set_common_formats(ctx, fmts_list);
  75. }
  76. static int config_props(AVFilterLink *inlink)
  77. {
  78. KerndeintContext *kerndeint = inlink->dst->priv;
  79. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  80. int ret;
  81. kerndeint->is_packed_rgb = av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_RGB;
  82. kerndeint->vsub = desc->log2_chroma_h;
  83. ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_linesize,
  84. inlink->w, inlink->h, inlink->format, 16);
  85. if (ret < 0)
  86. return ret;
  87. memset(kerndeint->tmp_data[0], 0, ret);
  88. if ((ret = av_image_fill_linesizes(kerndeint->tmp_bwidth, inlink->format, inlink->w)) < 0)
  89. return ret;
  90. return 0;
  91. }
  92. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  93. {
  94. KerndeintContext *kerndeint = inlink->dst->priv;
  95. AVFilterLink *outlink = inlink->dst->outputs[0];
  96. AVFrame *outpic;
  97. const uint8_t *prvp; ///< Previous field's pixel line number n
  98. const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1)
  99. const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1)
  100. const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2)
  101. const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2)
  102. const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4)
  103. const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4)
  104. const uint8_t *srcp; ///< Current field's pixel line number n
  105. const uint8_t *srcpp; ///< Current field's pixel line number (n - 1)
  106. const uint8_t *srcpn; ///< Current field's pixel line number (n + 1)
  107. const uint8_t *srcppp; ///< Current field's pixel line number (n - 2)
  108. const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2)
  109. const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3)
  110. const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3)
  111. const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4)
  112. const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4)
  113. uint8_t *dstp, *dstp_saved;
  114. const uint8_t *srcp_saved;
  115. int src_linesize, psrc_linesize, dst_linesize, bwidth;
  116. int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++;
  117. double valf;
  118. const int thresh = kerndeint->thresh;
  119. const int order = kerndeint->order;
  120. const int map = kerndeint->map;
  121. const int sharp = kerndeint->sharp;
  122. const int twoway = kerndeint->twoway;
  123. const int is_packed_rgb = kerndeint->is_packed_rgb;
  124. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  125. if (!outpic) {
  126. av_frame_free(&inpic);
  127. return AVERROR(ENOMEM);
  128. }
  129. av_frame_copy_props(outpic, inpic);
  130. outpic->interlaced_frame = 0;
  131. for (plane = 0; plane < 4 && inpic->data[plane] && inpic->linesize[plane]; plane++) {
  132. h = plane == 0 ? inlink->h : AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub);
  133. bwidth = kerndeint->tmp_bwidth[plane];
  134. srcp_saved = inpic->data[plane];
  135. src_linesize = inpic->linesize[plane];
  136. psrc_linesize = kerndeint->tmp_linesize[plane];
  137. dstp_saved = outpic->data[plane];
  138. dst_linesize = outpic->linesize[plane];
  139. srcp = srcp_saved + (1 - order) * src_linesize;
  140. dstp = dstp_saved + (1 - order) * dst_linesize;
  141. for (y = 0; y < h; y += 2) {
  142. memcpy(dstp, srcp, bwidth);
  143. srcp += 2 * src_linesize;
  144. dstp += 2 * dst_linesize;
  145. }
  146. // Copy through the lines that will be missed below.
  147. memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth);
  148. memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth);
  149. memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth);
  150. memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth);
  151. /* For the other field choose adaptively between using the previous field
  152. or the interpolant from the current field. */
  153. prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
  154. prvpp = prvp - psrc_linesize;
  155. prvppp = prvp - 2 * psrc_linesize;
  156. prvp4p = prvp - 4 * psrc_linesize;
  157. prvpn = prvp + psrc_linesize;
  158. prvpnn = prvp + 2 * psrc_linesize;
  159. prvp4n = prvp + 4 * psrc_linesize;
  160. srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
  161. srcpp = srcp - src_linesize;
  162. srcppp = srcp - 2 * src_linesize;
  163. srcp3p = srcp - 3 * src_linesize;
  164. srcp4p = srcp - 4 * src_linesize;
  165. srcpn = srcp + src_linesize;
  166. srcpnn = srcp + 2 * src_linesize;
  167. srcp3n = srcp + 3 * src_linesize;
  168. srcp4n = srcp + 4 * src_linesize;
  169. dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
  170. for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
  171. for (x = 0; x < bwidth; x++) {
  172. if (thresh == 0 || n == 0 ||
  173. (abs((int)prvp[x] - (int)srcp[x]) > thresh) ||
  174. (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) ||
  175. (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) {
  176. if (map) {
  177. g = x & ~3;
  178. if (is_packed_rgb) {
  179. AV_WB32(dstp + g, 0xffffffff);
  180. x = g + 3;
  181. } else if (inlink->format == AV_PIX_FMT_YUYV422) {
  182. // y <- 235, u <- 128, y <- 235, v <- 128
  183. AV_WB32(dstp + g, 0xeb80eb80);
  184. x = g + 3;
  185. } else {
  186. dstp[x] = plane == 0 ? 235 : 128;
  187. }
  188. } else {
  189. if (is_packed_rgb) {
  190. hi = 255;
  191. lo = 0;
  192. } else if (inlink->format == AV_PIX_FMT_YUYV422) {
  193. hi = x & 1 ? 240 : 235;
  194. lo = 16;
  195. } else {
  196. hi = plane == 0 ? 235 : 240;
  197. lo = 16;
  198. }
  199. if (sharp) {
  200. if (twoway) {
  201. valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
  202. + 0.170 * ((int)srcp[x] + (int)prvp[x])
  203. - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
  204. - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
  205. + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
  206. } else {
  207. valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
  208. + 0.170 * ((int)prvp[x])
  209. - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
  210. - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
  211. + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
  212. }
  213. dstp[x] = av_clip(valf, lo, hi);
  214. } else {
  215. if (twoway) {
  216. val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x])
  217. - (int)(srcppp[x]) - (int)(srcpnn[x])
  218. - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
  219. } else {
  220. val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x])
  221. - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
  222. }
  223. dstp[x] = av_clip(val, lo, hi);
  224. }
  225. }
  226. } else {
  227. dstp[x] = srcp[x];
  228. }
  229. }
  230. prvp += 2 * psrc_linesize;
  231. prvpp += 2 * psrc_linesize;
  232. prvppp += 2 * psrc_linesize;
  233. prvpn += 2 * psrc_linesize;
  234. prvpnn += 2 * psrc_linesize;
  235. prvp4p += 2 * psrc_linesize;
  236. prvp4n += 2 * psrc_linesize;
  237. srcp += 2 * src_linesize;
  238. srcpp += 2 * src_linesize;
  239. srcppp += 2 * src_linesize;
  240. srcp3p += 2 * src_linesize;
  241. srcp4p += 2 * src_linesize;
  242. srcpn += 2 * src_linesize;
  243. srcpnn += 2 * src_linesize;
  244. srcp3n += 2 * src_linesize;
  245. srcp4n += 2 * src_linesize;
  246. dstp += 2 * dst_linesize;
  247. }
  248. srcp = inpic->data[plane];
  249. dstp = kerndeint->tmp_data[plane];
  250. av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
  251. }
  252. av_frame_free(&inpic);
  253. return ff_filter_frame(outlink, outpic);
  254. }
  255. static const AVFilterPad kerndeint_inputs[] = {
  256. {
  257. .name = "default",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. .filter_frame = filter_frame,
  260. .config_props = config_props,
  261. },
  262. { NULL }
  263. };
  264. static const AVFilterPad kerndeint_outputs[] = {
  265. {
  266. .name = "default",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. },
  269. { NULL }
  270. };
  271. AVFilter ff_vf_kerndeint = {
  272. .name = "kerndeint",
  273. .description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
  274. .priv_size = sizeof(KerndeintContext),
  275. .priv_class = &kerndeint_class,
  276. .uninit = uninit,
  277. .query_formats = query_formats,
  278. .inputs = kerndeint_inputs,
  279. .outputs = kerndeint_outputs,
  280. };