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 BITSTREAM_READER_LE
00045 #include "get_bits.h"
00046
00047
00048 typedef struct NellyMoserDecodeContext {
00049 AVCodecContext* avctx;
00050 AVFrame frame;
00051 AVLFG random_state;
00052 GetBitContext gb;
00053 float scale_bias;
00054 DSPContext dsp;
00055 FFTContext imdct_ctx;
00056 DECLARE_ALIGNED(32, float, imdct_buf)[2][NELLY_BUF_LEN];
00057 float *imdct_out;
00058 float *imdct_prev;
00059 } NellyMoserDecodeContext;
00060
00061 static void nelly_decode_block(NellyMoserDecodeContext *s,
00062 const unsigned char block[NELLY_BLOCK_LEN],
00063 float audio[NELLY_SAMPLES])
00064 {
00065 int i,j;
00066 float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
00067 float *aptr, *bptr, *pptr, val, pval;
00068 int bits[NELLY_BUF_LEN];
00069 unsigned char v;
00070
00071 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00072
00073 bptr = buf;
00074 pptr = pows;
00075 val = ff_nelly_init_table[get_bits(&s->gb, 6)];
00076 for (i=0 ; i<NELLY_BANDS ; i++) {
00077 if (i > 0)
00078 val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
00079 pval = -pow(2, val/2048) * s->scale_bias;
00080 for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
00081 *bptr++ = val;
00082 *pptr++ = pval;
00083 }
00084
00085 }
00086
00087 ff_nelly_get_sample_bits(buf, bits);
00088
00089 for (i = 0; i < 2; i++) {
00090 aptr = audio + i * NELLY_BUF_LEN;
00091
00092 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
00093 skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
00094
00095 for (j = 0; j < NELLY_FILL_LEN; j++) {
00096 if (bits[j] <= 0) {
00097 aptr[j] = M_SQRT1_2*pows[j];
00098 if (av_lfg_get(&s->random_state) & 1)
00099 aptr[j] *= -1.0;
00100 } else {
00101 v = get_bits(&s->gb, bits[j]);
00102 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
00103 }
00104 }
00105 memset(&aptr[NELLY_FILL_LEN], 0,
00106 (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
00107
00108 s->imdct_ctx.imdct_half(&s->imdct_ctx, s->imdct_out, aptr);
00109 s->dsp.vector_fmul_window(aptr, s->imdct_prev + NELLY_BUF_LEN/2, s->imdct_out, ff_sine_128, NELLY_BUF_LEN/2);
00110 FFSWAP(float *, s->imdct_out, s->imdct_prev);
00111 }
00112 }
00113
00114 static av_cold int decode_init(AVCodecContext * avctx) {
00115 NellyMoserDecodeContext *s = avctx->priv_data;
00116
00117 s->avctx = avctx;
00118 s->imdct_out = s->imdct_buf[0];
00119 s->imdct_prev = s->imdct_buf[1];
00120 av_lfg_init(&s->random_state, 0);
00121 ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
00122
00123 ff_dsputil_init(&s->dsp, avctx);
00124
00125 s->scale_bias = 1.0/(32768*8);
00126 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00127
00128
00129 if (!ff_sine_128[127])
00130 ff_init_ff_sine_windows(7);
00131
00132 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00133
00134 avcodec_get_frame_defaults(&s->frame);
00135 avctx->coded_frame = &s->frame;
00136
00137 return 0;
00138 }
00139
00140 static int decode_tag(AVCodecContext *avctx, void *data,
00141 int *got_frame_ptr, AVPacket *avpkt)
00142 {
00143 const uint8_t *buf = avpkt->data;
00144 const uint8_t *side=av_packet_get_side_data(avpkt, 'F', NULL);
00145 int buf_size = avpkt->size;
00146 NellyMoserDecodeContext *s = avctx->priv_data;
00147 int blocks, i, ret;
00148 float *samples_flt;
00149
00150 blocks = buf_size / NELLY_BLOCK_LEN;
00151
00152 if (blocks <= 0) {
00153 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00154 return AVERROR_INVALIDDATA;
00155 }
00156
00157 if (buf_size % NELLY_BLOCK_LEN) {
00158 av_log(avctx, AV_LOG_WARNING, "Leftover bytes: %d.\n",
00159 buf_size % NELLY_BLOCK_LEN);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 if(side && blocks>1 && avctx->sample_rate%11025==0 && (1<<((side[0]>>2)&3)) == blocks)
00169 avctx->sample_rate= 11025*(blocks/2);
00170
00171
00172 s->frame.nb_samples = NELLY_SAMPLES * blocks;
00173 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00174 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00175 return ret;
00176 }
00177 samples_flt = (float *)s->frame.data[0];
00178
00179 for (i=0 ; i<blocks ; i++) {
00180 nelly_decode_block(s, buf, samples_flt);
00181 samples_flt += NELLY_SAMPLES;
00182 buf += NELLY_BLOCK_LEN;
00183 }
00184
00185 *got_frame_ptr = 1;
00186 *(AVFrame *)data = s->frame;
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
00196 return 0;
00197 }
00198
00199 AVCodec ff_nellymoser_decoder = {
00200 .name = "nellymoser",
00201 .type = AVMEDIA_TYPE_AUDIO,
00202 .id = AV_CODEC_ID_NELLYMOSER,
00203 .priv_data_size = sizeof(NellyMoserDecodeContext),
00204 .init = decode_init,
00205 .close = decode_end,
00206 .decode = decode_tag,
00207 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE,
00208 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
00209 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
00210 AV_SAMPLE_FMT_NONE },
00211 };