File: filter_smartdeinter.c

package info (click to toggle)
transcode 3%3A1.1.7-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,468 kB
  • ctags: 14,721
  • sloc: ansic: 117,006; sh: 11,468; xml: 2,849; makefile: 1,898; perl: 1,492; pascal: 526; php: 191; python: 144; sed: 43
file content (1078 lines) | stat: -rw-r--r-- 29,699 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
/*
    filter_smartdeinter.c

    This file is part of transcode, a video stream processing tool

    Smart Deinterlacing Filter for VirtualDub -- performs deinterlacing only
    in moving picture areas, allowing full resolution in static areas.
    Copyright (C) 1999-2001 Donald A. Graft
    Miscellaneous suggestions and optimizations by Avery Lee.
    Useful suggestions by Hans Zimmer, Jim Casaburi, Ondrej Kavka,
	and Gunnar Thalin. Field-only differencing based on algorithm by
	Gunnar Thalin.

    modified 2002 by Tilmann Bitterberg for use with transcode

    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.

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

    The author can be contacted at:
    Donald Graft
    neuron2@home.com.
    http://sauron.mordor.net/dgraft/
*/

#define MOD_NAME    "filter_smartdeinter.so"
#define MOD_VERSION "v2.7b (2003-02-01)"
#define MOD_CAP     "VirtualDub's smart deinterlacer"
#define MOD_AUTHOR  "Donald Graft, Tilmann Bitterberg"

#include "transcode.h"
#include "filter.h"
#include "libtc/libtc.h"
#include "libtc/optstr.h"

#include "libtcvideo/tcvideo.h"


static vob_t *vob=NULL;

/* vdub compat */
typedef unsigned int	Pixel;
typedef unsigned int	Pixel32;
typedef unsigned char	Pixel8;
typedef int		PixCoord;
typedef	int		PixDim;
typedef	int		PixOffset;


///////////////////////////////////////////////////////////////////////////

#define FRAME_ONLY 0
#define FIELD_ONLY 1
#define FRAME_AND_FIELD 2

typedef struct MyFilterData {
	int			*prevFrame;
	int			*saveFrame;
	Pixel32			*convertFrameIn;
	Pixel32			*convertFrameOut;
	unsigned char		*moving;
	unsigned char		*fmoving;
	int			srcPitch;
	int			dstPitch;
	int			motionOnly;
	int 			Blend;
	int 			threshold;
	int			scenethreshold;
	int			fieldShift;
	int			inswap;
	int			outswap;
	int			highq;
	int			diffmode;
	int			colordiff;
	int			noMotion;
	int			cubic;
	int			codec;
	TCVHandle		tcvhandle;
} MyFilterData;

static MyFilterData *mfd;

static void help_optstr(void)
{
   tc_log_info (MOD_NAME, "(%s) help\n"
"* Overview\n"
"    This filter provides a smart, motion-based deinterlacing\n"
"    capability. In static picture areas, interlacing artifacts do not\n"
"    appear, so data from both fields is used to provide full detail. In\n"
"    moving areas, deinterlacing is performed\n"
"\n"
"* Options\n"
"       'threshold' Motion Threshold (0-255) [15]\n"
"  'scenethreshold' Scene Change Threshold (0-255) [100]:\n"
"        'diffmode' Motion Detection (0=frame, 1=field, 2=both) [0] \n"
"       'colordiff' Compare color channels instead of luma (0=off, 1=on) [1]\n"
"      'motionOnly' Show motion areas only (0=off, 1=on) [0]\n"
"           'Blend' Blend instead of interpolate in motion areas (0=off, 1=on) [0]\n"
"           'cubic' Use cubic for interpolation (0=off, 1=on) [0]\n"
"      'fieldShift' Phase shift (0=off, 1=on) [0]\n"
"          'inswap' Field swap before phase shift (0=off, 1=on) [0]\n"
"         'outswap' Field swap after phase shift (0=off, 1=on) [0]\n"
"           'highq' Motion map denoising for field-only (0=off, 1=on) [0]\n"
"        'noMotion' Disable motion processing (0=off, 1=on) [0]\n"
		, MOD_CAP);
}

