nellymoserdec.c 6.28 KB
Newer Older
Benjamin Larsson's avatar
Benjamin Larsson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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.
 */

/**
29
 * @file
Benjamin Larsson's avatar
Benjamin Larsson committed
30 31 32
 * 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/
 */
33

34
#include "nellymoser.h"
35 36
#include "libavutil/lfg.h"
#include "libavutil/random_seed.h"
37
#include "libavutil/audioconvert.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
38 39
#include "avcodec.h"
#include "dsputil.h"
40
#include "fft.h"
41
#include "fmtconvert.h"
42
#include "sinewin.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
43 44

#define ALT_BITSTREAM_READER_LE
45
#include "get_bits.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
46 47 48 49


typedef struct NellyMoserDecodeContext {
    AVCodecContext* avctx;
50
    DECLARE_ALIGNED(16, float,float_buf)[NELLY_SAMPLES];
51
    float           state[128];
52
    AVLFG           random_state;
Benjamin Larsson's avatar
Benjamin Larsson committed
53
    GetBitContext   gb;
54
    float           scale_bias;
Benjamin Larsson's avatar
Benjamin Larsson committed
55
    DSPContext      dsp;
56
    FFTContext      imdct_ctx;
57
    FmtConvertContext fmt_conv;
58
    DECLARE_ALIGNED(16, float,imdct_out)[NELLY_BUF_LEN * 2];
Benjamin Larsson's avatar
Benjamin Larsson committed
59 60
} NellyMoserDecodeContext;

Michael Niedermayer's avatar
Michael Niedermayer committed
61
static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
Benjamin Larsson's avatar
Benjamin Larsson committed
62
{
Michael Niedermayer's avatar
unused  
Michael Niedermayer committed
63
    int bot, top;
Benjamin Larsson's avatar
Benjamin Larsson committed
64 65 66 67

    bot = 0;
    top = NELLY_BUF_LEN-1;

Michael Niedermayer's avatar
Michael Niedermayer committed
68
    while (bot < NELLY_BUF_LEN) {
69
        audio[bot] = a_in [bot]*ff_sine_128[bot]
70
                    +state[bot]*ff_sine_128[top];
Benjamin Larsson's avatar
Benjamin Larsson committed
71 72 73 74

        bot++;
        top--;
    }
75
    memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
Benjamin Larsson's avatar
Benjamin Larsson committed
76 77
}

78 79 80
static void nelly_decode_block(NellyMoserDecodeContext *s,
                               const unsigned char block[NELLY_BLOCK_LEN],
                               float audio[NELLY_SAMPLES])
Benjamin Larsson's avatar
Benjamin Larsson committed
81 82
{
    int i,j;
83
    float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
Benjamin Larsson's avatar
Benjamin Larsson committed
84 85 86 87 88 89 90 91
    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;
92
    val = ff_nelly_init_table[get_bits(&s->gb, 6)];
Benjamin Larsson's avatar
Benjamin Larsson committed
93 94
    for (i=0 ; i<NELLY_BANDS ; i++) {
        if (i > 0)
95
            val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
96
        pval = -pow(2, val/2048) * s->scale_bias;
97
        for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
Benjamin Larsson's avatar
Benjamin Larsson committed
98 99 100 101 102 103
            *bptr++ = val;
            *pptr++ = pval;
        }

    }

104
    ff_nelly_get_sample_bits(buf, bits);
Benjamin Larsson's avatar
Benjamin Larsson committed
105 106

    for (i = 0; i < 2; i++) {
107 108
        aptr = audio + i * NELLY_BUF_LEN;

Benjamin Larsson's avatar
Benjamin Larsson committed
109
        init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
110
        skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
Benjamin Larsson's avatar
Benjamin Larsson committed
111 112 113

        for (j = 0; j < NELLY_FILL_LEN; j++) {
            if (bits[j] <= 0) {
114
                aptr[j] = M_SQRT1_2*pows[j];
115
                if (av_lfg_get(&s->random_state) & 1)
116
                    aptr[j] *= -1.0;
Benjamin Larsson's avatar
Benjamin Larsson committed
117 118
            } else {
                v = get_bits(&s->gb, bits[j]);
119
                aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
Benjamin Larsson's avatar
Benjamin Larsson committed
120 121
            }
        }
122 123 124
        memset(&aptr[NELLY_FILL_LEN], 0,
               (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));

125
        s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
126 127
        /* XXX: overlapping and windowing should be part of a more
           generic imdct function */
Michael Niedermayer's avatar
Michael Niedermayer committed
128
        overlap_and_window(s, s->state, aptr, s->imdct_out);
Benjamin Larsson's avatar
Benjamin Larsson committed
129 130 131
    }
}

