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.

237 lines
6.8KB

  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 {
  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. } CropDetectContext;
  40. #define OFFSET(x) offsetof(CropDetectContext, x)
  41. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  42. static const AVOption cropdetect_options[] = {
  43. { "limit", "set black threshold", OFFSET(limit), AV_OPT_TYPE_INT, {.i64=24}, 0, 255, FLAGS },
  44. { "round", "set width/height round value", OFFSET(round), AV_OPT_TYPE_INT, {.i64=16}, 0, INT_MAX, FLAGS },
  45. { "reset_count", "set after how many frames to reset detected info", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  46. { NULL }
  47. };
  48. AVFILTER_DEFINE_CLASS(cropdetect);
  49. static int query_formats(AVFilterContext *ctx)
  50. {
  51. static const enum AVPixelFormat pix_fmts[] = {
  52. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  53. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  54. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  55. AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  56. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  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. switch (bpp) {
  67. case 1:
  68. while (--len >= 0) {
  69. total += src[0];
  70. src += stride;
  71. }
  72. break;
  73. case 3:
  74. case 4:
  75. while (--len >= 0) {
  76. total += src[0] + src[1] + src[2];
  77. src += stride;
  78. }
  79. div *= 3;
  80. break;
  81. }
  82. total /= div;
  83. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  84. return total;
  85. }
  86. static av_cold int init(AVFilterContext *ctx, const char *args)
  87. {
  88. CropDetectContext *cd = ctx->priv;
  89. cd->frame_nb = -2;
  90. av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
  91. cd->limit, cd->round, cd->reset_count);
  92. return 0;
  93. }
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. CropDetectContext *cd = ctx->priv;
  98. av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
  99. av_pix_fmt_desc_get(inlink->format));
  100. cd->x1 = inlink->w - 1;
  101. cd->y1 = inlink->h - 1;
  102. cd->x2 = 0;
  103. cd->y2 = 0;
  104. return 0;
  105. }
  106. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  107. {
  108. AVFilterContext *ctx = inlink->dst;
  109. CropDetectContext *cd = ctx->priv;
  110. int bpp = cd->max_pixsteps[0];
  111. int w, h, x, y, shrink_by;
  112. // ignore first 2 frames - they may be empty
  113. if (++cd->frame_nb > 0) {
  114. // Reset the crop area every reset_count frames, if reset_count is > 0
  115. if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
  116. cd->x1 = frame->width - 1;
  117. cd->y1 = frame->height - 1;
  118. cd->x2 = 0;
  119. cd->y2 = 0;
  120. cd->frame_nb = 1;
  121. }
  122. for (y = 0; y < cd->y1; y++) {
  123. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
  124. cd->y1 = y;
  125. break;
  126. }
  127. }
  128. for (y = frame->height - 1; y > cd->y2; y--) {
  129. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
  130. cd->y2 = y;
  131. break;
  132. }
  133. }
  134. for (y = 0; y < cd->x1; y++) {
  135. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
  136. cd->x1 = y;
  137. break;
  138. }
  139. }
  140. for (y = frame->width - 1; y > cd->x2; y--) {
  141. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
  142. cd->x2 = y;
  143. break;
  144. }
  145. }
  146. // round x and y (up), important for yuv colorspaces
  147. // make sure they stay rounded!
  148. x = (cd->x1+1) & ~1;
  149. y = (cd->y1+1) & ~1;
  150. w = cd->x2 - x + 1;
  151. h = cd->y2 - y + 1;
  152. // w and h must be divisible by 2 as well because of yuv
  153. // colorspace problems.
  154. if (cd->round <= 1)
  155. cd->round = 16;
  156. if (cd->round % 2)
  157. cd->round *= 2;
  158. shrink_by = w % cd->round;
  159. w -= shrink_by;
  160. x += (shrink_by/2 + 1) & ~1;
  161. shrink_by = h % cd->round;
  162. h -= shrink_by;
  163. y += (shrink_by/2 + 1) & ~1;
  164. av_log(ctx, AV_LOG_INFO,
  165. "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",
  166. cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
  167. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  168. w, h, x, y);
  169. }
  170. return ff_filter_frame(inlink->dst->outputs[0], frame);
  171. }
  172. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  173. {
  174. .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO,
  176. .config_props = config_input,
  177. .get_video_buffer = ff_null_get_video_buffer,
  178. .filter_frame = filter_frame,
  179. },
  180. { NULL }
  181. };
  182. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  183. {
  184. .name = "default",
  185. .type = AVMEDIA_TYPE_VIDEO
  186. },
  187. { NULL }
  188. };
  189. static const char *const shorthand[] = { "limit", "round", "reset_count", NULL };
  190. AVFilter avfilter_vf_cropdetect = {
  191. .name = "cropdetect",
  192. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  193. .priv_size = sizeof(CropDetectContext),
  194. .init = init,
  195. .query_formats = query_formats,
  196. .inputs = avfilter_vf_cropdetect_inputs,
  197. .outputs = avfilter_vf_cropdetect_outputs,
  198. .priv_class = &cropdetect_class,
  199. .shorthand = shorthand,
  200. };