Libav
vf_cropdetect.c
Go to the documentation of this file.
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 
26 #include <stdio.h>
27 
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
31 
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 typedef struct CropDetectContext {
38  const AVClass *class;
39  int x1, y1, x2, y2;
40  int limit;
41  int round;
43  int frame_nb;
44  int max_pixsteps[4];
46 
48 {
49  static const enum AVPixelFormat pix_fmts[] = {
56  };
57 
59  return 0;
60 }
61 
62 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
63 {
64  int total = 0;
65  int div = len;
66 
67  switch (bpp) {
68  case 1:
69  while (--len >= 0) {
70  total += src[0];
71  src += stride;
72  }
73  break;
74  case 3:
75  case 4:
76  while (--len >= 0) {
77  total += src[0] + src[1] + src[2];
78  src += stride;
79  }
80  div *= 3;
81  break;
82  }
83  total /= div;
84 
85  av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
86  return total;
87 }
88 
89 static av_cold int init(AVFilterContext *ctx)
90 {
91  CropDetectContext *s = ctx->priv;
92 
93  s->frame_nb = -2;
94 
95  av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
96  s->limit, s->round, s->reset_count);
97 
98  return 0;
99 }
100 
101 static int config_input(AVFilterLink *inlink)
102 {
103  AVFilterContext *ctx = inlink->dst;
104  CropDetectContext *s = ctx->priv;
105 
107  av_pix_fmt_desc_get(inlink->format));
108 
109  s->x1 = inlink->w - 1;
110  s->y1 = inlink->h - 1;
111  s->x2 = 0;
112  s->y2 = 0;
113 
114  return 0;
115 }
116 
117 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
118 {
119  AVFilterContext *ctx = inlink->dst;
120  CropDetectContext *s = ctx->priv;
121  int bpp = s->max_pixsteps[0];
122  int w, h, x, y, shrink_by;
123 
124  // ignore first 2 frames - they may be empty
125  if (++s->frame_nb > 0) {
126  // Reset the crop area every reset_count frames, if reset_count is > 0
127  if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
128  s->x1 = frame->width - 1;
129  s->y1 = frame->height - 1;
130  s->x2 = 0;
131  s->y2 = 0;
132  s->frame_nb = 1;
133  }
134 
135  for (y = 0; y < s->y1; y++) {
136  if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
137  s->y1 = y;
138  break;
139  }
140  }
141 
142  for (y = frame->height - 1; y > s->y2; y--) {
143  if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
144  s->y2 = y;
145  break;
146  }
147  }
148 
149  for (y = 0; y < s->x1; y++) {
150  if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
151  s->x1 = y;
152  break;
153  }
154  }
155 
156  for (y = frame->width - 1; y > s->x2; y--) {
157  if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
158  s->x2 = y;
159  break;
160  }
161  }
162 
163  // round x and y (up), important for yuv colorspaces
164  // make sure they stay rounded!
165  x = (s->x1+1) & ~1;
166  y = (s->y1+1) & ~1;
167 
168  w = s->x2 - x + 1;
169  h = s->y2 - y + 1;
170 
171  // w and h must be divisible by 2 as well because of yuv
172  // colorspace problems.
173  if (s->round <= 1)
174  s->round = 16;
175  if (s->round % 2)
176  s->round *= 2;
177 
178  shrink_by = w % s->round;
179  w -= shrink_by;
180  x += (shrink_by/2 + 1) & ~1;
181 
182  shrink_by = h % s->round;
183  h -= shrink_by;
184  y += (shrink_by/2 + 1) & ~1;
185 
186  av_log(ctx, AV_LOG_INFO,
187  "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",
188  s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
189  frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
190  w, h, x, y);
191  }
192 
193  return ff_filter_frame(inlink->dst->outputs[0], frame);
194 }
195 
196 #define OFFSET(x) offsetof(CropDetectContext, x)
197 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
198 static const AVOption options[] = {
199  { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, INT_MAX, FLAGS },
200  { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
201  { "reset", "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 static const AVClass cropdetect_class = {
206  .class_name = "cropdetect",
207  .item_name = av_default_item_name,
208  .option = options,
209  .version = LIBAVUTIL_VERSION_INT,
210 };
211 
213  {
214  .name = "default",
215  .type = AVMEDIA_TYPE_VIDEO,
216  .config_props = config_input,
217  .get_video_buffer = ff_null_get_video_buffer,
218  .filter_frame = filter_frame,
219  },
220  { NULL }
221 };
222 
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_VIDEO
227  },
228  { NULL }
229 };
230 
232  .name = "cropdetect",
233  .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
234 
235  .priv_size = sizeof(CropDetectContext),
236  .priv_class = &cropdetect_class,
237  .init = init,
238 
240 
241  .inputs = avfilter_vf_cropdetect_inputs,
242 
243  .outputs = avfilter_vf_cropdetect_outputs,
244 };
static const AVClass cropdetect_class
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
int stride
Definition: mace.c:144
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
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:32
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:728
#define av_cold
Definition: attributes.h:66
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
static av_cold int init(AVFilterContext *ctx)
Definition: vf_cropdetect.c:89
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:78
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
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:379
A filter pad used for either input or output.
Definition: internal.h:36
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static const AVOption options[]
int width
width and height of the video frame
Definition: frame.h:174
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void * priv
private data for use by the filter
Definition: avfilter.h:584
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:92
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
static av_always_inline av_const double round(double x)
Definition: libm.h:151
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
common internal API header
as above, but U and V bytes are swapped
Definition: pixfmt.h:93
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int query_formats(AVFilterContext *ctx)
Definition: vf_cropdetect.c:47
static const AVFilterPad avfilter_vf_cropdetect_inputs[]
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
#define FLAGS
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
const char * name
Filter name.
Definition: avfilter.h:425
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
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:563
int height
Definition: frame.h:174
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
Definition: vf_cropdetect.c:62