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.

280 lines
9.0KB

  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. int 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. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  60. return 0;
  61. }
  62. static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
  63. {
  64. int total = 0;
  65. int div = len;
  66. const uint16_t *src16 = (const uint16_t *)src;
  67. switch (bpp) {
  68. case 1:
  69. while (len >= 8) {
  70. total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
  71. + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
  72. src += 8*stride;
  73. len -= 8;
  74. }
  75. while (--len >= 0) {
  76. total += src[0];
  77. src += stride;
  78. }
  79. break;
  80. case 2:
  81. stride >>= 1;
  82. while (len >= 8) {
  83. total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
  84. + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
  85. src += 8*stride;
  86. len -= 8;
  87. }
  88. while (--len >= 0) {
  89. total += src16[0];
  90. src += stride;
  91. }
  92. break;
  93. case 3:
  94. case 4:
  95. while (len >= 4) {
  96. total += src[0] + src[1 ] + src[2 ]
  97. + src[ stride] + src[1+ stride] + src[2+ stride]
  98. + src[2*stride] + src[1+2*stride] + src[2+2*stride]
  99. + src[3*stride] + src[1+3*stride] + src[2+3*stride];
  100. src += 4*stride;
  101. len -= 4;
  102. }
  103. while (--len >= 0) {
  104. total += src[0] + src[1] + src[2];
  105. src += stride;
  106. }
  107. div *= 3;
  108. break;
  109. }
  110. total /= div;
  111. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  112. return total;
  113. }
  114. static av_cold int init(AVFilterContext *ctx)
  115. {
  116. CropDetectContext *s = ctx->priv;
  117. s->frame_nb = -2;
  118. av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
  119. s->limit, s->round, s->reset_count);
  120. return 0;
  121. }
  122. static int config_input(AVFilterLink *inlink)
  123. {
  124. AVFilterContext *ctx = inlink->dst;
  125. CropDetectContext *s = ctx->priv;
  126. av_image_fill_max_pixsteps(s->max_pixsteps, NULL,
  127. av_pix_fmt_desc_get(inlink->format));
  128. s->x1 = inlink->w - 1;
  129. s->y1 = inlink->h - 1;
  130. s->x2 = 0;
  131. s->y2 = 0;
  132. return 0;
  133. }
  134. #define SET_META(key, value) \
  135. av_dict_set_int(metadata, key, value, 0)
  136. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  137. {
  138. AVFilterContext *ctx = inlink->dst;
  139. CropDetectContext *s = ctx->priv;
  140. int bpp = s->max_pixsteps[0];
  141. int w, h, x, y, shrink_by;
  142. AVDictionary **metadata;
  143. int outliers, last_y;
  144. // ignore first 2 frames - they may be empty
  145. if (++s->frame_nb > 0) {
  146. metadata = avpriv_frame_get_metadatap(frame);
  147. // Reset the crop area every reset_count frames, if reset_count is > 0
  148. if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
  149. s->x1 = frame->width - 1;
  150. s->y1 = frame->height - 1;
  151. s->x2 = 0;
  152. s->y2 = 0;
  153. s->frame_nb = 1;
  154. }
  155. #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
  156. outliers = 0;\
  157. for (last_y = y = FROM; NOEND; y = y INC) {\
  158. if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > s->limit) {\
  159. if (++outliers > s->max_outliers) { \
  160. DST = last_y;\
  161. break;\
  162. }\
  163. } else\
  164. last_y = y INC;\
  165. }
  166. FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
  167. FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
  168. FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
  169. FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
  170. // round x and y (up), important for yuv colorspaces
  171. // make sure they stay rounded!
  172. x = (s->x1+1) & ~1;
  173. y = (s->y1+1) & ~1;
  174. w = s->x2 - x + 1;
  175. h = s->y2 - y + 1;
  176. // w and h must be divisible by 2 as well because of yuv
  177. // colorspace problems.
  178. if (s->round <= 1)
  179. s->round = 16;
  180. if (s->round % 2)
  181. s->round *= 2;
  182. shrink_by = w % s->round;
  183. w -= shrink_by;
  184. x += (shrink_by/2 + 1) & ~1;
  185. shrink_by = h % s->round;
  186. h -= shrink_by;
  187. y += (shrink_by/2 + 1) & ~1;
  188. SET_META("lavfi.cropdetect.x1", s->x1);
  189. SET_META("lavfi.cropdetect.x2", s->x2);
  190. SET_META("lavfi.cropdetect.y1", s->y1);
  191. SET_META("lavfi.cropdetect.y2", s->y2);
  192. SET_META("lavfi.cropdetect.w", w);
  193. SET_META("lavfi.cropdetect.h", h);
  194. SET_META("lavfi.cropdetect.x", x);
  195. SET_META("lavfi.cropdetect.y", y);
  196. av_log(ctx, AV_LOG_INFO,
  197. "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",
  198. s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
  199. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  200. w, h, x, y);
  201. }
  202. return ff_filter_frame(inlink->dst->outputs[0], frame);
  203. }
  204. #define OFFSET(x) offsetof(CropDetectContext, x)
  205. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  206. static const AVOption cropdetect_options[] = {
  207. { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 65535, FLAGS },
  208. { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
  209. { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  210. { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
  211. { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  212. { NULL }
  213. };
  214. AVFILTER_DEFINE_CLASS(cropdetect);
  215. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .config_props = config_input,
  220. .filter_frame = filter_frame,
  221. },
  222. { NULL }
  223. };
  224. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  225. {
  226. .name = "default",
  227. .type = AVMEDIA_TYPE_VIDEO
  228. },
  229. { NULL }
  230. };
  231. AVFilter ff_vf_cropdetect = {
  232. .name = "cropdetect",
  233. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  234. .priv_size = sizeof(CropDetectContext),
  235. .priv_class = &cropdetect_class,
  236. .init = init,
  237. .query_formats = query_formats,
  238. .inputs = avfilter_vf_cropdetect_inputs,
  239. .outputs = avfilter_vf_cropdetect_outputs,
  240. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  241. };