~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/nellymoserdec.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 * implementors. The original code is available from http://code.google.com/p/nelly2pcm/
32
32
 */
33
33
 
34
 
#include "nellymoser.h"
 
34
#include "libavutil/channel_layout.h"
35
35
#include "libavutil/lfg.h"
36
36
#include "libavutil/random_seed.h"
37
 
#include "libavutil/audioconvert.h"
38
37
#include "avcodec.h"
39
38
#include "dsputil.h"
40
39
#include "fft.h"
41
40
#include "fmtconvert.h"
 
41
#include "internal.h"
 
42
#include "nellymoser.h"
42
43
#include "sinewin.h"
43
44
 
44
45
#define BITSTREAM_READER_LE
48
49
typedef struct NellyMoserDecodeContext {
49
50
    AVCodecContext* avctx;
50
51
    AVFrame         frame;
51
 
    float          *float_buf;
52
 
    DECLARE_ALIGNED(16, float, state)[NELLY_BUF_LEN];
53
52
    AVLFG           random_state;
54
53
    GetBitContext   gb;
55
54
    float           scale_bias;
56
55
    DSPContext      dsp;
57
56
    FFTContext      imdct_ctx;
58
 
    FmtConvertContext fmt_conv;
59
 
    DECLARE_ALIGNED(32, float, imdct_out)[NELLY_BUF_LEN * 2];
 
57
    DECLARE_ALIGNED(32, float, imdct_buf)[2][NELLY_BUF_LEN];
 
58
    float          *imdct_out;
 
59
    float          *imdct_prev;
60
60
} NellyMoserDecodeContext;
61
61
 
62
62
static void nelly_decode_block(NellyMoserDecodeContext *s,
106
106
        memset(&aptr[NELLY_FILL_LEN], 0,
107
107
               (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
108
108
 
109
 
        s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);
110
 
        /* XXX: overlapping and windowing should be part of a more
111
 
           generic imdct function */
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);
 
109
        s->imdct_ctx.imdct_half(&s->imdct_ctx, s->imdct_out, aptr);
 
110
        s->dsp.vector_fmul_window(aptr, s->imdct_prev + NELLY_BUF_LEN/2, s->imdct_out, ff_sine_128, NELLY_BUF_LEN/2);
 
111
        FFSWAP(float *, s->imdct_out, s->imdct_prev);
115
112
    }
116
113
}
117
114
 
119
116
    NellyMoserDecodeContext *s = avctx->priv_data;
120
117
 
121
118
    s->avctx = avctx;
 
119
    s->imdct_out = s->imdct_buf[0];
 
120
    s->imdct_prev = s->imdct_buf[1];
122
121
    av_lfg_init(&s->random_state, 0);
123
122
    ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
124
123
 
125
 
    dsputil_init(&s->dsp, avctx);
 
124
    ff_dsputil_init(&s->dsp, avctx);
126
125
 
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;
130
 
    } else {
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));
135
 
        if (!s->float_buf) {
136
 
            av_log(avctx, AV_LOG_ERROR, "error allocating float buffer\n");
137
 
            return AVERROR(ENOMEM);
138
 
        }
139
 
    }
 
126
    s->scale_bias = 1.0/(32768*8);
 
127
    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
140
128
 
141
129
    /* Generate overlap window */
142
130
    if (!ff_sine_128[127])
143
131
        ff_init_ff_sine_windows(7);
144
132
 
 
133
    avctx->channels       = 1;
145
134
    avctx->channel_layout = AV_CH_LAYOUT_MONO;
146
135
 
147
136
    avcodec_get_frame_defaults(&s->frame);
157
146
    int buf_size = avpkt->size;
158
147
    NellyMoserDecodeContext *s = avctx->priv_data;
159
148
    int blocks, i, ret;
160
 
    int16_t *samples_s16;
161
149
    float   *samples_flt;
162
150
 
163
151
    blocks     = buf_size / NELLY_BLOCK_LEN;
179
167
 
180
168
    /* get output buffer */
181
169
    s->frame.nb_samples = NELLY_SAMPLES * blocks;
182
 
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
 
170
    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
183
171
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
184
172
        return ret;
185
173
    }
186
 
    samples_s16 = (int16_t *)s->frame.data[0];
187
174
    samples_flt = (float   *)s->frame.data[0];
188
175
 
189
176
    for (i=0 ; i<blocks ; i++) {
190
 
        if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
191
 
            nelly_decode_block(s, buf, samples_flt);
192
 
            samples_flt += NELLY_SAMPLES;
193
 
        } else {
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;
197
 
        }
 
177
        nelly_decode_block(s, buf, samples_flt);
 
178
        samples_flt += NELLY_SAMPLES;
198
179
        buf += NELLY_BLOCK_LEN;
199
180
    }
200
181
 
207
188
static av_cold int decode_end(AVCodecContext * avctx) {
208
189
    NellyMoserDecodeContext *s = avctx->priv_data;
209
190
 
210
 
    av_freep(&s->float_buf);
211
191
    ff_mdct_end(&s->imdct_ctx);
212
192
 
213
193
    return 0;
216
196
AVCodec ff_nellymoser_decoder = {
217
197
    .name           = "nellymoser",
218
198
    .type           = AVMEDIA_TYPE_AUDIO,
219
 
    .id             = CODEC_ID_NELLYMOSER,
 
199
    .id             = AV_CODEC_ID_NELLYMOSER,
220
200
    .priv_data_size = sizeof(NellyMoserDecodeContext),
221
201
    .init           = decode_init,
222
202
    .close          = decode_end,
223
203
    .decode         = decode_tag,
224
204
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE,
225
 
    .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
 
205
    .long_name      = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
226
206
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
227
 
                                                      AV_SAMPLE_FMT_S16,
228
207
                                                      AV_SAMPLE_FMT_NONE },
229
208
};
230