Statistics
| Branch: | Revision:

ffmpeg / libswscale / x86 / yuv2rgb_template2.c @ bffc36e0

History | View | Annotate | Download (16.5 KB)

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 HAVE_AMD3DNOW
29
/* On K6 femms is faster than emms. On K7 femms is directly mapped to emms. */
30
#define EMMS   "femms"
31
#else
32
#define EMMS   "emms"
33
#endif
34

    
35
#if HAVE_MMX2
36
#define MOVNTQ "movntq"
37
#define SFENCE "sfence"
38
#else
39
#define MOVNTQ "movq"
40
#define SFENCE " # nop"
41
#endif
42

    
43
#define REG_BLUE  "0"
44
#define REG_RED   "1"
45
#define REG_GREEN "2"
46
#define REG_ALPHA "3"
47

    
48
#define YUV2RGB_LOOP(depth)                                          \
49
    h_size = (c->dstW + 7) & ~7;                                     \
50
    if (h_size * depth > FFABS(dstStride[0]))                        \
51
        h_size -= 8;                                                 \
52
                                                                     \
53
    if (c->srcFormat == PIX_FMT_YUV422P) {                           \
54
        srcStride[1] *= 2;                                           \
55
        srcStride[2] *= 2;                                           \
56
    }                                                                \
57
                                                                     \
58
    __asm__ volatile ("pxor %mm4, %mm4\n\t");                        \
59
    for (y = 0; y < srcSliceH; y++) {                                \
60
        uint8_t *image    = dst[0] + (y + srcSliceY) * dstStride[0]; \
61
        const uint8_t *py = src[0] +               y * srcStride[0]; \
62
        const uint8_t *pu = src[1] +        (y >> 1) * srcStride[1]; \
63
        const uint8_t *pv = src[2] +        (y >> 1) * srcStride[2]; \
64
        x86_reg index = -h_size / 2;                                 \
65

    
66
#define YUV2RGB_INITIAL_LOAD          \
67
    __asm__ volatile (                \
68
        "movq (%5, %0, 2), %%mm6\n\t" \
69
        "movd    (%2, %0), %%mm0\n\t" \
70
        "movd    (%3, %0), %%mm1\n\t" \
71
        "1: \n\t"                     \
72

    
73
/* YUV2RGB core
74
 * Conversion is performed in usual way:
75
 * R = Y' * Ycoef + Vred * V'
76
 * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
77
 * B = Y' * Ycoef               + Ublue * U'
78
 *
79
 * where X' = X * 8 - Xoffset (multiplication is performed to increase
80
 * precision a bit).
81
 * Since it operates in YUV420 colorspace, Y component is additionally
82
 * split into Y1 and Y2 for even and odd pixels.
83
 *
84
 * Input:
85
 * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
86
 * Output:
87
 * mm1 - R, mm2 - G, mm0 - B
88
 */
89
#define YUV2RGB                                  \
90
    /* convert Y, U, V into Y1', Y2', U', V' */  \
91
    "movq      %%mm6, %%mm7\n\t"                 \
92
    "punpcklbw %%mm4, %%mm0\n\t"                 \
93
    "punpcklbw %%mm4, %%mm1\n\t"                 \
94
    "pand     "MANGLE(mmx_00ffw)", %%mm6\n\t"    \
95
    "psrlw     $8,    %%mm7\n\t"                 \
96
    "psllw     $3,    %%mm0\n\t"                 \
97
    "psllw     $3,    %%mm1\n\t"                 \
98
    "psllw     $3,    %%mm6\n\t"                 \
99
    "psllw     $3,    %%mm7\n\t"                 \
100
    "psubsw   "U_OFFSET"(%4), %%mm0\n\t"         \
101
    "psubsw   "V_OFFSET"(%4), %%mm1\n\t"         \
102
    "psubw    "Y_OFFSET"(%4), %%mm6\n\t"         \
103
    "psubw    "Y_OFFSET"(%4), %%mm7\n\t"         \
104
\
105
     /* multiply by coefficients */              \
106
    "movq      %%mm0, %%mm2\n\t"                 \
107
    "movq      %%mm1, %%mm3\n\t"                 \
108
    "pmulhw   "UG_COEFF"(%4), %%mm2\n\t"         \
109
    "pmulhw   "VG_COEFF"(%4), %%mm3\n\t"         \
110
    "pmulhw   "Y_COEFF" (%4), %%mm6\n\t"         \
111
    "pmulhw   "Y_COEFF" (%4), %%mm7\n\t"         \
112
    "pmulhw   "UB_COEFF"(%4), %%mm0\n\t"         \
113
    "pmulhw   "VR_COEFF"(%4), %%mm1\n\t"         \
114
    "paddsw    %%mm3, %%mm2\n\t"                 \
115
    /* now: mm0 = UB, mm1 = VR, mm2 = CG */      \
116
    /*      mm6 = Y1, mm7 = Y2 */                \
117
\
118
    /* produce RGB */                            \
119
    "movq      %%mm7, %%mm3\n\t"                 \
120
    "movq      %%mm7, %%mm5\n\t"                 \
121
    "paddsw    %%mm0, %%mm3\n\t"                 \
122
    "paddsw    %%mm1, %%mm5\n\t"                 \
123
    "paddsw    %%mm2, %%mm7\n\t"                 \
124
    "paddsw    %%mm6, %%mm0\n\t"                 \
125
    "paddsw    %%mm6, %%mm1\n\t"                 \
126
    "paddsw    %%mm6, %%mm2\n\t"                 \
127

    
128
#define RGB_PACK_INTERLEAVE                  \
129
    /* pack and interleave even/odd pixels */    \
130
    "packuswb  %%mm1, %%mm0\n\t"                 \
131
    "packuswb  %%mm5, %%mm3\n\t"                 \
132
    "packuswb  %%mm2, %%mm2\n\t"                 \
133
    "movq      %%mm0, %%mm1\n\n"                 \
134
    "packuswb  %%mm7, %%mm7\n\t"                 \
135
    "punpcklbw %%mm3, %%mm0\n\t"                 \
136
    "punpckhbw %%mm3, %%mm1\n\t"                 \
137
    "punpcklbw %%mm7, %%mm2\n\t"                 \
138

    
139
#define YUV2RGB_ENDLOOP(depth)                   \
140
    "movq 8 (%5, %0, 2), %%mm6\n\t"              \
141
    "movd 4 (%3, %0),    %%mm1\n\t"              \
142
    "movd 4 (%2, %0),    %%mm0\n\t"              \
143
    "add $"AV_STRINGIFY(depth * 8)", %1\n\t"     \
144
    "add  $4, %0\n\t"                            \
145
    "js   1b\n\t"                                \
146

    
147
#define YUV2RGB_OPERANDS                                          \
148
        : "+r" (index), "+r" (image)                              \
149
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
150
          "r" (py - 2*index)                                      \
151
        );                                                        \
152
    }                                                             \
