ffmpeg / libswscale / x86 / yuv2rgb_template2.c @ 8a26b2c6
History  View  Annotate  Download (16.2 KB)
1 
/*


2 
* software YUV to RGB converter

3 
*

4 
* Copyright (C) 20012007 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 021101301 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 
#define IF0(x)

166 
#define IF1(x) x

167  
168 
#define RGB_PACK16(gmask, is15) \

169 
"pand "MANGLE(mmx_redmask)", %%mm0\n\t" \ 
170 
"pand "MANGLE(mmx_redmask)", %%mm1\n\t" \ 
171 
"movq %%mm2, %%mm3\n\t" \

172 
"psllw $"AV_STRINGIFY(3is15)", %%mm2\n\t" \ 
173 
"psrlw $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \ 
174 
"psrlw $3, %%mm0\n\t" \

175 
IF##is15("psrlw $1, %%mm1\n\t") \ 
176 
"pand "MANGLE(pb_e0)", %%mm2\n\t" \ 
177 
"pand "MANGLE(gmask)", %%mm3\n\t" \ 
178 
"por %%mm2, %%mm0\n\t" \

179 
"por %%mm3, %%mm1\n\t" \

180 
"movq %%mm0, %%mm2\n\t" \

181 
"punpcklbw %%mm1, %%mm0\n\t" \

182 
"punpckhbw %%mm1, %%mm2\n\t" \

183 
MOVNTQ " %%mm0, (%1)\n\t" \

184 
MOVNTQ " %%mm2, 8(%1)\n\t" \

185  
186 
#define DITHER_RGB \

187 
"paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \ 
188 
"paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \ 
189 
"paddusb "RED_DITHER"(%4), %%mm1\n\t" \ 
190  
191 
static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[], 
192 
int srcStride[],

193 
int srcSliceY, int srcSliceH, 
194 
uint8_t *dst[], int dstStride[])

195 
{ 
196 
int y, h_size;

197  
198 
YUV2RGB_LOOP(2)

199  
200 
#ifdef DITHER1XBPP

201 
c>blueDither = ff_dither8[y & 1];

202 
c>greenDither = ff_dither8[y & 1];

203 
c>redDither = ff_dither8[(y + 1) & 1]; 
204 
#endif

205  
206 
YUV2RGB_INITIAL_LOAD 
207 
YUV2RGB 
208 
RGB_PACK_INTERLEAVE 
209 
#ifdef DITHER1XBPP

210 
DITHER_RGB 
211 
#endif

212 
RGB_PACK16(pb_03, 1)

213  
214 
YUV2RGB_ENDLOOP(2)

215 
YUV2RGB_OPERANDS 
216 
YUV2RGB_ENDFUNC 
217 
} 
218  
219 
static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[], 
220 
int srcStride[],

221 
int srcSliceY, int srcSliceH, 
222 
uint8_t *dst[], int dstStride[])

223 
{ 
224 
int y, h_size;

225  
226 
YUV2RGB_LOOP(2)

227  
228 
#ifdef DITHER1XBPP

229 
c>blueDither = ff_dither8[y & 1];

230 
c>greenDither = ff_dither4[y & 1];

231 
c>redDither = ff_dither8[(y + 1) & 1]; 
232 
#endif

233  
234 
YUV2RGB_INITIAL_LOAD 
235 
YUV2RGB 
236 
RGB_PACK_INTERLEAVE 
237 
#ifdef DITHER1XBPP

238 
DITHER_RGB 
239 
#endif

240 
RGB_PACK16(pb_07, 0)

241  
242 
YUV2RGB_ENDLOOP(2)

243 
YUV2RGB_OPERANDS 
244 
YUV2RGB_ENDFUNC 
245 
} 
246  
247 
#define RGB_PACK24(blue, red)\

248 
"packuswb %%mm3, %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\ 
249 
"packuswb %%mm5, %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\ 
250 
"packuswb %%mm7, %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\ 
251 
"movq %%mm"red", %%mm3 \n"\ 
252 
"movq %%mm"blue", %%mm6 \n"\ 
253 
"psrlq $32, %%mm"red" \n" /* R1 R3 R5 R7 */\ 
254 
"punpcklbw %%mm2, %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\ 
255 
"punpcklbw %%mm"red", %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\ 
256 
"movq %%mm3, %%mm5 \n"\

