vf_mcdeint.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. /**
  19. * @file
  20. * Motion Compensation Deinterlacer
  21. * Ported from MPlayer libmpcodecs/vf_mcdeint.c.
  22. *
  23. * Known Issues:
  24. *
  25. * The motion estimation is somewhat at the mercy of the input, if the
  26. * input frames are created purely based on spatial interpolation then
  27. * for example a thin black line or another random and not
  28. * interpolateable pattern will cause problems.
  29. * Note: completely ignoring the "unavailable" lines during motion
  30. * estimation did not look any better, so the most obvious solution
  31. * would be to improve tfields or penalize problematic motion vectors.
  32. *
  33. * If non iterative ME is used then snow currently ignores the OBMC
  34. * window and as a result sometimes creates artifacts.
  35. *
  36. * Only past frames are used, we should ideally use future frames too,
  37. * something like filtering the whole movie in forward and then
  38. * backward direction seems like a interesting idea but the current
  39. * filter framework is FAR from supporting such things.
  40. *
  41. * Combining the motion compensated image with the input image also is
  42. * not as trivial as it seems, simple blindly taking even lines from
  43. * one and odd ones from the other does not work at all as ME/MC
  44. * sometimes has nothing in the previous frames which matches the
  45. * current. The current algorithm has been found by trial and error
  46. * and almost certainly can be improved...
  47. */
  48. #include "libavutil/opt.h"
  49. #include "libavutil/pixdesc.h"
  50. #include "libavcodec/avcodec.h"
  51. #include "avfilter.h"
  52. #include "formats.h"
  53. #include "internal.h"
  54. enum MCDeintMode {
  55. MODE_FAST = 0,
  56. MODE_MEDIUM,
  57. MODE_SLOW,
  58. MODE_EXTRA_SLOW,
  59. MODE_NB,
  60. };
  61. enum MCDeintParity {
  62. PARITY_TFF = 0, ///< top field first
  63. PARITY_BFF = 1, ///< bottom field first
  64. };
  65. typedef struct {
  66. const AVClass *class;
  67. enum MCDeintMode mode;
  68. enum MCDeintParity parity;
  69. int qp;
  70. AVCodecContext *enc_ctx;
  71. } MCDeintContext;
  72. #define OFFSET(x) offsetof(MCDeintContext, x)
  73. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  74. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  75. static const AVOption mcdeint_options[] = {
  76. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_FAST}, 0, MODE_NB-1, FLAGS, .unit="mode" },
  77. CONST("fast", NULL, MODE_FAST, "mode"),
  78. CONST("medium", NULL, MODE_MEDIUM, "mode"),
  79. CONST("slow", NULL, MODE_SLOW, "mode"),
  80. CONST("extra_slow", NULL, MODE_EXTRA_SLOW, "mode"),
  81. { "parity", "set the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=PARITY_BFF}, -1, 1, FLAGS, "parity" },
  82. CONST("tff", "assume top field first", PARITY_TFF, "parity"),
  83. CONST("bff", "assume bottom field first", PARITY_BFF, "parity"),
  84. { "qp", "set qp", OFFSET(qp), AV_OPT_TYPE_INT, {.i64=1}, INT_MIN, INT_MAX, FLAGS },
  85. { NULL }
  86. };
  87. AVFILTER_DEFINE_CLASS(mcdeint);
  88. static int config_props(AVFilterLink *inlink)
  89. {
  90. AVFilterContext *ctx = inlink->dst;
  91. MCDeintContext *mcdeint = ctx->priv;
  92. AVCodec *enc;
  93. AVCodecContext *enc_ctx;
  94. AVDictionary *opts = NULL;
  95. int ret;
  96. if (!(enc = avcodec_find_encoder(AV_CODEC_ID_SNOW))) {
  97. av_log(ctx, AV_LOG_ERROR, "Snow encoder is not enabled in libavcodec\n");
  98. return AVERROR(EINVAL);
  99. }
  100. mcdeint->enc_ctx = avcodec_alloc_context3(enc);
  101. if (!mcdeint->enc_ctx)
  102. return AVERROR(ENOMEM);
  103. enc_ctx = mcdeint->enc_ctx;
  104. enc_ctx->width = inlink->w;
  105. enc_ctx->height = inlink->h;
  106. enc_ctx->time_base = (AVRational){1,25}; // meaningless
  107. enc_ctx->gop_size = 300;
  108. enc_ctx->max_b_frames = 0;
  109. enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  110. enc_ctx->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY;
  111. enc_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  112. enc_ctx->global_quality = 1;
  113. enc_ctx->me_cmp = enc_ctx->me_sub_cmp = FF_CMP_SAD;
  114. enc_ctx->mb_cmp = FF_CMP_SSE;
  115. av_dict_set(&opts, "memc_only", "1", 0);
  116. switch (mcdeint->mode) {
  117. case MODE_EXTRA_SLOW:
  118. enc_ctx->refs = 3;
  119. case MODE_SLOW:
  120. enc_ctx->me_method = ME_ITER;
  121. case MODE_MEDIUM:
  122. enc_ctx->flags |= CODEC_FLAG_4MV;
  123. enc_ctx->dia_size = 2;
  124. case MODE_FAST:
  125. enc_ctx->flags |= CODEC_FLAG_QPEL;
  126. }
  127. ret = avcodec_open2(enc_ctx, enc, &opts);
  128. av_dict_free(&opts);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static av_cold void uninit(AVFilterContext *ctx)
  134. {
  135. MCDeintContext *mcdeint = ctx->priv;
  136. if (mcdeint->enc_ctx) {
  137. avcodec_close(mcdeint->enc_ctx);
  138. av_freep(&mcdeint->enc_ctx);
  139. }
  140. }
  141. static int query_formats(AVFilterContext *ctx)
  142. {
  143. static const enum PixelFormat pix_fmts[] = {
  144. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  145. };
  146. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  147. return 0;
  148. }
  149. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  150. {
  151. MCDeintContext *mcdeint = inlink->dst->priv;
  152. AVFilterLink *outlink = inlink->dst->outputs[0];
  153. AVFrame *outpic, *frame_dec;
  154. AVPacket pkt;
  155. int x, y, i, ret, got_frame = 0;
  156. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  157. if (!outpic) {
  158. av_frame_free(&inpic);
  159. return AVERROR(ENOMEM);
  160. }
  161. av_frame_copy_props(outpic, inpic);
  162. inpic->quality = mcdeint->qp * FF_QP2LAMBDA;
  163. av_init_packet(&pkt);
  164. pkt.data = NULL; // packet data will be allocated by the encoder
  165. pkt.size = 0;
  166. ret = avcodec_encode_video2(mcdeint->enc_ctx, &pkt, inpic, &got_frame);
  167. if (ret < 0)
  168. goto end;
  169. frame_dec = mcdeint->enc_ctx->coded_frame;
  170. for (i = 0; i < 3; i++) {
  171. int is_chroma = !!i;
  172. int w = FF_CEIL_RSHIFT(inlink->w, is_chroma);
  173. int h = FF_CEIL_RSHIFT(inlink->h, is_chroma);
  174. int fils = frame_dec->linesize[i];
  175. int srcs = inpic ->linesize[i];
  176. int dsts = outpic ->linesize[i];
  177. for (y = 0; y < h; y++) {
  178. if ((y ^ mcdeint->parity) & 1) {
  179. for (x = 0; x < w; x++) {
  180. uint8_t *filp = &frame_dec->data[i][x + y*fils];
  181. uint8_t *srcp = &inpic ->data[i][x + y*srcs];
  182. uint8_t *dstp = &outpic ->data[i][x + y*dsts];
  183. if (y > 0 && y < h-1){
  184. int is_edge = x < 3 || x > w-4;
  185. int diff0 = filp[-fils] - srcp[-srcs];
  186. int diff1 = filp[+fils] - srcp[+srcs];
  187. int temp = filp[0];
  188. #define DELTA(j) av_clip(j, -x, w-1-x)
  189. #define GET_SCORE_EDGE(j)\
  190. FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
  191. FFABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\
  192. FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
  193. #define GET_SCORE(j)\
  194. FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
  195. FFABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\
  196. FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
  197. #define CHECK_EDGE(j)\
  198. { int score = GET_SCORE_EDGE(j);\
  199. if (score < spatial_score){\
  200. spatial_score = score;\
  201. diff0 = filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\
  202. diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
  203. #define CHECK(j)\
  204. { int score = GET_SCORE(j);\
  205. if (score < spatial_score){\
  206. spatial_score= score;\
  207. diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
  208. diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
  209. if (is_edge) {
  210. int spatial_score = GET_SCORE_EDGE(0) - 1;
  211. CHECK_EDGE(-1) CHECK_EDGE(-2) }} }}
  212. CHECK_EDGE( 1) CHECK_EDGE( 2) }} }}
  213. } else {
  214. int spatial_score = GET_SCORE(0) - 1;
  215. CHECK(-1) CHECK(-2) }} }}
  216. CHECK( 1) CHECK( 2) }} }}
  217. }
  218. if (diff0 + diff1 > 0)
  219. temp -= (diff0 + diff1 - FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
  220. else
  221. temp -= (diff0 + diff1 + FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
  222. *filp = *dstp = temp > 255U ? ~(temp>>31) : temp;
  223. } else {
  224. *dstp = *filp;
  225. }
  226. }
  227. }
  228. }
  229. for (y = 0; y < h; y++) {
  230. if (!((y ^ mcdeint->parity) & 1)) {
  231. for (x = 0; x < w; x++) {
  232. frame_dec->data[i][x + y*fils] =
  233. outpic ->data[i][x + y*dsts] = inpic->data[i][x + y*srcs];
  234. }
  235. }
  236. }
  237. }
  238. mcdeint->parity ^= 1;
  239. end:
  240. av_free_packet(&pkt);
  241. av_frame_free(&inpic);
  242. if (ret < 0) {
  243. av_frame_free(&outpic);
  244. return ret;
  245. }
  246. return ff_filter_frame(outlink, outpic);
  247. }
  248. static const AVFilterPad mcdeint_inputs[] = {
  249. {
  250. .name = "default",
  251. .type = AVMEDIA_TYPE_VIDEO,
  252. .filter_frame = filter_frame,
  253. .config_props = config_props,
  254. },
  255. { NULL }
  256. };
  257. static const AVFilterPad mcdeint_outputs[] = {
  258. {
  259. .name = "default",
  260. .type = AVMEDIA_TYPE_VIDEO,
  261. },
  262. { NULL }
  263. };
  264. AVFilter ff_vf_mcdeint = {
  265. .name = "mcdeint",
  266. .description = NULL_IF_CONFIG_SMALL("Apply motion compensating deinterlacing."),
  267. .priv_size = sizeof(MCDeintContext),
  268. .uninit = uninit,
  269. .query_formats = query_formats,
  270. .inputs = mcdeint_inputs,
  271. .outputs = mcdeint_outputs,
  272. .priv_class = &mcdeint_class,
  273. };