]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_idet.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_idet.c
1 /*
2  * Copyright (C) 2012 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "internal.h"
26
27 #undef NDEBUG
28 #include <assert.h>
29
30 #define HIST_SIZE 4
31
32 typedef enum {
33     TFF,
34     BFF,
35     PROGRSSIVE,
36     UNDETERMINED,
37 } Type;
38
39 typedef struct {
40     float interlace_threshold;
41     float progressive_threshold;
42
43     Type last_type;
44     Type prestat[4];
45     Type poststat[4];
46
47     uint8_t history[HIST_SIZE];
48
49     AVFilterBufferRef *cur;
50     AVFilterBufferRef *next;
51     AVFilterBufferRef *prev;
52     int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
53
54     const AVPixFmtDescriptor *csp;
55 } IDETContext;
56
57 static const char *type2str(Type type)
58 {
59     switch(type) {
60         case TFF         : return "Top Field First   ";
61         case BFF         : return "Bottom Field First";
62         case PROGRSSIVE  : return "Progressive       ";
63         case UNDETERMINED: return "Undetermined      ";
64     }
65     return NULL;
66 }
67
68 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
69 {
70     int x;
71     int ret=0;
72
73     for(x=0; x<w; x++){
74         ret += FFABS((*a++ + *c++) - 2 * *b++);
75     }
76
77     return ret;
78 }
79
80 static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
81 {
82     int x;
83     int ret=0;
84
85     for(x=0; x<w; x++){
86         ret += FFABS((*a++ + *c++) - 2 * *b++);
87     }
88
89     return ret;
90 }
91
92 static void filter(AVFilterContext *ctx)
93 {
94     IDETContext *idet = ctx->priv;
95     int y, i;
96     int64_t alpha[2]={0};
97     int64_t delta=0;
98     Type type, best_type;
99     int match = 0;
100
101     for (i = 0; i < idet->csp->nb_components; i++) {
102         int w = idet->cur->video->w;
103         int h = idet->cur->video->h;
104         int refs = idet->cur->linesize[i];
105
106         if (i && i<3) {
107             w >>= idet->csp->log2_chroma_w;
108             h >>= idet->csp->log2_chroma_h;
109         }
110
111         for (y = 2; y < h - 2; y++) {
112             uint8_t *prev = &idet->prev->data[i][y*refs];
113             uint8_t *cur  = &idet->cur ->data[i][y*refs];
114             uint8_t *next = &idet->next->data[i][y*refs];
115             alpha[ y   &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
116             alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
117             delta          += idet->filter_line(cur-refs,  cur, cur+refs, w);
118         }
119     }
120
121     if      (alpha[0] > idet->interlace_threshold * alpha[1]){
122         type = TFF;
123     }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
124         type = BFF;
125     }else if(alpha[1] > idet->progressive_threshold * delta){
126         type = PROGRSSIVE;
127     }else{
128         type = UNDETERMINED;
129     }
130
131     memmove(idet->history+1, idet->history, HIST_SIZE-1);
132     idet->history[0] = type;
133     best_type = UNDETERMINED;
134     for(i=0; i<HIST_SIZE; i++){
135         if(idet->history[i] != UNDETERMINED){
136             if(best_type == UNDETERMINED)
137                 best_type = idet->history[i];
138
139             if(idet->history[i] == best_type) {
140                 match++;
141             }else{
142                 match=0;
143                 break;
144             }
145         }
146     }
147     if(idet->last_type == UNDETERMINED){
148         if(match  ) idet->last_type = best_type;
149     }else{
150         if(match>2) idet->last_type = best_type;
151     }
152
153     if      (idet->last_type == TFF){
154         idet->cur->video->top_field_first = 1;
155         idet->cur->video->interlaced = 1;
156     }else if(idet->last_type == BFF){
157         idet->cur->video->top_field_first = 0;
158         idet->cur->video->interlaced = 1;
159     }else if(idet->last_type == PROGRSSIVE){
160         idet->cur->video->interlaced = 0;
161     }
162
163     idet->prestat [           type] ++;
164     idet->poststat[idet->last_type] ++;
165     av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
166 }
167
168 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
169 {
170     AVFilterContext *ctx = link->dst;
171     IDETContext *idet = ctx->priv;
172
173     if (idet->prev)
174         avfilter_unref_buffer(idet->prev);
175     idet->prev = idet->cur;
176     idet->cur  = idet->next;
177     idet->next = picref;
178
179     if (!idet->cur)
180         return 0;
181
182     if (!idet->prev)
183         idet->prev = avfilter_ref_buffer(idet->cur, ~0);
184
185     if (!idet->csp)
186         idet->csp = av_pix_fmt_desc_get(link->format);
187     if (idet->csp->comp[0].depth_minus1 / 8 == 1)
188         idet->filter_line = (void*)filter_line_c_16bit;
189
190     filter(ctx);
191
192     return ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
193 }
194
195 static int request_frame(AVFilterLink *link)
196 {
197     AVFilterContext *ctx = link->src;
198     IDETContext *idet = ctx->priv;
199
200     do {
201         int ret;
202
203         if ((ret = ff_request_frame(link->src->inputs[0])))
204             return ret;
205     } while (!idet->cur);
206
207     return 0;
208 }
209
210 static int poll_frame(AVFilterLink *link)
211 {
212     IDETContext *idet = link->src->priv;
213     int ret, val;
214
215     val = ff_poll_frame(link->src->inputs[0]);
216
217     if (val >= 1 && !idet->next) { //FIXME change API to not requre this red tape
218         if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
219             return ret;
220         val = ff_poll_frame(link->src->inputs[0]);
221     }
222     assert(idet->next || !val);
223
224     return val;
225 }
226
227 static av_cold void uninit(AVFilterContext *ctx)
228 {
229     IDETContext *idet = ctx->priv;
230
231     av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
232            idet->prestat[TFF],
233            idet->prestat[BFF],
234            idet->prestat[PROGRSSIVE],
235            idet->prestat[UNDETERMINED]
236     );
237     av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
238            idet->poststat[TFF],
239            idet->poststat[BFF],
240            idet->poststat[PROGRSSIVE],
241            idet->poststat[UNDETERMINED]
242     );
243
244     avfilter_unref_bufferp(&idet->prev);
245     avfilter_unref_bufferp(&idet->cur );
246     avfilter_unref_bufferp(&idet->next);
247 }
248
249 static int query_formats(AVFilterContext *ctx)
250 {
251     static const enum AVPixelFormat pix_fmts[] = {
252         AV_PIX_FMT_YUV420P,
253         AV_PIX_FMT_YUV422P,
254         AV_PIX_FMT_YUV444P,
255         AV_PIX_FMT_YUV410P,
256         AV_PIX_FMT_YUV411P,
257         AV_PIX_FMT_GRAY8,
258         AV_PIX_FMT_YUVJ420P,
259         AV_PIX_FMT_YUVJ422P,
260         AV_PIX_FMT_YUVJ444P,
261         AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
262         AV_PIX_FMT_YUV440P,
263         AV_PIX_FMT_YUVJ440P,
264         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
265         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
266         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
267         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
268         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
269         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
270         AV_PIX_FMT_YUVA420P,
271         AV_PIX_FMT_NONE
272     };
273
274     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
275
276     return 0;
277 }
278
279 static av_cold int init(AVFilterContext *ctx, const char *args)
280 {
281     IDETContext *idet = ctx->priv;
282
283     idet->csp = NULL;
284
285     idet->interlace_threshold   = 1.01;
286     idet->progressive_threshold = 2.5;
287
288     if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
289
290     idet->last_type = UNDETERMINED;
291     memset(idet->history, UNDETERMINED, HIST_SIZE);
292
293     idet->filter_line = filter_line_c;
294
295     return 0;
296 }
297
298
299 static const AVFilterPad idet_inputs[] = {
300     {
301         .name         = "default",
302         .type         = AVMEDIA_TYPE_VIDEO,
303         .filter_frame = filter_frame,
304         .min_perms    = AV_PERM_PRESERVE,
305     },
306     { NULL }
307 };
308
309 static const AVFilterPad idet_outputs[] = {
310     {
311         .name          = "default",
312         .type          = AVMEDIA_TYPE_VIDEO,
313         .rej_perms     = AV_PERM_WRITE,
314         .poll_frame    = poll_frame,
315         .request_frame = request_frame,
316     },
317     { NULL }
318 };
319
320 AVFilter avfilter_vf_idet = {
321     .name          = "idet",
322     .description   = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
323
324     .priv_size     = sizeof(IDETContext),
325     .init          = init,
326     .uninit        = uninit,
327     .query_formats = query_formats,
328     .inputs        = idet_inputs,
329     .outputs       = idet_outputs,
330 };