summaryrefslogtreecommitdiff
path: root/libswscale/x86/yuv2rgb_template.c
blob: b028e93152c178a6b5a220c6cb29f6d3b5331e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * software YUV to RGB converter
 *
 * Copyright (C) 2001-2007 Michael Niedermayer
 *           (c) 2010 Konstantin Shishkov
 *
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Libav is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#undef MOVNTQ
#undef EMMS
#undef SFENCE

#if COMPILE_TEMPLATE_MMXEXT
#define MOVNTQ "movntq"
#define SFENCE "sfence"
#else
#define MOVNTQ "movq"
#define SFENCE " # nop"
#endif

#define REG_BLUE  "0"
#define REG_RED   "1"
#define REG_GREEN "2"
#define REG_ALPHA "3"

#define YUV2RGB_LOOP(depth)                                          \
    h_size = (c->dstW + 7) & ~7;                                     \
    if (h_size * depth > FFABS(dstStride[0]))                        \
        h_size -= 8;                                                 \
                                                                     \
    if (c->srcFormat == AV_PIX_FMT_YUV422P) {                           \
        srcStride[1] *= 2;                                           \
        srcStride[2] *= 2;                                           \
    }                                                                \
                                                                     \
    __asm__ volatile ("pxor %mm4, %mm4\n\t");                        \
    for (y = 0; y < srcSliceH; y++) {                                \
        uint8_t *image    = dst[0] + (y + srcSliceY) * dstStride[0]; \
        const uint8_t *py = src[0] +               y * srcStride[0]; \
        const uint8_t *pu = src[1] +        (y >> 1) * srcStride[1]; \
        const uint8_t *pv = src[2] +        (y >> 1) * srcStride[2]; \
        x86_reg index = -h_size / 2;                                 \

#define YUV2RGB_INITIAL_LOAD          \
    __asm__ volatile (                \
        "movq (%5, %0, 2), %%mm6\n\t" \
        "movd    (%2, %0), %%mm0\n\t" \
        "movd    (%3, %0), %%mm1\n\t" \
        "1: \n\t"                     \

/* YUV2RGB core
 * Conversion is performed in usual way:
 * R = Y' * Ycoef + Vred * V'
 * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
 * B = Y' * Ycoef               + Ublue * U'
 *
 * where X' = X * 8 - Xoffset (multiplication is performed to increase
 * precision a bit).
 * Since it operates in YUV420 colorspace, Y component is additionally
 * split into Y1 and Y2 for even and odd pixels.
 *
 * Input:
 * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
 * Output:
 * mm1 - R, mm2 - G, mm0 - B
 */
#define YUV2RGB                                  \
    /* convert Y, U, V into Y1', Y2', U', V' */  \
    "movq      %%mm6, %%mm7\n\t"                 \
    "punpcklbw %%mm4, %%mm0\n\t"                 \
    "punpcklbw %%mm4, %%mm1\n\t"                 \
    "pand     "MANGLE(mmx_00ffw)", %%mm6\n\t"    \
    "psrlw     $8,    %%mm7\n\t"                 \
    "psllw     $3,    %%mm0\n\t"                 \
    "psllw     $3,    %%mm1\n\t"                 \
    "psllw     $3,    %%mm6\n\t"                 \
    "psllw     $3,    %%mm7\n\t"                 \
    "psubsw   "U_OFFSET"(%4), %%mm0\n\t"         \
    "psubsw   "V_OFFSET"(%4), %%mm1\n\t"         \
    "psubw    "Y_OFFSET"(%4), %%mm6\n\t"         \
    "psubw    "Y_OFFSET"(%4), %%mm7\n\t"         \
\
     /* multiply by coefficients */              \
    "movq      %%mm0, %%mm2\n\t"                 \
    "movq      %%mm1, %%mm3\n\t"                 \
    "pmulhw   "UG_COEFF"(%4), %%mm2\n\t"         \
    "pmulhw   "VG_COEFF"(%4), %%mm3\n\t"         \
    "pmulhw   "Y_COEFF" (%4), %%mm6\n\t"         \
    "pmulhw   "Y_COEFF" (%4), %%mm7\n\t"         \
    "pmulhw   "UB_COEFF"(%4), %%mm0\n\t"         \
    "pmulhw   "VR_COEFF"(%4), %%mm1\n\t"         \
    "paddsw    %%mm3, %%mm2\n\t"                 \
    /* now: mm0 = UB, mm1 = VR, mm2 = CG */      \
    /*      mm6 = Y1, mm7 = Y2 */                \
\
    /* produce RGB */                            \
    "movq      %%mm7, %%mm3\n\t"                 \
    "movq      %%mm7, %%mm5\n\t"                 \
    "paddsw    %%mm0, %%mm3\n\t"                 \
    "paddsw    %%mm1, %%mm5\n\t"                 \
    "paddsw    %%mm2, %%mm7\n\t"                 \
    "paddsw    %%mm6, %%mm0\n\t"                 \
    "paddsw    %%mm6, %%mm1\n\t"                 \
    "paddsw    %%mm6, %%mm2\n\t"                 \

#define RGB_PACK_INTERLEAVE                  \
    /* pack and interleave even/odd pixels */    \
    "packuswb  %%mm1, %%mm0\n\t"                 \
    "packuswb  %%mm5, %%mm3\n\t"                 \
    "packuswb  %%mm2, %%mm2\n\t"                 \
    "movq      %%mm0, %%mm1\n\n"                 \
    "packuswb  %%mm7, %%mm7\n\t"                 \
    "punpcklbw %%mm3, %%mm0\n\t"                 \
    "punpckhbw %%mm3, %%mm1\n\t"                 \
    "punpcklbw %%mm7, %%mm2\n\t"                 \

#define YUV2RGB_ENDLOOP(depth)                   \
    "movq 8 (%5, %0, 2), %%mm6\n\t"              \
    "movd 4 (%3, %0),    %%mm1\n\t"              \
    "movd 4 (%2, %0),    %%mm0\n\t"              \
    "add $"AV_STRINGIFY(depth * 8)", %1\n\t"     \
    "add  $4, %0\n\t"                            \
    "js   1b\n\t"                                \

#define YUV2RGB_OPERANDS                                          \
        : "+r" (index), "+r" (image)                              \
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
          "r" (py - 2*index)                                      \
        );                                                        \
    }                                                             \

