[FFmpeg-devel] [RFC][PATCH 1/3] lavu: Add av_crc2() and av_crc_init2()

Paul B Mahol onemda at gmail.com
Sun Jan 27 12:00:23 CET 2013


On 1/27/13, James Almer <jamrial at gmail.com> wrote:
> Functionally the same as av_crc() and av_crc_init(), but
> supporting CRCs of up to 64 bits.
>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavutil/crc.c | 33 ++++++++++++++++++++++++---------
>  libavutil/crc.h |  7 ++++++-
>  2 files changed, 30 insertions(+), 10 deletions(-)
>
> diff --git a/libavutil/crc.c b/libavutil/crc.c
> index aeb9e46..792e898 100644
> --- a/libavutil/crc.c
> +++ b/libavutil/crc.c
> @@ -29,7 +29,7 @@
>  static struct {
>      uint8_t  le;
>      uint8_t  bits;
> -    uint32_t poly;
> +    uint64_t poly;

I do not think this is safe.

>  } av_crc_table_params[AV_CRC_MAX] = {
>      [AV_CRC_8_ATM]      = { 0,  8,       0x07 },
>      [AV_CRC_16_ANSI]    = { 0, 16,     0x8005 },
> @@ -42,10 +42,18 @@ static AVCRC av_crc_table[AV_CRC_MAX][257];
>
>  int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
>  {
> -    unsigned i, j;
> -    uint32_t c;
> +    if(bits > 32 || poly >= (1LL << bits))
> +        return -1;
> +
> +    return av_crc_init2(ctx, le, bits, poly, ctx_size);
> +}
> +
> +int av_crc_init2(AVCRC *ctx, int le, int bits, uint64_t poly, int
> ctx_size)
> +{
> +    unsigned j;
> +    uint64_t i, c;
>
> -    if (bits < 8 || bits > 32 || poly >= (1LL << bits))
> +    if (bits < 8 || bits > 64)
>          return -1;
>      if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) *
> 1024)
>          return -1;
> @@ -56,9 +64,9 @@ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t
> poly, int ctx_size)
>                  c = (c >> 1) ^ (poly & (-(c & 1)));
>              ctx[i] = c;
>          } else {
> -            for (c = i << 24, j = 0; j < 8; j++)
> -                c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >>
> 31));
> -            ctx[i] = av_bswap32(c);
> +            for (c = i << 56, j = 0; j < 8; j++)
> +                c = (c << 1) ^ ((poly << (64 - bits)) & (((int64_t) c) >>
> 63));
> +            ctx[i] = av_bswap64(c);
>          }
>      }
>      ctx[256] = 1;
> @@ -77,7 +85,7 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id)
>  {
>  #if !CONFIG_HARDCODED_TABLES
>      if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1])
> -        if (av_crc_init(av_crc_table[crc_id],
> +        if (av_crc_init2(av_crc_table[crc_id],
>                          av_crc_table_params[crc_id].le,
>                          av_crc_table_params[crc_id].bits,
>                          av_crc_table_params[crc_id].poly,
> @@ -90,6 +98,12 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id)
>  uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
>                  const uint8_t *buffer, size_t length)
>  {
> +    return (uint32_t)av_crc2(ctx, crc, buffer, length);
> +}
> +
> +uint64_t av_crc2(const AVCRC *ctx, uint64_t crc,
> +                 const uint8_t *buffer, size_t length)
> +{
>      const uint8_t *end = buffer + length;
>
>  #if !CONFIG_SMALL
> @@ -101,8 +115,9 @@ uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
>              crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
>              crc = ctx[3 * 256 + ( crc        & 0xFF)] ^
>                    ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
> +                                 (crc >> 32)          ^
>                    ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
> -                  ctx[0 * 256 + ((crc >> 24)       )];
> +                  ctx[0 * 256 + ((crc >> 24) & 0xFF)];
>          }
>      }
>  #endif
> diff --git a/libavutil/crc.h b/libavutil/crc.h
> index 2bdfca8..ca693a2 100644
> --- a/libavutil/crc.h
> +++ b/libavutil/crc.h
> @@ -25,7 +25,7 @@
>  #include <stddef.h>
>  #include "attributes.h"
>
> -typedef uint32_t AVCRC;
> +typedef uint64_t AVCRC;
>
>  typedef enum {
>      AV_CRC_8_ATM,
> @@ -52,6 +52,8 @@ typedef enum {
>   * @param ctx_size size of ctx in bytes
>   * @return <0 on failure
>   */
> +int av_crc_init2(AVCRC *ctx, int le, int bits, uint64_t poly, int
> ctx_size);
> +
>  int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int
> ctx_size);
>
>  /**
> @@ -68,6 +70,9 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id);
>   *
>   * @see av_crc_init() "le" parameter
>   */
> +uint64_t av_crc2(const AVCRC *ctx, uint64_t crc,
> +                 const uint8_t *buffer, size_t length) av_pure;
> +
>  uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
>                  const uint8_t *buffer, size_t length) av_pure;
>
> --
> 1.7.12.4
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list