]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/crc.c
Additional documentation for CRC functions
[frescor/ffmpeg.git] / libavutil / crc.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "common.h"
22 #include "crc.h"
23
24 #if LIBAVUTIL_VERSION_INT  < (50<<16)
25 AVCRC *av_crcEDB88320;
26 AVCRC *av_crc04C11DB7;
27 AVCRC *av_crc8005    ;
28 AVCRC *av_crc07      ;
29 #else
30 AVCRC av_crcEDB88320[257];
31 AVCRC av_crc04C11DB7[257];
32 AVCRC av_crc8005    [257];
33 AVCRC av_crc07      [257];
34 #endif
35
36 /**
37  * Inits a crc table.
38  * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
39  * @param cts_size size of ctx in bytes
40  * @param le if 1, lowest bit represents coefficient for highest exponent
41  *           of corresponding polynomial (both for poly and actual CRC).
42  *           If 0, you must swap the crc parameter and the result of av_crc
43  *           if you need the standard representation (can be simplified in
44  *           most cases to e.g. bswap16):
45  *           bswap_32(crc << (32-bits))
46  * @param bits number of bits for the CRC
47  * @param poly generator polynomial without the x**bits coefficient, in the
48  *             representation as specified by le
49  * @return <0 on failure
50  */
51 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
52     int i, j;
53     uint32_t c;
54
55     if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
56         return -1;
57     if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
58         return -1;
59
60     for (i = 0; i < 256; i++) {
61         if (le) {
62             for (c = i, j = 0; j < 8; j++)
63                 c = (c>>1)^(poly & (-(c&1)));
64             ctx[i] = c;
65         } else {
66             for (c = i << 24, j = 0; j < 8; j++)
67                 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
68             ctx[i] = bswap_32(c);
69         }
70     }
71     ctx[256]=1;
72 #ifndef CONFIG_SMALL
73     if(ctx_size >= sizeof(AVCRC)*1024)
74         for (i = 0; i < 256; i++)
75             for(j=0; j<3; j++)
76                 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
77 #endif
78
79     return 0;
80 }
81
82 /**
83  * Calculate the CRC of a block
84  * @param crc CRC of previous blocks if any or initial value for CRC.
85  * @return CRC updated with the data from the given block
86  *
87  * @see av_crc_init() "le" parameter
88  */
89 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
90     const uint8_t *end= buffer+length;
91
92 #ifndef CONFIG_SMALL
93     if(!ctx[256])
94         while(buffer<end-3){
95             crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
96             crc =  ctx[3*256 + ( crc     &0xFF)]
97                   ^ctx[2*256 + ((crc>>8 )&0xFF)]
98                   ^ctx[1*256 + ((crc>>16)&0xFF)]
99                   ^ctx[0*256 + ((crc>>24)     )];
100         }
101 #endif
102     while(buffer<end)
103         crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
104
105     return crc;
106 }
107
108 #ifdef TEST
109 #undef printf
110 main(){
111     uint8_t buf[1999];
112     int i;
113     int p[4][4]={{1, 32, AV_CRC_32_IEEE_LE, 0x3D5CDD04},
114                  {0, 32, AV_CRC_32_IEEE   , 0xC0F5BAE0},
115                  {0, 16, AV_CRC_16        , 0x1FBB    },
116                  {0,  8, AV_CRC_8_ATM     , 0xE3      },};
117     AVCRC ctx[1 ? 1024:257];
118
119     for(i=0; i<sizeof(buf); i++)
120         buf[i]= i+i*i;
121
122     for(i=0; i<4; i++){
123         av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
124         printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));
125     }
126 }
127 #endif