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.

231 lines
6.2KB

  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 AVPixelFormat pix_fmts[] = {
  42. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  43. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  44. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  45. AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  46. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  47. AV_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_desc_get(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 filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. CropDetectContext *cd = ctx->priv;
  105. int bpp = cd->max_pixsteps[0];
  106. int w, h, x, y, shrink_by;
  107. // ignore first 2 frames - they may be empty
  108. if (++cd->frame_nb > 0) {
  109. // Reset the crop area every reset_count frames, if reset_count is > 0
  110. if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
  111. cd->x1 = frame->video->w-1;
  112. cd->y1 = frame->video->h-1;
  113. cd->x2 = 0;
  114. cd->y2 = 0;
  115. cd->frame_nb = 1;
  116. }
  117. for (y = 0; y < cd->y1; y++) {
  118. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
  119. cd->y1 = y;
  120. break;
  121. }
  122. }
  123. for (y = frame->video->h-1; y > cd->y2; y--) {
  124. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
  125. cd->y2 = y;
  126. break;
  127. }
  128. }
  129. for (y = 0; y < cd->x1; y++) {
  130. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
  131. cd->x1 = y;
  132. break;
  133. }
  134. }
  135. for (y = frame->video->w-1; y > cd->x2; y--) {
  136. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
  137. cd->x2 = y;
  138. break;
  139. }
  140. }
  141. // round x and y (up), important for yuv colorspaces
  142. // make sure they stay rounded!
  143. x = (cd->x1+1) & ~1;
  144. y = (cd->y1+1) & ~1;
  145. w = cd->x2 - x + 1;
  146. h = cd->y2 - y + 1;
  147. // w and h must be divisible by 2 as well because of yuv
  148. // colorspace problems.
  149. if (cd->round <= 1)
  150. cd->round = 16;
  151. if (cd->round % 2)
  152. cd->round *= 2;
  153. shrink_by = w % cd->round;
  154. w -= shrink_by;
  155. x += (shrink_by/2 + 1) & ~1;
  156. shrink_by = h % cd->round;
  157. h -= shrink_by;
  158. y += (shrink_by/2 + 1) & ~1;
  159. av_log(ctx, AV_LOG_INFO,
  160. "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",
  161. cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pos, frame->pts,
  162. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  163. w, h, x, y);
  164. }
  165. return ff_filter_frame(inlink->dst->outputs[0], frame);
  166. }
  167. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  168. {
  169. .name = "default",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .config_props = config_input,
  172. .get_video_buffer = ff_null_get_video_buffer,
  173. .filter_frame = filter_frame,
  174. },
  175. { NULL }
  176. };
  177. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  178. {
  179. .name = "default",
  180. .type = AVMEDIA_TYPE_VIDEO
  181. },
  182. { NULL }
  183. };
  184. AVFilter avfilter_vf_cropdetect = {
  185. .name = "cropdetect",
  186. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  187. .priv_size = sizeof(CropDetectContext),
  188. .init = init,
  189. .query_formats = query_formats,
  190. .inputs = avfilter_vf_cropdetect_inputs,
  191. .outputs = avfilter_vf_cropdetect_outputs,
  192. };