41
41
#include "fmtconvert.h"
42
42
#include "sinewin.h"
44
#define ALT_BITSTREAM_READER_LE
44
#define BITSTREAM_READER_LE
45
45
#include "get_bits.h"
48
48
typedef struct NellyMoserDecodeContext {
49
49
AVCodecContext* avctx;
50
DECLARE_ALIGNED(32, float, float_buf)[NELLY_SAMPLES];
52
DECLARE_ALIGNED(16, float, state)[NELLY_BUF_LEN];
52
53
AVLFG random_state;
58
59
DECLARE_ALIGNED(32, float, imdct_out)[NELLY_BUF_LEN * 2];
59
60
} NellyMoserDecodeContext;
61
static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in)
66
top = NELLY_BUF_LEN-1;
68
while (bot < NELLY_BUF_LEN) {
69
audio[bot] = a_in [bot]*ff_sine_128[bot]
70
+state[bot]*ff_sine_128[top];
75
memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
78
62
static void nelly_decode_block(NellyMoserDecodeContext *s,
79
63
const unsigned char block[NELLY_BLOCK_LEN],
80
64
float audio[NELLY_SAMPLES])
125
109
s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
126
110
/* XXX: overlapping and windowing should be part of a more
127
111
generic imdct function */
128
overlap_and_window(s, s->state, aptr, s->imdct_out);
112
s->dsp.vector_fmul_reverse(s->state, s->state, ff_sine_128, NELLY_BUF_LEN);
113
s->dsp.vector_fmul_add(aptr, s->imdct_out, ff_sine_128, s->state, NELLY_BUF_LEN);
114
memcpy(s->state, s->imdct_out + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);
137
123
ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
139
125
dsputil_init(&s->dsp, avctx);
140
ff_fmt_convert_init(&s->fmt_conv, avctx);
142
s->scale_bias = 1.0/(1*8);
127
if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
128
s->scale_bias = 1.0/(32768*8);
129
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
131
s->scale_bias = 1.0/(1*8);
132
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
133
ff_fmt_convert_init(&s->fmt_conv, avctx);
134
s->float_buf = av_mallocz(NELLY_SAMPLES * sizeof(*s->float_buf));
136
av_log(avctx, AV_LOG_ERROR, "error allocating float buffer\n");
137
return AVERROR(ENOMEM);
144
141
/* Generate overlap window */
145
142
if (!ff_sine_128[127])
146
143
ff_init_ff_sine_windows(7);
148
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
149
145
avctx->channel_layout = AV_CH_LAYOUT_MONO;
147
avcodec_get_frame_defaults(&s->frame);
148
avctx->coded_frame = &s->frame;
153
static int decode_tag(AVCodecContext * avctx,
154
void *data, int *data_size,
153
static int decode_tag(AVCodecContext *avctx, void *data,
154
int *got_frame_ptr, AVPacket *avpkt)
156
156
const uint8_t *buf = avpkt->data;
157
157
int buf_size = avpkt->size;
158
158
NellyMoserDecodeContext *s = avctx->priv_data;
162
samples = (int16_t*)data;
164
if (buf_size < avctx->block_align)
168
av_log(avctx, AV_LOG_ERROR, "Tag size %d.\n", buf_size);
171
blocks = buf_size / 64;
160
int16_t *samples_s16;
163
blocks = buf_size / NELLY_BLOCK_LEN;
165
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
166
return AVERROR_INVALIDDATA;
168
if (buf_size % NELLY_BLOCK_LEN) {
169
av_log(avctx, AV_LOG_WARNING, "Leftover bytes: %d.\n",
170
buf_size % NELLY_BLOCK_LEN);
172
172
/* Normal numbers of blocks for sample rates:
180
/* get output buffer */
181
s->frame.nb_samples = NELLY_SAMPLES * blocks;
182
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
183
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
186
samples_s16 = (int16_t *)s->frame.data[0];
187
samples_flt = (float *)s->frame.data[0];
180
189
for (i=0 ; i<blocks ; i++) {
181
nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf);
182
s->fmt_conv.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES);
183
*data_size += NELLY_SAMPLES*sizeof(int16_t);
190
if (avctx->sample_fmt == SAMPLE_FMT_FLT) {
191
nelly_decode_block(s, buf, samples_flt);
192
samples_flt += NELLY_SAMPLES;
194
nelly_decode_block(s, buf, s->float_buf);
195
s->fmt_conv.float_to_int16(samples_s16, s->float_buf, NELLY_SAMPLES);
196
samples_s16 += NELLY_SAMPLES;
198
buf += NELLY_BLOCK_LEN;
202
*(AVFrame *)data = s->frame;
189
207
static av_cold int decode_end(AVCodecContext * avctx) {
190
208
NellyMoserDecodeContext *s = avctx->priv_data;
210
av_freep(&s->float_buf);
192
211
ff_mdct_end(&s->imdct_ctx);
196
216
AVCodec ff_nellymoser_decoder = {
200
sizeof(NellyMoserDecodeContext),
217
.name = "nellymoser",
218
.type = AVMEDIA_TYPE_AUDIO,
219
.id = CODEC_ID_NELLYMOSER,
220
.priv_data_size = sizeof(NellyMoserDecodeContext),
223
.decode = decode_tag,
224
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE,
205
225
.long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
226
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
228
AV_SAMPLE_FMT_NONE },