vf_find_rect.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "internal.h"
  26. #include "lavfutils.h"
  27. #define MAX_MIPMAPS 5
  28. typedef struct FOCContext {
  29. AVClass *class;
  30. float threshold;
  31. int mipmaps;
  32. int xmin, ymin, xmax, ymax;
  33. char *obj_filename;
  34. int last_x, last_y;
  35. AVFrame *obj_frame;
  36. AVFrame *needle_frame[MAX_MIPMAPS];
  37. AVFrame *haystack_frame[MAX_MIPMAPS];
  38. } FOCContext;
  39. #define OFFSET(x) offsetof(FOCContext, x)
  40. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  41. static const AVOption find_rect_options[] = {
  42. { "object", "object bitmap filename", OFFSET(obj_filename), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  43. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1.0, FLAGS },
  44. { "mipmaps", "set mipmaps", OFFSET(mipmaps), AV_OPT_TYPE_INT, {.i64 = 3}, 1, MAX_MIPMAPS, FLAGS },
  45. { "xmin", "", OFFSET(xmin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  46. { "ymin", "", OFFSET(ymin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  47. { "xmax", "", OFFSET(xmax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  48. { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(find_rect);
  52. static int query_formats(AVFilterContext *ctx)
  53. {
  54. static const enum AVPixelFormat pix_fmts[] = {
  55. AV_PIX_FMT_YUV420P,
  56. AV_PIX_FMT_YUVJ420P,
  57. AV_PIX_FMT_NONE
  58. };
  59. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  60. }
  61. static AVFrame *downscale(AVFrame *in)
  62. {
  63. int x, y;
  64. AVFrame *frame = av_frame_alloc();
  65. uint8_t *src, *dst;
  66. if (!frame)
  67. return NULL;
  68. frame->format = in->format;
  69. frame->width = (in->width + 1) / 2;
  70. frame->height = (in->height+ 1) / 2;
  71. if (av_frame_get_buffer(frame, 32) < 0) {
  72. av_frame_free(&frame);
  73. return NULL;
  74. }
  75. src = in ->data[0];
  76. dst = frame->data[0];
  77. for(y = 0; y < frame->height; y++) {
  78. for(x = 0; x < frame->width; x++) {
  79. dst[x] = ( src[2*x+0]
  80. + src[2*x+1]
  81. + src[2*x+0 + in->linesize[0]]
  82. + src[2*x+1 + in->linesize[0]]
  83. + 2) >> 2;
  84. }
  85. src += 2*in->linesize[0];
  86. dst += frame->linesize[0];
  87. }
  88. return frame;
  89. }
  90. static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
  91. {
  92. int x,y;
  93. int o_sum_v = 0;
  94. int h_sum_v = 0;
  95. int64_t oo_sum_v = 0;
  96. int64_t hh_sum_v = 0;
  97. int64_t oh_sum_v = 0;
  98. float c;
  99. int n = obj->height * obj->width;
  100. const uint8_t *odat = obj ->data[0];
  101. const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
  102. int64_t o_sigma, h_sigma;
  103. for(y = 0; y < obj->height; y++) {
  104. for(x = 0; x < obj->width; x++) {
  105. int o_v = odat[x];
  106. int h_v = hdat[x];
  107. o_sum_v += o_v;
  108. h_sum_v += h_v;
  109. oo_sum_v += o_v * o_v;
  110. hh_sum_v += h_v * h_v;
  111. oh_sum_v += o_v * h_v;
  112. }
  113. odat += obj->linesize[0];
  114. hdat += haystack->linesize[0];
  115. }
  116. o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
  117. h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
  118. if (o_sigma == 0 || h_sigma == 0)
  119. return 1.0;
  120. c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
  121. return 1 - fabs(c);
  122. }
  123. static int config_input(AVFilterLink *inlink)
  124. {
  125. AVFilterContext *ctx = inlink->dst;
  126. FOCContext *foc = ctx->priv;
  127. if (foc->xmax <= 0)
  128. foc->xmax = inlink->w - foc->obj_frame->width;
  129. if (foc->ymax <= 0)
  130. foc->ymax = inlink->h - foc->obj_frame->height;
  131. return 0;
  132. }
  133. 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)
  134. {
  135. int x, y;
  136. if (pass + 1 <= maxpass) {
  137. int sub_x, sub_y;
  138. search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 2.0);
  139. xmin = FFMAX(xmin, 2*sub_x - 4);
  140. xmax = FFMIN(xmax, 2*sub_x + 4);
  141. ymin = FFMAX(ymin, 2*sub_y - 4);
  142. ymax = FFMIN(ymax, 2*sub_y + 4);
  143. }
  144. for (y = ymin; y <= ymax; y++) {
  145. for (x = xmin; x <= xmax; x++) {
  146. float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
  147. if (score < best_score) {
  148. best_score = score;
  149. *best_x = x;
  150. *best_y = y;
  151. }
  152. }
  153. }
  154. return best_score;
  155. }
  156. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  157. {
  158. AVFilterContext *ctx = inlink->dst;
  159. FOCContext *foc = ctx->priv;
  160. float best_score;
  161. int best_x, best_y;
  162. int i;
  163. foc->haystack_frame[0] = av_frame_clone(in);
  164. for (i=1; i<foc->mipmaps; i++) {
  165. foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
  166. }
  167. best_score = search(foc, 0, 0,
  168. FFMAX(foc->xmin, foc->last_x - 8),
  169. FFMIN(foc->xmax, foc->last_x + 8),
  170. FFMAX(foc->ymin, foc->last_y - 8),
  171. FFMIN(foc->ymax, foc->last_y + 8),
  172. &best_x, &best_y, 2.0);
  173. best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
  174. &best_x, &best_y, best_score);
  175. for (i=0; i<MAX_MIPMAPS; i++) {
  176. av_frame_free(&foc->haystack_frame[i]);
  177. }
  178. if (best_score > foc->threshold) {
  179. return ff_filter_frame(ctx->outputs[0], in);
  180. }
  181. av_log(ctx, AV_LOG_DEBUG, "Found at %d %d score %f\n", best_x, best_y, best_score);
  182. foc->last_x = best_x;
  183. foc->last_y = best_y;
  184. av_frame_make_writable(in);
  185. av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
  186. av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
  187. av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
  188. av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
  189. return ff_filter_frame(ctx->outputs[0], in);
  190. }
  191. static av_cold void uninit(AVFilterContext *ctx)
  192. {
  193. FOCContext *foc = ctx->priv;
  194. int i;
  195. for (i = 0; i < MAX_MIPMAPS; i++) {
  196. av_frame_free(&foc->needle_frame[i]);
  197. av_frame_free(&foc->haystack_frame[i]);
  198. }
  199. if (foc->obj_frame)
  200. av_freep(&foc->obj_frame->data[0]);
  201. av_frame_free(&foc->obj_frame);
  202. }
  203. static av_cold int init(AVFilterContext *ctx)
  204. {
  205. FOCContext *foc = ctx->priv;
  206. int ret, i;
  207. if (!foc->obj_filename) {
  208. av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
  209. return AVERROR(EINVAL);
  210. }
  211. foc->obj_frame = av_frame_alloc();
  212. if (!foc->obj_frame)
  213. return AVERROR(ENOMEM);
  214. if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
  215. &foc->obj_frame->width, &foc->obj_frame->height,
  216. &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
  217. return ret;
  218. if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
  219. av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
  220. return AVERROR(EINVAL);
  221. }
  222. foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
  223. for (i = 1; i < foc->mipmaps; i++) {
  224. foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
  225. if (!foc->needle_frame[i])
  226. return AVERROR(ENOMEM);
  227. }
  228. return 0;
  229. }
  230. static const AVFilterPad foc_inputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. .config_props = config_input,
  235. .filter_frame = filter_frame,
  236. },
  237. { NULL }
  238. };
  239. static const AVFilterPad foc_outputs[] = {
  240. {
  241. .name = "default",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. },
  244. { NULL }
  245. };
  246. AVFilter ff_vf_find_rect = {
  247. .name = "find_rect",
  248. .description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
  249. .priv_size = sizeof(FOCContext),
  250. .init = init,
  251. .uninit = uninit,
  252. .query_formats = query_formats,
  253. .inputs = foc_inputs,
  254. .outputs = foc_outputs,
  255. .priv_class = &find_rect_class,
  256. };