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.

1248 lines
49KB

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