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