]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/copy.c
avcodec: split generic YV12/NV12 copy out of SSE2 functions
[vlc] / modules / codec / avcodec / copy.c
1 /*****************************************************************************
2  * copy.c: Fast YV12/NV12 copy
3  *****************************************************************************
4  * Copyright (C) 2010 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_picture.h>
30 #include <vlc_cpu.h>
31 #include <assert.h>
32
33 #include "copy.h"
34
35 int CopyInitCache(copy_cache_t *cache, unsigned width)
36 {
37 #ifdef CAN_COMPILE_SSE2
38     cache->size = __MAX((width + 0x0f) & ~ 0x0f, 4096);
39     cache->buffer = vlc_memalign(16, cache->size);
40     if (!cache->buffer)
41         return VLC_EGENERIC;
42 #else
43     (void) cache; (void) width;
44 #endif
45     return VLC_SUCCESS;
46 }
47
48 void CopyCleanCache(copy_cache_t *cache)
49 {
50 #ifdef CAN_COMPILE_SSE2
51     vlc_free(cache->buffer);
52     cache->buffer = NULL;
53     cache->size   = 0;
54 #else
55     (void) cache;
56 #endif
57 }
58
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.
62  */
63 #define COPY64(dstp, srcp, load, store) \
64     asm volatile (                      \
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")
74
75 #ifndef __SSE4_1__
76 # undef vlc_CPU_SSE4_1
77 # define vlc_CPU_SSE4_1() ((cpu & VLC_CPU_SSE4_1) != 0)
78 #endif
79
80 #ifndef __SSSE3__
81 # undef vlc_CPU_SSSE3
82 # define vlc_CPU_SSSE3() ((cpu & VLC_CPU_SSSE3) != 0)
83 #endif
84
85 #ifndef __SSE2__
86 # undef vlc_CPU_SSE2
87 # define vlc_CPU_SSE2() ((cpu & VLC_CPU_SSE2) != 0)
88 #endif
89
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.
93  */
94 static void CopyFromUswc(uint8_t *dst, size_t dst_pitch,
95                          const uint8_t *src, size_t src_pitch,
96                          unsigned width, unsigned height,
97                          unsigned cpu)
98 {
99     assert(((intptr_t)dst & 0x0f) == 0 && (dst_pitch & 0x0f) == 0);
100
101     asm volatile ("mfence");
102
103     for (unsigned y = 0; y < height; y++) {
104         const unsigned unaligned = (-(uintptr_t)src) & 0x0f;
105         unsigned x = 0;
106
107         for (; x < unaligned; x++)
108             dst[x] = src[x];
109
110 #ifdef CAN_COMPILE_SSE4_1
111         if (vlc_CPU_SSE4_1()) {
112             if (!unaligned) {
113                 for (; x+63 < width; x += 64)
114                     COPY64(&dst[x], &src[x], "movntdqa", "movdqa");
115             } else {
116                 for (; x+63 < width; x += 64)
117                     COPY64(&dst[x], &src[x], "movntdqa", "movdqu");
118             }
119         } else
120 #endif
121         {
122             if (!unaligned) {
123                 for (; x+63 < width; x += 64)
124                     COPY64(&dst[x], &src[x], "movdqa", "movdqa");
125             } else {
126                 for (; x+63 < width; x += 64)
127                     COPY64(&dst[x], &src[x], "movdqa", "movdqu");
128             }
129         }
130
131         for (; x < width; x++)
132             dst[x] = src[x];
133
134         src += src_pitch;
135         dst += dst_pitch;
136     }
137 }
138
139 static void Copy2d(uint8_t *dst, size_t dst_pitch,
140                    const uint8_t *src, size_t src_pitch,
141                    unsigned width, unsigned height)
142 {
143     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
144
145     asm volatile ("mfence");
146
147     for (unsigned y = 0; y < height; y++) {
148         unsigned x = 0;
149
150         bool unaligned = ((intptr_t)dst & 0x0f) != 0;
151         if (!unaligned) {
152             for (; x+63 < width; x += 64)
153                 COPY64(&dst[x], &src[x], "movdqa", "movntdq");
154         } else {
155             for (; x+63 < width; x += 64)
156                 COPY64(&dst[x], &src[x], "movdqa", "movdqu");
157         }
158
159         for (; x < width; x++)
160             dst[x] = src[x];
161
162         src += src_pitch;
163         dst += dst_pitch;
164     }
165 }
166
167 static void SSE_SplitUV(uint8_t *dstu, size_t dstu_pitch,
168                         uint8_t *dstv, size_t dstv_pitch,
169                         const uint8_t *src, size_t src_pitch,
170                         unsigned width, unsigned height, unsigned cpu)
171 {
172     const uint8_t shuffle[] = { 0, 2, 4, 6, 8, 10, 12, 14,
173                                 1, 3, 5, 7, 9, 11, 13, 15 };
174     const uint8_t mask[] = { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
175                              0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00 };
176
177     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
178
179     asm volatile ("mfence");
180
181     for (unsigned y = 0; y < height; y++) {
182         unsigned x = 0;
183
184 #define LOAD64 \
185     "movdqa  0(%[src]), %%xmm0\n" \
186     "movdqa 16(%[src]), %%xmm1\n" \
187     "movdqa 32(%[src]), %%xmm2\n" \
188     "movdqa 48(%[src]), %%xmm3\n"
189
190 #define STORE2X32 \
191     "movq   %%xmm0,   0(%[dst1])\n" \
192     "movq   %%xmm1,   8(%[dst1])\n" \
193     "movhpd %%xmm0,   0(%[dst2])\n" \
194     "movhpd %%xmm1,   8(%[dst2])\n" \
195     "movq   %%xmm2,  16(%[dst1])\n" \
196     "movq   %%xmm3,  24(%[dst1])\n" \
197     "movhpd %%xmm2,  16(%[dst2])\n" \
198     "movhpd %%xmm3,  24(%[dst2])\n"
199
200 #ifdef CAN_COMPILE_SSSE3
201         if (vlc_CPU_SSSE3())
202         {
203             for (x = 0; x < (width & ~31); x += 32) {
204                 asm volatile (
205                     "movdqu (%[shuffle]), %%xmm7\n"
206                     LOAD64
207                     "pshufb  %%xmm7, %%xmm0\n"
208                     "pshufb  %%xmm7, %%xmm1\n"
209                     "pshufb  %%xmm7, %%xmm2\n"
210                     "pshufb  %%xmm7, %%xmm3\n"
211                     STORE2X32
212                     : : [dst1]"r"(&dstu[x]), [dst2]"r"(&dstv[x]), [src]"r"(&src[2*x]), [shuffle]"r"(shuffle) : "memory");
213             }
214         } else
215 #endif
216         {
217             for (x = 0; x < (width & ~31); x += 32) {
218                 asm volatile (
219                     "movdqu (%[mask]), %%xmm7\n"
220                     LOAD64
221                     "movdqa   %%xmm0, %%xmm4\n"
222                     "movdqa   %%xmm1, %%xmm5\n"
223                     "movdqa   %%xmm2, %%xmm6\n"
224                     "psrlw    $8,     %%xmm0\n"
225                     "psrlw    $8,     %%xmm1\n"
226                     "pand     %%xmm7, %%xmm4\n"
227                     "pand     %%xmm7, %%xmm5\n"
228                     "pand     %%xmm7, %%xmm6\n"
229                     "packuswb %%xmm4, %%xmm0\n"
230                     "packuswb %%xmm5, %%xmm1\n"
231                     "pand     %%xmm3, %%xmm7\n"
232                     "psrlw    $8,     %%xmm2\n"
233                     "psrlw    $8,     %%xmm3\n"
234                     "packuswb %%xmm6, %%xmm2\n"
235                     "packuswb %%xmm7, %%xmm3\n"
236                     STORE2X32
237                     : : [dst2]"r"(&dstu[x]), [dst1]"r"(&dstv[x]), [src]"r"(&src[2*x]), [mask]"r"(mask) : "memory");
238             }
239         }
240 #undef STORE2X32
241 #undef LOAD64
242
243         for (; x < width; x++) {
244             dstu[x] = src[2*x+0];
245             dstv[x] = src[2*x+1];
246         }
247         src  += src_pitch;
248         dstu += dstu_pitch;
249         dstv += dstv_pitch;
250     }
251 }
252
253 static void SSE_CopyPlane(uint8_t *dst, size_t dst_pitch,
254                           const uint8_t *src, size_t src_pitch,
255                           uint8_t *cache, size_t cache_size,
256                           unsigned width, unsigned height, unsigned cpu)
257 {
258     const unsigned w16 = (width+15) & ~15;
259     const unsigned hstep = cache_size / w16;
260     assert(hstep > 0);
261
262     for (unsigned y = 0; y < height; y += hstep) {
263         const unsigned hblock =  __MIN(hstep, height - y);
264
265         /* Copy a bunch of line into our cache */
266         CopyFromUswc(cache, w16,
267                      src, src_pitch,
268                      width, hblock, cpu);
269
270         /* Copy from our cache to the destination */
271         Copy2d(dst, dst_pitch,
272                cache, w16,
273                width, hblock);
274
275         /* */
276         src += src_pitch * hblock;
277         dst += dst_pitch * hblock;
278     }
279     asm volatile ("mfence");
280 }
281
282 static void SSE_SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
283                             uint8_t *dstv, size_t dstv_pitch,
284                             const uint8_t *src, size_t src_pitch,
285                             uint8_t *cache, size_t cache_size,
286                             unsigned width, unsigned height, unsigned cpu)
287 {
288     const unsigned w2_16 = (2*width+15) & ~15;
289     const unsigned hstep = cache_size / w2_16;
290     assert(hstep > 0);
291
292     for (unsigned y = 0; y < height; y += hstep) {
293         const unsigned hblock =  __MIN(hstep, height - y);
294
295         /* Copy a bunch of line into our cache */
296         CopyFromUswc(cache, w2_16, src, src_pitch,
297                      2*width, hblock, cpu);
298
299         /* Copy from our cache to the destination */
300         SSE_SplitUV(dstu, dstu_pitch, dstv, dstv_pitch,
301                     cache, w2_16, width, hblock, cpu);
302
303         /* */
304         src  += src_pitch  * hblock;
305         dstu += dstu_pitch * hblock;
306         dstv += dstv_pitch * hblock;
307     }
308     asm volatile ("mfence");
309 }
310
311 static void SSE_CopyFromNv12(picture_t *dst,
312                              uint8_t *src[2], size_t src_pitch[2],
313                              unsigned width, unsigned height,
314                              copy_cache_t *cache)
315 {
316     const unsigned cpu = vlc_CPU();
317
318     /* */
319     SSE_CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
320                   src[0], src_pitch[0],
321                   cache->buffer, cache->size,
322                   width, height, cpu);
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");
329 }
330
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)
335 {
336     const unsigned cpu = vlc_CPU();
337
338     /* */
339     for (unsigned n = 0; n < 3; n++) {
340         const unsigned d = n > 0 ? 2 : 1;
341         SSE_CopyPlane(dst->p[n].p_pixels, dst->p[n].i_pitch,
342                       src[n], src_pitch[n],
343                       cache->buffer, cache->size,
344                       width/d, height/d, cpu);
345     }
346     asm volatile ("emms");
347 }
348 #undef COPY64
349 #endif /* CAN_COMPILE_SSE2 */
350
351 static void CopyPlane(uint8_t *dst, size_t dst_pitch,
352                       const uint8_t *src, size_t src_pitch,
353                       unsigned width, unsigned height)
354 {
355     for (unsigned y = 0; y < height; y++) {
356         memcpy(dst, src, width);
357         src += src_pitch;
358         dst += dst_pitch;
359     }
360 }
361
362 static void SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
363                         uint8_t *dstv, size_t dstv_pitch,
364                         const uint8_t *src, size_t src_pitch,
365                         unsigned width, unsigned height)
366 {
367     for (unsigned y = 0; y < height; y++) {
368         for (unsigned x = 0; x < width; x++) {
369             dstu[x] = src[2*x+0];
370             dstv[x] = src[2*x+1];
371         }
372         src  += src_pitch;
373         dstu += dstu_pitch;
374         dstv += dstv_pitch;
375     }
376 }
377
378 void CopyFromNv12(picture_t *dst, uint8_t *src[2], size_t src_pitch[2],
379                   unsigned width, unsigned height,
380                   copy_cache_t *cache)
381 {
382 #ifdef CAN_COMPILE_SSE2
383     unsigned cpu = vlc_CPU();
384     if (vlc_CPU_SSE2())
385         return SSE_CopyFromNv12(dst, src, src_pitch, width, height, cache);
386 #else
387     (void) cache;
388 #endif
389
390     CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
391               src[0], src_pitch[0],
392               width, height);
393     SplitPlanes(dst->p[2].p_pixels, dst->p[2].i_pitch,
394                 dst->p[1].p_pixels, dst->p[1].i_pitch,
395                 src[1], src_pitch[1],
396                 width/2, height/2);
397 }
398
399 void CopyFromYv12(picture_t *dst, uint8_t *src[3], size_t src_pitch[3],
400                   unsigned width, unsigned height,
401                   copy_cache_t *cache)
402 {
403 #ifdef CAN_COMPILE_SSE2
404     unsigned cpu = vlc_CPU();
405     if (vlc_CPU_SSE2())
406         return SSE_CopyFromYv12(dst, src, src_pitch, width, height, cache);
407 #else
408     (void) cache;
409 #endif
410
411      CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
412                src[0], src_pitch[0], width, height);
413      CopyPlane(dst->p[1].p_pixels, dst->p[1].i_pitch,
414                src[1], src_pitch[1], width / 2, height / 2);
415      CopyPlane(dst->p[2].p_pixels, dst->p[2].i_pitch,
416                src[1], src_pitch[2], width / 2, height / 2);
417 }