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.

249 lines
7.0KB

  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. static const char *shorthand[] = { "limit", "round", "reset_count", NULL };
  90. int ret;
  91. cd->frame_nb = -2;
  92. cd->class = &cropdetect_class;
  93. av_opt_set_defaults(cd);
  94. if ((ret = av_opt_set_from_string(cd, args, shorthand, "=", ":")) < 0)
  95. return ret;
  96. av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
  97. cd->limit, cd->round, cd->reset_count);
  98. return 0;
  99. }
  100. static av_cold void uninit(AVFilterContext *ctx)
  101. {
  102. CropDetectContext *cd = ctx->priv;
  103. av_opt_free(cd);
  104. }
  105. static int config_input(AVFilterLink *inlink)
  106. {
  107. AVFilterContext *ctx = inlink->dst;
  108. CropDetectContext *cd = ctx->priv;
  109. av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
  110. av_pix_fmt_desc_get(inlink->format));
  111. cd->x1 = inlink->w - 1;
  112. cd->y1 = inlink->h - 1;
  113. cd->x2 = 0;
  114. cd->y2 = 0;
  115. return 0;
  116. }
  117. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  118. {
  119. AVFilterContext *ctx = inlink->dst;
  120. CropDetectContext *cd = ctx->priv;
  121. int bpp = cd->max_pixsteps[0];
  122. int w, h, x, y, shrink_by;
  123. // ignore first 2 frames - they may be empty
  124. if (++cd->frame_nb > 0) {
  125. // Reset the crop area every reset_count frames, if reset_count is > 0
  126. if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
  127. cd->x1 = frame->width - 1;
  128. cd->y1 = frame->height - 1;
  129. cd->x2 = 0;
  130. cd->y2 = 0;
  131. cd->frame_nb = 1;
  132. }
  133. for (y = 0; y < cd->y1; y++) {
  134. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
  135. cd->y1 = y;
  136. break;
  137. }
  138. }
  139. for (y = frame->height - 1; y > cd->y2; y--) {
  140. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
  141. cd->y2 = y;
  142. break;
  143. }
  144. }
  145. for (y = 0; y < cd->x1; y++) {
  146. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
  147. cd->x1 = y;
  148. break;
  149. }
  150. }
  151. for (y = frame->width - 1; y > cd->x2; y--) {
  152. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
  153. cd->x2 = y;
  154. break;
  155. }
  156. }
  157. // round x and y (up), important for yuv colorspaces
  158. // make sure they stay rounded!
  159. x = (cd->x1+1) & ~1;
  160. y = (cd->y1+1) & ~1;
  161. w = cd->x2 - x + 1;
  162. h = cd->y2 - y + 1;
  163. // w and h must be divisible by 2 as well because of yuv
  164. // colorspace problems.
  165. if (cd->round <= 1)
  166. cd->round = 16;
  167. if (cd->round % 2)
  168. cd->round *= 2;
  169. shrink_by = w % cd->round;
  170. w -= shrink_by;
  171. x += (shrink_by/2 + 1) & ~1;
  172. shrink_by = h % cd->round;
  173. h -= shrink_by;
  174. y += (shrink_by/2 + 1) & ~1;
  175. av_log(ctx, AV_LOG_INFO,
  176. "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",
  177. cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
  178. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  179. w, h, x, y);
  180. }
  181. return ff_filter_frame(inlink->dst->outputs[0], frame);
  182. }
  183. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .config_props = config_input,
  188. .get_video_buffer = ff_null_get_video_buffer,
  189. .filter_frame = filter_frame,
  190. },
  191. { NULL }
  192. };
  193. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  194. {
  195. .name = "default",
  196. .type = AVMEDIA_TYPE_VIDEO
  197. },
  198. { NULL }
  199. };
  200. AVFilter avfilter_vf_cropdetect = {
  201. .name = "cropdetect",
  202. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  203. .priv_size = sizeof(CropDetectContext),
  204. .init = init,
  205. .uninit = uninit,
  206. .query_formats = query_formats,
  207. .inputs = avfilter_vf_cropdetect_inputs,
  208. .outputs = avfilter_vf_cropdetect_outputs,
  209. .priv_class = &cropdetect_class,
  210. };