• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/crc.c

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "config.h"
00022 #include "bswap.h"
00023 #include "crc.h"
00024 
00025 #if CONFIG_HARDCODED_TABLES
00026 #include "crc_data.h"
00027 #else
00028 static struct {
00029     uint8_t  le;
00030     uint8_t  bits;
00031     uint32_t poly;
00032 } av_crc_table_params[AV_CRC_MAX] = {
00033     [AV_CRC_8_ATM]      = { 0,  8,       0x07 },
00034     [AV_CRC_16_ANSI]    = { 0, 16,     0x8005 },
00035     [AV_CRC_16_CCITT]   = { 0, 16,     0x1021 },
00036     [AV_CRC_32_IEEE]    = { 0, 32, 0x04C11DB7 },
00037     [AV_CRC_32_IEEE_LE] = { 1, 32, 0xEDB88320 },
00038 };
00039 static AVCRC av_crc_table[AV_CRC_MAX][257];
00040 #endif
00041 
00058 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
00059     int i, j;
00060     uint32_t c;
00061 
00062     if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
00063         return -1;
00064     if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
00065         return -1;
00066 
00067     for (i = 0; i < 256; i++) {
00068         if (le) {
00069             for (c = i, j = 0; j < 8; j++)
00070                 c = (c>>1)^(poly & (-(c&1)));
00071             ctx[i] = c;
00072         } else {
00073             for (c = i << 24, j = 0; j < 8; j++)
00074                 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
00075             ctx[i] = bswap_32(c);
00076         }
00077     }
00078     ctx[256]=1;
00079 #if !CONFIG_SMALL
00080     if(ctx_size >= sizeof(AVCRC)*1024)
00081         for (i = 0; i < 256; i++)
00082             for(j=0; j<3; j++)
00083                 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
00084 #endif
00085 
00086     return 0;
00087 }
00088 
00094 const AVCRC *av_crc_get_table(AVCRCId crc_id){
00095 #if !CONFIG_HARDCODED_TABLES
00096     if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id])-1])
00097         if (av_crc_init(av_crc_table[crc_id],
00098                         av_crc_table_params[crc_id].le,
00099                         av_crc_table_params[crc_id].bits,
00100                         av_crc_table_params[crc_id].poly,
00101                         sizeof(av_crc_table[crc_id])) < 0)
00102             return NULL;
00103 #endif
00104     return av_crc_table[crc_id];
00105 }
00106 
00114 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
00115     const uint8_t *end= buffer+length;
00116 
00117 #if !CONFIG_SMALL
00118     if(!ctx[256])
00119         while(buffer<end-3){
00120             crc ^= le2me_32(*(const uint32_t*)buffer); buffer+=4;
00121             crc =  ctx[3*256 + ( crc     &0xFF)]
00122                   ^ctx[2*256 + ((crc>>8 )&0xFF)]
00123                   ^ctx[1*256 + ((crc>>16)&0xFF)]
00124                   ^ctx[0*256 + ((crc>>24)     )];
00125         }
00126 #endif
00127     while(buffer<end)
00128         crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
00129 
00130     return crc;
00131 }
00132 
00133 #ifdef TEST
00134 #undef printf
00135 int main(void){
00136     uint8_t buf[1999];
00137     int i;
00138     int p[4][3]={{AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04},
00139                  {AV_CRC_32_IEEE   , 0x04C11DB7, 0xC0F5BAE0},
00140                  {AV_CRC_16_ANSI   , 0x8005,     0x1FBB    },
00141                  {AV_CRC_8_ATM     , 0x07,       0xE3      },};
00142     const AVCRC *ctx;
00143 
00144     for(i=0; i<sizeof(buf); i++)
00145         buf[i]= i+i*i;
00146 
00147     for(i=0; i<4; i++){
00148         ctx = av_crc_get_table(p[i][0]);
00149         printf("crc %08X =%X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
00150     }
00151     return 0;
00152 }
00153 #endif

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1