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>
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.
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.
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.
22 #include <framework/mlt.h>
28 //#define DEBUG_PATTERN_GUIDANCE
32 #define BLKSIZE_TIMES2 (2 * BLKSIZE)
38 #define POST_METRICS 1
40 #define POST_FULL_MAP 3
41 #define POST_FULL_NOMATCH 4
42 #define POST_FULL_NOMATCH_MAP 5
43 #define CACHE_SIZE 100000
51 #define BACK_ON_COMBED 1
57 unsigned int metrics[5];
65 unsigned int predicted;
66 unsigned int predicted_metric;
71 mlt_properties image_cache;
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;
81 unsigned int *matchc, *matchp, highest_matchc, highest_matchp;
83 unsigned int *sumc, *sump, highest_sumc, highest_sump;
85 unsigned int *overrides, *overrides_p;
86 int film, override, inpattern, found;
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;
96 unsigned int p, c, pblock, cblock, lowest, predicted, predicted_metric;
97 unsigned int np, nc, npblock, ncblock, nframe;
100 unsigned char *crp, *prp;
101 unsigned char *crpU, *prpU;
102 unsigned char *crpV, *prpV;
107 struct CACHE_ENTRY *cache;
109 // Pattern guidance data.
111 struct PREDICTION pred[MAX_CYCLE+1];
113 typedef struct context_s *context;
117 void BitBlt(uint8_t* dstp, int dst_pitch, const uint8_t* srcp,
118 int src_pitch, int row_size, int height)
121 for(y=0;y<height;y++)
123 memcpy(dstp,srcp,row_size);
129 static void Show(context cx, int frame, mlt_properties properties)
134 if (cx->chosen == P) use = 'p';
135 else if (cx->chosen == C) use = 'c';
137 snprintf(buf, sizeof(buf), "Telecide: frame %d: matches: %d %d %d\n", frame, cx->p, cx->c, cx->np);
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);
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 );
148 static void Debug(context cx, int frame)
152 if (cx->chosen == P) use = 'p';
153 else if (cx->chosen == C) use = 'c';
155 fprintf(stderr, "Telecide: frame %d: matches: %d %d %d\n", frame, cx->p, cx->c, cx->np);
157 fprintf(stderr, "Telecide: frame %d: vmetrics: %d %d %d [chosen=%d]\n", frame, cx->pblock, cx->cblock, cx->npblock, cx->vmetric);
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 : "");
165 static void WriteHints(int film, int inpattern, mlt_properties frame_properties)
167 mlt_properties_set_int( frame_properties, "telecide.progressive", film);
168 mlt_properties_set_int( frame_properties, "telecide.in_pattern", inpattern);
171 static void PutChosen(context cx, int frame, unsigned int chosen)
173 int f = frame % CACHE_SIZE;
174 if (frame < 0 || frame > cx->out || cx->cache[f].frame != frame)
176 cx->cache[f].chosen = chosen;
179 static void CacheInsert(context cx, int frame, unsigned int p, unsigned int pblock,
180 unsigned int c, unsigned int cblock)
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;
194 static int CacheQuery(context cx, int frame, unsigned int *p, unsigned int *pblock,
195 unsigned int *c, unsigned int *cblock)
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)
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];
213 static int PredictHardYUY2(context cx, int frame, unsigned int *predicted, unsigned int *predicted_metric)
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 )
219 if (cx->cache[(frame- cx->cycle)%CACHE_SIZE ].chosen == 0xff ||
220 cx->cache[(frame- cx->cycle+1)%CACHE_SIZE].chosen == 0xff)
222 switch ((cx->cache[(frame- cx->cycle)%CACHE_SIZE ].chosen << 4) +
223 (cx->cache[(frame- cx->cycle+1)%CACHE_SIZE].chosen))
227 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
231 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
236 else if ( cx->guide == GUIDE_32 )
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)
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))
258 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
265 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
270 else if ( cx->guide == GUIDE_32322 )
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)
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))
296 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[C];
304 *predicted_metric = cx->cache[frame%CACHE_SIZE].metrics[N];
309 #ifdef DEBUG_PATTERN_GUIDANCE
310 fprintf( stderr, "%s: pos=%d HARD: predicted=%d\n", __FUNCTION__, frame, *predicted);
315 static struct PREDICTION *PredictSoftYUY2(context cx, int frame )
317 // Use heuristics to look forward for a match.
318 int i, j, y, c, n, phase;
321 cx->pred[0].metric = 0xffffffff;
322 if (frame < 0 || frame > cx->out - cx->cycle) return cx->pred;
324 // Look at the next cycle of frames.
325 for (y = frame + 1; y <= frame + cx->cycle; y++)
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];
335 metric = (100 * abs (c - n)) / c;
336 phase = y % cx->cycle;
339 // Place the new candidate phase in sorted order in the list.
340 // Find the insertion point.
342 while (metric > cx->pred[i].metric) i++;
343 // Find the end-of-list marker.
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.
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;
356 // Insert the new candidate data.
357 cx->pred[j].metric = metric;
358 cx->pred[j].phase = phase;
359 if ( cx->guide == GUIDE_32 )
361 switch ((frame % cx->cycle) - phase)
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;
374 else if ( cx->guide == GUIDE_32322 )
376 switch ((frame % cx->cycle) - phase)
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;
392 #ifdef DEBUG_PATTERN_GUIDANCE
393 fprintf( stderr, "%s: pos=%d metric=%d phase=%d\n", __FUNCTION__, frame, metric, phase);
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)
403 int x, y, p, c, tmp1, tmp2, skip;
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;
411 /* Clear the block sums. */
412 for (y = 0; y < cx->yblocks; y++)
414 for (x = 0; x < cx->xblocks; x++)
416 #ifdef WINDOWED_MATCH
417 matchp[y*xblocks+x] = 0;
418 matchc[y*xblocks+x] = 0;
420 cx->sump[y * cx->xblocks + x] = 0;
421 cx->sumc[y * cx->xblocks + x] = 0;
425 /* Find the best field match. Subsample the frames for speed. */
426 currbot0 = fcrp + cx->pitch;
427 currbot2 = fcrp + 3 * cx->pitch;
429 currtop2 = fcrp + 2 * cx->pitch;
430 currtop4 = fcrp + 4 * cx->pitch;
431 prevbot0 = fprp + cx->pitch;
432 prevbot2 = fprp + 3 * cx->pitch;
434 prevtop2 = fprp + 2 * cx->pitch;
435 prevtop4 = fprp + 4 * cx->pitch;
454 // Calculate the field match and film/video metrics.
455 // if (vi.IsYV12()) skip = 1;
457 skip = 1 + ( !cx->chroma );
458 for (y = 0, index = 0; y < cx->h - 4; y+=4)
460 /* Exclusion band. Good for ignoring subtitles. */
461 if (cx->y0 == cx->y1 || y < cx->y0 || y > cx->y1)
463 for (x = 0; x < cx->w;)
466 // index = (y/BLKSIZE)*xblocks + x/BLKSIZE;
468 index = (y/BLKSIZE) * cx->xblocks + x/BLKSIZE_TIMES2;
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);
477 #ifdef WINDOWED_MATCH
478 matchc[index] += diff;
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]);
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);
497 #ifdef WINDOWED_MATCH
498 matchp[index] += diff;
504 vc = (tmp1 < b0[x] && tmp1 < b2[x]) ||
505 (tmp2 > b0[x] && tmp2 > b2[x]);
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;
527 // if (vi.IsYV12() && chroma == true)
531 // for (z = 0; z < 2; z++)
533 // // Do the same for the U plane.
536 // currbot0 = fcrpU + pitchover2;
537 // currbot2 = fcrpU + 3 * pitchover2;
539 // currtop2 = fcrpU + 2 * pitchover2;
540 // currtop4 = fcrpU + 4 * pitchover2;
541 // prevbot0 = fprpU + pitchover2;
542 // prevbot2 = fprpU + 3 * pitchover2;
544 // prevtop2 = fprpU + 2 * pitchover2;
545 // prevtop4 = fprpU + 4 * pitchover2;
549 // currbot0 = fcrpV + pitchover2;
550 // currbot2 = fcrpV + 3 * pitchover2;
552 // currtop2 = fcrpV + 2 * pitchover2;
553 // currtop4 = fcrpV + 4 * pitchover2;
554 // prevbot0 = fprpV + pitchover2;
555 // prevbot2 = fprpV + 3 * pitchover2;
557 // prevtop2 = fprpV + 2 * pitchover2;
558 // prevtop4 = fprpV + 4 * pitchover2;
577 // for (y = 0, index = 0; y < hover2 - 4; y+=4)
579 // /* Exclusion band. Good for ignoring subtitles. */
580 // if (y0 == y1 || y < y0/2 || y > y1/2)
582 // for (x = 0; x < wover2;)
585 // index = (y/BLKSIZE)*xblocks + x/BLKSIZE;
587 // index = (y/BLKSIZE)*xblocks + x/BLKSIZE_TIMES2;
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);
595 //#ifdef WINDOWED_MATCH
596 // matchc[index] += diff;
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]);
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);
615 //#ifdef WINDOWED_MATCH
616 // matchp[index] += diff;
622 // vc = (tmp1 < b0[x] && tmp1 < b2[x]) ||
623 // (tmp2 > b0[x] && tmp2 > b2[x]);
630 // if (!(x&3)) x += 4;
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;
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++)
652 // for (x = 0; x < xblocks; x++)
654 //if (frame == 45 && matchp[y * xblocks + x] > 2500)
656 // sprintf(buf, "%d/%d = %d\n", x, y, matchp[y * xblocks + x]);
657 // OutputDebugString(buf);
659 // if (matchp[y * xblocks + x] > highest_matchp)
661 // highest_matchp = matchp[y * xblocks + x];
665 // highest_matchc = 0;
666 // for (y = 0; y < yblocks; y++)
668 // for (x = 0; x < xblocks; x++)
670 //if (frame == 44 && matchc[y * xblocks + x] > 2500)
672 // sprintf(buf, "%d/%d = %d\n", x, y, matchc[y * xblocks + x]);
673 // OutputDebugString(buf);
675 // if (matchc[y * xblocks + x] > highest_matchc)
677 // highest_matchc = matchc[y * xblocks + x];
684 cx->highest_sump = 0;
685 for (y = 0; y < cx->yblocks; y++)
687 for (x = 0; x < cx->xblocks; x++)
689 if (cx->sump[y * cx->xblocks + x] > cx->highest_sump)
691 cx->highest_sump = cx->sump[y * cx->xblocks + x];
695 cx->highest_sumc = 0;
696 for (y = 0; y < cx->yblocks; y++)
698 for (x = 0; x < cx->xblocks; x++)
700 if (cx->sumc[y * cx->xblocks + x] > cx->highest_sumc)
702 cx->highest_sumc = cx->sumc[y * cx->xblocks + x];
707 #ifdef WINDOWED_MATCH
708 CacheInsert(frame, highest_matchp, highest_sump, highest_matchc, highest_sumc);
710 CacheInsert( cx, frame, p, cx->highest_sump, c, cx->highest_sumc);
715 /** Process the image.
718 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
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;
728 if ( ! cx->is_configured )
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" );
748 int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
752 int guide = mlt_properties_get_int( properties, "guide" );
754 if ( guide == GUIDE_32 )
756 // 24fps to 30 fps telecine.
759 else if ( guide == GUIDE_22 )
761 // PAL guidance (expect the current match to be continued).
764 else if ( guide == GUIDE_32322 )
766 // 25fps to 30 fps telecine.
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 );
780 // Only process if we have no error and a valid colour space
781 if ( error == 0 && *format == mlt_image_yuv422 )
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_frame_get_position( frame );
786 uint8_t *image_copy = mlt_pool_alloc( image_size );
787 memcpy( image_copy, *image, image_size );
789 sprintf( key, "%d", pos );
790 mlt_properties_set_data( cx->image_cache, key, image_copy, image_size, (mlt_destructor)mlt_pool_release, NULL );
792 // Only if we have enough frame images cached
793 if ( pos > 1 && pos > cx->cycle + 1 )
795 pos -= cx->cycle + 1;
796 // Get the current frame image
797 sprintf( key, "%d", pos );
798 cx->fcrp = mlt_properties_get_data( cx->image_cache, key, NULL );
799 if (!cx->fcrp) return error;
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;
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;
813 cx->pitch = *width << 1;
814 cx->pitchover2 = cx->pitch >> 1;
815 cx->pitchtimes4 = cx->pitch << 2;
819 fprintf( stderr, "%s: width must be a multiple of 2\n", __FUNCTION__ );
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;
828 // Ensure that the metrics for the frames
829 // after the current frame are in the cache. They will be used for
833 for ( cx->y = pos + 1; (cx->y <= pos + cx->cycle + 1) && (cx->y <= cx->out); cx->y++ )
835 if ( ! CacheQuery( cx, cx->y, &cx->p, &cx->pblock, &cx->c, &cx->cblock ) )
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 ); }
845 // Check for manual overrides of the field matching.
850 cx->vthresh = cx->vthresh;
851 cx->back = cx->back_saved;
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 ) )
856 CalculateMetrics( cx, pos, cx->fcrp, NULL, NULL, cx->fprp, NULL, NULL );
857 CacheQuery( cx, pos, &cx->p, &cx->pblock, &cx->c, &cx->cblock );
859 if ( ! CacheQuery( cx, cx->nframe, &cx->np, &cx->npblock, &cx->nc, &cx->ncblock ) )
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 );
865 // Determine the best candidate match.
870 if ( cx->back == ALWAYS_BACK && cx->p < cx->lowest )
875 if ( cx->np < cx->lowest )
881 if ((pos == 0 && cx->chosen == P) || (pos == cx->out && cx->chosen == N))
887 // See if we can apply pattern guidance.
888 cx->mismatch = 100.0;
892 if ( pos >= cx->cycle && PredictHardYUY2( cx, pos, &cx->predicted, &cx->predicted_metric) )
897 if ( cx->chosen != cx->predicted )
899 // The chosen frame doesn't match the prediction.
900 if ( cx->predicted_metric == 0 )
903 cx->mismatch = (100.0 * abs( cx->predicted_metric - cx->lowest ) ) / cx->predicted_metric;
904 if ( cx->mismatch < cx->gthresh )
906 // It's close enough, so use the predicted one.
909 cx->chosen = cx->predicted;
921 if ( !cx->hard && cx->guide != GUIDE_22 )
924 struct PREDICTION *pred = PredictSoftYUY2( cx, pos );
926 if ( ( pos <= cx->out - cx->cycle) && ( pred[0].metric != 0xffffffff ) )
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
933 while ( pred[i].metric != 0xffffffff )
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);
940 if ( cx->chosen != cx->predicted )
942 // The chosen frame doesn't match the prediction.
943 if ( cx->predicted_metric == 0 )
946 cx->mismatch = (100.0 * abs( cx->predicted_metric - cx->lowest )) / cx->predicted_metric;
947 if ( (int) cx->mismatch <= cx->gthresh )
949 // It's close enough, so use the predicted one.
952 cx->chosen = cx->predicted;
960 // Looks like we're not in a predictable pattern.
976 // Check the match for progressive versus interlaced.
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;
983 if ( !cx->found && cx->back == BACK_ON_COMBED && cx->vmetric > cx->bthresh && cx->p < cx->lowest )
986 cx->vmetric = cx->pblock;
991 if ( cx->vmetric > cx->vthresh )
993 // After field matching and pattern guidance the frame is still combed.
995 if ( !cx->found && ( cx->post == POST_FULL_NOMATCH || cx->post == POST_FULL_NOMATCH_MAP ) )
998 cx->vmetric = cx->cblock;
1004 cx->vthresh = cx->vthresh_saved;
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]" );
1011 // Assemble and output the reconstructed frame according to the final match.
1013 if ( cx->chosen == N )
1015 // The best match was with the next frame.
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 );
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 );
1027 else if ( cx->chosen == C )
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 );
1033 else if ( ! cx->tff )
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 );
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 );
1046 PutChosen( cx, pos, cx->chosen );
1048 if ( !cx->post || cx->post == POST_METRICS )
1050 if ( cx->force == '+') cx->film = 0;
1051 else if ( cx->force == '-' ) cx->film = 1;
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 != '-')))
1057 unsigned char *dstpp, *dstpn;
1062 // Do first and last lines.
1063 uint8_t *final = mlt_pool_alloc( image_size );
1065 mlt_properties_set_data( frame_properties, "image", final, image_size, (mlt_destructor)mlt_pool_release, NULL );
1066 dstpn = cx->dstp + cx->dpitch;
1067 for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1069 cx->finalp[cx->x] = (((int)cx->dstp[cx->x] + (int)dstpn[cx->x]) >> 1);
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++ )
1076 cx->finalp[cx->x] = (((int)cx->dstp[cx->x] + (int)dstpp[cx->x]) >> 1);
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++ )
1085 for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1087 v1 = (int) cx->dstp[cx->x] - cx->dthresh;
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]))
1094 if ( cx->post == POST_FULL_MAP || cx->post == POST_FULL_NOMATCH_MAP )
1096 if (cx->x & 1) cx->finalp[cx->x] = 128;
1097 else cx->finalp[cx->x] = 235;
1100 cx->finalp[cx->x] = ((int)dstpp[cx->x] + (int)dstpn[cx->x] + (int)cx->dstp[cx->x] + (int)cx->dstp[cx->x]) >> 2;
1102 else cx->finalp[cx->x] = cx->dstp[cx->x];
1104 cx->finalp += cx->dpitch;
1105 cx->dstp += cx->dpitch;
1106 dstpp += cx->dpitch;
1107 dstpn += cx->dpitch;
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);
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 )
1123 for ( cx->x = 0; cx->x < cx->w; cx->x++ )
1125 v1 = (int) cx->dstp[cx->x] - cx->dthresh;
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]))
1131 if ( cx->post == POST_FULL_MAP || cx->post == POST_FULL_NOMATCH_MAP )
1133 if (cx->x & 1) cx->dstp[cx->x] = 128;
1134 else cx->dstp[cx->x] = 235;
1137 cx->dstp[cx->x] = (dstpp[cx->x] + dstpn[cx->x]) >> 1;
1140 cx->dstp += 2 * cx->dpitch;
1141 dstpp += 2 * cx->dpitch;
1142 dstpn += 2 * cx->dpitch;
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);
1150 // Flush frame at tail of period from the cache
1151 sprintf( key, "%d", pos - 1 );
1152 mlt_properties_set_data( cx->image_cache, key, NULL, 0, NULL, NULL );
1156 // Signal the first {cycle} frames as invalid
1157 mlt_properties_set_int( frame_properties, "garbage", 1 );
1160 else if ( error == 0 && *format == mlt_image_yuv420p )
1162 fprintf(stderr,"%s: %d pos %d\n", __FUNCTION__, *width * *height * 3/2, mlt_frame_get_position(frame) );
1168 /** Process the frame object.
1171 static mlt_frame process( mlt_filter this, mlt_frame frame )
1173 // Push the filter on to the stack
1174 mlt_frame_push_service( frame, this );
1176 // Push the frame filter
1177 mlt_frame_push_get_image( frame, get_image );
1182 /** Constructor for the filter.
1185 mlt_filter filter_telecide_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
1187 mlt_filter this = mlt_filter_new( );
1190 this->process = process;
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 );
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 );
1202 for (i = 0; i < CACHE_SIZE; i++)
1204 cx->cache[i].frame = 0xffffffff;
1205 cx->cache[i].chosen = 0xff;
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 );
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 );