vf_cropdetect.c 7.4 KB

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