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/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
00122
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
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 data_max = *data_size;
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 switch (buf_size) {
00167 case 64:
00168 blocks = 1; break;
00169 case 128:
00170 blocks = 2; break;
00171 case 256:
00172 blocks = 4; break;
00173 case 512:
00174 blocks = 8; break;
00175 default:
00176 av_log(avctx, AV_LOG_DEBUG, "Tag size %d.\n", buf_size);
00177 return buf_size;
00178 }
00179
00180 for (i=0 ; i<blocks ; i++) {
00181 if ((i + 1) * NELLY_SAMPLES * sizeof(int16_t) > data_max)
00182 return i > 0 ? i * NELLY_BLOCK_LEN : -1;
00183 nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
00184 s->dsp.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
00185 *data_size += NELLY_SAMPLES*sizeof(int16_t);
00186 }
00187
00188 return buf_size;
00189 }
00190
00191 static av_cold int decode_end(AVCodecContext * avctx) {
00192 NellyMoserDecodeContext *s = avctx->priv_data;
00193
00194 ff_mdct_end(&s->imdct_ctx);
00195 return 0;
00196 }
00197
00198 AVCodec nellymoser_decoder = {
00199 "nellymoser",
00200 CODEC_TYPE_AUDIO,
00201 CODEC_ID_NELLYMOSER,
00202 sizeof(NellyMoserDecodeContext),
00203 decode_init,
00204 NULL,
00205 decode_end,
00206 decode_tag,
00207 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
00208 };
00209