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

libavcodec/nellymoserdec.c

Go to the documentation of this file.
00001 /*
00002  * NellyMoser audio decoder
00003  * Copyright (c) 2007 a840bda5870ba11f19698ff6eb9581dfb0f95fa5,
00004  *                    539459aeb7d425140b62a3ec7dbf6dc8e408a306, and
00005  *                    520e17cd55896441042b14df2566a6eb610ed444
00006  * Copyright (c) 2007 Loic Minier <lool at dooz.org>
00007  *                    Benjamin Larsson
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a
00010  * copy of this software and associated documentation files (the "Software"),
00011  * to deal in the Software without restriction, including without limitation
00012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00013  * and/or sell copies of the Software, and to permit persons to whom the
00014  * Software is furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  */
00027 
00034 #include "nellymoser.h"
00035 #include "libavutil/lfg.h"
00036 #include "libavutil/random_seed.h"
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "fft.h"
00040 
00041 #define ALT_BITSTREAM_READER_LE
00042 #include "get_bits.h"
00043 
00044 
00045 typedef struct NellyMoserDecodeContext {
00046     AVCodecContext* avctx;
00047     DECLARE_ALIGNED(16, float,float_buf)[NELLY_SAMPLES];
00048     float           state[128];
00049     AVLFG           random_state;
00050     GetBitContext   gb;
00051     int             add_bias;
00052     float           scale_bias;
00053     DSPContext      dsp;
00054     FFTContext      imdct_ctx;
00055     DECLARE_ALIGNED(16, float,imdct_out)[NELLY_BUF_LEN * 2];
00056 } NellyMoserDecodeContext;
00057 
00058 static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
00059 {
00060     int bot, top;
00061 
00062     bot = 0;
00063     top = NELLY_BUF_LEN-1;
00064 
00065     while (bot < NELLY_BUF_LEN) {
00066         audio[bot] = a_in [bot]*ff_sine_128[bot]
00067                     +state[bot]*ff_sine_128[top] + s->add_bias;
00068 
00069         bot++;
00070         top--;
00071     }
00072     memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
00073 }
00074 
00075 static void nelly_decode_block(NellyMoserDecodeContext *s,
00076                                const unsigned char block[NELLY_BLOCK_LEN],
00077                                float audio[NELLY_SAMPLES])
00078 {
00079     int i,j;
00080     float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
00081     float *aptr, *bptr, *pptr, val, pval;
00082     int bits[NELLY_BUF_LEN];
00083     unsigned char v;
00084 
00085     init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00086 
00087     bptr = buf;
00088     pptr = pows;
00089     val = ff_nelly_init_table[get_bits(&s->gb, 6)];
00090     for (i=0 ; i<NELLY_BANDS ; i++) {
00091         if (i > 0)
00092             val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
00093         pval = -pow(2, val/2048) * s->scale_bias;
00094         for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
00095             *bptr++ = val;
00096             *pptr++ = pval;
00097         }
00098 
00099     }
00100 
00101     ff_nelly_get_sample_bits(buf, bits);
00102 
00103     for (i = 0; i < 2; i++) {
00104         aptr = audio + i * NELLY_BUF_LEN;
00105 
00106         init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00107         skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
00108 
00109         for (j = 0; j < NELLY_FILL_LEN; j++) {
00110             if (bits[j] <= 0) {
00111                 aptr[j] = M_SQRT1_2*pows[j];
00112                 if (av_lfg_get(&s->random_state) & 1)
00113                     aptr[j] *= -1.0;
00114             } else {
00115                 v = get_bits(&s->gb, bits[j]);
00116                 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
00117             }
00118         }
00119         memset(&aptr[NELLY_FILL_LEN], 0,
00120                (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
00121 
00122         ff_imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
00123         /* XXX: overlapping and windowing should be part of a more
00124            generic imdct function */
00125         overlap_and_window(s, s->state, aptr, s->imdct_out);
00126     }
00127 }
00128 
00129 static av_cold int decode_init(AVCodecContext * avctx) {
00130     NellyMoserDecodeContext *s = avctx->priv_data;
00131 
00132     s->avctx = avctx;
00133     av_lfg_init(&s->random_state, 0);
00134     ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
00135 
00136     dsputil_init(&s->dsp, avctx);
00137 
00138     if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
00139         s->add_bias = 385;
00140         s->scale_bias = 1.0/(8*32768);
00141     } else {
00142         s->add_bias = 0;
00143         s->scale_bias = 1.0/(1*8);
00144     }
00145 
00146     /* Generate overlap window */
00147     if (!ff_sine_128[127])
00148         ff_init_ff_sine_windows(7);
00149 
00150     avctx->sample_fmt = SAMPLE_FMT_S16;
00151     avctx->channel_layout = CH_LAYOUT_MONO;
00152     return 0;
00153 }
00154 
00155 static int decode_tag(AVCodecContext * avctx,
00156                       void *data, int *data_size,
00157                       AVPacket *avpkt) {
00158     const uint8_t *buf = avpkt->data;
00159     int buf_size = avpkt->size;
00160     NellyMoserDecodeContext *s = avctx->priv_data;
00161     int blocks, i;
00162     int16_t* samples;
00163     *data_size = 0;
00164     samples = (int16_t*)data;
00165 
00166     if (buf_size < avctx->block_align)
00167         return buf_size;
00168 
00169     switch (buf_size) {
00170         case 64:    // 8000Hz
00171             blocks = 1; break;
00172         case 128:   // 11025Hz
00173             blocks = 2; break;
00174         case 192:   // 16000Hz
00175             blocks = 3; break;
00176         case 256:   // 22050Hz
00177             blocks = 4; break;
00178         case 512:   // 44100Hz
00179             blocks = 8; break;
00180         default:
00181             av_log(avctx, AV_LOG_DEBUG, "Tag size %d.\n", buf_size);
00182             return buf_size;
00183     }
00184 
00185     for (i=0 ; i<blocks ; i++) {
00186         nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
00187         s->dsp.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
00188         *data_size += NELLY_SAMPLES*sizeof(int16_t);
00189     }
00190 
00191     return buf_size;
00192 }
00193 
00194 static av_cold int decode_end(AVCodecContext * avctx) {
00195     NellyMoserDecodeContext *s = avctx->priv_data;
00196 
00197     ff_mdct_end(&s->imdct_ctx);
00198     return 0;
00199 }
00200 
00201 AVCodec nellymoser_decoder = {
00202     "nellymoser",
00203     AVMEDIA_TYPE_AUDIO,
00204     CODEC_ID_NELLYMOSER,
00205     sizeof(NellyMoserDecodeContext),
00206     decode_init,
00207     NULL,
00208     decode_end,
00209     decode_tag,
00210     .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
00211 };
00212 

Generated on Fri Sep 16 2011 17:17:41 for FFmpeg by  doxygen 1.7.1