r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / toolame-02l / crc.c
blobb83ac039b8281c5720931d24043d18369ab0bb9c
1 #include <stdio.h>
2 #include <string.h>
3 #include "common.h"
4 #include "crc.h"
6 /*****************************************************************************
8 * CRC error protection package
10 *****************************************************************************/
12 void CRC_calc (frame_info * frame, unsigned int bit_alloc[2][SBLIMIT],
13 unsigned int scfsi[2][SBLIMIT], unsigned int *crc)
15 int i, k;
16 frame_header *header = frame->header;
17 int nch = frame->nch;
18 int sblimit = frame->sblimit;
19 int jsbound = frame->jsbound;
20 al_table *alloc = frame->alloc;
22 *crc = 0xffff; /* changed from '0' 92-08-11 shn */
23 update_CRC (header->bitrate_index, 4, crc);
24 update_CRC (header->sampling_frequency, 2, crc);
25 update_CRC (header->padding, 1, crc);
26 update_CRC (header->extension, 1, crc);
27 update_CRC (header->mode, 2, crc);
28 update_CRC (header->mode_ext, 2, crc);
29 update_CRC (header->copyright, 1, crc);
30 update_CRC (header->original, 1, crc);
31 update_CRC (header->emphasis, 2, crc);
33 for (i = 0; i < sblimit; i++)
34 for (k = 0; k < ((i < jsbound) ? nch : 1); k++)
35 update_CRC (bit_alloc[k][i], (*alloc)[i][0].bits, crc);
37 for (i = 0; i < sblimit; i++)
38 for (k = 0; k < nch; k++)
39 if (bit_alloc[k][i])
40 update_CRC (scfsi[k][i], 2, crc);
43 void update_CRC (unsigned int data, unsigned int length, unsigned int *crc)
45 unsigned int masking, carry;
47 masking = 1 << length;
49 while ((masking >>= 1)) {
50 carry = *crc & 0x8000;
51 *crc <<= 1;
52 if (!carry ^ !(data & masking))
53 *crc ^= CRC16_POLYNOMIAL;
55 *crc &= 0xffff;
58 void
59 CRC_calcDAB (frame_info * frame,
60 unsigned int bit_alloc[2][SBLIMIT],
61 unsigned int scfsi[2][SBLIMIT],
62 unsigned int scalar[2][3][SBLIMIT], unsigned int *crc,
63 int packed)
65 int i, j, k;
66 int nch = frame->nch;
67 int nb_scalar;
68 int f[5] = { 0, 4, 8, 16, 30 };
69 int first, last;
71 first = f[packed];
72 last = f[packed + 1];
73 if (last > frame->sblimit)
74 last = frame->sblimit;
76 nb_scalar = 0;
77 *crc = 0x0;
78 for (i = first; i < last; i++)
79 for (k = 0; k < nch; k++)
80 if (bit_alloc[k][i]) /* above jsbound, bit_alloc[0][i] == ba[1][i] */
81 switch (scfsi[k][i]) {
82 case 0:
83 for (j = 0; j < 3; j++) {
84 nb_scalar++;
85 update_CRCDAB (scalar[k][j][i] >> 3, 3, crc);
87 break;
88 case 1:
89 case 3:
90 nb_scalar += 2;
91 update_CRCDAB (scalar[k][0][i] >> 3, 3, crc);
92 update_CRCDAB (scalar[k][2][i] >> 3, 3, crc);
93 break;
94 case 2:
95 nb_scalar++;
96 update_CRCDAB (scalar[k][0][i] >> 3, 3, crc);
100 void update_CRCDAB (unsigned int data, unsigned int length, unsigned int *crc)
102 unsigned int masking, carry;
104 masking = 1 << length;
106 while ((masking >>= 1)) {
107 carry = *crc & 0x80;
108 *crc <<= 1;
109 if (!carry ^ !(data & masking))
110 *crc ^= CRC8_POLYNOMIAL;
112 *crc &= 0xff;