00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00035 #include <math.h>
00036 #include <stddef.h>
00037 #include <stdio.h>
00038
00039 #include "libavutil/float_dsp.h"
00040 #include "libavutil/libm.h"
00041 #include "avcodec.h"
00042 #include "bytestream.h"
00043 #include "fft.h"
00044 #include "fmtconvert.h"
00045 #include "get_bits.h"
00046 #include "internal.h"
00047
00048 #include "atrac.h"
00049 #include "atrac3data.h"
00050
00051 #define JOINT_STEREO 0x12
00052 #define STEREO 0x2
00053
00054 #define SAMPLES_PER_FRAME 1024
00055 #define MDCT_SIZE 512
00056
00057 typedef struct GainInfo {
00058 int num_gain_data;
00059 int lev_code[8];
00060 int loc_code[8];
00061 } GainInfo;
00062
00063 typedef struct GainBlock {
00064 GainInfo g_block[4];
00065 } GainBlock;
00066
00067 typedef struct TonalComponent {
00068 int pos;
00069 int num_coefs;
00070 float coef[8];
00071 } TonalComponent;
00072
00073 typedef struct ChannelUnit {
00074 int bands_coded;
00075 int num_components;
00076 float prev_frame[SAMPLES_PER_FRAME];
00077 int gc_blk_switch;
00078 TonalComponent components[64];
00079 GainBlock gain_block[2];
00080
00081 DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
00082 DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
00083
00084 float delay_buf1[46];
00085 float delay_buf2[46];
00086 float delay_buf3[46];
00087 } ChannelUnit;
00088
00089 typedef struct ATRAC3Context {
00090 AVFrame frame;
00091 GetBitContext gb;
00093
00094 int coding_mode;
00095
00096 ChannelUnit *units;
00098
00099
00100 int matrix_coeff_index_prev[4];
00101 int matrix_coeff_index_now[4];
00102 int matrix_coeff_index_next[4];
00103 int weighting_delay[6];
00105
00106
00107 uint8_t *decoded_bytes_buffer;
00108 float temp_buf[1070];
00110
00111
00112 int scrambled_stream;
00114
00115 FFTContext mdct_ctx;
00116 FmtConvertContext fmt_conv;
00117 AVFloatDSPContext fdsp;
00118 } ATRAC3Context;
00119
00120 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
00121 static VLC_TYPE atrac3_vlc_table[4096][2];
00122 static VLC spectral_coeff_tab[7];
00123 static float gain_tab1[16];
00124 static float gain_tab2[31];
00125
00126
00133 static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
00134 {
00135 int i;
00136
00137 if (odd_band) {
00146 for (i = 0; i < 128; i++)
00147 FFSWAP(float, input[i], input[255 - i]);
00148 }
00149
00150 q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
00151
00152
00153 q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE);
00154 }
00155
00156
00157
00158
00159 static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
00160 {
00161 int i, off;
00162 uint32_t c;
00163 const uint32_t *buf;
00164 uint32_t *output = (uint32_t *)out;
00165
00166 off = (intptr_t)input & 3;
00167 buf = (const uint32_t *)(input - off);
00168 c = av_be2ne32((0x537F6103 >> (off * 8)) | (0x537F6103 << (32 - (off * 8))));
00169 bytes += 3 + off;
00170 for (i = 0; i < bytes / 4; i++)
00171 output[i] = c ^ buf[i];
00172
00173 if (off)
00174 av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
00175
00176 return off;
00177 }
00178
00179 static av_cold void init_atrac3_window(void)
00180 {
00181 int i, j;
00182
00183
00184
00185 for (i = 0, j = 255; i < 128; i++, j--) {
00186 float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
00187 float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
00188 float w = 0.5 * (wi * wi + wj * wj);
00189 mdct_window[i] = mdct_window[511 - i] = wi / w;
00190 mdct_window[j] = mdct_window[511 - j] = wj / w;
00191 }
00192 }
00193
00194 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
00195 {
00196 ATRAC3Context *q = avctx->priv_data;
00197
00198 av_free(q->units);
00199 av_free(q->decoded_bytes_buffer);
00200
00201 ff_mdct_end(&q->mdct_ctx);
00202
00203 return 0;
00204 }
00205
00214 static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
00215 int coding_flag, int *mantissas,
00216 int num_codes)
00217 {
00218 int i, code, huff_symb;
00219
00220 if (selector == 1)
00221 num_codes /= 2;
00222
00223 if (coding_flag != 0) {
00224
00225 int num_bits = clc_length_tab[selector];
00226
00227 if (selector > 1) {
00228 for (i = 0; i < num_codes; i++) {
00229 if (num_bits)
00230 code = get_sbits(gb, num_bits);
00231 else
00232 code = 0;
00233 mantissas[i] = code;
00234 }
00235 } else {
00236 for (i = 0; i < num_codes; i++) {
00237 if (num_bits)
00238 code = get_bits(gb, num_bits);
00239 else
00240 code = 0;
00241 mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
00242 mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
00243 }
00244 }
00245 } else {
00246
00247 if (selector != 1) {
00248 for (i = 0; i < num_codes; i++) {
00249 huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
00250 spectral_coeff_tab[selector-1].bits, 3);
00251 huff_symb += 1;
00252 code = huff_symb >> 1;
00253 if (huff_symb & 1)
00254 code = -code;
00255 mantissas[i] = code;
00256 }
00257 } else {
00258 for (i = 0; i < num_codes; i++) {
00259 huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
00260 spectral_coeff_tab[selector - 1].bits, 3);
00261 mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
00262 mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
00263 }
00264 }
00265 }
00266 }
00267
00273 static int decode_spectrum(GetBitContext *gb, float *output)
00274 {
00275 int num_subbands, coding_mode, i, j, first, last, subband_size;
00276 int subband_vlc_index[32], sf_index[32];
00277 int mantissas[128];
00278 float scale_factor;
00279
00280 num_subbands = get_bits(gb, 5);
00281 coding_mode = get_bits1(gb);
00282
00283
00284 for (i = 0; i <= num_subbands; i++)
00285 subband_vlc_index[i] = get_bits(gb, 3);
00286
00287
00288 for (i = 0; i <= num_subbands; i++) {
00289 if (subband_vlc_index[i] != 0)
00290 sf_index[i] = get_bits(gb, 6);
00291 }
00292
00293 for (i = 0; i <= num_subbands; i++) {
00294 first = subband_tab[i ];
00295 last = subband_tab[i + 1];
00296
00297 subband_size = last - first;
00298
00299 if (subband_vlc_index[i] != 0) {
00300
00301
00302
00303 read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
00304 mantissas, subband_size);
00305
00306
00307 scale_factor = ff_atrac_sf_table[sf_index[i]] *
00308 inv_max_quant[subband_vlc_index[i]];
00309
00310
00311 for (j = 0; first < last; first++, j++)
00312 output[first] = mantissas[j] * scale_factor;
00313 } else {
00314
00315 memset(output + first, 0, subband_size * sizeof(*output));
00316 }
00317 }
00318
00319
00320 first = subband_tab[i];
00321 memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
00322 return num_subbands;
00323 }
00324
00331 static int decode_tonal_components(GetBitContext *gb,
00332 TonalComponent *components, int num_bands)
00333 {
00334 int i, b, c, m;
00335 int nb_components, coding_mode_selector, coding_mode;
00336 int band_flags[4], mantissa[8];
00337 int component_count = 0;
00338
00339 nb_components = get_bits(gb, 5);
00340
00341
00342 if (nb_components == 0)
00343 return 0;
00344
00345 coding_mode_selector = get_bits(gb, 2);
00346 if (coding_mode_selector == 2)
00347 return AVERROR_INVALIDDATA;
00348
00349 coding_mode = coding_mode_selector & 1;
00350
00351 for (i = 0; i < nb_components; i++) {
00352 int coded_values_per_component, quant_step_index;
00353
00354 for (b = 0; b <= num_bands; b++)
00355 band_flags[b] = get_bits1(gb);
00356
00357 coded_values_per_component = get_bits(gb, 3);
00358
00359 quant_step_index = get_bits(gb, 3);
00360 if (quant_step_index <= 1)
00361 return AVERROR_INVALIDDATA;
00362
00363 if (coding_mode_selector == 3)
00364 coding_mode = get_bits1(gb);
00365
00366 for (b = 0; b < (num_bands + 1) * 4; b++) {
00367 int coded_components;
00368
00369 if (band_flags[b >> 2] == 0)
00370 continue;
00371
00372 coded_components = get_bits(gb, 3);
00373
00374 for (c = 0; c < coded_components; c++) {
00375 TonalComponent *cmp = &components[component_count];
00376 int sf_index, coded_values, max_coded_values;
00377 float scale_factor;
00378
00379 sf_index = get_bits(gb, 6);
00380 if (component_count >= 64)
00381 return AVERROR_INVALIDDATA;
00382
00383 cmp->pos = b * 64 + get_bits(gb, 6);
00384
00385 max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
00386 coded_values = coded_values_per_component + 1;
00387 coded_values = FFMIN(max_coded_values, coded_values);
00388
00389 scale_factor = ff_atrac_sf_table[sf_index] *
00390 inv_max_quant[quant_step_index];
00391
00392 read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
00393 mantissa, coded_values);
00394
00395 cmp->num_coefs = coded_values;
00396
00397
00398 for (m = 0; m < coded_values; m++)
00399 cmp->coef[m] = mantissa[m] * scale_factor;
00400
00401 component_count++;
00402 }
00403 }
00404 }
00405
00406 return component_count;
00407 }
00408
00415 static int decode_gain_control(GetBitContext *gb, GainBlock *block,
00416 int num_bands)
00417 {
00418 int i, cf, num_data;
00419 int *level, *loc;
00420
00421 GainInfo *gain = block->g_block;
00422
00423 for (i = 0; i <= num_bands; i++) {
00424 num_data = get_bits(gb, 3);
00425 gain[i].num_gain_data = num_data;
00426 level = gain[i].lev_code;
00427 loc = gain[i].loc_code;
00428
00429 for (cf = 0; cf < gain[i].num_gain_data; cf++) {
00430 level[cf] = get_bits(gb, 4);
00431 loc [cf] = get_bits(gb, 5);
00432 if (cf && loc[cf] <= loc[cf - 1])
00433 return AVERROR_INVALIDDATA;
00434 }
00435 }
00436
00437
00438 for (; i < 4 ; i++)
00439 gain[i].num_gain_data = 0;
00440
00441 return 0;
00442 }
00443
00453 static void gain_compensate_and_overlap(float *input, float *prev,
00454 float *output, GainInfo *gain1,
00455 GainInfo *gain2)
00456 {
00457 float g1, g2, gain_inc;
00458 int i, j, num_data, start_loc, end_loc;
00459
00460
00461 if (gain2->num_gain_data == 0)
00462 g1 = 1.0;
00463 else
00464 g1 = gain_tab1[gain2->lev_code[0]];
00465
00466 if (gain1->num_gain_data == 0) {
00467 for (i = 0; i < 256; i++)
00468 output[i] = input[i] * g1 + prev[i];
00469 } else {
00470 num_data = gain1->num_gain_data;
00471 gain1->loc_code[num_data] = 32;
00472 gain1->lev_code[num_data] = 4;
00473
00474 for (i = 0, j = 0; i < num_data; i++) {
00475 start_loc = gain1->loc_code[i] * 8;
00476 end_loc = start_loc + 8;
00477
00478 g2 = gain_tab1[gain1->lev_code[i]];
00479 gain_inc = gain_tab2[gain1->lev_code[i + 1] -
00480 gain1->lev_code[i ] + 15];
00481
00482
00483 for (; j < start_loc; j++)
00484 output[j] = (input[j] * g1 + prev[j]) * g2;
00485
00486
00487 for (; j < end_loc; j++) {
00488 output[j] = (input[j] * g1 + prev[j]) * g2;
00489 g2 *= gain_inc;
00490 }
00491 }
00492
00493 for (; j < 256; j++)
00494 output[j] = input[j] * g1 + prev[j];
00495 }
00496
00497
00498 memcpy(prev, &input[256], 256 * sizeof(*prev));
00499 }
00500
00509 static int add_tonal_components(float *spectrum, int num_components,
00510 TonalComponent *components)
00511 {
00512 int i, j, last_pos = -1;
00513 float *input, *output;
00514
00515 for (i = 0; i < num_components; i++) {
00516 last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
00517 input = components[i].coef;
00518 output = &spectrum[components[i].pos];
00519
00520 for (j = 0; j < components[i].num_coefs; j++)
00521 output[i] += input[i];
00522 }
00523
00524 return last_pos;
00525 }
00526
00527 #define INTERPOLATE(old, new, nsample) \
00528 ((old) + (nsample) * 0.125 * ((new) - (old)))
00529
00530 static void reverse_matrixing(float *su1, float *su2, int *prev_code,
00531 int *curr_code)
00532 {
00533 int i, nsample, band;
00534 float mc1_l, mc1_r, mc2_l, mc2_r;
00535
00536 for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
00537 int s1 = prev_code[i];
00538 int s2 = curr_code[i];
00539 nsample = band;
00540
00541 if (s1 != s2) {
00542
00543 mc1_l = matrix_coeffs[s1 * 2 ];
00544 mc1_r = matrix_coeffs[s1 * 2 + 1];
00545 mc2_l = matrix_coeffs[s2 * 2 ];
00546 mc2_r = matrix_coeffs[s2 * 2 + 1];
00547
00548
00549 for (; nsample < band + 8; nsample++) {
00550 float c1 = su1[nsample];
00551 float c2 = su2[nsample];
00552 c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
00553 c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
00554 su1[nsample] = c2;
00555 su2[nsample] = c1 * 2.0 - c2;
00556 }
00557 }
00558
00559
00560 switch (s2) {
00561 case 0:
00562 for (; nsample < band + 256; nsample++) {
00563 float c1 = su1[nsample];
00564 float c2 = su2[nsample];
00565 su1[nsample] = c2 * 2.0;
00566 su2[nsample] = (c1 - c2) * 2.0;
00567 }
00568 break;
00569 case 1:
00570 for (; nsample < band + 256; nsample++) {
00571 float c1 = su1[nsample];
00572 float c2 = su2[nsample];
00573 su1[nsample] = (c1 + c2) * 2.0;
00574 su2[nsample] = c2 * -2.0;
00575 }
00576 break;
00577 case 2:
00578 case 3:
00579 for (; nsample < band + 256; nsample++) {
00580 float c1 = su1[nsample];
00581 float c2 = su2[nsample];
00582 su1[nsample] = c1 + c2;
00583 su2[nsample] = c1 - c2;
00584 }
00585 break;
00586 default:
00587 av_assert1(0);
00588 }
00589 }
00590 }
00591
00592 static void get_channel_weights(int index, int flag, float ch[2])
00593 {
00594 if (index == 7) {
00595 ch[0] = 1.0;
00596 ch[1] = 1.0;
00597 } else {
00598 ch[0] = (index & 7) / 7.0;
00599 ch[1] = sqrt(2 - ch[0] * ch[0]);
00600 if (flag)
00601 FFSWAP(float, ch[0], ch[1]);
00602 }
00603 }
00604
00605 static void channel_weighting(float *su1, float *su2, int *p3)
00606 {
00607 int band, nsample;
00608
00609 float w[2][2];
00610
00611 if (p3[1] != 7 || p3[3] != 7) {
00612 get_channel_weights(p3[1], p3[0], w[0]);
00613 get_channel_weights(p3[3], p3[2], w[1]);
00614
00615 for (band = 256; band < 4 * 256; band += 256) {
00616 for (nsample = band; nsample < band + 8; nsample++) {
00617 su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
00618 su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
00619 }
00620 for(; nsample < band + 256; nsample++) {
00621 su1[nsample] *= w[1][0];
00622 su2[nsample] *= w[1][1];
00623 }
00624 }
00625 }
00626 }
00627
00636 static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
00637 ChannelUnit *snd, float *output,
00638 int channel_num, int coding_mode)
00639 {
00640 int band, ret, num_subbands, last_tonal, num_bands;
00641 GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
00642 GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
00643
00644 if (coding_mode == JOINT_STEREO && channel_num == 1) {
00645 if (get_bits(gb, 2) != 3) {
00646 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00647 return AVERROR_INVALIDDATA;
00648 }
00649 } else {
00650 if (get_bits(gb, 6) != 0x28) {
00651 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00652 return AVERROR_INVALIDDATA;
00653 }
00654 }
00655
00656
00657 snd->bands_coded = get_bits(gb, 2);
00658
00659 ret = decode_gain_control(gb, gain2, snd->bands_coded);
00660 if (ret)
00661 return ret;
00662
00663 snd->num_components = decode_tonal_components(gb, snd->components,
00664 snd->bands_coded);
00665 if (snd->num_components == -1)
00666 return -1;
00667
00668 num_subbands = decode_spectrum(gb, snd->spectrum);
00669
00670
00671 last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
00672 snd->components);
00673
00674
00675
00676
00677 num_bands = (subband_tab[num_subbands] - 1) >> 8;
00678 if (last_tonal >= 0)
00679 num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
00680
00681
00682
00683 for (band = 0; band < 4; band++) {
00684
00685 if (band <= num_bands)
00686 imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
00687 else
00688 memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
00689
00690
00691 gain_compensate_and_overlap(snd->imdct_buf,
00692 &snd->prev_frame[band * 256],
00693 &output[band * 256],
00694 &gain1->g_block[band],
00695 &gain2->g_block[band]);
00696 }
00697
00698
00699 snd->gc_blk_switch ^= 1;
00700
00701 return 0;
00702 }
00703
00704 static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
00705 float **out_samples)
00706 {
00707 ATRAC3Context *q = avctx->priv_data;
00708 int ret, i;
00709 uint8_t *ptr1;
00710
00711 if (q->coding_mode == JOINT_STEREO) {
00712
00713
00714 init_get_bits(&q->gb, databuf, avctx->block_align * 8);
00715
00716 ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
00717 JOINT_STEREO);
00718 if (ret != 0)
00719 return ret;
00720
00721
00722
00723 if (databuf == q->decoded_bytes_buffer) {
00724 uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1;
00725 ptr1 = q->decoded_bytes_buffer;
00726 for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--)
00727 FFSWAP(uint8_t, *ptr1, *ptr2);
00728 } else {
00729 const uint8_t *ptr2 = databuf + avctx->block_align - 1;
00730 for (i = 0; i < avctx->block_align; i++)
00731 q->decoded_bytes_buffer[i] = *ptr2--;
00732 }
00733
00734
00735 ptr1 = q->decoded_bytes_buffer;
00736 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00737 if (i >= avctx->block_align)
00738 return AVERROR_INVALIDDATA;
00739 }
00740
00741
00742
00743 init_get_bits(&q->gb, ptr1, avctx->block_align * 8);
00744
00745
00746 memmove(q->weighting_delay, &q->weighting_delay[2],
00747 4 * sizeof(*q->weighting_delay));
00748 q->weighting_delay[4] = get_bits1(&q->gb);
00749 q->weighting_delay[5] = get_bits(&q->gb, 3);
00750
00751 for (i = 0; i < 4; i++) {
00752 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00753 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00754 q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
00755 }
00756
00757
00758 ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
00759 out_samples[1], 1, JOINT_STEREO);
00760 if (ret != 0)
00761 return ret;
00762
00763
00764 reverse_matrixing(out_samples[0], out_samples[1],
00765 q->matrix_coeff_index_prev,
00766 q->matrix_coeff_index_now);
00767
00768 channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
00769 } else {
00770
00771
00772 for (i = 0; i < avctx->channels; i++) {
00773
00774 init_get_bits(&q->gb,
00775 databuf + i * avctx->block_align / avctx->channels,
00776 avctx->block_align * 8 / avctx->channels);
00777
00778 ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
00779 out_samples[i], i, q->coding_mode);
00780 if (ret != 0)
00781 return ret;
00782 }
00783 }
00784
00785
00786 for (i = 0; i < avctx->channels; i++) {
00787 float *p1 = out_samples[i];
00788 float *p2 = p1 + 256;
00789 float *p3 = p2 + 256;
00790 float *p4 = p3 + 256;
00791 ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
00792 ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
00793 ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
00794 }
00795
00796 return 0;
00797 }
00798
00799 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
00800 int *got_frame_ptr, AVPacket *avpkt)
00801 {
00802 const uint8_t *buf = avpkt->data;
00803 int buf_size = avpkt->size;
00804 ATRAC3Context *q = avctx->priv_data;
00805 int ret;
00806 const uint8_t *databuf;
00807
00808 if (buf_size < avctx->block_align) {
00809 av_log(avctx, AV_LOG_ERROR,
00810 "Frame too small (%d bytes). Truncated file?\n", buf_size);
00811 return AVERROR_INVALIDDATA;
00812 }
00813
00814
00815 q->frame.nb_samples = SAMPLES_PER_FRAME;
00816 if ((ret = ff_get_buffer(avctx, &q->frame)) < 0) {
00817 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00818 return ret;
00819 }
00820
00821
00822 if (q->scrambled_stream) {
00823 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00824 databuf = q->decoded_bytes_buffer;
00825 } else {
00826 databuf = buf;
00827 }
00828
00829 ret = decode_frame(avctx, databuf, (float **)q->frame.extended_data);
00830 if (ret) {
00831 av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
00832 return ret;
00833 }
00834
00835 *got_frame_ptr = 1;
00836 *(AVFrame *)data = q->frame;
00837
00838 return avctx->block_align;
00839 }
00840
00841 static void atrac3_init_static_data(void)
00842 {
00843 int i;
00844
00845 init_atrac3_window();
00846 ff_atrac_generate_tables();
00847
00848
00849 for (i = 0; i < 7; i++) {
00850 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
00851 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
00852 atrac3_vlc_offs[i ];
00853 init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
00854 huff_bits[i], 1, 1,
00855 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00856 }
00857
00858
00859 for (i = 0; i < 16; i++)
00860 gain_tab1[i] = exp2f (4 - i);
00861
00862 for (i = -15; i < 16; i++)
00863 gain_tab2[i + 15] = exp2f (i * -0.125);
00864 }
00865
00866 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00867 {
00868 static int static_init_done;
00869 int i, ret;
00870 int version, delay, samples_per_frame, frame_factor;
00871 const uint8_t *edata_ptr = avctx->extradata;
00872 ATRAC3Context *q = avctx->priv_data;
00873
00874 if (avctx->channels <= 0 || avctx->channels > 2) {
00875 av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
00876 return AVERROR(EINVAL);
00877 }
00878
00879 if (!static_init_done)
00880 atrac3_init_static_data();
00881 static_init_done = 1;
00882
00883
00884 if (avctx->extradata_size == 14) {
00885
00886 av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
00887 bytestream_get_le16(&edata_ptr));
00888 edata_ptr += 4;
00889 q->coding_mode = bytestream_get_le16(&edata_ptr);
00890 av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
00891 bytestream_get_le16(&edata_ptr));
00892 frame_factor = bytestream_get_le16(&edata_ptr);
00893 av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
00894 bytestream_get_le16(&edata_ptr));
00895
00896
00897 samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
00898 version = 4;
00899 delay = 0x88E;
00900 q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;
00901 q->scrambled_stream = 0;
00902
00903 if (avctx->block_align != 96 * avctx->channels * frame_factor &&
00904 avctx->block_align != 152 * avctx->channels * frame_factor &&
00905 avctx->block_align != 192 * avctx->channels * frame_factor) {
00906 av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
00907 "configuration %d/%d/%d\n", avctx->block_align,
00908 avctx->channels, frame_factor);
00909 return AVERROR_INVALIDDATA;
00910 }
00911 } else if (avctx->extradata_size == 10) {
00912
00913 version = bytestream_get_be32(&edata_ptr);
00914 samples_per_frame = bytestream_get_be16(&edata_ptr);
00915 delay = bytestream_get_be16(&edata_ptr);
00916 q->coding_mode = bytestream_get_be16(&edata_ptr);
00917 q->scrambled_stream = 1;
00918
00919 } else {
00920 av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
00921 avctx->extradata_size);
00922 return AVERROR(EINVAL);
00923 }
00924
00925 if (q->coding_mode == JOINT_STEREO && avctx->channels < 2) {
00926 av_log(avctx, AV_LOG_ERROR, "Invalid coding mode\n");
00927 return AVERROR_INVALIDDATA;
00928 }
00929
00930
00931
00932 if (version != 4) {
00933 av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
00934 return AVERROR_INVALIDDATA;
00935 }
00936
00937 if (samples_per_frame != SAMPLES_PER_FRAME &&
00938 samples_per_frame != SAMPLES_PER_FRAME * 2) {
00939 av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
00940 samples_per_frame);
00941 return AVERROR_INVALIDDATA;
00942 }
00943
00944 if (delay != 0x88E) {
00945 av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
00946 delay);
00947 return AVERROR_INVALIDDATA;
00948 }
00949
00950 if (q->coding_mode == STEREO)
00951 av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
00952 else if (q->coding_mode == JOINT_STEREO)
00953 av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
00954 else {
00955 av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
00956 q->coding_mode);
00957 return AVERROR_INVALIDDATA;
00958 }
00959
00960 if (avctx->block_align >= UINT_MAX / 2)
00961 return AVERROR(EINVAL);
00962
00963 q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
00964 FF_INPUT_BUFFER_PADDING_SIZE);
00965 if (q->decoded_bytes_buffer == NULL)
00966 return AVERROR(ENOMEM);
00967
00968 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
00969
00970
00971 if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
00972 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
00973 av_freep(&q->decoded_bytes_buffer);
00974 return ret;
00975 }
00976
00977
00978 q->weighting_delay[0] = 0;
00979 q->weighting_delay[1] = 7;
00980 q->weighting_delay[2] = 0;
00981 q->weighting_delay[3] = 7;
00982 q->weighting_delay[4] = 0;
00983 q->weighting_delay[5] = 7;
00984
00985 for (i = 0; i < 4; i++) {
00986 q->matrix_coeff_index_prev[i] = 3;
00987 q->matrix_coeff_index_now[i] = 3;
00988 q->matrix_coeff_index_next[i] = 3;
00989 }
00990
00991 avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
00992 ff_fmt_convert_init(&q->fmt_conv, avctx);
00993
00994 q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
00995 if (!q->units) {
00996 atrac3_decode_close(avctx);
00997 return AVERROR(ENOMEM);
00998 }
00999
01000 avcodec_get_frame_defaults(&q->frame);
01001 avctx->coded_frame = &q->frame;
01002
01003 return 0;
01004 }
01005
01006 AVCodec ff_atrac3_decoder = {
01007 .name = "atrac3",
01008 .type = AVMEDIA_TYPE_AUDIO,
01009 .id = AV_CODEC_ID_ATRAC3,
01010 .priv_data_size = sizeof(ATRAC3Context),
01011 .init = atrac3_decode_init,
01012 .close = atrac3_decode_close,
01013 .decode = atrac3_decode_frame,
01014 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01015 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01016 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
01017 AV_SAMPLE_FMT_NONE },
01018 };