summaryrefslogtreecommitdiff
path: root/lib/rbcodec/dsp/dsp_sample_input.c
blob: 97b4ec27ad7b865a0bed3dddd26a00a8f9815f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Miika Pekkarinen
 * Copyright (C) 2012 Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "system.h"
#include "dsp_core.h"
#include "dsp_sample_io.h"

#if 1
#include <debug.h>
#else
#undef DEBUGF
#define DEBUGF(...)
#endif

/* The internal format is 32-bit samples, non-interleaved, stereo. This
 * format is similar to the raw output from several codecs, so no copying is
 * needed for that case.
 *
 * Note that for mono, dst[0] equals dst[1], as there is no point in
 * processing the same data twice nor should it be done when modifying
 * samples in-place.
 *
 * When conversion is required:
 * Updates source buffer to point past the samples "consumed" also consuming
 * that portion of the input buffer and the destination is set to the buffer
 * of samples for later stages to consume.
 *
 * Input operates similarly to how an out-of-place processing stage should
 * behave.
 */

extern void dsp_sample_output_init(struct sample_io_data *this);
extern void dsp_sample_output_flush(struct sample_io_data *this);

/* convert count 16-bit mono to 32-bit mono */
static void sample_input_mono16(struct sample_io_data *this,
                                struct dsp_buffer **buf_p)
{
    struct dsp_buffer *src = *buf_p;
    struct dsp_buffer *dst = &this->sample_buf;

    *buf_p = dst;

    if (dst->remcount > 0)
        return; /* data still remains */

    int count = MIN(src->remcount, SAMPLE_BUF_COUNT);

    dst->remcount  = count;
    dst->p32[0]    = this->sample_buf_arr[0];
    dst->p32[1]    = this->sample_buf_arr[0];
    dst->proc_mask = src->proc_mask;

    if (count <= 0)
        return; /* purged sample_buf */

    const int16_t *s = src->pin[0];
    int32_t *d = dst->p32[0];
    const int scale = WORD_SHIFT;

    dsp_advance_buffer_input(src, count, sizeof (int16_t));

    do
    {
        *d++ = *s++ << scale;
    }
    while (--count > 0);
}

/* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
static void sample_input_i_stereo16(struct sample_io_data *this,
                                    struct dsp_buffer **buf_p)
{
    struct dsp_buffer *src = *buf_p;
    struct dsp_buffer *dst = &this->sample_buf;

    *buf_p = dst;

    if (dst->remcount > 0)
        return; /* data still remains */

    int count = MIN(src->remcount, SAMPLE_BUF_COUNT);

    dst->remcount  = count;
    dst->p32[0]    = this->sample_buf_arr[0];
    dst->p32[1]    = this->sample_buf_arr[1];
    dst->proc_mask = src->proc_mask;

    if (count <= 0)
        return; /* purged sample_buf */

    const int16_t *s = src->pin[0];
    int32_t *dl = dst->p32[0];
    int32_t *dr = dst->p32[1];
    const int scale = WORD_SHIFT;

    dsp_advance_buffer_input(src, count, 2*sizeof (int16_t));

    do
    {
        *dl++ = *s++ << scale;
        *dr++ = *s++ << scale;
    }
    while (--count > 0);
}

/* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
static void sample_input_ni_stereo16(struct sample_io_data *this,
                                     struct dsp_buffer **buf_p)
{
    struct dsp_buffer *src = *buf_p;
    struct dsp_buffer *dst = &this->sample_buf;

    *buf_p = dst;

    if (dst->remcount > 0)
        return; /* data still remains */

    int count = MIN(src->remcount, SAMPLE_BUF_COUNT);

    dst->remcount  = count;
    dst->p32[0]    = this->sample_buf_arr[0];
    dst->p32[1]    = this->sample_buf_arr[1];
    dst->proc_mask = src->proc_mask;

    if (count <= 0)
        return; /* purged sample_buf */

    const int16_t *sl = src->pin[0];
    const int16_t *sr = src->pin[1];
    int32_t *dl = dst->p32[0];
    int32_t *dr = dst->p32[1];
    const int scale = WORD_SHIFT;

    dsp_advance_buffer_input(src, count, sizeof (int16_t));

    do
    {
        *dl++ = *sl++ << scale;
        *dr++ = *sr++ << scale;
    }
    while (--count > 0);
}

/* convert count 32-bit mono to 32-bit mono */
static void sample_input_mono32(struct sample_io_data *this,
                                struct dsp_buffer **buf_p)
{
    struct dsp_buffer *dst = &this->sample_buf;

    if (dst->remcount > 0)
    {
        *buf_p = dst;
        return; /* data still remains */
    }
    /* else no buffer switch */

    struct dsp_buffer *src = *buf_p;
    src->p32[1] = src->p32[0];
}


/* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
static void sample_input_i_stereo32(struct sample_io_data *this,
                                    struct dsp_buffer **buf_p)
{
    struct dsp_buffer *src = *buf_p;
    struct dsp_buffer *dst = &this->sample_buf;

    *buf_p = dst;

    if (dst->remcount > 0)
        return; /* data still remains */

    int count = MIN(src->remcount, SAMPLE_BUF_COUNT);

    dst->remcount  = count;
    dst->p32[0]    = this->sample_buf_arr[0];
    dst->p32[1]    = this->sample_buf_arr[1];
    dst->proc_mask = src->proc_mask;

    if (count <= 0)
        return; /* purged sample_buf */

    const int32_t *s = src->pin[0];
    int32_t *dl = dst->p32[0];
    int32_t *dr = dst->p32[1];

    dsp_advance_buffer_input(src, count, 2*sizeof (int32_t));

    do
    {
        *dl++ = *s++;
        *dr++ = *s++;
    }
    while (--count > 0);
}

/* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
static void sample_input_ni_stereo32(struct sample_io_data *this,
                                     struct dsp_buffer **buf_p)
{
    struct dsp_buffer *dst = &this->sample_buf;

    if (dst->remcount > 0)
        *buf_p = dst; /* data still remains */
    /* else no buffer switch */
}

/* set the to-native sample conversion function based on dsp sample
 * parameters */
static void dsp_sample_input_format_change(struct sample_io_data *this,
                                           struct dsp_buffer **buf_p)
{
    static const sample_input_fn_type fns[STEREO_NUM_MODES][2] =
    {
        [STEREO_INTERLEAVED] =
            { sample_input_i_stereo16,
              sample_input_i_stereo32 },
        [STEREO_NONINTERLEAVED] =
            { sample_input_ni_stereo16,
              sample_input_ni_stereo32 },
        [STEREO_MONO] =
            { sample_input_mono16,
              sample_input_mono32 },
    };

    struct dsp_buffer *src = *buf_p;
    struct dsp_buffer *dst = &this->sample_buf;

    /* Ack configured format change */
    format_change_ack(&this->format);

    if (dst->remcount > 0)
    {
        *buf_p = dst;
        return; /* data still remains */
    }

    DSP_PRINT_FORMAT(DSP Input, -1, src->format);

    /* new format - remember it and pass it along */
    dst->format = src->format;
    this->input_samples[0] = fns[this->stereo_mode]
                                [this->sample_depth > NATIVE_DEPTH ? 1 : 0];

    this->input_samples[0](this, buf_p);

    if (*buf_p == dst) /* buffer switch? */
        format_change_ack(&src->format);
}

static void dsp_sample_input_init(struct sample_io_data *this)
{
    this->input_samples[0] = sample_input_ni_stereo32;
    this->input_samples[1] = dsp_sample_input_format_change;
}

/* discard the sample buffer */
static void dsp_sample_input_flush(struct sample_io_data *this)
{
    this->sample_buf.remcount = 0;
}

void dsp_sample_io_configure(struct sample_io_data *this,
                             unsigned int setting,
                             intptr_t value)
{
    switch (setting)
    {
    case DSP_INIT:
        dsp_sample_input_init(this);
        dsp_sample_output_init(this);
        break;

    case DSP_RESET:
        /* Reset all sample descriptions to default */
        format_change_set(&this->format);
        this->format.num_channels = 2;
        this->format.frac_bits = WORD_FRACBITS;
        this->format.output_scale = WORD_FRACBITS + 1 - NATIVE_DEPTH;
        this->format.frequency = NATIVE_FREQUENCY;
        this->format.codec_frequency = NATIVE_FREQUENCY;
        this->sample_depth = NATIVE_DEPTH;
        this->stereo_mode = STEREO_NONINTERLEAVED;
        break;

    case DSP_SET_FREQUENCY:
        value = value > 0 ? value : NATIVE_FREQUENCY;
        format_change_set(&this->format);
        this->format.frequency = value;
        this->format.codec_frequency = value;
        break;

    case DSP_SET_SAMPLE_DEPTH:
        format_change_set(&this->format);
        this->format.frac_bits =
            value <= NATIVE_DEPTH ? WORD_FRACBITS : value;
        this->format.output_scale =
            this->format.frac_bits + 1 - NATIVE_DEPTH;
        this->sample_depth = value;
        break;

    case DSP_SET_STEREO_MODE:
        format_change_set(&this->format);
        this->format.num_channels = value == STEREO_MONO ? 1 : 2;
        this->stereo_mode = value;
        break;

    case DSP_FLUSH:
        dsp_sample_input_flush(this);
        dsp_sample_output_flush(this);
        break;
    }
}