85static int comp(
const int *
a,
const int *
b)
94 const uint16_t *src16 = (
const uint16_t *)
src;
148 if (
src[0])
return 0;
159 s->frame_nb = -1 *
s->skip;
160 s->low_u8 =
s->low * 255. + .5;
161 s->high_u8 =
s->high * 255. + .5;
164 s->limit,
s->round,
s->skip,
s->reset_count);
188 const int bufsize = inlink->
w * inlink->
h;
192 s->bitdepth =
desc->comp[0].depth;
195 s->limit_upscaled =
s->limit * ((1 <<
s->bitdepth) - 1);
197 s->limit_upscaled =
s->limit;
199 s->x1 = inlink->
w - 1;
200 s->y1 = inlink->
h - 1;
204 s->window_size =
FFMAX(
s->reset_count, 15);
206 s->filterbuf =
av_malloc(bufsize *
s->max_pixsteps[0]);
207 s->gradients =
av_calloc(bufsize,
sizeof(*
s->gradients));
209 s->bboxes[0] =
av_malloc(
s->window_size *
sizeof(*
s->bboxes[0]));
210 s->bboxes[1] =
av_malloc(
s->window_size *
sizeof(*
s->bboxes[1]));
211 s->bboxes[2] =
av_malloc(
s->window_size *
sizeof(*
s->bboxes[2]));
212 s->bboxes[3] =
av_malloc(
s->window_size *
sizeof(*
s->bboxes[3]));
214 if (!
s->tmpbuf || !
s->filterbuf || !
s->gradients || !
s->directions ||
215 !
s->bboxes[0] || !
s->bboxes[1] || !
s->bboxes[2] || !
s->bboxes[3])
221#define SET_META(key, value) \
222 av_dict_set_int(metadata, key, value, 0)
228 int bpp =
s->max_pixsteps[0];
229 int w,
h, x, y, shrink_by,
i;
231 int outliers, last_y;
232 int limit_upscaled =
lrint(
s->limit_upscaled);
235 const int inw = inlink->
w;
236 const int inh = inlink->
h;
237 uint8_t *tmpbuf =
s->tmpbuf;
238 uint8_t *filterbuf =
s->filterbuf;
239 uint16_t *gradients =
s->gradients;
240 int8_t *directions =
s->directions;
242 int scan_w, scan_h, bboff;
244 void (*
sobel)(
int w,
int h, uint16_t *
dst,
int dst_linesize,
245 int8_t *dir,
int dir_linesize,
246 const uint8_t *
src,
int src_linesize,
int src_stride) = (bpp == 2) ? &ff_sobel_16 : &ff_sobel_8;
248 uint8_t *
dst,
int dst_linesize,
249 const uint8_t *
src,
int src_linesize,
int src_stride) = (bpp == 2) ? &ff_gaussian_blur_16 : &ff_gaussian_blur_8;
253 if (++
s->frame_nb > 0) {
257 if (
s->reset_count > 0 &&
s->frame_nb >
s->reset_count) {
259 s->y1 =
frame->height - 1;
265#define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
267 for (last_y = y = FROM; NOEND; y = y INC) {\
268 if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit_upscaled) {\
269 if (++outliers > s->max_outliers) { \
298 sobel(inw, inh, gradients, inw, directions, inw, filterbuf, inw*bpp, bpp);
302 memset(tmpbuf, 0, inw * inh);
308 tmpbuf, inw, tmpbuf, inw);
317 const int mx =
mv->dst_x -
mv->src_x;
318 const int my =
mv->dst_y -
mv->src_y;
320 if (
mv->dst_x >= 0 &&
mv->dst_x < inw &&
321 mv->dst_y >= 0 &&
mv->dst_y < inh &&
322 mv->src_x >= 0 &&
mv->src_x < inw &&
323 mv->src_y >= 0 &&
mv->src_y < inh &&
324 mx *
mx +
my *
my >=
s->mv_threshold *
s->mv_threshold) {
325 s->x1 =
mv->dst_x <
s->x1 ?
mv->dst_x :
s->x1;
326 s->y1 =
mv->dst_y <
s->y1 ?
mv->dst_y :
s->y1;
327 s->x2 =
mv->dst_x >
s->x2 ?
mv->dst_x :
s->x2;
328 s->y2 =
mv->dst_y >
s->y2 ?
mv->dst_y :
s->y2;
337 scan_w =
s->x2 -
s->x1;
338 scan_h =
s->y2 -
s->y1;
340#define FIND_EDGE(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
341 for (last_y = y = FROM; NOEND; y = y INC) { \
342 if (checkline_edge(ctx, tmpbuf + STEP0 * y, STEP1, LEN)) { \
343 if (last_y INC == y) { \
354 FIND_EDGE(
s->y1,
s->y1, y >= 0, -1, inw, 1, scan_w);
355 FIND_EDGE(
s->y2,
s->y2, y < inh, +1, inw, 1, scan_w);
356 FIND_EDGE(
s->x1,
s->x1, y >= 0, -1, 1, inw, scan_h);
357 FIND_EDGE(
s->x2,
s->x2, y < inw, +1, 1, inw, scan_h);
360 bboff = (
s->frame_nb - 1) %
s->window_size;
361 s->bboxes[0][bboff] =
s->x1;
362 s->bboxes[1][bboff] =
s->x2;
363 s->bboxes[2][bboff] =
s->y1;
364 s->bboxes[3][bboff] =
s->y2;
367 bboff =
FFMIN(
s->frame_nb,
s->window_size);
374 s->x1 =
s->bboxes[0][bboff/2];
375 s->x2 =
s->bboxes[1][bboff/2];
376 s->y1 =
s->bboxes[2][bboff/2];
377 s->y2 =
s->bboxes[3][bboff/2];
396 shrink_by =
w %
s->round;
398 x += (shrink_by/2 + 1) & ~1;
400 shrink_by =
h %
s->round;
402 y += (shrink_by/2 + 1) & ~1;
413 snprintf(limit_str,
sizeof(limit_str),
"%f",
s->limit);
417 "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64
" t:%f limit:%f crop=%d:%d:%d:%d\n",
418 s->x1,
s->x2,
s->y1,
s->y2,
w,
h, x, y,
frame->pts,
420 s->limit,
w,
h, x, y);
427 char *res,
int res_len,
int flags)
430 float old_limit =
s->limit;
436 if (old_limit !=
s->limit) {
438 s->limit_upscaled =
s->limit * ((1 <<
s->bitdepth) - 1);
440 s->limit_upscaled =
s->limit;
441 s->frame_nb =
s->reset_count;
447#define OFFSET(x) offsetof(CropDetectContext, x)
448#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
449#define TFLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
453 {
"round",
"Value by which the width/height should be divisible",
OFFSET(round),
AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX,
FLAGS },
454 {
"reset",
"Recalculate the crop area after this many frames",
OFFSET(reset_count),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
FLAGS },
456 {
"reset_count",
"Recalculate the crop area after this many frames",
OFFSET(reset_count),
AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX,
FLAGS },
463 {
"mv_threshold",
"motion vector threshold when estimating video window size",
OFFSET(mv_threshold),
AV_OPT_TYPE_INT, {.i64=8}, 0, 100,
FLAGS},
479 .p.name =
"cropdetect",
481 .p.priv_class = &cropdetect_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_vf_cropdetect
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Main libavfilter public API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int(* init)(AVBSFContext *ctx)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
void ff_non_maximum_suppression(int w, int h, uint8_t *dst, int dst_linesize, const int8_t *dir, int dir_linesize, const uint16_t *src, int src_linesize)
Filters rounded gradients to drop all non-maxima pixels in the magnitude image Expects gradients gene...
void ff_double_threshold(int low, int high, int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Filters all pixels in src to keep all pixels > high, and keep all pixels > low where all surrounding ...
common functions for edge detection
void fn sobel(int w, int h, uint16_t *dst, int dst_linesize, int8_t *dir, int dir_linesize, const uint8_t *src, int src_linesize, int src_stride)
void fn gaussian_blur(int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int src_stride)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
@ AV_FRAME_DATA_MOTION_VECTORS
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
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.
#define AV_NOPTS_VALUE
Undefined timestamp value.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static const int8_t mv[256][2]
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FILTER_PIXFMTS_ARRAY(array)
#define AVFILTER_DEFINE_CLASS(fname)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
#define FFSWAP(type, a, b)
#define FFDIFFSIGN(x, y)
Comparator.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P14
AVPixelFormat
Pixel format.
@ 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...
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P10
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Describe the class of an AVClass context structure.
AVFilterLink ** outputs
array of pointers to output links
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
Structure to hold side data for an AVFrame.
This structure describes decoded (raw) audio or video data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
#define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN)
static const AVOption cropdetect_options[]
#define FIND_EDGE(DST, FROM, NOEND, INC, STEP0, STEP1, LEN)
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static int comp(const int *a, const int *b)
static const AVFilterPad avfilter_vf_cropdetect_inputs[]
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static av_cold void uninit(AVFilterContext *ctx)
#define SET_META(key, value)
static int checkline_edge(void *ctx, const unsigned char *src, int stride, int len)
static double limit(double x)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.