[Ffmpeg-devel] [PATCH] flacenc - lpc and options

Michael Niedermayer michaelni
Fri Jun 30 15:12:54 CEST 2006


Hi

On Fri, Jun 30, 2006 at 01:51:41PM +0200, Ivo wrote:
> On Friday 30 June 2006 12:02, Michael Niedermayer wrote:
> > On Fri, Jun 30, 2006 at 11:01:15AM +0200, Ivo wrote:
> > > A while ago I imported MD5.[ch] from
> > > http://ubiqx.org/libcifs/source/Auth/ into the MPlayer source tree (as
> > > libvo/md5sum.[ch]). Maybe you could move those to libavutil and reuse
> > > them? I will change vo_md5sum accordingly to use libavutil/md5sum if
> > > that happens.
> >
> > forget it, this code is a terribly slow and bloated mess, completely
> > unfit for libav*
> 
> Apart from being overly commented, I wasn't aware of it being slow and 
> bloated. I think it's less than 50 lines of actual code and the object file 
> is just a little over 2kB. But anyway, Justin can also write it from 
> scratch or only peek at it for inspiration. In any case, if libavutil gets 
> a fast(er) md5 function, I'll modify vo_md5sum to use that and drop the 
> imported code.

i wouldnt call it overly commented, but rather obfuscated by useless comments
and its code is bloated in the sense that its just doing shitloads of uneeded
stuff, for example:

#define GetLongByte( L, idx ) ((uchar)(( L >> (((idx) & 0x03) << 3) ) & 0xFF))

idx is always 0..3 the &3 is unneeded, the uchar and & 0xFF is redundant

then for example we have code like:

/* Add the total length and perform the final perumation.
   * Note:  The 60'th byte is read from the *original* <ctx->len> value
   *        and shifted to the correct position.  This neatly avoids
   *        any MAXINT numeric overflow issues.
   */
  l = ctx->len << 3;
  for( i = 0; i < 4; i++ )
    ctx->block[56+i] |= GetLongByte( l, i );
  ctx->block[60] = ((GetLongByte( ctx->len, 3 ) & 0xE0) >> 5);  /* See Above! */

this code is not only obfuscated but wrong, the author tries to solve the
problem of ctx->len << 3 not fitting in a 32bit int in case this isnt
obvious ...
the |= is useless too, as its guranteed to be 0 so = would do

the correct code (after changing len to uint64_t which is needed per spec)

    for(i=0; i<8; i++)
        ctx->block[56+i] = (ctx->len << 3) >> (i<<3);


a cleaned up md5.c is attached, iam not claiming it to be faster, probably
its not but that is a gcc issue, if gcc would unroll the main loop it would
be quite fast as everything could be optimized away

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

In the past you could go to a library and read, borrow or copy any book
Today you'd get arrested for mere telling someone where the library is
-------------- next part --------------
/*
 * Copyright (C) 2006 Michael Niedermayer (michaelni at gmx.at)
 * Copyright (C) 2003-2005 by Christopher R. Hertel (crh at ubiqx.mn.org)
 * #inlcude LGPL license
 *
 * References:
 *  IETF RFC 1321: The MD5 Message-Digest Algorithm
 *       Ron Rivest. IETF, April, 1992
 *
 * based on http://ubiqx.org/libcifs/source/Auth/MD5.c 
 *          from Christopher R. Hertel (crh at ubiqx.mn.org)
 * simplified, cleaned and IMO redundant comments removed by michael
 */

#include <string.h>
#include <inttypes.h>

typedef struct {
    uint8_t  block[64];
    uint32_t ABCD[4];
    uint64_t len;
    int      b_used;
} av_md5Ctx;

static const uint8_t S[4][4] = {
    { 7, 12, 17, 22 },  /* Round 1 */
    { 5,  9, 14, 20 },  /* Round 2 */
    { 4, 11, 16, 23 },  /* Round 3 */
    { 6, 10, 15, 21 }   /* Round 4 */
};

static const uint32_t T[64] = {
    0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,   /* Round 1 */
    0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
    0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
    0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,

    0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,   /* Round 2 */
    0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
    0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
    0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,

    0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,   /* Round 3 */
    0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
    0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
    0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,

    0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,   /* Round 4 */
    0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
    0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
    0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
};


