input: make slave command file descriptors nonblocking
[mplayer/greg.git] / libmpcodecs / vf_filmdint.c
blobb40a66831acced07ec08a7832917ababe921e34f
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/time.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "cpudetect.h"
27 #include "options.h"
29 #include "img_format.h"
30 #include "mp_image.h"
31 #include "vf.h"
32 #include "cmmx.h"
34 #include "libvo/fastmemcpy.h"
36 #define NUM_STORED 4
38 enum pu_field_type_t {
39 PU_1ST_OF_3,
40 PU_2ND_OF_3,
41 PU_3RD_OF_3,
42 PU_1ST_OF_2,
43 PU_2ND_OF_2,
44 PU_INTERLACED
47 struct metrics {
48 /* This struct maps to a packed word 64-bit MMX register */
49 unsigned short int even;
50 unsigned short int odd;
51 unsigned short int noise;
52 unsigned short int temp;
53 } __attribute__ ((aligned (8)));
55 struct frame_stats {
56 struct metrics tiny, low, high, bigger, twox, max;
57 struct { unsigned int even, odd, noise, temp; } sad;
58 unsigned short interlaced_high;
59 unsigned short interlaced_low;
60 unsigned short num_blocks;
63 struct vf_priv_s {
64 unsigned long inframes;
65 unsigned long outframes;
66 enum pu_field_type_t prev_type;
67 unsigned swapped, chroma_swapped;
68 unsigned luma_only;
69 unsigned verbose;
70 unsigned fast;
71 unsigned long w, h, cw, ch, stride, chroma_stride, nplanes;
72 unsigned long sad_thres;
73 unsigned long dint_thres;
74 unsigned char *memory_allocated;
75 unsigned char *planes[2*NUM_STORED][4];
76 unsigned char **old_planes;
77 unsigned long static_idx;
78 unsigned long temp_idx;
79 unsigned long crop_x, crop_y, crop_cx, crop_cy;
80 unsigned long export_count, merge_count;
81 unsigned long num_breaks;
82 unsigned long num_copies;
83 long in_inc, out_dec, iosync;
84 long num_fields;
85 long prev_fields;
86 long notout;
87 long mmx2;
88 unsigned small_bytes[2];
89 unsigned mmx_temp[2];
90 struct frame_stats stats[2];
91 struct metrics thres;
92 char chflag;
93 double diff_time, merge_time, decode_time, vo_time, filter_time;
94 struct vf_detc_pts_buf ptsbuf;
97 #define PPZ { 2000, 2000, 0, 2000 }
98 #define PPR { 2000, 2000, 0, 2000 }
99 static const struct frame_stats ppzs = {PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,0,0,9999};
100 static const struct frame_stats pprs = {PPR,PPR,PPR,PPR,PPR,PPR,PPR,0,0,9999};
102 #ifndef MIN
103 #define MIN(a,b) (((a)<(b))?(a):(b))
104 #endif
105 #ifndef MAX
106 #define MAX(a,b) (((a)>(b))?(a):(b))
107 #endif
109 #define PDIFFUB(X,Y,T) "movq " #X "," #T "\n\t" \
110 "psubusb " #Y "," #T "\n\t" \
111 "psubusb " #X "," #Y "\n\t" \
112 "paddusb " #Y "," #T "\n\t"
114 #define PDIFFUBT(X,Y,T) "movq " #X "," #T "\n\t" \
115 "psubusb " #Y "," #T "\n\t" \
116 "psubusb " #X "," #Y "\n\t" \
117 "paddusb " #T "," #Y "\n\t"
119 #define PSUMBW(X,T,Z) "movq " #X "," #T "\n\t" \
120 "punpcklbw " #Z "," #X "\n\t" \
121 "punpckhbw " #Z "," #T "\n\t" \
122 "paddw " #T "," #X "\n\t" \
123 "movq " #X "," #T "\n\t" \
124 "psllq $32, " #T "\n\t" \
125 "paddw " #T "," #X "\n\t" \
126 "movq " #X "," #T "\n\t" \
127 "psllq $16, " #T "\n\t" \
128 "paddw " #T "," #X "\n\t" \
129 "psrlq $48, " #X "\n\t"
131 #define PSADBW(X,Y,T,Z) PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
133 #define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
134 #define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
135 #define PMINUBT(X,Y,T) "movq " #Y "," #T "\n\t" \
136 "psubusb " #X "," #T "\n\t" \
137 "psubusb " #T "," #Y "\n\t"
138 #define PAVGB(X,Y) "pavgusb " #X "," #Y "\n\t"
140 static inline void
141 get_metrics_c(unsigned char *a, unsigned char *b, int as, int bs, int lines,
142 struct metrics *m)
144 a -= as;
145 b -= bs;
146 do {
147 cmmx_t old_po = *(cmmx_t*)(a );
148 cmmx_t po = *(cmmx_t*)(b );
149 cmmx_t e = *(cmmx_t*)(b + bs);
150 cmmx_t old_o = *(cmmx_t*)(a + 2*as);
151 cmmx_t o = *(cmmx_t*)(b + 2*bs);
152 cmmx_t ne = *(cmmx_t*)(b + 3*bs);
153 cmmx_t old_no = *(cmmx_t*)(a + 4*as);
154 cmmx_t no = *(cmmx_t*)(b + 4*bs);
156 cmmx_t qup_old_odd = p31avgb(old_o, old_po);
157 cmmx_t qup_odd = p31avgb( o, po);
158 cmmx_t qdown_old_odd = p31avgb(old_o, old_no);
159 cmmx_t qdown_odd = p31avgb( o, no);
161 cmmx_t qup_even = p31avgb(ne, e);
162 cmmx_t qdown_even = p31avgb(e, ne);
164 cmmx_t temp_up_diff = pdiffub(qdown_even, qup_old_odd);
165 cmmx_t noise_up_diff = pdiffub(qdown_even, qup_odd);
166 cmmx_t temp_down_diff = pdiffub(qup_even, qdown_old_odd);
167 cmmx_t noise_down_diff = pdiffub(qup_even, qdown_odd);
169 cmmx_t odd_diff = pdiffub(o, old_o);
170 m->odd += psumbw(odd_diff);
171 m->even += psadbw(e, *(cmmx_t*)(a+as));
173 temp_up_diff = pminub(temp_up_diff, temp_down_diff);
174 temp_up_diff = pminub(temp_up_diff, odd_diff);
175 m->temp += psumbw(temp_up_diff);
176 noise_up_diff = pminub(noise_up_diff, odd_diff);
177 noise_up_diff = pminub(noise_up_diff, noise_down_diff);
179 m->noise += psumbw(noise_up_diff);
180 a += 2*as;
181 b += 2*bs;
182 } while (--lines);
185 static inline void
186 get_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
187 int lines, struct metrics *m)
189 a -= as;
190 b -= bs;
191 do {
192 cmmx_t old_po = (*(cmmx_t*)(a ) >> 1) & ~SIGN_BITS;
193 cmmx_t po = (*(cmmx_t*)(b ) >> 1) & ~SIGN_BITS;
194 cmmx_t old_e = (*(cmmx_t*)(a + as) >> 1) & ~SIGN_BITS;
195 cmmx_t e = (*(cmmx_t*)(b + bs) >> 1) & ~SIGN_BITS;
196 cmmx_t old_o = (*(cmmx_t*)(a + 2*as) >> 1) & ~SIGN_BITS;
197 cmmx_t o = (*(cmmx_t*)(b + 2*bs) >> 1) & ~SIGN_BITS;
198 cmmx_t ne = (*(cmmx_t*)(b + 3*bs) >> 1) & ~SIGN_BITS;
199 cmmx_t old_no = (*(cmmx_t*)(a + 4*as) >> 1) & ~SIGN_BITS;
200 cmmx_t no = (*(cmmx_t*)(b + 4*bs) >> 1) & ~SIGN_BITS;
202 cmmx_t qup_old_odd = p31avgb_s(old_o, old_po);
203 cmmx_t qup_odd = p31avgb_s( o, po);
204 cmmx_t qdown_old_odd = p31avgb_s(old_o, old_no);
205 cmmx_t qdown_odd = p31avgb_s( o, no);
207 cmmx_t qup_even = p31avgb_s(ne, e);
208 cmmx_t qdown_even = p31avgb_s(e, ne);
210 cmmx_t temp_up_diff = pdiffub_s(qdown_even, qup_old_odd);
211 cmmx_t noise_up_diff = pdiffub_s(qdown_even, qup_odd);
212 cmmx_t temp_down_diff = pdiffub_s(qup_even, qdown_old_odd);
213 cmmx_t noise_down_diff = pdiffub_s(qup_even, qdown_odd);
215 cmmx_t odd_diff = pdiffub_s(o, old_o);
216 m->odd += psumbw_s(odd_diff) << 1;
217 m->even += psadbw_s(e, old_e) << 1;
219 temp_up_diff = pminub_s(temp_up_diff, temp_down_diff);
220 temp_up_diff = pminub_s(temp_up_diff, odd_diff);
221 m->temp += psumbw_s(temp_up_diff) << 1;
222 noise_up_diff = pminub_s(noise_up_diff, odd_diff);
223 noise_up_diff = pminub_s(noise_up_diff, noise_down_diff);
225 m->noise += psumbw_s(noise_up_diff) << 1;
226 a += 2*as;
227 b += 2*bs;
228 } while (--lines);
231 static inline void
232 get_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
233 int lines, struct metrics *m)
235 a -= as;
236 b -= bs;
237 do {
238 cmmx_t old_po = (*(cmmx_t*)(a )>>1) & ~SIGN_BITS;
239 cmmx_t po = (*(cmmx_t*)(b )>>1) & ~SIGN_BITS;
240 cmmx_t old_e = (*(cmmx_t*)(a + as)>>1) & ~SIGN_BITS;
241 cmmx_t e = (*(cmmx_t*)(b + bs)>>1) & ~SIGN_BITS;
242 cmmx_t old_o = (*(cmmx_t*)(a + 2*as)>>1) & ~SIGN_BITS;
243 cmmx_t o = (*(cmmx_t*)(b + 2*bs)>>1) & ~SIGN_BITS;
244 cmmx_t ne = (*(cmmx_t*)(b + 3*bs)>>1) & ~SIGN_BITS;
246 cmmx_t down_even = p31avgb_s(e, ne);
247 cmmx_t up_odd = p31avgb_s(o, po);
248 cmmx_t up_old_odd = p31avgb_s(old_o, old_po);
250 cmmx_t odd_diff = pdiffub_s(o, old_o);
251 cmmx_t temp_diff = pdiffub_s(down_even, up_old_odd);
252 cmmx_t noise_diff = pdiffub_s(down_even, up_odd);
254 m->even += psadbw_s(e, old_e) << 1;
255 m->odd += psumbw_s(odd_diff) << 1;
257 temp_diff = pminub_s(temp_diff, odd_diff);
258 noise_diff = pminub_s(noise_diff, odd_diff);
260 m->noise += psumbw_s(noise_diff) << 1;
261 m->temp += psumbw_s(temp_diff) << 1;
262 a += 2*as;
263 b += 2*bs;
264 } while (--lines);
268 static inline void
269 get_block_stats(struct metrics *m, struct vf_priv_s *p, struct frame_stats *s)
271 unsigned two_e = m->even + MAX(m->even , p->thres.even );
272 unsigned two_o = m->odd + MAX(m->odd , p->thres.odd );
273 unsigned two_n = m->noise + MAX(m->noise, p->thres.noise);
274 unsigned two_t = m->temp + MAX(m->temp , p->thres.temp );
276 unsigned e_big = m->even >= (m->odd + two_o + 1)/2;
277 unsigned o_big = m->odd >= (m->even + two_e + 1)/2;
278 unsigned n_big = m->noise >= (m->temp + two_t + 1)/2;
279 unsigned t_big = m->temp >= (m->noise + two_n + 1)/2;
281 unsigned e2x = m->even >= two_o;
282 unsigned o2x = m->odd >= two_e;
283 unsigned n2x = m->noise >= two_t;
284 unsigned t2x = m->temp >= two_n;
286 unsigned ntiny_e = m->even > p->thres.even ;
287 unsigned ntiny_o = m->odd > p->thres.odd ;
288 unsigned ntiny_n = m->noise > p->thres.noise;
289 unsigned ntiny_t = m->temp > p->thres.temp ;
291 unsigned nlow_e = m->even > 2*p->thres.even ;
292 unsigned nlow_o = m->odd > 2*p->thres.odd ;
293 unsigned nlow_n = m->noise > 2*p->thres.noise;
294 unsigned nlow_t = m->temp > 2*p->thres.temp ;
296 unsigned high_e = m->even > 4*p->thres.even ;
297 unsigned high_o = m->odd > 4*p->thres.odd ;
298 unsigned high_n = m->noise > 4*p->thres.noise;
299 unsigned high_t = m->temp > 4*p->thres.temp ;
301 unsigned low_il = !n_big && !t_big && ntiny_n && ntiny_t;
302 unsigned high_il = !n_big && !t_big && nlow_n && nlow_t;
304 if (low_il | high_il) {
305 s->interlaced_low += low_il;
306 s->interlaced_high += high_il;
307 } else {
308 s->tiny.even += ntiny_e;
309 s->tiny.odd += ntiny_o;
310 s->tiny.noise += ntiny_n;
311 s->tiny.temp += ntiny_t;
313 s->low .even += nlow_e ;
314 s->low .odd += nlow_o ;
315 s->low .noise += nlow_n ;
316 s->low .temp += nlow_t ;
318 s->high.even += high_e ;
319 s->high.odd += high_o ;
320 s->high.noise += high_n ;
321 s->high.temp += high_t ;
323 if (m->even >= p->sad_thres) s->sad.even += m->even ;
324 if (m->odd >= p->sad_thres) s->sad.odd += m->odd ;
325 if (m->noise >= p->sad_thres) s->sad.noise += m->noise;
326 if (m->temp >= p->sad_thres) s->sad.temp += m->temp ;
328 s->num_blocks++;
329 s->max.even = MAX(s->max.even , m->even );
330 s->max.odd = MAX(s->max.odd , m->odd );
331 s->max.noise = MAX(s->max.noise, m->noise);
332 s->max.temp = MAX(s->max.temp , m->temp );
334 s->bigger.even += e_big ;
335 s->bigger.odd += o_big ;
336 s->bigger.noise += n_big ;
337 s->bigger.temp += t_big ;
339 s->twox.even += e2x ;
340 s->twox.odd += o2x ;
341 s->twox.noise += n2x ;
342 s->twox.temp += t2x ;
346 static inline struct metrics
347 block_metrics_c(unsigned char *a, unsigned char *b, int as, int bs,
348 int lines, struct vf_priv_s *p, struct frame_stats *s)
350 struct metrics tm;
351 tm.even = tm.odd = tm.noise = tm.temp = 0;
352 get_metrics_c(a, b, as, bs, lines, &tm);
353 if (sizeof(cmmx_t) < 8)
354 get_metrics_c(a+4, b+4, as, bs, lines, &tm);
355 get_block_stats(&tm, p, s);
356 return tm;
359 static inline struct metrics
360 block_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
361 int lines, struct vf_priv_s *p, struct frame_stats *s)
363 struct metrics tm;
364 tm.even = tm.odd = tm.noise = tm.temp = 0;
365 get_metrics_fast_c(a, b, as, bs, lines, &tm);
366 if (sizeof(cmmx_t) < 8)
367 get_metrics_fast_c(a+4, b+4, as, bs, lines, &tm);
368 get_block_stats(&tm, p, s);
369 return tm;
372 static inline struct metrics
373 block_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
374 int lines, struct vf_priv_s *p, struct frame_stats *s)
376 struct metrics tm;
377 tm.even = tm.odd = tm.noise = tm.temp = 0;
378 get_metrics_faster_c(a, b, as, bs, lines, &tm);
379 if (sizeof(cmmx_t) < 8)
380 get_metrics_faster_c(a+4, b+4, as, bs, lines, &tm);
381 get_block_stats(&tm, p, s);
382 return tm;
385 #define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
387 #define BLOCK_METRICS_TEMPLATE() \
388 __asm__ volatile("pxor %mm7, %mm7\n\t" /* The result is colleted in mm7 */ \
389 "pxor %mm6, %mm6\n\t" /* Temp to stay at 0 */ \
390 ); \
391 a -= as; \
392 b -= bs; \
393 do { \
394 __asm__ volatile( \
395 "movq (%0,%2), %%mm0\n\t" \
396 "movq (%1,%3), %%mm1\n\t" /* mm1 = even */ \
397 PSADBW(%%mm1, %%mm0, %%mm4, %%mm6) \
398 "paddusw %%mm0, %%mm7\n\t" /* even diff */ \
399 "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */ \
400 "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */ \
401 "movq (%0), %%mm3\n\t" \
402 "psubusb %4, %%mm3\n\t" \
403 PAVGB(%%mm0, %%mm3) \
404 PAVGB(%%mm0, %%mm3) /* mm3 = qup old odd */ \
405 "movq %%mm0, %%mm5\n\t" \
406 PSADBW(%%mm2, %%mm0, %%mm4, %%mm6) \
407 "psllq $16, %%mm0\n\t" \
408 "paddusw %%mm0, %%mm7\n\t" \
409 "movq (%1), %%mm4\n\t" \
410 "lea (%0,%2,2), %0\n\t" \
411 "lea (%1,%3,2), %1\n\t" \
412 "psubusb %4, %%mm4\n\t" \
413 PAVGB(%%mm2, %%mm4) \
414 PAVGB(%%mm2, %%mm4) /* mm4 = qup odd */ \
415 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */ \
416 "movq (%1,%3), %%mm5\n\t" \
417 "psubusb %4, %%mm5\n\t" \
418 PAVGB(%%mm1, %%mm5) \
419 PAVGB(%%mm5, %%mm1) /* mm1 = qdown even */ \
420 PAVGB((%1,%3), %%mm5) /* mm5 = qup next even */ \
421 PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */ \
422 PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */ \
423 PMINUBT(%%mm2, %%mm3, %%mm0) /* limit temp to odd diff */ \
424 PMINUBT(%%mm2, %%mm4, %%mm0) /* limit noise to odd diff */ \
425 "movq (%1,%3,2), %%mm2\n\t" \
426 "psubusb %4, %%mm2\n\t" \
427 PAVGB((%1), %%mm2) \
428 PAVGB((%1), %%mm2) /* mm2 = qdown odd */ \
429 "movq (%0,%2,2), %%mm1\n\t" \
430 "psubusb %4, %%mm1\n\t" \
431 PAVGB((%0), %%mm1) \
432 PAVGB((%0), %%mm1) /* mm1 = qdown old odd */ \
433 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */ \
434 PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */ \
435 PMINUBT(%%mm4, %%mm2, %%mm0) /* current */ \
436 PMINUBT(%%mm3, %%mm1, %%mm0) /* old */ \
437 PSUMBW(%%mm2, %%mm0, %%mm6) \
438 PSUMBW(%%mm1, %%mm0, %%mm6) \
439 "psllq $32, %%mm2\n\t" \
440 "psllq $48, %%mm1\n\t" \
441 "paddusw %%mm2, %%mm7\n\t" \
442 "paddusw %%mm1, %%mm7\n\t" \
443 : "=r" (a), "=r" (b) \
444 : "r"((x86_reg)as), "r"((x86_reg)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
445 ); \
446 } while (--lines);
448 static inline struct metrics
449 block_metrics_3dnow(unsigned char *a, unsigned char *b, int as, int bs,
450 int lines, struct vf_priv_s *p, struct frame_stats *s)
452 struct metrics tm;
453 #if !HAVE_AMD3DNOW
454 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_3dnow: internal error\n");
455 #else
456 static const unsigned long long ones = 0x0101010101010101ull;
458 BLOCK_METRICS_TEMPLATE();
459 __asm__ volatile("movq %%mm7, %0\n\temms" : "=m" (tm));
460 get_block_stats(&tm, p, s);
461 #endif
462 return tm;
465 #undef PSUMBW
466 #undef PSADBW
467 #undef PMAXUB
468 #undef PMINUBT
469 #undef PAVGB
471 #define PSUMBW(X,T,Z) "psadbw " #Z "," #X "\n\t"
472 #define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
473 #define PMAXUB(X,Y) "pmaxub " #X "," #Y "\n\t"
474 #define PMINUBT(X,Y,T) "pminub " #X "," #Y "\n\t"
475 #define PAVGB(X,Y) "pavgb " #X "," #Y "\n\t"
477 static inline struct metrics
478 block_metrics_mmx2(unsigned char *a, unsigned char *b, int as, int bs,
479 int lines, struct vf_priv_s *p, struct frame_stats *s)
481 struct metrics tm;
482 #if !HAVE_MMX
483 mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_mmx2: internal error\n");
484 #else
485 static const unsigned long long ones = 0x0101010101010101ull;
486 x86_reg interlaced;
487 x86_reg prefetch_line = (((long)a>>3) & 7) + 10;
488 #ifdef DEBUG
489 struct frame_stats ts = *s;
490 #endif
491 __asm__ volatile("prefetcht0 (%0,%2)\n\t"
492 "prefetcht0 (%1,%3)\n\t" :
493 : "r" (a), "r" (b),
494 "r" (prefetch_line * as), "r" (prefetch_line * bs));
496 BLOCK_METRICS_TEMPLATE();
498 s->num_blocks++;
499 __asm__ volatile(
500 "movq %3, %%mm0\n\t"
501 "movq %%mm7, %%mm1\n\t"
502 "psubusw %%mm0, %%mm1\n\t"
503 "movq %%mm1, %%mm2\n\t"
504 "paddusw %%mm0, %%mm2\n\t"
505 "paddusw %%mm7, %%mm2\n\t"
506 "pshufw $0xb1, %%mm2, %%mm3\n\t"
507 "pavgw %%mm7, %%mm2\n\t"
508 "pshufw $0xb1, %%mm2, %%mm2\n\t"
509 "psubusw %%mm7, %%mm2\n\t"
510 "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
511 "psubusw %%mm7, %%mm3\n\t"
512 "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
513 "movq %1, %%mm4\n\t"
514 "movq %2, %%mm5\n\t"
515 "psubw %%mm2, %%mm4\n\t"
516 "psubw %%mm3, %%mm5\n\t"
517 "movq %%mm4, %1\n\t"
518 "movq %%mm5, %2\n\t"
519 "pxor %%mm4, %%mm4\n\t"
520 "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
521 "psubusw %%mm0, %%mm1\n\t"
522 "pxor %%mm5, %%mm5\n\t"
523 "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
524 "psubusw %%mm0, %%mm1\n\t"
525 "psubusw %%mm0, %%mm1\n\t"
526 "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
527 "pshufw $0xb1, %%mm2, %%mm0\n\t"
528 "por %%mm2, %%mm0\n\t" /* 1 if not close */
529 "punpckhdq %%mm0, %%mm0\n\t"
530 "movq %%mm4, %%mm2\n\t" /* tttt */
531 "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
532 "por %%mm2, %%mm0\n\t"
533 "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
534 "psrlq $16, %%mm0\n\t"
535 "psrlw $15, %%mm0\n\t"
536 "movd %%mm0, %0\n\t"
537 : "=r" (interlaced), "=m" (s->bigger), "=m" (s->twox)
538 : "m" (p->thres)
541 if (interlaced) {
542 s->interlaced_high += interlaced >> 16;
543 s->interlaced_low += interlaced;
544 } else {
545 __asm__ volatile(
546 "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
547 "psubw %%mm0, %%mm4\n\t"
548 "psubw %%mm0, %%mm5\n\t"
549 "psubw %%mm0, %%mm1\n\t"
550 "paddw %0, %%mm4\n\t"
551 "paddw %1, %%mm5\n\t"
552 "paddw %2, %%mm1\n\t"
553 "movq %%mm4, %0\n\t"
554 "movq %%mm5, %1\n\t"
555 "movq %%mm1, %2\n\t"
556 : "=m" (s->tiny), "=m" (s->low), "=m" (s->high)
559 __asm__ volatile(
560 "pshufw $0, %2, %%mm0\n\t"
561 "psubusw %%mm7, %%mm0\n\t"
562 "pcmpeqw %%mm6, %%mm0\n\t" /* 0 if below sad_thres */
563 "pand %%mm7, %%mm0\n\t"
564 "movq %%mm0, %%mm1\n\t"
565 "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
566 "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
567 "paddd %0, %%mm0\n\t"
568 "paddd %1, %%mm1\n\t"
569 "movq %%mm0, %0\n\t"
570 "movq %%mm1, %1\n\t"
571 : "=m" (s->sad.even), "=m" (s->sad.noise)
572 : "m" (p->sad_thres)
576 __asm__ volatile(
577 "movq %%mm7, (%1)\n\t"
578 PMAXUW((%0), %%mm7)
579 "movq %%mm7, (%0)\n\t"
580 "emms"
581 : : "r" (&s->max), "r" (&tm), "X" (s->max)
582 : "memory"
584 #ifdef DEBUG
585 if (1) {
586 struct metrics cm;
587 a -= 7*as;
588 b -= 7*bs;
589 cm = block_metrics_c(a, b, as, bs, 4, p, &ts);
590 if (!MEQ(tm, cm))
591 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad metrics\n");
592 if (s) {
593 # define CHECK(X) if (!MEQ(s->X, ts.X)) \
594 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
595 CHECK(tiny);
596 CHECK(low);
597 CHECK(high);
598 CHECK(sad);
599 CHECK(max);
602 #endif
603 #endif
604 return tm;
607 static inline int
608 dint_copy_line_mmx2(unsigned char *dst, unsigned char *a, long bos,
609 long cos, int ds, int ss, int w, int t)
611 #if !HAVE_MMX
612 mp_msg(MSGT_VFILTER, MSGL_FATAL, "dint_copy_line_mmx2: internal error\n");
613 return 0;
614 #else
615 unsigned long len = (w+7) >> 3;
616 int ret;
617 __asm__ volatile (
618 "pxor %%mm6, %%mm6 \n\t" /* deinterlaced pixel counter */
619 "movd %0, %%mm7 \n\t"
620 "punpcklbw %%mm7, %%mm7 \n\t"
621 "punpcklwd %%mm7, %%mm7 \n\t"
622 "punpckldq %%mm7, %%mm7 \n\t" /* mm7 = threshold */
623 : /* no output */
624 : "rm" (t)
626 do {
627 __asm__ volatile (
628 "movq (%0), %%mm0\n\t"
629 "movq (%0,%3,2), %%mm1\n\t"
630 "movq %%mm0, (%2)\n\t"
631 "pmaxub %%mm1, %%mm0\n\t"
632 "pavgb (%0), %%mm1\n\t"
633 "psubusb %%mm1, %%mm0\n\t"
634 "paddusb %%mm7, %%mm0\n\t" /* mm0 = max-avg+thr */
635 "movq (%0,%1), %%mm2\n\t"
636 "movq (%0,%5), %%mm3\n\t"
637 "movq %%mm2, %%mm4\n\t"
638 PDIFFUBT(%%mm1, %%mm2, %%mm5)
639 PDIFFUBT(%%mm1, %%mm3, %%mm5)
640 "pminub %%mm2, %%mm3\n\t"
641 "pcmpeqb %%mm3, %%mm2\n\t" /* b = min */
642 "pand %%mm2, %%mm4\n\t"
643 "pandn (%0,%5), %%mm2\n\t"
644 "por %%mm4, %%mm2\n\t"
645 "pminub %%mm0, %%mm3\n\t"
646 "pcmpeqb %%mm0, %%mm3\n\t" /* set to 1s if >= threshold */
647 "psubb %%mm3, %%mm6\n\t" /* count pixels above thr. */
648 "pand %%mm3, %%mm1 \n\t"
649 "pandn %%mm2, %%mm3 \n\t"
650 "por %%mm3, %%mm1 \n\t" /* avg if >= threshold */
651 "movq %%mm1, (%2,%4) \n\t"
652 : /* no output */
653 : "r" (a), "r" ((x86_reg)bos), "r" ((x86_reg)dst), "r" ((x86_reg)ss), "r" ((x86_reg)ds), "r" ((x86_reg)cos)
655 a += 8;
656 dst += 8;
657 } while (--len);
659 __asm__ volatile ("pxor %%mm7, %%mm7 \n\t"
660 "psadbw %%mm6, %%mm7 \n\t"
661 "movd %%mm7, %0 \n\t"
662 "emms \n\t"
663 : "=r" (ret)
665 return ret;
666 #endif
669 static inline int
670 dint_copy_line(unsigned char *dst, unsigned char *a, long bos,
671 long cos, int ds, int ss, int w, int t)
673 unsigned long len = ((unsigned long)w+sizeof(cmmx_t)-1) / sizeof(cmmx_t);
674 cmmx_t dint_count = 0;
675 cmmx_t thr;
676 t |= t << 8;
677 thr = t | (t << 16);
678 if (sizeof(cmmx_t) > 4)
679 thr |= thr << (sizeof(cmmx_t)*4);
680 do {
681 cmmx_t e = *(cmmx_t*)a;
682 cmmx_t ne = *(cmmx_t*)(a+2*ss);
683 cmmx_t o = *(cmmx_t*)(a+bos);
684 cmmx_t oo = *(cmmx_t*)(a+cos);
685 cmmx_t maxe = pmaxub(e, ne);
686 cmmx_t avge = pavgb(e, ne);
687 cmmx_t max_diff = maxe - avge + thr; /* 0<=max-avg<128, thr<128 */
688 cmmx_t diffo = pdiffub(avge, o);
689 cmmx_t diffoo = pdiffub(avge, oo);
690 cmmx_t diffcmp = pcmpgtub(diffo, diffoo);
691 cmmx_t bo = ((oo ^ o) & diffcmp) ^ o;
692 cmmx_t diffbo = ((diffoo ^ diffo) & diffcmp) ^ diffo;
693 cmmx_t above_thr = ~pcmpgtub(max_diff, diffbo);
694 cmmx_t bo_or_avg = ((avge ^ bo) & above_thr) ^ bo;
695 dint_count += above_thr & ONE_BYTES;
696 *(cmmx_t*)(dst) = e;
697 *(cmmx_t*)(dst+ds) = bo_or_avg;
698 a += sizeof(cmmx_t);
699 dst += sizeof(cmmx_t);
700 } while (--len);
701 return psumbw(dint_count);
704 static int
705 dint_copy_plane(unsigned char *d, unsigned char *a, unsigned char *b,
706 unsigned char *c, unsigned long w, unsigned long h,
707 unsigned long ds, unsigned long ss, unsigned long threshold,
708 long field, long mmx2)
710 unsigned long ret = 0;
711 long bos = b - a;
712 long cos = c - a;
713 if (field) {
714 fast_memcpy(d, b, w);
715 h--;
716 d += ds;
717 a += ss;
719 bos += ss;
720 cos += ss;
721 while (h > 2) {
722 if (threshold >= 128) {
723 fast_memcpy(d, a, w);
724 fast_memcpy(d+ds, a+bos, w);
725 } else if (mmx2 == 1) {
726 ret += dint_copy_line_mmx2(d, a, bos, cos, ds, ss, w, threshold);
727 } else
728 ret += dint_copy_line(d, a, bos, cos, ds, ss, w, threshold);
729 h -= 2;
730 d += 2*ds;
731 a += 2*ss;
733 fast_memcpy(d, a, w);
734 if (h == 2)
735 fast_memcpy(d+ds, a+bos, w);
736 return ret;
739 static void
740 copy_merge_fields(struct vf_priv_s *p, mp_image_t *dmpi,
741 unsigned char **old, unsigned char **new, unsigned long show)
743 unsigned long threshold = 256;
744 unsigned long field = p->swapped;
745 unsigned long dint_pixels = 0;
746 unsigned char **other = old;
747 if (show >= 12 || !(show & 3))
748 show >>= 2, other = new, new = old;
749 if (show <= 2) { /* Single field: de-interlace */
750 threshold = p->dint_thres;
751 field ^= show & 1;
752 old = new;
753 } else if (show == 3)
754 old = new;
755 else
756 field ^= 1;
757 dint_pixels +=dint_copy_plane(dmpi->planes[0], old[0], new[0],
758 other[0], p->w, p->h, dmpi->stride[0],
759 p->stride, threshold, field, p->mmx2);
760 if (dmpi->flags & MP_IMGFLAG_PLANAR) {
761 if (p->luma_only)
762 old = new, other = new;
763 else
764 threshold = threshold/2 + 1;
765 field ^= p->chroma_swapped;
766 dint_copy_plane(dmpi->planes[1], old[1], new[1],
767 other[1], p->cw, p->ch, dmpi->stride[1],
768 p->chroma_stride, threshold, field, p->mmx2);
769 dint_copy_plane(dmpi->planes[2], old[2], new[2],
770 other[2], p->cw, p->ch, dmpi->stride[2],
771 p->chroma_stride, threshold, field, p->mmx2);
773 if (dint_pixels > 0 && p->verbose)
774 mp_msg(MSGT_VFILTER,MSGL_INFO,"Deinterlaced %lu pixels\n",dint_pixels);
777 static void diff_planes(struct vf_priv_s *p, struct frame_stats *s,
778 unsigned char *of, unsigned char *nf,
779 int w, int h, int os, int ns, int swapped)
781 int i, y;
782 int align = -(long)nf & 7;
783 of += align;
784 nf += align;
785 w -= align;
786 if (swapped)
787 of -= os, nf -= ns;
788 i = (h*3 >> 7) & ~1;
789 of += i*os + 8;
790 nf += i*ns + 8;
791 h -= i;
792 w -= 16;
794 memset(s, 0, sizeof(*s));
796 for (y = (h-8) >> 3; y; y--) {
797 if (p->mmx2 == 1) {
798 for (i = 0; i < w; i += 8)
799 block_metrics_mmx2(of+i, nf+i, os, ns, 4, p, s);
800 } else if (p->mmx2 == 2) {
801 for (i = 0; i < w; i += 8)
802 block_metrics_3dnow(of+i, nf+i, os, ns, 4, p, s);
803 } else if (p->fast > 3) {
804 for (i = 0; i < w; i += 8)
805 block_metrics_faster_c(of+i, nf+i, os, ns, 4, p, s);
806 } else if (p->fast > 1) {
807 for (i = 0; i < w; i += 8)
808 block_metrics_fast_c(of+i, nf+i, os, ns, 4, p, s);
809 } else {
810 for (i = 0; i < w; i += 8)
811 block_metrics_c(of+i, nf+i, os, ns, 4, p, s);
813 of += 8*os;
814 nf += 8*ns;
818 #define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
820 static void diff_fields(struct vf_priv_s *p, struct frame_stats *s,
821 unsigned char **old, unsigned char **new)
823 diff_planes(p, s, old[0], new[0], p->w, p->h,
824 p->stride, p->stride, p->swapped);
825 s->sad.even = (s->sad.even * 16ul) / s->num_blocks;
826 s->sad.odd = (s->sad.odd * 16ul) / s->num_blocks;
827 s->sad.noise = (s->sad.noise * 16ul) / s->num_blocks;
828 s->sad.temp = (s->sad.temp * 16ul) / s->num_blocks;
829 if (p->verbose)
830 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu%c M:%d/%d/%d/%d - %d, "
831 "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
832 "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
833 p->inframes, p->chflag, METRICS(s->max), s->num_blocks,
834 METRICS(s->tiny), METRICS(s->low), METRICS(s->high),
835 METRICS(s->bigger), METRICS(s->twox), METRICS(s->sad),
836 s->interlaced_low, s->interlaced_high,
837 p->iosync / (double) p->in_inc);
840 static const char *parse_args(struct vf_priv_s *p, const char *args)
842 args--;
843 while (args && *++args &&
844 (sscanf(args, "io=%lu:%lu", &p->out_dec, &p->in_inc) == 2 ||
845 sscanf(args, "diff_thres=%hu", &p->thres.even ) == 1 ||
846 sscanf(args, "comb_thres=%hu", &p->thres.noise) == 1 ||
847 sscanf(args, "sad_thres=%lu", &p->sad_thres ) == 1 ||
848 sscanf(args, "dint_thres=%lu", &p->dint_thres ) == 1 ||
849 sscanf(args, "fast=%u", &p->fast ) == 1 ||
850 sscanf(args, "mmx2=%lu", &p->mmx2 ) == 1 ||
851 sscanf(args, "luma_only=%u", &p->luma_only ) == 1 ||
852 sscanf(args, "verbose=%u", &p->verbose ) == 1 ||
853 sscanf(args, "crop=%lu:%lu:%lu:%lu", &p->w,
854 &p->h, &p->crop_x, &p->crop_y) == 4))
855 args = strchr(args, '/');
856 return args;
859 static unsigned long gcd(unsigned long x, unsigned long y)
861 unsigned long t;
862 if (x > y)
863 t = x, x = y, y = t;
865 while (x) {
866 t = y % x;
867 y = x;
868 x = t;
870 return y;
873 static void init(struct vf_priv_s *p, mp_image_t *mpi)
875 unsigned long i;
876 unsigned long plane_size, chroma_plane_size;
877 unsigned char *plane;
878 unsigned long cos, los;
879 p->crop_cx = p->crop_x >> mpi->chroma_x_shift;
880 p->crop_cy = p->crop_y >> mpi->chroma_y_shift;
881 if (mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) {
882 p->stride = (mpi->w + 15) & ~15;
883 p->chroma_stride = p->stride >> mpi->chroma_x_shift;
884 } else {
885 p->stride = mpi->width;
886 p->chroma_stride = mpi->chroma_width;
888 p->cw = p->w >> mpi->chroma_x_shift;
889 p->ch = p->h >> mpi->chroma_y_shift;
890 p->nplanes = 1;
891 p->static_idx = 0;
892 p->temp_idx = 0;
893 p->old_planes = p->planes[0];
894 plane_size = mpi->h * p->stride;
895 chroma_plane_size = mpi->flags & MP_IMGFLAG_PLANAR ?
896 mpi->chroma_height * p->chroma_stride : 0;
897 p->memory_allocated =
898 malloc(NUM_STORED * (plane_size+2*chroma_plane_size) +
899 8*p->chroma_stride + 4096);
900 /* align to page boundary */
901 plane = p->memory_allocated + (-(long)p->memory_allocated & 4095);
902 memset(plane, 0, NUM_STORED * plane_size);
903 los = p->crop_x + p->crop_y * p->stride;
904 cos = p->crop_cx + p->crop_cy * p->chroma_stride;
905 for (i = 0; i != NUM_STORED; i++, plane += plane_size) {
906 p->planes[i][0] = plane;
907 p->planes[NUM_STORED + i][0] = plane + los;
909 if (mpi->flags & MP_IMGFLAG_PLANAR) {
910 p->nplanes = 3;
911 memset(plane, 0x80, NUM_STORED * 2 * chroma_plane_size);
912 for (i = 0; i != NUM_STORED; i++) {
913 p->planes[i][1] = plane;
914 p->planes[NUM_STORED + i][1] = plane + cos;
915 plane += chroma_plane_size;
916 p->planes[i][2] = plane;
917 p->planes[NUM_STORED + i][2] = plane + cos;
918 plane += chroma_plane_size;
921 p->out_dec <<= 2;
922 i = gcd(p->in_inc, p->out_dec);
923 p->in_inc /= i;
924 p->out_dec /= i;
925 p->iosync = 0;
926 p->num_fields = 3;
929 static inline double get_time(void)
931 struct timeval tv;
932 gettimeofday(&tv, 0);
933 return tv.tv_sec + tv.tv_usec * 1e-6;
936 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
938 struct vf_priv_s *p = vf->priv;
939 static unsigned char **planes, planes_idx;
941 if (mpi->type == MP_IMGTYPE_STATIC) return;
943 if (!p->planes[0][0]) init(p, mpi);
945 if (mpi->type == MP_IMGTYPE_TEMP ||
946 (mpi->type == MP_IMGTYPE_IPB && !(mpi->flags & MP_IMGFLAG_READABLE)))
947 planes_idx = NUM_STORED/2 + (++p->temp_idx % (NUM_STORED/2));
948 else
949 planes_idx = ++p->static_idx % (NUM_STORED/2);
950 planes = p->planes[planes_idx];
951 mpi->priv = p->planes[NUM_STORED + planes_idx];
952 if (mpi->priv == p->old_planes) {
953 unsigned char **old_planes =
954 p->planes[NUM_STORED + 2 + (++p->temp_idx & 1)];
955 my_memcpy_pic(old_planes[0], p->old_planes[0],
956 p->w, p->h, p->stride, p->stride);
957 if (mpi->flags & MP_IMGFLAG_PLANAR) {
958 my_memcpy_pic(old_planes[1], p->old_planes[1],
959 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
960 my_memcpy_pic(old_planes[2], p->old_planes[2],
961 p->cw, p->ch, p->chroma_stride, p->chroma_stride);
963 p->old_planes = old_planes;
964 p->num_copies++;
966 mpi->planes[0] = planes[0];
967 mpi->stride[0] = p->stride;
968 if (mpi->flags & MP_IMGFLAG_PLANAR) {
969 mpi->planes[1] = planes[1];
970 mpi->planes[2] = planes[2];
971 mpi->stride[1] = mpi->stride[2] = p->chroma_stride;
973 mpi->width = p->stride;
975 mpi->flags |= MP_IMGFLAG_DIRECT;
976 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
979 static inline long
980 cmpe(unsigned long x, unsigned long y, unsigned long err, unsigned long e)
982 long diff = x-y;
983 long unit = ((x+y+err) >> e);
984 long ret = (diff > unit) - (diff < -unit);
985 unit >>= 1;
986 return ret + (diff > unit) - (diff < -unit);
989 static unsigned long
990 find_breaks(struct vf_priv_s *p, struct frame_stats *s)
992 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
993 long notfilm = 5*p->in_inc - p->out_dec;
994 unsigned long n = s->num_blocks >> 8;
995 unsigned long sad_comb_cmp = cmpe(s->sad.temp, s->sad.noise, 512, 1);
996 unsigned long ret = 8;
998 if (cmpe(s->sad.temp, s->sad.even, 512, 1) > 0)
999 mp_msg(MSGT_VFILTER, MSGL_WARN,
1000 "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
1001 if (s->sad.temp > 1000 && s->sad.noise > 1000)
1002 return 3;
1003 if (s->interlaced_high >= 2*n && s->sad.temp > 256 && s->sad.noise > 256)
1004 return 3;
1005 if (s->high.noise > s->num_blocks/4 && s->sad.noise > 10000 &&
1006 s->sad.noise > 2*s->sad.even && s->sad.noise > 2*ps->sad.odd) {
1007 // Mid-frame scene change
1008 if (s->tiny.temp + s->interlaced_low < n ||
1009 s->low.temp + s->interlaced_high < n/4 ||
1010 s->high.temp + s->interlaced_high < n/8 ||
1011 s->sad.temp < 160)
1012 return 1;
1013 return 3;
1015 if (s->high.temp > s->num_blocks/4 && s->sad.temp > 10000 &&
1016 s->sad.temp > 2*ps->sad.odd && s->sad.temp > 2*ps->sad.even) {
1017 // Start frame scene change
1018 if (s->tiny.noise + s->interlaced_low < n ||
1019 s->low.noise + s->interlaced_high < n/4 ||
1020 s->high.noise + s->interlaced_high < n/8 ||
1021 s->sad.noise < 160)
1022 return 2;
1023 return 3;
1025 if (sad_comb_cmp == 2)
1026 return 2;
1027 if (sad_comb_cmp == -2)
1028 return 1;
1030 if (s->tiny.odd > 3*MAX(n,s->tiny.even) + s->interlaced_low)
1031 return 1;
1032 if (s->tiny.even > 3*MAX(n,s->tiny.odd)+s->interlaced_low &&
1033 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1034 return 4;
1036 if (s->sad.noise < 64 && s->sad.temp < 64 &&
1037 s->low.noise <= n/2 && s->high.noise <= n/4 &&
1038 s->low.temp <= n/2 && s->high.temp <= n/4)
1039 goto still;
1041 if (s->tiny.temp > 3*MAX(n,s->tiny.noise) + s->interlaced_low)
1042 return 2;
1043 if (s->tiny.noise > 3*MAX(n,s->tiny.temp) + s->interlaced_low)
1044 return 1;
1046 if (s->low.odd > 3*MAX(n/4,s->low.even) + s->interlaced_high)
1047 return 1;
1048 if (s->low.even > 3*MAX(n/4,s->low.odd)+s->interlaced_high &&
1049 s->sad.even > 2*s->sad.odd &&
1050 (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
1051 return 4;
1053 if (s->low.temp > 3*MAX(n/4,s->low.noise) + s->interlaced_high)
1054 return 2;
1055 if (s->low.noise > 3*MAX(n/4,s->low.temp) + s->interlaced_high)
1056 return 1;
1058 if (sad_comb_cmp == 1 && s->sad.noise < 64)
1059 return 2;
1060 if (sad_comb_cmp == -1 && s->sad.temp < 64)
1061 return 1;
1063 if (s->tiny.odd <= n || (s->tiny.noise <= n/2 && s->tiny.temp <= n/2)) {
1064 if (s->interlaced_low <= n) {
1065 if (p->num_fields == 1)
1066 goto still;
1067 if (s->tiny.even <= n || ps->tiny.noise <= n/2)
1068 /* Still frame */
1069 goto still;
1070 if (s->bigger.even >= 2*MAX(n,s->bigger.odd) + s->interlaced_low)
1071 return 4;
1072 if (s->low.even >= 2*n + s->interlaced_low)
1073 return 4;
1074 goto still;
1077 if (s->low.odd <= n/4) {
1078 if (s->interlaced_high <= n/4) {
1079 if (p->num_fields == 1)
1080 goto still;
1081 if (s->low.even <= n/4)
1082 /* Still frame */
1083 goto still;
1084 if (s->bigger.even >= 2*MAX(n/4,s->bigger.odd)+s->interlaced_high)
1085 return 4;
1086 if (s->low.even >= n/2 + s->interlaced_high)
1087 return 4;
1088 goto still;
1091 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_low)
1092 return 2;
1093 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_low)
1094 return 1;
1095 if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_high)
1096 return 2;
1097 if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_high)
1098 return 1;
1099 if (s->twox.temp > 2*MAX(n,s->twox.noise) + s->interlaced_high)
1100 return 2;
1101 if (s->twox.noise > 2*MAX(n,s->twox.temp) + s->interlaced_high)
1102 return 1;
1103 if (s->bigger.even > 2*MAX(n,s->bigger.odd) + s->interlaced_low &&
1104 s->bigger.temp < n && s->bigger.noise < n)
1105 return 4;
1106 if (s->interlaced_low > MIN(2*n, s->tiny.odd))
1107 return 3;
1108 ret = 8 + (1 << (s->sad.temp > s->sad.noise));
1109 still:
1110 if (p->num_fields == 1 && p->prev_fields == 3 && notfilm >= 0 &&
1111 (s->tiny.temp <= s->tiny.noise || s->sad.temp < s->sad.noise+16))
1112 return 1;
1113 if (p->notout < p->num_fields && p->iosync > 2*p->in_inc && notfilm < 0)
1114 notfilm = 0;
1115 if (p->num_fields < 2 ||
1116 (p->num_fields == 2 && p->prev_fields == 2 && notfilm < 0))
1117 return ret;
1118 if (!notfilm && (p->prev_fields&~1) == 2) {
1119 if (p->prev_fields + p->num_fields == 5) {
1120 if (s->tiny.noise <= s->tiny.temp ||
1121 s->low.noise == 0 || s->low.noise < s->low.temp ||
1122 s->sad.noise < s->sad.temp+16)
1123 return 2;
1125 if (p->prev_fields + p->num_fields == 4) {
1126 if (s->tiny.temp <= s->tiny.noise ||
1127 s->low.temp == 0 || s->low.temp < s->low.noise ||
1128 s->sad.temp < s->sad.noise+16)
1129 return 1;
1132 if (p->num_fields > 2 &&
1133 ps->sad.noise > s->sad.noise && ps->sad.noise > s->sad.temp)
1134 return 4;
1135 return 2 >> (s->sad.noise > s->sad.temp);
1138 #define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
1140 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
1142 mp_image_t *dmpi;
1143 struct vf_priv_s *p = vf->priv;
1144 unsigned char **planes, **old_planes;
1145 struct frame_stats *s = &p->stats[p->inframes & 1];
1146 struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
1147 int swapped = 0;
1148 const int flags = mpi->fields;
1149 int breaks, prev;
1150 int show_fields = 0;
1151 int dropped_fields = 0;
1152 double start_time, diff_time;
1153 char prev_chflag = p->chflag;
1154 int keep_rate;
1156 if (!p->planes[0][0]) init(p, mpi);
1158 old_planes = p->old_planes;
1160 if ((mpi->flags & MP_IMGFLAG_DIRECT) && mpi->priv) {
1161 planes = mpi->priv;
1162 mpi->priv = 0;
1163 } else {
1164 planes = p->planes[2 + (++p->temp_idx & 1)];
1165 my_memcpy_pic(planes[0],
1166 mpi->planes[0] + p->crop_x + p->crop_y * mpi->stride[0],
1167 p->w, p->h, p->stride, mpi->stride[0]);
1168 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1169 my_memcpy_pic(planes[1],
1170 mpi->planes[1] + p->crop_cx + p->crop_cy * mpi->stride[1],
1171 p->cw, p->ch, p->chroma_stride, mpi->stride[1]);
1172 my_memcpy_pic(planes[2],
1173 mpi->planes[2] + p->crop_cx + p->crop_cy * mpi->stride[2],
1174 p->cw, p->ch, p->chroma_stride, mpi->stride[2]);
1175 p->num_copies++;
1179 p->old_planes = planes;
1180 p->chflag = ';';
1181 if (flags & MP_IMGFIELD_ORDERED) {
1182 swapped = !(flags & MP_IMGFIELD_TOP_FIRST);
1183 p->chflag = (flags & MP_IMGFIELD_REPEAT_FIRST ? '|' :
1184 flags & MP_IMGFIELD_TOP_FIRST ? ':' : '.');
1186 p->swapped = swapped;
1188 start_time = get_time();
1189 if (p->chflag == '|') {
1190 *s = ppzs;
1191 p->iosync += p->in_inc;
1192 } else if ((p->fast & 1) && prev_chflag == '|')
1193 *s = pprs;
1194 else
1195 diff_fields(p, s, old_planes, planes);
1196 diff_time = get_time();
1197 p->diff_time += diff_time - start_time;
1198 breaks = p->inframes ? find_breaks(p, s) : 2;
1199 p->inframes++;
1200 keep_rate = 4*p->in_inc == p->out_dec;
1202 switch (breaks) {
1203 case 0:
1204 case 8:
1205 case 9:
1206 case 10:
1207 if (!keep_rate && p->notout < p->num_fields && p->iosync < 2*p->in_inc)
1208 break;
1209 if (p->notout < p->num_fields)
1210 dropped_fields = -2;
1211 case 4:
1212 if (keep_rate || p->iosync >= -2*p->in_inc)
1213 show_fields = (4<<p->num_fields)-1;
1214 break;
1215 case 3:
1216 if (keep_rate)
1217 show_fields = 2;
1218 else if (p->iosync > 0) {
1219 if (p->notout >= p->num_fields && p->iosync > 2*p->in_inc) {
1220 show_fields = 4; /* prev odd only */
1221 if (p->num_fields > 1)
1222 show_fields |= 8; /* + prev even */
1223 } else {
1224 show_fields = 2; /* even only */
1225 if (p->notout >= p->num_fields)
1226 dropped_fields += p->num_fields;
1229 break;
1230 case 2:
1231 if (p->iosync <= -3*p->in_inc) {
1232 if (p->notout >= p->num_fields)
1233 dropped_fields = p->num_fields;
1234 break;
1236 if (p->num_fields == 1) {
1237 int prevbreak = ps->sad.noise >= 128;
1238 if (p->iosync < 4*p->in_inc) {
1239 show_fields = 3;
1240 dropped_fields = prevbreak;
1241 } else {
1242 show_fields = 4 | (!prevbreak << 3);
1243 if (p->notout < 1 + p->prev_fields)
1244 dropped_fields = -!prevbreak;
1246 break;
1248 default:
1249 if (keep_rate)
1250 show_fields = 3 << (breaks & 1);
1251 else if (p->notout >= p->num_fields &&
1252 p->iosync >= (breaks == 1 ? -p->in_inc :
1253 p->in_inc << (p->num_fields == 1))) {
1254 show_fields = (1 << (2 + p->num_fields)) - (1<<breaks);
1255 } else {
1256 if (p->notout >= p->num_fields)
1257 dropped_fields += p->num_fields + 2 - breaks;
1258 if (breaks == 1) {
1259 if (p->iosync >= 4*p->in_inc)
1260 show_fields = 6;
1261 } else if (p->iosync > -3*p->in_inc)
1262 show_fields = 3; /* odd+even */
1264 break;
1267 show_fields &= 15;
1268 prev = p->prev_fields;
1269 if (breaks < 8) {
1270 if (p->num_fields == 1)
1271 breaks &= ~4;
1272 if (breaks)
1273 p->num_breaks++;
1274 if (breaks == 3)
1275 p->prev_fields = p->num_fields = 1;
1276 else if (breaks) {
1277 p->prev_fields = p->num_fields + (breaks==1) - (breaks==4);
1278 p->num_fields = breaks - (breaks == 4) + (p->chflag == '|');
1279 } else
1280 p->num_fields += 2;
1281 } else
1282 p->num_fields += 2;
1284 p->iosync += 4 * p->in_inc;
1285 if (p->chflag == '|')
1286 p->iosync += p->in_inc;
1288 if (show_fields) {
1289 p->iosync -= p->out_dec;
1290 p->notout = !(show_fields & 1) + !(show_fields & 3);
1291 if (((show_fields & 3) == 3 &&
1292 (s->low.noise + s->interlaced_low < (s->num_blocks>>8) ||
1293 s->sad.noise < 160)) ||
1294 ((show_fields & 12) == 12 &&
1295 (ps->low.noise + ps->interlaced_low < (s->num_blocks>>8) ||
1296 ps->sad.noise < 160))) {
1297 p->export_count++;
1298 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT,
1299 MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE,
1300 p->w, p->h);
1301 if ((show_fields & 3) != 3) planes = old_planes;
1302 dmpi->planes[0] = planes[0];
1303 dmpi->stride[0] = p->stride;
1304 dmpi->width = mpi->width;
1305 if (mpi->flags & MP_IMGFLAG_PLANAR) {
1306 dmpi->planes[1] = planes[1];
1307 dmpi->planes[2] = planes[2];
1308 dmpi->stride[1] = p->chroma_stride;
1309 dmpi->stride[2] = p->chroma_stride;
1311 } else {
1312 p->merge_count++;
1313 dmpi = vf_get_image(vf->next, mpi->imgfmt,
1314 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
1315 p->w, p->h);
1316 copy_merge_fields(p, dmpi, old_planes, planes, show_fields);
1318 p->outframes++;
1319 } else
1320 p->notout += 2;
1322 if (p->verbose)
1323 mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
1324 p->inframes, p->outframes,
1325 breaks, breaks<8 && breaks>0 ? (int) p->prev_fields+'0' : ' ',
1326 ITOC(show_fields),
1327 p->num_breaks, 5*p->in_inc == p->out_dec && breaks<8 &&
1328 breaks>0 && ((prev&~1)!=2 || prev+p->prev_fields!=5) ?
1329 " ######## bad telecine ########" : "",
1330 dropped_fields ? " ======== dropped ":"", ITOC(dropped_fields),
1331 !show_fields || (show_fields & (show_fields-1)) ?
1332 "" : " @@@@@@@@@@@@@@@@@");
1334 p->merge_time += get_time() - diff_time;
1335 pts = vf_detc_adjust_pts(&p->ptsbuf, pts, 0, !show_fields);
1336 return show_fields ? vf_next_put_image(vf, dmpi, pts) : 0;
1339 static int query_format(struct vf_instance *vf, unsigned int fmt)
1341 /* FIXME - support more formats */
1342 switch (fmt) {
1343 case IMGFMT_YV12:
1344 case IMGFMT_IYUV:
1345 case IMGFMT_I420:
1346 case IMGFMT_411P:
1347 case IMGFMT_422P:
1348 case IMGFMT_444P:
1349 return vf_next_query_format(vf, fmt);
1351 return 0;
1354 static int config(struct vf_instance *vf,
1355 int width, int height, int d_width, int d_height,
1356 unsigned int flags, unsigned int outfmt)
1358 struct MPOpts *opts = vf->opts;
1359 unsigned long cxm = 0;
1360 unsigned long cym = 0;
1361 struct vf_priv_s *p = vf->priv;
1362 vf_detc_init_pts_buf(&p->ptsbuf);
1363 // rounding:
1364 if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
1365 switch(outfmt){
1366 case IMGFMT_444P:
1367 case IMGFMT_Y800:
1368 case IMGFMT_Y8:
1369 break;
1370 case IMGFMT_YVU9:
1371 case IMGFMT_IF09:
1372 cym = 3;
1373 case IMGFMT_411P:
1374 cxm = 3;
1375 break;
1376 case IMGFMT_YV12:
1377 case IMGFMT_I420:
1378 case IMGFMT_IYUV:
1379 cym = 1;
1380 default:
1381 cxm = 1;
1384 p->chroma_swapped = !!(p->crop_y & (cym+1));
1385 if (p->w) p->w += p->crop_x & cxm;
1386 if (p->h) p->h += p->crop_y & cym;
1387 p->crop_x &= ~cxm;
1388 p->crop_y &= ~cym;
1389 if (!p->w || p->w > width ) p->w = width;
1390 if (!p->h || p->h > height) p->h = height;
1391 if (p->crop_x + p->w > width ) p->crop_x = 0;
1392 if (p->crop_y + p->h > height) p->crop_y = 0;
1394 if(!opts->screen_size_x && !opts->screen_size_y){
1395 d_width = d_width * p->w/width;
1396 d_height = d_height * p->h/height;
1398 return vf_next_config(vf, p->w, p->h, d_width, d_height, flags, outfmt);
1401 static void uninit(struct vf_instance *vf)
1403 struct vf_priv_s *p = vf->priv;
1404 mp_msg(MSGT_VFILTER, MSGL_INFO, "diff_time: %.3f, merge_time: %.3f, "
1405 "export: %lu, merge: %lu, copy: %lu\n", p->diff_time, p->merge_time,
1406 p->export_count, p->merge_count, p->num_copies);
1407 free(p->memory_allocated);
1408 free(p);
1411 static int vf_open(vf_instance_t *vf, char *args)
1413 struct vf_priv_s *p;
1414 vf->get_image = get_image;
1415 vf->put_image = put_image;
1416 vf->config = config;
1417 vf->query_format = query_format;
1418 vf->uninit = uninit;
1419 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
1420 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
1421 p->out_dec = 5;
1422 p->in_inc = 4;
1423 p->thres.noise = 128;
1424 p->thres.even = 128;
1425 p->sad_thres = 64;
1426 p->dint_thres = 4;
1427 p->luma_only = 0;
1428 p->fast = 3;
1429 p->mmx2 = gCpuCaps.hasMMX2 ? 1 : gCpuCaps.has3DNow ? 2 : 0;
1430 if (args) {
1431 const char *args_remain = parse_args(p, args);
1432 if (args_remain) {
1433 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1434 "filmdint: unknown suboption: %s\n", args_remain);
1435 return 0;
1437 if (p->out_dec < p->in_inc) {
1438 mp_msg(MSGT_VFILTER, MSGL_FATAL,
1439 "filmdint: increasing the frame rate is not supported\n");
1440 return 0;
1443 if (p->mmx2 > 2)
1444 p->mmx2 = 0;
1445 #if !HAVE_MMX
1446 p->mmx2 = 0;
1447 #endif
1448 #if !HAVE_AMD3DNOW
1449 p->mmx2 &= 1;
1450 #endif
1451 p->thres.odd = p->thres.even;
1452 p->thres.temp = p->thres.noise;
1453 p->diff_time = 0;
1454 p->merge_time = 0;
1455 return 1;
1458 const vf_info_t vf_info_filmdint = {
1459 "Advanced inverse telecine filer",
1460 "filmdint",
1461 "Zoltan Hidvegi",
1463 vf_open,
1464 NULL