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