• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libswscale/x86/yuv2rgb_template.c

Go to the documentation of this file.
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 COMPILE_TEMPLATE_MMX2
00029 #define MOVNTQ "movntq"
00030 #define SFENCE "sfence"
00031 #else
00032 #define MOVNTQ "movq"
00033 #define SFENCE " # nop"
00034 #endif
00035 
00036 #define REG_BLUE  "0"
00037 #define REG_RED   "1"
00038 #define REG_GREEN "2"
00039 #define REG_ALPHA "3"
00040 
00041 #define YUV2RGB_LOOP(depth)                                          \
00042     h_size = (c->dstW + 7) & ~7;                                     \
00043     if (h_size * depth > FFABS(dstStride[0]))                        \
00044         h_size -= 8;                                                 \
00045                                                                      \
00046     vshift = c->srcFormat != PIX_FMT_YUV422P;                        \
00047                                                                      \
00048     __asm__ volatile ("pxor %mm4, %mm4\n\t");                        \
00049     for (y = 0; y < srcSliceH; y++) {                                \
00050         uint8_t *image    = dst[0] + (y + srcSliceY) * dstStride[0]; \
00051         const uint8_t *py = src[0] +               y * srcStride[0]; \
00052         const uint8_t *pu = src[1] +   (y >> vshift) * srcStride[1]; \
00053         const uint8_t *pv = src[2] +   (y >> vshift) * srcStride[2]; \
00054         x86_reg index = -h_size / 2;                                 \
00055 
00056 #define YUV2RGB_INITIAL_LOAD          \
00057     __asm__ volatile (                \
00058         "movq (%5, %0, 2), %%mm6\n\t" \
00059         "movd    (%2, %0), %%mm0\n\t" \
00060         "movd    (%3, %0), %%mm1\n\t" \
00061         "1: \n\t"                     \
00062 
00063 /* YUV2RGB core
00064  * Conversion is performed in usual way:
00065  * R = Y' * Ycoef + Vred * V'
00066  * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
00067  * B = Y' * Ycoef               + Ublue * U'
00068  *
00069  * where X' = X * 8 - Xoffset (multiplication is performed to increase
00070  * precision a bit).
00071  * Since it operates in YUV420 colorspace, Y component is additionally
00072  * split into Y1 and Y2 for even and odd pixels.
00073  *
00074  * Input:
00075  * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
00076  * Output:
00077  * mm1 - R, mm2 - G, mm0 - B
00078  */
00079 #define YUV2RGB                                  \
00080     /* convert Y, U, V into Y1', Y2', U', V' */  \
00081     "movq      %%mm6, %%mm7\n\t"                 \
00082     "punpcklbw %%mm4, %%mm0\n\t"                 \
00083     "punpcklbw %%mm4, %%mm1\n\t"                 \
00084     "pand     "MANGLE(mmx_00ffw)", %%mm6\n\t"    \
00085     "psrlw     $8,    %%mm7\n\t"                 \
00086     "psllw     $3,    %%mm0\n\t"                 \
00087     "psllw     $3,    %%mm1\n\t"                 \
00088     "psllw     $3,    %%mm6\n\t"                 \
00089     "psllw     $3,    %%mm7\n\t"                 \
00090     "psubsw   "U_OFFSET"(%4), %%mm0\n\t"         \
00091     "psubsw   "V_OFFSET"(%4), %%mm1\n\t"         \
00092     "psubw    "Y_OFFSET"(%4), %%mm6\n\t"         \
00093     "psubw    "Y_OFFSET"(%4), %%mm7\n\t"         \
00094 \
00095      /* multiply by coefficients */              \
00096     "movq      %%mm0, %%mm2\n\t"                 \
00097     "movq      %%mm1, %%mm3\n\t"                 \
00098     "pmulhw   "UG_COEFF"(%4), %%mm2\n\t"         \
00099     "pmulhw   "VG_COEFF"(%4), %%mm3\n\t"         \
00100     "pmulhw   "Y_COEFF" (%4), %%mm6\n\t"         \
00101     "pmulhw   "Y_COEFF" (%4), %%mm7\n\t"         \
00102     "pmulhw   "UB_COEFF"(%4), %%mm0\n\t"         \
00103     "pmulhw   "VR_COEFF"(%4), %%mm1\n\t"         \
00104     "paddsw    %%mm3, %%mm2\n\t"                 \
00105     /* now: mm0 = UB, mm1 = VR, mm2 = CG */      \
00106     /*      mm6 = Y1, mm7 = Y2 */                \
00107 \
00108     /* produce RGB */                            \
00109     "movq      %%mm7, %%mm3\n\t"                 \
00110     "movq      %%mm7, %%mm5\n\t"                 \
00111     "paddsw    %%mm0, %%mm3\n\t"                 \
00112     "paddsw    %%mm1, %%mm5\n\t"                 \
00113     "paddsw    %%mm2, %%mm7\n\t"                 \
00114     "paddsw    %%mm6, %%mm0\n\t"                 \
00115     "paddsw    %%mm6, %%mm1\n\t"                 \
00116     "paddsw    %%mm6, %%mm2\n\t"                 \
00117 
00118 #define RGB_PACK_INTERLEAVE                  \
00119     /* pack and interleave even/odd pixels */    \
00120     "packuswb  %%mm1, %%mm0\n\t"                 \
00121     "packuswb  %%mm5, %%mm3\n\t"                 \
00122     "packuswb  %%mm2, %%mm2\n\t"                 \
00123     "movq      %%mm0, %%mm1\n\n"                 \
00124     "packuswb  %%mm7, %%mm7\n\t"                 \
00125     "punpcklbw %%mm3, %%mm0\n\t"                 \
00126     "punpckhbw %%mm3, %%mm1\n\t"                 \
00127     "punpcklbw %%mm7, %%mm2\n\t"                 \
00128 
00129 #define YUV2RGB_ENDLOOP(depth)                   \
00130     "movq 8 (%5, %0, 2), %%mm6\n\t"              \
00131     "movd 4 (%3, %0),    %%mm1\n\t"              \
00132     "movd 4 (%2, %0),    %%mm0\n\t"              \
00133     "add $"AV_STRINGIFY(depth * 8)", %1\n\t"     \
00134     "add  $4, %0\n\t"                            \
00135     "js   1b\n\t"                                \
00136 
00137 #define YUV2RGB_OPERANDS                                          \
00138         : "+r" (index), "+r" (image)                              \
00139         : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
00140           "r" (py - 2*index)                                      \
00141         : "memory"                                                \
00142         );                                                        \
00143     }                                                             \
00144 
00145 #define YUV2RGB_OPERANDS_ALPHA                                    \
00146         : "+r" (index), "+r" (image)                              \
00147         : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
00148           "r" (py - 2*index), "r" (pa - 2*index)                  \
00149         : "memory"                                                \
00150         );                                                        \
00151     }                                                             \
00152 
00153 #define YUV2RGB_ENDFUNC                          \
00154     __asm__ volatile (SFENCE"\n\t"               \
00155                     "emms    \n\t");             \
00156     return srcSliceH;                            \
00157 
00158 #define IF0(x)
00159 #define IF1(x) x
00160 
00161 #define RGB_PACK16(gmask, is15)                  \
00162     "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
00163     "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
00164     "movq      %%mm2,     %%mm3\n\t"             \
00165     "psllw   $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
00166     "psrlw   $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
00167     "psrlw     $3,        %%mm0\n\t"             \
00168     IF##is15("psrlw  $1,  %%mm1\n\t")            \
00169     "pand "MANGLE(pb_e0)", %%mm2\n\t"            \
00170     "pand "MANGLE(gmask)", %%mm3\n\t"            \
00171     "por       %%mm2,     %%mm0\n\t"             \
00172     "por       %%mm3,     %%mm1\n\t"             \
00173     "movq      %%mm0,     %%mm2\n\t"             \
00174     "punpcklbw %%mm1,     %%mm0\n\t"             \
00175     "punpckhbw %%mm1,     %%mm2\n\t"             \
00176     MOVNTQ "   %%mm0,      (%1)\n\t"             \
00177     MOVNTQ "   %%mm2,     8(%1)\n\t"             \
00178 
00179 #define DITHER_RGB                               \
00180     "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
00181     "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
00182     "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \
00183 
00184 #if !COMPILE_TEMPLATE_MMX2
00185 static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
00186                                        int srcStride[],
00187                                        int srcSliceY, int srcSliceH,
00188                                        uint8_t *dst[], int dstStride[])
00189 {
00190     int y, h_size, vshift;
00191 
00192     YUV2RGB_LOOP(2)
00193 
00194 #ifdef DITHER1XBPP
00195         c->blueDither  = ff_dither8[y       & 1];
00196         c->greenDither = ff_dither8[y       & 1];
00197         c->redDither   = ff_dither8[(y + 1) & 1];
00198 #endif
00199 
00200         YUV2RGB_INITIAL_LOAD
00201         YUV2RGB
00202         RGB_PACK_INTERLEAVE
00203 #ifdef DITHER1XBPP
00204         DITHER_RGB
00205 #endif
00206         RGB_PACK16(pb_03, 1)
00207 
00208     YUV2RGB_ENDLOOP(2)
00209     YUV2RGB_OPERANDS
00210     YUV2RGB_ENDFUNC
00211 }
00212 
00213 static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
00214                                        int srcStride[],
00215                                        int srcSliceY, int srcSliceH,
00216                                        uint8_t *dst[], int dstStride[])
00217 {
00218     int y, h_size, vshift;
00219 
00220     YUV2RGB_LOOP(2)
00221 
00222 #ifdef DITHER1XBPP
00223         c->blueDither  = ff_dither8[y       & 1];
00224         c->greenDither = ff_dither4[y       & 1];
00225         c->redDither   = ff_dither8[(y + 1) & 1];
00226 #endif
00227 
00228         YUV2RGB_INITIAL_LOAD
00229         YUV2RGB
00230         RGB_PACK_INTERLEAVE
00231 #ifdef DITHER1XBPP
00232         DITHER_RGB
00233 #endif
00234         RGB_PACK16(pb_07, 0)
00235 
00236     YUV2RGB_ENDLOOP(2)
00237     YUV2RGB_OPERANDS
00238     YUV2RGB_ENDFUNC
00239 }
00240 #endif /* !COMPILE_TEMPLATE_MMX2 */
00241 
00242 #define RGB_PACK24(blue, red)\
00243     "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
00244     "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
00245     "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
00246     "movq      %%mm"red",  %%mm3 \n"\
00247     "movq      %%mm"blue", %%mm6 \n"\
00248     "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
00249     "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
00250     "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
00251     "movq      %%mm3,      %%mm5 \n"\
00252     "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
00253     "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
00254     "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
00255     RGB_PACK24_B
00256 
00257 #if COMPILE_TEMPLATE_MMX2
00258 DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
00259 DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
00260 DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
00261 DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
00262 DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
00263 #undef RGB_PACK24_B
00264 #define RGB_PACK24_B\
00265     "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
00266     "pshufw    $0x84,  %%mm3, %%mm6 \n"\
00267     "pshufw    $0x38,  %%mm5, %%mm7 \n"\
00268     "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
00269     "movq      %%mm1,         %%mm0 \n"\
00270     "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
00271     "movq      %%mm1,         %%mm2 \n"\
00272     "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
00273     "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
00274     "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
00275     "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
00276     "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
00277     "por       %%mm3,         %%mm1 \n"\
00278     "por       %%mm6,         %%mm0 \n"\
00279     "por       %%mm5,         %%mm1 \n"\
00280     "por       %%mm7,         %%mm2 \n"\
00281     MOVNTQ"    %%mm0,          (%1) \n"\
00282     MOVNTQ"    %%mm1,         8(%1) \n"\
00283     MOVNTQ"    %%mm2,        16(%1) \n"\
00284 
00285 #else
00286 #undef RGB_PACK24_B
00287 #define RGB_PACK24_B\
00288     "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
00289     "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
00290     "psrlq     $32,        %%mm3 \n"\
00291     "psrlq     $16,        %%mm2 \n"\
00292     "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
00293     "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
00294     "psrlq     $16,        %%mm2 \n"\
00295     "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
00296     "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
00297     "psrlq     $32,        %%mm5 \n"\
00298     "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
00299     "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\
00300 
00301 #endif
00302 
00303 static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
00304                                        int srcStride[],
00305                                        int srcSliceY, int srcSliceH,
00306                                        uint8_t *dst[], int dstStride[])
00307 {
00308     int y, h_size, vshift;
00309 
00310     YUV2RGB_LOOP(3)
00311 
00312         YUV2RGB_INITIAL_LOAD
00313         YUV2RGB
00314         RGB_PACK24(REG_BLUE, REG_RED)
00315 
00316     YUV2RGB_ENDLOOP(3)
00317     YUV2RGB_OPERANDS
00318     YUV2RGB_ENDFUNC
00319 }
00320 
00321 static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
00322                                        int srcStride[],
00323                                        int srcSliceY, int srcSliceH,
00324                                        uint8_t *dst[], int dstStride[])
00325 {
00326     int y, h_size, vshift;
00327 
00328     YUV2RGB_LOOP(3)
00329 
00330         YUV2RGB_INITIAL_LOAD
00331         YUV2RGB
00332         RGB_PACK24(REG_RED, REG_BLUE)
00333 
00334     YUV2RGB_ENDLOOP(3)
00335     YUV2RGB_OPERANDS
00336     YUV2RGB_ENDFUNC
00337 }
00338 
00339 
00340 #define SET_EMPTY_ALPHA                                                      \
00341     "pcmpeqd   %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
00342 
00343 #define LOAD_ALPHA                                   \
00344     "movq      (%6, %0, 2),     %%mm"REG_ALPHA"\n\t" \
00345 
00346 #define RGB_PACK32(red, green, blue, alpha)  \
00347     "movq      %%mm"blue",  %%mm5\n\t"       \
00348     "movq      %%mm"red",   %%mm6\n\t"       \
00349     "punpckhbw %%mm"green", %%mm5\n\t"       \
00350     "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
00351     "punpckhbw %%mm"alpha", %%mm6\n\t"       \
00352     "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
00353     "movq      %%mm"blue",  %%mm"green"\n\t" \
00354     "movq      %%mm5,       %%mm"alpha"\n\t" \
00355     "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
00356     "punpckhwd %%mm"red",   %%mm"green"\n\t" \
00357     "punpcklwd %%mm6,       %%mm5\n\t"       \
00358     "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
00359     MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
00360     MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
00361     MOVNTQ "   %%mm5,       16(%1)\n\t"      \
00362     MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \
00363 
00364 #if !COMPILE_TEMPLATE_MMX2
00365 static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
00366                                        int srcStride[],
00367                                        int srcSliceY, int srcSliceH,
00368                                        uint8_t *dst[], int dstStride[])
00369 {
00370     int y, h_size, vshift;
00371 
00372     YUV2RGB_LOOP(4)
00373 
00374         YUV2RGB_INITIAL_LOAD
00375         YUV2RGB
00376         RGB_PACK_INTERLEAVE
00377         SET_EMPTY_ALPHA
00378         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
00379 
00380     YUV2RGB_ENDLOOP(4)
00381     YUV2RGB_OPERANDS
00382     YUV2RGB_ENDFUNC
00383 }
00384 
00385 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
00386 static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
00387                                         int srcStride[],
00388                                         int srcSliceY, int srcSliceH,
00389                                         uint8_t *dst[], int dstStride[])
00390 {
00391     int y, h_size, vshift;
00392 
00393     YUV2RGB_LOOP(4)
00394 
00395         const uint8_t *pa = src[3] + y * srcStride[3];
00396         YUV2RGB_INITIAL_LOAD
00397         YUV2RGB
00398         RGB_PACK_INTERLEAVE
00399         LOAD_ALPHA
00400         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
00401 
00402     YUV2RGB_ENDLOOP(4)
00403     YUV2RGB_OPERANDS_ALPHA
00404     YUV2RGB_ENDFUNC
00405 }
00406 #endif
00407 
00408 static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
00409                                        int srcStride[],
00410                                        int srcSliceY, int srcSliceH,
00411                                        uint8_t *dst[], int dstStride[])
00412 {
00413     int y, h_size, vshift;
00414 
00415     YUV2RGB_LOOP(4)
00416 
00417         YUV2RGB_INITIAL_LOAD
00418         YUV2RGB
00419         RGB_PACK_INTERLEAVE
00420         SET_EMPTY_ALPHA
00421         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
00422 
00423     YUV2RGB_ENDLOOP(4)
00424     YUV2RGB_OPERANDS
00425     YUV2RGB_ENDFUNC
00426 }
00427 
00428 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
00429 static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
00430                                         int srcStride[],
00431                                         int srcSliceY, int srcSliceH,
00432                                         uint8_t *dst[], int dstStride[])
00433 {
00434     int y, h_size, vshift;
00435 
00436     YUV2RGB_LOOP(4)
00437 
00438         const uint8_t *pa = src[3] + y * srcStride[3];
00439         YUV2RGB_INITIAL_LOAD
00440         YUV2RGB
00441         RGB_PACK_INTERLEAVE
00442         LOAD_ALPHA
00443         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
00444 
00445     YUV2RGB_ENDLOOP(4)
00446     YUV2RGB_OPERANDS_ALPHA
00447     YUV2RGB_ENDFUNC
00448 }
00449 #endif
00450 
00451 #endif /* !COMPILE_TEMPLATE_MMX2 */
Generated on Fri Feb 1 2013 14:34:57 for FFmpeg by doxygen 1.7.1