Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_filmdint.c
blob2888c43131bdd14125989c4be0e5657f780d7b90
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <sys/time.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "cpudetect.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 extern int opt_screen_size_x;
84 extern int opt_screen_size_y;
86 #ifndef MIN
87 #define MIN(a,b) (((a)<(b))?(a):(b))
88 #endif
89 #ifndef MAX
90 #define MAX(a,b) (((a)>(b))?(a):(b))
91 #endif
93 #define PDIFFUB(X,Y,T) "movq " #X "," #T "\n\t" \
94 "psubusb " #Y "," #T "\n\t" \
95 "psubusb " #X "," #Y "\n\t" \
96 "paddusb " #Y "," #T "\n\t"
98 #define PDIFFUBT(X,Y,T) "movq " #X "," #T "\n\t" \
99 "psubusb " #Y "," #T "\n\t" \
100 "psubusb " #X "," #Y "\n\t" \
101 "paddusb " #T "," #Y "\n\t"
103 #define PSUMBW(X,T,Z) "movq " #X "," #T "\n\t" \
104 "punpcklbw " #Z "," #X "\n\t" \
105 "punpckhbw " #Z "," #T "\n\t" \
106 "paddw " #T "," #X "\n\t" \
107 "movq " #X "," #T "\n\t" \
108 "psllq $32, " #T "\n\t" \
109 "paddw " #T "," #X "\n\t" \
110 "movq " #X "," #T "\n\t" \
111 "psllq $16, " #T "\n\t" \
112 "paddw " #T "," #X "\n\t" \
113 "psrlq $48, " #X "\n\t"
115 #define PSADBW(X,Y,T,Z) PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
117 #define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
118 #define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
119 #define PMINUBT(X,Y,T) "movq " #Y "," #T "\n\t" \
120 "psubusb " #X "," #T "\n\t" \
121 "psubusb " #T "," #Y "\n\t"
122 #define PAVGB(X,Y) "pavgusb " #X "," #Y "\n\t"
124 static inline void
125 get_metrics_c(unsigned char *a, unsigned char *b, int as, int bs, int lines,
126 struct metrics *m)
128 a -= as;
129 b -= bs;
130 do {
131 cmmx_t old_po = *(cmmx_t*)(a );
132 cmmx_t po = *(cmmx_t*)(b );
133 cmmx_t e = *(cmmx_t*)(b + bs);
134 cmmx_t old_o = *(cmmx_t*)(a + 2*as);
135 cmmx_t o = *(cmmx_t*)(b + 2*bs);
136 cmmx_t ne = *(cmmx_t*)(b + 3*bs);
137 cmmx_t old_no = *(cmmx_t*)(a + 4*as);
138 cmmx_t no = *(cmmx_t*)(b + 4*bs);
140 cmmx_t qup_old_odd = p31avgb(old_o, old_po);
141 cmmx_t qup_odd = p31avgb( o, po);
142 cmmx_t qdown_old_odd = p31avgb(old_o, old_no);
143 cmmx_t qdown_odd = p31avgb( o, no);
145 cmmx_t qup_even = p31avgb(ne, e);
146 cmmx_t qdown_even = p31avgb(e, ne);
148 cmmx_t temp_up_diff = pdiffub(qdown_even, qup_old_odd);
149 cmmx_t noise_up_diff = pdiffub(qdown_even, qup_odd);
150 cmmx_t temp_down_diff = pdiffub(qup_even, qdown_old_odd);
151 cmmx_t noise_down_diff = pdiffub(qup_even, qdown_odd);
153 cmmx_t odd_diff = pdiffub(o, old_o);
154 m->odd += psumbw(odd_diff);
155 m->even += psadbw(e, *(cmmx_t*)(a+as));
157 temp_up_diff = pminub(temp_up_diff, temp_down_diff);
158 temp_up_diff = pminub(temp_up_diff, odd_diff);
159 m->temp += psumbw(temp_up_diff);
160 noise_up_diff = pminub(noise_up_diff, odd_diff);
161 noise_up_diff = pminub(noise_up_diff, noise_down_diff);
163 m->noise += psumbw(noise_up_diff);
164 a += 2*as;
165 b += 2*bs;
166 } while (--lines);
169 static inline void
170 get_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
171 int lines, struct metrics *m)
173 a -= as;
174 b -= bs;
175 do {
176 cmmx_t old_po = (*(cmmx_t*)(a ) >> 1) & ~SIGN_BITS;
177 cmmx_t po = (*(cmmx_t*)(b ) >> 1) & ~SIGN_BITS;
178 cmmx_t old_e = (*(cmmx_t*)(a + as) >> 1) & ~SIGN_BITS;
179 cmmx_t e = (*(cmmx_t*)(b + bs) >> 1) & ~SIGN_BITS;
180 cmmx_t old_o = (*(cmmx_t*)(a + 2*as) >> 1) & ~SIGN_BITS;
181 cmmx_t o = (*(cmmx_t*)(b + 2*bs) >> 1) & ~SIGN_BITS;
182 cmmx_t ne = (*(cmmx_t*)(b + 3*bs) >> 1) & ~SIGN_BITS;
183 cmmx_t old_no = (*(cmmx_t*)(a + 4*as) >> 1) & ~SIGN_BITS;
184 cmmx_t no = (*(cmmx_t*)(b + 4*bs) >> 1) & ~SIGN_BITS;
186 cmmx_t qup_old_odd = p31avgb_s(old_o, old_po);
187 cmmx_t qup_odd = p31avgb_s( o, po);
188 cmmx_t qdown_old_odd = p31avgb_s(old_o, old_no);
189 cmmx_t qdown_odd = p31avgb_s( o, no);
191 cmmx_t qup_even = p31avgb_s(ne, e);
192 cmmx_t qdown_even = p31avgb_s(e, ne);
194 cmmx_t temp_up_diff = pdiffub_s(qdown_even, qup_old_odd);
195 cmmx_t noise_up_diff = pdiffub_s(qdown_even, qup_odd);
196 cmmx_t temp_down_diff = pdiffub_s(qup_even, qdown_old_odd);
197 cmmx_t noise_down_diff = pdiffub_s(qup_even, qdown_odd);
199 cmmx_t odd_diff = pdiffub_s(o, old_o);
200 m->odd += psumbw_s(odd_diff) << 1;
201 m->even += psadbw_s(e, old_e) << 1;
203 temp_up_diff = pminub_s(temp_up_diff, temp_down_diff);
204 temp_up_diff = pminub_s(temp_up_diff, odd_diff);
205 m->temp += psumbw_s(temp_up_diff) << 1;
206 noise_up_diff = pminub_s(noise_up_diff, odd_diff);
207 noise_up_diff = pminub_s(noise_up_diff, noise_down_diff);
209 m->noise += psumbw_s(noise_up_diff) << 1;
210 a += 2*as;
211 b += 2*bs;
212 } while (--lines);
215 static inline void
216 get_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
217 int lines, struct metrics *m)
219 a -= as;
220 b -= bs;
221 do {
222 cmmx_t old_po = (*(cmmx_t*)(a )>>1) & ~SIGN_BITS;
223 cmmx_t po = (*(cmmx_t*)(b )>>1) & ~SIGN_BITS;
224 cmmx_t old_e = (*(cmmx_t*)(a + as)>>1) & ~SIGN_BITS;
225 cmmx_t e = (*(cmmx_t*)(b + bs)>>1) & ~SIGN_BITS;
226 cmmx_t old_o = (*(cmmx_t*)(a + 2*as)>>1) & ~SIGN_BITS;
227 cmmx_t o = (*(cmmx_t*)(b + 2*bs)>>1) & ~SIGN_BITS;
228 cmmx_t ne = (*(cmmx_t*)(b + 3*bs)>>1) & ~SIGN_BITS;
230 cmmx_t down_even = p31avgb_s(e, ne);
231 cmmx_t up_odd = p31avgb_s(o, po);
232 cmmx_t up_old_odd = p31avgb_s(old_o, old_po);
234 cmmx_t odd_diff = pdiffub_s(o, old_o);
235 cmmx_t temp_diff = pdiffub_s(down_even, up_old_odd);
236 cmmx_t noise_diff = pdiffub_s(down_even, up_odd);
238 m->even += psadbw_s(e, old_e) << 1;
239 m->odd += psumbw_s(odd_diff) << 1;
241 temp_diff = pminub_s(temp_diff, odd_diff);
242 noise_diff = pminub_s(noise_diff, odd_diff);
244 m->noise += psumbw_s(noise_diff) << 1;
245 m->temp += psumbw_s(temp_diff) << 1;
246 a += 2*as;
247 b += 2*bs;
248 } while (--lines);
252 static inline void
253 get_block_stats(struct metrics *m, struct vf_priv_s *p, struct frame_stats *s)
255 unsigned two_e = m->even + MAX(m->even , p->thres.even );
256 unsigned two_o = m->odd + MAX(m->odd , p->thres.odd );
257 unsigned two_n = m->noise + MAX(m->noise, p->thres.noise);
258 unsigned two_t = m->temp + MAX(m->temp , p->thres.temp );
260 unsigned e_big = m->even >= (m->odd + two_o + 1)/2;
261 unsigned o_big = m->odd >= (m->even + two_e + 1)/2;
262 unsigned n_big = m->noise >= (m->temp + two_t + 1)/2;
263 unsigned t_big = m->temp >= (m->noise + two_n + 1)/2;
265 unsigned e2x = m->even >= two_o;
266 unsigned o2x = m->odd >= two_e;
267 unsigned n2x = m->noise >= two_t;
268 unsigned t2x = m->temp >= two_n;
270 unsigned ntiny_e = m->even > p->thres.even ;
271 unsigned ntiny_o = m->odd > p->thres.odd ;
272 unsigned ntiny_n = m->noise > p->thres.noise;
273 unsigned ntiny_t = m->temp > p->thres.temp ;
275 unsigned nlow_e = m->even > 2*p->thres.even ;
276 unsigned nlow_o = m->odd > 2*p->thres.odd ;
277 unsigned nlow_n = m->noise > 2*p->thres.noise;
278 unsigned nlow_t = m->temp > 2*p->thres.temp ;
280 unsigned high_e = m->even > 4*p->thres.even ;
281 unsigned high_o = m->odd > 4*p->thres.odd ;
282 unsigned high_n = m->noise > 4*p->thres.noise;
283 unsigned high_t = m->temp > 4*p->thres.temp ;
285 unsigned low_il = !n_big && !t_big && ntiny_n && ntiny_t;
286 unsigned high_il = !n_big && !t_big && nlow_n && nlow_t;
288 if (low_il | high_il) {
289 s->interlaced_low += low_il;
290 s->interlaced_high += high_il;
291 } else {
292 s->tiny.even += ntiny_e;
293 s->tiny.odd += ntiny_o;
294 s->tiny.noise += ntiny_n;
295 s->tiny.temp += ntiny_t;
297 s->low .even += nlow_e ;
298 s->low .odd += nlow_o ;
299 s->low .noise += nlow_n ;
300 s->low .temp += nlow_t ;
302 s->high.even += high_e ;
303 s->high.odd += high_o ;
304 s->high.noise += high_n ;
305 s->high.temp += high_t ;
307 if (m->even >= p->sad_thres) s->sad.even += m->even ;
308 if (m->odd >= p->sad_thres) s->sad.odd += m->odd ;
309 if (m->noise >= p->sad_thres) s->sad.noise += m->noise;
310 if (m->temp >= p->sad_thres) s->sad.temp += m->temp ;
312 s->num_blocks++;
313 s->max.even = MAX(s->max.even , m->even );
314 s->max.odd = MAX(s->max.odd , m->odd );
315 s->max.noise = MAX(s->max.noise, m->noise);
316 s->max.temp = MAX(s->max.temp , m->temp );
318 s->bigger.even += e_big ;
319 s->bigger.odd += o_big ;
320 s->bigger.noise += n_big ;
321 s->bigger.temp += t_big ;
323 s->twox.even += e2x ;
324 s->twox.odd += o2x ;
325 s->twox.noise += n2x ;
326 s->twox.temp += t2x ;
330 static inline struct metrics
331 block_metrics_c(unsigned char *a, unsigned char *b, int as, int bs,
332 int lines, struct vf_priv_s *p, struct frame_stats *s)
334 struct metrics tm;
335 tm.even = tm.odd = tm.noise = tm.temp = 0;
336 get_metrics_c(a, b, as, bs, lines, &tm);
337 if (sizeof(cmmx_t) < 8)
338 get_metrics_c(a+4, b+4, as, bs, lines, &tm);
339 get_block_stats(&tm, p, s);
340 return tm;
343 static inline struct metrics
344 block_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
345 int lines, struct vf_priv_s *p, struct frame_stats *s)
347 struct metrics tm;
348 tm.even = tm.odd = tm.noise = tm.temp = 0;
349 get_metrics_fast_c(a, b, as, bs, lines, &tm);
350 if (sizeof(cmmx_t) < 8)
351 get_metrics_fast_c(a+4, b+4, as, bs, lines, &tm);
352 get_block_stats(&tm, p, s);
353 return tm;
356 static inline struct metrics
357 block_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
358 int lines, struct vf_priv_s *p, struct frame_stats *s)
360 struct metrics tm;
361 tm.even = tm.odd = tm.noise = tm.temp = 0;
362 get_metrics_faster_c(a, b, as, bs, lines, &tm);
363 if (sizeof(cmmx_t) < 8)
364 get_metrics_faster_c(a+4, b+4, as, bs, lines, &tm);
365 get_block_stats(&tm, p, s);
366 return tm;
369 #define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
371 #define BLOCK_METRICS_TEMPLATE() \
372 asm volatile("pxor %mm7, %mm7\n\t" /* The result is colleted in mm7 */ \
373 "pxor %mm6, %mm6\n\t" /* Temp to stay at 0 */ \
374 ); \
375 a -= as; \
376 b -= bs; \
377 do { \
378 asm volatile( \
379 "movq (%0,%2), %%mm0\n\t" \
380 "movq (%1,%3), %%mm1\n\t" /* mm1 = even */ \
381 PSADBW(%%mm1, %%mm0, %%mm4, %%mm6) \
382 "paddusw %%mm0, %%mm7\n\t" /* even diff */ \
383 "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */ \
384 "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */ \
385 "movq (%0), %%mm3\n\t" \
386 "psubusb %4, %%mm3\n\t" \
387 PAVGB(%%mm0, %%mm3) \
388 PAVGB(%%mm0, %%mm3) /* mm3 = qup old odd */ \
389 "movq %%mm0, %%mm5\n\t" \
390 PSADBW(%%mm2, %%mm0, %%mm4, %%mm6) \
391 "psllq $16, %%mm0\n\t" \
392 "paddusw %%mm0, %%mm7\n\t" \
393 "movq (%1), %%mm4\n\t" \
394 "lea (%0,%2,2), %0\n\t" \
395 "lea (%1,%3,2), %1\n\t" \
396 "psubusb %4, %%mm4\n\t" \
397 PAVGB(%%mm2, %%mm4) \
398 PAVGB(%%mm2, %%mm4) /* mm4 = qup odd */ \
399 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */ \
400 "movq (%1,%3), %%mm5\n\t" \
401 "psubusb %4, %%mm5\n\t" \
402 PAVGB(%%mm1, %%mm5) \
403 PAVGB(%%mm5, %%mm1) /* mm1 = qdown even */ \
404 PAVGB((%1,%3), %%mm5) /* mm5 = qup next even */ \
405 PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */ \
406 PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */ \
407 PMINUBT(%%mm2, %%mm3, %%mm0) /* limit temp to odd diff */ \
408 PMINUBT(%%mm2, %%mm4, %%mm0) /* limit noise to odd diff */ \
409 "movq (%1,%3,2), %%mm2\n\t" \
410 "psubusb %4, %%mm2\n\t" \
411 PAVGB((%1), %%mm2) \
412 PAVGB((%1), %%mm2) /* mm2 = qdown odd */ \
413 "movq (%0,%2,2), %%mm1\n\t" \
414 "psubusb %4, %%mm1\n\t" \
415 PAVGB((%0), %%mm1) \
416 PAVGB((%0), %%mm1) /* mm1 = qdown old odd */ \
417 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */ \
418 PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */ \
419 PMINUBT(%%mm4, %%mm2, %%mm0) /* current */ \
420 PMINUBT(%%mm3, %%mm1, %%mm0) /* old */ \
421 PSUMBW(%%mm2, %%mm0, %%mm6) \
422 PSUMBW(%%mm1, %%mm0, %%mm6) \
423 "psllq $32, %%mm2\n\t" \
424 "psllq $48, %%mm1\n\t" \
425 "paddusw %%mm2, %%mm7\n\t" \
426 "paddusw %%mm1, %%mm7\n\t" \
427 : "=r" (a), "=r" (b) \
428 : "r"((long)as), "r"((long)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
429 ); \
430 } while (--lines);
432 static inline struct metrics
433 block_metrics_3dnow(unsigned char *a, unsigned char *b, int as, int bs,
434 int lines, struct vf_priv_s *p, struct frame_stats *s)
436 struct metrics tm;
437 #ifndef HAVE_3DNOW
438 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_3dnow: internal error\n");
439 #else
440 static const unsigned long long ones = 0x0101010101010101ull;
442 BLOCK_METRICS_TEMPLATE();
443 asm volatile("movq %%mm7, %0\n\temms" : "=m" (tm));
444 get_block_stats(&tm, p, s);
445 #endif
446 return tm;
449 #undef PSUMBW
450 #undef PSADBW
451 #undef PMAXUB
452 #undef PMINUBT
453 #undef PAVGB
455 #define PSUMBW(X,T,Z) "psadbw " #Z "," #X "\n\t"
456 #define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
457 #define PMAXUB(X,Y) "pmaxub " #X "," #Y "\n\t"
458 #define PMINUBT(X,Y,T) "pminub " #X "," #Y "\n\t"
459 #define PAVGB(X,Y) "pavgb " #X "," #Y "\n\t"
461 static inline struct metrics
462 block_metrics_mmx2(unsigned char *a, unsigned char *b, int as, int bs,
463 int lines, struct vf_priv_s *p, struct frame_stats *s)
465 struct metrics tm;
466 #ifndef HAVE_MMX
467 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_mmx2: internal error\n");
468 #else
469 static const unsigned long long ones = 0x0101010101010101ull;
470 unsigned long interlaced;
471 unsigned long prefetch_line = (((long)a>>3) & 7) + 10;
472 #ifdef DEBUG
473 struct frame_stats ts = *s;
474 #endif
475 asm volatile("prefetcht0 (%0,%2)\n\t"
476 "prefetcht0 (%1,%3)\n\t" :
477 : "r" (a), "r" (b),
478 "r" (prefetch_line * as), "r" (prefetch_line * bs));
480 BLOCK_METRICS_TEMPLATE();
482 s->num_blocks++;
483 asm volatile(
484 "movq %3, %%mm0\n\t"
485 "movq %%mm7, %%mm1\n\t"
486 "psubusw %%mm0, %%mm1\n\t"
487 "movq %%mm1, %%mm2\n\t"
488 "paddusw %%mm0, %%mm2\n\t"
489 "paddusw %%mm7, %%mm2\n\t"
490 "pshufw $0xb1, %%mm2, %%mm3\n\t"
491 "pavgw %%mm7, %%mm2\n\t"
492 "pshufw $0xb1, %%mm2, %%mm2\n\t"
493 "psubusw %%mm7, %%mm2\n\t"
494 "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
495 "psubusw %%mm7, %%mm3\n\t"
496 "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
497 "movq %1, %%mm4\n\t"
498 "movq %2, %%mm5\n\t"
499 "psubw %%mm2, %%mm4\n\t"
500 "psubw %%mm3, %%mm5\n\t"
501 "movq %%mm4, %1\n\t"
502 "movq %%mm5, %2\n\t"
503 "pxor %%mm4, %%mm4\n\t"
504 "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
505 "psubusw %%mm0, %%mm1\n\t"
506 "pxor %%mm5, %%mm5\n\t"
507 "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
508 "psubusw %%mm0, %%mm1\n\t"
509 "psubusw %%mm0, %%mm1\n\t"
510 "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
511 "pshufw $0xb1, %%mm2, %%mm0\n\t"
512 "por %%mm2, %%mm0\n\t" /* 1 if not close */
513 "punpckhdq %%mm0, %%mm0\n\t"
514 "movq %%mm4, %%mm2\n\t" /* tttt */
515 "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
516 "por %%mm2, %%mm0\n\t"
517 "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
518 "psrlq $16, %%mm0\n\t"
519 "psrlw $15, %%mm0\n\t"
520 "movd %%mm0, %0\n\t"
521 : "=r" (interlaced), "=m" (s->bigger), "=m" (s->twox)
522 : "m" (p->thres)
525 if (interlaced) {
526 s->interlaced_high += interlaced >> 16;
527 s->interlaced_low += interlaced;
528 } else {
529 asm volatile(
530 "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
531 "psubw %%mm0, %%mm4\n\t"
532 "psubw %%mm0, %%mm5\n\t"
533 "psubw %%mm0, %%mm1\n\t"
534 "paddw %0, %%mm4\n\t"
535 "paddw %1, %%mm5\n\t"
536 "paddw %2, %%mm1\n\t"
537 "movq %%mm4, %0\n\t"
538 "movq %%mm5, %1\n\t"
539 "movq %%mm1, %2\n\t"
540 : "=m" (s->tiny), "=m" (s->low), "=m" (s->high)
543 asm volatile(
544 "pshufw $0, %2, %%mm0\n\t"
545 "psubusw %%mm7, %%mm0\n\t"
546 "pcmpeqw %%mm6, %%mm0\n\t" /* 0 if below sad_thres */
547 "pand %%mm7, %%mm0\n\t"
548 "movq %%mm0, %%mm1\n\t"
549 "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
550 "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
551 "paddd %0, %%mm0\n\t"
552 "paddd %1, %%mm1\n\t"
553 "movq %%mm0, %0\n\t"
554 "movq %%mm1, %1\n\t"
555 : "=m" (s->sad.even), "=m" (s->sad.noise)
556 : "m" (p->sad_thres)
560 asm volatile(
561 "movq %%mm7, (%1)\n\t"
562 PMAXUW((%0), %%mm7)
563 "movq %%mm7, (%0)\n\t"
564 "emms"
565 : : "r" (&s->max), "r" (&tm), "X" (s->max)
566 : "memory"
568 #ifdef DEBUG
569 if (1) {
570 struct metrics cm;
571 a -= 7*as;
572 b -= 7*bs;
573 cm = block_metrics_c(a, b, as, bs, 4, p, &ts);
574 if (!MEQ(tm, cm))
575 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad metrics\n");
576 if (s) {
577 # define CHECK(X) if (!MEQ(s->X, ts.X)) \
578 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
579 CHECK(tiny);
580 CHECK(low);
581 CHECK(high);
582 CHECK(sad);
583 CHECK(max);
586 #endif
587 #endif
588 return tm;
591 static inline int
592 dint_copy_line_mmx2(unsigned char *dst, unsigned char *a, long bos,
593 long cos, int ds, int ss, int w, int t)
595 #ifndef HAVE_MMX
596 mp_msg(MSGT_VFILTER, MSGL_FATAL, "dint_copy_line_mmx2: internal error\n");
597 return 0;
598 #else
599 unsigned long len = (w+7) >> 3;
600 int ret;
601 asm volatile (
602 "pxor %%mm6, %%mm6 \n\t" /* deinterlaced pixel counter */
603 "movd %0, %%mm7 \n\t"
604 "punpcklbw %%mm7, %%mm7 \n\t"
605 "punpcklwd %%mm7, %%mm7 \n\t"
606 "punpckldq %%mm7, %%mm7 \n\t" /* mm7 = threshold */
607 : /* no output */
608 : "rm" (t)
610 do {
611 asm volatile (
612 "movq (%0), %%mm0\n\t"
613 "movq (%0,%3,2), %%mm1\n\t"
614 "movq %%mm0, (%2)\n\t"
615 "pmaxub %%mm1, %%mm0\n\t"
616 "pavgb (%0), %%mm1\n\t"
617 "psubusb %%mm1, %%mm0\n\t"
618 "paddusb %%mm7, %%mm0\n\t" /* mm0 = max-avg+thr */
619 "movq (%0,%1), %%mm2\n\t"
620 "movq (%0,%5), %%mm3\n\t"
621 "movq %%mm2, %%mm4\n\t"
622 PDIFFUBT(%%mm1, %%mm2, %%mm5)
623 PDIFFUBT(%%mm1, %%mm3, %%mm5)
624 "pminub %%mm2, %%mm3\n\t"
625 "pcmpeqb %%mm3, %%mm2\n\t" /* b = min */
626 "pand %%mm2, %%mm4\n\t"
627 "pandn (%0,%5), %%mm2\n\t"
628 "por %%mm4, %%mm2\n\t"
629 "pminub %%mm0, %%mm3\n\t"
630 "pcmpeqb %%mm0, %%mm3\n\t" /* set to 1s if >= threshold */
631 "psubb %%mm3, %%mm6\n\t" /* count pixels above thr. */
632 "pand %%mm3, %%mm1 \n\t"
633 "pandn %%mm2, %%mm3 \n\t"
634 "por %%mm3, %%mm1 \n\t" /* avg if >= threshold */
635 "movq %%mm1, (%2,%4) \n\t"
636 : /* no output */
637 : "r" (a), "r" (bos), "r" (dst), "r" ((long)ss), "r" ((long)ds), "r" (cos)
639 a += 8;
640 dst += 8;
641 } while (--len);
643 asm volatile ("pxor %%mm7, %%mm7 \n\t"
644 "psadbw %%mm6, %%mm7 \n\t"
645 "movd %%mm7, %0 \n\t"
646 "emms \n\t"
647 : "=r" (ret)
649 return ret;
650 #endif
653 static inline int
654 dint_copy_line(unsigned char *dst, unsigned char *a, long bos,
655 long cos, int ds, int ss, int w, int t)
657 unsigned long len = ((unsigned long)w+sizeof(cmmx_t)-1) / sizeof(cmmx_t);
658 cmmx_t dint_count = 0;
659 cmmx_t thr;
660 t |= t << 8;
661 thr = t | (t << 16);
662 if (sizeof(cmmx_t) > 4)
663 thr |= thr << (sizeof(cmmx_t)*4);
664 do {
665 cmmx_t e = *(cmmx_t*)a;
666 cmmx_t ne = *(cmmx_t*)(a+2*ss);
667 cmmx_t o = *(cmmx_t*)(a+bos);
668 cmmx_t oo = *(cmmx_t*)(a+cos);
669 cmmx_t maxe = pmaxub(e, ne);
670 cmmx_t avge = pavgb(e, ne);
671 cmmx_t max_diff = maxe - avge + thr; /* 0<=max-avg<128, thr<128 */
672 cmmx_t diffo = pdiffub(avge, o);
673 cmmx_t diffoo = pdiffub(avge, oo);
674 cmmx_t diffcmp = pcmpgtub(diffo, diffoo);
675 cmmx_t bo = ((oo ^ o) & diffcmp) ^ o;
676 cmmx_t diffbo = ((diffoo ^ diffo) & diffcmp) ^ diffo;
677 cmmx_t above_thr = ~pcmpgtub(max_diff, diffbo);
678 cmmx_t bo_or_avg = ((avge ^ bo) & above_thr) ^ bo;
679 dint_count += above_thr & ONE_BYTES;
680 *(cmmx_t*)(dst) = e;
681 *(cmmx_t*)(dst+ds) = bo_or_avg;
682 a += sizeof(cmmx_t);
683 dst += sizeof(cmmx_t);
684 } while (--len);
685 return psumbw(dint_count);
688 static int
689 dint_copy_plane(unsigned char *d, unsigned char *a, unsigned char *b,
690 unsigned char *c, unsigned long w, unsigned long h,
691 unsigned long ds, unsigned long ss, unsigned long threshold,
692 long field, long mmx2)
694 unsigned long ret = 0;
695 long bos = b - a;
696 long cos = c - a;
697 if (field) {
698 fast_memcpy(d, b, w);
699 h--;
700 d += ds;
701 a += ss;
703 bos += ss;
704 cos += ss;
705 while (h > 2) {
706 if (threshold >= 128) {
707 fast_memcpy(d, a, w);
708 fast_memcpy(d+ds, a+bos, w);
709 } else if (mmx2 == 1) {
710 ret += dint_copy_line_mmx2(d, a, bos, cos, ds, ss, w, threshold);
711 } else
712 ret += dint_copy_line(d, a, bos, cos, ds, ss, w, threshold);
713 h -= 2;
714 d += 2*ds;
715 a += 2*ss;
717 fast_memcpy(d, a, w);
718 if (h == 2)
719 fast_memcpy(d+ds, a+bos, w);
720 return ret;
723 static void
724 copy_merge_fields(struct vf_priv_s *p, mp_image_t *dmpi,
725 unsigned char **old, unsigned char **new, unsigned long show)
727 unsigned long threshold = 256;
728 unsigned long field = p->swapped;
729 unsigned long dint_pixels = 0;
730 unsigned char **other = old;
731 if (show >= 12 || !(show & 3))
732 show >>= 2, other = new, new = old;
733 if (show <= 2) { /* Single field: de-interlace */
734 threshold = p->dint_thres;
735 field ^= show & 1;
736 old = new;
737 } else if (show == 3)
738 old = new;
739 else
740 field ^= 1;
741 dint_pixels +=dint_copy_plane(dmpi->planes[0], old[0], new[0],
742 other[0], p->w, p->h, dmpi->stride[0],
743 p->stride, threshold, field, p->mmx2);
744 if (dmpi->flags & MP_IMGFLAG_PLANAR) {
745 if (p->luma_only)
746 old = new, other = new;
747 else
748 threshold = threshold/2 + 1;
749 field ^= p->chroma_swapped;
750 dint_copy_plane(dmpi->planes[1], old[1], new[1],
751 other[1], p->cw, p->ch, dmpi->stride[1],
752 p->chroma_stride, threshold, field, p->mmx2);
753 dint_copy_plane(dmpi->planes[2], old[2], new[2],
754 other[2], p->cw, p->ch, dmpi->stride[2],
755 p->chroma_stride, threshold, field, p->mmx2);
757 if (dint_pixels > 0 && p->verbose)
758 mp_msg(MSGT_VFILTER,MSGL_INFO,"Deinterlaced %lu pixels\n",dint_pixels);
761 static void diff_planes(struct vf_priv_s *p, struct frame_stats *s,
762 unsigned char *of, unsigned char *nf,
763 int w, int h, int os, int ns, int swapped)
765 int i, y;
766 int align = -(long)nf & 7;
767 of += align;
768 nf += align;
769 w -= align;
770 if (swapped)
771 of -= os, nf -= ns;
772 i = (h*3 >> 7) & ~1;
773 of += i*os + 8;
774 nf += i*ns + 8;
775 h -= i;
776 w -= 16;
778 memset(s, 0, sizeof(*s));
780 for (y = (h-8) >> 3; y; y--) {
781 if (p->mmx2 == 1) {
782 for (i = 0; i < w; i += 8)
783 block_metrics_mmx2(of+i, nf+i, os, ns, 4, p, s);
784 } else if (p->mmx2 == 2) {
785 for (i = 0; i < w; i += 8)
786 block_metrics_3dnow(of+i, nf+i, os, ns, 4, p, s);
787 } else if (p->fast > 3) {
788 for (i = 0; i < w; i += 8)
789 block_metrics_faster_c(of+i, nf+i, os, ns, 4, p, s);
790 } else if (p->fast > 1) {
791 for (i = 0; i < w; i += 8)
792 block_metrics_fast_c(of+i, nf+i, os, ns, 4, p, s);
793 } else {
794 for (i = 0; i < w; i += 8)
795 block_metrics_c(of+i, nf+i, os, ns, 4, p, s);
797 of += 8*os;
798 nf += 8*ns;
802 #define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
804 static void diff_fields(struct vf_priv_s *p, struct frame_stats *s,
805 unsigned char **old, unsigned char **new)
807 diff_planes(p, s, old[0], new[0], p->w, p->h,
808 p->stride, p->stride, p->swapped);
809 s->sad.even = (s->sad.even * 16ul) / s->num_blocks;
810 s->sad.odd = (s->sad.odd * 16ul) / s->num_blocks;
811 s->sad.noise = (s->sad.noise * 16ul) / s->num_blocks;
812 s->sad.temp = (s->sad.temp * 16ul) / s->num_blocks;
813 if (p->verbose)
814 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu%c M:%d/%d/%d/%d - %d, "
815 "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
816 "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
817 p->inframes, p->chflag, METRICS(s->max), s->num_blocks,
818 METRICS(s->tiny), METRICS(s->low), METRICS(s->high),
819 METRICS(s->bigger), METRICS(s->twox), METRICS(s->sad),
820 s->interlaced_low, s->interlaced_high,
821 p->iosync / (double) p->in_inc);
824 static const char *parse_args(struct vf_priv_s *p, const char *args)
826 args--;
827 while (args && *++args &&
828 (sscanf(args, "io=%lu:%lu", &p->out_dec, &p->in_inc) == 2 ||
829 sscanf(args, "diff_thres=%hu", &p->thres.even ) == 1 ||
830 sscanf(args, "comb_thres=%hu", &p->thres.noise) == 1 ||
831 sscanf(args, "sad_thres=%lu", &p->sad_thres ) == 1 ||
832 sscanf(args, "dint_thres=%lu", &p->dint_thres ) == 1 ||
833 sscanf(args, "fast=%u", &p->fast ) == 1 ||
834 sscanf(args, "mmx2=%lu", &p->mmx2 ) == 1 ||
835 sscanf(args, "luma_only=%u", &p->luma_only ) == 1 ||
836 sscanf(args, "verbose=%u", &p->verbose ) == 1 ||
837 sscanf(args, "crop=%lu:%lu:%lu:%lu", &p->w,
838 &p->h, &p->crop_x, &p->crop_y) == 4))
839 args = strchr(args, '/');
840 return args;
843 static unsigned long gcd(unsigned long x, unsigned long y)
845 unsigned long t;
846 if (x > y)
847 t = x, x = y, y = t;
849 while (x) {
850 t = y % x;
851 y = x;
852 x = t;
854 return y;
857 static void init(struct vf_priv_s *p, mp_image_t *mpi)
859 unsigned long i;
860 unsigned long plane_size, chroma_plane_size;
861 unsigned char *plane;
862 unsigned long cos, los;
863 p->crop_cx = p->crop_x >> mpi->chroma_x_shift;
864 p->crop_cy = p->crop_y >> mpi->chroma_y_shift;
865 if (mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) {
866 p->stride = (mpi->w + 15) & ~15;
867 p->chroma_stride = p->stride >> mpi->chroma_x_shift;
868 } else {
869 p->stride = mpi->width;
870 p->chroma_stride = mpi->chroma_width;
872 p->cw = p->w >> mpi->chroma_x_shift;
873 p->ch = p->h >> mpi->chroma_y_shift;
874 p->nplanes = 1;
875 p->static_idx = 0;
876 p->temp_idx = 0;
877 p->old_planes = p->planes[0];
878 plane_size = mpi->h * p->stride;
879 chroma_plane_size = mpi->flags & MP_IMGFLAG_PLANAR ?
880 mpi->chroma_height * p->chroma_stride : 0;
881 p->memory_allocated =
882 malloc(NUM_STORED * (plane_size+2*chroma_plane_size) +
883 8*p->chroma_stride + 4096);
884 /* align to page boundary */
885 plane = p->memory_allocated + (-(long)p->memory_allocated & 4095);
886 memset(plane, 0, NUM_STORED * plane_size);
887 los = p->crop_x + p->crop_y * p->stride;
888 cos = p->crop_cx + p->crop_cy * p->chroma_stride;
889 for (i = 0; i != NUM_STORED; i++, plane += plane_size) {
890 p->planes[i][0] = plane;
891 p->planes[NUM_STORED + i][0] = plane + los;
893 if (mpi->flags & MP_IMGFLAG_PLANAR) {
894 p->nplanes = 3;
895 memset(plane, 0x80, NUM_STORED * 2 * chroma_plane_size);
896 for (i = 0; i != NUM_STORED; i++) {
897 p->planes[i][1] = plane;
898 p->planes[NUM_STORED + i][1] = plane + cos;
899 plane += chroma_plane_size;
900 p->planes[i][2] = plane;
901 p->planes[NUM_STORED + i][2] = plane + cos;
902 plane += chroma_plane_size;
905 p->out_dec <<= 2;
906 i = gcd(p->in_inc, p->out_dec);
907 p->in_inc /= i;
908 p->out_dec /= i;
909 p->iosync = 0;
910 p->num_fields = 3;
913 static inline double get_time(void)
915 struct timeval tv;
916 gettimeofday(&tv, 0);
917 return tv.tv_sec + tv.tv_usec * 1e-6;
920 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi)
922 struct vf_priv_s *p = vf->priv;
923 static unsigned char **planes, planes_idx;
925 if (mpi->type == MP_IMGTYPE_STATIC) return;
927 if (!p->planes[0][0]) init(p, mpi);
929 if (mpi->type == MP_IMGTYPE_TEMP ||
930 (mpi->type == MP_IMGTYPE_IPB && !(mpi->flags & MP_IMGFLAG_READABLE)))
931 planes_idx = NUM_STORED/2 + (++p->temp_idx % (NUM_STORED/2));
932 else
933 planes_idx = ++p->static_idx % (NUM_STORED/2);
934 planes = p->planes[planes_idx];
935 mpi->priv = p->planes[NUM_STORED + planes_idx];
936 if (mpi->priv == p->old_planes) {
937 unsigned char **old_planes =
938 p->planes[NUM_STORED + 2 + (++p->temp_idx & 1)];
939 my_memcpy_pic(old_planes[0], p->old_planes[0],
940 p->w, p->h, p->stride, p->stride);
941 if (mpi->flags & MP_IMGFLAG_PLANAR) {
942 my_memcpy_pic(old_planes[1], p->old_planes[1],
943 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
944 my_memcpy_pic(old_planes[2], p->old_planes[2],
945 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
947 p->old_planes = old_planes;
948 p->num_copies++;
950 mpi->planes[0] = planes[0];
951 mpi->stride[0] = p->stride;
952 if (mpi->flags & MP_IMGFLAG_PLANAR) {
953 mpi->planes[1] = planes[1];
954 mpi->planes[2] = planes[2];
955 mpi->stride[1] = mpi->stride[2] = p->chroma_stride;
957 mpi->width = p->stride;
959 mpi->flags |= MP_IMGFLAG_DIRECT;
960 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
963 static inline long
964 cmpe(unsigned long x, unsigned long y, unsigned long err, unsigned long e)
966 long diff = x-y;
967 long unit = ((x+y+err) >> e);
968 long ret = (diff > unit) - (diff < -unit);
969 unit >>= 1;
970 return ret + (diff > unit) - (diff < -unit);
973 static unsigned long
974 find_breaks(struct vf_priv_s *p, struct frame_stats *s)
976 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
977 long notfilm = 5*p->in_inc - p->out_dec;
978 unsigned long n = s->num_blocks >> 8;
979 unsigned long sad_comb_cmp = cmpe(s->sad.temp, s->sad.noise, 512, 1);
980 unsigned long ret = 8;
982 if (cmpe(s->sad.temp, s->sad.even, 512, 1) > 0)
983 mp_msg(MSGT_VFILTER, MSGL_WARN,
984 "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
985 if (s->sad.temp > 1000 && s->sad.noise > 1000)
986 return 3;
987 if (s->interlaced_high >= 2*n && s->sad.temp > 256 && s->sad.noise > 256)
988 return 3;
989 if (s->high.noise > s->num_blocks/4 && s->sad.noise > 10000 &&
990 s->sad.noise > 2*s->sad.even && s->sad.noise > 2*ps->sad.odd) {
991 // Mid-frame scene change
992 if (s->tiny.temp + s->interlaced_low < n ||
993 s->low.temp + s->interlaced_high < n/4 ||
994 s->high.temp + s->interlaced_high < n/8 ||
995 s->sad.temp < 160)
996 return 1;
997 return 3;
999 if (s->high.temp > s->num_blocks/4 && s->sad.temp > 10000 &&
1000 s->sad.temp > 2*ps->sad.odd && s->sad.temp > 2*ps->sad.even) {
1001 // Start frame scene change
1002 if (s->tiny.noise + s->interlaced_low < n ||
1003 s->low.noise + s->interlaced_high < n/4 ||
1004 s->high.noise + s->interlaced_high < n/8 ||
1005 s->sad.noise < 160)
1006 return 2;
1007 return 3;
1009 if (sad_comb_cmp == 2)
1010 return 2;
1011 if (sad_comb_cmp == -2)
1012 return 1;
1014 if (s->tiny.odd > 3*MAX(n,s->tiny.even) + s->interlaced_low)
1015 return 1;
1016 if (s->tiny.even > 3*MAX(n,s->tiny.odd)+s->interlaced_low &&
1017 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1018 return 4;
1020 if (s->sad.noise < 64 && s->sad.temp < 64 &&
1021 s->low.noise <= n/2 && s->high.noise <= n/4 &&
1022 s->low.temp <= n/2 && s->high.temp <= n/4)
1023 goto still;
1025 if (s->tiny.temp > 3*MAX(n,s->tiny.noise) + s->interlaced_low)
1026 return 2;
1027 if (s->tiny.noise > 3*MAX(n,s->tiny.temp) + s->interlaced_low)
1028 return 1;
1030 if (s->low.odd > 3*MAX(n/4,s->low.even) + s->interlaced_high)
1031 return 1;
1032 if (s->low.even > 3*MAX(n/4,s->low.odd)+s->interlaced_high &&
1033 s->sad.even > 2*s->sad.odd &&
1034 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1035 return 4;
1037 if (s->low.temp > 3*MAX(n/4,s->low.noise) + s->interlaced_high)
1038 return 2;
1039 if (s->low.noise > 3*MAX(n/4,s->low.temp) + s->interlaced_high)
1040 return 1;
1042 if (sad_comb_cmp == 1 && s->sad.noise < 64)
1043 return 2;
1044 if (sad_comb_cmp == -1 && s->sad.temp < 64)
1045 return 1;
1047 if (s->tiny.odd <= n || (s->tiny.noise <= n/2 && s->tiny.temp <= n/2)) {
1048 if (s->interlaced_low <= n) {
1049 if (p->num_fields == 1)
1050 goto still;
1051 if (s->tiny.even <= n || ps->tiny.noise <= n/2)
1052 /* Still frame */
1053 goto still;
1054 if (s->bigger.even >= 2*MAX(n,s->bigger.odd) + s->interlaced_low)
1055 return 4;
1056 if (s->low.even >= 2*n + s->interlaced_low)
1057 return 4;
1058 goto still;
1061 if (s->low.odd <= n/4) {
1062 if (s->interlaced_high <= n/4) {
1063 if (p->num_fields == 1)
1064 goto still;
1065 if (s->low.even <= n/4)
1066 /* Still frame */
1067 goto still;
1068 if (s->bigger.even >= 2*MAX(n/4,s->bigger.odd)+s->interlaced_high)
1069 return 4;
1070 if (s->low.even >= n/2 + s->interlaced_high)
1071 return 4;
1072 goto still;
1075 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_low)
1076 return 2;
1077 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_low)
1078 return 1;
1079 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_high)
1080 return 2;
1081 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_high)
1082 return 1;
1083 if (s->twox.temp > 2*MAX(n,s->twox.noise) + s->interlaced_high)
1084 return 2;
1085 if (s->twox.noise > 2*MAX(n,s->twox.temp) + s->interlaced_high)
1086 return 1;
1087 if (s->bigger.even > 2*MAX(n,s->bigger.odd) + s->interlaced_low &&
1088 s->bigger.temp < n && s->bigger.noise < n)
1089 return 4;
1090 if (s->interlaced_low > MIN(2*n, s->tiny.odd))
1091 return 3;
1092 ret = 8 + (1 << (s->sad.temp > s->sad.noise));
1093 still:
1094 if (p->num_fields == 1 && p->prev_fields == 3 && notfilm >= 0 &&
1095 (s->tiny.temp <= s->tiny.noise || s->sad.temp < s->sad.noise+16))
1096 return 1;
1097 if (p->notout < p->num_fields && p->iosync > 2*p->in_inc && notfilm < 0)
1098 notfilm = 0;
1099 if (p->num_fields < 2 ||
1100 (p->num_fields == 2 && p->prev_fields == 2 && notfilm < 0))
1101 return ret;
1102 if (!notfilm && (p->prev_fields&~1) == 2) {
1103 if (p->prev_fields + p->num_fields == 5) {
1104 if (s->tiny.noise <= s->tiny.temp ||
1105 s->low.noise == 0 || s->low.noise < s->low.temp ||
1106 s->sad.noise < s->sad.temp+16)
1107 return 2;
1109 if (p->prev_fields + p->num_fields == 4) {
1110 if (s->tiny.temp <= s->tiny.noise ||
1111 s->low.temp == 0 || s->low.temp < s->low.noise ||
1112 s->sad.temp < s->sad.noise+16)
1113 return 1;
1116 if (p->num_fields > 2 &&
1117 ps->sad.noise > s->sad.noise && ps->sad.noise > s->sad.temp)
1118 return 4;
1119 return 2 >> (s->sad.noise > s->sad.temp);
1122 #define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
1124 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
1126 mp_image_t *dmpi;
1127 struct vf_priv_s *p = vf->priv;
1128 unsigned char **planes, **old_planes;
1129 struct frame_stats *s = &p->stats[p->inframes & 1];
1130 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
1131 int swapped = 0;
1132 const int flags = mpi->fields;
1133 int breaks, prev;
1134 int show_fields = 0;
1135 int dropped_fields = 0;
1136 double start_time, diff_time;
1137 char prev_chflag = p->chflag;
1138 int keep_rate;
1140 if (!p->planes[0][0]) init(p, mpi);
1142 old_planes = p->old_planes;
1144 if ((mpi->flags & MP_IMGFLAG_DIRECT) && mpi->priv) {
1145 planes = mpi->priv;
1146 mpi->priv = 0;
1147 } else {
1148 planes = p->planes[2 + (++p->temp_idx & 1)];
1149 my_memcpy_pic(planes[0],
1150 mpi->planes[0] + p->crop_x + p->crop_y * mpi->stride[0],
1151 p->w, p->h, p->stride, mpi->stride[0]);
1152 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1153 my_memcpy_pic(planes[1],
1154 mpi->planes[1] + p->crop_cx + p->crop_cy * mpi->stride[1],
1155 p->cw, p->ch, p->chroma_stride, mpi->stride[1]);
1156 my_memcpy_pic(planes[2],
1157 mpi->planes[2] + p->crop_cx + p->crop_cy * mpi->stride[2],
1158 p->cw, p->ch, p->chroma_stride, mpi->stride[2]);
1159 p->num_copies++;
1163 p->old_planes = planes;
1164 p->chflag = ';';
1165 if (flags & MP_IMGFIELD_ORDERED) {
1166 swapped = !(flags & MP_IMGFIELD_TOP_FIRST);
1167 p->chflag = (flags & MP_IMGFIELD_REPEAT_FIRST ? '|' :
1168 flags & MP_IMGFIELD_TOP_FIRST ? ':' : '.');
1170 p->swapped = swapped;
1172 start_time = get_time();
1173 if (p->chflag == '|') {
1174 *s = ppzs;
1175 p->iosync += p->in_inc;
1176 } else if ((p->fast & 1) && prev_chflag == '|')
1177 *s = pprs;
1178 else
1179 diff_fields(p, s, old_planes, planes);
1180 diff_time = get_time();
1181 p->diff_time += diff_time - start_time;
1182 breaks = p->inframes ? find_breaks(p, s) : 2;
1183 p->inframes++;
1184 keep_rate = 4*p->in_inc == p->out_dec;
1186 switch (breaks) {
1187 case 0:
1188 case 8:
1189 case 9:
1190 case 10:
1191 if (!keep_rate && p->notout < p->num_fields && p->iosync < 2*p->in_inc)
1192 break;
1193 if (p->notout < p->num_fields)
1194 dropped_fields = -2;
1195 case 4:
1196 if (keep_rate || p->iosync >= -2*p->in_inc)
1197 show_fields = (4<<p->num_fields)-1;
1198 break;
1199 case 3:
1200 if (keep_rate)
1201 show_fields = 2;
1202 else if (p->iosync > 0) {
1203 if (p->notout >= p->num_fields && p->iosync > 2*p->in_inc) {
1204 show_fields = 4; /* prev odd only */
1205 if (p->num_fields > 1)
1206 show_fields |= 8; /* + prev even */
1207 } else {
1208 show_fields = 2; /* even only */
1209 if (p->notout >= p->num_fields)
1210 dropped_fields += p->num_fields;
1213 break;
1214 case 2:
1215 if (p->iosync <= -3*p->in_inc) {
1216 if (p->notout >= p->num_fields)
1217 dropped_fields = p->num_fields;
1218 break;
1220 if (p->num_fields == 1) {
1221 int prevbreak = ps->sad.noise >= 128;
1222 if (p->iosync < 4*p->in_inc) {
1223 show_fields = 3;
1224 dropped_fields = prevbreak;
1225 } else {
1226 show_fields = 4 | (!prevbreak << 3);
1227 if (p->notout < 1 + p->prev_fields)
1228 dropped_fields = -!prevbreak;
1230 break;
1232 default:
1233 if (keep_rate)
1234 show_fields = 3 << (breaks & 1);
1235 else if (p->notout >= p->num_fields &&
1236 p->iosync >= (breaks == 1 ? -p->in_inc :
1237 p->in_inc << (p->num_fields == 1))) {
1238 show_fields = (1 << (2 + p->num_fields)) - (1<<breaks);
1239 } else {
1240 if (p->notout >= p->num_fields)
1241 dropped_fields += p->num_fields + 2 - breaks;
1242 if (breaks == 1) {
1243 if (p->iosync >= 4*p->in_inc)
1244 show_fields = 6;
1245 } else if (p->iosync > -3*p->in_inc)
1246 show_fields = 3; /* odd+even */
1248 break;
1251 show_fields &= 15;
1252 prev = p->prev_fields;
1253 if (breaks < 8) {
1254 if (p->num_fields == 1)
1255 breaks &= ~4;
1256 if (breaks)
1257 p->num_breaks++;
1258 if (breaks == 3)
1259 p->prev_fields = p->num_fields = 1;
1260 else if (breaks) {
1261 p->prev_fields = p->num_fields + (breaks==1) - (breaks==4);
1262 p->num_fields = breaks - (breaks == 4) + (p->chflag == '|');
1263 } else
1264 p->num_fields += 2;
1265 } else
1266 p->num_fields += 2;
1268 p->iosync += 4 * p->in_inc;
1269 if (p->chflag == '|')
1270 p->iosync += p->in_inc;
1272 if (show_fields) {
1273 p->iosync -= p->out_dec;
1274 p->notout = !(show_fields & 1) + !(show_fields & 3);
1275 if (((show_fields & 3) == 3 &&
1276 (s->low.noise + s->interlaced_low < (s->num_blocks>>8) ||
1277 s->sad.noise < 160)) ||
1278 ((show_fields & 12) == 12 &&
1279 (ps->low.noise + ps->interlaced_low < (s->num_blocks>>8) ||
1280 ps->sad.noise < 160))) {
1281 p->export_count++;
1282 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT,
1283 MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE,
1284 p->w, p->h);
1285 if ((show_fields & 3) != 3) planes = old_planes;
1286 dmpi->planes[0] = planes[0];
1287 dmpi->stride[0] = p->stride;
1288 dmpi->width = mpi->width;
1289 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1290 dmpi->planes[1] = planes[1];
1291 dmpi->planes[2] = planes[2];
1292 dmpi->stride[1] = p->chroma_stride;
1293 dmpi->stride[2] = p->chroma_stride;
1295 } else {
1296 p->merge_count++;
1297 dmpi = vf_get_image(vf->next, mpi->imgfmt,
1298 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
1299 p->w, p->h);
1300 copy_merge_fields(p, dmpi, old_planes, planes, show_fields);
1302 p->outframes++;
1303 } else
1304 p->notout += 2;
1306 if (p->verbose)
1307 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
1308 p->inframes, p->outframes,
1309 breaks, breaks<8 && breaks>0 ? (int) p->prev_fields+'0' : ' ',
1310 ITOC(show_fields),
1311 p->num_breaks, 5*p->in_inc == p->out_dec && breaks<8 &&
1312 breaks>0 && ((prev&~1)!=2 || prev+p->prev_fields!=5) ?
1313 " ######## bad telecine ########" : "",
1314 dropped_fields ? " ======== dropped ":"", ITOC(dropped_fields),
1315 !show_fields || (show_fields & (show_fields-1)) ?
1316 "" : " @@@@@@@@@@@@@@@@@");
1318 p->merge_time += get_time() - diff_time;
1319 return show_fields ? vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE) : 0;
1322 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
1324 /* FIXME - support more formats */
1325 switch (fmt) {
1326 case IMGFMT_YV12:
1327 case IMGFMT_IYUV:
1328 case IMGFMT_I420:
1329 case IMGFMT_411P:
1330 case IMGFMT_422P:
1331 case IMGFMT_444P:
1332 return vf_next_query_format(vf, fmt);
1334 return 0;
1337 static int config(struct vf_instance_s* vf,
1338 int width, int height, int d_width, int d_height,
1339 unsigned int flags, unsigned int outfmt)
1341 unsigned long cxm = 0;
1342 unsigned long cym = 0;
1343 struct vf_priv_s *p = vf->priv;
1344 // rounding:
1345 if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
1346 switch(outfmt){
1347 case IMGFMT_444P:
1348 case IMGFMT_Y800:
1349 case IMGFMT_Y8:
1350 break;
1351 case IMGFMT_YVU9:
1352 case IMGFMT_IF09:
1353 cym = 3;
1354 case IMGFMT_411P:
1355 cxm = 3;
1356 break;
1357 case IMGFMT_YV12:
1358 case IMGFMT_I420:
1359 case IMGFMT_IYUV:
1360 cym = 1;
1361 default:
1362 cxm = 1;
1365 p->chroma_swapped = !!(p->crop_y & (cym+1));
1366 if (p->w) p->w += p->crop_x & cxm;
1367 if (p->h) p->h += p->crop_y & cym;
1368 p->crop_x &= ~cxm;
1369 p->crop_y &= ~cym;
1370 if (!p->w || p->w > width ) p->w = width;
1371 if (!p->h || p->h > height) p->h = height;
1372 if (p->crop_x + p->w > width ) p->crop_x = 0;
1373 if (p->crop_y + p->h > height) p->crop_y = 0;
1375 if(!opt_screen_size_x && !opt_screen_size_y){
1376 d_width = d_width * p->w/width;
1377 d_height = d_height * p->h/height;
1379 return vf_next_config(vf, p->w, p->h, d_width, d_height, flags, outfmt);
1382 static void uninit(struct vf_instance_s* vf)
1384 struct vf_priv_s *p = vf->priv;
1385 mp_msg(MSGT_VFILTER, MSGL_INFO, "diff_time: %.3f, merge_time: %.3f, "
1386 "export: %lu, merge: %lu, copy: %lu\n", p->diff_time, p->merge_time,
1387 p->export_count, p->merge_count, p->num_copies);
1388 free(p->memory_allocated);
1389 free(p);
1392 static int open(vf_instance_t *vf, char* args)
1394 struct vf_priv_s *p;
1395 vf->get_image = get_image;
1396 vf->put_image = put_image;
1397 vf->config = config;
1398 vf->query_format = query_format;
1399 vf->uninit = uninit;
1400 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
1401 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
1402 p->out_dec = 5;
1403 p->in_inc = 4;
1404 p->thres.noise = 128;
1405 p->thres.even = 128;
1406 p->sad_thres = 64;
1407 p->dint_thres = 4;
1408 p->luma_only = 0;
1409 p->fast = 3;
1410 p->mmx2 = gCpuCaps.hasMMX2 ? 1 : gCpuCaps.has3DNow ? 2 : 0;
1411 if (args) {
1412 const char *args_remain = parse_args(p, args);
1413 if (args_remain) {
1414 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1415 "filmdint: unknown suboption: %s\n", args_remain);
1416 return 0;
1418 if (p->out_dec < p->in_inc) {
1419 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1420 "filmdint: increasing the frame rate is not supported\n");
1421 return 0;
1424 if (p->mmx2 > 2)
1425 p->mmx2 = 0;
1426 #ifndef HAVE_MMX
1427 p->mmx2 = 0;
1428 #endif
1429 #ifndef HAVE_3DNOW
1430 p->mmx2 &= 1;
1431 #endif
1432 p->thres.odd = p->thres.even;
1433 p->thres.temp = p->thres.noise;
1434 p->diff_time = 0;
1435 p->merge_time = 0;
1436 return 1;
1439 const vf_info_t vf_info_filmdint = {
1440 "Advanced inverse telecine filer",
1441 "filmdint",
1442 "Zoltan Hidvegi",
1444 open,
1445 NULL