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