• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "get_bits.h"
00037 #include "dsputil.h"
00038 #include "fft.h"
00039 #include "fmtconvert.h"
00040 #include "sinewin.h"
00041 
00042 #include "atrac.h"
00043 #include "atrac1data.h"
00044 
00045 #define AT1_MAX_BFU      52                 ///< max number of block floating units in a sound unit
00046 #define AT1_SU_SIZE      212                ///< number of bytes in a sound unit
00047 #define AT1_SU_SAMPLES   512                ///< number of samples in a sound unit
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     float              *out_samples[AT1_MAX_CHANNELS];
00084     FFTContext          mdct_ctx[3];
00085     int                 channels;
00086     DSPContext          dsp;
00087     FmtConvertContext   fmt_conv;
00088 } AT1Ctx;
00089 
00091 static const uint16_t samples_per_band[3] = {128, 128, 256};
00092 static const uint8_t   mdct_long_nbits[3] = {7, 7, 8};
00093 
00094 
00095 static void at1_imdct(AT1Ctx *q, float *spec, float *out, int nbits,
00096                       int rev_spec)
00097 {
00098     FFTContext* mdct_context = &q->mdct_ctx[nbits - 5 - (nbits > 6)];
00099     int transf_size = 1 << nbits;
00100 
00101     if (rev_spec) {
00102         int i;
00103         for (i = 0; i < transf_size / 2; i++)
00104             FFSWAP(float, spec[i], spec[transf_size - 1 - i]);
00105     }
00106     mdct_context->imdct_half(mdct_context, out, spec);
00107 }
00108 
00109 
00110 static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q)
00111 {
00112     int          band_num, band_samples, log2_block_count, nbits, num_blocks, block_size;
00113     unsigned int start_pos, ref_pos = 0, pos = 0;
00114 
00115     for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00116         float *prev_buf;
00117         int j;
00118 
00119         band_samples = samples_per_band[band_num];
00120         log2_block_count = su->log2_block_count[band_num];
00121 
00122         /* number of mdct blocks in the current QMF band: 1 - for long mode */
00123         /* 4 for short mode(low/middle bands) and 8 for short mode(high band)*/
00124         num_blocks = 1 << log2_block_count;
00125 
00126         if (num_blocks == 1) {
00127             /* mdct block size in samples: 128 (long mode, low & mid bands), */
00128             /* 256 (long mode, high band) and 32 (short mode, all bands) */
00129             block_size = band_samples >> log2_block_count;
00130 
00131             /* calc transform size in bits according to the block_size_mode */
00132             nbits = mdct_long_nbits[band_num] - log2_block_count;
00133 
00134             if (nbits != 5 && nbits != 7 && nbits != 8)
00135                 return AVERROR_INVALIDDATA;
00136         } else {
00137             block_size = 32;
00138             nbits = 5;
00139         }
00140 
00141         start_pos = 0;
00142         prev_buf = &su->spectrum[1][ref_pos + band_samples - 16];
00143         for (j=0; j < num_blocks; j++) {
00144             at1_imdct(q, &q->spec[pos], &su->spectrum[0][ref_pos + start_pos], nbits, band_num);
00145 
00146             /* overlap and window */
00147             q->dsp.vector_fmul_window(&q->bands[band_num][start_pos], prev_buf,
00148                                       &su->spectrum[0][ref_pos + start_pos], ff_sine_32, 16);
00149 
00150             prev_buf = &su->spectrum[0][ref_pos+start_pos + 16];
00151             start_pos += block_size;
00152             pos += block_size;
00153         }
00154 
00155         if (num_blocks == 1)
00156             memcpy(q->bands[band_num] + 32, &su->spectrum[0][ref_pos + 16], 240 * sizeof(float));
00157 
00158         ref_pos += band_samples;
00159     }
00160 
00161     /* Swap buffers so the mdct overlap works */
00162     FFSWAP(float*, su->spectrum[0], su->spectrum[1]);
00163 
00164     return 0;
00165 }
00166 
00171 static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS])
00172 {
00173     int log2_block_count_tmp, i;
00174 
00175     for (i = 0; i < 2; i++) {
00176         /* low and mid band */
00177         log2_block_count_tmp = get_bits(gb, 2);
00178         if (log2_block_count_tmp & 1)
00179             return AVERROR_INVALIDDATA;
00180         log2_block_cnt[i] = 2 - log2_block_count_tmp;
00181     }
00182 
00183     /* high band */
00184     log2_block_count_tmp = get_bits(gb, 2);
00185     if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3)
00186         return AVERROR_INVALIDDATA;
00187     log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp;
00188 
00189     skip_bits(gb, 2);
00190     return 0;
00191 }
00192 
00193 
00194 static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su,
00195                               float spec[AT1_SU_SAMPLES])
00196 {
00197     int bits_used, band_num, bfu_num, i;
00198     uint8_t idwls[AT1_MAX_BFU];                 
00199     uint8_t idsfs[AT1_MAX_BFU];                 
00200 
00201     /* parse the info byte (2nd byte) telling how much BFUs were coded */
00202     su->num_bfus = bfu_amount_tab1[get_bits(gb, 3)];
00203 
00204     /* calc number of consumed bits:
00205         num_BFUs * (idwl(4bits) + idsf(6bits)) + log2_block_count(8bits) + info_byte(8bits)
00206         + info_byte_copy(8bits) + log2_block_count_copy(8bits) */
00207     bits_used = su->num_bfus * 10 + 32 +
00208                 bfu_amount_tab2[get_bits(gb, 2)] +
00209                 (bfu_amount_tab3[get_bits(gb, 3)] << 1);
00210 
00211     /* get word length index (idwl) for each BFU */
00212     for (i = 0; i < su->num_bfus; i++)
00213         idwls[i] = get_bits(gb, 4);
00214 
00215     /* get scalefactor index (idsf) for each BFU */
00216     for (i = 0; i < su->num_bfus; i++)
00217         idsfs[i] = get_bits(gb, 6);
00218 
00219     /* zero idwl/idsf for empty BFUs */
00220     for (i = su->num_bfus; i < AT1_MAX_BFU; i++)
00221         idwls[i] = idsfs[i] = 0;
00222 
00223     /* read in the spectral data and reconstruct MDCT spectrum of this channel */
00224     for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {
00225         for (bfu_num = bfu_bands_t[band_num]; bfu_num < bfu_bands_t[band_num+1]; bfu_num++) {
00226             int pos;
00227 
00228             int num_specs = specs_per_bfu[bfu_num];
00229             int word_len  = !!idwls[bfu_num] + idwls[bfu_num];
00230             float scale_factor = ff_atrac_sf_table[idsfs[bfu_num]];
00231             bits_used += word_len * num_specs; /* add number of bits consumed by current BFU */
00232 
00233             /* check for bitstream overflow */
00234             if (bits_used > AT1_SU_MAX_BITS)
00235                 return AVERROR_INVALIDDATA;
00236 
00237             /* get the position of the 1st spec according to the block size mode */
00238             pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];
00239 
00240             if (word_len) {
00241                 float   max_quant = 1.0 / (float)((1 << (word_len - 1)) - 1);
00242 
00243                 for (i = 0; i < num_specs; i++) {
00244                     /* read in a quantized spec and convert it to
00245                      * signed int and then inverse quantization
00246                      */
00247                     spec[pos+i] = get_sbits(gb, word_len) * scale_factor * max_quant;
00248                 }
00249             } else { /* word_len = 0 -> empty BFU, zero all specs in the emty BFU */
00250                 memset(&spec[pos], 0, num_specs * sizeof(float));
00251             }
00252         }
00253     }
00254 
00255     return 0;
00256 }
00257 
00258 
00259 static void at1_subband_synthesis(AT1Ctx *q, AT1SUCtx* su, float *pOut)
00260 {
00261     float temp[256];
00262     float iqmf_temp[512 + 46];
00263 
00264     /* combine low and middle bands */
00265     atrac_iqmf(q->bands[0], q->bands[1], 128, temp, su->fst_qmf_delay, iqmf_temp);
00266 
00267     /* delay the signal of the high band by 23 samples */
00268     memcpy( su->last_qmf_delay,    &su->last_qmf_delay[256], sizeof(float) *  23);
00269     memcpy(&su->last_qmf_delay[23], q->bands[2],             sizeof(float) * 256);
00270 
00271     /* combine (low + middle) and high bands */
00272     atrac_iqmf(temp, su->last_qmf_delay, 256, pOut, su->snd_qmf_delay, iqmf_temp);
00273 }
00274 
00275 
00276 static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
00277                                int *got_frame_ptr, AVPacket *avpkt)
00278 {
00279     const uint8_t *buf = avpkt->data;
00280     int buf_size       = avpkt->size;
00281     AT1Ctx *q          = avctx->priv_data;
00282     int ch, ret;
00283     GetBitContext gb;
00284     float *samples;
00285 
00286 
00287     if (buf_size < 212 * q->channels) {
00288         av_log(avctx, AV_LOG_ERROR, "Not enough data to decode!\n");
00289         return AVERROR_INVALIDDATA;
00290     }
00291 
00292     /* get output buffer */
00293     q->frame.nb_samples = AT1_SU_SAMPLES;
00294     if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
00295         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00296         return ret;
00297     }
00298     samples = (float *)q->frame.data[0];
00299 
00300     for (ch = 0; ch < q->channels; ch++) {
00301         AT1SUCtx* su = &q->SUs[ch];
00302 
00303         init_get_bits(&gb, &buf[212 * ch], 212 * 8);
00304 
00305         /* parse block_size_mode, 1st byte */
00306         ret = at1_parse_bsm(&gb, su->log2_block_count);
00307         if (ret < 0)
00308             return ret;
00309 
00310         ret = at1_unpack_dequant(&gb, su, q->spec);
00311         if (ret < 0)
00312             return ret;
00313 
00314         ret = at1_imdct_block(su, q);
00315         if (ret < 0)
00316             return ret;
00317         at1_subband_synthesis(q, su, q->channels == 1 ? samples : q->out_samples[ch]);
00318     }
00319 
00320     /* interleave */
00321     if (q->channels == 2) {
00322         q->fmt_conv.float_interleave(samples, (const float **)q->out_samples,
00323                                      AT1_SU_SAMPLES, 2);
00324     }
00325 
00326     *got_frame_ptr   = 1;
00327     *(AVFrame *)data = q->frame;
00328 
00329     return avctx->block_align;
00330 }
00331 
00332 
00333 static av_cold int atrac1_decode_end(AVCodecContext * avctx)
00334 {
00335     AT1Ctx *q = avctx->priv_data;
00336 
00337     av_freep(&q->out_samples[0]);
00338 
00339     ff_mdct_end(&q->mdct_ctx[0]);
00340     ff_mdct_end(&q->mdct_ctx[1]);
00341     ff_mdct_end(&q->mdct_ctx[2]);
00342 
00343     return 0;
00344 }
00345 
00346 
00347 static av_cold int atrac1_decode_init(AVCodecContext *avctx)
00348 {
00349     AT1Ctx *q = avctx->priv_data;
00350     int ret;
00351 
00352     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00353 
00354     if (avctx->channels < 1 || avctx->channels > AT1_MAX_CHANNELS) {
00355         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n",
00356                avctx->channels);
00357         return AVERROR(EINVAL);
00358     }
00359     q->channels = avctx->channels;
00360 
00361     if (avctx->channels == 2) {
00362         q->out_samples[0] = av_malloc(2 * AT1_SU_SAMPLES * sizeof(*q->out_samples[0]));
00363         q->out_samples[1] = q->out_samples[0] + AT1_SU_SAMPLES;
00364         if (!q->out_samples[0]) {
00365             av_freep(&q->out_samples[0]);
00366             return AVERROR(ENOMEM);
00367         }
00368     }
00369 
00370     /* Init the mdct transforms */
00371     if ((ret = ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15))) ||
00372         (ret = ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1 << 15))) ||
00373         (ret = ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1 << 15)))) {
00374         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
00375         atrac1_decode_end(avctx);
00376         return ret;
00377     }
00378 
00379     ff_init_ff_sine_windows(5);
00380 
00381     atrac_generate_tables();
00382 
00383     dsputil_init(&q->dsp, avctx);
00384     ff_fmt_convert_init(&q->fmt_conv, avctx);
00385 
00386     q->bands[0] = q->low;
00387     q->bands[1] = q->mid;
00388     q->bands[2] = q->high;
00389 
00390     /* Prepare the mdct overlap buffers */
00391     q->SUs[0].spectrum[0] = q->SUs[0].spec1;
00392     q->SUs[0].spectrum[1] = q->SUs[0].spec2;
00393     q->SUs[1].spectrum[0] = q->SUs[1].spec1;
00394     q->SUs[1].spectrum[1] = q->SUs[1].spec2;
00395 
00396     avcodec_get_frame_defaults(&q->frame);
00397     avctx->coded_frame = &q->frame;
00398 
00399     return 0;
00400 }
00401 
00402 
00403 AVCodec ff_atrac1_decoder = {
00404     .name = "atrac1",
00405     .type = AVMEDIA_TYPE_AUDIO,
00406     .id = CODEC_ID_ATRAC1,
00407     .priv_data_size = sizeof(AT1Ctx),
00408     .init = atrac1_decode_init,
00409     .close = atrac1_decode_end,
00410     .decode = atrac1_decode_frame,
00411     .capabilities = CODEC_CAP_DR1,
00412     .long_name = NULL_IF_CONFIG_SMALL("Atrac 1 (Adaptive TRansform Acoustic Coding)"),
00413 };
Generated on Fri Feb 1 2013 14:34:30 for FFmpeg by doxygen 1.7.1