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 <float.h> /* FLT_MAX */
23 #include "libavutil/cpu.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
29 #define OFFSET(x) offsetof(IDETContext, x)
30 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 { "rep_thres", "set repeat threshold", OFFSET(repeat_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 3.0}, -1, FLT_MAX, FLAGS },
36 { "half_life", "half life of cumulative statistics", OFFSET(half_life), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -1, INT_MAX, FLAGS },
37 { "analyze_interlaced_flag", "set number of frames to use to determine if the interlace flag is accurate", OFFSET(analyze_interlaced_flag), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, FLAGS },
41 AVFILTER_DEFINE_CLASS(idet);
43 static const char *type2str(Type type)
46 case TFF : return "tff";
47 case BFF : return "bff";
48 case PROGRESSIVE : return "progressive";
49 case UNDETERMINED : return "undetermined";
54 #define PRECISION 1048576
56 static uint64_t uintpow(uint64_t b,unsigned int e)
63 static int av_dict_set_fxp(AVDictionary **pm, const char *key, uint64_t value, unsigned int digits,
67 uint64_t print_precision = uintpow(10, digits);
69 value = av_rescale(value, print_precision, PRECISION);
71 snprintf(valuestr, sizeof(valuestr), "%"PRId64".%0*"PRId64,
72 value / print_precision, digits, value % print_precision);
74 return av_dict_set(pm, key, valuestr, flags);
77 static const char *rep2str(RepeatedField repeated_field)
79 switch(repeated_field) {
80 case REPEAT_NONE : return "neither";
81 case REPEAT_TOP : return "top";
82 case REPEAT_BOTTOM : return "bottom";
87 int ff_idet_filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
93 int v = (*a++ + *c++) - 2 * *b++;
100 int ff_idet_filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
106 int v = (*a++ + *c++) - 2 * *b++;
113 static void filter(AVFilterContext *ctx)
115 IDETContext *idet = ctx->priv;
117 int64_t alpha[2]={0};
119 int64_t gamma[2]={0};
120 Type type, best_type;
121 RepeatedField repeat;
123 AVDictionary **metadata = &idet->cur->metadata;
125 for (i = 0; i < idet->csp->nb_components; i++) {
126 int w = idet->cur->width;
127 int h = idet->cur->height;
128 int refs = idet->cur->linesize[i];
131 w = AV_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
132 h = AV_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
135 for (y = 2; y < h - 2; y++) {
136 uint8_t *prev = &idet->prev->data[i][y*refs];
137 uint8_t *cur = &idet->cur ->data[i][y*refs];
138 uint8_t *next = &idet->next->data[i][y*refs];
139 alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
140 alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
141 delta += idet->filter_line(cur-refs, cur, cur+refs, w);
142 gamma[(y^1)&1] += idet->filter_line(cur , prev, cur , w);
146 if (alpha[0] > idet->interlace_threshold * alpha[1]){
148 }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
150 }else if(alpha[1] > idet->progressive_threshold * delta){
156 if ( gamma[0] > idet->repeat_threshold * gamma[1] ){
158 } else if ( gamma[1] > idet->repeat_threshold * gamma[0] ){
159 repeat = REPEAT_BOTTOM;
161 repeat = REPEAT_NONE;
164 memmove(idet->history+1, idet->history, HIST_SIZE-1);
165 idet->history[0] = type;
166 best_type = UNDETERMINED;
167 for(i=0; i<HIST_SIZE; i++){
168 if(idet->history[i] != UNDETERMINED){
169 if(best_type == UNDETERMINED)
170 best_type = idet->history[i];
172 if(idet->history[i] == best_type) {
180 if(idet->last_type == UNDETERMINED){
181 if(match ) idet->last_type = best_type;
183 if(match>2) idet->last_type = best_type;
186 if (idet->last_type == TFF){
187 idet->cur->top_field_first = 1;
188 idet->cur->interlaced_frame = 1;
189 }else if(idet->last_type == BFF){
190 idet->cur->top_field_first = 0;
191 idet->cur->interlaced_frame = 1;
192 }else if(idet->last_type == PROGRESSIVE){
193 idet->cur->interlaced_frame = 0;
197 idet->repeats[i] = av_rescale(idet->repeats [i], idet->decay_coefficient, PRECISION);
200 idet->prestat [i] = av_rescale(idet->prestat [i], idet->decay_coefficient, PRECISION);
201 idet->poststat[i] = av_rescale(idet->poststat[i], idet->decay_coefficient, PRECISION);
204 idet->total_repeats [ repeat] ++;
205 idet->repeats [ repeat] += PRECISION;
207 idet->total_prestat [ type] ++;
208 idet->prestat [ type] += PRECISION;
210 idet->total_poststat[idet->last_type] ++;
211 idet->poststat [idet->last_type] += PRECISION;
213 av_log(ctx, AV_LOG_DEBUG, "Repeated Field:%12s, Single frame:%12s, Multi frame:%12s\n",
214 rep2str(repeat), type2str(type), type2str(idet->last_type));
216 av_dict_set (metadata, "lavfi.idet.repeated.current_frame", rep2str(repeat), 0);
217 av_dict_set_fxp(metadata, "lavfi.idet.repeated.neither", idet->repeats[REPEAT_NONE], 2, 0);
218 av_dict_set_fxp(metadata, "lavfi.idet.repeated.top", idet->repeats[REPEAT_TOP], 2, 0);
219 av_dict_set_fxp(metadata, "lavfi.idet.repeated.bottom", idet->repeats[REPEAT_BOTTOM], 2, 0);
221 av_dict_set (metadata, "lavfi.idet.single.current_frame", type2str(type), 0);
222 av_dict_set_fxp(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 2 , 0);
223 av_dict_set_fxp(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 2, 0);
224 av_dict_set_fxp(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 2, 0);
225 av_dict_set_fxp(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 2, 0);
227 av_dict_set (metadata, "lavfi.idet.multiple.current_frame", type2str(idet->last_type), 0);
228 av_dict_set_fxp(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 2, 0);
229 av_dict_set_fxp(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 2, 0);
230 av_dict_set_fxp(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 2, 0);
231 av_dict_set_fxp(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 2, 0);
234 static int filter_frame(AVFilterLink *link, AVFrame *picref)
236 AVFilterContext *ctx = link->dst;
237 IDETContext *idet = ctx->priv;
239 // initial frame(s) and not interlaced, just pass through for
240 // the analyze_interlaced_flag mode
241 if (idet->analyze_interlaced_flag &&
242 !picref->interlaced_frame &&
244 return ff_filter_frame(ctx->outputs[0], picref);
246 if (idet->analyze_interlaced_flag_done) {
247 if (picref->interlaced_frame && idet->interlaced_flag_accuracy < 0)
248 picref->interlaced_frame = 0;
249 return ff_filter_frame(ctx->outputs[0], picref);
252 av_frame_free(&idet->prev);
254 if( picref->width != link->w
255 || picref->height != link->h
256 || picref->format != link->format) {
257 link->dst->inputs[0]->format = picref->format;
258 link->dst->inputs[0]->w = picref->width;
259 link->dst->inputs[0]->h = picref->height;
261 av_frame_free(&idet->cur );
262 av_frame_free(&idet->next);
265 idet->prev = idet->cur;
266 idet->cur = idet->next;
270 !(idet->cur = av_frame_clone(idet->next)))
271 return AVERROR(ENOMEM);
277 idet->csp = av_pix_fmt_desc_get(link->format);
278 if (idet->csp->comp[0].depth > 8){
279 idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;
281 ff_idet_init_x86(idet, 1);
284 if (idet->analyze_interlaced_flag) {
285 if (idet->cur->interlaced_frame) {
286 idet->cur->interlaced_frame = 0;
288 if (idet->last_type == PROGRESSIVE) {
289 idet->interlaced_flag_accuracy --;
290 idet->analyze_interlaced_flag --;
291 } else if (idet->last_type != UNDETERMINED) {
292 idet->interlaced_flag_accuracy ++;
293 idet->analyze_interlaced_flag --;
295 if (idet->analyze_interlaced_flag == 1) {
296 ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
298 if (idet->next->interlaced_frame && idet->interlaced_flag_accuracy < 0)
299 idet->next->interlaced_frame = 0;
300 idet->analyze_interlaced_flag_done = 1;
301 av_log(ctx, AV_LOG_INFO, "Final flag accuracy %d\n", idet->interlaced_flag_accuracy);
302 return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->next));
309 return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
312 static int request_frame(AVFilterLink *link)
314 AVFilterContext *ctx = link->src;
315 IDETContext *idet = ctx->priv;
321 ret = ff_request_frame(link->src->inputs[0]);
323 if (ret == AVERROR_EOF && idet->cur && !idet->analyze_interlaced_flag_done) {
324 AVFrame *next = av_frame_clone(idet->next);
327 return AVERROR(ENOMEM);
329 ret = filter_frame(link->src->inputs[0], next);
336 static av_cold void uninit(AVFilterContext *ctx)
338 IDETContext *idet = ctx->priv;
339 int level = strncmp(ctx->name, "auto-inserted", 13) ? AV_LOG_INFO : AV_LOG_DEBUG;
341 av_log(ctx, level, "Repeated Fields: Neither:%6"PRId64" Top:%6"PRId64" Bottom:%6"PRId64"\n",
342 idet->total_repeats[REPEAT_NONE],
343 idet->total_repeats[REPEAT_TOP],
344 idet->total_repeats[REPEAT_BOTTOM]
346 av_log(ctx, level, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
347 idet->total_prestat[TFF],
348 idet->total_prestat[BFF],
349 idet->total_prestat[PROGRESSIVE],
350 idet->total_prestat[UNDETERMINED]
352 av_log(ctx, level, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
353 idet->total_poststat[TFF],
354 idet->total_poststat[BFF],
355 idet->total_poststat[PROGRESSIVE],
356 idet->total_poststat[UNDETERMINED]
359 av_frame_free(&idet->prev);
360 av_frame_free(&idet->cur );
361 av_frame_free(&idet->next);
364 static int query_formats(AVFilterContext *ctx)
366 static const enum AVPixelFormat pix_fmts[] = {
382 AV_PIX_FMT_YUV420P10,
383 AV_PIX_FMT_YUV422P10,
384 AV_PIX_FMT_YUV444P10,
385 AV_PIX_FMT_YUV420P12,
386 AV_PIX_FMT_YUV422P12,
387 AV_PIX_FMT_YUV444P12,
388 AV_PIX_FMT_YUV420P14,
389 AV_PIX_FMT_YUV422P14,
390 AV_PIX_FMT_YUV444P14,
391 AV_PIX_FMT_YUV420P16,
392 AV_PIX_FMT_YUV422P16,
393 AV_PIX_FMT_YUV444P16,
399 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
401 return AVERROR(ENOMEM);
402 return ff_set_common_formats(ctx, fmts_list);
405 static av_cold int init(AVFilterContext *ctx)
407 IDETContext *idet = ctx->priv;
410 idet->last_type = UNDETERMINED;
411 memset(idet->history, UNDETERMINED, HIST_SIZE);
413 if( idet->half_life > 0 )
414 idet->decay_coefficient = lrint( PRECISION * exp2(-1.0 / idet->half_life) );
416 idet->decay_coefficient = PRECISION;
418 idet->filter_line = ff_idet_filter_line_c;
421 ff_idet_init_x86(idet, 0);
426 static const AVFilterPad idet_inputs[] = {
429 .type = AVMEDIA_TYPE_VIDEO,
430 .filter_frame = filter_frame,
435 static const AVFilterPad idet_outputs[] = {
438 .type = AVMEDIA_TYPE_VIDEO,
439 .request_frame = request_frame
444 AVFilter ff_vf_idet = {
446 .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
447 .priv_size = sizeof(IDETContext),
450 .query_formats = query_formats,
451 .inputs = idet_inputs,
452 .outputs = idet_outputs,
453 .priv_class = &idet_class,