vf_cropdetect.c
Go to the documentation of this file.
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 /**
21  * @file
22  * border detection filter
23  * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
24  */
25 
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef struct {
36  const AVClass *class;
37  int x1, y1, x2, y2;
38  int limit;
39  int round;
41  int frame_nb;
42  int max_pixsteps[4];
44 
46 {
47  static const enum AVPixelFormat pix_fmts[] = {
54  };
55 
57  return 0;
58 }
59 
60 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
61 {
62  int total = 0;
63  int div = len;
64 
65  switch (bpp) {
66  case 1:
67  while (--len >= 0) {
68  total += src[0];
69  src += stride;
70  }
71  break;
72  case 3:
73  case 4:
74  while (--len >= 0) {
75  total += src[0] + src[1] + src[2];
76  src += stride;
77  }
78  div *= 3;
79  break;
80  }
81  total /= div;
82 
83  av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
84  return total;
85 }
86 
87 static av_cold int init(AVFilterContext *ctx)
88 {
89  CropDetectContext *cd = ctx->priv;
90 
91  cd->frame_nb = -2;
92 
93  av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
94  cd->limit, cd->round, cd->reset_count);
95 
96  return 0;
97 }
98 
99 static int config_input(AVFilterLink *inlink)
100 {
101  AVFilterContext *ctx = inlink->dst;
102  CropDetectContext *cd = ctx->priv;
103 
105  av_pix_fmt_desc_get(inlink->format));
106 
107  cd->x1 = inlink->w - 1;
108  cd->y1 = inlink->h - 1;
109  cd->x2 = 0;
110  cd->y2 = 0;
111 
112  return 0;
113 }
114 
115 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
116 {
117  AVFilterContext *ctx = inlink->dst;
118  CropDetectContext *cd = ctx->priv;
119  int bpp = cd->max_pixsteps[0];
120  int w, h, x, y, shrink_by;
121 
122  // ignore first 2 frames - they may be empty
123  if (++cd->frame_nb > 0) {
124  // Reset the crop area every reset_count frames, if reset_count is > 0
125  if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
126  cd->x1 = frame->width - 1;
127  cd->y1 = frame->height - 1;
128  cd->x2 = 0;
129  cd->y2 = 0;
130  cd->frame_nb = 1;
131  }
132 
133  for (y = 0; y < cd->y1; y++) {
134  if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
135  cd->y1 = y;
136  break;
137  }
138  }
139 
140  for (y = frame->height - 1; y > cd->y2; y--) {
141  if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
142  cd->y2 = y;
143  break;
144  }
145  }
146 
147  for (y = 0; y < cd->x1; y++) {
148  if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
149  cd->x1 = y;
150  break;
151  }
152  }
153 
154  for (y = frame->width - 1; y > cd->x2; y--) {
155  if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
156  cd->x2 = y;
157  break;
158  }
159  }
160 
161  // round x and y (up), important for yuv colorspaces
162  // make sure they stay rounded!
163  x = (cd->x1+1) & ~1;
164  y = (cd->y1+1) & ~1;
165 
166  w = cd->x2 - x + 1;
167  h = cd->y2 - y + 1;
168 
169  // w and h must be divisible by 2 as well because of yuv
170  // colorspace problems.
171  if (cd->round <= 1)
172  cd->round = 16;
173  if (cd->round % 2)
174  cd->round *= 2;
175 
176  shrink_by = w % cd->round;
177  w -= shrink_by;
178  x += (shrink_by/2 + 1) & ~1;
179 
180  shrink_by = h % cd->round;
181  h -= shrink_by;
182  y += (shrink_by/2 + 1) & ~1;
183 
184  av_log(ctx, AV_LOG_INFO,
185  "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",
186  cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
187  frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
188  w, h, x, y);
189  }
190 
191  return ff_filter_frame(inlink->dst->outputs[0], frame);
192 }
193 
194 #define OFFSET(x) offsetof(CropDetectContext, x)
195 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
196 
197 static const AVOption cropdetect_options[] = {
198  { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255, FLAGS },
199  { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
200  { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
201  { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
202  { NULL },
203 };
204 
205 AVFILTER_DEFINE_CLASS(cropdetect);
206 
208  {
209  .name = "default",
210  .type = AVMEDIA_TYPE_VIDEO,
211  .config_props = config_input,
212  .get_video_buffer = ff_null_get_video_buffer,
213  .filter_frame = filter_frame,
214  },
215  { NULL }
216 };
217 
219  {
220  .name = "default",
221  .type = AVMEDIA_TYPE_VIDEO
222  },
223  { NULL }
224 };
225 
227  .name = "cropdetect",
228  .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
229 
230  .priv_size = sizeof(CropDetectContext),
231  .priv_class = &cropdetect_class,
232  .init = init,
234  .inputs = avfilter_vf_cropdetect_inputs,
235  .outputs = avfilter_vf_cropdetect_outputs,
236 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
AVFilter avfilter_vf_cropdetect
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
y1
Definition: lab5.m:33
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
x1
Definition: genspecsines3.m:7
int stride
Definition: mace.c:144
output residual component w
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:32
const char * name
Pad name.
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
static av_cold int init(AVFilterContext *ctx)
Definition: vf_cropdetect.c:87
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:545
frame
Definition: stft.m:14
A filter pad used for either input or output.
Discrete Time axis x
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
int width
width and height of the video frame
Definition: frame.h:122
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:93
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static av_always_inline av_const double round(double x)
Definition: libm.h:162
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
common internal API header
#define AV_LOG_VERBOSE
Definition: log.h:157
as above, but U and V bytes are swapped
Definition: pixfmt.h:94
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
static const AVOption cropdetect_options[]
AVFILTER_DEFINE_CLASS(cropdetect)
static int query_formats(AVFilterContext *ctx)
Definition: vf_cropdetect.c:45
static const AVFilterPad avfilter_vf_cropdetect_inputs[]
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
#define FLAGS
Describe the class of an AVClass context structure.
Definition: log.h:50
x2
Definition: genspecsines3.m:8
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
function y
Definition: D.m:1
static const AVFilterPad avfilter_vf_cropdetect_outputs[]
#define OFFSET(x)
int len
static int config_input(AVFilterLink *inlink)
Definition: vf_cropdetect.c:99
An instance of a filter.
Definition: avfilter.h:524
int height
Definition: frame.h:122
#define AV_LOG_INFO
Definition: log.h:156
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
Definition: vf_cropdetect.c:60