00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/cpu.h"
00022 #include "libavutil/common.h"
00023 #include "libavutil/pixdesc.h"
00024 #include "avfilter.h"
00025 #include "internal.h"
00026
00027 #undef NDEBUG
00028 #include <assert.h>
00029
00030 #define HIST_SIZE 4
00031
00032 typedef enum {
00033 TFF,
00034 BFF,
00035 PROGRSSIVE,
00036 UNDETERMINED,
00037 } Type;
00038
00039 typedef struct {
00040 float interlace_threshold;
00041 float progressive_threshold;
00042
00043 Type last_type;
00044 Type prestat[4];
00045 Type poststat[4];
00046
00047 uint8_t history[HIST_SIZE];
00048
00049 AVFilterBufferRef *cur;
00050 AVFilterBufferRef *next;
00051 AVFilterBufferRef *prev;
00052 AVFilterBufferRef *out;
00053 int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
00054
00055 const AVPixFmtDescriptor *csp;
00056 } IDETContext;
00057
00058 static const char *type2str(Type type)
00059 {
00060 switch(type) {
00061 case TFF : return "Top Field First ";
00062 case BFF : return "Bottom Field First";
00063 case PROGRSSIVE : return "Progressive ";
00064 case UNDETERMINED: return "Undetermined ";
00065 }
00066 return NULL;
00067 }
00068
00069 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
00070 {
00071 int x;
00072 int ret=0;
00073
00074 for(x=0; x<w; x++){
00075 ret += FFABS((*a++ + *c++) - 2 * *b++);
00076 }
00077
00078 return ret;
00079 }
00080
00081 static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
00082 {
00083 int x;
00084 int ret=0;
00085
00086 for(x=0; x<w; x++){
00087 ret += FFABS((*a++ + *c++) - 2 * *b++);
00088 }
00089
00090 return ret;
00091 }
00092
00093 static void filter(AVFilterContext *ctx)
00094 {
00095 IDETContext *idet = ctx->priv;
00096 int y, i;
00097 int64_t alpha[2]={0};
00098 int64_t delta=0;
00099 Type type, best_type;
00100 int match = 0;
00101
00102 for (i = 0; i < idet->csp->nb_components; i++) {
00103 int w = idet->cur->video->w;
00104 int h = idet->cur->video->h;
00105 int refs = idet->cur->linesize[i];
00106
00107 if (i && i<3) {
00108 w >>= idet->csp->log2_chroma_w;
00109 h >>= idet->csp->log2_chroma_h;
00110 }
00111
00112 for (y = 2; y < h - 2; y++) {
00113 uint8_t *prev = &idet->prev->data[i][y*refs];
00114 uint8_t *cur = &idet->cur ->data[i][y*refs];
00115 uint8_t *next = &idet->next->data[i][y*refs];
00116 alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
00117 alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
00118 delta += idet->filter_line(cur-refs, cur, cur+refs, w);
00119 }
00120 }
00121
00122 if (alpha[0] / (float)alpha[1] > idet->interlace_threshold){
00123 type = TFF;
00124 }else if(alpha[1] / (float)alpha[0] > idet->interlace_threshold){
00125 type = BFF;
00126 }else if(alpha[1] / (float)delta > idet->progressive_threshold){
00127 type = PROGRSSIVE;
00128 }else{
00129 type = UNDETERMINED;
00130 }
00131
00132 memmove(idet->history+1, idet->history, HIST_SIZE-1);
00133 idet->history[0] = type;
00134 best_type = UNDETERMINED;
00135 for(i=0; i<HIST_SIZE; i++){
00136 if(idet->history[i] != UNDETERMINED){
00137 if(best_type == UNDETERMINED)
00138 best_type = idet->history[i];
00139
00140 if(idet->history[i] == best_type) {
00141 match++;
00142 }else{
00143 match=0;
00144 break;
00145 }
00146 }
00147 }
00148 if(idet->last_type == UNDETERMINED){
00149 if(match ) idet->last_type = best_type;
00150 }else{
00151 if(match>2) idet->last_type = best_type;
00152 }
00153
00154 if (idet->last_type == TFF){
00155 idet->cur->video->top_field_first = 1;
00156 idet->cur->video->interlaced = 1;
00157 }else if(idet->last_type == BFF){
00158 idet->cur->video->top_field_first = 0;
00159 idet->cur->video->interlaced = 1;
00160 }else if(idet->last_type == PROGRSSIVE){
00161 idet->cur->video->interlaced = 0;
00162 }
00163
00164 idet->prestat [ type] ++;
00165 idet->poststat[idet->last_type] ++;
00166 av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
00167 }
00168
00169 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00170 {
00171 AVFilterContext *ctx = link->dst;
00172 IDETContext *idet = ctx->priv;
00173
00174 if (idet->prev)
00175 avfilter_unref_buffer(idet->prev);
00176 idet->prev = idet->cur;
00177 idet->cur = idet->next;
00178 idet->next = picref;
00179
00180 if (!idet->cur)
00181 return 0;
00182
00183 if (!idet->prev)
00184 idet->prev = avfilter_ref_buffer(idet->cur, ~0);
00185
00186 return ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
00187 }
00188
00189 static int end_frame(AVFilterLink *link)
00190 {
00191 AVFilterContext *ctx = link->dst;
00192 IDETContext *idet = ctx->priv;
00193
00194 if (!idet->cur)
00195 return 0;
00196
00197 if (!idet->csp)
00198 idet->csp = &av_pix_fmt_descriptors[link->format];
00199 if (idet->csp->comp[0].depth_minus1 / 8 == 1)
00200 idet->filter_line = (void*)filter_line_c_16bit;
00201
00202 filter(ctx);
00203
00204 ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
00205 return ff_end_frame(ctx->outputs[0]);
00206 }
00207
00208 static int request_frame(AVFilterLink *link)
00209 {
00210 AVFilterContext *ctx = link->src;
00211 IDETContext *idet = ctx->priv;
00212
00213 do {
00214 int ret;
00215
00216 if ((ret = ff_request_frame(link->src->inputs[0])))
00217 return ret;
00218 } while (!idet->cur);
00219
00220 return 0;
00221 }
00222
00223 static int poll_frame(AVFilterLink *link)
00224 {
00225 IDETContext *idet = link->src->priv;
00226 int ret, val;
00227
00228 val = ff_poll_frame(link->src->inputs[0]);
00229
00230 if (val >= 1 && !idet->next) {
00231 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
00232 return ret;
00233 val = ff_poll_frame(link->src->inputs[0]);
00234 }
00235 assert(idet->next || !val);
00236
00237 return val;
00238 }
00239
00240 static av_cold void uninit(AVFilterContext *ctx)
00241 {
00242 IDETContext *idet = ctx->priv;
00243
00244 av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
00245 idet->prestat[TFF],
00246 idet->prestat[BFF],
00247 idet->prestat[PROGRSSIVE],
00248 idet->prestat[UNDETERMINED]
00249 );
00250 av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
00251 idet->poststat[TFF],
00252 idet->poststat[BFF],
00253 idet->poststat[PROGRSSIVE],
00254 idet->poststat[UNDETERMINED]
00255 );
00256
00257 if (idet->prev) avfilter_unref_buffer(idet->prev);
00258 if (idet->cur ) avfilter_unref_buffer(idet->cur );
00259 if (idet->next) avfilter_unref_buffer(idet->next);
00260 }
00261
00262 static int query_formats(AVFilterContext *ctx)
00263 {
00264 static const enum PixelFormat pix_fmts[] = {
00265 PIX_FMT_YUV420P,
00266 PIX_FMT_YUV422P,
00267 PIX_FMT_YUV444P,
00268 PIX_FMT_YUV410P,
00269 PIX_FMT_YUV411P,
00270 PIX_FMT_GRAY8,
00271 PIX_FMT_YUVJ420P,
00272 PIX_FMT_YUVJ422P,
00273 PIX_FMT_YUVJ444P,
00274 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
00275 PIX_FMT_YUV440P,
00276 PIX_FMT_YUVJ440P,
00277 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
00278 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
00279 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
00280 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
00281 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
00282 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
00283 PIX_FMT_YUVA420P,
00284 PIX_FMT_NONE
00285 };
00286
00287 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00288
00289 return 0;
00290 }
00291
00292 static av_cold int init(AVFilterContext *ctx, const char *args)
00293 {
00294 IDETContext *idet = ctx->priv;
00295
00296 idet->csp = NULL;
00297
00298 idet->interlace_threshold = 1.01;
00299 idet->progressive_threshold = 2.5;
00300
00301 if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
00302
00303 idet->last_type = UNDETERMINED;
00304 memset(idet->history, UNDETERMINED, HIST_SIZE);
00305
00306 idet->filter_line = filter_line_c;
00307
00308 return 0;
00309 }
00310
00311 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
00312
00313 AVFilter avfilter_vf_idet = {
00314 .name = "idet",
00315 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
00316
00317 .priv_size = sizeof(IDETContext),
00318 .init = init,
00319 .uninit = uninit,
00320 .query_formats = query_formats,
00321
00322 .inputs = (const AVFilterPad[]) {{ .name = "default",
00323 .type = AVMEDIA_TYPE_VIDEO,
00324 .start_frame = start_frame,
00325 .draw_slice = null_draw_slice,
00326 .end_frame = end_frame,
00327 .min_perms = AV_PERM_PRESERVE },
00328 { .name = NULL}},
00329
00330 .outputs = (const AVFilterPad[]) {{ .name = "default",
00331 .type = AVMEDIA_TYPE_VIDEO,
00332 .rej_perms = AV_PERM_WRITE,
00333 .poll_frame = poll_frame,
00334 .request_frame = request_frame, },
00335 { .name = NULL}},
00336 };