libavcodec/nellymoserdec.c
636b13c5
 /*
  * NellyMoser audio decoder
  * Copyright (c) 2007 a840bda5870ba11f19698ff6eb9581dfb0f95fa5,
  *                    539459aeb7d425140b62a3ec7dbf6dc8e408a306, and
  *                    520e17cd55896441042b14df2566a6eb610ed444
  * Copyright (c) 2007 Loic Minier <lool at dooz.org>
  *                    Benjamin Larsson
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
 
 /**
ba87f080
  * @file
636b13c5
  * The 3 alphanumeric copyright notices are md5summed they are from the original
  * implementors. The original code is available from http://code.google.com/p/nelly2pcm/
  */
245976da
 
681f5c12
 #include "nellymoser.h"
3b0b8479
 #include "libavutil/lfg.h"
 #include "libavutil/random_seed.h"
737eb597
 #include "libavutil/audioconvert.h"
636b13c5
 #include "avcodec.h"
 #include "dsputil.h"
1429224b
 #include "fft.h"
c73d99e6
 #include "fmtconvert.h"
4538729a
 #include "sinewin.h"
636b13c5
 
 #define ALT_BITSTREAM_READER_LE
9106a698
 #include "get_bits.h"
636b13c5
 
 
 typedef struct NellyMoserDecodeContext {
     AVCodecContext* avctx;
9d35fa52
     DECLARE_ALIGNED(32, float, float_buf)[NELLY_SAMPLES];
5c656605
     float           state[128];
3b0b8479
     AVLFG           random_state;
636b13c5
     GetBitContext   gb;
5d32325a
     float           scale_bias;
636b13c5
     DSPContext      dsp;
01b22147
     FFTContext      imdct_ctx;
c73d99e6
     FmtConvertContext fmt_conv;
9d35fa52
     DECLARE_ALIGNED(32, float, imdct_out)[NELLY_BUF_LEN * 2];
636b13c5
 } NellyMoserDecodeContext;
 
e616c6d6
 static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
636b13c5
 {
efe6079d
     int bot, top;
636b13c5
 
     bot = 0;
     top = NELLY_BUF_LEN-1;
 
687550d3
     while (bot < NELLY_BUF_LEN) {
8a569fee
         audio[bot] = a_in [bot]*ff_sine_128[bot]
9d06d7bc
                     +state[bot]*ff_sine_128[top];
636b13c5
 
         bot++;
         top--;
     }
5c656605
     memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
636b13c5
 }
 
53e85f5f
 static void nelly_decode_block(NellyMoserDecodeContext *s,
                                const unsigned char block[NELLY_BLOCK_LEN],
                                float audio[NELLY_SAMPLES])
636b13c5
 {
     int i,j;
eb1c687b
     float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
636b13c5
     float *aptr, *bptr, *pptr, val, pval;
     int bits[NELLY_BUF_LEN];
     unsigned char v;
 
     init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
 
     bptr = buf;
     pptr = pows;
681f5c12
     val = ff_nelly_init_table[get_bits(&s->gb, 6)];
636b13c5
     for (i=0 ; i<NELLY_BANDS ; i++) {
         if (i > 0)
681f5c12
             val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
a1b914b2
         pval = -pow(2, val/2048) * s->scale_bias;
681f5c12
         for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
636b13c5
             *bptr++ = val;
             *pptr++ = pval;
         }
 
     }
 
681f5c12
     ff_nelly_get_sample_bits(buf, bits);
636b13c5
 
     for (i = 0; i < 2; i++) {
eb1c687b
         aptr = audio + i * NELLY_BUF_LEN;
 
636b13c5
         init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
0a8dedc9
         skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
636b13c5
 
         for (j = 0; j < NELLY_FILL_LEN; j++) {
             if (bits[j] <= 0) {
eb1c687b
                 aptr[j] = M_SQRT1_2*pows[j];
3b0b8479
                 if (av_lfg_get(&s->random_state) & 1)
eb1c687b
                     aptr[j] *= -1.0;
636b13c5
             } else {
                 v = get_bits(&s->gb, bits[j]);
681f5c12
                 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
636b13c5
             }
         }
eb1c687b
         memset(&aptr[NELLY_FILL_LEN], 0,
                (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
 
26f548bb
         s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
eb1c687b
         /* XXX: overlapping and windowing should be part of a more
            generic imdct function */
e616c6d6
         overlap_and_window(s, s->state, aptr, s->imdct_out);
636b13c5
     }
 }
 
98a6fff9
 static av_cold int decode_init(AVCodecContext * avctx) {
636b13c5
     NellyMoserDecodeContext *s = avctx->priv_data;
 
     s->avctx = avctx;
b8cef7be
     av_lfg_init(&s->random_state, 0);
7d485f16
     ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
eb1c687b
 
636b13c5
     dsputil_init(&s->dsp, avctx);
c73d99e6
     ff_fmt_convert_init(&s->fmt_conv, avctx);
636b13c5
 
b5ec6383
     s->scale_bias = 1.0/(1*8);
636b13c5
 
     /* Generate overlap window */
8a569fee
     if (!ff_sine_128[127])
14b86070
         ff_init_ff_sine_windows(7);
636b13c5
 
5d6e4c16
     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
63e8d976
     avctx->channel_layout = AV_CH_LAYOUT_MONO;
636b13c5
     return 0;
 }
 
 static int decode_tag(AVCodecContext * avctx,
                       void *data, int *data_size,
7a00bbad
                       AVPacket *avpkt) {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
636b13c5
     NellyMoserDecodeContext *s = avctx->priv_data;
533dbaa5
     int data_max = *data_size;
636b13c5
     int blocks, i;
     int16_t* samples;
     *data_size = 0;
     samples = (int16_t*)data;
 
     if (buf_size < avctx->block_align)
         return buf_size;
 
133cd270
     if (buf_size % 64) {
caea217d
         av_log(avctx, AV_LOG_ERROR, "Tag size %d.\n", buf_size);
133cd270
         return buf_size;
     }
     blocks = buf_size / 64;
c5d68fbd
     /* Normal numbers of blocks for sample rates:
      *  8000 Hz - 1
      * 11025 Hz - 2
      * 16000 Hz - 3
      * 22050 Hz - 4
      * 44100 Hz - 8
      */
636b13c5
 
     for (i=0 ; i<blocks ; i++) {
533dbaa5
         if ((i + 1) * NELLY_SAMPLES * sizeof(int16_t) > data_max)
             return i > 0 ? i * NELLY_BLOCK_LEN : -1;
636b13c5
         nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
c73d99e6
         s->fmt_conv.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
636b13c5
         *data_size += NELLY_SAMPLES*sizeof(int16_t);
     }
 
5b0b5ecf
     return buf_size;
636b13c5
 }
 
98a6fff9
 static av_cold int decode_end(AVCodecContext * avctx) {
636b13c5
     NellyMoserDecodeContext *s = avctx->priv_data;
 
eb1c687b
     ff_mdct_end(&s->imdct_ctx);
636b13c5
     return 0;
 }
 
d36beb3f
 AVCodec ff_nellymoser_decoder = {
636b13c5
     "nellymoser",
72415b2a
     AVMEDIA_TYPE_AUDIO,
636b13c5
     CODEC_ID_NELLYMOSER,
     sizeof(NellyMoserDecodeContext),
     decode_init,
     NULL,
     decode_end,
     decode_tag,
fe4bf374
     .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
636b13c5
 };