vf_cropdetect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2002 A'rpi
  3. * This file is part of Libav.
  4. *
  5. * Libav 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. * Libav 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 Libav; 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 <stdio.h>
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct CropDetectContext {
  33. const AVClass *class;
  34. int x1, y1, x2, y2;
  35. int limit;
  36. int round;
  37. int reset_count;
  38. int frame_nb;
  39. int max_pixsteps[4];
  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_NV12, AV_PIX_FMT_NV21,
  49. AV_PIX_FMT_NONE
  50. };
  51. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  52. return 0;
  53. }
  54. static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
  55. {
  56. int total = 0;
  57. int div = len;
  58. switch (bpp) {
  59. case 1:
  60. while (--len >= 0) {
  61. total += src[0];
  62. src += stride;
  63. }
  64. break;
  65. case 3:
  66. case 4:
  67. while (--len >= 0) {
  68. total += src[0] + src[1] + src[2];
  69. src += stride;
  70. }
  71. div *= 3;
  72. break;
  73. }
  74. total /= div;
  75. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  76. return total;
  77. }
  78. static av_cold int init(AVFilterContext *ctx)
  79. {
  80. CropDetectContext *s = ctx->priv;
  81. s->frame_nb = -2;
  82. av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
  83. s->limit, s->round, s->reset_count);
  84. return 0;
  85. }
  86. static int config_input(AVFilterLink *inlink)
  87. {
  88. AVFilterContext *ctx = inlink->dst;
  89. CropDetectContext *s = ctx->priv;
  90. av_image_fill_max_pixsteps(s->max_pixsteps, NULL,
  91. av_pix_fmt_desc_get(inlink->format));
  92. s->x1 = inlink->w - 1;
  93. s->y1 = inlink->h - 1;
  94. s->x2 = 0;
  95. s->y2 = 0;
  96. return 0;
  97. }
  98. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. CropDetectContext *s = ctx->priv;
  102. int bpp = s->max_pixsteps[0];
  103. int w, h, x, y, shrink_by;
  104. // ignore first 2 frames - they may be empty
  105. if (++s->frame_nb > 0) {
  106. // Reset the crop area every reset_count frames, if reset_count is > 0
  107. if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
  108. s->x1 = frame->width - 1;
  109. s->y1 = frame->height - 1;
  110. s->x2 = 0;
  111. s->y2 = 0;
  112. s->frame_nb = 1;
  113. }
  114. for (y = 0; y < s->y1; y++) {
  115. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
  116. s->y1 = y;
  117. break;
  118. }
  119. }
  120. for (y = frame->height - 1; y > s->y2; y--) {
  121. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
  122. s->y2 = y;
  123. break;
  124. }
  125. }
  126. for (y = 0; y < s->x1; y++) {
  127. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
  128. s->x1 = y;
  129. break;
  130. }
  131. }
  132. for (y = frame->width - 1; y > s->x2; y--) {
  133. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
  134. s->x2 = y;
  135. break;
  136. }
  137. }
  138. // round x and y (up), important for yuv colorspaces
  139. // make sure they stay rounded!
  140. x = (s->x1+1) & ~1;
  141. y = (s->y1+1) & ~1;
  142. w = s->x2 - x + 1;
  143. h = s->y2 - y + 1;
  144. // w and h must be divisible by 2 as well because of yuv
  145. // colorspace problems.
  146. if (s->round <= 1)
  147. s->round = 16;
  148. if (s->round % 2)
  149. s->round *= 2;
  150. shrink_by = w % s->round;
  151. w -= shrink_by;
  152. x += (shrink_by/2 + 1) & ~1;
  153. shrink_by = h % s->round;
  154. h -= shrink_by;
  155. y += (shrink_by/2 + 1) & ~1;
  156. av_log(ctx, AV_LOG_INFO,
  157. "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",
  158. s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
  159. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  160. w, h, x, y);
  161. }
  162. return ff_filter_frame(inlink->dst->outputs[0], frame);
  163. }
  164. #define OFFSET(x) offsetof(CropDetectContext, x)
  165. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  166. static const AVOption options[] = {
  167. { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, INT_MAX, FLAGS },
  168. { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  169. { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  170. { NULL },
  171. };
  172. static const AVClass cropdetect_class = {
  173. .class_name = "cropdetect",
  174. .item_name = av_default_item_name,
  175. .option = options,
  176. .version = LIBAVUTIL_VERSION_INT,
  177. };
  178. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .config_props = config_input,
  183. .get_video_buffer = ff_null_get_video_buffer,
  184. .filter_frame = filter_frame,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_VIDEO
  192. },
  193. { NULL }
  194. };
  195. AVFilter ff_vf_cropdetect = {
  196. .name = "cropdetect",
  197. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  198. .priv_size = sizeof(CropDetectContext),
  199. .priv_class = &cropdetect_class,
  200. .init = init,
  201. .query_formats = query_formats,
  202. .inputs = avfilter_vf_cropdetect_inputs,
  203. .outputs = avfilter_vf_cropdetect_outputs,
  204. };