153

    
154
#define YUV2RGB_OPERANDS_ALPHA                                    \
155
        : "+r" (index), "+r" (image)                              \
156
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
157
          "r" (py - 2*index), "r" (pa - 2*index)                  \
158
        );                                                        \
159
    }                                                             \
160

    
161
#define YUV2RGB_ENDFUNC                          \
162
    __asm__ volatile (SFENCE"\n\t"EMMS);         \
163
    return srcSliceH;                            \
164

    
165

    
166
#define RGB_PACK16(gmask, gshift, rshift)        \
167
    "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
168
    "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
169
    "psrlw     $3,        %%mm0\n\t"             \
170
    "pand      "MANGLE(gmask)",       %%mm2\n\t" \
171
    "movq      %%mm0,     %%mm5\n\t"             \
172
    "movq      %%mm1,     %%mm6\n\t"             \
173
    "movq      %%mm2,     %%mm7\n\t"             \
174
    "punpcklbw %%mm4,     %%mm0\n\t"             \
175
    "punpcklbw %%mm4,     %%mm1\n\t"             \
176
    "punpcklbw %%mm4,     %%mm2\n\t"             \
177
    "punpckhbw %%mm4,     %%mm5\n\t"             \
178
    "punpckhbw %%mm4,     %%mm6\n\t"             \
179
    "punpckhbw %%mm4,     %%mm7\n\t"             \
180
    "psllw     $"rshift", %%mm1\n\t"             \
181
    "psllw     $"rshift", %%mm6\n\t"             \
182
    "psllw     $"gshift", %%mm2\n\t"             \
183
    "psllw     $"gshift", %%mm7\n\t"             \
184
    "por       %%mm1,     %%mm0\n\t"             \
185
    "por       %%mm6,     %%mm5\n\t"             \
186
    "por       %%mm2,     %%mm0\n\t"             \
187
    "por       %%mm7,     %%mm5\n\t"             \
188
    MOVNTQ "   %%mm0,      (%1)\n\t"             \
