1 /*
2 	Telecide plugin for Avisynth -- recovers original progressive
3 	frames from  telecined streams. The filter operates by matching
4 	fields and automatically adapts to phase/pattern changes.
5 
6 	Copyright (C) 2003 Donald A. Graft
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation.
11 
12 	This program is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 	GNU General Public License for more details.
16 
17 	You should have received a copy of the GNU General Public License
18 	along with this program; if not, write to the Free Software
19 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 
22 #include "ADM_default.h"
23 #include "ADM_coreVideoFilter.h"
24 #include "ADM_videoFilterCache.h"
25 #include "DIA_factory.h"
26 #include "telec.h"
27 
28 #define DrawString drawString
29 #define BitBlt BitBlit
30 
31 #undef DEBUG_PATTERN_GUIDANCE
32 
33 #undef WINDOWED_MATCH
34 
35 #define MAX_CYCLE 6
36 #define BLKSIZE 24
37 #define BLKSIZE_TIMES2 (2 * BLKSIZE)
38 #define GUIDE_NONE 0
39 #define GUIDE_32 1
40 #define GUIDE_22 2
41 #define GUIDE_32322 3
42 #define AHEAD 0
43 #define BEHIND 1
44 #define POST_NONE 0
45 #define POST_METRICS 1
46 #define POST_FULL 2
47 #define POST_FULL_MAP 3
48 #define POST_FULL_NOMATCH 4
49 #define POST_FULL_NOMATCH_MAP 5
50 #define CACHE_SIZE 100000
51 #define P 0
52 #define C 1
53 #define N 2
54 #define PBLOCK 3
55 #define CBLOCK 4
56 
57 #define NO_BACK 0
58 #define BACK_ON_COMBED 1
59 #define ALWAYS_BACK 2
60 
61 #define OutputDebugString(x) aprintf("%s\n",x)
62 typedef uint8_t* PVideoFrame ;
63 
64 
65 struct CACHE_ENTRY
66 {
67 	unsigned int frame;
68 	unsigned int metrics[5];
69 	unsigned int chosen;
70 };
71 
72 struct PREDICTION
73 {
74 	unsigned int metric;
75 	unsigned int phase;
76 	unsigned int predicted;
77 	unsigned int predicted_metric;
78 };
79 
80 
81 
82 /**
83     \class Telecide
84 
85 */
86 class  Telecide:public ADM_coreVideoFilterCached
87 {
88 protected:
89         teleCide           configuration;
90 protected:
91         bool tff;
92         int xblocks, yblocks;
93     #ifdef WINDOWED_MATCH
94         unsigned int *matchc, *matchp, highest_matchc, highest_matchp;
95     #endif
96         unsigned int *sumc, *sump, highest_sumc, highest_sump;
97         int vmetric;
98 
99         bool film, override, inpattern, found;
100 
101         int chosen;
102         unsigned int p,c,np,pblock,cblock,npblock,nc,ncblock;
103         float mismatch;
104         char status[80];
105         // Metrics cache.
106         struct CACHE_ENTRY *cache;
107 
108         // Pattern guidance data.
109         int cycle;
110         struct PREDICTION pred[MAX_CYCLE+1];
111 
112         // For output message formatting.
113         char buf[255];
114 
115 public:
116                             Telecide(ADM_coreVideoFilter *previous,CONFcouple *conf);
117                             ~Telecide();
118         bool                goToTime(uint64_t usSeek);
119         virtual const char   *getConfiguration(void);                   /// Return  current configuration as a human readable string
120         virtual bool         getNextFrame(uint32_t *fn,ADMImage *image);    /// Return the next image
121         virtual bool         getCoupledConf(CONFcouple **couples) ;   /// Return the current filter configuration
122 		virtual void setCoupledConf(CONFcouple *couples);
123         virtual bool         configure(void) ;           /// Start graphical user interface
124 
125 protected:
126 	void CalculateMetrics(int frame, ADMImage *fcurrent, ADMImage *fprevious);
127 	void Show(ADMImage *dst, int frame);
128 	void Debug(int frame);
129 
130 
131 	void PutChosen(int frame, unsigned int chosen);
132 
133     bool CachePurge(void);
134 	void CacheInsert(int frame, unsigned int p, unsigned int pblock,
135 				unsigned int c, unsigned int cblock);
136 
137 	bool CacheQuery(int frame, unsigned int *p, unsigned int *pblock,
138 				unsigned int *c, unsigned int *cblock);
139 
140 	bool PredictHardYUY2(int frame, unsigned int *predicted, unsigned int *predicted_metric) ;
141 
142 	struct PREDICTION *PredictSoftYUY2(int frame);
143 
144 	void WriteHints(unsigned char *dst, bool film, bool inpattern);
145 
146     bool interpolatePlane(ADMImage *dst, ADM_PLANE plane);
147     bool blendPlane(ADMImage *dst, ADMImage *src,ADM_PLANE plane);
148 
149 
150 };
151