]> git.sesse.net Git - vlc/blob - modules/video_filter/deinterlace/algo_ivtc.c
Refactored deinterlacer module
[vlc] / modules / video_filter / deinterlace / algo_ivtc.c
1 /*****************************************************************************
2  * algo_ivtc.c : IVTC (inverse telecine) algorithm for the VLC deinterlacer
3  *****************************************************************************
4  * Copyright (C) 2010-2011 the VideoLAN team
5  * $Id$
6  *
7  * Author: Juha Jeronen <juha.jeronen@jyu.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #   include "config.h"
26 #endif
27
28 #ifdef CAN_COMPILE_MMXEXT
29 #   include "mmx.h"
30 #endif
31
32 #include <stdint.h>
33 #include <assert.h>
34
35 #include <vlc_common.h>
36 #include <vlc_cpu.h>
37 #include <vlc_picture.h>
38 #include <vlc_filter.h>
39
40 #include "deinterlace.h" /* filter_sys_t */
41 #include "helpers.h"
42
43 #include "algo_ivtc.h"
44
45 /*****************************************************************************
46  * Local data
47  *****************************************************************************/
48
49 /* Fasten your seatbelt - lots of IVTC constants follow... */
50
51 /**
52  * IVTC filter modes.
53  *
54  * Hard telecine: burned into video stream.
55  * Soft telecine: stream consists of progressive frames;
56  *                telecining handled by stream flags.
57  *
58  * @see ivtc_sys_t
59  * @see RenderIVTC()
60  */
61 typedef enum { IVTC_MODE_DETECTING           = 0,
62                IVTC_MODE_TELECINED_NTSC_HARD = 1,
63                IVTC_MODE_TELECINED_NTSC_SOFT = 2 } ivtc_mode;
64
65 /**
66  *  Field pair combinations from successive frames in the PCN stencil.
67  *  T = top, B = bottom, P = previous, C = current, N = next
68  *  These are used as array indices; hence the explicit numbering.
69  */
70 typedef enum { FIELD_PAIR_TPBP = 0, FIELD_PAIR_TPBC = 1,
71                FIELD_PAIR_TCBP = 2, FIELD_PAIR_TCBC = 3,
72                FIELD_PAIR_TCBN = 4, FIELD_PAIR_TNBC = 5,
73                FIELD_PAIR_TNBN = 6 } ivtc_field_pair;
74
75 /* Note: only valid ones count for NUM */
76 #define NUM_CADENCE_POS 9
77 /**
78  * Cadence positions for the PCN stencil (PCN, Previous Current Next).
79  *
80  * Note that "dea" in both cadence tables and a pure progressive signal
81  * are indistinguishable.
82  *
83  * Used as array indices except the -1.
84  *
85  * This is a combined raw position containing both i_cadence_pos
86  * and telecine field dominance.
87  * @see pi_detected_pos_to_cadence_pos
88  * @see pi_detected_pos_to_tfd
89  */
90 typedef enum { CADENCE_POS_INVALID     = -1,
91                CADENCE_POS_PROGRESSIVE =  0,
92                CADENCE_POS_TFF_ABC     =  1,
93                CADENCE_POS_TFF_BCD     =  2,
94                CADENCE_POS_TFF_CDE     =  3,
95                CADENCE_POS_TFF_EAB     =  4,
96                CADENCE_POS_BFF_ABC     =  5,
97                CADENCE_POS_BFF_BCD     =  6,
98                CADENCE_POS_BFF_CDE     =  7,
99                CADENCE_POS_BFF_EAB     =  8 } ivtc_cadence_pos;
100 /* First and one-past-end for TFF-only and BFF-only raw positions. */
101 #define CADENCE_POS_TFF_FIRST 1
102 #define CADENCE_POS_TFF_END   5
103 #define CADENCE_POS_BFF_FIRST 5
104 #define CADENCE_POS_BFF_END   9
105
106 /**
107  * For the "vektor" cadence detector algorithm.
108  *
109  * The algorithm produces a set of possible positions instead of a unique
110  * position, until it locks on. The set is represented as a bitmask.
111  *
112  * The bitmask is stored in a word, and its layout is:
113  * blank blank BFF_CARRY BFF4 BFF3 BFF2 BFF1 BFF0   (high byte)
114  * blank blank TFF_CARRY TFF4 TFF3 TFF2 TFF1 TFF0   (low byte)
115  *
116  * This allows predicting the next position by left-shifting the previous
117  * result by one bit, copying the CARRY bits to the respective zeroth position,
118  * and ANDing with 0x1F1F.
119  *
120  * This table is indexed with a valid ivtc_cadence_pos.
121  * @see ivtc_cadence_pos
122  */
123 const int pi_detected_pos_to_bitmask[NUM_CADENCE_POS] = { 0x0808, /* prog. */
124                                                           0x0001, /* TFF ABC */
125                                                           0x0002, /* TFF BCD */
126                                                           0x0004, /* TFF CDE */
127                                                           0x0010, /* TFF EAB */
128                                                           0x0100, /* BFF ABC */
129                                                           0x0200, /* BFF BCD */
130                                                           0x0400, /* BFF CDE */
131                                                           0x1000, /* BFF EAB */
132                                                         };
133 #define VEKTOR_CADENCE_POS_ALL 0x1F1F
134 #define VEKTOR_CADENCE_POS_TFF 0x00FF
135 #define VEKTOR_CADENCE_POS_BFF 0xFF00
136 #define VEKTOR_CADENCE_POS_TFF_HIGH 0x0010
137 #define VEKTOR_CADENCE_POS_TFF_LOW  0x0001
138 #define VEKTOR_CADENCE_POS_BFF_HIGH 0x1000
139 #define VEKTOR_CADENCE_POS_BFF_LOW  0x0100
140
141 /* Telecine field dominance */
142 typedef enum { TFD_INVALID = -1, TFD_TFF = 0, TFD_BFF = 1 } ivtc_tfd;
143
144 /**
145  * Position detection table for the "scores" cadence detector algorithm.
146  *
147  * These are the (only) field pair combinations that should give progressive
148  * frames. There are three for each position.
149  *
150  * First index: ivtc_cadence_pos
151  */
152 static const ivtc_field_pair pi_best_field_pairs[NUM_CADENCE_POS][3] = {
153     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBC, FIELD_PAIR_TNBN}, /* prog. */
154
155     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBP, FIELD_PAIR_TNBC}, /* TFF ABC */
156     {FIELD_PAIR_TCBP, FIELD_PAIR_TNBC, FIELD_PAIR_TNBN}, /* TFF BCD */
157     {FIELD_PAIR_TCBP, FIELD_PAIR_TCBC, FIELD_PAIR_TNBN}, /* TFF CDE */
158     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBC, FIELD_PAIR_TNBC}, /* TFF EAB */
159
160     {FIELD_PAIR_TPBP, FIELD_PAIR_TPBC, FIELD_PAIR_TCBN}, /* BFF ABC */
161     {FIELD_PAIR_TPBC, FIELD_PAIR_TCBN, FIELD_PAIR_TNBN}, /* BFF BCD */
162     {FIELD_PAIR_TPBC, FIELD_PAIR_TCBC, FIELD_PAIR_TNBN}, /* BFF CDE */
163     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBC, FIELD_PAIR_TCBN}, /* BFF EAB */
164 };
165
166 /**
167  * Alternative position detection table for the "scores" cadence detector
168  * algorithm.
169  *
170  * These field pair combinations should give only interlaced frames.
171  * There are four for each position.
172  *
173  * First index: ivtc_cadence_pos
174  *
175  * Currently unused. During development it was tested that whether we detect
176  * best or worst, the resulting detected cadence positions are identical
177  * (neither strategy performs any different from the other).
178  */
179 static const ivtc_field_pair pi_worst_field_pairs[NUM_CADENCE_POS][4] = {
180     {FIELD_PAIR_TPBC, FIELD_PAIR_TCBP,
181         FIELD_PAIR_TCBN, FIELD_PAIR_TNBC}, /* prog. */
182
183     {FIELD_PAIR_TPBC, FIELD_PAIR_TCBC,
184         FIELD_PAIR_TCBN, FIELD_PAIR_TNBN}, /* TFF ABC */
185     {FIELD_PAIR_TPBP, FIELD_PAIR_TPBC,
186         FIELD_PAIR_TCBC, FIELD_PAIR_TCBN}, /* TFF BCD */
187     {FIELD_PAIR_TPBP, FIELD_PAIR_TPBC,
188         FIELD_PAIR_TCBN, FIELD_PAIR_TNBC}, /* TFF CDE */
189     {FIELD_PAIR_TPBC, FIELD_PAIR_TCBP,
190         FIELD_PAIR_TCBN, FIELD_PAIR_TNBN}, /* TFF EAB */
191
192     {FIELD_PAIR_TCBP, FIELD_PAIR_TCBC,
193         FIELD_PAIR_TNBC, FIELD_PAIR_TNBN}, /* BFF ABC */
194     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBP,
195         FIELD_PAIR_TCBC, FIELD_PAIR_TNBC}, /* BFF BCD */
196     {FIELD_PAIR_TPBP, FIELD_PAIR_TCBP,
197         FIELD_PAIR_TNBC, FIELD_PAIR_TCBN}, /* BFF CDE */
198     {FIELD_PAIR_TCBP, FIELD_PAIR_TPBC,
199         FIELD_PAIR_TNBC, FIELD_PAIR_TNBN}, /* BFF EAB */
200 };
201
202 /**
203  * Table for extracting the i_cadence_pos part of detected cadence position
204  * (ivtc_cadence_pos).
205  *
206  * The counter goes from 0 to 4, where "abc" = 0, "bcd" = 1, ...
207  *
208  * @see ivtc_cadence_pos
209  */
210 static const int pi_detected_pos_to_cadence_pos[NUM_CADENCE_POS] = {
211     3, /* prog. */
212     0, /* TFF ABC */
213     1, /* TFF BCD */
214     2, /* TFF CDE */
215     4, /* TFF EAB */
216     0, /* BFF ABC */
217     1, /* BFF BCD */
218     2, /* BFF CDE */
219     4, /* BFF EAB */
220 };
221
222 /**
223  * Table for extracting the telecine field dominance part of detected
224  * cadence position (ivtc_cadence_pos).
225  *
226  * The position "dea" does not provide TFF/BFF information, because it is
227  * indistinguishable from progressive.
228  *
229  * @see ivtc_cadence_pos
230  */
231 static const int pi_detected_pos_to_tfd[NUM_CADENCE_POS] = {
232     TFD_INVALID, /* prog. */
233     TFD_TFF, /* TFF ABC */
234     TFD_TFF, /* TFF BCD */
235     TFD_TFF, /* TFF CDE */
236     TFD_TFF, /* TFF EAB */
237     TFD_BFF, /* BFF ABC */
238     TFD_BFF, /* BFF BCD */
239     TFD_BFF, /* BFF CDE */
240     TFD_BFF, /* BFF EAB */
241 };
242
243 /* Valid telecine sequences (TFF and BFF). Indices: [TFD][i_cadence_pos] */
244 /* Currently unused and left here for documentation only.
245    There is an easier way - just decode the i_cadence_pos part of the
246    detected position using the pi_detected_pos_to_cadence_pos table,
247    and check that it is successive mod 5. See IVTCCadenceAnalyze(). */
248 /*static const int pi_valid_cadences[2][5] = { {CADENCE_POS_TFF_ABC,
249                                              CADENCE_POS_TFF_BCD,
250                                              CADENCE_POS_TFF_CDE,
251                                              CADENCE_POS_PROGRESSIVE,
252                                              CADENCE_POS_TFF_EAB},
253
254                                              {CADENCE_POS_BFF_ABC,
255                                              CADENCE_POS_BFF_BCD,
256                                              CADENCE_POS_BFF_CDE,
257                                              CADENCE_POS_PROGRESSIVE,
258                                              CADENCE_POS_BFF_EAB},
259                                            };
260 */
261
262 /**
263  * Operations needed in film frame reconstruction.
264  */
265 typedef enum { IVTC_OP_DROP_FRAME,
266                IVTC_OP_COPY_N,
267                IVTC_OP_COPY_C,
268                IVTC_OP_COMPOSE_TNBC,
269                IVTC_OP_COMPOSE_TCBN } ivtc_op;
270
271 /* Note: During hard IVTC, we must avoid COPY_C and do a compose instead.
272    If we COPY_C, some subtitles will flicker badly, even if we use the
273    cadence-based film frame reconstruction. Try the first scene in
274    Kanon (2006) vol. 3 to see the problem.
275
276    COPY_C can be used without problems when it is used consistently
277    (not constantly mixed in with COPY_N and compose operations),
278    for example in soft IVTC.
279 */
280 /**
281  * Operation table for film frame reconstruction depending on cadence position.
282  * Indices: [TFD][i_cadence_pos]
283  * @see pi_detected_pos_to_tfd
284  * @see pi_detected_pos_to_cadence_pos
285  */
286 static const ivtc_op pi_reconstruction_ops[2][5] = { /* TFF */
287                                                      {IVTC_OP_COMPOSE_TNBC,
288                                                       IVTC_OP_COPY_N,
289                                                       IVTC_OP_COPY_N,
290                                                       IVTC_OP_DROP_FRAME,
291                                                       IVTC_OP_COMPOSE_TNBC},
292
293                                                      /* BFF */
294                                                      {IVTC_OP_COMPOSE_TCBN,
295                                                       IVTC_OP_COPY_N,
296                                                       IVTC_OP_COPY_N,
297                                                       IVTC_OP_DROP_FRAME,
298                                                       IVTC_OP_COMPOSE_TCBN},
299                                                    };
300
301 /**
302  * Timestamp mangling table.
303  *
304  * This is used in the 29.97 -> 23.976 fps conversion.
305  *
306  * Index: i_cadence_pos, 0..4.
307  *
308  * Valid values are nonnegative. The -1 corresponds to the dropped frame
309  * and is never used, except for a debug assert.
310  *
311  * The unit of the values is 1/4 of frame duration.
312  * See the function documentation of RenderIVTC() for an explanation.
313  * @see ivtc_cadence_pos
314  * @see pi_detected_pos_to_cadence_pos
315  * @see pi_reconstruction_ops
316  * @see RenderIVTC()
317  */
318 static const int pi_timestamp_deltas[5] = { 1, 2, 3, -1, 0 };
319
320 /*****************************************************************************
321  * Internal functions
322  *****************************************************************************/
323
324 /**
325  * Internal helper function for RenderIVTC(): performs initialization
326  * at the start of a new frame.
327  *
328  * In practice, this slides detector histories.
329  *
330  * This function should only perform initialization that does NOT require
331  * the input frame history buffer. This runs at every frame, including
332  * the first two.
333  *
334  * This is an internal function only used by RenderIVTC().
335  * There is no need to call this function manually.
336  *
337  * @param p_filter The filter instance.
338  * @see RenderIVTC()
339  */
340 static void IVTCFrameInit( filter_t *p_filter )
341 {
342     assert( p_filter != NULL );
343
344     filter_sys_t *p_sys = p_filter->p_sys;
345     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
346
347     /* Slide detector histories */
348     for( int i = 1; i < IVTC_DETECTION_HISTORY_SIZE; i++ )
349     {
350         p_ivtc->pi_top_rep[i-1] = p_ivtc->pi_top_rep[i];
351         p_ivtc->pi_bot_rep[i-1] = p_ivtc->pi_bot_rep[i];
352         p_ivtc->pi_motion[i-1]  = p_ivtc->pi_motion[i];
353
354         p_ivtc->pi_s_cadence_pos[i-1] = p_ivtc->pi_s_cadence_pos[i];
355         p_ivtc->pb_s_reliable[i-1]    = p_ivtc->pb_s_reliable[i];
356         p_ivtc->pi_v_cadence_pos[i-1] = p_ivtc->pi_v_cadence_pos[i];
357         p_ivtc->pi_v_raw[i-1]         = p_ivtc->pi_v_raw[i];
358         p_ivtc->pb_v_reliable[i-1]    = p_ivtc->pb_v_reliable[i];
359
360         p_ivtc->pi_cadence_pos_history[i-1]
361                                       = p_ivtc->pi_cadence_pos_history[i];
362
363         p_ivtc->pb_all_progressives[i-1] = p_ivtc->pb_all_progressives[i];
364     }
365     /* The latest position has not been detected yet. */
366     p_ivtc->pi_s_cadence_pos[IVTC_LATEST] = CADENCE_POS_INVALID;
367     p_ivtc->pb_s_reliable[IVTC_LATEST]    = false;
368     p_ivtc->pi_v_cadence_pos[IVTC_LATEST] = CADENCE_POS_INVALID;
369     p_ivtc->pi_v_raw[IVTC_LATEST]         = VEKTOR_CADENCE_POS_ALL;
370     p_ivtc->pb_v_reliable[IVTC_LATEST]    = false;
371     p_ivtc->pi_cadence_pos_history[IVTC_LATEST] = CADENCE_POS_INVALID;
372     p_ivtc->pi_top_rep[IVTC_LATEST] =  0;
373     p_ivtc->pi_bot_rep[IVTC_LATEST] =  0;
374     p_ivtc->pi_motion[IVTC_LATEST]  = -1;
375     p_ivtc->pb_all_progressives[IVTC_LATEST] = false;
376
377     /* Slide history of field pair interlace scores */
378     p_ivtc->pi_scores[FIELD_PAIR_TPBP] = p_ivtc->pi_scores[FIELD_PAIR_TCBC];
379     p_ivtc->pi_scores[FIELD_PAIR_TPBC] = p_ivtc->pi_scores[FIELD_PAIR_TCBN];
380     p_ivtc->pi_scores[FIELD_PAIR_TCBP] = p_ivtc->pi_scores[FIELD_PAIR_TNBC];
381     p_ivtc->pi_scores[FIELD_PAIR_TCBC] = p_ivtc->pi_scores[FIELD_PAIR_TNBN];
382     /* These have not been detected yet */
383     p_ivtc->pi_scores[FIELD_PAIR_TCBN] = 0;
384     p_ivtc->pi_scores[FIELD_PAIR_TNBC] = 0;
385     p_ivtc->pi_scores[FIELD_PAIR_TNBN] = 0;
386 }
387
388 /**
389  * Internal helper function for RenderIVTC(): computes various raw detector
390  * data at the start of a new frame.
391  *
392  * This function requires the input frame history buffer.
393  * IVTCFrameInit() must have been called first.
394  * Last two frames must be available in the history buffer.
395  *
396  * This is an internal function only used by RenderIVTC().
397  * There is no need to call this function manually.
398  *
399  * @param p_filter The filter instance.
400  * @see RenderIVTC()
401  * @see IVTCFrameInit()
402  */
403 static void IVTCLowLevelDetect( filter_t *p_filter )
404 {
405     assert( p_filter != NULL );
406
407     filter_sys_t *p_sys = p_filter->p_sys;
408     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
409     picture_t *p_curr = p_sys->pp_history[1];
410     picture_t *p_next = p_sys->pp_history[2];
411
412     assert( p_next != NULL );
413     assert( p_curr != NULL );
414
415     /* Compute interlace scores for TNBN, TNBC and TCBN.
416         Note that p_next contains TNBN. */
417     p_ivtc->pi_scores[FIELD_PAIR_TNBN] = CalculateInterlaceScore( p_next,
418                                                                   p_next );
419     p_ivtc->pi_scores[FIELD_PAIR_TNBC] = CalculateInterlaceScore( p_next,
420                                                                   p_curr );
421     p_ivtc->pi_scores[FIELD_PAIR_TCBN] = CalculateInterlaceScore( p_curr,
422                                                                   p_next );
423
424     int i_top = 0, i_bot = 0;
425     int i_motion = EstimateNumBlocksWithMotion(p_curr, p_next, &i_top, &i_bot);
426     p_ivtc->pi_motion[IVTC_LATEST] = i_motion;
427
428     /* If one field changes "clearly more" than the other, we know the
429        less changed one is a likely duplicate.
430
431        Threshold 1/2 is too low for some scenes (e.g. pan of the space junk
432        at beginning of The Third ep. 1, right after the OP). Thus, we use 2/3,
433        which seems to work.
434     */
435     p_ivtc->pi_top_rep[IVTC_LATEST] = (i_top <= 2*i_bot/3);
436     p_ivtc->pi_bot_rep[IVTC_LATEST] = (i_bot <= 2*i_top/3);
437 }
438
439 /**
440  * Internal helper function for RenderIVTC(): using raw detector data,
441  * detect cadence position by an interlace scores based algorithm ("scores").
442  *
443  * IVTCFrameInit() and IVTCLowLevelDetect() must have been called first.
444  * Last frame must be available in the history buffer.
445  *
446  * This is an internal function only used by RenderIVTC().
447  * There is no need to call this function manually.
448  *
449  * @param p_filter The filter instance.
450  * @see RenderIVTC()
451  * @see IVTCFrameInit()
452  * @see IVTCLowLevelDetect()
453  * @see IVTCCadenceDetectFinalize()
454  */
455 static void IVTCCadenceDetectAlgoScores( filter_t *p_filter )
456 {
457     assert( p_filter != NULL );
458
459     filter_sys_t *p_sys = p_filter->p_sys;
460     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
461     picture_t *p_next = p_sys->pp_history[2];
462
463     assert( p_next != NULL );
464
465     /* Detect likely cadence position according to the tables,
466        using the tabulated combinations of all 7 available interlace scores.
467     */
468     int pi_ivtc_scores[NUM_CADENCE_POS];
469     for( int i = 0; i < NUM_CADENCE_POS; i++ )
470         pi_ivtc_scores[i] = p_ivtc->pi_scores[ pi_best_field_pairs[i][0] ]
471                           + p_ivtc->pi_scores[ pi_best_field_pairs[i][1] ]
472                           + p_ivtc->pi_scores[ pi_best_field_pairs[i][2] ];
473     /* Find minimum */
474     int j = CADENCE_POS_PROGRESSIVE; /* valid regardless of TFD */
475     int minscore = pi_ivtc_scores[j];
476     /* A TFF (respectively BFF) stream may only have TFF (respectively BFF)
477        telecine. Don't bother looking at the wrong table. */
478     int imin = CADENCE_POS_TFF_FIRST; /* first TFF-only entry */
479     int iend = CADENCE_POS_TFF_END;   /* one past last TFF-only entry */
480     if( !p_next->b_top_field_first )
481     {
482         imin = CADENCE_POS_BFF_FIRST; /* first BFF-only entry */
483         iend = CADENCE_POS_BFF_END;   /* one past last BFF-only entry */
484     }
485     for( int i = imin; i < iend; i++ )
486     {
487         if( pi_ivtc_scores[i] < minscore )
488         {
489             minscore = pi_ivtc_scores[i];
490             j = i;
491         }
492     }
493
494     /* Now "j" contains the most likely position according to the tables,
495        accounting also for video TFF/BFF. */
496     p_ivtc->pi_s_cadence_pos[IVTC_LATEST] = j;
497
498     /* Estimate reliability of detector result.
499
500        We do this by checking if the winner is an outlier at least
501        to some extent. For anyone better versed in statistics,
502        feel free to improve this.
503     */
504
505     /* Compute sample mean with the winner included and without.
506
507        Sample mean is defined as mu = sum( x_i, i ) / N ,
508        where N is the number of samples.
509     */
510     int mean = pi_ivtc_scores[CADENCE_POS_PROGRESSIVE];
511     int mean_except_min = 0;
512     if( j != CADENCE_POS_PROGRESSIVE )
513         mean_except_min = pi_ivtc_scores[CADENCE_POS_PROGRESSIVE];
514     for( int i = imin; i < iend; i++ )
515     {
516         mean += pi_ivtc_scores[i];
517         if( i != j )
518             mean_except_min += pi_ivtc_scores[i];
519     }
520     /* iend points one past end, but progressive counts as the +1. */
521     mean /= (iend - imin + 1);
522     mean_except_min /= (iend - imin);
523
524     /* Check how much excluding the winner changes the mean. */
525     double mean_ratio = (double)mean_except_min / (double)mean;
526
527     /* Let's pretend that the detected position is a stochastic variable.
528        Compute sample variance with the winner included and without.
529
530        var = sum( (x_i - mu)^2, i ) / N ,
531
532        where mu is the sample mean.
533
534        Note that we really need int64_t; the numbers are pretty large.
535     */
536     int64_t diff = (int64_t)(pi_ivtc_scores[CADENCE_POS_PROGRESSIVE] - mean);
537     int64_t var = diff*diff;
538     int64_t var_except_min = 0;
539     if( j != CADENCE_POS_PROGRESSIVE )
540     {
541         int64_t diff_exm = (int64_t)(pi_ivtc_scores[CADENCE_POS_PROGRESSIVE]
542                                       - mean_except_min);
543         var_except_min = diff_exm*diff_exm;
544     }
545     for( int i = imin; i < iend; i++ )
546     {
547         diff = (int64_t)(pi_ivtc_scores[i] - mean);
548         var += (diff*diff);
549         if( i != j )
550         {
551             int64_t diff_exm = (int64_t)(pi_ivtc_scores[i] - mean_except_min);
552             var_except_min += (diff_exm*diff_exm);
553         }
554     }
555     /* iend points one past end, but progressive counts as the +1. */
556     var /= (uint64_t)(iend - imin + 1);
557     var_except_min /= (uint64_t)(iend - imin);
558
559     /* Extract cadence counter part of detected positions for the
560        last two frames.
561
562        Note that for the previous frame, we use the final detected cadence
563        position, which was not necessarily produced by this algorithm.
564        It is the result that was judged the most reliable.
565     */
566     int j_curr = p_ivtc->pi_cadence_pos_history[IVTC_LATEST-1];
567     int pos_next = pi_detected_pos_to_cadence_pos[j];
568
569     /* Be optimistic when unsure. We bias the detection toward accepting
570        the next "correct" position, even if the variance check comes up bad.
571     */
572     bool b_expected = false;
573     if( j_curr != CADENCE_POS_INVALID )
574     {
575         int pos_curr = pi_detected_pos_to_cadence_pos[j_curr];
576         b_expected = (pos_next == (pos_curr + 1) % 5);
577     }
578
579     /* Use motion detect result as a final sanity check.
580        If no motion, the result from this algorithm cannot be reliable.
581     */
582     int i_blocks_with_motion = p_ivtc->pi_motion[IVTC_LATEST];
583
584     /* The numbers given here are empirical constants that have been tuned
585        through trial and error. The test material used was NTSC anime DVDs.
586
587         Easy-to-detect parts seem to give variance boosts of 40-70%, but
588         hard-to-detect parts sometimes only 18%. Anything with a smaller boost
589         in variance doesn't seem reliable for catching a new lock-on,
590
591         Additionally, it seems that if the mean changes by less than 0.5%,
592         the result is not reliable.
593
594         Note that the numbers given are only valid for the pi_best_field_pairs
595         detector strategy.
596
597         For motion detection, the detector seems good enough so that
598         we can threshold at zero.
599     */
600     bool b_result_reliable =
601       ( i_blocks_with_motion > 0      &&
602         mean_ratio           > 1.005  &&
603         ( b_expected || ( (double)var > 1.17*(double)var_except_min ) )
604       );
605     p_ivtc->pb_s_reliable[IVTC_LATEST] = b_result_reliable;
606 }
607
608 /**
609  * Internal helper function for RenderIVTC(): using raw detector data,
610  * detect cadence position by a hard field repeat based algorithm ("vektor").
611  *
612  * This algorithm is inspired by the classic TVTime/Xine IVTC filter
613  * by Billy Biggs (Vektor); hence the name. There are however some
614  * differences between this and the TVTime/Xine filter.
615  *
616  * IVTCFrameInit() and IVTCLowLevelDetect() must have been called first.
617  * Last frame must be available in the history buffer.
618  *
619  * This is an internal function only used by RenderIVTC().
620  * There is no need to call this function manually.
621  *
622  * @param p_filter The filter instance.
623  * @see RenderIVTC()
624  * @see IVTCFrameInit()
625  * @see IVTCLowLevelDetect()
626  * @see IVTCCadenceDetectFinalize()
627  */
628 static void IVTCCadenceDetectAlgoVektor( filter_t *p_filter )
629 {
630     assert( p_filter != NULL );
631
632     filter_sys_t *p_sys = p_filter->p_sys;
633     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
634
635     picture_t *p_next = p_sys->pp_history[2];
636
637     assert( p_next != NULL );
638
639     /* This algorithm is based on detecting hard-repeated fields (by motion
640        detection), and conservatively estimating what the seen repeats could
641        mean for the cadence position.
642
643        "Conservative" means that we do not rule out possibilities if repeats
644        are *not* seen, but only *add* possibilities based on what repeats
645        *are* seen. This is important. Otherwise full-frame repeats in the
646        original film (8fps or 12fps animation is very common in anime),
647        causing spurious field repeats, would mess up the detection.
648        With this strategy, spurious repeats will only slow down the lock-on,
649        and will not break an existing lock-on once acquired.
650
651        Several possibilities are kept open until the sequence gives enough
652        information to make a unique detection. When the sequence becomes
653        inconsistent (e.g. bad cut), the detector resets itself.
654
655        The main ideas taken from the TVTime/Xine algorithm are:
656         1) Conservatively using information from detected field repeats,
657         2) Cadence counting the earlier detection results and combining with
658            the new detection result, and
659         3) The observation that video TFF/BFF uniquely determines TFD.
660
661        The main differences are
662         1) Different motion detection (see EstimateNumBlocksWithMotion()).
663            Vektor's original estimates the average top/bottom field diff
664            over the last 3 frames, while ours uses a block-based approach
665            for diffing and just compares the field diffs between "curr" and
666            "next" against each other (see IVTCLowLevelDetect()).
667            Both approaches are adaptive, but in a different way.
668         2) The specific detection logic used is a bit different (see both
669            codes for details; the original is in xine-lib, function
670            determine_pulldown_offset_short_history_new() in pulldown.c;
671            ours is obviously given below). I think the one given here
672            is a bit simpler.
673
674        Note that we don't have to worry about getting a detection in all cases.
675        It's enough if we work reliably, say, 99% of the time, and the other 1%
676        of the time just admit that we don't know the cadence position.
677        (This mostly happens after a bad cut, when the new scene has
678        "difficult" motion characteristics, such as repeated film frames.)
679        Our frame composer is built to handle also cases where we have no
680        reliable detection of the cadence position; see IVTCOutputOrDropFrame().
681        More important is to never lock on incorrectly, as this would both
682        generate interlacing artifacts where none existed, and cause motion
683        to stutter (because duplicate frames would be shown and unique ones
684        dropped).
685     */
686
687     /* Progressive requires no repeats, so it is always a possibility.
688        Filtering will drop it out if we know that the current position
689        cannot be "dea".
690     */
691     int detected = 0;
692     detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_PROGRESSIVE ];
693
694     /* Add in other possibilities depending on field repeats seen during the
695        last three input frames (i.e. two transitions between input frames).
696        See the "Dups." column in the cadence tables.
697     */
698     bool b_top_rep     = p_ivtc->pi_top_rep[IVTC_LATEST];
699     bool b_bot_rep     = p_ivtc->pi_bot_rep[IVTC_LATEST];
700     bool b_old_top_rep = p_ivtc->pi_top_rep[IVTC_LATEST-1];
701     bool b_old_bot_rep = p_ivtc->pi_bot_rep[IVTC_LATEST-1];
702     if( b_top_rep )
703     {
704         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_TFF_EAB ];
705         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_BFF_BCD ];
706     }
707     if( b_old_top_rep )
708     {
709         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_TFF_ABC ];
710         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_BFF_CDE ];
711     }
712     if( b_bot_rep )
713     {
714         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_TFF_BCD ];
715         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_BFF_EAB ];
716     }
717     if( b_old_bot_rep )
718     {
719         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_TFF_CDE ];
720         detected |= pi_detected_pos_to_bitmask[ CADENCE_POS_BFF_ABC ];
721     }
722
723     /* A TFF stream may only have TFF telecine, and similarly for BFF.
724        Discard the possibility we know to be incorrect for this stream.
725        (The stream may flipflop between the possibilities if it contains
726         soft-telecined sequences or lone field repeats, so we must keep
727         detecting this for each incoming frame.)
728     */
729     bool b_tff = p_next->b_top_field_first;
730     if( b_tff )
731         detected &= VEKTOR_CADENCE_POS_TFF;
732     else
733         detected &= VEKTOR_CADENCE_POS_BFF;
734
735     /* Predict possible next positions based on our last detection.
736        Begin with a shift and carry. */
737     int predicted = p_ivtc->pi_v_raw[IVTC_LATEST-1];
738     bool b_wrap_tff = false;
739     bool b_wrap_bff = false;
740     if( predicted & VEKTOR_CADENCE_POS_TFF_HIGH )
741         b_wrap_tff = true;
742     if( predicted & VEKTOR_CADENCE_POS_BFF_HIGH )
743         b_wrap_bff = true;
744     /* bump to next position and keep only valid bits */
745     predicted = (predicted << 1) & VEKTOR_CADENCE_POS_ALL;
746     /* carry */
747     if( b_wrap_tff )
748         predicted |= VEKTOR_CADENCE_POS_TFF_LOW;
749     if( b_wrap_bff )
750         predicted |= VEKTOR_CADENCE_POS_BFF_LOW;
751
752     /* Filter: narrow down possibilities based on previous detection,
753        if consistent. If not consistent, reset the detector.
754        Reset works better than just using the latest raw detection.
755     */
756     if( (detected & predicted) != 0 )
757         detected = detected & predicted;
758     else
759         detected = VEKTOR_CADENCE_POS_ALL;
760
761     /* We're done. Save result to our internal storage so we can use it
762        for prediction at the next frame.
763
764        Note that the outgoing frame check in IVTCOutputOrDropFrame()
765        has a veto right, resetting our state if it determines that
766        the cadence has become broken.
767     */
768     p_ivtc->pi_v_raw[IVTC_LATEST] = detected;
769
770     /* See if the position has been detected uniquely.
771        If so, we have acquired a lock-on. */
772     ivtc_cadence_pos exact = CADENCE_POS_INVALID;
773     if( detected != 0 )
774     {
775         for( int i = 0; i < NUM_CADENCE_POS; i++ )
776         {
777             /* Note that we must use "&" instead of just equality to catch
778                the progressive case, and also not to trigger on an incomplete
779                detection. */
780             if( detected == (detected & pi_detected_pos_to_bitmask[i]) )
781             {
782                 exact = i;
783                 break;
784             }
785         }
786     }
787
788     /* If the result was unique, now "exact" contains the detected
789        cadence position (and otherwise CADENCE_POS_INVALID).
790
791        In practice, if the result from this algorithm is unique,
792        it is always reliable.
793     */
794     p_ivtc->pi_v_cadence_pos[IVTC_LATEST] =  exact;
795     p_ivtc->pb_v_reliable[IVTC_LATEST]    = (exact != CADENCE_POS_INVALID);
796 }
797
798 /**
799  * Internal helper function for RenderIVTC(): decide the final detected
800  * cadence position for the current position of the PCN stencil,
801  * using the results of the different cadence detection algorithms.
802  *
803  * Must be called after all IVTCCadenceDetectAlgo*() functions.
804  *
805  * This is an internal function only used by RenderIVTC().
806  * There is no need to call this function manually.
807  *
808  * @param p_filter The filter instance.
809  * @see RenderIVTC()
810  * @see IVTCCadenceDetectAlgoScores()
811  * @see IVTCCadenceDetectAlgoVektor()
812  */
813 static void IVTCCadenceDetectFinalize( filter_t *p_filter )
814 {
815     assert( p_filter != NULL );
816
817     filter_sys_t *p_sys = p_filter->p_sys;
818     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
819
820     /* In practice "vektor" is more reliable than "scores", but it may
821        take longer to lock on. Thus, we prefer "vektor" if its reliable bit
822        is set, then "scores", and finally just give up.
823
824        For progressive sequences, "vektor" outputs "3, -, 3, -, ...",
825        because the repeated progressive position is an inconsistent prediction.
826        In this case, "scores" fills in the blanks. (This particular task
827        could also be done without another cadence detector, by just
828        detecting the alternating pattern of "3" and no result.)
829     */
830     int pos = CADENCE_POS_INVALID;
831     if( p_ivtc->pb_v_reliable[IVTC_LATEST] )
832         pos = p_ivtc->pi_v_cadence_pos[IVTC_LATEST];
833     else if( p_ivtc->pb_s_reliable[IVTC_LATEST] )
834         pos = p_ivtc->pi_s_cadence_pos[IVTC_LATEST];
835     p_ivtc->pi_cadence_pos_history[IVTC_LATEST] = pos;
836 }
837
838 /**
839  * Internal helper function for RenderIVTC(): using stream flags,
840  * detect soft telecine.
841  *
842  * This function is different from the other detectors; it may enter or exit
843  * IVTC_MODE_TELECINED_NTSC_SOFT, if it detects that soft telecine has just
844  * been entered or exited.
845  *
846  * Upon exit from soft telecine, the filter will resume operation in its
847  * previous mode (which it had when soft telecine was entered).
848  *
849  * Last three frames must be available in the history buffer.
850  *
851  * This is an internal function only used by RenderIVTC().
852  * There is no need to call this function manually.
853  *
854  * @param p_filter The filter instance.
855  * @see RenderIVTC()
856  */
857 static void IVTCSoftTelecineDetect( filter_t *p_filter )
858 {
859     assert( p_filter != NULL );
860
861     filter_sys_t *p_sys = p_filter->p_sys;
862     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
863     picture_t *p_prev = p_sys->pp_history[0];
864     picture_t *p_curr = p_sys->pp_history[1];
865     picture_t *p_next = p_sys->pp_history[2];
866
867     assert( p_next != NULL );
868     assert( p_curr != NULL );
869     assert( p_prev != NULL );
870
871     /* Soft telecine can be detected from the flag pattern:
872        nb_fields = 3,2,3,2,... and *video* TFF = true, false, false, true
873        (TFF telecine) or false, true, true, false (BFF telecine).
874
875        We don't particularly care which field goes first, because in soft TC
876        we're working with progressive frames. And in any case, the video FDs
877        of successive frames must match any field repeats in order for field
878        renderers (such as traditional DVD player + CRT TV) to work correctly.
879        Thus the video TFF/BFF flag provides no additional useful information
880        for us on top of checking nb_fields.
881
882        The only thing to *do* to soft telecine in an IVTC filter is to even
883        out the outgoing PTS diffs to 2.5 fields each, so that we get
884        a steady 24fps output. Thus, we can do this processing even if it turns
885        out that we saw a lone field repeat (which are also sometimes used,
886        such as in the Silent Mobius OP and in Sol Bianca). We can be aggressive
887        and don't need to care about false positives - as long as we are equally
888        aggressive about dropping out of soft telecine mode the moment a "2" is
889        followed by another "2" and not a "3" as in soft TC.
890
891        Finally, we conclude that the one-frame future buffer is enough for us
892        to make soft TC decisions just in time for rendering the frame in the
893        "current" position. The flag patterns given below constitute proof
894        of this property.
895
896        Soft telecine is relatively rare at least in anime, but it exists;
897        e.g. Angel Links OP, Silent Mobius, and Stellvia of the Universe have
898        sequences that are soft telecined. Stellvia, especially, alternates
899        between soft and hard telecine all the time.
900     */
901
902     /* Valid stream flag patterns for soft telecine. There are three: */
903
904     /* Entering soft telecine at frame curr, or running inside it already */
905     bool b_soft_telecine_1 = (p_prev->i_nb_fields == 2) &&
906                              (p_curr->i_nb_fields == 3) &&
907                              (p_next->i_nb_fields == 2);
908     /* Running inside soft telecine */
909     bool b_soft_telecine_2 = (p_prev->i_nb_fields == 3) &&
910                              (p_curr->i_nb_fields == 2) &&
911                              (p_next->i_nb_fields == 3);
912     /* Exiting soft telecine at frame curr (curr is the last frame
913        that should be handled as soft TC) */
914     bool b_soft_telecine_3 = (p_prev->i_nb_fields == 3) &&
915                              (p_curr->i_nb_fields == 2) &&
916                              (p_next->i_nb_fields == 2);
917
918     /* Soft telecine is very clear-cut - the moment we see or do not see
919        a valid flag pattern, we can change the filter mode.
920     */
921     if( b_soft_telecine_1 || b_soft_telecine_2 || b_soft_telecine_3 )
922     {
923         if( p_ivtc->i_mode != IVTC_MODE_TELECINED_NTSC_SOFT )
924         {
925             msg_Dbg( p_filter, "IVTC: 3:2 pulldown: NTSC soft telecine "\
926                                "detected." );
927             p_ivtc->i_old_mode = p_ivtc->i_mode;
928         }
929
930         /* Valid flag pattern seen, this frame is soft telecined */
931         p_ivtc->i_mode = IVTC_MODE_TELECINED_NTSC_SOFT;
932
933         /* Only used during IVTC'ing hard telecine. */
934         p_ivtc->i_cadence_pos = CADENCE_POS_INVALID;
935         p_ivtc->i_tfd         = TFD_INVALID;
936     }
937     /* Note: no flag pattern match now */
938     else if( p_ivtc->i_mode == IVTC_MODE_TELECINED_NTSC_SOFT )
939     {
940         msg_Dbg( p_filter, "IVTC: 3:2 pulldown: NTSC soft telecine ended. "\
941                            "Returning to previous mode." );
942
943         /* No longer soft telecined, return filter to the mode it had earlier.
944            This is needed to fix cases where we came in from hard telecine, and
945            should go back, but can't catch a cadence in time before telecined
946            frames slip through. Kickstarting back to hard IVTC, using the
947            emergency frame composer until the cadence locks on again,
948            fixes the problem. This happens a lot in Stellvia.
949         */
950         p_ivtc->i_mode = p_ivtc->i_old_mode;
951         p_ivtc->i_cadence_pos = 0; /* Wild guess. The film frame reconstruction
952                                       will start in emergency mode, and this
953                                       will be filled in by the detector ASAP.*/
954         /* I suppose video field dominance no longer flipflops. */
955         p_ivtc->i_tfd = !p_next->b_top_field_first; /* tff  <=>  TFD == 0 */
956     }
957 }
958
959 /**
960  * Internal helper function for RenderIVTC(): using the history of detected
961  * cadence positions, analyze the cadence and enter or exit
962  * IVTC_MODE_TELECINED_NTSC_HARD when appropriate.
963  *
964  * This also updates b_sequence_valid.
965  *
966  * Last three frames must be available in the history buffer.
967  *
968  * This is an internal function only used by RenderIVTC().
969  * There is no need to call this function manually.
970  *
971  * @param p_filter The filter instance.
972  * @see RenderIVTC()
973  */
974 static void IVTCCadenceAnalyze( filter_t *p_filter )
975 {
976     assert( p_filter != NULL );
977
978     filter_sys_t *p_sys = p_filter->p_sys;
979     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
980     picture_t *p_prev = p_sys->pp_history[0];
981     picture_t *p_curr = p_sys->pp_history[1];
982     picture_t *p_next = p_sys->pp_history[2];
983
984     assert( p_next != NULL );
985     assert( p_curr != NULL );
986     assert( p_prev != NULL );
987
988     /* Determine which frames in the buffer qualify for analysis.
989
990        Note that hard telecine always has nb_fields = 2 and
991        video TFF = constant (i.e. the stream flags look no different from
992        a true interlaced or true progressive stream). Basically, no one ever
993        sets the progressive frame flag for the input frames d, e, and a -
994        in practice they're all flagged as interlaced.
995
996        A frame may qualify for hard TC analysis if it has no soft field repeat
997        (i.e. it cannot be part of a soft telecine). The condition
998        nb_fields == 2 must always match.
999
1000        Additionally, curr and next must have had motion with respect to the
1001        previous frame, to ensure that the different field combinations have
1002        produced unique pictures.
1003
1004        Alternatively, if there was no motion, but the cadence position was
1005        reliably detected and it was the expected one, we qualify the frame
1006        for analysis (mainly, for TFD voting).
1007
1008        We only proceed with the cadence analysis if all three frames
1009        in the buffer qualify.
1010     */
1011
1012     /* Note that these are the final detected positions
1013        produced by IVTCCadenceDetectFinalize(). */
1014     int j_next = p_ivtc->pi_cadence_pos_history[IVTC_LATEST];
1015     int j_curr = p_ivtc->pi_cadence_pos_history[IVTC_LATEST-1];
1016     int j_prev = p_ivtc->pi_cadence_pos_history[IVTC_LATEST-2];
1017
1018     bool b_expected = false;
1019     if( j_next != CADENCE_POS_INVALID  &&  j_curr != CADENCE_POS_INVALID )
1020     {
1021         int pos_next = pi_detected_pos_to_cadence_pos[j_next];
1022         int pos_curr = pi_detected_pos_to_cadence_pos[j_curr];
1023         b_expected = (pos_next == (pos_curr + 1) % 5);
1024     }
1025     bool b_old_expected  = false;
1026     if( j_curr != CADENCE_POS_INVALID  &&  j_prev != CADENCE_POS_INVALID )
1027     {
1028         int pos_curr = pi_detected_pos_to_cadence_pos[j_curr];
1029         int pos_prev = pi_detected_pos_to_cadence_pos[j_prev];
1030         b_old_expected = (pos_curr == (pos_prev + 1) % 5);
1031     }
1032
1033     int i_motion     = p_ivtc->pi_motion[IVTC_LATEST];
1034     int i_old_motion = p_ivtc->pi_motion[IVTC_LATEST-1];
1035
1036     bool b_prev_valid  = (p_prev->i_nb_fields == 2);
1037     bool b_curr_valid  = (p_curr->i_nb_fields == 2)  &&
1038                          (i_old_motion > 0  ||  b_old_expected);
1039     bool b_next_valid  = (p_next->i_nb_fields == 2)  &&
1040                          (i_motion > 0      ||  b_expected);
1041     bool b_no_invalids = (b_prev_valid && b_curr_valid && b_next_valid);
1042
1043     /* Final sanity check: see that the detection history has been
1044        completely filled,  i.e. the latest three positions of the stencil
1045        have given a result from the cadence detector.
1046     */
1047     if( b_no_invalids )
1048     {
1049         for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE; ++i )
1050         {
1051             const int i_detected_pos = p_ivtc->pi_cadence_pos_history[i];
1052             if( i_detected_pos == CADENCE_POS_INVALID )
1053             {
1054                 b_no_invalids = false;
1055                 break;
1056             }
1057         }
1058     }
1059
1060     /* If still ok, do the analysis. */
1061     p_ivtc->b_sequence_valid = false; /* needed in frame reconstruction */
1062     if( b_no_invalids )
1063     {
1064         /* Convert the history elements to cadence position and TFD. */
1065         int pi_tfd[IVTC_DETECTION_HISTORY_SIZE];
1066         int pi_pos[IVTC_DETECTION_HISTORY_SIZE];
1067         for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE; ++i )
1068         {
1069             const int i_detected_pos = p_ivtc->pi_cadence_pos_history[i];
1070             pi_pos[i] = pi_detected_pos_to_cadence_pos[i_detected_pos];
1071             pi_tfd[i] = pi_detected_pos_to_tfd[i_detected_pos];
1072         }
1073
1074         /* See if the sequence is valid. The cadence positions must be
1075            successive mod 5. We can't say anything about TFF/BFF yet,
1076            because the progressive-looking position "dea" may be there.
1077            If the sequence otherwise looks valid, we handle that last
1078            by voting.
1079
1080            We also test for a progressive signal here, so that we know
1081            when to exit IVTC_MODE_TELECINED_NTSC_HARD.
1082         */
1083         p_ivtc->b_sequence_valid = true;
1084         bool b_all_progressive = (pi_pos[0] == 3);
1085         int j = pi_pos[0];
1086         for( int i = 1; i < IVTC_DETECTION_HISTORY_SIZE; ++i )
1087         {
1088             if( pi_pos[i] != (++j % 5) )
1089                 p_ivtc->b_sequence_valid = false;
1090             if( pi_pos[i] != 3 )
1091                 b_all_progressive = false;
1092         }
1093         p_ivtc->pb_all_progressives[IVTC_LATEST] = b_all_progressive;
1094
1095         if( p_ivtc->b_sequence_valid )
1096         {
1097             /* Determine TFF/BFF. */
1098             int i_vote_invalid = 0;
1099             int i_vote_tff     = 0;
1100             int i_vote_bff     = 0;
1101             for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE; ++i )
1102             {
1103                 if( pi_tfd[i] == TFD_INVALID )
1104                     i_vote_invalid++;
1105                 else if( pi_tfd[i] == TFD_TFF )
1106                     i_vote_tff++;
1107                 else if( pi_tfd[i] == TFD_BFF )
1108                     i_vote_bff++;
1109             }
1110
1111             /* With three entries, two votes for any one item are enough
1112                to decide this conclusively. */
1113             int i_telecine_field_dominance = TFD_INVALID;
1114             if( i_vote_tff >= 2)
1115                 i_telecine_field_dominance = TFD_TFF;
1116             else if( i_vote_bff >= 2)
1117                 i_telecine_field_dominance = TFD_BFF;
1118             /* In all other cases, "invalid" won or no winner.
1119                This means no NTSC telecine detected. */
1120
1121             /* Lock on to the cadence if it was valid and TFF/BFF was found.
1122
1123                Also, aggressively update the cadence counter from the
1124                lock-on data whenever we can. In practice this has been found
1125                to be a reliable strategy (if the cadence detectors are
1126                good enough).
1127             */
1128             if( i_telecine_field_dominance == TFD_TFF )
1129             {
1130                 if( p_ivtc->i_mode != IVTC_MODE_TELECINED_NTSC_HARD )
1131                     msg_Dbg( p_filter, "IVTC: 3:2 pulldown: NTSC TFF "\
1132                                        "hard telecine detected." );
1133                 p_ivtc->i_mode        = IVTC_MODE_TELECINED_NTSC_HARD;
1134                 p_ivtc->i_cadence_pos = pi_pos[IVTC_LATEST];
1135                 p_ivtc->i_tfd         = TFD_TFF;
1136             }
1137             else if( i_telecine_field_dominance == TFD_BFF )
1138             {
1139                 if( p_ivtc->i_mode != IVTC_MODE_TELECINED_NTSC_HARD )
1140                     msg_Dbg( p_filter, "IVTC: 3:2 pulldown: NTSC BFF "\
1141                                        "hard telecine detected." );
1142                 p_ivtc->i_mode        = IVTC_MODE_TELECINED_NTSC_HARD;
1143                 p_ivtc->i_cadence_pos = pi_pos[IVTC_LATEST];
1144                 p_ivtc->i_tfd         = TFD_BFF;
1145             }
1146         }
1147         /* No telecine... maybe a progressive signal? */
1148         else if( b_all_progressive )
1149         {
1150             /* It seems that in practice, three "3"s in a row can still be
1151                a fluke rather often. Four or five usually are not.
1152                This fixes the Stellvia OP. */
1153
1154             bool b_really_all_progressive = true;
1155             for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE ; i++ )
1156             {
1157                 if( p_ivtc->pb_all_progressives[i] == false )
1158                 {
1159                     b_really_all_progressive = false;
1160                     break;
1161                 }
1162             }
1163
1164             /* If we still think the signal is progressive... */
1165             if( b_really_all_progressive )
1166             {
1167                 /* ...exit film mode immediately. This does not break
1168                    soft TC handling, because for soft TC at least one
1169                    of the frames will not qualify (due to i_nb_fields == 3),
1170                    and in that case this analysis will not run.
1171                 */
1172                 if( p_ivtc->i_mode == IVTC_MODE_TELECINED_NTSC_HARD )
1173                     msg_Dbg( p_filter, "IVTC: 3:2 pulldown: progressive "\
1174                                        "signal detected." );
1175                 p_ivtc->i_mode        = IVTC_MODE_DETECTING;
1176                 p_ivtc->i_cadence_pos = CADENCE_POS_INVALID;
1177                 p_ivtc->i_tfd         = TFD_INVALID;
1178             }
1179         }
1180         /* Final missing "else": no valid NTSC telecine sequence detected.
1181
1182            Either there is no telecine, or the detector - although it produced
1183            results - had trouble finding it. In this case we do nothing,
1184            as it's not a good idea to act on unreliable data.
1185
1186            Note that if we are already in IVTC_MODE_TELECINED_NTSC_HARD, this
1187            case means that we have lost the lock-on, but are still (probably)
1188            in a hard-telecined stream. This will start the emergency mode
1189            for film frame reconstruction. See IVTCOutputOrDropFrame().
1190         */
1191     }
1192 }
1193
1194 /**
1195  * Internal helper function for RenderIVTC(): render or drop frame,
1196  * whichever needs to be done. This also sets the output frame PTS.
1197  *
1198  * Last two frames must be available in the history buffer.
1199  *
1200  * This is an internal function only used by RenderIVTC().
1201  * There is no need to call this function manually.
1202  *
1203  * @param p_filter The filter instance. Must be non-NULL.
1204  * @param[out] p_dst Frame will be rendered here. Must be non-NULL.
1205  * @return Whether a frame was constructed.
1206  * @retval true Yes, output frame is in p_dst.
1207  * @retval false No, this frame was dropped as part of normal IVTC operation.
1208  * @see RenderIVTC()
1209  */
1210 static bool IVTCOutputOrDropFrame( filter_t *p_filter, picture_t *p_dst )
1211 {
1212     assert( p_filter != NULL );
1213     assert( p_dst != NULL );
1214
1215     filter_sys_t *p_sys = p_filter->p_sys;
1216     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
1217     mtime_t t_final = VLC_TS_INVALID; /* for custom timestamp mangling */
1218
1219     picture_t *p_curr = p_sys->pp_history[1];
1220     picture_t *p_next = p_sys->pp_history[2];
1221
1222     assert( p_next != NULL );
1223     assert( p_curr != NULL );
1224
1225     /* Perform IVTC if we're in film mode (either hard or soft telecine).
1226
1227        Note that we don't necessarily have a lock-on, even if we are in
1228        IVTC_MODE_TELECINED_NTSC_HARD. We *may* be locked on, or alternatively,
1229        we have seen a valid cadence some time in the past, but lock-on has
1230        since been lost, and we have not seen a progressive signal after that.
1231        The latter case usually results from bad cuts, which interrupt
1232        the cadence.
1233
1234        Lock-on state is given by p_ivtc->b_sequence_valid.
1235     */
1236     int i_result_score = -1;
1237     int op;
1238     if( p_ivtc->i_mode == IVTC_MODE_TELECINED_NTSC_HARD )
1239     {
1240         /* Decide what to do. The operation table is only enabled
1241            if the cadence seems reliable. Otherwise we use a backup strategy.
1242         */
1243         if( p_ivtc->b_sequence_valid )
1244         {
1245             assert( p_ivtc->i_cadence_pos != CADENCE_POS_INVALID );
1246             assert( p_ivtc->i_tfd != TFD_INVALID );
1247
1248             /* Pick correct operation from the operation table. */
1249             op = pi_reconstruction_ops[p_ivtc->i_tfd][p_ivtc->i_cadence_pos];
1250
1251             if( op == IVTC_OP_DROP_FRAME )
1252             {
1253                 /* Bump cadence counter into the next expected position */
1254                 p_ivtc->i_cadence_pos = ++p_ivtc->i_cadence_pos % 5;
1255
1256                 /* Drop frame. We're done. */
1257                 return false;
1258             }
1259             else
1260             {
1261                 if( op == IVTC_OP_COPY_N )
1262                     i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TNBN];
1263                 else if( op == IVTC_OP_COPY_C )
1264                     i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TCBC];
1265                 else if( op == IVTC_OP_COMPOSE_TNBC )
1266                     i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TNBC];
1267                 else if( op == IVTC_OP_COMPOSE_TCBN )
1268                     i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TCBN];
1269
1270                 /* Sanity check the result */
1271
1272                 /* Compute running mean of outgoing interlace score.
1273                    See below for history mechanism. */
1274                 int i_avg = 0;
1275                 for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE; i++)
1276                     i_avg += p_ivtc->pi_final_scores[i];
1277                 i_avg /= IVTC_DETECTION_HISTORY_SIZE;
1278
1279                 /* Check if the score suddenly became "clearly larger".
1280                    Also, filter out spurious peaks at the low end. */
1281                 if( i_result_score > 1000  &&  i_result_score > 2*i_avg )
1282                 {
1283                     /* Sequence wasn't reliable after all; we'll use
1284                        the Transcode strategy for this frame. */
1285                     p_ivtc->b_sequence_valid = false;
1286                     msg_Dbg( p_filter, "Rejected cadence-based frame "\
1287                                        "construction: interlace score %d "\
1288                                        "(running average %d)",
1289                                        i_result_score, i_avg );
1290
1291                     /* We also reset the detector used in the "vektor"
1292                        algorithm, as it depends on having a reliable previous
1293                        position. In practice, we continue using the Transcode
1294                        strategy until the cadence becomes locked on again.
1295                        (At that point, b_sequence_valid will become true again,
1296                        and we continue with this strategy.)
1297                     */
1298                     p_ivtc->pi_v_raw[IVTC_LATEST] = VEKTOR_CADENCE_POS_ALL;
1299                 }
1300             }
1301         }
1302
1303         /* Frame not dropped, and the cadence counter seems unreliable.
1304
1305             Note that this is not an "else" to the previous case. This may
1306             begin with a valid sequence, and then the above logic decides
1307             that it wasn't valid after all.
1308         */
1309         if( !p_ivtc->b_sequence_valid )
1310         {
1311             /* In this case, we must proceed with no cadence information.
1312                 We use a Transcode-like strategy.
1313
1314                 We check which field paired with TN or BN (accounting for
1315                 the field dominance) gives the smallest interlace score,
1316                 and declare that combination the resulting progressive frame.
1317
1318                 This strategy gives good results on average, but often fails
1319                 in talking scenes in anime. Those can be handled more reliably
1320                 with a locked-on cadence produced by the "vektor" algorithm.
1321             */
1322
1323             int tnbn = p_ivtc->pi_scores[FIELD_PAIR_TNBN]; /* TFF and BFF */
1324             int tnbc = p_ivtc->pi_scores[FIELD_PAIR_TNBC]; /* TFF only */
1325             int tcbn = p_ivtc->pi_scores[FIELD_PAIR_TCBN]; /* BFF only */
1326
1327             if( p_next->b_top_field_first )
1328             {
1329                 if( tnbn <= tnbc )
1330                 {
1331                     op = IVTC_OP_COPY_N;
1332                     i_result_score = tnbn;
1333                 }
1334                 else
1335                 {
1336                     op = IVTC_OP_COMPOSE_TNBC;
1337                     i_result_score = tnbc;
1338                 }
1339             }
1340             else
1341             {
1342                 if( tnbn <= tcbn )
1343                 {
1344                     op = IVTC_OP_COPY_N;
1345                     i_result_score = tnbn;
1346                 }
1347                 else
1348                 {
1349                     op = IVTC_OP_COMPOSE_TCBN;
1350                     i_result_score = tcbn;
1351                 }
1352             }
1353         }
1354
1355         /* Mangle timestamps when locked on.
1356
1357            "Current" is the frame that is being extracted now. Use its original
1358            timestamp as the base.
1359
1360            Note that this way there will be no extra delay compared to the
1361            raw stream, even though we look one frame into the future.
1362         */
1363         if( p_ivtc->b_sequence_valid )
1364         {
1365             /* Convert 29.97 -> 23.976 fps. We get to this point only if we
1366                didn't drop the frame, so we always get a valid delta.
1367             */
1368             int i_timestamp_delta = pi_timestamp_deltas[p_ivtc->i_cadence_pos];
1369             assert( i_timestamp_delta >= 0 );
1370
1371             /* FIXME: use field length as measured by Deinterlace()? */
1372             t_final = p_curr->date
1373                     + (p_next->date - p_curr->date)*i_timestamp_delta/4;
1374         }
1375         else /* Do not mangle timestamps (or drop frames, either) if cadence
1376                 is not locked on. This causes one of five output frames - if
1377                 all are reconstructed correctly - to be a duplicate, but in
1378                 practice at least with anime (which is the kind of material
1379                 that tends to have this problem) this is less noticeable than
1380                 a sudden jump in the cadence. Especially, a consistently wrong
1381                 lock-on will cause a very visible stutter, which we wish
1382                 to avoid. */
1383         {
1384             t_final = p_curr->date;
1385         }
1386
1387         /* Bump cadence counter into the next expected position. */
1388         p_ivtc->i_cadence_pos = ++p_ivtc->i_cadence_pos % 5;
1389     }
1390     else if( p_ivtc->i_mode == IVTC_MODE_TELECINED_NTSC_SOFT )
1391     {
1392         /* Soft telecine. We have the progressive frames already;
1393            even out PTS diffs only. */
1394
1395         /* Pass through the "current" frame. We must choose the frame "current"
1396            in order to be able to detect soft telecine before we have to output
1397            the frame. See IVTCSoftTelecineDetect(). Also, this allows
1398            us to peek at the next timestamp to calculate the duration of
1399            "current".
1400         */
1401         op = IVTC_OP_COPY_C;
1402         i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TCBC];
1403
1404         /* Timestamp mangling for soft telecine: bump "threes" forward by
1405            0.5 field durations. This is more forgiving for the renderer
1406            than bumping the "twos" back (which would require to render
1407            them sooner),
1408         */
1409         if( p_curr->i_nb_fields == 3 )
1410         {
1411             /* Approximate field duration from the PTS difference. */
1412             /* FIXME: use field length as measured by Deinterlace()? */
1413             mtime_t i_half_field_dur = ( (p_next->date - p_curr->date)/3 ) / 2;
1414             t_final = p_curr->date + i_half_field_dur;
1415         }
1416         else /* Otherwise, use original PTS of the outgoing frame. */
1417         {
1418             t_final = p_curr->date;
1419         }
1420     }
1421     else /* Not film mode, timestamp mangling bypassed. */
1422     {
1423         op = IVTC_OP_COPY_N;
1424         i_result_score = p_ivtc->pi_scores[FIELD_PAIR_TNBN];
1425
1426         /* Preserve original PTS (note that now, in principle,
1427                                   "next" is the outgoing frame) */
1428         t_final = p_next->date;
1429     }
1430
1431     /* There is only one case where we should drop the frame,
1432        and it was already handled above. */
1433     assert( op != IVTC_OP_DROP_FRAME );
1434
1435     /* Render into p_dst according to the final operation chosen. */
1436     if( op == IVTC_OP_COPY_N )
1437         picture_Copy( p_dst, p_next );
1438     else if( op == IVTC_OP_COPY_C )
1439         picture_Copy( p_dst, p_curr );
1440     else if( op == IVTC_OP_COMPOSE_TNBC )
1441         ComposeFrame( p_filter, p_dst, p_next, p_curr, CC_ALTLINE );
1442     else if( op == IVTC_OP_COMPOSE_TCBN )
1443         ComposeFrame( p_filter, p_dst, p_curr, p_next, CC_ALTLINE );
1444
1445     /* Slide history of outgoing interlace scores. This must be done last,
1446        and only if the frame was not dropped, so we do it here.
1447
1448        This is used during the reconstruction to get an idea of what is
1449        (in the temporally local sense) an acceptable interlace score
1450        for a correctly reconstructed frame. See above.
1451     */
1452     for( int i = 1; i < IVTC_DETECTION_HISTORY_SIZE; i++ )
1453         p_ivtc->pi_final_scores[i-1] = p_ivtc->pi_final_scores[i];
1454     p_ivtc->pi_final_scores[IVTC_LATEST] = i_result_score;
1455
1456     /* Note that picture_Copy() copies the PTS, too. Apply timestamp mangling
1457        now, if any was needed.
1458     */
1459     if( t_final > VLC_TS_INVALID )
1460         p_dst->date = t_final;
1461
1462     return true;
1463 }
1464
1465 /*****************************************************************************
1466  * Public functions
1467  *****************************************************************************/
1468
1469 /* See function doc in header. */
1470 int RenderIVTC( filter_t *p_filter, picture_t *p_dst )
1471 {
1472     assert( p_filter != NULL );
1473     assert( p_dst != NULL );
1474
1475     filter_sys_t *p_sys = p_filter->p_sys;
1476     ivtc_sys_t *p_ivtc  = &p_sys->ivtc;
1477
1478     picture_t *p_prev = p_sys->pp_history[0];
1479     picture_t *p_curr = p_sys->pp_history[1];
1480     picture_t *p_next = p_sys->pp_history[2];
1481
1482     /* If the history mechanism has failed, we have nothing to do. */
1483     if( !p_next )
1484         return VLC_EGENERIC;
1485
1486     /* Slide algorithm-specific histories */
1487     IVTCFrameInit( p_filter );
1488
1489     /* Filter if we have all the pictures we need.
1490        Note that we always have p_next at this point. */
1491     if( p_prev && p_curr )
1492     {
1493         /* Update raw data for motion, field repeats, interlace scores... */
1494         IVTCLowLevelDetect( p_filter );
1495
1496         /* Detect soft telecine.
1497
1498            Enter/exit IVTC_MODE_TELECINED_NTSC_SOFT when needed.
1499         */
1500         IVTCSoftTelecineDetect( p_filter );
1501
1502         /* Detect hard telecine.
1503
1504            Enter/exit IVTC_MODE_TELECINED_NTSC_HARD when needed.
1505
1506            If we happen to be running in IVTC_MODE_TELECINED_NTSC_SOFT,
1507            we nevertheless let the algorithms see for themselves that
1508            the stream is progressive. This doesn't break anything,
1509            and this way the full filter state gets updated at each frame.
1510
1511            See the individual function docs for details.
1512         */
1513         IVTCCadenceDetectAlgoScores( p_filter );
1514         IVTCCadenceDetectAlgoVektor( p_filter );
1515         IVTCCadenceDetectFinalize( p_filter ); /* pick winner */
1516         IVTCCadenceAnalyze( p_filter ); /* update filter state */
1517
1518         /* Now we can... */
1519         bool b_have_output_frame = IVTCOutputOrDropFrame( p_filter, p_dst );
1520
1521         /* The next frame will get a custom timestamp, too. */
1522         p_sys->i_frame_offset = CUSTOM_PTS;
1523
1524         if( b_have_output_frame )
1525             return VLC_SUCCESS;
1526         else
1527             return VLC_EGENERIC; /* Signal the caller not to expect a frame */
1528     }
1529     else if( !p_prev && !p_curr ) /* first frame */
1530     {
1531         /* Render the first frame as-is, so that a picture appears immediately.
1532
1533            We will also do some init for the filter. This score will become
1534            TPBP by the time the actual filter starts. Note that the sliding of
1535            final scores only starts when the filter has started (third frame).
1536         */
1537         int i_score = CalculateInterlaceScore( p_next, p_next );
1538         p_ivtc->pi_scores[FIELD_PAIR_TNBN] = i_score;
1539         p_ivtc->pi_final_scores[0]         = i_score;
1540
1541         picture_Copy( p_dst, p_next );
1542         return VLC_SUCCESS;
1543     }
1544     else /* second frame */
1545     {
1546         /* If the history sliding mechanism works correctly,
1547            the only remaining possibility is that: */
1548         assert( p_curr && !p_prev );
1549
1550         /* We need three frames for the cadence detector to work, so we just
1551            do some init for the detector and pass the frame through.
1552            Passthrough for second frame, too, works better than drop
1553            for some still-image DVD menus.
1554
1555            Now that we have two frames, we can run a full IVTCLowLevelDetect().
1556
1557            The interlace scores from here will become TCBC, TCBP and TPBC
1558            when the filter starts. The score for the current TCBC has already
1559            been computed at the first frame, and slid into place at the start
1560            of this frame (by IVTCFrameInit()).
1561         */
1562         IVTCLowLevelDetect( p_filter );
1563
1564         /* Note that the sliding mechanism for output scores only starts
1565            when the actual filter does.
1566         */
1567         p_ivtc->pi_final_scores[1] = p_ivtc->pi_scores[FIELD_PAIR_TNBN];
1568
1569         /* At the next frame, the filter starts. The next frame will get
1570            a custom timestamp. */
1571         p_sys->i_frame_offset = CUSTOM_PTS;
1572
1573         picture_Copy( p_dst, p_next );
1574         return VLC_SUCCESS;
1575     }
1576 }
1577
1578 /* See function doc in header. */
1579 void IVTCClearState( filter_t *p_filter )
1580 {
1581     assert( p_filter != NULL );
1582
1583     filter_sys_t *p_sys = p_filter->p_sys;
1584     ivtc_sys_t *p_ivtc = &p_sys->ivtc;
1585
1586     p_ivtc->i_cadence_pos = CADENCE_POS_INVALID;
1587     p_ivtc->i_tfd         = TFD_INVALID;
1588     p_ivtc->b_sequence_valid = false;
1589     p_ivtc->i_mode     = IVTC_MODE_DETECTING;
1590     p_ivtc->i_old_mode = IVTC_MODE_DETECTING;
1591     for( int i = 0; i < IVTC_NUM_FIELD_PAIRS; i++ )
1592         p_ivtc->pi_scores[i] = 0;
1593     for( int i = 0; i < IVTC_DETECTION_HISTORY_SIZE; i++ )
1594     {
1595         p_ivtc->pi_cadence_pos_history[i] = CADENCE_POS_INVALID;
1596
1597         p_ivtc->pi_s_cadence_pos[i] = CADENCE_POS_INVALID;
1598         p_ivtc->pb_s_reliable[i]    = false;
1599         p_ivtc->pi_v_cadence_pos[i] = CADENCE_POS_INVALID;
1600         p_ivtc->pb_v_reliable[i]    = false;
1601
1602         p_ivtc->pi_v_raw[i]         = VEKTOR_CADENCE_POS_ALL;
1603
1604         p_ivtc->pi_top_rep[i] =  0;
1605         p_ivtc->pi_bot_rep[i] =  0;
1606         p_ivtc->pi_motion[i]  = -1;
1607
1608         p_ivtc->pb_all_progressives[i] = false;
1609
1610         p_ivtc->pi_final_scores[i] = 0;
1611     }
1612 }