#define YUV2RGB_OPERANDS_ALPHA                                    \
        : "+r" (index), "+r" (image)                              \
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
          "r" (py - 2*index), "r" (pa - 2*index)                  \
        );                                                        \
    }                                                             \

#define YUV2RGB_ENDFUNC                          \
    __asm__ volatile (SFENCE"\n\t"               \
                    "emms    \n\t");             \
    return srcSliceH;                            \

#define IF0(x)
#define IF1(x) x

#define RGB_PACK16(gmask, is15)                  \
    "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
    "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
    "movq      %%mm2,     %%mm3\n\t"             \
    "psllw   $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
    "psrlw   $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
    "psrlw     $3,        %%mm0\n\t"             \
    IF##is15("psrlw  $1,  %%mm1\n\t")            \
    "pand "MANGLE(pb_e0)", %%mm2\n\t"            \
    "pand "MANGLE(gmask)", %%mm3\n\t"            \
    "por       %%mm2,     %%mm0\n\t"             \
    "por       %%mm3,     %%mm1\n\t"             \
    "movq      %%mm0,     %%mm2\n\t"             \
    "punpcklbw %%mm1,     %%mm0\n\t"             \
    "punpckhbw %%mm1,     %%mm2\n\t"             \
    MOVNTQ "   %%mm0,      (%1)\n\t"             \
    MOVNTQ "   %%mm2,     8(%1)\n\t"             \

#define DITHER_RGB                               \
    "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
    "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
    "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \

#if !COMPILE_TEMPLATE_MMXEXT
static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(2)

#ifdef DITHER1XBPP
        c->blueDither  = ff_dither8[y       & 1];
        c->greenDither = ff_dither8[y       & 1];
        c->redDither   = ff_dither8[(y + 1) & 1];
#endif

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
#ifdef DITHER1XBPP
        DITHER_RGB
#endif
        RGB_PACK16(pb_03, 1)

    YUV2RGB_ENDLOOP(2)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(2)

