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.

217 lines
6.3KB

  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 "avfilter.h"
  26. #include "formats.h"
  27. #include "video.h"
  28. typedef struct {
  29. int x1, y1, x2, y2;
  30. int limit;
  31. int round;
  32. int reset_count;
  33. int frame_nb;
  34. int max_pixsteps[4];
  35. } CropDetectContext;
  36. static int query_formats(AVFilterContext *ctx)
  37. {
  38. static const enum PixelFormat pix_fmts[] = {
  39. PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
  40. PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
  41. PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
  42. PIX_FMT_YUV411P, PIX_FMT_GRAY8,
  43. PIX_FMT_NV12, PIX_FMT_NV21,
  44. PIX_FMT_NONE
  45. };
  46. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  47. return 0;
  48. }
  49. static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
  50. {
  51. int total = 0;
  52. int div = len;
  53. switch (bpp) {
  54. case 1:
  55. while (--len >= 0) {
  56. total += src[0];
  57. src += stride;
  58. }
  59. break;
  60. case 3:
  61. case 4:
  62. while (--len >= 0) {
  63. total += src[0] + src[1] + src[2];
  64. src += stride;
  65. }
  66. div *= 3;
  67. break;
  68. }
  69. total /= div;
  70. av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
  71. return total;
  72. }
  73. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  74. {
  75. CropDetectContext *cd = ctx->priv;
  76. cd->limit = 24;
  77. cd->round = 0;
  78. cd->reset_count = 0;
  79. cd->frame_nb = -2;
  80. if (args)
  81. sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
  82. av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n",
  83. cd->limit, cd->round, cd->reset_count);
  84. return 0;
  85. }
  86. static int config_input(AVFilterLink *inlink)
  87. {
  88. AVFilterContext *ctx = inlink->dst;
  89. CropDetectContext *cd = ctx->priv;
  90. av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
  91. &av_pix_fmt_descriptors[inlink->format]);
  92. cd->x1 = inlink->w - 1;
  93. cd->y1 = inlink->h - 1;
  94. cd->x2 = 0;
  95. cd->y2 = 0;
  96. return 0;
  97. }
  98. static void end_frame(AVFilterLink *inlink)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. CropDetectContext *cd = ctx->priv;
  102. AVFilterBufferRef *picref = inlink->cur_buf;
  103. int bpp = cd->max_pixsteps[0];
  104. int w, h, x, y, shrink_by;
  105. // ignore first 2 frames - they may be empty
  106. if (++cd->frame_nb > 0) {
  107. // Reset the crop area every reset_count frames, if reset_count is > 0
  108. if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
  109. cd->x1 = picref->video->w-1;
  110. cd->y1 = picref->video->h-1;
  111. cd->x2 = 0;
  112. cd->y2 = 0;
  113. cd->frame_nb = 1;
  114. }
  115. for (y = 0; y < cd->y1; y++) {
  116. if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
  117. cd->y1 = y;
  118. break;
  119. }
  120. }
  121. for (y = picref->video->h-1; y > cd->y2; y--) {
  122. if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
  123. cd->y2 = y;
  124. break;
  125. }
  126. }
  127. for (y = 0; y < cd->x1; y++) {
  128. if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
  129. cd->x1 = y;
  130. break;
  131. }
  132. }
  133. for (y = picref->video->w-1; y > cd->x2; y--) {
  134. if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
  135. cd->x2 = y;
  136. break;
  137. }
  138. }
  139. // round x and y (up), important for yuv colorspaces
  140. // make sure they stay rounded!
  141. x = (cd->x1+1) & ~1;
  142. y = (cd->y1+1) & ~1;
  143. w = cd->x2 - x + 1;
  144. h = cd->y2 - y + 1;
  145. // w and h must be divisible by 2 as well because of yuv
  146. // colorspace problems.
  147. if (cd->round <= 1)
  148. cd->round = 16;
  149. if (cd->round % 2)
  150. cd->round *= 2;
  151. shrink_by = w % cd->round;
  152. w -= shrink_by;
  153. x += (shrink_by/2 + 1) & ~1;
  154. shrink_by = h % cd->round;
  155. h -= shrink_by;
  156. y += (shrink_by/2 + 1) & ~1;
  157. av_log(ctx, AV_LOG_INFO,
  158. "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
  159. cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
  160. picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
  161. w, h, x, y);
  162. }
  163. ff_end_frame(inlink->dst->outputs[0]);
  164. }
  165. AVFilter avfilter_vf_cropdetect = {
  166. .name = "cropdetect",
  167. .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
  168. .priv_size = sizeof(CropDetectContext),
  169. .init = init,
  170. .query_formats = query_formats,
  171. .inputs = (const AVFilterPad[]) {{ .name = "default",
  172. .type = AVMEDIA_TYPE_VIDEO,
  173. .config_props = config_input,
  174. .get_video_buffer = ff_null_get_video_buffer,
  175. .start_frame = ff_null_start_frame,
  176. .end_frame = end_frame, },
  177. { .name = NULL}},
  178. .outputs = (const AVFilterPad[]) {{ .name = "default",
  179. .type = AVMEDIA_TYPE_VIDEO },
  180. { .name = NULL}},
  181. };