11 #include "img_format.h"
16 #include "libvo/fastmemcpy.h"
20 enum pu_field_type_t
{
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)));
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
;
46 unsigned long inframes
;
47 unsigned long outframes
;
48 enum pu_field_type_t prev_type
;
49 unsigned swapped
, chroma_swapped
;
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
;
70 unsigned small_bytes
[2];
72 struct frame_stats stats
[2];
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};
84 #define MIN(a,b) (((a)<(b))?(a):(b))
87 #define MAX(a,b) (((a)>(b))?(a):(b))
90 #define PDIFFUB(X,Y,T) "movq " #X "," #T "\n\t" \
91 "psubusb " #Y "," #T "\n\t" \
92 "psubusb " #X "," #Y "\n\t" \
93 "paddusb " #Y "," #T "\n\t"
95 #define PDIFFUBT(X,Y,T) "movq " #X "," #T "\n\t" \
96 "psubusb " #Y "," #T "\n\t" \
97 "psubusb " #X "," #Y "\n\t" \
98 "paddusb " #T "," #Y "\n\t"
100 #define PSUMBW(X,T,Z) "movq " #X "," #T "\n\t" \
101 "punpcklbw " #Z "," #X "\n\t" \
102 "punpckhbw " #Z "," #T "\n\t" \
103 "paddw " #T "," #X "\n\t" \
104 "movq " #X "," #T "\n\t" \
105 "psllq $32, " #T "\n\t" \
106 "paddw " #T "," #X "\n\t" \
107 "movq " #X "," #T "\n\t" \
108 "psllq $16, " #T "\n\t" \
109 "paddw " #T "," #X "\n\t" \
110 "psrlq $48, " #X "\n\t"
112 #define PSADBW(X,Y,T,Z) PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
114 #define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
115 #define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
116 #define PMINUBT(X,Y,T) "movq " #Y "," #T "\n\t" \
117 "psubusb " #X "," #T "\n\t" \
118 "psubusb " #T "," #Y "\n\t"
119 #define PAVGB(X,Y) "pavgusb " #X "," #Y "\n\t"
122 get_metrics_c(unsigned char *a
, unsigned char *b
, int as
, int bs
, int lines
,
128 cmmx_t old_po
= *(cmmx_t
*)(a
);
129 cmmx_t po
= *(cmmx_t
*)(b
);
130 cmmx_t e
= *(cmmx_t
*)(b
+ bs
);
131 cmmx_t old_o
= *(cmmx_t
*)(a
+ 2*as
);
132 cmmx_t o
= *(cmmx_t
*)(b
+ 2*bs
);
133 cmmx_t ne
= *(cmmx_t
*)(b
+ 3*bs
);
134 cmmx_t old_no
= *(cmmx_t
*)(a
+ 4*as
);
135 cmmx_t no
= *(cmmx_t
*)(b
+ 4*bs
);
137 cmmx_t qup_old_odd
= p31avgb(old_o
, old_po
);
138 cmmx_t qup_odd
= p31avgb( o
, po
);
139 cmmx_t qdown_old_odd
= p31avgb(old_o
, old_no
);
140 cmmx_t qdown_odd
= p31avgb( o
, no
);
142 cmmx_t qup_even
= p31avgb(ne
, e
);
143 cmmx_t qdown_even
= p31avgb(e
, ne
);
145 cmmx_t temp_up_diff
= pdiffub(qdown_even
, qup_old_odd
);
146 cmmx_t noise_up_diff
= pdiffub(qdown_even
, qup_odd
);
147 cmmx_t temp_down_diff
= pdiffub(qup_even
, qdown_old_odd
);
148 cmmx_t noise_down_diff
= pdiffub(qup_even
, qdown_odd
);
150 cmmx_t odd_diff
= pdiffub(o
, old_o
);
151 m
->odd
+= psumbw(odd_diff
);
152 m
->even
+= psadbw(e
, *(cmmx_t
*)(a
+as
));
154 temp_up_diff
= pminub(temp_up_diff
, temp_down_diff
);
155 temp_up_diff
= pminub(temp_up_diff
, odd_diff
);
156 m
->temp
+= psumbw(temp_up_diff
);
157 noise_up_diff
= pminub(noise_up_diff
, odd_diff
);
158 noise_up_diff
= pminub(noise_up_diff
, noise_down_diff
);
160 m
->noise
+= psumbw(noise_up_diff
);
167 get_metrics_fast_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
168 int lines
, struct metrics
*m
)
173 cmmx_t old_po
= (*(cmmx_t
*)(a
) >> 1) & ~SIGN_BITS
;
174 cmmx_t po
= (*(cmmx_t
*)(b
) >> 1) & ~SIGN_BITS
;
175 cmmx_t old_e
= (*(cmmx_t
*)(a
+ as
) >> 1) & ~SIGN_BITS
;
176 cmmx_t e
= (*(cmmx_t
*)(b
+ bs
) >> 1) & ~SIGN_BITS
;
177 cmmx_t old_o
= (*(cmmx_t
*)(a
+ 2*as
) >> 1) & ~SIGN_BITS
;
178 cmmx_t o
= (*(cmmx_t
*)(b
+ 2*bs
) >> 1) & ~SIGN_BITS
;
179 cmmx_t ne
= (*(cmmx_t
*)(b
+ 3*bs
) >> 1) & ~SIGN_BITS
;
180 cmmx_t old_no
= (*(cmmx_t
*)(a
+ 4*as
) >> 1) & ~SIGN_BITS
;
181 cmmx_t no
= (*(cmmx_t
*)(b
+ 4*bs
) >> 1) & ~SIGN_BITS
;
183 cmmx_t qup_old_odd
= p31avgb_s(old_o
, old_po
);
184 cmmx_t qup_odd
= p31avgb_s( o
, po
);
185 cmmx_t qdown_old_odd
= p31avgb_s(old_o
, old_no
);
186 cmmx_t qdown_odd
= p31avgb_s( o
, no
);
188 cmmx_t qup_even
= p31avgb_s(ne
, e
);
189 cmmx_t qdown_even
= p31avgb_s(e
, ne
);
191 cmmx_t temp_up_diff
= pdiffub_s(qdown_even
, qup_old_odd
);
192 cmmx_t noise_up_diff
= pdiffub_s(qdown_even
, qup_odd
);
193 cmmx_t temp_down_diff
= pdiffub_s(qup_even
, qdown_old_odd
);
194 cmmx_t noise_down_diff
= pdiffub_s(qup_even
, qdown_odd
);
196 cmmx_t odd_diff
= pdiffub_s(o
, old_o
);
197 m
->odd
+= psumbw_s(odd_diff
) << 1;
198 m
->even
+= psadbw_s(e
, old_e
) << 1;
200 temp_up_diff
= pminub_s(temp_up_diff
, temp_down_diff
);
201 temp_up_diff
= pminub_s(temp_up_diff
, odd_diff
);
202 m
->temp
+= psumbw_s(temp_up_diff
) << 1;
203 noise_up_diff
= pminub_s(noise_up_diff
, odd_diff
);
204 noise_up_diff
= pminub_s(noise_up_diff
, noise_down_diff
);
206 m
->noise
+= psumbw_s(noise_up_diff
) << 1;
213 get_metrics_faster_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
214 int lines
, struct metrics
*m
)
219 cmmx_t old_po
= (*(cmmx_t
*)(a
)>>1) & ~SIGN_BITS
;
220 cmmx_t po
= (*(cmmx_t
*)(b
)>>1) & ~SIGN_BITS
;
221 cmmx_t old_e
= (*(cmmx_t
*)(a
+ as
)>>1) & ~SIGN_BITS
;
222 cmmx_t e
= (*(cmmx_t
*)(b
+ bs
)>>1) & ~SIGN_BITS
;
223 cmmx_t old_o
= (*(cmmx_t
*)(a
+ 2*as
)>>1) & ~SIGN_BITS
;
224 cmmx_t o
= (*(cmmx_t
*)(b
+ 2*bs
)>>1) & ~SIGN_BITS
;
225 cmmx_t ne
= (*(cmmx_t
*)(b
+ 3*bs
)>>1) & ~SIGN_BITS
;
227 cmmx_t down_even
= p31avgb_s(e
, ne
);
228 cmmx_t up_odd
= p31avgb_s(o
, po
);
229 cmmx_t up_old_odd
= p31avgb_s(old_o
, old_po
);
231 cmmx_t odd_diff
= pdiffub_s(o
, old_o
);
232 cmmx_t temp_diff
= pdiffub_s(down_even
, up_old_odd
);
233 cmmx_t noise_diff
= pdiffub_s(down_even
, up_odd
);
235 m
->even
+= psadbw_s(e
, old_e
) << 1;
236 m
->odd
+= psumbw_s(odd_diff
) << 1;
238 temp_diff
= pminub_s(temp_diff
, odd_diff
);
239 noise_diff
= pminub_s(noise_diff
, odd_diff
);
241 m
->noise
+= psumbw_s(noise_diff
) << 1;
242 m
->temp
+= psumbw_s(temp_diff
) << 1;
250 get_block_stats(struct metrics
*m
, struct vf_priv_s
*p
, struct frame_stats
*s
)
252 unsigned two_e
= m
->even
+ MAX(m
->even
, p
->thres
.even
);
253 unsigned two_o
= m
->odd
+ MAX(m
->odd
, p
->thres
.odd
);
254 unsigned two_n
= m
->noise
+ MAX(m
->noise
, p
->thres
.noise
);
255 unsigned two_t
= m
->temp
+ MAX(m
->temp
, p
->thres
.temp
);
257 unsigned e_big
= m
->even
>= (m
->odd
+ two_o
+ 1)/2;
258 unsigned o_big
= m
->odd
>= (m
->even
+ two_e
+ 1)/2;
259 unsigned n_big
= m
->noise
>= (m
->temp
+ two_t
+ 1)/2;
260 unsigned t_big
= m
->temp
>= (m
->noise
+ two_n
+ 1)/2;
262 unsigned e2x
= m
->even
>= two_o
;
263 unsigned o2x
= m
->odd
>= two_e
;
264 unsigned n2x
= m
->noise
>= two_t
;
265 unsigned t2x
= m
->temp
>= two_n
;
267 unsigned ntiny_e
= m
->even
> p
->thres
.even
;
268 unsigned ntiny_o
= m
->odd
> p
->thres
.odd
;
269 unsigned ntiny_n
= m
->noise
> p
->thres
.noise
;
270 unsigned ntiny_t
= m
->temp
> p
->thres
.temp
;
272 unsigned nlow_e
= m
->even
> 2*p
->thres
.even
;
273 unsigned nlow_o
= m
->odd
> 2*p
->thres
.odd
;
274 unsigned nlow_n
= m
->noise
> 2*p
->thres
.noise
;
275 unsigned nlow_t
= m
->temp
> 2*p
->thres
.temp
;
277 unsigned high_e
= m
->even
> 4*p
->thres
.even
;
278 unsigned high_o
= m
->odd
> 4*p
->thres
.odd
;
279 unsigned high_n
= m
->noise
> 4*p
->thres
.noise
;
280 unsigned high_t
= m
->temp
> 4*p
->thres
.temp
;
282 unsigned low_il
= !n_big
&& !t_big
&& ntiny_n
&& ntiny_t
;
283 unsigned high_il
= !n_big
&& !t_big
&& nlow_n
&& nlow_t
;
285 if (low_il
| high_il
) {
286 s
->interlaced_low
+= low_il
;
287 s
->interlaced_high
+= high_il
;
289 s
->tiny
.even
+= ntiny_e
;
290 s
->tiny
.odd
+= ntiny_o
;
291 s
->tiny
.noise
+= ntiny_n
;
292 s
->tiny
.temp
+= ntiny_t
;
294 s
->low
.even
+= nlow_e
;
295 s
->low
.odd
+= nlow_o
;
296 s
->low
.noise
+= nlow_n
;
297 s
->low
.temp
+= nlow_t
;
299 s
->high
.even
+= high_e
;
300 s
->high
.odd
+= high_o
;
301 s
->high
.noise
+= high_n
;
302 s
->high
.temp
+= high_t
;
304 if (m
->even
>= p
->sad_thres
) s
->sad
.even
+= m
->even
;
305 if (m
->odd
>= p
->sad_thres
) s
->sad
.odd
+= m
->odd
;
306 if (m
->noise
>= p
->sad_thres
) s
->sad
.noise
+= m
->noise
;
307 if (m
->temp
>= p
->sad_thres
) s
->sad
.temp
+= m
->temp
;
310 s
->max
.even
= MAX(s
->max
.even
, m
->even
);
311 s
->max
.odd
= MAX(s
->max
.odd
, m
->odd
);
312 s
->max
.noise
= MAX(s
->max
.noise
, m
->noise
);
313 s
->max
.temp
= MAX(s
->max
.temp
, m
->temp
);
315 s
->bigger
.even
+= e_big
;
316 s
->bigger
.odd
+= o_big
;
317 s
->bigger
.noise
+= n_big
;
318 s
->bigger
.temp
+= t_big
;
320 s
->twox
.even
+= e2x
;
322 s
->twox
.noise
+= n2x
;
323 s
->twox
.temp
+= t2x
;
327 static inline struct metrics
328 block_metrics_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
329 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
332 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
333 get_metrics_c(a
, b
, as
, bs
, lines
, &tm
);
334 if (sizeof(cmmx_t
) < 8)
335 get_metrics_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
336 get_block_stats(&tm
, p
, s
);
340 static inline struct metrics
341 block_metrics_fast_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
342 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
345 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
346 get_metrics_fast_c(a
, b
, as
, bs
, lines
, &tm
);
347 if (sizeof(cmmx_t
) < 8)
348 get_metrics_fast_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
349 get_block_stats(&tm
, p
, s
);
353 static inline struct metrics
354 block_metrics_faster_c(unsigned char *a
, unsigned char *b
, int as
, int bs
,
355 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
358 tm
.even
= tm
.odd
= tm
.noise
= tm
.temp
= 0;
359 get_metrics_faster_c(a
, b
, as
, bs
, lines
, &tm
);
360 if (sizeof(cmmx_t
) < 8)
361 get_metrics_faster_c(a
+4, b
+4, as
, bs
, lines
, &tm
);
362 get_block_stats(&tm
, p
, s
);
366 #define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
368 #define BLOCK_METRICS_TEMPLATE() \
369 __asm__ volatile("pxor %mm7, %mm7\n\t" /* The result is colleted in mm7 */ \
370 "pxor %mm6, %mm6\n\t" /* Temp to stay at 0 */ \
376 "movq (%0,%2), %%mm0\n\t" \
377 "movq (%1,%3), %%mm1\n\t" /* mm1 = even */ \
378 PSADBW(%%mm1, %%mm0, %%mm4, %%mm6) \
379 "paddusw %%mm0, %%mm7\n\t" /* even diff */ \
380 "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */ \
381 "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */ \
382 "movq (%0), %%mm3\n\t" \
383 "psubusb %4, %%mm3\n\t" \
384 PAVGB(%%mm0, %%mm3) \
385 PAVGB(%%mm0, %%mm3) /* mm3 = qup old odd */ \
386 "movq %%mm0, %%mm5\n\t" \
387 PSADBW(%%mm2, %%mm0, %%mm4, %%mm6) \
388 "psllq $16, %%mm0\n\t" \
389 "paddusw %%mm0, %%mm7\n\t" \
390 "movq (%1), %%mm4\n\t" \
391 "lea (%0,%2,2), %0\n\t" \
392 "lea (%1,%3,2), %1\n\t" \
393 "psubusb %4, %%mm4\n\t" \
394 PAVGB(%%mm2, %%mm4) \
395 PAVGB(%%mm2, %%mm4) /* mm4 = qup odd */ \
396 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */ \
397 "movq (%1,%3), %%mm5\n\t" \
398 "psubusb %4, %%mm5\n\t" \
399 PAVGB(%%mm1, %%mm5) \
400 PAVGB(%%mm5, %%mm1) /* mm1 = qdown even */ \
401 PAVGB((%1,%3), %%mm5) /* mm5 = qup next even */ \
402 PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */ \
403 PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */ \
404 PMINUBT(%%mm2, %%mm3, %%mm0) /* limit temp to odd diff */ \
405 PMINUBT(%%mm2, %%mm4, %%mm0) /* limit noise to odd diff */ \
406 "movq (%1,%3,2), %%mm2\n\t" \
407 "psubusb %4, %%mm2\n\t" \
409 PAVGB((%1), %%mm2) /* mm2 = qdown odd */ \
410 "movq (%0,%2,2), %%mm1\n\t" \
411 "psubusb %4, %%mm1\n\t" \
413 PAVGB((%0), %%mm1) /* mm1 = qdown old odd */ \
414 PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */ \
415 PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */ \
416 PMINUBT(%%mm4, %%mm2, %%mm0) /* current */ \
417 PMINUBT(%%mm3, %%mm1, %%mm0) /* old */ \
418 PSUMBW(%%mm2, %%mm0, %%mm6) \
419 PSUMBW(%%mm1, %%mm0, %%mm6) \
420 "psllq $32, %%mm2\n\t" \
421 "psllq $48, %%mm1\n\t" \
422 "paddusw %%mm2, %%mm7\n\t" \
423 "paddusw %%mm1, %%mm7\n\t" \
424 : "=r" (a), "=r" (b) \
425 : "r"((x86_reg)as), "r"((x86_reg)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
429 static inline struct metrics
430 block_metrics_3dnow(unsigned char *a
, unsigned char *b
, int as
, int bs
,
431 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
435 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "block_metrics_3dnow: internal error\n");
437 static const unsigned long long ones
= 0x0101010101010101ull
;
439 BLOCK_METRICS_TEMPLATE();
440 __asm__
volatile("movq %%mm7, %0\n\temms" : "=m" (tm
));
441 get_block_stats(&tm
, p
, s
);
452 #define PSUMBW(X,T,Z) "psadbw " #Z "," #X "\n\t"
453 #define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
454 #define PMAXUB(X,Y) "pmaxub " #X "," #Y "\n\t"
455 #define PMINUBT(X,Y,T) "pminub " #X "," #Y "\n\t"
456 #define PAVGB(X,Y) "pavgb " #X "," #Y "\n\t"
458 static inline struct metrics
459 block_metrics_mmx2(unsigned char *a
, unsigned char *b
, int as
, int bs
,
460 int lines
, struct vf_priv_s
*p
, struct frame_stats
*s
)
464 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "block_metrics_mmx2: internal error\n");
466 static const unsigned long long ones
= 0x0101010101010101ull
;
468 x86_reg prefetch_line
= (((long)a
>>3) & 7) + 10;
470 struct frame_stats ts
= *s
;
472 __asm__
volatile("prefetcht0 (%0,%2)\n\t"
473 "prefetcht0 (%1,%3)\n\t" :
475 "r" (prefetch_line
* as
), "r" (prefetch_line
* bs
));
477 BLOCK_METRICS_TEMPLATE();
482 "movq %%mm7, %%mm1\n\t"
483 "psubusw %%mm0, %%mm1\n\t"
484 "movq %%mm1, %%mm2\n\t"
485 "paddusw %%mm0, %%mm2\n\t"
486 "paddusw %%mm7, %%mm2\n\t"
487 "pshufw $0xb1, %%mm2, %%mm3\n\t"
488 "pavgw %%mm7, %%mm2\n\t"
489 "pshufw $0xb1, %%mm2, %%mm2\n\t"
490 "psubusw %%mm7, %%mm2\n\t"
491 "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
492 "psubusw %%mm7, %%mm3\n\t"
493 "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
496 "psubw %%mm2, %%mm4\n\t"
497 "psubw %%mm3, %%mm5\n\t"
500 "pxor %%mm4, %%mm4\n\t"
501 "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
502 "psubusw %%mm0, %%mm1\n\t"
503 "pxor %%mm5, %%mm5\n\t"
504 "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
505 "psubusw %%mm0, %%mm1\n\t"
506 "psubusw %%mm0, %%mm1\n\t"
507 "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
508 "pshufw $0xb1, %%mm2, %%mm0\n\t"
509 "por %%mm2, %%mm0\n\t" /* 1 if not close */
510 "punpckhdq %%mm0, %%mm0\n\t"
511 "movq %%mm4, %%mm2\n\t" /* tttt */
512 "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
513 "por %%mm2, %%mm0\n\t"
514 "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
515 "psrlq $16, %%mm0\n\t"
516 "psrlw $15, %%mm0\n\t"
518 : "=r" (interlaced
), "=m" (s
->bigger
), "=m" (s
->twox
)
523 s
->interlaced_high
+= interlaced
>> 16;
524 s
->interlaced_low
+= interlaced
;
527 "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
528 "psubw %%mm0, %%mm4\n\t"
529 "psubw %%mm0, %%mm5\n\t"
530 "psubw %%mm0, %%mm1\n\t"
531 "paddw %0, %%mm4\n\t"
532 "paddw %1, %%mm5\n\t"
533 "paddw %2, %%mm1\n\t"
537 : "=m" (s
->tiny
), "=m" (s
->low
), "=m" (s
->high
)
541 "pshufw $0, %2, %%mm0\n\t"
542 "psubusw %%mm7, %%mm0\n\t"
543 "pcmpeqw %%mm6, %%mm0\n\t" /* 0 if below sad_thres */
544 "pand %%mm7, %%mm0\n\t"
545 "movq %%mm0, %%mm1\n\t"
546 "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
547 "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
548 "paddd %0, %%mm0\n\t"
549 "paddd %1, %%mm1\n\t"
552 : "=m" (s
->sad
.even
), "=m" (s
->sad
.noise
)
558 "movq %%mm7, (%1)\n\t"
560 "movq %%mm7, (%0)\n\t"
562 : : "r" (&s
->max
), "r" (&tm
), "X" (s
->max
)
570 cm
= block_metrics_c(a
, b
, as
, bs
, 4, p
, &ts
);
572 mp_msg(MSGT_VFILTER
, MSGL_WARN
, "Bad metrics\n");
574 # define CHECK(X) if (!MEQ(s->X, ts.X)) \
575 mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
589 dint_copy_line_mmx2(unsigned char *dst
, unsigned char *a
, long bos
,
590 long cos
, int ds
, int ss
, int w
, int t
)
593 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "dint_copy_line_mmx2: internal error\n");
596 unsigned long len
= (w
+7) >> 3;
599 "pxor %%mm6, %%mm6 \n\t" /* deinterlaced pixel counter */
600 "movd %0, %%mm7 \n\t"
601 "punpcklbw %%mm7, %%mm7 \n\t"
602 "punpcklwd %%mm7, %%mm7 \n\t"
603 "punpckldq %%mm7, %%mm7 \n\t" /* mm7 = threshold */
609 "movq (%0), %%mm0\n\t"
610 "movq (%0,%3,2), %%mm1\n\t"
611 "movq %%mm0, (%2)\n\t"
612 "pmaxub %%mm1, %%mm0\n\t"
613 "pavgb (%0), %%mm1\n\t"
614 "psubusb %%mm1, %%mm0\n\t"
615 "paddusb %%mm7, %%mm0\n\t" /* mm0 = max-avg+thr */
616 "movq (%0,%1), %%mm2\n\t"
617 "movq (%0,%5), %%mm3\n\t"
618 "movq %%mm2, %%mm4\n\t"
619 PDIFFUBT(%%mm1
, %%mm2
, %%mm5
)
620 PDIFFUBT(%%mm1
, %%mm3
, %%mm5
)
621 "pminub %%mm2, %%mm3\n\t"
622 "pcmpeqb %%mm3, %%mm2\n\t" /* b = min */
623 "pand %%mm2, %%mm4\n\t"
624 "pandn (%0,%5), %%mm2\n\t"
625 "por %%mm4, %%mm2\n\t"
626 "pminub %%mm0, %%mm3\n\t"
627 "pcmpeqb %%mm0, %%mm3\n\t" /* set to 1s if >= threshold */
628 "psubb %%mm3, %%mm6\n\t" /* count pixels above thr. */
629 "pand %%mm3, %%mm1 \n\t"
630 "pandn %%mm2, %%mm3 \n\t"
631 "por %%mm3, %%mm1 \n\t" /* avg if >= threshold */
632 "movq %%mm1, (%2,%4) \n\t"
634 : "r" (a
), "r" ((x86_reg
)bos
), "r" ((x86_reg
)dst
), "r" ((x86_reg
)ss
), "r" ((x86_reg
)ds
), "r" ((x86_reg
)cos
)
640 __asm__
volatile ("pxor %%mm7, %%mm7 \n\t"
641 "psadbw %%mm6, %%mm7 \n\t"
642 "movd %%mm7, %0 \n\t"
651 dint_copy_line(unsigned char *dst
, unsigned char *a
, long bos
,
652 long cos
, int ds
, int ss
, int w
, int t
)
654 unsigned long len
= ((unsigned long)w
+sizeof(cmmx_t
)-1) / sizeof(cmmx_t
);
655 cmmx_t dint_count
= 0;
659 if (sizeof(cmmx_t
) > 4)
660 thr
|= thr
<< (sizeof(cmmx_t
)*4);
662 cmmx_t e
= *(cmmx_t
*)a
;
663 cmmx_t ne
= *(cmmx_t
*)(a
+2*ss
);
664 cmmx_t o
= *(cmmx_t
*)(a
+bos
);
665 cmmx_t oo
= *(cmmx_t
*)(a
+cos
);
666 cmmx_t maxe
= pmaxub(e
, ne
);
667 cmmx_t avge
= pavgb(e
, ne
);
668 cmmx_t max_diff
= maxe
- avge
+ thr
; /* 0<=max-avg<128, thr<128 */
669 cmmx_t diffo
= pdiffub(avge
, o
);
670 cmmx_t diffoo
= pdiffub(avge
, oo
);
671 cmmx_t diffcmp
= pcmpgtub(diffo
, diffoo
);
672 cmmx_t bo
= ((oo
^ o
) & diffcmp
) ^ o
;
673 cmmx_t diffbo
= ((diffoo
^ diffo
) & diffcmp
) ^ diffo
;
674 cmmx_t above_thr
= ~pcmpgtub(max_diff
, diffbo
);
675 cmmx_t bo_or_avg
= ((avge
^ bo
) & above_thr
) ^ bo
;
676 dint_count
+= above_thr
& ONE_BYTES
;
678 *(cmmx_t
*)(dst
+ds
) = bo_or_avg
;
680 dst
+= sizeof(cmmx_t
);
682 return psumbw(dint_count
);
686 dint_copy_plane(unsigned char *d
, unsigned char *a
, unsigned char *b
,
687 unsigned char *c
, unsigned long w
, unsigned long h
,
688 unsigned long ds
, unsigned long ss
, unsigned long threshold
,
689 long field
, long mmx2
)
691 unsigned long ret
= 0;
695 fast_memcpy(d
, b
, w
);
703 if (threshold
>= 128) {
704 fast_memcpy(d
, a
, w
);
705 fast_memcpy(d
+ds
, a
+bos
, w
);
706 } else if (mmx2
== 1) {
707 ret
+= dint_copy_line_mmx2(d
, a
, bos
, cos
, ds
, ss
, w
, threshold
);
709 ret
+= dint_copy_line(d
, a
, bos
, cos
, ds
, ss
, w
, threshold
);
714 fast_memcpy(d
, a
, w
);
716 fast_memcpy(d
+ds
, a
+bos
, w
);
721 copy_merge_fields(struct vf_priv_s
*p
, mp_image_t
*dmpi
,
722 unsigned char **old
, unsigned char **new, unsigned long show
)
724 unsigned long threshold
= 256;
725 unsigned long field
= p
->swapped
;
726 unsigned long dint_pixels
= 0;
727 unsigned char **other
= old
;
728 if (show
>= 12 || !(show
& 3))
729 show
>>= 2, other
= new, new = old
;
730 if (show
<= 2) { /* Single field: de-interlace */
731 threshold
= p
->dint_thres
;
734 } else if (show
== 3)
738 dint_pixels
+=dint_copy_plane(dmpi
->planes
[0], old
[0], new[0],
739 other
[0], p
->w
, p
->h
, dmpi
->stride
[0],
740 p
->stride
, threshold
, field
, p
->mmx2
);
741 if (dmpi
->flags
& MP_IMGFLAG_PLANAR
) {
743 old
= new, other
= new;
745 threshold
= threshold
/2 + 1;
746 field
^= p
->chroma_swapped
;
747 dint_copy_plane(dmpi
->planes
[1], old
[1], new[1],
748 other
[1], p
->cw
, p
->ch
, dmpi
->stride
[1],
749 p
->chroma_stride
, threshold
, field
, p
->mmx2
);
750 dint_copy_plane(dmpi
->planes
[2], old
[2], new[2],
751 other
[2], p
->cw
, p
->ch
, dmpi
->stride
[2],
752 p
->chroma_stride
, threshold
, field
, p
->mmx2
);
754 if (dint_pixels
> 0 && p
->verbose
)
755 mp_msg(MSGT_VFILTER
,MSGL_INFO
,"Deinterlaced %lu pixels\n",dint_pixels
);
758 static void diff_planes(struct vf_priv_s
*p
, struct frame_stats
*s
,
759 unsigned char *of
, unsigned char *nf
,
760 int w
, int h
, int os
, int ns
, int swapped
)
763 int align
= -(long)nf
& 7;
775 memset(s
, 0, sizeof(*s
));
777 for (y
= (h
-8) >> 3; y
; y
--) {
779 for (i
= 0; i
< w
; i
+= 8)
780 block_metrics_mmx2(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
781 } else if (p
->mmx2
== 2) {
782 for (i
= 0; i
< w
; i
+= 8)
783 block_metrics_3dnow(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
784 } else if (p
->fast
> 3) {
785 for (i
= 0; i
< w
; i
+= 8)
786 block_metrics_faster_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
787 } else if (p
->fast
> 1) {
788 for (i
= 0; i
< w
; i
+= 8)
789 block_metrics_fast_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
791 for (i
= 0; i
< w
; i
+= 8)
792 block_metrics_c(of
+i
, nf
+i
, os
, ns
, 4, p
, s
);
799 #define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
801 static void diff_fields(struct vf_priv_s
*p
, struct frame_stats
*s
,
802 unsigned char **old
, unsigned char **new)
804 diff_planes(p
, s
, old
[0], new[0], p
->w
, p
->h
,
805 p
->stride
, p
->stride
, p
->swapped
);
806 s
->sad
.even
= (s
->sad
.even
* 16ul) / s
->num_blocks
;
807 s
->sad
.odd
= (s
->sad
.odd
* 16ul) / s
->num_blocks
;
808 s
->sad
.noise
= (s
->sad
.noise
* 16ul) / s
->num_blocks
;
809 s
->sad
.temp
= (s
->sad
.temp
* 16ul) / s
->num_blocks
;
811 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "%lu%c M:%d/%d/%d/%d - %d, "
812 "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
813 "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
814 p
->inframes
, p
->chflag
, METRICS(s
->max
), s
->num_blocks
,
815 METRICS(s
->tiny
), METRICS(s
->low
), METRICS(s
->high
),
816 METRICS(s
->bigger
), METRICS(s
->twox
), METRICS(s
->sad
),
817 s
->interlaced_low
, s
->interlaced_high
,
818 p
->iosync
/ (double) p
->in_inc
);
821 static const char *parse_args(struct vf_priv_s
*p
, const char *args
)
824 while (args
&& *++args
&&
825 (sscanf(args
, "io=%lu:%lu", &p
->out_dec
, &p
->in_inc
) == 2 ||
826 sscanf(args
, "diff_thres=%hu", &p
->thres
.even
) == 1 ||
827 sscanf(args
, "comb_thres=%hu", &p
->thres
.noise
) == 1 ||
828 sscanf(args
, "sad_thres=%lu", &p
->sad_thres
) == 1 ||
829 sscanf(args
, "dint_thres=%lu", &p
->dint_thres
) == 1 ||
830 sscanf(args
, "fast=%u", &p
->fast
) == 1 ||
831 sscanf(args
, "mmx2=%lu", &p
->mmx2
) == 1 ||
832 sscanf(args
, "luma_only=%u", &p
->luma_only
) == 1 ||
833 sscanf(args
, "verbose=%u", &p
->verbose
) == 1 ||
834 sscanf(args
, "crop=%lu:%lu:%lu:%lu", &p
->w
,
835 &p
->h
, &p
->crop_x
, &p
->crop_y
) == 4))
836 args
= strchr(args
, '/');
840 static unsigned long gcd(unsigned long x
, unsigned long y
)
854 static void init(struct vf_priv_s
*p
, mp_image_t
*mpi
)
857 unsigned long plane_size
, chroma_plane_size
;
858 unsigned char *plane
;
859 unsigned long cos
, los
;
860 p
->crop_cx
= p
->crop_x
>> mpi
->chroma_x_shift
;
861 p
->crop_cy
= p
->crop_y
>> mpi
->chroma_y_shift
;
862 if (mpi
->flags
& MP_IMGFLAG_ACCEPT_STRIDE
) {
863 p
->stride
= (mpi
->w
+ 15) & ~15;
864 p
->chroma_stride
= p
->stride
>> mpi
->chroma_x_shift
;
866 p
->stride
= mpi
->width
;
867 p
->chroma_stride
= mpi
->chroma_width
;
869 p
->cw
= p
->w
>> mpi
->chroma_x_shift
;
870 p
->ch
= p
->h
>> mpi
->chroma_y_shift
;
874 p
->old_planes
= p
->planes
[0];
875 plane_size
= mpi
->h
* p
->stride
;
876 chroma_plane_size
= mpi
->flags
& MP_IMGFLAG_PLANAR
?
877 mpi
->chroma_height
* p
->chroma_stride
: 0;
878 p
->memory_allocated
=
879 malloc(NUM_STORED
* (plane_size
+2*chroma_plane_size
) +
880 8*p
->chroma_stride
+ 4096);
881 /* align to page boundary */
882 plane
= p
->memory_allocated
+ (-(long)p
->memory_allocated
& 4095);
883 memset(plane
, 0, NUM_STORED
* plane_size
);
884 los
= p
->crop_x
+ p
->crop_y
* p
->stride
;
885 cos
= p
->crop_cx
+ p
->crop_cy
* p
->chroma_stride
;
886 for (i
= 0; i
!= NUM_STORED
; i
++, plane
+= plane_size
) {
887 p
->planes
[i
][0] = plane
;
888 p
->planes
[NUM_STORED
+ i
][0] = plane
+ los
;
890 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
892 memset(plane
, 0x80, NUM_STORED
* 2 * chroma_plane_size
);
893 for (i
= 0; i
!= NUM_STORED
; i
++) {
894 p
->planes
[i
][1] = plane
;
895 p
->planes
[NUM_STORED
+ i
][1] = plane
+ cos
;
896 plane
+= chroma_plane_size
;
897 p
->planes
[i
][2] = plane
;
898 p
->planes
[NUM_STORED
+ i
][2] = plane
+ cos
;
899 plane
+= chroma_plane_size
;
903 i
= gcd(p
->in_inc
, p
->out_dec
);
910 static inline double get_time(void)
913 gettimeofday(&tv
, 0);
914 return tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
917 static void get_image(struct vf_instance
* vf
, mp_image_t
*mpi
)
919 struct vf_priv_s
*p
= vf
->priv
;
920 static unsigned char **planes
, planes_idx
;
922 if (mpi
->type
== MP_IMGTYPE_STATIC
) return;
924 if (!p
->planes
[0][0]) init(p
, mpi
);
926 if (mpi
->type
== MP_IMGTYPE_TEMP
||
927 (mpi
->type
== MP_IMGTYPE_IPB
&& !(mpi
->flags
& MP_IMGFLAG_READABLE
)))
928 planes_idx
= NUM_STORED
/2 + (++p
->temp_idx
% (NUM_STORED
/2));
930 planes_idx
= ++p
->static_idx
% (NUM_STORED
/2);
931 planes
= p
->planes
[planes_idx
];
932 mpi
->priv
= p
->planes
[NUM_STORED
+ planes_idx
];
933 if (mpi
->priv
== p
->old_planes
) {
934 unsigned char **old_planes
=
935 p
->planes
[NUM_STORED
+ 2 + (++p
->temp_idx
& 1)];
936 my_memcpy_pic(old_planes
[0], p
->old_planes
[0],
937 p
->w
, p
->h
, p
->stride
, p
->stride
);
938 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
939 my_memcpy_pic(old_planes
[1], p
->old_planes
[1],
940 p
->cw
, p
->ch
, p
->chroma_stride
, p
->chroma_stride
);
941 my_memcpy_pic(old_planes
[2], p
->old_planes
[2],
942 p
->cw
, p
->ch
, p
->chroma_stride
, p
->chroma_stride
);
944 p
->old_planes
= old_planes
;
947 mpi
->planes
[0] = planes
[0];
948 mpi
->stride
[0] = p
->stride
;
949 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
950 mpi
->planes
[1] = planes
[1];
951 mpi
->planes
[2] = planes
[2];
952 mpi
->stride
[1] = mpi
->stride
[2] = p
->chroma_stride
;
954 mpi
->width
= p
->stride
;
956 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
957 mpi
->flags
&= ~MP_IMGFLAG_DRAW_CALLBACK
;
961 cmpe(unsigned long x
, unsigned long y
, unsigned long err
, unsigned long e
)
964 long unit
= ((x
+y
+err
) >> e
);
965 long ret
= (diff
> unit
) - (diff
< -unit
);
967 return ret
+ (diff
> unit
) - (diff
< -unit
);
971 find_breaks(struct vf_priv_s
*p
, struct frame_stats
*s
)
973 struct frame_stats
*ps
= &p
->stats
[(p
->inframes
-1) & 1];
974 long notfilm
= 5*p
->in_inc
- p
->out_dec
;
975 unsigned long n
= s
->num_blocks
>> 8;
976 unsigned long sad_comb_cmp
= cmpe(s
->sad
.temp
, s
->sad
.noise
, 512, 1);
977 unsigned long ret
= 8;
979 if (cmpe(s
->sad
.temp
, s
->sad
.even
, 512, 1) > 0)
980 mp_msg(MSGT_VFILTER
, MSGL_WARN
,
981 "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
982 if (s
->sad
.temp
> 1000 && s
->sad
.noise
> 1000)
984 if (s
->interlaced_high
>= 2*n
&& s
->sad
.temp
> 256 && s
->sad
.noise
> 256)
986 if (s
->high
.noise
> s
->num_blocks
/4 && s
->sad
.noise
> 10000 &&
987 s
->sad
.noise
> 2*s
->sad
.even
&& s
->sad
.noise
> 2*ps
->sad
.odd
) {
988 // Mid-frame scene change
989 if (s
->tiny
.temp
+ s
->interlaced_low
< n
||
990 s
->low
.temp
+ s
->interlaced_high
< n
/4 ||
991 s
->high
.temp
+ s
->interlaced_high
< n
/8 ||
996 if (s
->high
.temp
> s
->num_blocks
/4 && s
->sad
.temp
> 10000 &&
997 s
->sad
.temp
> 2*ps
->sad
.odd
&& s
->sad
.temp
> 2*ps
->sad
.even
) {
998 // Start frame scene change
999 if (s
->tiny
.noise
+ s
->interlaced_low
< n
||
1000 s
->low
.noise
+ s
->interlaced_high
< n
/4 ||
1001 s
->high
.noise
+ s
->interlaced_high
< n
/8 ||
1006 if (sad_comb_cmp
== 2)
1008 if (sad_comb_cmp
== -2)
1011 if (s
->tiny
.odd
> 3*MAX(n
,s
->tiny
.even
) + s
->interlaced_low
)
1013 if (s
->tiny
.even
> 3*MAX(n
,s
->tiny
.odd
)+s
->interlaced_low
&&
1014 (!sad_comb_cmp
|| (s
->low
.noise
<= n
/4 && s
->low
.temp
<= n
/4)))
1017 if (s
->sad
.noise
< 64 && s
->sad
.temp
< 64 &&
1018 s
->low
.noise
<= n
/2 && s
->high
.noise
<= n
/4 &&
1019 s
->low
.temp
<= n
/2 && s
->high
.temp
<= n
/4)
1022 if (s
->tiny
.temp
> 3*MAX(n
,s
->tiny
.noise
) + s
->interlaced_low
)
1024 if (s
->tiny
.noise
> 3*MAX(n
,s
->tiny
.temp
) + s
->interlaced_low
)
1027 if (s
->low
.odd
> 3*MAX(n
/4,s
->low
.even
) + s
->interlaced_high
)
1029 if (s
->low
.even
> 3*MAX(n
/4,s
->low
.odd
)+s
->interlaced_high
&&
1030 s
->sad
.even
> 2*s
->sad
.odd
&&
1031 (!sad_comb_cmp
|| (s
->low
.noise
<= n
/4 && s
->low
.temp
<= n
/4)))
1034 if (s
->low
.temp
> 3*MAX(n
/4,s
->low
.noise
) + s
->interlaced_high
)
1036 if (s
->low
.noise
> 3*MAX(n
/4,s
->low
.temp
) + s
->interlaced_high
)
1039 if (sad_comb_cmp
== 1 && s
->sad
.noise
< 64)
1041 if (sad_comb_cmp
== -1 && s
->sad
.temp
< 64)
1044 if (s
->tiny
.odd
<= n
|| (s
->tiny
.noise
<= n
/2 && s
->tiny
.temp
<= n
/2)) {
1045 if (s
->interlaced_low
<= n
) {
1046 if (p
->num_fields
== 1)
1048 if (s
->tiny
.even
<= n
|| ps
->tiny
.noise
<= n
/2)
1051 if (s
->bigger
.even
>= 2*MAX(n
,s
->bigger
.odd
) + s
->interlaced_low
)
1053 if (s
->low
.even
>= 2*n
+ s
->interlaced_low
)
1058 if (s
->low
.odd
<= n
/4) {
1059 if (s
->interlaced_high
<= n
/4) {
1060 if (p
->num_fields
== 1)
1062 if (s
->low
.even
<= n
/4)
1065 if (s
->bigger
.even
>= 2*MAX(n
/4,s
->bigger
.odd
)+s
->interlaced_high
)
1067 if (s
->low
.even
>= n
/2 + s
->interlaced_high
)
1072 if (s
->bigger
.temp
> 2*MAX(n
,s
->bigger
.noise
) + s
->interlaced_low
)
1074 if (s
->bigger
.noise
> 2*MAX(n
,s
->bigger
.temp
) + s
->interlaced_low
)
1076 if (s
->bigger
.temp
> 2*MAX(n
,s
->bigger
.noise
) + s
->interlaced_high
)
1078 if (s
->bigger
.noise
> 2*MAX(n
,s
->bigger
.temp
) + s
->interlaced_high
)
1080 if (s
->twox
.temp
> 2*MAX(n
,s
->twox
.noise
) + s
->interlaced_high
)
1082 if (s
->twox
.noise
> 2*MAX(n
,s
->twox
.temp
) + s
->interlaced_high
)
1084 if (s
->bigger
.even
> 2*MAX(n
,s
->bigger
.odd
) + s
->interlaced_low
&&
1085 s
->bigger
.temp
< n
&& s
->bigger
.noise
< n
)
1087 if (s
->interlaced_low
> MIN(2*n
, s
->tiny
.odd
))
1089 ret
= 8 + (1 << (s
->sad
.temp
> s
->sad
.noise
));
1091 if (p
->num_fields
== 1 && p
->prev_fields
== 3 && notfilm
>= 0 &&
1092 (s
->tiny
.temp
<= s
->tiny
.noise
|| s
->sad
.temp
< s
->sad
.noise
+16))
1094 if (p
->notout
< p
->num_fields
&& p
->iosync
> 2*p
->in_inc
&& notfilm
< 0)
1096 if (p
->num_fields
< 2 ||
1097 (p
->num_fields
== 2 && p
->prev_fields
== 2 && notfilm
< 0))
1099 if (!notfilm
&& (p
->prev_fields
&~1) == 2) {
1100 if (p
->prev_fields
+ p
->num_fields
== 5) {
1101 if (s
->tiny
.noise
<= s
->tiny
.temp
||
1102 s
->low
.noise
== 0 || s
->low
.noise
< s
->low
.temp
||
1103 s
->sad
.noise
< s
->sad
.temp
+16)
1106 if (p
->prev_fields
+ p
->num_fields
== 4) {
1107 if (s
->tiny
.temp
<= s
->tiny
.noise
||
1108 s
->low
.temp
== 0 || s
->low
.temp
< s
->low
.noise
||
1109 s
->sad
.temp
< s
->sad
.noise
+16)
1113 if (p
->num_fields
> 2 &&
1114 ps
->sad
.noise
> s
->sad
.noise
&& ps
->sad
.noise
> s
->sad
.temp
)
1116 return 2 >> (s
->sad
.noise
> s
->sad
.temp
);
1119 #define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
1121 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
)
1124 struct vf_priv_s
*p
= vf
->priv
;
1125 unsigned char **planes
, **old_planes
;
1126 struct frame_stats
*s
= &p
->stats
[p
->inframes
& 1];
1127 struct frame_stats
*ps
= &p
->stats
[(p
->inframes
-1) & 1];
1129 const int flags
= mpi
->fields
;
1131 int show_fields
= 0;
1132 int dropped_fields
= 0;
1133 double start_time
, diff_time
;
1134 char prev_chflag
= p
->chflag
;
1137 if (!p
->planes
[0][0]) init(p
, mpi
);
1139 old_planes
= p
->old_planes
;
1141 if ((mpi
->flags
& MP_IMGFLAG_DIRECT
) && mpi
->priv
) {
1145 planes
= p
->planes
[2 + (++p
->temp_idx
& 1)];
1146 my_memcpy_pic(planes
[0],
1147 mpi
->planes
[0] + p
->crop_x
+ p
->crop_y
* mpi
->stride
[0],
1148 p
->w
, p
->h
, p
->stride
, mpi
->stride
[0]);
1149 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
1150 my_memcpy_pic(planes
[1],
1151 mpi
->planes
[1] + p
->crop_cx
+ p
->crop_cy
* mpi
->stride
[1],
1152 p
->cw
, p
->ch
, p
->chroma_stride
, mpi
->stride
[1]);
1153 my_memcpy_pic(planes
[2],
1154 mpi
->planes
[2] + p
->crop_cx
+ p
->crop_cy
* mpi
->stride
[2],
1155 p
->cw
, p
->ch
, p
->chroma_stride
, mpi
->stride
[2]);
1160 p
->old_planes
= planes
;
1162 if (flags
& MP_IMGFIELD_ORDERED
) {
1163 swapped
= !(flags
& MP_IMGFIELD_TOP_FIRST
);
1164 p
->chflag
= (flags
& MP_IMGFIELD_REPEAT_FIRST
? '|' :
1165 flags
& MP_IMGFIELD_TOP_FIRST
? ':' : '.');
1167 p
->swapped
= swapped
;
1169 start_time
= get_time();
1170 if (p
->chflag
== '|') {
1172 p
->iosync
+= p
->in_inc
;
1173 } else if ((p
->fast
& 1) && prev_chflag
== '|')
1176 diff_fields(p
, s
, old_planes
, planes
);
1177 diff_time
= get_time();
1178 p
->diff_time
+= diff_time
- start_time
;
1179 breaks
= p
->inframes
? find_breaks(p
, s
) : 2;
1181 keep_rate
= 4*p
->in_inc
== p
->out_dec
;
1188 if (!keep_rate
&& p
->notout
< p
->num_fields
&& p
->iosync
< 2*p
->in_inc
)
1190 if (p
->notout
< p
->num_fields
)
1191 dropped_fields
= -2;
1193 if (keep_rate
|| p
->iosync
>= -2*p
->in_inc
)
1194 show_fields
= (4<<p
->num_fields
)-1;
1199 else if (p
->iosync
> 0) {
1200 if (p
->notout
>= p
->num_fields
&& p
->iosync
> 2*p
->in_inc
) {
1201 show_fields
= 4; /* prev odd only */
1202 if (p
->num_fields
> 1)
1203 show_fields
|= 8; /* + prev even */
1205 show_fields
= 2; /* even only */
1206 if (p
->notout
>= p
->num_fields
)
1207 dropped_fields
+= p
->num_fields
;
1212 if (p
->iosync
<= -3*p
->in_inc
) {
1213 if (p
->notout
>= p
->num_fields
)
1214 dropped_fields
= p
->num_fields
;
1217 if (p
->num_fields
== 1) {
1218 int prevbreak
= ps
->sad
.noise
>= 128;
1219 if (p
->iosync
< 4*p
->in_inc
) {
1221 dropped_fields
= prevbreak
;
1223 show_fields
= 4 | (!prevbreak
<< 3);
1224 if (p
->notout
< 1 + p
->prev_fields
)
1225 dropped_fields
= -!prevbreak
;
1231 show_fields
= 3 << (breaks
& 1);
1232 else if (p
->notout
>= p
->num_fields
&&
1233 p
->iosync
>= (breaks
== 1 ? -p
->in_inc
:
1234 p
->in_inc
<< (p
->num_fields
== 1))) {
1235 show_fields
= (1 << (2 + p
->num_fields
)) - (1<<breaks
);
1237 if (p
->notout
>= p
->num_fields
)
1238 dropped_fields
+= p
->num_fields
+ 2 - breaks
;
1240 if (p
->iosync
>= 4*p
->in_inc
)
1242 } else if (p
->iosync
> -3*p
->in_inc
)
1243 show_fields
= 3; /* odd+even */
1249 prev
= p
->prev_fields
;
1251 if (p
->num_fields
== 1)
1256 p
->prev_fields
= p
->num_fields
= 1;
1258 p
->prev_fields
= p
->num_fields
+ (breaks
==1) - (breaks
==4);
1259 p
->num_fields
= breaks
- (breaks
== 4) + (p
->chflag
== '|');
1265 p
->iosync
+= 4 * p
->in_inc
;
1266 if (p
->chflag
== '|')
1267 p
->iosync
+= p
->in_inc
;
1270 p
->iosync
-= p
->out_dec
;
1271 p
->notout
= !(show_fields
& 1) + !(show_fields
& 3);
1272 if (((show_fields
& 3) == 3 &&
1273 (s
->low
.noise
+ s
->interlaced_low
< (s
->num_blocks
>>8) ||
1274 s
->sad
.noise
< 160)) ||
1275 ((show_fields
& 12) == 12 &&
1276 (ps
->low
.noise
+ ps
->interlaced_low
< (s
->num_blocks
>>8) ||
1277 ps
->sad
.noise
< 160))) {
1279 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
, MP_IMGTYPE_EXPORT
,
1280 MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
,
1282 if ((show_fields
& 3) != 3) planes
= old_planes
;
1283 dmpi
->planes
[0] = planes
[0];
1284 dmpi
->stride
[0] = p
->stride
;
1285 dmpi
->width
= mpi
->width
;
1286 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
1287 dmpi
->planes
[1] = planes
[1];
1288 dmpi
->planes
[2] = planes
[2];
1289 dmpi
->stride
[1] = p
->chroma_stride
;
1290 dmpi
->stride
[2] = p
->chroma_stride
;
1294 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
1295 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
1297 copy_merge_fields(p
, dmpi
, old_planes
, planes
, show_fields
);
1304 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
1305 p
->inframes
, p
->outframes
,
1306 breaks
, breaks
<8 && breaks
>0 ? (int) p
->prev_fields
+'0' : ' ',
1308 p
->num_breaks
, 5*p
->in_inc
== p
->out_dec
&& breaks
<8 &&
1309 breaks
>0 && ((prev
&~1)!=2 || prev
+p
->prev_fields
!=5) ?
1310 " ######## bad telecine ########" : "",
1311 dropped_fields
? " ======== dropped ":"", ITOC(dropped_fields
),
1312 !show_fields
|| (show_fields
& (show_fields
-1)) ?
1313 "" : " @@@@@@@@@@@@@@@@@");
1315 p
->merge_time
+= get_time() - diff_time
;
1316 return show_fields
? vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
) : 0;
1319 static int query_format(struct vf_instance
* vf
, unsigned int fmt
)
1321 /* FIXME - support more formats */
1329 return vf_next_query_format(vf
, fmt
);
1334 static int config(struct vf_instance
* vf
,
1335 int width
, int height
, int d_width
, int d_height
,
1336 unsigned int flags
, unsigned int outfmt
)
1338 struct MPOpts
*opts
= vf
->opts
;
1339 unsigned long cxm
= 0;
1340 unsigned long cym
= 0;
1341 struct vf_priv_s
*p
= vf
->priv
;
1343 if(!IMGFMT_IS_RGB(outfmt
) && !IMGFMT_IS_BGR(outfmt
)){
1363 p
->chroma_swapped
= !!(p
->crop_y
& (cym
+1));
1364 if (p
->w
) p
->w
+= p
->crop_x
& cxm
;
1365 if (p
->h
) p
->h
+= p
->crop_y
& cym
;
1368 if (!p
->w
|| p
->w
> width
) p
->w
= width
;
1369 if (!p
->h
|| p
->h
> height
) p
->h
= height
;
1370 if (p
->crop_x
+ p
->w
> width
) p
->crop_x
= 0;
1371 if (p
->crop_y
+ p
->h
> height
) p
->crop_y
= 0;
1373 if(!opts
->screen_size_x
&& !opts
->screen_size_y
){
1374 d_width
= d_width
* p
->w
/width
;
1375 d_height
= d_height
* p
->h
/height
;
1377 return vf_next_config(vf
, p
->w
, p
->h
, d_width
, d_height
, flags
, outfmt
);
1380 static void uninit(struct vf_instance
* vf
)
1382 struct vf_priv_s
*p
= vf
->priv
;
1383 mp_msg(MSGT_VFILTER
, MSGL_INFO
, "diff_time: %.3f, merge_time: %.3f, "
1384 "export: %lu, merge: %lu, copy: %lu\n", p
->diff_time
, p
->merge_time
,
1385 p
->export_count
, p
->merge_count
, p
->num_copies
);
1386 free(p
->memory_allocated
);
1390 static int open(vf_instance_t
*vf
, char* args
)
1392 struct vf_priv_s
*p
;
1393 vf
->get_image
= get_image
;
1394 vf
->put_image
= put_image
;
1395 vf
->config
= config
;
1396 vf
->query_format
= query_format
;
1397 vf
->uninit
= uninit
;
1398 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
1399 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
1402 p
->thres
.noise
= 128;
1403 p
->thres
.even
= 128;
1408 p
->mmx2
= gCpuCaps
.hasMMX2
? 1 : gCpuCaps
.has3DNow
? 2 : 0;
1410 const char *args_remain
= parse_args(p
, args
);
1412 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
1413 "filmdint: unknown suboption: %s\n", args_remain
);
1416 if (p
->out_dec
< p
->in_inc
) {
1417 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
1418 "filmdint: increasing the frame rate is not supported\n");
1430 p
->thres
.odd
= p
->thres
.even
;
1431 p
->thres
.temp
= p
->thres
.noise
;
1437 const vf_info_t vf_info_filmdint
= {
1438 "Advanced inverse telecine filer",