#ifdef DITHER1XBPP
        c->blueDither  = ff_dither8[y       & 1];
        c->greenDither = ff_dither4[y       & 1];
        c->redDither   = ff_dither8[(y + 1) & 1];
#endif

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
#ifdef DITHER1XBPP
        DITHER_RGB
#endif
        RGB_PACK16(pb_07, 0)

    YUV2RGB_ENDLOOP(2)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}
#endif /* !COMPILE_TEMPLATE_MMXEXT */

#define RGB_PACK24(blue, red)\
    "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
    "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
    "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
    "movq      %%mm"red",  %%mm3 \n"\
    "movq      %%mm"blue", %%mm6 \n"\
    "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
    "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
    "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
    "movq      %%mm3,      %%mm5 \n"\
    "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
    "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
    "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
    RGB_PACK24_B

#if COMPILE_TEMPLATE_MMXEXT
DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
#undef RGB_PACK24_B
#define RGB_PACK24_B\
    "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
    "pshufw    $0x84,  %%mm3, %%mm6 \n"\
    "pshufw    $0x38,  %%mm5, %%mm7 \n"\
    "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
    "movq      %%mm1,         %%mm0 \n"\
    "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
    "movq      %%mm1,         %%mm2 \n"\
    "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
    "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
    "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
    "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
    "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
    "por       %%mm3,         %%mm1 \n"\
    "por       %%mm6,         %%mm0 \n"\
    "por       %%mm5,         %%mm1 \n"\
    "por       %%mm7,         %%mm2 \n"\
    MOVNTQ"    %%mm0,          (%1) \n"\
    MOVNTQ"    %%mm1,         8(%1) \n"\
    MOVNTQ"    %%mm2,        16(%1) \n"\

#else
#undef RGB_PACK24_B
#define RGB_PACK24_B\
    "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
    "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
    "psrlq     $32,        %%mm3 \n"\
    "psrlq     $16,        %%mm2 \n"\
    "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
    "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
    "psrlq     $16,        %%mm2 \n"\
    "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
    "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
    "psrlq     $32,        %%mm5 \n"\
    "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
    "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\

#endif

static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(3)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK24(REG_BLUE, REG_RED)

    YUV2RGB_ENDLOOP(3)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(3)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK24(REG_RED, REG_BLUE)

    YUV2RGB_ENDLOOP(3)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}


#define SET_EMPTY_ALPHA                                                      \
    "pcmpeqd   %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \

#define LOAD_ALPHA                                   \
    "movq      (%6, %0, 2),     %%mm"REG_ALPHA"\n\t" \

#define RGB_PACK32(red, green, blue, alpha)  \
    "movq      %%mm"blue",  %%mm5\n\t"       \
    "movq      %%mm"red",   %%mm6\n\t"       \
    "punpckhbw %%mm"green", %%mm5\n\t"       \
    "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
    "punpckhbw %%mm"alpha", %%mm6\n\t"       \
    "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
    "movq      %%mm"blue",  %%mm"green"\n\t" \
    "movq      %%mm5,       %%mm"alpha"\n\t" \
    "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
    "punpckhwd %%mm"red",   %%mm"green"\n\t" \
    "punpcklwd %%mm6,       %%mm5\n\t"       \
    "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
    MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
    MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
    MOVNTQ "   %%mm5,       16(%1)\n\t"      \
    MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \

#if !COMPILE_TEMPLATE_MMXEXT
static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(4)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
        SET_EMPTY_ALPHA
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

#if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
                                        int srcStride[],
                                        int srcSliceY, int srcSliceH,
                                        uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(4)

        const uint8_t *pa = src[3] + y * srcStride[3];
        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
        LOAD_ALPHA
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS_ALPHA
    YUV2RGB_ENDFUNC
}
#endif

static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(4)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
        SET_EMPTY_ALPHA
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

#if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
                                        int srcStride[],
                                        int srcSliceY, int srcSliceH,
                                        uint8_t *dst[], int dstStride[])
{
    int y, h_size;

    YUV2RGB_LOOP(4)

        const uint8_t *pa = src[3] + y * srcStride[3];
        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK_INTERLEAVE
        LOAD_ALPHA
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS_ALPHA
    YUV2RGB_ENDFUNC
}
#endif

#endif /* !COMPILE_TEMPLATE_MMXEXT */