vf_cropdetect.c 6.3 KB

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