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.

218 lines
6.3KB

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