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 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
00121 if (alpha[0] > idet->interlace_threshold * alpha[1]){
00122 type = TFF;
00123 }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
00124 type = BFF;
00125 }else if(alpha[1] > idet->progressive_threshold * delta){
00126 type = PROGRSSIVE;
00127 }else{
00128 type = UNDETERMINED;
00129 }
00130
00131 memmove(idet->history+1, idet->history, HIST_SIZE-1);
00132 idet->history[0] = type;
00133 best_type = UNDETERMINED;
00134 for(i=0; i<HIST_SIZE; i++){
00135 if(idet->history[i] != UNDETERMINED){
00136 if(best_type == UNDETERMINED)
00137 best_type = idet->history[i];
00138
00139 if(idet->history[i] == best_type) {
00140 match++;
00141 }else{
00142 match=0;
00143 break;
00144 }
00145 }
00146 }
00147 if(idet->last_type == UNDETERMINED){
00148 if(match ) idet->last_type = best_type;
00149 }else{
00150 if(match>2) idet->last_type = best_type;
00151 }
00152
00153 if (idet->last_type == TFF){
00154 idet->cur->video->top_field_first = 1;
00155 idet->cur->video->interlaced = 1;
00156 }else if(idet->last_type == BFF){
00157 idet->cur->video->top_field_first = 0;
00158 idet->cur->video->interlaced = 1;
00159 }else if(idet->last_type == PROGRSSIVE){
00160 idet->cur->video->interlaced = 0;
00161 }
00162
00163 idet->prestat [ type] ++;
00164 idet->poststat[idet->last_type] ++;
00165 av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
00166 }
00167
00168 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00169 {
00170 AVFilterContext *ctx = link->dst;
00171 IDETContext *idet = ctx->priv;
00172
00173 if (idet->prev)
00174 avfilter_unref_buffer(idet->prev);
00175 idet->prev = idet->cur;
00176 idet->cur = idet->next;
00177 idet->next = picref;
00178
00179 if (!idet->cur)
00180 return 0;
00181
00182 if (!idet->prev)
00183 idet->prev = avfilter_ref_buffer(idet->cur, ~0);
00184
00185 if (!idet->csp)
00186 idet->csp = av_pix_fmt_desc_get(link->format);
00187 if (idet->csp->comp[0].depth_minus1 / 8 == 1)
00188 idet->filter_line = (void*)filter_line_c_16bit;
00189
00190 filter(ctx);
00191
00192 return ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
00193 }
00194
00195 static int request_frame(AVFilterLink *link)
00196 {
00197 AVFilterContext *ctx = link->src;
00198 IDETContext *idet = ctx->priv;
00199
00200 do {
00201 int ret;
00202
00203 if ((ret = ff_request_frame(link->src->inputs[0])))
00204 return ret;
00205 } while (!idet->cur);
00206
00207 return 0;
00208 }
00209
00210 static int poll_frame(AVFilterLink *link)
00211 {
00212 IDETContext *idet = link->src->priv;
00213 int ret, val;
00214
00215 val = ff_poll_frame(link->src->inputs[0]);
00216
00217 if (val >= 1 && !idet->next) {
00218 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
00219 return ret;
00220 val = ff_poll_frame(link->src->inputs[0]);
00221 }
00222 assert(idet->next || !val);
00223
00224 return val;
00225 }
00226
00227 static av_cold void uninit(AVFilterContext *ctx)
00228 {
00229 IDETContext *idet = ctx->priv;
00230
00231 av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
00232 idet->prestat[TFF],
00233 idet->prestat[BFF],
00234 idet->prestat[PROGRSSIVE],
00235 idet->prestat[UNDETERMINED]
00236 );
00237 av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
00238 idet->poststat[TFF],
00239 idet->poststat[BFF],
00240 idet->poststat[PROGRSSIVE],
00241 idet->poststat[UNDETERMINED]
00242 );
00243
00244 avfilter_unref_bufferp(&idet->prev);
00245 avfilter_unref_bufferp(&idet->cur );
00246 avfilter_unref_bufferp(&idet->next);
00247 }
00248
00249 static int query_formats(AVFilterContext *ctx)
00250 {
00251 static const enum AVPixelFormat pix_fmts[] = {
00252 AV_PIX_FMT_YUV420P,
00253 AV_PIX_FMT_YUV422P,
00254 AV_PIX_FMT_YUV444P,
00255 AV_PIX_FMT_YUV410P,
00256 AV_PIX_FMT_YUV411P,
00257 AV_PIX_FMT_GRAY8,
00258 AV_PIX_FMT_YUVJ420P,
00259 AV_PIX_FMT_YUVJ422P,
00260 AV_PIX_FMT_YUVJ444P,
00261 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
00262 AV_PIX_FMT_YUV440P,
00263 AV_PIX_FMT_YUVJ440P,
00264 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
00265 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
00266 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
00267 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
00268 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
00269 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
00270 AV_PIX_FMT_YUVA420P,
00271 AV_PIX_FMT_NONE
00272 };
00273
00274 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00275
00276 return 0;
00277 }
00278
00279 static av_cold int init(AVFilterContext *ctx, const char *args)
00280 {
00281 IDETContext *idet = ctx->priv;
00282
00283 idet->csp = NULL;
00284
00285 idet->interlace_threshold = 1.01;
00286 idet->progressive_threshold = 2.5;
00287
00288 if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
00289
00290 idet->last_type = UNDETERMINED;
00291 memset(idet->history, UNDETERMINED, HIST_SIZE);
00292
00293 idet->filter_line = filter_line_c;
00294
00295 return 0;
00296 }
00297
00298
00299 static const AVFilterPad idet_inputs[] = {
00300 {
00301 .name = "default",
00302 .type = AVMEDIA_TYPE_VIDEO,
00303 .filter_frame = filter_frame,
00304 .min_perms = AV_PERM_PRESERVE,
00305 },
00306 { NULL }
00307 };
00308
00309 static const AVFilterPad idet_outputs[] = {
00310 {
00311 .name = "default",
00312 .type = AVMEDIA_TYPE_VIDEO,
00313 .rej_perms = AV_PERM_WRITE,
00314 .poll_frame = poll_frame,
00315 .request_frame = request_frame,
00316 },
00317 { NULL }
00318 };
00319
00320 AVFilter avfilter_vf_idet = {
00321 .name = "idet",
00322 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
00323
00324 .priv_size = sizeof(IDETContext),
00325 .init = init,
00326 .uninit = uninit,
00327 .query_formats = query_formats,
00328 .inputs = idet_inputs,
00329 .outputs = idet_outputs,
00330 };