forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheedi2.c
1872 lines (1792 loc) · 68.9 KB
/
eedi2.c
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eedi2.c
Copyright (c) 2003-2020 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
The EEDI2 interpolator was created by tritical:
http://web.missouri.edu/~kes25c/
*/
#include "handbrake/handbrake.h"
#include "handbrake/eedi2.h"
/**
* EEDI2 directional limit lookup table
*
* These values are used to limit the range of edge direction searches and filtering.
*/
const int eedi2_limlut[33] __attribute__ ((aligned (16))) = {
6, 6, 7, 7, 8, 8, 9, 9, 9, 10,
10, 11, 11, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, -1, -1 };
/**
* Analog of _aligned_malloc
* @param size Size of memory being pointed to
* @param align_size Size of memory chunks to align to (must be power of 2)
*/
void *eedi2_aligned_malloc( size_t size, size_t align_size )
{
char * ptr, * ptr2, * aligned_ptr;
int align_mask = align_size - 1;
ptr = (char *)malloc( size + align_size + sizeof( int ) );
if( ptr==NULL ) return( NULL );
ptr2 = ptr + sizeof( int );
aligned_ptr = ptr2 + ( align_size - ( (size_t)ptr2 & align_mask ) );
ptr2 = aligned_ptr - sizeof( int );
*( (int *)ptr2 ) = (int)( aligned_ptr - ptr );
return( aligned_ptr );
}
/**
* Analog of _aligned_free
* @param ptr The aligned pointer, created with eedi2_aligned_malloc, to be freed
*/
void eedi2_aligned_free( void *ptr )
{
int * ptr2 = (int *)ptr - 1;
ptr -= * ptr2;
free(ptr);
}
/**
* Sorts metrics for median filtering
* @param order Pointer to the table of values to sort
* @param length Length of the order array
*/
void eedi2_sort_metrics( int *order, const int length )
{
int i;
for( i = 1; i < length; ++i )
{
int j = i;
const int temp = order[j];
while( j > 0 && order[j-1] > temp )
{
order[j] = order[j-1];
--j;
}
order[j] = temp;
}
}
/**
* Bitblits an image plane (overwrites one bitmap with another)
* @param dtsp Pointer to destination bitmap
* @param dst_pitch Stride of destination bitmap
* @param srcp Pointer to source bitmap
* @param src_pitch Stride of destination bitmap
* @param row_size Width of the bitmap being copied
* @param height Height of the source bitmap
*
* When row_size, dst_pitch, and src_pitch are equal, eedi2_bit_blit can work more quickly by copying the whole plane at once instead of individual lines.
*/
void eedi2_bit_blit( uint8_t * dstp, int dst_pitch,
const uint8_t * srcp, int src_pitch,
int row_size, int height )
{
if( ( !height ) || ( !row_size ) )
return;
if( height == 1 || ( dst_pitch == src_pitch && src_pitch == row_size ) )
{
memcpy( dstp, srcp, row_size * height );
}
else
{
int y;
for( y = height; y > 0; --y )
{
memcpy( dstp, srcp, row_size );
dstp += dst_pitch;
srcp += src_pitch;
}
}
}
/**
* A specialized variant of bit_blit, just for setting up the initial, field-sized bitmap planes that EEDI2 interpolates from.
* @param src Pointer to source bitmap plane being copied from
* @param dst Pointer to the destination bitmap plane being copied to
* @param pitch Stride of both bitmaps
* @param height Height of the original, full-size src plane being copied from
*/
void eedi2_fill_half_height_buffer_plane( uint8_t * src, uint8_t * dst, int pitch, int height )
{
/* When TFF, we want to copy alternating
lines starting at 0, the top field.
When BFF, we want to start at line 1. */
int y;
for( y = height; y > 0; y = y - 2 )
{
memcpy( dst, src, pitch );
dst += pitch;
src += pitch * 2;
}
}
/**
* A specialized variant of bit_blit, just for resizing the field-height maps EEDI2 generates to frame-height...a simple line doubler
* @param srcp Pointer to source bitmap plane being copied from
* @param dstp Pointer to the destination bitmap plane being copied to
* @param height Height of the input, half-size src plane being copied from
* @param pitch Stride of both bitmaps
*/
void eedi2_upscale_by_2( uint8_t * srcp, uint8_t * dstp, int height, int pitch )
{
int y;
for( y = height; y > 0; y-- )
{
memcpy( dstp, srcp, pitch );
dstp += pitch;
memcpy( dstp, srcp, pitch );
srcp += pitch;
dstp += pitch;
}
}
/**
* Finds places where vertically adjacent pixels abruptly change in intensity, i.e., sharp edges.
* @param dstp Pointer to the destination bitmap
* @param dst_pitch Stride of dstp
* @param srcp Pointer to the source bitmap
* @param src_pitch Stride of srcp
* @param mtresh Magnitude threshold, ensures it doesn't mark edges on pixels that are too similar (10 is a good default value)
* @param vthresh Variance threshold, ensures it doesn't look for edges in highly random pixel blocks (20 is a good default value)
* @param lthresh Laplacian threshold, ensures edges are still prominent in the 2nd spatial derivative of the srcp plane (20 is a good default value)
* @param height Height of half-height single-field frame
* @param width Width of srcp bitmap rows, as opposed to the padded stride in src_pitch
*/
void eedi2_build_edge_mask( uint8_t * dstp, int dst_pitch, uint8_t *srcp, int src_pitch,
int mthresh, int lthresh, int vthresh, int height, int width )
{
int x, y;
mthresh = mthresh * 10;
vthresh = vthresh * 81;
memset( dstp, 0, ( height / 2 ) * dst_pitch );
srcp += src_pitch;
dstp += dst_pitch;
unsigned char *srcpp = srcp-src_pitch;
unsigned char *srcpn = srcp+src_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width-1; ++x )
{
if( ( abs( srcpp[x] - srcp[x] ) < 10 &&
abs( srcp[x] - srcpn[x] ) < 10 &&
abs( srcpp[x] - srcpn[x] ) < 10 )
||
( abs( srcpp[x-1] - srcp[x-1] ) < 10 &&
abs( srcp[x-1] - srcpn[x-1] ) < 10 &&
abs( srcpp[x-1] - srcpn[x-1] ) < 10 &&
abs( srcpp[x+1] - srcp[x+1] ) < 10 &&
abs( srcp[x+1] - srcpn[x+1] ) < 10 &&
abs( srcpp[x+1] - srcpn[x+1] ) < 10) )
continue;
const int sum = srcpp[x-1] + srcpp[x] + srcpp[x+1] +
srcp[x-1] + srcp[x]+ srcp[x+1] +
srcpn[x-1] + srcpn[x] + srcpn[x+1];
const int sumsq = srcpp[x-1] * srcpp[x-1] +
srcpp[x] * srcpp[x] +
srcpp[x+1] * srcpp[x+1] +
srcp[x-1] * srcp[x-1] +
srcp[x] * srcp[x] +
srcp[x+1] * srcp[x+1] +
srcpn[x-1] * srcpn[x-1] +
srcpn[x] * srcpn[x] +
srcpn[x+1] * srcpn[x+1];
if( 9 * sumsq-sum * sum < vthresh )
continue;
const int Ix = srcp[x+1] - srcp[x-1];
const int Iy = MAX( MAX( abs( srcpp[x] - srcpn[x] ),
abs( srcpp[x] - srcp[x] ) ),
abs( srcp[x] - srcpn[x] ) );
if( Ix * Ix + Iy * Iy >= mthresh )
{
dstp[x] = 255;
continue;
}
const int Ixx = srcp[x-1] - 2 * srcp[x] + srcp[x+1];
const int Iyy = srcpp[x] - 2 * srcp[x] + srcpn[x];
if( abs( Ixx ) + abs( Iyy ) >= lthresh )
dstp[x] = 255;
}
dstp += dst_pitch;
srcpp += src_pitch;
srcp += src_pitch;
srcpn += src_pitch;
}
}
/**
* Expands and smooths out the edge mask
* @param mskp Pointer to the source edge mask being read from
* @param msk_pitch Stride of mskp
* @param dstp Pointer to the destination to store the dilated edge mask
* @param dst_pitch Stride of dstp
* @param dstr Dilation threshold, ensures a pixel is only retained as an edge in dstp if this number of adjacent pixels or greater are also edges in mskp (4 is a good default value)
* @param height Height of half-height field-sized frame
* @param width Width of mskp bitmap rows, as opposed to the pdded stride in msk_pitch
*/
void eedi2_dilate_edge_mask( uint8_t *mskp, int msk_pitch, uint8_t *dstp, int dst_pitch,
int dstr, int height, int width )
{
int x, y;
eedi2_bit_blit( dstp, dst_pitch, mskp, msk_pitch, width, height );
mskp += msk_pitch;
unsigned char *mskpp = mskp - msk_pitch;
unsigned char *mskpn = mskp + msk_pitch;
dstp += dst_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0 )
continue;
int count = 0;
if( mskpp[x-1] == 0xFF ) ++count;
if( mskpp[x] == 0xFF ) ++count;
if( mskpp[x+1] == 0xFF ) ++count;
if( mskp[x-1] == 0xFF ) ++count;
if( mskp[x+1] == 0xFF ) ++count;
if( mskpn[x-1] == 0xFF ) ++count;
if( mskpn[x] == 0xFF ) ++count;
if( mskpn[x+1] == 0xFF ) ++count;
if( count >= dstr )
dstp[x] = 0xFF;
}
mskpp += msk_pitch;
mskp += msk_pitch;
mskpn += msk_pitch;
dstp += dst_pitch;
}
}
/**
* Contracts the edge mask
* @param mskp Pointer to the source edge mask being read from
* @param msk_pitch Stride of mskp
* @param dstp Pointer to the destination to store the eroded edge mask
* @param dst_pitch Stride of dstp
* @param estr Erosion threshold, ensures a pixel isn't retained as an edge in dstp if fewer than this number of adjacent pixels are also edges in mskp (2 is a good default value)
* @param height Height of half-height field-sized frame
* @param width Width of mskp bitmap rows, as opposed to the pdded stride in msk_pitch
*/
void eedi2_erode_edge_mask( uint8_t *mskp, int msk_pitch, uint8_t *dstp, int dst_pitch,
int estr, int height, int width )
{
int x, y;
eedi2_bit_blit( dstp, dst_pitch, mskp, msk_pitch, width, height );
mskp += msk_pitch;
unsigned char *mskpp = mskp - msk_pitch;
unsigned char *mskpn = mskp + msk_pitch;
dstp += dst_pitch;
for ( y = 1; y < height - 1; ++y )
{
for ( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0xFF ) continue;
int count = 0;
if ( mskpp[x-1] == 0xFF ) ++count;
if ( mskpp[x] == 0xFF ) ++count;
if ( mskpp[x+1] == 0xFF ) ++count;
if ( mskp[x-1] == 0xFF ) ++count;
if ( mskp[x+1] == 0xFF ) ++count;
if ( mskpn[x-1] == 0xFF ) ++count;
if ( mskpn[x] == 0xFF ) ++count;
if ( mskpn[x+1] == 0xFF ) ++count;
if ( count < estr) dstp[x] = 0;
}
mskpp += msk_pitch;
mskp += msk_pitch;
mskpn += msk_pitch;
dstp += dst_pitch;
}
}
/**
* Smooths out horizontally aligned holes in the mask
*
* If none of the 6 horizontally adjacent pixels are edges, mark the current pixel as not edged.
* If at least 1 of the 3 on either side are edges, mark the current pixel as an edge.
*
* @param mskp Pointer to the source edge mask being read from
* @param msk_pitch Stride of mskp
* @param dstp Pointer to the destination to store the smoothed edge mask
* @param dst_pitch Stride of dstp
* @param height Height of half-height field-sized frame
* @param width Width of mskp bitmap rows, as opposed to the pdded stride in msk_pitch
*/
void eedi2_remove_small_gaps( uint8_t * mskp, int msk_pitch, uint8_t * dstp, int dst_pitch,
int height, int width )
{
int x, y;
eedi2_bit_blit( dstp, dst_pitch, mskp, msk_pitch, width, height );
mskp += msk_pitch;
dstp += dst_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 3; x < width - 3; ++x )
{
if( mskp[x] )
{
if( mskp[x-3] ) continue;
if( mskp[x-2] ) continue;
if( mskp[x-1] ) continue;
if( mskp[x+1] ) continue;
if( mskp[x+2] ) continue;
if( mskp[x+3] ) continue;
dstp[x] = 0;
}
else
{
if ( ( mskp[x+1] && ( mskp[x-1] || mskp[x-2] || mskp[x-3] ) ) ||
( mskp[x+2] && ( mskp[x-1] || mskp[x-2] ) ) ||
( mskp[x+3] && mskp[x-1] ) )
dstp[x] = 0xFF;
}
}
mskp += msk_pitch;
dstp += dst_pitch;
}
}
/**
* Calculates spatial direction vectors for the edges. This is EEDI2's timesink, and can be thought of as YADIF_CHECK on steroids, as both try to discern which angle a given edge follows
* @param plane The plane of the image being processed, to know to reduce maxd for chroma planes (HandBrake only works with YUV420 video so it is assumed they are half-height)
* @param mskp Pointer to the source edge mask being read from
* @param msk_pitch Stride of mskp
* @param srcp Pointer to the source image being filtered
* @param src_pitch Stride of srcp
* @param dstp Pointer to the destination to store the dilated edge mask
* @param dst_pitch Stride of dstp
* @param maxd Maximum pixel distance to search (24 is a good default value)
* @param nt Noise threshold (50 is a good default value)
* @param height Height of half-height field-sized frame
* @param width Width of srcp bitmap rows, as opposed to the pdded stride in src_pitch
*/
void eedi2_calc_directions( const int plane, uint8_t * mskp, int msk_pitch, uint8_t * srcp, int src_pitch,
uint8_t * dstp, int dst_pitch, int maxd, int nt, int height, int width )
{
int x, y, u, i;
memset( dstp, 255, dst_pitch * height );
mskp += msk_pitch;
dstp += dst_pitch;
srcp += src_pitch;
unsigned char *src2p = srcp - src_pitch * 2;
unsigned char *srcpp = srcp - src_pitch;
unsigned char *srcpn = srcp + src_pitch;
unsigned char *src2n = srcp + src_pitch * 2;
unsigned char *mskpp = mskp - msk_pitch;
unsigned char *mskpn = mskp + msk_pitch;
const int maxdt = plane == 0 ? maxd : ( maxd >> 1 );
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0xFF || ( mskp[x-1] != 0xFF && mskp[x+1] != 0xFF ) )
continue;
const int startu = MAX( -x + 1, -maxdt );
const int stopu = MIN( width - 2 - x, maxdt );
int minb = MIN( 13 * nt,
( abs( srcp[x] - srcpn[x] ) +
abs( srcp[x] - srcpp[x] ) ) * 6 );
int mina = MIN( 19 * nt,
( abs( srcp[x] - srcpn[x] ) +
abs( srcp[x] - srcpp[x] ) ) * 9 );
int minc = mina;
int mind = minb;
int mine = minb;
int dira = -5000, dirb = -5000, dirc = -5000, dird = -5000, dire = -5000;
for( u = startu; u <= stopu; ++u )
{
if( y == 1 ||
mskpp[x-1+u] == 0xFF || mskpp[x+u] == 0xFF || mskpp[x+1+u] == 0xFF )
{
if( y == height - 2 ||
mskpn[x-1-u] == 0xFF || mskpn[x-u] == 0xFF || mskpn[x+1-u] == 0xFF )
{
const int diffsn = abs( srcp[x-1] - srcpn[x-1-u] ) +
abs( srcp[x] - srcpn[x-u] ) +
abs( srcp[x+1] - srcpn[x+1-u] );
const int diffsp = abs( srcp[x-1] - srcpp[x-1+u] ) +
abs( srcp[x] - srcpp[x+u] ) +
abs( srcp[x+1] - srcpp[x+1+u] );
const int diffps = abs( srcpp[x-1] - srcp[x-1-u] ) +
abs( srcpp[x] - srcp[x-u] ) +
abs( srcpp[x+1] - srcp[x+1-u] );
const int diffns = abs( srcpn[x-1] - srcp[x-1+u] ) +
abs( srcpn[x] - srcp[x+u] ) +
abs( srcpn[x+1] - srcp[x+1+u] );
const int diff = diffsn + diffsp + diffps + diffns;
int diffd = diffsp + diffns;
int diffe = diffsn + diffps;
if( diff < minb )
{
dirb = u;
minb = diff;
}
if( __builtin_expect( y > 1, 1) )
{
const int diff2pp = abs( src2p[x-1] - srcpp[x-1-u] ) +
abs( src2p[x] - srcpp[x-u] ) +
abs( src2p[x+1] - srcpp[x+1-u] );
const int diffp2p = abs( srcpp[x-1] - src2p[x-1+u] ) +
abs( srcpp[x] - src2p[x+u] ) +
abs( srcpp[x+1] - src2p[x+1+u] );
const int diffa = diff + diff2pp + diffp2p;
diffd += diffp2p;
diffe += diff2pp;
if( diffa < mina )
{
dira = u;
mina = diffa;
}
}
if( __builtin_expect( y < height-2, 1) )
{
const int diff2nn = abs( src2n[x-1] - srcpn[x-1+u] ) +
abs( src2n[x] - srcpn[x+u] ) +
abs( src2n[x+1] - srcpn[x+1+u] );
const int diffn2n = abs( srcpn[x-1] - src2n[x-1-u] ) +
abs( srcpn[x] - src2n[x-u] ) +
abs( srcpn[x+1] - src2n[x+1-u] );
const int diffc = diff + diff2nn + diffn2n;
diffd += diff2nn;
diffe += diffn2n;
if( diffc < minc )
{
dirc = u;
minc = diffc;
}
}
if( diffd < mind )
{
dird = u;
mind = diffd;
}
if( diffe < mine )
{
dire = u;
mine = diffe;
}
}
}
}
int order[5], k=0;
if( dira != -5000 ) order[k++] = dira;
if( dirb != -5000 ) order[k++] = dirb;
if( dirc != -5000 ) order[k++] = dirc;
if( dird != -5000 ) order[k++] = dird;
if( dire != -5000 ) order[k++] = dire;
if( k > 1 )
{
eedi2_sort_metrics( order, k );
const int mid = ( k & 1 ) ?
order[k>>1] :
( order[(k-1)>>1] + order[k>>1] + 1 ) >> 1;
const int tlim = MAX( eedi2_limlut[abs(mid)] >> 2, 2 );
int sum = 0, count = 0;
for( i = 0; i < k; ++i )
{
if( abs( order[i] - mid ) <= tlim )
{
++count;
sum += order[i];
}
}
if( count > 1 )
dstp[x] = 128 + ( (int)( (float)sum / (float)count ) * 4 );
else
dstp[x] = 128;
}
else dstp[x] = 128;
}
mskpp += msk_pitch;
mskp += msk_pitch;
mskpn += msk_pitch;
src2p += src_pitch;
srcpp += src_pitch;
srcp += src_pitch;
srcpn += src_pitch;
src2n += src_pitch;
dstp += dst_pitch;
}
}
/**
* Filters the edge mask
* @param mskp Pointer to the source edge mask being read from
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the filtered edge mask
* @param dst_pitch Stride of dstp
* @param height Height of half-height field-sized frame
* @param width Width of mskp bitmap rows, as opposed to the pdded stride in msk_pitch
*/
void eedi2_filter_map( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int height, int width )
{
int x, y, j;
eedi2_bit_blit( dstp, dst_pitch, dmskp, dmsk_pitch, width, height );
mskp += msk_pitch;
dmskp += dmsk_pitch;
dstp += dst_pitch;
unsigned char *dmskpp = dmskp - dmsk_pitch;
unsigned char *dmskpn = dmskp + dmsk_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width - 1; ++x )
{
if( dmskp[x] == 0xFF || mskp[x] != 0xFF )
continue;
const int dir = ( dmskp[x] - 128 ) >> 2;
const int lim = MAX( abs( dir ) * 2, 12 );
int ict = 0, icb = 0;
if( dir < 0 )
{
const int dirt = MAX( -x, dir );
for( j = dirt; j <= 0; ++j )
{
if( ( abs( dmskpp[x+j] - dmskp[x] ) > lim && dmskpp[x+j] != 0xFF ) ||
( dmskp[x+j] == 0xFF && dmskpp[x+j] == 0xFF ) ||
( abs( dmskp[x+j] - dmskp[x] ) > lim && dmskp[x+j] != 0xFF ) )
{
ict = 1;
break;
}
}
}
else
{
const int dirt = MIN( width - x - 1, dir );
for( j = 0; j <= dirt; ++j )
{
if( ( abs( dmskpp[x+j] - dmskp[x] ) > lim && dmskpp[x+j] != 0xFF ) ||
( dmskp[x+j] == 0xFF && dmskpp[x+j] == 0xFF ) ||
( abs( dmskp[x+j] - dmskp[x] ) > lim && dmskp[x+j] != 0xFF ) )
{
ict = 1;
break;
}
}
}
if( ict )
{
if( dir < 0 )
{
const int dirt = MIN( width - x - 1, abs( dir ) );
for( j = 0; j <= dirt; ++j )
{
if( ( abs( dmskpn[x+j] - dmskp[x] ) > lim && dmskpn[x+j] != 0xFF ) ||
( dmskpn[x+j] == 0xFF && dmskp[x+j] == 0xFF ) ||
( abs( dmskp[x+j] - dmskp[x] ) > lim && dmskp[x+j] != 0xFF ) )
{
icb = 1;
break;
}
}
}
else
{
const int dirt = MAX( -x, -dir );
for( j = dirt; j <= 0; ++j )
{
if( ( abs( dmskpn[x+j] - dmskp[x] ) > lim && dmskpn[x+j] != 0xFF ) ||
( dmskpn[x+j] == 0xFF && dmskp[x+j] == 0xFF ) ||
( abs( dmskp[x+j] - dmskp[x] ) > lim && dmskp[x+j] != 0xFF ) )
{
icb = 1;
break;
}
}
}
if( icb )
dstp[x] = 255;
}
}
mskp += msk_pitch;
dmskpp += dmsk_pitch;
dmskp += dmsk_pitch;
dmskpn += dmsk_pitch;
dstp += dst_pitch;
}
}
/**
* Filters the edge direction mask
* @param mskp Pointer to the edge mask
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask being read from
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the filtered edge direction mask
* @param dst_pitch Stride of dstp
* @param height Height of half_height field-sized frame
* @param width Width of dmskp bitmap rows, as opposed to the pdded stride in dmsk_pitch
*/
void eedi2_filter_dir_map( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int height, int width )
{
int x, y, i;
eedi2_bit_blit( dstp, dst_pitch, dmskp, dmsk_pitch, width, height );
dmskp += dmsk_pitch;
unsigned char *dmskpp = dmskp - dmsk_pitch;
unsigned char *dmskpn = dmskp + dmsk_pitch;
dstp += dst_pitch;
mskp += msk_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0xFF ) continue;
int u = 0, order[9];
if( dmskpp[x-1] != 0xFF ) order[u++] = dmskpp[x-1];
if( dmskpp[x] != 0xFF ) order[u++] = dmskpp[x];
if( dmskpp[x+1] != 0xFF ) order[u++] = dmskpp[x+1];
if( dmskp[x-1] != 0xFF ) order[u++] = dmskp[x-1];
if( dmskp[x] != 0xFF ) order[u++] = dmskp[x];
if( dmskp[x+1] != 0xFF ) order[u++] = dmskp[x+1];
if( dmskpn[x-1] != 0xFF ) order[u++] = dmskpn[x-1];
if( dmskpn[x] != 0xFF ) order[u++] = dmskpn[x];
if( dmskpn[x+1] != 0xFF ) order[u++] = dmskpn[x+1];
if( u < 4 )
{
dstp[x] = 255;
continue;
}
eedi2_sort_metrics( order, u );
const int mid = ( u & 1 ) ?
order[u>>1] : ( order[(u-1)>>1] + order[u>>1] + 1 ) >> 1;
int sum = 0, count = 0;
const int lim = eedi2_limlut[abs(mid-128)>>2];
for( i = 0; i < u; ++i )
{
if( abs( order[i] - mid ) <= lim )
{
++count;
sum += order[i];
}
}
if( count < 4 || ( count < 5 && dmskp[x] == 0xFF ) )
{
dstp[x] = 255;
continue;
}
dstp[x] = (int)( ( (float)( sum + mid ) / (float)( count + 1 ) ) + 0.5f );
}
dmskpp += dmsk_pitch;
dmskp += dmsk_pitch;
dmskpn += dmsk_pitch;
dstp += dst_pitch;
mskp += msk_pitch;
}
}
/**
* Smoothes out the edge direction map
* @param mskp Pointer to the edge mask
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask being read from
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the expanded edge direction mask
* @param dst_pitch Stride of dstp
* @param height Height of half-height field-sized frame
* @param width Width of dmskp bitmap rows, as opposed to the pdded stride in dmsk_pitch
*/
void eedi2_expand_dir_map( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int height, int width )
{
int x, y, i;
eedi2_bit_blit( dstp, dst_pitch, dmskp, dmsk_pitch, width, height );
dmskp += dmsk_pitch;
unsigned char *dmskpp = dmskp - dmsk_pitch;
unsigned char *dmskpn = dmskp + dmsk_pitch;
dstp += dst_pitch;
mskp += msk_pitch;
for( y = 1; y < height - 1; ++y )
{
for( x = 1; x < width - 1; ++x )
{
if( dmskp[x] != 0xFF || mskp[x] != 0xFF ) continue;
int u = 0, order[9];
if( dmskpp[x-1] != 0xFF ) order[u++] = dmskpp[x-1];
if( dmskpp[x] != 0xFF ) order[u++] = dmskpp[x];
if( dmskpp[x+1] != 0xFF ) order[u++] = dmskpp[x+1];
if( dmskp[x-1] != 0xFF ) order[u++] = dmskp[x-1];
if( dmskp[x+1] != 0xFF ) order[u++] = dmskp[x+1];
if( dmskpn[x-1] != 0xFF ) order[u++] = dmskpn[x-1];
if( dmskpn[x] != 0xFF ) order[u++] = dmskpn[x];
if( dmskpn[x+1] != 0xFF ) order[u++] = dmskpn[x+1];
if( u < 5 ) continue;
eedi2_sort_metrics( order, u );
const int mid = ( u & 1 ) ?
order[u>>1] : ( order[(u-1)>>1] + order[u>>1] + 1 ) >> 1;
int sum = 0, count = 0;
const int lim = eedi2_limlut[abs(mid-128)>>2];
for( i = 0; i < u; ++i )
{
if( abs( order[i] - mid ) <= lim )
{
++count;
sum += order[i];
}
}
if( count < 5 ) continue;
dstp[x] = (int)( ( (float)( sum + mid ) / (float)( count + 1 ) ) + 0.5f );
}
dmskpp += dmsk_pitch;
dmskp += dmsk_pitch;
dmskpn += dmsk_pitch;
dstp += dst_pitch;
mskp += msk_pitch;
}
}
/**
* Re-draws a clearer, less blocky frame-height edge direction mask
* @param mskp Pointer to the edge mask
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask being read from
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the redrawn direction mask
* @param dst_pitch Stride of dstp
* @param tff Whether or not the frame parity is Top Field First
* @param height Height of the full-frame output
* @param width Width of dmskp bitmap rows, as opposed to the pdded stride in dmsk_pitch
*/
void eedi2_mark_directions_2x( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int tff, int height, int width )
{
int x, y, i;
memset( dstp, 255, dst_pitch * height );
dstp += dst_pitch * ( 2 - tff );
dmskp += dmsk_pitch * ( 1 - tff );
mskp += msk_pitch * ( 1 - tff );
unsigned char *dmskpn = dmskp + dmsk_pitch * 2;
unsigned char *mskpn = mskp + msk_pitch * 2;
for( y = 2 - tff; y < height - 1; y += 2 )
{
for( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0xFF && mskpn[x] != 0xFF ) continue;
int v = 0, order[6];
if( dmskp[x-1] != 0xFF ) order[v++] = dmskp[x-1];
if( dmskp[x] != 0xFF ) order[v++] = dmskp[x];
if( dmskp[x+1] != 0xFF ) order[v++] = dmskp[x+1];
if( dmskpn[x-1] != 0xFF ) order[v++] = dmskpn[x-1];
if( dmskpn[x] != 0xFF ) order[v++] = dmskpn[x];
if( dmskpn[x+1] != 0xFF ) order[v++] = dmskpn[x+1];
if( v < 3 ) continue;
else
{
eedi2_sort_metrics( order, v );
const int mid = ( v & 1 ) ? order[v>>1] : ( order[(v-1)>>1] + order[v>>1]+1) >> 1;
const int lim = eedi2_limlut[abs(mid-128)>>2];
int u = 0;
if( abs( dmskp[x-1] - dmskpn[x-1] ) <= lim ||
dmskp[x-1] == 0xFF || dmskpn[x-1] == 0xFF )
++u;
if( abs( dmskp[x] - dmskpn[x] ) <= lim ||
dmskp[x] == 0xFF || dmskpn[x] == 0xFF )
++u;
if( abs( dmskp[x+1] - dmskpn[x-1] ) <= lim ||
dmskp[x+1] == 0xFF || dmskpn[x+1] == 0xFF)
++u;
if( u < 2 ) continue;
int count = 0, sum = 0;
for( i = 0; i < v; ++i )
{
if( abs( order[i] - mid ) <= lim )
{
++count;
sum += order[i];
}
}
if( count < v - 2 || count < 2 ) continue;
dstp[x] = (int)( ( (float)( sum + mid ) / (float)( count + 1 ) ) + 0.5f );
}
}
mskp += msk_pitch * 2;
mskpn += msk_pitch * 2;
dstp += dst_pitch * 2;
dmskp += dmsk_pitch * 2;
dmskpn += dmsk_pitch * 2;
}
}
/**
* Filters the frane-height edge direction mask
* @param mskp Pointer to the edge mask
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask being read from
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the filtered direction mask
* @param dst_pitch Stride of dstp
* @param field Field to filter
* @param height Height of the full-frame output
* @param width Width of dmskp bitmap rows, as opposed to the pdded stride in dmsk_pitch
*/
void eedi2_filter_dir_map_2x( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int field, int height, int width )
{
int x, y, i;
eedi2_bit_blit( dstp, dst_pitch, dmskp, dmsk_pitch, width, height );
dmskp += dmsk_pitch * ( 2 - field );
unsigned char *dmskpp = dmskp - dmsk_pitch * 2;
unsigned char *dmskpn = dmskp + dmsk_pitch * 2;
mskp += msk_pitch * ( 1 - field );
unsigned char *mskpn = mskp + msk_pitch * 2;
dstp += dst_pitch * ( 2 - field );
for( y = 2 - field; y < height - 1; y += 2 )
{
for( x = 1; x < width - 1; ++x )
{
if( mskp[x] != 0xFF && mskpn[x] != 0xFF ) continue;
int u = 0, order[9];
if( y > 1 )
{
if( dmskpp[x-1] != 0xFF ) order[u++] = dmskpp[x-1];
if( dmskpp[x] != 0xFF ) order[u++] = dmskpp[x];
if( dmskpp[x+1] != 0xFF ) order[u++] = dmskpp[x+1];
}
if( dmskp[x-1] != 0xFF ) order[u++] = dmskp[x-1];
if( dmskp[x] != 0xFF ) order[u++] = dmskp[x];
if( dmskp[x+1] != 0xFF ) order[u++] = dmskp[x+1];
if( y < height - 2 )
{
if( dmskpn[x-1] != 0xFF ) order[u++] = dmskpn[x-1];
if( dmskpn[x] != 0xFF ) order[u++] = dmskpn[x];
if( dmskpn[x+1] != 0xFF ) order[u++] = dmskpn[x+1];
}
if( u < 4 )
{
dstp[x] = 255;
continue;
}
eedi2_sort_metrics( order, u );
const int mid = ( u & 1 ) ? order[u>>1] : (order[(u-1)>>1] + order[u>>1] + 1 ) >> 1;
int sum = 0, count = 0;
const int lim = eedi2_limlut[abs(mid-128)>>2];
for( i = 0; i < u; ++i )
{
if( abs( order[i] - mid ) <= lim )
{
++count;
sum += order[i];
}
}
if( count < 4 || ( count < 5 && dmskp[x] == 0xFF ) )
{
dstp[x] = 255;
continue;
}
dstp[x] = (int)( ( (float)( sum + mid ) / (float)( count + 1 ) ) + 0.5f );
}
mskp += msk_pitch * 2;
mskpn += msk_pitch * 2;
dmskpp += dmsk_pitch * 2;
dmskp += dmsk_pitch * 2;
dmskpn += dmsk_pitch * 2;
dstp += dst_pitch * 2;
}
}
/**
* Smoothes out the frame-height edge direction mask
* @param mskp Pointer to the edge mask
* @param msk_pitch Stride of mskp
* @param dmskp Pointer to the edge direction mask being read from
* @param dmsk_pitch Stride of dmskp
* @param dstp Pointer to the destination to store the expanded direction mask
* @param dst_pitch Stride of dstp
* @param field Field to filter
* @param height Height of the full-frame output
* @param width Width of dmskp bitmap rows, as opposed to the pdded stride in dmsk_pitch
*/
void eedi2_expand_dir_map_2x( uint8_t * mskp, int msk_pitch, uint8_t * dmskp, int dmsk_pitch,
uint8_t * dstp, int dst_pitch, int field, int height, int width )
{
int x, y, i;
eedi2_bit_blit( dstp, dst_pitch, dmskp, dmsk_pitch, width, height );
dmskp += dmsk_pitch * ( 2 - field );
unsigned char *dmskpp = dmskp - dmsk_pitch * 2;
unsigned char *dmskpn = dmskp + dmsk_pitch * 2;
mskp += msk_pitch * ( 1 - field );
unsigned char *mskpn = mskp + msk_pitch * 2;
dstp += dst_pitch * ( 2 - field );
for( y = 2 - field; y < height - 1; y += 2)
{
for( x = 1; x < width - 1; ++x )
{
if( dmskp[x] != 0xFF || ( mskp[x] != 0xFF && mskpn[x] != 0xFF ) ) continue;
int u = 0, order[9];
if( y > 1 )
{
if( dmskpp[x-1] != 0xFF ) order[u++] = dmskpp[x-1];
if( dmskpp[x] != 0xFF ) order[u++] = dmskpp[x];
if( dmskpp[x+1] != 0xFF ) order[u++] = dmskpp[x+1];
}
if( dmskp[x-1] != 0xFF ) order[u++] = dmskp[x-1];
if( dmskp[x+1] != 0xFF ) order[u++] = dmskp[x+1];
if( y < height - 2 )
{
if( dmskpn[x-1] != 0xFF) order[u++] = dmskpn[x-1];
if( dmskpn[x] != 0xFF) order[u++] = dmskpn[x];
if( dmskpn[x+1] != 0xFF) order[u++] = dmskpn[x+1];
}
if( u < 5 ) continue;
eedi2_sort_metrics( order, u );
const int mid = ( u & 1 ) ? order[u>>1] : ( order[(u-1)>>1] + order[u>>1] + 1 ) >> 1;
int sum = 0, count = 0;
const int lim = eedi2_limlut[abs(mid-128)>>2];
for( i = 0; i < u; ++i )
{
if( abs( order[i] - mid ) <= lim )
{
++count;
sum += order[i];
}
}
if( count < 5 ) continue;
dstp[x] = (int)( ( (float)( sum + mid ) / (float)( count + 1 ) ) + 0.5f );
}
mskp += msk_pitch * 2;
mskpn += msk_pitch * 2;
dmskpp += dmsk_pitch * 2;
dmskp += dmsk_pitch * 2;