vf_cropdetect.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <stdio.h>
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/internal.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct {
  32. int x1, y1, x2, y2;
  33. int limit;
  34. int round;
  35. int reset_count;
  36. int frame_nb;
  37. int max_pixsteps[4];
  38. } CropDetectContext;
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. static const enum PixelFormat pix_fmts[] = {
  42. PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
  43. PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
  44. PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
  45. PIX_FMT_YUV411P, PIX_FMT_GRAY8,
  46. PIX_FMT_NV12, PIX_FMT_NV21,
  47. PIX_FMT_NONE
  48. };
  49. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  50. return 0;
  51. }
  52. static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
  53. {
  54. int total = 0;
  55. int div = len;
  56. switch (bpp) {
  57. case 1:
  58. while (--len >= 0) {
  59. total += src[0];
  60. src += stride;
  61. }
  62. break;
  63. case 3:
  64. case 4:
  65. while (--len >= 0) {
  66. total += src[0] + src[1] + src[2];
  67. src += stride;
  68. }
  69. div *= 3;
  70. break;
  71. }
  72. total /= div;
  73. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  74. return total;
  75. }
  76. static av_cold int init(AVFilterContext *ctx, const char *args)
  77. {
  78. CropDetectContext *cd = ctx->priv;
  79. cd->limit = 24;
  80. cd->round = 0;
  81. cd->reset_count = 0;
  82. cd->frame_nb = -2;
  83. if (args)
  84. sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
  85. av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
  86. cd->limit, cd->round, cd->reset_count);
  87. return 0;
  88. }
  89. static int config_input(AVFilterLink *inlink)
  90. {
  91. AVFilterContext *ctx = inlink->dst;
  92. CropDetectContext *cd = ctx->priv;
  93. av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
  94. &av_pix_fmt_descriptors[inlink->format]);
  95. cd->x1 = inlink->w - 1;
  96. cd->y1 = inlink->h - 1;
  97. cd->x2 = 0;
  98. cd->y2 = 0;
  99. return 0;
  100. }
  101. static int end_frame(AVFilterLink *inlink)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. CropDetectContext *cd = ctx->priv;
  105. AVFilterBufferRef *picref = inlink->cur_buf;
  106. int bpp = cd->max_pixsteps[0];
  107. int w, h, x, y, shrink_by;
  108. // ignore first 2 frames - they may be empty
  109. if (++cd->frame_nb > 0) {
  110. // Reset the crop area every reset_count frames, if reset_count is > 0
  111. if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
  112. cd->x1 = picref->video->w-1;
  113. cd->y1 = picref->video->h-1;
  114. cd->x2 = 0;
  115. cd->y2 = 0;
  116. cd->frame_nb = 1;
  117. }
  118. for (y = 0; y < cd->y1; y++) {
  119. if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
  120. cd->y1 = y;
  121. break;
  122. }
  123. }
  124. for (y = picref->video->h-1; y > cd->y2; y--) {
  125. if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
  126. cd->y2 = y;
  127. break;
  128. }
  129. }
  130. for (y = 0; y < cd->x1; y++) {
  131. if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
  132. cd->x1 = y;
  133. break;
  134. }
  135. }
  136. for (y = picref->video->w-1; y > cd->x2; y--) {
  137. if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
  138. cd->x2 = y;
  139. break;
  140. }
  141. }
  142. // round x and y (up), important for yuv colorspaces
  143. // make sure they stay rounded!
  144. x = (cd->x1+1) & ~1;
  145. y = (cd->y1+1) & ~1;
  146. w = cd->x2 - x + 1;
  147. h = cd->y2 - y + 1;
  148. // w and h must be divisible by 2 as well because of yuv
  149. // colorspace problems.
  150. if (cd->round <= 1)
  151. cd->round = 16;
  152. if (cd->round % 2)
  153. cd->round *= 2;
  154. shrink_by = w % cd->round;
  155. w -= shrink_by;
  156. x += (shrink_by/2 + 1) & ~1;
  157. shrink_by = h % cd->round;
  158. h -= shrink_by;
  159. y += (shrink_by/2 + 1) & ~1;
  160. av_log(ctx, AV_LOG_INFO,
  161. "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
  162. cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
  163. picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
  164. w, h, x, y);
  165. }
  166. return ff_end_frame(inlink->dst->outputs[0]);
  167. }
  168. AVFilter avfilter_vf_cropdetect = {
  169. .name = "cropdetect",
  170. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  171. .priv_size = sizeof(CropDetectContext),
  172. .init = init,
  173. .query_formats = query_formats,
  174. .inputs = (const AVFilterPad[]) {{ .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO,
  176. .config_props = config_input,
  177. .get_video_buffer = ff_null_get_video_buffer,
  178. .start_frame = ff_null_start_frame,
  179. .end_frame = end_frame, },
  180. { .name = NULL}},
  181. .outputs = (const AVFilterPad[]) {{ .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO },
  183. { .name = NULL}},
  184. };