Remove disabled code cruft.
[libav.git] / libpostproc / postprocess_template.c
CommitLineData
3057fa66 1/*
b78e7197
DB
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
e5a389a1 19 */
3057fa66 20
b304569a 21/**
bad5537e 22 * @file libpostproc/postprocess_template.c
b304569a
MN
23 * mmx/mmx2/3dnow postprocess code.
24 */
25
245976da 26#include "libavutil/x86_cpu.h"
b304569a 27
6fcc9af0 28#define ALIGN_MASK "$-8"
053dea12 29
cc9b0679
MN
30#undef PAVGB
31#undef PMINUB
32#undef PMAXUB
e939e1c3 33
b250f9c6 34#if HAVE_MMX2
053dea12 35#define REAL_PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
ebc3209a 36#elif HAVE_AMD3DNOW
4471e59b 37#define REAL_PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
e939e1c3 38#endif
053dea12 39#define PAVGB(a,b) REAL_PAVGB(a,b)
3057fa66 40
b250f9c6 41#if HAVE_MMX2
2e212618 42#define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
b250f9c6 43#elif HAVE_MMX
2e212618 44#define PMINUB(b,a,t) \
16e0bf73
DB
45 "movq " #a ", " #t " \n\t"\
46 "psubusb " #b ", " #t " \n\t"\
47 "psubb " #t ", " #a " \n\t"
2e212618
MN
48#endif
49
b250f9c6 50#if HAVE_MMX2
2e212618 51#define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
b250f9c6 52#elif HAVE_MMX
2e212618 53#define PMAXUB(a,b) \
16e0bf73
DB
54 "psubusb " #a ", " #b " \n\t"\
55 "paddb " #a ", " #b " \n\t"
2e212618
MN
56#endif
57
755bfeab 58//FIXME? |255-0| = 1 (should not be a problem ...)
b250f9c6 59#if HAVE_MMX
3057fa66 60/**
acced553 61 * Check if the middle 8x8 Block in the given 8x16 block is flat
3057fa66 62 */
cb482d25 63static inline int RENAME(vertClassify)(uint8_t src[], int stride, PPContext *c){
16e0bf73
DB
64 int numEq= 0, dcOk;
65 src+= stride*4; // src points to begin of the 8x8 Block
be449fca 66 __asm__ volatile(
16e0bf73
DB
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
be449fca 72 __asm__ volatile(
16e0bf73 73 "lea (%2, %3), %%"REG_a" \n\t"
bb270c08
DB
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
16e0bf73
DB
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"
b250f9c6 139#if HAVE_MMX2
16e0bf73
DB
140 "pxor %%mm7, %%mm7 \n\t"
141 "psadbw %%mm7, %%mm0 \n\t"
cd38e322 142#else
16e0bf73
DB
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"
cd38e322 152#endif
16e0bf73
DB
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)
7cebed70 161 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
16e0bf73
DB
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 }
3057fa66 172}
64c968da 173#endif //HAVE_MMX
3057fa66 174
3057fa66 175/**
acced553 176 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
a6be8111 177 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
3057fa66 178 */
b250f9c6 179#if !HAVE_ALTIVEC
9c9e467d 180static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
3057fa66 181{
ebc3209a 182#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 183 src+= stride*3;
be449fca 184 __asm__ volatile( //"movv %0 %1 %2\n\t"
16e0bf73
DB
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 :
7cebed70 306 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
16e0bf73
DB
307 : "%"REG_a, "%"REG_c
308 );
ebc3209a 309#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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 }
ebc3209a 348#endif //HAVE_MMX2 || HAVE_AMD3DNOW
3057fa66 349}
b0ac780a 350#endif //HAVE_ALTIVEC
3057fa66 351
13e00528
A
352/**
353 * Experimental Filter 1
9f45d04d 354 * will not damage linear gradients
bd107136 355 * Flat blocks should look like they were passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
755bfeab
DB
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
13e00528 358 */
9c9e467d 359static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
13e00528 360{
ebc3209a 361#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 362 src+= stride*3;
bb270c08 363
be449fca 364 __asm__ volatile(
16e0bf73
DB
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"
bb270c08
DB
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
16e0bf73
DB
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 :
7cebed70 444 : "r" (src), "r" ((x86_reg)stride), "m" (co->pQPb)
16e0bf73
DB
445 : "%"REG_a, "%"REG_c
446 );
ebc3209a 447#else //HAVE_MMX2 || HAVE_AMD3DNOW
d5a1a995 448
16e0bf73
DB
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;
bb270c08 478 }
16e0bf73
DB
479 src++;
480 }
ebc3209a 481#endif //HAVE_MMX2 || HAVE_AMD3DNOW
13e00528
A
482}
483
b250f9c6 484#if !HAVE_ALTIVEC
9c9e467d 485static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
3057fa66 486{
ebc3209a 487#if HAVE_MMX2 || HAVE_AMD3DNOW
7f16f6e6 488/*
16e0bf73
DB
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);
7f16f6e6 501*/
16e0bf73 502 src+= stride*4;
be449fca 503 __asm__ volatile(
7f16f6e6 504
04932b0d 505#if 0 //slightly more accurate and slightly slower
16e0bf73
DB
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"
bb270c08
DB
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
16e0bf73
DB
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
7f16f6e6
MN
532// mm1= |lenergy|, mm2= l2, mm3= l3, mm7=0
533
16e0bf73
DB
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)
7f16f6e6
MN
551// mm0= SIGN(menergy), mm1= |lenergy|, mm2= l5, mm3= |menergy|, mm4=l4, mm5= l3, mm7=0
552
16e0bf73
DB
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
7f16f6e6
MN
569// mm0= SIGN(menergy), mm1= |lenergy|/8, mm2= |renergy|/8, mm3= |menergy|/8, mm7=0
570
571
16e0bf73
DB
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"
64c968da 610#endif //0
7f16f6e6 611
16e0bf73
DB
612 "lea (%0, %1), %%"REG_a" \n\t"
613 "pcmpeqb %%mm6, %%mm6 \n\t" // -1
bb270c08
DB
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
7f16f6e6
MN
617
618
16e0bf73
DB
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
7f16f6e6
MN
623// mm1=-l3-1, mm0=128-q
624
16e0bf73
DB
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
7f16f6e6
MN
635// mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
636
16e0bf73
DB
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
7f16f6e6
MN
645// mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
646
16e0bf73
DB
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
7f16f6e6
MN
655// mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
656
16e0bf73
DB
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
7f16f6e6
MN
664
665// mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
666
16e0bf73
DB
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
7f16f6e6
MN
678// mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
679
16e0bf73
DB
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 :
7cebed70 709 : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
16e0bf73
DB
710 : "%"REG_a, "%"REG_c
711 );
7f16f6e6
MN
712
713/*
16e0bf73
DB
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;
bb270c08 740 }
16e0bf73
DB
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 }
bb270c08 764 }
16e0bf73 765 }
7f16f6e6
MN
766}
767*/
b250f9c6 768#elif HAVE_MMX
16e0bf73 769 src+= stride*4;
be449fca 770 __asm__ volatile(
16e0bf73
DB
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
bb270c08
DB
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
16e0bf73
DB
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
3057fa66 837//50 opcodes so far
16e0bf73
DB
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
8405b3fd 876
b250f9c6 877#if HAVE_MMX2
16e0bf73
DB
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|
8405b3fd 890#else
16e0bf73
DB
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|
8405b3fd 907#endif
3057fa66 908
b250f9c6 909#if HAVE_MMX2
16e0bf73
DB
910 "pminsw %%mm2, %%mm0 \n\t"
911 "pminsw %%mm3, %%mm1 \n\t"
3057fa66 912#else
16e0bf73
DB
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"
3057fa66
A
919#endif
920
16e0bf73
DB
921 "movd %2, %%mm2 \n\t" // QP
922 "punpcklbw %%mm7, %%mm2 \n\t"
792a5a7c 923
16e0bf73
DB
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|
3057fa66 931// 100 opcodes
16e0bf73
DB
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"
3057fa66 972
b250f9c6 973#if HAVE_MMX2
16e0bf73
DB
974 "pminsw %%mm0, %%mm4 \n\t"
975 "pminsw %%mm1, %%mm5 \n\t"
3057fa66 976#else
16e0bf73
DB
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"
3057fa66 983#endif
16e0bf73
DB
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)
7cebed70 997 : "r" ((x86_reg)stride), "m" (c->pQPb)
16e0bf73
DB
998 : "%"REG_a, "%"REG_c
999 );
ebc3209a 1000#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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;
bb270c08 1035 }
16e0bf73
DB
1036 src++;
1037 }
ebc3209a 1038#endif //HAVE_MMX2 || HAVE_AMD3DNOW
3057fa66 1039}
b0ac780a 1040#endif //HAVE_ALTIVEC
3057fa66 1041
b250f9c6 1042#if !HAVE_ALTIVEC
9c9e467d 1043static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
3057fa66 1044{
ebc3209a 1045#if HAVE_MMX2 || HAVE_AMD3DNOW
be449fca 1046 __asm__ volatile(
16e0bf73
DB
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"
bb270c08
DB
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
3057fa66 1061
cc9b0679 1062#undef FIND_MIN_MAX
b250f9c6 1063#if HAVE_MMX2
053dea12 1064#define REAL_FIND_MIN_MAX(addr)\
16e0bf73
DB
1065 "movq " #addr ", %%mm0 \n\t"\
1066 "pminub %%mm0, %%mm7 \n\t"\
1067 "pmaxub %%mm0, %%mm6 \n\t"
e0f8ffae 1068#else
053dea12 1069#define REAL_FIND_MIN_MAX(addr)\
16e0bf73
DB
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"
e0f8ffae 1076#endif
053dea12 1077#define FIND_MIN_MAX(addr) REAL_FIND_MIN_MAX(addr)
3057fa66 1078
053dea12
AJ
1079FIND_MIN_MAX((%%REGa))
1080FIND_MIN_MAX((%%REGa, %1))
1081FIND_MIN_MAX((%%REGa, %1, 2))
70c5ae87 1082FIND_MIN_MAX((%0, %1, 4))
053dea12
AJ
1083FIND_MIN_MAX((%%REGd))
1084FIND_MIN_MAX((%%REGd, %1))
1085FIND_MIN_MAX((%%REGd, %1, 2))
70c5ae87 1086FIND_MIN_MAX((%0, %1, 8))
3057fa66 1087
16e0bf73
DB
1088 "movq %%mm7, %%mm4 \n\t"
1089 "psrlq $8, %%mm7 \n\t"
b250f9c6 1090#if HAVE_MMX2
16e0bf73
DB
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"
e5c30e06 1096#else
16e0bf73
DB
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"
e5c30e06 1110#endif
cd38e322
MN
1111
1112
16e0bf73
DB
1113 "movq %%mm6, %%mm4 \n\t"
1114 "psrlq $8, %%mm6 \n\t"
b250f9c6 1115#if HAVE_MMX2
16e0bf73
DB
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"
cd38e322 1121#else
16e0bf73
DB
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"
cd38e322 1132#endif
16e0bf73
DB
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"
70c5ae87 1191// 0, 2, 3, 1
053dea12 1192#define REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
16e0bf73
DB
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"\
70c5ae87 1219\
16e0bf73
DB
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"
2e212618 1237
053dea12
AJ
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)
70c5ae87
MN
1240/*
12410000000
12421111111
e5c30e06 1243
70c5ae87
MN
12441111110
12451111101
12461111100
12471111011
12481111010
12491111001
e5c30e06 1250
70c5ae87
MN
12511111000
12521110111
e5c30e06 1253
70c5ae87 1254*/
bb270c08
DB
1255//DERING_CORE(dst ,src ,ppsx ,psx ,sx ,pplx ,plx ,lx ,t0 ,t1)
1256DERING_CORE((%%REGa) ,(%%REGa, %1) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1257DERING_CORE((%%REGa, %1) ,(%%REGa, %1, 2),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1258DERING_CORE((%%REGa, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1259DERING_CORE((%0, %1, 4) ,(%%REGd) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1260DERING_CORE((%%REGd) ,(%%REGd, %1) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1261DERING_CORE((%%REGd, %1) ,(%%REGd, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1262DERING_CORE((%%REGd, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1263DERING_CORE((%0, %1, 8) ,(%%REGd, %1, 4),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1264
16e0bf73 1265 "1: \n\t"
7cebed70 1266 : : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb), "m"(c->pQPb2)
16e0bf73
DB
1267 : "%"REG_a, "%"REG_d, "%"REG_c
1268 );
ebc3209a 1269#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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;
bb270c08 1285 }
16e0bf73
DB
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];
bb270c08 1319
16e0bf73
DB
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;
2e212618 1328
cd38e322 1329#ifdef DEBUG_DERING_THRESHOLD
be449fca 1330 __asm__ volatile("emms\n\t":);
16e0bf73
DB
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){
a94948d3 1338 static int numSkipped=0;
16e0bf73
DB
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
a94948d3 1349 numSkipped++;
16e0bf73
DB
1350 if(absDiff > worstDiff){
1351 worstDiff= absDiff;
1352 worstQP= QP;
1353 worstRange= max-min;
bb270c08 1354 }
16e0bf73
DB
1355 errorSum+= error;
1356
a94948d3 1357 if(1024LL*1024LL*1024LL % numSkipped == 0){
16e0bf73
DB
1358 av_log(c, AV_LOG_INFO, "sum:%1.3f, skip:%d, wQP:%d, "
1359 "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
a94948d3
DB
1360 (float)errorSum/numSkipped, numSkipped, worstQP, worstRange,
1361 worstDiff, (float)numSkipped/numPixels);
16e0bf73
DB
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 }
bb270c08 1370 }
16e0bf73 1371 }
cd38e322 1372#ifdef DEBUG_DERING_THRESHOLD
16e0bf73
DB
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 }
bb270c08 1382 }
16e0bf73
DB
1383// src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
1384 }
cd38e322 1385#endif
ebc3209a 1386#endif //HAVE_MMX2 || HAVE_AMD3DNOW
3057fa66 1387}
b0ac780a 1388#endif //HAVE_ALTIVEC
3057fa66 1389
3b58b885 1390/**
b304569a 1391 * Deinterlaces the given block by linearly interpolating every second line.
7fb36f6c 1392 * will be called for every 8x8 block and can read & write from line 4-15
bd107136 1393 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
7fb36f6c 1394 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
3b58b885 1395 */
cc9b0679 1396static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
3b58b885 1397{
ebc3209a 1398#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 1399 src+= 4*stride;
be449fca 1400 __asm__ volatile(
16e0bf73
DB
1401 "lea (%0, %1), %%"REG_a" \n\t"
1402 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
bb270c08
DB
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
16e0bf73
DB
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
7cebed70 1420 : : "r" (src), "r" ((x86_reg)stride)
16e0bf73
DB
1421 : "%"REG_a, "%"REG_c
1422 );
3b58b885 1423#else
16e0bf73
DB
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 }
3b58b885
MN
1439#endif
1440}
1441
1442/**
b304569a 1443 * Deinterlaces the given block by cubic interpolating every second line.
7fb36f6c 1444 * will be called for every 8x8 block and can read & write from line 4-15
bd107136 1445 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
7fb36f6c
MN
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
3b58b885 1448 */
cc9b0679 1449static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
3b58b885 1450{
ebc3209a 1451#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 1452 src+= stride*3;
be449fca 1453 __asm__ volatile(
16e0bf73
DB
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"
bb270c08
DB
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
3b58b885 1461
053dea12 1462#define REAL_DEINT_CUBIC(a,b,c,d,e)\
16e0bf73
DB
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"
053dea12 1483#define DEINT_CUBIC(a,b,c,d,e) REAL_DEINT_CUBIC(a,b,c,d,e)
acced553 1484
bb270c08
DB
1485DEINT_CUBIC((%0) , (%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd, %1))
1486DEINT_CUBIC((%%REGa, %1), (%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%0, %1, 8))
1487DEINT_CUBIC((%0, %1, 4) , (%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGc))
1488DEINT_CUBIC((%%REGd, %1), (%0, %1, 8) , (%%REGd, %1, 4), (%%REGc) , (%%REGc, %1, 2))
3b58b885 1489
7cebed70 1490 : : "r" (src), "r" ((x86_reg)stride)
16e0bf73
DB
1491 : "%"REG_a, "%"REG_d, "%"REG_c
1492 );
ebc3209a 1493#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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 }
ebc3209a 1503#endif //HAVE_MMX2 || HAVE_AMD3DNOW
3b58b885
MN
1504}
1505
1506/**
b304569a 1507 * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
7fb36f6c 1508 * will be called for every 8x8 block and can read & write from line 4-15
bd107136 1509 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
7fb36f6c 1510 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
9c9e467d 1511 * this filter will read lines 4-13 and write 5-11
9c9e467d
MN
1512 */
1513static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
1514{
ebc3209a 1515#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 1516 src+= stride*4;
be449fca 1517 __asm__ volatile(
16e0bf73
DB
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"
bb270c08
DB
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
9c9e467d 1524
053dea12 1525#define REAL_DEINT_FF(a,b,c,d)\
16e0bf73
DB
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"\
9c9e467d 1552
053dea12
AJ
1553#define DEINT_FF(a,b,c,d) REAL_DEINT_FF(a,b,c,d)
1554
bb270c08
DB
1555DEINT_FF((%0) , (%%REGa) , (%%REGa, %1), (%%REGa, %1, 2))
1556DEINT_FF((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
1557DEINT_FF((%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%%REGd, %1, 2))
1558DEINT_FF((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
9c9e467d 1559
16e0bf73 1560 "movq %%mm0, (%2) \n\t"
7cebed70 1561 : : "r" (src), "r" ((x86_reg)stride), "r"(tmp)
16e0bf73
DB
1562 : "%"REG_a, "%"REG_d
1563 );
ebc3209a 1564#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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 }
ebc3209a 1582#endif //HAVE_MMX2 || HAVE_AMD3DNOW
9c9e467d
MN
1583}
1584
1585/**
134eb1e5
MN
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
bd107136 1588 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
134eb1e5
MN
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 */
1592static inline void RENAME(deInterlaceL5)(uint8_t src[], int stride, uint8_t *tmp, uint8_t *tmp2)
1593{
ebc3209a 1594#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 1595 src+= stride*4;
be449fca 1596 __asm__ volatile(
16e0bf73
DB
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"
bb270c08
DB
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
134eb1e5 1604
053dea12 1605#define REAL_DEINT_L5(t1,t2,a,b,c)\
16e0bf73
DB
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"\
134eb1e5 1637
053dea12
AJ
1638#define DEINT_L5(t1,t2,a,b,c) REAL_DEINT_L5(t1,t2,a,b,c)
1639
1640DEINT_L5(%%mm0, %%mm1, (%0) , (%%REGa) , (%%REGa, %1) )
1641DEINT_L5(%%mm1, %%mm0, (%%REGa) , (%%REGa, %1) , (%%REGa, %1, 2))
1642DEINT_L5(%%mm0, %%mm1, (%%REGa, %1) , (%%REGa, %1, 2), (%0, %1, 4) )
1643DEINT_L5(%%mm1, %%mm0, (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
115329f1 1644DEINT_L5(%%mm0, %%mm1, (%0, %1, 4) , (%%REGd) , (%%REGd, %1) )
053dea12
AJ
1645DEINT_L5(%%mm1, %%mm0, (%%REGd) , (%%REGd, %1) , (%%REGd, %1, 2))
1646DEINT_L5(%%mm0, %%mm1, (%%REGd, %1) , (%%REGd, %1, 2), (%0, %1, 8) )
1647DEINT_L5(%%mm1, %%mm0, (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
134eb1e5 1648
16e0bf73
DB
1649 "movq %%mm0, (%2) \n\t"
1650 "movq %%mm1, (%3) \n\t"
7cebed70 1651 : : "r" (src), "r" ((x86_reg)stride), "r"(tmp), "r"(tmp2)
16e0bf73
DB
1652 : "%"REG_a, "%"REG_d
1653 );
ebc3209a 1654#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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 }
ebc3209a 1683#endif //HAVE_MMX2 || HAVE_AMD3DNOW
134eb1e5
MN
1684}
1685
1686/**
b304569a 1687 * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
9c9e467d 1688 * will be called for every 8x8 block and can read & write from line 4-15
bd107136 1689 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
9c9e467d 1690 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
7fb36f6c 1691 * this filter will read lines 4-13 and write 4-11
3b58b885 1692 */
13ba9ae4 1693static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride, uint8_t *tmp)
3b58b885 1694{
ebc3209a 1695#if HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73 1696 src+= 4*stride;
be449fca 1697 __asm__ volatile(
16e0bf73
DB
1698 "lea (%0, %1), %%"REG_a" \n\t"
1699 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
bb270c08
DB
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
16e0bf73
DB
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
7cebed70 1739 : : "r" (src), "r" ((x86_reg)stride), "r" (tmp)
16e0bf73
DB
1740 : "%"REG_a, "%"REG_d
1741 );
ebc3209a 1742#else //HAVE_MMX2 || HAVE_AMD3DNOW
16e0bf73
DB
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 }
ebc3209a 1785#endif //HAVE_MMX2 || HAVE_AMD3DNOW
3b58b885
MN
1786}
1787
1788/**
b304569a 1789 * Deinterlaces the given block by applying a median filter to every second line.
7fb36f6c 1790 * will be called for every 8x8 block and can read & write from line 4-15,
bd107136 1791 * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
7fb36f6c 1792 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
3b58b885 1793 */
cc9b0679 1794static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
3b58b885 1795{
ff5d91d9 1796#if HAVE_MMX
16e0bf73 1797 src+= 4*stride;
b250f9c6 1798#if HAVE_MMX2
be449fca 1799 __asm__ volatile(
16e0bf73
DB
1800 "lea (%0, %1), %%"REG_a" \n\t"
1801 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
bb270c08
DB
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
16e0bf73
DB
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
7cebed70 1843 : : "r" (src), "r" ((x86_reg)stride)
16e0bf73
DB
1844 : "%"REG_a, "%"REG_d
1845 );
a6be8111
MN
1846
1847#else // MMX without MMX2
be449fca 1848 __asm__ volatile(
16e0bf73
DB
1849 "lea (%0, %1), %%"REG_a" \n\t"
1850 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
bb270c08
DB
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
16e0bf73 1853 "pxor %%mm7, %%mm7 \n\t"
a6be8111 1854
053dea12 1855#define REAL_MEDIAN(a,b,c)\
16e0bf73
DB
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"
053dea12 1878#define MEDIAN(a,b,c) REAL_MEDIAN(a,b,c)
a6be8111 1879
bb270c08 1880MEDIAN((%0) , (%%REGa) , (%%REGa, %1))
053dea12 1881MEDIAN((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4))
bb270c08 1882MEDIAN((%0, %1, 4) , (%%REGd) , (%%REGd, %1))
053dea12 1883MEDIAN((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8))
a6be8111 1884
7cebed70 1885 : : "r" (src), "r" ((x86_reg)stride)
16e0bf73
DB
1886 : "%"REG_a, "%"REG_d
1887 );
64c968da
DB
1888#endif //HAVE_MMX2
1889#else //HAVE_MMX
16e0bf73
DB
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;
bb270c08 1905 }
16e0bf73
DB
1906 src++;
1907 }
64c968da 1908#endif //HAVE_MMX
3b58b885
MN
1909}
1910
b250f9c6 1911#if HAVE_MMX
4e4dcbc5
MN
1912/**
1913 * transposes and shift the given 8x8 Block into dst1 and dst2
1914 */
cc9b0679 1915static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
4e4dcbc5 1916{
be449fca 1917 __asm__(
16e0bf73 1918 "lea (%0, %1), %%"REG_a" \n\t"
bb270c08
DB
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
16e0bf73
DB
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"
bb270c08
DB
1990
1991
7cebed70 1992 :: "r" (src), "r" ((x86_reg)srcStride), "r" (dst1), "r" (dst2)
bb270c08 1993 : "%"REG_a
16e0bf73 1994 );
4e4dcbc5
MN
1995}
1996
1997/**
1998 * transposes the given 8x8 block
1999 */
cc9b0679 2000static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
4e4dcbc5 2001{
be449fca 2002 __asm__(
16e0bf73
DB
2003 "lea (%0, %1), %%"REG_a" \n\t"
2004 "lea (%%"REG_a",%1,4), %%"REG_d" \n\t"
bb270c08
DB
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
16e0bf73
DB
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"
bb270c08 2071
7cebed70 2072 :: "r" (dst), "r" ((x86_reg)dstStride), "r" (src)
bb270c08 2073 : "%"REG_a, "%"REG_d
16e0bf73 2074 );
4e4dcbc5 2075}
64c968da 2076#endif //HAVE_MMX
053dea12 2077//static long test=0;
4e4dcbc5 2078
b250f9c6 2079#if !HAVE_ALTIVEC
a2596758 2080static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
aa089f6c 2081 uint8_t *tempBlurred, uint32_t *tempBlurredPast, int *maxNoise)
117e45b0 2082{
16e0bf73 2083 // to save a register (FIXME do this outside of the loops)
aa089f6c
DB
2084 tempBlurredPast[127]= maxNoise[0];
2085 tempBlurredPast[128]= maxNoise[1];
2086 tempBlurredPast[129]= maxNoise[2];
115329f1 2087
be44a4d7
MN
2088#define FAST_L2_DIFF
2089//#define L1_DIFF //u should change the thresholds too if u try that one
ebc3209a 2090#if HAVE_MMX2 || HAVE_AMD3DNOW
be449fca 2091 __asm__ volatile(
16e0bf73
DB
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
bb270c08
DB
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
be44a4d7
MN
2097//FIXME reorder?
2098#ifdef L1_DIFF //needs mmx2
16e0bf73
DB
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"
64c968da 2123#else //L1_DIFF
053dea12 2124#if defined (FAST_L2_DIFF)
16e0bf73
DB
2125 "pcmpeqb %%mm7, %%mm7 \n\t"
2126 "movq "MANGLE(b80)", %%mm6 \n\t"
2127 "pxor %%mm0, %%mm0 \n\t"
053dea12 2128#define REAL_L2_DIFF_CORE(a, b)\
16e0bf73
DB
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"
be44a4d7 2141
64c968da 2142#else //defined (FAST_L2_DIFF)
16e0bf73
DB
2143 "pxor %%mm7, %%mm7 \n\t"
2144 "pxor %%mm0, %%mm0 \n\t"
053dea12 2145#define REAL_L2_DIFF_CORE(a, b)\
16e0bf73
DB
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"
be44a4d7 2160
64c968da 2161#endif //defined (FAST_L2_DIFF)
053dea12
AJ
2162
2163#define L2_DIFF_CORE(a, b) REAL_L2_DIFF_CORE(a, b)
2164
bb270c08
DB
2165L2_DIFF_CORE((%0) , (%1))
2166L2_DIFF_CORE((%0, %2) , (%1, %2))
2167L2_DIFF_CORE((%0, %2, 2) , (%1, %2, 2))
2168L2_DIFF_CORE((%0, %%REGa) , (%1, %%REGa))
2169L2_DIFF_CORE((%0, %2, 4) , (%1, %2, 4))
2170L2_DIFF_CORE((%0, %%REGd) , (%1, %%REGd))
053dea12 2171L2_DIFF_CORE((%0, %%REGa,2), (%1, %%REGa,2))
bb270c08 2172L2_DIFF_CORE((%0, %%REGc) , (%1, %%REGc))
be44a4d7 2173
64c968da 2174#endif //L1_DIFF
be44a4d7 2175
16e0bf73
DB
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
7cebed70 2375 :: "r" (src), "r" (tempBlurred), "r"((x86_reg)stride), "m" (tempBlurredPast)
16e0bf73
DB
2376 : "%"REG_a, "%"REG_d, "%"REG_c, "memory"
2377 );
ebc3209a 2378#else //HAVE_MMX2 || HAVE_AMD3DNOW
5936be4c 2379{
16e0bf73
DB
2380 int y;
2381 int d=0;
2382// int sysd=0;
2383 int i;
bb270c08 2384
16e0bf73
DB
2385 for(y=0; y<8; y++){
2386 int x;
2387 for(x=0; x<8; x++){
aa089f6c 2388 int ref= tempBlurred[ x + y*stride ];
16e0bf73
DB
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;
bb270c08 2396 }
16e0bf73
DB
2397 }
2398 i=d;
2399 d= (
2400 4*d
aa089f6c
DB
2401 +(*(tempBlurredPast-256))
2402 +(*(tempBlurredPast-1))+ (*(tempBlurredPast+1))
2403 +(*(tempBlurredPast+256))
16e0bf73 2404 +4)>>3;
aa089f6c
DB
2405 *tempBlurredPast=i;
2406// ((*tempBlurredPast)*3 + d + 2)>>2;
a9c77978 2407
117e45b0
MN
2408/*
2409Switch between
2410 1 0 0 0 0 0 0 (0)
241164 32 16 8 4 2 1 (1)
241264 48 36 27 20 15 11 (33) (approx)
241364 56 49 43 37 33 29 (200) (approx)
2414*/
16e0bf73
DB
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++){
aa089f6c 2420 int ref= tempBlurred[ x + y*stride ];
16e0bf73 2421 int cur= src[ x + y*stride ];
aa089f6c 2422 tempBlurred[ x + y*stride ]=
16e0bf73
DB
2423 src[ x + y*stride ]=
2424 (ref + cur + 1)>>1;
bb270c08 2425 }
16e0bf73
DB
2426 }
2427 }else{
2428 for(y=0; y<8; y++){
2429 int x;
2430 for(x=0; x<8; x++){
aa089f6c 2431 tempBlurred[ x + y*stride ]= src[ x + y*stride ];
bb270c08 2432 }
16e0bf73 2433 }
bb270c08 2434 }
16e0bf73
DB
2435 }else{
2436 if(d < maxNoise[0]){
2437 for(y=0; y<8; y++){
2438 int x;
2439 for(x=0; x<8; x++){
aa089f6c 2440 int ref= tempBlurred[ x + y*stride ];
16e0bf73 2441 int cur= src[ x + y*stride ];
aa089f6c 2442 tempBlurred[ x + y*stride ]=
16e0bf73
DB
2443 src[ x + y*stride ]=
2444 (ref*7 + cur + 4)>>3;
bb270c08 2445 }
16e0bf73
DB
2446 }
2447 }else{
2448 for(y=0; y<8; y++){
2449 int x;
2450 for(x=0; x<8; x++){
aa089f6c 2451 int ref= tempBlurred[ x + y*stride ];
16e0bf73 2452 int cur= src[ x + y*stride ];
aa089f6c 2453 tempBlurred[ x + y*stride ]=
16e0bf73
DB
2454 src[ x + y*stride ]=
2455 (ref*3 + cur + 2)>>2;
bb270c08 2456 }
16e0bf73 2457 }
bb270c08 2458 }
16e0bf73 2459 }
5936be4c 2460}
ebc3209a 2461#endif //HAVE_MMX2 || HAVE_AMD3DNOW
117e45b0 2462}
a7b2871c 2463#endif //HAVE_ALTIVEC
117e45b0 2464
b250f9c6 2465#if HAVE_MMX
792a5a7c
MN
2466/**
2467 * accurate deblock filter
2468 */
849f1035 2469static av_always_inline void RENAME(do_a_deblock)(uint8_t *src, int step, int stride, PPContext *c){
16e0bf73
DB
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
792a5a7c 2473//START_TIMER
be449fca 2474 __asm__ volatile(
16e0bf73
DB
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
be449fca 2480 __asm__ volatile(
16e0bf73 2481 "lea (%2, %3), %%"REG_a" \n\t"
bb270c08
DB
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
16e0bf73
DB
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)
7cebed70 2575 : "r" (src), "r" ((x86_reg)step), "m" (c->pQPb), "m"(c->ppMode.flatnessThreshold)
16e0bf73
DB
2576 : "%"REG_a
2577 );
2578
2579 both_masks = dc_mask & eq_mask;
2580
2581 if(both_masks){
7cebed70 2582 x86_reg offset= -8*step;
16e0bf73
DB
2583 int64_t *temp_sums= sums;
2584
be449fca 2585 __asm__ volatile(
16e0bf73
DB
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"
39d89b69
MN
2631
2632#define NEXT\
16e0bf73
DB
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"
39d89b69
MN
2640
2641#define PREV\
16e0bf73
DB
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)
7cebed70 2719 : "r" ((x86_reg)step), "m" (c->pQPb), "r"(sums), "g"(src)
16e0bf73
DB
2720 );
2721
2722 src+= step; // src points to begin of the 8x8 Block
2723
be449fca 2724 __asm__ volatile(
16e0bf73
DB
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)
7cebed70 2756 : "r" ((x86_reg)step), "r"(src - offset), "m"(both_masks)
16e0bf73
DB
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;
be449fca 2763 __asm__ volatile(
16e0bf73
DB
2764 "pxor %%mm7, %%mm7 \n\t"
2765 "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
2766 "and "ALIGN_MASK", %%"REG_c" \n\t" // align
bb270c08
DB
2767// 0 1 2 3 4 5 6 7 8 9
2768// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %1+8%1 ecx+4%1
2769
16e0bf73
DB
2770 "movq (%0), %%mm0 \n\t"
2771 "movq %%mm0, %%mm1 \n\t"
2772 "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
2773 "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
2774
2775 "movq (%0, %1), %%mm2 \n\t"
2776 "lea (%0, %1, 2), %%"REG_a" \n\t"
2777 "movq %%mm2, %%mm3 \n\t"
2778 "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
2779 "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
2780
2781 "movq (%%"REG_a"), %%mm4 \n\t"
2782 "movq %%mm4, %%mm5 \n\t"
2783 "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
2784 "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
2785
2786 "paddw %%mm0, %%mm0 \n\t" // 2L0
2787 "paddw %%mm1, %%mm1 \n\t" // 2H0
2788 "psubw %%mm4, %%mm2 \n\t" // L1 - L2
2789 "psubw %%mm5, %%mm3 \n\t" // H1 - H2
2790 "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
2791 "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
2792
2793 "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
2794 "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
2795 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
2796 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
2797
2798 "movq (%%"REG_a", %1), %%mm2 \n\t"
2799 "movq %%mm2, %%mm3 \n\t"
2800 "punpcklbw %%mm7, %%mm2 \n\t" // L3
2801 "punpckhbw %%mm7, %%mm3 \n\t" // H3
2802
2803 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
2804 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
2805 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
2806 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
2807 "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
2808 "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
2809
2810 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
2811 "movq %%mm0, %%mm1 \n\t"
2812 "punpcklbw %%mm7, %%mm0 \n\t" // L4
2813 "punpckhbw %%mm7, %%mm1 \n\t" // H4
2814
2815 "psubw %%mm0, %%mm2 \n\t" // L3 - L4
2816 "psubw %%mm1, %%mm3 \n\t" // H3 - H4
2817 "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
2818 "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
2819 "paddw %%mm4, %%mm4 \n\t" // 2L2
2820 "paddw %%mm5, %%mm5 \n\t" // 2H2
2821 "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
2822 "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
2823
2824 "lea (%%"REG_a", %1), %0 \n\t"
2825 "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
2826 "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
2827 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
2828 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
792a5a7c 2829//50 opcodes so far
16e0bf73
DB
2830 "movq (%0, %1, 2), %%mm2 \n\t"
2831 "movq %%mm2, %%mm3 \n\t"
2832 "punpcklbw %%mm7, %%mm2 \n\t" // L5
2833 "punpckhbw %%mm7, %%mm3 \n\t" // H5
2834 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
2835 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
2836 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
2837 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
2838
2839 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
2840 "punpcklbw %%mm7, %%mm6 \n\t" // L6
2841 "psubw %%mm6, %%mm2 \n\t" // L5 - L6
2842 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
2843 "punpckhbw %%mm7, %%mm6 \n\t" // H6
2844 "psubw %%mm6, %%mm3 \n\t" // H5 - H6
2845
2846 "paddw %%mm0, %%mm0 \n\t" // 2L4
2847 "paddw %%mm1, %%mm1 \n\t" // 2H4
2848 "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
2849 "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
2850
2851 "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
2852 "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
2853 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
2854 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
2855
2856 "movq (%0, %1, 4), %%mm2 \n\t"
2857 "movq %%mm2, %%mm3 \n\t"
2858 "punpcklbw %%mm7, %%mm2 \n\t" // L7
2859 "punpckhbw %%mm7, %%mm3 \n\t" // H7
2860
2861 "paddw %%mm2, %%mm2 \n\t" // 2L7
2862 "paddw %%mm3, %%mm3 \n\t" // 2H7
2863 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
2864 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
2865
2866 "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
2867 "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
792a5a7c 2868
b250f9c6 2869#if HAVE_MMX2
16e0bf73
DB
2870 "movq %%mm7, %%mm6 \n\t" // 0
2871 "psubw %%mm0, %%mm6 \n\t"
2872 "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
2873 "movq %%mm7, %%mm6 \n\t" // 0
2874 "psubw %%mm1, %%mm6 \n\t"
2875 "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
2876 "movq %%mm7, %%mm6 \n\t" // 0
2877 "psubw %%mm2, %%mm6 \n\t"
2878 "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
2879 "movq %%mm7, %%mm6 \n\t" // 0
2880 "psubw %%mm3, %%mm6 \n\t"
2881 "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
792a5a7c 2882#else
16e0bf73
DB
2883 "movq %%mm7, %%mm6 \n\t" // 0
2884 "pcmpgtw %%mm0, %%mm6 \n\t"
2885 "pxor %%mm6, %%mm0 \n\t"
2886 "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
2887 "movq %%mm7, %%mm6 \n\t" // 0
2888 "pcmpgtw %%mm1, %%mm6 \n\t"
2889 "pxor %%mm6, %%mm1 \n\t"
2890 "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
2891 "movq %%mm7, %%mm6 \n\t" // 0
2892 "pcmpgtw %%mm2, %%mm6 \n\t"
2893 "pxor %%mm6, %%mm2 \n\t"
2894 "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
2895 "movq %%mm7, %%mm6 \n\t" // 0
2896 "pcmpgtw %%mm3, %%mm6 \n\t"
2897 "pxor %%mm6, %%mm3 \n\t"
2898 "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
792a5a7c
MN
2899#endif
2900
b250f9c6 2901#if HAVE_MMX2
16e0bf73
DB
2902 "pminsw %%mm2, %%mm0 \n\t"
2903 "pminsw %%mm3, %%mm1 \n\t"
792a5a7c 2904#else
16e0bf73
DB
2905 "movq %%mm0, %%mm6 \n\t"
2906 "psubusw %%mm2, %%mm6 \n\t"
2907 "psubw %%mm6, %%mm0 \n\t"
2908 "movq %%mm1, %%mm6 \n\t"
2909 "psubusw %%mm3, %%mm6 \n\t"
2910 "psubw %%mm6, %%mm1 \n\t"
792a5a7c
MN
2911#endif
2912
16e0bf73
DB
2913 "movd %2, %%mm2 \n\t" // QP
2914 "punpcklbw %%mm7, %%mm2 \n\t"
792a5a7c 2915
16e0bf73
DB
2916 "movq %%mm7, %%mm6 \n\t" // 0
2917 "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
2918 "pxor %%mm6, %%mm4 \n\t"
2919 "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
2920 "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
2921 "pxor %%mm7, %%mm5 \n\t"
2922 "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
792a5a7c 2923// 100 opcodes
16e0bf73
DB
2924 "psllw $3, %%mm2 \n\t" // 8QP
2925 "movq %%mm2, %%mm3 \n\t" // 8QP
2926 "pcmpgtw %%mm4, %%mm2 \n\t"
2927 "pcmpgtw %%mm5, %%mm3 \n\t"
2928 "pand %%mm2, %%mm4 \n\t"
2929 "pand %%mm3, %%mm5 \n\t"
2930
2931
2932 "psubusw %%mm0, %%mm4 \n\t" // hd
2933 "psubusw %%mm1, %%mm5 \n\t" // ld
2934
2935
2936 "movq "MANGLE(w05)", %%mm2 \n\t" // 5
2937 "pmullw %%mm2, %%mm4 \n\t"
2938 "pmullw %%mm2, %%mm5 \n\t"
2939 "movq "MANGLE(w20)", %%mm2 \n\t" // 32
2940 "paddw %%mm2, %%mm4 \n\t"
2941 "paddw %%mm2, %%mm5 \n\t"
2942 "psrlw $6, %%mm4 \n\t"
2943 "psrlw $6, %%mm5 \n\t"
2944
2945 "movq 16(%%"REG_c"), %%mm0 \n\t" // L3 - L4
2946 "movq 24(%%"REG_c"), %%mm1 \n\t" // H3 - H4
2947
2948 "pxor %%mm2, %%mm2 \n\t"
2949 "pxor %%mm3, %%mm3 \n\t"
2950
2951 "pcmpgtw %%mm0, %%mm2 \n\t" // sign (L3-L4)
2952 "pcmpgtw %%mm1, %%mm3 \n\t" // sign (H3-H4)
2953 "pxor %%mm2, %%mm0 \n\t"
2954 "pxor %%mm3, %%mm1 \n\t"
2955 "psubw %%mm2, %%mm0 \n\t" // |L3-L4|
2956 "psubw %%mm3, %%mm1 \n\t" // |H3-H4|
2957 "psrlw $1, %%mm0 \n\t" // |L3 - L4|/2
2958 "psrlw $1, %%mm1 \n\t" // |H3 - H4|/2
2959
2960 "pxor %%mm6, %%mm2 \n\t"
2961 "pxor %%mm7, %%mm3 \n\t"
2962 "pand %%mm2, %%mm4 \n\t"
2963 "pand %%mm3, %%mm5 \n\t"
792a5a7c 2964
b250f9c6 2965#if HAVE_MMX2
16e0bf73
DB
2966 "pminsw %%mm0, %%mm4 \n\t"
2967 "pminsw %%mm1, %%mm5 \n\t"
792a5a7c 2968#else
16e0bf73
DB
2969 "movq %%mm4, %%mm2 \n\t"
2970 "psubusw %%mm0, %%mm2 \n\t"
2971 "psubw %%mm2, %%mm4 \n\t"
2972 "movq %%mm5, %%mm2 \n\t"
2973 "psubusw %%mm1, %%mm2 \n\t"
2974 "psubw %%mm2, %%mm5 \n\t"
792a5a7c 2975#endif
16e0bf73
DB
2976 "pxor %%mm6, %%mm4 \n\t"
2977 "pxor %%mm7, %%mm5 \n\t"
2978 "psubw %%mm6, %%mm4 \n\t"
2979 "psubw %%mm7, %%mm5 \n\t"
2980 "packsswb %%mm5, %%mm4 \n\t"
2981 "movq %3, %%mm1 \n\t"
2982 "pandn %%mm4, %%mm1 \n\t"
2983 "movq (%0), %%mm0 \n\t"
2984 "paddb %%mm1, %%mm0 \n\t"
2985 "movq %%mm0, (%0) \n\t"
2986 "movq (%0, %1), %%mm0 \n\t"
2987 "psubb %%mm1, %%mm0 \n\t"
2988 "movq %%mm0, (%0, %1) \n\t"
2989
2990 : "+r" (temp_src)
7cebed70 2991 : "r" ((x86_reg)step), "m" (c->pQPb), "m"(eq_mask)
16e0bf73
DB
2992 : "%"REG_a, "%"REG_c
2993 );
2994 }
792a5a7c
MN
2995/*if(step==16){
2996 STOP_TIMER("step16")
2997}else{
2998 STOP_TIMER("stepX")
2999}*/
3000}
3001#endif //HAVE_MMX
3002
6c51fd3f 3003static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
16e0bf73 3004 const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c);
13e00528 3005
3057fa66 3006/**
755bfeab
DB
3007 * Copies a block from src to dst and fixes the blacklevel.
3008 * levelFix == 0 -> do not touch the brighness & contrast
3057fa66 3009 */
6b791538
A
3010#undef SCALED_CPY
3011
6c51fd3f 3012static inline void RENAME(blockCopy)(uint8_t dst[], int dstStride, const uint8_t src[], int srcStride,
16e0bf73 3013 int levelFix, int64_t *packedOffsetAndScale)
3057fa66 3014{
b250f9c6 3015#if !HAVE_MMX
16e0bf73 3016 int i;
e5c30e06 3017#endif
16e0bf73 3018 if(levelFix){
b250f9c6 3019#if HAVE_MMX
be449fca 3020 __asm__ volatile(
16e0bf73
DB
3021 "movq (%%"REG_a"), %%mm2 \n\t" // packedYOffset
3022 "movq 8(%%"REG_a"), %%mm3 \n\t" // packedYScale
3023 "lea (%2,%4), %%"REG_a" \n\t"
3024 "lea (%3,%5), %%"REG_d" \n\t"
3025 "pxor %%mm4, %%mm4 \n\t"
b250f9c6 3026#if HAVE_MMX2
bb270c08 3027#define REAL_SCALED_CPY(src1, src2, dst1, dst2) \
16e0bf73
DB
3028 "movq " #src1 ", %%mm0 \n\t"\
3029 "movq " #src1 ", %%mm5 \n\t"\
3030 "movq " #src2 ", %%mm1 \n\t"\
3031 "movq " #src2 ", %%mm6 \n\t"\
3032 "punpcklbw %%mm0, %%mm0 \n\t"\
3033 "punpckhbw %%mm5, %%mm5 \n\t"\
3034 "punpcklbw %%mm1, %%mm1 \n\t"\
3035 "punpckhbw %%mm6, %%mm6 \n\t"\
3036 "pmulhuw %%mm3, %%mm0 \n\t"\
3037 "pmulhuw %%mm3, %%mm5 \n\t"\
3038 "pmulhuw %%mm3, %%mm1 \n\t"\
3039 "pmulhuw %%mm3, %%mm6 \n\t"\
3040 "psubw %%mm2, %%mm0 \n\t"\
3041 "psubw %%mm2, %%mm5 \n\t"\
3042 "psubw %%mm2, %%mm1 \n\t"\
3043 "psubw %%mm2, %%mm6 \n\t"\
3044 "packuswb %%mm5, %%mm0 \n\t"\
3045 "packuswb %%mm6, %%mm1 \n\t"\
3046 "movq %%mm0, " #dst1 " \n\t"\
3047 "movq %%mm1, " #dst2 " \n\t"\
3057fa66 3048
6e9b4840 3049#else //HAVE_MMX2
bb270c08 3050#define REAL_SCALED_CPY(src1, src2, dst1, dst2) \
16e0bf73
DB
3051 "movq " #src1 ", %%mm0 \n\t"\
3052 "movq " #src1 ", %%mm5 \n\t"\
3053 "punpcklbw %%mm4, %%mm0 \n\t"\
3054 "punpckhbw %%mm4, %%mm5 \n\t"\
3055 "psubw %%mm2, %%mm0 \n\t"\
3056 "psubw %%mm2, %%mm5 \n\t"\
3057 "movq " #src2 ", %%mm1 \n\t"\
3058 "psllw $6, %%mm0 \n\t"\
3059 "psllw $6, %%mm5 \n\t"\
3060 "pmulhw %%mm3, %%mm0 \n\t"\
3061 "movq " #src2 ", %%mm6 \n\t"\
3062 "pmulhw %%mm3, %%mm5 \n\t"\
3063 "punpcklbw %%mm4, %%mm1 \n\t"\
3064 "punpckhbw %%mm4, %%mm6 \n\t"\
3065 "psubw %%mm2, %%mm1 \n\t"\
3066 "psubw %%mm2, %%mm6 \n\t"\
3067 "psllw $6, %%mm1 \n\t"\
3068 "psllw $6, %%mm6 \n\t"\
3069 "pmulhw %%mm3, %%mm1 \n\t"\
3070 "pmulhw %%mm3, %%mm6 \n\t"\
3071 "packuswb %%mm5, %%mm0 \n\t"\
3072 "packuswb %%mm6, %%mm1 \n\t"\
3073 "movq %%mm0, " #dst1 " \n\t"\
3074 "movq %%mm1, " #dst2 " \n\t"\
043ba56f 3075
64c968da 3076#endif //HAVE_MMX2
053dea12
AJ
3077#define SCALED_CPY(src1, src2, dst1, dst2)\
3078 REAL_SCALED_CPY(src1, src2, dst1, dst2)
6e9b4840 3079
9c9e467d 3080SCALED_CPY((%2) , (%2, %4) , (%3) , (%3, %5))
053dea12
AJ
3081SCALED_CPY((%2, %4, 2), (%%REGa, %4, 2), (%3, %5, 2), (%%REGd, %5, 2))
3082SCALED_CPY((%2, %4, 4), (%%REGa, %4, 4), (%3, %5, 4), (%%REGd, %5, 4))
16e0bf73
DB
3083 "lea (%%"REG_a",%4,4), %%"REG_a" \n\t"
3084 "lea (%%"REG_d",%5,4), %%"REG_d" \n\t"
053dea12 3085SCALED_CPY((%%REGa, %4), (%%REGa, %4, 2), (%%REGd, %5), (%%REGd, %5, 2))
043ba56f
MN
3086
3087
16e0bf73
DB
3088 : "=&a" (packedOffsetAndScale)
3089 : "0" (packedOffsetAndScale),
3090 "r"(src),
3091 "r"(dst),
7cebed70
RD
3092 "r" ((x86_reg)srcStride),
3093 "r" ((x86_reg)dstStride)
16e0bf73
DB
3094 : "%"REG_d
3095 );
64c968da 3096#else //HAVE_MMX
16e0bf73
DB
3097 for(i=0; i<8; i++)
3098 memcpy( &(dst[dstStride*i]),
3099 &(src[srcStride*i]), BLOCK_SIZE);
64c968da 3100#endif //HAVE_MMX
16e0bf73 3101 }else{
b250f9c6 3102#if HAVE_MMX
be449fca 3103 __asm__ volatile(
16e0bf73
DB
3104 "lea (%0,%2), %%"REG_a" \n\t"
3105 "lea (%1,%3), %%"REG_d" \n\t"
043ba56f 3106
bb270c08 3107#define REAL_SIMPLE_CPY(src1, src2, dst1, dst2) \
16e0bf73
DB
3108 "movq " #src1 ", %%mm0 \n\t"\
3109 "movq " #src2 ", %%mm1 \n\t"\
3110 "movq %%mm0, " #dst1 " \n\t"\
3111 "movq %%mm1, " #dst2 " \n\t"\
043ba56f 3112
053dea12
AJ
3113#define SIMPLE_CPY(src1, src2, dst1, dst2)\
3114 REAL_SIMPLE_CPY(src1, src2, dst1, dst2)
3115
bb270c08 3116SIMPLE_CPY((%0) , (%0, %2) , (%1) , (%1, %3))
053dea12
AJ
3117SIMPLE_CPY((%0, %2, 2), (%%REGa, %2, 2), (%1, %3, 2), (%%REGd, %3, 2))
3118SIMPLE_CPY((%0, %2, 4), (%%REGa, %2, 4), (%1, %3, 4), (%%REGd, %3, 4))
16e0bf73
DB
3119 "lea (%%"REG_a",%2,4), %%"REG_a" \n\t"
3120 "lea (%%"REG_d",%3,4), %%"REG_d" \n\t"
053dea12 3121SIMPLE_CPY((%%REGa, %2), (%%REGa, %2, 2), (%%REGd, %3), (%%REGd, %3, 2))
043ba56f 3122
16e0bf73
DB
3123 : : "r" (src),
3124 "r" (dst),
7cebed70
RD
3125 "r" ((x86_reg)srcStride),
3126 "r" ((x86_reg)dstStride)
16e0bf73
DB
3127 : "%"REG_a, "%"REG_d
3128 );
64c968da 3129#else //HAVE_MMX
16e0bf73
DB
3130 for(i=0; i<8; i++)
3131 memcpy( &(dst[dstStride*i]),
3132 &(src[srcStride*i]), BLOCK_SIZE);
64c968da 3133#endif //HAVE_MMX
16e0bf73 3134 }
3057fa66
A
3135}
3136
4b6388d1
MN
3137/**
3138 * Duplicates the given 8 src pixels ? times upward
3139 */
3140static inline void RENAME(duplicate)(uint8_t src[], int stride)
3141{
b250f9c6 3142#if HAVE_MMX
be449fca 3143 __asm__ volatile(
16e0bf73
DB
3144 "movq (%0), %%mm0 \n\t"
3145 "add %1, %0 \n\t"
3146 "movq %%mm0, (%0) \n\t"
3147 "movq %%mm0, (%0, %1) \n\t"
3148 "movq %%mm0, (%0, %1, 2) \n\t"
3149 : "+r" (src)
7cebed70 3150 : "r" ((x86_reg)-stride)
16e0bf73 3151 );
4b6388d1 3152#else
16e0bf73
DB
3153 int i;
3154 uint8_t *p=src;
3155 for(i=0; i<3; i++){
3156 p-= stride;
3157 memcpy(p, src, 8);
3158 }
4b6388d1
MN
3159#endif
3160}
3057fa66
A
3161
3162/**
3163 * Filters array of bytes (Y or U or V values)
3164 */
6c51fd3f 3165static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
16e0bf73 3166 const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c2)
3057fa66 3167{
16e0bf73
DB
3168 DECLARE_ALIGNED(8, PPContext, c)= *c2; //copy to stack for faster access
3169 int x,y;
06c4804a 3170#ifdef COMPILE_TIME_MODE
16e0bf73 3171 const int mode= COMPILE_TIME_MODE;
06c4804a 3172#else
16e0bf73 3173 const int mode= isColor ? c.ppMode.chromMode : c.ppMode.lumMode;
06c4804a 3174#endif
16e0bf73
DB
3175 int black=0, white=255; // blackest black and whitest white in the picture
3176 int QPCorrecture= 256*256;
3057fa66 3177
16e0bf73 3178 int copyAhead;
b250f9c6 3179#if HAVE_MMX
16e0bf73 3180 int i;
c9f99fef 3181#endif
c09dc465 3182
16e0bf73
DB
3183 const int qpHShift= isColor ? 4-c.hChromaSubSample : 4;
3184 const int qpVShift= isColor ? 4-c.vChromaSubSample : 4;
e9effafd 3185
16e0bf73
DB
3186 //FIXME remove
3187 uint64_t * const yHistogram= c.yHistogram;
3188 uint8_t * const tempSrc= srcStride > 0 ? c.tempSrc : c.tempSrc - 23*srcStride;
3189 uint8_t * const tempDst= dstStride > 0 ? c.tempDst : c.tempDst - 23*dstStride;
3190 //const int mbWidth= isColor ? (width+7)>>3 : (width+15)>>4;
07f8991b 3191
b250f9c6 3192#if HAVE_MMX
16e0bf73
DB
3193 for(i=0; i<57; i++){
3194 int offset= ((i*c.ppMode.baseDcDiff)>>8) + 1;
3195 int threshold= offset*2 + 1;
3196 c.mmxDcOffset[i]= 0x7F - offset;
3197 c.mmxDcThreshold[i]= 0x7F - threshold;
3198 c.mmxDcOffset[i]*= 0x0101010101010101LL;
3199 c.mmxDcThreshold[i]*= 0x0101010101010101LL;
3200 }
a9c77978 3201#endif
3057fa66 3202
16e0bf73
DB
3203 if(mode & CUBIC_IPOL_DEINT_FILTER) copyAhead=16;
3204 else if( (mode & LINEAR_BLEND_DEINT_FILTER)
3205 || (mode & FFMPEG_DEINT_FILTER)
3206 || (mode & LOWPASS5_DEINT_FILTER)) copyAhead=14;
3207 else if( (mode & V_DEBLOCK)
3208 || (mode & LINEAR_IPOL_DEINT_FILTER)
3209 || (mode & MEDIAN_DEINT_FILTER)
3210 || (mode & V_A_DEBLOCK)) copyAhead=13;
3211 else if(mode & V_X1_FILTER) copyAhead=11;
3212// else if(mode & V_RK1_FILTER) copyAhead=10;
3213 else if(mode & DERING) copyAhead=9;
3214 else copyAhead=8;
3215
3216 copyAhead-= 8;
3217
3218 if(!isColor){
3219 uint64_t sum= 0;
3220 int i;
3221 uint64_t maxClipped;
3222 uint64_t clipped;
3223 double scale;
bb270c08 3224
16e0bf73
DB
3225 c.frameNum++;
3226 // first frame is fscked so we ignore it
3227 if(c.frameNum == 1) yHistogram[0]= width*height/64*15/256;
bb270c08 3228
16e0bf73
DB
3229 for(i=0; i<256; i++){
3230 sum+= yHistogram[i];
3231 }
bb270c08 3232
16e0bf73
DB
3233 /* We always get a completely black picture first. */
3234 maxClipped= (uint64_t)(sum * c.ppMode.maxClippedThreshold);
bb270c08 3235
16e0bf73
DB
3236 clipped= sum;
3237 for(black=255; black>0; black--){
3238 if(clipped < maxClipped) break;
3239 clipped-= yHistogram[black];
3240 }
bb270c08 3241
16e0bf73
DB
3242 clipped= sum;
3243 for(white=0; white<256; white++){
3244 if(clipped < maxClipped) break;
3245 clipped-= yHistogram[white];
3246 }
bb270c08 3247
16e0bf73 3248 scale= (double)(c.ppMode.maxAllowedY - c.ppMode.minAllowedY) / (double)(white-black);
6e9b4840 3249
b250f9c6 3250#if HAVE_MMX2
16e0bf73
DB
3251 c.packedYScale= (uint16_t)(scale*256.0 + 0.5);
3252 c.packedYOffset= (((black*c.packedYScale)>>8) - c.ppMode.minAllowedY) & 0xFFFF;
6e9b4840 3253#else
16e0bf73
DB
3254 c.packedYScale= (uint16_t)(scale*1024.0 + 0.5);
3255 c.packedYOffset= (black - c.ppMode.minAllowedY) & 0xFFFF;
6e9b4840
MN
3256#endif
3257
16e0bf73
DB
3258 c.packedYOffset|= c.packedYOffset<<32;
3259 c.packedYOffset|= c.packedYOffset<<16;
bb270c08 3260
16e0bf73
DB
3261 c.packedYScale|= c.packedYScale<<32;
3262 c.packedYScale|= c.packedYScale<<16;
bb270c08 3263
16e0bf73
DB
3264 if(mode & LEVEL_FIX) QPCorrecture= (int)(scale*256*256 + 0.5);
3265 else QPCorrecture= 256*256;
3266 }else{
3267 c.packedYScale= 0x0100010001000100LL;
3268 c.packedYOffset= 0;
3269 QPCorrecture= 256*256;
3270 }
bb270c08 3271
16e0bf73
DB
3272 /* copy & deinterlace first row of blocks */
3273 y=-BLOCK_SIZE;
3274 {
3275 const uint8_t *srcBlock= &(src[y*srcStride]);
3276 uint8_t *dstBlock= tempDst + dstStride;
bb270c08 3277
16e0bf73
DB