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"
30 float interlace_threshold;
31 float progressive_threshold;
33 AVFilterBufferRef *cur;
34 AVFilterBufferRef *next;
35 AVFilterBufferRef *prev;
36 AVFilterBufferRef *out;
37 int (*filter_line)(uint8_t *prev, uint8_t *cur, uint8_t *next, int w);
39 const AVPixFmtDescriptor *csp;
43 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
49 ret += FFABS((*a++ + *c++) - 2 * *b++);
55 static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
61 ret += FFABS((*a++ + *c++) - 2 * *b++);
67 static void filter(AVFilterContext *ctx)
69 IDETContext *idet = ctx->priv;
73 static int p=0, t=0, b=0, u=0;
75 for (i = 0; i < idet->csp->nb_components; i++) {
76 int w = idet->cur->video->w;
77 int h = idet->cur->video->h;
78 int refs = idet->cur->linesize[i];
79 int df = (idet->csp->comp[i].depth_minus1 + 8) / 8;
82 w >>= idet->csp->log2_chroma_w;
83 h >>= idet->csp->log2_chroma_h;
86 for (y = 2; y < h - 2; y++) {
87 uint8_t *prev = &idet->prev->data[i][y*refs];
88 uint8_t *cur = &idet->cur ->data[i][y*refs];
89 uint8_t *next = &idet->next->data[i][y*refs];
90 alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
91 alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
92 delta += idet->filter_line(cur-refs, cur, cur+refs, w);
96 __asm__ volatile("emms \n\t" : : : "memory");
99 if (alpha[0] / (float)alpha[1] > idet->interlace_threshold){
100 av_log(ctx, AV_LOG_INFO, "Interlaced, top field first\n");
102 }else if(alpha[1] / (float)alpha[0] > idet->interlace_threshold){
103 av_log(ctx, AV_LOG_INFO, "Interlaced, bottom field first\n");
105 }else if(alpha[1] / (float)delta > idet->progressive_threshold){
106 av_log(ctx, AV_LOG_INFO, "Progressive\n");
109 av_log(ctx, AV_LOG_INFO, "Undetermined\n");
112 // av_log(ctx,0, "t%d b%d p%d u%d\n", t,b,p,u);
115 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
117 AVFilterContext *ctx = link->dst;
118 IDETContext *idet = ctx->priv;
121 avfilter_unref_buffer(idet->prev);
122 idet->prev = idet->cur;
123 idet->cur = idet->next;
130 idet->prev = avfilter_ref_buffer(idet->cur, AV_PERM_READ);
132 avfilter_start_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, AV_PERM_READ));
135 static void end_frame(AVFilterLink *link)
137 AVFilterContext *ctx = link->dst;
138 IDETContext *idet = ctx->priv;
144 idet->csp = &av_pix_fmt_descriptors[link->format];
145 if (idet->csp->comp[0].depth_minus1 / 8 == 1)
146 idet->filter_line = (void*)filter_line_c_16bit;
150 avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
151 avfilter_end_frame(ctx->outputs[0]);
154 static int request_frame(AVFilterLink *link)
156 AVFilterContext *ctx = link->src;
157 IDETContext *idet = ctx->priv;
162 if ((ret = avfilter_request_frame(link->src->inputs[0])))
164 } while (!idet->cur);
169 static int poll_frame(AVFilterLink *link)
171 IDETContext *idet = link->src->priv;
174 val = avfilter_poll_frame(link->src->inputs[0]);
176 if (val >= 1 && !idet->next) { //FIXME change API to not requre this red tape
177 if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
179 val = avfilter_poll_frame(link->src->inputs[0]);
181 assert(idet->next || !val);
186 static av_cold void uninit(AVFilterContext *ctx)
188 IDETContext *idet = ctx->priv;
190 if (idet->prev) avfilter_unref_buffer(idet->prev);
191 if (idet->cur ) avfilter_unref_buffer(idet->cur );
192 if (idet->next) avfilter_unref_buffer(idet->next);
195 static int query_formats(AVFilterContext *ctx)
197 static const enum PixelFormat pix_fmts[] = {
207 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
210 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
211 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
212 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
213 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
214 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
215 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
220 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
225 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
227 IDETContext *idet = ctx->priv;
228 int cpu_flags = av_get_cpu_flags();
232 idet->interlace_threshold = 1.01;
233 idet->progressive_threshold = 2.5;
235 if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
237 idet->filter_line = filter_line_c;
242 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
244 AVFilter avfilter_vf_idet = {
246 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
248 .priv_size = sizeof(IDETContext),
251 .query_formats = query_formats,
253 .inputs = (const AVFilterPad[]) {{ .name = "default",
254 .type = AVMEDIA_TYPE_VIDEO,
255 .start_frame = start_frame,
256 .draw_slice = null_draw_slice,
257 .end_frame = end_frame,
258 .rej_perms = AV_PERM_REUSE2, },
261 .outputs = (const AVFilterPad[]) {{ .name = "default",
262 .type = AVMEDIA_TYPE_VIDEO,
263 .poll_frame = poll_frame,
264 .request_frame = request_frame, },