257 
"punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\ 
258 
"punpcklwd %%mm6, %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\ 
259 
"punpckhwd %%mm6, %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\ 
260 
RGB_PACK24_B 
261  
262 
#if HAVE_MMX2

263 
DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {1,1, 0,1}; 
264 
DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,1, 0}; 
265 
DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,1,1, 0}; 
266 
DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {1, 0, 0,1}; 
267 
DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,1, 0, 0}; 
268 
#undef RGB_PACK24_B

269 
#define RGB_PACK24_B\

270 
"pshufw $0xc6, %%mm2, %%mm1 \n"\

271 
"pshufw $0x84, %%mm3, %%mm6 \n"\

272 
"pshufw $0x38, %%mm5, %%mm7 \n"\

273 
"pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1   R2 G2 */\ 
274 
"movq %%mm1, %%mm0 \n"\

275 
"pand "MANGLE(mask0110)", %%mm7 \n" /*   R6 G6 B6 R7   */\ 
276 
"movq %%mm1, %%mm2 \n"\

277 
"pand "MANGLE(mask0100)", %%mm1 \n" /*   G3 B3     */\ 
278 
"psrlq $48, %%mm3 \n" /* B2 R3       */\ 
279 
"pand "MANGLE(mask0010)", %%mm0 \n" /*     G1 B1   */\ 
280 
"psllq $32, %%mm5 \n" /*     R4 G4 B4 R5 */\ 
281 
"pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5     G7 B7 */\ 
282 
"por %%mm3, %%mm1 \n"\

283 
"por %%mm6, %%mm0 \n"\

284 
"por %%mm5, %%mm1 \n"\

285 
"por %%mm7, %%mm2 \n"\

286 
MOVNTQ" %%mm0, (%1) \n"\

287 
MOVNTQ" %%mm1, 8(%1) \n"\

288 
MOVNTQ" %%mm2, 16(%1) \n"\

289  
290 
#else

291 
#undef RGB_PACK24_B

292 
#define RGB_PACK24_B\

293 
"movd %%mm3, (%1) \n" /* R0 G0 B0 R1 */\ 
294 
"movd %%mm2, 4(%1) \n" /* G1 B1 */\ 
295 
"psrlq $32, %%mm3 \n"\

296 
"psrlq $16, %%mm2 \n"\

297 
"movd %%mm3, 6(%1) \n" /* R2 G2 B2 R3 */\ 
298 
"movd %%mm2, 10(%1) \n" /* G3 B3 */\ 
299 
"psrlq $16, %%mm2 \n"\

300 
"movd %%mm5, 12(%1) \n" /* R4 G4 B4 R5 */\ 
301 
"movd %%mm2, 16(%1) \n" /* G5 B5 */\ 
302 
"psrlq $32, %%mm5 \n"\

303 
"movd %%mm2, 20(%1) \n" /*   G7 B7 */\ 
304 
"movd %%mm5, 18(%1) \n" /* R6 G6 B6 R7 */\ 
305  
306 
#endif

307  
308 
static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[], 
309 
int srcStride[],

310 
int srcSliceY, int srcSliceH, 
311 
uint8_t *dst[], int dstStride[])

312 
{ 
313 
int y, h_size;

314  
315 
YUV2RGB_LOOP(3)

316  
317 
YUV2RGB_INITIAL_LOAD 
318 
YUV2RGB 
319 
RGB_PACK24(REG_BLUE, REG_RED) 
320  
321 
YUV2RGB_ENDLOOP(3)

322 
YUV2RGB_OPERANDS 
323 
YUV2RGB_ENDFUNC 
324 
} 
325  
326 
static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[], 
327 
int srcStride[],

328 
int srcSliceY, int srcSliceH, 
329 
uint8_t *dst[], int dstStride[])

330 
{ 
331 
int y, h_size;

332  
333 
YUV2RGB_LOOP(3)

334  
335 
YUV2RGB_INITIAL_LOAD 
336 
YUV2RGB 
337 
RGB_PACK24(REG_RED, REG_BLUE) 
338  
339 
YUV2RGB_ENDLOOP(3)

340 
YUV2RGB_OPERANDS 
341 
YUV2RGB_ENDFUNC 
342 
} 
343  
344  
345 
#define SET_EMPTY_ALPHA \

346 
"pcmpeqd %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \ 
347  
348 
#define LOAD_ALPHA \

