1 #include "common.h"
2 #include "crc.h"
3 
II_CRC_calc(frame_params * fr_ps,unsigned int (* bit_alloc)[32],unsigned int (* scfsi)[32],unsigned int * crc)4 void II_CRC_calc (frame_params * fr_ps, unsigned int (*bit_alloc)[32],
5 		  unsigned int (*scfsi)[32], unsigned int *crc)
6 {
7   int i, k;
8   layer *info = fr_ps->header;
9   int stereo = fr_ps->stereo;
10   int sblimit = fr_ps->sblimit;
11   int jsbound = fr_ps->jsbound;
12   al_table *alloc = fr_ps->alloc;
13 
14   *crc = 0xffff;		/* changed from '0' 92-08-11 shn */
15   update_CRC (info->bitrate_index, 4, crc);
16   update_CRC (info->sampling_frequency, 2, crc);
17   update_CRC (info->padding, 1, crc);
18   update_CRC (info->extension, 1, crc);
19   update_CRC (info->mode, 2, crc);
20   update_CRC (info->mode_ext, 2, crc);
21   update_CRC (info->copyright, 1, crc);
22   update_CRC (info->original, 1, crc);
23   update_CRC (info->emphasis, 2, crc);
24 
25   for (i = 0; i < sblimit; i++)
26     for (k = 0; k < ((i < jsbound) ? stereo : 1); k++)
27       update_CRC (bit_alloc[k][i], (*alloc)[i][0].bits, crc);
28 
29   for (i = 0; i < sblimit; i++)
30     for (k = 0; k < stereo; k++)
31       if (bit_alloc[k][i])
32 	update_CRC (scfsi[k][i], 2, crc);
33 }
34 
II_CRC_calc_mc(frame_params * fr_ps,unsigned int (* bit_alloc)[32],unsigned int lfe_alloc,unsigned int (* scfsi)[32],unsigned int * crc)35 void II_CRC_calc_mc (frame_params * fr_ps, unsigned int (*bit_alloc)[32],
36 		     unsigned int lfe_alloc, unsigned int (*scfsi)[32],
37 		     unsigned int *crc)
38 {
39   int i, k, m, l;
40   layer *info = fr_ps->header;
41   //int nr_tc = fr_ps->stereo + fr_ps->stereomc;
42   int center = info->center;
43   int surround = info->surround;
44   int sblimit = fr_ps->sblimit_mc;
45   al_table *alloc = fr_ps->alloc_mc;
46 
47   *crc = 0xffff;		/* changed from '0' 92-08-11 shn */
48   update_CRC (info->ext_bit_stream_present, 1, crc);
49   if (info->ext_bit_stream_present == 1)
50     update_CRC (info->n_ad_bytes, 8, crc);
51   update_CRC (center, 2, crc);
52   update_CRC (surround, 2, crc);
53   update_CRC (info->lfe, 1, crc);
54   update_CRC (info->audio_mix, 1, crc);
55   update_CRC (info->matrix, 2, crc);
56   update_CRC (info->multiling_ch, 3, crc);
57   update_CRC (info->multiling_fs, 1, crc);
58   update_CRC (info->multiling_lay, 1, crc);
59   update_CRC (info->copy_ident_bit, 1, crc);
60   update_CRC (info->copy_ident_start, 1, crc);
61 
62   update_CRC (info->tc_sbgr_select, 1, crc);
63   update_CRC (info->dyn_cross_on, 1, crc);
64   update_CRC (info->mc_prediction_on, 1, crc);
65   /* 960627 FdB tca bits dependent on configuration */
66   if ((center == 1 || center == 3) && (surround == 1 || surround == 2)) {
67     /* 3 bits for tca's */
68     if (info->tc_sbgr_select == 1)
69       update_CRC (info->tc_allocation, 3, crc);
70     else
71       for (i = 0; i < 12; i++)
72 	update_CRC (info->tc_alloc[i], 3, crc);
73   } else if (center == 1 || center == 3 || surround == 1 || surround == 2) {
74     /* 2 bits for tca's */
75     if (info->tc_sbgr_select == 1)
76       update_CRC (info->tc_allocation, 2, crc);
77     else
78       for (i = 0; i < 12; i++)
79 	update_CRC (info->tc_alloc[i], 2, crc);
80   }
81   if (info->dyn_cross_on == 1) {
82     update_CRC (info->dyn_cross_LR, 1, crc);
83     for (i = 0; i < 12; i++) {
84       /* 960627 FdB DynX bits dependent on configuration */
85       if ((center == 1 || center == 3) && surround == 2)
86 	/* 3/2 */
87 	update_CRC (info->dyn_cross[i], 4, crc);
88       else if (((center == 1 || center == 3) && surround == 1) ||
89 	       (center == 0 && surround == 2))
90 	/* 3/1 and 2/2 */
91 	update_CRC (info->dyn_cross[i], 3, crc);
92       else if (center == 1 || center == 3 || surround == 1)
93 	/* 3/0 (+2/0) and 2/1 */
94 	update_CRC (info->dyn_cross[i], 1, crc);
95       if (surround == 3)
96 	update_CRC (info->dyn_second_stereo[i], 1, crc);
97     }
98   }
99 
100   if (info->mc_prediction_on == 1) {
101     for (i = 0; i < 8; i++) {
102       update_CRC (info->mc_pred[i], 1, crc);
103       if (info->mc_pred[i] == 1)
104 	for (k = 0; k < n_pred_coef[info->dyn_cross[i]]; k++)
105 	  update_CRC (info->predsi[i][k], 2, crc);
106     }
107   }
108 
109   if (fr_ps->header->lfe == 1)
110     update_CRC (lfe_alloc, (*alloc)[0][0].bits, crc);
111 
112   for (i = 0; i < sblimit; i++) {
113     l = sbgrp[i];
114 
115     for (m = fr_ps->stereo; m < fr_ps->stereo + fr_ps->stereomc; m++) {
116       k = transmission_channel (fr_ps, l, m);
117 
118       if ((i < 12) || (k != 2) || (fr_ps->header->center != 3)) {
119 	/* 960627 FdB DynX dependent on configuration */
120 	if (dyn_ch (fr_ps, l, m) == 1)
121 	  update_CRC (bit_alloc[k][i], (*alloc)[i][0].bits, crc);
122       }
123     }
124   }
125 
126   for (i = 0; i < sblimit; i++) {
127     l = sbgrp[i];
128 
129     for (m = fr_ps->stereo; m < fr_ps->stereo + fr_ps->stereomc; m++) {
130       k = transmission_channel (fr_ps, l, m);
131 
132       if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
133 	update_CRC (scfsi[k][i], 2, crc);
134     }
135   }
136 }
137 
II_CRC_calc_ext(frame_params * fr_ps,unsigned int * z,unsigned int * crc)138 void II_CRC_calc_ext (frame_params * fr_ps, unsigned int *z, unsigned int *crc)
139 {
140   int i;
141 
142   layer *info = fr_ps->header;
143   *crc = 0xffff;
144   update_CRC (info->ext_length, 11, crc);
145   update_CRC (info->ext_bit, 1, crc);
146   for (i = 0; i < 14; i++) {
147     update_CRC (z[i], 8, crc);
148   }
149   update_CRC (z[14], 4, crc);
150 }
151 
152 
update_CRC(unsigned int data,unsigned int length,unsigned int * crc)153 void update_CRC (unsigned int data, unsigned int length, unsigned int *crc)
154 {
155   unsigned int masking, carry;
156 
157   masking = 1 << length;
158 
159   while (masking >>= 1) {
160     carry = *crc & 0x8000;
161     *crc <<= 1;
162     if (!carry ^ !(data & masking))
163       *crc ^= CRC16_POLYNOMIAL;
164   }
165   *crc &= 0xffff;
166 
167 #ifdef	PrintCRCDebug
168   printf ("crc_len: %2d code: %4x crc: %4x\n", length, data, *crc);
169   fflush (stdout);
170 #endif
171 }
172