132
static av_cold int decode_init(AVCodecContext * avctx) {
Benjamin Larsson's avatar
Benjamin Larsson committed
133 134 135
    NellyMoserDecodeContext *s = avctx->priv_data;

    s->avctx = avctx;
136
    av_lfg_init(&s->random_state, 0);
137
    ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
138

Benjamin Larsson's avatar
Benjamin Larsson committed
139
    dsputil_init(&s->dsp, avctx);
140
    ff_fmt_convert_init(&s->fmt_conv, avctx);
Benjamin Larsson's avatar
Benjamin Larsson committed
141

142
    s->scale_bias = 1.0/(1*8);
Benjamin Larsson's avatar
Benjamin Larsson committed
143 144

    /* Generate overlap window */
145
    if (!ff_sine_128[127])
146
        ff_init_ff_sine_windows(7);
Benjamin Larsson's avatar
Benjamin Larsson committed
147

148
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
149
    avctx->channel_layout = AV_CH_LAYOUT_MONO;
Benjamin Larsson's avatar
Benjamin Larsson committed
150 151 152 153 154
    return 0;
}

static int decode_tag(AVCodecContext * avctx,
                      void *data, int *data_size,
155 156 157
                      AVPacket *avpkt) {
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
Benjamin Larsson's avatar
Benjamin Larsson committed
158 159 160 161 162 163 164 165 166
    NellyMoserDecodeContext *s = avctx->priv_data;
    int blocks, i;
    int16_t* samples;
    *data_size = 0;
    samples = (int16_t*)data;

    if (buf_size < avctx->block_align)
        return buf_size;

Martin Storsjö's avatar
Martin Storsjö committed
167
    if (buf_size % 64) {
168
        av_log(avctx, AV_LOG_ERROR, "Tag size %d.\n", buf_size);
Martin Storsjö's avatar
Martin Storsjö committed
169 170 171
        return buf_size;
    }
    blocks = buf_size / 64;
172 173 174 175 176 177 178
    /* Normal numbers of blocks for sample rates:
     *  8000 Hz - 1
     * 11025 Hz - 2
     * 16000 Hz - 3
     * 22050 Hz - 4
     * 44100 Hz - 8
     */
Benjamin Larsson's avatar
Benjamin Larsson committed
179 180 181

    for (i=0 ; i<blocks ; i++) {
        nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
182
        s->fmt_conv.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
Benjamin Larsson's avatar
Benjamin Larsson committed
183 184 185
        *data_size += NELLY_SAMPLES*sizeof(int16_t);
    }

186
    return buf_size;
Benjamin Larsson's avatar
Benjamin Larsson committed
187 188
}

189
static av_cold int decode_end(AVCodecContext * avctx) {
Benjamin Larsson's avatar
Benjamin Larsson committed
190 191
    NellyMoserDecodeContext *s = avctx->priv_data;

192
    ff_mdct_end(&s->imdct_ctx);
Benjamin Larsson's avatar
Benjamin Larsson committed
193 194 195
    return 0;
}

196
AVCodec ff_nellymoser_decoder = {
Benjamin Larsson's avatar
Benjamin Larsson committed
197
    "nellymoser",
198
    AVMEDIA_TYPE_AUDIO,
Benjamin Larsson's avatar
Benjamin Larsson committed
199 200 201 202 203 204
    CODEC_ID_NELLYMOSER,
    sizeof(NellyMoserDecodeContext),
    decode_init,
    NULL,
    decode_end,
    decode_tag,
205
    .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
Benjamin Larsson's avatar
Benjamin Larsson committed
206 207
};