189
    MOVNTQ "   %%mm5,     8(%1)\n\t"             \
190

    
191
#define DITHER_RGB                               \
192
    "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
193
    "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
194
    "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \
195

    
196
static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
197
                                       int srcStride[],
198
                                       int srcSliceY, int srcSliceH,
199
                                       uint8_t *dst[], int dstStride[])
200
{
201
    int y, h_size;
202

    
203
    YUV2RGB_LOOP(2)
204

    
205
#ifdef DITHER1XBPP
206
        c->blueDither  = ff_dither8[y       & 1];
207
        c->greenDither = ff_dither8[y       & 1];
208
        c->redDither   = ff_dither8[(y + 1) & 1];
209
#endif
210

    
211
        YUV2RGB_INITIAL_LOAD
212
        YUV2RGB
213
        RGB_PACK_INTERLEAVE
214
#ifdef DITHER1XBPP
215
        DITHER_RGB
216
#endif
217
        RGB_PACK16(mmx_redmask, "2", "7")
218

    
219
    YUV2RGB_ENDLOOP(2)
220
    YUV2RGB_OPERANDS
221
    YUV2RGB_ENDFUNC
222
}
223

    
224
static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
225
                                       int srcStride[],
226
                                       int srcSliceY, int srcSliceH,
227
                                       uint8_t *dst[], int dstStride[])
228
{
229
    int y, h_size;
230

    
231
    YUV2RGB_LOOP(2)
232

    
233
#ifdef DITHER1XBPP
234
        c->blueDither  = ff_dither8[y       & 1];
235
        c->greenDither = ff_dither4[y       & 1];
236
        c->redDither   = ff_dither8[(y + 1) & 1];
237
#endif
238

    
239
        YUV2RGB_INITIAL_LOAD
240
        YUV2RGB
241
        RGB_PACK_INTERLEAVE
242
#ifdef DITHER1XBPP
243
        DITHER_RGB
244
#endif
245
        RGB_PACK16(mmx_grnmask, "3", "8")
246

    
247
    YUV2RGB_ENDLOOP(2)
248
    YUV2RGB_OPERANDS
249
    YUV2RGB_ENDFUNC
250
}
251

    
252
#define RGB_PACK24(blue, red)\
253
    "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
254
    "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
255
    "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
256
    "movq      %%mm"red",  %%mm3 \n"\
257
    "movq      %%mm"blue", %%mm6 \n"\
258
    "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
259
    "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
260
    "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
261
    "movq      %%mm3,      %%mm5 \n"\
262
    "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
263
    "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
264
    "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
265
    RGB_PACK24_B
266

    
267
#if HAVE_MMX2
268
DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
269
DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
270
DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
271
DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
272
DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
273
#undef RGB_PACK24_B
274
#define RGB_PACK24_B\
275
    "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
276
    "pshufw    $0x84,  %%mm3, %%mm6 \n"\
277
    "pshufw    $0x38,  %%mm5, %%mm7 \n"\
278
    "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
279
    "movq      %%mm1,         %%mm0 \n"\
280
    "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
281
    "movq      %%mm1,         %%mm2 \n"\
282
    "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
283
    "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
284
    "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
285
    "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
286
    "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
287
    "por       %%mm3,         %%mm1 \n"\
288
    "por       %%mm6,         %%mm0 \n"\
289
    "por       %%mm5,         %%mm1 \n"\
290
    "por       %%mm7,         %%mm2 \n"\
291
    MOVNTQ"    %%mm0,          (%1) \n"\
292
    MOVNTQ"    %%mm1,         8(%1) \n"\
293
    MOVNTQ"    %%mm2,        16(%1) \n"\
294

    
295
#else
296
#undef RGB_PACK24_B
297
#define RGB_PACK24_B\
298
    "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
299
    "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
300
    "psrlq     $32,        %%mm3 \n"\
301
    "psrlq     $16,        %%mm2 \n"\
302
    "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
303
    "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
304
    "psrlq     $16,        %%mm2 \n"\
305
    "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
306
    "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
307
    "psrlq     $32,        %%mm5 \n"\
308
    "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
309
    "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\
310

    
311
#endif
312

    
313
static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
314
                                       int srcStride[],
315
                                       int srcSliceY, int srcSliceH,
316
                                       uint8_t *dst[], int dstStride[])
