vf_mestimate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**
  2. * Copyright (c) 2016 Davinder Singh (DSM_) <ds.mudhar<@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "motion_estimation.h"
  21. #include "libavcodec/mathops.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/motion_vector.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct MEContext {
  33. const AVClass *class;
  34. AVMotionEstContext me_ctx;
  35. int method; ///< motion estimation method
  36. int mb_size; ///< macroblock size
  37. int search_param; ///< search parameter
  38. int b_width, b_height, b_count;
  39. int log2_mb_size;
  40. AVFrame *prev, *cur, *next;
  41. int (*mv_table[3])[2][2]; ///< motion vectors of current & prev 2 frames
  42. } MEContext;
  43. #define OFFSET(x) offsetof(MEContext, x)
  44. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  46. static const AVOption mestimate_options[] = {
  47. { "method", "motion estimation method", OFFSET(method), AV_OPT_TYPE_INT, {.i64 = AV_ME_METHOD_ESA}, AV_ME_METHOD_ESA, AV_ME_METHOD_UMH, FLAGS, "method" },
  48. CONST("esa", "exhaustive search", AV_ME_METHOD_ESA, "method"),
  49. CONST("tss", "three step search", AV_ME_METHOD_TSS, "method"),
  50. CONST("tdls", "two dimensional logarithmic search", AV_ME_METHOD_TDLS, "method"),
  51. CONST("ntss", "new three step search", AV_ME_METHOD_NTSS, "method"),
  52. CONST("fss", "four step search", AV_ME_METHOD_FSS, "method"),
  53. CONST("ds", "diamond search", AV_ME_METHOD_DS, "method"),
  54. CONST("hexbs", "hexagon-based search", AV_ME_METHOD_HEXBS, "method"),
  55. CONST("epzs", "enhanced predictive zonal search", AV_ME_METHOD_EPZS, "method"),
  56. CONST("umh", "uneven multi-hexagon search", AV_ME_METHOD_UMH, "method"),
  57. { "mb_size", "macroblock size", OFFSET(mb_size), AV_OPT_TYPE_INT, {.i64 = 16}, 8, INT_MAX, FLAGS },
  58. { "search_param", "search parameter", OFFSET(search_param), AV_OPT_TYPE_INT, {.i64 = 7}, 4, INT_MAX, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(mestimate);
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. static const enum AVPixelFormat pix_fmts[] = {
  65. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  66. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  67. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  68. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  69. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  70. AV_PIX_FMT_YUVJ411P,
  71. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  72. AV_PIX_FMT_GRAY8,
  73. AV_PIX_FMT_NONE
  74. };
  75. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  76. if (!fmts_list)
  77. return AVERROR(ENOMEM);
  78. return ff_set_common_formats(ctx, fmts_list);
  79. }
  80. static int config_input(AVFilterLink *inlink)
  81. {
  82. MEContext *s = inlink->dst->priv;
  83. int i;
  84. s->log2_mb_size = av_ceil_log2_c(s->mb_size);
  85. s->mb_size = 1 << s->log2_mb_size;
  86. s->b_width = inlink->w >> s->log2_mb_size;
  87. s->b_height = inlink->h >> s->log2_mb_size;
  88. s->b_count = s->b_width * s->b_height;
  89. for (i = 0; i < 3; i++) {
  90. s->mv_table[i] = av_mallocz_array(s->b_count, sizeof(*s->mv_table[0]));
  91. if (!s->mv_table[i])
  92. return AVERROR(ENOMEM);
  93. }
  94. ff_me_init_context(&s->me_ctx, s->mb_size, s->search_param, inlink->w, inlink->h, 0, (s->b_width - 1) << s->log2_mb_size, 0, (s->b_height - 1) << s->log2_mb_size);
  95. return 0;
  96. }
  97. static void add_mv_data(AVMotionVector *mv, int mb_size,
  98. int x, int y, int x_mv, int y_mv, int dir)
  99. {
  100. mv->w = mb_size;
  101. mv->h = mb_size;
  102. mv->dst_x = x + (mb_size >> 1);
  103. mv->dst_y = y + (mb_size >> 1);
  104. mv->src_x = x_mv + (mb_size >> 1);
  105. mv->src_y = y_mv + (mb_size >> 1);
  106. mv->source = dir ? 1 : -1;
  107. mv->flags = 0;
  108. }
  109. #define SEARCH_MV(method)\
  110. do {\
  111. for (mb_y = 0; mb_y < s->b_height; mb_y++)\
  112. for (mb_x = 0; mb_x < s->b_width; mb_x++) {\
  113. const int x_mb = mb_x << s->log2_mb_size;\
  114. const int y_mb = mb_y << s->log2_mb_size;\
  115. int mv[2] = {x_mb, y_mb};\
  116. ff_me_search_##method(me_ctx, x_mb, y_mb, mv);\
  117. add_mv_data(((AVMotionVector *) sd->data) + mv_count++, me_ctx->mb_size, x_mb, y_mb, mv[0], mv[1], dir);\
  118. }\
  119. } while (0)
  120. #define ADD_PRED(preds, px, py)\
  121. do {\
  122. preds.mvs[preds.nb][0] = px;\
  123. preds.mvs[preds.nb][1] = py;\
  124. preds.nb++;\
  125. } while(0)
  126. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  127. {
  128. AVFilterContext *ctx = inlink->dst;
  129. MEContext *s = ctx->priv;
  130. AVMotionEstContext *me_ctx = &s->me_ctx;
  131. AVFrameSideData *sd;
  132. AVFrame *out;
  133. int mb_x, mb_y, dir;
  134. int32_t mv_count = 0;
  135. int ret;
  136. if (frame->pts == AV_NOPTS_VALUE) {
  137. ret = ff_filter_frame(ctx->outputs[0], frame);
  138. return ret;
  139. }
  140. av_frame_free(&s->prev);
  141. s->prev = s->cur;
  142. s->cur = s->next;
  143. s->next = frame;
  144. s->mv_table[2] = memcpy(s->mv_table[2], s->mv_table[1], sizeof(*s->mv_table[1]) * s->b_count);
  145. s->mv_table[1] = memcpy(s->mv_table[1], s->mv_table[0], sizeof(*s->mv_table[0]) * s->b_count);
  146. if (!s->cur) {
  147. s->cur = av_frame_clone(frame);
  148. if (!s->cur)
  149. return AVERROR(ENOMEM);
  150. }
  151. if (!s->prev)
  152. return 0;
  153. out = av_frame_clone(s->cur);
  154. if (!out)
  155. return AVERROR(ENOMEM);
  156. sd = av_frame_new_side_data(out, AV_FRAME_DATA_MOTION_VECTORS, 2 * s->b_count * sizeof(AVMotionVector));
  157. if (!sd) {
  158. av_frame_free(&out);
  159. return AVERROR(ENOMEM);
  160. }
  161. me_ctx->data_cur = s->cur->data[0];
  162. me_ctx->linesize = s->cur->linesize[0];
  163. for (dir = 0; dir < 2; dir++) {
  164. me_ctx->data_ref = (dir ? s->next : s->prev)->data[0];
  165. if (s->method == AV_ME_METHOD_DS)
  166. SEARCH_MV(ds);
  167. else if (s->method == AV_ME_METHOD_ESA)
  168. SEARCH_MV(esa);
  169. else if (s->method == AV_ME_METHOD_FSS)
  170. SEARCH_MV(fss);
  171. else if (s->method == AV_ME_METHOD_NTSS)
  172. SEARCH_MV(ntss);
  173. else if (s->method == AV_ME_METHOD_TDLS)
  174. SEARCH_MV(tdls);
  175. else if (s->method == AV_ME_METHOD_TSS)
  176. SEARCH_MV(tss);
  177. else if (s->method == AV_ME_METHOD_HEXBS)
  178. SEARCH_MV(hexbs);
  179. else if (s->method == AV_ME_METHOD_UMH) {
  180. for (mb_y = 0; mb_y < s->b_height; mb_y++)
  181. for (mb_x = 0; mb_x < s->b_width; mb_x++) {
  182. const int mb_i = mb_x + mb_y * s->b_width;
  183. const int x_mb = mb_x << s->log2_mb_size;
  184. const int y_mb = mb_y << s->log2_mb_size;
  185. int mv[2] = {x_mb, y_mb};
  186. AVMotionEstPredictor *preds = me_ctx->preds;
  187. preds[0].nb = 0;
  188. ADD_PRED(preds[0], 0, 0);
  189. //left mb in current frame
  190. if (mb_x > 0)
  191. ADD_PRED(preds[0], s->mv_table[0][mb_i - 1][dir][0], s->mv_table[0][mb_i - 1][dir][1]);
  192. if (mb_y > 0) {
  193. //top mb in current frame
  194. ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width][dir][0], s->mv_table[0][mb_i - s->b_width][dir][1]);
  195. //top-right mb in current frame
  196. if (mb_x + 1 < s->b_width)
  197. ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width + 1][dir][0], s->mv_table[0][mb_i - s->b_width + 1][dir][1]);
  198. //top-left mb in current frame
  199. else if (mb_x > 0)
  200. ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width - 1][dir][0], s->mv_table[0][mb_i - s->b_width - 1][dir][1]);
  201. }
  202. //median predictor
  203. if (preds[0].nb == 4) {
  204. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  205. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  206. } else if (preds[0].nb == 3) {
  207. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  208. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  209. } else if (preds[0].nb == 2) {
  210. me_ctx->pred_x = preds[0].mvs[1][0];
  211. me_ctx->pred_y = preds[0].mvs[1][1];
  212. } else {
  213. me_ctx->pred_x = 0;
  214. me_ctx->pred_y = 0;
  215. }
  216. ff_me_search_umh(me_ctx, x_mb, y_mb, mv);
  217. s->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
  218. s->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
  219. add_mv_data(((AVMotionVector *) sd->data) + mv_count++, me_ctx->mb_size, x_mb, y_mb, mv[0], mv[1], dir);
  220. }
  221. } else if (s->method == AV_ME_METHOD_EPZS) {
  222. for (mb_y = 0; mb_y < s->b_height; mb_y++)
  223. for (mb_x = 0; mb_x < s->b_width; mb_x++) {
  224. const int mb_i = mb_x + mb_y * s->b_width;
  225. const int x_mb = mb_x << s->log2_mb_size;
  226. const int y_mb = mb_y << s->log2_mb_size;
  227. int mv[2] = {x_mb, y_mb};
  228. AVMotionEstPredictor *preds = me_ctx->preds;
  229. preds[0].nb = 0;
  230. preds[1].nb = 0;
  231. ADD_PRED(preds[0], 0, 0);
  232. //left mb in current frame
  233. if (mb_x > 0)
  234. ADD_PRED(preds[0], s->mv_table[0][mb_i - 1][dir][0], s->mv_table[0][mb_i - 1][dir][1]);
  235. //top mb in current frame
  236. if (mb_y > 0)
  237. ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width][dir][0], s->mv_table[0][mb_i - s->b_width][dir][1]);
  238. //top-right mb in current frame
  239. if (mb_y > 0 && mb_x + 1 < s->b_width)
  240. ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width + 1][dir][0], s->mv_table[0][mb_i - s->b_width + 1][dir][1]);
  241. //median predictor
  242. if (preds[0].nb == 4) {
  243. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  244. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  245. } else if (preds[0].nb == 3) {
  246. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  247. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  248. } else if (preds[0].nb == 2) {
  249. me_ctx->pred_x = preds[0].mvs[1][0];
  250. me_ctx->pred_y = preds[0].mvs[1][1];
  251. } else {
  252. me_ctx->pred_x = 0;
  253. me_ctx->pred_y = 0;
  254. }
  255. //collocated mb in prev frame
  256. ADD_PRED(preds[0], s->mv_table[1][mb_i][dir][0], s->mv_table[1][mb_i][dir][1]);
  257. //accelerator motion vector of collocated block in prev frame
  258. ADD_PRED(preds[1], s->mv_table[1][mb_i][dir][0] + (s->mv_table[1][mb_i][dir][0] - s->mv_table[2][mb_i][dir][0]),
  259. s->mv_table[1][mb_i][dir][1] + (s->mv_table[1][mb_i][dir][1] - s->mv_table[2][mb_i][dir][1]));
  260. //left mb in prev frame
  261. if (mb_x > 0)
  262. ADD_PRED(preds[1], s->mv_table[1][mb_i - 1][dir][0], s->mv_table[1][mb_i - 1][dir][1]);
  263. //top mb in prev frame
  264. if (mb_y > 0)
  265. ADD_PRED(preds[1], s->mv_table[1][mb_i - s->b_width][dir][0], s->mv_table[1][mb_i - s->b_width][dir][1]);
  266. //right mb in prev frame
  267. if (mb_x + 1 < s->b_width)
  268. ADD_PRED(preds[1], s->mv_table[1][mb_i + 1][dir][0], s->mv_table[1][mb_i + 1][dir][1]);
  269. //bottom mb in prev frame
  270. if (mb_y + 1 < s->b_height)
  271. ADD_PRED(preds[1], s->mv_table[1][mb_i + s->b_width][dir][0], s->mv_table[1][mb_i + s->b_width][dir][1]);
  272. ff_me_search_epzs(me_ctx, x_mb, y_mb, mv);
  273. s->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
  274. s->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
  275. add_mv_data(((AVMotionVector *) sd->data) + mv_count++, s->mb_size, x_mb, y_mb, mv[0], mv[1], dir);
  276. }
  277. }
  278. }
  279. return ff_filter_frame(ctx->outputs[0], out);
  280. }
  281. static av_cold void uninit(AVFilterContext *ctx)
  282. {
  283. MEContext *s = ctx->priv;
  284. int i;
  285. av_frame_free(&s->prev);
  286. av_frame_free(&s->cur);
  287. av_frame_free(&s->next);
  288. for (i = 0; i < 3; i++)
  289. av_freep(&s->mv_table[i]);
  290. }
  291. static const AVFilterPad mestimate_inputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .filter_frame = filter_frame,
  296. .config_props = config_input,
  297. },
  298. { NULL }
  299. };
  300. static const AVFilterPad mestimate_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. },
  305. { NULL }
  306. };
  307. AVFilter ff_vf_mestimate = {
  308. .name = "mestimate",
  309. .description = NULL_IF_CONFIG_SMALL("Generate motion vectors."),
  310. .priv_size = sizeof(MEContext),
  311. .priv_class = &mestimate_class,
  312. .uninit = uninit,
  313. .query_formats = query_formats,
  314. .inputs = mestimate_inputs,
  315. .outputs = mestimate_outputs,
  316. };