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