317
{
318
    int y, h_size;
319

    
320
    YUV2RGB_LOOP(3)
321

    
322
        YUV2RGB_INITIAL_LOAD
323
        YUV2RGB
324
        RGB_PACK24(REG_BLUE, REG_RED)
325

    
326
    YUV2RGB_ENDLOOP(3)
327
    YUV2RGB_OPERANDS
328
    YUV2RGB_ENDFUNC
329
}
330

    
331
static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
332
                                       int srcStride[],
333
                                       int srcSliceY, int srcSliceH,
334
                                       uint8_t *dst[], int dstStride[])
335
{
336
    int y, h_size;
337

    
338
    YUV2RGB_LOOP(3)
339

    
340
        YUV2RGB_INITIAL_LOAD
341
        YUV2RGB
342
        RGB_PACK24(REG_RED, REG_BLUE)
343

    
344
    YUV2RGB_ENDLOOP(3)
345
    YUV2RGB_OPERANDS
346
    YUV2RGB_ENDFUNC
347
}
348

    
349

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

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

    
356
#define RGB_PACK32(red, green, blue, alpha)  \
357
    "movq      %%mm"blue",  %%mm5\n\t"       \
358
    "movq      %%mm"red",   %%mm6\n\t"       \
359
    "punpckhbw %%mm"green", %%mm5\n\t"       \
360
    "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
361
    "punpckhbw %%mm"alpha", %%mm6\n\t"       \
362
    "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
363
    "movq      %%mm"blue",  %%mm"green"\n\t" \
364
    "movq      %%mm5,       %%mm"alpha"\n\t" \
365
    "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
366
    "punpckhwd %%mm"red",   %%mm"green"\n\t" \
367
    "punpcklwd %%mm6,       %%mm5\n\t"       \
368
    "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
369
    MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
370
    MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
371
    MOVNTQ "   %%mm5,       16(%1)\n\t"      \
372
    MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \
373

    
374
static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
375
                                       int srcStride[],
376
                                       int srcSliceY, int srcSliceH,
377
                                       uint8_t *dst[], int dstStride[])
378
{
379
    int y, h_size;
380

    
381
    YUV2RGB_LOOP(4)
382

    
383
        YUV2RGB_INITIAL_LOAD
384
        YUV2RGB
385
        RGB_PACK_INTERLEAVE
386
        SET_EMPTY_ALPHA
387
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
388

    
389
    YUV2RGB_ENDLOOP(4)
390
    YUV2RGB_OPERANDS
391
    YUV2RGB_ENDFUNC
392
}
393

    
394
static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
395
                                        int srcStride[],
396
                                        int srcSliceY, int srcSliceH,
397
                                        uint8_t *dst[], int dstStride[])
398
{
399
#if HAVE_7REGS
400
    int y, h_size;
401

    
402
    YUV2RGB_LOOP(4)
403

    
404
        const uint8_t *pa = src[3] + y * srcStride[3];
405
        YUV2RGB_INITIAL_LOAD
406
        YUV2RGB
407
        RGB_PACK_INTERLEAVE
408
        LOAD_ALPHA
409
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
410

    
411
    YUV2RGB_ENDLOOP(4)
412
    YUV2RGB_OPERANDS_ALPHA
413
    YUV2RGB_ENDFUNC
414
#endif
415
}
416

    
417
static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
418
                                       int srcStride[],
419
                                       int srcSliceY, int srcSliceH,
420
                                       uint8_t *dst[], int dstStride[])
421
{
422
    int y, h_size;
423

    
424
    YUV2RGB_LOOP(4)
425

    
426
        YUV2RGB_INITIAL_LOAD
427
        YUV2RGB
428
        RGB_PACK_INTERLEAVE
429
        SET_EMPTY_ALPHA
430
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
431

    
432
    YUV2RGB_ENDLOOP(4)
433
    YUV2RGB_OPERANDS
434
    YUV2RGB_ENDFUNC
435
}
436

    
437
static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
438
                                        int srcStride[],
439
                                        int srcSliceY, int srcSliceH,
440
                                        uint8_t *dst[], int dstStride[])
441
{
442
#if HAVE_7REGS
443
    int y, h_size;
444

    
445
    YUV2RGB_LOOP(4)
446

    
447
        const uint8_t *pa = src[3] + y * srcStride[3];
448
        YUV2RGB_INITIAL_LOAD
449
        YUV2RGB
450
        RGB_PACK_INTERLEAVE
451
        LOAD_ALPHA
452
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
453

    
454
    YUV2RGB_ENDLOOP(4)
455
    YUV2RGB_OPERANDS_ALPHA
456
    YUV2RGB_ENDFUNC
457
#endif
458
}