ad8d7c0099f99892dee28935166055c858d71a0e
[libav.git] / libpostproc / postprocess_template.c
1 /*
2 * Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file libpostproc/postprocess_template.c
23 * mmx/mmx2/3dnow postprocess code.
24 */
25
26 #include "libavutil/x86_cpu.h"
27
28 #define ALIGN_MASK "$-8"
29
30 #undef PAVGB
31 #undef PMINUB
32 #undef PMAXUB
33
34 #if HAVE_MMX2
35 #define REAL_PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
36 #elif HAVE_AMD3DNOW
37 #define REAL_PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
38 #endif
39 #define PAVGB(a,b) REAL_PAVGB(a,b)
40
41 #if HAVE_MMX2
42 #define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
43 #elif HAVE_MMX
44 #define PMINUB(b,a,t) \
45 "movq " #a ", " #t " \n\t"\
46 "psubusb " #b ", " #t " \n\t"\
47 "psubb " #t ", " #a " \n\t"
48 #endif
49
50 #if HAVE_MMX2
51 #define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
52 #elif HAVE_MMX
53 #define PMAXUB(a,b) \
54 "psubusb " #a ", " #b " \n\t"\
55 "paddb " #a ", " #b " \n\t"
56 #endif
57
58 //FIXME? |255-0| = 1 (should not be a problem ...)
59 #if HAVE_MMX
60 /**
61 * Check if the middle 8x8 Block in the given 8x16 block is flat
62 */
63 static inline int RENAME(vertClassify)(uint8_t src[], int stride, PPContext *c){
64 int numEq= 0, dcOk;
65 src+= stride*4; // src points to begin of the 8x8 Block
66 __asm__ volatile(
67 "movq %0, %%mm7 \n\t"
68 "movq %1, %%mm6 \n\t"
69 : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
70 );
71
72 __asm__ volatile(
73 "lea (%2, %3), %%"REG_a" \n\t"
74 // 0 1 2 3 4 5 6 7 8 9
75 // %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
76
77 "movq (%2), %%mm0 \n\t"
78 "movq (%%"REG_a"), %%mm1 \n\t"
79 "movq %%mm0, %%mm3 \n\t"
80 "movq %%mm0, %%mm4 \n\t"
81 PMAXUB(%%mm1, %%mm4)
82 PMINUB(%%mm1, %%mm3, %%mm5)
83 "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
84 "paddb %%mm7, %%mm0 \n\t"
85 "pcmpgtb %%mm6, %%mm0 \n\t"
86
87 "movq (%%"REG_a",%3), %%mm2 \n\t"
88 PMAXUB(%%mm2, %%mm4)
89 PMINUB(%%mm2, %%mm3, %%mm5)
90 "psubb %%mm2, %%mm1 \n\t"
91 "paddb %%mm7, %%mm1 \n\t"
92 "pcmpgtb %%mm6, %%mm1 \n\t"
93 "paddb %%mm1, %%mm0 \n\t"
94
95 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
96 PMAXUB(%%mm1, %%mm4)
97 PMINUB(%%mm1, %%mm3, %%mm5)
98 "psubb %%mm1, %%mm2 \n\t"
99 "paddb %%mm7, %%mm2 \n\t"
100 "pcmpgtb %%mm6, %%mm2 \n\t"
101 "paddb %%mm2, %%mm0 \n\t"
102
103 "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
104
105 "movq (%2, %3, 4), %%mm2 \n\t"
106 PMAXUB(%%mm2, %%mm4)
107 PMINUB(%%mm2, %%mm3, %%mm5)
108 "psubb %%mm2, %%mm1 \n\t"
109 "paddb %%mm7, %%mm1 \n\t"
110 "pcmpgtb %%mm6, %%mm1 \n\t"
111 "paddb %%mm1, %%mm0 \n\t"
112
113 "movq (%%"REG_a"), %%mm1 \n\t"
114 PMAXUB(%%mm1, %%mm4)
115 PMINUB(%%mm1, %%mm3, %%mm5)
116 "psubb %%mm1, %%mm2 \n\t"
117 "paddb %%mm7, %%mm2 \n\t"
118 "pcmpgtb %%mm6, %%mm2 \n\t"
119 "paddb %%mm2, %%mm0 \n\t"
120
121 "movq (%%"REG_a", %3), %%mm2 \n\t"
122 PMAXUB(%%mm2, %%mm4)
123 PMINUB(%%mm2, %%mm3, %%mm5)
124 "psubb %%mm2, %%mm1 \n\t"
125 "paddb %%mm7, %%mm1 \n\t"
126 "pcmpgtb %%mm6, %%mm1 \n\t"
127 "paddb %%mm1, %%mm0 \n\t"
128
129 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
130 PMAXUB(%%mm1, %%mm4)
131 PMINUB(%%mm1, %%mm3, %%mm5)
132 "psubb %%mm1, %%mm2 \n\t"
133 "paddb %%mm7, %%mm2 \n\t"
134 "pcmpgtb %%mm6, %%mm2 \n\t"
135 "paddb %%mm2, %%mm0 \n\t"
136 "psubusb %%mm3, %%mm4 \n\t"
137
138 " \n\t"
139 #if HAVE_MMX2
140 "pxor %%mm7, %%mm7 \n\t"
141 "psadbw %%mm7, %%mm0 \n\t"
142 #else
143 "movq %%mm0, %%mm1 \n\t"
144 "psrlw $8, %%mm0 \n\t"
145 "paddb %%mm1, %%mm0 \n\t"
146 "movq %%mm0, %%mm1 \n\t"
147 "psrlq $16, %%mm0 \n\t"
148 "paddb %%mm1, %%mm0 \n\t"
149 "movq %%mm0, %%mm1 \n\t"
150 "psrlq $32, %%mm0 \n\t"
151 "paddb %%mm1, %%mm0 \n\t"
152 #endif
153 "movq %4, %%mm7 \n\t" // QP,..., QP
154 "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
155 "psubusb %%mm7, %%mm4 \n\t" // Diff <= 2QP -> 0
156 "packssdw %%mm4, %%mm4 \n\t"
157 "movd %%mm0, %0 \n\t"
158 "movd %%mm4, %1 \n\t"
159
160 : "=r" (numEq), "=r" (dcOk)
161 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
162 : "%"REG_a
163 );
164
165 numEq= (-numEq) &0xFF;
166 if(numEq > c->ppMode.flatnessThreshold){
167 if(dcOk) return 0;
168 else return 1;
169 }else{
170 return 2;
171 }
172 }
173 #endif //HAVE_MMX
174
175 /**
176 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
177 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
178 */
179 #if !HAVE_ALTIVEC
180 static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
181 {
182 #if HAVE_MMX2 || HAVE_AMD3DNOW
183 src+= stride*3;
184 __asm__ volatile( //"movv %0 %1 %2\n\t"
185 "movq %2, %%mm0 \n\t" // QP,..., QP
186 "pxor %%mm4, %%mm4 \n\t"
187
188 "movq (%0), %%mm6 \n\t"
189 "movq (%0, %1), %%mm5 \n\t"
190 "movq %%mm5, %%mm1 \n\t"
191 "movq %%mm6, %%mm2 \n\t"
192 "psubusb %%mm6, %%mm5 \n\t"
193 "psubusb %%mm1, %%mm2 \n\t"
194 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
195 "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
196 "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
197
198 "pand %%mm2, %%mm6 \n\t"
199 "pandn %%mm1, %%mm2 \n\t"
200 "por %%mm2, %%mm6 \n\t"// First Line to Filter
201
202 "movq (%0, %1, 8), %%mm5 \n\t"
203 "lea (%0, %1, 4), %%"REG_a" \n\t"
204 "lea (%0, %1, 8), %%"REG_c" \n\t"
205 "sub %1, %%"REG_c" \n\t"
206 "add %1, %0 \n\t" // %0 points to line 1 not 0
207 "movq (%0, %1, 8), %%mm7 \n\t"
208 "movq %%mm5, %%mm1 \n\t"
209 "movq %%mm7, %%mm2 \n\t"
210 "psubusb %%mm7, %%mm5 \n\t"
211 "psubusb %%mm1, %%mm2 \n\t"
212 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
213 "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
214 "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
215
216 "pand %%mm2, %%mm7 \n\t"
217 "pandn %%mm1, %%mm2 \n\t"
218 "por %%mm2, %%mm7 \n\t" // First Line to Filter
219
220
221 // 1 2 3 4 5 6 7 8
222 // %0 %0+%1 %0+2%1 eax %0+4%1 eax+2%1 ecx eax+4%1
223 // 6 4 2 2 1 1
224 // 6 4 4 2
225 // 6 8 2
226
227 "movq (%0, %1), %%mm0 \n\t" // 1
228 "movq %%mm0, %%mm1 \n\t" // 1
229 PAVGB(%%mm6, %%mm0) //1 1 /2
230 PAVGB(%%mm6, %%mm0) //3 1 /4
231
232 "movq (%0, %1, 4), %%mm2 \n\t" // 1
233 "movq %%mm2, %%mm5 \n\t" // 1
234 PAVGB((%%REGa), %%mm2) // 11 /2
235 PAVGB((%0, %1, 2), %%mm2) // 211 /4
236 "movq %%mm2, %%mm3 \n\t" // 211 /4
237 "movq (%0), %%mm4 \n\t" // 1
238 PAVGB(%%mm4, %%mm3) // 4 211 /8
239 PAVGB(%%mm0, %%mm3) //642211 /16
240 "movq %%mm3, (%0) \n\t" // X
241 // mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
242 "movq %%mm1, %%mm0 \n\t" // 1
243 PAVGB(%%mm6, %%mm0) //1 1 /2
244 "movq %%mm4, %%mm3 \n\t" // 1
245 PAVGB((%0,%1,2), %%mm3) // 1 1 /2
246 PAVGB((%%REGa,%1,2), %%mm5) // 11 /2
247 PAVGB((%%REGa), %%mm5) // 211 /4
248 PAVGB(%%mm5, %%mm3) // 2 2211 /8
249 PAVGB(%%mm0, %%mm3) //4242211 /16
250 "movq %%mm3, (%0,%1) \n\t" // X
251 // mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
252 PAVGB(%%mm4, %%mm6) //11 /2
253 "movq (%%"REG_c"), %%mm0 \n\t" // 1
254 PAVGB((%%REGa, %1, 2), %%mm0) // 11/2
255 "movq %%mm0, %%mm3 \n\t" // 11/2
256 PAVGB(%%mm1, %%mm0) // 2 11/4
257 PAVGB(%%mm6, %%mm0) //222 11/8
258 PAVGB(%%mm2, %%mm0) //22242211/16
259 "movq (%0, %1, 2), %%mm2 \n\t" // 1
260 "movq %%mm0, (%0, %1, 2) \n\t" // X
261 // mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
262 "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
263 PAVGB((%%REGc), %%mm0) // 11 /2
264 PAVGB(%%mm0, %%mm6) //11 11 /4
265 PAVGB(%%mm1, %%mm4) // 11 /2
266 PAVGB(%%mm2, %%mm1) // 11 /2
267 PAVGB(%%mm1, %%mm6) //1122 11 /8
268 PAVGB(%%mm5, %%mm6) //112242211 /16
269 "movq (%%"REG_a"), %%mm5 \n\t" // 1
270 "movq %%mm6, (%%"REG_a") \n\t" // X
271 // mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
272 "movq (%%"REG_a", %1, 4), %%mm6 \n\t" // 1
273 PAVGB(%%mm7, %%mm6) // 11 /2
274 PAVGB(%%mm4, %%mm6) // 11 11 /4
275 PAVGB(%%mm3, %%mm6) // 11 2211 /8
276 PAVGB(%%mm5, %%mm2) // 11 /2
277 "movq (%0, %1, 4), %%mm4 \n\t" // 1
278 PAVGB(%%mm4, %%mm2) // 112 /4
279 PAVGB(%%mm2, %%mm6) // 112242211 /16
280 "movq %%mm6, (%0, %1, 4) \n\t" // X
281 // mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
282 PAVGB(%%mm7, %%mm1) // 11 2 /4
283 PAVGB(%%mm4, %%mm5) // 11 /2
284 PAVGB(%%mm5, %%mm0) // 11 11 /4
285 "movq (%%"REG_a", %1, 2), %%mm6 \n\t" // 1
286 PAVGB(%%mm6, %%mm1) // 11 4 2 /8
287 PAVGB(%%mm0, %%mm1) // 11224222 /16
288 "movq %%mm1, (%%"REG_a", %1, 2) \n\t" // X
289 // mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
290 PAVGB((%%REGc), %%mm2) // 112 4 /8
291 "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
292 PAVGB(%%mm0, %%mm6) // 1 1 /2
293 PAVGB(%%mm7, %%mm6) // 1 12 /4
294 PAVGB(%%mm2, %%mm6) // 1122424 /4
295 "movq %%mm6, (%%"REG_c") \n\t" // X
296 // mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
297 PAVGB(%%mm7, %%mm5) // 11 2 /4
298 PAVGB(%%mm7, %%mm5) // 11 6 /8
299
300 PAVGB(%%mm3, %%mm0) // 112 /4
301 PAVGB(%%mm0, %%mm5) // 112246 /16
302 "movq %%mm5, (%%"REG_a", %1, 4) \n\t" // X
303 "sub %1, %0 \n\t"
304
305 :
306 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
307 : "%"REG_a, "%"REG_c
308 );
309 #else //HAVE_MMX2 || HAVE_AMD3DNOW
310 const int l1= stride;
311 const int l2= stride + l1;
312 const int l3= stride + l2;
313 const int l4= stride + l3;
314 const int l5= stride + l4;
315 const int l6= stride + l5;
316 const int l7= stride + l6;
317 const int l8= stride + l7;
318 const int l9= stride + l8;
319 int x;
320 src+= stride*3;
321 for(x=0; x<BLOCK_SIZE; x++){
322 const int first= FFABS(src[0] - src[l1]) < c->QP ? src[0] : src[l1];
323 const int last= FFABS(src[l8] - src[l9]) < c->QP ? src[l9] : src[l8];
324
325 int sums[10];
326 sums[0] = 4*first + src[l1] + src[l2] + src[l3] + 4;
327 sums[1] = sums[0] - first + src[l4];
328 sums[2] = sums[1] - first + src[l5];
329 sums[3] = sums[2] - first + src[l6];
330 sums[4] = sums[3] - first + src[l7];
331 sums[5] = sums[4] - src[l1] + src[l8];
332 sums[6] = sums[5] - src[l2] + last;
333 sums[7] = sums[6] - src[l3] + last;
334 sums[8] = sums[7] - src[l4] + last;
335 sums[9] = sums[8] - src[l5] + last;
336
337 src[l1]= (sums[0] + sums[2] + 2*src[l1])>>4;
338 src[l2]= (sums[1] + sums[3] + 2*src[l2])>>4;
339 src[l3]= (sums[2] + sums[4] + 2*src[l3])>>4;
340 src[l4]= (sums[3] + sums[5] + 2*src[l4])>>4;
341 src[l5]= (sums[4] + sums[6] + 2*src[l5])>>4;
342 src[l6]= (sums[5] + sums[7] + 2*src[l6])>>4;
343 src[l7]= (sums[6] + sums[8] + 2*src[l7])>>4;
344 src[l8]= (sums[7] + sums[9] + 2*src[l8])>>4;
345
346 src++;
347 }
348 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
349 }
350 #endif //HAVE_ALTIVEC
351
352 /**
353 * Experimental Filter 1
354 * will not damage linear gradients
355 * Flat blocks should look like they were passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
356 * can only smooth blocks at the expected locations (it cannot smooth them if they did move)
357 * MMX2 version does correct clipping C version does not
358 */
359 static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
360 {
361 #if HAVE_MMX2 || HAVE_AMD3DNOW
362 src+= stride*3;
363
364 __asm__ volatile(
365 "pxor %%mm7, %%mm7 \n\t" // 0
366 "lea (%0, %1), %%"REG_a" \n\t"
367 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
368 // 0 1 2 3 4 5 6 7 8 9
369 // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
370 "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
371 "movq (%0, %1, 4), %%mm1 \n\t" // line 4
372 "movq %%mm1, %%mm2 \n\t" // line 4
373 "psubusb %%mm0, %%mm1 \n\t"
374 "psubusb %%mm2, %%mm0 \n\t"
375 "por %%mm1, %%mm0 \n\t" // |l2 - l3|
376 "movq (%%"REG_c"), %%mm3 \n\t" // line 5
377 "movq (%%"REG_c", %1), %%mm4 \n\t" // line 6
378 "movq %%mm3, %%mm5 \n\t" // line 5
379 "psubusb %%mm4, %%mm3 \n\t"
380 "psubusb %%mm5, %%mm4 \n\t"
381 "por %%mm4, %%mm3 \n\t" // |l5 - l6|
382 PAVGB(%%mm3, %%mm0) // (|l2 - l3| + |l5 - l6|)/2
383 "movq %%mm2, %%mm1 \n\t" // line 4
384 "psubusb %%mm5, %%mm2 \n\t"
385 "movq %%mm2, %%mm4 \n\t"
386 "pcmpeqb %%mm7, %%mm2 \n\t" // (l4 - l5) <= 0 ? -1 : 0
387 "psubusb %%mm1, %%mm5 \n\t"
388 "por %%mm5, %%mm4 \n\t" // |l4 - l5|
389 "psubusb %%mm0, %%mm4 \n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
390 "movq %%mm4, %%mm3 \n\t" // d
391 "movq %2, %%mm0 \n\t"
392 "paddusb %%mm0, %%mm0 \n\t"
393 "psubusb %%mm0, %%mm4 \n\t"
394 "pcmpeqb %%mm7, %%mm4 \n\t" // d <= QP ? -1 : 0
395 "psubusb "MANGLE(b01)", %%mm3 \n\t"
396 "pand %%mm4, %%mm3 \n\t" // d <= QP ? d : 0
397
398 PAVGB(%%mm7, %%mm3) // d/2
399 "movq %%mm3, %%mm1 \n\t" // d/2
400 PAVGB(%%mm7, %%mm3) // d/4
401 PAVGB(%%mm1, %%mm3) // 3*d/8
402
403 "movq (%0, %1, 4), %%mm0 \n\t" // line 4
404 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
405 "psubusb %%mm3, %%mm0 \n\t"
406 "pxor %%mm2, %%mm0 \n\t"
407 "movq %%mm0, (%0, %1, 4) \n\t" // line 4
408
409 "movq (%%"REG_c"), %%mm0 \n\t" // line 5
410 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
411 "paddusb %%mm3, %%mm0 \n\t"
412 "pxor %%mm2, %%mm0 \n\t"
413 "movq %%mm0, (%%"REG_c") \n\t" // line 5
414
415 PAVGB(%%mm7, %%mm1) // d/4
416
417 "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
418 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
419 "psubusb %%mm1, %%mm0 \n\t"
420 "pxor %%mm2, %%mm0 \n\t"
421 "movq %%mm0, (%%"REG_a", %1, 2) \n\t" // line 3
422
423 "movq (%%"REG_c", %1), %%mm0 \n\t" // line 6
424 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
425 "paddusb %%mm1, %%mm0 \n\t"
426 "pxor %%mm2, %%mm0 \n\t"
427 "movq %%mm0, (%%"REG_c", %1) \n\t" // line 6
428
429 PAVGB(%%mm7, %%mm1) // d/8
430
431 "movq (%%"REG_a", %1), %%mm0 \n\t" // line 2
432 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
433 "psubusb %%mm1, %%mm0 \n\t"
434 "pxor %%mm2, %%mm0 \n\t"
435 "movq %%mm0, (%%"REG_a", %1) \n\t" // line 2
436
437 "movq (%%"REG_c", %1, 2), %%mm0 \n\t" // line 7
438 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
439 "paddusb %%mm1, %%mm0 \n\t"
440 "pxor %%mm2, %%mm0 \n\t"
441 "movq %%mm0, (%%"REG_c", %1, 2) \n\t" // line 7
442
443 :
444 : "r" (src), "r" ((x86_reg)stride), "m" (co->pQPb)
445 : "%"REG_a, "%"REG_c
446 );
447 #else //HAVE_MMX2 || HAVE_AMD3DNOW
448
449 const int l1= stride;
450 const int l2= stride + l1;
451 const int l3= stride + l2;
452 const int l4= stride + l3;
453 const int l5= stride + l4;
454 const int l6= stride + l5;
455 const int l7= stride + l6;
456 // const int l8= stride + l7;
457 // const int l9= stride + l8;
458 int x;
459
460 src+= stride*3;
461 for(x=0; x<BLOCK_SIZE; x++){
462 int a= src[l3] - src[l4];
463 int b= src[l4] - src[l5];
464 int c= src[l5] - src[l6];
465
466 int d= FFABS(b) - ((FFABS(a) + FFABS(c))>>1);
467 d= FFMAX(d, 0);
468
469 if(d < co->QP*2){
470 int v = d * FFSIGN(-b);
471
472 src[l2] +=v>>3;
473 src[l3] +=v>>2;
474 src[l4] +=(3*v)>>3;
475 src[l5] -=(3*v)>>3;
476 src[l6] -=v>>2;
477 src[l7] -=v>>3;
478 }
479 src++;
480 }
481 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
482 }
483
484 #if !HAVE_ALTIVEC
485 static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
486 {
487 #if HAVE_MMX2 || HAVE_AMD3DNOW
488 /*
489 uint8_t tmp[16];
490 const int l1= stride;
491 const int l2= stride + l1;
492 const int l3= stride + l2;
493 const int l4= (int)tmp - (int)src - stride*3;
494 const int l5= (int)tmp - (int)src - stride*3 + 8;
495 const int l6= stride*3 + l3;
496 const int l7= stride + l6;
497 const int l8= stride + l7;
498
499 memcpy(tmp, src+stride*7, 8);
500 memcpy(tmp+8, src+stride*8, 8);
501 */
502 src+= stride*4;
503 __asm__ volatile(
504
505 #if 0 //slightly more accurate and slightly slower
506 "pxor %%mm7, %%mm7 \n\t" // 0
507 "lea (%0, %1), %%"REG_a" \n\t"
508 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
509 // 0 1 2 3 4 5 6 7
510 // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
511 // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
512
513
514 "movq (%0, %1, 2), %%mm0 \n\t" // l2
515 "movq (%0), %%mm1 \n\t" // l0
516 "movq %%mm0, %%mm2 \n\t" // l2
517 PAVGB(%%mm7, %%mm0) // ~l2/2
518 PAVGB(%%mm1, %%mm0) // ~(l2 + 2l0)/4
519 PAVGB(%%mm2, %%mm0) // ~(5l2 + 2l0)/8
520
521 "movq (%%"REG_a"), %%mm1 \n\t" // l1
522 "movq (%%"REG_a", %1, 2), %%mm3 \n\t" // l3
523 "movq %%mm1, %%mm4 \n\t" // l1
524 PAVGB(%%mm7, %%mm1) // ~l1/2
525 PAVGB(%%mm3, %%mm1) // ~(l1 + 2l3)/4
526 PAVGB(%%mm4, %%mm1) // ~(5l1 + 2l3)/8
527
528 "movq %%mm0, %%mm4 \n\t" // ~(5l2 + 2l0)/8
529 "psubusb %%mm1, %%mm0 \n\t"
530 "psubusb %%mm4, %%mm1 \n\t"
531 "por %%mm0, %%mm1 \n\t" // ~|2l0 - 5l1 + 5l2 - 2l3|/8
532 // mm1= |lenergy|, mm2= l2, mm3= l3, mm7=0
533
534 "movq (%0, %1, 4), %%mm0 \n\t" // l4
535 "movq %%mm0, %%mm4 \n\t" // l4
536 PAVGB(%%mm7, %%mm0) // ~l4/2
537 PAVGB(%%mm2, %%mm0) // ~(l4 + 2l2)/4
538 PAVGB(%%mm4, %%mm0) // ~(5l4 + 2l2)/8
539
540 "movq (%%"REG_c"), %%mm2 \n\t" // l5
541 "movq %%mm3, %%mm5 \n\t" // l3
542 PAVGB(%%mm7, %%mm3) // ~l3/2
543 PAVGB(%%mm2, %%mm3) // ~(l3 + 2l5)/4
544 PAVGB(%%mm5, %%mm3) // ~(5l3 + 2l5)/8
545
546 "movq %%mm0, %%mm6 \n\t" // ~(5l4 + 2l2)/8
547 "psubusb %%mm3, %%mm0 \n\t"
548 "psubusb %%mm6, %%mm3 \n\t"
549 "por %%mm0, %%mm3 \n\t" // ~|2l2 - 5l3 + 5l4 - 2l5|/8
550 "pcmpeqb %%mm7, %%mm0 \n\t" // SIGN(2l2 - 5l3 + 5l4 - 2l5)
551 // mm0= SIGN(menergy), mm1= |lenergy|, mm2= l5, mm3= |menergy|, mm4=l4, mm5= l3, mm7=0
552
553 "movq (%%"REG_c", %1), %%mm6 \n\t" // l6
554 "movq %%mm6, %%mm5 \n\t" // l6
555 PAVGB(%%mm7, %%mm6) // ~l6/2
556 PAVGB(%%mm4, %%mm6) // ~(l6 + 2l4)/4
557 PAVGB(%%mm5, %%mm6) // ~(5l6 + 2l4)/8
558
559 "movq (%%"REG_c", %1, 2), %%mm5 \n\t" // l7
560 "movq %%mm2, %%mm4 \n\t" // l5
561 PAVGB(%%mm7, %%mm2) // ~l5/2
562 PAVGB(%%mm5, %%mm2) // ~(l5 + 2l7)/4
563 PAVGB(%%mm4, %%mm2) // ~(5l5 + 2l7)/8
564
565 "movq %%mm6, %%mm4 \n\t" // ~(5l6 + 2l4)/8
566 "psubusb %%mm2, %%mm6 \n\t"
567 "psubusb %%mm4, %%mm2 \n\t"
568 "por %%mm6, %%mm2 \n\t" // ~|2l4 - 5l5 + 5l6 - 2l7|/8
569 // mm0= SIGN(menergy), mm1= |lenergy|/8, mm2= |renergy|/8, mm3= |menergy|/8, mm7=0
570
571
572 PMINUB(%%mm2, %%mm1, %%mm4) // MIN(|lenergy|,|renergy|)/8
573 "movq %2, %%mm4 \n\t" // QP //FIXME QP+1 ?
574 "paddusb "MANGLE(b01)", %%mm4 \n\t"
575 "pcmpgtb %%mm3, %%mm4 \n\t" // |menergy|/8 < QP
576 "psubusb %%mm1, %%mm3 \n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
577 "pand %%mm4, %%mm3 \n\t"
578
579 "movq %%mm3, %%mm1 \n\t"
580 // "psubusb "MANGLE(b01)", %%mm3 \n\t"
581 PAVGB(%%mm7, %%mm3)
582 PAVGB(%%mm7, %%mm3)
583 "paddusb %%mm1, %%mm3 \n\t"
584 // "paddusb "MANGLE(b01)", %%mm3 \n\t"
585
586 "movq (%%"REG_a", %1, 2), %%mm6 \n\t" //l3
587 "movq (%0, %1, 4), %%mm5 \n\t" //l4
588 "movq (%0, %1, 4), %%mm4 \n\t" //l4
589 "psubusb %%mm6, %%mm5 \n\t"
590 "psubusb %%mm4, %%mm6 \n\t"
591 "por %%mm6, %%mm5 \n\t" // |l3-l4|
592 "pcmpeqb %%mm7, %%mm6 \n\t" // SIGN(l3-l4)
593 "pxor %%mm6, %%mm0 \n\t"
594 "pand %%mm0, %%mm3 \n\t"
595 PMINUB(%%mm5, %%mm3, %%mm0)
596
597 "psubusb "MANGLE(b01)", %%mm3 \n\t"
598 PAVGB(%%mm7, %%mm3)
599
600 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
601 "movq (%0, %1, 4), %%mm2 \n\t"
602 "pxor %%mm6, %%mm0 \n\t"
603 "pxor %%mm6, %%mm2 \n\t"
604 "psubb %%mm3, %%mm0 \n\t"
605 "paddb %%mm3, %%mm2 \n\t"
606 "pxor %%mm6, %%mm0 \n\t"
607 "pxor %%mm6, %%mm2 \n\t"
608 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
609 "movq %%mm2, (%0, %1, 4) \n\t"
610 #endif //0
611
612 "lea (%0, %1), %%"REG_a" \n\t"
613 "pcmpeqb %%mm6, %%mm6 \n\t" // -1
614 // 0 1 2 3 4 5 6 7
615 // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
616 // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
617
618
619 "movq (%%"REG_a", %1, 2), %%mm1 \n\t" // l3
620 "movq (%0, %1, 4), %%mm0 \n\t" // l4
621 "pxor %%mm6, %%mm1 \n\t" // -l3-1
622 PAVGB(%%mm1, %%mm0) // -q+128 = (l4-l3+256)/2
623 // mm1=-l3-1, mm0=128-q
624
625 "movq (%%"REG_a", %1, 4), %%mm2 \n\t" // l5
626 "movq (%%"REG_a", %1), %%mm3 \n\t" // l2
627 "pxor %%mm6, %%mm2 \n\t" // -l5-1
628 "movq %%mm2, %%mm5 \n\t" // -l5-1
629 "movq "MANGLE(b80)", %%mm4 \n\t" // 128
630 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
631 PAVGB(%%mm3, %%mm2) // (l2-l5+256)/2
632 PAVGB(%%mm0, %%mm4) // ~(l4-l3)/4 + 128
633 PAVGB(%%mm2, %%mm4) // ~(l2-l5)/4 +(l4-l3)/8 + 128
634 PAVGB(%%mm0, %%mm4) // ~(l2-l5)/8 +5(l4-l3)/16 + 128
635 // mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
636
637 "movq (%%"REG_a"), %%mm2 \n\t" // l1
638 "pxor %%mm6, %%mm2 \n\t" // -l1-1
639 PAVGB(%%mm3, %%mm2) // (l2-l1+256)/2
640 PAVGB((%0), %%mm1) // (l0-l3+256)/2
641 "movq "MANGLE(b80)", %%mm3 \n\t" // 128
642 PAVGB(%%mm2, %%mm3) // ~(l2-l1)/4 + 128
643 PAVGB(%%mm1, %%mm3) // ~(l0-l3)/4 +(l2-l1)/8 + 128
644 PAVGB(%%mm2, %%mm3) // ~(l0-l3)/8 +5(l2-l1)/16 + 128
645 // mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
646
647 PAVGB((%%REGc, %1), %%mm5) // (l6-l5+256)/2
648 "movq (%%"REG_c", %1, 2), %%mm1 \n\t" // l7
649 "pxor %%mm6, %%mm1 \n\t" // -l7-1
650 PAVGB((%0, %1, 4), %%mm1) // (l4-l7+256)/2
651 "movq "MANGLE(b80)", %%mm2 \n\t" // 128
652 PAVGB(%%mm5, %%mm2) // ~(l6-l5)/4 + 128
653 PAVGB(%%mm1, %%mm2) // ~(l4-l7)/4 +(l6-l5)/8 + 128
654 PAVGB(%%mm5, %%mm2) // ~(l4-l7)/8 +5(l6-l5)/16 + 128
655 // mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
656
657 "movq "MANGLE(b00)", %%mm1 \n\t" // 0
658 "movq "MANGLE(b00)", %%mm5 \n\t" // 0
659 "psubb %%mm2, %%mm1 \n\t" // 128 - renergy/16
660 "psubb %%mm3, %%mm5 \n\t" // 128 - lenergy/16
661 PMAXUB(%%mm1, %%mm2) // 128 + |renergy/16|
662 PMAXUB(%%mm5, %%mm3) // 128 + |lenergy/16|
663 PMINUB(%%mm2, %%mm3, %%mm1) // 128 + MIN(|lenergy|,|renergy|)/16
664
665 // mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
666
667 "movq "MANGLE(b00)", %%mm7 \n\t" // 0
668 "movq %2, %%mm2 \n\t" // QP
669 PAVGB(%%mm6, %%mm2) // 128 + QP/2
670 "psubb %%mm6, %%mm2 \n\t"
671
672 "movq %%mm4, %%mm1 \n\t"
673 "pcmpgtb %%mm7, %%mm1 \n\t" // SIGN(menergy)
674 "pxor %%mm1, %%mm4 \n\t"
675 "psubb %%mm1, %%mm4 \n\t" // 128 + |menergy|/16
676 "pcmpgtb %%mm4, %%mm2 \n\t" // |menergy|/16 < QP/2
677 "psubusb %%mm3, %%mm4 \n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
678 // mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
679
680 "movq %%mm4, %%mm3 \n\t" // d
681 "psubusb "MANGLE(b01)", %%mm4 \n\t"
682 PAVGB(%%mm7, %%mm4) // d/32
683 PAVGB(%%mm7, %%mm4) // (d + 32)/64
684 "paddb %%mm3, %%mm4 \n\t" // 5d/64
685 "pand %%mm2, %%mm4 \n\t"
686
687 "movq "MANGLE(b80)", %%mm5 \n\t" // 128
688 "psubb %%mm0, %%mm5 \n\t" // q
689 "paddsb %%mm6, %%mm5 \n\t" // fix bad rounding
690 "pcmpgtb %%mm5, %%mm7 \n\t" // SIGN(q)
691 "pxor %%mm7, %%mm5 \n\t"
692
693 PMINUB(%%mm5, %%mm4, %%mm3) // MIN(|q|, 5d/64)
694 "pxor %%mm1, %%mm7 \n\t" // SIGN(d*q)
695
696 "pand %%mm7, %%mm4 \n\t"
697 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
698 "movq (%0, %1, 4), %%mm2 \n\t"
699 "pxor %%mm1, %%mm0 \n\t"
700 "pxor %%mm1, %%mm2 \n\t"
701 "paddb %%mm4, %%mm0 \n\t"
702 "psubb %%mm4, %%mm2 \n\t"
703 "pxor %%mm1, %%mm0 \n\t"
704 "pxor %%mm1, %%mm2 \n\t"
705 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
706 "movq %%mm2, (%0, %1, 4) \n\t"
707
708 :
709 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
710 : "%"REG_a, "%"REG_c
711 );
712
713 /*
714 {
715 int x;
716 src-= stride;
717 for(x=0; x<BLOCK_SIZE; x++){
718 const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
719 if(FFABS(middleEnergy)< 8*QP){
720 const int q=(src[l4] - src[l5])/2;
721 const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
722 const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
723
724 int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
725 d= FFMAX(d, 0);
726
727 d= (5*d + 32) >> 6;
728 d*= FFSIGN(-middleEnergy);
729
730 if(q>0){
731 d= d<0 ? 0 : d;
732 d= d>q ? q : d;
733 }else{
734 d= d>0 ? 0 : d;
735 d= d<q ? q : d;
736 }
737
738 src[l4]-= d;
739 src[l5]+= d;
740 }
741 src++;
742 }
743 src-=8;
744 for(x=0; x<8; x++){
745 int y;
746 for(y=4; y<6; y++){
747 int d= src[x+y*stride] - tmp[x+(y-4)*8];
748 int ad= FFABS(d);
749 static int max=0;
750 static int sum=0;
751 static int num=0;
752 static int bias=0;
753
754 if(max<ad) max=ad;
755 sum+= ad>3 ? 1 : 0;
756 if(ad>3){
757 src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
758 }
759 if(y==4) bias+=d;
760 num++;
761 if(num%1000000 == 0){
762 av_log(c, AV_LOG_INFO, " %d %d %d %d\n", num, sum, max, bias);
763 }
764 }
765 }
766 }
767 */
768 #elif HAVE_MMX
769 src+= stride*4;
770 __asm__ volatile(
771 "pxor %%mm7, %%mm7 \n\t"
772 "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
773 "and "ALIGN_MASK", %%"REG_c" \n\t" // align
774 // 0 1 2 3 4 5 6 7
775 // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 edx+%1 edx+2%1
776 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1
777
778 "movq (%0), %%mm0 \n\t"
779 "movq %%mm0, %%mm1 \n\t"
780 "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
781 "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
782
783 "movq (%0, %1), %%mm2 \n\t"
784 "lea (%0, %1, 2), %%"REG_a" \n\t"
785 "movq %%mm2, %%mm3 \n\t"
786 "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
787 "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
788
789 "movq (%%"REG_a"), %%mm4 \n\t"
790 "movq %%mm4, %%mm5 \n\t"
791 "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
792 "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
793
794 "paddw %%mm0, %%mm0 \n\t" // 2L0
795 "paddw %%mm1, %%mm1 \n\t" // 2H0
796 "psubw %%mm4, %%mm2 \n\t" // L1 - L2
797 "psubw %%mm5, %%mm3 \n\t" // H1 - H2
798 "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
799 "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
800
801 "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
802 "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
803 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
804 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
805
806 "movq (%%"REG_a", %1), %%mm2 \n\t"
807 "movq %%mm2, %%mm3 \n\t"
808 "punpcklbw %%mm7, %%mm2 \n\t" // L3
809 "punpckhbw %%mm7, %%mm3 \n\t" // H3
810
811 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
812 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
813 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
814 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
815 "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
816 "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
817
818 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
819 "movq %%mm0, %%mm1 \n\t"
820 "punpcklbw %%mm7, %%mm0 \n\t" // L4
821 "punpckhbw %%mm7, %%mm1 \n\t" // H4
822
823 "psubw %%mm0, %%mm2 \n\t" // L3 - L4
824 "psubw %%mm1, %%mm3 \n\t" // H3 - H4
825 "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
826 "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
827 "paddw %%mm4, %%mm4 \n\t" // 2L2
828 "paddw %%mm5, %%mm5 \n\t" // 2H2
829 "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
830 "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
831
832 "lea (%%"REG_a", %1), %0 \n\t"
833 "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
834 "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
835 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
836 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
837 //50 opcodes so far
838 "movq (%0, %1, 2), %%mm2 \n\t"
839 "movq %%mm2, %%mm3 \n\t"
840 "punpcklbw %%mm7, %%mm2 \n\t" // L5
841 "punpckhbw %%mm7, %%mm3 \n\t" // H5
842 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
843 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
844 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
845 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
846
847 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
848 "punpcklbw %%mm7, %%mm6 \n\t" // L6
849 "psubw %%mm6, %%mm2 \n\t" // L5 - L6
850 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
851 "punpckhbw %%mm7, %%mm6 \n\t" // H6
852 "psubw %%mm6, %%mm3 \n\t" // H5 - H6
853
854 "paddw %%mm0, %%mm0 \n\t" // 2L4
855 "paddw %%mm1, %%mm1 \n\t" // 2H4
856 "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
857 "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
858
859 "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
860 "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
861 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
862 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
863
864 "movq (%0, %1, 4), %%mm2 \n\t"
865 "movq %%mm2, %%mm3 \n\t"
866 "punpcklbw %%mm7, %%mm2 \n\t" // L7
867 "punpckhbw %%mm7, %%mm3 \n\t" // H7
868
869 "paddw %%mm2, %%mm2 \n\t" // 2L7
870 "paddw %%mm3, %%mm3 \n\t" // 2H7
871 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
872 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
873
874 "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
875 "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
876
877 #if HAVE_MMX2
878 "movq %%mm7, %%mm6 \n\t" // 0
879 "psubw %%mm0, %%mm6 \n\t"
880 "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
881 "movq %%mm7, %%mm6 \n\t" // 0
882 "psubw %%mm1, %%mm6 \n\t"
883 "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
884 "movq %%mm7, %%mm6 \n\t" // 0
885 "psubw %%mm2, %%mm6 \n\t"
886 "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
887 "movq %%mm7, %%mm6 \n\t" // 0
888 "psubw %%mm3, %%mm6 \n\t"
889 "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
890 #else
891 "movq %%mm7, %%mm6 \n\t" // 0
892 "pcmpgtw %%mm0, %%mm6 \n\t"
893 "pxor %%mm6, %%mm0 \n\t"
894 "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
895 "movq %%mm7, %%mm6 \n\t" // 0
896 "pcmpgtw %%mm1, %%mm6 \n\t"
897 "pxor %%mm6, %%mm1 \n\t"
898 "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
899 "movq %%mm7, %%mm6 \n\t" // 0
900 "pcmpgtw %%mm2, %%mm6 \n\t"
901 "pxor %%mm6, %%mm2 \n\t"
902 "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
903 "movq %%mm7, %%mm6 \n\t" // 0
904 "pcmpgtw %%mm3, %%mm6 \n\t"
905 "pxor %%mm6, %%mm3 \n\t"
906 "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
907 #endif
908
909 #if HAVE_MMX2
910 "pminsw %%mm2, %%mm0 \n\t"
911 "pminsw %%mm3, %%mm1 \n\t"
912 #else
913 "movq %%mm0, %%mm6 \n\t"
914 "psubusw %%mm2, %%mm6 \n\t"
915 "psubw %%mm6, %%mm0 \n\t"
916 "movq %%mm1, %%mm6 \n\t"
917 "psubusw %%mm3, %%mm6 \n\t"
918 "psubw %%mm6, %%mm1 \n\t"
919 #endif
920
921 "movd %2, %%mm2 \n\t" // QP
922 "punpcklbw %%mm7, %%mm2 \n\t"
923
924 "movq %%mm7, %%mm6 \n\t" // 0
925 "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
926 "pxor %%mm6, %%mm4 \n\t"
927 "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
928 "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
929 "pxor %%mm7, %%mm5 \n\t"
930 "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
931 // 100 opcodes
932 "psllw $3, %%mm2 \n\t" // 8QP
933 "movq %%mm2, %%mm3 \n\t" // 8QP
934 "pcmpgtw %%mm4, %%mm2 \n\t"
935 "pcmpgtw %%mm5, %%mm3 \n\t"
936 "pand %%mm2, %%mm4 \n\t"
937 "pand %%mm3, %%mm5 \n\t"
938
939
940 "psubusw %%mm0, %%mm4 \n\t" // hd
941 "psubusw %%mm1, %%mm5 \n\t" // ld
942
943
944 "movq "MANGLE(w05)", %%mm2 \n\t" // 5
945 "pmullw %%mm2, %%mm4 \n\t"
946 "pmullw %%mm2, %%mm5 \n\t"
947 "movq "MANGLE(w20)", %%mm2 \n\t" // 32
948 "paddw %%mm2, %%mm4 \n\t"
949 "paddw %%mm2, %%mm5 \n\t"
950 "psrlw $6, %%mm4 \n\t"
951 "psrlw $6, %%mm5 \n\t"
952
953 "movq 16(%%"REG_c"), %%mm0 \n\t" // L3 - L4
954 "movq 24(%%"REG_c"), %%mm1 \n\t" // H3 - H4
955
956 "pxor %%mm2, %%mm2 \n\t"
957 "pxor %%mm3, %%mm3 \n\t"
958
959 "pcmpgtw %%mm0, %%mm2 \n\t" // sign (L3-L4)
960 "pcmpgtw %%mm1, %%mm3 \n\t" // sign (H3-H4)
961 "pxor %%mm2, %%mm0 \n\t"
962 "pxor %%mm3, %%mm1 \n\t"
963 "psubw %%mm2, %%mm0 \n\t" // |L3-L4|
964 "psubw %%mm3, %%mm1 \n\t" // |H3-H4|
965 "psrlw $1, %%mm0 \n\t" // |L3 - L4|/2
966 "psrlw $1, %%mm1 \n\t" // |H3 - H4|/2
967
968 "pxor %%mm6, %%mm2 \n\t"
969 "pxor %%mm7, %%mm3 \n\t"
970 "pand %%mm2, %%mm4 \n\t"
971 "pand %%mm3, %%mm5 \n\t"
972
973 #if HAVE_MMX2
974 "pminsw %%mm0, %%mm4 \n\t"
975 "pminsw %%mm1, %%mm5 \n\t"
976 #else
977 "movq %%mm4, %%mm2 \n\t"
978 "psubusw %%mm0, %%mm2 \n\t"
979 "psubw %%mm2, %%mm4 \n\t"
980 "movq %%mm5, %%mm2 \n\t"
981 "psubusw %%mm1, %%mm2 \n\t"
982 "psubw %%mm2, %%mm5 \n\t"
983 #endif
984 "pxor %%mm6, %%mm4 \n\t"
985 "pxor %%mm7, %%mm5 \n\t"
986 "psubw %%mm6, %%mm4 \n\t"
987 "psubw %%mm7, %%mm5 \n\t"
988 "packsswb %%mm5, %%mm4 \n\t"
989 "movq (%0), %%mm0 \n\t"
990 "paddb %%mm4, %%mm0 \n\t"
991 "movq %%mm0, (%0) \n\t"
992 "movq (%0, %1), %%mm0 \n\t"
993 "psubb %%mm4, %%mm0 \n\t"
994 "movq %%mm0, (%0, %1) \n\t"
995
996 : "+r" (src)
997 : "r" ((x86_reg)stride), "m" (c->pQPb)
998 : "%"REG_a, "%"REG_c
999 );
1000 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1001 const int l1= stride;
1002 const int l2= stride + l1;
1003 const int l3= stride + l2;
1004 const int l4= stride + l3;
1005 const int l5= stride + l4;
1006 const int l6= stride + l5;
1007 const int l7= stride + l6;
1008 const int l8= stride + l7;
1009 // const int l9= stride + l8;
1010 int x;
1011 src+= stride*3;
1012 for(x=0; x<BLOCK_SIZE; x++){
1013 const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
1014 if(FFABS(middleEnergy) < 8*c->QP){
1015 const int q=(src[l4] - src[l5])/2;
1016 const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
1017 const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
1018
1019 int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
1020 d= FFMAX(d, 0);
1021
1022 d= (5*d + 32) >> 6;
1023 d*= FFSIGN(-middleEnergy);
1024
1025 if(q>0){
1026 d= d<0 ? 0 : d;
1027 d= d>q ? q : d;
1028 }else{
1029 d= d>0 ? 0 : d;
1030 d= d<q ? q : d;
1031 }
1032
1033 src[l4]-= d;
1034 src[l5]+= d;
1035 }
1036 src++;
1037 }
1038 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1039 }
1040 #endif //HAVE_ALTIVEC
1041
1042 #if !HAVE_ALTIVEC
1043 static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
1044 {
1045 #if HAVE_MMX2 || HAVE_AMD3DNOW
1046 __asm__ volatile(
1047 "pxor %%mm6, %%mm6 \n\t"
1048 "pcmpeqb %%mm7, %%mm7 \n\t"
1049 "movq %2, %%mm0 \n\t"
1050 "punpcklbw %%mm6, %%mm0 \n\t"
1051 "psrlw $1, %%mm0 \n\t"
1052 "psubw %%mm7, %%mm0 \n\t"
1053 "packuswb %%mm0, %%mm0 \n\t"
1054 "movq %%mm0, %3 \n\t"
1055
1056 "lea (%0, %1), %%"REG_a" \n\t"
1057 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1058
1059 // 0 1 2 3 4 5 6 7 8 9
1060 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
1061
1062 #undef FIND_MIN_MAX
1063 #if HAVE_MMX2
1064 #define REAL_FIND_MIN_MAX(addr)\
1065 "movq " #addr ", %%mm0 \n\t"\
1066 "pminub %%mm0, %%mm7 \n\t"\
1067 "pmaxub %%mm0, %%mm6 \n\t"
1068 #else
1069 #define REAL_FIND_MIN_MAX(addr)\
1070 "movq " #addr ", %%mm0 \n\t"\
1071 "movq %%mm7, %%mm1 \n\t"\
1072 "psubusb %%mm0, %%mm6 \n\t"\
1073 "paddb %%mm0, %%mm6 \n\t"\
1074 "psubusb %%mm0, %%mm1 \n\t"\
1075 "psubb %%mm1, %%mm7 \n\t"
1076 #endif
1077 #define FIND_MIN_MAX(addr) REAL_FIND_MIN_MAX(addr)
1078
1079 FIND_MIN_MAX((%%REGa))
1080 FIND_MIN_MAX((%%REGa, %1))
1081 FIND_MIN_MAX((%%REGa, %1, 2))
1082 FIND_MIN_MAX((%0, %1, 4))
1083 FIND_MIN_MAX((%%REGd))
1084 FIND_MIN_MAX((%%REGd, %1))
1085 FIND_MIN_MAX((%%REGd, %1, 2))
1086 FIND_MIN_MAX((%0, %1, 8))
1087
1088 "movq %%mm7, %%mm4 \n\t"
1089 "psrlq $8, %%mm7 \n\t"
1090 #if HAVE_MMX2
1091 "pminub %%mm4, %%mm7 \n\t" // min of pixels
1092 "pshufw $0xF9, %%mm7, %%mm4 \n\t"
1093 "pminub %%mm4, %%mm7 \n\t" // min of pixels
1094 "pshufw $0xFE, %%mm7, %%mm4 \n\t"
1095 "pminub %%mm4, %%mm7 \n\t"
1096 #else
1097 "movq %%mm7, %%mm1 \n\t"
1098 "psubusb %%mm4, %%mm1 \n\t"
1099 "psubb %%mm1, %%mm7 \n\t"
1100 "movq %%mm7, %%mm4 \n\t"
1101 "psrlq $16, %%mm7 \n\t"
1102 "movq %%mm7, %%mm1 \n\t"
1103 "psubusb %%mm4, %%mm1 \n\t"
1104 "psubb %%mm1, %%mm7 \n\t"
1105 "movq %%mm7, %%mm4 \n\t"
1106 "psrlq $32, %%mm7 \n\t"
1107 "movq %%mm7, %%mm1 \n\t"
1108 "psubusb %%mm4, %%mm1 \n\t"
1109 "psubb %%mm1, %%mm7 \n\t"
1110 #endif
1111
1112
1113 "movq %%mm6, %%mm4 \n\t"
1114 "psrlq $8, %%mm6 \n\t"
1115 #if HAVE_MMX2
1116 "pmaxub %%mm4, %%mm6 \n\t" // max of pixels
1117 "pshufw $0xF9, %%mm6, %%mm4 \n\t"
1118 "pmaxub %%mm4, %%mm6 \n\t"
1119 "pshufw $0xFE, %%mm6, %%mm4 \n\t"
1120 "pmaxub %%mm4, %%mm6 \n\t"
1121 #else
1122 "psubusb %%mm4, %%mm6 \n\t"
1123 "paddb %%mm4, %%mm6 \n\t"
1124 "movq %%mm6, %%mm4 \n\t"
1125 "psrlq $16, %%mm6 \n\t"
1126 "psubusb %%mm4, %%mm6 \n\t"
1127 "paddb %%mm4, %%mm6 \n\t"
1128 "movq %%mm6, %%mm4 \n\t"
1129 "psrlq $32, %%mm6 \n\t"
1130 "psubusb %%mm4, %%mm6 \n\t"
1131 "paddb %%mm4, %%mm6 \n\t"
1132 #endif
1133 "movq %%mm6, %%mm0 \n\t" // max
1134 "psubb %%mm7, %%mm6 \n\t" // max - min
1135 "movd %%mm6, %%ecx \n\t"
1136 "cmpb "MANGLE(deringThreshold)", %%cl \n\t"
1137 " jb 1f \n\t"
1138 "lea -24(%%"REG_SP"), %%"REG_c" \n\t"
1139 "and "ALIGN_MASK", %%"REG_c" \n\t"
1140 PAVGB(%%mm0, %%mm7) // a=(max + min)/2
1141 "punpcklbw %%mm7, %%mm7 \n\t"
1142 "punpcklbw %%mm7, %%mm7 \n\t"
1143 "punpcklbw %%mm7, %%mm7 \n\t"
1144 "movq %%mm7, (%%"REG_c") \n\t"
1145
1146 "movq (%0), %%mm0 \n\t" // L10
1147 "movq %%mm0, %%mm1 \n\t" // L10
1148 "movq %%mm0, %%mm2 \n\t" // L10
1149 "psllq $8, %%mm1 \n\t"
1150 "psrlq $8, %%mm2 \n\t"
1151 "movd -4(%0), %%mm3 \n\t"
1152 "movd 8(%0), %%mm4 \n\t"
1153 "psrlq $24, %%mm3 \n\t"
1154 "psllq $56, %%mm4 \n\t"
1155 "por %%mm3, %%mm1 \n\t" // L00
1156 "por %%mm4, %%mm2 \n\t" // L20
1157 "movq %%mm1, %%mm3 \n\t" // L00
1158 PAVGB(%%mm2, %%mm1) // (L20 + L00)/2
1159 PAVGB(%%mm0, %%mm1) // (L20 + L00 + 2L10)/4
1160 "psubusb %%mm7, %%mm0 \n\t"
1161 "psubusb %%mm7, %%mm2 \n\t"
1162 "psubusb %%mm7, %%mm3 \n\t"
1163 "pcmpeqb "MANGLE(b00)", %%mm0 \n\t" // L10 > a ? 0 : -1
1164 "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L20 > a ? 0 : -1
1165 "pcmpeqb "MANGLE(b00)", %%mm3 \n\t" // L00 > a ? 0 : -1
1166 "paddb %%mm2, %%mm0 \n\t"
1167 "paddb %%mm3, %%mm0 \n\t"
1168
1169 "movq (%%"REG_a"), %%mm2 \n\t" // L11
1170 "movq %%mm2, %%mm3 \n\t" // L11
1171 "movq %%mm2, %%mm4 \n\t" // L11
1172 "psllq $8, %%mm3 \n\t"
1173 "psrlq $8, %%mm4 \n\t"
1174 "movd -4(%%"REG_a"), %%mm5 \n\t"
1175 "movd 8(%%"REG_a"), %%mm6 \n\t"
1176 "psrlq $24, %%mm5 \n\t"
1177 "psllq $56, %%mm6 \n\t"
1178 "por %%mm5, %%mm3 \n\t" // L01
1179 "por %%mm6, %%mm4 \n\t" // L21
1180 "movq %%mm3, %%mm5 \n\t" // L01
1181 PAVGB(%%mm4, %%mm3) // (L21 + L01)/2
1182 PAVGB(%%mm2, %%mm3) // (L21 + L01 + 2L11)/4
1183 "psubusb %%mm7, %%mm2 \n\t"
1184 "psubusb %%mm7, %%mm4 \n\t"
1185 "psubusb %%mm7, %%mm5 \n\t"
1186 "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L11 > a ? 0 : -1
1187 "pcmpeqb "MANGLE(b00)", %%mm4 \n\t" // L21 > a ? 0 : -1
1188 "pcmpeqb "MANGLE(b00)", %%mm5 \n\t" // L01 > a ? 0 : -1
1189 "paddb %%mm4, %%mm2 \n\t"
1190 "paddb %%mm5, %%mm2 \n\t"
1191 // 0, 2, 3, 1
1192 #define REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
1193 "movq " #src ", " #sx " \n\t" /* src[0] */\
1194 "movq " #sx ", " #lx " \n\t" /* src[0] */\
1195 "movq " #sx ", " #t0 " \n\t" /* src[0] */\
1196 "psllq $8, " #lx " \n\t"\
1197 "psrlq $8, " #t0 " \n\t"\
1198 "movd -4" #src ", " #t1 " \n\t"\
1199 "psrlq $24, " #t1 " \n\t"\
1200 "por " #t1 ", " #lx " \n\t" /* src[-1] */\
1201 "movd 8" #src ", " #t1 " \n\t"\
1202 "psllq $56, " #t1 " \n\t"\
1203 "por " #t1 ", " #t0 " \n\t" /* src[+1] */\
1204 "movq " #lx ", " #t1 " \n\t" /* src[-1] */\
1205 PAVGB(t0, lx) /* (src[-1] + src[+1])/2 */\
1206 PAVGB(sx, lx) /* (src[-1] + 2src[0] + src[+1])/4 */\
1207 PAVGB(lx, pplx) \
1208 "movq " #lx ", 8(%%"REG_c") \n\t"\
1209 "movq (%%"REG_c"), " #lx " \n\t"\
1210 "psubusb " #lx ", " #t1 " \n\t"\
1211 "psubusb " #lx ", " #t0 " \n\t"\
1212 "psubusb " #lx ", " #sx " \n\t"\
1213 "movq "MANGLE(b00)", " #lx " \n\t"\
1214 "pcmpeqb " #lx ", " #t1 " \n\t" /* src[-1] > a ? 0 : -1*/\
1215 "pcmpeqb " #lx ", " #t0 " \n\t" /* src[+1] > a ? 0 : -1*/\
1216 "pcmpeqb " #lx ", " #sx " \n\t" /* src[0] > a ? 0 : -1*/\
1217 "paddb " #t1 ", " #t0 " \n\t"\
1218 "paddb " #t0 ", " #sx " \n\t"\
1219 \
1220 PAVGB(plx, pplx) /* filtered */\
1221 "movq " #dst ", " #t0 " \n\t" /* dst */\
1222 "movq " #t0 ", " #t1 " \n\t" /* dst */\
1223 "psubusb %3, " #t0 " \n\t"\
1224 "paddusb %3, " #t1 " \n\t"\
1225 PMAXUB(t0, pplx)\
1226 PMINUB(t1, pplx, t0)\
1227 "paddb " #sx ", " #ppsx " \n\t"\
1228 "paddb " #psx ", " #ppsx " \n\t"\
1229 "#paddb "MANGLE(b02)", " #ppsx " \n\t"\
1230 "pand "MANGLE(b08)", " #ppsx " \n\t"\
1231 "pcmpeqb " #lx ", " #ppsx " \n\t"\
1232 "pand " #ppsx ", " #pplx " \n\t"\
1233 "pandn " #dst ", " #ppsx " \n\t"\
1234 "por " #pplx ", " #ppsx " \n\t"\
1235 "movq " #ppsx ", " #dst " \n\t"\
1236 "movq 8(%%"REG_c"), " #lx " \n\t"
1237
1238 #define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
1239 REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1)
1240 /*
1241 0000000
1242 1111111
1243
1244 1111110
1245 1111101
1246 1111100
1247 1111011
1248 1111010
1249 1111001
1250
1251 1111000
1252 1110111
1253
1254 */
1255 //DERING_CORE(dst ,src ,ppsx ,psx ,sx ,pplx ,plx ,lx ,t0 ,t1)
1256 DERING_CORE((%%REGa) ,(%%REGa, %1) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1257 DERING_CORE((%%REGa, %1) ,(%%REGa, %1, 2),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1258 DERING_CORE((%%REGa, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1259 DERING_CORE((%0, %1, 4) ,(%%REGd) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1260 DERING_CORE((%%REGd) ,(%%REGd, %1) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1261 DERING_CORE((%%REGd, %1) ,(%%REGd, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1262 DERING_CORE((%%REGd, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1263 DERING_CORE((%0, %1, 8) ,(%%REGd, %1, 4),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1264
1265 "1: \n\t"
1266 : : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb), "m"(c->pQPb2)
1267 : "%"REG_a, "%"REG_d, "%"REG_c
1268 );
1269 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1270 int y;
1271 int min=255;
1272 int max=0;
1273 int avg;
1274 uint8_t *p;
1275 int s[10];
1276 const int QP2= c->QP/2 + 1;
1277
1278 for(y=1; y<9; y++){
1279 int x;
1280 p= src + stride*y;
1281 for(x=1; x<9; x++){
1282 p++;
1283 if(*p > max) max= *p;
1284 if(*p < min) min= *p;
1285 }
1286 }
1287 avg= (min + max + 1)>>1;
1288
1289 if(max - min <deringThreshold) return;
1290
1291 for(y=0; y<10; y++){
1292 int t = 0;
1293
1294 if(src[stride*y + 0] > avg) t+= 1;
1295 if(src[stride*y + 1] > avg) t+= 2;
1296 if(src[stride*y + 2] > avg) t+= 4;
1297 if(src[stride*y + 3] > avg) t+= 8;
1298 if(src[stride*y + 4] > avg) t+= 16;
1299 if(src[stride*y + 5] > avg) t+= 32;
1300 if(src[stride*y + 6] > avg) t+= 64;
1301 if(src[stride*y + 7] > avg) t+= 128;
1302 if(src[stride*y + 8] > avg) t+= 256;
1303 if(src[stride*y + 9] > avg) t+= 512;
1304
1305 t |= (~t)<<16;
1306 t &= (t<<1) & (t>>1);
1307 s[y] = t;
1308 }
1309
1310 for(y=1; y<9; y++){
1311 int t = s[y-1] & s[y] & s[y+1];
1312 t|= t>>16;
1313 s[y-1]= t;
1314 }
1315
1316 for(y=1; y<9; y++){
1317 int x;
1318 int t = s[y-1];
1319
1320 p= src + stride*y;
1321 for(x=1; x<9; x++){
1322 p++;
1323 if(t & (1<<x)){
1324 int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
1325 +2*(*(p -1)) + 4*(*p ) + 2*(*(p +1))
1326 +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
1327 f= (f + 8)>>4;
1328
1329 #ifdef DEBUG_DERING_THRESHOLD
1330 __asm__ volatile("emms\n\t":);
1331 {
1332 static long long numPixels=0;
1333 if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
1334 // if((max-min)<20 || (max-min)*QP<200)
1335 // if((max-min)*QP < 500)
1336 // if(max-min<QP/2)
1337 if(max-min < 20){
1338 static int numSkipped=0;
1339 static int errorSum=0;
1340 static int worstQP=0;
1341 static int worstRange=0;
1342 static int worstDiff=0;
1343 int diff= (f - *p);
1344 int absDiff= FFABS(diff);
1345 int error= diff*diff;
1346
1347 if(x==1 || x==8 || y==1 || y==8) continue;
1348
1349 numSkipped++;
1350 if(absDiff > worstDiff){
1351 worstDiff= absDiff;
1352 worstQP= QP;
1353 worstRange= max-min;
1354 }
1355 errorSum+= error;
1356
1357 if(1024LL*1024LL*1024LL % numSkipped == 0){
1358 av_log(c, AV_LOG_INFO, "sum:%1.3f, skip:%d, wQP:%d, "
1359 "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
1360 (float)errorSum/numSkipped, numSkipped, worstQP, worstRange,
1361 worstDiff, (float)numSkipped/numPixels);
1362 }
1363 }
1364 }
1365 #endif
1366 if (*p + QP2 < f) *p= *p + QP2;
1367 else if(*p - QP2 > f) *p= *p - QP2;
1368 else *p=f;
1369 }
1370 }
1371 }
1372 #ifdef DEBUG_DERING_THRESHOLD
1373 if(max-min < 20){
1374 for(y=1; y<9; y++){
1375 int x;
1376 int t = 0;
1377 p= src + stride*y;
1378 for(x=1; x<9; x++){
1379 p++;
1380 *p = FFMIN(*p + 20, 255);
1381 }
1382 }
1383 // src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
1384 }
1385 #endif
1386 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1387 }
1388 #endif //HAVE_ALTIVEC
1389
1390 /**
1391 * Deinterlaces the given block by linearly interpolating every second line.
1392 * will be called for every 8x8 block and can read & write from line 4-15
1393 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1394 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1395 */
1396 static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
1397 {
1398 #if HAVE_MMX2 || HAVE_AMD3DNOW
1399 src+= 4*stride;
1400 __asm__ volatile(
1401 "lea (%0, %1), %%"REG_a" \n\t"
1402 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
1403 // 0 1 2 3 4 5 6 7 8 9
1404 // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
1405
1406 "movq (%0), %%mm0 \n\t"
1407 "movq (%%"REG_a", %1), %%mm1 \n\t"
1408 PAVGB(%%mm1, %%mm0)
1409 "movq %%mm0, (%%"REG_a") \n\t"
1410 "movq (%0, %1, 4), %%mm0 \n\t"
1411 PAVGB(%%mm0, %%mm1)
1412 "movq %%mm1, (%%"REG_a", %1, 2) \n\t"
1413 "movq (%%"REG_c", %1), %%mm1 \n\t"
1414 PAVGB(%%mm1, %%mm0)
1415 "movq %%mm0, (%%"REG_c") \n\t"
1416 "movq (%0, %1, 8), %%mm0 \n\t"
1417 PAVGB(%%mm0, %%mm1)
1418 "movq %%mm1, (%%"REG_c", %1, 2) \n\t"
1419
1420 : : "r" (src), "r" ((x86_reg)stride)
1421 : "%"REG_a, "%"REG_c
1422 );
1423 #else
1424 int a, b, x;
1425 src+= 4*stride;
1426
1427 for(x=0; x<2; x++){
1428 a= *(uint32_t*)&src[stride*0];
1429 b= *(uint32_t*)&src[stride*2];
1430 *(uint32_t*)&src[stride*1]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1431 a= *(uint32_t*)&src[stride*4];
1432 *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1433 b= *(uint32_t*)&src[stride*6];
1434 *(uint32_t*)&src[stride*5]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1435 a= *(uint32_t*)&src[stride*8];
1436 *(uint32_t*)&src[stride*7]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1437 src += 4;
1438 }
1439 #endif
1440 }
1441
1442 /**
1443 * Deinterlaces the given block by cubic interpolating every second line.
1444 * will be called for every 8x8 block and can read & write from line 4-15
1445 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1446 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1447 * this filter will read lines 3-15 and write 7-13
1448 */
1449 static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
1450 {
1451 #if HAVE_MMX2 || HAVE_AMD3DNOW
1452 src+= stride*3;
1453 __asm__ volatile(
1454 "lea (%0, %1), %%"REG_a" \n\t"
1455 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1456 "lea (%%"REG_d", %1, 4), %%"REG_c" \n\t"
1457 "add %1, %%"REG_c" \n\t"
1458 "pxor %%mm7, %%mm7 \n\t"
1459 // 0 1 2 3 4 5 6 7 8 9 10
1460 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
1461
1462 #define REAL_DEINT_CUBIC(a,b,c,d,e)\
1463 "movq " #a ", %%mm0 \n\t"\
1464 "movq " #b ", %%mm1 \n\t"\
1465 "movq " #d ", %%mm2 \n\t"\
1466 "movq " #e ", %%mm3 \n\t"\
1467 PAVGB(%%mm2, %%mm1) /* (b+d) /2 */\
1468 PAVGB(%%mm3, %%mm0) /* a(a+e) /2 */\
1469 "movq %%mm0, %%mm2 \n\t"\
1470 "punpcklbw %%mm7, %%mm0 \n\t"\
1471 "punpckhbw %%mm7, %%mm2 \n\t"\
1472 "movq %%mm1, %%mm3 \n\t"\
1473 "punpcklbw %%mm7, %%mm1 \n\t"\
1474 "punpckhbw %%mm7, %%mm3 \n\t"\
1475 "psubw %%mm1, %%mm0 \n\t" /* L(a+e - (b+d))/2 */\
1476 "psubw %%mm3, %%mm2 \n\t" /* H(a+e - (b+d))/2 */\
1477 "psraw $3, %%mm0 \n\t" /* L(a+e - (b+d))/16 */\
1478 "psraw $3, %%mm2 \n\t" /* H(a+e - (b+d))/16 */\
1479 "psubw %%mm0, %%mm1 \n\t" /* L(9b + 9d - a - e)/16 */\
1480 "psubw %%mm2, %%mm3 \n\t" /* H(9b + 9d - a - e)/16 */\
1481 "packuswb %%mm3, %%mm1 \n\t"\
1482 "movq %%mm1, " #c " \n\t"
1483 #define DEINT_CUBIC(a,b,c,d,e) REAL_DEINT_CUBIC(a,b,c,d,e)
1484
1485 DEINT_CUBIC((%0) , (%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd, %1))
1486 DEINT_CUBIC((%%REGa, %1), (%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%0, %1, 8))
1487 DEINT_CUBIC((%0, %1, 4) , (%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGc))
1488 DEINT_CUBIC((%%REGd, %1), (%0, %1, 8) , (%%REGd, %1, 4), (%%REGc) , (%%REGc, %1, 2))
1489
1490 : : "r" (src), "r" ((x86_reg)stride)
1491 : "%"REG_a, "%"REG_d, "%"REG_c
1492 );
1493 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1494 int x;
1495 src+= stride*3;
1496 for(x=0; x<8; x++){
1497 src[stride*3] = CLIP((-src[0] + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4);
1498 src[stride*5] = CLIP((-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4);
1499 src[stride*7] = CLIP((-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4);
1500 src[stride*9] = CLIP((-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4);
1501 src++;
1502 }
1503 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1504 }
1505
1506 /**
1507 * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
1508 * will be called for every 8x8 block and can read & write from line 4-15
1509 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1510 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1511 * this filter will read lines 4-13 and write 5-11
1512 */
1513 static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
1514 {
1515 #if HAVE_MMX2 || HAVE_AMD3DNOW
1516 src+= stride*4;
1517 __asm__ volatile(
1518 "lea (%0, %1), %%"REG_a" \n\t"
1519 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1520 "pxor %%mm7, %%mm7 \n\t"
1521 "movq (%2), %%mm0 \n\t"
1522 // 0 1 2 3 4 5 6 7 8 9 10
1523 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
1524
1525 #define REAL_DEINT_FF(a,b,c,d)\
1526 "movq " #a ", %%mm1 \n\t"\
1527 "movq " #b ", %%mm2 \n\t"\
1528 "movq " #c ", %%mm3 \n\t"\
1529 "movq " #d ", %%mm4 \n\t"\
1530 PAVGB(%%mm3, %%mm1) \
1531 PAVGB(%%mm4, %%mm0) \
1532 "movq %%mm0, %%mm3 \n\t"\
1533 "punpcklbw %%mm7, %%mm0 \n\t"\
1534 "punpckhbw %%mm7, %%mm3 \n\t"\
1535 "movq %%mm1, %%mm4 \n\t"\
1536 "punpcklbw %%mm7, %%mm1 \n\t"\
1537 "punpckhbw %%mm7, %%mm4 \n\t"\
1538 "psllw $2, %%mm1 \n\t"\
1539 "psllw $2, %%mm4 \n\t"\
1540 "psubw %%mm0, %%mm1 \n\t"\
1541 "psubw %%mm3, %%mm4 \n\t"\
1542 "movq %%mm2, %%mm5 \n\t"\
1543 "movq %%mm2, %%mm0 \n\t"\
1544 "punpcklbw %%mm7, %%mm2 \n\t"\
1545 "punpckhbw %%mm7, %%mm5 \n\t"\
1546 "paddw %%mm2, %%mm1 \n\t"\
1547 "paddw %%mm5, %%mm4 \n\t"\
1548 "psraw $2, %%mm1 \n\t"\
1549 "psraw $2, %%mm4 \n\t"\
1550 "packuswb %%mm4, %%mm1 \n\t"\
1551 "movq %%mm1, " #b " \n\t"\
1552
1553 #define DEINT_FF(a,b,c,d) REAL_DEINT_FF(a,b,c,d)
1554
1555 DEINT_FF((%0) , (%%REGa) , (%%REGa, %1), (%%REGa, %1, 2))
1556 DEINT_FF((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
1557 DEINT_FF((%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%%REGd, %1, 2))
1558 DEINT_FF((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
1559
1560 "movq %%mm0, (%2) \n\t"
1561 : : "r" (src), "r" ((x86_reg)stride), "r"(tmp)
1562 : "%"REG_a, "%"REG_d
1563 );
1564 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1565 int x;
1566 src+= stride*4;
1567 for(x=0; x<8; x++){
1568 int t1= tmp[x];
1569 int t2= src[stride*1];
1570
1571 src[stride*1]= CLIP((-t1 + 4*src[stride*0] + 2*t2 + 4*src[stride*2] - src[stride*3] + 4)>>3);
1572 t1= src[stride*4];
1573 src[stride*3]= CLIP((-t2 + 4*src[stride*2] + 2*t1 + 4*src[stride*4] - src[stride*5] + 4)>>3);
1574 t2= src[stride*6];
1575 src[stride*5]= CLIP((-t1 + 4*src[stride*4] + 2*t2 + 4*src[stride*6] - src[stride*7] + 4)>>3);
1576 t1= src[stride*8];
1577 src[stride*7]= CLIP((-t2 + 4*src[stride*6] + 2*t1 + 4*src[stride*8] - src[stride*9] + 4)>>3);
1578 tmp[x]= t1;
1579
1580 src++;
1581 }
1582 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1583 }
1584
1585 /**
1586 * Deinterlaces the given block by filtering every line with a (-1 2 6 2 -1) filter.
1587 * will be called for every 8x8 block and can read & write from line 4-15
1588 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1589 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1590 * this filter will read lines 4-13 and write 4-11
1591 */
1592 static inline void RENAME(deInterlaceL5)(uint8_t src[], int stride, uint8_t *tmp, uint8_t *tmp2)
1593 {
1594 #if HAVE_MMX2 || HAVE_AMD3DNOW
1595 src+= stride*4;
1596 __asm__ volatile(
1597 "lea (%0, %1), %%"REG_a" \n\t"
1598 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1599 "pxor %%mm7, %%mm7 \n\t"
1600 "movq (%2), %%mm0 \n\t"
1601 "movq (%3), %%mm1 \n\t"
1602 // 0 1 2 3 4 5 6 7 8 9 10
1603 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
1604
1605 #define REAL_DEINT_L5(t1,t2,a,b,c)\
1606 "movq " #a ", %%mm2 \n\t"\
1607 "movq " #b ", %%mm3 \n\t"\
1608 "movq " #c ", %%mm4 \n\t"\
1609 PAVGB(t2, %%mm3) \
1610 PAVGB(t1, %%mm4) \
1611 "movq %%mm2, %%mm5 \n\t"\
1612 "movq %%mm2, " #t1 " \n\t"\
1613 "punpcklbw %%mm7, %%mm2 \n\t"\
1614 "punpckhbw %%mm7, %%mm5 \n\t"\
1615 "movq %%mm2, %%mm6 \n\t"\
1616 "paddw %%mm2, %%mm2 \n\t"\
1617 "paddw %%mm6, %%mm2 \n\t"\
1618 "movq %%mm5, %%mm6 \n\t"\
1619 "paddw %%mm5, %%mm5 \n\t"\
1620 "paddw %%mm6, %%mm5 \n\t"\
1621 "movq %%mm3, %%mm6 \n\t"\
1622 "punpcklbw %%mm7, %%mm3 \n\t"\
1623 "punpckhbw %%mm7, %%mm6 \n\t"\
1624 "paddw %%mm3, %%mm3 \n\t"\
1625 "paddw %%mm6, %%mm6 \n\t"\
1626 "paddw %%mm3, %%mm2 \n\t"\
1627 "paddw %%mm6, %%mm5 \n\t"\
1628 "movq %%mm4, %%mm6 \n\t"\
1629 "punpcklbw %%mm7, %%mm4 \n\t"\
1630 "punpckhbw %%mm7, %%mm6 \n\t"\
1631 "psubw %%mm4, %%mm2 \n\t"\
1632 "psubw %%mm6, %%mm5 \n\t"\
1633 "psraw $2, %%mm2 \n\t"\
1634 "psraw $2, %%mm5 \n\t"\
1635 "packuswb %%mm5, %%mm2 \n\t"\
1636 "movq %%mm2, " #a " \n\t"\
1637
1638 #define DEINT_L5(t1,t2,a,b,c) REAL_DEINT_L5(t1,t2,a,b,c)
1639
1640 DEINT_L5(%%mm0, %%mm1, (%0) , (%%REGa) , (%%REGa, %1) )
1641 DEINT_L5(%%mm1, %%mm0, (%%REGa) , (%%REGa, %1) , (%%REGa, %1, 2))
1642 DEINT_L5(%%mm0, %%mm1, (%%REGa, %1) , (%%REGa, %1, 2), (%0, %1, 4) )
1643 DEINT_L5(%%mm1, %%mm0, (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
1644 DEINT_L5(%%mm0, %%mm1, (%0, %1, 4) , (%%REGd) , (%%REGd, %1) )
1645 DEINT_L5(%%mm1, %%mm0, (%%REGd) , (%%REGd, %1) , (%%REGd, %1, 2))
1646 DEINT_L5(%%mm0, %%mm1, (%%REGd, %1) , (%%REGd, %1, 2), (%0, %1, 8) )
1647 DEINT_L5(%%mm1, %%mm0, (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
1648
1649 "movq %%mm0, (%2) \n\t"
1650 "movq %%mm1, (%3) \n\t"
1651 : : "r" (src), "r" ((x86_reg)stride), "r"(tmp), "r"(tmp2)
1652 : "%"REG_a, "%"REG_d
1653 );
1654 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1655 int x;
1656 src+= stride*4;
1657 for(x=0; x<8; x++){
1658 int t1= tmp[x];
1659 int t2= tmp2[x];
1660 int t3= src[0];
1661
1662 src[stride*0]= CLIP((-(t1 + src[stride*2]) + 2*(t2 + src[stride*1]) + 6*t3 + 4)>>3);
1663 t1= src[stride*1];
1664 src[stride*1]= CLIP((-(t2 + src[stride*3]) + 2*(t3 + src[stride*2]) + 6*t1 + 4)>>3);
1665 t2= src[stride*2];
1666 src[stride*2]= CLIP((-(t3 + src[stride*4]) + 2*(t1 + src[stride*3]) + 6*t2 + 4)>>3);
1667 t3= src[stride*3];
1668 src[stride*3]= CLIP((-(t1 + src[stride*5]) + 2*(t2 + src[stride*4]) + 6*t3 + 4)>>3);
1669 t1= src[stride*4];
1670 src[stride*4]= CLIP((-(t2 + src[stride*6]) + 2*(t3 + src[stride*5]) + 6*t1 + 4)>>3);
1671 t2= src[stride*5];
1672 src[stride*5]= CLIP((-(t3 + src[stride*7]) + 2*(t1 + src[stride*6]) + 6*t2 + 4)>>3);
1673 t3= src[stride*6];
1674 src[stride*6]= CLIP((-(t1 + src[stride*8]) + 2*(t2 + src[stride*7]) + 6*t3 + 4)>>3);
1675 t1= src[stride*7];
1676 src[stride*7]= CLIP((-(t2 + src[stride*9]) + 2*(t3 + src[stride*8]) + 6*t1 + 4)>>3);
1677
1678 tmp[x]= t3;
1679 tmp2[x]= t1;
1680
1681 src++;
1682 }
1683 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1684 }
1685
1686 /**
1687 * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
1688 * will be called for every 8x8 block and can read & write from line 4-15
1689 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1690 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1691 * this filter will read lines 4-13 and write 4-11
1692 */
1693 static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride, uint8_t *tmp)
1694 {
1695 #if HAVE_MMX2 || HAVE_AMD3DNOW
1696 src+= 4*stride;
1697 __asm__ volatile(
1698 "lea (%0, %1), %%"REG_a" \n\t"
1699 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1700 // 0 1 2 3 4 5 6 7 8 9
1701 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
1702
1703 "movq (%2), %%mm0 \n\t" // L0
1704 "movq (%%"REG_a"), %%mm1 \n\t" // L2
1705 PAVGB(%%mm1, %%mm0) // L0+L2
1706 "movq (%0), %%mm2 \n\t" // L1
1707 PAVGB(%%mm2, %%mm0)
1708 "movq %%mm0, (%0) \n\t"
1709 "movq (%%"REG_a", %1), %%mm0 \n\t" // L3
1710 PAVGB(%%mm0, %%mm2) // L1+L3
1711 PAVGB(%%mm1, %%mm2) // 2L2 + L1 + L3
1712 "movq %%mm2, (%%"REG_a") \n\t"
1713 "movq (%%"REG_a", %1, 2), %%mm2 \n\t" // L4
1714 PAVGB(%%mm2, %%mm1) // L2+L4
1715 PAVGB(%%mm0, %%mm1) // 2L3 + L2 + L4
1716 "movq %%mm1, (%%"REG_a", %1) \n\t"
1717 "movq (%0, %1, 4), %%mm1 \n\t" // L5
1718 PAVGB(%%mm1, %%mm0) // L3+L5
1719 PAVGB(%%mm2, %%mm0) // 2L4 + L3 + L5
1720 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
1721 "movq (%%"REG_d"), %%mm0 \n\t" // L6
1722 PAVGB(%%mm0, %%mm2) // L4+L6
1723 PAVGB(%%mm1, %%mm2) // 2L5 + L4 + L6
1724 "movq %%mm2, (%0, %1, 4) \n\t"
1725 "movq (%%"REG_d", %1), %%mm2 \n\t" // L7
1726 PAVGB(%%mm2, %%mm1) // L5+L7
1727 PAVGB(%%mm0, %%mm1) // 2L6 + L5 + L7
1728 "movq %%mm1, (%%"REG_d") \n\t"
1729 "movq (%%"REG_d", %1, 2), %%mm1 \n\t" // L8
1730 PAVGB(%%mm1, %%mm0) // L6+L8
1731 PAVGB(%%mm2, %%mm0) // 2L7 + L6 + L8
1732 "movq %%mm0, (%%"REG_d", %1) \n\t"
1733 "movq (%0, %1, 8), %%mm0 \n\t" // L9
1734 PAVGB(%%mm0, %%mm2) // L7+L9
1735 PAVGB(%%mm1, %%mm2) // 2L8 + L7 + L9
1736 "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
1737 "movq %%mm1, (%2) \n\t"
1738
1739 : : "r" (src), "r" ((x86_reg)stride), "r" (tmp)
1740 : "%"REG_a, "%"REG_d
1741 );
1742 #else //HAVE_MMX2 || HAVE_AMD3DNOW
1743 int a, b, c, x;
1744 src+= 4*stride;
1745
1746 for(x=0; x<2; x++){
1747 a= *(uint32_t*)&tmp[stride*0];
1748 b= *(uint32_t*)&src[stride*0];
1749 c= *(uint32_t*)&src[stride*1];
1750 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1751 *(uint32_t*)&src[stride*0]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1752
1753 a= *(uint32_t*)&src[stride*2];
1754 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1755 *(uint32_t*)&src[stride*1]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1756
1757 b= *(uint32_t*)&src[stride*3];
1758 c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
1759 *(uint32_t*)&src[stride*2]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
1760
1761 c= *(uint32_t*)&src[stride*4];
1762 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1763 *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1764
1765 a= *(uint32_t*)&src[stride*5];
1766 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1767 *(uint32_t*)&src[stride*4]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1768
1769 b= *(uint32_t*)&src[stride*6];
1770 c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
1771 *(uint32_t*)&src[stride*5]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
1772
1773 c= *(uint32_t*)&src[stride*7];
1774 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1775 *(uint32_t*)&src[stride*6]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1776
1777 a= *(uint32_t*)&src[stride*8];
1778 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1779 *(uint32_t*)&src[stride*7]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1780
1781 *(uint32_t*)&tmp[stride*0]= c;
1782 src += 4;
1783 tmp += 4;
1784 }
1785 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
1786 }
1787
1788 /**
1789 * Deinterlaces the given block by applying a median filter to every second line.
1790 * will be called for every 8x8 block and can read & write from line 4-15,
1791 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
1792 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1793 */
1794 static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
1795 {
1796 #if HAVE_MMX
1797 src+= 4*stride;
1798 #if HAVE_MMX2
1799 __asm__ volatile(
1800 "lea (%0, %1), %%"REG_a" \n\t"
1801 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1802 // 0 1 2 3 4 5 6 7 8 9
1803 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
1804
1805 "movq (%0), %%mm0 \n\t" //
1806 "movq (%%"REG_a", %1), %%mm2 \n\t" //
1807 "movq (%%"REG_a"), %%mm1 \n\t" //
1808 "movq %%mm0, %%mm3 \n\t"
1809 "pmaxub %%mm1, %%mm0 \n\t" //
1810 "pminub %%mm3, %%mm1 \n\t" //
1811 "pmaxub %%mm2, %%mm1 \n\t" //
1812 "pminub %%mm1, %%mm0 \n\t"
1813 "movq %%mm0, (%%"REG_a") \n\t"
1814
1815 "movq (%0, %1, 4), %%mm0 \n\t" //
1816 "movq (%%"REG_a", %1, 2), %%mm1 \n\t" //
1817 "movq %%mm2, %%mm3 \n\t"
1818 "pmaxub %%mm1, %%mm2 \n\t" //
1819 "pminub %%mm3, %%mm1 \n\t" //
1820 "pmaxub %%mm0, %%mm1 \n\t" //
1821 "pminub %%mm1, %%mm2 \n\t"
1822 "movq %%mm2, (%%"REG_a", %1, 2) \n\t"
1823
1824 "movq (%%"REG_d"), %%mm2 \n\t" //
1825 "movq (%%"REG_d", %1), %%mm1 \n\t" //
1826 "movq %%mm2, %%mm3 \n\t"
1827 "pmaxub %%mm0, %%mm2 \n\t" //
1828 "pminub %%mm3, %%mm0 \n\t" //
1829 "pmaxub %%mm1, %%mm0 \n\t" //
1830 "pminub %%mm0, %%mm2 \n\t"
1831 "movq %%mm2, (%%"REG_d") \n\t"
1832
1833 "movq (%%"REG_d", %1, 2), %%mm2 \n\t" //
1834 "movq (%0, %1, 8), %%mm0 \n\t" //
1835 "movq %%mm2, %%mm3 \n\t"
1836 "pmaxub %%mm0, %%mm2 \n\t" //
1837 "pminub %%mm3, %%mm0 \n\t" //
1838 "pmaxub %%mm1, %%mm0 \n\t" //
1839 "pminub %%mm0, %%mm2 \n\t"
1840 "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
1841
1842
1843 : : "r" (src), "r" ((x86_reg)stride)
1844 : "%"REG_a, "%"REG_d
1845 );
1846
1847 #else // MMX without MMX2
1848 __asm__ volatile(
1849 "lea (%0, %1), %%"REG_a" \n\t"
1850 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1851 // 0 1 2 3 4 5 6 7 8 9
1852 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
1853 "pxor %%mm7, %%mm7 \n\t"
1854
1855 #define REAL_MEDIAN(a,b,c)\
1856 "movq " #a ", %%mm0 \n\t"\
1857 "movq " #b ", %%mm2 \n\t"\
1858 "movq " #c ", %%mm1 \n\t"\
1859 "movq %%mm0, %%mm3 \n\t"\
1860 "movq %%mm1, %%mm4 \n\t"\
1861 "movq %%mm2, %%mm5 \n\t"\
1862 "psubusb %%mm1, %%mm3 \n\t"\
1863 "psubusb %%mm2, %%mm4 \n\t"\
1864 "psubusb %%mm0, %%mm5 \n\t"\
1865 "pcmpeqb %%mm7, %%mm3 \n\t"\
1866 "pcmpeqb %%mm7, %%mm4 \n\t"\
1867 "pcmpeqb %%mm7, %%mm5 \n\t"\
1868 "movq %%mm3, %%mm6 \n\t"\
1869 "pxor %%mm4, %%mm3 \n\t"\
1870 "pxor %%mm5, %%mm4 \n\t"\
1871 "pxor %%mm6, %%mm5 \n\t"\
1872 "por %%mm3, %%mm1 \n\t"\
1873 "por %%mm4, %%mm2 \n\t"\
1874 "por %%mm5, %%mm0 \n\t"\
1875 "pand %%mm2, %%mm0 \n\t"\
1876 "pand %%mm1, %%mm0 \n\t"\
1877 "movq %%mm0, " #b " \n\t"
1878 #define MEDIAN(a,b,c) REAL_MEDIAN(a,b,c)
1879
1880 MEDIAN((%0) , (%%REGa) , (%%REGa, %1))
1881 MEDIAN((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4))
1882 MEDIAN((%0, %1, 4) , (%%REGd) , (%%REGd, %1))
1883 MEDIAN((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8))
1884
1885 : : "r" (src), "r" ((x86_reg)stride)
1886 : "%"REG_a, "%"REG_d
1887 );
1888 #endif //HAVE_MMX2
1889 #else //HAVE_MMX
1890 int x, y;
1891 src+= 4*stride;
1892 // FIXME - there should be a way to do a few columns in parallel like w/mmx
1893 for(x=0; x<8; x++){
1894 uint8_t *colsrc = src;
1895 for (y=0; y<4; y++){
1896 int a, b, c, d, e, f;
1897 a = colsrc[0 ];
1898 b = colsrc[stride ];
1899 c = colsrc[stride*2];
1900 d = (a-b)>>31;
1901 e = (b-c)>>31;
1902 f = (c-a)>>31;
1903 colsrc[stride ] = (a|(d^f)) & (b|(d^e)) & (c|(e^f));
1904 colsrc += stride*2;
1905 }
1906 src++;
1907 }
1908 #endif //HAVE_MMX
1909 }
1910
1911 #if HAVE_MMX
1912 /**
1913 * transposes and shift the given 8x8 Block into dst1 and dst2
1914 */
1915 static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
1916 {
1917 __asm__(
1918 "lea (%0, %1), %%"REG_a" \n\t"
1919 // 0 1 2 3 4 5 6 7 8 9
1920 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
1921 "movq (%0), %%mm0 \n\t" // 12345678
1922 "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
1923 "movq %%mm0, %%mm2 \n\t" // 12345678
1924 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
1925 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
1926
1927 "movq (%%"REG_a", %1), %%mm1 \n\t"
1928 "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
1929 "movq %%mm1, %%mm4 \n\t"
1930 "punpcklbw %%mm3, %%mm1 \n\t"
1931 "punpckhbw %%mm3, %%mm4 \n\t"
1932
1933 "movq %%mm0, %%mm3 \n\t"
1934 "punpcklwd %%mm1, %%mm0 \n\t"
1935 "punpckhwd %%mm1, %%mm3 \n\t"
1936 "movq %%mm2, %%mm1 \n\t"
1937 "punpcklwd %%mm4, %%mm2 \n\t"
1938 "punpckhwd %%mm4, %%mm1 \n\t"
1939
1940 "movd %%mm0, 128(%2) \n\t"
1941 "psrlq $32, %%mm0 \n\t"
1942 "movd %%mm0, 144(%2) \n\t"
1943 "movd %%mm3, 160(%2) \n\t"
1944 "psrlq $32, %%mm3 \n\t"
1945 "movd %%mm3, 176(%2) \n\t"
1946 "movd %%mm3, 48(%3) \n\t"
1947 "movd %%mm2, 192(%2) \n\t"
1948 "movd %%mm2, 64(%3) \n\t"
1949 "psrlq $32, %%mm2 \n\t"
1950 "movd %%mm2, 80(%3) \n\t"
1951 "movd %%mm1, 96(%3) \n\t"
1952 "psrlq $32, %%mm1 \n\t"
1953 "movd %%mm1, 112(%3) \n\t"
1954
1955 "lea (%%"REG_a", %1, 4), %%"REG_a" \n\t"
1956
1957 "movq (%0, %1, 4), %%mm0 \n\t" // 12345678
1958 "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
1959 "movq %%mm0, %%mm2 \n\t" // 12345678
1960 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
1961 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
1962
1963 "movq (%%"REG_a", %1), %%mm1 \n\t"
1964 "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
1965 "movq %%mm1, %%mm4 \n\t"
1966 "punpcklbw %%mm3, %%mm1 \n\t"
1967 "punpckhbw %%mm3, %%mm4 \n\t"
1968
1969 "movq %%mm0, %%mm3 \n\t"
1970 "punpcklwd %%mm1, %%mm0 \n\t"
1971 "punpckhwd %%mm1, %%mm3 \n\t"
1972 "movq %%mm2, %%mm1 \n\t"
1973 "punpcklwd %%mm4, %%mm2 \n\t"
1974 "punpckhwd %%mm4, %%mm1 \n\t"
1975
1976 "movd %%mm0, 132(%2) \n\t"
1977 "psrlq $32, %%mm0 \n\t"
1978 "movd %%mm0, 148(%2) \n\t"
1979 "movd %%mm3, 164(%2) \n\t"
1980 "psrlq $32, %%mm3 \n\t"
1981 "movd %%mm3, 180(%2) \n\t"
1982 "movd %%mm3, 52(%3) \n\t"
1983 "movd %%mm2, 196(%2) \n\t"
1984 "movd %%mm2, 68(%3) \n\t"
1985 "psrlq $32, %%mm2 \n\t"
1986 "movd %%mm2, 84(%3) \n\t"
1987 "movd %%mm1, 100(%3) \n\t"
1988 "psrlq $32, %%mm1 \n\t"
1989 "movd %%mm1, 116(%3) \n\t"
1990
1991
1992 :: "r" (src), "r" ((x86_reg)srcStride), "r" (dst1), "r" (dst2)
1993 : "%"REG_a
1994 );
1995 }
1996
1997 /**
1998 * transposes the given 8x8 block
1999 */
2000 static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
2001 {
2002 __asm__(
2003 "lea (%0, %1), %%"REG_a" \n\t"
2004 "lea (%%"REG_a",%1,4), %%"REG_d" \n\t"
2005 // 0 1 2 3 4 5 6 7 8 9
2006 // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
2007 "movq (%2), %%mm0 \n\t" // 12345678
2008 "movq 16(%2), %%mm1 \n\t" // abcdefgh
2009 "movq %%mm0, %%mm2 \n\t" // 12345678
2010 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2011 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2012
2013 "movq 32(%2), %%mm1 \n\t"
2014 "movq 48(%2), %%mm3 \n\t"
2015 "movq %%mm1, %%mm4 \n\t"
2016 "punpcklbw %%mm3, %%mm1 \n\t"
2017 "punpckhbw %%mm3, %%mm4 \n\t"
2018
2019 "movq %%mm0, %%mm3 \n\t"
2020 "punpcklwd %%mm1, %%mm0 \n\t"
2021 "punpckhwd %%mm1, %%mm3 \n\t"
2022 "movq %%mm2, %%mm1 \n\t"
2023 "punpcklwd %%mm4, %%mm2 \n\t"
2024 "punpckhwd %%mm4, %%mm1 \n\t"
2025
2026 "movd %%mm0, (%0) \n\t"
2027 "psrlq $32, %%mm0 \n\t"
2028 "movd %%mm0, (%%"REG_a") \n\t"
2029 "movd %%mm3, (%%"REG_a", %1) \n\t"
2030 "psrlq $32, %%mm3 \n\t"
2031 "movd %%mm3, (%%"REG_a", %1, 2) \n\t"
2032 "movd %%mm2, (%0, %1, 4) \n\t"
2033 "psrlq $32, %%mm2 \n\t"
2034 "movd %%mm2, (%%"REG_d") \n\t"
2035 "movd %%mm1, (%%"REG_d", %1) \n\t"
2036 "psrlq $32, %%mm1 \n\t"
2037 "movd %%mm1, (%%"REG_d", %1, 2) \n\t"
2038
2039
2040 "movq 64(%2), %%mm0 \n\t" // 12345678
2041 "movq 80(%2), %%mm1 \n\t" // abcdefgh
2042 "movq %%mm0, %%mm2 \n\t" // 12345678
2043 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2044 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2045
2046 "movq 96(%2), %%mm1 \n\t"
2047 "movq 112(%2), %%mm3 \n\t"
2048 "movq %%mm1, %%mm4 \n\t"
2049 "punpcklbw %%mm3, %%mm1 \n\t"
2050 "punpckhbw %%mm3, %%mm4 \n\t"
2051
2052 "movq %%mm0, %%mm3 \n\t"
2053 "punpcklwd %%mm1, %%mm0 \n\t"
2054 "punpckhwd %%mm1, %%mm3 \n\t"
2055 "movq %%mm2, %%mm1 \n\t"
2056 "punpcklwd %%mm4, %%mm2 \n\t"
2057 "punpckhwd %%mm4, %%mm1 \n\t"
2058
2059 "movd %%mm0, 4(%0) \n\t"
2060 "psrlq $32, %%mm0 \n\t"
2061 "movd %%mm0, 4(%%"REG_a") \n\t"
2062 "movd %%mm3, 4(%%"REG_a", %1) \n\t"
2063 "psrlq $32, %%mm3 \n\t"
2064 "movd %%mm3, 4(%%"REG_a", %1, 2) \n\t"
2065 "movd %%mm2, 4(%0, %1, 4) \n\t"
2066 "psrlq $32, %%mm2 \n\t"
2067 "movd %%mm2, 4(%%"REG_d") \n\t"
2068 "movd %%mm1, 4(%%"REG_d", %1) \n\t"
2069 "psrlq $32, %%mm1 \n\t"
2070 "movd %%mm1, 4(%%"REG_d", %1, 2) \n\t"
2071
2072 :: "r" (dst), "r" ((x86_reg)dstStride), "r" (src)
2073 : "%"REG_a, "%"REG_d
2074 );
2075 }
2076 #endif //HAVE_MMX
2077 //static long test=0;
2078
2079 #if !HAVE_ALTIVEC
2080 static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
2081 uint8_t *tempBlurred, uint32_t *tempBlurredPast, int *maxNoise)
2082 {
2083 // to save a register (FIXME do this outside of the loops)
2084 tempBlurredPast[127]= maxNoise[0];
2085 tempBlurredPast[128]= maxNoise[1];
2086 tempBlurredPast[129]= maxNoise[2];
2087
2088 #define FAST_L2_DIFF
2089 //#define L1_DIFF //u should change the thresholds too if u try that one
2090 #if HAVE_MMX2 || HAVE_AMD3DNOW
2091 __asm__ volatile(
2092 "lea (%2, %2, 2), %%"REG_a" \n\t" // 3*stride
2093 "lea (%2, %2, 4), %%"REG_d" \n\t" // 5*stride
2094 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
2095 // 0 1 2 3 4 5 6 7 8 9
2096 // %x %x+%2 %x+2%2 %x+eax %x+4%2 %x+edx %x+2eax %x+ecx %x+8%2
2097 //FIXME reorder?
2098 #ifdef L1_DIFF //needs mmx2
2099 "movq (%0), %%mm0 \n\t" // L0
2100 "psadbw (%1), %%mm0 \n\t" // |L0-R0|
2101 "movq (%0, %2), %%mm1 \n\t" // L1
2102 "psadbw (%1, %2), %%mm1 \n\t" // |L1-R1|
2103 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2104 "psadbw (%1, %2, 2), %%mm2 \n\t" // |L2-R2|
2105 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2106 "psadbw (%1, %%"REG_a"), %%mm3 \n\t" // |L3-R3|
2107
2108 "movq (%0, %2, 4), %%mm4 \n\t" // L4
2109 "paddw %%mm1, %%mm0 \n\t"
2110 "psadbw (%1, %2, 4), %%mm4 \n\t" // |L4-R4|
2111 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
2112 "paddw %%mm2, %%mm0 \n\t"
2113 "psadbw (%1, %%"REG_d"), %%mm5 \n\t" // |L5-R5|
2114 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
2115 "paddw %%mm3, %%mm0 \n\t"
2116 "psadbw (%1, %%"REG_a", 2), %%mm6 \n\t" // |L6-R6|
2117 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
2118 "paddw %%mm4, %%mm0 \n\t"
2119 "psadbw (%1, %%"REG_c"), %%mm7 \n\t" // |L7-R7|
2120 "paddw %%mm5, %%mm6 \n\t"
2121 "paddw %%mm7, %%mm6 \n\t"
2122 "paddw %%mm6, %%mm0 \n\t"
2123 #else //L1_DIFF
2124 #if defined (FAST_L2_DIFF)
2125 "pcmpeqb %%mm7, %%mm7 \n\t"
2126 "movq "MANGLE(b80)", %%mm6 \n\t"
2127 "pxor %%mm0, %%mm0 \n\t"
2128 #define REAL_L2_DIFF_CORE(a, b)\
2129 "movq " #a ", %%mm5 \n\t"\
2130 "movq " #b ", %%mm2 \n\t"\
2131 "pxor %%mm7, %%mm2 \n\t"\
2132 PAVGB(%%mm2, %%mm5)\
2133 "paddb %%mm6, %%mm5 \n\t"\
2134 "movq %%mm5, %%mm2 \n\t"\
2135 "psllw $8, %%mm5 \n\t"\
2136 "pmaddwd %%mm5, %%mm5 \n\t"\
2137 "pmaddwd %%mm2, %%mm2 \n\t"\
2138 "paddd %%mm2, %%mm5 \n\t"\
2139 "psrld $14, %%mm5 \n\t"\
2140 "paddd %%mm5, %%mm0 \n\t"
2141
2142 #else //defined (FAST_L2_DIFF)
2143 "pxor %%mm7, %%mm7 \n\t"
2144 "pxor %%mm0, %%mm0 \n\t"
2145 #define REAL_L2_DIFF_CORE(a, b)\
2146 "movq " #a ", %%mm5 \n\t"\
2147 "movq " #b ", %%mm2 \n\t"\
2148 "movq %%mm5, %%mm1 \n\t"\
2149 "movq %%mm2, %%mm3 \n\t"\
2150 "punpcklbw %%mm7, %%mm5 \n\t"\
2151 "punpckhbw %%mm7, %%mm1 \n\t"\
2152 "punpcklbw %%mm7, %%mm2 \n\t"\
2153 "punpckhbw %%mm7, %%mm3 \n\t"\
2154 "psubw %%mm2, %%mm5 \n\t"\
2155 "psubw %%mm3, %%mm1 \n\t"\
2156 "pmaddwd %%mm5, %%mm5 \n\t"\
2157 "pmaddwd %%mm1, %%mm1 \n\t"\
2158 "paddd %%mm1, %%mm5 \n\t"\
2159 "paddd %%mm5, %%mm0 \n\t"
2160
2161 #endif //defined (FAST_L2_DIFF)
2162
2163 #define L2_DIFF_CORE(a, b) REAL_L2_DIFF_CORE(a, b)
2164
2165 L2_DIFF_CORE((%0) , (%1))
2166 L2_DIFF_CORE((%0, %2) , (%1, %2))
2167 L2_DIFF_CORE((%0, %2, 2) , (%1, %2, 2))
2168 L2_DIFF_CORE((%0, %%REGa) , (%1, %%REGa))
2169 L2_DIFF_CORE((%0, %2, 4) , (%1, %2, 4))
2170 L2_DIFF_CORE((%0, %%REGd) , (%1, %%REGd))
2171 L2_DIFF_CORE((%0, %%REGa,2), (%1, %%REGa,2))
2172 L2_DIFF_CORE((%0, %%REGc) , (%1, %%REGc))
2173
2174 #endif //L1_DIFF
2175
2176 "movq %%mm0, %%mm4 \n\t"
2177 "psrlq $32, %%mm0 \n\t"
2178 "paddd %%mm0, %%mm4 \n\t"
2179 "movd %%mm4, %%ecx \n\t"
2180 "shll $2, %%ecx \n\t"
2181 "mov %3, %%"REG_d" \n\t"
2182 "addl -4(%%"REG_d"), %%ecx \n\t"
2183 "addl 4(%%"REG_d"), %%ecx \n\t"
2184 "addl -1024(%%"REG_d"), %%ecx \n\t"
2185 "addl $4, %%ecx \n\t"
2186 "addl 1024(%%"REG_d"), %%ecx \n\t"
2187 "shrl $3, %%ecx \n\t"
2188 "movl %%ecx, (%%"REG_d") \n\t"
2189
2190 // "mov %3, %%"REG_c" \n\t"
2191 // "mov %%"REG_c", test \n\t"
2192 // "jmp 4f \n\t"
2193 "cmpl 512(%%"REG_d"), %%ecx \n\t"
2194 " jb 2f \n\t"
2195 "cmpl 516(%%"REG_d"), %%ecx \n\t"
2196 " jb 1f \n\t"
2197
2198 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2199 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
2200 "movq (%0), %%mm0 \n\t" // L0
2201 "movq (%0, %2), %%mm1 \n\t" // L1
2202 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2203 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2204 "movq (%0, %2, 4), %%mm4 \n\t" // L4
2205 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
2206 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
2207 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
2208 "movq %%mm0, (%1) \n\t" // L0
2209 "movq %%mm1, (%1, %2) \n\t" // L1
2210 "movq %%mm2, (%1, %2, 2) \n\t" // L2
2211 "movq %%mm3, (%1, %%"REG_a") \n\t" // L3
2212 "movq %%mm4, (%1, %2, 4) \n\t" // L4
2213 "movq %%mm5, (%1, %%"REG_d") \n\t" // L5
2214 "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // L6
2215 "movq %%mm7, (%1, %%"REG_c") \n\t" // L7
2216 "jmp 4f \n\t"
2217
2218 "1: \n\t"
2219 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2220 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
2221 "movq (%0), %%mm0 \n\t" // L0
2222 PAVGB((%1), %%mm0) // L0
2223 "movq (%0, %2), %%mm1 \n\t" // L1
2224 PAVGB((%1, %2), %%mm1) // L1
2225 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2226 PAVGB((%1, %2, 2), %%mm2) // L2
2227 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2228 PAVGB((%1, %%REGa), %%mm3) // L3
2229 "movq (%0, %2, 4), %%mm4 \n\t" // L4
2230 PAVGB((%1, %2, 4), %%mm4) // L4
2231 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
2232 PAVGB((%1, %%REGd), %%mm5) // L5
2233 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
2234 PAVGB((%1, %%REGa, 2), %%mm6) // L6
2235 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
2236 PAVGB((%1, %%REGc), %%mm7) // L7
2237 "movq %%mm0, (%1) \n\t" // R0
2238 "movq %%mm1, (%1, %2) \n\t" // R1
2239 "movq %%mm2, (%1, %2, 2) \n\t" // R2
2240 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
2241 "movq %%mm4, (%1, %2, 4) \n\t" // R4
2242 "movq %%mm5, (%1, %%"REG_d") \n\t" // R5
2243 "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // R6
2244 "movq %%mm7, (%1, %%"REG_c") \n\t" // R7
2245 "movq %%mm0, (%0) \n\t" // L0
2246 "movq %%mm1, (%0, %2) \n\t" // L1
2247 "movq %%mm2, (%0, %2, 2) \n\t" // L2
2248 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
2249 "movq %%mm4, (%0, %2, 4) \n\t" // L4
2250 "movq %%mm5, (%0, %%"REG_d") \n\t" // L5
2251 "movq %%mm6, (%0, %%"REG_a", 2) \n\t" // L6
2252 "movq %%mm7, (%0, %%"REG_c") \n\t" // L7
2253 "jmp 4f \n\t"
2254
2255 "2: \n\t"
2256 "cmpl 508(%%"REG_d"), %%ecx \n\t"
2257 " jb 3f \n\t"
2258
2259 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2260 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
2261 "movq (%0), %%mm0 \n\t" // L0
2262 "movq (%0, %2), %%mm1 \n\t" // L1
2263 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2264 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2265 "movq (%1), %%mm4 \n\t" // R0
2266 "movq (%1, %2), %%mm5 \n\t" // R1
2267 "movq (%1, %2, 2), %%mm6 \n\t" // R2
2268 "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
2269 PAVGB(%%mm4, %%mm0)
2270 PAVGB(%%mm5, %%mm1)
2271 PAVGB(%%mm6, %%mm2)
2272 PAVGB(%%mm7, %%mm3)
2273 PAVGB(%%mm4, %%mm0)
2274 PAVGB(%%mm5, %%mm1)
2275 PAVGB(%%mm6, %%mm2)
2276 PAVGB(%%mm7, %%mm3)
2277 "movq %%mm0, (%1) \n\t" // R0
2278 "movq %%mm1, (%1, %2) \n\t" // R1
2279 "movq %%mm2, (%1, %2, 2) \n\t" // R2
2280 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
2281 "movq %%mm0, (%0) \n\t" // L0
2282 "movq %%mm1, (%0, %2) \n\t" // L1
2283 "movq %%mm2, (%0, %2, 2) \n\t" // L2
2284 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
2285
2286 "movq (%0, %2, 4), %%mm0 \n\t" // L4
2287 "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
2288 "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
2289 "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
2290 "movq (%1, %2, 4), %%mm4 \n\t" // R4
2291 "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
2292 "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
2293 "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
2294 PAVGB(%%mm4, %%mm0)
2295 PAVGB(%%mm5, %%mm1)
2296 PAVGB(%%mm6, %%mm2)
2297 PAVGB(%%mm7, %%mm3)
2298 PAVGB(%%mm4, %%mm0)
2299 PAVGB(%%mm5, %%mm1)
2300 PAVGB(%%mm6, %%mm2)
2301 PAVGB(%%mm7, %%mm3)
2302 "movq %%mm0, (%1, %2, 4) \n\t" // R4
2303 "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
2304 "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
2305 "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
2306 "movq %%mm0, (%0, %2, 4) \n\t" // L4
2307 "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
2308 "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
2309 "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
2310 "jmp 4f \n\t"
2311
2312 "3: \n\t"
2313 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2314 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
2315 "movq (%0), %%mm0 \n\t" // L0
2316 "movq (%0, %2), %%mm1 \n\t" // L1
2317 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2318 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2319 "movq (%1), %%mm4 \n\t" // R0
2320 "movq (%1, %2), %%mm5 \n\t" // R1
2321 "movq (%1, %2, 2), %%mm6 \n\t" // R2
2322 "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
2323 PAVGB(%%mm4, %%mm0)
2324 PAVGB(%%mm5, %%mm1)
2325 PAVGB(%%mm6, %%mm2)
2326 PAVGB(%%mm7, %%mm3)
2327 PAVGB(%%mm4, %%mm0)
2328 PAVGB(%%mm5, %%mm1)
2329 PAVGB(%%mm6, %%mm2)
2330 PAVGB(%%mm7, %%mm3)
2331 PAVGB(%%mm4, %%mm0)
2332 PAVGB(%%mm5, %%mm1)
2333 PAVGB(%%mm6, %%mm2)
2334 PAVGB(%%mm7, %%mm3)
2335 "movq %%mm0, (%1) \n\t" // R0
2336 "movq %%mm1, (%1, %2) \n\t" // R1
2337 "movq %%mm2, (%1, %2, 2) \n\t" // R2
2338 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
2339 "movq %%mm0, (%0) \n\t" // L0
2340 "movq %%mm1, (%0, %2) \n\t" // L1
2341 "movq %%mm2, (%0, %2, 2) \n\t" // L2
2342 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
2343
2344 "movq (%0, %2, 4), %%mm0 \n\t" // L4
2345 "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
2346 "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
2347 "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
2348 "movq (%1, %2, 4), %%mm4 \n\t" // R4
2349 "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
2350 "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
2351 "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
2352 PAVGB(%%mm4, %%mm0)
2353 PAVGB(%%mm5, %%mm1)
2354 PAVGB(%%mm6, %%mm2)
2355 PAVGB(%%mm7, %%mm3)
2356 PAVGB(%%mm4, %%mm0)
2357 PAVGB(%%mm5, %%mm1)
2358 PAVGB(%%mm6, %%mm2)
2359 PAVGB(%%mm7, %%mm3)
2360 PAVGB(%%mm4, %%mm0)
2361 PAVGB(%%mm5, %%mm1)
2362 PAVGB(%%mm6, %%mm2)
2363 PAVGB(%%mm7, %%mm3)
2364 "movq %%mm0, (%1, %2, 4) \n\t" // R4
2365 "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
2366 "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
2367 "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
2368 "movq %%mm0, (%0, %2, 4) \n\t" // L4
2369 "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
2370 "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
2371 "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
2372
2373 "4: \n\t"
2374
2375 :: "r" (src), "r" (tempBlurred), "r"((x86_reg)stride), "m" (tempBlurredPast)
2376 : "%"REG_a, "%"REG_d, "%"REG_c, "memory"
2377 );
2378 #else //HAVE_MMX2 || HAVE_AMD3DNOW
2379 {
2380 int y;
2381 int d=0;
2382 // int sysd=0;
2383 int i;
2384
2385 for(y=0; y<8; y++){
2386 int x;
2387 for(x=0; x<8; x++){
2388 int ref= tempBlurred[ x + y*stride ];
2389 int cur= src[ x + y*stride ];
2390 int d1=ref - cur;
2391 // if(x==0 || x==7) d1+= d1>>1;
2392 // if(y==0 || y==7) d1+= d1>>1;
2393 // d+= FFABS(d1);
2394 d+= d1*d1;
2395 // sysd+= d1;
2396 }
2397 }
2398 i=d;
2399 d= (
2400 4*d
2401 +(*(tempBlurredPast-256))
2402 +(*(tempBlurredPast-1))+ (*(tempBlurredPast+1))
2403 +(*(tempBlurredPast+256))
2404 +4)>>3;
2405 *tempBlurredPast=i;
2406 // ((*tempBlurredPast)*3 + d + 2)>>2;
2407
2408 /*
2409 Switch between
2410 1 0 0 0 0 0 0 (0)
2411 64 32 16 8 4 2 1 (1)
2412 64 48 36 27 20 15 11 (33) (approx)
2413 64 56 49 43 37 33 29 (200) (approx)
2414 */
2415 if(d > maxNoise[1]){
2416 if(d < maxNoise[2]){
2417 for(y=0; y<8; y++){
2418 int x;
2419 for(x=0; x<8; x++){
2420 int ref= tempBlurred[ x + y*stride ];
2421 int cur= src[ x + y*stride ];
2422 tempBlurred[ x + y*stride ]=
2423 src[ x + y*stride ]=
2424 (ref + cur + 1)>>1;
2425 }
2426 }
2427 }else{
2428 for(y=0; y<8; y++){
2429 int x;
2430 for(x=0; x<8; x++){
2431 tempBlurred[ x + y*stride ]= src[ x + y*stride ];
2432 }
2433 }
2434 }
2435 }else{
2436 if(d < maxNoise[0]){
2437 for(y=0; y<8; y++){
2438 int x;
2439 for(x=0; x<8; x++){
2440 int ref= tempBlurred[ x + y*stride ];
2441 int cur= src[ x + y*stride ];
2442 tempBlurred[ x + y*stride ]=
2443 src[ x + y*stride ]=
2444 (ref*7 + cur + 4)>>3;
2445 }
2446 }
2447 }else{
2448 for(y=0; y<8; y++){
2449 int x;
2450 for(x=0; x<8; x++){
2451 int ref= tempBlurred[ x + y*stride ];
2452 int cur= src[ x + y*stride ];
2453 tempBlurred[ x + y*stride ]=
2454 src[ x + y*stride ]=
2455 (ref*3 + cur + 2)>>2;
2456 }
2457 }
2458 }
2459 }
2460 }
2461 #endif //HAVE_MMX2 || HAVE_AMD3DNOW
2462 }
2463 #endif //HAVE_ALTIVEC
2464
2465 #if HAVE_MMX
2466 /**
2467 * accurate deblock filter
2468 */
2469 static av_always_inline void RENAME(do_a_deblock)(uint8_t *src, int step, int stride, PPContext *c){
2470 int64_t dc_mask, eq_mask, both_masks;
2471 int64_t sums[10*8*2];
2472 src+= step*3; // src points to begin of the 8x8 Block
2473 //START_TIMER
2474 __asm__ volatile(
2475 "movq %0, %%mm7 \n\t"
2476 "movq %1, %%mm6 \n\t"
2477 : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
2478 );
2479
2480 __asm__ volatile(
2481 "lea (%2, %3), %%"REG_a" \n\t"
2482 // 0 1 2 3 4 5 6 7 8 9
2483 // %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
2484
2485 "movq (%2), %%mm0 \n\t"
2486 "movq (%%"REG_a"), %%mm1 \n\t"
2487 "movq %%mm1, %%mm3 \n\t"
2488 "movq %%mm1, %%mm4 \n\t"
2489 "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
2490 "paddb %%mm7, %%mm0 \n\t"
2491 "pcmpgtb %%mm6, %%mm0 \n\t"
2492
2493 "movq (%%"REG_a",%3), %%mm2 \n\t"
2494 PMAXUB(%%mm2, %%mm4)
2495 PMINUB(%%mm2, %%mm3, %%mm5)
2496 "psubb %%mm2, %%mm1 \n\t"
2497 "paddb %%mm7, %%mm1 \n\t"
2498 "pcmpgtb %%mm6, %%mm1 \n\t"
2499 "paddb %%mm1, %%mm0 \n\t"
2500
2501 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
2502 PMAXUB(%%mm1, %%mm4)
2503 PMINUB(%%mm1, %%mm3, %%mm5)
2504 "psubb %%mm1, %%mm2 \n\t"
2505 "paddb %%mm7, %%mm2 \n\t"
2506 "pcmpgtb %%mm6, %%mm2 \n\t"
2507 "paddb %%mm2, %%mm0 \n\t"
2508
2509 "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
2510
2511 "movq (%2, %3, 4), %%mm2 \n\t"
2512 PMAXUB(%%mm2, %%mm4)
2513 PMINUB(%%mm2, %%mm3, %%mm5)
2514 "psubb %%mm2, %%mm1 \n\t"
2515 "paddb %%mm7, %%mm1 \n\t"
2516 "pcmpgtb %%mm6, %%mm1 \n\t"
2517 "paddb %%mm1, %%mm0 \n\t"
2518
2519 "movq (%%"REG_a"), %%mm1 \n\t"
2520 PMAXUB(%%mm1, %%mm4)
2521 PMINUB(%%mm1, %%mm3, %%mm5)
2522 "psubb %%mm1, %%mm2 \n\t"
2523 "paddb %%mm7, %%mm2 \n\t"
2524 "pcmpgtb %%mm6, %%mm2 \n\t"
2525 "paddb %%mm2, %%mm0 \n\t"
2526
2527 "movq (%%"REG_a", %3), %%mm2 \n\t"
2528 PMAXUB(%%mm2, %%mm4)
2529 PMINUB(%%mm2, %%mm3, %%mm5)
2530 "psubb %%mm2, %%mm1 \n\t"
2531 "paddb %%mm7, %%mm1 \n\t"
2532 "pcmpgtb %%mm6, %%mm1 \n\t"
2533 "paddb %%mm1, %%mm0 \n\t"
2534
2535 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
2536 PMAXUB(%%mm1, %%mm4)
2537 PMINUB(%%mm1, %%mm3, %%mm5)
2538 "psubb %%mm1, %%mm2 \n\t"
2539 "paddb %%mm7, %%mm2 \n\t"
2540 "pcmpgtb %%mm6, %%mm2 \n\t"
2541 "paddb %%mm2, %%mm0 \n\t"
2542
2543 "movq (%2, %3, 8), %%mm2 \n\t"
2544 PMAXUB(%%mm2, %%mm4)
2545 PMINUB(%%mm2, %%mm3, %%mm5)
2546 "psubb %%mm2, %%mm1 \n\t"
2547 "paddb %%mm7, %%mm1 \n\t"
2548 "pcmpgtb %%mm6, %%mm1 \n\t"
2549 "paddb %%mm1, %%mm0 \n\t"
2550
2551 "movq (%%"REG_a", %3, 4), %%mm1 \n\t"
2552 "psubb %%mm1, %%mm2 \n\t"
2553 "paddb %%mm7, %%mm2 \n\t"
2554 "pcmpgtb %%mm6, %%mm2 \n\t"
2555 "paddb %%mm2, %%mm0 \n\t"
2556 "psubusb %%mm3, %%mm4 \n\t"
2557
2558 "pxor %%mm6, %%mm6 \n\t"
2559 "movq %4, %%mm7 \n\t" // QP,..., QP
2560 "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
2561 "psubusb %%mm4, %%mm7 \n\t" // Diff >=2QP -> 0
2562 "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
2563 "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
2564 "movq %%mm7, %1 \n\t"
2565
2566 "movq %5, %%mm7 \n\t"
2567 "punpcklbw %%mm7, %%mm7 \n\t"
2568 "punpcklbw %%mm7, %%mm7 \n\t"
2569 "punpcklbw %%mm7, %%mm7 \n\t"
2570 "psubb %%mm0, %%mm6 \n\t"
2571 "pcmpgtb %%mm7, %%mm6 \n\t"
2572 "movq %%mm6, %0 \n\t"
2573
2574 : "=m" (eq_mask), "=m" (dc_mask)
2575 : "r" (src), "r" ((x86_reg)step), "m" (c->pQPb), "m"(c->ppMode.flatnessThreshold)
2576 : "%"REG_a
2577 );
2578
2579 both_masks = dc_mask & eq_mask;
2580
2581 if(both_masks){
2582 x86_reg offset= -8*step;
2583 int64_t *temp_sums= sums;
2584
2585 __asm__ volatile(
2586 "movq %2, %%mm0 \n\t" // QP,..., QP
2587 "pxor %%mm4, %%mm4 \n\t"
2588
2589 "movq (%0), %%mm6 \n\t"
2590 "movq (%0, %1), %%mm5 \n\t"
2591 "movq %%mm5, %%mm1 \n\t"
2592 "movq %%mm6, %%mm2 \n\t"
2593 "psubusb %%mm6, %%mm5 \n\t"
2594 "psubusb %%mm1, %%mm2 \n\t"
2595 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
2596 "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
2597 "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
2598
2599 "pxor %%mm6, %%mm1 \n\t"
2600 "pand %%mm0, %%mm1 \n\t"
2601 "pxor %%mm1, %%mm6 \n\t"
2602 // 0:QP 6:First
2603
2604 "movq (%0, %1, 8), %%mm5 \n\t"
2605 "add %1, %0 \n\t" // %0 points to line 1 not 0
2606 "movq (%0, %1, 8), %%mm7 \n\t"
2607 "movq %%mm5, %%mm1 \n\t"
2608 "movq %%mm7, %%mm2 \n\t"
2609 "psubusb %%mm7, %%mm5 \n\t"
2610 "psubusb %%mm1, %%mm2 \n\t"
2611 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
2612 "movq %2, %%mm0 \n\t" // QP,..., QP
2613 "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
2614 "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
2615
2616 "pxor %%mm7, %%mm1 \n\t"
2617 "pand %%mm0, %%mm1 \n\t"
2618 "pxor %%mm1, %%mm7 \n\t"
2619
2620 "movq %%mm6, %%mm5 \n\t"
2621 "punpckhbw %%mm4, %%mm6 \n\t"
2622 "punpcklbw %%mm4, %%mm5 \n\t"
2623 // 4:0 5/6:First 7:Last
2624
2625 "movq %%mm5, %%mm0 \n\t"
2626 "movq %%mm6, %%mm1 \n\t"
2627 "psllw $2, %%mm0 \n\t"
2628 "psllw $2, %%mm1 \n\t"
2629 "paddw "MANGLE(w04)", %%mm0 \n\t"
2630 "paddw "MANGLE(w04)", %%mm1 \n\t"
2631
2632 #define NEXT\
2633 "movq (%0), %%mm2 \n\t"\
2634 "movq (%0), %%mm3 \n\t"\
2635 "add %1, %0 \n\t"\
2636 "punpcklbw %%mm4, %%mm2 \n\t"\
2637 "punpckhbw %%mm4, %%mm3 \n\t"\
2638 "paddw %%mm2, %%mm0 \n\t"\
2639 "paddw %%mm3, %%mm1 \n\t"
2640
2641 #define PREV\
2642 "movq (%0), %%mm2 \n\t"\
2643 "movq (%0), %%mm3 \n\t"\
2644 "add %1, %0 \n\t"\
2645 "punpcklbw %%mm4, %%mm2 \n\t"\
2646 "punpckhbw %%mm4, %%mm3 \n\t"\
2647 "psubw %%mm2, %%mm0 \n\t"\
2648 "psubw %%mm3, %%mm1 \n\t"
2649
2650
2651 NEXT //0
2652 NEXT //1
2653 NEXT //2
2654 "movq %%mm0, (%3) \n\t"
2655 "movq %%mm1, 8(%3) \n\t"
2656
2657 NEXT //3
2658 "psubw %%mm5, %%mm0 \n\t"
2659 "psubw %%mm6, %%mm1 \n\t"
2660 "movq %%mm0, 16(%3) \n\t"
2661 "movq %%mm1, 24(%3) \n\t"
2662
2663 NEXT //4
2664 "psubw %%mm5, %%mm0 \n\t"
2665 "psubw %%mm6, %%mm1 \n\t"
2666 "movq %%mm0, 32(%3) \n\t"
2667 "movq %%mm1, 40(%3) \n\t"
2668
2669 NEXT //5
2670 "psubw %%mm5, %%mm0 \n\t"
2671 "psubw %%mm6, %%mm1 \n\t"
2672 "movq %%mm0, 48(%3) \n\t"
2673 "movq %%mm1, 56(%3) \n\t"
2674
2675 NEXT //6
2676 "psubw %%mm5, %%mm0 \n\t"
2677 "psubw %%mm6, %%mm1 \n\t"
2678 "movq %%mm0, 64(%3) \n\t"
2679 "movq %%mm1, 72(%3) \n\t"
2680
2681 "movq %%mm7, %%mm6 \n\t"
2682 "punpckhbw %%mm4, %%mm7 \n\t"
2683 "punpcklbw %%mm4, %%mm6 \n\t"
2684
2685 NEXT //7
2686 "mov %4, %0 \n\t"
2687 "add %1, %0 \n\t"
2688 PREV //0
2689 "movq %%mm0, 80(%3) \n\t"
2690 "movq %%mm1, 88(%3) \n\t"
2691
2692 PREV //1
2693 "paddw %%mm6, %%mm0 \n\t"
2694 "paddw %%mm7, %%mm1 \n\t"
2695 "movq %%mm0, 96(%3) \n\t"
2696 "movq %%mm1, 104(%3) \n\t"
2697
2698 PREV //2
2699 "paddw %%mm6, %%mm0 \n\t"
2700 "paddw %%mm7, %%mm1 \n\t"
2701 "movq %%mm0, 112(%3) \n\t"
2702 "movq %%mm1, 120(%3) \n\t"
2703
2704 PREV //3
2705 "paddw %%mm6, %%mm0 \n\t"
2706 "paddw %%mm7, %%mm1 \n\t"
2707 "movq %%mm0, 128(%3) \n\t"
2708 "movq %%mm1, 136(%3) \n\t"
2709
2710 PREV //4
2711 "paddw %%mm6, %%mm0 \n\t"
2712 "paddw %%mm7, %%mm1 \n\t"
2713 "movq %%mm0, 144(%3) \n\t"
2714 "movq %%mm1, 152(%3) \n\t"
2715
2716 "mov %4, %0 \n\t" //FIXME
2717
2718 : "+&r"(src)
2719 : "r" ((x86_reg)step), "m" (c->pQPb), "r"(sums), "g"(src)
2720 );
2721
2722 src+= step; // src points to begin of the 8x8 Block
2723
2724 __asm__ volatile(
2725 "movq %4, %%mm6 \n\t"
2726 "pcmpeqb %%mm5, %%mm5 \n\t"
2727 "pxor %%mm6, %%mm5 \n\t"
2728 "pxor %%mm7, %%mm7 \n\t"
2729
2730 "1: \n\t"
2731 "movq (%1), %%mm0 \n\t"
2732 "movq 8(%1), %%mm1 \n\t"
2733 "paddw 32(%1), %%mm0 \n\t"
2734 "paddw 40(%1), %%mm1 \n\t"
2735 "movq (%0, %3), %%mm2 \n\t"
2736 "movq %%mm2, %%mm3 \n\t"
2737 "movq %%mm2, %%mm4 \n\t"
2738 "punpcklbw %%mm7, %%mm2 \n\t"
2739 "punpckhbw %%mm7, %%mm3 \n\t"
2740 "paddw %%mm2, %%mm0 \n\t"
2741 "paddw %%mm3, %%mm1 \n\t"
2742 "paddw %%mm2, %%mm0 \n\t"
2743 "paddw %%mm3, %%mm1 \n\t"
2744 "psrlw $4, %%mm0 \n\t"
2745 "psrlw $4, %%mm1 \n\t"
2746 "packuswb %%mm1, %%mm0 \n\t"
2747 "pand %%mm6, %%mm0 \n\t"
2748 "pand %%mm5, %%mm4 \n\t"
2749 "por %%mm4, %%mm0 \n\t"
2750 "movq %%mm0, (%0, %3) \n\t"
2751 "add $16, %1 \n\t"
2752 "add %2, %0 \n\t"
2753 " js 1b \n\t"
2754
2755 : "+r"(offset), "+r"(temp_sums)
2756 : "r" ((x86_reg)step), "r"(src - offset), "m"(both_masks)
2757 );
2758 }else
2759 src+= step; // src points to begin of the 8x8 Block
2760
2761 if(eq_mask != -1LL){
2762 uint8_t *temp_src= src;
2763 __asm__ volatile(
2764 "pxor %%mm7, %%mm7 \n\t"
2765 "lea -40(%%"REG_SP"), %%"REG_c"