Libav
|
00001 /* 00002 * software YUV to RGB converter 00003 * 00004 * Copyright (C) 2001-2007 Michael Niedermayer 00005 * (c) 2010 Konstantin Shishkov 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #undef MOVNTQ 00025 #undef EMMS 00026 #undef SFENCE 00027 00028 #if HAVE_AMD3DNOW 00029 /* On K6 femms is faster than emms. On K7 femms is directly mapped to emms. */ 00030 #define EMMS "femms" 00031 #else 00032 #define EMMS "emms" 00033 #endif 00034 00035 #if HAVE_MMX2 00036 #define MOVNTQ "movntq" 00037 #define SFENCE "sfence" 00038 #else 00039 #define MOVNTQ "movq" 00040 #define SFENCE " # nop" 00041 #endif 00042 00043 #define REG_BLUE "0" 00044 #define REG_RED "1" 00045 #define REG_GREEN "2" 00046 #define REG_ALPHA "3" 00047 00048 #define YUV2RGB_LOOP(depth) \ 00049 h_size = (c->dstW + 7) & ~7; \ 00050 if (h_size * depth > FFABS(dstStride[0])) \ 00051 h_size -= 8; \ 00052 \ 00053 if (c->srcFormat == PIX_FMT_YUV422P) { \ 00054 srcStride[1] *= 2; \ 00055 srcStride[2] *= 2; \ 00056 } \ 00057 \ 00058 __asm__ volatile ("pxor %mm4, %mm4\n\t"); \ 00059 for (y = 0; y < srcSliceH; y++) { \ 00060 uint8_t *image = dst[0] + (y + srcSliceY) * dstStride[0]; \ 00061 const uint8_t *py = src[0] + y * srcStride[0]; \ 00062 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \ 00063 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \ 00064 x86_reg index = -h_size / 2; \ 00065 00066 #define YUV2RGB_INITIAL_LOAD \ 00067 __asm__ volatile ( \ 00068 "movq (%5, %0, 2), %%mm6\n\t" \ 00069 "movd (%2, %0), %%mm0\n\t" \ 00070 "movd (%3, %0), %%mm1\n\t" \ 00071 "1: \n\t" \ 00072 00073 /* YUV2RGB core 00074 * Conversion is performed in usual way: 00075 * R = Y' * Ycoef + Vred * V' 00076 * G = Y' * Ycoef + Vgreen * V' + Ugreen * U' 00077 * B = Y' * Ycoef + Ublue * U' 00078 * 00079 * where X' = X * 8 - Xoffset (multiplication is performed to increase 00080 * precision a bit). 00081 * Since it operates in YUV420 colorspace, Y component is additionally 00082 * split into Y1 and Y2 for even and odd pixels. 00083 * 00084 * Input: 00085 * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register 00086 * Output: 00087 * mm1 - R, mm2 - G, mm0 - B 00088 */ 00089 #define YUV2RGB \ 00090 /* convert Y, U, V into Y1', Y2', U', V' */ \ 00091 "movq %%mm6, %%mm7\n\t" \ 00092 "punpcklbw %%mm4, %%mm0\n\t" \ 00093 "punpcklbw %%mm4, %%mm1\n\t" \ 00094 "pand "MANGLE(mmx_00ffw)", %%mm6\n\t" \ 00095 "psrlw $8, %%mm7\n\t" \ 00096 "psllw $3, %%mm0\n\t" \ 00097 "psllw $3, %%mm1\n\t" \ 00098 "psllw $3, %%mm6\n\t" \ 00099 "psllw $3, %%mm7\n\t" \ 00100 "psubsw "U_OFFSET"(%4), %%mm0\n\t" \ 00101 "psubsw "V_OFFSET"(%4), %%mm1\n\t" \ 00102 "psubw "Y_OFFSET"(%4), %%mm6\n\t" \ 00103 "psubw "Y_OFFSET"(%4), %%mm7\n\t" \ 00104 \ 00105 /* multiply by coefficients */ \ 00106 "movq %%mm0, %%mm2\n\t" \ 00107 "movq %%mm1, %%mm3\n\t" \ 00108 "pmulhw "UG_COEFF"(%4), %%mm2\n\t" \ 00109 "pmulhw "VG_COEFF"(%4), %%mm3\n\t" \ 00110 "pmulhw "Y_COEFF" (%4), %%mm6\n\t" \ 00111 "pmulhw "Y_COEFF" (%4), %%mm7\n\t" \ 00112 "pmulhw "UB_COEFF"(%4), %%mm0\n\t" \ 00113 "pmulhw "VR_COEFF"(%4), %%mm1\n\t" \ 00114 "paddsw %%mm3, %%mm2\n\t" \ 00115 /* now: mm0 = UB, mm1 = VR, mm2 = CG */ \ 00116 /* mm6 = Y1, mm7 = Y2 */ \ 00117 \ 00118 /* produce RGB */ \ 00119 "movq %%mm7, %%mm3\n\t" \ 00120 "movq %%mm7, %%mm5\n\t" \ 00121 "paddsw %%mm0, %%mm3\n\t" \ 00122 "paddsw %%mm1, %%mm5\n\t" \ 00123 "paddsw %%mm2, %%mm7\n\t" \ 00124 "paddsw %%mm6, %%mm0\n\t" \ 00125 "paddsw %%mm6, %%mm1\n\t" \ 00126 "paddsw %%mm6, %%mm2\n\t" \ 00127 \ 00128 /* pack and interleave even/odd pixels */ \ 00129 "packuswb %%mm0, %%mm0\n\t" \ 00130 "packuswb %%mm1, %%mm1\n\t" \ 00131 "packuswb %%mm2, %%mm2\n\t" \ 00132 "packuswb %%mm3, %%mm3\n\t" \ 00133 "packuswb %%mm5, %%mm5\n\t" \ 00134 "packuswb %%mm7, %%mm7\n\t" \ 00135 "punpcklbw %%mm3, %%mm0\n\t" \ 00136 "punpcklbw %%mm5, %%mm1\n\t" \ 00137 "punpcklbw %%mm7, %%mm2\n\t" \ 00138 00139 #define YUV2RGB_ENDLOOP(depth) \ 00140 "movq 8 (%5, %0, 2), %%mm6\n\t" \ 00141 "movd 4 (%3, %0), %%mm1\n\t" \ 00142 "movd 4 (%2, %0), %%mm0\n\t" \ 00143 "add $"AV_STRINGIFY(depth * 8)", %1\n\t" \ 00144 "add $4, %0\n\t" \ 00145 "js 1b\n\t" \ 00146 00147 #define YUV2RGB_OPERANDS \ 00148 : "+r" (index), "+r" (image) \ 00149 : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \ 00150 "r" (py - 2*index) \ 00151 ); \ 00152 } \ 00153 00154 #define YUV2RGB_OPERANDS_ALPHA \ 00155 : "+r" (index), "+r" (image) \ 00156 : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \ 00157 "r" (py - 2*index), "r" (pa - 2*index) \ 00158 ); \ 00159 } \ 00160 00161 #define YUV2RGB_ENDFUNC \ 00162 __asm__ volatile (SFENCE"\n\t"EMMS); \ 00163 return srcSliceH; \ 00164 00165 00166 #define RGB_PACK16(gmask, gshift, rshift) \ 00167 "pand "MANGLE(mmx_redmask)", %%mm0\n\t" \ 00168 "pand "MANGLE(mmx_redmask)", %%mm1\n\t" \ 00169 "psrlw $3, %%mm0\n\t" \ 00170 "pand "MANGLE(gmask)", %%mm2\n\t" \ 00171 "movq %%mm0, %%mm5\n\t" \ 00172 "movq %%mm1, %%mm6\n\t" \ 00173 "movq %%mm2, %%mm7\n\t" \ 00174 "punpcklbw %%mm4, %%mm0\n\t" \ 00175 "punpcklbw %%mm4, %%mm1\n\t" \ 00176 "punpcklbw %%mm4, %%mm2\n\t" \ 00177 "punpckhbw %%mm4, %%mm5\n\t" \ 00178 "punpckhbw %%mm4, %%mm6\n\t" \ 00179 "punpckhbw %%mm4, %%mm7\n\t" \ 00180 "psllw $"rshift", %%mm1\n\t" \ 00181 "psllw $"rshift", %%mm6\n\t" \ 00182 "psllw $"gshift", %%mm2\n\t" \ 00183 "psllw $"gshift", %%mm7\n\t" \ 00184 "por %%mm1, %%mm0\n\t" \ 00185 "por %%mm6, %%mm5\n\t" \ 00186 "por %%mm2, %%mm0\n\t" \ 00187 "por %%mm7, %%mm5\n\t" \ 00188 MOVNTQ " %%mm0, (%1)\n\t" \ 00189 MOVNTQ " %%mm5, 8(%1)\n\t" \ 00190 00191 #define DITHER_RGB \ 00192 "paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \ 00193 "paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \ 00194 "paddusb "RED_DITHER"(%4), %%mm1\n\t" \ 00195 00196 static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[], 00197 int srcStride[], 00198 int srcSliceY, int srcSliceH, 00199 uint8_t *dst[], int dstStride[]) 00200 { 00201 int y, h_size; 00202 00203 YUV2RGB_LOOP(2) 00204 00205 #ifdef DITHER1XBPP 00206 c->blueDither = ff_dither8[y & 1]; 00207 c->greenDither = ff_dither8[y & 1]; 00208 c->redDither = ff_dither8[(y + 1) & 1]; 00209 #endif 00210 00211 YUV2RGB_INITIAL_LOAD 00212 YUV2RGB 00213 #ifdef DITHER1XBPP 00214 DITHER_RGB 00215 #endif 00216 RGB_PACK16(mmx_redmask, "2", "7") 00217 00218 YUV2RGB_ENDLOOP(2) 00219 YUV2RGB_OPERANDS 00220 YUV2RGB_ENDFUNC 00221 } 00222 00223 static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[], 00224 int srcStride[], 00225 int srcSliceY, int srcSliceH, 00226 uint8_t *dst[], int dstStride[]) 00227 { 00228 int y, h_size; 00229 00230 YUV2RGB_LOOP(2) 00231 00232 #ifdef DITHER1XBPP 00233 c->blueDither = ff_dither8[y & 1]; 00234 c->greenDither = ff_dither4[y & 1]; 00235 c->redDither = ff_dither8[(y + 1) & 1]; 00236 #endif 00237 00238 YUV2RGB_INITIAL_LOAD 00239 YUV2RGB 00240 #ifdef DITHER1XBPP 00241 DITHER_RGB 00242 #endif 00243 RGB_PACK16(mmx_grnmask, "3", "8") 00244 00245 YUV2RGB_ENDLOOP(2) 00246 YUV2RGB_OPERANDS 00247 YUV2RGB_ENDFUNC 00248 } 00249 00250 00251 #define RGB_PACK24(red, blue) \ 00252 /* generate first packed RGB octet */ \ 00253 "movq %%mm2, %%mm5\n\t" \ 00254 "movq %%mm"blue", %%mm6\n\t" \ 00255 "movq %%mm"red", %%mm7\n\t" \ 00256 "punpcklbw %%mm5, %%mm6\n\t" \ 00257 "punpcklbw %%mm4, %%mm7\n\t" \ 00258 "movq %%mm6, %%mm3\n\t" \ 00259 "punpcklwd %%mm7, %%mm6\n\t" \ 00260 "psrlq $32, %%mm3\n\t" \ 00261 "movq %%mm6, %%mm5\n\t" \ 00262 "psllq $40, %%mm6\n\t" \ 00263 "psllq $48, %%mm3\n\t" \ 00264 "psrlq $32, %%mm5\n\t" \ 00265 "psrlq $40, %%mm6\n\t" \ 00266 "psllq $24, %%mm5\n\t" \ 00267 "por %%mm3, %%mm6\n\t" \ 00268 "por %%mm5, %%mm6\n\t" \ 00269 MOVNTQ " %%mm6, (%1)\n\t" \ 00270 \ 00271 /* generate second packed RGB octet */ \ 00272 "movq %%mm"red", %%mm7\n\t" \ 00273 "movq %%mm2, %%mm5\n\t" \ 00274 "movq %%mm"blue", %%mm6\n\t" \ 00275 "punpcklbw %%mm4, %%mm7\n\t" \ 00276 "punpcklbw %%mm5, %%mm6\n\t" \ 00277 "movq %%mm7, %%mm3\n\t" \ 00278 "punpckhwd %%mm7, %%mm6\n\t" \ 00279 "psllq $16, %%mm3\n\t" \ 00280 "psrlq $32, %%mm6\n\t" \ 00281 "psrlq $48, %%mm3\n\t" \ 00282 "psllq $8, %%mm6\n\t" \ 00283 "movq %%mm"red", %%mm7\n\t" \ 00284 "por %%mm6, %%mm3\n\t" \ 00285 "movq %%mm"blue", %%mm6\n\t" \ 00286 "movq %%mm2, %%mm5\n\t" \ 00287 "punpckhbw %%mm4, %%mm7\n\t" \ 00288 "punpckhbw %%mm5, %%mm6\n\t" \ 00289 "movq %%mm6, %%mm5\n\t" \ 00290 "punpcklwd %%mm7, %%mm6\n\t" \ 00291 "psrlq $16, %%mm5\n\t" \ 00292 "psllq $56, %%mm5\n\t" \ 00293 "por %%mm5, %%mm3\n\t" \ 00294 "psllq $32, %%mm6\n\t" \ 00295 "por %%mm6, %%mm3\n\t" \ 00296 MOVNTQ " %%mm3, 8(%1)\n\t" \ 00297 \ 00298 /* generate third packed RGB octet */ \ 00299 "movq %%mm"red", %%mm7\n\t" \ 00300 "movq %%mm2, %%mm5\n\t" \ 00301 "movq %%mm2, %%mm3\n\t" \ 00302 "movq %%mm"blue", %%mm6\n\t" \ 00303 "punpckhbw %%mm"red", %%mm3\n\t" \ 00304 "punpckhbw %%mm4, %%mm7\n\t" \ 00305 "psllq $32, %%mm3\n\t" \ 00306 "punpckhbw %%mm5, %%mm6\n\t" \ 00307 "psrlq $48, %%mm3\n\t" \ 00308 "punpckhwd %%mm7, %%mm6\n\t" \ 00309 "movq %%mm6, %%mm7\n\t" \ 00310 "psrlq $32, %%mm6\n\t" \ 00311 "psllq $32, %%mm7\n\t" \ 00312 "psllq $40, %%mm6\n\t" \ 00313 "psrlq $16, %%mm7\n\t" \ 00314 "por %%mm6, %%mm3\n\t" \ 00315 "por %%mm7, %%mm3\n\t" \ 00316 MOVNTQ " %%mm3, 16(%1)\n\t" \ 00317 00318 static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[], 00319 int srcStride[], 00320 int srcSliceY, int srcSliceH, 00321 uint8_t *dst[], int dstStride[]) 00322 { 00323 int y, h_size; 00324 00325 YUV2RGB_LOOP(3) 00326 00327 YUV2RGB_INITIAL_LOAD 00328 YUV2RGB 00329 RGB_PACK24(REG_BLUE, REG_RED) 00330 00331 YUV2RGB_ENDLOOP(3) 00332 YUV2RGB_OPERANDS 00333 YUV2RGB_ENDFUNC 00334 } 00335 00336 static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[], 00337 int srcStride[], 00338 int srcSliceY, int srcSliceH, 00339 uint8_t *dst[], int dstStride[]) 00340 { 00341 int y, h_size; 00342 00343 YUV2RGB_LOOP(3) 00344 00345 YUV2RGB_INITIAL_LOAD 00346 YUV2RGB 00347 RGB_PACK24(REG_RED, REG_BLUE) 00348 00349 YUV2RGB_ENDLOOP(3) 00350 YUV2RGB_OPERANDS 00351 YUV2RGB_ENDFUNC 00352 } 00353 00354 00355 #define SET_EMPTY_ALPHA \ 00356 "pcmpeqd %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \ 00357 00358 #define LOAD_ALPHA \ 00359 "movq (%6, %0, 2), %%mm"REG_ALPHA"\n\t" \ 00360 00361 #define RGB_PACK32(red, green, blue, alpha) \ 00362 "movq %%mm"blue", %%mm5\n\t" \ 00363 "movq %%mm"red", %%mm6\n\t" \ 00364 "punpckhbw %%mm"green", %%mm5\n\t" \ 00365 "punpcklbw %%mm"green", %%mm"blue"\n\t" \ 00366 "punpckhbw %%mm"alpha", %%mm6\n\t" \ 00367 "punpcklbw %%mm"alpha", %%mm"red"\n\t" \ 00368 "movq %%mm"blue", %%mm"green"\n\t" \ 00369 "movq %%mm5, %%mm"alpha"\n\t" \ 00370 "punpcklwd %%mm"red", %%mm"blue"\n\t" \ 00371 "punpckhwd %%mm"red", %%mm"green"\n\t" \ 00372 "punpcklwd %%mm6, %%mm5\n\t" \ 00373 "punpckhwd %%mm6, %%mm"alpha"\n\t" \ 00374 MOVNTQ " %%mm"blue", 0(%1)\n\t" \ 00375 MOVNTQ " %%mm"green", 8(%1)\n\t" \ 00376 MOVNTQ " %%mm5, 16(%1)\n\t" \ 00377 MOVNTQ " %%mm"alpha", 24(%1)\n\t" \ 00378 00379 static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[], 00380 int srcStride[], 00381 int srcSliceY, int srcSliceH, 00382 uint8_t *dst[], int dstStride[]) 00383 { 00384 int y, h_size; 00385 00386 YUV2RGB_LOOP(4) 00387 00388 YUV2RGB_INITIAL_LOAD 00389 YUV2RGB 00390 SET_EMPTY_ALPHA 00391 RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA) 00392 00393 YUV2RGB_ENDLOOP(4) 00394 YUV2RGB_OPERANDS 00395 YUV2RGB_ENDFUNC 00396 } 00397 00398 static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[], 00399 int srcStride[], 00400 int srcSliceY, int srcSliceH, 00401 uint8_t *dst[], int dstStride[]) 00402 { 00403 #if HAVE_7REGS 00404 int y, h_size; 00405 00406 YUV2RGB_LOOP(4) 00407 00408 const uint8_t *pa = src[3] + y * srcStride[3]; 00409 YUV2RGB_INITIAL_LOAD 00410 YUV2RGB 00411 LOAD_ALPHA 00412 RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA) 00413 00414 YUV2RGB_ENDLOOP(4) 00415 YUV2RGB_OPERANDS_ALPHA 00416 YUV2RGB_ENDFUNC 00417 #endif 00418 } 00419 00420 static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[], 00421 int srcStride[], 00422 int srcSliceY, int srcSliceH, 00423 uint8_t *dst[], int dstStride[]) 00424 { 00425 int y, h_size; 00426 00427 YUV2RGB_LOOP(4) 00428 00429 YUV2RGB_INITIAL_LOAD 00430 YUV2RGB 00431 SET_EMPTY_ALPHA 00432 RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA) 00433 00434 YUV2RGB_ENDLOOP(4) 00435 YUV2RGB_OPERANDS 00436 YUV2RGB_ENDFUNC 00437 } 00438 00439 static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[], 00440 int srcStride[], 00441 int srcSliceY, int srcSliceH, 00442 uint8_t *dst[], int dstStride[]) 00443 { 00444 #if HAVE_7REGS 00445 int y, h_size; 00446 00447 YUV2RGB_LOOP(4) 00448 00449 const uint8_t *pa = src[3] + y * srcStride[3]; 00450 YUV2RGB_INITIAL_LOAD 00451 YUV2RGB 00452 LOAD_ALPHA 00453 RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA) 00454 00455 YUV2RGB_ENDLOOP(4) 00456 YUV2RGB_OPERANDS_ALPHA 00457 YUV2RGB_ENDFUNC 00458 #endif 00459 }