vf_find_rect.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2014-2015 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. /**
  21. * @todo switch to dualinput
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "internal.h"
  27. #include "lavfutils.h"
  28. #define MAX_MIPMAPS 5
  29. typedef struct FOCContext {
  30. AVClass *class;
  31. float threshold;
  32. int mipmaps;
  33. int xmin, ymin, xmax, ymax;
  34. char *obj_filename;
  35. int last_x, last_y;
  36. AVFrame *obj_frame;
  37. AVFrame *needle_frame[MAX_MIPMAPS];
  38. AVFrame *haystack_frame[MAX_MIPMAPS];
  39. } FOCContext;
  40. #define OFFSET(x) offsetof(FOCContext, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  42. static const AVOption find_rect_options[] = {
  43. { "object", "object bitmap filename", OFFSET(obj_filename), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  44. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1.0, FLAGS },
  45. { "mipmaps", "set mipmaps", OFFSET(mipmaps), AV_OPT_TYPE_INT, {.i64 = 3}, 1, MAX_MIPMAPS, FLAGS },
  46. { "xmin", "", OFFSET(xmin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  47. { "ymin", "", OFFSET(ymin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  48. { "xmax", "", OFFSET(xmax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  49. { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(find_rect);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. static const enum AVPixelFormat pix_fmts[] = {
  56. AV_PIX_FMT_YUV420P,
  57. AV_PIX_FMT_YUVJ420P,
  58. AV_PIX_FMT_NONE
  59. };
  60. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  61. }
  62. static AVFrame *downscale(AVFrame *in)
  63. {
  64. int x, y;
  65. AVFrame *frame = av_frame_alloc();
  66. uint8_t *src, *dst;
  67. if (!frame)
  68. return NULL;
  69. frame->format = in->format;
  70. frame->width = (in->width + 1) / 2;
  71. frame->height = (in->height+ 1) / 2;
  72. if (av_frame_get_buffer(frame, 32) < 0) {
  73. av_frame_free(&frame);
  74. return NULL;
  75. }
  76. src = in ->data[0];
  77. dst = frame->data[0];
  78. for(y = 0; y < frame->height; y++) {
  79. for(x = 0; x < frame->width; x++) {
  80. dst[x] = ( src[2*x+0]
  81. + src[2*x+1]
  82. + src[2*x+0 + in->linesize[0]]
  83. + src[2*x+1 + in->linesize[0]]
  84. + 2) >> 2;
  85. }
  86. src += 2*in->linesize[0];
  87. dst += frame->linesize[0];
  88. }
  89. return frame;
  90. }
  91. static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
  92. {
  93. int x,y;
  94. int o_sum_v = 0;
  95. int h_sum_v = 0;
  96. int64_t oo_sum_v = 0;
  97. int64_t hh_sum_v = 0;
  98. int64_t oh_sum_v = 0;
  99. float c;
  100. int n = obj->height * obj->width;
  101. const uint8_t *odat = obj ->data[0];
  102. const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
  103. int64_t o_sigma, h_sigma;
  104. for(y = 0; y < obj->height; y++) {
  105. for(x = 0; x < obj->width; x++) {
  106. int o_v = odat[x];
  107. int h_v = hdat[x];
  108. o_sum_v += o_v;
  109. h_sum_v += h_v;
  110. oo_sum_v += o_v * o_v;
  111. hh_sum_v += h_v * h_v;
  112. oh_sum_v += o_v * h_v;
  113. }
  114. odat += obj->linesize[0];
  115. hdat += haystack->linesize[0];
  116. }
  117. o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
  118. h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
  119. if (o_sigma == 0 || h_sigma == 0)
  120. return 1.0;
  121. c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
  122. return 1 - fabs(c);
  123. }
  124. static int config_input(AVFilterLink *inlink)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. FOCContext *foc = ctx->priv;
  128. if (foc->xmax <= 0)
  129. foc->xmax = inlink->w - foc->obj_frame->width;
  130. if (foc->ymax <= 0)
  131. foc->ymax = inlink->h - foc->obj_frame->height;
  132. return 0;
  133. }
  134. static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, int ymin, int ymax, int *best_x, int *best_y, float best_score)
  135. {
  136. int x, y;
  137. if (pass + 1 <= maxpass) {
  138. int sub_x, sub_y;
  139. search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 1.0);
  140. xmin = FFMAX(xmin, 2*sub_x - 4);
  141. xmax = FFMIN(xmax, 2*sub_x + 4);
  142. ymin = FFMAX(ymin, 2*sub_y - 4);
  143. ymax = FFMIN(ymax, 2*sub_y + 4);
  144. }
  145. for (y = ymin; y <= ymax; y++) {
  146. for (x = xmin; x <= xmax; x++) {
  147. float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
  148. av_assert0(score != 0);
  149. if (score < best_score) {
  150. best_score = score;
  151. *best_x = x;
  152. *best_y = y;
  153. }
  154. }
  155. }
  156. return best_score;
  157. }
  158. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  159. {
  160. AVFilterContext *ctx = inlink->dst;
  161. FOCContext *foc = ctx->priv;
  162. float best_score;
  163. int best_x, best_y;
  164. int i;
  165. foc->haystack_frame[0] = av_frame_clone(in);
  166. for (i=1; i<foc->mipmaps; i++) {
  167. foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
  168. }
  169. best_score = search(foc, 0, 0,
  170. FFMAX(foc->xmin, foc->last_x - 8),
  171. FFMIN(foc->xmax, foc->last_x + 8),
  172. FFMAX(foc->ymin, foc->last_y - 8),
  173. FFMIN(foc->ymax, foc->last_y + 8),
  174. &best_x, &best_y, 1.0);
  175. best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
  176. &best_x, &best_y, best_score);
  177. for (i=0; i<MAX_MIPMAPS; i++) {
  178. av_frame_free(&foc->haystack_frame[i]);
  179. }
  180. if (best_score > foc->threshold) {
  181. return ff_filter_frame(ctx->outputs[0], in);
  182. }
  183. av_log(ctx, AV_LOG_DEBUG, "Found at %d %d score %f\n", best_x, best_y, best_score);
  184. foc->last_x = best_x;
  185. foc->last_y = best_y;
  186. av_frame_make_writable(in);
  187. av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
  188. av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
  189. av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
  190. av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
  191. return ff_filter_frame(ctx->outputs[0], in);
  192. }
  193. static av_cold void uninit(AVFilterContext *ctx)
  194. {
  195. FOCContext *foc = ctx->priv;
  196. int i;
  197. for (i = 0; i < MAX_MIPMAPS; i++) {
  198. av_frame_free(&foc->needle_frame[i]);
  199. av_frame_free(&foc->haystack_frame[i]);
  200. }
  201. if (foc->obj_frame)
  202. av_freep(&foc->obj_frame->data[0]);
  203. av_frame_free(&foc->obj_frame);
  204. }
  205. static av_cold int init(AVFilterContext *ctx)
  206. {
  207. FOCContext *foc = ctx->priv;
  208. int ret, i;
  209. if (!foc->obj_filename) {
  210. av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
  211. return AVERROR(EINVAL);
  212. }
  213. foc->obj_frame = av_frame_alloc();
  214. if (!foc->obj_frame)
  215. return AVERROR(ENOMEM);
  216. if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
  217. &foc->obj_frame->width, &foc->obj_frame->height,
  218. &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
  219. return ret;
  220. if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
  221. av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
  222. return AVERROR(EINVAL);
  223. }
  224. foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
  225. for (i = 1; i < foc->mipmaps; i++) {
  226. foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
  227. if (!foc->needle_frame[i])
  228. return AVERROR(ENOMEM);
  229. }
  230. return 0;
  231. }
  232. static const AVFilterPad foc_inputs[] = {
  233. {
  234. .name = "default",
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .config_props = config_input,
  237. .filter_frame = filter_frame,
  238. },
  239. { NULL }
  240. };
  241. static const AVFilterPad foc_outputs[] = {
  242. {
  243. .name = "default",
  244. .type = AVMEDIA_TYPE_VIDEO,
  245. },
  246. { NULL }
  247. };
  248. AVFilter ff_vf_find_rect = {
  249. .name = "find_rect",
  250. .description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
  251. .priv_size = sizeof(FOCContext),
  252. .init = init,
  253. .uninit = uninit,
  254. .query_formats = query_formats,
  255. .inputs = foc_inputs,
  256. .outputs = foc_outputs,
  257. .priv_class = &find_rect_class,
  258. };