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