libavcodec/atrac1.c
Go to the documentation of this file.
00001 /*
00002  * Atrac 1 compatible decoder
00003  * Copyright (c) 2009 Maxim Poliakovski
00004  * Copyright (c) 2009 Benjamin Larsson
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00029 /* Many thanks to Tim Craig for all the help! */
00030 
00031 #include <math.h>
00032 #include <stddef.h>
00033 #include <stdio.h>
00034 
00035 #include "avcodec.h"
00036 #include "internal.h"
00037 #include "get_bits.h"
00038 #include "dsputil.h"
00039 #include "fft.h"
00040 #include "fmtconvert.h"
00041 #include "sinewin.h"
00042 
00043 #include "atrac.h"
00044 #include "atrac1data.h"
00045 
00046 #define AT1_MAX_BFU      52                 ///< max number of block floating units in a sound unit
00047 #define AT1_SU_SIZE      212                ///< number of bytes in a sound unit
00048 #define AT1_SU_SAMPLES   512                ///< number of samples in a sound unit
00049 #define AT1_FRAME_SIZE   AT1_SU_SIZE * 2
00050 #define AT1_SU_MAX_BITS  AT1_SU_SIZE * 8
00051 #define AT1_MAX_CHANNELS 2
00052 
00053 #define AT1_QMF_BANDS    3
00054 #define IDX_LOW_BAND     0
00055 #define IDX_MID_BAND     1
00056 #define IDX_HIGH_BAND    2
00057 
00061 typedef struct {
00062     int                 log2_block_count[AT1_QMF_BANDS];    
00063     int                 num_bfus;                           
00064     float*              spectrum[2];
00065     DECLARE_ALIGNED(32, float, spec1)[AT1_SU_SAMPLES];     
00066     DECLARE_ALIGNED(32, float, spec2)[AT1_SU_SAMPLES];     
00067     DECLARE_ALIGNED(32, float, fst_qmf_delay)[46];         
00068     DECLARE_ALIGNED(32, float, snd_qmf_delay)[46];         
00069     DECLARE_ALIGNED(32, float, last_qmf_delay)[256+23];    
00070 } AT1SUCtx;
00071 
00075 typedef struct {
00076     AVFrame frame;
00077     AT1SUCtx            SUs[AT1_MAX_CHANNELS];              
00078     DECLARE_ALIGNED(32, float, spec)[AT1_SU_SAMPLES];      
00079 
00080     DECLARE_ALIGNED(32, float,  low)[256];
00081     DECLARE_ALIGNED(32, float,  mid)[256];
00082     DECLARE_ALIGNED(32, float, high)[512];
00083     float*              bands[3];
00084     float              *out_samples[AT1_MAX_CHANNELS];
00085     FFTContext          mdct_ctx[3];
00086     int                 channels;
00087     DSPContext          dsp;
00088     FmtConvertContext   fmt_conv;
00089 } AT1Ctx;
00090 
00092 static const uint16_t samples_per_band[3] = {128, 128, 256};
00093 static const uint8_t   mdct_long_nbits[3] = {7, 7, 8};
00094 
00095 
00096 static void at1_imdct(AT1Ctx *q, float *spec, float *out, int nbits,
00097                       int rev_spec)
00098 {
00099     FFTContext* mdct_context = &q->mdct_ctx[nbits - 5 - (nbits > 6)];
00100     int transf_size = 1 << nbits;
00101 
00102     if (rev_spec) {
00103         int i;
00104         for (i = 0; i < transf_size / 2; i++)
00105             FFSWAP(float, spec[i], spec[transf_size - 1 - i]);
00106     }
00107     mdct_context->imdct_half(mdct_context, out, spec);
00108 }
00109 
00110 
00111 static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q)
00112 {
00113     int          band_num, band_samples, log2_block_count, nbits, num_blocks, block_size;
00114     unsigned int start_pos, ref_pos = 0, pos = 0;
00115 
00116     for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00117         float *prev_buf;
00118         int j;
00119 
00120         band_samples = samples_per_band[band_num];
00121         log2_block_count = su->log2_block_count[band_num];
00122 
00123         /* number of mdct blocks in the current QMF band: 1 - for long mode */
00124         /* 4 for short mode(low/middle bands) and 8 for short mode(high band)*/
00125         num_blocks = 1 << log2_block_count;
00126 
00127         if (num_blocks == 1) {
00128             /* mdct block size in samples: 128 (long mode, low & mid bands), */
00129             /* 256 (long mode, high band) and 32 (short mode, all bands) */
00130             block_size = band_samples >> log2_block_count;
00131 
00132             /* calc transform size in bits according to the block_size_mode */
00133             nbits = mdct_long_nbits[band_num] - log2_block_count;
00134 
00135             if (nbits != 5 && nbits != 7 && nbits != 8)
00136                 return AVERROR_INVALIDDATA;
00137         } else {
00138             block_size = 32;
00139             nbits = 5;
00140         }
00141 
00142         start_pos = 0;
00143         prev_buf = &su->spectrum[1][ref_pos + band_samples - 16];
00144         for (j=0; j < num_blocks; j++) {
00145             at1_imdct(q, &q->spec[pos], &su->spectrum[0][ref_pos + start_pos], nbits, band_num);
00146 
00147             /* overlap and window */
00148             q->dsp.vector_fmul_window(&q->bands[band_num][start_pos], prev_buf,
00149                                       &su->spectrum[0][ref_pos + start_pos], ff_sine_32, 16);
00150 
00151             prev_buf = &su->spectrum[0][ref_pos+start_pos + 16];
00152             start_pos += block_size;
00153             pos += block_size;
00154         }
00155 
00156         if (num_blocks == 1)
00157             memcpy(q->bands[band_num] + 32, &su->spectrum[0][ref_pos + 16], 240 * sizeof(float));
00158 
00159         ref_pos += band_samples;
00160     }
00161 
00162     /* Swap buffers so the mdct overlap works */
00163     FFSWAP(float*, su->spectrum[0], su->spectrum[1]);
00164 
00165     return 0;
00166 }
00167 
00172 static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS])
00173 {
00174     int log2_block_count_tmp, i;
00175 
00176     for (i = 0; i < 2; i++) {
00177         /* low and mid band */
00178         log2_block_count_tmp = get_bits(gb, 2);
00179         if (log2_block_count_tmp & 1)
00180             return AVERROR_INVALIDDATA;
00181         log2_block_cnt[i] = 2 - log2_block_count_tmp;
00182     }
00183 
00184     /* high band */
00185     log2_block_count_tmp = get_bits(gb, 2);
00186     if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3)
00187         return AVERROR_INVALIDDATA;
00188     log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp;
00189 
00190     skip_bits(gb, 2);
00191     return 0;
00192 }
00193 
00194 
00195 static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su,
00196                               float spec[AT1_SU_SAMPLES])
00197 {
00198     int bits_used, band_num, bfu_num, i;
00199     uint8_t idwls[AT1_MAX_BFU];                 
00200     uint8_t idsfs[AT1_MAX_BFU];                 
00201 
00202     /* parse the info byte (2nd byte) telling how much BFUs were coded */
00203     su->num_bfus = bfu_amount_tab1[get_bits(gb, 3)];
00204 
00205     /* calc number of consumed bits:
00206         num_BFUs * (idwl(4bits) + idsf(6bits)) + log2_block_count(8bits) + info_byte(8bits)
00207         + info_byte_copy(8bits) + log2_block_count_copy(8bits) */
00208     bits_used = su->num_bfus * 10 + 32 +
00209                 bfu_amount_tab2[get_bits(gb, 2)] +
00210                 (bfu_amount_tab3[get_bits(gb, 3)] << 1);
00211 
00212     /* get word length index (idwl) for each BFU */
00213     for (i = 0; i < su->num_bfus; i++)
00214         idwls[i] = get_bits(gb, 4);
00215 
00216     /* get scalefactor index (idsf) for each BFU */
00217     for (i = 0; i < su->num_bfus; i++)
00218         idsfs[i] = get_bits(gb, 6);
00219 
00220     /* zero idwl/idsf for empty BFUs */
00221     for (i = su->num_bfus; i < AT1_MAX_BFU; i++)
00222         idwls[i] = idsfs[i] = 0;
00223 
00224     /* read in the spectral data and reconstruct MDCT spectrum of this channel */
00225     for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00226         for (bfu_num = bfu_bands_t[band_num]; bfu_num < bfu_bands_t[band_num+1]; bfu_num++) {
00227             int pos;
00228 
00229             int num_specs = specs_per_bfu[bfu_num];
00230             int word_len  = !!idwls[bfu_num] + idwls[bfu_num];
00231             float scale_factor = ff_atrac_sf_table[idsfs[bfu_num]];
00232             bits_used += word_len * num_specs; /* add number of bits consumed by current BFU */
00233 
00234             /* check for bitstream overflow */
00235             if (bits_used > AT1_SU_MAX_BITS)
00236                 return AVERROR_INVALIDDATA;
00237 
00238             /* get the position of the 1st spec according to the block size mode */
00239             pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];
00240 
00241             if (word_len) {
00242                 float   max_quant = 1.0 / (float)((1 << (word_len - 1)) - 1);
00243 
00244                 for (i = 0; i < num_specs; i++) {
00245                     /* read in a quantized spec and convert it to
00246                      * signed int and then inverse quantization
00247                      */
00248                     spec[pos+i] = get_sbits(gb, word_len) * scale_factor * max_quant;
00249                 }
00250             } else { /* word_len = 0 -> empty BFU, zero all specs in the emty BFU */
00251                 memset(&spec[pos], 0, num_specs * sizeof(float));
00252             }
00253         }
00254     }
00255 
00256     return 0;
00257 }
00258 
00259 
00260 static void at1_subband_synthesis(AT1Ctx *q, AT1SUCtx* su, float *pOut)
00261 {
00262     float temp[256];
00263     float iqmf_temp[512 + 46];
00264 
00265     /* combine low and middle bands */
00266     atrac_iqmf(q->bands[0], q->bands[1], 128, temp, su->fst_qmf_delay, iqmf_temp);
00267 
00268     /* delay the signal of the high band by 23 samples */
00269     memcpy( su->last_qmf_delay,    &su->last_qmf_delay[256], sizeof(float) *  23);
00270     memcpy(&su->last_qmf_delay[23], q->bands[2],             sizeof(float) * 256);
00271 
00272     /* combine (low + middle) and high bands */
00273     atrac_iqmf(temp, su->last_qmf_delay, 256, pOut, su->snd_qmf_delay, iqmf_temp);
00274 }
00275 
00276 
00277 static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
00278                                int *got_frame_ptr, AVPacket *avpkt)
00279 {
00280     const uint8_t *buf = avpkt->data;
00281     int buf_size       = avpkt->size;
00282     AT1Ctx *q          = avctx->priv_data;
00283     int ch, ret;
00284     GetBitContext gb;
00285     float *samples;
00286 
00287 
00288     if (buf_size < 212 * q->channels) {
00289         av_log(avctx, AV_LOG_ERROR, "Not enough data to decode!\n");
00290         return AVERROR_INVALIDDATA;
00291     }
00292 
00293     /* get output buffer */
00294     q->frame.nb_samples = AT1_SU_SAMPLES;
00295     if ((ret = ff_get_buffer(avctx, &q->frame)) < 0) {
00296         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00297         return ret;
00298     }
00299     samples = (float *)q->frame.data[0];
00300 
00301     for (ch = 0; ch < q->channels; ch++) {
00302         AT1SUCtx* su = &q->SUs[ch];
00303 
00304         init_get_bits(&gb, &buf[212 * ch], 212 * 8);
00305 
00306         /* parse block_size_mode, 1st byte */
00307         ret = at1_parse_bsm(&gb, su->log2_block_count);
00308         if (ret < 0)
00309             return ret;
00310 
00311         ret = at1_unpack_dequant(&gb, su, q->spec);
00312         if (ret < 0)
00313             return ret;
00314 
00315         ret = at1_imdct_block(su, q);
00316         if (ret < 0)
00317             return ret;
00318         at1_subband_synthesis(q, su, q->channels == 1 ? samples : q->out_samples[ch]);
00319     }
00320 
00321     /* interleave */
00322     if (q->channels == 2) {
00323         q->fmt_conv.float_interleave(samples, (const float **)q->out_samples,
00324                                      AT1_SU_SAMPLES, 2);
00325     }
00326 
00327     *got_frame_ptr   = 1;
00328     *(AVFrame *)data = q->frame;
00329 
00330     return avctx->block_align;
00331 }
00332 
00333 
00334 static av_cold int atrac1_decode_end(AVCodecContext * avctx)
00335 {
00336     AT1Ctx *q = avctx->priv_data;
00337 
00338     av_freep(&q->out_samples[0]);
00339 
00340     ff_mdct_end(&q->mdct_ctx[0]);
00341     ff_mdct_end(&q->mdct_ctx[1]);
00342     ff_mdct_end(&q->mdct_ctx[2]);
00343 
00344     return 0;
00345 }
00346 
00347 
00348 static av_cold int atrac1_decode_init(AVCodecContext *avctx)
00349 {
00350     AT1Ctx *q = avctx->priv_data;
00351     int ret;
00352 
00353     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00354 
00355     if (avctx->channels < 1 || avctx->channels > AT1_MAX_CHANNELS) {
00356         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n",
00357                avctx->channels);
00358         return AVERROR(EINVAL);
00359     }
00360     q->channels = avctx->channels;
00361 
00362     if (avctx->channels == 2) {
00363         q->out_samples[0] = av_malloc(2 * AT1_SU_SAMPLES * sizeof(*q->out_samples[0]));
00364         q->out_samples[1] = q->out_samples[0] + AT1_SU_SAMPLES;
00365         if (!q->out_samples[0]) {
00366             av_freep(&q->out_samples[0]);
00367             return AVERROR(ENOMEM);
00368         }
00369     }
00370 
00371     /* Init the mdct transforms */
00372     if ((ret = ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15))) ||
00373         (ret = ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1 << 15))) ||
00374         (ret = ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1 << 15)))) {
00375         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
00376         atrac1_decode_end(avctx);
00377         return ret;
00378     }
00379 
00380     ff_init_ff_sine_windows(5);
00381 
00382     atrac_generate_tables();
00383 
00384     dsputil_init(&q->dsp, avctx);
00385     ff_fmt_convert_init(&q->fmt_conv, avctx);
00386 
00387     q->bands[0] = q->low;
00388     q->bands[1] = q->mid;
00389     q->bands[2] = q->high;
00390 
00391     /* Prepare the mdct overlap buffers */
00392     q->SUs[0].spectrum[0] = q->SUs[0].spec1;
00393     q->SUs[0].spectrum[1] = q->SUs[0].spec2;
00394     q->SUs[1].spectrum[0] = q->SUs[1].spec1;
00395     q->SUs[1].spectrum[1] = q->SUs[1].spec2;
00396 
00397     avcodec_get_frame_defaults(&q->frame);
00398     avctx->coded_frame = &q->frame;
00399 
00400     return 0;
00401 }
00402 
00403 
00404 AVCodec ff_atrac1_decoder = {
00405     .name = "atrac1",
00406     .type = AVMEDIA_TYPE_AUDIO,
00407     .id = CODEC_ID_ATRAC1,
00408     .priv_data_size = sizeof(AT1Ctx),
00409     .init = atrac1_decode_init,
00410     .close = atrac1_decode_end,
00411     .decode = atrac1_decode_frame,
00412     .capabilities = CODEC_CAP_DR1,
00413     .long_name = NULL_IF_CONFIG_SMALL("Atrac 1 (Adaptive TRansform Acoustic Coding)"),
00414 };