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.

317 lines
10KB

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