FFmpeg
 All Data Structures Namespaces 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 "internal.h"
27 #include "vf_idet.h"
28 
29 #define OFFSET(x) offsetof(IDETContext, x)
30 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
31 
32 static const AVOption idet_options[] = {
33  { "intl_thres", "set interlacing threshold", OFFSET(interlace_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.04}, -1, FLT_MAX, FLAGS },
34  { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.5}, -1, FLT_MAX, FLAGS },
35  { NULL }
36 };
37 
39 
40 static const char *type2str(Type type)
41 {
42  switch(type) {
43  case TFF : return "Top Field First ";
44  case BFF : return "Bottom Field First";
45  case PROGRESSIVE : return "Progressive ";
46  case UNDETERMINED : return "Undetermined ";
47  }
48  return NULL;
49 }
50 
51 int ff_idet_filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
52 {
53  int x;
54  int ret=0;
55 
56  for(x=0; x<w; x++){
57  int v = (*a++ + *c++) - 2 * *b++;
58  ret += FFABS(v);
59  }
60 
61  return ret;
62 }
63 
64 int ff_idet_filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
65 {
66  int x;
67  int ret=0;
68 
69  for(x=0; x<w; x++){
70  int v = (*a++ + *c++) - 2 * *b++;
71  ret += FFABS(v);
72  }
73 
74  return ret;
75 }
76 
77 static void filter(AVFilterContext *ctx)
78 {
79  IDETContext *idet = ctx->priv;
80  int y, i;
81  int64_t alpha[2]={0};
82  int64_t delta=0;
83  Type type, best_type;
84  int match = 0;
85 
86  for (i = 0; i < idet->csp->nb_components; i++) {
87  int w = idet->cur->width;
88  int h = idet->cur->height;
89  int refs = idet->cur->linesize[i];
90 
91  if (i && i<3) {
92  w = FF_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
93  h = FF_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
94  }
95 
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);
103  }
104  }
105 
106  if (alpha[0] > idet->interlace_threshold * alpha[1]){
107  type = TFF;
108  }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
109  type = BFF;
110  }else if(alpha[1] > idet->progressive_threshold * delta){
111  type = PROGRESSIVE;
112  }else{
113  type = UNDETERMINED;
114  }
115 
116  memmove(idet->history+1, idet->history, HIST_SIZE-1);
117  idet->history[0] = type;
118  best_type = UNDETERMINED;
119  for(i=0; i<HIST_SIZE; i++){
120  if(idet->history[i] != UNDETERMINED){
121  if(best_type == UNDETERMINED)
122  best_type = idet->history[i];
123 
124  if(idet->history[i] == best_type) {
125  match++;
126  }else{
127  match=0;
128  break;
129  }
130  }
131  }
132  if(idet->last_type == UNDETERMINED){
133  if(match ) idet->last_type = best_type;
134  }else{
135  if(match>2) idet->last_type = best_type;
136  }
137 
138  if (idet->last_type == TFF){
139  idet->cur->top_field_first = 1;
140  idet->cur->interlaced_frame = 1;
141  }else if(idet->last_type == BFF){
142  idet->cur->top_field_first = 0;
143  idet->cur->interlaced_frame = 1;
144  }else if(idet->last_type == PROGRESSIVE){
145  idet->cur->interlaced_frame = 0;
146  }
147 
148  idet->prestat [ type] ++;
149  idet->poststat[idet->last_type] ++;
150  av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
151 }
152 
153 static int filter_frame(AVFilterLink *link, AVFrame *picref)
154 {
155  AVFilterContext *ctx = link->dst;
156  IDETContext *idet = ctx->priv;
157  AVDictionary **metadata = avpriv_frame_get_metadatap(picref);
158 
159  if (idet->prev)
160  av_frame_free(&idet->prev);
161  idet->prev = idet->cur;
162  idet->cur = idet->next;
163  idet->next = picref;
164 
165  if (!idet->cur)
166  return 0;
167 
168  if (!idet->prev)
169  idet->prev = av_frame_clone(idet->cur);
170 
171  if (!idet->csp)
172  idet->csp = av_pix_fmt_desc_get(link->format);
173  if (idet->csp->comp[0].depth_minus1 / 8 == 1){
175  if (ARCH_X86)
176  ff_idet_init_x86(idet, 1);
177  }
178 
179  filter(ctx);
180 
181  av_dict_set_int(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 0);
182  av_dict_set_int(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 0);
183  av_dict_set_int(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 0);
184  av_dict_set_int(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 0);
185 
186  av_dict_set_int(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 0);
187  av_dict_set_int(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 0);
188  av_dict_set_int(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 0);
189  av_dict_set_int(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 0);
190 
191  return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
192 }
193 
194 static int request_frame(AVFilterLink *link)
195 {
196  AVFilterContext *ctx = link->src;
197  IDETContext *idet = ctx->priv;
198 
199  do {
200  int ret;
201 
202  if (idet->eof)
203  return AVERROR_EOF;
204 
205  ret = ff_request_frame(link->src->inputs[0]);
206 
207  if (ret == AVERROR_EOF && idet->cur) {
208  AVFrame *next = av_frame_clone(idet->next);
209 
210  if (!next)
211  return AVERROR(ENOMEM);
212 
213  filter_frame(link->src->inputs[0], next);
214  idet->eof = 1;
215  } else if (ret < 0) {
216  return ret;
217  }
218  } while (!idet->cur);
219 
220  return 0;
221 }
222 
223 static av_cold void uninit(AVFilterContext *ctx)
224 {
225  IDETContext *idet = ctx->priv;
226 
227  av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
228  idet->prestat[TFF],
229  idet->prestat[BFF],
230  idet->prestat[PROGRESSIVE],
231  idet->prestat[UNDETERMINED]
232  );
233  av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
234  idet->poststat[TFF],
235  idet->poststat[BFF],
236  idet->poststat[PROGRESSIVE],
237  idet->poststat[UNDETERMINED]
238  );
239 
240  av_frame_free(&idet->prev);
241  av_frame_free(&idet->cur );
242  av_frame_free(&idet->next);
243 }
244 
246 {
247  static const enum AVPixelFormat pix_fmts[] = {
268  };
269 
271 
272  return 0;
273 }
274 
275 static int config_output(AVFilterLink *outlink)
276 {
277  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
278  return 0;
279 }
280 
281 static av_cold int init(AVFilterContext *ctx)
282 {
283  IDETContext *idet = ctx->priv;
284 
285  idet->eof = 0;
286  idet->last_type = UNDETERMINED;
287  memset(idet->history, UNDETERMINED, HIST_SIZE);
288 
290 
291  if (ARCH_X86)
292  ff_idet_init_x86(idet, 0);
293 
294  return 0;
295 }
296 
297 
298 static const AVFilterPad idet_inputs[] = {
299  {
300  .name = "default",
301  .type = AVMEDIA_TYPE_VIDEO,
302  .filter_frame = filter_frame,
303  },
304  { NULL }
305 };
306 
307 static const AVFilterPad idet_outputs[] = {
308  {
309  .name = "default",
310  .type = AVMEDIA_TYPE_VIDEO,
311  .config_props = config_output,
312  .request_frame = request_frame
313  },
314  { NULL }
315 };
316 
318  .name = "idet",
319  .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
320  .priv_size = sizeof(IDETContext),
321  .init = init,
322  .uninit = uninit,
324  .inputs = idet_inputs,
325  .outputs = idet_outputs,
326  .priv_class = &idet_class,
327 };