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.

1272 lines
50KB

  1. /**
  2. * Copyright (c) 2014-2015 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2016 Davinder Singh (DSM_) <ds.mudhar<@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "motion_estimation.h"
  22. #include "libavcodec/mathops.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/motion_vector.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #include "scene_sad.h"
  33. #define ME_MODE_BIDIR 0
  34. #define ME_MODE_BILAT 1
  35. #define MC_MODE_OBMC 0
  36. #define MC_MODE_AOBMC 1
  37. #define SCD_METHOD_NONE 0
  38. #define SCD_METHOD_FDIFF 1
  39. #define NB_FRAMES 4
  40. #define NB_PIXEL_MVS 32
  41. #define NB_CLUSTERS 128
  42. #define ALPHA_MAX 1024
  43. #define CLUSTER_THRESHOLD 4
  44. #define PX_WEIGHT_MAX 255
  45. #define COST_PRED_SCALE 64
  46. static const uint8_t obmc_linear32[1024] = {
  47. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  48. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  49. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  50. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  51. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  52. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  53. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  54. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  55. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  56. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  57. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  58. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  59. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  60. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  61. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  62. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  63. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  64. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  65. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  66. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  67. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  68. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  69. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  70. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  71. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  72. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  73. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  74. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  75. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  76. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  77. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  78. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  79. };
  80. static const uint8_t obmc_linear16[256] = {
  81. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  82. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  83. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  84. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  85. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  86. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  87. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  88. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  89. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  90. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  91. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  92. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  93. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  94. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  95. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  96. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  97. };
  98. static const uint8_t obmc_linear8[64] = {
  99. 4, 12, 20, 28, 28, 20, 12, 4,
  100. 12, 36, 60, 84, 84, 60, 36, 12,
  101. 20, 60,100,140,140,100, 60, 20,
  102. 28, 84,140,196,196,140, 84, 28,
  103. 28, 84,140,196,196,140, 84, 28,
  104. 20, 60,100,140,140,100, 60, 20,
  105. 12, 36, 60, 84, 84, 60, 36, 12,
  106. 4, 12, 20, 28, 28, 20, 12, 4,
  107. };
  108. static const uint8_t obmc_linear4[16] = {
  109. 16, 48, 48, 16,
  110. 48,144,144, 48,
  111. 48,144,144, 48,
  112. 16, 48, 48, 16,
  113. };
  114. static const uint8_t * const obmc_tab_linear[4]= {
  115. obmc_linear32, obmc_linear16, obmc_linear8, obmc_linear4
  116. };
  117. enum MIMode {
  118. MI_MODE_DUP = 0,
  119. MI_MODE_BLEND = 1,
  120. MI_MODE_MCI = 2,
  121. };
  122. typedef struct Cluster {
  123. int64_t sum[2];
  124. int nb;
  125. } Cluster;
  126. typedef struct Block {
  127. int16_t mvs[2][2];
  128. int cid;
  129. uint64_t sbad;
  130. int sb;
  131. struct Block *subs;
  132. } Block;
  133. typedef struct PixelMVS {
  134. int16_t mvs[NB_PIXEL_MVS][2];
  135. } PixelMVS;
  136. typedef struct PixelWeights {
  137. uint32_t weights[NB_PIXEL_MVS];
  138. } PixelWeights;
  139. typedef struct PixelRefs {
  140. int8_t refs[NB_PIXEL_MVS];
  141. int nb;
  142. } PixelRefs;
  143. typedef struct Frame {
  144. AVFrame *avf;
  145. Block *blocks;
  146. } Frame;
  147. typedef struct MIContext {
  148. const AVClass *class;
  149. AVMotionEstContext me_ctx;
  150. AVRational frame_rate;
  151. enum MIMode mi_mode;
  152. int mc_mode;
  153. int me_mode;
  154. int me_method;
  155. int mb_size;
  156. int search_param;
  157. int vsbmc;
  158. Frame frames[NB_FRAMES];
  159. Cluster clusters[NB_CLUSTERS];
  160. Block *int_blocks;
  161. PixelMVS *pixel_mvs;
  162. PixelWeights *pixel_weights;
  163. PixelRefs *pixel_refs;
  164. int (*mv_table[3])[2][2];
  165. int64_t out_pts;
  166. int b_width, b_height, b_count;
  167. int log2_mb_size;
  168. int bitdepth;
  169. int scd_method;
  170. int scene_changed;
  171. ff_scene_sad_fn sad;
  172. double prev_mafd;
  173. double scd_threshold;
  174. int log2_chroma_w;
  175. int log2_chroma_h;
  176. int nb_planes;
  177. } MIContext;
  178. #define OFFSET(x) offsetof(MIContext, x)
  179. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  180. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  181. static const AVOption minterpolate_options[] = {
  182. { "fps", "output's frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "60"}, 0, INT_MAX, FLAGS },
  183. { "mi_mode", "motion interpolation mode", OFFSET(mi_mode), AV_OPT_TYPE_INT, {.i64 = MI_MODE_MCI}, MI_MODE_DUP, MI_MODE_MCI, FLAGS, "mi_mode" },
  184. CONST("dup", "duplicate frames", MI_MODE_DUP, "mi_mode"),
  185. CONST("blend", "blend frames", MI_MODE_BLEND, "mi_mode"),
  186. CONST("mci", "motion compensated interpolation", MI_MODE_MCI, "mi_mode"),
  187. { "mc_mode", "motion compensation mode", OFFSET(mc_mode), AV_OPT_TYPE_INT, {.i64 = MC_MODE_OBMC}, MC_MODE_OBMC, MC_MODE_AOBMC, FLAGS, "mc_mode" },
  188. CONST("obmc", "overlapped block motion compensation", MC_MODE_OBMC, "mc_mode"),
  189. CONST("aobmc", "adaptive overlapped block motion compensation", MC_MODE_AOBMC, "mc_mode"),
  190. { "me_mode", "motion estimation mode", OFFSET(me_mode), AV_OPT_TYPE_INT, {.i64 = ME_MODE_BILAT}, ME_MODE_BIDIR, ME_MODE_BILAT, FLAGS, "me_mode" },
  191. CONST("bidir", "bidirectional motion estimation", ME_MODE_BIDIR, "me_mode"),
  192. CONST("bilat", "bilateral motion estimation", ME_MODE_BILAT, "me_mode"),
  193. { "me", "motion estimation method", OFFSET(me_method), AV_OPT_TYPE_INT, {.i64 = AV_ME_METHOD_EPZS}, AV_ME_METHOD_ESA, AV_ME_METHOD_UMH, FLAGS, "me" },
  194. CONST("esa", "exhaustive search", AV_ME_METHOD_ESA, "me"),
  195. CONST("tss", "three step search", AV_ME_METHOD_TSS, "me"),
  196. CONST("tdls", "two dimensional logarithmic search", AV_ME_METHOD_TDLS, "me"),
  197. CONST("ntss", "new three step search", AV_ME_METHOD_NTSS, "me"),
  198. CONST("fss", "four step search", AV_ME_METHOD_FSS, "me"),
  199. CONST("ds", "diamond search", AV_ME_METHOD_DS, "me"),
  200. CONST("hexbs", "hexagon-based search", AV_ME_METHOD_HEXBS, "me"),
  201. CONST("epzs", "enhanced predictive zonal search", AV_ME_METHOD_EPZS, "me"),
  202. CONST("umh", "uneven multi-hexagon search", AV_ME_METHOD_UMH, "me"),
  203. { "mb_size", "macroblock size", OFFSET(mb_size), AV_OPT_TYPE_INT, {.i64 = 16}, 4, 16, FLAGS },
  204. { "search_param", "search parameter", OFFSET(search_param), AV_OPT_TYPE_INT, {.i64 = 32}, 4, INT_MAX, FLAGS },
  205. { "vsbmc", "variable-size block motion compensation", OFFSET(vsbmc), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  206. { "scd", "scene change detection method", OFFSET(scd_method), AV_OPT_TYPE_INT, {.i64 = SCD_METHOD_FDIFF}, SCD_METHOD_NONE, SCD_METHOD_FDIFF, FLAGS, "scene" },
  207. CONST("none", "disable detection", SCD_METHOD_NONE, "scene"),
  208. CONST("fdiff", "frame difference", SCD_METHOD_FDIFF, "scene"),
  209. { "scd_threshold", "scene change threshold", OFFSET(scd_threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100.0, FLAGS },
  210. { NULL }
  211. };
  212. AVFILTER_DEFINE_CLASS(minterpolate);
  213. static int query_formats(AVFilterContext *ctx)
  214. {
  215. static const enum AVPixelFormat pix_fmts[] = {
  216. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  217. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  218. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  219. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  220. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  221. AV_PIX_FMT_YUVJ411P,
  222. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  223. AV_PIX_FMT_GRAY8,
  224. AV_PIX_FMT_NONE
  225. };
  226. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  227. if (!fmts_list)
  228. return AVERROR(ENOMEM);
  229. return ff_set_common_formats(ctx, fmts_list);
  230. }
  231. static uint64_t get_sbad(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  232. {
  233. uint8_t *data_cur = me_ctx->data_cur;
  234. uint8_t *data_next = me_ctx->data_ref;
  235. int linesize = me_ctx->linesize;
  236. int mv_x1 = x_mv - x;
  237. int mv_y1 = y_mv - y;
  238. int mv_x, mv_y, i, j;
  239. uint64_t sbad = 0;
  240. x = av_clip(x, me_ctx->x_min, me_ctx->x_max);
  241. y = av_clip(y, me_ctx->y_min, me_ctx->y_max);
  242. mv_x = av_clip(x_mv - x, -FFMIN(x - me_ctx->x_min, me_ctx->x_max - x), FFMIN(x - me_ctx->x_min, me_ctx->x_max - x));
  243. mv_y = av_clip(y_mv - y, -FFMIN(y - me_ctx->y_min, me_ctx->y_max - y), FFMIN(y - me_ctx->y_min, me_ctx->y_max - y));
  244. data_cur += (y + mv_y) * linesize;
  245. data_next += (y - mv_y) * linesize;
  246. for (j = 0; j < me_ctx->mb_size; j++)
  247. for (i = 0; i < me_ctx->mb_size; i++)
  248. sbad += FFABS(data_cur[x + mv_x + i + j * linesize] - data_next[x - mv_x + i + j * linesize]);
  249. return sbad + (FFABS(mv_x1 - me_ctx->pred_x) + FFABS(mv_y1 - me_ctx->pred_y)) * COST_PRED_SCALE;
  250. }
  251. static uint64_t get_sbad_ob(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  252. {
  253. uint8_t *data_cur = me_ctx->data_cur;
  254. uint8_t *data_next = me_ctx->data_ref;
  255. int linesize = me_ctx->linesize;
  256. int x_min = me_ctx->x_min + me_ctx->mb_size / 2;
  257. int x_max = me_ctx->x_max - me_ctx->mb_size / 2;
  258. int y_min = me_ctx->y_min + me_ctx->mb_size / 2;
  259. int y_max = me_ctx->y_max - me_ctx->mb_size / 2;
  260. int mv_x1 = x_mv - x;
  261. int mv_y1 = y_mv - y;
  262. int mv_x, mv_y, i, j;
  263. uint64_t sbad = 0;
  264. x = av_clip(x, x_min, x_max);
  265. y = av_clip(y, y_min, y_max);
  266. mv_x = av_clip(x_mv - x, -FFMIN(x - x_min, x_max - x), FFMIN(x - x_min, x_max - x));
  267. mv_y = av_clip(y_mv - y, -FFMIN(y - y_min, y_max - y), FFMIN(y - y_min, y_max - y));
  268. for (j = -me_ctx->mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
  269. for (i = -me_ctx->mb_size / 2; i < me_ctx->mb_size * 3 / 2; i++)
  270. sbad += FFABS(data_cur[x + mv_x + i + (y + mv_y + j) * linesize] - data_next[x - mv_x + i + (y - mv_y + j) * linesize]);
  271. return sbad + (FFABS(mv_x1 - me_ctx->pred_x) + FFABS(mv_y1 - me_ctx->pred_y)) * COST_PRED_SCALE;
  272. }
  273. static uint64_t get_sad_ob(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  274. {
  275. uint8_t *data_ref = me_ctx->data_ref;
  276. uint8_t *data_cur = me_ctx->data_cur;
  277. int linesize = me_ctx->linesize;
  278. int x_min = me_ctx->x_min + me_ctx->mb_size / 2;
  279. int x_max = me_ctx->x_max - me_ctx->mb_size / 2;
  280. int y_min = me_ctx->y_min + me_ctx->mb_size / 2;
  281. int y_max = me_ctx->y_max - me_ctx->mb_size / 2;
  282. int mv_x = x_mv - x;
  283. int mv_y = y_mv - y;
  284. int i, j;
  285. uint64_t sad = 0;
  286. x = av_clip(x, x_min, x_max);
  287. y = av_clip(y, y_min, y_max);
  288. x_mv = av_clip(x_mv, x_min, x_max);
  289. y_mv = av_clip(y_mv, y_min, y_max);
  290. for (j = -me_ctx->mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
  291. for (i = -me_ctx->mb_size / 2; i < me_ctx->mb_size * 3 / 2; i++)
  292. sad += FFABS(data_ref[x_mv + i + (y_mv + j) * linesize] - data_cur[x + i + (y + j) * linesize]);
  293. return sad + (FFABS(mv_x - me_ctx->pred_x) + FFABS(mv_y - me_ctx->pred_y)) * COST_PRED_SCALE;
  294. }
  295. static int config_input(AVFilterLink *inlink)
  296. {
  297. MIContext *mi_ctx = inlink->dst->priv;
  298. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  299. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  300. const int height = inlink->h;
  301. const int width = inlink->w;
  302. int i, ret = 0;
  303. mi_ctx->log2_chroma_h = desc->log2_chroma_h;
  304. mi_ctx->log2_chroma_w = desc->log2_chroma_w;
  305. mi_ctx->bitdepth = desc->comp[0].depth;
  306. mi_ctx->nb_planes = av_pix_fmt_count_planes(inlink->format);
  307. mi_ctx->log2_mb_size = av_ceil_log2_c(mi_ctx->mb_size);
  308. mi_ctx->mb_size = 1 << mi_ctx->log2_mb_size;
  309. mi_ctx->b_width = width >> mi_ctx->log2_mb_size;
  310. mi_ctx->b_height = height >> mi_ctx->log2_mb_size;
  311. mi_ctx->b_count = mi_ctx->b_width * mi_ctx->b_height;
  312. for (i = 0; i < NB_FRAMES; i++) {
  313. Frame *frame = &mi_ctx->frames[i];
  314. frame->blocks = av_mallocz_array(mi_ctx->b_count, sizeof(Block));
  315. if (!frame->blocks)
  316. return AVERROR(ENOMEM);
  317. }
  318. if (mi_ctx->mi_mode == MI_MODE_MCI) {
  319. mi_ctx->pixel_mvs = av_mallocz_array(width * height, sizeof(PixelMVS));
  320. mi_ctx->pixel_weights = av_mallocz_array(width * height, sizeof(PixelWeights));
  321. mi_ctx->pixel_refs = av_mallocz_array(width * height, sizeof(PixelRefs));
  322. if (!mi_ctx->pixel_mvs || !mi_ctx->pixel_weights || !mi_ctx->pixel_refs) {
  323. ret = AVERROR(ENOMEM);
  324. goto fail;
  325. }
  326. if (mi_ctx->me_mode == ME_MODE_BILAT)
  327. if (!(mi_ctx->int_blocks = av_mallocz_array(mi_ctx->b_count, sizeof(Block))))
  328. return AVERROR(ENOMEM);
  329. if (mi_ctx->me_method == AV_ME_METHOD_EPZS) {
  330. for (i = 0; i < 3; i++) {
  331. mi_ctx->mv_table[i] = av_mallocz_array(mi_ctx->b_count, sizeof(*mi_ctx->mv_table[0]));
  332. if (!mi_ctx->mv_table[i])
  333. return AVERROR(ENOMEM);
  334. }
  335. }
  336. }
  337. if (mi_ctx->scd_method == SCD_METHOD_FDIFF) {
  338. mi_ctx->sad = ff_scene_sad_get_fn(mi_ctx->bitdepth == 8 ? 8 : 16);
  339. if (!mi_ctx->sad)
  340. return AVERROR(EINVAL);
  341. }
  342. ff_me_init_context(me_ctx, mi_ctx->mb_size, mi_ctx->search_param, width, height, 0, (mi_ctx->b_width - 1) << mi_ctx->log2_mb_size, 0, (mi_ctx->b_height - 1) << mi_ctx->log2_mb_size);
  343. if (mi_ctx->me_mode == ME_MODE_BIDIR)
  344. me_ctx->get_cost = &get_sad_ob;
  345. else if (mi_ctx->me_mode == ME_MODE_BILAT)
  346. me_ctx->get_cost = &get_sbad_ob;
  347. return 0;
  348. fail:
  349. for (i = 0; i < NB_FRAMES; i++)
  350. av_freep(&mi_ctx->frames[i].blocks);
  351. av_freep(&mi_ctx->pixel_mvs);
  352. av_freep(&mi_ctx->pixel_weights);
  353. av_freep(&mi_ctx->pixel_refs);
  354. return ret;
  355. }
  356. static int config_output(AVFilterLink *outlink)
  357. {
  358. MIContext *mi_ctx = outlink->src->priv;
  359. outlink->frame_rate = mi_ctx->frame_rate;
  360. outlink->time_base = av_inv_q(mi_ctx->frame_rate);
  361. return 0;
  362. }
  363. #define ADD_PRED(preds, px, py)\
  364. do {\
  365. preds.mvs[preds.nb][0] = px;\
  366. preds.mvs[preds.nb][1] = py;\
  367. preds.nb++;\
  368. } while(0)
  369. static void search_mv(MIContext *mi_ctx, Block *blocks, int mb_x, int mb_y, int dir)
  370. {
  371. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  372. AVMotionEstPredictor *preds = me_ctx->preds;
  373. Block *block = &blocks[mb_x + mb_y * mi_ctx->b_width];
  374. const int x_mb = mb_x << mi_ctx->log2_mb_size;
  375. const int y_mb = mb_y << mi_ctx->log2_mb_size;
  376. const int mb_i = mb_x + mb_y * mi_ctx->b_width;
  377. int mv[2] = {x_mb, y_mb};
  378. switch (mi_ctx->me_method) {
  379. case AV_ME_METHOD_ESA:
  380. ff_me_search_esa(me_ctx, x_mb, y_mb, mv);
  381. break;
  382. case AV_ME_METHOD_TSS:
  383. ff_me_search_tss(me_ctx, x_mb, y_mb, mv);
  384. break;
  385. case AV_ME_METHOD_TDLS:
  386. ff_me_search_tdls(me_ctx, x_mb, y_mb, mv);
  387. break;
  388. case AV_ME_METHOD_NTSS:
  389. ff_me_search_ntss(me_ctx, x_mb, y_mb, mv);
  390. break;
  391. case AV_ME_METHOD_FSS:
  392. ff_me_search_fss(me_ctx, x_mb, y_mb, mv);
  393. break;
  394. case AV_ME_METHOD_DS:
  395. ff_me_search_ds(me_ctx, x_mb, y_mb, mv);
  396. break;
  397. case AV_ME_METHOD_HEXBS:
  398. ff_me_search_hexbs(me_ctx, x_mb, y_mb, mv);
  399. break;
  400. case AV_ME_METHOD_EPZS:
  401. preds[0].nb = 0;
  402. preds[1].nb = 0;
  403. ADD_PRED(preds[0], 0, 0);
  404. //left mb in current frame
  405. if (mb_x > 0)
  406. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - 1][dir][0], mi_ctx->mv_table[0][mb_i - 1][dir][1]);
  407. //top mb in current frame
  408. if (mb_y > 0)
  409. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width][dir][0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width][dir][1]);
  410. //top-right mb in current frame
  411. if (mb_y > 0 && mb_x + 1 < mi_ctx->b_width)
  412. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width + 1][dir][0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width + 1][dir][1]);
  413. //median predictor
  414. if (preds[0].nb == 4) {
  415. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  416. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  417. } else if (preds[0].nb == 3) {
  418. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  419. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  420. } else if (preds[0].nb == 2) {
  421. me_ctx->pred_x = preds[0].mvs[1][0];
  422. me_ctx->pred_y = preds[0].mvs[1][1];
  423. } else {
  424. me_ctx->pred_x = 0;
  425. me_ctx->pred_y = 0;
  426. }
  427. //collocated mb in prev frame
  428. ADD_PRED(preds[0], mi_ctx->mv_table[1][mb_i][dir][0], mi_ctx->mv_table[1][mb_i][dir][1]);
  429. //accelerator motion vector of collocated block in prev frame
  430. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i][dir][0] + (mi_ctx->mv_table[1][mb_i][dir][0] - mi_ctx->mv_table[2][mb_i][dir][0]),
  431. mi_ctx->mv_table[1][mb_i][dir][1] + (mi_ctx->mv_table[1][mb_i][dir][1] - mi_ctx->mv_table[2][mb_i][dir][1]));
  432. //left mb in prev frame
  433. if (mb_x > 0)
  434. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i - 1][dir][0], mi_ctx->mv_table[1][mb_i - 1][dir][1]);
  435. //top mb in prev frame
  436. if (mb_y > 0)
  437. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i - mi_ctx->b_width][dir][0], mi_ctx->mv_table[1][mb_i - mi_ctx->b_width][dir][1]);
  438. //right mb in prev frame
  439. if (mb_x + 1 < mi_ctx->b_width)
  440. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i + 1][dir][0], mi_ctx->mv_table[1][mb_i + 1][dir][1]);
  441. //bottom mb in prev frame
  442. if (mb_y + 1 < mi_ctx->b_height)
  443. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i + mi_ctx->b_width][dir][0], mi_ctx->mv_table[1][mb_i + mi_ctx->b_width][dir][1]);
  444. ff_me_search_epzs(me_ctx, x_mb, y_mb, mv);
  445. mi_ctx->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
  446. mi_ctx->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
  447. break;
  448. case AV_ME_METHOD_UMH:
  449. preds[0].nb = 0;
  450. ADD_PRED(preds[0], 0, 0);
  451. //left mb in current frame
  452. if (mb_x > 0)
  453. ADD_PRED(preds[0], blocks[mb_i - 1].mvs[dir][0], blocks[mb_i - 1].mvs[dir][1]);
  454. if (mb_y > 0) {
  455. //top mb in current frame
  456. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width].mvs[dir][0], blocks[mb_i - mi_ctx->b_width].mvs[dir][1]);
  457. //top-right mb in current frame
  458. if (mb_x + 1 < mi_ctx->b_width)
  459. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width + 1].mvs[dir][0], blocks[mb_i - mi_ctx->b_width + 1].mvs[dir][1]);
  460. //top-left mb in current frame
  461. else if (mb_x > 0)
  462. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width - 1].mvs[dir][0], blocks[mb_i - mi_ctx->b_width - 1].mvs[dir][1]);
  463. }
  464. //median predictor
  465. if (preds[0].nb == 4) {
  466. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  467. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  468. } else if (preds[0].nb == 3) {
  469. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  470. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  471. } else if (preds[0].nb == 2) {
  472. me_ctx->pred_x = preds[0].mvs[1][0];
  473. me_ctx->pred_y = preds[0].mvs[1][1];
  474. } else {
  475. me_ctx->pred_x = 0;
  476. me_ctx->pred_y = 0;
  477. }
  478. ff_me_search_umh(me_ctx, x_mb, y_mb, mv);
  479. break;
  480. }
  481. block->mvs[dir][0] = mv[0] - x_mb;
  482. block->mvs[dir][1] = mv[1] - y_mb;
  483. }
  484. static void bilateral_me(MIContext *mi_ctx)
  485. {
  486. Block *block;
  487. int mb_x, mb_y;
  488. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  489. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  490. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  491. block->cid = 0;
  492. block->sb = 0;
  493. block->mvs[0][0] = 0;
  494. block->mvs[0][1] = 0;
  495. }
  496. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  497. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++)
  498. search_mv(mi_ctx, mi_ctx->int_blocks, mb_x, mb_y, 0);
  499. }
  500. static int var_size_bme(MIContext *mi_ctx, Block *block, int x_mb, int y_mb, int n)
  501. {
  502. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  503. uint64_t cost_sb, cost_old;
  504. int mb_size = me_ctx->mb_size;
  505. int search_param = me_ctx->search_param;
  506. int mv_x, mv_y;
  507. int x, y;
  508. int ret;
  509. me_ctx->mb_size = 1 << n;
  510. cost_old = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]);
  511. me_ctx->mb_size = mb_size;
  512. if (!cost_old) {
  513. block->sb = 0;
  514. return 0;
  515. }
  516. if (!block->subs) {
  517. block->subs = av_mallocz_array(4, sizeof(Block));
  518. if (!block->subs)
  519. return AVERROR(ENOMEM);
  520. }
  521. block->sb = 1;
  522. for (y = 0; y < 2; y++)
  523. for (x = 0; x < 2; x++) {
  524. Block *sb = &block->subs[x + y * 2];
  525. int mv[2] = {x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]};
  526. me_ctx->mb_size = 1 << (n - 1);
  527. me_ctx->search_param = 2;
  528. me_ctx->pred_x = block->mvs[0][0];
  529. me_ctx->pred_y = block->mvs[0][1];
  530. cost_sb = ff_me_search_ds(&mi_ctx->me_ctx, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1], mv);
  531. mv_x = mv[0] - x_mb;
  532. mv_y = mv[1] - y_mb;
  533. me_ctx->mb_size = mb_size;
  534. me_ctx->search_param = search_param;
  535. if (cost_sb < cost_old / 4) {
  536. sb->mvs[0][0] = mv_x;
  537. sb->mvs[0][1] = mv_y;
  538. if (n > 1) {
  539. if (ret = var_size_bme(mi_ctx, sb, x_mb + (x << (n - 1)), y_mb + (y << (n - 1)), n - 1))
  540. return ret;
  541. } else
  542. sb->sb = 0;
  543. } else {
  544. block->sb = 0;
  545. return 0;
  546. }
  547. }
  548. return 0;
  549. }
  550. static int cluster_mvs(MIContext *mi_ctx)
  551. {
  552. int changed, c, c_max = 0;
  553. int mb_x, mb_y, x, y;
  554. int mv_x, mv_y, avg_x, avg_y, dx, dy;
  555. int d, ret;
  556. Block *block;
  557. Cluster *cluster, *cluster_new;
  558. do {
  559. changed = 0;
  560. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  561. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  562. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  563. c = block->cid;
  564. cluster = &mi_ctx->clusters[c];
  565. mv_x = block->mvs[0][0];
  566. mv_y = block->mvs[0][1];
  567. if (cluster->nb < 2)
  568. continue;
  569. avg_x = cluster->sum[0] / cluster->nb;
  570. avg_y = cluster->sum[1] / cluster->nb;
  571. dx = avg_x - mv_x;
  572. dy = avg_y - mv_y;
  573. if (FFABS(dx) > CLUSTER_THRESHOLD || FFABS(dy) > CLUSTER_THRESHOLD) {
  574. for (d = 1; d < 5; d++)
  575. for (y = FFMAX(mb_y - d, 0); y < FFMIN(mb_y + d + 1, mi_ctx->b_height); y++)
  576. for (x = FFMAX(mb_x - d, 0); x < FFMIN(mb_x + d + 1, mi_ctx->b_width); x++) {
  577. Block *nb = &mi_ctx->int_blocks[x + y * mi_ctx->b_width];
  578. if (nb->cid > block->cid) {
  579. if (nb->cid < c || c == block->cid)
  580. c = nb->cid;
  581. }
  582. }
  583. if (c == block->cid)
  584. c = c_max + 1;
  585. if (c >= NB_CLUSTERS) {
  586. continue;
  587. }
  588. cluster_new = &mi_ctx->clusters[c];
  589. cluster_new->sum[0] += mv_x;
  590. cluster_new->sum[1] += mv_y;
  591. cluster->sum[0] -= mv_x;
  592. cluster->sum[1] -= mv_y;
  593. cluster_new->nb++;
  594. cluster->nb--;
  595. c_max = FFMAX(c_max, c);
  596. block->cid = c;
  597. changed = 1;
  598. }
  599. }
  600. } while (changed);
  601. /* find boundaries */
  602. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  603. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  604. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  605. for (y = FFMAX(mb_y - 1, 0); y < FFMIN(mb_y + 2, mi_ctx->b_height); y++)
  606. for (x = FFMAX(mb_x - 1, 0); x < FFMIN(mb_x + 2, mi_ctx->b_width); x++) {
  607. dx = x - mb_x;
  608. dy = y - mb_y;
  609. if ((x - mb_x) && (y - mb_y) || !dx && !dy)
  610. continue;
  611. if (!mb_x || !mb_y || mb_x == mi_ctx->b_width - 1 || mb_y == mi_ctx->b_height - 1)
  612. continue;
  613. if (block->cid != mi_ctx->int_blocks[x + y * mi_ctx->b_width].cid) {
  614. if (!dx && block->cid == mi_ctx->int_blocks[x + (mb_y - dy) * mi_ctx->b_width].cid ||
  615. !dy && block->cid == mi_ctx->int_blocks[(mb_x - dx) + y * mi_ctx->b_width].cid) {
  616. if (ret = var_size_bme(mi_ctx, block, mb_x << mi_ctx->log2_mb_size, mb_y << mi_ctx->log2_mb_size, mi_ctx->log2_mb_size))
  617. return ret;
  618. }
  619. }
  620. }
  621. }
  622. return 0;
  623. }
  624. static int inject_frame(AVFilterLink *inlink, AVFrame *avf_in)
  625. {
  626. AVFilterContext *ctx = inlink->dst;
  627. MIContext *mi_ctx = ctx->priv;
  628. Frame frame_tmp;
  629. int mb_x, mb_y, dir;
  630. av_frame_free(&mi_ctx->frames[0].avf);
  631. frame_tmp = mi_ctx->frames[0];
  632. memmove(&mi_ctx->frames[0], &mi_ctx->frames[1], sizeof(mi_ctx->frames[0]) * (NB_FRAMES - 1));
  633. mi_ctx->frames[NB_FRAMES - 1] = frame_tmp;
  634. mi_ctx->frames[NB_FRAMES - 1].avf = avf_in;
  635. if (mi_ctx->mi_mode == MI_MODE_MCI) {
  636. if (mi_ctx->me_method == AV_ME_METHOD_EPZS) {
  637. mi_ctx->mv_table[2] = memcpy(mi_ctx->mv_table[2], mi_ctx->mv_table[1], sizeof(*mi_ctx->mv_table[1]) * mi_ctx->b_count);
  638. mi_ctx->mv_table[1] = memcpy(mi_ctx->mv_table[1], mi_ctx->mv_table[0], sizeof(*mi_ctx->mv_table[0]) * mi_ctx->b_count);
  639. }
  640. if (mi_ctx->me_mode == ME_MODE_BIDIR) {
  641. if (mi_ctx->frames[1].avf) {
  642. for (dir = 0; dir < 2; dir++) {
  643. mi_ctx->me_ctx.linesize = mi_ctx->frames[2].avf->linesize[0];
  644. mi_ctx->me_ctx.data_cur = mi_ctx->frames[2].avf->data[0];
  645. mi_ctx->me_ctx.data_ref = mi_ctx->frames[dir ? 3 : 1].avf->data[0];
  646. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  647. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++)
  648. search_mv(mi_ctx, mi_ctx->frames[2].blocks, mb_x, mb_y, dir);
  649. }
  650. }
  651. } else if (mi_ctx->me_mode == ME_MODE_BILAT) {
  652. Block *block;
  653. int i, ret;
  654. if (!mi_ctx->frames[0].avf)
  655. return 0;
  656. mi_ctx->me_ctx.linesize = mi_ctx->frames[0].avf->linesize[0];
  657. mi_ctx->me_ctx.data_cur = mi_ctx->frames[1].avf->data[0];
  658. mi_ctx->me_ctx.data_ref = mi_ctx->frames[2].avf->data[0];
  659. bilateral_me(mi_ctx);
  660. if (mi_ctx->mc_mode == MC_MODE_AOBMC) {
  661. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  662. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  663. int x_mb = mb_x << mi_ctx->log2_mb_size;
  664. int y_mb = mb_y << mi_ctx->log2_mb_size;
  665. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  666. block->sbad = get_sbad(&mi_ctx->me_ctx, x_mb, y_mb, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]);
  667. }
  668. }
  669. if (mi_ctx->vsbmc) {
  670. for (i = 0; i < NB_CLUSTERS; i++) {
  671. mi_ctx->clusters[i].sum[0] = 0;
  672. mi_ctx->clusters[i].sum[1] = 0;
  673. mi_ctx->clusters[i].nb = 0;
  674. }
  675. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  676. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  677. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  678. mi_ctx->clusters[0].sum[0] += block->mvs[0][0];
  679. mi_ctx->clusters[0].sum[1] += block->mvs[0][1];
  680. }
  681. mi_ctx->clusters[0].nb = mi_ctx->b_count;
  682. if (ret = cluster_mvs(mi_ctx))
  683. return ret;
  684. }
  685. }
  686. }
  687. return 0;
  688. }
  689. static int detect_scene_change(MIContext *mi_ctx)
  690. {
  691. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  692. uint8_t *p1 = mi_ctx->frames[1].avf->data[0];
  693. ptrdiff_t linesize1 = mi_ctx->frames[1].avf->linesize[0];
  694. uint8_t *p2 = mi_ctx->frames[2].avf->data[0];
  695. ptrdiff_t linesize2 = mi_ctx->frames[2].avf->linesize[0];
  696. if (mi_ctx->scd_method == SCD_METHOD_FDIFF) {
  697. double ret = 0, mafd, diff;
  698. uint64_t sad;
  699. mi_ctx->sad(p1, linesize1, p2, linesize2, me_ctx->width, me_ctx->height, &sad);
  700. emms_c();
  701. mafd = (double) sad * 100.0 / (me_ctx->height * me_ctx->width) / (1 << mi_ctx->bitdepth);
  702. diff = fabs(mafd - mi_ctx->prev_mafd);
  703. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  704. mi_ctx->prev_mafd = mafd;
  705. return ret >= mi_ctx->scd_threshold;
  706. }
  707. return 0;
  708. }
  709. #define ADD_PIXELS(b_weight, mv_x, mv_y)\
  710. do {\
  711. if (!b_weight || pixel_refs->nb + 1 >= NB_PIXEL_MVS)\
  712. continue;\
  713. pixel_refs->refs[pixel_refs->nb] = 1;\
  714. pixel_weights->weights[pixel_refs->nb] = b_weight * (ALPHA_MAX - alpha);\
  715. pixel_mvs->mvs[pixel_refs->nb][0] = av_clip((mv_x * alpha) / ALPHA_MAX, x_min, x_max);\
  716. pixel_mvs->mvs[pixel_refs->nb][1] = av_clip((mv_y * alpha) / ALPHA_MAX, y_min, y_max);\
  717. pixel_refs->nb++;\
  718. pixel_refs->refs[pixel_refs->nb] = 2;\
  719. pixel_weights->weights[pixel_refs->nb] = b_weight * alpha;\
  720. pixel_mvs->mvs[pixel_refs->nb][0] = av_clip(-mv_x * (ALPHA_MAX - alpha) / ALPHA_MAX, x_min, x_max);\
  721. pixel_mvs->mvs[pixel_refs->nb][1] = av_clip(-mv_y * (ALPHA_MAX - alpha) / ALPHA_MAX, y_min, y_max);\
  722. pixel_refs->nb++;\
  723. } while(0)
  724. static void bidirectional_obmc(MIContext *mi_ctx, int alpha)
  725. {
  726. int x, y;
  727. int width = mi_ctx->frames[0].avf->width;
  728. int height = mi_ctx->frames[0].avf->height;
  729. int mb_y, mb_x, dir;
  730. for (y = 0; y < height; y++)
  731. for (x = 0; x < width; x++)
  732. mi_ctx->pixel_refs[x + y * width].nb = 0;
  733. for (dir = 0; dir < 2; dir++)
  734. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  735. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  736. int a = dir ? alpha : (ALPHA_MAX - alpha);
  737. int mv_x = mi_ctx->frames[2 - dir].blocks[mb_x + mb_y * mi_ctx->b_width].mvs[dir][0];
  738. int mv_y = mi_ctx->frames[2 - dir].blocks[mb_x + mb_y * mi_ctx->b_width].mvs[dir][1];
  739. int start_x, start_y;
  740. int startc_x, startc_y, endc_x, endc_y;
  741. start_x = (mb_x << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2 + mv_x * a / ALPHA_MAX;
  742. start_y = (mb_y << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2 + mv_y * a / ALPHA_MAX;
  743. startc_x = av_clip(start_x, 0, width - 1);
  744. startc_y = av_clip(start_y, 0, height - 1);
  745. endc_x = av_clip(start_x + (2 << mi_ctx->log2_mb_size), 0, width - 1);
  746. endc_y = av_clip(start_y + (2 << mi_ctx->log2_mb_size), 0, height - 1);
  747. if (dir) {
  748. mv_x = -mv_x;
  749. mv_y = -mv_y;
  750. }
  751. for (y = startc_y; y < endc_y; y++) {
  752. int y_min = -y;
  753. int y_max = height - y - 1;
  754. for (x = startc_x; x < endc_x; x++) {
  755. int x_min = -x;
  756. int x_max = width - x - 1;
  757. int obmc_weight = obmc_tab_linear[4 - mi_ctx->log2_mb_size][(x - start_x) + ((y - start_y) << (mi_ctx->log2_mb_size + 1))];
  758. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  759. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  760. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  761. ADD_PIXELS(obmc_weight, mv_x, mv_y);
  762. }
  763. }
  764. }
  765. }
  766. static void set_frame_data(MIContext *mi_ctx, int alpha, AVFrame *avf_out)
  767. {
  768. int x, y, plane;
  769. for (plane = 0; plane < mi_ctx->nb_planes; plane++) {
  770. int width = avf_out->width;
  771. int height = avf_out->height;
  772. int chroma = plane == 1 || plane == 2;
  773. for (y = 0; y < height; y++)
  774. for (x = 0; x < width; x++) {
  775. int x_mv, y_mv;
  776. int weight_sum = 0;
  777. int i, val = 0;
  778. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * avf_out->width];
  779. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * avf_out->width];
  780. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * avf_out->width];
  781. for (i = 0; i < pixel_refs->nb; i++)
  782. weight_sum += pixel_weights->weights[i];
  783. if (!weight_sum || !pixel_refs->nb) {
  784. pixel_weights->weights[0] = ALPHA_MAX - alpha;
  785. pixel_refs->refs[0] = 1;
  786. pixel_mvs->mvs[0][0] = 0;
  787. pixel_mvs->mvs[0][1] = 0;
  788. pixel_weights->weights[1] = alpha;
  789. pixel_refs->refs[1] = 2;
  790. pixel_mvs->mvs[1][0] = 0;
  791. pixel_mvs->mvs[1][1] = 0;
  792. pixel_refs->nb = 2;
  793. weight_sum = ALPHA_MAX;
  794. }
  795. for (i = 0; i < pixel_refs->nb; i++) {
  796. Frame *frame = &mi_ctx->frames[pixel_refs->refs[i]];
  797. if (chroma) {
  798. x_mv = (x >> mi_ctx->log2_chroma_w) + pixel_mvs->mvs[i][0] / (1 << mi_ctx->log2_chroma_w);
  799. y_mv = (y >> mi_ctx->log2_chroma_h) + pixel_mvs->mvs[i][1] / (1 << mi_ctx->log2_chroma_h);
  800. } else {
  801. x_mv = x + pixel_mvs->mvs[i][0];
  802. y_mv = y + pixel_mvs->mvs[i][1];
  803. }
  804. val += pixel_weights->weights[i] * frame->avf->data[plane][x_mv + y_mv * frame->avf->linesize[plane]];
  805. }
  806. val = ROUNDED_DIV(val, weight_sum);
  807. if (chroma)
  808. avf_out->data[plane][(x >> mi_ctx->log2_chroma_w) + (y >> mi_ctx->log2_chroma_h) * avf_out->linesize[plane]] = val;
  809. else
  810. avf_out->data[plane][x + y * avf_out->linesize[plane]] = val;
  811. }
  812. }
  813. }
  814. static void var_size_bmc(MIContext *mi_ctx, Block *block, int x_mb, int y_mb, int n, int alpha)
  815. {
  816. int sb_x, sb_y;
  817. int width = mi_ctx->frames[0].avf->width;
  818. int height = mi_ctx->frames[0].avf->height;
  819. for (sb_y = 0; sb_y < 2; sb_y++)
  820. for (sb_x = 0; sb_x < 2; sb_x++) {
  821. Block *sb = &block->subs[sb_x + sb_y * 2];
  822. if (sb->sb)
  823. var_size_bmc(mi_ctx, sb, x_mb + (sb_x << (n - 1)), y_mb + (sb_y << (n - 1)), n - 1, alpha);
  824. else {
  825. int x, y;
  826. int mv_x = sb->mvs[0][0] * 2;
  827. int mv_y = sb->mvs[0][1] * 2;
  828. int start_x = x_mb + (sb_x << (n - 1));
  829. int start_y = y_mb + (sb_y << (n - 1));
  830. int end_x = start_x + (1 << (n - 1));
  831. int end_y = start_y + (1 << (n - 1));
  832. for (y = start_y; y < end_y; y++) {
  833. int y_min = -y;
  834. int y_max = height - y - 1;
  835. for (x = start_x; x < end_x; x++) {
  836. int x_min = -x;
  837. int x_max = width - x - 1;
  838. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  839. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  840. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  841. ADD_PIXELS(PX_WEIGHT_MAX, mv_x, mv_y);
  842. }
  843. }
  844. }
  845. }
  846. }
  847. static void bilateral_obmc(MIContext *mi_ctx, Block *block, int mb_x, int mb_y, int alpha)
  848. {
  849. int x, y;
  850. int width = mi_ctx->frames[0].avf->width;
  851. int height = mi_ctx->frames[0].avf->height;
  852. Block *nb;
  853. int nb_x, nb_y;
  854. uint64_t sbads[9];
  855. int mv_x = block->mvs[0][0] * 2;
  856. int mv_y = block->mvs[0][1] * 2;
  857. int start_x, start_y;
  858. int startc_x, startc_y, endc_x, endc_y;
  859. if (mi_ctx->mc_mode == MC_MODE_AOBMC)
  860. for (nb_y = FFMAX(0, mb_y - 1); nb_y < FFMIN(mb_y + 2, mi_ctx->b_height); nb_y++)
  861. for (nb_x = FFMAX(0, mb_x - 1); nb_x < FFMIN(mb_x + 2, mi_ctx->b_width); nb_x++) {
  862. int x_nb = nb_x << mi_ctx->log2_mb_size;
  863. int y_nb = nb_y << mi_ctx->log2_mb_size;
  864. if (nb_x - mb_x || nb_y - mb_y)
  865. sbads[nb_x - mb_x + 1 + (nb_y - mb_y + 1) * 3] = get_sbad(&mi_ctx->me_ctx, x_nb, y_nb, x_nb + block->mvs[0][0], y_nb + block->mvs[0][1]);
  866. }
  867. start_x = (mb_x << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2;
  868. start_y = (mb_y << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2;
  869. startc_x = av_clip(start_x, 0, width - 1);
  870. startc_y = av_clip(start_y, 0, height - 1);
  871. endc_x = av_clip(start_x + (2 << mi_ctx->log2_mb_size), 0, width - 1);
  872. endc_y = av_clip(start_y + (2 << mi_ctx->log2_mb_size), 0, height - 1);
  873. for (y = startc_y; y < endc_y; y++) {
  874. int y_min = -y;
  875. int y_max = height - y - 1;
  876. for (x = startc_x; x < endc_x; x++) {
  877. int x_min = -x;
  878. int x_max = width - x - 1;
  879. int obmc_weight = obmc_tab_linear[4 - mi_ctx->log2_mb_size][(x - start_x) + ((y - start_y) << (mi_ctx->log2_mb_size + 1))];
  880. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  881. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  882. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  883. if (mi_ctx->mc_mode == MC_MODE_AOBMC) {
  884. nb_x = (((x - start_x) >> (mi_ctx->log2_mb_size - 1)) * 2 - 3) / 2;
  885. nb_y = (((y - start_y) >> (mi_ctx->log2_mb_size - 1)) * 2 - 3) / 2;
  886. if (nb_x || nb_y) {
  887. uint64_t sbad = sbads[nb_x + 1 + (nb_y + 1) * 3];
  888. nb = &mi_ctx->int_blocks[mb_x + nb_x + (mb_y + nb_y) * mi_ctx->b_width];
  889. if (sbad && sbad != UINT64_MAX && nb->sbad != UINT64_MAX) {
  890. int phi = av_clip(ALPHA_MAX * nb->sbad / sbad, 0, ALPHA_MAX);
  891. obmc_weight = obmc_weight * phi / ALPHA_MAX;
  892. }
  893. }
  894. }
  895. ADD_PIXELS(obmc_weight, mv_x, mv_y);
  896. }
  897. }
  898. }
  899. static void interpolate(AVFilterLink *inlink, AVFrame *avf_out)
  900. {
  901. AVFilterContext *ctx = inlink->dst;
  902. AVFilterLink *outlink = ctx->outputs[0];
  903. MIContext *mi_ctx = ctx->priv;
  904. int x, y;
  905. int plane, alpha;
  906. int64_t pts;
  907. pts = av_rescale(avf_out->pts, (int64_t) ALPHA_MAX * outlink->time_base.num * inlink->time_base.den,
  908. (int64_t) outlink->time_base.den * inlink->time_base.num);
  909. alpha = (pts - mi_ctx->frames[1].avf->pts * ALPHA_MAX) / (mi_ctx->frames[2].avf->pts - mi_ctx->frames[1].avf->pts);
  910. alpha = av_clip(alpha, 0, ALPHA_MAX);
  911. if (alpha == 0 || alpha == ALPHA_MAX) {
  912. av_frame_copy(avf_out, alpha ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  913. return;
  914. }
  915. if (mi_ctx->scene_changed) {
  916. av_log(ctx, AV_LOG_DEBUG, "scene changed, input pts %"PRId64"\n", mi_ctx->frames[1].avf->pts);
  917. /* duplicate frame */
  918. av_frame_copy(avf_out, alpha > ALPHA_MAX / 2 ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  919. return;
  920. }
  921. switch(mi_ctx->mi_mode) {
  922. case MI_MODE_DUP:
  923. av_frame_copy(avf_out, alpha > ALPHA_MAX / 2 ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  924. break;
  925. case MI_MODE_BLEND:
  926. for (plane = 0; plane < mi_ctx->nb_planes; plane++) {
  927. int width = avf_out->width;
  928. int height = avf_out->height;
  929. if (plane == 1 || plane == 2) {
  930. width = AV_CEIL_RSHIFT(width, mi_ctx->log2_chroma_w);
  931. height = AV_CEIL_RSHIFT(height, mi_ctx->log2_chroma_h);
  932. }
  933. for (y = 0; y < height; y++) {
  934. for (x = 0; x < width; x++) {
  935. avf_out->data[plane][x + y * avf_out->linesize[plane]] =
  936. (alpha * mi_ctx->frames[2].avf->data[plane][x + y * mi_ctx->frames[2].avf->linesize[plane]] +
  937. (ALPHA_MAX - alpha) * mi_ctx->frames[1].avf->data[plane][x + y * mi_ctx->frames[1].avf->linesize[plane]] + 512) >> 10;
  938. }
  939. }
  940. }
  941. break;
  942. case MI_MODE_MCI:
  943. if (mi_ctx->me_mode == ME_MODE_BIDIR) {
  944. bidirectional_obmc(mi_ctx, alpha);
  945. set_frame_data(mi_ctx, alpha, avf_out);
  946. } else if (mi_ctx->me_mode == ME_MODE_BILAT) {
  947. int mb_x, mb_y;
  948. Block *block;
  949. for (y = 0; y < mi_ctx->frames[0].avf->height; y++)
  950. for (x = 0; x < mi_ctx->frames[0].avf->width; x++)
  951. mi_ctx->pixel_refs[x + y * mi_ctx->frames[0].avf->width].nb = 0;
  952. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  953. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  954. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  955. if (block->sb)
  956. var_size_bmc(mi_ctx, block, mb_x << mi_ctx->log2_mb_size, mb_y << mi_ctx->log2_mb_size, mi_ctx->log2_mb_size, alpha);
  957. bilateral_obmc(mi_ctx, block, mb_x, mb_y, alpha);
  958. }
  959. set_frame_data(mi_ctx, alpha, avf_out);
  960. }
  961. break;
  962. }
  963. }
  964. static int filter_frame(AVFilterLink *inlink, AVFrame *avf_in)
  965. {
  966. AVFilterContext *ctx = inlink->dst;
  967. AVFilterLink *outlink = ctx->outputs[0];
  968. MIContext *mi_ctx = ctx->priv;
  969. int ret;
  970. if (avf_in->pts == AV_NOPTS_VALUE) {
  971. ret = ff_filter_frame(ctx->outputs[0], avf_in);
  972. return ret;
  973. }
  974. if (!mi_ctx->frames[NB_FRAMES - 1].avf || avf_in->pts < mi_ctx->frames[NB_FRAMES - 1].avf->pts) {
  975. av_log(ctx, AV_LOG_VERBOSE, "Initializing out pts from input pts %"PRId64"\n", avf_in->pts);
  976. mi_ctx->out_pts = av_rescale_q(avf_in->pts, inlink->time_base, outlink->time_base);
  977. }
  978. if (!mi_ctx->frames[NB_FRAMES - 1].avf)
  979. if (ret = inject_frame(inlink, av_frame_clone(avf_in)))
  980. return ret;
  981. if (ret = inject_frame(inlink, avf_in))
  982. return ret;
  983. if (!mi_ctx->frames[0].avf)
  984. return 0;
  985. mi_ctx->scene_changed = detect_scene_change(mi_ctx);
  986. for (;;) {
  987. AVFrame *avf_out;
  988. if (av_compare_ts(mi_ctx->out_pts, outlink->time_base, mi_ctx->frames[2].avf->pts, inlink->time_base) > 0)
  989. break;
  990. if (!(avf_out = ff_get_video_buffer(ctx->outputs[0], inlink->w, inlink->h)))
  991. return AVERROR(ENOMEM);
  992. av_frame_copy_props(avf_out, mi_ctx->frames[NB_FRAMES - 1].avf);
  993. avf_out->pts = mi_ctx->out_pts++;
  994. interpolate(inlink, avf_out);
  995. if ((ret = ff_filter_frame(ctx->outputs[0], avf_out)) < 0)
  996. return ret;
  997. }
  998. return 0;
  999. }
  1000. static av_cold void free_blocks(Block *block, int sb)
  1001. {
  1002. if (block->subs)
  1003. free_blocks(block->subs, 1);
  1004. if (sb)
  1005. av_freep(&block);
  1006. }
  1007. static av_cold void uninit(AVFilterContext *ctx)
  1008. {
  1009. MIContext *mi_ctx = ctx->priv;
  1010. int i, m;
  1011. av_freep(&mi_ctx->pixel_mvs);
  1012. av_freep(&mi_ctx->pixel_weights);
  1013. av_freep(&mi_ctx->pixel_refs);
  1014. if (mi_ctx->int_blocks)
  1015. for (m = 0; m < mi_ctx->b_count; m++)
  1016. free_blocks(&mi_ctx->int_blocks[m], 0);
  1017. av_freep(&mi_ctx->int_blocks);
  1018. for (i = 0; i < NB_FRAMES; i++) {
  1019. Frame *frame = &mi_ctx->frames[i];
  1020. av_freep(&frame->blocks);
  1021. av_frame_free(&frame->avf);
  1022. }
  1023. for (i = 0; i < 3; i++)
  1024. av_freep(&mi_ctx->mv_table[i]);
  1025. }
  1026. static const AVFilterPad minterpolate_inputs[] = {
  1027. {
  1028. .name = "default",
  1029. .type = AVMEDIA_TYPE_VIDEO,
  1030. .filter_frame = filter_frame,
  1031. .config_props = config_input,
  1032. },
  1033. { NULL }
  1034. };
  1035. static const AVFilterPad minterpolate_outputs[] = {
  1036. {
  1037. .name = "default",
  1038. .type = AVMEDIA_TYPE_VIDEO,
  1039. .config_props = config_output,
  1040. },
  1041. { NULL }
  1042. };
  1043. AVFilter ff_vf_minterpolate = {
  1044. .name = "minterpolate",
  1045. .description = NULL_IF_CONFIG_SMALL("Frame rate conversion using Motion Interpolation."),
  1046. .priv_size = sizeof(MIContext),
  1047. .priv_class = &minterpolate_class,
  1048. .uninit = uninit,
  1049. .query_formats = query_formats,
  1050. .inputs = minterpolate_inputs,
  1051. .outputs = minterpolate_outputs,
  1052. };