[FFmpeg-devel] [PATCH] MMX2 scaler: Determine "funnyCode" size at runtime.

Michael Niedermayer michaelni
Thu Aug 13 03:38:51 CEST 2009


On Wed, Aug 12, 2009 at 09:38:00PM -0300, Ramiro Polla wrote:
> Hi,
> 
> $subj again.
> 
> Ramiro Polla

>  swscale.c          |   49 ++++++++++++++++++++++++++++++-------------------
>  swscale_internal.h |    2 ++
>  2 files changed, 32 insertions(+), 19 deletions(-)
> d69a3a913f17ec8d300b681bb8b41b2f7ef92576  0001-MMX2-scaler-Determine-funnyCode-size-at-runtime.patch
> From 49139b8e6daf70ad595698279406753cbaf40d98 Mon Sep 17 00:00:00 2001
> From: Ramiro Polla <ramiro.polla at gmail.com>
> Date: Mon, 27 Jul 2009 04:47:30 -0300
> Subject: [PATCH] MMX2 scaler: Determine "funnyCode" size at runtime.
> 
> ---
>  swscale.c          |   49 ++++++++++++++++++++++++++++++-------------------
>  swscale_internal.h |    2 ++
>  2 files changed, 32 insertions(+), 19 deletions(-)
> 
> diff --git a/swscale.c b/swscale.c
> index 2adf393..6e0535c 100644
> --- a/swscale.c
> +++ b/swscale.c
> @@ -1753,7 +1753,7 @@ error:
>  }
>  
>  #ifdef COMPILE_MMX2
> -static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
> +static void initMMX2HScaler(int dstW, int xInc, uint8_t **funnyCodePtr, int *funnyCodeSizePtr, int16_t *filter, int32_t *filterPos, int numSplits)
>  {
>      uint8_t *fragmentA;
>      x86_reg imm8OfPShufW1A;
> @@ -1763,6 +1763,8 @@ static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *fil
>      x86_reg imm8OfPShufW1B;
>      x86_reg imm8OfPShufW2B;
>      x86_reg fragmentLengthB;
> +    int funnyCodeSize = 1;
> +    uint8_t *funnyCode;
>      int fragmentPos;
>  
>      int xpos, i;

> @@ -1852,6 +1854,27 @@ static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *fil
>      xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
>      fragmentPos=0;
>  
> +    /* Determine code size. */
> +    for (i=0; i<dstW/numSplits; i+=4) {
> +        if (((xpos+xInc*3)>>16) - (xpos>>16) < 3)
> +            funnyCodeSize += fragmentLengthB;
> +        else
> +            funnyCodeSize += fragmentLengthA;
> +        xpos+=xInc*4;
> +    }
> +    *funnyCodeSizePtr = funnyCodeSize;

this is obfuscated
someone changng some related code would almost certainly miss updating this
also *A/*B are very poor names
i think the safest way is to just run the code twice and in the first run dont
write anything, just count how much would be written



> +
> +#ifdef MAP_ANONYMOUS
> +    *funnyCodePtr = mmap(NULL, funnyCodeSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +#elif HAVE_VIRTUALALLOC
> +    *funnyCodePtr = VirtualAlloc(NULL, funnyCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
> +#else
> +    *funnyCodePtr = av_malloc(funnyCodeSize);
> +#endif
> +    funnyCode = *funnyCodePtr;
> +
> +    xpos= 0;
> +

memory should never be writeable and executable,
making it writeable, write into it and then switch from write to executeable
is what should be done.

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Those who are too smart to engage in politics are punished by being
governed by those who are dumber. -- Plato 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090813/58eb3086/attachment.pgp>



More information about the ffmpeg-devel mailing list