FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_idet.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 135 188 71.8%
Functions: 9 9 100.0%
Branches: 51 105 48.6%

Line Branch Exec Source
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/common.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26
27 #include "filters.h"
28 #include "vf_idetdsp.h"
29
30 typedef enum {
31 TFF,
32 BFF,
33 PROGRESSIVE,
34 UNDETERMINED,
35 } Type;
36
37 typedef enum {
38 REPEAT_NONE,
39 REPEAT_TOP,
40 REPEAT_BOTTOM,
41 } RepeatedField;
42
43 typedef struct IDETContext {
44 const AVClass *class;
45 IDETDSPContext dsp;
46
47 float interlace_threshold;
48 float progressive_threshold;
49 float repeat_threshold;
50 float half_life;
51 uint64_t decay_coefficient;
52
53 Type last_type;
54
55 uint64_t repeats[3];
56 uint64_t prestat[4];
57 uint64_t poststat[4];
58 uint64_t total_repeats[3];
59 uint64_t total_prestat[4];
60 uint64_t total_poststat[4];
61
62 #define HIST_SIZE 4
63 uint8_t history[HIST_SIZE];
64
65 AVFrame *cur;
66 AVFrame *next;
67 AVFrame *prev;
68
69 int interlaced_flag_accuracy;
70 int analyze_interlaced_flag;
71 int analyze_interlaced_flag_done;
72
73 const AVPixFmtDescriptor *csp;
74 int eof;
75 } IDETContext;
76
77 #define OFFSET(x) offsetof(IDETContext, x)
78 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
79
80 static const AVOption idet_options[] = {
81 { "intl_thres", "set interlacing threshold", OFFSET(interlace_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.04}, -1, FLT_MAX, FLAGS },
82 { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.5}, -1, FLT_MAX, FLAGS },
83 { "rep_thres", "set repeat threshold", OFFSET(repeat_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 3.0}, -1, FLT_MAX, FLAGS },
84 { "half_life", "half life of cumulative statistics", OFFSET(half_life), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -1, INT_MAX, FLAGS },
85 { "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 },
86 { NULL }
87 };
88
89 AVFILTER_DEFINE_CLASS(idet);
90
91 104 static const char *type2str(Type type)
92 {
93
2/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
104 switch(type) {
94 case TFF : return "tff";
95 case BFF : return "bff";
96 102 case PROGRESSIVE : return "progressive";
97 2 case UNDETERMINED : return "undetermined";
98 }
99 return NULL;
100 }
101
102 #define PRECISION 1048576
103
104 286 static uint64_t uintpow(uint64_t b,unsigned int e)
105 {
106 286 uint64_t r=1;
107
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 286 times.
858 while(e--) r*=b;
108 286 return r;
109 }
110
111 286 static int av_dict_set_fxp(AVDictionary **pm, const char *key, uint64_t value, unsigned int digits,
112 int flags)
113 {
114 char valuestr[44];
115 286 uint64_t print_precision = uintpow(10, digits);
116
117 286 value = av_rescale(value, print_precision, PRECISION);
118
119 286 snprintf(valuestr, sizeof(valuestr), "%"PRId64".%0*"PRId64,
120 value / print_precision, digits, value % print_precision);
121
122 286 return av_dict_set(pm, key, valuestr, flags);
123 }
124
125 52 static const char *rep2str(RepeatedField repeated_field)
126 {
127
1/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
52 switch(repeated_field) {
128 52 case REPEAT_NONE : return "neither";
129 case REPEAT_TOP : return "top";
130 case REPEAT_BOTTOM : return "bottom";
131 }
132 return NULL;
133 }
134
135 26 static void filter(AVFilterContext *ctx)
136 {
137 26 IDETContext *idet = ctx->priv;
138 int y, i;
139 26 int64_t alpha[2]={0};
140 26 int64_t delta=0;
141 26 int64_t gamma[2]={0};
142 Type type, best_type;
143 RepeatedField repeat;
144 26 int match = 0;
145 26 AVDictionary **metadata = &idet->cur->metadata;
146 26 ff_idet_filter_func filter_line = idet->dsp.filter_line;
147
148
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 for (i = 0; i < idet->csp->nb_components; i++) {
149 26 int w = idet->cur->width;
150 26 int h = idet->cur->height;
151 26 int refs = idet->cur->linesize[i];
152
153
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 if (i && i<3) {
154 w = AV_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
155 h = AV_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
156 }
157
158
2/2
✓ Branch 0 taken 11128 times.
✓ Branch 1 taken 26 times.
11154 for (y = 2; y < h - 2; y++) {
159 11128 uint8_t *prev = &idet->prev->data[i][y*refs];
160 11128 uint8_t *cur = &idet->cur ->data[i][y*refs];
161 11128 uint8_t *next = &idet->next->data[i][y*refs];
162 11128 alpha[ y &1] += filter_line(cur-refs, prev, cur+refs, w);
163 11128 alpha[(y^1)&1] += filter_line(cur-refs, next, cur+refs, w);
164 11128 delta += filter_line(cur-refs, cur, cur+refs, w);
165 11128 gamma[(y^1)&1] += filter_line(cur , prev, cur , w);
166 }
167 }
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (alpha[0] > idet->interlace_threshold * alpha[1]){
170 type = TFF;
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
172 type = BFF;
173
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 times.
26 }else if(alpha[1] > idet->progressive_threshold * delta){
174 25 type = PROGRESSIVE;
175 }else{
176 1 type = UNDETERMINED;
177 }
178
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if ( gamma[0] > idet->repeat_threshold * gamma[1] ){
180 repeat = REPEAT_TOP;
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 } else if ( gamma[1] > idet->repeat_threshold * gamma[0] ){
182 repeat = REPEAT_BOTTOM;
183 } else {
184 26 repeat = REPEAT_NONE;
185 }
186
187 26 memmove(idet->history+1, idet->history, HIST_SIZE-1);
188 26 idet->history[0] = type;
189 26 best_type = UNDETERMINED;
190
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 26 times.
130 for(i=0; i<HIST_SIZE; i++){
191
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 9 times.
104 if(idet->history[i] != UNDETERMINED){
192
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 69 times.
95 if(best_type == UNDETERMINED)
193 26 best_type = idet->history[i];
194
195
1/2
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
95 if(idet->history[i] == best_type) {
196 95 match++;
197 }else{
198 match=0;
199 break;
200 }
201 }
202 }
203
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
26 if(idet->last_type == UNDETERMINED){
204
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(match ) idet->last_type = best_type;
205 }else{
206
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if(match>2) idet->last_type = best_type;
207 }
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (idet->last_type == TFF){
210 idet->cur->flags |= (AV_FRAME_FLAG_INTERLACED | AV_FRAME_FLAG_TOP_FIELD_FIRST);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 }else if(idet->last_type == BFF){
212 idet->cur->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
213 idet->cur->flags |= AV_FRAME_FLAG_INTERLACED;
214
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 }else if(idet->last_type == PROGRESSIVE){
215 26 idet->cur->flags &= ~AV_FRAME_FLAG_INTERLACED;
216 }
217
218
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for(i=0; i<3; i++)
219 78 idet->repeats[i] = av_rescale(idet->repeats [i], idet->decay_coefficient, PRECISION);
220
221
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 26 times.
130 for(i=0; i<4; i++){
222 104 idet->prestat [i] = av_rescale(idet->prestat [i], idet->decay_coefficient, PRECISION);
223 104 idet->poststat[i] = av_rescale(idet->poststat[i], idet->decay_coefficient, PRECISION);
224 }
225
226 26 idet->total_repeats [ repeat] ++;
227 26 idet->repeats [ repeat] += PRECISION;
228
229 26 idet->total_prestat [ type] ++;
230 26 idet->prestat [ type] += PRECISION;
231
232 26 idet->total_poststat[idet->last_type] ++;
233 26 idet->poststat [idet->last_type] += PRECISION;
234
235 26 av_log(ctx, AV_LOG_DEBUG, "Repeated Field:%12s, Single frame:%12s, Multi frame:%12s\n",
236 rep2str(repeat), type2str(type), type2str(idet->last_type));
237
238 26 av_dict_set (metadata, "lavfi.idet.repeated.current_frame", rep2str(repeat), 0);
239 26 av_dict_set_fxp(metadata, "lavfi.idet.repeated.neither", idet->repeats[REPEAT_NONE], 2, 0);
240 26 av_dict_set_fxp(metadata, "lavfi.idet.repeated.top", idet->repeats[REPEAT_TOP], 2, 0);
241 26 av_dict_set_fxp(metadata, "lavfi.idet.repeated.bottom", idet->repeats[REPEAT_BOTTOM], 2, 0);
242
243 26 av_dict_set (metadata, "lavfi.idet.single.current_frame", type2str(type), 0);
244 26 av_dict_set_fxp(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 2 , 0);
245 26 av_dict_set_fxp(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 2, 0);
246 26 av_dict_set_fxp(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 2, 0);
247 26 av_dict_set_fxp(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 2, 0);
248
249 26 av_dict_set (metadata, "lavfi.idet.multiple.current_frame", type2str(idet->last_type), 0);
250 26 av_dict_set_fxp(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 2, 0);
251 26 av_dict_set_fxp(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 2, 0);
252 26 av_dict_set_fxp(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 2, 0);
253 26 av_dict_set_fxp(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 2, 0);
254 26 }
255
256 27 static int filter_frame(AVFilterLink *link, AVFrame *picref)
257 {
258 27 AVFilterContext *ctx = link->dst;
259 27 IDETContext *idet = ctx->priv;
260
261 // initial frame(s) and not interlaced, just pass through for
262 // the analyze_interlaced_flag mode
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (idet->analyze_interlaced_flag &&
264 !(picref->flags & AV_FRAME_FLAG_INTERLACED) &&
265 !idet->next) {
266 return ff_filter_frame(ctx->outputs[0], picref);
267 }
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (idet->analyze_interlaced_flag_done) {
269 if ((picref->flags & AV_FRAME_FLAG_INTERLACED) && idet->interlaced_flag_accuracy < 0) {
270 picref->flags &= ~AV_FRAME_FLAG_INTERLACED;
271 }
272 return ff_filter_frame(ctx->outputs[0], picref);
273 }
274
275 27 av_frame_free(&idet->prev);
276
277
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if( picref->width != link->w
278
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 || picref->height != link->h
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 || picref->format != link->format) {
280 link->dst->inputs[0]->format = picref->format;
281 link->dst->inputs[0]->w = picref->width;
282 link->dst->inputs[0]->h = picref->height;
283
284 av_frame_free(&idet->cur );
285 av_frame_free(&idet->next);
286 idet->csp = NULL;
287 }
288
289 27 idet->prev = idet->cur;
290 27 idet->cur = idet->next;
291 27 idet->next = picref;
292
293
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (!idet->cur &&
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 !(idet->cur = av_frame_clone(idet->next)))
295 return AVERROR(ENOMEM);
296
297
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (!idet->prev)
298 1 return 0;
299
300
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
26 if (!idet->csp) {
301 1 idet->csp = av_pix_fmt_desc_get(link->format);
302 1 ff_idet_dsp_init(&idet->dsp, idet->csp->comp[0].depth);
303 }
304
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (idet->analyze_interlaced_flag) {
306 if (idet->cur->flags & AV_FRAME_FLAG_INTERLACED) {
307 idet->cur->flags &= ~AV_FRAME_FLAG_INTERLACED;
308 filter(ctx);
309 if (idet->last_type == PROGRESSIVE) {
310 idet->interlaced_flag_accuracy --;
311 idet->analyze_interlaced_flag --;
312 } else if (idet->last_type != UNDETERMINED) {
313 idet->interlaced_flag_accuracy ++;
314 idet->analyze_interlaced_flag --;
315 }
316 if (idet->analyze_interlaced_flag == 1) {
317 ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
318
319 if ((idet->next->flags & AV_FRAME_FLAG_INTERLACED) && idet->interlaced_flag_accuracy < 0) {
320 idet->next->flags &= ~AV_FRAME_FLAG_INTERLACED;
321 }
322 idet->analyze_interlaced_flag_done = 1;
323 av_log(ctx, AV_LOG_INFO, "Final flag accuracy %d\n", idet->interlaced_flag_accuracy);
324 return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->next));
325 }
326 }
327 } else {
328 26 filter(ctx);
329 }
330
331 26 return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
332 }
333
334 26 static int request_frame(AVFilterLink *link)
335 {
336 26 AVFilterContext *ctx = link->src;
337 26 IDETContext *idet = ctx->priv;
338 int ret;
339
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (idet->eof)
341 return AVERROR_EOF;
342
343 26 ret = ff_request_frame(link->src->inputs[0]);
344
345
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
26 if (ret == AVERROR_EOF && idet->cur && !idet->analyze_interlaced_flag_done) {
346 AVFrame *next = av_frame_clone(idet->next);
347
348 if (!next)
349 return AVERROR(ENOMEM);
350
351 ret = filter_frame(link->src->inputs[0], next);
352 idet->eof = 1;
353 }
354
355 26 return ret;
356 }
357
358 2 static av_cold void uninit(AVFilterContext *ctx)
359 {
360 2 IDETContext *idet = ctx->priv;
361
362 2 av_log(ctx, AV_LOG_INFO, "Repeated Fields: Neither:%6"PRId64" Top:%6"PRId64" Bottom:%6"PRId64"\n",
363 idet->total_repeats[REPEAT_NONE],
364 idet->total_repeats[REPEAT_TOP],
365 idet->total_repeats[REPEAT_BOTTOM]
366 );
367 2 av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
368 idet->total_prestat[TFF],
369 idet->total_prestat[BFF],
370 idet->total_prestat[PROGRESSIVE],
371 idet->total_prestat[UNDETERMINED]
372 );
373 2 av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
374 idet->total_poststat[TFF],
375 idet->total_poststat[BFF],
376 idet->total_poststat[PROGRESSIVE],
377 idet->total_poststat[UNDETERMINED]
378 );
379
380 2 av_frame_free(&idet->prev);
381 2 av_frame_free(&idet->cur );
382 2 av_frame_free(&idet->next);
383 2 }
384
385 static const enum AVPixelFormat pix_fmts[] = {
386 AV_PIX_FMT_YUV420P,
387 AV_PIX_FMT_YUV422P,
388 AV_PIX_FMT_YUV444P,
389 AV_PIX_FMT_YUV410P,
390 AV_PIX_FMT_YUV411P,
391 AV_PIX_FMT_GRAY8,
392 AV_PIX_FMT_YUVJ420P,
393 AV_PIX_FMT_YUVJ422P,
394 AV_PIX_FMT_YUVJ444P,
395 AV_PIX_FMT_GRAY16,
396 AV_PIX_FMT_YUV440P,
397 AV_PIX_FMT_YUVJ440P,
398 AV_PIX_FMT_YUV420P9,
399 AV_PIX_FMT_YUV422P9,
400 AV_PIX_FMT_YUV444P9,
401 AV_PIX_FMT_YUV420P10,
402 AV_PIX_FMT_YUV422P10,
403 AV_PIX_FMT_YUV444P10,
404 AV_PIX_FMT_YUV420P12,
405 AV_PIX_FMT_YUV422P12,
406 AV_PIX_FMT_YUV444P12,
407 AV_PIX_FMT_YUV420P14,
408 AV_PIX_FMT_YUV422P14,
409 AV_PIX_FMT_YUV444P14,
410 AV_PIX_FMT_YUV420P16,
411 AV_PIX_FMT_YUV422P16,
412 AV_PIX_FMT_YUV444P16,
413 AV_PIX_FMT_YUVA420P,
414 AV_PIX_FMT_YUVA422P,
415 AV_PIX_FMT_YUVA444P,
416 AV_PIX_FMT_NONE
417 };
418
419 2 static av_cold int init(AVFilterContext *ctx)
420 {
421 2 IDETContext *idet = ctx->priv;
422
423 2 idet->eof = 0;
424 2 idet->last_type = UNDETERMINED;
425 2 memset(idet->history, UNDETERMINED, HIST_SIZE);
426
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if( idet->half_life > 0 )
428 idet->decay_coefficient = lrint( PRECISION * exp2(-1.0 / idet->half_life) );
429 else
430 2 idet->decay_coefficient = PRECISION;
431
432 2 ff_idet_dsp_init(&idet->dsp, 8);
433
434 2 return 0;
435 }
436
437 static const AVFilterPad idet_inputs[] = {
438 {
439 .name = "default",
440 .type = AVMEDIA_TYPE_VIDEO,
441 .filter_frame = filter_frame,
442 },
443 };
444
445 static const AVFilterPad idet_outputs[] = {
446 {
447 .name = "default",
448 .type = AVMEDIA_TYPE_VIDEO,
449 .request_frame = request_frame
450 },
451 };
452
453 const FFFilter ff_vf_idet = {
454 .p.name = "idet",
455 .p.description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
456 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
457 .p.priv_class = &idet_class,
458 .priv_size = sizeof(IDETContext),
459 .init = init,
460 .uninit = uninit,
461 FILTER_INPUTS(idet_inputs),
462 FILTER_OUTPUTS(idet_outputs),
463 FILTER_PIXFMTS_ARRAY(pix_fmts),
464 };
465