]> git.sesse.net Git - mlt/blob - src/modules/plusgpl/filter_telecide.c
Start new plusgpl module from dgraft.
[mlt] / src / modules / plusgpl / filter_telecide.c
1 /*
2  * filter_telecide.c -- Donald Graft's Inverse Telecine Filter
3  * Copyright (C) 2003 Donald A. Graft
4  * Copyright (C) 2008 Dan Dennedy <dan@dennedy.org>
5  * Author: Dan Dennedy <dan@dennedy.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
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 Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <framework/mlt.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 //#define DEBUG_PATTERN_GUIDANCE
29
30 #define MAX_CYCLE 6
31 #define BLKSIZE 24
32 #define BLKSIZE_TIMES2 (2 * BLKSIZE)
33 #define GUIDE_32 1
34 #define GUIDE_22 2
35 #define GUIDE_32322 3
36 #define AHEAD 0
37 #define BEHIND 1
38 #define POST_METRICS 1
39 #define POST_FULL 2
40 #define POST_FULL_MAP 3
41 #define POST_FULL_NOMATCH 4
42 #define POST_FULL_NOMATCH_MAP 5
43 #define CACHE_SIZE 100000
44 #define P 0
45 #define C 1
46 #define N 2
47 #define PBLOCK 3
48 #define CBLOCK 4
49
50 #define NO_BACK 0
51 #define BACK_ON_COMBED 1
52 #define ALWAYS_BACK 2
53
54 struct CACHE_ENTRY
55 {
56         unsigned int frame;
57         unsigned int metrics[5];
58         unsigned int chosen;
59 };
60
61 struct PREDICTION
62 {
63         unsigned int metric;
64         unsigned int phase;
65         unsigned int predicted;
66         unsigned int predicted_metric;
67 };
68
69 struct context_s {
70         int is_configured;
71         mlt_properties image_cache;
72         int out;
73         
74         int tff, chroma, blend, hints, show, debug;
75         float dthresh, gthresh, vthresh, vthresh_saved, bthresh;
76         int y0, y1, nt, guide, post, back, back_saved;
77         int pitch, dpitch, pitchover2, pitchtimes4;
78         int w, h, wover2, hover2, hplus1over2, hminus2;
79         int xblocks, yblocks;
80 #ifdef WINDOWED_MATCH
81         unsigned int *matchc, *matchp, highest_matchc, highest_matchp;
82 #endif
83         unsigned int *sumc, *sump, highest_sumc, highest_sump;
84         int vmetric;
85         unsigned int *overrides, *overrides_p;
86         int film, override, inpattern, found;
87         int force;
88
89         // Used by field matching.
90         unsigned char *fprp, *fcrp, *fcrp_saved, *fnrp;
91 //      unsigned char *fprpU, *fcrpU, *fcrp_savedU, *fnrpU;
92 //      unsigned char *fprpV, *fcrpV, *fcrp_savedV, *fnrpV;
93         unsigned char *dstp, *finalp;
94 //      unsigned char *dstpU, *dstpV;
95         int chosen;
96         unsigned int p, c, pblock, cblock, lowest, predicted, predicted_metric;
97         unsigned int np, nc, npblock, ncblock, nframe;
98         float mismatch;
99         int pframe, x, y;
100         unsigned char *crp, *prp;
101         unsigned char *crpU, *prpU;
102         unsigned char *crpV, *prpV;
103         int hard;
104         char status[80];
105
106         // Metrics cache.
107         struct CACHE_ENTRY *cache;
108
109         // Pattern guidance data.
110         int cycle;
111         struct PREDICTION pred[MAX_CYCLE+1];
112 };
113 typedef struct context_s *context;
114
115
116 static inline
117 void BitBlt(uint8_t* dstp, int dst_pitch, const uint8_t* srcp,
118             int src_pitch, int row_size, int height)
119 {
120         uint32_t y;
121         for(y=0;y<height;y++)
122         {
123                 memcpy(dstp,srcp,row_size);
124                 dstp+=dst_pitch;
125                 srcp+=src_pitch;
126         }
127 }
128
129 static void Show(context cx, int frame, mlt_properties properties)
130 {
131         char use;
132         char buf[512];
133
134         if (cx->chosen == P) use = 'p';
135         else if (cx->chosen == C) use = 'c';
136         else use = 'n';
137         snprintf(buf, sizeof(buf), "Telecide: frame %d: matches: %d %d %d\n", frame, cx->p, cx->c, cx->np);
138         if ( cx->post )
139                 snprintf(buf, sizeof(buf), "%sTelecide: frame %d: vmetrics: %d %d %d [chosen=%d]\n", buf, frame, cx->pblock, cx->cblock, cx->npblock, cx->vmetric);
140         if ( cx->guide )
141                 snprintf(buf, sizeof(buf), "%spattern mismatch=%0.2f%%\n", buf, cx->mismatch);
142         snprintf(buf, sizeof(buf), "%sTelecide: frame %d: [%s %c]%s %s\n", buf, frame, cx->found ? "forcing" : "using", use,
143                 cx->post ? (cx->film ? " [progressive]" : " [interlaced]") : "",
144                 cx->guide ? cx->status : "");
145         mlt_properties_set( properties, "meta.attr.telecide.markup", buf );
146 }
147
148 static void Debug(context cx, int frame)
149 {
150         char use;
151
152         if (cx->chosen == P) use = 'p';
153         else if (cx->chosen == C) use = 'c';
154         else use = 'n';
155         fprintf(stderr, "Telecide: frame %d: matches: %d %d %d\n", frame, cx->p, cx->c, cx->np);
156         if ( cx->post )
157                 fprintf(stderr, "Telecide: frame %d: vmetrics: %d %d %d [chosen=%d]\n", frame, cx->pblock, cx->cblock, cx->npblock, cx->vmetric);
158         if ( cx->guide )
159                 fprintf(stderr, "pattern mismatch=%0.2f%%\n", cx->mismatch); 
160         fprintf(stderr, "Telecide: frame %d: [%s %c]%s %s\n", frame, cx->found ? "forcing" : "using", use,
161                 cx->post ? (cx->film ? " [progressive]" : " [interlaced]") : "",
162                 cx->guide ? cx->status : "");
163 }
164
165 static void WriteHints(int film, int inpattern, mlt_properties frame_properties)
166 {
167         mlt_properties_set_int( frame_properties, "telecide.progressive", film);
168         mlt_properties_set_int( frame_properties, "telecide.in_pattern", inpattern);
169 }
170
171 static void PutChosen(context cx, int frame, unsigned int chosen)
172 {
173         int f = frame % CACHE_SIZE;
174         if (frame < 0 || frame > cx->out || cx->cache[f].frame != frame)
175                 return;
176         cx->cache[f].chosen = chosen;
177 }
178
179 static void CacheInsert(context cx, int frame, unsigned int p, unsigned int pblock,
180                                          unsigned int c, unsigned int cblock)
181 {
182         int f = frame % CACHE_SIZE;
183         if (frame < 0 || frame > cx->out)
184                 fprintf( stderr, "%s: internal error: invalid frame %d for CacheInsert", __FUNCTION__, frame);
185         cx->cache[f].frame = frame;
186         cx->cache[f].metrics[P] = p;
187         if (f) cx->cache[f-1].metrics[N] = p;
188         cx->cache[f].metrics[C] = c;
189         cx->cache[f].metrics[PBLOCK] = pblock;
190         cx->cache[f].metrics[CBLOCK] = cblock;
191         cx->cache[f].chosen = 0xff;
192 }
193
194 static int CacheQuery(context cx, int frame, unsigned int *p, unsigned int *pblock,
195                                         unsigned int *c, unsigned int *cblock)
196 {
197         int f;
198
199         f = frame % CACHE_SIZE;
200         if (frame < 0 || frame > cx->out)
201                 fprintf( stderr, "%s: internal error: invalid frame %d for CacheQuery", __FUNCTION__, frame);
202         if (cx->cache[f].frame != frame)
203         {
204                 return 0;
205         }
206         *p = cx->cache[f].metrics[P];
207         *c = cx->cache[f].metrics[C];
208         *pblock = cx->cache[f].metrics[PBLOCK];
209         *cblock = cx->cache[f].metrics[CBLOCK];
210         return 1;
211 }
212
213 static int PredictHardYUY2(context cx, int frame, unsigned int *predicted, unsigned int *predicted_metric)
214 {
215         // Look for pattern in the actual delivered matches of the previous cycle of frames.
216         // If a pattern is found, use that to predict the current match.
217         if ( cx->guide == GUIDE_22 )
218         {
219                 if (cx->cache[(frame- cx->cycle)%CACHE_SIZE  ].chosen == 0xff ||
220                         cx->cache[(frame- cx->cycle+1)%CACHE_SIZE].chosen == 0xff)
221                         return 0;
222                 switch ((cx->cache[(frame- cx->cycle)%CACHE_SIZE  ].chosen << 4) +
223                                 (cx->cache[(frame- cx->cycle+1)%CACHE_SIZE].chosen))
224                 {
225                 case 0x11:
226                         *predicted = C;
227                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
228                         break;
229                 case 0x22:
230                         *predicted = N;
231                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
232                         break;
233                 default: return 0;
234                 }
235         }
236         else if ( cx->guide == GUIDE_32 )
237         {
238                 if (cx->cache[(frame-cx->cycle)%CACHE_SIZE  ].chosen == 0xff ||
239                         cx->cache[(frame-cx->cycle+1)%CACHE_SIZE].chosen == 0xff ||
240                         cx->cache[(frame-cx->cycle+2)%CACHE_SIZE].chosen == 0xff ||
241                         cx->cache[(frame-cx->cycle+3)%CACHE_SIZE].chosen == 0xff ||
242                         cx->cache[(frame-cx->cycle+4)%CACHE_SIZE].chosen == 0xff)
243                         return 0;
244
245                 switch ((cx->cache[(frame-cx->cycle)%CACHE_SIZE  ].chosen << 16) +
246                                 (cx->cache[(frame-cx->cycle+1)%CACHE_SIZE].chosen << 12) +
247                                 (cx->cache[(frame-cx->cycle+2)%CACHE_SIZE].chosen <<  8) +
248                                 (cx->cache[(frame-cx->cycle+3)%CACHE_SIZE].chosen <<  4) +
249                                 (cx->cache[(frame-cx->cycle+4)%CACHE_SIZE].chosen))
250                 {
251                 case 0x11122:
252                 case 0x11221:
253                 case 0x12211:
254                 case 0x12221: 
255                 case 0x21122: 
256                 case 0x11222: 
257                         *predicted = C;
258                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
259                         break;
260                 case 0x22111:
261                 case 0x21112:
262                 case 0x22112: 
263                 case 0x22211: 
264                         *predicted = N;
265                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
266                         break;
267                 default: return 0;
268                 }
269         }
270         else if ( cx->guide == GUIDE_32322 )
271         {
272                 if (cx->cache[(frame- cx->cycle)%CACHE_SIZE  ].chosen == 0xff ||
273                         cx->cache[(frame- cx->cycle +1)%CACHE_SIZE].chosen == 0xff ||
274                         cx->cache[(frame- cx->cycle +2)%CACHE_SIZE].chosen == 0xff ||
275                         cx->cache[(frame- cx->cycle +3)%CACHE_SIZE].chosen == 0xff ||
276                         cx->cache[(frame- cx->cycle +4)%CACHE_SIZE].chosen == 0xff ||
277                         cx->cache[(frame- cx->cycle +5)%CACHE_SIZE].chosen == 0xff)
278                         return 0;
279
280                 switch ((cx->cache[(frame- cx->cycle)%CACHE_SIZE  ].chosen << 20) +
281                                 (cx->cache[(frame- cx->cycle +1)%CACHE_SIZE].chosen << 16) +
282                                 (cx->cache[(frame- cx->cycle +2)%CACHE_SIZE].chosen << 12) +
283                                 (cx->cache[(frame- cx->cycle +3)%CACHE_SIZE].chosen <<  8) +
284                                 (cx->cache[(frame- cx->cycle +4)%CACHE_SIZE].chosen <<  4) +
285                                 (cx->cache[(frame- cx->cycle +5)%CACHE_SIZE].chosen))
286                 {
287                 case 0x111122:
288                 case 0x111221:
289                 case 0x112211:
290                 case 0x122111:
291                 case 0x111222: 
292                 case 0x112221:
293                 case 0x122211:
294                 case 0x222111: 
295                         *predicted = C;
296                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
297                         break;
298                 case 0x221111:
299                 case 0x211112:
300
301                 case 0x221112: 
302                 case 0x211122: 
303                         *predicted = N;
304                         *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
305                         break;
306                 default: return 0;
307                 }
308         }
309 #ifdef DEBUG_PATTERN_GUIDANCE
310         fprintf( stderr, "%s: pos=%d HARD: predicted=%d\n", __FUNCTION__, frame, *predicted);
311 #endif
312         return 1;
313 }
314
315 static struct PREDICTION *PredictSoftYUY2(context cx, int frame )
316 {
317         // Use heuristics to look forward for a match.
318         int i, j, y, c, n, phase;
319         unsigned int metric;
320
321         cx->pred[0].metric = 0xffffffff;
322         if (frame < 0 || frame > cx->out - cx->cycle) return cx->pred;
323
324         // Look at the next cycle of frames.
325         for (y = frame + 1; y <= frame + cx->cycle; y++)
326         {
327                 // Look for a frame where the current and next match values are
328                 // very close. Those are candidates to predict the phase, because
329                 // that condition should occur only once per cycle. Store the candidate
330                 // phases and predictions in a list sorted by goodness. The list will
331                 // be used by the caller to try the phases in order.
332                 c = cx->cache[y%CACHE_SIZE].metrics[C]; 
333                 n = cx->cache[y%CACHE_SIZE].metrics[N];
334                 if (c == 0) c = 1;
335                 metric = (100 * abs (c - n)) / c;
336                 phase = y % cx->cycle;
337                 if (metric < 5)
338                 {
339                         // Place the new candidate phase in sorted order in the list.
340                         // Find the insertion point.
341                         i = 0;
342                         while (metric > cx->pred[i].metric) i++;
343                         // Find the end-of-list marker.
344                         j = 0;
345                         while (cx->pred[j].metric != 0xffffffff) j++;
346                         // Shift all items below the insertion point down by one to make
347                         // room for the insertion.
348                         j++;
349                         for (; j > i; j--)
350                         {
351                                 cx->pred[j].metric = cx->pred[j-1].metric;
352                                 cx->pred[j].phase = cx->pred[j-1].phase;
353                                 cx->pred[j].predicted = cx->pred[j-1].predicted;
354                                 cx->pred[j].predicted_metric = cx->pred[j-1].predicted_metric;
355                         }
356                         // Insert the new candidate data.
357                         cx->pred[j].metric = metric;
358                         cx->pred[j].phase = phase;
359                         if ( cx->guide == GUIDE_32 )
360                         {
361                                 switch ((frame % cx->cycle) - phase)
362                                 {
363                                 case -4: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
364                                 case -3: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
365                                 case -2: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
366                                 case -1: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
367                                 case  0: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
368                                 case +1: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
369                                 case +2: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
370                                 case +3: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
371                                 case +4: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
372                                 }
373                         }
374                         else if ( cx->guide == GUIDE_32322 )
375                         {
376                                 switch ((frame % cx->cycle) - phase)
377                                 {
378                                 case -5: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
379                                 case -4: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
380                                 case -3: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
381                                 case -2: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
382                                 case -1: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
383                                 case  0: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
384                                 case +1: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
385                                 case +2: cx->pred[j].predicted = N; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N]; break; 
386                                 case +3: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
387                                 case +4: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
388                                 case +5: cx->pred[j].predicted = C; cx->pred[j].predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C]; break; 
389                                 }
390                         }
391                 }
392 #ifdef DEBUG_PATTERN_GUIDANCE
393                 fprintf( stderr, "%s: pos=%d metric=%d phase=%d\n", __FUNCTION__, frame, metric, phase);
394 #endif
395         }
396         return cx->pred;
397 }
398
399 static
400 void CalculateMetrics(context cx, int frame, unsigned char *fcrp, unsigned char *fcrpU, unsigned char *fcrpV,
401                                         unsigned char *fprp, unsigned char *fprpU, unsigned char *fprpV)
402 {
403         int x, y, p, c, tmp1, tmp2, skip;
404         int vc;
405     unsigned char *currbot0, *currbot2, *prevbot0, *prevbot2;
406         unsigned char *prevtop0, *prevtop2, *prevtop4, *currtop0, *currtop2, *currtop4;
407         unsigned char *a0, *a2, *b0, *b2, *b4;
408         unsigned int diff, index;
409 #       define T 4
410
411         /* Clear the block sums. */
412         for (y = 0; y < cx->yblocks; y++)
413         {
414                 for (x = 0; x < cx->xblocks; x++)
415                 {
416 #ifdef WINDOWED_MATCH
417                         matchp[y*xblocks+x] = 0;
418                         matchc[y*xblocks+x] = 0;
419 #endif
420                         cx->sump[y * cx->xblocks + x] = 0;
421                         cx->sumc[y * cx->xblocks + x] = 0;
422                 }
423         }
424
425         /* Find the best field match. Subsample the frames for speed. */
426         currbot0  = fcrp + cx->pitch;
427         currbot2  = fcrp + 3 * cx->pitch;
428         currtop0 = fcrp;
429         currtop2 = fcrp + 2 * cx->pitch;
430         currtop4 = fcrp + 4 * cx->pitch;
431         prevbot0  = fprp + cx->pitch;
432         prevbot2  = fprp + 3 * cx->pitch;
433         prevtop0 = fprp;
434         prevtop2 = fprp + 2 * cx->pitch;
435         prevtop4 = fprp + 4 * cx->pitch;
436         if ( cx->tff )
437         {
438                 a0 = prevbot0;
439                 a2 = prevbot2;
440                 b0 = currtop0;
441                 b2 = currtop2;
442                 b4 = currtop4;
443         }
444         else
445         {
446                 a0 = currbot0;
447                 a2 = currbot2;
448                 b0 = prevtop0;
449                 b2 = prevtop2;
450                 b4 = prevtop4;
451         }
452         p = c = 0;
453
454         // Calculate the field match and film/video metrics.
455 //      if (vi.IsYV12()) skip = 1;
456 //      else 
457         skip = 1 + ( !cx->chroma );
458         for (y = 0, index = 0; y < cx->h - 4; y+=4)
459         {
460                 /* Exclusion band. Good for ignoring subtitles. */
461                 if (cx->y0 == cx->y1 || y < cx->y0 || y > cx->y1)
462                 {
463                         for (x = 0; x < cx->w;)
464                         {
465 //                              if (vi.IsYV12())
466 //                                      index = (y/BLKSIZE)*xblocks + x/BLKSIZE;
467 //                              else
468                                         index = (y/BLKSIZE) * cx->xblocks + x/BLKSIZE_TIMES2;
469
470                                 // Test combination with current frame.
471                                 tmp1 = ((long)currbot0[x] + (long)currbot2[x]);
472 //                              diff = abs((long)currtop0[x] - (tmp1 >> 1));
473                                 diff = abs((((long)currtop0[x] + (long)currtop2[x] + (long)currtop4[x])) - (tmp1 >> 1) - tmp1);
474                                 if (diff > cx->nt)
475                                 {
476                                         c += diff;
477 #ifdef WINDOWED_MATCH
478                                         matchc[index] += diff;
479 #endif
480                                 }
481
482                                 tmp1 = currbot0[x] + T;
483                                 tmp2 = currbot0[x] - T;
484                                 vc = (tmp1 < currtop0[x] && tmp1 < currtop2[x]) ||
485                                          (tmp2 > currtop0[x] && tmp2 > currtop2[x]);
486                                 if (vc)
487                                 {
488                                         cx->sumc[index]++;
489                                 }
490
491                                 // Test combination with previous frame.
492                                 tmp1 = ((long)a0[x] + (long)a2[x]);
493                                 diff = abs((((long)b0[x] + (long)b2[x] + (long)b4[x])) - (tmp1 >> 1) - tmp1);
494                                 if (diff > cx->nt)
495                                 {
496                                         p += diff;
497 #ifdef WINDOWED_MATCH
498                                         matchp[index] += diff;
499 #endif
500                                 }
501
502                                 tmp1 = a0[x] + T;
503                                 tmp2 = a0[x] - T;
504                                 vc = (tmp1 < b0[x] && tmp1 < b2[x]) ||
505                                          (tmp2 > b0[x] && tmp2 > b2[x]);
506                                 if (vc)
507                                 {
508                                         cx->sump[index]++;
509                                 }
510
511                                 x += skip;
512                                 if (!(x&3)) x += 4;
513                         }
514                 }
515                 currbot0 += cx->pitchtimes4;
516                 currbot2 += cx->pitchtimes4;
517                 currtop0 += cx->pitchtimes4;
518                 currtop2 += cx->pitchtimes4;
519                 currtop4 += cx->pitchtimes4;
520                 a0               += cx->pitchtimes4;
521                 a2               += cx->pitchtimes4;
522                 b0               += cx->pitchtimes4;
523                 b2               += cx->pitchtimes4;
524                 b4               += cx->pitchtimes4;
525         }
526
527 //      if (vi.IsYV12() && chroma == true)
528 //      {
529 //              int z;
530 //
531 //              for (z = 0; z < 2; z++)
532 //              {
533 //                      // Do the same for the U plane.
534 //                      if (z == 0)
535 //                      {
536 //                              currbot0  = fcrpU + pitchover2;
537 //                              currbot2  = fcrpU + 3 * pitchover2;
538 //                              currtop0 = fcrpU;
539 //                              currtop2 = fcrpU + 2 * pitchover2;
540 //                              currtop4 = fcrpU + 4 * pitchover2;
541 //                              prevbot0  = fprpU + pitchover2;
542 //                              prevbot2  = fprpU + 3 * pitchover2;
543 //                              prevtop0 = fprpU;
544 //                              prevtop2 = fprpU + 2 * pitchover2;
545 //                              prevtop4 = fprpU + 4 * pitchover2;
546 //                      }
547 //                      else
548 //                      {
549 //                              currbot0  = fcrpV + pitchover2;
550 //                              currbot2  = fcrpV + 3 * pitchover2;
551 //                              currtop0 = fcrpV;
552 //                              currtop2 = fcrpV + 2 * pitchover2;
553 //                              currtop4 = fcrpV + 4 * pitchover2;
554 //                              prevbot0  = fprpV + pitchover2;
555 //                              prevbot2  = fprpV + 3 * pitchover2;
556 //                              prevtop0 = fprpV;
557 //                              prevtop2 = fprpV + 2 * pitchover2;
558 //                              prevtop4 = fprpV + 4 * pitchover2;
559 //                      }
560 //                      if (tff == true)
561 //                      {
562 //                              a0 = prevbot0;
563 //                              a2 = prevbot2;
564 //                              b0 = currtop0;
565 //                              b2 = currtop2;
566 //                              b4 = currtop4;
567 //                      }
568 //                      else
569 //                      {
570 //                              a0 = currbot0;
571 //                              a2 = currbot2;
572 //                              b0 = prevtop0;
573 //                              b2 = prevtop2;
574 //                              b4 = prevtop4;
575 //                      }
576 //
577 //                      for (y = 0, index = 0; y < hover2 - 4; y+=4)
578 //                      {
579 //                              /* Exclusion band. Good for ignoring subtitles. */
580 //                              if (y0 == y1 || y < y0/2 || y > y1/2)
581 //                              {
582 //                                      for (x = 0; x < wover2;)
583 //                                      {
584 //                                              if (vi.IsYV12())
585 //                                                      index = (y/BLKSIZE)*xblocks + x/BLKSIZE;
586 //                                              else
587 //                                                      index = (y/BLKSIZE)*xblocks + x/BLKSIZE_TIMES2;
588 //
589 //                                              // Test combination with current frame.
590 //                                              tmp1 = ((long)currbot0[x] + (long)currbot2[x]);
591 //                                              diff = abs((((long)currtop0[x] + (long)currtop2[x] + (long)currtop4[x])) - (tmp1 >> 1) - tmp1);
592 //                                              if (diff > nt)
593 //                                              {
594 //                                                      c += diff;
595 //#ifdef WINDOWED_MATCH
596 //                                                      matchc[index] += diff;
597 //#endif
598 //                                              }
599 //
600 //                                              tmp1 = currbot0[x] + T;
601 //                                              tmp2 = currbot0[x] - T;
602 //                                              vc = (tmp1 < currtop0[x] && tmp1 < currtop2[x]) ||
603 //                                                       (tmp2 > currtop0[x] && tmp2 > currtop2[x]);
604 //                                              if (vc)
605 //                                              {
606 //                                                      sumc[index]++;
607 //                                              }
608 //
609 //                                              // Test combination with previous frame.
610 //                                              tmp1 = ((long)a0[x] + (long)a2[x]);
611 //                                              diff = abs((((long)b0[x] + (long)b2[x] + (long)b4[x])) - (tmp1 >> 1) - tmp1);
612 //                                              if (diff > nt)
613 //                                              {
614 //                                                      p += diff;
615 //#ifdef WINDOWED_MATCH
616 //                                                      matchp[index] += diff;
617 //#endif
618 //                                              }
619 //
620 //                                              tmp1 = a0[x] + T;
621 //                                              tmp2 = a0[x] - T;
622 //                                              vc = (tmp1 < b0[x] && tmp1 < b2[x]) ||
623 //                                                       (tmp2 > b0[x] && tmp2 > b2[x]);
624 //                                              if (vc)
625 //                                              {
626 //                                                      sump[index]++;
627 //                                              }
628 //
629 //                                              x ++;
630 //                                              if (!(x&3)) x += 4;
631 //                                      }
632 //                              }
633 //                              currbot0 += 4*pitchover2;
634 //                              currbot2 += 4*pitchover2;
635 //                              currtop0 += 4*pitchover2;
636 //                              currtop2 += 4*pitchover2;
637 //                              currtop4 += 4*pitchover2;
638 //                              a0               += 4*pitchover2;
639 //                              a2               += 4*pitchover2;
640 //                              b0               += 4*pitchover2;
641 //                              b2               += 4*pitchover2;
642 //                              b4               += 4*pitchover2;
643 //                      }
644 //              }
645 //      }
646 //
647 //      // Now find the blocks that have the greatest differences.
648 //#ifdef WINDOWED_MATCH
649 //      highest_matchp = 0;
650 //      for (y = 0; y < yblocks; y++)
651 //      {
652 //              for (x = 0; x < xblocks; x++)
653 //              {
654 //if (frame == 45 && matchp[y * xblocks + x] > 2500)
655 //{
656 //      sprintf(buf, "%d/%d = %d\n", x, y, matchp[y * xblocks + x]);
657 //      OutputDebugString(buf);
658 //}
659 //                      if (matchp[y * xblocks + x] > highest_matchp)
660 //                      {
661 //                              highest_matchp = matchp[y * xblocks + x];
662 //                      }
663 //              }
664 //      }
665 //      highest_matchc = 0;
666 //      for (y = 0; y < yblocks; y++)
667 //      {
668 //              for (x = 0; x < xblocks; x++)
669 //              {
670 //if (frame == 44 && matchc[y * xblocks + x] > 2500)
671 //{
672 //      sprintf(buf, "%d/%d = %d\n", x, y, matchc[y * xblocks + x]);
673 //      OutputDebugString(buf);
674 //}
675 //                      if (matchc[y * xblocks + x] > highest_matchc)
676 //                      {
677 //                              highest_matchc = matchc[y * xblocks + x];
678 //                      }
679 //              }
680 //      }
681 //#endif
682         if ( cx->post )
683         {
684                 cx->highest_sump = 0;
685                 for (y = 0; y < cx->yblocks; y++)
686                 {
687                         for (x = 0; x < cx->xblocks; x++)
688                         {
689                                 if (cx->sump[y * cx->xblocks + x] > cx->highest_sump)
690                                 {
691                                         cx->highest_sump = cx->sump[y * cx->xblocks + x];
692                                 }
693                         }
694                 }
695                 cx->highest_sumc = 0;
696                 for (y = 0; y < cx->yblocks; y++)
697                 {
698                         for (x = 0; x < cx->xblocks; x++)
699                         {
700                                 if (cx->sumc[y * cx->xblocks + x] > cx->highest_sumc)
701                                 {
702                                         cx->highest_sumc = cx->sumc[y * cx->xblocks + x];
703                                 }
704                         }
705                 }
706         }
707 #ifdef WINDOWED_MATCH
708         CacheInsert(frame, highest_matchp, highest_sump, highest_matchc, highest_sumc);
709 #else
710         CacheInsert( cx, frame, p, cx->highest_sump, c, cx->highest_sumc);
711 #endif
712 }
713
714
715 /** Process the image.
716 */
717
718 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
719 {
720         // Get the filter service
721         mlt_filter filter = mlt_frame_pop_service( frame );
722         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
723         mlt_properties frame_properties = mlt_frame_properties( frame );
724         context cx = mlt_properties_get_data( properties, "context", NULL );
725         mlt_service producer = mlt_service_producer( mlt_filter_service( filter ) );
726         cx->out = producer? mlt_producer_get_playtime( MLT_PRODUCER( producer ) ) : 999999;
727
728         if ( ! cx->is_configured )
729         {
730                 cx->back = mlt_properties_get_int( properties, "back" );
731                 cx->chroma = mlt_properties_get_int( properties, "chroma" );
732                 cx->guide = mlt_properties_get_int( properties, "guide" );
733                 cx->gthresh = mlt_properties_get_double( properties, "gthresh" );
734                 cx->post = mlt_properties_get_int( properties, "post" );
735                 cx->vthresh = mlt_properties_get_double( properties, "vthresh" );
736                 cx->bthresh = mlt_properties_get_double( properties, "bthresh" );
737                 cx->dthresh = mlt_properties_get_double( properties, "dthresh" );
738                 cx->blend = mlt_properties_get_int( properties, "blend" );
739                 cx->nt = mlt_properties_get_int( properties, "nt" );
740                 cx->y0 = mlt_properties_get_int( properties, "y0" );
741                 cx->y1 = mlt_properties_get_int( properties, "y1" );
742                 cx->hints = mlt_properties_get_int( properties, "hints" );
743                 cx->debug = mlt_properties_get_int( properties, "debug" );
744                 cx->show = mlt_properties_get_int( properties, "show" );
745         }
746
747         // Get the image
748         int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
749
750         if ( ! cx->sump )
751         {
752                 int guide = mlt_properties_get_int( properties, "guide" );
753                 cx->cycle = 0;
754                 if ( guide == GUIDE_32 )
755                 {
756                         // 24fps to 30 fps telecine.
757                         cx->cycle = 5;
758                 }
759                 else if ( guide == GUIDE_22 )
760                 {
761                         // PAL guidance (expect the current match to be continued).
762                         cx->cycle = 2;
763                 }
764                 else if ( guide == GUIDE_32322 )
765                 {
766                         // 25fps to 30 fps telecine.
767                         cx->cycle = 6;
768                 }
769
770                 cx->xblocks = (*width+BLKSIZE-1) / BLKSIZE;
771                 cx->yblocks = (*height+BLKSIZE-1) / BLKSIZE;
772                 cx->sump = (unsigned int *) mlt_pool_alloc( cx->xblocks * cx->yblocks * sizeof(unsigned int) );
773                 cx->sumc = (unsigned int *) mlt_pool_alloc( cx->xblocks * cx->yblocks * sizeof(unsigned int) );
774                 mlt_properties_set_data( properties, "sump", cx->sump, cx->xblocks * cx->yblocks * sizeof(unsigned int), (mlt_destructor)mlt_pool_release, NULL );
775                 mlt_properties_set_data( properties, "sumc", cx->sumc, cx->xblocks * cx->yblocks * sizeof(unsigned int), (mlt_destructor)mlt_pool_release, NULL );
776                 cx->tff = mlt_properties_get_int( frame_properties, "top_field_first" );
777         //      fprintf(stderr, "%s: TOP FIELD FIRST %d\n", __FUNCTION__, cx->tff );
778         }
779
780         // Only process if we have no error and a valid colour space
781         if ( error == 0 && *format == mlt_image_yuv422 )
782         {
783                 // Put the current image into the image cache, keyed on position
784                 size_t image_size = (*width * *height) << 1;
785                 mlt_position pos = mlt_filter_get_position( filter, frame );
786                 uint8_t *image_copy = mlt_pool_alloc( image_size );
787                 memcpy( image_copy, *image, image_size );
788                 char key[20];
789                 sprintf( key, MLT_POSITION_FMT, pos );
790                 mlt_properties_set_data( cx->image_cache, key, image_copy, image_size, (mlt_destructor)mlt_pool_release, NULL );
791                 
792                 // Only if we have enough frame images cached
793                 if ( pos > 1 && pos > cx->cycle + 1 )
794                 {
795                         pos -= cx->cycle + 1;
796                         // Get the current frame image
797                         sprintf( key, MLT_POSITION_FMT, pos );
798                         cx->fcrp = mlt_properties_get_data( cx->image_cache, key, NULL );
799                         if (!cx->fcrp) return error;
800                          
801                         // Get the previous frame image
802                         cx->pframe = pos == 0 ? 0 : pos - 1;
803                         sprintf( key, "%d", cx->pframe );
804                         cx->fprp = mlt_properties_get_data( cx->image_cache, key, NULL );
805                         if (!cx->fprp) return error;
806                         
807                         // Get the next frame image
808                         cx->nframe = pos > cx->out ? cx->out : pos + 1;
809                         sprintf( key, "%d", cx->nframe );
810                         cx->fnrp = mlt_properties_get_data( cx->image_cache, key, NULL );
811                         if (!cx->fnrp) return error;
812                         
813                         cx->pitch = *width << 1;
814                         cx->pitchover2 = cx->pitch >> 1;
815                         cx->pitchtimes4 = cx->pitch << 2;
816                         cx->w = *width << 1;
817                         cx->h = *height;
818                         if ((cx->w/2) & 1)
819                                 fprintf( stderr, "%s: width must be a multiple of 2\n", __FUNCTION__ );
820                         if (cx->h & 1)
821                                 fprintf( stderr, "%s: height must be a multiple of 2\n", __FUNCTION__ );
822                         cx->wover2 = cx->w/2;
823                         cx->hover2 = cx->h/2;
824                         cx->hplus1over2 = (cx->h+1)/2;
825                         cx->hminus2 = cx->h - 2;
826                         cx->dpitch = cx->pitch;
827                         
828                         // Ensure that the metrics for the frames
829                         // after the current frame are in the cache. They will be used for
830                         // pattern guidance.
831                         if ( cx->guide )
832                         {
833                                 for ( cx->y = pos + 1; (cx->y <= pos + cx->cycle + 1) && (cx->y <= cx->out); cx->y++ )
834                                 {
835                                         if ( ! CacheQuery( cx, cx->y, &cx->p, &cx->pblock, &cx->c, &cx->cblock ) )
836                                         {
837                                                 sprintf( key, "%d", cx->y );
838                                                 cx->crp = (unsigned char *) mlt_properties_get_data( cx->image_cache, key, NULL );
839                                                 sprintf( key, "%d", cx->y ? cx->y - 1 : 1 );
840                                                 cx->prp = (unsigned char *) mlt_properties_get_data( cx->image_cache, key, NULL );
841                                                 CalculateMetrics( cx, cx->y, cx->crp, NULL, NULL, cx->prp, NULL, NULL );                                        }
842                                 }
843                         }
844                         
845                         // Check for manual overrides of the field matching.
846                         cx->found = 0;
847                         cx->film = 1;
848                         cx->override = 0;
849                         cx->inpattern = 0;
850                         cx->vthresh = cx->vthresh;
851                         cx->back = cx->back_saved;
852                         
853                         // Get the metrics for the current-previous (p), current-current (c), and current-next (n) match candidates.
854                         if ( ! CacheQuery( cx, pos, &cx->p, &cx->pblock, &cx->c, &cx->cblock ) )
855                         {
856                                 CalculateMetrics( cx, pos, cx->fcrp, NULL, NULL, cx->fprp, NULL, NULL );
857                                 CacheQuery( cx, pos, &cx->p, &cx->pblock, &cx->c, &cx->cblock );
858                         }                               
859                         if ( ! CacheQuery( cx, cx->nframe, &cx->np, &cx->npblock, &cx->nc, &cx->ncblock ) )
860                         {
861                                 CalculateMetrics( cx, cx->nframe, cx->fnrp, NULL, NULL, cx->fcrp, NULL, NULL );
862                                 CacheQuery( cx, cx->nframe, &cx->np, &cx->npblock, &cx->nc, &cx->ncblock );
863                         }                               
864                         
865                         // Determine the best candidate match.
866                         if ( !cx->found )
867                         {
868                                 cx->lowest = cx->c;
869                                 cx->chosen = C;
870                                 if ( cx->back == ALWAYS_BACK && cx->p < cx->lowest )
871                                 {
872                                         cx->lowest = cx->p;
873                                         cx->chosen = P;
874                                 }
875                                 if ( cx->np < cx->lowest )
876                                 {
877                                         cx->lowest = cx->np;
878                                         cx->chosen = N;
879                                 }
880                         }
881                         if ((pos == 0 && cx->chosen == P) || (pos == cx->out && cx->chosen == N))
882                         {
883                                 cx->chosen = C;
884                                 cx->lowest = cx->c;
885                         }
886
887                         // See if we can apply pattern guidance.
888                         cx->mismatch = 100.0;
889                         if ( cx->guide )
890                         {
891                                 cx->hard = 0;
892                                 if ( pos >= cx->cycle && PredictHardYUY2( cx, pos, &cx->predicted, &cx->predicted_metric) )
893                                 {
894                                         cx->inpattern = 1;
895                                         cx->mismatch = 0.0;
896                                         cx->hard = 1;
897                                         if ( cx->chosen != cx->predicted )
898                                         {
899                                                 // The chosen frame doesn't match the prediction.
900                                                 if ( cx->predicted_metric == 0 )
901                                                         cx->mismatch = 0.0;
902                                                 else
903                                                         cx->mismatch = (100.0 * abs( cx->predicted_metric - cx->lowest ) ) / cx->predicted_metric;
904                                                 if ( cx->mismatch < cx->gthresh )
905                                                 {
906                                                         // It's close enough, so use the predicted one.
907                                                         if ( !cx->found )
908                                                         {
909                                                                 cx->chosen = cx->predicted;
910                                                                 cx->override = 1;
911                                                         }
912                                                 }
913                                                 else
914                                                 {
915                                                         cx->hard = 0;
916                                                         cx->inpattern = 0;
917                                                 }
918                                         }
919                                 }
920                 
921                                 if ( !cx->hard && cx->guide != GUIDE_22 )
922                                 {
923                                         int i;
924                                         struct PREDICTION *pred = PredictSoftYUY2( cx, pos );
925                 
926                                         if ( ( pos <= cx->out - cx->cycle) && ( pred[0].metric != 0xffffffff ) )
927                                         {
928                                                 // Apply pattern guidance.
929                                                 // If the predicted match metric is within defined percentage of the
930                                                 // best calculated one, then override the calculated match with the
931                                                 // predicted match.
932                                                 i = 0;
933                                                 while ( pred[i].metric != 0xffffffff )
934                                                 {
935                                                         cx->predicted = pred[i].predicted;
936                                                         cx->predicted_metric = pred[i].predicted_metric;
937 #ifdef DEBUG_PATTERN_GUIDANCE
938                                                         fprintf(stderr, "%s: pos=%d predicted=%d\n", __FUNCTION__, pos, cx->predicted);
939 #endif
940                                                         if ( cx->chosen != cx->predicted )
941                                                         {
942                                                                 // The chosen frame doesn't match the prediction.
943                                                                 if ( cx->predicted_metric == 0 )
944                                                                         cx->mismatch = 0.0;
945                                                                 else
946                                                                         cx->mismatch = (100.0 * abs( cx->predicted_metric - cx->lowest )) / cx->predicted_metric;
947                                                                 if ( (int) cx->mismatch <= cx->gthresh )
948                                                                 {
949                                                                         // It's close enough, so use the predicted one.
950                                                                         if ( !cx->found )
951                                                                         {
952                                                                                 cx->chosen = cx->predicted;
953                                                                                 cx->override = 1;
954                                                                         }
955                                                                         cx->inpattern = 1;
956                                                                         break;
957                                                                 }
958                                                                 else
959                                                                 {
960                                                                         // Looks like we're not in a predictable pattern.
961                                                                         cx->inpattern = 0;
962                                                                 }
963                                                         }
964                                                         else
965                                                         {
966                                                                 cx->inpattern = 1;
967                                                                 cx->mismatch = 0.0;
968                                                                 break;
969                                                         }
970                                                         i++;
971                                                 }
972                                         }
973                                 }
974                         }
975
976                         // Check the match for progressive versus interlaced.
977                         if ( cx->post )
978                         {
979                                 if (cx->chosen == P) cx->vmetric = cx->pblock;
980                                 else if (cx->chosen == C) cx->vmetric = cx->cblock;
981                                 else if (cx->chosen == N) cx->vmetric = cx->npblock;
982                 
983                                 if ( !cx->found && cx->back == BACK_ON_COMBED && cx->vmetric > cx->bthresh && cx->p < cx->lowest )
984                                 {
985                                         // Backward match.
986                                         cx->vmetric = cx->pblock;
987                                         cx->chosen = P;
988                                         cx->inpattern = 0;
989                                         cx->mismatch = 100;
990                                 }
991                                 if ( cx->vmetric > cx->vthresh )
992                                 {
993                                         // After field matching and pattern guidance the frame is still combed.
994                                         cx->film = 0;
995                                         if ( !cx->found && ( cx->post == POST_FULL_NOMATCH || cx->post == POST_FULL_NOMATCH_MAP ) )
996                                         {
997                                                 cx->chosen = C;
998                                                 cx->vmetric = cx->cblock;
999                                                 cx->inpattern = 0;
1000                                                 cx->mismatch = 100;
1001                                         }
1002                                 }
1003                         }
1004                         cx->vthresh = cx->vthresh_saved;
1005                 
1006                         // Setup strings for debug info.
1007                         if ( cx->inpattern && !cx->override ) strcpy( cx->status, "[in-pattern]" );
1008                         else if ( cx->inpattern && cx->override ) strcpy( cx->status, "[in-pattern*]" );
1009                         else strcpy( cx->status, "[out-of-pattern]" );
1010         
1011                         // Assemble and output the reconstructed frame according to the final match.
1012                         cx->dstp = *image;
1013                         if ( cx->chosen == N )
1014                         {
1015                                 // The best match was with the next frame.
1016                                 if ( cx->tff )
1017                                 {
1018                                         BitBlt( cx->dstp, 2 * cx->dpitch, cx->fnrp, 2 * cx->pitch, cx->w, cx->hover2 );
1019                                         BitBlt( cx->dstp + cx->dpitch, 2 * cx->dpitch, cx->fcrp + cx->pitch, 2 * cx->pitch,     cx->w, cx->hover2 );
1020                                 }
1021                                 else
1022                                 {
1023                                         BitBlt( cx->dstp, 2 * cx->dpitch, cx->fcrp, 2 * cx->pitch, cx->w, cx->hplus1over2 );
1024                                         BitBlt( cx->dstp + cx->dpitch, 2 * cx->dpitch, cx->fnrp + cx->pitch, 2 * cx->pitch,     cx->w, cx->hover2 );
1025                                 }
1026                         }
1027                         else if ( cx->chosen == C )
1028                         {
1029                                 // The best match was with the current frame.
1030                                 BitBlt( cx->dstp, 2 * cx->dpitch, cx->fcrp, 2 * cx->pitch, cx->w, cx->hplus1over2 );
1031                                 BitBlt( cx->dstp + cx->dpitch, 2 * cx->dpitch, cx->fcrp + cx->pitch, 2 * cx->pitch,     cx->w, cx->hover2 );
1032                         }
1033                         else if ( ! cx->tff )
1034                         {
1035                                 // The best match was with the previous frame.
1036                                 BitBlt( cx->dstp, 2 * cx->dpitch, cx->fprp, 2 * cx->pitch, cx->w, cx->hplus1over2 );
1037                                 BitBlt( cx->dstp + cx->dpitch, 2 * cx->dpitch, cx->fcrp + cx->pitch, 2 * cx->pitch, cx->w, cx->hover2 );
1038                         }
1039                         else
1040                         {
1041                                 // The best match was with the previous frame.
1042                                 BitBlt( cx->dstp, 2 * cx->dpitch, cx->fcrp, 2 * cx->pitch, cx->w, cx->hplus1over2 );
1043                                 BitBlt( cx->dstp + cx->dpitch, 2 * cx->dpitch, cx->fprp + cx->pitch, 2 * cx->pitch,     cx->w, cx->hover2 );
1044                         }
1045                         if ( cx->guide )
1046                                 PutChosen( cx, pos, cx->chosen );
1047
1048                         if ( !cx->post || cx->post == POST_METRICS )
1049                         {
1050                                 if ( cx->force == '+') cx->film = 0;
1051                                 else if ( cx->force == '-' ) cx->film = 1;
1052                         }
1053                         else if ((cx->force == '+') ||
1054                                 ((cx->post == POST_FULL || cx->post == POST_FULL_MAP || cx->post == POST_FULL_NOMATCH || cx->post == POST_FULL_NOMATCH_MAP)
1055                                          && (cx->film == 0 && cx->force != '-')))
1056                         {
1057                                 unsigned char *dstpp, *dstpn;
1058                                 int v1, v2;
1059                 
1060                                 if ( cx->blend )
1061                                 {
1062                                         // Do first and last lines.
1063                                         uint8_t *final = mlt_pool_alloc( image_size );
1064                                         cx->finalp = final;
1065                                         mlt_frame_set_image( frame, final, image_size, mlt_pool_release );
1066                                         dstpn = cx->dstp + cx->dpitch;
1067                                         for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1068                                         {
1069                                                 cx->finalp[cx->x] = (((int)cx->dstp[cx->x] + (int)dstpn[cx->x]) >> 1);
1070                                         }
1071                                         cx->finalp = final + (cx->h-1)*cx->dpitch;
1072                                         cx->dstp = *image + (cx->h-1)*cx->dpitch;
1073                                         dstpp = cx->dstp - cx->dpitch;
1074                                         for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1075                                         {
1076                                                 cx->finalp[cx->x] = (((int)cx->dstp[cx->x] + (int)dstpp[cx->x]) >> 1);
1077                                         }
1078                                         // Now do the rest.
1079                                         cx->dstp = *image + cx->dpitch;
1080                                         dstpp = cx->dstp - cx->dpitch;
1081                                         dstpn = cx->dstp + cx->dpitch;
1082                                         cx->finalp = final + cx->dpitch;
1083                                         for ( cx->y = 1; cx->y < cx->h - 1; cx->y++ )
1084                                         {
1085                                                 for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1086                                                 {
1087                                                         v1 = (int) cx->dstp[cx->x] - cx->dthresh;
1088                                                         if ( v1 < 0 )
1089                                                                 v1 = 0; 
1090                                                         v2 = (int) cx->dstp[cx->x] + cx->dthresh;
1091                                                         if (v2 > 235) v2 = 235; 
1092                                                         if ((v1 > dstpp[cx->x] && v1 > dstpn[cx->x]) || (v2 < dstpp[cx->x] && v2 < dstpn[cx->x]))
1093                                                         {
1094                                                                 if ( cx->post == POST_FULL_MAP || cx->post == POST_FULL_NOMATCH_MAP )
1095                                                                 {
1096                                                                         if (cx->x & 1) cx->finalp[cx->x] = 128;
1097                                                                         else cx->finalp[cx->x] = 235;
1098                                                                 }
1099                                                                 else
1100                                                                         cx->finalp[cx->x] = ((int)dstpp[cx->x] + (int)dstpn[cx->x] + (int)cx->dstp[cx->x] + (int)cx->dstp[cx->x]) >> 2;
1101                                                         }
1102                                                         else cx->finalp[cx->x] = cx->dstp[cx->x];
1103                                                 }
1104                                                 cx->finalp += cx->dpitch;
1105                                                 cx->dstp += cx->dpitch;
1106                                                 dstpp += cx->dpitch;
1107                                                 dstpn += cx->dpitch;
1108                                         }
1109                 
1110                 
1111                                         if (cx->show ) Show( cx, pos, frame_properties);
1112                                         if (cx->debug) Debug(cx, pos);
1113                                         if (cx->hints) WriteHints(cx->film, cx->inpattern, frame_properties);
1114                                         goto final;
1115                                 }
1116                 
1117                                 // Interpolate mode.
1118                                 cx->dstp = *image + cx->dpitch;
1119                                 dstpp = cx->dstp - cx->dpitch;
1120                                 dstpn = cx->dstp + cx->dpitch;
1121                                 for ( cx->y = 1; cx->y < cx->h - 1; cx->y+=2 )
1122                                 {
1123                                         for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1124                                         {
1125                                                 v1 = (int) cx->dstp[cx->x] - cx->dthresh;
1126                                                 if (v1 < 0) v1 = 0; 
1127                                                 v2 = (int) cx->dstp[cx->x] + cx->dthresh;
1128                                                 if (v2 > 235) v2 = 235; 
1129                                                 if ((v1 > dstpp[cx->x] && v1 > dstpn[cx->x]) || (v2 < dstpp[cx->x] && v2 < dstpn[cx->x]))
1130                                                 {
1131                                                         if ( cx->post == POST_FULL_MAP || cx->post == POST_FULL_NOMATCH_MAP )
1132                                                         {
1133                                                                 if (cx->x & 1) cx->dstp[cx->x] = 128;
1134                                                                 else cx->dstp[cx->x] = 235;
1135                                                         }
1136                                                         else
1137                                                                 cx->dstp[cx->x] = (dstpp[cx->x] + dstpn[cx->x]) >> 1;
1138                                                 }
1139                                         }
1140                                         cx->dstp += 2 * cx->dpitch;
1141                                         dstpp += 2 * cx->dpitch;
1142                                         dstpn += 2 * cx->dpitch;
1143                                 }
1144                         }
1145                         if (cx->show ) Show( cx, pos, frame_properties);
1146                         if (cx->debug) Debug(cx, pos);
1147                         if (cx->hints) WriteHints(cx->film, cx->inpattern, frame_properties);
1148
1149 final:                  
1150                         // Flush frame at tail of period from the cache
1151                         sprintf( key, MLT_POSITION_FMT, pos - 1 );
1152                         mlt_properties_set_data( cx->image_cache, key, NULL, 0, NULL, NULL );
1153                 }
1154                 else
1155                 {
1156                         // Signal the first {cycle} frames as invalid
1157                         mlt_properties_set_int( frame_properties, "garbage", 1 );
1158                 }
1159         }
1160         else if ( error == 0 && *format == mlt_image_yuv420p )
1161         {
1162                 fprintf(stderr,"%s: %d pos " MLT_POSITION_FMT "\n", __FUNCTION__, *width * *height * 3/2, mlt_frame_get_position(frame) );
1163         }
1164
1165         return error;
1166 }
1167
1168 /** Process the frame object.
1169 */
1170
1171 static mlt_frame process( mlt_filter this, mlt_frame frame )
1172 {
1173         // Push the filter on to the stack
1174         mlt_frame_push_service( frame, this );
1175
1176         // Push the frame filter
1177         mlt_frame_push_get_image( frame, get_image );
1178
1179         return frame;
1180 }
1181
1182 /** Constructor for the filter.
1183 */
1184
1185 mlt_filter filter_telecide_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
1186 {
1187         mlt_filter this = mlt_filter_new( );
1188         if ( this != NULL )
1189         {
1190                 this->process = process;
1191
1192                 // Allocate the context and set up for garbage collection               
1193                 context cx = (context) mlt_pool_alloc( sizeof(struct context_s) );
1194                 memset( cx, 0, sizeof( struct context_s ) );
1195                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
1196                 mlt_properties_set_data( properties, "context", cx, sizeof(struct context_s), (mlt_destructor)mlt_pool_release, NULL );
1197
1198                 // Allocate the metrics cache and set up for garbage collection
1199                 cx->cache = (struct CACHE_ENTRY *) mlt_pool_alloc(CACHE_SIZE * sizeof(struct CACHE_ENTRY ));
1200                 mlt_properties_set_data( properties, "cache", cx->cache, CACHE_SIZE * sizeof(struct CACHE_ENTRY), (mlt_destructor)mlt_pool_release, NULL );
1201                 int i;
1202                 for (i = 0; i < CACHE_SIZE; i++)
1203                 {
1204                         cx->cache[i].frame = 0xffffffff;
1205                         cx->cache[i].chosen = 0xff;
1206                 }
1207                 
1208                 // Allocate the image cache and set up for garbage collection
1209                 cx->image_cache = mlt_properties_new();
1210                 mlt_properties_set_data( properties, "image_cache", cx->image_cache, 0, (mlt_destructor)mlt_properties_close, NULL );
1211                 
1212                 // Initialize the parameter defaults
1213                 mlt_properties_set_int( properties, "guide", 0 );
1214                 mlt_properties_set_int( properties, "back", 0 );
1215                 mlt_properties_set_int( properties, "chroma", 0 );
1216                 mlt_properties_set_int( properties, "post", POST_FULL );
1217                 mlt_properties_set_double( properties, "gthresh", 10.0 );
1218                 mlt_properties_set_double( properties, "vthresh", 50.0 );
1219                 mlt_properties_set_double( properties, "bthresh", 50.0 );
1220                 mlt_properties_set_double( properties, "dthresh", 7.0 );
1221                 mlt_properties_set_int( properties, "blend", 0 );
1222                 mlt_properties_set_int( properties, "nt", 10 );
1223                 mlt_properties_set_int( properties, "y0", 0 );
1224                 mlt_properties_set_int( properties, "y1", 0 );
1225                 mlt_properties_set_int( properties, "hints", 1 );
1226         }
1227         return this;
1228 }
1229