00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 # ifdef HAVE_CONFIG_H
00023 # include "config.h"
00024 # endif
00025
00026 # include "global.h"
00027
00028 # ifdef HAVE_LIMITS_H
00029 # include <limits.h>
00030 # else
00031 # define CHAR_BIT 8
00032 # endif
00033
00034 # include "fixed.h"
00035 # include "bit.h"
00036 # include "stream.h"
00037 # include "frame.h"
00038 # include "layer12.h"
00039
00040
00041
00042
00043
00044 static
00045 mad_fixed_t const sf_table[64] = {
00046 # include "sf_table.dat"
00047 };
00048
00049
00050
00051
00052 static
00053 mad_fixed_t const linear_table[14] = {
00054 MAD_F(0x15555555),
00055 MAD_F(0x12492492),
00056 MAD_F(0x11111111),
00057 MAD_F(0x10842108),
00058 MAD_F(0x10410410),
00059 MAD_F(0x10204081),
00060 MAD_F(0x10101010),
00061 MAD_F(0x10080402),
00062 MAD_F(0x10040100),
00063 MAD_F(0x10020040),
00064 MAD_F(0x10010010),
00065 MAD_F(0x10008004),
00066 MAD_F(0x10004001),
00067 MAD_F(0x10002000)
00068 };
00069
00070
00071
00072
00073
00074 static
00075 mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
00076 {
00077 mad_fixed_t sample;
00078
00079 sample = mad_bit_read(ptr, nb);
00080
00081
00082
00083 sample ^= 1 << (nb - 1);
00084 sample |= -(sample & (1 << (nb - 1)));
00085
00086 sample <<= MAD_F_FRACBITS - (nb - 1);
00087
00088
00089
00090
00091
00092 sample += MAD_F_ONE >> (nb - 1);
00093
00094 return mad_f_mul(sample, linear_table[nb - 2]);
00095
00096
00097
00098 }
00099
00100
00101
00102
00103
00104 int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
00105 {
00106 struct mad_header *header = &frame->header;
00107 unsigned int nch, bound, ch, s, sb, nb;
00108 unsigned char allocation[2][32], scalefactor[2][32];
00109
00110 nch = MAD_NCHANNELS(header);
00111
00112 bound = 32;
00113 if (header->mode == MAD_MODE_JOINT_STEREO) {
00114 header->flags |= MAD_FLAG_I_STEREO;
00115 bound = 4 + header->mode_extension * 4;
00116 }
00117
00118
00119
00120 if (header->flags & MAD_FLAG_PROTECTION) {
00121 header->crc_check =
00122 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
00123 header->crc_check);
00124
00125 if (header->crc_check != header->crc_target &&
00126 !(frame->options & MAD_OPTION_IGNORECRC)) {
00127 stream->error = MAD_ERROR_BADCRC;
00128 return -1;
00129 }
00130 }
00131
00132
00133
00134 for (sb = 0; sb < bound; ++sb) {
00135 for (ch = 0; ch < nch; ++ch) {
00136 nb = mad_bit_read(&stream->ptr, 4);
00137
00138 if (nb == 15) {
00139 stream->error = MAD_ERROR_BADBITALLOC;
00140 return -1;
00141 }
00142
00143 allocation[ch][sb] = nb ? nb + 1 : 0;
00144 }
00145 }
00146
00147 for (sb = bound; sb < 32; ++sb) {
00148 nb = mad_bit_read(&stream->ptr, 4);
00149
00150 if (nb == 15) {
00151 stream->error = MAD_ERROR_BADBITALLOC;
00152 return -1;
00153 }
00154
00155 allocation[0][sb] =
00156 allocation[1][sb] = nb ? nb + 1 : 0;
00157 }
00158
00159
00160
00161 for (sb = 0; sb < 32; ++sb) {
00162 for (ch = 0; ch < nch; ++ch) {
00163 if (allocation[ch][sb]) {
00164 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
00165
00166 # if defined(OPT_STRICT)
00167
00168
00169
00170
00171
00172 if (scalefactor[ch][sb] == 63) {
00173 stream->error = MAD_ERROR_BADSCALEFACTOR;
00174 return -1;
00175 }
00176 # endif
00177 }
00178 }
00179 }
00180
00181
00182
00183 for (s = 0; s < 12; ++s) {
00184 for (sb = 0; sb < bound; ++sb) {
00185 for (ch = 0; ch < nch; ++ch) {
00186 nb = allocation[ch][sb];
00187 frame->sbsample[ch][s][sb] = nb ?
00188 mad_f_mul(I_sample(&stream->ptr, nb),
00189 sf_table[scalefactor[ch][sb]]) : 0;
00190 }
00191 }
00192
00193 for (sb = bound; sb < 32; ++sb) {
00194 if ((nb = allocation[0][sb])) {
00195 mad_fixed_t sample;
00196
00197 sample = I_sample(&stream->ptr, nb);
00198
00199 for (ch = 0; ch < nch; ++ch) {
00200 frame->sbsample[ch][s][sb] =
00201 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
00202 }
00203 }
00204 else {
00205 for (ch = 0; ch < nch; ++ch)
00206 frame->sbsample[ch][s][sb] = 0;
00207 }
00208 }
00209 }
00210
00211 return 0;
00212 }
00213
00214
00215
00216
00217 static
00218 struct {
00219 unsigned int sblimit;
00220 unsigned char const offsets[30];
00221 } const sbquant_table[5] = {
00222
00223 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3,
00224 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
00225
00226 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3,
00227 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
00228
00229 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } },
00230
00231 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } },
00232
00233 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
00234 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
00235 };
00236
00237
00238 static
00239 struct {
00240 unsigned short nbal;
00241 unsigned short offset;
00242 } const bitalloc_table[8] = {
00243 { 2, 0 },
00244 { 2, 3 },
00245 { 3, 3 },
00246 { 3, 1 },
00247 { 4, 2 },
00248 { 4, 3 },
00249 { 4, 4 },
00250 { 4, 5 }
00251 };
00252
00253
00254 static
00255 unsigned char const offset_table[6][15] = {
00256 { 0, 1, 16 },
00257 { 0, 1, 2, 3, 4, 5, 16 },
00258 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
00259 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
00260 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 },
00261 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
00262 };
00263
00264
00265 static
00266 struct quantclass {
00267 unsigned short nlevels;
00268 unsigned char group;
00269 unsigned char bits;
00270 mad_fixed_t C;
00271 mad_fixed_t D;
00272 } const qc_table[17] = {
00273 # include "qc_table.dat"
00274 };
00275
00276
00277
00278
00279
00280 static
00281 void II_samples(struct mad_bitptr *ptr,
00282 struct quantclass const *quantclass,
00283 mad_fixed_t output[3])
00284 {
00285 unsigned int nb, s, sample[3];
00286
00287 if ((nb = quantclass->group)) {
00288 unsigned int c, nlevels;
00289
00290
00291 c = mad_bit_read(ptr, quantclass->bits);
00292 nlevels = quantclass->nlevels;
00293
00294 for (s = 0; s < 3; ++s) {
00295 sample[s] = c % nlevels;
00296 c /= nlevels;
00297 }
00298 }
00299 else {
00300 nb = quantclass->bits;
00301
00302 for (s = 0; s < 3; ++s)
00303 sample[s] = mad_bit_read(ptr, nb);
00304 }
00305
00306 for (s = 0; s < 3; ++s) {
00307 mad_fixed_t requantized;
00308
00309
00310
00311 requantized = sample[s] ^ (1 << (nb - 1));
00312 requantized |= -(requantized & (1 << (nb - 1)));
00313
00314 requantized <<= MAD_F_FRACBITS - (nb - 1);
00315
00316
00317
00318
00319
00320 output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C);
00321
00322
00323
00324 }
00325 }
00326
00327
00328
00329
00330
00331 int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
00332 {
00333 struct mad_header *header = &frame->header;
00334 struct mad_bitptr start;
00335 unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb;
00336 unsigned char const *offsets;
00337 unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
00338 mad_fixed_t samples[3];
00339
00340 nch = MAD_NCHANNELS(header);
00341
00342 if (header->flags & MAD_FLAG_LSF_EXT)
00343 index = 4;
00344 else {
00345 switch (nch == 2 ? header->bitrate / 2 : header->bitrate) {
00346 case 32000:
00347 case 48000:
00348 index = (header->samplerate == 32000) ? 3 : 2;
00349 break;
00350
00351 case 56000:
00352 case 64000:
00353 case 80000:
00354 index = 0;
00355 break;
00356
00357 default:
00358 index = (header->samplerate == 48000) ? 0 : 1;
00359 }
00360 }
00361
00362 sblimit = sbquant_table[index].sblimit;
00363 offsets = sbquant_table[index].offsets;
00364
00365 bound = 32;
00366 if (header->mode == MAD_MODE_JOINT_STEREO) {
00367 header->flags |= MAD_FLAG_I_STEREO;
00368 bound = 4 + header->mode_extension * 4;
00369 }
00370
00371 if (bound > sblimit)
00372 bound = sblimit;
00373
00374 start = stream->ptr;
00375
00376
00377
00378 for (sb = 0; sb < bound; ++sb) {
00379 nbal = bitalloc_table[offsets[sb]].nbal;
00380
00381 for (ch = 0; ch < nch; ++ch)
00382 allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
00383 }
00384
00385 for (sb = bound; sb < sblimit; ++sb) {
00386 nbal = bitalloc_table[offsets[sb]].nbal;
00387
00388 allocation[0][sb] =
00389 allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
00390 }
00391
00392
00393
00394 for (sb = 0; sb < sblimit; ++sb) {
00395 for (ch = 0; ch < nch; ++ch) {
00396 if (allocation[ch][sb])
00397 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
00398 }
00399 }
00400
00401
00402
00403 if (header->flags & MAD_FLAG_PROTECTION) {
00404 header->crc_check =
00405 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
00406 header->crc_check);
00407
00408 if (header->crc_check != header->crc_target &&
00409 !(frame->options & MAD_OPTION_IGNORECRC)) {
00410 stream->error = MAD_ERROR_BADCRC;
00411 return -1;
00412 }
00413 }
00414
00415
00416
00417 for (sb = 0; sb < sblimit; ++sb) {
00418 for (ch = 0; ch < nch; ++ch) {
00419 if (allocation[ch][sb]) {
00420 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
00421
00422 switch (scfsi[ch][sb]) {
00423 case 2:
00424 scalefactor[ch][sb][2] =
00425 scalefactor[ch][sb][1] =
00426 scalefactor[ch][sb][0];
00427 break;
00428
00429 case 0:
00430 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
00431
00432
00433 case 1:
00434 case 3:
00435 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
00436 }
00437
00438 if (scfsi[ch][sb] & 1)
00439 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
00440
00441 # if defined(OPT_STRICT)
00442
00443
00444
00445
00446
00447 if (scalefactor[ch][sb][0] == 63 ||
00448 scalefactor[ch][sb][1] == 63 ||
00449 scalefactor[ch][sb][2] == 63) {
00450 stream->error = MAD_ERROR_BADSCALEFACTOR;
00451 return -1;
00452 }
00453 # endif
00454 }
00455 }
00456 }
00457
00458
00459
00460 for (gr = 0; gr < 12; ++gr) {
00461 for (sb = 0; sb < bound; ++sb) {
00462 for (ch = 0; ch < nch; ++ch) {
00463 if ((index = allocation[ch][sb])) {
00464 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
00465
00466 II_samples(&stream->ptr, &qc_table[index], samples);
00467
00468 for (s = 0; s < 3; ++s) {
00469 frame->sbsample[ch][3 * gr + s][sb] =
00470 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
00471 }
00472 }
00473 else {
00474 for (s = 0; s < 3; ++s)
00475 frame->sbsample[ch][3 * gr + s][sb] = 0;
00476 }
00477 }
00478 }
00479
00480 for (sb = bound; sb < sblimit; ++sb) {
00481 if ((index = allocation[0][sb])) {
00482 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
00483
00484 II_samples(&stream->ptr, &qc_table[index], samples);
00485
00486 for (ch = 0; ch < nch; ++ch) {
00487 for (s = 0; s < 3; ++s) {
00488 frame->sbsample[ch][3 * gr + s][sb] =
00489 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
00490 }
00491 }
00492 }
00493 else {
00494 for (ch = 0; ch < nch; ++ch) {
00495 for (s = 0; s < 3; ++s)
00496 frame->sbsample[ch][3 * gr + s][sb] = 0;
00497 }
00498 }
00499 }
00500
00501 for (ch = 0; ch < nch; ++ch) {
00502 for (s = 0; s < 3; ++s) {
00503 for (sb = sblimit; sb < 32; ++sb)
00504 frame->sbsample[ch][3 * gr + s][sb] = 0;
00505 }
00506 }
00507 }
00508
00509 return 0;
00510 }