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.

253 lines
7.4KB

  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. snprintf(buf, sizeof(buf), "%d", value); \
  99. av_dict_set(metadata, key, buf, 0)
  100. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  101. {
  102. AVFilterContext *ctx = inlink->dst;
  103. CropDetectContext *s = ctx->priv;
  104. int bpp = s->max_pixsteps[0];
  105. int w, h, x, y, shrink_by;
  106. AVDictionary **metadata;
  107. char buf[32];
  108. // ignore first 2 frames - they may be empty
  109. if (++s->frame_nb > 0) {
  110. metadata = avpriv_frame_get_metadatap(frame);
  111. // Reset the crop area every reset_count frames, if reset_count is > 0
  112. if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
  113. s->x1 = frame->width - 1;
  114. s->y1 = frame->height - 1;
  115. s->x2 = 0;
  116. s->y2 = 0;
  117. s->frame_nb = 1;
  118. }
  119. for (y = 0; y < s->y1; y++) {
  120. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
  121. s->y1 = y;
  122. break;
  123. }
  124. }
  125. for (y = frame->height - 1; y > s->y2; y--) {
  126. if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
  127. s->y2 = y;
  128. break;
  129. }
  130. }
  131. for (y = 0; y < s->x1; y++) {
  132. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
  133. s->x1 = y;
  134. break;
  135. }
  136. }
  137. for (y = frame->width - 1; y > s->x2; y--) {
  138. if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
  139. s->x2 = y;
  140. break;
  141. }
  142. }
  143. // round x and y (up), important for yuv colorspaces
  144. // make sure they stay rounded!
  145. x = (s->x1+1) & ~1;
  146. y = (s->y1+1) & ~1;
  147. w = s->x2 - x + 1;
  148. h = s->y2 - y + 1;
  149. // w and h must be divisible by 2 as well because of yuv
  150. // colorspace problems.
  151. if (s->round <= 1)
  152. s->round = 16;
  153. if (s->round % 2)
  154. s->round *= 2;
  155. shrink_by = w % s->round;
  156. w -= shrink_by;
  157. x += (shrink_by/2 + 1) & ~1;
  158. shrink_by = h % s->round;
  159. h -= shrink_by;
  160. y += (shrink_by/2 + 1) & ~1;
  161. SET_META("lavfi.cropdetect.x1", s->x1);
  162. SET_META("lavfi.cropdetect.x2", s->x2);
  163. SET_META("lavfi.cropdetect.y1", s->y1);
  164. SET_META("lavfi.cropdetect.y2", s->y2);
  165. SET_META("lavfi.cropdetect.w", w);
  166. SET_META("lavfi.cropdetect.h", h);
  167. SET_META("lavfi.cropdetect.x", x);
  168. SET_META("lavfi.cropdetect.y", y);
  169. av_log(ctx, AV_LOG_INFO,
  170. "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",
  171. s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
  172. frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
  173. w, h, x, y);
  174. }
  175. return ff_filter_frame(inlink->dst->outputs[0], frame);
  176. }
  177. #define OFFSET(x) offsetof(CropDetectContext, x)
  178. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  179. static const AVOption cropdetect_options[] = {
  180. { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255, FLAGS },
  181. { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
  182. { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  183. { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
  184. { NULL }
  185. };
  186. AVFILTER_DEFINE_CLASS(cropdetect);
  187. static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .config_props = config_input,
  192. .filter_frame = filter_frame,
  193. },
  194. { NULL }
  195. };
  196. static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_VIDEO
  200. },
  201. { NULL }
  202. };
  203. AVFilter ff_vf_cropdetect = {
  204. .name = "cropdetect",
  205. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  206. .priv_size = sizeof(CropDetectContext),
  207. .priv_class = &cropdetect_class,
  208. .init = init,
  209. .query_formats = query_formats,
  210. .inputs = avfilter_vf_cropdetect_inputs,
  211. .outputs = avfilter_vf_cropdetect_outputs,
  212. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  213. };