10 #include "img_format.h"
15 #include "libvo/fastmemcpy.h"
19 enum pu_field_type_t
{
29 /* This struct maps to a packed word 64-bit MMX register */
30 unsigned short int even
;
31 unsigned short int odd
;
32 unsigned short int noise
;
33 unsigned short int temp
;
34 } __attribute__ ((aligned (8)));
37 struct metrics tiny
, low
, high
, bigger
, twox
, max
;
38 struct { unsigned int even
, odd
, noise
, temp
; } sad
;
39 unsigned short interlaced_high
;
40 unsigned short interlaced_low
;
41 unsigned short num_blocks
;
45 unsigned long inframes
;
46 unsigned long outframes
;
47 enum pu_field_type_t prev_type
;
48 unsigned swapped
, chroma_swapped
;
52 unsigned long w
, h
, cw
, ch
, stride
, chroma_stride
, nplanes
;
53 unsigned long sad_thres
;
54 unsigned long dint_thres
;
55 unsigned char *memory_allocated
;
56 unsigned char *planes
[2*NUM_STORED
][4];
57 unsigned char **old_planes
;
58 unsigned long static_idx
;
59 unsigned long temp_idx
;
60 unsigned long crop_x
, crop_y
, crop_cx
, crop_cy
;
61 unsigned long export_count
, merge_count
;
62 unsigned long num_breaks
;
63 unsigned long num_copies
;
64 long in_inc
, out_dec
, iosync
;
69 unsigned small_bytes
[2];
71 struct frame_stats stats
[2];
74 double diff_time
, merge_time
, decode_time
, vo_time
, filter_time
;
77 #define PPZ { 2000, 2000, 0, 2000 }
78 #define PPR { 2000, 2000, 0, 2000 }
79 static const struct frame_stats ppzs
= {PPZ
,PPZ
,PPZ
,PPZ
,PPZ
,PPZ
,PPZ
,0,0,9999};
80 static const struct frame_stats pprs
= {PPR
,PPR
,PPR
,PPR
,PPR
,PPR
,PPR
,0,0,9999};
82 extern int opt_screen_size_x
;
83 extern int opt_screen_size_y
;
86 #define MIN(a,b) (((a)<(b))?(a):(b))
89 #define MAX(a,b) (((a)>(b))?(a):(b))
92 #define PDIFFUB(X,Y,T) "movq " #X "," #T "\n\t" \
93 "psubusb " #Y "," #T "\n\t" \
94 "psubusb " #X "," #Y "\n\t" \
95 "paddusb " #Y "," #T "\n\t"
97 #define PDIFFUBT(X,Y,T) "movq " #X "," #T "\n\t" \
98 "psubusb " #Y "," #T "\n\t" \
99 "psubusb " #X "," #Y "\n\t" \
100 "paddusb " #T "," #Y "\n\t"
102 #define PSUMBW(X,T,Z) "movq " #X "," #T "\n\t" \
103 "punpcklbw " #Z "," #X "\n\t" \
104 "punpckhbw " #Z "," #T "\n\t" \
105 "paddw " #T "," #X "\n\t" \
106 "movq " #X "," #T "\n\t" \
107 "psllq $32, " #T "\n\t" \
108 "paddw " #T "," #X "\n\t" \
109 "movq " #X "," #T "\n\t" \
110 "psllq $16, " #T "\n\t" \
111 "paddw " #T "," #X "\n\t" \
112 "psrlq $48, " #X "\n\t"
114 #define PSADBW(X,Y,T,Z) PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
116 #define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
117 #define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
118 #define PMINUBT(X,Y,T) "movq " #Y "," #T "\n\t" \
119 "psubusb " #X "," #T "\n\t" \
120 "psubusb " #T "," #Y "\n\t"
121 #define PAVGB(X,Y) "pavgusb " #X "," #Y "\n\t"
124 get_metrics_c(unsigned char *a
, unsigned char *b
, int as
, int bs
, int lines
,
130 cmmx_t old_po
= *(cmmx_t
*)(a
);
131 cmmx_t po
= *(cmmx_t
*)(b
);
132 cmmx_t e
= *(cmmx_t
*)(b
+ bs
);
133 cmmx_t old_o
= *(cmmx_t
*)(a
+ 2*as
);
134 cmmx_t o
= *(cmmx_t
*)(b
+ 2*bs
);
135 cmmx_t ne
= *(cmmx_t
*)(b
+ 3*bs
);
136 cmmx_t old_no
= *(cmmx_t
*)(a
+ 4*as
);
137 cmmx_t no
= *(cmmx_t
*)(b
+ 4*bs
);
139 cmmx_t qup_old_odd
= p31avgb(old_o
, old_po
);
140 cmmx_t qup_odd
= p31avgb( o
, po
);
141 cmmx_t qdown_old_odd
= p31avgb(old_o
, old_no
);
142 cmmx_t qdown_odd
= p31avgb( o
, no
);
144 cmmx_t qup_even
= p31avgb(ne
, e
);
145 cmmx_t qdown_even
= p31avgb(e
, ne
);
147 cmmx_t temp_up_diff
= pdiffub(qdown_even
, qup_old_odd
);
148 cmmx_t noise_up_diff
= pdiffub(qdown_even
, qup_odd
);
149 cmmx_t temp_down_diff
= pdiffub(qup_even
, qdown_old_odd
);
150 cmmx_t noise_down_diff
= pdiffub(qup_even
, qdown_odd
);
152 cmmx_t odd_diff
= pdiffub(o
, old_o
);
153 m
->odd
+= psumbw(odd_diff
);
154 m
->even
+= psadbw(e
, *(cmmx_t
*)(a
+as
));
156 temp_up_diff
= pminub(temp_up_diff
, temp_down_diff
);
157 temp_up_diff
= pminub(temp_up_diff
, odd_diff
);
158 m
->temp
+= psumbw(temp_up_diff
);
159 noise_up_diff
= pminub(noise_up_diff
, odd_diff
);
160 noise_up_diff
= pminub(noise_up_diff
, noise_down_diff
);
162 m
->noise
+= psumbw(noise_up_diff
);
169 get_metrics_fast_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
170 int lines
, struct metrics
*m
)
175 cmmx_t old_po
= (*(cmmx_t
*)(a
) >> 1) & ~SIGN_BITS
;
176 cmmx_t po
= (*(cmmx_t
*)(b
) >> 1) & ~SIGN_BITS
;
177 cmmx_t old_e
= (*(cmmx_t
*)(a
+ as
) >> 1) & ~SIGN_BITS
;
178 cmmx_t e
= (*(cmmx_t
*)(b
+ bs
) >> 1) & ~SIGN_BITS
;
179 cmmx_t old_o
= (*(cmmx_t
*)(a
+ 2*as
) >> 1) & ~SIGN_BITS
;
180 cmmx_t o
= (*(cmmx_t
*)(b
+ 2*bs
) >> 1) & ~SIGN_BITS
;
181 cmmx_t ne
= (*(cmmx_t
*)(b
+ 3*bs
) >> 1) & ~SIGN_BITS
;
182 cmmx_t old_no
= (*(cmmx_t
*)(a
+ 4*as
) >> 1) & ~SIGN_BITS
;
183 cmmx_t no
= (*(cmmx_t
*)(b
+ 4*bs
) >> 1) & ~SIGN_BITS
;
185 cmmx_t qup_old_odd
= p31avgb_s(old_o
, old_po
);
186 cmmx_t qup_odd
= p31avgb_s( o
, po
);
187 cmmx_t qdown_old_odd
= p31avgb_s(old_o
, old_no
);
188 cmmx_t qdown_odd
= p31avgb_s( o
, no
);
190 cmmx_t qup_even
= p31avgb_s(ne
, e
);
191 cmmx_t qdown_even
= p31avgb_s(e
, ne
);
193 cmmx_t temp_up_diff
= pdiffub_s(qdown_even
, qup_old_odd
);
194 cmmx_t noise_up_diff
= pdiffub_s(qdown_even
, qup_odd
);
195 cmmx_t temp_down_diff
= pdiffub_s(qup_even
, qdown_old_odd
);
196 cmmx_t noise_down_diff
= pdiffub_s(qup_even
, qdown_odd
);
198 cmmx_t odd_diff
= pdiffub_s(o
, old_o
);
199 m
->odd
+= psumbw_s(odd_diff
) << 1;
200 m
->even
+= psadbw_s(e
, old_e
) << 1;
202 temp_up_diff
= pminub_s(temp_up_diff
, temp_down_diff
);
203 temp_up_diff
= pminub_s(temp_up_diff
, odd_diff
);
204 m
->temp
+= psumbw_s(temp_up_diff
) << 1;
205 noise_up_diff
= pminub_s(noise_up_diff
, odd_diff
);
206 noise_up_diff
= pminub_s(noise_up_diff
, noise_down_diff
);
208 m
->noise
+= psumbw_s(noise_up_diff
) << 1;
215 get_metrics_faster_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
216 int lines
, struct metrics
*m
)
221 cmmx_t old_po
= (*(cmmx_t
*)(a
)>>1) & ~SIGN_BITS
;
222 cmmx_t po
= (*(cmmx_t
*)(b
)>>1) & ~SIGN_BITS
;
223 cmmx_t old_e
= (*(cmmx_t
*)(a
+ as
)>>1) & ~SIGN_BITS
;
224 cmmx_t e
= (*(cmmx_t
*)(b
+ bs
)>>1) & ~SIGN_BITS
;
225 cmmx_t old_o
= (*(cmmx_t
*)(a
+ 2*as
)>>1) & ~SIGN_BITS
;
226 cmmx_t o
= (*(cmmx_t
*)(b
+ 2*bs
)>>1) & ~SIGN_BITS
;
227 cmmx_t ne
= (*(cmmx_t
*)(b
+ 3*bs
)>>1) & ~SIGN_BITS
;
229 cmmx_t down_even
= p31avgb_s(e
, ne
);
230 cmmx_t up_odd
= p31avgb_s(o
, po
);
231 cmmx_t up_old_odd
= p31avgb_s(old_o
, old_po
);
233 cmmx_t odd_diff
= pdiffub_s(o
, old_o
);
234 cmmx_t temp_diff
= pdiffub_s(down_even
, up_old_odd
);
235 cmmx_t noise_diff
= pdiffub_s(down_even
, up_odd
);
237 m
->even
+= psadbw_s(e
, old_e
) << 1;
238 m
->odd
+= psumbw_s(odd_diff
) << 1;
240 temp_diff
= pminub_s(temp_diff
, odd_diff
);
241 noise_diff
= pminub_s(noise_diff
, odd_diff
);
243 m
->noise
+= psumbw_s(noise_diff
) << 1;
244 m
->temp
+= psumbw_s(temp_diff
) << 1;
252 get_block_stats(struct metrics
*m
, struct vf_priv_s
*p
, struct frame_stats
*s
)
254 unsigned two_e
= m
->even
+ MAX(m
->even
, p
->thres
.even
);
255 unsigned two_o
= m
->odd
+ MAX(m
->odd
, p
->thres
.odd
);
256 unsigned two_n
= m
->noise
+ MAX(m
->noise
, p
->thres
.noise
);
257 unsigned two_t
= m
->temp
+ MAX(m
->temp
, p
->thres
.temp
);
259 unsigned e_big
= m
->even
>= (m
->odd
+ two_o
+ 1)/2;
260 unsigned o_big
= m
->odd
>= (m
->even
+ two_e
+ 1)/2;
261 unsigned n_big
= m
->noise
>= (m
->temp
+ two_t
+ 1)/2;
262 unsigned t_big
= m
->temp
>= (m
->noise
+ two_n
+ 1)/2;
264 unsigned e2x
= m
->even
>= two_o
;
265 unsigned o2x
= m
->odd
>= two_e
;
266 unsigned n2x
= m
->noise
>= two_t
;
267 unsigned t2x
= m
->temp
>= two_n
;
269 unsigned ntiny_e
= m
->even
> p
->thres
.even
;
270 unsigned ntiny_o
= m
->odd
> p
->thres
.odd
;
271 unsigned ntiny_n
= m
->noise
> p
->thres
.noise
;
272 unsigned ntiny_t
= m
->temp
> p
->thres
.temp
;
274 unsigned nlow_e
= m
->even
> 2*p
->thres
.even
;
275 unsigned nlow_o
= m
->odd
> 2*p
->thres
.odd
;
276 unsigned nlow_n
= m
->noise
> 2*p
->thres
.noise
;
277 unsigned nlow_t
= m
->temp
> 2*p
->thres
.temp
;
279 unsigned high_e
= m
->even
> 4*p
->thres
.even
;
280 unsigned high_o
= m
->odd
> 4*p
->thres
.odd
;
281 unsigned high_n
= m
->noise
> 4*p
->thres
.noise
;
282 unsigned high_t
= m
->temp
> 4*p
->thres
.temp
;
284 unsigned low_il
= !n_big
&& !t_big
&& ntiny_n
&& ntiny_t
;
285 unsigned high_il
= !n_big
&& !t_big
&& nlow_n
&& nlow_t
;
287 if (low_il
| high_il
) {
288 s
->interlaced_low
+= low_il
;
289 s
->interlaced_high
+= high_il
;
291 s
->tiny
.even
+= ntiny_e
;
292 s
->tiny
.odd
+= ntiny_o
;
293 s
->tiny
.noise
+= ntiny_n
;
294 s
->tiny
.temp
+= ntiny_t
;
296 s
->low
.even
+= nlow_e
;
297 s
->low
.odd
+= nlow_o
;
298 s
->low
.noise
+= nlow_n
;
299 s
->low
.temp
+= nlow_t
;
301 s
->high
.even
+= high_e
;
302 s
->high
.odd
+= high_o
;
303 s
->high
.noise
+= high_n
;
304 s
->high
.temp
+= high_t
;
306 if (m
->even
>= p
->sad_thres
) s
->sad
.even
+= m
->even
;
307 if (m
->odd
>= p
->sad_thres
) s
->sad
.odd
+= m
->odd
;
308 if (m
->noise
>= p
->sad_thres
) s
->sad
.noise
+= m
->noise
;
309 if (m
->temp
>= p
->sad_thres
) s
->sad
.temp
+= m
->temp
;
312 s
->max
.even
= MAX(s
->max
.even
, m
->even
);
313 s
->max
.odd
= MAX(s
->max
.odd
, m
->odd
);
314 s
->max
.noise
= MAX(s
->max
.noise
, m
->noise
);
315 s
->max
.temp
= MAX(s
->max
.temp
, m
->temp
);
317 s
->bigger
.even
+= e_big
;
318 s
->bigger
.odd
+= o_big
;
319 s
->bigger
.noise
+= n_big
;
320 s
->bigger
.temp
+= t_big
;
322 s
->twox
.even
+= e2x
;
324 s
->twox
.noise
+= n2x
;
325 s
->twox
.temp
+= t2x
;
329 static inline struct metrics
330 block_metrics_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
331 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
334 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
335 get_metrics_c(a
, b
, as
, bs
, lines
, &tm
);
336 if (sizeof(cmmx_t
) < 8)
337 get_metrics_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
338 get_block_stats(&tm
, p
, s
);
342 static inline struct metrics
343 block_metrics_fast_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
344 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
347 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
348 get_metrics_fast_c(a
, b
, as
, bs
, lines
, &tm
);
349 if (sizeof(cmmx_t
) < 8)
350 get_metrics_fast_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
351 get_block_stats(&tm
, p
, s
);
355 static inline struct metrics
356 block_metrics_faster_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
357 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
360 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
361 get_metrics_faster_c(a
, b
, as
, bs
, lines
, &tm
);
362 if (sizeof(cmmx_t
) < 8)
363 get_metrics_faster_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
364 get_block_stats(&tm
, p
, s
);
368 #define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
370 #define BLOCK_METRICS_TEMPLATE() \
371 __asm__ volatile("pxor %mm7, %mm7\n\t" /* The result is colleted in mm7 */ \
372 "pxor %mm6, %mm6\n\t" /* Temp to stay at 0 */ \
378 "movq (%0,%2), %%mm0\n\t" \
379 "movq (%1,%3), %%mm1\n\t" /* mm1 = even */ \
380 PSADBW(%%mm1, %%mm0, %%mm4, %%mm6) \
381 "paddusw %%mm0, %%mm7\n\t" /* even diff */ \
382 "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */ \
383 "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */ \
384 "movq (%0), %%mm3\n\t" \
385 "psubusb %4, %%mm3\n\t" \
386 PAVGB(%%mm0, %%mm3) \
387 PAVGB(%%mm0, %%mm3) /* mm3 = qup old odd */ \
388 "movq %%mm0, %%mm5\n\t" \
389 PSADBW(%%mm2, %%mm0, %%mm4, %%mm6) \
390 "psllq $16, %%mm0\n\t" \
391 "paddusw %%mm0, %%mm7\n\t" \
392 "movq (%1), %%mm4\n\t" \
393 "lea (%0,%2,2), %0\n\t" \
394 "lea (%1,%3,2), %1\n\t" \
395 "psubusb %4, %%mm4\n\t" \
396 PAVGB(%%mm2, %%mm4) \
397 PAVGB(%%mm2, %%mm4) /* mm4 = qup odd */ \
398 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */ \
399 "movq (%1,%3), %%mm5\n\t" \
400 "psubusb %4, %%mm5\n\t" \
401 PAVGB(%%mm1, %%mm5) \
402 PAVGB(%%mm5, %%mm1) /* mm1 = qdown even */ \
403 PAVGB((%1,%3), %%mm5) /* mm5 = qup next even */ \
404 PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */ \
405 PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */ \
406 PMINUBT(%%mm2, %%mm3, %%mm0) /* limit temp to odd diff */ \
407 PMINUBT(%%mm2, %%mm4, %%mm0) /* limit noise to odd diff */ \
408 "movq (%1,%3,2), %%mm2\n\t" \
409 "psubusb %4, %%mm2\n\t" \
411 PAVGB((%1), %%mm2) /* mm2 = qdown odd */ \
412 "movq (%0,%2,2), %%mm1\n\t" \
413 "psubusb %4, %%mm1\n\t" \
415 PAVGB((%0), %%mm1) /* mm1 = qdown old odd */ \
416 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */ \
417 PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */ \
418 PMINUBT(%%mm4, %%mm2, %%mm0) /* current */ \
419 PMINUBT(%%mm3, %%mm1, %%mm0) /* old */ \
420 PSUMBW(%%mm2, %%mm0, %%mm6) \
421 PSUMBW(%%mm1, %%mm0, %%mm6) \
422 "psllq $32, %%mm2\n\t" \
423 "psllq $48, %%mm1\n\t" \
424 "paddusw %%mm2, %%mm7\n\t" \
425 "paddusw %%mm1, %%mm7\n\t" \
426 : "=r" (a), "=r" (b) \
427 : "r"((x86_reg)as), "r"((x86_reg)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
431 static inline struct metrics
432 block_metrics_3dnow(unsigned char *a
, unsigned char *b
, int as
, int bs
,
433 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
437 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "block_metrics_3dnow: internal error\n");
439 static const unsigned long long ones
= 0x0101010101010101ull
;
441 BLOCK_METRICS_TEMPLATE();
442 __asm__
volatile("movq %%mm7, %0\n\temms" : "=m" (tm
));
443 get_block_stats(&tm
, p
, s
);
454 #define PSUMBW(X,T,Z) "psadbw " #Z "," #X "\n\t"
455 #define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
456 #define PMAXUB(X,Y) "pmaxub " #X "," #Y "\n\t"
457 #define PMINUBT(X,Y,T) "pminub " #X "," #Y "\n\t"
458 #define PAVGB(X,Y) "pavgb " #X "," #Y "\n\t"
460 static inline struct metrics
461 block_metrics_mmx2(unsigned char *a
, unsigned char *b
, int as
, int bs
,
462 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
466 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "block_metrics_mmx2: internal error\n");
468 static const unsigned long long ones
= 0x0101010101010101ull
;
470 x86_reg prefetch_line
= (((long)a
>>3) & 7) + 10;
472 struct frame_stats ts
= *s
;
474 __asm__
volatile("prefetcht0 (%0,%2)\n\t"
475 "prefetcht0 (%1,%3)\n\t" :
477 "r" (prefetch_line
* as
), "r" (prefetch_line
* bs
));
479 BLOCK_METRICS_TEMPLATE();
484 "movq %%mm7, %%mm1\n\t"
485 "psubusw %%mm0, %%mm1\n\t"
486 "movq %%mm1, %%mm2\n\t"
487 "paddusw %%mm0, %%mm2\n\t"
488 "paddusw %%mm7, %%mm2\n\t"
489 "pshufw $0xb1, %%mm2, %%mm3\n\t"
490 "pavgw %%mm7, %%mm2\n\t"
491 "pshufw $0xb1, %%mm2, %%mm2\n\t"
492 "psubusw %%mm7, %%mm2\n\t"
493 "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
494 "psubusw %%mm7, %%mm3\n\t"
495 "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
498 "psubw %%mm2, %%mm4\n\t"
499 "psubw %%mm3, %%mm5\n\t"
502 "pxor %%mm4, %%mm4\n\t"
503 "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
504 "psubusw %%mm0, %%mm1\n\t"
505 "pxor %%mm5, %%mm5\n\t"
506 "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
507 "psubusw %%mm0, %%mm1\n\t"
508 "psubusw %%mm0, %%mm1\n\t"
509 "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
510 "pshufw $0xb1, %%mm2, %%mm0\n\t"
511 "por %%mm2, %%mm0\n\t" /* 1 if not close */
512 "punpckhdq %%mm0, %%mm0\n\t"
513 "movq %%mm4, %%mm2\n\t" /* tttt */
514 "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
515 "por %%mm2, %%mm0\n\t"
516 "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
517 "psrlq $16, %%mm0\n\t"
518 "psrlw $15, %%mm0\n\t"
520 : "=r" (interlaced
), "=m" (s
->bigger
), "=m" (s
->twox
)
525 s
->interlaced_high
+= interlaced
>> 16;
526 s
->interlaced_low
+= interlaced
;
529 "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
530 "psubw %%mm0, %%mm4\n\t"
531 "psubw %%mm0, %%mm5\n\t"
532 "psubw %%mm0, %%mm1\n\t"
533 "paddw %0, %%mm4\n\t"
534 "paddw %1, %%mm5\n\t"
535 "paddw %2, %%mm1\n\t"
539 : "=m" (s
->tiny
), "=m" (s
->low
), "=m" (s
->high
)
543 "pshufw $0, %2, %%mm0\n\t"
544 "psubusw %%mm7, %%mm0\n\t"
545 "pcmpeqw %%mm6, %%mm0\n\t" /* 0 if below sad_thres */
546 "pand %%mm7, %%mm0\n\t"
547 "movq %%mm0, %%mm1\n\t"
548 "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
549 "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
550 "paddd %0, %%mm0\n\t"
551 "paddd %1, %%mm1\n\t"
554 : "=m" (s
->sad
.even
), "=m" (s
->sad
.noise
)
560 "movq %%mm7, (%1)\n\t"
562 "movq %%mm7, (%0)\n\t"
564 : : "r" (&s
->max
), "r" (&tm
), "X" (s
->max
)
572 cm
= block_metrics_c(a
, b
, as
, bs
, 4, p
, &ts
);
574 mp_msg(MSGT_VFILTER
, MSGL_WARN
, "Bad metrics\n");
576 # define CHECK(X) if (!MEQ(s->X, ts.X)) \
577 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
591 dint_copy_line_mmx2(unsigned char *dst
, unsigned char *a
, long bos
,
592 long cos
, int ds
, int ss
, int w
, int t
)
595 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "dint_copy_line_mmx2: internal error\n");
598 unsigned long len
= (w
+7) >> 3;
601 "pxor %%mm6, %%mm6 \n\t" /* deinterlaced pixel counter */
602 "movd %0, %%mm7 \n\t"
603 "punpcklbw %%mm7, %%mm7 \n\t"
604 "punpcklwd %%mm7, %%mm7 \n\t"
605 "punpckldq %%mm7, %%mm7 \n\t" /* mm7 = threshold */
611 "movq (%0), %%mm0\n\t"
612 "movq (%0,%3,2), %%mm1\n\t"
613 "movq %%mm0, (%2)\n\t"
614 "pmaxub %%mm1, %%mm0\n\t"
615 "pavgb (%0), %%mm1\n\t"
616 "psubusb %%mm1, %%mm0\n\t"
617 "paddusb %%mm7, %%mm0\n\t" /* mm0 = max-avg+thr */
618 "movq (%0,%1), %%mm2\n\t"
619 "movq (%0,%5), %%mm3\n\t"
620 "movq %%mm2, %%mm4\n\t"
621 PDIFFUBT(%%mm1
, %%mm2
, %%mm5
)
622 PDIFFUBT(%%mm1
, %%mm3
, %%mm5
)
623 "pminub %%mm2, %%mm3\n\t"
624 "pcmpeqb %%mm3, %%mm2\n\t" /* b = min */
625 "pand %%mm2, %%mm4\n\t"
626 "pandn (%0,%5), %%mm2\n\t"
627 "por %%mm4, %%mm2\n\t"
628 "pminub %%mm0, %%mm3\n\t"
629 "pcmpeqb %%mm0, %%mm3\n\t" /* set to 1s if >= threshold */
630 "psubb %%mm3, %%mm6\n\t" /* count pixels above thr. */
631 "pand %%mm3, %%mm1 \n\t"
632 "pandn %%mm2, %%mm3 \n\t"
633 "por %%mm3, %%mm1 \n\t" /* avg if >= threshold */
634 "movq %%mm1, (%2,%4) \n\t"
636 : "r" (a
), "r" ((x86_reg
)bos
), "r" ((x86_reg
)dst
), "r" ((x86_reg
)ss
), "r" ((x86_reg
)ds
), "r" ((x86_reg
)cos
)
642 __asm__
volatile ("pxor %%mm7, %%mm7 \n\t"
643 "psadbw %%mm6, %%mm7 \n\t"
644 "movd %%mm7, %0 \n\t"
653 dint_copy_line(unsigned char *dst
, unsigned char *a
, long bos
,
654 long cos
, int ds
, int ss
, int w
, int t
)
656 unsigned long len
= ((unsigned long)w
+sizeof(cmmx_t
)-1) / sizeof(cmmx_t
);
657 cmmx_t dint_count
= 0;
661 if (sizeof(cmmx_t
) > 4)
662 thr
|= thr
<< (sizeof(cmmx_t
)*4);
664 cmmx_t e
= *(cmmx_t
*)a
;
665 cmmx_t ne
= *(cmmx_t
*)(a
+2*ss
);
666 cmmx_t o
= *(cmmx_t
*)(a
+bos
);
667 cmmx_t oo
= *(cmmx_t
*)(a
+cos
);
668 cmmx_t maxe
= pmaxub(e
, ne
);
669 cmmx_t avge
= pavgb(e
, ne
);
670 cmmx_t max_diff
= maxe
- avge
+ thr
; /* 0<=max-avg<128, thr<128 */
671 cmmx_t diffo
= pdiffub(avge
, o
);
672 cmmx_t diffoo
= pdiffub(avge
, oo
);
673 cmmx_t diffcmp
= pcmpgtub(diffo
, diffoo
);
674 cmmx_t bo
= ((oo
^ o
) & diffcmp
) ^ o
;
675 cmmx_t diffbo
= ((diffoo
^ diffo
) & diffcmp
) ^ diffo
;
676 cmmx_t above_thr
= ~pcmpgtub(max_diff
, diffbo
);
677 cmmx_t bo_or_avg
= ((avge
^ bo
) & above_thr
) ^ bo
;
678 dint_count
+= above_thr
& ONE_BYTES
;
680 *(cmmx_t
*)(dst
+ds
) = bo_or_avg
;
682 dst
+= sizeof(cmmx_t
);
684 return psumbw(dint_count
);
688 dint_copy_plane(unsigned char *d
, unsigned char *a
, unsigned char *b
,
689 unsigned char *c
, unsigned long w
, unsigned long h
,
690 unsigned long ds
, unsigned long ss
, unsigned long threshold
,
691 long field
, long mmx2
)
693 unsigned long ret
= 0;
697 fast_memcpy(d
, b
, w
);
705 if (threshold
>= 128) {
706 fast_memcpy(d
, a
, w
);
707 fast_memcpy(d
+ds
, a
+bos
, w
);
708 } else if (mmx2
== 1) {
709 ret
+= dint_copy_line_mmx2(d
, a
, bos
, cos
, ds
, ss
, w
, threshold
);
711 ret
+= dint_copy_line(d
, a
, bos
, cos
, ds
, ss
, w
, threshold
);
716 fast_memcpy(d
, a
, w
);
718 fast_memcpy(d
+ds
, a
+bos
, w
);
723 copy_merge_fields(struct vf_priv_s
*p
, mp_image_t
*dmpi
,
724 unsigned char **old
, unsigned char **new, unsigned long show
)
726 unsigned long threshold
= 256;
727 unsigned long field
= p
->swapped
;
728 unsigned long dint_pixels
= 0;
729 unsigned char **other
= old
;
730 if (show
>= 12 || !(show
& 3))
731 show
>>= 2, other
= new, new = old
;
732 if (show
<= 2) { /* Single field: de-interlace */
733 threshold
= p
->dint_thres
;
736 } else if (show
== 3)
740 dint_pixels
+=dint_copy_plane(dmpi
->planes
[0], old
[0], new[0],
741 other
[0], p
->w
, p
->h
, dmpi
->stride
[0],
742 p
->stride
, threshold
, field
, p
->mmx2
);
743 if (dmpi
->flags
& MP_IMGFLAG_PLANAR
) {
745 old
= new, other
= new;
747 threshold
= threshold
/2 + 1;
748 field
^= p
->chroma_swapped
;
749 dint_copy_plane(dmpi
->planes
[1], old
[1], new[1],
750 other
[1], p
->cw
, p
->ch
, dmpi
->stride
[1],
751 p
->chroma_stride
, threshold
, field
, p
->mmx2
);
752 dint_copy_plane(dmpi
->planes
[2], old
[2], new[2],
753 other
[2], p
->cw
, p
->ch
, dmpi
->stride
[2],
754 p
->chroma_stride
, threshold
, field
, p
->mmx2
);
756 if (dint_pixels
> 0 && p
->verbose
)
757 mp_msg(MSGT_VFILTER
,MSGL_INFO
,"Deinterlaced %lu pixels\n",dint_pixels
);
760 static void diff_planes(struct vf_priv_s
*p
, struct frame_stats
*s
,
761 unsigned char *of
, unsigned char *nf
,
762 int w
, int h
, int os
, int ns
, int swapped
)
765 int align
= -(long)nf
& 7;
777 memset(s
, 0, sizeof(*s
));
779 for (y
= (h
-8) >> 3; y
; y
--) {
781 for (i
= 0; i
< w
; i
+= 8)
782 block_metrics_mmx2(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
783 } else if (p
->mmx2
== 2) {
784 for (i
= 0; i
< w
; i
+= 8)
785 block_metrics_3dnow(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
786 } else if (p
->fast
> 3) {
787 for (i
= 0; i
< w
; i
+= 8)
788 block_metrics_faster_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
789 } else if (p
->fast
> 1) {
790 for (i
= 0; i
< w
; i
+= 8)
791 block_metrics_fast_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
793 for (i
= 0; i
< w
; i
+= 8)
794 block_metrics_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
801 #define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
803 static void diff_fields(struct vf_priv_s
*p
, struct frame_stats
*s
,
804 unsigned char **old
, unsigned char **new)
806 diff_planes(p
, s
, old
[0], new[0], p
->w
, p
->h
,
807 p
->stride
, p
->stride
, p
->swapped
);
808 s
->sad
.even
= (s
->sad
.even
* 16ul) / s
->num_blocks
;
809 s
->sad
.odd
= (s
->sad
.odd
* 16ul) / s
->num_blocks
;
810 s
->sad
.noise
= (s
->sad
.noise
* 16ul) / s
->num_blocks
;
811 s
->sad
.temp
= (s
->sad
.temp
* 16ul) / s
->num_blocks
;
813 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "%lu%c M:%d/%d/%d/%d - %d, "
814 "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
815 "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
816 p
->inframes
, p
->chflag
, METRICS(s
->max
), s
->num_blocks
,
817 METRICS(s
->tiny
), METRICS(s
->low
), METRICS(s
->high
),
818 METRICS(s
->bigger
), METRICS(s
->twox
), METRICS(s
->sad
),
819 s
->interlaced_low
, s
->interlaced_high
,
820 p
->iosync
/ (double) p
->in_inc
);
823 static const char *parse_args(struct vf_priv_s
*p
, const char *args
)
826 while (args
&& *++args
&&
827 (sscanf(args
, "io=%lu:%lu", &p
->out_dec
, &p
->in_inc
) == 2 ||
828 sscanf(args
, "diff_thres=%hu", &p
->thres
.even
) == 1 ||
829 sscanf(args
, "comb_thres=%hu", &p
->thres
.noise
) == 1 ||
830 sscanf(args
, "sad_thres=%lu", &p
->sad_thres
) == 1 ||
831 sscanf(args
, "dint_thres=%lu", &p
->dint_thres
) == 1 ||
832 sscanf(args
, "fast=%u", &p
->fast
) == 1 ||
833 sscanf(args
, "mmx2=%lu", &p
->mmx2
) == 1 ||
834 sscanf(args
, "luma_only=%u", &p
->luma_only
) == 1 ||
835 sscanf(args
, "verbose=%u", &p
->verbose
) == 1 ||
836 sscanf(args
, "crop=%lu:%lu:%lu:%lu", &p
->w
,
837 &p
->h
, &p
->crop_x
, &p
->crop_y
) == 4))
838 args
= strchr(args
, '/');
842 static unsigned long gcd(unsigned long x
, unsigned long y
)
856 static void init(struct vf_priv_s
*p
, mp_image_t
*mpi
)
859 unsigned long plane_size
, chroma_plane_size
;
860 unsigned char *plane
;
861 unsigned long cos
, los
;
862 p
->crop_cx
= p
->crop_x
>> mpi
->chroma_x_shift
;
863 p
->crop_cy
= p
->crop_y
>> mpi
->chroma_y_shift
;
864 if (mpi
->flags
& MP_IMGFLAG_ACCEPT_STRIDE
) {
865 p
->stride
= (mpi
->w
+ 15) & ~15;
866 p
->chroma_stride
= p
->stride
>> mpi
->chroma_x_shift
;
868 p
->stride
= mpi
->width
;
869 p
->chroma_stride
= mpi
->chroma_width
;
871 p
->cw
= p
->w
>> mpi
->chroma_x_shift
;
872 p
->ch
= p
->h
>> mpi
->chroma_y_shift
;
876 p
->old_planes
= p
->planes
[0];
877 plane_size
= mpi
->h
* p
->stride
;
878 chroma_plane_size
= mpi
->flags
& MP_IMGFLAG_PLANAR
?
879 mpi
->chroma_height
* p
->chroma_stride
: 0;
880 p
->memory_allocated
=
881 malloc(NUM_STORED
* (plane_size
+2*chroma_plane_size
) +
882 8*p
->chroma_stride
+ 4096);
883 /* align to page boundary */
884 plane
= p
->memory_allocated
+ (-(long)p
->memory_allocated
& 4095);
885 memset(plane
, 0, NUM_STORED
* plane_size
);
886 los
= p
->crop_x
+ p
->crop_y
* p
->stride
;
887 cos
= p
->crop_cx
+ p
->crop_cy
* p
->chroma_stride
;
888 for (i
= 0; i
!= NUM_STORED
; i
++, plane
+= plane_size
) {
889 p
->planes
[i
][0] = plane
;
890 p
->planes
[NUM_STORED
+ i
][0] = plane
+ los
;
892 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
894 memset(plane
, 0x80, NUM_STORED
* 2 * chroma_plane_size
);
895 for (i
= 0; i
!= NUM_STORED
; i
++) {
896 p
->planes
[i
][1] = plane
;
897 p
->planes
[NUM_STORED
+ i
][1] = plane
+ cos
;
898 plane
+= chroma_plane_size
;
899 p
->planes
[i
][2] = plane
;
900 p
->planes
[NUM_STORED
+ i
][2] = plane
+ cos
;
901 plane
+= chroma_plane_size
;
905 i
= gcd(p
->in_inc
, p
->out_dec
);
912 static inline double get_time(void)
915 gettimeofday(&tv
, 0);
916 return tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
919 static void get_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
)
921 struct vf_priv_s
*p
= vf
->priv
;
922 static unsigned char **planes
, planes_idx
;
924 if (mpi
->type
== MP_IMGTYPE_STATIC
) return;
926 if (!p
->planes
[0][0]) init(p
, mpi
);
928 if (mpi
->type
== MP_IMGTYPE_TEMP
||
929 (mpi
->type
== MP_IMGTYPE_IPB
&& !(mpi
->flags
& MP_IMGFLAG_READABLE
)))
930 planes_idx
= NUM_STORED
/2 + (++p
->temp_idx
% (NUM_STORED
/2));
932 planes_idx
= ++p
->static_idx
% (NUM_STORED
/2);
933 planes
= p
->planes
[planes_idx
];
934 mpi
->priv
= p
->planes
[NUM_STORED
+ planes_idx
];
935 if (mpi
->priv
== p
->old_planes
) {
936 unsigned char **old_planes
=
937 p
->planes
[NUM_STORED
+ 2 + (++p
->temp_idx
& 1)];
938 my_memcpy_pic(old_planes
[0], p
->old_planes
[0],
939 p
->w
, p
->h
, p
->stride
, p
->stride
);
940 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
941 my_memcpy_pic(old_planes
[1], p
->old_planes
[1],
942 p
->cw
, p
->ch
, p
->chroma_stride
, p
->chroma_stride
);
943 my_memcpy_pic(old_planes
[2], p
->old_planes
[2],
944 p
->cw
, p
->ch
, p
->chroma_stride
, p
->chroma_stride
);
946 p
->old_planes
= old_planes
;
949 mpi
->planes
[0] = planes
[0];
950 mpi
->stride
[0] = p
->stride
;
951 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
952 mpi
->planes
[1] = planes
[1];
953 mpi
->planes
[2] = planes
[2];
954 mpi
->stride
[1] = mpi
->stride
[2] = p
->chroma_stride
;
956 mpi
->width
= p
->stride
;
958 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
959 mpi
->flags
&= ~MP_IMGFLAG_DRAW_CALLBACK
;
963 cmpe(unsigned long x
, unsigned long y
, unsigned long err
, unsigned long e
)
966 long unit
= ((x
+y
+err
) >> e
);
967 long ret
= (diff
> unit
) - (diff
< -unit
);
969 return ret
+ (diff
> unit
) - (diff
< -unit
);
973 find_breaks(struct vf_priv_s
*p
, struct frame_stats
*s
)
975 struct frame_stats
*ps
= &p
->stats
[(p
->inframes
-1) & 1];
976 long notfilm
= 5*p
->in_inc
- p
->out_dec
;
977 unsigned long n
= s
->num_blocks
>> 8;
978 unsigned long sad_comb_cmp
= cmpe(s
->sad
.temp
, s
->sad
.noise
, 512, 1);
979 unsigned long ret
= 8;
981 if (cmpe(s
->sad
.temp
, s
->sad
.even
, 512, 1) > 0)
982 mp_msg(MSGT_VFILTER
, MSGL_WARN
,
983 "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
984 if (s
->sad
.temp
> 1000 && s
->sad
.noise
> 1000)
986 if (s
->interlaced_high
>= 2*n
&& s
->sad
.temp
> 256 && s
->sad
.noise
> 256)
988 if (s
->high
.noise
> s
->num_blocks
/4 && s
->sad
.noise
> 10000 &&
989 s
->sad
.noise
> 2*s
->sad
.even
&& s
->sad
.noise
> 2*ps
->sad
.odd
) {
990 // Mid-frame scene change
991 if (s
->tiny
.temp
+ s
->interlaced_low
< n
||
992 s
->low
.temp
+ s
->interlaced_high
< n
/4 ||
993 s
->high
.temp
+ s
->interlaced_high
< n
/8 ||
998 if (s
->high
.temp
> s
->num_blocks
/4 && s
->sad
.temp
> 10000 &&
999 s
->sad
.temp
> 2*ps
->sad
.odd
&& s
->sad
.temp
> 2*ps
->sad
.even
) {
1000 // Start frame scene change
1001 if (s
->tiny
.noise
+ s
->interlaced_low
< n
||
1002 s
->low
.noise
+ s
->interlaced_high
< n
/4 ||
1003 s
->high
.noise
+ s
->interlaced_high
< n
/8 ||
1008 if (sad_comb_cmp
== 2)
1010 if (sad_comb_cmp
== -2)
1013 if (s
->tiny
.odd
> 3*MAX(n
,s
->tiny
.even
) + s
->interlaced_low
)
1015 if (s
->tiny
.even
> 3*MAX(n
,s
->tiny
.odd
)+s
->interlaced_low
&&
1016 (!sad_comb_cmp
|| (s
->low
.noise
<= n
/4 && s
->low
.temp
<= n
/4)))
1019 if (s
->sad
.noise
< 64 && s
->sad
.temp
< 64 &&
1020 s
->low
.noise
<= n
/2 && s
->high
.noise
<= n
/4 &&
1021 s
->low
.temp
<= n
/2 && s
->high
.temp
<= n
/4)
1024 if (s
->tiny
.temp
> 3*MAX(n
,s
->tiny
.noise
) + s
->interlaced_low
)
1026 if (s
->tiny
.noise
> 3*MAX(n
,s
->tiny
.temp
) + s
->interlaced_low
)
1029 if (s
->low
.odd
> 3*MAX(n
/4,s
->low
.even
) + s
->interlaced_high
)
1031 if (s
->low
.even
> 3*MAX(n
/4,s
->low
.odd
)+s
->interlaced_high
&&
1032 s
->sad
.even
> 2*s
->sad
.odd
&&
1033 (!sad_comb_cmp
|| (s
->low
.noise
<= n
/4 && s
->low
.temp
<= n
/4)))
1036 if (s
->low
.temp
> 3*MAX(n
/4,s
->low
.noise
) + s
->interlaced_high
)
1038 if (s
->low
.noise
> 3*MAX(n
/4,s
->low
.temp
) + s
->interlaced_high
)
1041 if (sad_comb_cmp
== 1 && s
->sad
.noise
< 64)
1043 if (sad_comb_cmp
== -1 && s
->sad
.temp
< 64)
1046 if (s
->tiny
.odd
<= n
|| (s
->tiny
.noise
<= n
/2 && s
->tiny
.temp
<= n
/2)) {
1047 if (s
->interlaced_low
<= n
) {
1048 if (p
->num_fields
== 1)
1050 if (s
->tiny
.even
<= n
|| ps
->tiny
.noise
<= n
/2)
1053 if (s
->bigger
.even
>= 2*MAX(n
,s
->bigger
.odd
) + s
->interlaced_low
)
1055 if (s
->low
.even
>= 2*n
+ s
->interlaced_low
)
1060 if (s
->low
.odd
<= n
/4) {
1061 if (s
->interlaced_high
<= n
/4) {
1062 if (p
->num_fields
== 1)
1064 if (s
->low
.even
<= n
/4)
1067 if (s
->bigger
.even
>= 2*MAX(n
/4,s
->bigger
.odd
)+s
->interlaced_high
)
1069 if (s
->low
.even
>= n
/2 + s
->interlaced_high
)
1074 if (s
->bigger
.temp
> 2*MAX(n
,s
->bigger
.noise
) + s
->interlaced_low
)
1076 if (s
->bigger
.noise
> 2*MAX(n
,s
->bigger
.temp
) + s
->interlaced_low
)
1078 if (s
->bigger
.temp
> 2*MAX(n
,s
->bigger
.noise
) + s
->interlaced_high
)
1080 if (s
->bigger
.noise
> 2*MAX(n
,s
->bigger
.temp
) + s
->interlaced_high
)
1082 if (s
->twox
.temp
> 2*MAX(n
,s
->twox
.noise
) + s
->interlaced_high
)
1084 if (s
->twox
.noise
> 2*MAX(n
,s
->twox
.temp
) + s
->interlaced_high
)
1086 if (s
->bigger
.even
> 2*MAX(n
,s
->bigger
.odd
) + s
->interlaced_low
&&
1087 s
->bigger
.temp
< n
&& s
->bigger
.noise
< n
)
1089 if (s
->interlaced_low
> MIN(2*n
, s
->tiny
.odd
))
1091 ret
= 8 + (1 << (s
->sad
.temp
> s
->sad
.noise
));
1093 if (p
->num_fields
== 1 && p
->prev_fields
== 3 && notfilm
>= 0 &&
1094 (s
->tiny
.temp
<= s
->tiny
.noise
|| s
->sad
.temp
< s
->sad
.noise
+16))
1096 if (p
->notout
< p
->num_fields
&& p
->iosync
> 2*p
->in_inc
&& notfilm
< 0)
1098 if (p
->num_fields
< 2 ||
1099 (p
->num_fields
== 2 && p
->prev_fields
== 2 && notfilm
< 0))
1101 if (!notfilm
&& (p
->prev_fields
&~1) == 2) {
1102 if (p
->prev_fields
+ p
->num_fields
== 5) {
1103 if (s
->tiny
.noise
<= s
->tiny
.temp
||
1104 s
->low
.noise
== 0 || s
->low
.noise
< s
->low
.temp
||
1105 s
->sad
.noise
< s
->sad
.temp
+16)
1108 if (p
->prev_fields
+ p
->num_fields
== 4) {
1109 if (s
->tiny
.temp
<= s
->tiny
.noise
||
1110 s
->low
.temp
== 0 || s
->low
.temp
< s
->low
.noise
||
1111 s
->sad
.temp
< s
->sad
.noise
+16)
1115 if (p
->num_fields
> 2 &&
1116 ps
->sad
.noise
> s
->sad
.noise
&& ps
->sad
.noise
> s
->sad
.temp
)
1118 return 2 >> (s
->sad
.noise
> s
->sad
.temp
);
1121 #define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
1123 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
)
1126 struct vf_priv_s
*p
= vf
->priv
;
1127 unsigned char **planes
, **old_planes
;
1128 struct frame_stats
*s
= &p
->stats
[p
->inframes
& 1];
1129 struct frame_stats
*ps
= &p
->stats
[(p
->inframes
-1) & 1];
1131 const int flags
= mpi
->fields
;
1133 int show_fields
= 0;
1134 int dropped_fields
= 0;
1135 double start_time
, diff_time
;
1136 char prev_chflag
= p
->chflag
;
1139 if (!p
->planes
[0][0]) init(p
, mpi
);
1141 old_planes
= p
->old_planes
;
1143 if ((mpi
->flags
& MP_IMGFLAG_DIRECT
) && mpi
->priv
) {
1147 planes
= p
->planes
[2 + (++p
->temp_idx
& 1)];
1148 my_memcpy_pic(planes
[0],
1149 mpi
->planes
[0] + p
->crop_x
+ p
->crop_y
* mpi
->stride
[0],
1150 p
->w
, p
->h
, p
->stride
, mpi
->stride
[0]);
1151 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
1152 my_memcpy_pic(planes
[1],
1153 mpi
->planes
[1] + p
->crop_cx
+ p
->crop_cy
* mpi
->stride
[1],
1154 p
->cw
, p
->ch
, p
->chroma_stride
, mpi
->stride
[1]);
1155 my_memcpy_pic(planes
[2],
1156 mpi
->planes
[2] + p
->crop_cx
+ p
->crop_cy
* mpi
->stride
[2],
1157 p
->cw
, p
->ch
, p
->chroma_stride
, mpi
->stride
[2]);
1162 p
->old_planes
= planes
;
1164 if (flags
& MP_IMGFIELD_ORDERED
) {
1165 swapped
= !(flags
& MP_IMGFIELD_TOP_FIRST
);
1166 p
->chflag
= (flags
& MP_IMGFIELD_REPEAT_FIRST
? '|' :
1167 flags
& MP_IMGFIELD_TOP_FIRST
? ':' : '.');
1169 p
->swapped
= swapped
;
1171 start_time
= get_time();
1172 if (p
->chflag
== '|') {
1174 p
->iosync
+= p
->in_inc
;
1175 } else if ((p
->fast
& 1) && prev_chflag
== '|')
1178 diff_fields(p
, s
, old_planes
, planes
);
1179 diff_time
= get_time();
1180 p
->diff_time
+= diff_time
- start_time
;
1181 breaks
= p
->inframes
? find_breaks(p
, s
) : 2;
1183 keep_rate
= 4*p
->in_inc
== p
->out_dec
;
1190 if (!keep_rate
&& p
->notout
< p
->num_fields
&& p
->iosync
< 2*p
->in_inc
)
1192 if (p
->notout
< p
->num_fields
)
1193 dropped_fields
= -2;
1195 if (keep_rate
|| p
->iosync
>= -2*p
->in_inc
)
1196 show_fields
= (4<<p
->num_fields
)-1;
1201 else if (p
->iosync
> 0) {
1202 if (p
->notout
>= p
->num_fields
&& p
->iosync
> 2*p
->in_inc
) {
1203 show_fields
= 4; /* prev odd only */
1204 if (p
->num_fields
> 1)
1205 show_fields
|= 8; /* + prev even */
1207 show_fields
= 2; /* even only */
1208 if (p
->notout
>= p
->num_fields
)
1209 dropped_fields
+= p
->num_fields
;
1214 if (p
->iosync
<= -3*p
->in_inc
) {
1215 if (p
->notout
>= p
->num_fields
)
1216 dropped_fields
= p
->num_fields
;
1219 if (p
->num_fields
== 1) {
1220 int prevbreak
= ps
->sad
.noise
>= 128;
1221 if (p
->iosync
< 4*p
->in_inc
) {
1223 dropped_fields
= prevbreak
;
1225 show_fields
= 4 | (!prevbreak
<< 3);
1226 if (p
->notout
< 1 + p
->prev_fields
)
1227 dropped_fields
= -!prevbreak
;
1233 show_fields
= 3 << (breaks
& 1);
1234 else if (p
->notout
>= p
->num_fields
&&
1235 p
->iosync
>= (breaks
== 1 ? -p
->in_inc
:
1236 p
->in_inc
<< (p
->num_fields
== 1))) {
1237 show_fields
= (1 << (2 + p
->num_fields
)) - (1<<breaks
);
1239 if (p
->notout
>= p
->num_fields
)
1240 dropped_fields
+= p
->num_fields
+ 2 - breaks
;
1242 if (p
->iosync
>= 4*p
->in_inc
)
1244 } else if (p
->iosync
> -3*p
->in_inc
)
1245 show_fields
= 3; /* odd+even */
1251 prev
= p
->prev_fields
;
1253 if (p
->num_fields
== 1)
1258 p
->prev_fields
= p
->num_fields
= 1;
1260 p
->prev_fields
= p
->num_fields
+ (breaks
==1) - (breaks
==4);
1261 p
->num_fields
= breaks
- (breaks
== 4) + (p
->chflag
== '|');
1267 p
->iosync
+= 4 * p
->in_inc
;
1268 if (p
->chflag
== '|')
1269 p
->iosync
+= p
->in_inc
;
1272 p
->iosync
-= p
->out_dec
;
1273 p
->notout
= !(show_fields
& 1) + !(show_fields
& 3);
1274 if (((show_fields
& 3) == 3 &&
1275 (s
->low
.noise
+ s
->interlaced_low
< (s
->num_blocks
>>8) ||
1276 s
->sad
.noise
< 160)) ||
1277 ((show_fields
& 12) == 12 &&
1278 (ps
->low
.noise
+ ps
->interlaced_low
< (s
->num_blocks
>>8) ||
1279 ps
->sad
.noise
< 160))) {
1281 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
, MP_IMGTYPE_EXPORT
,
1282 MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
,
1284 if ((show_fields
& 3) != 3) planes
= old_planes
;
1285 dmpi
->planes
[0] = planes
[0];
1286 dmpi
->stride
[0] = p
->stride
;
1287 dmpi
->width
= mpi
->width
;
1288 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
1289 dmpi
->planes
[1] = planes
[1];
1290 dmpi
->planes
[2] = planes
[2];
1291 dmpi
->stride
[1] = p
->chroma_stride
;
1292 dmpi
->stride
[2] = p
->chroma_stride
;
1296 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
1297 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
1299 copy_merge_fields(p
, dmpi
, old_planes
, planes
, show_fields
);
1306 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
1307 p
->inframes
, p
->outframes
,
1308 breaks
, breaks
<8 && breaks
>0 ? (int) p
->prev_fields
+'0' : ' ',
1310 p
->num_breaks
, 5*p
->in_inc
== p
->out_dec
&& breaks
<8 &&
1311 breaks
>0 && ((prev
&~1)!=2 || prev
+p
->prev_fields
!=5) ?
1312 " ######## bad telecine ########" : "",
1313 dropped_fields
? " ======== dropped ":"", ITOC(dropped_fields
),
1314 !show_fields
|| (show_fields
& (show_fields
-1)) ?
1315 "" : " @@@@@@@@@@@@@@@@@");
1317 p
->merge_time
+= get_time() - diff_time
;
1318 return show_fields
? vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
) : 0;
1321 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
1323 /* FIXME - support more formats */
1331 return vf_next_query_format(vf
, fmt
);
1336 static int config(struct vf_instance_s
* vf
,
1337 int width
, int height
, int d_width
, int d_height
,
1338 unsigned int flags
, unsigned int outfmt
)
1340 unsigned long cxm
= 0;
1341 unsigned long cym
= 0;
1342 struct vf_priv_s
*p
= vf
->priv
;
1344 if(!IMGFMT_IS_RGB(outfmt
) && !IMGFMT_IS_BGR(outfmt
)){
1364 p
->chroma_swapped
= !!(p
->crop_y
& (cym
+1));
1365 if (p
->w
) p
->w
+= p
->crop_x
& cxm
;
1366 if (p
->h
) p
->h
+= p
->crop_y
& cym
;
1369 if (!p
->w
|| p
->w
> width
) p
->w
= width
;
1370 if (!p
->h
|| p
->h
> height
) p
->h
= height
;
1371 if (p
->crop_x
+ p
->w
> width
) p
->crop_x
= 0;
1372 if (p
->crop_y
+ p
->h
> height
) p
->crop_y
= 0;
1374 if(!opt_screen_size_x
&& !opt_screen_size_y
){
1375 d_width
= d_width
* p
->w
/width
;
1376 d_height
= d_height
* p
->h
/height
;
1378 return vf_next_config(vf
, p
->w
, p
->h
, d_width
, d_height
, flags
, outfmt
);
1381 static void uninit(struct vf_instance_s
* vf
)
1383 struct vf_priv_s
*p
= vf
->priv
;
1384 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "diff_time: %.3f, merge_time: %.3f, "
1385 "export: %lu, merge: %lu, copy: %lu\n", p
->diff_time
, p
->merge_time
,
1386 p
->export_count
, p
->merge_count
, p
->num_copies
);
1387 free(p
->memory_allocated
);
1391 static int open(vf_instance_t
*vf
, char* args
)
1393 struct vf_priv_s
*p
;
1394 vf
->get_image
= get_image
;
1395 vf
->put_image
= put_image
;
1396 vf
->config
= config
;
1397 vf
->query_format
= query_format
;
1398 vf
->uninit
= uninit
;
1399 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
1400 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
1403 p
->thres
.noise
= 128;
1404 p
->thres
.even
= 128;
1409 p
->mmx2
= gCpuCaps
.hasMMX2
? 1 : gCpuCaps
.has3DNow
? 2 : 0;
1411 const char *args_remain
= parse_args(p
, args
);
1413 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
1414 "filmdint: unknown suboption: %s\n", args_remain
);
1417 if (p
->out_dec
< p
->in_inc
) {
1418 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
1419 "filmdint: increasing the frame rate is not supported\n");
1431 p
->thres
.odd
= p
->thres
.even
;
1432 p
->thres
.temp
= p
->thres
.noise
;
1438 const vf_info_t vf_info_filmdint
= {
1439 "Advanced inverse telecine filer",