1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
/**
* Copyright (C) 2001, 2002, 2003 Billy Biggs <vektor@dumbterm.net>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <limits.h>
#include <string.h>
#if HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include "pulldown.h"
/**
* scratch paper:
*
* A A A B B C C C D D
* [T B T][B T][B T B][T B]
* [1 1][2 2][3 3][4 4][5 5]
* [C C] [M M][C C][C C]
* D A A A B B C C C D
*
* Top 1 : Drop
* Bot 1 : Show
* Top 2 : Drop
* Bot 2 : Drop
* Top 3 : Merge
* Bot 3 : Drop
* Top 4 : Show
* Bot 4 : Drop
* Top 5 : Drop
* Bot 5 : Show
*
* A A 1
* A B 2
* B C 4
* C C 8
* D D 16
*
* D D A A A B B C C C D D A A A +------------
* [ ]
* [* * ] | 1 top AA
* [ * *] | 0 AA bottom AA
*
* [ ]
* [* * ] | 1 top AB
* [* * ] | 1 AB bottom AB
*
* [ ]
* [ * *] | 0 top BC
* [* * ] | 1 BC bottom BC
*
* [ ]
* [ * *] | 0 top CC
* [ * *] | 0 CC bottom CC
*
* [ ]
* [* * ] | 1 top DD
* [ * *] | 0 DD bottom DD
*
*
* [* * ] | 1 top AA
* [ * *] | 0 AA bottom AA
*
*/
/* Offset 1 2 3 4 5 */
/* Field Pattern [T B T][B T][B T B] [T B] */
/* Action Copy Save Merge Copy Copy */
/* Bot Top */
static const int tff_top_pattern[] = { 0, 1, 0, 0, 0 };
static const int tff_bot_pattern[] = { 0, 0, 0, 1, 0 };
/* Offset 1 2 3 4 5 */
/* Field Pattern [B T B][T B][T B T] [B T] */
/* Action Copy Save Merge Copy Copy */
/* Top Bot */
static const int bff_top_pattern[] = { 0, 0, 0, 1, 0 };
static const int bff_bot_pattern[] = { 0, 1, 0, 0, 0 };
/* Timestamp mangling */
/* From the DVD : 0 + 3003+ 6006 + 9009+ 12012 = 15015 */
/* In 24fps time: 0 + + 3754 + 7508+ 11262 = 15016 */
/**
* Flag Pattern Treat as
* on DVD last offset
* ============================
* T B T bff 3
* B T bff 4
* B T B tff 3
* T B tff 4
*/
int determine_pulldown_offset( int top_repeat, int bot_repeat, int tff,
int last_offset )
{
int predicted_offset;
int pd_patterns = 0;
int offset = -1;
int exact = -1;
int i;
predicted_offset = last_offset << 1;
if( predicted_offset > PULLDOWN_SEQ_DD ) predicted_offset = PULLDOWN_SEQ_AA;
/**
* Detect our pattern.
*/
for( i = 0; i < 5; i++ ) {
/**
* Truth table:
*
* ref repeat, frame repeat valid
* ===========+==============+=======
* 0 0 -> 1
* 0 1 -> 1
* 1 0 -> 0
* 1 1 -> 1
*/
if( tff ) {
if( ( !tff_top_pattern[ i ] || top_repeat )
&& ( !tff_bot_pattern[ i ] || bot_repeat ) ) {
pd_patterns |= ( 1 << i );
offset = i;
}
} else {
if( ( !bff_top_pattern[ i ] || top_repeat )
&& ( !bff_bot_pattern[ i ] || bot_repeat ) ) {
pd_patterns |= ( 1 << i );
offset = i;
}
if( bff_top_pattern[ i ] == top_repeat && bff_bot_pattern[ i ] == bot_repeat ) {
exact = i;
}
}
}
offset = 1 << offset;
/**
* Check if the 3:2 pulldown pattern we previously decided on is
* valid for this set. If so, we use that.
*/
if( pd_patterns & predicted_offset ) offset = predicted_offset;
if( ( top_repeat || bot_repeat ) && exact > 0 ) offset = ( 1 << exact );
return offset;
}
#define HISTORY_SIZE 5
static int tophistory[ 5 ];
static int bothistory[ 5 ];
static int tophistory_diff[ 5 ];
static int bothistory_diff[ 5 ];
static int histpos = 0;
#if 0 /* FIXME: unused */
static void fill_history( int tff )
{
if( tff ) {
tophistory[ 0 ] = INT_MAX; bothistory[ 0 ] = INT_MAX;
tophistory[ 1 ] = 0; bothistory[ 1 ] = INT_MAX;
tophistory[ 2 ] = INT_MAX; bothistory[ 2 ] = INT_MAX;
tophistory[ 3 ] = INT_MAX; bothistory[ 3 ] = 0;
tophistory[ 4 ] = INT_MAX; bothistory[ 3 ] = INT_MAX;
tophistory_diff[ 0 ] = 0; bothistory_diff[ 0 ] = 0;
tophistory_diff[ 1 ] = 1; bothistory_diff[ 1 ] = 0;
tophistory_diff[ 2 ] = 0; bothistory_diff[ 2 ] = 0;
tophistory_diff[ 3 ] = 0; bothistory_diff[ 3 ] = 1;
tophistory_diff[ 4 ] = 0; bothistory_diff[ 3 ] = 0;
} else {
tophistory[ 0 ] = INT_MAX; bothistory[ 0 ] = INT_MAX;
tophistory[ 1 ] = INT_MAX; bothistory[ 1 ] = 0;
tophistory[ 2 ] = INT_MAX; bothistory[ 2 ] = INT_MAX;
tophistory[ 3 ] = 0; bothistory[ 3 ] = INT_MAX;
tophistory[ 4 ] = INT_MAX; bothistory[ 3 ] = INT_MAX;
tophistory_diff[ 0 ] = 0; bothistory_diff[ 0 ] = 0;
tophistory_diff[ 1 ] = 0; bothistory_diff[ 1 ] = 1;
tophistory_diff[ 2 ] = 0; bothistory_diff[ 2 ] = 0;
tophistory_diff[ 3 ] = 1; bothistory_diff[ 3 ] = 0;
tophistory_diff[ 4 ] = 0; bothistory_diff[ 3 ] = 0;
}
histpos = 0;
}
#endif
int determine_pulldown_offset_history( int top_repeat, int bot_repeat, int tff, int *realbest )
{
int avgbot = 0;
int avgtop = 0;
int best = 0;
int min = -1;
int minpos = 0;
int minbot = 0;
int j;
int ret;
int mintopval = -1;
int mintoppos = -1;
int minbotval = -1;
int minbotpos = -1;
tophistory[ histpos ] = top_repeat;
bothistory[ histpos ] = bot_repeat;
for( j = 0; j < HISTORY_SIZE; j++ ) {
avgtop += tophistory[ j ];
avgbot += bothistory[ j ];
}
avgtop /= 5;
avgbot /= 5;
for( j = 0; j < HISTORY_SIZE; j++ ) {
// int cur = (tophistory[ j ] - avgtop);
int cur = tophistory[ j ];
if( cur < min || min < 0 ) {
min = cur;
minpos = j;
}
if( cur < mintopval || mintopval < 0 ) {
mintopval = cur;
mintoppos = j;
}
}
for( j = 0; j < HISTORY_SIZE; j++ ) {
// int cur = (bothistory[ j ] - avgbot);
int cur = bothistory[ j ];
if( cur < min || min < 0 ) {
min = cur;
minpos = j;
minbot = 1;
}
if( cur < minbotval || minbotval < 0 ) {
minbotval = cur;
minbotpos = j;
}
}
if( minbot ) {
best = tff ? ( minpos + 2 ) : ( minpos + 4 );
} else {
best = tff ? ( minpos + 4 ) : ( minpos + 2 );
}
best = best % HISTORY_SIZE;
*realbest = 1 << ( ( histpos + (2*HISTORY_SIZE) - best ) % HISTORY_SIZE );
best = (minbotpos + 2) % 5;
ret = 1 << ( ( histpos + (2*HISTORY_SIZE) - best ) % HISTORY_SIZE );
best = (mintoppos + 4) % 5;
ret |= 1 << ( ( histpos + (2*HISTORY_SIZE) - best ) % HISTORY_SIZE );
histpos = (histpos + 1) % HISTORY_SIZE;
return ret;
}
static int reference = 0;
int determine_pulldown_offset_history_new( int top_repeat, int bot_repeat, int tff, int predicted )
{
int avgbot = 0;
int avgtop = 0;
int i, j;
int ret;
int mintopval = -1;
int mintoppos = -1;
int min2topval = -1;
int min2toppos = -1;
int minbotval = -1;
int minbotpos = -1;
int min2botval = -1;
int min2botpos = -1;
/*int predicted_pos = 0;*/
tophistory[ histpos ] = top_repeat;
bothistory[ histpos ] = bot_repeat;
for( j = 0; j < HISTORY_SIZE; j++ ) {
avgtop += tophistory[ j ];
avgbot += bothistory[ j ];
}
avgtop /= 5;
avgbot /= 5;
for( i = 0; i < 5; i++ ) { if( (1<<i) == predicted ) { /*predicted_pos = i;*/ break; } }
/*
printf(top: %8d bot: %8d\ttop-avg: %8d bot-avg: %8d (%d)\n", top_repeat, bot_repeat, top_repeat - avgtop, bot_repeat - avgbot, (5 + predicted_pos - reference) % 5 );
*/
for( j = 0; j < HISTORY_SIZE; j++ ) {
int cur = tophistory[ j ];
if( cur < mintopval || mintopval < 0 ) {
min2topval = mintopval;
min2toppos = mintoppos;
mintopval = cur;
mintoppos = j;
} else if( cur < min2topval || min2topval < 0 ) {
min2topval = cur;
min2toppos = j;
}
}
for( j = 0; j < HISTORY_SIZE; j++ ) {
int cur = bothistory[ j ];
if( cur < minbotval || minbotval < 0 ) {
min2botval = minbotval;
min2botpos = minbotpos;
minbotval = cur;
minbotpos = j;
} else if( cur < min2botval || min2botval < 0 ) {
min2botval = cur;
min2botpos = j;
}
}
tophistory_diff[ histpos ] = ((mintoppos == histpos) || (min2toppos == histpos));
bothistory_diff[ histpos ] = ((minbotpos == histpos) || (min2botpos == histpos));
ret = 0;
for( i = 0; i < 5; i++ ) {
int valid = 1;
for( j = 0; j < 5; j++ ) {
// if( tff_top_pattern[ j ] && !tophistory_diff[ (i + j) % 5 ] && tophistory[ (i + j) % 5 ] != mintopval ) {
if( tff_top_pattern[ j ] && (tophistory[ (i + j) % 5 ] > avgtop || !tophistory_diff[ (i + j) % 5 ]) ) {
valid = 0;
break;
}
// if( tff_bot_pattern[ j ] && !bothistory_diff[ (i + j) % 5 ] && bothistory[ (i + j) % 5 ] != minbotval ) {
if( tff_bot_pattern[ j ] && (bothistory[ (i + j) % 5 ] > avgbot || !bothistory_diff[ (i + j) % 5 ]) ) {
valid = 0;
break;
}
}
if( valid ) ret |= (1<<(((5-i)+histpos)%5));
}
/*
printf( "ret: %d %d %d %d %d\n",
PULLDOWN_OFFSET_1 & ret,
PULLDOWN_OFFSET_2 & ret,
PULLDOWN_OFFSET_3 & ret,
PULLDOWN_OFFSET_4 & ret,
PULLDOWN_OFFSET_5 & ret );
*/
histpos = (histpos + 1) % HISTORY_SIZE;
reference = (reference + 1) % 5;
if( !ret ) {
/* No pulldown sequence is valid, return an error. */
return 0;
} else if( !(predicted & ret) ) {
/**
* We have a valid sequence, but it doesn't match our prediction.
* Return the first 'valid' sequence in the list.
*/
for( i = 0; i < 5; i++ ) { if( ret & (1<<i) ) return (1<<i); }
}
/**
* The predicted phase is still valid.
*/
return predicted;
}
int determine_pulldown_offset_short_history_new( int top_repeat, int bot_repeat, int tff, int predicted )
{
int avgbot = 0;
int avgtop = 0;
int i, j;
int ret;
int mintopval = -1;
int mintoppos = -1;
int min2topval = -1;
int min2toppos = -1;
int minbotval = -1;
int minbotpos = -1;
int min2botval = -1;
int min2botpos = -1;
/*int predicted_pos = 0;*/
tophistory[ histpos ] = top_repeat;
bothistory[ histpos ] = bot_repeat;
for( j = 0; j < 3; j++ ) {
avgtop += tophistory[ (histpos + 5 - j) % 5 ];
avgbot += bothistory[ (histpos + 5 - j) % 5 ];
}
avgtop /= 3;
avgbot /= 3;
for( i = 0; i < 5; i++ ) { if( (1<<i) == predicted ) { /*predicted_pos = i;*/ break; } }
/*
printf( "top: %8d bot: %8d\ttop-avg: %8d bot-avg: %8d (%d)\n",
top_repeat, bot_repeat, top_repeat - avgtop, bot_repeat - avgbot,
(5 + predicted_pos - reference) % 5 );
*/
for( j = 0; j < 3; j++ ) {
int cur = tophistory[ (histpos + 5 - j) % 5 ];
if( cur < mintopval || mintopval < 0 ) {
min2topval = mintopval;
min2toppos = mintoppos;
mintopval = cur;
mintoppos = j;
} else if( cur < min2topval || min2topval < 0 ) {
min2topval = cur;
min2toppos = j;
}
}
for( j = 0; j < 3; j++ ) {
int cur = bothistory[ (histpos + 5 - j) % 5 ];
if( cur < minbotval || minbotval < 0 ) {
min2botval = minbotval;
min2botpos = minbotpos;
minbotval = cur;
minbotpos = j;
} else if( cur < min2botval || min2botval < 0 ) {
min2botval = cur;
min2botpos = j;
}
}
tophistory_diff[ histpos ] = ((mintoppos == histpos) || (min2toppos == histpos));
bothistory_diff[ histpos ] = ((minbotpos == histpos) || (min2botpos == histpos));
ret = 0;
for( i = 0; i < 5; i++ ) {
int valid = 1;
for( j = 0; j < 3; j++ ) {
// if( tff_top_pattern[ j ] && !tophistory_diff[ (i + j) % 5 ] && tophistory[ (i + j) % 5 ] != mintopval ) {
// if( tff_top_pattern[ j ] && (tophistory[ (i + j) % 5 ] > avgtop || !tophistory_diff[ (i + j) % 5 ]) ) {
if( tff_top_pattern[ (i + 5 - j) % 5 ] && tophistory[ (histpos + 5 - j) % 5 ] > avgtop ) {
// if( tff_top_pattern[ (i + 5 - j) % 5 ] && !tophistory_diff[ (histpos + 5 - j) % 5 ] && tophistory[ (histpos + 5 - j) % 5 ] != mintopval ) {
valid = 0;
break;
}
// if( tff_bot_pattern[ j ] && !bothistory_diff[ (i + j) % 5 ] && bothistory[ (i + j) % 5 ] != minbotval ) {
// if( tff_bot_pattern[ j ] && (bothistory[ (i + j) % 5 ] > avgbot || !bothistory_diff[ (i + j) % 5 ]) ) {
if( tff_bot_pattern[ (i + 5 - j) % 5 ] && bothistory[ (histpos + 5 - j) % 5 ] > avgbot ) {
// if( tff_bot_pattern[ (i + 5 - j) % 5 ] && !bothistory_diff[ (histpos + 5 - j) % 5 ] && bothistory[ (histpos + 5 - j) % 5 ] != minbotval ) {
valid = 0;
break;
}
}
if( valid ) ret |= (1<<i);
}
/*
printf( "ret: %d %d %d %d %d\n",
PULLDOWN_OFFSET_1 & ret,
PULLDOWN_OFFSET_2 & ret,
PULLDOWN_OFFSET_3 & ret,
PULLDOWN_OFFSET_4 & ret,
PULLDOWN_OFFSET_5 & ret );
*/
histpos = (histpos + 1) % HISTORY_SIZE;
reference = (reference + 1) % 5;
if( !ret ) {
/* No pulldown sequence is valid, return an error. */
return 0;
} else if( !(predicted & ret) ) {
/**
* We have a valid sequence, but it doesn't match our prediction.
* Return the first 'valid' sequence in the list.
*/
for( i = 0; i < 5; i++ ) { if( ret & (1<<i) ) return (1<<i); }
}
/**
* The predicted phase is still valid.
*/
return predicted;
}
int determine_pulldown_offset_dalias( pulldown_metrics_t *old_peak,
pulldown_metrics_t *old_relative,
pulldown_metrics_t *old_mean,
pulldown_metrics_t *new_peak,
pulldown_metrics_t *new_relative,
pulldown_metrics_t *new_mean )
{
int laced = 0;
if (old_peak->d > 360) {
if (3*old_relative->e < old_relative->o) laced=1;
if ((2*old_relative->d < old_relative->s) && (old_relative->s > 600))
laced=1;
}
if (new_peak->d > 360) {
if ((2*new_relative->t < new_relative->p) && (new_relative->p > 600))
laced=1;
}
if( !laced ) return PULLDOWN_ACTION_NEXT_PREV;
if (new_relative->t < 2*new_relative->p) {
if ((3*old_relative->e < old_relative->o) || (2*new_relative->t < new_relative->p)) {
return PULLDOWN_ACTION_PREV_NEXT;
}
}
return PULLDOWN_ACTION_PREV_NEXT;
}
#define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
void diff_factor_packed422_frame( pulldown_metrics_t *peak, pulldown_metrics_t *rel, pulldown_metrics_t *mean,
uint8_t *old, uint8_t *new, int w, int h, int os, int ns )
{
int x, y;
pulldown_metrics_t l;
memset(peak, 0, sizeof(pulldown_metrics_t));
memset(rel, 0, sizeof(pulldown_metrics_t));
memset(mean, 0, sizeof(pulldown_metrics_t));
for (y = 0; y < h-7; y += 8) {
for (x = 8; x < w-8-7; x += 8) {
diff_packed422_block8x8(&l, old+x+y*os, new+x+y*ns, os, ns);
mean->d += l.d;
mean->e += l.e;
mean->o += l.o;
mean->s += l.s;
mean->p += l.p;
mean->t += l.t;
MAXUP(peak->d, l.d);
MAXUP(peak->e, l.e);
MAXUP(peak->o, l.o);
MAXUP(peak->s, l.s);
MAXUP(peak->p, l.p);
MAXUP(peak->t, l.t);
MAXUP(rel->e, l.e-l.o);
MAXUP(rel->o, l.o-l.e);
MAXUP(rel->s, l.s-l.t);
MAXUP(rel->p, l.p-l.t);
MAXUP(rel->t, l.t-l.p);
MAXUP(rel->d, l.t-l.s); /* hack */
}
}
x = (w/8-2)*(h/8);
mean->d /= x;
mean->e /= x;
mean->o /= x;
mean->s /= x;
mean->p /= x;
mean->t /= x;
}
int pulldown_source( int action, int bottom_field )
{
if( action == PULLDOWN_SEQ_AA ) {
return !bottom_field;
} else if( action == PULLDOWN_SEQ_AB ) {
return 1;
} else if( action == PULLDOWN_SEQ_BC ) {
return bottom_field;
} else if( action == PULLDOWN_SEQ_CC ) {
return 0;
} else if( action == PULLDOWN_SEQ_DD ) {
return !bottom_field;
}
return 0;
}
int pulldown_drop( int action, int bottom_field )
{
int ret = 1;
if( action == PULLDOWN_SEQ_AA && bottom_field )
ret = 0;
if( action == PULLDOWN_SEQ_BC && !bottom_field )
ret = 0;
if( action == PULLDOWN_SEQ_CC && !bottom_field )
ret = 0;
if( action == PULLDOWN_SEQ_DD && bottom_field )
ret = 0;
return ret;
}
|