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