00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029
00030
00031 #include <math.h>
00032 #include <stddef.h>
00033 #include <stdio.h>
00034
00035 #include "avcodec.h"
00036 #include "get_bits.h"
00037 #include "dsputil.h"
00038 #include "fft.h"
00039 #include "internal.h"
00040 #include "sinewin.h"
00041
00042 #include "atrac.h"
00043 #include "atrac1data.h"
00044
00045 #define AT1_MAX_BFU 52
00046 #define AT1_SU_SIZE 212
00047 #define AT1_SU_SAMPLES 512
00048 #define AT1_FRAME_SIZE AT1_SU_SIZE * 2
00049 #define AT1_SU_MAX_BITS AT1_SU_SIZE * 8
00050 #define AT1_MAX_CHANNELS 2
00051
00052 #define AT1_QMF_BANDS 3
00053 #define IDX_LOW_BAND 0
00054 #define IDX_MID_BAND 1
00055 #define IDX_HIGH_BAND 2
00056
00060 typedef struct {
00061 int log2_block_count[AT1_QMF_BANDS];
00062 int num_bfus;
00063 float* spectrum[2];
00064 DECLARE_ALIGNED(32, float, spec1)[AT1_SU_SAMPLES];
00065 DECLARE_ALIGNED(32, float, spec2)[AT1_SU_SAMPLES];
00066 DECLARE_ALIGNED(32, float, fst_qmf_delay)[46];
00067 DECLARE_ALIGNED(32, float, snd_qmf_delay)[46];
00068 DECLARE_ALIGNED(32, float, last_qmf_delay)[256+23];
00069 } AT1SUCtx;
00070
00074 typedef struct {
00075 AVFrame frame;
00076 AT1SUCtx SUs[AT1_MAX_CHANNELS];
00077 DECLARE_ALIGNED(32, float, spec)[AT1_SU_SAMPLES];
00078
00079 DECLARE_ALIGNED(32, float, low)[256];
00080 DECLARE_ALIGNED(32, float, mid)[256];
00081 DECLARE_ALIGNED(32, float, high)[512];
00082 float* bands[3];
00083 FFTContext mdct_ctx[3];
00084 DSPContext dsp;
00085 } AT1Ctx;
00086
00088 static const uint16_t samples_per_band[3] = {128, 128, 256};
00089 static const uint8_t mdct_long_nbits[3] = {7, 7, 8};
00090
00091
00092 static void at1_imdct(AT1Ctx *q, float *spec, float *out, int nbits,
00093 int rev_spec)
00094 {
00095 FFTContext* mdct_context = &q->mdct_ctx[nbits - 5 - (nbits > 6)];
00096 int transf_size = 1 << nbits;
00097
00098 if (rev_spec) {
00099 int i;
00100 for (i = 0; i < transf_size / 2; i++)
00101 FFSWAP(float, spec[i], spec[transf_size - 1 - i]);
00102 }
00103 mdct_context->imdct_half(mdct_context, out, spec);
00104 }
00105
00106
00107 static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q)
00108 {
00109 int band_num, band_samples, log2_block_count, nbits, num_blocks, block_size;
00110 unsigned int start_pos, ref_pos = 0, pos = 0;
00111
00112 for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00113 float *prev_buf;
00114 int j;
00115
00116 band_samples = samples_per_band[band_num];
00117 log2_block_count = su->log2_block_count[band_num];
00118
00119
00120
00121 num_blocks = 1 << log2_block_count;
00122
00123 if (num_blocks == 1) {
00124
00125
00126 block_size = band_samples >> log2_block_count;
00127
00128
00129 nbits = mdct_long_nbits[band_num] - log2_block_count;
00130
00131 if (nbits != 5 && nbits != 7 && nbits != 8)
00132 return AVERROR_INVALIDDATA;
00133 } else {
00134 block_size = 32;
00135 nbits = 5;
00136 }
00137
00138 start_pos = 0;
00139 prev_buf = &su->spectrum[1][ref_pos + band_samples - 16];
00140 for (j=0; j < num_blocks; j++) {
00141 at1_imdct(q, &q->spec[pos], &su->spectrum[0][ref_pos + start_pos], nbits, band_num);
00142
00143
00144 q->dsp.vector_fmul_window(&q->bands[band_num][start_pos], prev_buf,
00145 &su->spectrum[0][ref_pos + start_pos], ff_sine_32, 16);
00146
00147 prev_buf = &su->spectrum[0][ref_pos+start_pos + 16];
00148 start_pos += block_size;
00149 pos += block_size;
00150 }
00151
00152 if (num_blocks == 1)
00153 memcpy(q->bands[band_num] + 32, &su->spectrum[0][ref_pos + 16], 240 * sizeof(float));
00154
00155 ref_pos += band_samples;
00156 }
00157
00158
00159 FFSWAP(float*, su->spectrum[0], su->spectrum[1]);
00160
00161 return 0;
00162 }
00163
00168 static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS])
00169 {
00170 int log2_block_count_tmp, i;
00171
00172 for (i = 0; i < 2; i++) {
00173
00174 log2_block_count_tmp = get_bits(gb, 2);
00175 if (log2_block_count_tmp & 1)
00176 return AVERROR_INVALIDDATA;
00177 log2_block_cnt[i] = 2 - log2_block_count_tmp;
00178 }
00179
00180
00181 log2_block_count_tmp = get_bits(gb, 2);
00182 if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3)
00183 return AVERROR_INVALIDDATA;
00184 log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp;
00185
00186 skip_bits(gb, 2);
00187 return 0;
00188 }
00189
00190
00191 static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su,
00192 float spec[AT1_SU_SAMPLES])
00193 {
00194 int bits_used, band_num, bfu_num, i;
00195 uint8_t idwls[AT1_MAX_BFU];
00196 uint8_t idsfs[AT1_MAX_BFU];
00197
00198
00199 su->num_bfus = bfu_amount_tab1[get_bits(gb, 3)];
00200
00201
00202
00203
00204 bits_used = su->num_bfus * 10 + 32 +
00205 bfu_amount_tab2[get_bits(gb, 2)] +
00206 (bfu_amount_tab3[get_bits(gb, 3)] << 1);
00207
00208
00209 for (i = 0; i < su->num_bfus; i++)
00210 idwls[i] = get_bits(gb, 4);
00211
00212
00213 for (i = 0; i < su->num_bfus; i++)
00214 idsfs[i] = get_bits(gb, 6);
00215
00216
00217 for (i = su->num_bfus; i < AT1_MAX_BFU; i++)
00218 idwls[i] = idsfs[i] = 0;
00219
00220
00221 for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00222 for (bfu_num = bfu_bands_t[band_num]; bfu_num < bfu_bands_t[band_num+1]; bfu_num++) {
00223 int pos;
00224
00225 int num_specs = specs_per_bfu[bfu_num];
00226 int word_len = !!idwls[bfu_num] + idwls[bfu_num];
00227 float scale_factor = ff_atrac_sf_table[idsfs[bfu_num]];
00228 bits_used += word_len * num_specs;
00229
00230
00231 if (bits_used > AT1_SU_MAX_BITS)
00232 return AVERROR_INVALIDDATA;
00233
00234
00235 pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];
00236
00237 if (word_len) {
00238 float max_quant = 1.0 / (float)((1 << (word_len - 1)) - 1);
00239
00240 for (i = 0; i < num_specs; i++) {
00241
00242
00243
00244 spec[pos+i] = get_sbits(gb, word_len) * scale_factor * max_quant;
00245 }
00246 } else {
00247 memset(&spec[pos], 0, num_specs * sizeof(float));
00248 }
00249 }
00250 }
00251
00252 return 0;
00253 }
00254
00255
00256 static void at1_subband_synthesis(AT1Ctx *q, AT1SUCtx* su, float *pOut)
00257 {
00258 float temp[256];
00259 float iqmf_temp[512 + 46];
00260
00261
00262 ff_atrac_iqmf(q->bands[0], q->bands[1], 128, temp, su->fst_qmf_delay, iqmf_temp);
00263
00264
00265 memcpy( su->last_qmf_delay, &su->last_qmf_delay[256], sizeof(float) * 23);
00266 memcpy(&su->last_qmf_delay[23], q->bands[2], sizeof(float) * 256);
00267
00268
00269 ff_atrac_iqmf(temp, su->last_qmf_delay, 256, pOut, su->snd_qmf_delay, iqmf_temp);
00270 }
00271
00272
00273 static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
00274 int *got_frame_ptr, AVPacket *avpkt)
00275 {
00276 const uint8_t *buf = avpkt->data;
00277 int buf_size = avpkt->size;
00278 AT1Ctx *q = avctx->priv_data;
00279 int ch, ret;
00280 GetBitContext gb;
00281
00282
00283 if (buf_size < 212 * avctx->channels) {
00284 av_log(avctx, AV_LOG_ERROR, "Not enough data to decode!\n");
00285 return AVERROR_INVALIDDATA;
00286 }
00287
00288
00289 q->frame.nb_samples = AT1_SU_SAMPLES;
00290 if ((ret = ff_get_buffer(avctx, &q->frame)) < 0) {
00291 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00292 return ret;
00293 }
00294
00295 for (ch = 0; ch < avctx->channels; ch++) {
00296 AT1SUCtx* su = &q->SUs[ch];
00297
00298 init_get_bits(&gb, &buf[212 * ch], 212 * 8);
00299
00300
00301 ret = at1_parse_bsm(&gb, su->log2_block_count);
00302 if (ret < 0)
00303 return ret;
00304
00305 ret = at1_unpack_dequant(&gb, su, q->spec);
00306 if (ret < 0)
00307 return ret;
00308
00309 ret = at1_imdct_block(su, q);
00310 if (ret < 0)
00311 return ret;
00312 at1_subband_synthesis(q, su, (float *)q->frame.extended_data[ch]);
00313 }
00314
00315 *got_frame_ptr = 1;
00316 *(AVFrame *)data = q->frame;
00317
00318 return avctx->block_align;
00319 }
00320
00321
00322 static av_cold int atrac1_decode_end(AVCodecContext * avctx)
00323 {
00324 AT1Ctx *q = avctx->priv_data;
00325
00326 ff_mdct_end(&q->mdct_ctx[0]);
00327 ff_mdct_end(&q->mdct_ctx[1]);
00328 ff_mdct_end(&q->mdct_ctx[2]);
00329
00330 return 0;
00331 }
00332
00333
00334 static av_cold int atrac1_decode_init(AVCodecContext *avctx)
00335 {
00336 AT1Ctx *q = avctx->priv_data;
00337 int ret;
00338
00339 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
00340
00341 if (avctx->channels < 1 || avctx->channels > AT1_MAX_CHANNELS) {
00342 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n",
00343 avctx->channels);
00344 return AVERROR(EINVAL);
00345 }
00346
00347 if (avctx->block_align <= 0) {
00348 av_log_ask_for_sample(avctx, "unsupported block align\n");
00349 return AVERROR_PATCHWELCOME;
00350 }
00351
00352
00353 if ((ret = ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15))) ||
00354 (ret = ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1 << 15))) ||
00355 (ret = ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1 << 15)))) {
00356 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
00357 atrac1_decode_end(avctx);
00358 return ret;
00359 }
00360
00361 ff_init_ff_sine_windows(5);
00362
00363 ff_atrac_generate_tables();
00364
00365 ff_dsputil_init(&q->dsp, avctx);
00366
00367 q->bands[0] = q->low;
00368 q->bands[1] = q->mid;
00369 q->bands[2] = q->high;
00370
00371
00372 q->SUs[0].spectrum[0] = q->SUs[0].spec1;
00373 q->SUs[0].spectrum[1] = q->SUs[0].spec2;
00374 q->SUs[1].spectrum[0] = q->SUs[1].spec1;
00375 q->SUs[1].spectrum[1] = q->SUs[1].spec2;
00376
00377 avcodec_get_frame_defaults(&q->frame);
00378 avctx->coded_frame = &q->frame;
00379
00380 return 0;
00381 }
00382
00383
00384 AVCodec ff_atrac1_decoder = {
00385 .name = "atrac1",
00386 .type = AVMEDIA_TYPE_AUDIO,
00387 .id = AV_CODEC_ID_ATRAC1,
00388 .priv_data_size = sizeof(AT1Ctx),
00389 .init = atrac1_decode_init,
00390 .close = atrac1_decode_end,
00391 .decode = atrac1_decode_frame,
00392 .capabilities = CODEC_CAP_DR1,
00393 .long_name = NULL_IF_CONFIG_SMALL("Atrac 1 (Adaptive TRansform Acoustic Coding)"),
00394 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
00395 AV_SAMPLE_FMT_NONE },
00396 };