FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_idet.c
Go to the documentation of this file.
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 <float.h> /* FLT_MAX */
22 
23 #include "libavutil/cpu.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 
30 #define HIST_SIZE 4
31 
32 typedef enum {
33  TFF,
34  BFF,
37 } Type;
38 
39 typedef struct {
40  const AVClass *class;
43 
45  int prestat[4];
46  int poststat[4];
47 
48  uint8_t history[HIST_SIZE];
49 
53  int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
54 
56 } IDETContext;
57 
58 #define OFFSET(x) offsetof(IDETContext, x)
59 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60 
61 static const AVOption idet_options[] = {
62  { "intl_thres", "set interlacing threshold", OFFSET(interlace_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.01}, -1, FLT_MAX, FLAGS },
63  { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 2.5}, -1, FLT_MAX, FLAGS },
64  { NULL }
65 };
66 
68 
69 static const char *type2str(Type type)
70 {
71  switch(type) {
72  case TFF : return "Top Field First ";
73  case BFF : return "Bottom Field First";
74  case PROGRSSIVE : return "Progressive ";
75  case UNDETERMINED: return "Undetermined ";
76  }
77  return NULL;
78 }
79 
80 static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_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 int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
93 {
94  int x;
95  int ret=0;
96 
97  for(x=0; x<w; x++){
98  ret += FFABS((*a++ + *c++) - 2 * *b++);
99  }
100 
101  return ret;
102 }
103 
104 static void filter(AVFilterContext *ctx)
105 {
106  IDETContext *idet = ctx->priv;
107  int y, i;
108  int64_t alpha[2]={0};
109  int64_t delta=0;
110  Type type, best_type;
111  int match = 0;
112 
113  for (i = 0; i < idet->csp->nb_components; i++) {
114  int w = idet->cur->video->w;
115  int h = idet->cur->video->h;
116  int refs = idet->cur->linesize[i];
117 
118  if (i && i<3) {
119  w >>= idet->csp->log2_chroma_w;
120  h >>= idet->csp->log2_chroma_h;
121  }
122 
123  for (y = 2; y < h - 2; y++) {
124  uint8_t *prev = &idet->prev->data[i][y*refs];
125  uint8_t *cur = &idet->cur ->data[i][y*refs];
126  uint8_t *next = &idet->next->data[i][y*refs];
127  alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
128  alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
129  delta += idet->filter_line(cur-refs, cur, cur+refs, w);
130  }
131  }
132 
133  if (alpha[0] > idet->interlace_threshold * alpha[1]){
134  type = TFF;
135  }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
136  type = BFF;
137  }else if(alpha[1] > idet->progressive_threshold * delta){
138  type = PROGRSSIVE;
139  }else{
140  type = UNDETERMINED;
141  }
142 
143  memmove(idet->history+1, idet->history, HIST_SIZE-1);
144  idet->history[0] = type;
145  best_type = UNDETERMINED;
146  for(i=0; i<HIST_SIZE; i++){
147  if(idet->history[i] != UNDETERMINED){
148  if(best_type == UNDETERMINED)
149  best_type = idet->history[i];
150 
151  if(idet->history[i] == best_type) {
152  match++;
153  }else{
154  match=0;
155  break;
156  }
157  }
158  }
159  if(idet->last_type == UNDETERMINED){
160  if(match ) idet->last_type = best_type;
161  }else{
162  if(match>2) idet->last_type = best_type;
163  }
164 
165  if (idet->last_type == TFF){
166  idet->cur->video->top_field_first = 1;
167  idet->cur->video->interlaced = 1;
168  }else if(idet->last_type == BFF){
169  idet->cur->video->top_field_first = 0;
170  idet->cur->video->interlaced = 1;
171  }else if(idet->last_type == PROGRSSIVE){
172  idet->cur->video->interlaced = 0;
173  }
174 
175  idet->prestat [ type] ++;
176  idet->poststat[idet->last_type] ++;
177  av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
178 }
179 
180 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
181 {
182  AVFilterContext *ctx = link->dst;
183  IDETContext *idet = ctx->priv;
184 
185  if (idet->prev)
187  idet->prev = idet->cur;
188  idet->cur = idet->next;
189  idet->next = picref;
190 
191  if (!idet->cur)
192  return 0;
193 
194  if (!idet->prev)
195  idet->prev = avfilter_ref_buffer(idet->cur, ~0);
196 
197  if (!idet->csp)
198  idet->csp = av_pix_fmt_desc_get(link->format);
199  if (idet->csp->comp[0].depth_minus1 / 8 == 1)
200  idet->filter_line = (void*)filter_line_c_16bit;
201 
202  filter(ctx);
203 
204  return ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
205 }
206 
207 static int request_frame(AVFilterLink *link)
208 {
209  AVFilterContext *ctx = link->src;
210  IDETContext *idet = ctx->priv;
211 
212  do {
213  int ret;
214 
215  if ((ret = ff_request_frame(link->src->inputs[0])))
216  return ret;
217  } while (!idet->cur);
218 
219  return 0;
220 }
221 
222 static av_cold void uninit(AVFilterContext *ctx)
223 {
224  IDETContext *idet = ctx->priv;
225 
226  av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
227  idet->prestat[TFF],
228  idet->prestat[BFF],
229  idet->prestat[PROGRSSIVE],
230  idet->prestat[UNDETERMINED]
231  );
232  av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
233  idet->poststat[TFF],
234  idet->poststat[BFF],
235  idet->poststat[PROGRSSIVE],
236  idet->poststat[UNDETERMINED]
237  );
238 
240  avfilter_unref_bufferp(&idet->cur );
242 }
243 
245 {
246  static const enum AVPixelFormat pix_fmts[] = {
267  };
268 
270 
271  return 0;
272 }
273 
274 static av_cold int init(AVFilterContext *ctx, const char *args)
275 {
276  IDETContext *idet = ctx->priv;
277  static const char *shorthand[] = { "intl_thres", "prog_thres", NULL };
278  int ret;
279 
280  idet->class = &idet_class;
281  av_opt_set_defaults(idet);
282 
283  if ((ret = av_opt_set_from_string(idet, args, shorthand, "=", ":")) < 0)
284  return ret;
285 
286  idet->last_type = UNDETERMINED;
287  memset(idet->history, UNDETERMINED, HIST_SIZE);
288 
289  idet->filter_line = filter_line_c;
290 
291  return 0;
292 }
293 
294 
295 static const AVFilterPad idet_inputs[] = {
296  {
297  .name = "default",
298  .type = AVMEDIA_TYPE_VIDEO,
299  .filter_frame = filter_frame,
300  .min_perms = AV_PERM_PRESERVE,
301  },
302  { NULL }
303 };
304 
305 static const AVFilterPad idet_outputs[] = {
306  {
307  .name = "default",
308  .type = AVMEDIA_TYPE_VIDEO,
309  .rej_perms = AV_PERM_WRITE,
310  .request_frame = request_frame,
311  },
312  { NULL }
313 };
314 
316  .name = "idet",
317  .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
318 
319  .priv_size = sizeof(IDETContext),
320  .init = init,
321  .uninit = uninit,
323  .inputs = idet_inputs,
324  .outputs = idet_outputs,
325  .priv_class = &idet_class,
326 };