• 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 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         );                                                        \
00142     }                                                             \
00143 
00144 #define YUV2RGB_OPERANDS_ALPHA                                    \
00145         : "+r" (index), "+r" (image)                              \
00146         : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
00147           "r" (py - 2*index), "r" (pa - 2*index)                  \
00148         );                                                        \
00149     }                                                             \
00150 
00151 #define YUV2RGB_ENDFUNC                          \
00152     __asm__ volatile (SFENCE"\n\t"               \
00153                     "emms    \n\t");             \
00154     return srcSliceH;                            \
00155 
00156 #define IF0(x)
00157 #define IF1(x) x
00158 
00159 #define RGB_PACK16(gmask, is15)                  \
00160     "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
00161     "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
00162     "movq      %%mm2,     %%mm3\n\t"             \
00163     "psllw   $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
00164     "psrlw   $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
00165     "psrlw     $3,        %%mm0\n\t"             \
00166     IF##is15("psrlw  $1,  %%mm1\n\t")            \
00167     "pand "MANGLE(pb_e0)", %%mm2\n\t"            \
00168     "pand "MANGLE(gmask)", %%mm3\n\t"            \
00169     "por       %%mm2,     %%mm0\n\t"             \
00170     "por       %%mm3,     %%mm1\n\t"             \
00171     "movq      %%mm0,     %%mm2\n\t"             \
00172     "punpcklbw %%mm1,     %%mm0\n\t"             \
00173     "punpckhbw %%mm1,     %%mm2\n\t"             \
00174     MOVNTQ "   %%mm0,      (%1)\n\t"             \
00175     MOVNTQ "   %%mm2,     8(%1)\n\t"             \
00176 
00177 #define DITHER_RGB                               \
00178     "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
00179     "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
00180     "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \
00181 
00182 #if !COMPILE_TEMPLATE_MMX2
00183 static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
00184                                        int srcStride[],
00185                                        int srcSliceY, int srcSliceH,
00186                                        uint8_t *dst[], int dstStride[])
00187 {
00188     int y, h_size, vshift;
00189 
00190     YUV2RGB_LOOP(2)
00191 
00192 #ifdef DITHER1XBPP
00193         c->blueDither  = ff_dither8[y       & 1];
00194         c->greenDither = ff_dither8[y       & 1];
00195         c->redDither   = ff_dither8[(y + 1) & 1];
00196 #endif
00197 
00198         YUV2RGB_INITIAL_LOAD
00199         YUV2RGB
00200         RGB_PACK_INTERLEAVE
00201 #ifdef DITHER1XBPP
00202         DITHER_RGB
00203 #endif
00204         RGB_PACK16(pb_03, 1)
00205 
00206     YUV2RGB_ENDLOOP(2)
00207     YUV2RGB_OPERANDS
00208     YUV2RGB_ENDFUNC
00209 }
00210 
00211 static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
00212                                        int srcStride[],
00213                                        int srcSliceY, int srcSliceH,
00214                                        uint8_t *dst[], int dstStride[])
00215 {
00216     int y, h_size, vshift;
00217 
00218     YUV2RGB_LOOP(2)
00219 
00220 #ifdef DITHER1XBPP
00221         c->blueDither  = ff_dither8[y       & 1];
00222         c->greenDither = ff_dither4[y       & 1];
00223         c->redDither   = ff_dither8[(y + 1) & 1];
00224 #endif
00225 
00226         YUV2RGB_INITIAL_LOAD
00227         YUV2RGB
00228         RGB_PACK_INTERLEAVE
00229 #ifdef DITHER1XBPP
00230         DITHER_RGB
00231 #endif
00232         RGB_PACK16(pb_07, 0)
00233 
00234     YUV2RGB_ENDLOOP(2)
00235     YUV2RGB_OPERANDS
00236     YUV2RGB_ENDFUNC
00237 }
00238 #endif /* !COMPILE_TEMPLATE_MMX2 */
00239 
00240 #define RGB_PACK24(blue, red)\
00241     "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
00242     "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
00243     "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
00244     "movq      %%mm"red",  %%mm3 \n"\
00245     "movq      %%mm"blue", %%mm6 \n"\
00246     "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
00247     "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
00248     "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
00249     "movq      %%mm3,      %%mm5 \n"\
00250     "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
00251     "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
00252     "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
00253     RGB_PACK24_B
00254 
00255 #if COMPILE_TEMPLATE_MMX2
00256 DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
00257 DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
00258 DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
00259 DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
00260 DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
00261 #undef RGB_PACK24_B
00262 #define RGB_PACK24_B\
00263     "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
00264     "pshufw    $0x84,  %%mm3, %%mm6 \n"\
00265     "pshufw    $0x38,  %%mm5, %%mm7 \n"\
00266     "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
00267     "movq      %%mm1,         %%mm0 \n"\
00268     "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
00269     "movq      %%mm1,         %%mm2 \n"\
00270     "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
00271     "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
00272     "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
00273     "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
00274     "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
00275     "por       %%mm3,         %%mm1 \n"\
00276     "por       %%mm6,         %%mm0 \n"\
00277     "por       %%mm5,         %%mm1 \n"\
00278     "por       %%mm7,         %%mm2 \n"\
00279     MOVNTQ"    %%mm0,          (%1) \n"\
00280     MOVNTQ"    %%mm1,         8(%1) \n"\
00281     MOVNTQ"    %%mm2,        16(%1) \n"\
00282 
00283 #else
00284 #undef RGB_PACK24_B
00285 #define RGB_PACK24_B\
00286     "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
00287     "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
00288     "psrlq     $32,        %%mm3 \n"\
00289     "psrlq     $16,        %%mm2 \n"\
00290     "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
00291     "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
00292     "psrlq     $16,        %%mm2 \n"\
00293     "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
00294     "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
00295     "psrlq     $32,        %%mm5 \n"\
00296     "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
00297     "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\
00298 
00299 #endif
00300 
00301 static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
00302                                        int srcStride[],
00303                                        int srcSliceY, int srcSliceH,
00304                                        uint8_t *dst[], int dstStride[])
00305 {
00306     int y, h_size, vshift;
00307 
00308     YUV2RGB_LOOP(3)
00309 
00310         YUV2RGB_INITIAL_LOAD
00311         YUV2RGB
00312         RGB_PACK24(REG_BLUE, REG_RED)
00313 
00314     YUV2RGB_ENDLOOP(3)
00315     YUV2RGB_OPERANDS
00316     YUV2RGB_ENDFUNC
00317 }
00318 
00319 static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
00320                                        int srcStride[],
00321                                        int srcSliceY, int srcSliceH,
00322                                        uint8_t *dst[], int dstStride[])
00323 {
00324     int y, h_size, vshift;
00325 
00326     YUV2RGB_LOOP(3)
00327 
00328         YUV2RGB_INITIAL_LOAD
00329         YUV2RGB
00330         RGB_PACK24(REG_RED, REG_BLUE)
00331 
00332     YUV2RGB_ENDLOOP(3)
00333     YUV2RGB_OPERANDS
00334     YUV2RGB_ENDFUNC
00335 }
00336 
00337 
00338 #define SET_EMPTY_ALPHA                                                      \
00339     "pcmpeqd   %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
00340 
00341 #define LOAD_ALPHA                                   \
00342     "movq      (%6, %0, 2),     %%mm"REG_ALPHA"\n\t" \
00343 
00344 #define RGB_PACK32(red, green, blue, alpha)  \
00345     "movq      %%mm"blue",  %%mm5\n\t"       \
00346     "movq      %%mm"red",   %%mm6\n\t"       \
00347     "punpckhbw %%mm"green", %%mm5\n\t"       \
00348     "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
00349     "punpckhbw %%mm"alpha", %%mm6\n\t"       \
00350     "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
00351     "movq      %%mm"blue",  %%mm"green"\n\t" \
00352     "movq      %%mm5,       %%mm"alpha"\n\t" \
00353     "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
00354     "punpckhwd %%mm"red",   %%mm"green"\n\t" \
00355     "punpcklwd %%mm6,       %%mm5\n\t"       \
00356     "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
00357     MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
00358     MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
00359     MOVNTQ "   %%mm5,       16(%1)\n\t"      \
00360     MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \
00361 
00362 #if !COMPILE_TEMPLATE_MMX2
00363 static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
00364                                        int srcStride[],
00365                                        int srcSliceY, int srcSliceH,
00366                                        uint8_t *dst[], int dstStride[])
00367 {
00368     int y, h_size, vshift;
00369 
00370     YUV2RGB_LOOP(4)
00371 
00372         YUV2RGB_INITIAL_LOAD
00373         YUV2RGB
00374         RGB_PACK_INTERLEAVE
00375         SET_EMPTY_ALPHA
00376         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
00377 
00378     YUV2RGB_ENDLOOP(4)
00379     YUV2RGB_OPERANDS
00380     YUV2RGB_ENDFUNC
00381 }
00382 
00383 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
00384 static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
00385                                         int srcStride[],
00386                                         int srcSliceY, int srcSliceH,
00387                                         uint8_t *dst[], int dstStride[])
00388 {
00389     int y, h_size, vshift;
00390 
00391     YUV2RGB_LOOP(4)
00392 
00393         const uint8_t *pa = src[3] + y * srcStride[3];
00394         YUV2RGB_INITIAL_LOAD
00395         YUV2RGB
00396         RGB_PACK_INTERLEAVE
00397         LOAD_ALPHA
00398         RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
00399 
00400     YUV2RGB_ENDLOOP(4)
00401     YUV2RGB_OPERANDS_ALPHA
00402     YUV2RGB_ENDFUNC
00403 }
00404 #endif
00405 
00406 static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
00407                                        int srcStride[],
00408                                        int srcSliceY, int srcSliceH,
00409                                        uint8_t *dst[], int dstStride[])
00410 {
00411     int y, h_size, vshift;
00412 
00413     YUV2RGB_LOOP(4)
00414 
00415         YUV2RGB_INITIAL_LOAD
00416         YUV2RGB
00417         RGB_PACK_INTERLEAVE
00418         SET_EMPTY_ALPHA
00419         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
00420 
00421     YUV2RGB_ENDLOOP(4)
00422     YUV2RGB_OPERANDS
00423     YUV2RGB_ENDFUNC
00424 }
00425 
00426 #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
00427 static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
00428                                         int srcStride[],
00429                                         int srcSliceY, int srcSliceH,
00430                                         uint8_t *dst[], int dstStride[])
00431 {
00432     int y, h_size, vshift;
00433 
00434     YUV2RGB_LOOP(4)
00435 
00436         const uint8_t *pa = src[3] + y * srcStride[3];
00437         YUV2RGB_INITIAL_LOAD
00438         YUV2RGB
00439         RGB_PACK_INTERLEAVE
00440         LOAD_ALPHA
00441         RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
00442 
00443     YUV2RGB_ENDLOOP(4)
00444     YUV2RGB_OPERANDS_ALPHA
00445     YUV2RGB_ENDFUNC
00446 }
00447 #endif
00448 
00449 #endif /* !COMPILE_TEMPLATE_MMX2 */

Generated on Wed Apr 11 2012 07:31:37 for FFmpeg by  doxygen 1.7.1