static void Permute( uint32_t ABCD2[4], const uint32_t X[16] ){
    int i;
    uint32_t ABCD[4];

    memcpy(ABCD, ABCD2, sizeof(ABCD));

#ifdef WORDS_BIGENDIAN
    for(i=0; i<16; i++)
        X[i]= bswap_32(X[i]);
#endif

    for( i = 0; i < 64; i++ ){
        unsigned int s = S[i>>4][i&3];
        unsigned int a = ABCD[(3+i) & 0x3] + T[i];
        unsigned int b = ABCD[(2+i) & 0x3];
        unsigned int c = ABCD[(1+i) & 0x3];
        unsigned int d = ABCD[(0+i) & 0x3];

        switch(i>>4){
        case 0: a += (b&c | (~b)&d) + X[     i  &15 ]; break;
        case 1: a += (b&d | c&(~d)) + X[ (1+5*i)&15 ]; break;
        case 2: a += (b^c^d)        + X[ (5+3*i)&15 ]; break;
        case 3: a += (c^(b|~d))     + X[ (  7*i)&15 ]; break;
        }
        ABCD[(3+i) & 0x3] = b + (( a << s ) | ( a >> (32 - s) ));
    }

    for( i = 0; i < 4; i++ )
        ABCD2[i] += ABCD[i];
}

av_md5Ctx *av_md5InitCtx( av_md5Ctx *ctx ){
  ctx->len    =
  ctx->b_used = 0;

  ctx->ABCD[0] = 0x10325476;
  ctx->ABCD[1] = 0x98badcfe;
  ctx->ABCD[2] = 0xefcdab89;
  ctx->ABCD[3] = 0x67452301;

  return ctx;
}

av_md5Ctx *av_md5SumCtx( av_md5Ctx *ctx,
                             const uint8_t *src,
                             const int    len ){
    int i;

    ctx->len += len;

    for( i = 0; i < len; i++ ){
        ctx->block[ ctx->b_used++ ] = src[i];
        if( 64 == ctx->b_used ){
            Permute( ctx->ABCD, ctx->block );
            ctx->b_used = 0;
        }
    }

    return ctx;
}


av_md5Ctx *av_md5CloseCtx( av_md5Ctx *ctx, uint8_t *dst ){
    int      i;

    ctx->block[ctx->b_used++] = 0x80;

    memset(&ctx->block[ctx->b_used], 0, 64 - ctx->b_used);

    if( 56 < ctx->b_used ){
        Permute( ctx->ABCD, ctx->block );
        memset(ctx->block, 0, 64);
    }

    for(i=0; i<8; i++)
        ctx->block[56+i] = (ctx->len << 3) >> (i<<3);

    Permute( ctx->ABCD, ctx->block );

#define le2me_32(a) a
    for(i=0; i<4; i++)
        ((uint32_t*)dst)[i]= le2me_32(ctx->ABCD[3-i]);

    return ctx;
}


uint8_t *av_md5Sum( uint8_t *dst, const uint8_t *src, const int len ){
    av_md5Ctx ctx[1];

    av_md5InitCtx( ctx );             /* Open a context.      */
    av_md5SumCtx( ctx, src, len );    /* Pass only one block. */
    av_md5CloseCtx( ctx, dst );       /* Close the context.   */

    return dst;
}

#ifdef TEST
#include <stdio.h>
main(){
    uint64_t md5val;
    int i;
    uint8_t in[1000];

    for(i=0; i<1000; i++) in[i]= i*i;
    av_md5Sum( (uint8_t*)&md5val, in,  1000); printf("%lld\n", md5val);
    av_md5Sum( (uint8_t*)&md5val, in,  63); printf("%lld\n", md5val);
    av_md5Sum( (uint8_t*)&md5val, in,  64); printf("%lld\n", md5val);
    av_md5Sum( (uint8_t*)&md5val, in,  65); printf("%lld\n", md5val);
    for(i=0; i<1000; i++) in[i]= i % 127;
    av_md5Sum( (uint8_t*)&md5val, in,  999); printf("%lld\n", md5val);
}
#endif



More information about the ffmpeg-devel mailing list