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