int tc_filter(frame_list_t *ptr_, char *options)
{
  vframe_list_t *ptr = (vframe_list_t *)ptr_;

  //----------------------------------
  //
  // filter init
  //
  //----------------------------------


  if(ptr->tag & TC_FILTER_INIT) {

	unsigned int width, height;

	if((vob = tc_get_vob())==NULL) return(-1);


	mfd = tc_zalloc(sizeof(MyFilterData));

	if (!mfd) {
		tc_log_error(MOD_NAME, "No memory!");
		return (-1);
	}

	width  = vob->im_v_width;
	height = vob->im_v_height;

	/* default values */
	mfd->threshold      = 15;
	mfd->scenethreshold = 100;
	mfd->highq          = 0;
	mfd->diffmode       = FRAME_ONLY;
	mfd->colordiff      = 1;
	mfd->noMotion       = 0;
	mfd->cubic          = 0;
	mfd->codec          = vob->im_v_codec;

	if (options != NULL) {

	  if(verbose) tc_log_info(MOD_NAME, "options=%s", options);

	  optstr_get (options, "motionOnly",     "%d",  &mfd->motionOnly       );
	  optstr_get (options, "Blend",          "%d",  &mfd->Blend            );
	  optstr_get (options, "threshold",      "%d",  &mfd->threshold        );
	  optstr_get (options, "scenethreshold", "%d",  &mfd->scenethreshold   );
	  optstr_get (options, "fieldShift",     "%d",  &mfd->fieldShift       );
	  optstr_get (options, "inswap",         "%d",  &mfd->inswap           );
	  optstr_get (options, "outswap",        "%d",  &mfd->outswap          );
	  optstr_get (options, "noMotion",       "%d",  &mfd->noMotion         );
	  optstr_get (options, "highq",          "%d",  &mfd->highq            );
	  optstr_get (options, "diffmode",       "%d",  &mfd->diffmode         );
	  optstr_get (options, "colordiff",      "%d",  &mfd->colordiff        );
	  optstr_get (options, "cubic",          "%d",  &mfd->cubic            );

	  if (optstr_lookup (options, "help") != NULL) {
		  help_optstr();
	  }
	}

	if (verbose > 1) {

	  tc_log_info (MOD_NAME, " Smart Deinterlacer Filter Settings (%dx%d):", width, height);
	  tc_log_info (MOD_NAME, "        motionOnly = %d", mfd->motionOnly);
	  tc_log_info (MOD_NAME, "             Blend = %d", mfd->Blend);
	  tc_log_info (MOD_NAME, "         threshold = %d", mfd->threshold);
	  tc_log_info (MOD_NAME, "    scenethreshold = %d", mfd->scenethreshold);
	  tc_log_info (MOD_NAME, "        fieldShift = %d", mfd->fieldShift);
	  tc_log_info (MOD_NAME, "            inswap = %d", mfd->inswap);
	  tc_log_info (MOD_NAME, "           outswap = %d", mfd->outswap);
	  tc_log_info (MOD_NAME, "          noMotion = %d", mfd->noMotion);
	  tc_log_info (MOD_NAME, "             highq = %d", mfd->highq);
	  tc_log_info (MOD_NAME, "          diffmode = %d", mfd->diffmode);
	  tc_log_info (MOD_NAME, "         colordiff = %d", mfd->colordiff);
	  tc_log_info (MOD_NAME, "             cubic = %d", mfd->cubic);
	}

	/* fetch memory */

	mfd->convertFrameIn = tc_zalloc (width * height * sizeof(Pixel32));
	mfd->convertFrameOut = tc_zalloc (width * height * sizeof(Pixel32));

	if (mfd->diffmode == FRAME_ONLY || mfd->diffmode == FRAME_AND_FIELD)
	{
		mfd->prevFrame = tc_zalloc (width*height*sizeof(int));
	}

	if (mfd->fieldShift ||
		(mfd->inswap && !mfd->outswap) || (!mfd->inswap && mfd->outswap))
	{
		mfd->saveFrame = tc_malloc (width*height*sizeof(int));
	}

	if (!mfd->noMotion)
	{
		mfd->moving = tc_zalloc (sizeof(unsigned char)*width*height);
	}

	if (mfd->highq)
	{
		mfd->fmoving = tc_malloc (sizeof(unsigned char)*width*height);
	}

	mfd->tcvhandle = tcv_init();

	// filter init ok.
	if(verbose) tc_log_info(MOD_NAME, "%s %s", MOD_VERSION, MOD_CAP);

	return 0;

  } /* TC_FILTER_INIT */


  if(ptr->tag & TC_FILTER_GET_CONFIG) {
      char buf[255];
      optstr_filter_desc (options, MOD_NAME, MOD_CAP, MOD_VERSION, MOD_AUTHOR, "VRYE", "1");

      tc_snprintf (buf, sizeof(buf), "%d", mfd->motionOnly);
      optstr_param (options, "motionOnly", "Show motion areas only" ,"%d", buf, "0", "1");
      tc_snprintf (buf, sizeof(buf), "%d", mfd->Blend);
      optstr_param (options, "Blend", "Blend instead of interpolate in motion areas", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->threshold);
      optstr_param (options, "threshold", "Motion Threshold", "%d", buf, "0", "255" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->scenethreshold);
      optstr_param (options, "scenethreshold", "Scene Change Threshold", "%d", buf, "0", "255" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->fieldShift);
      optstr_param (options, "fieldShift", "Phase shift", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->inswap);
      optstr_param (options, "inswap", "Field swap before phase shift", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->outswap);
      optstr_param (options, "outswap", "Field swap after phase shift", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->noMotion);
      optstr_param (options, "noMotion", "Disable motion processing", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->highq);
      optstr_param (options, "highq", "Motion map denoising for field-only", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->diffmode);
      optstr_param (options, "diffmode", "Motion Detection (0=frame, 1=field, 2=both)", "%d", buf, "0", "2" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->colordiff);
      optstr_param (options, "colordiff", "Compare color channels instead of luma", "%d", buf, "0", "1" );
      tc_snprintf (buf, sizeof(buf), "%d", mfd->cubic);
      optstr_param (options, "cubic", "Use cubic for interpolation", "%d", buf, "0", "1" );

      return (0);
  }

  if(ptr->tag & TC_FILTER_CLOSE) {

	if (!mfd)
		return 0;

	if (mfd->diffmode == FRAME_ONLY || mfd->diffmode == FRAME_AND_FIELD)
	{
		if (mfd->prevFrame)
			free(mfd->prevFrame);
		mfd->prevFrame = NULL;
	}

	if (mfd->fieldShift ||
		(mfd->inswap && !mfd->outswap) || (!mfd->inswap && mfd->outswap))
	{
		if (mfd->saveFrame)
			free(mfd->saveFrame);
		mfd->saveFrame = NULL;
	}

	if (!mfd->noMotion)
	{
		if (mfd->moving)
			free(mfd->moving);
		mfd->moving = NULL;
	}

	if (mfd->highq)
	{
		if (mfd->fmoving)
			free(mfd->fmoving);
		mfd->fmoving = NULL;
	}

	if (mfd->convertFrameIn) {
		free (mfd->convertFrameIn);
		mfd->convertFrameIn = NULL;
	}

	if (mfd->convertFrameOut) {
		free (mfd->convertFrameOut);
		mfd->convertFrameOut = NULL;
	}

	tcv_free(mfd->tcvhandle);

	if (mfd)
		free(mfd);

	return 0;

  } /* TC_FILTER_CLOSE */

///////////////////////////////////////////////////////////////////////////

      if(ptr->tag & TC_PRE_M_PROCESS && ptr->tag & TC_VIDEO && !(ptr->attributes & TC_FRAME_IS_SKIPPED)) {

	const int		srcpitch = ptr->v_width*sizeof(Pixel32);
	const int		srcpitchtimes2 = 2 * srcpitch;
	const int		dstpitch = ptr->v_width*sizeof(Pixel32);
	const int		dstpitchtimes2 = 2 * dstpitch;

	const PixDim		w = ptr->v_width;
	const int		wminus1 = w - 1;
	const int		wtimes2 = w * 2;
	const int		wtimes4 = w * 4;

	const PixDim		h = ptr->v_height;
	const int		hminus1 = h - 1;
	const int		hminus3 = h - 3;
	const int		hover2 = h / 2;

	Pixel32			*src, *dst, *srcminus, *srcplus, *srcminusminus=NULL, *srcplusplus=NULL;
	unsigned char		*moving, *movingminus, *movingplus;
	unsigned char		*fmoving;
	int			*saved=NULL, *sv;
	Pixel32 		*src1=NULL, *src2=NULL, *s1, *s2;
	Pixel32 		*dst1=NULL, *dst2=NULL, *d1, *d2;
	int			*prev;
	int			scenechange;
	long			count;
	int			x, y;
	long			prevValue, nextValue, luma=0, lumap, luman;
	Pixel32			p0, p1, p2;
	long			r, g, b, rp, gp, bp, rn, gn, bn, T;
	long			rpp, gpp, bpp, rnn, gnn, bnn, R, G, B;
	unsigned char		frMotion, fiMotion;
	int			copyback;
	int			cubic = mfd->cubic;

	Pixel32 * dst_buf;
	Pixel32 * src_buf;

	tcv_convert(mfd->tcvhandle, ptr->video_buf,
		    (uint8_t *)mfd->convertFrameIn,
		    ptr->v_width, ptr->v_height,
		    mfd->codec==CODEC_YUV ? IMG_YUV_DEFAULT : IMG_RGB24,
		    ac_endian()==AC_LITTLE_ENDIAN ? IMG_BGRA32 : IMG_ARGB32);

	src_buf = mfd->convertFrameIn;
	dst_buf = mfd->convertFrameOut;

	/* If we are performing Advanced Processing... */
	if (mfd->inswap || mfd->outswap || mfd->fieldShift)
	{
		/* Advanced Processing is used typically to clean up PAL video
		   which has erroneously been digitized with the field phase off by
		   one field. The result is that the frames look interlaced,
		   but really if we can phase shift by one field, we'll get back
		   the original progressive frames. Also supported are field swaps
		   before and/or after the phase shift to accommodate different
		   capture cards and telecining methods, as explained in the
		   help file. Finally, the user can optionally disable full
		   motion processing after this processing. */
		copyback = 1;
		if (!mfd->fieldShift)
		{
			/* No phase shift enabled, but we have swap(s) enabled. */
			if (mfd->inswap && mfd->outswap)
			{
				if (mfd->noMotion)
				{
					/* Swapping twice is a null operation. */
					src1 = src_buf;
					dst1 = dst_buf;
					for (y = 0; y < h; y++)
					{
						ac_memcpy(dst1, src1, wtimes4);
						src1 = (Pixel *)((char *)src1 + srcpitch);
						dst1 = (Pixel *)((char *)dst1 + dstpitch);
					}
					goto filter_done;
				}
				else
				{
					copyback = 0;
				}
			}
			else
			{
				/* Swap fields. */
				src1 = (Pixel32 *)((char *)src_buf + srcpitch);
				saved = mfd->saveFrame + w;
				for (y = 0; y < hover2; y++)
				{
					ac_memcpy(saved, src1, wtimes4);
					src1 = (Pixel *)((char *)src1 + srcpitchtimes2);
					saved += wtimes2;
				}
				src1 = src_buf;
				dst1 = (Pixel32 *)((char *)dst_buf+ dstpitch);
				for (y = 0; y < hover2; y++)
				{
					ac_memcpy(dst1, src1, wtimes4);
					src1 = (Pixel *)((char *)src1 + srcpitchtimes2);
					dst1 = (Pixel *)((char *)dst1 + dstpitchtimes2);
				}
				dst1 = dst_buf;
				saved = mfd->saveFrame + w;
				for (y = 0; y < hover2; y++)
				{
					ac_memcpy(dst1, saved, wtimes4);
					dst1 = (Pixel *)((char *)dst1 + dstpitchtimes2);
					saved += wtimes2;
				}
			}
		}
		/* If we reach here, then phase shift has been enabled. */
		else
		{
			switch (mfd->inswap | (mfd->outswap << 1))
			{
			case 0:
				/* No inswap, no outswap. */
				src1 = src_buf;
				src2 = (Pixel32 *)((char *)src1 + srcpitch);
				dst1 = (Pixel32 *)((char *)dst_buf+ dstpitch);
				dst2 = dst_buf;
				saved = mfd->saveFrame + w;
				break;
			case 1:
				/* Inswap, no outswap. */
				src1 = (Pixel32 *)((char *)src_buf + srcpitch);
				src2 = src_buf;
				dst1 = (Pixel32 *)((char *)dst_buf+ dstpitch);
				dst2 = dst_buf;
				saved = mfd->saveFrame;
				break;
			case 2:
				/* No inswap, outswap. */
				src1 = src_buf;
				src2 = (Pixel32 *)((char *)src_buf + srcpitch);
				dst1 = dst_buf;
				dst2 = (Pixel32 *)((char *)dst_buf + dstpitch);
				saved = mfd->saveFrame + w;
				break;
			case 3:
				/* Inswap, outswap. */
				src1 = (Pixel32 *)((char *)src_buf + srcpitch);
				src2 = src_buf;
				dst1 = dst_buf;
				dst2 = (Pixel32 *)((char *)dst_buf + dstpitch);
				saved = mfd->saveFrame;
				break;
			}

			s1 = src1;
			d1 = dst1;
			for (y = 0; y < hover2; y++)
			{
				ac_memcpy(d1, s1, wtimes4);
				s1 = (Pixel *)((char *)s1 + srcpitchtimes2);
				d1 = (Pixel *)((char *)d1 + dstpitchtimes2);
			}

			/* If this is not the first frame, copy the buffered field
			   of the last frame to the output. This creates a correct progressive
			   output frame. If this is the first frame, a buffered field is not
			   available, so interpolate the field from the current field. */
			if (ptr->id <= 1 )
			{
				s1 = src1;
				d2 = dst2;
				for (y = 0; y < hover2; y++)
				{
					ac_memcpy(d2, s1, wtimes4);
					s1 = (Pixel *)((char *)s1 + srcpitchtimes2);
					d2 = (Pixel *)((char *)d2 + dstpitchtimes2);
				}
			}
			else
			{
				d2 = dst2;
				sv = saved;
				for (y = 0; y < hover2; y++)
				{
					ac_memcpy(d2, sv, wtimes4);
					sv += wtimes2;
					d2 = (Pixel *)((char *)d2 + dstpitchtimes2);
				}
			}
			/* Finally, save the unused field of the current frame in the buffer.
			   It will be used to create the next frame. */
			s2 = src2;
			sv = saved;
			for (y = 0; y < hover2; y++)
			{
				ac_memcpy(sv, s2, wtimes4);
				sv += wtimes2;
				s2 = (Pixel *)((char *)s2 + srcpitchtimes2);
			}
		}
		if (mfd->noMotion) goto filter_done;

		if (copyback)
		{
			/* We're going to do motion processing also, so copy
			   the result back into the src bitmap. */
			src1 = dst_buf;
			dst1 = src_buf;
			for (y = 0; y < h; y++)
			{
				ac_memcpy(dst1, src1, wtimes4);
				src1 = (Pixel *)((char *)src1 + srcpitch);
				dst1 = (Pixel *)((char *)dst1 + dstpitch);
			}
		}
	}
	else if (mfd->noMotion)
	{
		/* Well, I suppose somebody might select no advanced processing options
		   but tick disable motion processing. This covers that. */
		src1 = src_buf;
		dst1 = dst_buf;
		for (y = 0; y < h; y++)
		{
			ac_memcpy(dst1, src1, srcpitch);
			src1 = (Pixel *)((char *)src1 + srcpitch);
			dst1 = (Pixel *)((char *)dst1 + dstpitch);
		}
		goto filter_done;
	}

	/* End advanced processing mode code. Now do full motion-adaptive deinterlacing. */

	/* Not much deinterlacing to do if there aren't at least 2 lines. */
	if (h < 2) goto filter_done;

	count = 0;
	if (mfd->diffmode == FRAME_ONLY || mfd->diffmode == FRAME_AND_FIELD)
	{
		/* Skip first and last lines, they'll get a free ride. */
		src = (Pixel *)((char *)src_buf + srcpitch);
		srcminus = (Pixel *)((char *)src - srcpitch);
		prev = mfd->prevFrame + w;
		moving = mfd->moving + w;
		for (y = 1; y < hminus1; y++)
		{
			x = 0;
			do
			{
				// First check frame motion.
				// Set the moving flag if the diff exceeds the configured
				// threshold.
				moving[x] = 0;
				frMotion = 0;
				prevValue = *prev;
				if (!mfd->colordiff)
				{
					r = (src[x] >> 16) & 0xff;
					g = (src[x] >> 8) & 0xff;
					b = src[x] & 0xff;
					luma = (76 * r + 30 * b + 150 * g) >> 8;
					if (abs(luma - prevValue) > mfd->threshold) frMotion = 1;
				}
				else
				{
					b = src[x] & 0xff;
					bp = prevValue & 0xff;
					if (abs(b - bp) > mfd->threshold) frMotion = 1;
					else
					{
						r = (src[x] >>16) & 0xff;
						rp = (prevValue >> 16) & 0xff;
						if (abs(r - rp) > mfd->threshold) frMotion = 1;
						else
						{
							g = (src[x] >> 8) & 0xff;
							gp = (prevValue >> 8) & 0xff;
							if (abs(g - gp) > mfd->threshold) frMotion = 1;
						}
					}
				}

				// Now check field motion if applicable.
				if (mfd->diffmode == FRAME_ONLY) moving[x] = frMotion;
				else
				{
					fiMotion = 0;
					if (y & 1)
						prevValue = srcminus[x];
					else
						prevValue = *(prev + w);
					if (!mfd->colordiff)
					{
						r = (src[x] >> 16) & 0xff;
						g = (src[x] >> 8) & 0xff;
						b = src[x] & 0xff;
						luma = (76 * r + 30 * b + 150 * g) >> 8;
						if (abs(luma - prevValue) > mfd->threshold) fiMotion = 1;
					}
					else
					{
						b = src[x] & 0xff;
						bp = prevValue & 0xff;
						if (abs(b - bp) > mfd->threshold) fiMotion = 1;
						else
						{
							r = (src[x] >> 16) & 0xff;
							rp = (prevValue >> 16) & 0xff;
							if (abs(r - rp) > mfd->threshold) fiMotion = 1;
							else
							{
								g = (src[x] >> 8) & 0xff;
								gp = (prevValue >> 8) & 0xff;
								if (abs(g - gp) > mfd->threshold) fiMotion = 1;
							}
						}
					}
					moving[x] = (fiMotion && frMotion);
				}
				if (!mfd->colordiff)
					*prev++ = luma;
				else
					*prev++ = src[x];
				/* Keep a count of the number of moving pixels for the
				   scene change detection. */
				if (moving[x]) count++;
			} while(++x < w);
			src = (Pixel *)((char *)src + srcpitch);
			srcminus = (Pixel *)((char *)srcminus + srcpitch);
			moving += w;
		}

		/* Determine whether a scene change has occurred. */
		if ((100L * count) / (h * w) >= mfd->scenethreshold) scenechange = 1;
		else scenechange = 0;

		/*
		tc_log_msg(MOD_NAME, "Frame (%04d) count (%8ld) sc (%d) calc (%02ld)",
				ptr->id, count, scenechange, (100 * count) / (h * w));
				*/

		/* Perform a denoising of the motion map if enabled. */
		if (!scenechange && mfd->highq)
		{
			int xlo, xhi, ylo, yhi;
			int u, v;
			int N = 5;
			int Nover2 = N/2;
			int sum;
			unsigned char *m;

			// Erode.
			fmoving = mfd->fmoving;
			for (y = 0; y < h; y++)
			{
				for (x = 0; x < w; x++)
				{
					if (!((mfd->moving + y * w)[x]))
					{
						fmoving[x] = 0;
						continue;
					}
					xlo = x - Nover2; if (xlo < 0) xlo = 0;
					xhi = x + Nover2; if (xhi >= w) xhi = wminus1;
					ylo = y - Nover2; if (ylo < 0) ylo = 0;
					yhi = y + Nover2; if (yhi >= h) yhi = hminus1;
					m = mfd->moving + ylo * w;
					sum = 0;
					for (u = ylo; u <= yhi; u++)
					{
						for (v = xlo; v <= xhi; v++)
						{
							sum += m[v];
						}
						m += w;
					}
					if (sum > 9)
						fmoving[x] = 1;
					else
						fmoving[x] = 0;
				}
				fmoving += w;
			}
			// Dilate.
			N = 5;
			Nover2 = N/2;
			moving = mfd->moving;
			for (y = 0; y < h; y++)
			{
				for (x = 0; x < w; x++)
				{
					if (!((mfd->fmoving + y * w)[x]))
					{
						moving[x] = 0;
						continue;
					}
					xlo = x - Nover2; if (xlo < 0) xlo = 0;
					xhi = x + Nover2; if (xhi >= w) xhi = wminus1;
					ylo = y - Nover2; if (ylo < 0) ylo = 0;
					yhi = y + Nover2; if (yhi >= h) yhi = hminus1;
					m = mfd->moving + ylo * w;
					for (u = ylo; u <= yhi; u++)
					{
						for (v = xlo; v <= xhi; v++)
						{
							m[v] = 1;
						}
						m += w;
					}
				}
				moving += w;
			}
		}
	}
	else
	{
		/* Field differencing only mode. */
		T = mfd->threshold * mfd->threshold;
		src = (Pixel *)((char *)src_buf + srcpitch);
		srcminus = (Pixel *)((char *)src - srcpitch);
		srcplus = (Pixel *)((char *)src + srcpitch);
		moving = mfd->moving + w;
		for (y = 1; y < hminus1; y++)
		{
			x = 0;
			do
			{
				// Set the moving flag if the diff exceeds the configured
				// threshold.
				moving[x] = 0;
				if (y & 1)
				{
					// Now check field motion.
					fiMotion = 0;
					nextValue = srcplus[x];
					prevValue = srcminus[x];
					if (!mfd->colordiff)
					{
						r = (src[x] >> 16) & 0xff;
						rp = (prevValue >> 16) & 0xff;
						rn = (nextValue >> 16) & 0xff;
						g = (src[x] >> 8) & 0xff;
						gp = (prevValue >> 8) & 0xff;
						gn = (nextValue >> 8) & 0xff;
						b = src[x] & 0xff;
						bp = prevValue & 0xff;
						bn = nextValue & 0xff;
						luma = (76 * r + 30 * b + 150 * g) >> 8;
						lumap = (76 * rp + 30 * bp + 150 * gp) >> 8;
						luman = (76 * rn + 30 * bn + 150 * gn) >> 8;
						if ((lumap - luma) * (luman - luma) > T)
							moving[x] = 1;
					}
					else
					{
						b = src[x] & 0xff;
						bp = prevValue & 0xff;
						bn = nextValue & 0xff;
						if ((bp - b) * (bn - b) > T) moving[x] = 1;
						else
						{
							r = (src[x] >> 16) & 0xff;
							rp = (prevValue >> 16) & 0xff;
							rn = (nextValue >> 16) & 0xff;
							if ((rp - r) * (rn - r) > T) moving[x] = 1;
							else
							{
								g = (src[x] >> 8) & 0xff;
								gp = (prevValue >> 8) & 0xff;
								gn = (nextValue >> 8) & 0xff;
								if ((gp - g) * (gn - g) > T) moving[x] = 1;
							}
						}
					}
				}
				/* Keep a count of the number of moving pixels for the
				   scene change detection. */
				if (moving[x]) count++;
			} while(++x < w);
			src = (Pixel *)((char *)src + srcpitch);
			srcminus = (Pixel *)((char *)srcminus + srcpitch);
			srcplus = (Pixel *)((char *)srcplus + srcpitch);
			moving += w;
		}

		/* Determine whether a scene change has occurred. */
		if ((100L * count) / (h * w) >= mfd->scenethreshold) scenechange = 1;
		else scenechange = 0;

		/* Perform a denoising of the motion map if enabled. */
		if (!scenechange && mfd->highq)
		{
			int xlo, xhi, ylo, yhi;
			int u, v;
			int N = 5;
			int Nover2 = N/2;
			int sum;
			unsigned char *m;

			// Erode.
			fmoving = mfd->fmoving;
			for (y = 0; y < h; y++)
			{
				for (x = 0; x < w; x++)
				{
					if (!((mfd->moving + y * w)[x]))
					{
						fmoving[x] = 0;
						continue;
					}
					xlo = x - Nover2; if (xlo < 0) xlo = 0;
					xhi = x + Nover2; if (xhi >= w) xhi = wminus1;
					ylo = y - Nover2; if (ylo < 0) ylo = 0;
					yhi = y + Nover2; if (yhi >= h) yhi = hminus1;
					m = mfd->moving + ylo * w;
					sum = 0;
					for (u = ylo; u <= yhi; u++)
					{
						for (v = xlo; v <= xhi; v++)
						{
							sum += m[v];
						}
						m += w;
					}
					if (sum > 9)
						fmoving[x] = 1;
					else
						fmoving[x] = 0;
				}
				fmoving += w;
			}

			// Dilate.
			N = 5;
			Nover2 = N/2;
			moving = mfd->moving;
			for (y = 0; y < h; y++)
			{
				for (x = 0; x < w; x++)
				{
					if (!((mfd->fmoving + y * w)[x]))
					{
						moving[x] = 0;
						continue;
					}
					xlo = x - Nover2; if (xlo < 0) xlo = 0;
					xhi = x + Nover2; if (xhi >= w) xhi = wminus1;
					ylo = y - Nover2; if (ylo < 0) ylo = 0;
					yhi = y + Nover2; if (yhi >= h) yhi = hminus1;
					m = mfd->moving + ylo * w;
					for (u = ylo; u <= yhi; u++)
					{
						for (v = xlo; v <= xhi; v++)
						{
							m[v] = 1;
						}
						m += w;
					}
				}
				moving += w;
			}
		}
	}

	// Render.
    // The first line gets a free ride.
	src = src_buf;
	dst = dst_buf;
	ac_memcpy(dst, src, wtimes4);
	src = (Pixel *)((char *)src_buf + srcpitch);
	srcminus = (Pixel *)((char *)src - srcpitch);
	srcplus = (Pixel *)((char *)src + srcpitch);
	if (cubic)
	{
		srcminusminus = (Pixel *)((char *)src - 3 * srcpitch);
		srcplusplus = (Pixel *)((char *)src + 3 * srcpitch);
	}
	dst = (Pixel *)((char *)dst_buf + dstpitch);
	moving = mfd->moving + w;
	movingminus = moving - w;
	movingplus = moving + w;
	for (y = 1; y < hminus1; y++)
	{
		if (mfd->motionOnly)
		{
			if (mfd->Blend)
			{
				x = 0;
				do {
					if (!(movingminus[x] | moving[x] | movingplus[x]) && !scenechange)
						dst[x] = 0x7f7f7f;
					else
					{
						/* Blend fields. */
						p0 = src[x];
						p0 &= 0x00fefefe;

						p1 = srcminus[x];
						p1 &= 0x00fcfcfc;

						p2 = srcplus[x];
						p2 &= 0x00fcfcfc;

						dst[x] = (p0>>1) + (p1>>2) + (p2>>2);
					}
				} while(++x < w);
			}
			else
			{
				x = 0;
				do {
					if (!(movingminus[x] | moving[x] | movingplus[x]) && !scenechange)
						dst[x] = 0x7f7f7f;
					else if (y & 1)
					{
						if (cubic && (y > 2) && (y < hminus3))
						{
							rpp = (srcminusminus[x] >> 16) & 0xff;
							rp =  (srcminus[x] >> 16) & 0xff;
							rn =  (srcplus[x] >> 16) & 0xff;
							rnn = (srcplusplus[x] >>16) & 0xff;
							gpp = (srcminusminus[x] >> 8) & 0xff;
							gp =  (srcminus[x] >> 8) & 0xff;
							gn =  (srcplus[x] >>8) & 0xff;
							gnn = (srcplusplus[x] >> 8) & 0xff;
							bpp = (srcminusminus[x]) & 0xff;
							bp =  (srcminus[x]) & 0xff;
							bn =  (srcplus[x]) & 0xff;
							bnn = (srcplusplus[x]) & 0xff;
							R = (5 * (rp + rn) - (rpp + rnn)) >> 3;
							if (R > 255) R = 255;
							else if (R < 0) R = 0;
							G = (5 * (gp + gn) - (gpp + gnn)) >> 3;
							if (G > 255) G = 255;
							else if (G < 0) G = 0;
							B = (5 * (bp + bn) - (bpp + bnn)) >> 3;
							if (B > 255) B = 255;
							else if (B < 0) B = 0;
							dst[x] = (R << 16) | (G << 8) | B;
						}
						else
						{
							p1 = srcminus[x];
							p1 &= 0x00fefefe;

							p2 = srcplus[x];
							p2 &= 0x00fefefe;
							dst[x] = (p1>>1) + (p2>>1);
						}
					}
					else
						dst[x] = src[x];
				} while(++x < w);
			}
		}
		else  /* Not motion only */
		{
			if (mfd->Blend)
			{
				x = 0;
				do {
					if (!(movingminus[x] | moving[x] | movingplus[x]) && !scenechange)
						dst[x] = src[x];
					else
					{
						/* Blend fields. */
						p0 = src[x];
						p0 &= 0x00fefefe;

						p1 = srcminus[x];
						p1 &= 0x00fcfcfc;

						p2 = srcplus[x];
						p2 &= 0x00fcfcfc;

						dst[x] = (p0>>1) + (p1>>2) + (p2>>2);
					}
				} while(++x < w);
			}
			else
			{
				// Doing line interpolate. Thus, even lines are going through
				// for moving and non-moving mode. Odd line pixels will be subject
				// to the motion test.
				if (y&1)
				{
					x = 0;
					do {
						if (!(movingminus[x] | moving[x] | movingplus[x]) && !scenechange)
							dst[x] = src[x];
						else
						{
							if (cubic && (y > 2) && (y < hminus3))
							{
								rpp = (srcminusminus[x] >> 16) & 0xff;
								rp =  (srcminus[x] >> 16) & 0xff;
								rn =  (srcplus[x] >> 16) & 0xff;
								rnn = (srcplusplus[x] >>16) & 0xff;
								gpp = (srcminusminus[x] >> 8) & 0xff;
								gp =  (srcminus[x] >> 8) & 0xff;
								gn =  (srcplus[x] >>8) & 0xff;
								gnn = (srcplusplus[x] >> 8) & 0xff;
								bpp = (srcminusminus[x]) & 0xff;
								bp =  (srcminus[x]) & 0xff;
								bn =  (srcplus[x]) & 0xff;
								bnn = (srcplusplus[x]) & 0xff;
								R = (5 * (rp + rn) - (rpp + rnn)) >> 3;
								if (R > 255) R = 255;
								else if (R < 0) R = 0;
								G = (5 * (gp + gn) - (gpp + gnn)) >> 3;
								if (G > 255) G = 255;
								else if (G < 0) G = 0;
								B = (5 * (bp + bn) - (bpp + bnn)) >> 3;
								if (B > 255) B = 255;
								else if (B < 0) B = 0;
								dst[x] = (R << 16) | (G << 8) | B;
							}
							else
							{
								p1 = srcminus[x];
								p1 &= 0x00fefefe;

								p2 = srcplus[x];
								p2 &= 0x00fefefe;

								dst[x] = (p1>>1) + (p2>>1);
							}
						}
					} while(++x < w);
				}
				else
				{
					// Even line; pass it through.
					ac_memcpy(dst, src, wtimes4);
				}
			}
		}
		src = (Pixel *)((char *)src + srcpitch);
		srcminus = (Pixel *)((char *)srcminus + srcpitch);
		srcplus = (Pixel *)((char *)srcplus + srcpitch);
		if (cubic)
		{
			srcminusminus = (Pixel *)((char *)srcminusminus + srcpitch);
			srcplusplus = (Pixel *)((char *)srcplusplus + srcpitch);
		}
		dst = (Pixel *)((char *)dst + dstpitch);
		moving += w;
		movingminus += w;
		movingplus += w;
	}

	// The last line gets a free ride.
	ac_memcpy(dst, src, wtimes4);

filter_done:
	tcv_convert(mfd->tcvhandle, (uint8_t *)mfd->convertFrameOut,
		    ptr->video_buf, ptr->v_width, ptr->v_height,
		    ac_endian()==AC_LITTLE_ENDIAN ? IMG_BGRA32 : IMG_ARGB32,
		    mfd->codec==CODEC_YUV ? IMG_YUV_DEFAULT : IMG_RGB24);

	return 0;
  }
  return 0;
}