2 * Copyright (C) 2012 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
23 #include "libavutil/pixdesc.h"
40 float interlace_threshold;
41 float progressive_threshold;
47 uint8_t history[HIST_SIZE];
49 AVFilterBufferRef *cur;
50 AVFilterBufferRef *next;
51 AVFilterBufferRef *prev;
52 AVFilterBufferRef *out;
53 int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
55 const AVPixFmtDescriptor *csp;
58 static const char *type2str(Type type)
61 case TFF : return "Top Field First ";
62 case BFF : return "Bottom Field First";
63 case PROGRSSIVE : return "Progressive ";
64 case UNDETERMINED: return "Undetermined ";
69 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
75 ret += FFABS((*a++ + *c++) - 2 * *b++);
81 static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
87 ret += FFABS((*a++ + *c++) - 2 * *b++);
93 static void filter(AVFilterContext *ctx)
95 IDETContext *idet = ctx->priv;
102 for (i = 0; i < idet->csp->nb_components; i++) {
103 int w = idet->cur->video->w;
104 int h = idet->cur->video->h;
105 int refs = idet->cur->linesize[i];
108 w >>= idet->csp->log2_chroma_w;
109 h >>= idet->csp->log2_chroma_h;
112 for (y = 2; y < h - 2; y++) {
113 uint8_t *prev = &idet->prev->data[i][y*refs];
114 uint8_t *cur = &idet->cur ->data[i][y*refs];
115 uint8_t *next = &idet->next->data[i][y*refs];
116 alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
117 alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
118 delta += idet->filter_line(cur-refs, cur, cur+refs, w);
122 if (alpha[0] / (float)alpha[1] > idet->interlace_threshold){
124 }else if(alpha[1] / (float)alpha[0] > idet->interlace_threshold){
126 }else if(alpha[1] / (float)delta > idet->progressive_threshold){
132 memmove(idet->history+1, idet->history, HIST_SIZE-1);
133 idet->history[0] = type;
134 best_type = UNDETERMINED;
135 for(i=0; i<HIST_SIZE; i++){
136 if(idet->history[i] != UNDETERMINED){
137 if(best_type == UNDETERMINED)
138 best_type = idet->history[i];
140 if(idet->history[i] == best_type) {
148 if(idet->last_type == UNDETERMINED){
149 if(match ) idet->last_type = best_type;
151 if(match>2) idet->last_type = best_type;
154 if (idet->last_type == TFF){
155 idet->cur->video->top_field_first = 1;
156 idet->cur->video->interlaced = 1;
157 }else if(idet->last_type == BFF){
158 idet->cur->video->top_field_first = 0;
159 idet->cur->video->interlaced = 1;
160 }else if(idet->last_type == PROGRSSIVE){
161 idet->cur->video->interlaced = 0;
164 idet->prestat [ type] ++;
165 idet->poststat[idet->last_type] ++;
166 av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
169 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
171 AVFilterContext *ctx = link->dst;
172 IDETContext *idet = ctx->priv;
175 avfilter_unref_buffer(idet->prev);
176 idet->prev = idet->cur;
177 idet->cur = idet->next;
184 idet->prev = avfilter_ref_buffer(idet->cur, ~0);
186 return ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
189 static int end_frame(AVFilterLink *link)
191 AVFilterContext *ctx = link->dst;
192 IDETContext *idet = ctx->priv;
198 idet->csp = &av_pix_fmt_descriptors[link->format];
199 if (idet->csp->comp[0].depth_minus1 / 8 == 1)
200 idet->filter_line = (void*)filter_line_c_16bit;
204 ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
205 return ff_end_frame(ctx->outputs[0]);
208 static int request_frame(AVFilterLink *link)
210 AVFilterContext *ctx = link->src;
211 IDETContext *idet = ctx->priv;
216 if ((ret = ff_request_frame(link->src->inputs[0])))
218 } while (!idet->cur);
223 static int poll_frame(AVFilterLink *link)
225 IDETContext *idet = link->src->priv;
228 val = ff_poll_frame(link->src->inputs[0]);
230 if (val >= 1 && !idet->next) { //FIXME change API to not requre this red tape
231 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
233 val = ff_poll_frame(link->src->inputs[0]);
235 assert(idet->next || !val);
240 static av_cold void uninit(AVFilterContext *ctx)
242 IDETContext *idet = ctx->priv;
244 av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
247 idet->prestat[PROGRSSIVE],
248 idet->prestat[UNDETERMINED]
250 av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
253 idet->poststat[PROGRSSIVE],
254 idet->poststat[UNDETERMINED]
257 if (idet->prev) avfilter_unref_buffer(idet->prev);
258 if (idet->cur ) avfilter_unref_buffer(idet->cur );
259 if (idet->next) avfilter_unref_buffer(idet->next);
262 static int query_formats(AVFilterContext *ctx)
264 static const enum PixelFormat pix_fmts[] = {
274 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
277 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
278 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
279 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
280 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
281 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
282 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
287 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
292 static av_cold int init(AVFilterContext *ctx, const char *args)
294 IDETContext *idet = ctx->priv;
298 idet->interlace_threshold = 1.01;
299 idet->progressive_threshold = 2.5;
301 if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
303 idet->last_type = UNDETERMINED;
304 memset(idet->history, UNDETERMINED, HIST_SIZE);
306 idet->filter_line = filter_line_c;
311 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
313 AVFilter avfilter_vf_idet = {
315 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
317 .priv_size = sizeof(IDETContext),
320 .query_formats = query_formats,
322 .inputs = (const AVFilterPad[]) {{ .name = "default",
323 .type = AVMEDIA_TYPE_VIDEO,
324 .start_frame = start_frame,
325 .draw_slice = null_draw_slice,
326 .end_frame = end_frame,
327 .min_perms = AV_PERM_PRESERVE },
330 .outputs = (const AVFilterPad[]) {{ .name = "default",
331 .type = AVMEDIA_TYPE_VIDEO,
332 .rej_perms = AV_PERM_WRITE,
333 .poll_frame = poll_frame,
334 .request_frame = request_frame, },