• 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/random.h"
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 
00039 #define ALT_BITSTREAM_READER_LE
00040 #include "bitstream.h"
00041 
00042 
00043 typedef struct NellyMoserDecodeContext {
00044     AVCodecContext* avctx;
00045     DECLARE_ALIGNED_16(float,float_buf[NELLY_SAMPLES]);
00046     float           state[128];
00047     AVRandomState   random_state;
00048     GetBitContext   gb;
00049     int             add_bias;
00050     float           scale_bias;
00051     DSPContext      dsp;
00052     MDCTContext     imdct_ctx;
00053     DECLARE_ALIGNED_16(float,imdct_out[NELLY_BUF_LEN * 2]);
00054 } NellyMoserDecodeContext;
00055 
00056 static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
00057 {
00058     int bot, top;
00059 
00060     bot = 0;
00061     top = NELLY_BUF_LEN-1;
00062 
00063     while (bot < NELLY_BUF_LEN) {
00064         audio[bot] = a_in [bot]*ff_sine_128[bot]
00065                     +state[bot]*ff_sine_128[top] + s->add_bias;
00066 
00067         bot++;
00068         top--;
00069     }
00070     memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
00071 }
00072 
00073 static void nelly_decode_block(NellyMoserDecodeContext *s,
00074                                const unsigned char block[NELLY_BLOCK_LEN],
00075                                float audio[NELLY_SAMPLES])
00076 {
00077     int i,j;
00078     float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
00079     float *aptr, *bptr, *pptr, val, pval;
00080     int bits[NELLY_BUF_LEN];
00081     unsigned char v;
00082 
00083     init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00084 
00085     bptr = buf;
00086     pptr = pows;
00087     val = ff_nelly_init_table[get_bits(&s->gb, 6)];
00088     for (i=0 ; i<NELLY_BANDS ; i++) {
00089         if (i > 0)
00090             val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
00091         pval = -pow(2, val/2048) * s->scale_bias;
00092         for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
00093             *bptr++ = val;
00094             *pptr++ = pval;
00095         }
00096 
00097     }
00098 
00099     ff_nelly_get_sample_bits(buf, bits);
00100 
00101     for (i = 0; i < 2; i++) {
00102         aptr = audio + i * NELLY_BUF_LEN;
00103 
00104         init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00105         skip_bits(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
00106 
00107         for (j = 0; j < NELLY_FILL_LEN; j++) {
00108             if (bits[j] <= 0) {
00109                 aptr[j] = M_SQRT1_2*pows[j];
00110                 if (av_random(&s->random_state) & 1)
00111                     aptr[j] *= -1.0;
00112             } else {
00113                 v = get_bits(&s->gb, bits[j]);
00114                 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
00115             }
00116         }
00117         memset(&aptr[NELLY_FILL_LEN], 0,
00118                (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
00119 
00120         ff_imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
00121         /* XXX: overlapping and windowing should be part of a more
00122            generic imdct function */
00123         overlap_and_window(s, s->state, aptr, s->imdct_out);
00124     }
00125 }
00126 
00127 static av_cold int decode_init(AVCodecContext * avctx) {
00128     NellyMoserDecodeContext *s = avctx->priv_data;
00129 
00130     s->avctx = avctx;
00131     av_random_init(&s->random_state, 0);
00132     ff_mdct_init(&s->imdct_ctx, 8, 1);
00133 
00134     dsputil_init(&s->dsp, avctx);
00135 
00136     if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
00137         s->add_bias = 385;
00138         s->scale_bias = 1.0/(8*32768);
00139     } else {
00140         s->add_bias = 0;
00141         s->scale_bias = 1.0/(1*8);
00142     }
00143 
00144     /* Generate overlap window */
00145     if (!ff_sine_128[127])
00146         ff_sine_window_init(ff_sine_128, 128);
00147 
00148     avctx->sample_fmt = SAMPLE_FMT_S16;
00149     avctx->channel_layout = CH_LAYOUT_MONO;
00150     return 0;
00151 }
00152 
00153 static int decode_tag(AVCodecContext * avctx,
00154                       void *data, int *data_size,
00155                       const uint8_t * buf, int buf_size) {
00156     NellyMoserDecodeContext *s = avctx->priv_data;
00157     int blocks, i;
00158     int16_t* samples;
00159     *data_size = 0;
00160     samples = (int16_t*)data;
00161 
00162     if (buf_size < avctx->block_align)
00163         return buf_size;
00164 
00165     switch (buf_size) {
00166         case 64:    // 8000Hz
00167             blocks = 1; break;
00168         case 128:   // 11025Hz
00169             blocks = 2; break;
00170         case 256:   // 22050Hz
00171             blocks = 4; break;
00172         case 512:   // 44100Hz
00173             blocks = 8; break;
00174         default:
00175             av_log(avctx, AV_LOG_DEBUG, "Tag size %d.\n", buf_size);
00176             return buf_size;
00177     }
00178 
00179     for (i=0 ; i<blocks ; i++) {
00180         nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
00181         s->dsp.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
00182         *data_size += NELLY_SAMPLES*sizeof(int16_t);
00183     }
00184 
00185     return buf_size;
00186 }
00187 
00188 static av_cold int decode_end(AVCodecContext * avctx) {
00189     NellyMoserDecodeContext *s = avctx->priv_data;
00190 
00191     ff_mdct_end(&s->imdct_ctx);
00192     return 0;
00193 }
00194 
00195 AVCodec nellymoser_decoder = {
00196     "nellymoser",
00197     CODEC_TYPE_AUDIO,
00198     CODEC_ID_NELLYMOSER,
00199     sizeof(NellyMoserDecodeContext),
00200     decode_init,
00201     NULL,
00202     decode_end,
00203     decode_tag,
00204     .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
00205 };
00206 

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1