1 /******************************************************************************
2  *
3  *  Copyright 2014 The Android Open Source Project
4  *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights
5  *                        reserved.
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at:
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  ******************************************************************************/
20 
21 /*******************************************************************************
22   $Revision: #1 $
23  ******************************************************************************/
24 
25 /**
26 @file
27 Checksum and header-related functions.
28 
29 @ingroup codec_internal
30 */
31 
32 /**
33 @addtogroup codec_internal
34 @{
35 */
36 
37 #include "oi_assert.h"
38 #include "oi_codec_sbc_private.h"
39 
40 /* asdasd */
41 
42 #define USE_NIBBLEWISE_CRC
43 
44 /* #define PRINT_SAMPLES */
45 /* #define PRINT_SCALEFACTORS */
46 /* #define DEBUG_CRC */
47 
48 /*
49  * CRC-8 table for X^8 + X^4 + X^3 + X^2 + 1; byte-wise lookup
50  */
51 #ifdef USE_WIDE_CRC
52 /* Save space if a char is 16 bits, such as on the C54x */
53 const OI_BYTE crc8_wide[128] = {
54     0x001d, 0x3a27, 0x7469, 0x4e53, 0xe8f5, 0xd2cf, 0x9c81, 0xa6bb, 0xcdd0,
55     0xf7ea, 0xb9a4, 0x839e, 0x2538, 0x1f02, 0x514c, 0x6b76, 0x879a, 0xbda0,
56     0xf3ee, 0xc9d4, 0x6f72, 0x5548, 0x1b06, 0x213c, 0x4a57, 0x706d, 0x3e23,
57     0x0419, 0xa2bf, 0x9885, 0xd6cb, 0xecf1, 0x130e, 0x2934, 0x677a, 0x5d40,
58     0xfbe6, 0xc1dc, 0x8f92, 0xb5a8, 0xdec3, 0xe4f9, 0xaab7, 0x908d, 0x362b,
59     0x0c11, 0x425f, 0x7865, 0x9489, 0xaeb3, 0xe0fd, 0xdac7, 0x7c61, 0x465b,
60     0x0815, 0x322f, 0x5944, 0x637e, 0x2d30, 0x170a, 0xb1ac, 0x8b96, 0xc5d8,
61     0xffe2, 0x263b, 0x1c01, 0x524f, 0x6875, 0xced3, 0xf4e9, 0xbaa7, 0x809d,
62     0xebf6, 0xd1cc, 0x9f82, 0xa5b8, 0x031e, 0x3924, 0x776a, 0x4d50, 0xa1bc,
63     0x9b86, 0xd5c8, 0xeff2, 0x4954, 0x736e, 0x3d20, 0x071a, 0x6c71, 0x564b,
64     0x1805, 0x223f, 0x8499, 0xbea3, 0xf0ed, 0xcad7, 0x3528, 0x0f12, 0x415c,
65     0x7b66, 0xddc0, 0xe7fa, 0xa9b4, 0x938e, 0xf8e5, 0xc2df, 0x8c91, 0xb6ab,
66     0x100d, 0x2a37, 0x6479, 0x5e43, 0xb2af, 0x8895, 0xc6db, 0xfce1, 0x5a47,
67     0x607d, 0x2e33, 0x1409, 0x7f62, 0x4558, 0x0b16, 0x312c, 0x978a, 0xadb0,
68     0xe3fe, 0xd9c4,
69 };
70 #elif defined(USE_NIBBLEWISE_CRC)
71 const OI_BYTE crc8_narrow[16] = {0x00, 0x1d, 0x3a, 0x27, 0x74, 0x69,
72                                  0x4e, 0x53, 0xe8, 0xf5, 0xd2, 0xcf,
73                                  0x9c, 0x81, 0xa6, 0xbb};
74 #else
75 const OI_BYTE crc8_narrow[256] = {
76     0x00, 0x1d, 0x3a, 0x27, 0x74, 0x69, 0x4e, 0x53, 0xe8, 0xf5, 0xd2, 0xcf,
77     0x9c, 0x81, 0xa6, 0xbb, 0xcd, 0xd0, 0xf7, 0xea, 0xb9, 0xa4, 0x83, 0x9e,
78     0x25, 0x38, 0x1f, 0x02, 0x51, 0x4c, 0x6b, 0x76, 0x87, 0x9a, 0xbd, 0xa0,
79     0xf3, 0xee, 0xc9, 0xd4, 0x6f, 0x72, 0x55, 0x48, 0x1b, 0x06, 0x21, 0x3c,
80     0x4a, 0x57, 0x70, 0x6d, 0x3e, 0x23, 0x04, 0x19, 0xa2, 0xbf, 0x98, 0x85,
81     0xd6, 0xcb, 0xec, 0xf1, 0x13, 0x0e, 0x29, 0x34, 0x67, 0x7a, 0x5d, 0x40,
82     0xfb, 0xe6, 0xc1, 0xdc, 0x8f, 0x92, 0xb5, 0xa8, 0xde, 0xc3, 0xe4, 0xf9,
83     0xaa, 0xb7, 0x90, 0x8d, 0x36, 0x2b, 0x0c, 0x11, 0x42, 0x5f, 0x78, 0x65,
84     0x94, 0x89, 0xae, 0xb3, 0xe0, 0xfd, 0xda, 0xc7, 0x7c, 0x61, 0x46, 0x5b,
85     0x08, 0x15, 0x32, 0x2f, 0x59, 0x44, 0x63, 0x7e, 0x2d, 0x30, 0x17, 0x0a,
86     0xb1, 0xac, 0x8b, 0x96, 0xc5, 0xd8, 0xff, 0xe2, 0x26, 0x3b, 0x1c, 0x01,
87     0x52, 0x4f, 0x68, 0x75, 0xce, 0xd3, 0xf4, 0xe9, 0xba, 0xa7, 0x80, 0x9d,
88     0xeb, 0xf6, 0xd1, 0xcc, 0x9f, 0x82, 0xa5, 0xb8, 0x03, 0x1e, 0x39, 0x24,
89     0x77, 0x6a, 0x4d, 0x50, 0xa1, 0xbc, 0x9b, 0x86, 0xd5, 0xc8, 0xef, 0xf2,
90     0x49, 0x54, 0x73, 0x6e, 0x3d, 0x20, 0x07, 0x1a, 0x6c, 0x71, 0x56, 0x4b,
91     0x18, 0x05, 0x22, 0x3f, 0x84, 0x99, 0xbe, 0xa3, 0xf0, 0xed, 0xca, 0xd7,
92     0x35, 0x28, 0x0f, 0x12, 0x41, 0x5c, 0x7b, 0x66, 0xdd, 0xc0, 0xe7, 0xfa,
93     0xa9, 0xb4, 0x93, 0x8e, 0xf8, 0xe5, 0xc2, 0xdf, 0x8c, 0x91, 0xb6, 0xab,
94     0x10, 0x0d, 0x2a, 0x37, 0x64, 0x79, 0x5e, 0x43, 0xb2, 0xaf, 0x88, 0x95,
95     0xc6, 0xdb, 0xfc, 0xe1, 0x5a, 0x47, 0x60, 0x7d, 0x2e, 0x33, 0x14, 0x09,
96     0x7f, 0x62, 0x45, 0x58, 0x0b, 0x16, 0x31, 0x2c, 0x97, 0x8a, 0xad, 0xb0,
97     0xe3, 0xfe, 0xd9, 0xc4};
98 #endif
99 
100 #if defined(OI_DEBUG) || defined(PRINT_SAMPLES) || defined(PRINT_SCALEFACTORS)
101 #endif
102 
103 #ifdef USE_WIDE_CRC
crc_iterate(uint8_t oldcrc,uint8_t next)104 INLINE OI_CHAR crc_iterate(uint8_t oldcrc, uint8_t next) {
105   OI_UINT crc;
106   OI_UINT idx;
107   idx = oldcrc ^ next;
108   crc = crc8_wide[idx >> 1];
109   if (idx % 2) {
110     crc &= 0xff;
111   } else {
112     crc >>= 8;
113   }
114 
115   return crc;
116 }
117 
crc_iterate_top4(uint8_t oldcrc,uint8_t next)118 INLINE OI_CHAR crc_iterate_top4(uint8_t oldcrc, uint8_t next) {
119   OI_UINT crc;
120   OI_UINT idx;
121   idx = (oldcrc ^ next) >> 4;
122   crc = crc8_wide[idx >> 1];
123   if (idx % 2) {
124     crc &= 0xff;
125   } else {
126     crc >>= 8;
127   }
128 
129   return (oldcrc << 4) ^ crc;
130 }
131 
132 #else  // USE_WIDE_CRC
133 
crc_iterate_top4(uint8_t oldcrc,uint8_t next)134 INLINE uint8_t crc_iterate_top4(uint8_t oldcrc, uint8_t next) {
135   return (oldcrc << 4) ^ crc8_narrow[(oldcrc ^ next) >> 4];
136 }
137 
138 #ifdef USE_NIBBLEWISE_CRC
crc_iterate(uint8_t crc,uint8_t next)139 INLINE uint8_t crc_iterate(uint8_t crc, uint8_t next) {
140   crc = (crc << 4) ^ crc8_narrow[(crc ^ next) >> 4];
141   crc = (crc << 4) ^ crc8_narrow[((crc >> 4) ^ next) & 0xf];
142 
143   return crc;
144 }
145 
146 #else  // USE_NIBBLEWISE_CRC
crc_iterate(uint8_t crc,uint8_t next)147 INLINE uint8_t crc_iterate(uint8_t crc, uint8_t next) {
148   return crc8_narrow[crc ^ next];
149 }
150 
151 #endif  // USE_NIBBLEWISE_CRC
152 
153 #endif  // USE_WIDE_CRC
154 
OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO * frame,OI_BYTE const * data)155 PRIVATE uint8_t OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO* frame,
156                                          OI_BYTE const* data) {
157   OI_UINT i;
158   uint8_t crc = 0x0f;
159   /* Count is the number of whole bytes subject to CRC. Actually, it's one
160    * more than this number, because data[3] is the CRC field itself, which is
161    * explicitly skipped. Since crc_iterate (should be) inlined, it's cheaper
162    * spacewise to include the check in the loop. This shouldn't be much of a
163    * bottleneck routine in the first place. */
164   OI_UINT count = (frame->nrof_subbands * frame->nrof_channels / 2u) + 4;
165 
166   if (frame->mode == SBC_JOINT_STEREO && frame->nrof_subbands == 8) {
167     count++;
168   }
169 
170   for (i = 1; i < count; i++) {
171     if (i != 3) {
172       crc = crc_iterate(crc, data[i]);
173     }
174   }
175 
176   if (frame->mode == SBC_JOINT_STEREO && frame->nrof_subbands == 4) {
177     crc = crc_iterate_top4(crc, data[i]);
178   }
179 
180   return crc;
181 }
182 
OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO * frame)183 void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO* frame) {
184   frame->nrof_blocks = block_values[frame->blocks];
185   frame->nrof_subbands = band_values[frame->subbands];
186 
187   frame->frequency = freq_values[frame->freqIndex];
188   frame->nrof_channels = channel_values[frame->mode];
189 }
190 
191 /**
192  * Unrolled macro to copy 4 32-bit aligned 32-bit values backward in memory
193  */
194 #define COPY4WORDS_BACK(_dest, _src) \
195   do {                               \
196     int32_t _a, _b, _c, _d;          \
197     _a = *--(_src);                  \
198     _b = *--(_src);                  \
199     _c = *--(_src);                  \
200     _d = *--(_src);                  \
201     *--(_dest) = _a;                 \
202     *--(_dest) = _b;                 \
203     *--(_dest) = _c;                 \
204     *--(_dest) = _d;                 \
205   } while (0)
206 
207 #if defined(USE_PLATFORM_MEMMOVE) || defined(USE_PLATFORM_MEMCPY)
208 #include <string.h>
209 #endif
shift_buffer(SBC_BUFFER_T * dest,SBC_BUFFER_T * src,OI_UINT wordCount)210 PRIVATE void shift_buffer(SBC_BUFFER_T* dest, SBC_BUFFER_T* src,
211                           OI_UINT wordCount) {
212 #ifdef USE_PLATFORM_MEMMOVE
213   memmove(dest, src, wordCount * sizeof(SBC_BUFFER_T));
214 #elif defined(USE_PLATFORM_MEMCPY)
215   OI_ASSERT(((OI_CHAR*)(dest) - (OI_CHAR*)(src)) >= wordCount * sizeof(*dest));
216   memcpy(dest, src, wordCount * sizeof(SBC_BUFFER_T));
217 #else
218   OI_UINT n;
219   int32_t* d;
220   int32_t* s;
221   n = wordCount / 4 / (sizeof(int32_t) / sizeof(*dest));
222   OI_ASSERT((n * 4 * (sizeof(int32_t) / sizeof(*dest))) == wordCount);
223 
224   d = (void*)(dest + wordCount);
225   s = (void*)(src + wordCount);
226 
227   do {
228     COPY4WORDS_BACK(d, s);
229   } while (--n);
230 #endif
231 }
232 /**
233 @}
234 */
235