FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 CropDetectContext {
36  const AVClass *class;
37  int x1, y1, x2, y2;
38  float limit;
39  int round;
41  int frame_nb;
42  int max_pixsteps[4];
45 
47 {
48  static const enum AVPixelFormat pix_fmts[] = {
63  };
64 
66  return 0;
67 }
68 
69 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
70 {
71  int total = 0;
72  int div = len;
73  const uint16_t *src16 = (const uint16_t *)src;
74 
75  switch (bpp) {
76  case 1:
77  while (len >= 8) {
78  total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
79  + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
80  src += 8*stride;
81  len -= 8;
82  }
83  while (--len >= 0) {
84  total += src[0];
85  src += stride;
86  }
87  break;
88  case 2:
89  stride >>= 1;
90  while (len >= 8) {
91  total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
92  + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
93  src += 8*stride;
94  len -= 8;
95  }
96  while (--len >= 0) {
97  total += src16[0];
98  src += stride;
99  }
100  break;
101  case 3:
102  case 4:
103  while (len >= 4) {
104  total += src[0] + src[1 ] + src[2 ]
105  + src[ stride] + src[1+ stride] + src[2+ stride]
106  + src[2*stride] + src[1+2*stride] + src[2+2*stride]
107  + src[3*stride] + src[1+3*stride] + src[2+3*stride];
108  src += 4*stride;
109  len -= 4;
110  }
111  while (--len >= 0) {
112  total += src[0] + src[1] + src[2];
113  src += stride;
114  }
115  div *= 3;
116  break;
117  }
118  total /= div;
119 
120  av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
121  return total;
122 }
123 
124 static av_cold int init(AVFilterContext *ctx)
125 {
126  CropDetectContext *s = ctx->priv;
127 
128  s->frame_nb = -2;
129 
130  av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d reset_count:%d\n",
131  s->limit, s->round, s->reset_count);
132 
133  return 0;
134 }
135 
136 static int config_input(AVFilterLink *inlink)
137 {
138  AVFilterContext *ctx = inlink->dst;
139  CropDetectContext *s = ctx->priv;
140  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
141 
143 
144  if (s->limit < 1.0)
145  s->limit *= (1 << (desc->comp[0].depth_minus1 + 1)) - 1;
146 
147  s->x1 = inlink->w - 1;
148  s->y1 = inlink->h - 1;
149  s->x2 = 0;
150  s->y2 = 0;
151 
152  return 0;
153 }
154 
155 #define SET_META(key, value) \
156  av_dict_set_int(metadata, key, value, 0)
157 
158 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
159 {
160  AVFilterContext *ctx = inlink->dst;
161  CropDetectContext *s = ctx->priv;
162  int bpp = s->max_pixsteps[0];
163  int w, h, x, y, shrink_by;
164  AVDictionary **metadata;
165  int outliers, last_y;
166  int limit = round(s->limit);
167 
168  // ignore first 2 frames - they may be empty
169  if (++s->frame_nb > 0) {
170  metadata = avpriv_frame_get_metadatap(frame);
171 
172  // Reset the crop area every reset_count frames, if reset_count is > 0
173  if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
174  s->x1 = frame->width - 1;
175  s->y1 = frame->height - 1;
176  s->x2 = 0;
177  s->y2 = 0;
178  s->frame_nb = 1;
179  }
180 
181 #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
182  outliers = 0;\
183  for (last_y = y = FROM; NOEND; y = y INC) {\
184  if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit) {\
185  if (++outliers > s->max_outliers) { \
186  DST = last_y;\
187  break;\
188  }\
189  } else\
190  last_y = y INC;\
191  }
192 
193  FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
194  FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
195  FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
196  FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
197 
198 
199  // round x and y (up), important for yuv colorspaces
200  // make sure they stay rounded!
201  x = (s->x1+1) & ~1;
202  y = (s->y1+1) & ~1;
203 
204  w = s->x2 - x + 1;
205  h = s->y2 - y + 1;
206 
207  // w and h must be divisible by 2 as well because of yuv
208  // colorspace problems.
209  if (s->round <= 1)
210  s->round = 16;
211  if (s->round % 2)
212  s->round *= 2;
213 
214  shrink_by = w % s->round;
215  w -= shrink_by;
216  x += (shrink_by/2 + 1) & ~1;
217 
218  shrink_by = h % s->round;
219  h -= shrink_by;
220  y += (shrink_by/2 + 1) & ~1;
221 
222  SET_META("lavfi.cropdetect.x1", s->x1);
223  SET_META("lavfi.cropdetect.x2", s->x2);
224  SET_META("lavfi.cropdetect.y1", s->y1);
225  SET_META("lavfi.cropdetect.y2", s->y2);
226  SET_META("lavfi.cropdetect.w", w);
227  SET_META("lavfi.cropdetect.h", h);
228  SET_META("lavfi.cropdetect.x", x);
229  SET_META("lavfi.cropdetect.y", y);
230 
231  av_log(ctx, AV_LOG_INFO,
232  "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",
233  s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
234  frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
235  w, h, x, y);
236  }
237 
238  return ff_filter_frame(inlink->dst->outputs[0], frame);
239 }
240 
241 #define OFFSET(x) offsetof(CropDetectContext, x)
242 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
243 
244 static const AVOption cropdetect_options[] = {
245  { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, FLAGS },
246  { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
247  { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
248  { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
249  { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
250  { NULL }
251 };
252 
253 AVFILTER_DEFINE_CLASS(cropdetect);
254 
256  {
257  .name = "default",
258  .type = AVMEDIA_TYPE_VIDEO,
259  .config_props = config_input,
260  .filter_frame = filter_frame,
261  },
262  { NULL }
263 };
264 
266  {
267  .name = "default",
268  .type = AVMEDIA_TYPE_VIDEO
269  },
270  { NULL }
271 };
272 
274  .name = "cropdetect",
275  .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
276  .priv_size = sizeof(CropDetectContext),
277  .priv_class = &cropdetect_class,
278  .init = init,
280  .inputs = avfilter_vf_cropdetect_inputs,
281  .outputs = avfilter_vf_cropdetect_outputs,
283 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:669
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2029
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
AVOption.
Definition: opt.h:255
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:366
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:246
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
#define SET_META(key, value)
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:361
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
AVFilter ff_vf_cropdetect
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:34
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:451
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:100
#define av_cold
Definition: attributes.h:74
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:249
static av_cold int init(AVFilterContext *ctx)
static AVFrame * frame
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:191
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:369
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:539
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:362
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:61
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
uint16_t depth_minus1
Number of bits in the component minus 1.
Definition: pixdesc.h:57
int width
width and height of the video frame
Definition: frame.h:212
#define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
static av_always_inline av_const double round(double x)
Definition: libm.h:162
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:360
#define FFMAX(a, b)
Definition: common.h:79
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
common internal API header
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:356
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
float y
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[]
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:357
AVFILTER_DEFINE_CLASS(cropdetect)
static int query_formats(AVFilterContext *ctx)
Definition: vf_cropdetect.c:46
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:367
static const AVFilterPad avfilter_vf_cropdetect_inputs[]
#define AV_LOG_INFO
Standard information.
Definition: log.h:186
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:364
AVS_Value src
Definition: avisynth_c.h:524
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
#define FLAGS
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:358
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
Describe the class of an AVClass context structure.
Definition: log.h:66
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:237
const char * name
Filter name.
Definition: avfilter.h:474
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:355
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:365
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:359
static int flags
Definition: cpu.c:47
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:363
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
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
static const AVFilterPad avfilter_vf_cropdetect_outputs[]
#define OFFSET(x)
int len
static int config_input(AVFilterLink *inlink)
An instance of a filter.
Definition: avfilter.h:633
int height
Definition: frame.h:212
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:106
#define stride
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:368
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
Definition: vf_cropdetect.c:69