annotate crc.c @ 408:0ce74d34d02e libavutil

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