Add support for CTRL_(key) on input.conf
[mplayer/kovensky.git] / libmpcodecs / vf_filmdint.c
blobbdb8d57bdb2dcb4d0f44f8ba1555bdf48e9a2152
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "cpudetect.h"
9 #include "options.h"
11 #include "img_format.h"
12 #include "mp_image.h"
13 #include "vf.h"
14 #include "cmmx.h"
16 #include "libvo/fastmemcpy.h"
18 #define NUM_STORED 4
20 enum pu_field_type_t {
21 PU_1ST_OF_3,
22 PU_2ND_OF_3,
23 PU_3RD_OF_3,
24 PU_1ST_OF_2,
25 PU_2ND_OF_2,
26 PU_INTERLACED
29 struct metrics {
30 /* This struct maps to a packed word 64-bit MMX register */
31 unsigned short int even;
32 unsigned short int odd;
33 unsigned short int noise;
34 unsigned short int temp;
35 } __attribute__ ((aligned (8)));
37 struct frame_stats {
38 struct metrics tiny, low, high, bigger, twox, max;
39 struct { unsigned int even, odd, noise, temp; } sad;
40 unsigned short interlaced_high;
41 unsigned short interlaced_low;
42 unsigned short num_blocks;
45 struct vf_priv_s {
46 unsigned long inframes;
47 unsigned long outframes;
48 enum pu_field_type_t prev_type;
49 unsigned swapped, chroma_swapped;
50 unsigned luma_only;
51 unsigned verbose;
52 unsigned fast;
53 unsigned long w, h, cw, ch, stride, chroma_stride, nplanes;
54 unsigned long sad_thres;
55 unsigned long dint_thres;
56 unsigned char *memory_allocated;
57 unsigned char *planes[2*NUM_STORED][4];
58 unsigned char **old_planes;
59 unsigned long static_idx;
60 unsigned long temp_idx;
61 unsigned long crop_x, crop_y, crop_cx, crop_cy;
62 unsigned long export_count, merge_count;
63 unsigned long num_breaks;
64 unsigned long num_copies;
65 long in_inc, out_dec, iosync;
66 long num_fields;
67 long prev_fields;
68 long notout;
69 long mmx2;
70 unsigned small_bytes[2];
71 unsigned mmx_temp[2];
72 struct frame_stats stats[2];
73 struct metrics thres;
74 char chflag;
75 double diff_time, merge_time, decode_time, vo_time, filter_time;
78 #define PPZ { 2000, 2000, 0, 2000 }
79 #define PPR { 2000, 2000, 0, 2000 }
80 static const struct frame_stats ppzs = {PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,0,0,9999};
81 static const struct frame_stats pprs = {PPR,PPR,PPR,PPR,PPR,PPR,PPR,0,0,9999};
83 #ifndef MIN
84 #define MIN(a,b) (((a)<(b))?(a):(b))
85 #endif
86 #ifndef MAX
87 #define MAX(a,b) (((a)>(b))?(a):(b))
88 #endif
90 #define PDIFFUB(X,Y,T) "movq " #X "," #T "\n\t" \
91 "psubusb " #Y "," #T "\n\t" \
92 "psubusb " #X "," #Y "\n\t" \
93 "paddusb " #Y "," #T "\n\t"
95 #define PDIFFUBT(X,Y,T) "movq " #X "," #T "\n\t" \
96 "psubusb " #Y "," #T "\n\t" \
97 "psubusb " #X "," #Y "\n\t" \
98 "paddusb " #T "," #Y "\n\t"
100 #define PSUMBW(X,T,Z) "movq " #X "," #T "\n\t" \
101 "punpcklbw " #Z "," #X "\n\t" \
102 "punpckhbw " #Z "," #T "\n\t" \
103 "paddw " #T "," #X "\n\t" \
104 "movq " #X "," #T "\n\t" \
105 "psllq $32, " #T "\n\t" \
106 "paddw " #T "," #X "\n\t" \
107 "movq " #X "," #T "\n\t" \
108 "psllq $16, " #T "\n\t" \
109 "paddw " #T "," #X "\n\t" \
110 "psrlq $48, " #X "\n\t"
112 #define PSADBW(X,Y,T,Z) PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
114 #define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
115 #define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
116 #define PMINUBT(X,Y,T) "movq " #Y "," #T "\n\t" \
117 "psubusb " #X "," #T "\n\t" \
118 "psubusb " #T "," #Y "\n\t"
119 #define PAVGB(X,Y) "pavgusb " #X "," #Y "\n\t"
121 static inline void
122 get_metrics_c(unsigned char *a, unsigned char *b, int as, int bs, int lines,
123 struct metrics *m)
125 a -= as;
126 b -= bs;
127 do {
128 cmmx_t old_po = *(cmmx_t*)(a );
129 cmmx_t po = *(cmmx_t*)(b );
130 cmmx_t e = *(cmmx_t*)(b + bs);
131 cmmx_t old_o = *(cmmx_t*)(a + 2*as);
132 cmmx_t o = *(cmmx_t*)(b + 2*bs);
133 cmmx_t ne = *(cmmx_t*)(b + 3*bs);
134 cmmx_t old_no = *(cmmx_t*)(a + 4*as);
135 cmmx_t no = *(cmmx_t*)(b + 4*bs);
137 cmmx_t qup_old_odd = p31avgb(old_o, old_po);
138 cmmx_t qup_odd = p31avgb( o, po);
139 cmmx_t qdown_old_odd = p31avgb(old_o, old_no);
140 cmmx_t qdown_odd = p31avgb( o, no);
142 cmmx_t qup_even = p31avgb(ne, e);
143 cmmx_t qdown_even = p31avgb(e, ne);
145 cmmx_t temp_up_diff = pdiffub(qdown_even, qup_old_odd);
146 cmmx_t noise_up_diff = pdiffub(qdown_even, qup_odd);
147 cmmx_t temp_down_diff = pdiffub(qup_even, qdown_old_odd);
148 cmmx_t noise_down_diff = pdiffub(qup_even, qdown_odd);
150 cmmx_t odd_diff = pdiffub(o, old_o);
151 m->odd += psumbw(odd_diff);
152 m->even += psadbw(e, *(cmmx_t*)(a+as));
154 temp_up_diff = pminub(temp_up_diff, temp_down_diff);
155 temp_up_diff = pminub(temp_up_diff, odd_diff);
156 m->temp += psumbw(temp_up_diff);
157 noise_up_diff = pminub(noise_up_diff, odd_diff);
158 noise_up_diff = pminub(noise_up_diff, noise_down_diff);
160 m->noise += psumbw(noise_up_diff);
161 a += 2*as;
162 b += 2*bs;
163 } while (--lines);
166 static inline void
167 get_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
168 int lines, struct metrics *m)
170 a -= as;
171 b -= bs;
172 do {
173 cmmx_t old_po = (*(cmmx_t*)(a ) >> 1) & ~SIGN_BITS;
174 cmmx_t po = (*(cmmx_t*)(b ) >> 1) & ~SIGN_BITS;
175 cmmx_t old_e = (*(cmmx_t*)(a + as) >> 1) & ~SIGN_BITS;
176 cmmx_t e = (*(cmmx_t*)(b + bs) >> 1) & ~SIGN_BITS;
177 cmmx_t old_o = (*(cmmx_t*)(a + 2*as) >> 1) & ~SIGN_BITS;
178 cmmx_t o = (*(cmmx_t*)(b + 2*bs) >> 1) & ~SIGN_BITS;
179 cmmx_t ne = (*(cmmx_t*)(b + 3*bs) >> 1) & ~SIGN_BITS;
180 cmmx_t old_no = (*(cmmx_t*)(a + 4*as) >> 1) & ~SIGN_BITS;
181 cmmx_t no = (*(cmmx_t*)(b + 4*bs) >> 1) & ~SIGN_BITS;
183 cmmx_t qup_old_odd = p31avgb_s(old_o, old_po);
184 cmmx_t qup_odd = p31avgb_s( o, po);
185 cmmx_t qdown_old_odd = p31avgb_s(old_o, old_no);
186 cmmx_t qdown_odd = p31avgb_s( o, no);
188 cmmx_t qup_even = p31avgb_s(ne, e);
189 cmmx_t qdown_even = p31avgb_s(e, ne);
191 cmmx_t temp_up_diff = pdiffub_s(qdown_even, qup_old_odd);
192 cmmx_t noise_up_diff = pdiffub_s(qdown_even, qup_odd);
193 cmmx_t temp_down_diff = pdiffub_s(qup_even, qdown_old_odd);
194 cmmx_t noise_down_diff = pdiffub_s(qup_even, qdown_odd);
196 cmmx_t odd_diff = pdiffub_s(o, old_o);
197 m->odd += psumbw_s(odd_diff) << 1;
198 m->even += psadbw_s(e, old_e) << 1;
200 temp_up_diff = pminub_s(temp_up_diff, temp_down_diff);
201 temp_up_diff = pminub_s(temp_up_diff, odd_diff);
202 m->temp += psumbw_s(temp_up_diff) << 1;
203 noise_up_diff = pminub_s(noise_up_diff, odd_diff);
204 noise_up_diff = pminub_s(noise_up_diff, noise_down_diff);
206 m->noise += psumbw_s(noise_up_diff) << 1;
207 a += 2*as;
208 b += 2*bs;
209 } while (--lines);
212 static inline void
213 get_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
214 int lines, struct metrics *m)
216 a -= as;
217 b -= bs;
218 do {
219 cmmx_t old_po = (*(cmmx_t*)(a )>>1) & ~SIGN_BITS;
220 cmmx_t po = (*(cmmx_t*)(b )>>1) & ~SIGN_BITS;
221 cmmx_t old_e = (*(cmmx_t*)(a + as)>>1) & ~SIGN_BITS;
222 cmmx_t e = (*(cmmx_t*)(b + bs)>>1) & ~SIGN_BITS;
223 cmmx_t old_o = (*(cmmx_t*)(a + 2*as)>>1) & ~SIGN_BITS;
224 cmmx_t o = (*(cmmx_t*)(b + 2*bs)>>1) & ~SIGN_BITS;
225 cmmx_t ne = (*(cmmx_t*)(b + 3*bs)>>1) & ~SIGN_BITS;
227 cmmx_t down_even = p31avgb_s(e, ne);
228 cmmx_t up_odd = p31avgb_s(o, po);
229 cmmx_t up_old_odd = p31avgb_s(old_o, old_po);
231 cmmx_t odd_diff = pdiffub_s(o, old_o);
232 cmmx_t temp_diff = pdiffub_s(down_even, up_old_odd);
233 cmmx_t noise_diff = pdiffub_s(down_even, up_odd);
235 m->even += psadbw_s(e, old_e) << 1;
236 m->odd += psumbw_s(odd_diff) << 1;
238 temp_diff = pminub_s(temp_diff, odd_diff);
239 noise_diff = pminub_s(noise_diff, odd_diff);
241 m->noise += psumbw_s(noise_diff) << 1;
242 m->temp += psumbw_s(temp_diff) << 1;
243 a += 2*as;
244 b += 2*bs;
245 } while (--lines);
249 static inline void
250 get_block_stats(struct metrics *m, struct vf_priv_s *p, struct frame_stats *s)
252 unsigned two_e = m->even + MAX(m->even , p->thres.even );
253 unsigned two_o = m->odd + MAX(m->odd , p->thres.odd );
254 unsigned two_n = m->noise + MAX(m->noise, p->thres.noise);
255 unsigned two_t = m->temp + MAX(m->temp , p->thres.temp );
257 unsigned e_big = m->even >= (m->odd + two_o + 1)/2;
258 unsigned o_big = m->odd >= (m->even + two_e + 1)/2;
259 unsigned n_big = m->noise >= (m->temp + two_t + 1)/2;
260 unsigned t_big = m->temp >= (m->noise + two_n + 1)/2;
262 unsigned e2x = m->even >= two_o;
263 unsigned o2x = m->odd >= two_e;
264 unsigned n2x = m->noise >= two_t;
265 unsigned t2x = m->temp >= two_n;
267 unsigned ntiny_e = m->even > p->thres.even ;
268 unsigned ntiny_o = m->odd > p->thres.odd ;
269 unsigned ntiny_n = m->noise > p->thres.noise;
270 unsigned ntiny_t = m->temp > p->thres.temp ;
272 unsigned nlow_e = m->even > 2*p->thres.even ;
273 unsigned nlow_o = m->odd > 2*p->thres.odd ;
274 unsigned nlow_n = m->noise > 2*p->thres.noise;
275 unsigned nlow_t = m->temp > 2*p->thres.temp ;
277 unsigned high_e = m->even > 4*p->thres.even ;
278 unsigned high_o = m->odd > 4*p->thres.odd ;
279 unsigned high_n = m->noise > 4*p->thres.noise;
280 unsigned high_t = m->temp > 4*p->thres.temp ;
282 unsigned low_il = !n_big && !t_big && ntiny_n && ntiny_t;
283 unsigned high_il = !n_big && !t_big && nlow_n && nlow_t;
285 if (low_il | high_il) {
286 s->interlaced_low += low_il;
287 s->interlaced_high += high_il;
288 } else {
289 s->tiny.even += ntiny_e;
290 s->tiny.odd += ntiny_o;
291 s->tiny.noise += ntiny_n;
292 s->tiny.temp += ntiny_t;
294 s->low .even += nlow_e ;
295 s->low .odd += nlow_o ;
296 s->low .noise += nlow_n ;
297 s->low .temp += nlow_t ;
299 s->high.even += high_e ;
300 s->high.odd += high_o ;
301 s->high.noise += high_n ;
302 s->high.temp += high_t ;
304 if (m->even >= p->sad_thres) s->sad.even += m->even ;
305 if (m->odd >= p->sad_thres) s->sad.odd += m->odd ;
306 if (m->noise >= p->sad_thres) s->sad.noise += m->noise;
307 if (m->temp >= p->sad_thres) s->sad.temp += m->temp ;
309 s->num_blocks++;
310 s->max.even = MAX(s->max.even , m->even );
311 s->max.odd = MAX(s->max.odd , m->odd );
312 s->max.noise = MAX(s->max.noise, m->noise);
313 s->max.temp = MAX(s->max.temp , m->temp );
315 s->bigger.even += e_big ;
316 s->bigger.odd += o_big ;
317 s->bigger.noise += n_big ;
318 s->bigger.temp += t_big ;
320 s->twox.even += e2x ;
321 s->twox.odd += o2x ;
322 s->twox.noise += n2x ;
323 s->twox.temp += t2x ;
327 static inline struct metrics
328 block_metrics_c(unsigned char *a, unsigned char *b, int as, int bs,
329 int lines, struct vf_priv_s *p, struct frame_stats *s)
331 struct metrics tm;
332 tm.even = tm.odd = tm.noise = tm.temp = 0;
333 get_metrics_c(a, b, as, bs, lines, &tm);
334 if (sizeof(cmmx_t) < 8)
335 get_metrics_c(a+4, b+4, as, bs, lines, &tm);
336 get_block_stats(&tm, p, s);
337 return tm;
340 static inline struct metrics
341 block_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
342 int lines, struct vf_priv_s *p, struct frame_stats *s)
344 struct metrics tm;
345 tm.even = tm.odd = tm.noise = tm.temp = 0;
346 get_metrics_fast_c(a, b, as, bs, lines, &tm);
347 if (sizeof(cmmx_t) < 8)
348 get_metrics_fast_c(a+4, b+4, as, bs, lines, &tm);
349 get_block_stats(&tm, p, s);
350 return tm;
353 static inline struct metrics
354 block_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
355 int lines, struct vf_priv_s *p, struct frame_stats *s)
357 struct metrics tm;
358 tm.even = tm.odd = tm.noise = tm.temp = 0;
359 get_metrics_faster_c(a, b, as, bs, lines, &tm);
360 if (sizeof(cmmx_t) < 8)
361 get_metrics_faster_c(a+4, b+4, as, bs, lines, &tm);
362 get_block_stats(&tm, p, s);
363 return tm;
366 #define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
368 #define BLOCK_METRICS_TEMPLATE() \
369 __asm__ volatile("pxor %mm7, %mm7\n\t" /* The result is colleted in mm7 */ \
370 "pxor %mm6, %mm6\n\t" /* Temp to stay at 0 */ \
371 ); \
372 a -= as; \
373 b -= bs; \
374 do { \
375 __asm__ volatile( \
376 "movq (%0,%2), %%mm0\n\t" \
377 "movq (%1,%3), %%mm1\n\t" /* mm1 = even */ \
378 PSADBW(%%mm1, %%mm0, %%mm4, %%mm6) \
379 "paddusw %%mm0, %%mm7\n\t" /* even diff */ \
380 "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */ \
381 "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */ \
382 "movq (%0), %%mm3\n\t" \
383 "psubusb %4, %%mm3\n\t" \
384 PAVGB(%%mm0, %%mm3) \
385 PAVGB(%%mm0, %%mm3) /* mm3 = qup old odd */ \
386 "movq %%mm0, %%mm5\n\t" \
387 PSADBW(%%mm2, %%mm0, %%mm4, %%mm6) \
388 "psllq $16, %%mm0\n\t" \
389 "paddusw %%mm0, %%mm7\n\t" \
390 "movq (%1), %%mm4\n\t" \
391 "lea (%0,%2,2), %0\n\t" \
392 "lea (%1,%3,2), %1\n\t" \
393 "psubusb %4, %%mm4\n\t" \
394 PAVGB(%%mm2, %%mm4) \
395 PAVGB(%%mm2, %%mm4) /* mm4 = qup odd */ \
396 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */ \
397 "movq (%1,%3), %%mm5\n\t" \
398 "psubusb %4, %%mm5\n\t" \
399 PAVGB(%%mm1, %%mm5) \
400 PAVGB(%%mm5, %%mm1) /* mm1 = qdown even */ \
401 PAVGB((%1,%3), %%mm5) /* mm5 = qup next even */ \
402 PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */ \
403 PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */ \
404 PMINUBT(%%mm2, %%mm3, %%mm0) /* limit temp to odd diff */ \
405 PMINUBT(%%mm2, %%mm4, %%mm0) /* limit noise to odd diff */ \
406 "movq (%1,%3,2), %%mm2\n\t" \
407 "psubusb %4, %%mm2\n\t" \
408 PAVGB((%1), %%mm2) \
409 PAVGB((%1), %%mm2) /* mm2 = qdown odd */ \
410 "movq (%0,%2,2), %%mm1\n\t" \
411 "psubusb %4, %%mm1\n\t" \
412 PAVGB((%0), %%mm1) \
413 PAVGB((%0), %%mm1) /* mm1 = qdown old odd */ \
414 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */ \
415 PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */ \
416 PMINUBT(%%mm4, %%mm2, %%mm0) /* current */ \
417 PMINUBT(%%mm3, %%mm1, %%mm0) /* old */ \
418 PSUMBW(%%mm2, %%mm0, %%mm6) \
419 PSUMBW(%%mm1, %%mm0, %%mm6) \
420 "psllq $32, %%mm2\n\t" \
421 "psllq $48, %%mm1\n\t" \
422 "paddusw %%mm2, %%mm7\n\t" \
423 "paddusw %%mm1, %%mm7\n\t" \
424 : "=r" (a), "=r" (b) \
425 : "r"((x86_reg)as), "r"((x86_reg)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
426 ); \
427 } while (--lines);
429 static inline struct metrics
430 block_metrics_3dnow(unsigned char *a, unsigned char *b, int as, int bs,
431 int lines, struct vf_priv_s *p, struct frame_stats *s)
433 struct metrics tm;
434 #if !HAVE_AMD3DNOW
435 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_3dnow: internal error\n");
436 #else
437 static const unsigned long long ones = 0x0101010101010101ull;
439 BLOCK_METRICS_TEMPLATE();
440 __asm__ volatile("movq %%mm7, %0\n\temms" : "=m" (tm));
441 get_block_stats(&tm, p, s);
442 #endif
443 return tm;
446 #undef PSUMBW
447 #undef PSADBW
448 #undef PMAXUB
449 #undef PMINUBT
450 #undef PAVGB
452 #define PSUMBW(X,T,Z) "psadbw " #Z "," #X "\n\t"
453 #define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
454 #define PMAXUB(X,Y) "pmaxub " #X "," #Y "\n\t"
455 #define PMINUBT(X,Y,T) "pminub " #X "," #Y "\n\t"
456 #define PAVGB(X,Y) "pavgb " #X "," #Y "\n\t"
458 static inline struct metrics
459 block_metrics_mmx2(unsigned char *a, unsigned char *b, int as, int bs,
460 int lines, struct vf_priv_s *p, struct frame_stats *s)
462 struct metrics tm;
463 #if !HAVE_MMX
464 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_mmx2: internal error\n");
465 #else
466 static const unsigned long long ones = 0x0101010101010101ull;
467 x86_reg interlaced;
468 x86_reg prefetch_line = (((long)a>>3) & 7) + 10;
469 #ifdef DEBUG
470 struct frame_stats ts = *s;
471 #endif
472 __asm__ volatile("prefetcht0 (%0,%2)\n\t"
473 "prefetcht0 (%1,%3)\n\t" :
474 : "r" (a), "r" (b),
475 "r" (prefetch_line * as), "r" (prefetch_line * bs));
477 BLOCK_METRICS_TEMPLATE();
479 s->num_blocks++;
480 __asm__ volatile(
481 "movq %3, %%mm0\n\t"
482 "movq %%mm7, %%mm1\n\t"
483 "psubusw %%mm0, %%mm1\n\t"
484 "movq %%mm1, %%mm2\n\t"
485 "paddusw %%mm0, %%mm2\n\t"
486 "paddusw %%mm7, %%mm2\n\t"
487 "pshufw $0xb1, %%mm2, %%mm3\n\t"
488 "pavgw %%mm7, %%mm2\n\t"
489 "pshufw $0xb1, %%mm2, %%mm2\n\t"
490 "psubusw %%mm7, %%mm2\n\t"
491 "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
492 "psubusw %%mm7, %%mm3\n\t"
493 "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
494 "movq %1, %%mm4\n\t"
495 "movq %2, %%mm5\n\t"
496 "psubw %%mm2, %%mm4\n\t"
497 "psubw %%mm3, %%mm5\n\t"
498 "movq %%mm4, %1\n\t"
499 "movq %%mm5, %2\n\t"
500 "pxor %%mm4, %%mm4\n\t"
501 "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
502 "psubusw %%mm0, %%mm1\n\t"
503 "pxor %%mm5, %%mm5\n\t"
504 "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
505 "psubusw %%mm0, %%mm1\n\t"
506 "psubusw %%mm0, %%mm1\n\t"
507 "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
508 "pshufw $0xb1, %%mm2, %%mm0\n\t"
509 "por %%mm2, %%mm0\n\t" /* 1 if not close */
510 "punpckhdq %%mm0, %%mm0\n\t"
511 "movq %%mm4, %%mm2\n\t" /* tttt */
512 "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
513 "por %%mm2, %%mm0\n\t"
514 "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
515 "psrlq $16, %%mm0\n\t"
516 "psrlw $15, %%mm0\n\t"
517 "movd %%mm0, %0\n\t"
518 : "=r" (interlaced), "=m" (s->bigger), "=m" (s->twox)
519 : "m" (p->thres)
522 if (interlaced) {
523 s->interlaced_high += interlaced >> 16;
524 s->interlaced_low += interlaced;
525 } else {
526 __asm__ volatile(
527 "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
528 "psubw %%mm0, %%mm4\n\t"
529 "psubw %%mm0, %%mm5\n\t"
530 "psubw %%mm0, %%mm1\n\t"
531 "paddw %0, %%mm4\n\t"
532 "paddw %1, %%mm5\n\t"
533 "paddw %2, %%mm1\n\t"
534 "movq %%mm4, %0\n\t"
535 "movq %%mm5, %1\n\t"
536 "movq %%mm1, %2\n\t"
537 : "=m" (s->tiny), "=m" (s->low), "=m" (s->high)
540 __asm__ volatile(
541 "pshufw $0, %2, %%mm0\n\t"
542 "psubusw %%mm7, %%mm0\n\t"
543 "pcmpeqw %%mm6, %%mm0\n\t" /* 0 if below sad_thres */
544 "pand %%mm7, %%mm0\n\t"
545 "movq %%mm0, %%mm1\n\t"
546 "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
547 "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
548 "paddd %0, %%mm0\n\t"
549 "paddd %1, %%mm1\n\t"
550 "movq %%mm0, %0\n\t"
551 "movq %%mm1, %1\n\t"
552 : "=m" (s->sad.even), "=m" (s->sad.noise)
553 : "m" (p->sad_thres)
557 __asm__ volatile(
558 "movq %%mm7, (%1)\n\t"
559 PMAXUW((%0), %%mm7)
560 "movq %%mm7, (%0)\n\t"
561 "emms"
562 : : "r" (&s->max), "r" (&tm), "X" (s->max)
563 : "memory"
565 #ifdef DEBUG
566 if (1) {
567 struct metrics cm;
568 a -= 7*as;
569 b -= 7*bs;
570 cm = block_metrics_c(a, b, as, bs, 4, p, &ts);
571 if (!MEQ(tm, cm))
572 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad metrics\n");
573 if (s) {
574 # define CHECK(X) if (!MEQ(s->X, ts.X)) \
575 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
576 CHECK(tiny);
577 CHECK(low);
578 CHECK(high);
579 CHECK(sad);
580 CHECK(max);
583 #endif
584 #endif
585 return tm;
588 static inline int
589 dint_copy_line_mmx2(unsigned char *dst, unsigned char *a, long bos,
590 long cos, int ds, int ss, int w, int t)
592 #if !HAVE_MMX
593 mp_msg(MSGT_VFILTER, MSGL_FATAL, "dint_copy_line_mmx2: internal error\n");
594 return 0;
595 #else
596 unsigned long len = (w+7) >> 3;
597 int ret;
598 __asm__ volatile (
599 "pxor %%mm6, %%mm6 \n\t" /* deinterlaced pixel counter */
600 "movd %0, %%mm7 \n\t"
601 "punpcklbw %%mm7, %%mm7 \n\t"
602 "punpcklwd %%mm7, %%mm7 \n\t"
603 "punpckldq %%mm7, %%mm7 \n\t" /* mm7 = threshold */
604 : /* no output */
605 : "rm" (t)
607 do {
608 __asm__ volatile (
609 "movq (%0), %%mm0\n\t"
610 "movq (%0,%3,2), %%mm1\n\t"
611 "movq %%mm0, (%2)\n\t"
612 "pmaxub %%mm1, %%mm0\n\t"
613 "pavgb (%0), %%mm1\n\t"
614 "psubusb %%mm1, %%mm0\n\t"
615 "paddusb %%mm7, %%mm0\n\t" /* mm0 = max-avg+thr */
616 "movq (%0,%1), %%mm2\n\t"
617 "movq (%0,%5), %%mm3\n\t"
618 "movq %%mm2, %%mm4\n\t"
619 PDIFFUBT(%%mm1, %%mm2, %%mm5)
620 PDIFFUBT(%%mm1, %%mm3, %%mm5)
621 "pminub %%mm2, %%mm3\n\t"
622 "pcmpeqb %%mm3, %%mm2\n\t" /* b = min */
623 "pand %%mm2, %%mm4\n\t"
624 "pandn (%0,%5), %%mm2\n\t"
625 "por %%mm4, %%mm2\n\t"
626 "pminub %%mm0, %%mm3\n\t"
627 "pcmpeqb %%mm0, %%mm3\n\t" /* set to 1s if >= threshold */
628 "psubb %%mm3, %%mm6\n\t" /* count pixels above thr. */
629 "pand %%mm3, %%mm1 \n\t"
630 "pandn %%mm2, %%mm3 \n\t"
631 "por %%mm3, %%mm1 \n\t" /* avg if >= threshold */
632 "movq %%mm1, (%2,%4) \n\t"
633 : /* no output */
634 : "r" (a), "r" ((x86_reg)bos), "r" ((x86_reg)dst), "r" ((x86_reg)ss), "r" ((x86_reg)ds), "r" ((x86_reg)cos)
636 a += 8;
637 dst += 8;
638 } while (--len);
640 __asm__ volatile ("pxor %%mm7, %%mm7 \n\t"
641 "psadbw %%mm6, %%mm7 \n\t"
642 "movd %%mm7, %0 \n\t"
643 "emms \n\t"
644 : "=r" (ret)
646 return ret;
647 #endif
650 static inline int
651 dint_copy_line(unsigned char *dst, unsigned char *a, long bos,
652 long cos, int ds, int ss, int w, int t)
654 unsigned long len = ((unsigned long)w+sizeof(cmmx_t)-1) / sizeof(cmmx_t);
655 cmmx_t dint_count = 0;
656 cmmx_t thr;
657 t |= t << 8;
658 thr = t | (t << 16);
659 if (sizeof(cmmx_t) > 4)
660 thr |= thr << (sizeof(cmmx_t)*4);
661 do {
662 cmmx_t e = *(cmmx_t*)a;
663 cmmx_t ne = *(cmmx_t*)(a+2*ss);
664 cmmx_t o = *(cmmx_t*)(a+bos);
665 cmmx_t oo = *(cmmx_t*)(a+cos);
666 cmmx_t maxe = pmaxub(e, ne);
667 cmmx_t avge = pavgb(e, ne);
668 cmmx_t max_diff = maxe - avge + thr; /* 0<=max-avg<128, thr<128 */
669 cmmx_t diffo = pdiffub(avge, o);
670 cmmx_t diffoo = pdiffub(avge, oo);
671 cmmx_t diffcmp = pcmpgtub(diffo, diffoo);
672 cmmx_t bo = ((oo ^ o) & diffcmp) ^ o;
673 cmmx_t diffbo = ((diffoo ^ diffo) & diffcmp) ^ diffo;
674 cmmx_t above_thr = ~pcmpgtub(max_diff, diffbo);
675 cmmx_t bo_or_avg = ((avge ^ bo) & above_thr) ^ bo;
676 dint_count += above_thr & ONE_BYTES;
677 *(cmmx_t*)(dst) = e;
678 *(cmmx_t*)(dst+ds) = bo_or_avg;
679 a += sizeof(cmmx_t);
680 dst += sizeof(cmmx_t);
681 } while (--len);
682 return psumbw(dint_count);
685 static int
686 dint_copy_plane(unsigned char *d, unsigned char *a, unsigned char *b,
687 unsigned char *c, unsigned long w, unsigned long h,
688 unsigned long ds, unsigned long ss, unsigned long threshold,
689 long field, long mmx2)
691 unsigned long ret = 0;
692 long bos = b - a;
693 long cos = c - a;
694 if (field) {
695 fast_memcpy(d, b, w);
696 h--;
697 d += ds;
698 a += ss;
700 bos += ss;
701 cos += ss;
702 while (h > 2) {
703 if (threshold >= 128) {
704 fast_memcpy(d, a, w);
705 fast_memcpy(d+ds, a+bos, w);
706 } else if (mmx2 == 1) {
707 ret += dint_copy_line_mmx2(d, a, bos, cos, ds, ss, w, threshold);
708 } else
709 ret += dint_copy_line(d, a, bos, cos, ds, ss, w, threshold);
710 h -= 2;
711 d += 2*ds;
712 a += 2*ss;
714 fast_memcpy(d, a, w);
715 if (h == 2)
716 fast_memcpy(d+ds, a+bos, w);
717 return ret;
720 static void
721 copy_merge_fields(struct vf_priv_s *p, mp_image_t *dmpi,
722 unsigned char **old, unsigned char **new, unsigned long show)
724 unsigned long threshold = 256;
725 unsigned long field = p->swapped;
726 unsigned long dint_pixels = 0;
727 unsigned char **other = old;
728 if (show >= 12 || !(show & 3))
729 show >>= 2, other = new, new = old;
730 if (show <= 2) { /* Single field: de-interlace */
731 threshold = p->dint_thres;
732 field ^= show & 1;
733 old = new;
734 } else if (show == 3)
735 old = new;
736 else
737 field ^= 1;
738 dint_pixels +=dint_copy_plane(dmpi->planes[0], old[0], new[0],
739 other[0], p->w, p->h, dmpi->stride[0],
740 p->stride, threshold, field, p->mmx2);
741 if (dmpi->flags & MP_IMGFLAG_PLANAR) {
742 if (p->luma_only)
743 old = new, other = new;
744 else
745 threshold = threshold/2 + 1;
746 field ^= p->chroma_swapped;
747 dint_copy_plane(dmpi->planes[1], old[1], new[1],
748 other[1], p->cw, p->ch, dmpi->stride[1],
749 p->chroma_stride, threshold, field, p->mmx2);
750 dint_copy_plane(dmpi->planes[2], old[2], new[2],
751 other[2], p->cw, p->ch, dmpi->stride[2],
752 p->chroma_stride, threshold, field, p->mmx2);
754 if (dint_pixels > 0 && p->verbose)
755 mp_msg(MSGT_VFILTER,MSGL_INFO,"Deinterlaced %lu pixels\n",dint_pixels);
758 static void diff_planes(struct vf_priv_s *p, struct frame_stats *s,
759 unsigned char *of, unsigned char *nf,
760 int w, int h, int os, int ns, int swapped)
762 int i, y;
763 int align = -(long)nf & 7;
764 of += align;
765 nf += align;
766 w -= align;
767 if (swapped)
768 of -= os, nf -= ns;
769 i = (h*3 >> 7) & ~1;
770 of += i*os + 8;
771 nf += i*ns + 8;
772 h -= i;
773 w -= 16;
775 memset(s, 0, sizeof(*s));
777 for (y = (h-8) >> 3; y; y--) {
778 if (p->mmx2 == 1) {
779 for (i = 0; i < w; i += 8)
780 block_metrics_mmx2(of+i, nf+i, os, ns, 4, p, s);
781 } else if (p->mmx2 == 2) {
782 for (i = 0; i < w; i += 8)
783 block_metrics_3dnow(of+i, nf+i, os, ns, 4, p, s);
784 } else if (p->fast > 3) {
785 for (i = 0; i < w; i += 8)
786 block_metrics_faster_c(of+i, nf+i, os, ns, 4, p, s);
787 } else if (p->fast > 1) {
788 for (i = 0; i < w; i += 8)
789 block_metrics_fast_c(of+i, nf+i, os, ns, 4, p, s);
790 } else {
791 for (i = 0; i < w; i += 8)
792 block_metrics_c(of+i, nf+i, os, ns, 4, p, s);
794 of += 8*os;
795 nf += 8*ns;
799 #define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
801 static void diff_fields(struct vf_priv_s *p, struct frame_stats *s,
802 unsigned char **old, unsigned char **new)
804 diff_planes(p, s, old[0], new[0], p->w, p->h,
805 p->stride, p->stride, p->swapped);
806 s->sad.even = (s->sad.even * 16ul) / s->num_blocks;
807 s->sad.odd = (s->sad.odd * 16ul) / s->num_blocks;
808 s->sad.noise = (s->sad.noise * 16ul) / s->num_blocks;
809 s->sad.temp = (s->sad.temp * 16ul) / s->num_blocks;
810 if (p->verbose)
811 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu%c M:%d/%d/%d/%d - %d, "
812 "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
813 "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
814 p->inframes, p->chflag, METRICS(s->max), s->num_blocks,
815 METRICS(s->tiny), METRICS(s->low), METRICS(s->high),
816 METRICS(s->bigger), METRICS(s->twox), METRICS(s->sad),
817 s->interlaced_low, s->interlaced_high,
818 p->iosync / (double) p->in_inc);
821 static const char *parse_args(struct vf_priv_s *p, const char *args)
823 args--;
824 while (args && *++args &&
825 (sscanf(args, "io=%lu:%lu", &p->out_dec, &p->in_inc) == 2 ||
826 sscanf(args, "diff_thres=%hu", &p->thres.even ) == 1 ||
827 sscanf(args, "comb_thres=%hu", &p->thres.noise) == 1 ||
828 sscanf(args, "sad_thres=%lu", &p->sad_thres ) == 1 ||
829 sscanf(args, "dint_thres=%lu", &p->dint_thres ) == 1 ||
830 sscanf(args, "fast=%u", &p->fast ) == 1 ||
831 sscanf(args, "mmx2=%lu", &p->mmx2 ) == 1 ||
832 sscanf(args, "luma_only=%u", &p->luma_only ) == 1 ||
833 sscanf(args, "verbose=%u", &p->verbose ) == 1 ||
834 sscanf(args, "crop=%lu:%lu:%lu:%lu", &p->w,
835 &p->h, &p->crop_x, &p->crop_y) == 4))
836 args = strchr(args, '/');
837 return args;
840 static unsigned long gcd(unsigned long x, unsigned long y)
842 unsigned long t;
843 if (x > y)
844 t = x, x = y, y = t;
846 while (x) {
847 t = y % x;
848 y = x;
849 x = t;
851 return y;
854 static void init(struct vf_priv_s *p, mp_image_t *mpi)
856 unsigned long i;
857 unsigned long plane_size, chroma_plane_size;
858 unsigned char *plane;
859 unsigned long cos, los;
860 p->crop_cx = p->crop_x >> mpi->chroma_x_shift;
861 p->crop_cy = p->crop_y >> mpi->chroma_y_shift;
862 if (mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) {
863 p->stride = (mpi->w + 15) & ~15;
864 p->chroma_stride = p->stride >> mpi->chroma_x_shift;
865 } else {
866 p->stride = mpi->width;
867 p->chroma_stride = mpi->chroma_width;
869 p->cw = p->w >> mpi->chroma_x_shift;
870 p->ch = p->h >> mpi->chroma_y_shift;
871 p->nplanes = 1;
872 p->static_idx = 0;
873 p->temp_idx = 0;
874 p->old_planes = p->planes[0];
875 plane_size = mpi->h * p->stride;
876 chroma_plane_size = mpi->flags & MP_IMGFLAG_PLANAR ?
877 mpi->chroma_height * p->chroma_stride : 0;
878 p->memory_allocated =
879 malloc(NUM_STORED * (plane_size+2*chroma_plane_size) +
880 8*p->chroma_stride + 4096);
881 /* align to page boundary */
882 plane = p->memory_allocated + (-(long)p->memory_allocated & 4095);
883 memset(plane, 0, NUM_STORED * plane_size);
884 los = p->crop_x + p->crop_y * p->stride;
885 cos = p->crop_cx + p->crop_cy * p->chroma_stride;
886 for (i = 0; i != NUM_STORED; i++, plane += plane_size) {
887 p->planes[i][0] = plane;
888 p->planes[NUM_STORED + i][0] = plane + los;
890 if (mpi->flags & MP_IMGFLAG_PLANAR) {
891 p->nplanes = 3;
892 memset(plane, 0x80, NUM_STORED * 2 * chroma_plane_size);
893 for (i = 0; i != NUM_STORED; i++) {
894 p->planes[i][1] = plane;
895 p->planes[NUM_STORED + i][1] = plane + cos;
896 plane += chroma_plane_size;
897 p->planes[i][2] = plane;
898 p->planes[NUM_STORED + i][2] = plane + cos;
899 plane += chroma_plane_size;
902 p->out_dec <<= 2;
903 i = gcd(p->in_inc, p->out_dec);
904 p->in_inc /= i;
905 p->out_dec /= i;
906 p->iosync = 0;
907 p->num_fields = 3;
910 static inline double get_time(void)
912 struct timeval tv;
913 gettimeofday(&tv, 0);
914 return tv.tv_sec + tv.tv_usec * 1e-6;
917 static void get_image(struct vf_instance* vf, mp_image_t *mpi)
919 struct vf_priv_s *p = vf->priv;
920 static unsigned char **planes, planes_idx;
922 if (mpi->type == MP_IMGTYPE_STATIC) return;
924 if (!p->planes[0][0]) init(p, mpi);
926 if (mpi->type == MP_IMGTYPE_TEMP ||
927 (mpi->type == MP_IMGTYPE_IPB && !(mpi->flags & MP_IMGFLAG_READABLE)))
928 planes_idx = NUM_STORED/2 + (++p->temp_idx % (NUM_STORED/2));
929 else
930 planes_idx = ++p->static_idx % (NUM_STORED/2);
931 planes = p->planes[planes_idx];
932 mpi->priv = p->planes[NUM_STORED + planes_idx];
933 if (mpi->priv == p->old_planes) {
934 unsigned char **old_planes =
935 p->planes[NUM_STORED + 2 + (++p->temp_idx & 1)];
936 my_memcpy_pic(old_planes[0], p->old_planes[0],
937 p->w, p->h, p->stride, p->stride);
938 if (mpi->flags & MP_IMGFLAG_PLANAR) {
939 my_memcpy_pic(old_planes[1], p->old_planes[1],
940 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
941 my_memcpy_pic(old_planes[2], p->old_planes[2],
942 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
944 p->old_planes = old_planes;
945 p->num_copies++;
947 mpi->planes[0] = planes[0];
948 mpi->stride[0] = p->stride;
949 if (mpi->flags & MP_IMGFLAG_PLANAR) {
950 mpi->planes[1] = planes[1];
951 mpi->planes[2] = planes[2];
952 mpi->stride[1] = mpi->stride[2] = p->chroma_stride;
954 mpi->width = p->stride;
956 mpi->flags |= MP_IMGFLAG_DIRECT;
957 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
960 static inline long
961 cmpe(unsigned long x, unsigned long y, unsigned long err, unsigned long e)
963 long diff = x-y;
964 long unit = ((x+y+err) >> e);
965 long ret = (diff > unit) - (diff < -unit);
966 unit >>= 1;
967 return ret + (diff > unit) - (diff < -unit);
970 static unsigned long
971 find_breaks(struct vf_priv_s *p, struct frame_stats *s)
973 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
974 long notfilm = 5*p->in_inc - p->out_dec;
975 unsigned long n = s->num_blocks >> 8;
976 unsigned long sad_comb_cmp = cmpe(s->sad.temp, s->sad.noise, 512, 1);
977 unsigned long ret = 8;
979 if (cmpe(s->sad.temp, s->sad.even, 512, 1) > 0)
980 mp_msg(MSGT_VFILTER, MSGL_WARN,
981 "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
982 if (s->sad.temp > 1000 && s->sad.noise > 1000)
983 return 3;
984 if (s->interlaced_high >= 2*n && s->sad.temp > 256 && s->sad.noise > 256)
985 return 3;
986 if (s->high.noise > s->num_blocks/4 && s->sad.noise > 10000 &&
987 s->sad.noise > 2*s->sad.even && s->sad.noise > 2*ps->sad.odd) {
988 // Mid-frame scene change
989 if (s->tiny.temp + s->interlaced_low < n ||
990 s->low.temp + s->interlaced_high < n/4 ||
991 s->high.temp + s->interlaced_high < n/8 ||
992 s->sad.temp < 160)
993 return 1;
994 return 3;
996 if (s->high.temp > s->num_blocks/4 && s->sad.temp > 10000 &&
997 s->sad.temp > 2*ps->sad.odd && s->sad.temp > 2*ps->sad.even) {
998 // Start frame scene change
999 if (s->tiny.noise + s->interlaced_low < n ||
1000 s->low.noise + s->interlaced_high < n/4 ||
1001 s->high.noise + s->interlaced_high < n/8 ||
1002 s->sad.noise < 160)
1003 return 2;
1004 return 3;
1006 if (sad_comb_cmp == 2)
1007 return 2;
1008 if (sad_comb_cmp == -2)
1009 return 1;
1011 if (s->tiny.odd > 3*MAX(n,s->tiny.even) + s->interlaced_low)
1012 return 1;
1013 if (s->tiny.even > 3*MAX(n,s->tiny.odd)+s->interlaced_low &&
1014 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1015 return 4;
1017 if (s->sad.noise < 64 && s->sad.temp < 64 &&
1018 s->low.noise <= n/2 && s->high.noise <= n/4 &&
1019 s->low.temp <= n/2 && s->high.temp <= n/4)
1020 goto still;
1022 if (s->tiny.temp > 3*MAX(n,s->tiny.noise) + s->interlaced_low)
1023 return 2;
1024 if (s->tiny.noise > 3*MAX(n,s->tiny.temp) + s->interlaced_low)
1025 return 1;
1027 if (s->low.odd > 3*MAX(n/4,s->low.even) + s->interlaced_high)
1028 return 1;
1029 if (s->low.even > 3*MAX(n/4,s->low.odd)+s->interlaced_high &&
1030 s->sad.even > 2*s->sad.odd &&
1031 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1032 return 4;
1034 if (s->low.temp > 3*MAX(n/4,s->low.noise) + s->interlaced_high)
1035 return 2;
1036 if (s->low.noise > 3*MAX(n/4,s->low.temp) + s->interlaced_high)
1037 return 1;
1039 if (sad_comb_cmp == 1 && s->sad.noise < 64)
1040 return 2;
1041 if (sad_comb_cmp == -1 && s->sad.temp < 64)
1042 return 1;
1044 if (s->tiny.odd <= n || (s->tiny.noise <= n/2 && s->tiny.temp <= n/2)) {
1045 if (s->interlaced_low <= n) {
1046 if (p->num_fields == 1)
1047 goto still;
1048 if (s->tiny.even <= n || ps->tiny.noise <= n/2)
1049 /* Still frame */
1050 goto still;
1051 if (s->bigger.even >= 2*MAX(n,s->bigger.odd) + s->interlaced_low)
1052 return 4;
1053 if (s->low.even >= 2*n + s->interlaced_low)
1054 return 4;
1055 goto still;
1058 if (s->low.odd <= n/4) {
1059 if (s->interlaced_high <= n/4) {
1060 if (p->num_fields == 1)
1061 goto still;
1062 if (s->low.even <= n/4)
1063 /* Still frame */
1064 goto still;
1065 if (s->bigger.even >= 2*MAX(n/4,s->bigger.odd)+s->interlaced_high)
1066 return 4;
1067 if (s->low.even >= n/2 + s->interlaced_high)
1068 return 4;
1069 goto still;
1072 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_low)
1073 return 2;
1074 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_low)
1075 return 1;
1076 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_high)
1077 return 2;
1078 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_high)
1079 return 1;
1080 if (s->twox.temp > 2*MAX(n,s->twox.noise) + s->interlaced_high)
1081 return 2;
1082 if (s->twox.noise > 2*MAX(n,s->twox.temp) + s->interlaced_high)
1083 return 1;
1084 if (s->bigger.even > 2*MAX(n,s->bigger.odd) + s->interlaced_low &&
1085 s->bigger.temp < n && s->bigger.noise < n)
1086 return 4;
1087 if (s->interlaced_low > MIN(2*n, s->tiny.odd))
1088 return 3;
1089 ret = 8 + (1 << (s->sad.temp > s->sad.noise));
1090 still:
1091 if (p->num_fields == 1 && p->prev_fields == 3 && notfilm >= 0 &&
1092 (s->tiny.temp <= s->tiny.noise || s->sad.temp < s->sad.noise+16))
1093 return 1;
1094 if (p->notout < p->num_fields && p->iosync > 2*p->in_inc && notfilm < 0)
1095 notfilm = 0;
1096 if (p->num_fields < 2 ||
1097 (p->num_fields == 2 && p->prev_fields == 2 && notfilm < 0))
1098 return ret;
1099 if (!notfilm && (p->prev_fields&~1) == 2) {
1100 if (p->prev_fields + p->num_fields == 5) {
1101 if (s->tiny.noise <= s->tiny.temp ||
1102 s->low.noise == 0 || s->low.noise < s->low.temp ||
1103 s->sad.noise < s->sad.temp+16)
1104 return 2;
1106 if (p->prev_fields + p->num_fields == 4) {
1107 if (s->tiny.temp <= s->tiny.noise ||
1108 s->low.temp == 0 || s->low.temp < s->low.noise ||
1109 s->sad.temp < s->sad.noise+16)
1110 return 1;
1113 if (p->num_fields > 2 &&
1114 ps->sad.noise > s->sad.noise && ps->sad.noise > s->sad.temp)
1115 return 4;
1116 return 2 >> (s->sad.noise > s->sad.temp);
1119 #define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
1121 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts)
1123 mp_image_t *dmpi;
1124 struct vf_priv_s *p = vf->priv;
1125 unsigned char **planes, **old_planes;
1126 struct frame_stats *s = &p->stats[p->inframes & 1];
1127 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
1128 int swapped = 0;
1129 const int flags = mpi->fields;
1130 int breaks, prev;
1131 int show_fields = 0;
1132 int dropped_fields = 0;
1133 double start_time, diff_time;
1134 char prev_chflag = p->chflag;
1135 int keep_rate;
1137 if (!p->planes[0][0]) init(p, mpi);
1139 old_planes = p->old_planes;
1141 if ((mpi->flags & MP_IMGFLAG_DIRECT) && mpi->priv) {
1142 planes = mpi->priv;
1143 mpi->priv = 0;
1144 } else {
1145 planes = p->planes[2 + (++p->temp_idx & 1)];
1146 my_memcpy_pic(planes[0],
1147 mpi->planes[0] + p->crop_x + p->crop_y * mpi->stride[0],
1148 p->w, p->h, p->stride, mpi->stride[0]);
1149 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1150 my_memcpy_pic(planes[1],
1151 mpi->planes[1] + p->crop_cx + p->crop_cy * mpi->stride[1],
1152 p->cw, p->ch, p->chroma_stride, mpi->stride[1]);
1153 my_memcpy_pic(planes[2],
1154 mpi->planes[2] + p->crop_cx + p->crop_cy * mpi->stride[2],
1155 p->cw, p->ch, p->chroma_stride, mpi->stride[2]);
1156 p->num_copies++;
1160 p->old_planes = planes;
1161 p->chflag = ';';
1162 if (flags & MP_IMGFIELD_ORDERED) {
1163 swapped = !(flags & MP_IMGFIELD_TOP_FIRST);
1164 p->chflag = (flags & MP_IMGFIELD_REPEAT_FIRST ? '|' :
1165 flags & MP_IMGFIELD_TOP_FIRST ? ':' : '.');
1167 p->swapped = swapped;
1169 start_time = get_time();
1170 if (p->chflag == '|') {
1171 *s = ppzs;
1172 p->iosync += p->in_inc;
1173 } else if ((p->fast & 1) && prev_chflag == '|')
1174 *s = pprs;
1175 else
1176 diff_fields(p, s, old_planes, planes);
1177 diff_time = get_time();
1178 p->diff_time += diff_time - start_time;
1179 breaks = p->inframes ? find_breaks(p, s) : 2;
1180 p->inframes++;
1181 keep_rate = 4*p->in_inc == p->out_dec;
1183 switch (breaks) {
1184 case 0:
1185 case 8:
1186 case 9:
1187 case 10:
1188 if (!keep_rate && p->notout < p->num_fields && p->iosync < 2*p->in_inc)
1189 break;
1190 if (p->notout < p->num_fields)
1191 dropped_fields = -2;
1192 case 4:
1193 if (keep_rate || p->iosync >= -2*p->in_inc)
1194 show_fields = (4<<p->num_fields)-1;
1195 break;
1196 case 3:
1197 if (keep_rate)
1198 show_fields = 2;
1199 else if (p->iosync > 0) {
1200 if (p->notout >= p->num_fields && p->iosync > 2*p->in_inc) {
1201 show_fields = 4; /* prev odd only */
1202 if (p->num_fields > 1)
1203 show_fields |= 8; /* + prev even */
1204 } else {
1205 show_fields = 2; /* even only */
1206 if (p->notout >= p->num_fields)
1207 dropped_fields += p->num_fields;
1210 break;
1211 case 2:
1212 if (p->iosync <= -3*p->in_inc) {
1213 if (p->notout >= p->num_fields)
1214 dropped_fields = p->num_fields;
1215 break;
1217 if (p->num_fields == 1) {
1218 int prevbreak = ps->sad.noise >= 128;
1219 if (p->iosync < 4*p->in_inc) {
1220 show_fields = 3;
1221 dropped_fields = prevbreak;
1222 } else {
1223 show_fields = 4 | (!prevbreak << 3);
1224 if (p->notout < 1 + p->prev_fields)
1225 dropped_fields = -!prevbreak;
1227 break;
1229 default:
1230 if (keep_rate)
1231 show_fields = 3 << (breaks & 1);
1232 else if (p->notout >= p->num_fields &&
1233 p->iosync >= (breaks == 1 ? -p->in_inc :
1234 p->in_inc << (p->num_fields == 1))) {
1235 show_fields = (1 << (2 + p->num_fields)) - (1<<breaks);
1236 } else {
1237 if (p->notout >= p->num_fields)
1238 dropped_fields += p->num_fields + 2 - breaks;
1239 if (breaks == 1) {
1240 if (p->iosync >= 4*p->in_inc)
1241 show_fields = 6;
1242 } else if (p->iosync > -3*p->in_inc)
1243 show_fields = 3; /* odd+even */
1245 break;
1248 show_fields &= 15;
1249 prev = p->prev_fields;
1250 if (breaks < 8) {
1251 if (p->num_fields == 1)
1252 breaks &= ~4;
1253 if (breaks)
1254 p->num_breaks++;
1255 if (breaks == 3)
1256 p->prev_fields = p->num_fields = 1;
1257 else if (breaks) {
1258 p->prev_fields = p->num_fields + (breaks==1) - (breaks==4);
1259 p->num_fields = breaks - (breaks == 4) + (p->chflag == '|');
1260 } else
1261 p->num_fields += 2;
1262 } else
1263 p->num_fields += 2;
1265 p->iosync += 4 * p->in_inc;
1266 if (p->chflag == '|')
1267 p->iosync += p->in_inc;
1269 if (show_fields) {
1270 p->iosync -= p->out_dec;
1271 p->notout = !(show_fields & 1) + !(show_fields & 3);
1272 if (((show_fields & 3) == 3 &&
1273 (s->low.noise + s->interlaced_low < (s->num_blocks>>8) ||
1274 s->sad.noise < 160)) ||
1275 ((show_fields & 12) == 12 &&
1276 (ps->low.noise + ps->interlaced_low < (s->num_blocks>>8) ||
1277 ps->sad.noise < 160))) {
1278 p->export_count++;
1279 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT,
1280 MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE,
1281 p->w, p->h);
1282 if ((show_fields & 3) != 3) planes = old_planes;
1283 dmpi->planes[0] = planes[0];
1284 dmpi->stride[0] = p->stride;
1285 dmpi->width = mpi->width;
1286 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1287 dmpi->planes[1] = planes[1];
1288 dmpi->planes[2] = planes[2];
1289 dmpi->stride[1] = p->chroma_stride;
1290 dmpi->stride[2] = p->chroma_stride;
1292 } else {
1293 p->merge_count++;
1294 dmpi = vf_get_image(vf->next, mpi->imgfmt,
1295 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
1296 p->w, p->h);
1297 copy_merge_fields(p, dmpi, old_planes, planes, show_fields);
1299 p->outframes++;
1300 } else
1301 p->notout += 2;
1303 if (p->verbose)
1304 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
1305 p->inframes, p->outframes,
1306 breaks, breaks<8 && breaks>0 ? (int) p->prev_fields+'0' : ' ',
1307 ITOC(show_fields),
1308 p->num_breaks, 5*p->in_inc == p->out_dec && breaks<8 &&
1309 breaks>0 && ((prev&~1)!=2 || prev+p->prev_fields!=5) ?
1310 " ######## bad telecine ########" : "",
1311 dropped_fields ? " ======== dropped ":"", ITOC(dropped_fields),
1312 !show_fields || (show_fields & (show_fields-1)) ?
1313 "" : " @@@@@@@@@@@@@@@@@");
1315 p->merge_time += get_time() - diff_time;
1316 return show_fields ? vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE) : 0;
1319 static int query_format(struct vf_instance* vf, unsigned int fmt)
1321 /* FIXME - support more formats */
1322 switch (fmt) {
1323 case IMGFMT_YV12:
1324 case IMGFMT_IYUV:
1325 case IMGFMT_I420:
1326 case IMGFMT_411P:
1327 case IMGFMT_422P:
1328 case IMGFMT_444P:
1329 return vf_next_query_format(vf, fmt);
1331 return 0;
1334 static int config(struct vf_instance* vf,
1335 int width, int height, int d_width, int d_height,
1336 unsigned int flags, unsigned int outfmt)
1338 struct MPOpts *opts = vf->opts;
1339 unsigned long cxm = 0;
1340 unsigned long cym = 0;
1341 struct vf_priv_s *p = vf->priv;
1342 // rounding:
1343 if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
1344 switch(outfmt){
1345 case IMGFMT_444P:
1346 case IMGFMT_Y800:
1347 case IMGFMT_Y8:
1348 break;
1349 case IMGFMT_YVU9:
1350 case IMGFMT_IF09:
1351 cym = 3;
1352 case IMGFMT_411P:
1353 cxm = 3;
1354 break;
1355 case IMGFMT_YV12:
1356 case IMGFMT_I420:
1357 case IMGFMT_IYUV:
1358 cym = 1;
1359 default:
1360 cxm = 1;
1363 p->chroma_swapped = !!(p->crop_y & (cym+1));
1364 if (p->w) p->w += p->crop_x & cxm;
1365 if (p->h) p->h += p->crop_y & cym;
1366 p->crop_x &= ~cxm;
1367 p->crop_y &= ~cym;
1368 if (!p->w || p->w > width ) p->w = width;
1369 if (!p->h || p->h > height) p->h = height;
1370 if (p->crop_x + p->w > width ) p->crop_x = 0;
1371 if (p->crop_y + p->h > height) p->crop_y = 0;
1373 if(!opts->screen_size_x && !opts->screen_size_y){
1374 d_width = d_width * p->w/width;
1375 d_height = d_height * p->h/height;
1377 return vf_next_config(vf, p->w, p->h, d_width, d_height, flags, outfmt);
1380 static void uninit(struct vf_instance* vf)
1382 struct vf_priv_s *p = vf->priv;
1383 mp_msg(MSGT_VFILTER, MSGL_INFO, "diff_time: %.3f, merge_time: %.3f, "
1384 "export: %lu, merge: %lu, copy: %lu\n", p->diff_time, p->merge_time,
1385 p->export_count, p->merge_count, p->num_copies);
1386 free(p->memory_allocated);
1387 free(p);
1390 static int open(vf_instance_t *vf, char* args)
1392 struct vf_priv_s *p;
1393 vf->get_image = get_image;
1394 vf->put_image = put_image;
1395 vf->config = config;
1396 vf->query_format = query_format;
1397 vf->uninit = uninit;
1398 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
1399 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
1400 p->out_dec = 5;
1401 p->in_inc = 4;
1402 p->thres.noise = 128;
1403 p->thres.even = 128;
1404 p->sad_thres = 64;
1405 p->dint_thres = 4;
1406 p->luma_only = 0;
1407 p->fast = 3;
1408 p->mmx2 = gCpuCaps.hasMMX2 ? 1 : gCpuCaps.has3DNow ? 2 : 0;
1409 if (args) {
1410 const char *args_remain = parse_args(p, args);
1411 if (args_remain) {
1412 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1413 "filmdint: unknown suboption: %s\n", args_remain);
1414 return 0;
1416 if (p->out_dec < p->in_inc) {
1417 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1418 "filmdint: increasing the frame rate is not supported\n");
1419 return 0;
1422 if (p->mmx2 > 2)
1423 p->mmx2 = 0;
1424 #if !HAVE_MMX
1425 p->mmx2 = 0;
1426 #endif
1427 #if !HAVE_AMD3DNOW
1428 p->mmx2 &= 1;
1429 #endif
1430 p->thres.odd = p->thres.even;
1431 p->thres.temp = p->thres.noise;
1432 p->diff_time = 0;
1433 p->merge_time = 0;
1434 return 1;
1437 const vf_info_t vf_info_filmdint = {
1438 "Advanced inverse telecine filer",
1439 "filmdint",
1440 "Zoltan Hidvegi",
1442 open,
1443 NULL