1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
// VirtualDub - Video processing and capture application
// Copyright (C) 1998-2001 Avery Lee
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
void AvgLines8(BYTE* dst, DWORD h, DWORD pitch)
{
if(h <= 1)
return;
BYTE* s = dst;
BYTE* d = dst + (h-2)*pitch;
for(; s < d; s += pitch*2)
{
BYTE* tmp = s;
#ifndef _WIN64
if((g_cpuid.m_flags & CCpuID::sse2) && !((DWORD)tmp&0xf) && !((DWORD)pitch&0xf))
{
__asm
{
mov esi, tmp
mov ebx, pitch
mov ecx, ebx
shr ecx, 4
AvgLines8_sse2_loop:
movdqa xmm0, [esi]
pavgb xmm0, [esi+ebx*2]
movdqa [esi+ebx], xmm0
add esi, 16
dec ecx
jnz AvgLines8_sse2_loop
mov tmp, esi
}
for(ptrdiff_t i = pitch&7; i--; tmp++)
{
tmp[pitch] = (tmp[0] + tmp[pitch<<1] + 1) >> 1;
}
}
else if(g_cpuid.m_flags & CCpuID::mmx)
{
__asm
{
mov esi, tmp
mov ebx, pitch
mov ecx, ebx
shr ecx, 3
pxor mm7, mm7
AvgLines8_mmx_loop:
movq mm0, [esi]
movq mm1, mm0
punpcklbw mm0, mm7
punpckhbw mm1, mm7
movq mm2, [esi+ebx*2]
movq mm3, mm2
punpcklbw mm2, mm7
punpckhbw mm3, mm7
paddw mm0, mm2
psrlw mm0, 1
paddw mm1, mm3
psrlw mm1, 1
packuswb mm0, mm1
movq [esi+ebx], mm0
lea esi, [esi+8]
dec ecx
jnz AvgLines8_mmx_loop
mov tmp, esi
}
for(ptrdiff_t i = pitch&7; i--; tmp++)
{
tmp[pitch] = (tmp[0] + tmp[pitch<<1] + 1) >> 1;
}
}
else
#endif
{
for(ptrdiff_t i = pitch; i--; tmp++)
{
tmp[pitch] = (tmp[0] + tmp[pitch<<1] + 1) >> 1;
}
}
}
if(!(h&1) && h >= 2)
{
dst += (h-2)*pitch;
memcpy(dst + pitch, dst, pitch);
}
#ifndef _WIN64
__asm emms;
#endif
}
void AvgLines555(BYTE* dst, DWORD h, DWORD pitch)
{
if(h <= 1)
return;
unsigned __int64 __0x03e003e003e003e0 = 0x03e003e003e003e0;
unsigned __int64 __0x001f001f001f001f = 0x001f001f001f001f;
BYTE* s = dst;
BYTE* d = dst + (h-2)*pitch;
for(; s < d; s += pitch*2)
{
BYTE* tmp = s;
#ifndef _WIN64
__asm
{
mov esi, tmp
mov ebx, pitch
mov ecx, ebx
shr ecx, 3
movq mm6, __0x03e003e003e003e0
movq mm7, __0x001f001f001f001f
AvgLines555_loop:
movq mm0, [esi]
movq mm1, mm0
movq mm2, mm0
psrlw mm0, 10 // red1 bits: mm0 = 001f001f001f001f
pand mm1, mm6 // green1 bits: mm1 = 03e003e003e003e0
pand mm2, mm7 // blue1 bits: mm2 = 001f001f001f001f
movq mm3, [esi+ebx*2]
movq mm4, mm3
movq mm5, mm3
psrlw mm3, 10 // red2 bits: mm3 = 001f001f001f001f
pand mm4, mm6 // green2 bits: mm4 = 03e003e003e003e0
pand mm5, mm7 // blue2 bits: mm5 = 001f001f001f001f
paddw mm0, mm3
psrlw mm0, 1 // (red1+red2)/2
psllw mm0, 10 // red bits at 7c007c007c007c00
paddw mm1, mm4
psrlw mm1, 1 // (green1+green2)/2
pand mm1, mm6 // green bits at 03e003e003e003e0
paddw mm2, mm5
psrlw mm2, 1 // (blue1+blue2)/2
// blue bits at 001f001f001f001f (no need to pand, lower bits were discareded)
por mm0, mm1
por mm0, mm2
movq [esi+ebx], mm0
lea esi, [esi+8]
dec ecx
jnz AvgLines555_loop
mov tmp, esi
}
#endif
for(ptrdiff_t i = (pitch&7)>>1; i--; tmp++)
{
tmp[pitch] =
((((*tmp&0x7c00) + (tmp[pitch<<1]&0x7c00)) >> 1)&0x7c00)|
((((*tmp&0x03e0) + (tmp[pitch<<1]&0x03e0)) >> 1)&0x03e0)|
((((*tmp&0x001f) + (tmp[pitch<<1]&0x001f)) >> 1)&0x001f);
}
}
if(!(h&1) && h >= 2)
{
dst += (h-2)*pitch;
memcpy(dst + pitch, dst, pitch);
}
#ifndef _WIN64
__asm emms;
#endif
}
void AvgLines565(BYTE* dst, DWORD h, DWORD pitch)
{
if(h <= 1)
return;
unsigned __int64 __0x07e007e007e007e0 = 0x07e007e007e007e0;
unsigned __int64 __0x001f001f001f001f = 0x001f001f001f001f;
BYTE* s = dst;
BYTE* d = dst + (h-2)*pitch;
for(; s < d; s += pitch*2)
{
WORD* tmp = (WORD*)s;
#ifndef _WIN64
__asm
{
mov esi, tmp
mov ebx, pitch
mov ecx, ebx
shr ecx, 3
movq mm6, __0x07e007e007e007e0
movq mm7, __0x001f001f001f001f
AvgLines565_loop:
movq mm0, [esi]
movq mm1, mm0
movq mm2, mm0
psrlw mm0, 11 // red1 bits: mm0 = 001f001f001f001f
pand mm1, mm6 // green1 bits: mm1 = 07e007e007e007e0
pand mm2, mm7 // blue1 bits: mm2 = 001f001f001f001f
movq mm3, [esi+ebx*2]
movq mm4, mm3
movq mm5, mm3
psrlw mm3, 11 // red2 bits: mm3 = 001f001f001f001f
pand mm4, mm6 // green2 bits: mm4 = 07e007e007e007e0
pand mm5, mm7 // blue2 bits: mm5 = 001f001f001f001f
paddw mm0, mm3
psrlw mm0, 1 // (red1+red2)/2
psllw mm0, 11 // red bits at f800f800f800f800
paddw mm1, mm4
psrlw mm1, 1 // (green1+green2)/2
pand mm1, mm6 // green bits at 03e003e003e003e0
paddw mm2, mm5
psrlw mm2, 1 // (blue1+blue2)/2
// blue bits at 001f001f001f001f (no need to pand, lower bits were discareded)
por mm0, mm1
por mm0, mm2
movq [esi+ebx], mm0
lea esi, [esi+8]
dec ecx
jnz AvgLines565_loop
mov tmp, esi
}
#else
for(ptrdiff_t wd=(pitch>>3); wd--; tmp++)
{
tmp[0] =
((((*tmp&0xf800) + (tmp[pitch<<1]&0xf800)) >> 1)&0xf800)|
((((*tmp&0x07e0) + (tmp[pitch<<1]&0x07e0)) >> 1)&0x07e0)|
((((*tmp&0x001f) + (tmp[pitch<<1]&0x001f)) >> 1)&0x001f);
}
#endif
for(ptrdiff_t i = (pitch&7)>>1; i--; tmp++)
{
tmp[pitch] =
((((*tmp&0xf800) + (tmp[pitch<<1]&0xf800)) >> 1)&0xf800)|
((((*tmp&0x07e0) + (tmp[pitch<<1]&0x07e0)) >> 1)&0x07e0)|
((((*tmp&0x001f) + (tmp[pitch<<1]&0x001f)) >> 1)&0x001f);
}
}
if(!(h&1) && h >= 2)
{
dst += (h-2)*pitch;
memcpy(dst + pitch, dst, pitch);
}
#ifndef _WIN64
__asm emms;
#endif
}
|