00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030 #include <stdio.h>
00031 #include <inttypes.h>
00032
00033 #include "libavutil/internal.h"
00034 #include "avfilter.h"
00035 #include "internal.h"
00036 #include "formats.h"
00037 #include "internal.h"
00038 #include "video.h"
00039
00040 typedef struct {
00041 unsigned int bamount;
00042 unsigned int bthresh;
00043 unsigned int frame;
00044 unsigned int nblack;
00045 unsigned int last_keyframe;
00046 } BlackFrameContext;
00047
00048 static int query_formats(AVFilterContext *ctx)
00049 {
00050 static const enum PixelFormat pix_fmts[] = {
00051 PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12,
00052 PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
00053 PIX_FMT_NONE
00054 };
00055
00056 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00057 return 0;
00058 }
00059
00060 static av_cold int init(AVFilterContext *ctx, const char *args)
00061 {
00062 BlackFrameContext *blackframe = ctx->priv;
00063
00064 blackframe->bamount = 98;
00065 blackframe->bthresh = 32;
00066 blackframe->nblack = 0;
00067 blackframe->frame = 0;
00068 blackframe->last_keyframe = 0;
00069
00070 if (args)
00071 sscanf(args, "%u:%u", &blackframe->bamount, &blackframe->bthresh);
00072
00073 av_log(ctx, AV_LOG_VERBOSE, "bamount:%u bthresh:%u\n",
00074 blackframe->bamount, blackframe->bthresh);
00075
00076 if (blackframe->bamount > 100 || blackframe->bthresh > 255) {
00077 av_log(ctx, AV_LOG_ERROR, "Too big value for bamount (max is 100) or bthresh (max is 255)\n");
00078 return AVERROR(EINVAL);
00079 }
00080
00081 return 0;
00082 }
00083
00084 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00085 {
00086 AVFilterContext *ctx = inlink->dst;
00087 BlackFrameContext *blackframe = ctx->priv;
00088 AVFilterBufferRef *picref = inlink->cur_buf;
00089 int x, i;
00090 uint8_t *p = picref->data[0] + y * picref->linesize[0];
00091
00092 for (i = 0; i < h; i++) {
00093 for (x = 0; x < inlink->w; x++)
00094 blackframe->nblack += p[x] < blackframe->bthresh;
00095 p += picref->linesize[0];
00096 }
00097
00098 return ff_draw_slice(ctx->outputs[0], y, h, slice_dir);
00099 }
00100
00101 static int end_frame(AVFilterLink *inlink)
00102 {
00103 AVFilterContext *ctx = inlink->dst;
00104 BlackFrameContext *blackframe = ctx->priv;
00105 AVFilterBufferRef *picref = inlink->cur_buf;
00106 int pblack = 0;
00107
00108 if (picref->video->key_frame)
00109 blackframe->last_keyframe = blackframe->frame;
00110
00111 pblack = blackframe->nblack * 100 / (inlink->w * inlink->h);
00112 if (pblack >= blackframe->bamount)
00113 av_log(ctx, AV_LOG_INFO, "frame:%u pblack:%u pos:%"PRId64" pts:%"PRId64" t:%f "
00114 "type:%c last_keyframe:%d\n",
00115 blackframe->frame, pblack, picref->pos, picref->pts,
00116 picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
00117 av_get_picture_type_char(picref->video->pict_type), blackframe->last_keyframe);
00118
00119 blackframe->frame++;
00120 blackframe->nblack = 0;
00121 return ff_end_frame(inlink->dst->outputs[0]);
00122 }
00123
00124 AVFilter avfilter_vf_blackframe = {
00125 .name = "blackframe",
00126 .description = NULL_IF_CONFIG_SMALL("Detect frames that are (almost) black."),
00127
00128 .priv_size = sizeof(BlackFrameContext),
00129 .init = init,
00130
00131 .query_formats = query_formats,
00132
00133 .inputs = (const AVFilterPad[]) {{ .name = "default",
00134 .type = AVMEDIA_TYPE_VIDEO,
00135 .draw_slice = draw_slice,
00136 .get_video_buffer = ff_null_get_video_buffer,
00137 .start_frame = ff_null_start_frame,
00138 .end_frame = end_frame, },
00139 { .name = NULL}},
00140
00141 .outputs = (const AVFilterPad[]) {{ .name = "default",
00142 .type = AVMEDIA_TYPE_VIDEO },
00143 { .name = NULL}},
00144 };