FFmpeg
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;
40  int skip;
42  int frame_nb;
43  int max_pixsteps[4];
46 
47 static const enum AVPixelFormat pix_fmts[] = {
62 };
63 
64 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
65 {
66  int total = 0;
67  int div = len;
68  const uint16_t *src16 = (const uint16_t *)src;
69 
70  switch (bpp) {
71  case 1:
72  while (len >= 8) {
73  total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
74  + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
75  src += 8*stride;
76  len -= 8;
77  }
78  while (--len >= 0) {
79  total += src[0];
80  src += stride;
81  }
82  break;
83  case 2:
84  stride >>= 1;
85  while (len >= 8) {
86  total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
87  + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
88  src16 += 8*stride;
89  len -= 8;
90  }
91  while (--len >= 0) {
92  total += src16[0];
93  src16 += stride;
94  }
95  break;
96  case 3:
97  case 4:
98  while (len >= 4) {
99  total += src[0] + src[1 ] + src[2 ]
100  + src[ stride] + src[1+ stride] + src[2+ stride]
101  + src[2*stride] + src[1+2*stride] + src[2+2*stride]
102  + src[3*stride] + src[1+3*stride] + src[2+3*stride];
103  src += 4*stride;
104  len -= 4;
105  }
106  while (--len >= 0) {
107  total += src[0] + src[1] + src[2];
108  src += stride;
109  }
110  div *= 3;
111  break;
112  }
113  total /= div;
114 
115  av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
116  return total;
117 }
118 
120 {
121  CropDetectContext *s = ctx->priv;
122 
123  s->frame_nb = -1 * s->skip;
124 
125  av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d skip:%d reset_count:%d\n",
126  s->limit, s->round, s->skip, s->reset_count);
127 
128  return 0;
129 }
130 
132 {
133  AVFilterContext *ctx = inlink->dst;
134  CropDetectContext *s = ctx->priv;
136 
137  av_image_fill_max_pixsteps(s->max_pixsteps, NULL, desc);
138 
139  if (s->limit < 1.0)
140  s->limit *= (1 << desc->comp[0].depth) - 1;
141 
142  s->x1 = inlink->w - 1;
143  s->y1 = inlink->h - 1;
144  s->x2 = 0;
145  s->y2 = 0;
146 
147  return 0;
148 }
149 
150 #define SET_META(key, value) \
151  av_dict_set_int(metadata, key, value, 0)
152 
154 {
155  AVFilterContext *ctx = inlink->dst;
156  CropDetectContext *s = ctx->priv;
157  int bpp = s->max_pixsteps[0];
158  int w, h, x, y, shrink_by;
159  AVDictionary **metadata;
160  int outliers, last_y;
161  int limit = lrint(s->limit);
162 
163  // ignore first s->skip frames
164  if (++s->frame_nb > 0) {
165  metadata = &frame->metadata;
166 
167  // Reset the crop area every reset_count frames, if reset_count is > 0
168  if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
169  s->x1 = frame->width - 1;
170  s->y1 = frame->height - 1;
171  s->x2 = 0;
172  s->y2 = 0;
173  s->frame_nb = 1;
174  }
175 
176 #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
177  outliers = 0;\
178  for (last_y = y = FROM; NOEND; y = y INC) {\
179  if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit) {\
180  if (++outliers > s->max_outliers) { \
181  DST = last_y;\
182  break;\
183  }\
184  } else\
185  last_y = y INC;\
186  }
187 
188  FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
189  FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
190  FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
191  FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
192 
193 
194  // round x and y (up), important for yuv colorspaces
195  // make sure they stay rounded!
196  x = (s->x1+1) & ~1;
197  y = (s->y1+1) & ~1;
198 
199  w = s->x2 - x + 1;
200  h = s->y2 - y + 1;
201 
202  // w and h must be divisible by 2 as well because of yuv
203  // colorspace problems.
204  if (s->round <= 1)
205  s->round = 16;
206  if (s->round % 2)
207  s->round *= 2;
208 
209  shrink_by = w % s->round;
210  w -= shrink_by;
211  x += (shrink_by/2 + 1) & ~1;
212 
213  shrink_by = h % s->round;
214  h -= shrink_by;
215  y += (shrink_by/2 + 1) & ~1;
216 
217  SET_META("lavfi.cropdetect.x1", s->x1);
218  SET_META("lavfi.cropdetect.x2", s->x2);
219  SET_META("lavfi.cropdetect.y1", s->y1);
220  SET_META("lavfi.cropdetect.y2", s->y2);
221  SET_META("lavfi.cropdetect.w", w);
222  SET_META("lavfi.cropdetect.h", h);
223  SET_META("lavfi.cropdetect.x", x);
224  SET_META("lavfi.cropdetect.y", y);
225 
227  "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",
228  s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
229  frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
230  w, h, x, y);
231  }
232 
233  return ff_filter_frame(inlink->dst->outputs[0], frame);
234 }
235 
236 #define OFFSET(x) offsetof(CropDetectContext, x)
237 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
238 
239 static const AVOption cropdetect_options[] = {
240  { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, FLAGS },
241  { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
242  { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
243  { "skip", "Number of initial frames to skip", OFFSET(skip), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, FLAGS },
244  { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
245  { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
246  { NULL }
247 };
248 
249 AVFILTER_DEFINE_CLASS(cropdetect);
250 
252  {
253  .name = "default",
254  .type = AVMEDIA_TYPE_VIDEO,
255  .config_props = config_input,
256  .filter_frame = filter_frame,
257  },
258 };
259 
261  {
262  .name = "default",
263  .type = AVMEDIA_TYPE_VIDEO
264  },
265 };
266 
268  .name = "cropdetect",
269  .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
270  .priv_size = sizeof(CropDetectContext),
271  .priv_class = &cropdetect_class,
272  .init = init,
277 };
stride
int stride
Definition: mace.c:144
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_cropdetect.c:119
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:171
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
SET_META
#define SET_META(key, value)
Definition: vf_cropdetect.c:150
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:247
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:404
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AVDictionary
Definition: dict.c:30
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
video.h
checkline
static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
Definition: vf_cropdetect.c:64
formats.h
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:402
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:407
lrint
#define lrint
Definition: tablegen.h:53
av_cold
#define av_cold
Definition: attributes.h:90
CropDetectContext::skip
int skip
Definition: vf_cropdetect.c:40
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:416
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
CropDetectContext
Definition: vf_cropdetect.c:35
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:417
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:401
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:415
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(cropdetect)
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
CropDetectContext::y1
int y1
Definition: vf_cropdetect.c:37
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
CropDetectContext::max_pixsteps
int max_pixsteps[4]
Definition: vf_cropdetect.c:43
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_cropdetect.c:153
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
FLAGS
#define FLAGS
Definition: vf_cropdetect.c:237
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
src
#define src
Definition: vp8dsp.c:255
CropDetectContext::y2
int y2
Definition: vf_cropdetect.c:37
cropdetect_options
static const AVOption cropdetect_options[]
Definition: vf_cropdetect.c:239
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:405
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
CropDetectContext::x1
int x1
Definition: vf_cropdetect.c:37
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_cropdetect.c:47
FIND
#define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN)
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:409
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:411
OFFSET
#define OFFSET(x)
Definition: vf_cropdetect.c:236
CropDetectContext::max_outliers
int max_outliers
Definition: vf_cropdetect.c:44
CropDetectContext::reset_count
int reset_count
Definition: vf_cropdetect.c:41
CropDetectContext::limit
float limit
Definition: vf_cropdetect.c:38
CropDetectContext::round
int round
Definition: vf_cropdetect.c:39
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_cropdetect.c:131
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
CropDetectContext::frame_nb
int frame_nb
Definition: vf_cropdetect.c:42
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:146
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
internal.h
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:90
len
int len
Definition: vorbis_enc_data.h:426
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:403
limit
static double limit(double x)
Definition: vf_pseudocolor.c:128
AVFilter
Filter definition.
Definition: avfilter.h:165
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
frame
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:408
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:413
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:137
CropDetectContext::x2
int x2
Definition: vf_cropdetect.c:37
av_image_fill_max_pixsteps
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:35
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
avfilter_vf_cropdetect_inputs
static const AVFilterPad avfilter_vf_cropdetect_inputs[]
Definition: vf_cropdetect.c:251
avfilter_vf_cropdetect_outputs
static const AVFilterPad avfilter_vf_cropdetect_outputs[]
Definition: vf_cropdetect.c:260
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
imgutils.h
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:414
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:412
ff_vf_cropdetect
const AVFilter ff_vf_cropdetect
Definition: vf_cropdetect.c:267