1 /*
2  *  TwoLAME: an optimized MPEG Audio Layer Two encoder
3  *
4  *  Copyright (C) 2001-2004 Michael Cheng
5  *  Copyright (C) 2004-2006 The TwoLAME Project
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2.1 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  $Id$
22  *
23  */
24 
25 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "twolame.h"
30 #include "common.h"
31 #include "dab.h"
32 
33 
34 void
dab_crc_calc(twolame_options * glopts,unsigned int bit_alloc[2][SBLIMIT],unsigned int scfsi[2][SBLIMIT],unsigned int scalar[2][3][SBLIMIT],unsigned int * crc,int packed)35 dab_crc_calc(twolame_options * glopts,
36              unsigned int bit_alloc[2][SBLIMIT],
37              unsigned int scfsi[2][SBLIMIT],
38              unsigned int scalar[2][3][SBLIMIT], unsigned int *crc, int packed)
39 {
40     int i, j, k;
41     int nch = glopts->num_channels_out;
42     int nb_scalar;
43     int f[5] = { 0, 4, 8, 16, 30 };
44     int first, last;
45 
46     first = f[packed];
47     last = f[packed + 1];
48     if (last > glopts->sblimit)
49         last = glopts->sblimit;
50 
51     nb_scalar = 0;
52     *crc = 0x0;
53     for (i = first; i < last; i++)
54         for (k = 0; k < nch; k++)
55             if (bit_alloc[k][i])    /* above jsbound, bit_alloc[0][i] == ba[1][i] */
56                 switch (scfsi[k][i]) {
57                 case 0:
58                     for (j = 0; j < 3; j++) {
59                         nb_scalar++;
60                         dab_crc_update(scalar[k][j][i] >> 3, 3, crc);
61                     }
62                     break;
63                 case 1:
64                 case 3:
65                     nb_scalar += 2;
66                     dab_crc_update(scalar[k][0][i] >> 3, 3, crc);
67                     dab_crc_update(scalar[k][2][i] >> 3, 3, crc);
68                     break;
69                 case 2:
70                     nb_scalar++;
71                     dab_crc_update(scalar[k][0][i] >> 3, 3, crc);
72                 }
73 }
74 
dab_crc_update(unsigned int data,unsigned int length,unsigned int * crc)75 void dab_crc_update(unsigned int data, unsigned int length, unsigned int *crc)
76 {
77     unsigned int masking, carry;
78 
79     masking = 1 << length;
80 
81     while ((masking >>= 1)) {
82         carry = *crc & 0x80;
83         *crc <<= 1;
84         if (!carry ^ !(data & masking))
85             *crc ^= CRC8_POLYNOMIAL;
86     }
87     *crc &= 0xff;
88 }
89 
90 
91 #ifdef OLD_BROKEN_DAB_STUFF
92 // Leaving these here while DAB is reimplemented properly
93 
94 #define		MINIMUM			4   /* Minimum size of the buffer in bytes */
95 #define		MAX_LENGTH		32  /* Maximum length of word written */
96 
97 
98 /*
99   19 Jul 03
100   DAB stuff got broken in the update to 02m
101   Not sure who uses this. I would like to get it working again eventually.
102 */
103 
104 /*
105   Just after the available bits calculation in twolame,
106   allocated some bits for the DAB stuff
107 */
108 
109 if (header.dab_extension) {
110     /* in 24 kHz we always have 4 bytes */
111     if (header.sampling_frequency == 1)
112         header.dab_extension = 4;
113     /* You must have one frame in memory if you are in DAB mode */
114     /* in conformity of the norme ETS 300 401 http://www.etsi.org */
115     /* see bitstream.c */
116     if (frameNum == 1)
117         minimum = lg_frame + MINIMUM;
118     adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
119 }
120 
121 
122 /*
123    Just after we finishing padding up to the number of ADB
124    Put the extension stuff in (ancillary data, right at the end)
125 */
126 if (header.dab_extension) {
127     /* Reserve some bytes for X-PAD in DAB mode */
128     putbits(mybs, 0, header.dab_length * 8);
129 
130     for (i = header.dab_extension - 1; i >= 0; i--) {
131         CRC_calcDAB(&frame, bit_alloc, scfsi, scalar, &crc, i);
132         /* this crc is for the previous frame in DAB mode */
133         if (mybs->buf_byte_idx + lg_frame < mybs->buf_size)
134             mybs->buf[mybs->buf_byte_idx + lg_frame] = crc;
135         /* reserved 2 bytes for F-PAD in DAB mode */
136         putbits(mybs, crc, 8);
137     }
138     putbits(mybs, 0, 16);
139 }
140 #endif
141 
142 
143 // vim:ts=4:sw=4:nowrap:
144