Merge commit 'aba70bb5387f12dfa5e6cd8cb861c9c7e668151f'
[ffmpeg.git] / libswscale / x86 / yuv2rgb_template.c
1 /*
2  * software YUV to RGB converter
3  *
4  * Copyright (C) 2001-2007 Michael Niedermayer
5  *           (c) 2010 Konstantin Shishkov
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg 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 GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <stdint.h>
25
26 #include "libavutil/x86/asm.h"
27 #include "libswscale/swscale_internal.h"
28
29 #undef MOVNTQ
30 #undef EMMS
31 #undef SFENCE
32
33 #if COMPILE_TEMPLATE_MMXEXT
34 #define MOVNTQ "movntq"
35 #define SFENCE "sfence"
36 #else
37 #define MOVNTQ "movq"
38 #define SFENCE " # nop"
39 #endif
40
41 #define REG_BLUE  "0"
42 #define REG_RED   "1"
43 #define REG_GREEN "2"
44 #define REG_ALPHA "3"
45
46 #define YUV2RGB_LOOP(depth)                                          \
47     h_size = (c->dstW + 7) & ~7;                                     \
48     if (h_size * depth > FFABS(dstStride[0]))                        \
49         h_size -= 8;                                                 \
50                                                                      \
51     vshift = c->srcFormat != AV_PIX_FMT_YUV422P;                        \
52                                                                      \
53     __asm__ volatile ("pxor %mm4, %mm4\n\t");                        \
54     for (y = 0; y < srcSliceH; y++) {                                \
55         uint8_t *image    = dst[0] + (y + srcSliceY) * dstStride[0]; \
56         const uint8_t *py = src[0] +               y * srcStride[0]; \
57         const uint8_t *pu = src[1] +   (y >> vshift) * srcStride[1]; \
58         const uint8_t *pv = src[2] +   (y >> vshift) * srcStride[2]; \
59         x86_reg index = -h_size / 2;                                 \
60
61 #define YUV2RGB_INITIAL_LOAD          \
62     __asm__ volatile (                \
63         "movq (%5, %0, 2), %%mm6\n\t" \
64         "movd    (%2, %0), %%mm0\n\t" \
65         "movd    (%3, %0), %%mm1\n\t" \
66         "1: \n\t"                     \
67
68 /* YUV2RGB core
69  * Conversion is performed in usual way:
70  * R = Y' * Ycoef + Vred * V'
71  * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
72  * B = Y' * Ycoef               + Ublue * U'
73  *
74  * where X' = X * 8 - Xoffset (multiplication is performed to increase
75  * precision a bit).
76  * Since it operates in YUV420 colorspace, Y component is additionally
77  * split into Y1 and Y2 for even and odd pixels.
78  *
79  * Input:
80  * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
81  * Output:
82  * mm1 - R, mm2 - G, mm0 - B
83  */
84 #define YUV2RGB                                  \
85     /* convert Y, U, V into Y1', Y2', U', V' */  \
86     "movq      %%mm6, %%mm7\n\t"                 \
87     "punpcklbw %%mm4, %%mm0\n\t"                 \
88     "punpcklbw %%mm4, %%mm1\n\t"                 \
89     "pand     "MANGLE(mmx_00ffw)", %%mm6\n\t"    \
90     "psrlw     $8,    %%mm7\n\t"                 \
91     "psllw     $3,    %%mm0\n\t"                 \
92     "psllw     $3,    %%mm1\n\t"                 \
93     "psllw     $3,    %%mm6\n\t"                 \
94     "psllw     $3,    %%mm7\n\t"                 \
95     "psubsw   "U_OFFSET"(%4), %%mm0\n\t"         \
96     "psubsw   "V_OFFSET"(%4), %%mm1\n\t"         \
97     "psubw    "Y_OFFSET"(%4), %%mm6\n\t"         \
98     "psubw    "Y_OFFSET"(%4), %%mm7\n\t"         \
99 \
100      /* multiply by coefficients */              \
101     "movq      %%mm0, %%mm2\n\t"                 \
102     "movq      %%mm1, %%mm3\n\t"                 \
103     "pmulhw   "UG_COEFF"(%4), %%mm2\n\t"         \
104     "pmulhw   "VG_COEFF"(%4), %%mm3\n\t"         \
105     "pmulhw   "Y_COEFF" (%4), %%mm6\n\t"         \
106     "pmulhw   "Y_COEFF" (%4), %%mm7\n\t"         \
107     "pmulhw   "UB_COEFF"(%4), %%mm0\n\t"         \
108     "pmulhw   "VR_COEFF"(%4), %%mm1\n\t"         \
109     "paddsw    %%mm3, %%mm2\n\t"                 \
110     /* now: mm0 = UB, mm1 = VR, mm2 = CG */      \
111     /*      mm6 = Y1, mm7 = Y2 */                \
112 \
113     /* produce RGB */                            \
114     "movq      %%mm7, %%mm3\n\t"                 \
115     "movq      %%mm7, %%mm5\n\t"                 \
116     "paddsw    %%mm0, %%mm3\n\t"                 \
117     "paddsw    %%mm1, %%mm5\n\t"                 \
118     "paddsw    %%mm2, %%mm7\n\t"                 \
119     "paddsw    %%mm6, %%mm0\n\t"                 \
120     "paddsw    %%mm6, %%mm1\n\t"                 \
121     "paddsw    %%mm6, %%mm2\n\t"                 \
122
123 #define RGB_PACK_INTERLEAVE                  \
124     /* pack and interleave even/odd pixels */    \
125     "packuswb  %%mm1, %%mm0\n\t"                 \
126     "packuswb  %%mm5, %%mm3\n\t"                 \
127     "packuswb  %%mm2, %%mm2\n\t"                 \
128     "movq      %%mm0, %%mm1\n\n"                 \
129     "packuswb  %%mm7, %%mm7\n\t"                 \
130     "punpcklbw %%mm3, %%mm0\n\t"                 \
131     "punpckhbw %%mm3, %%mm1\n\t"                 \
132     "punpcklbw %%mm7, %%mm2\n\t"                 \
133
134 #define YUV2RGB_ENDLOOP(depth)                   \
135     "movq 8 (%5, %0, 2), %%mm6\n\t"              \
136     "movd 4 (%3, %0),    %%mm1\n\t"              \
137     "movd 4 (%2, %0),    %%mm0\n\t"              \
138     "add $"AV_STRINGIFY(depth * 8)", %1\n\t"     \
139     "add  $4, %0\n\t"                            \
140     "js   1b\n\t"                                \
141
142 #if COMPILE_TEMPLATE_MMXEXT
143 #define RGB_PACK24_B_OPERANDS NAMED_CONSTRAINTS_ADD(mask1101,mask0110,mask0100,mask0010,mask1001)
144 #else
145 #define RGB_PACK24_B_OPERANDS
146 #endif
147
148 #define YUV2RGB_OPERANDS                                          \
149         : "+r" (index), "+r" (image)                              \
150         : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
151           "r" (py - 2*index)                                      \
152           NAMED_CONSTRAINTS_ADD(mmx_00ffw,pb_03,pb_07,mmx_redmask,pb_e0) \
153           RGB_PACK24_B_OPERANDS                                   \
154         : "memory"                                                \
155         );                                                        \
156     }                                                             \
157
158 #define YUV2RGB_OPERANDS_ALPHA                                    \
159         : "+r" (index), "+r" (image)                              \
160         : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
161           "r" (py - 2*index), "r" (pa - 2*index)                  \
162           NAMED_CONSTRAINTS_ADD(mmx_00ffw)                        \
163         : "memory"                                                \
164         );                                                        \
165     }                                                             \
166
167 #define YUV2RGB_ENDFUNC                          \
168     __asm__ volatile (SFENCE"\n\t"               \
169                     "emms    \n\t");             \
170     return srcSliceH;                            \
171
172 #define IF0(x)
173 #define IF1(x) x
174
175 #define RGB_PACK16(gmask, is15)                  \
176     "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
177     "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
178     "movq      %%mm2,     %%mm3\n\t"             \
179     "psllw   $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
180     "psrlw   $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
181     "psrlw     $3,        %%mm0\n\t"             \
182     IF##is15("psrlw  $1,  %%mm1\n\t")            \
183     "pand "MANGLE(pb_e0)", %%mm2\n\t"            \
184     "pand "MANGLE(gmask)", %%mm3\n\t"            \
185     "por       %%mm2,     %%mm0\n\t"             \
186     "por       %%mm3,     %%mm1\n\t"             \
187     "movq      %%mm0,     %%mm2\n\t"             \
188     "punpcklbw %%mm1,     %%mm0\n\t"             \
189     "punpckhbw %%mm1,     %%mm2\n\t"             \
190     MOVNTQ "   %%mm0,      (%1)\n\t"             \
191     MOVNTQ "   %%mm2,     8(%1)\n\t"             \
192
193 #define DITHER_RGB                               \
194     "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
195     "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
196     "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \
197
198 #if !COMPILE_TEMPLATE_MMXEXT
199 static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
200                                        int srcStride[],
201                                        int srcSliceY, int srcSliceH,
202                                        uint8_t *dst[], int dstStride[])
203 {
204     int y, h_size, vshift;
205
206     YUV2RGB_LOOP(2)
207
208 #ifdef DITHER1XBPP
209         c->blueDither  = ff_dither8[y       & 1];
210         c->greenDither = ff_dither8[y       & 1];
211         c->redDither   = ff_dither8[(y + 1) & 1];
212 #endif
213
214         YUV2RGB_INITIAL_LOAD
215         YUV2RGB
216         RGB_PACK_INTERLEAVE
217 #ifdef DITHER1XBPP
218         DITHER_RGB
219 #endif
220         RGB_PACK16(pb_03, 1)
221
222     YUV2RGB_ENDLOOP(2)
223     YUV2RGB_OPERANDS
224     YUV2RGB_ENDFUNC
225 }
226
227 static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
228                                        int srcStride[],
229                                        int srcSliceY, int srcSliceH,
230                                        uint8_t *dst[], int dstStride[])
231 {
232     int y, h_size, vshift;
233
234     YUV2RGB_LOOP(2)
235
236 #ifdef DITHER1XBPP
237         c->blueDither  = ff_dither8[y       & 1];
238         c->greenDither = ff_dither4[y       & 1];
239         c->redDither   = ff_dither8[(y + 1) & 1];
240 #endif
241
242         YUV2RGB_INITIAL_LOAD
243         YUV2RGB
244         RGB_PACK_INTERLEAVE
245 #ifdef DITHER1XBPP
246         DITHER_RGB
247 #endif
248         RGB_PACK16(pb_07, 0)
249
250     YUV2RGB_ENDLOOP(2)
251     YUV2RGB_OPERANDS
252     YUV2RGB_ENDFUNC
253 }
254 #endif /* !COMPILE_TEMPLATE_MMXEXT */
255
256 #define RGB_PACK24(blue, red)\
257     "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
258     "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
259     "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
260     "movq      %%mm"red",  %%mm3 \n"\
261     "movq      %%mm"blue", %%mm6 \n"\
262     "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
263     "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
264     "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
265     "movq      %%mm3,      %%mm5 \n"\
266     "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
267     "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
268     "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
269     RGB_PACK24_B
270
271 #if COMPILE_TEMPLATE_MMXEXT
272 DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
273 DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
274 DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
275 DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
276 DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
277 #undef RGB_PACK24_B
278 #define RGB_PACK24_B\
279     "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
280     "pshufw    $0x84,  %%mm3, %%mm6 \n"\
281     "pshufw    $0x38,  %%mm5, %%mm7 \n"\
282     "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
283     "movq      %%mm1,         %%mm0 \n"\
284     "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
285     "movq      %%mm1,         %%mm2 \n"\
286     "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
287     "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
288     "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
289     "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
290     "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
291     "por       %%mm3,         %%mm1 \n"\
292     "por       %%mm6,         %%mm0 \n"\
293     "por       %%mm5,         %%mm1 \n"\
294     "por       %%mm7,         %%mm2 \n"\
295     MOVNTQ"    %%mm0,          (%1) \n"\
296     MOVNTQ"    %%mm1,         8(%1) \n"\
297     MOVNTQ"    %%mm2,        16(%1) \n"\
298
299 #else
300 #undef RGB_PACK24_B
301 #define RGB_PACK24_B\
302     "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
303     "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
304     "psrlq     $32,        %%mm3 \n"\
305     "psrlq     $16,        %%mm2 \n"\
306     "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
307     "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
308     "psrlq     $16,        %%mm2 \n"\
309     "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
310     "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
311     "psrlq     $32,        %%mm5 \n"\
312     "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
313     "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\
314
315 #endif
316
317 static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
318                                        int srcStride[],
319                                        int srcSliceY, int srcSliceH,
320                                        uint8_t *dst[], int dstStride[])
321 {
322     int y, h_size, vshift;
323
324     YUV2RGB_LOOP(3)
325
326         YUV2RGB_INITIAL_LOAD
327         YUV2RGB
328         RGB_PACK24(REG_BLUE, REG_RED)
329
330     YUV2RGB_ENDLOOP(3)
331     YUV2RGB_OPERANDS
332     YUV2RGB_ENDFUNC
333 }
334
335 static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
336                                        int srcStride[],
337                                        int srcSliceY, int srcSliceH,
338                                        uint8_t *dst[], int dstStride[])
339 {
340     int y, h_size, vshift;
341
342     YUV2RGB_LOOP(3)
343
344         YUV2RGB_INITIAL_LOAD
345         YUV2RGB
346         RGB_PACK24(REG_RED, REG_BLUE)
347
348     YUV2RGB_ENDLOOP(3)
349     YUV2RGB_OPERANDS
350     YUV2RGB_ENDFUNC
351 }
352
353
354 #define SET_EMPTY_ALPHA                                                      \
355     "pcmpeqd   %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
356
357 #define LOAD_ALPHA                                   \
358     "movq      (%6, %0, 2),     %%mm"REG_ALPHA"\n\t" \
359
360 #define RGB_PACK32(red, green, blue, alpha)  \
361     "movq      %%mm"blue",  %%mm5\n\t"       \
362     "movq      %%mm"red",   %%mm6\n\t"       \
363     "punpckhbw %%mm"green", %%mm5\n\t"       \
364     "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
365     "punpckhbw %%mm"alpha", %%mm6\n\t"       \
366     "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
367     "movq      %%mm"blue",  %%mm"green"\n\t" \
368     "movq      %%mm5,       %%mm"alpha"\n\t" \
369     "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
370     "punpckhwd %%mm"red",   %%mm"green"\n\t" \
371     "punpcklwd %%mm6,       %%mm5\n\t"       \
372     "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
373     MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
374     MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
375     MOVNTQ "   %%mm5,       16(%1)\n\t"      \
376     MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \
377
378 #if !COMPILE_TEMPLATE_MMXEXT
379 static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
380                                        int srcStride[],
381                                        int srcSliceY, int srcSliceH,
382                                        uint8_t *dst[], int dstStride[])
383 {
384     int y, h_size, vshift;
385
386     YUV2RGB_LOOP(4)
387
388         YUV2RGB_INITIAL_LOAD
389         YUV2RGB
390         RGB_PACK_INTERLEAVE
391         SET_EMPTY_ALPHA
392         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
393
394     YUV2RGB_ENDLOOP(4)
395     YUV2RGB_OPERANDS
396     YUV2RGB_ENDFUNC
397 }
398
399 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
400 static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
401                                         int srcStride[],
402                                         int srcSliceY, int srcSliceH,
403                                         uint8_t *dst[], int dstStride[])
404 {
405     int y, h_size, vshift;
406
407     YUV2RGB_LOOP(4)
408
409         const uint8_t *pa = src[3] + y * srcStride[3];
410         YUV2RGB_INITIAL_LOAD
411         YUV2RGB
412         RGB_PACK_INTERLEAVE
413         LOAD_ALPHA
414         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
415
416     YUV2RGB_ENDLOOP(4)
417     YUV2RGB_OPERANDS_ALPHA
418     YUV2RGB_ENDFUNC
419 }
420 #endif
421
422 static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
423                                        int srcStride[],
424                                        int srcSliceY, int srcSliceH,
425                                        uint8_t *dst[], int dstStride[])
426 {
427     int y, h_size, vshift;
428
429     YUV2RGB_LOOP(4)
430
431         YUV2RGB_INITIAL_LOAD
432         YUV2RGB
433         RGB_PACK_INTERLEAVE
434         SET_EMPTY_ALPHA
435         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
436
437     YUV2RGB_ENDLOOP(4)
438     YUV2RGB_OPERANDS
439     YUV2RGB_ENDFUNC
440 }
441
442 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
443 static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
444                                         int srcStride[],
445                                         int srcSliceY, int srcSliceH,
446                                         uint8_t *dst[], int dstStride[])
447 {
448     int y, h_size, vshift;
449
450     YUV2RGB_LOOP(4)
451
452         const uint8_t *pa = src[3] + y * srcStride[3];
453         YUV2RGB_INITIAL_LOAD
454         YUV2RGB
455         RGB_PACK_INTERLEAVE
456         LOAD_ALPHA
457         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
458
459     YUV2RGB_ENDLOOP(4)
460     YUV2RGB_OPERANDS_ALPHA
461     YUV2RGB_ENDFUNC
462 }
463 #endif
464
465 #endif /* !COMPILE_TEMPLATE_MMXEXT */