vf_cropdetect.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2002 A'rpi
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. /**
  20. * @file
  21. * border detection filter
  22. * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct CropDetectContext {
  32. const AVClass *class;
  33. int x1, y1, x2, y2;
  34. float limit;
  35. int round;
  36. int reset_count;
  37. int frame_nb;
  38. int max_pixsteps[4];
  39. int max_outliers;
  40. } CropDetectContext;
  41. static int query_formats(AVFilterContext *ctx)
  42. {
  43. static const enum AVPixelFormat pix_fmts[] = {
  44. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  45. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  46. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  47. AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  48. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
  49. AV_PIX_FMT_YUV420P9 , AV_PIX_FMT_YUV422P9 , AV_PIX_FMT_YUV444P9,
  50. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  51. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  52. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  53. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  54. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  55. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  56. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  57. AV_PIX_FMT_NONE
  58. };
  59. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  60. if (!fmts_list)
  61. return AVERROR(ENOMEM);
  62. return ff_set_common_formats(ctx, fmts_list);
  63. }
  64. static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
  65. {
  66. int total = 0;
  67. int div = len;
  68. const uint16_t *src16 = (const uint16_t *)src;
  69. switch (bpp) {
  70. case 1:
  71. while (len >= 8) {
  72. total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
  73. + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
  74. src += 8*stride;
  75. len -= 8;
  76. }
  77. while (--len >= 0) {
  78. total += src[0];
  79. src += stride;
  80. }
  81. break;
  82. case 2:
  83. stride >>= 1;
  84. while (len >= 8) {
  85. total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
  86. + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
  87. src16 += 8*stride;
  88. len -= 8;
  89. }
  90. while (--len >= 0) {
  91. total += src16[0];
  92. src16 += stride;
  93. }
  94. break;
  95. case 3:
  96. case 4:
  97. while (len >= 4) {
  98. total += src[0] + src[1 ] + src[2 ]
  99. + src[ stride] + src[1+ stride] + src[2+ stride]
  100. + src[2*stride] + src[1+2*stride] + src[2+2*stride]
  101. + src[3*stride] + src[1+3*stride] + src[2+3*stride];
  102. src += 4*stride;
  103. len -= 4;
  104. }
  105. while (--len >= 0) {
  106. total += src[0] + src[1] + src[2];
  107. src += stride;
  108. }
  109. div *= 3;
  110. break;
  111. }
  112. total /= div;
  113. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  114. return total;
  115. }
  116. static av_cold int init(AVFilterContext *ctx)
  117. {
  118. CropDetectContext *s = ctx->priv;
  119. s->frame_nb = -2;
  120. av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d reset_count:%d\n",
  121. s->limit, s->round, s->reset_count);
  122. return 0;
  123. }
  124. static int config_input(AVFilterLink *inlink)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. CropDetectContext *s = ctx->priv;
  128. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  129. av_image_fill_max_pixsteps(s->max_pixsteps, NULL, desc);
  130. if (s->limit < 1.0)
  131. s->limit *= (1 << desc->comp[0].depth) - 1;
  132. s->x1 = inlink->w - 1;
  133. s->y1 = inlink->h - 1;
  134. s->x2 = 0;
  135. s->y2 = 0;
  136. return 0;
  137. }
  138. #define SET_META(key, value) \
  139. av_dict_set_int(metadata, key, value, 0)
  140. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  141. {
  142. AVFilterContext *ctx = inlink->dst;
  143. CropDetectContext *s = ctx->priv;
  144. int bpp = s->max_pixsteps[0];
  145. int w, h, x, y, shrink_by;
  146. AVDictionary **metadata;
  147. int outliers, last_y;
  148. int limit = lrint(s->limit);
  149. // ignore first 2 frames - they may be empty
  150. if (++s->frame_nb > 0) {
  151. metadata = &frame->metadata;
  152. // Reset the crop area every reset_count frames, if reset_count is > 0
  153. if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
  154. s->x1 = frame->width - 1;
  155. s->y1 = frame->height - 1;
  156. s->x2 = 0;
  157. s->y2 = 0;
  158. s->frame_nb = 1;
  159. }
  160. #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
  161. outliers = 0;\
  162. for (last_y = y = FROM; NOEND; y = y INC) {\
  163. if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit) {\
  164. if (++outliers > s->max_outliers) { \
  165. DST = last_y;\
  166. break;\
  167. }\
  168. } else\
  169. last_y = y INC;\
  170. }
  171. FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
  172. FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
  173. FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
  174. FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
  175. // round x and y (up), important for yuv colorspaces
  176. // make sure they stay rounded!
  177. x = (s->x1+1) & ~1;
  178. y = (s->y1+1) & ~1;
  179. w = s->x2 - x + 1;
  180. h = s->y2 - y + 1;
  181. // w and h must be divisible by 2 as well because of yuv
  182. // colorspace problems.
  183. if (s->round <= 1)
  184. s->round = 16;
  185. if (s->round % 2)
  186. s->round *= 2;
  187. shrink_by = w % s->round;
  188. w -= shrink_by;
  189. x += (shrink_by/2 + 1) & ~1;
  190. shrink_by = h % s->round;
  191. h -= shrink_by;
  192. y += (shrink_by/2 + 1) & ~1;
  193. SET_META("lavfi.cropdetect.x1", s->x1);
  194. SET_META("lavfi.cropdetect.x2", s->x2);
  195. SET_META("lavfi.cropdetect.y1", s->y1);
  196. SET_META("lavfi.cropdetect.y2", s->y2);
  197. SET_META("lavfi.cropdetect.w", w);
  198. SET_META("lavfi.cropdetect.h", h);
  199. SET_META("lavfi.cropdetect.x", x);
  200. SET_META("lavfi.cropdetect.y", y);
  201. av_log(ctx, AV_LOG_INFO,
  202. "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
  203. s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
  204. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  205. w, h, x, y);
  206. }
  207. return ff_filter_frame(inlink->dst->outputs[0], frame);
  208. }
  209. #define OFFSET(x) offsetof(CropDetectContext, x)
  210. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  211. static const AVOption cropdetect_options[] = {
  212. { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, FLAGS },
  213. { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
  214. { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  215. { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
  216. { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  217. { NULL }
  218. };
  219. AVFILTER_DEFINE_CLASS(cropdetect);
  220. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  221. {
  222. .name = "default",
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .config_props = config_input,
  225. .filter_frame = filter_frame,
  226. },
  227. { NULL }
  228. };
  229. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  230. {
  231. .name = "default",
  232. .type = AVMEDIA_TYPE_VIDEO
  233. },
  234. { NULL }
  235. };
  236. AVFilter ff_vf_cropdetect = {
  237. .name = "cropdetect",
  238. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  239. .priv_size = sizeof(CropDetectContext),
  240. .priv_class = &cropdetect_class,
  241. .init = init,
  242. .query_formats = query_formats,
  243. .inputs = avfilter_vf_cropdetect_inputs,
  244. .outputs = avfilter_vf_cropdetect_outputs,
  245. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  246. };