38
44
static int query_formats(AVFilterContext *ctx)
40
static const enum PixelFormat pix_fmts[] = {
41
PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
42
PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
43
PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
44
PIX_FMT_YUV411P, PIX_FMT_GRAY8,
45
PIX_FMT_NV12, PIX_FMT_NV21,
46
static const enum AVPixelFormat pix_fmts[] = {
47
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
48
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
49
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
50
AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
51
AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
49
avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
55
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
80
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
86
static av_cold int init(AVFilterContext *ctx, const char *args)
82
88
CropDetectContext *cd = ctx->priv;
90
96
sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
92
av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n",
98
av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
93
99
cd->limit, cd->round, cd->reset_count);
101
107
CropDetectContext *cd = ctx->priv;
103
109
av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
104
&av_pix_fmt_descriptors[inlink->format]);
110
av_pix_fmt_desc_get(inlink->format));
106
112
cd->x1 = inlink->w - 1;
107
113
cd->y1 = inlink->h - 1;
114
static void end_frame(AVFilterLink *inlink)
120
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
116
122
AVFilterContext *ctx = inlink->dst;
117
123
CropDetectContext *cd = ctx->priv;
118
AVFilterBufferRef *picref = inlink->cur_buf;
119
124
int bpp = cd->max_pixsteps[0];
120
125
int w, h, x, y, shrink_by;
123
128
if (++cd->frame_nb > 0) {
124
129
// Reset the crop area every reset_count frames, if reset_count is > 0
125
130
if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
126
cd->x1 = picref->video->w-1;
127
cd->y1 = picref->video->h-1;
131
cd->x1 = frame->video->w-1;
132
cd->y1 = frame->video->h-1;
130
135
cd->frame_nb = 1;
133
138
for (y = 0; y < cd->y1; y++) {
134
if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
139
if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
140
for (y = picref->video->h-1; y > cd->y2; y--) {
141
if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
145
for (y = frame->video->h-1; y > cd->y2; y--) {
146
if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
147
152
for (y = 0; y < cd->x1; y++) {
148
if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
153
if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
154
for (y = picref->video->w-1; y > cd->x2; y--) {
155
if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
159
for (y = frame->video->w-1; y > cd->x2; y--) {
160
if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
184
189
av_log(ctx, AV_LOG_INFO,
185
190
"x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
186
cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
187
picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
191
cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pos, frame->pts,
192
frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
191
avfilter_end_frame(inlink->dst->outputs[0]);
196
return ff_filter_frame(inlink->dst->outputs[0], frame);
199
static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
202
.type = AVMEDIA_TYPE_VIDEO,
203
.config_props = config_input,
204
.get_video_buffer = ff_null_get_video_buffer,
205
.filter_frame = filter_frame,
210
static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
213
.type = AVMEDIA_TYPE_VIDEO
194
218
AVFilter avfilter_vf_cropdetect = {
195
219
.name = "cropdetect",
196
220
.description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
201
225
.query_formats = query_formats,
203
.inputs = (AVFilterPad[]) {{ .name = "default",
204
.type = AVMEDIA_TYPE_VIDEO,
205
.config_props = config_input,
206
.get_video_buffer = avfilter_null_get_video_buffer,
207
.start_frame = avfilter_null_start_frame,
208
.end_frame = end_frame, },
227
.inputs = avfilter_vf_cropdetect_inputs,
211
.outputs = (AVFilterPad[]) {{ .name = "default",
212
.type = AVMEDIA_TYPE_VIDEO },
229
.outputs = avfilter_vf_cropdetect_outputs,