349 
"movq (%6, %0, 2), %%mm"REG_ALPHA"\n\t" \ 
350  
351 
#define RGB_PACK32(red, green, blue, alpha) \

352 
"movq %%mm"blue", %%mm5\n\t" \ 
353 
"movq %%mm"red", %%mm6\n\t" \ 
354 
"punpckhbw %%mm"green", %%mm5\n\t" \ 
355 
"punpcklbw %%mm"green", %%mm"blue"\n\t" \ 
356 
"punpckhbw %%mm"alpha", %%mm6\n\t" \ 
357 
"punpcklbw %%mm"alpha", %%mm"red"\n\t" \ 
358 
"movq %%mm"blue", %%mm"green"\n\t" \ 
359 
"movq %%mm5, %%mm"alpha"\n\t" \ 
360 
"punpcklwd %%mm"red", %%mm"blue"\n\t" \ 
361 
"punpckhwd %%mm"red", %%mm"green"\n\t" \ 
362 
"punpcklwd %%mm6, %%mm5\n\t" \

363 
"punpckhwd %%mm6, %%mm"alpha"\n\t" \ 
364 
MOVNTQ " %%mm"blue", 0(%1)\n\t" \ 
365 
MOVNTQ " %%mm"green", 8(%1)\n\t" \ 
366 
MOVNTQ " %%mm5, 16(%1)\n\t" \

367 
MOVNTQ " %%mm"alpha", 24(%1)\n\t" \ 
368  
369 
static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[], 
370 
int srcStride[],

371 
int srcSliceY, int srcSliceH, 
372 
uint8_t *dst[], int dstStride[])

373 
{ 
374 
int y, h_size;

375  
376 
YUV2RGB_LOOP(4)

377  
378 
YUV2RGB_INITIAL_LOAD 
379 
YUV2RGB 
380 
RGB_PACK_INTERLEAVE 
381 
SET_EMPTY_ALPHA 
382 
RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA) 
383  
384 
YUV2RGB_ENDLOOP(4)

385 
YUV2RGB_OPERANDS 
386 
YUV2RGB_ENDFUNC 
387 
} 
388  
389 
static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[], 
390 
int srcStride[],

391 
int srcSliceY, int srcSliceH, 
392 
uint8_t *dst[], int dstStride[])

393 
{ 
394 
#if HAVE_7REGS

395 
int y, h_size;

396  
397 
YUV2RGB_LOOP(4)

398  
399 
const uint8_t *pa = src[3] + y * srcStride[3]; 
400 
YUV2RGB_INITIAL_LOAD 
401 
YUV2RGB 
402 
RGB_PACK_INTERLEAVE 
403 
LOAD_ALPHA 
404 
RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA) 
405  
406 
YUV2RGB_ENDLOOP(4)

407 
YUV2RGB_OPERANDS_ALPHA 
408 
YUV2RGB_ENDFUNC 
409 
#endif

410 
} 
411  
412 
static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[], 
413 
int srcStride[],

414 
int srcSliceY, int srcSliceH, 
415 
uint8_t *dst[], int dstStride[])

416 
{ 
417 
int y, h_size;

418  
419 
YUV2RGB_LOOP(4)

420  
421 
YUV2RGB_INITIAL_LOAD 
422 
YUV2RGB 
423 
RGB_PACK_INTERLEAVE 
424 
SET_EMPTY_ALPHA 
425 
RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA) 
426  
427 
YUV2RGB_ENDLOOP(4)

428 
YUV2RGB_OPERANDS 
429 
YUV2RGB_ENDFUNC 
430 
} 
431  
432 
static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[], 
433 
int srcStride[],

434 
int srcSliceY, int srcSliceH, 
435 
uint8_t *dst[], int dstStride[])

436 
{ 
437 
#if HAVE_7REGS

438 
int y, h_size;

439  
440 
YUV2RGB_LOOP(4)

441  
442 
const uint8_t *pa = src[3] + y * srcStride[3]; 
443 
YUV2RGB_INITIAL_LOAD 
444 
YUV2RGB 
445 
RGB_PACK_INTERLEAVE 
446 
LOAD_ALPHA 
447 
RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA) 
448  
449 
YUV2RGB_ENDLOOP(4)

450 
YUV2RGB_OPERANDS_ALPHA 
451 
YUV2RGB_ENDFUNC 
452 
#endif

453 
} 