inline static void RENAME(hcscale)(SwsContext *c, uint16_t *dst, long dstWidth, uint8_t *src1, uint8_t *src2,

                                   int srcW, int xInc, int flags, int canMMX2BeUsed, int16_t *hChrFilter,

                                   int16_t *hChrFilterPos, int hChrFilterSize, void *funnyUVCode,

                                   int srcFormat, uint8_t *formatConvBuffer, int16_t *mmx2Filter,

                                   int32_t *mmx2FilterPos, uint8_t *pal)

{

    if (srcFormat==PIX_FMT_YUYV422)

    {

        RENAME(yuy2ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_UYVY422)

    {

        RENAME(uyvyToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_RGB32)

    {

        if(c->chrSrcHSubSample)

            RENAME(bgr32ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(bgr32ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_RGB32_1)

    {

        if(c->chrSrcHSubSample)

            RENAME(bgr32ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1+ALT32_CORR, src2+ALT32_CORR, srcW);

        else

            RENAME(bgr32ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1+ALT32_CORR, src2+ALT32_CORR, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_BGR24)

    {

        if(c->chrSrcHSubSample)

            RENAME(bgr24ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(bgr24ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_BGR565)

    {

        if(c->chrSrcHSubSample)

            RENAME(bgr16ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(bgr16ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_BGR555)

    {

        if(c->chrSrcHSubSample)

            RENAME(bgr15ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(bgr15ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_BGR32)

    {

        if(c->chrSrcHSubSample)

            RENAME(rgb32ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(rgb32ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_BGR32_1)

    {

        if(c->chrSrcHSubSample)

            RENAME(rgb32ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1+ALT32_CORR, src2+ALT32_CORR, srcW);

        else

            RENAME(rgb32ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1+ALT32_CORR, src2+ALT32_CORR, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_RGB24)

    {

        if(c->chrSrcHSubSample)

            RENAME(rgb24ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(rgb24ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_RGB565)

    {

        if(c->chrSrcHSubSample)

            RENAME(rgb16ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(rgb16ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (srcFormat==PIX_FMT_RGB555)

    {

        if(c->chrSrcHSubSample)

            RENAME(rgb15ToUV_half)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        else

            RENAME(rgb15ToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }

    else if (isGray(srcFormat) || srcFormat==PIX_FMT_MONOBLACK || PIX_FMT_MONOWHITE)

    {

        return;

    }

    else if (srcFormat==PIX_FMT_RGB8 || srcFormat==PIX_FMT_BGR8 || srcFormat==PIX_FMT_PAL8 || srcFormat==PIX_FMT_BGR4_BYTE  || srcFormat==PIX_FMT_RGB4_BYTE)

    {

        RENAME(palToUV)(formatConvBuffer, formatConvBuffer+VOFW, src1, src2, srcW, (uint32_t*)pal);

        src1= formatConvBuffer;

        src2= formatConvBuffer+VOFW;

    }



#ifdef HAVE_MMX

    

    if (!(flags&SWS_FAST_BILINEAR) || (!canMMX2BeUsed))

#else

    if (!(flags&SWS_FAST_BILINEAR))

#endif

    {

        RENAME(hScale)(dst     , dstWidth, src1, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);

        RENAME(hScale)(dst+VOFW, dstWidth, src2, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);

    }

    else 

    {

#if defined(ARCH_X86)

#ifdef HAVE_MMX2

        int i;

#if defined(PIC)

        uint64_t ebxsave __attribute__((aligned(8)));

#endif

        if (canMMX2BeUsed)

        {

            asm volatile(

#if defined(PIC)

            "mov          %%"REG_b", %6         \n\t"

#endif

            "pxor             %%mm7, %%mm7      \n\t"

            "mov                 %0, %%"REG_c"  \n\t"

            "mov                 %1, %%"REG_D"  \n\t"

            "mov                 %2, %%"REG_d"  \n\t"

            "mov                 %3, %%"REG_b"  \n\t"

            "xor          %%"REG_a", %%"REG_a"  \n\t" 

            PREFETCH"   (%%"REG_c")             \n\t"

            PREFETCH" 32(%%"REG_c")             \n\t"

            PREFETCH" 64(%%"REG_c")             \n\t"



#ifdef ARCH_X86_64



#define FUNNY_UV_CODE \

            "movl       (%%"REG_b"), %%esi      \n\t"\

            "call               *%4             \n\t"\

            "movl (%%"REG_b", %%"REG_a"), %%esi \n\t"\

            "add          %%"REG_S", %%"REG_c"  \n\t"\

            "add          %%"REG_a", %%"REG_D"  \n\t"\

            "xor          %%"REG_a", %%"REG_a"  \n\t"\



#else



#define FUNNY_UV_CODE \

            "movl       (%%"REG_b"), %%esi      \n\t"\

            "call               *%4             \n\t"\

            "addl (%%"REG_b", %%"REG_a"), %%"REG_c" \n\t"\

            "add          %%"REG_a", %%"REG_D"  \n\t"\

            "xor          %%"REG_a", %%"REG_a"  \n\t"\



#endif 



FUNNY_UV_CODE

FUNNY_UV_CODE

FUNNY_UV_CODE

FUNNY_UV_CODE

            "xor          %%"REG_a", %%"REG_a"  \n\t" 

            "mov                 %5, %%"REG_c"  \n\t" 

            "mov                 %1, %%"REG_D"  \n\t" 

            "add              $"AV_STRINGIFY(VOF)", %%"REG_D"  \n\t"

            PREFETCH"   (%%"REG_c")             \n\t"

            PREFETCH" 32(%%"REG_c")             \n\t"

            PREFETCH" 64(%%"REG_c")             \n\t"



FUNNY_UV_CODE

FUNNY_UV_CODE

FUNNY_UV_CODE

FUNNY_UV_CODE



#if defined(PIC)

            "mov %6, %%"REG_b"    \n\t"

#endif

            :: "m" (src1), "m" (dst), "m" (mmx2Filter), "m" (mmx2FilterPos),

            "m" (funnyUVCode), "m" (src2)

#if defined(PIC)

            ,"m" (ebxsave)

#endif

            : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D

#if !defined(PIC)

             ,"%"REG_b

#endif

            );

            for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)

            {

                

                dst[i] = src1[srcW-1]*128;

                dst[i+VOFW] = src2[srcW-1]*128;

            }

        }

        else

        {

#endif 

            long xInc_shr16 = (long) (xInc >> 16);

            uint16_t xInc_mask = xInc & 0xffff;

            asm volatile(

            "xor %%"REG_a", %%"REG_a"               \n\t" 

            "xor %%"REG_d", %%"REG_d"               \n\t" 

            "xorl    %%ecx, %%ecx                   \n\t" 

            ASMALIGN(4)

            "1:                                     \n\t"

            "mov        %0, %%"REG_S"               \n\t"

            "movzbl  (%%"REG_S", %%"REG_d"), %%edi  \n\t" 

            "movzbl 1(%%"REG_S", %%"REG_d"), %%esi  \n\t" 

            "subl    %%edi, %%esi                   \n\t"  - src[xx]

            "imull   %%ecx, %%esi                   \n\t" 

            "shll      $16, %%edi                   \n\t"

            "addl    %%edi, %%esi                   \n\t" *2*xalpha + src[xx]*(1-2*xalpha)

            "mov        %1, %%"REG_D"               \n\t"

            "shrl       $9, %%esi                   \n\t"

            "movw     %%si, (%%"REG_D", %%"REG_a", 2)   \n\t"



            "movzbl    (%5, %%"REG_d"), %%edi       \n\t" 

            "movzbl   1(%5, %%"REG_d"), %%esi       \n\t" 

            "subl    %%edi, %%esi                   \n\t"  - src[xx]

            "imull   %%ecx, %%esi                   \n\t" 

            "shll      $16, %%edi                   \n\t"

            "addl    %%edi, %%esi                   \n\t" *2*xalpha + src[xx]*(1-2*xalpha)

            "mov        %1, %%"REG_D"               \n\t"

            "shrl       $9, %%esi                   \n\t"

            "movw     %%si, "AV_STRINGIFY(VOF)"(%%"REG_D", %%"REG_a", 2)   \n\t"



            "addw       %4, %%cx                    \n\t" 

            "adc        %3, %%"REG_d"               \n\t" 

            "add        $1, %%"REG_a"               \n\t"

            "cmp        %2, %%"REG_a"               \n\t"

            " jb        1b                          \n\t"







#if defined(ARCH_X86_64) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))

            :: "m" (src1), "m" (dst), "g" ((long)dstWidth), "m" (xInc_shr16), "m" (xInc_mask),

#else

            :: "m" (src1), "m" (dst), "m" ((long)dstWidth), "m" (xInc_shr16), "m" (xInc_mask),

#endif

            "r" (src2)

            : "%"REG_a, "%"REG_d, "%ecx", "%"REG_D, "%esi"

            );

#ifdef HAVE_MMX2

        } 

#endif

#else

        int i;

        unsigned int xpos=0;

        for (i=0;i<dstWidth;i++)

        {

            register unsigned int xx=xpos>>16;

            register unsigned int xalpha=(xpos&0xFFFF)>>9;

            dst[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);

            dst[i+VOFW]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);

            







            xpos+=xInc;

        }

#endif 

    }

    if(c->srcRange != c->dstRange && !(isRGB(c->dstFormat) || isBGR(c->dstFormat))){

        int i;

        

        

        if(c->srcRange){

            for (i=0; i<dstWidth; i++){

                dst[i     ]= (dst[i     ]*1799 + 4081085)>>11; 

                dst[i+VOFW]= (dst[i+VOFW]*1799 + 4081085)>>11; 

            }

        }else{

            for (i=0; i<dstWidth; i++){

                dst[i     ]= (FFMIN(dst[i     ],30775)*4663 - 9289992)>>12; 

                dst[i+VOFW]= (FFMIN(dst[i+VOFW],30775)*4663 - 9289992)>>12; 

            }

        }

    }

}
