File: crc.c

package info (click to toggle)
toolame 02i-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 604 kB
  • ctags: 558
  • sloc: ansic: 12,338; sh: 111; makefile: 96
file content (118 lines) | stat: -rw-r--r-- 2,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "common.h"

/*****************************************************************************
*
*  CRC error protection package
*
*****************************************************************************/

void
CRC_calc (frame_params * fr_ps,
	  unsigned int bit_alloc[2][SBLIMIT],
	  unsigned int scfsi[2][SBLIMIT], unsigned int *crc)
{
  int i, k;
  layer *info = fr_ps->header;
  int stereo = fr_ps->stereo;
  int sblimit = fr_ps->sblimit;
  int jsbound = fr_ps->jsbound;
  al_table *alloc = fr_ps->alloc;

  *crc = 0xffff;		/* changed from '0' 92-08-11 shn */
  update_CRC (info->bitrate_index, 4, crc);
  update_CRC (info->sampling_frequency, 2, crc);
  update_CRC (info->padding, 1, crc);
  update_CRC (info->extension, 1, crc);
  update_CRC (info->mode, 2, crc);
  update_CRC (info->mode_ext, 2, crc);
  update_CRC (info->copyright, 1, crc);
  update_CRC (info->original, 1, crc);
  update_CRC (info->emphasis, 2, crc);

  for (i = 0; i < sblimit; i++)
    for (k = 0; k < ((i < jsbound) ? stereo : 1); k++)
      update_CRC (bit_alloc[k][i], (*alloc)[i][0].bits, crc);

  for (i = 0; i < sblimit; i++)
    for (k = 0; k < stereo; k++)
      if (bit_alloc[k][i])
	update_CRC (scfsi[k][i], 2, crc);
}

void
update_CRC (unsigned int data, unsigned int length, unsigned int *crc)
{
  unsigned int masking, carry;

  masking = 1 << length;

  while ((masking >>= 1))
    {
      carry = *crc & 0x8000;
      *crc <<= 1;
      if (!carry ^ !(data & masking))
	*crc ^= CRC16_POLYNOMIAL;
    }
  *crc &= 0xffff;
}

void
CRC_calcDAB (frame_params * fr_ps,
	     unsigned int bit_alloc[2][SBLIMIT],
	     unsigned int scfsi[2][SBLIMIT],
	     unsigned int scalar[2][3][SBLIMIT],
	     unsigned int *crc, int packed)
{
  int i, j, k;
  int stereo = fr_ps->stereo;
  int nb_scalar;
  int f[5] = { 0, 4, 8, 16, 30 };
  int first, last;

  first = f[packed];
  last = f[packed + 1];
  if (last > fr_ps->sblimit)
    last = fr_ps->sblimit;

  nb_scalar = 0;
  *crc = 0x0;
  for (i = first; i < last; i++)
    for (k = 0; k < stereo; k++)
      if (bit_alloc[k][i])	/* above jsbound, bit_alloc[0][i] == ba[1][i] */
	switch (scfsi[k][i])
	  {
	  case 0:
	    for (j = 0; j < 3; j++)
	      {
		nb_scalar++;
		update_CRCDAB (scalar[k][j][i] >> 3, 3, crc);
	      }
	    break;
	  case 1:
	  case 3:
	    nb_scalar += 2;
	    update_CRCDAB (scalar[k][0][i] >> 3, 3, crc);
	    update_CRCDAB (scalar[k][2][i] >> 3, 3, crc);
	    break;
	  case 2:
	    nb_scalar++;
	    update_CRCDAB (scalar[k][0][i] >> 3, 3, crc);
	  }
}

void
update_CRCDAB (unsigned int data, unsigned int length, unsigned int *crc)
{
  unsigned int masking, carry;

  masking = 1 << length;

  while ((masking >>= 1))
    {
      carry = *crc & 0x80;
      *crc <<= 1;
      if (!carry ^ !(data & masking))
	*crc ^= CRC8_POLYNOMIAL;
    }
  *crc &= 0xff;
}