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.

245 lines
7.5KB

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