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"
37 float interlace_threshold;
38 float progressive_threshold;
43 AVFilterBufferRef *cur;
44 AVFilterBufferRef *next;
45 AVFilterBufferRef *prev;
46 AVFilterBufferRef *out;
47 int (*filter_line)(uint8_t *prev, uint8_t *cur, uint8_t *next, int w);
49 const AVPixFmtDescriptor *csp;
53 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
59 ret += FFABS((*a++ + *c++) - 2 * *b++);
65 static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
71 ret += FFABS((*a++ + *c++) - 2 * *b++);
77 static void filter(AVFilterContext *ctx)
79 IDETContext *idet = ctx->priv;
85 for (i = 0; i < idet->csp->nb_components; i++) {
86 int w = idet->cur->video->w;
87 int h = idet->cur->video->h;
88 int refs = idet->cur->linesize[i];
89 int df = (idet->csp->comp[i].depth_minus1 + 8) / 8;
92 w >>= idet->csp->log2_chroma_w;
93 h >>= idet->csp->log2_chroma_h;
96 for (y = 2; y < h - 2; y++) {
97 uint8_t *prev = &idet->prev->data[i][y*refs];
98 uint8_t *cur = &idet->cur ->data[i][y*refs];
99 uint8_t *next = &idet->next->data[i][y*refs];
100 alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
101 alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
102 delta += idet->filter_line(cur-refs, cur, cur+refs, w);
106 __asm__ volatile("emms \n\t" : : : "memory");
109 if (alpha[0] / (float)alpha[1] > idet->interlace_threshold){
111 }else if(alpha[1] / (float)alpha[0] > idet->interlace_threshold){
113 }else if(alpha[1] / (float)delta > idet->progressive_threshold){
119 idet->prestat[type] ++;
122 av_log(ctx, AV_LOG_INFO, "Interlaced, top field first\n");
123 idet->cur->video->top_field_first = 1;
124 idet->cur->video->interlaced = 1;
125 }else if(type == BFF){
126 av_log(ctx, AV_LOG_INFO, "Interlaced, bottom field first\n");
127 idet->cur->video->top_field_first = 0;
128 idet->cur->video->interlaced = 1;
129 }else if(type == PROGRSSIVE){
130 av_log(ctx, AV_LOG_INFO, "Progressive\n");
131 idet->cur->video->interlaced = 0;
133 av_log(ctx, AV_LOG_INFO, "Undetermined\n");
134 idet->cur->video->interlaced = idet->prev->video->interlaced;
135 idet->cur->video->top_field_first = idet->prev->video->top_field_first;
139 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
141 AVFilterContext *ctx = link->dst;
142 IDETContext *idet = ctx->priv;
145 avfilter_unref_buffer(idet->prev);
146 idet->prev = idet->cur;
147 idet->cur = idet->next;
154 idet->prev = avfilter_ref_buffer(idet->cur, AV_PERM_READ);
156 avfilter_start_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, AV_PERM_READ));
159 static void end_frame(AVFilterLink *link)
161 AVFilterContext *ctx = link->dst;
162 IDETContext *idet = ctx->priv;
168 idet->csp = &av_pix_fmt_descriptors[link->format];
169 if (idet->csp->comp[0].depth_minus1 / 8 == 1)
170 idet->filter_line = (void*)filter_line_c_16bit;
174 avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
175 avfilter_end_frame(ctx->outputs[0]);
178 static int request_frame(AVFilterLink *link)
180 AVFilterContext *ctx = link->src;
181 IDETContext *idet = ctx->priv;
186 if ((ret = avfilter_request_frame(link->src->inputs[0])))
188 } while (!idet->cur);
193 static int poll_frame(AVFilterLink *link)
195 IDETContext *idet = link->src->priv;
198 val = avfilter_poll_frame(link->src->inputs[0]);
200 if (val >= 1 && !idet->next) { //FIXME change API to not requre this red tape
201 if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
203 val = avfilter_poll_frame(link->src->inputs[0]);
205 assert(idet->next || !val);
210 static av_cold void uninit(AVFilterContext *ctx)
212 IDETContext *idet = ctx->priv;
214 av_log(ctx, AV_LOG_INFO, "TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
217 idet->prestat[PROGRSSIVE],
218 idet->prestat[UNDETERMINED]
221 if (idet->prev) avfilter_unref_buffer(idet->prev);
222 if (idet->cur ) avfilter_unref_buffer(idet->cur );
223 if (idet->next) avfilter_unref_buffer(idet->next);
226 static int query_formats(AVFilterContext *ctx)
228 static const enum PixelFormat pix_fmts[] = {
238 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
241 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
242 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
243 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
244 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
245 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
246 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
251 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
256 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
258 IDETContext *idet = ctx->priv;
259 int cpu_flags = av_get_cpu_flags();
263 idet->interlace_threshold = 1.01;
264 idet->progressive_threshold = 2.5;
266 if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
268 idet->filter_line = filter_line_c;
273 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
275 AVFilter avfilter_vf_idet = {
277 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
279 .priv_size = sizeof(IDETContext),
282 .query_formats = query_formats,
284 .inputs = (const AVFilterPad[]) {{ .name = "default",
285 .type = AVMEDIA_TYPE_VIDEO,
286 .start_frame = start_frame,
287 .draw_slice = null_draw_slice,
288 .end_frame = end_frame,
289 .rej_perms = AV_PERM_REUSE2, },
292 .outputs = (const AVFilterPad[]) {{ .name = "default",
293 .type = AVMEDIA_TYPE_VIDEO,
294 .poll_frame = poll_frame,
295 .request_frame = request_frame, },