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
)
16 frame_header
*header
= frame
->header
;
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
++)
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;
52 if (!carry
^ !(data
& masking
))
53 *crc
^= CRC16_POLYNOMIAL
;
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
,
68 int f
[5] = { 0, 4, 8, 16, 30 };
73 if (last
> frame
->sblimit
)
74 last
= frame
->sblimit
;
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
]) {
83 for (j
= 0; j
< 3; j
++) {
85 update_CRCDAB (scalar
[k
][j
][i
] >> 3, 3, crc
);
91 update_CRCDAB (scalar
[k
][0][i
] >> 3, 3, crc
);
92 update_CRCDAB (scalar
[k
][2][i
] >> 3, 3, crc
);
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)) {
109 if (!carry
^ !(data
& masking
))
110 *crc
^= CRC8_POLYNOMIAL
;