1 /*****************************************************************************
2 * copy.c: Fast YV12/NV12 copy
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_picture.h>
35 int CopyInitCache(copy_cache_t *cache, unsigned width)
37 #ifdef CAN_COMPILE_SSE2
38 cache->size = __MAX((width + 0x0f) & ~ 0x0f, 4096);
39 cache->buffer = vlc_memalign(16, cache->size);
43 (void) cache; (void) width;
48 void CopyCleanCache(copy_cache_t *cache)
50 #ifdef CAN_COMPILE_SSE2
51 vlc_free(cache->buffer);
59 #ifdef CAN_COMPILE_SSE2
60 /* Copy 64 bytes from srcp to dstp loading data with the SSE>=2 instruction
61 * load and storing data with the SSE>=2 instruction store.
63 #define COPY64(dstp, srcp, load, store) \
65 load " 0(%[src]), %%xmm1\n" \
66 load " 16(%[src]), %%xmm2\n" \
67 load " 32(%[src]), %%xmm3\n" \
68 load " 48(%[src]), %%xmm4\n" \
69 store " %%xmm1, 0(%[dst])\n" \
70 store " %%xmm2, 16(%[dst])\n" \
71 store " %%xmm3, 32(%[dst])\n" \
72 store " %%xmm4, 48(%[dst])\n" \
73 : : [dst]"r"(dstp), [src]"r"(srcp) : "memory", "xmm1", "xmm2", "xmm3", "xmm4")
76 # undef vlc_CPU_SSE4_1
77 # define vlc_CPU_SSE4_1() ((cpu & VLC_CPU_SSE4_1) != 0)
82 # define vlc_CPU_SSSE3() ((cpu & VLC_CPU_SSSE3) != 0)
87 # define vlc_CPU_SSE2() ((cpu & VLC_CPU_SSE2) != 0)
90 /* Optimized copy from "Uncacheable Speculative Write Combining" memory
91 * as used by some video surface.
92 * XXX It is really efficient only when SSE4.1 is available.
95 static void CopyFromUswc(uint8_t *dst, size_t dst_pitch,
96 const uint8_t *src, size_t src_pitch,
97 unsigned width, unsigned height,
100 assert(((intptr_t)dst & 0x0f) == 0 && (dst_pitch & 0x0f) == 0);
102 asm volatile ("mfence");
104 for (unsigned y = 0; y < height; y++) {
105 const unsigned unaligned = (-(uintptr_t)src) & 0x0f;
108 for (; x < unaligned; x++)
111 #ifdef CAN_COMPILE_SSE4_1
112 if (vlc_CPU_SSE4_1()) {
114 for (; x+63 < width; x += 64)
115 COPY64(&dst[x], &src[x], "movntdqa", "movdqa");
117 for (; x+63 < width; x += 64)
118 COPY64(&dst[x], &src[x], "movntdqa", "movdqu");
124 for (; x+63 < width; x += 64)
125 COPY64(&dst[x], &src[x], "movdqa", "movdqa");
127 for (; x+63 < width; x += 64)
128 COPY64(&dst[x], &src[x], "movdqa", "movdqu");
132 for (; x < width; x++)
141 static void Copy2d(uint8_t *dst, size_t dst_pitch,
142 const uint8_t *src, size_t src_pitch,
143 unsigned width, unsigned height)
145 assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
147 asm volatile ("mfence");
149 for (unsigned y = 0; y < height; y++) {
152 bool unaligned = ((intptr_t)dst & 0x0f) != 0;
154 for (; x+63 < width; x += 64)
155 COPY64(&dst[x], &src[x], "movdqa", "movntdq");
157 for (; x+63 < width; x += 64)
158 COPY64(&dst[x], &src[x], "movdqa", "movdqu");
161 for (; x < width; x++)
170 static void SSE_SplitUV(uint8_t *dstu, size_t dstu_pitch,
171 uint8_t *dstv, size_t dstv_pitch,
172 const uint8_t *src, size_t src_pitch,
173 unsigned width, unsigned height, unsigned cpu)
175 const uint8_t shuffle[] = { 0, 2, 4, 6, 8, 10, 12, 14,
176 1, 3, 5, 7, 9, 11, 13, 15 };
177 const uint8_t mask[] = { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
178 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00 };
180 assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
182 asm volatile ("mfence");
184 for (unsigned y = 0; y < height; y++) {
188 "movdqa 0(%[src]), %%xmm0\n" \
189 "movdqa 16(%[src]), %%xmm1\n" \
190 "movdqa 32(%[src]), %%xmm2\n" \
191 "movdqa 48(%[src]), %%xmm3\n"
194 "movq %%xmm0, 0(%[dst1])\n" \
195 "movq %%xmm1, 8(%[dst1])\n" \
196 "movhpd %%xmm0, 0(%[dst2])\n" \
197 "movhpd %%xmm1, 8(%[dst2])\n" \
198 "movq %%xmm2, 16(%[dst1])\n" \
199 "movq %%xmm3, 24(%[dst1])\n" \
200 "movhpd %%xmm2, 16(%[dst2])\n" \
201 "movhpd %%xmm3, 24(%[dst2])\n"
203 #ifdef CAN_COMPILE_SSSE3
206 for (x = 0; x < (width & ~31); x += 32) {
208 "movdqu (%[shuffle]), %%xmm7\n"
210 "pshufb %%xmm7, %%xmm0\n"
211 "pshufb %%xmm7, %%xmm1\n"
212 "pshufb %%xmm7, %%xmm2\n"
213 "pshufb %%xmm7, %%xmm3\n"
215 : : [dst1]"r"(&dstu[x]), [dst2]"r"(&dstv[x]), [src]"r"(&src[2*x]), [shuffle]"r"(shuffle) : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm7");
220 for (x = 0; x < (width & ~31); x += 32) {
222 "movdqu (%[mask]), %%xmm7\n"
224 "movdqa %%xmm0, %%xmm4\n"
225 "movdqa %%xmm1, %%xmm5\n"
226 "movdqa %%xmm2, %%xmm6\n"
229 "pand %%xmm7, %%xmm4\n"
230 "pand %%xmm7, %%xmm5\n"
231 "pand %%xmm7, %%xmm6\n"
232 "packuswb %%xmm4, %%xmm0\n"
233 "packuswb %%xmm5, %%xmm1\n"
234 "pand %%xmm3, %%xmm7\n"
237 "packuswb %%xmm6, %%xmm2\n"
238 "packuswb %%xmm7, %%xmm3\n"
240 : : [dst2]"r"(&dstu[x]), [dst1]"r"(&dstv[x]), [src]"r"(&src[2*x]), [mask]"r"(mask) : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7");
246 for (; x < width; x++) {
247 dstu[x] = src[2*x+0];
248 dstv[x] = src[2*x+1];
256 static void SSE_CopyPlane(uint8_t *dst, size_t dst_pitch,
257 const uint8_t *src, size_t src_pitch,
258 uint8_t *cache, size_t cache_size,
259 unsigned width, unsigned height, unsigned cpu)
261 const unsigned w16 = (width+15) & ~15;
262 const unsigned hstep = cache_size / w16;
265 for (unsigned y = 0; y < height; y += hstep) {
266 const unsigned hblock = __MIN(hstep, height - y);
268 /* Copy a bunch of line into our cache */
269 CopyFromUswc(cache, w16,
273 /* Copy from our cache to the destination */
274 Copy2d(dst, dst_pitch,
279 src += src_pitch * hblock;
280 dst += dst_pitch * hblock;
282 asm volatile ("mfence");
285 static void SSE_SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
286 uint8_t *dstv, size_t dstv_pitch,
287 const uint8_t *src, size_t src_pitch,
288 uint8_t *cache, size_t cache_size,
289 unsigned width, unsigned height, unsigned cpu)
291 const unsigned w2_16 = (2*width+15) & ~15;
292 const unsigned hstep = cache_size / w2_16;
295 for (unsigned y = 0; y < height; y += hstep) {
296 const unsigned hblock = __MIN(hstep, height - y);
298 /* Copy a bunch of line into our cache */
299 CopyFromUswc(cache, w2_16, src, src_pitch,
300 2*width, hblock, cpu);
302 /* Copy from our cache to the destination */
303 SSE_SplitUV(dstu, dstu_pitch, dstv, dstv_pitch,
304 cache, w2_16, width, hblock, cpu);
307 src += src_pitch * hblock;
308 dstu += dstu_pitch * hblock;
309 dstv += dstv_pitch * hblock;
311 asm volatile ("mfence");
314 static void SSE_CopyFromNv12(picture_t *dst,
315 uint8_t *src[2], size_t src_pitch[2],
316 unsigned width, unsigned height,
317 copy_cache_t *cache, unsigned cpu)
319 SSE_CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
320 src[0], src_pitch[0],
321 cache->buffer, cache->size,
323 SSE_SplitPlanes(dst->p[2].p_pixels, dst->p[2].i_pitch,
324 dst->p[1].p_pixels, dst->p[1].i_pitch,
325 src[1], src_pitch[1],
326 cache->buffer, cache->size,
327 width/2, height/2, cpu);
328 asm volatile ("emms");
331 static void SSE_CopyFromYv12(picture_t *dst,
332 uint8_t *src[3], size_t src_pitch[3],
333 unsigned width, unsigned height,
334 copy_cache_t *cache, unsigned cpu)
336 for (unsigned n = 0; n < 3; n++) {
337 const unsigned d = n > 0 ? 2 : 1;
338 SSE_CopyPlane(dst->p[n].p_pixels, dst->p[n].i_pitch,
339 src[n], src_pitch[n],
340 cache->buffer, cache->size,
341 width/d, height/d, cpu);
343 asm volatile ("emms");
346 #endif /* CAN_COMPILE_SSE2 */
348 static void CopyPlane(uint8_t *dst, size_t dst_pitch,
349 const uint8_t *src, size_t src_pitch,
350 unsigned width, unsigned height)
352 for (unsigned y = 0; y < height; y++) {
353 memcpy(dst, src, width);
359 static void SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
360 uint8_t *dstv, size_t dstv_pitch,
361 const uint8_t *src, size_t src_pitch,
362 unsigned width, unsigned height)
364 for (unsigned y = 0; y < height; y++) {
365 for (unsigned x = 0; x < width; x++) {
366 dstu[x] = src[2*x+0];
367 dstv[x] = src[2*x+1];
375 void CopyFromNv12(picture_t *dst, uint8_t *src[2], size_t src_pitch[2],
376 unsigned width, unsigned height,
379 #ifdef CAN_COMPILE_SSE2
380 unsigned cpu = vlc_CPU();
382 return SSE_CopyFromNv12(dst, src, src_pitch, width, height,
388 CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
389 src[0], src_pitch[0],
391 SplitPlanes(dst->p[2].p_pixels, dst->p[2].i_pitch,
392 dst->p[1].p_pixels, dst->p[1].i_pitch,
393 src[1], src_pitch[1],
397 void CopyFromYv12(picture_t *dst, uint8_t *src[3], size_t src_pitch[3],
398 unsigned width, unsigned height,
401 #ifdef CAN_COMPILE_SSE2
402 unsigned cpu = vlc_CPU();
404 return SSE_CopyFromYv12(dst, src, src_pitch, width, height,
410 CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
411 src[0], src_pitch[0], width, height);
412 CopyPlane(dst->p[1].p_pixels, dst->p[1].i_pitch,
413 src[1], src_pitch[1], width / 2, height / 2);
414 CopyPlane(dst->p[2].p_pixels, dst->p[2].i_pitch,
415 src[1], src_pitch[2], width / 2, height / 2);