00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #include "nellymoser.h"
00035 #include "libavutil/lfg.h"
00036 #include "libavutil/random_seed.h"
00037 #include "libavutil/audioconvert.h"
00038 #include "avcodec.h"
00039 #include "dsputil.h"
00040 #include "fft.h"
00041 #include "fmtconvert.h"
00042
00043 #define ALT_BITSTREAM_READER_LE
00044 #include "get_bits.h"
00045
00046
00047 typedef struct NellyMoserDecodeContext {
00048 AVCodecContext* avctx;
00049 DECLARE_ALIGNED(16, float,float_buf)[NELLY_SAMPLES];
00050 float state[128];
00051 AVLFG random_state;
00052 GetBitContext gb;
00053 float scale_bias;
00054 DSPContext dsp;
00055 FFTContext imdct_ctx;
00056 FmtConvertContext fmt_conv;
00057 DECLARE_ALIGNED(16, float,imdct_out)[NELLY_BUF_LEN * 2];
00058 } NellyMoserDecodeContext;
00059
00060 static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
00061 {
00062 int bot, top;
00063
00064 bot = 0;
00065 top = NELLY_BUF_LEN-1;
00066
00067 while (bot < NELLY_BUF_LEN) {
00068 audio[bot] = a_in [bot]*ff_sine_128[bot]
00069 +state[bot]*ff_sine_128[top];
00070
00071 bot++;
00072 top--;
00073 }
00074 memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
00075 }
00076
00077 static void nelly_decode_block(NellyMoserDecodeContext *s,
00078 const unsigned char block[NELLY_BLOCK_LEN],
00079 float audio[NELLY_SAMPLES])
00080 {
00081 int i,j;
00082 float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
00083 float *aptr, *bptr, *pptr, val, pval;
00084 int bits[NELLY_BUF_LEN];
00085 unsigned char v;
00086
00087 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00088
00089 bptr = buf;
00090 pptr = pows;
00091 val = ff_nelly_init_table[get_bits(&s->gb, 6)];
00092 for (i=0 ; i<NELLY_BANDS ; i++) {
00093 if (i > 0)
00094 val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
00095 pval = -pow(2, val/2048) * s->scale_bias;
00096 for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
00097 *bptr++ = val;
00098 *pptr++ = pval;
00099 }
00100
00101 }
00102
00103 ff_nelly_get_sample_bits(buf, bits);
00104
00105 for (i = 0; i < 2; i++) {
00106 aptr = audio + i * NELLY_BUF_LEN;
00107
00108 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00109 skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
00110
00111 for (j = 0; j < NELLY_FILL_LEN; j++) {
00112 if (bits[j] <= 0) {
00113 aptr[j] = M_SQRT1_2*pows[j];
00114 if (av_lfg_get(&s->random_state) & 1)
00115 aptr[j] *= -1.0;
00116 } else {
00117 v = get_bits(&s->gb, bits[j]);
00118 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
00119 }
00120 }
00121 memset(&aptr[NELLY_FILL_LEN], 0,
00122 (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
00123
00124 ff_imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
00125
00126
00127 overlap_and_window(s, s->state, aptr, s->imdct_out);
00128 }
00129 }
00130
00131 static av_cold int decode_init(AVCodecContext * avctx) {
00132 NellyMoserDecodeContext *s = avctx->priv_data;
00133
00134 s->avctx = avctx;
00135 av_lfg_init(&s->random_state, 0);
00136 ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
00137
00138 dsputil_init(&s->dsp, avctx);
00139 ff_fmt_convert_init(&s->fmt_conv, avctx);
00140
00141 s->scale_bias = 1.0/(1*8);
00142
00143
00144 if (!ff_sine_128[127])
00145 ff_init_ff_sine_windows(7);
00146
00147 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00148 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00149 return 0;
00150 }
00151
00152 static int decode_tag(AVCodecContext * avctx,
00153 void *data, int *data_size,
00154 AVPacket *avpkt) {
00155 const uint8_t *buf = avpkt->data;
00156 int buf_size = avpkt->size;
00157 NellyMoserDecodeContext *s = avctx->priv_data;
00158 int blocks, i;
00159 int16_t* samples;
00160 *data_size = 0;
00161 samples = (int16_t*)data;
00162
00163 if (buf_size < avctx->block_align)
00164 return buf_size;
00165
00166 if (buf_size % 64) {
00167 av_log(avctx, AV_LOG_ERROR, "Tag size %d.\n", buf_size);
00168 return buf_size;
00169 }
00170 blocks = buf_size / 64;
00171
00172
00173
00174
00175
00176
00177
00178
00179 for (i=0 ; i<blocks ; i++) {
00180 nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
00181 s->fmt_conv.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 ff_nellymoser_decoder = {
00196 "nellymoser",
00197 AVMEDIA_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