blob: c19828aa1d7a47c3f5e513c51e06c22262ff9a3b [file] [log] [blame]
Seppo Ingalsuo6a274832017-06-07 14:17:55 +03001/*
2 * Copyright (c) 2017, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Intel Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
29 * Liam Girdwood <liam.r.girdwood@linux.intel.com>
30 * Keyon Jie <yang.jie@linux.intel.com>
31 */
32
33#include <stdint.h>
34#include <stddef.h>
35#include <errno.h>
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050036#include <sof/sof.h>
37#include <sof/lock.h>
38#include <sof/list.h>
39#include <sof/stream.h>
40#include <sof/alloc.h>
41#include <sof/work.h>
42#include <sof/clock.h>
43#include <sof/audio/component.h>
44#include <sof/audio/pipeline.h>
Seppo Ingalsuo92dc65f2018-05-04 18:01:16 +030045#include <sof/math/numbers.h>
Liam Girdwood17641522017-06-09 17:27:02 +010046#include <uapi/ipc.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020047
48#include "src_config.h"
49#include "src.h"
50
51#if SRC_SHORT
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050052#include <sof/audio/coefficients/src/src_tiny_int16_define.h>
53#include <sof/audio/coefficients/src/src_tiny_int16_table.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020054#else
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050055#include <sof/audio/coefficients/src/src_std_int32_define.h>
56#include <sof/audio/coefficients/src/src_std_int32_table.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020057#endif
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030058
59#ifdef MODULE_TEST
60#include <stdio.h>
61#endif
62
63#define trace_src(__e) trace_event(TRACE_CLASS_SRC, __e)
64#define tracev_src(__e) tracev_event(TRACE_CLASS_SRC, __e)
65#define trace_src_error(__e) trace_error(TRACE_CLASS_SRC, __e)
66
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020067/* The FIR maximum lengths are per channel so need to multiply them */
68#define MAX_FIR_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_FIR_DELAY_SIZE)
69#define MAX_OUT_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_OUT_DELAY_SIZE)
70
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030071/* src component private data */
72struct comp_data {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +030073 struct polyphase_src src;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030074 struct src_param param;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030075 int32_t *delay_lines;
Liam Girdwood3488cce2017-08-10 11:59:08 +010076 uint32_t sink_rate;
77 uint32_t source_rate;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030078 int32_t *sbuf_w_ptr;
79 int32_t *sbuf_r_ptr;
80 int sbuf_avail;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020081 void (*src_func)(struct comp_dev *dev,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030082 struct comp_buffer *source,
83 struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030084 size_t *consumed,
85 size_t *produced);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020086 void (*polyphase_func)(struct src_stage_prm *s);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030087};
88
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020089/* Calculate ceil() for integer division */
90int src_ceil_divide(int a, int b)
91{
92 int c;
93
94 c = a / b;
95 if (c * b < a)
96 c++;
97
98 return c;
99}
100
101/* Calculates the needed FIR delay line length */
102static int src_fir_delay_length(struct src_stage *s)
103{
104 return s->subfilter_length + (s->num_of_subfilters - 1) * s->idm
105 + s->blk_in;
106}
107
108/* Calculates the FIR output delay line length */
109static int src_out_delay_length(struct src_stage *s)
110{
111 return 1 + (s->num_of_subfilters - 1) * s->odm;
112}
113
114/* Returns index of a matching sample rate */
115static int src_find_fs(int fs_list[], int list_length, int fs)
116{
117 int i;
118
119 for (i = 0; i < list_length; i++) {
120 if (fs_list[i] == fs)
121 return i;
122 }
123 return -EINVAL;
124}
125
126/* Calculates buffers to allocate for a SRC mode */
127int src_buffer_lengths(struct src_param *a, int fs_in, int fs_out, int nch,
128 int frames, int frames_is_for_source)
129{
130 struct src_stage *stage1;
131 struct src_stage *stage2;
132 int q;
133 int den;
134 int num;
135 int frames2;
136
137 if (nch > PLATFORM_MAX_CHANNELS) {
138 trace_src_error("che");
139 tracev_value(nch);
140 return -EINVAL;
141 }
142
143 a->nch = nch;
144 a->idx_in = src_find_fs(src_in_fs, NUM_IN_FS, fs_in);
145 a->idx_out = src_find_fs(src_out_fs, NUM_OUT_FS, fs_out);
146
147 /* Check that both in and out rates are supported */
148 if (a->idx_in < 0 || a->idx_out < 0) {
149 trace_src_error("us1");
150 tracev_value(fs_in);
151 tracev_value(fs_out);
152 return -EINVAL;
153 }
154
155 stage1 = src_table1[a->idx_out][a->idx_in];
156 stage2 = src_table2[a->idx_out][a->idx_in];
157
158 /* Check from stage1 parameter for a deleted in/out rate combination.*/
159 if (stage1->filter_length < 1) {
160 trace_src_error("us2");
161 tracev_value(fs_in);
162 tracev_value(fs_out);
163 return -EINVAL;
164 }
165
166 a->fir_s1 = nch * src_fir_delay_length(stage1);
167 a->out_s1 = nch * src_out_delay_length(stage1);
168
169 /* Find out how many additional times the SRC can be executed
170 * while having block size less or equal to max_frames.
171 */
172 if (frames_is_for_source) {
173 /* Times that stage1 needs to run to input length of frames */
174 a->stage1_times_max = src_ceil_divide(frames, stage1->blk_in);
175 q = frames / stage1->blk_in;
176 a->stage1_times = MAX(q, 1);
177 a->blk_in = a->stage1_times * stage1->blk_in;
178
179 /* Times that stage2 needs to run */
180 den = stage2->blk_in * stage1->blk_in;
181 num = frames * stage2->blk_out * stage1->blk_out;
182 frames2 = src_ceil_divide(num, den);
183 a->stage2_times_max = src_ceil_divide(frames2, stage2->blk_out);
184 q = frames2 / stage2->blk_out;
185 a->stage2_times = MAX(q, 1);
186 a->blk_out = a->stage2_times * stage2->blk_out;
187 } else {
188 /* Times that stage2 needs to run to output length of frames */
189 a->stage2_times_max = src_ceil_divide(frames, stage2->blk_out);
190 q = frames / stage2->blk_out;
191 a->stage2_times = MAX(q, 1);
192 a->blk_out = a->stage2_times * stage2->blk_out;
193
194 /* Times that stage1 needs to run */
195 num = frames * stage2->blk_in * stage1->blk_in;
196 den = stage2->blk_out * stage1->blk_out;
197 frames2 = src_ceil_divide(num, den);
198 a->stage1_times_max = src_ceil_divide(frames2, stage1->blk_in);
199 q = frames2 / stage1->blk_in;
200 a->stage1_times = MAX(q, 1);
201 a->blk_in = a->stage1_times * stage1->blk_in;
202 }
203
204 if (stage2->filter_length == 1) {
205 a->fir_s2 = 0;
206 a->out_s2 = 0;
207 a->stage2_times = 0;
208 a->stage2_times_max = 0;
209 a->sbuf_length = 0;
210 } else {
211 a->fir_s2 = nch * src_fir_delay_length(stage2);
212 a->out_s2 = nch * src_out_delay_length(stage2);
213 /* 2x is an empirically tested length. Since the sink buffer
214 * capability to receive samples varies a shorter stage 2 output
215 * block will create a peak in internal buffer usage.
216 */
217
218 /* TODO 1: Equation for needed length */
219 a->sbuf_length = 2 * nch * stage1->blk_out
220 * a->stage1_times_max;
221 }
222
223 a->src_multich = a->fir_s1 + a->fir_s2 + a->out_s1 + a->out_s2;
224 a->total = a->sbuf_length + a->src_multich;
225
226 return 0;
227}
228
229static void src_state_reset(struct src_state *state)
230{
231 state->fir_delay_size = 0;
232 state->out_delay_size = 0;
233}
234
235static int init_stages(struct src_stage *stage1, struct src_stage *stage2,
236 struct polyphase_src *src, struct src_param *p,
237 int n, int32_t *delay_lines_start)
238{
239 /* Clear FIR state */
240 src_state_reset(&src->state1);
241 src_state_reset(&src->state2);
242
243 src->number_of_stages = n;
244 src->stage1 = stage1;
245 src->stage2 = stage2;
246 if (n == 1 && stage1->blk_out == 0)
247 return -EINVAL;
248
249 /* Optimized SRC requires subfilter length multiple of 4 */
250 if (stage1->filter_length > 1 && (stage1->subfilter_length & 0x3) > 0)
251 return -EINVAL;
252
253 if (stage2->filter_length > 1 && (stage2->subfilter_length & 0x3) > 0)
254 return -EINVAL;
255
256 /* Delay line sizes */
257 src->state1.fir_delay_size = p->fir_s1;
258 src->state1.out_delay_size = p->out_s1;
259 src->state1.fir_delay = delay_lines_start;
260 src->state1.out_delay =
261 src->state1.fir_delay + src->state1.fir_delay_size;
262 /* Initialize to last ensures that circular wrap cannot happen
263 * mid-frame. The size is multiple of channels count.
264 */
265 src->state1.fir_wp = &src->state1.fir_delay[p->fir_s1 - 1];
266 src->state1.out_rp = src->state1.out_delay;
267 if (n > 1) {
268 src->state2.fir_delay_size = p->fir_s2;
269 src->state2.out_delay_size = p->out_s2;
270 src->state2.fir_delay =
271 src->state1.out_delay + src->state1.out_delay_size;
272 src->state2.out_delay =
273 src->state2.fir_delay + src->state2.fir_delay_size;
274 /* Initialize to last ensures that circular wrap cannot happen
275 * mid-frame. The size is multiple of channels count.
276 */
277 src->state2.fir_wp = &src->state2.fir_delay[p->fir_s2 - 1];
278 src->state2.out_rp = src->state2.out_delay;
279 } else {
280 src->state2.fir_delay_size = 0;
281 src->state2.out_delay_size = 0;
282 src->state2.fir_delay = NULL;
283 src->state2.out_delay = NULL;
284 }
285
286 /* Check the sizes are less than MAX */
287 if (src->state1.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH ||
288 src->state1.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH ||
289 src->state2.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH ||
290 src->state2.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH) {
291 src->state1.fir_delay = NULL;
292 src->state1.out_delay = NULL;
293 src->state2.fir_delay = NULL;
294 src->state2.out_delay = NULL;
295 return -EINVAL;
296 }
297
298 return 0;
299}
300
301void src_polyphase_reset(struct polyphase_src *src)
302{
303 src->number_of_stages = 0;
304 src->stage1 = NULL;
305 src->stage2 = NULL;
306 src_state_reset(&src->state1);
307 src_state_reset(&src->state2);
308}
309
310int src_polyphase_init(struct polyphase_src *src, struct src_param *p,
311 int32_t *delay_lines_start)
312{
313 struct src_stage *stage1;
314 struct src_stage *stage2;
315 int n_stages;
316 int ret;
317
318 if (p->idx_in < 0 || p->idx_out < 0)
319 return -EINVAL;
320
321 /* Get setup for 2 stage conversion */
322 stage1 = src_table1[p->idx_out][p->idx_in];
323 stage2 = src_table2[p->idx_out][p->idx_in];
324 ret = init_stages(stage1, stage2, src, p, 2, delay_lines_start);
325 if (ret < 0)
326 return -EINVAL;
327
328 /* Get number of stages used for optimize opportunity. 2nd
329 * stage length is one if conversion needs only one stage.
330 * If input and output rate is the same return 0 to
331 * use a simple copy function instead of 1 stage FIR with one
332 * tap.
333 */
334 n_stages = (src->stage2->filter_length == 1) ? 1 : 2;
335 if (p->idx_in == p->idx_out)
336 n_stages = 0;
337
338 /* If filter length for first stage is zero this is a deleted
339 * mode from in/out matrix. Computing of such SRC mode needs
340 * to be prevented.
341 */
342 if (src->stage1->filter_length == 0)
343 return -EINVAL;
344
345 return n_stages;
346}
347
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300348/* Fallback function */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300349static void src_fallback(struct comp_dev *dev, struct comp_buffer *source,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300350 struct comp_buffer *sink, size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300351{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300352 *bytes_read = 0;
353 *bytes_written = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300354}
355
356/* Normal 2 stage SRC */
357static void src_2s_s32_default(struct comp_dev *dev,
358 struct comp_buffer *source, struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300359 size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300360{
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500361 struct src_stage_prm s1;
362 struct src_stage_prm s2;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300363 int s1_blk_in;
364 int s1_blk_out;
365 int s2_blk_in;
366 int s2_blk_out;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300367 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200368 int32_t *dest = (int32_t *)sink->w_ptr;
369 int32_t *src = (int32_t *)source->r_ptr;
370 int32_t *sbuf_addr = cd->delay_lines;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300371 int32_t *sbuf_end_addr = &cd->delay_lines[cd->param.sbuf_length];
372 int32_t sbuf_size = cd->param.sbuf_length * sizeof(int32_t);
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300373 int nch = dev->params.channels;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300374 int sbuf_free = cd->param.sbuf_length - cd->sbuf_avail;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300375 int n_read = 0;
376 int n_written = 0;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300377 int n1 = 0;
378 int n2 = 0;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300379 int avail_b = source->avail;
380 int free_b = sink->free;
381 int sz = sizeof(int32_t);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300382
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300383 s1.x_end_addr = source->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100384 s1.x_size = source->size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200385 s1.y_addr = sbuf_addr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300386 s1.y_end_addr = sbuf_end_addr;
387 s1.y_size = sbuf_size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300388 s1.state = &cd->src.state1;
389 s1.stage = cd->src.stage1;
390 s1.x_rptr = src;
391 s1.y_wptr = cd->sbuf_w_ptr;
392 s1.nch = nch;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300393
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300394 s2.x_end_addr = sbuf_end_addr;
395 s2.x_size = sbuf_size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200396 s2.y_addr = sink->addr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300397 s2.y_end_addr = sink->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100398 s2.y_size = sink->size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300399 s2.state = &cd->src.state2;
400 s2.stage = cd->src.stage2;
401 s2.x_rptr = cd->sbuf_r_ptr;
402 s2.y_wptr = dest;
403 s2.nch = nch;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300404
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300405 /* Test if 1st stage can be run with default block length to reach
406 * the period length or just under it.
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300407 */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300408 s1.times = cd->param.stage1_times;
409 s1_blk_in = s1.times * cd->src.stage1->blk_in * nch;
410 s1_blk_out = s1.times * cd->src.stage1->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200411 if (avail_b >= s1_blk_in * sz && sbuf_free >= s1_blk_out) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300412 cd->polyphase_func(&s1);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300413
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300414 cd->sbuf_w_ptr = s1.y_wptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300415 cd->sbuf_avail += s1_blk_out;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300416 n_read += s1_blk_in;
417 avail_b -= s1_blk_in * sz;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300418 sbuf_free -= s1_blk_out;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300419 n1 = s1.times;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300420 }
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300421
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300422 /* Run one block at time the remaining data for 1st stage. */
423 s1.times = 1;
424 s1_blk_in = cd->src.stage1->blk_in * nch;
425 s1_blk_out = cd->src.stage1->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200426 while (n1 < cd->param.stage1_times_max &&
427 avail_b >= s1_blk_in * sz &&
428 sbuf_free >= s1_blk_out) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300429 cd->polyphase_func(&s1);
430
431 cd->sbuf_w_ptr = s1.y_wptr;
432 cd->sbuf_avail += s1_blk_out;
433 n_read += s1_blk_in;
434 avail_b -= s1_blk_in * sz;
435 sbuf_free -= s1_blk_out;
436 n1 += s1.times;
437 }
438
439 /* Test if 2nd stage can be run with default block length. */
440 s2.times = cd->param.stage2_times;
441 s2_blk_in = s2.times * cd->src.stage2->blk_in * nch;
442 s2_blk_out = s2.times * cd->src.stage2->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200443 if (cd->sbuf_avail >= s2_blk_in && free_b >= s2_blk_out * sz) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300444 cd->polyphase_func(&s2);
445
446 cd->sbuf_r_ptr = s2.x_rptr;
447 cd->sbuf_avail -= s2_blk_in;
448 free_b -= s2_blk_out * sz;
449 n_written += s2_blk_out;
450 n2 = s2.times;
451 }
452
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300453 /* Run one block at time the remaining 2nd stage output */
454 s2.times = 1;
455 s2_blk_in = cd->src.stage2->blk_in * nch;
456 s2_blk_out = cd->src.stage2->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200457 while (n2 < cd->param.stage2_times_max &&
458 cd->sbuf_avail >= s2_blk_in &&
459 free_b >= s2_blk_out * sz) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300460 cd->polyphase_func(&s2);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300461
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300462 cd->sbuf_r_ptr = s2.x_rptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300463 cd->sbuf_avail -= s2_blk_in;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300464 free_b -= s2_blk_out * sz;
465 n_written += s2_blk_out;
466 n2 += s2.times;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300467 }
468 *bytes_read = sizeof(int32_t) * n_read;
469 *bytes_written = sizeof(int32_t) * n_written;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300470}
471
472/* 1 stage SRC for simple conversions */
473static void src_1s_s32_default(struct comp_dev *dev,
474 struct comp_buffer *source, struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300475 size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300476{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300477 struct src_stage_prm s1;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300478 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300479 int nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300480 int n_read = 0;
481 int n_written = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300482
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300483 s1.times = cd->param.stage1_times;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200484 s1.x_rptr = (int32_t *)source->r_ptr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300485 s1.x_end_addr = source->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100486 s1.x_size = source->size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200487 s1.y_wptr = (int32_t *)sink->w_ptr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300488 s1.y_end_addr = sink->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100489 s1.y_size = sink->size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300490 s1.state = &cd->src.state1;
491 s1.stage = cd->src.stage1;
492 s1.nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300493
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300494 cd->polyphase_func(&s1);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300495
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300496 n_read += nch * cd->param.blk_in;
497 n_written += nch * cd->param.blk_out;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300498 *bytes_read = n_read * sizeof(int32_t);
499 *bytes_written = n_written * sizeof(int32_t);
500}
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300501
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300502/* A fast copy function for same in and out rate */
503static void src_copy_s32_default(struct comp_dev *dev,
504 struct comp_buffer *source, struct comp_buffer *sink,
505 size_t *bytes_read, size_t *bytes_written)
506{
507 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200508 int32_t *src = (int32_t *)source->r_ptr;
509 int32_t *snk = (int32_t *)sink->w_ptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300510 int nch = dev->params.channels;
511 int frames = cd->param.blk_in;
512 int n;
513 int n_wrap_src;
514 int n_wrap_snk;
515 int n_wrap_min;
516 int n_copy;
517
518 n = frames * nch;
519 while (n > 0) {
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200520 n_wrap_src = (int32_t *)source->end_addr - src;
521 n_wrap_snk = (int32_t *)sink->end_addr - snk;
522 n_wrap_min = (n_wrap_src < n_wrap_snk) ?
523 n_wrap_src : n_wrap_snk;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300524 n_copy = (n < n_wrap_min) ? n : n_wrap_min;
525 memcpy(snk, src, n_copy * sizeof(int32_t));
526
527 /* Update and check both source and destination for wrap */
528 n -= n_copy;
529 src += n_copy;
530 snk += n_copy;
531 src_circ_inc_wrap(&src, source->end_addr, source->size);
532 src_circ_inc_wrap(&snk, sink->end_addr, sink->size);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300533 }
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300534 *bytes_read = frames * nch * sizeof(int32_t);
535 *bytes_written = frames * nch * sizeof(int32_t);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300536}
537
538static struct comp_dev *src_new(struct sof_ipc_comp *comp)
539{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300540 struct comp_dev *dev;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100541 struct sof_ipc_comp_src *src;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200542 struct sof_ipc_comp_src *ipc_src = (struct sof_ipc_comp_src *)comp;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300543 struct comp_data *cd;
544
Liam Girdwood3488cce2017-08-10 11:59:08 +0100545 trace_src("new");
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100546
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100547 /* validate init data - either SRC sink or source rate must be set */
548 if (ipc_src->source_rate == 0 && ipc_src->sink_rate == 0) {
549 trace_src_error("sn1");
550 return NULL;
551 }
552
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000553 dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM,
Liam Girdwood17641522017-06-09 17:27:02 +0100554 COMP_SIZE(struct sof_ipc_comp_src));
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200555 if (!dev)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300556 return NULL;
557
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200558 src = (struct sof_ipc_comp_src *)&dev->comp;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100559 memcpy(src, ipc_src, sizeof(struct sof_ipc_comp_src));
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300560
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000561 cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd));
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200562 if (!cd) {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300563 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300564 return NULL;
565 }
566
567 comp_set_drvdata(dev, cd);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300568
569 cd->delay_lines = NULL;
570 cd->src_func = src_2s_s32_default;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300571 cd->polyphase_func = src_polyphase_stage_cir;
572 src_polyphase_reset(&cd->src);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300573
Liam Girdwoodbe41b682017-09-21 16:48:18 +0100574 dev->state = COMP_STATE_READY;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300575 return dev;
576}
577
578static void src_free(struct comp_dev *dev)
579{
580 struct comp_data *cd = comp_get_drvdata(dev);
581
Liam Girdwood3488cce2017-08-10 11:59:08 +0100582 trace_src("fre");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300583
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300584 /* Free dynamically reserved buffers for SRC algorithm */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200585 if (!cd->delay_lines)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300586 rfree(cd->delay_lines);
587
588 rfree(cd);
589 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300590}
591
592/* set component audio stream parameters */
Liam Girdwood2cfaebe2017-08-21 17:13:52 +0100593static int src_params(struct comp_dev *dev)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300594{
Liam Girdwood3488cce2017-08-10 11:59:08 +0100595 struct sof_ipc_stream_params *params = &dev->params;
596 struct sof_ipc_comp_src *src = COMP_GET_IPC(dev, sof_ipc_comp_src);
597 struct sof_ipc_comp_config *config = COMP_GET_CONFIG(dev);
598 struct comp_data *cd = comp_get_drvdata(dev);
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500599 struct comp_buffer *sink;
600 struct comp_buffer *source;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300601 size_t delay_lines_size;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500602 uint32_t source_rate;
603 uint32_t sink_rate;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300604 int32_t *buffer_start;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500605 int n = 0;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500606 int err;
607 int frames_is_for_source;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500608 int q;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300609
Liam Girdwood3488cce2017-08-10 11:59:08 +0100610 trace_src("par");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300611
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300612 /* SRC supports S24_4LE and S32_LE formats */
613 switch (config->frame_fmt) {
614 case SOF_IPC_FRAME_S24_4LE:
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300615 cd->polyphase_func = src_polyphase_stage_cir_s24;
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300616 break;
617 case SOF_IPC_FRAME_S32_LE:
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300618 cd->polyphase_func = src_polyphase_stage_cir;
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300619 break;
620 default:
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100621 trace_src_error("sr0");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300622 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100623 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300624
Liam Girdwood3488cce2017-08-10 11:59:08 +0100625 /* Calculate source and sink rates, one rate will come from IPC new
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200626 * and the other from params.
627 */
Liam Girdwood3488cce2017-08-10 11:59:08 +0100628 if (src->source_rate == 0) {
629 /* params rate is source rate */
630 source_rate = params->rate;
631 sink_rate = src->sink_rate;
632 /* re-write our params with output rate for next component */
633 params->rate = sink_rate;
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300634 frames_is_for_source = 0;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100635 } else {
636 /* params rate is sink rate */
637 source_rate = src->source_rate;
638 sink_rate = params->rate;
639 /* re-write our params with output rate for next component */
640 params->rate = source_rate;
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300641 frames_is_for_source = 1;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100642 }
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300643
644 /* Allocate needed memory for delay lines */
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300645 err = src_buffer_lengths(&cd->param, source_rate, sink_rate,
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300646 params->channels, dev->frames, frames_is_for_source);
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100647 if (err < 0) {
648 trace_src_error("sr1");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700649 trace_error_value(source_rate);
650 trace_error_value(sink_rate);
651 trace_error_value(params->channels);
652 trace_error_value(dev->frames);
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100653 return err;
654 }
655
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300656 delay_lines_size = sizeof(int32_t) * cd->param.total;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100657 if (delay_lines_size == 0) {
658 trace_src_error("sr2");
659 return -EINVAL;
660 }
661
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600662 /* free any existing delay lines. TODO reuse if same size */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200663 if (!cd->delay_lines)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300664 rfree(cd->delay_lines);
665
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000666 cd->delay_lines = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM,
667 delay_lines_size);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200668 if (!cd->delay_lines) {
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100669 trace_src_error("sr3");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700670 trace_error_value(delay_lines_size);
Liam Girdwood3488cce2017-08-10 11:59:08 +0100671 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100672 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300673
674 /* Clear all delay lines here */
675 memset(cd->delay_lines, 0, delay_lines_size);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300676 buffer_start = cd->delay_lines + cd->param.sbuf_length;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300677
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600678 /* Initialize SRC for actual sample rate */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300679 n = src_polyphase_init(&cd->src, &cd->param, buffer_start);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300680
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300681 /* Reset stage buffer */
682 cd->sbuf_r_ptr = cd->delay_lines;
683 cd->sbuf_w_ptr = cd->delay_lines;
684 cd->sbuf_avail = 0;
685
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300686 switch (n) {
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300687 case 0:
688 cd->src_func = src_copy_s32_default; /* 1:1 fast copy */
689 break;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300690 case 1:
691 cd->src_func = src_1s_s32_default; /* Simpler 1 stage SRC */
692 break;
693 case 2:
694 cd->src_func = src_2s_s32_default; /* Default 2 stage SRC */
695 break;
696 default:
697 /* This is possibly due to missing coefficients for
698 * requested rates combination. Sink audio will be
699 * muted if copy() is run.
700 */
701 trace_src("SFa");
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300702 cd->src_func = src_fallback;
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300703 return -EINVAL;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300704 }
705
Seppo Ingalsuo14d10a82017-09-22 16:08:47 +0300706 /* Calculate period size based on config. First make sure that
707 * frame_bytes is set.
708 */
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300709 dev->frame_bytes =
710 dev->params.sample_container_bytes * dev->params.channels;
Liam Girdwood44509242017-09-07 15:34:42 +0100711
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300712 /* The downstream buffer must be at least length of blk_out plus
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300713 * a dev->frames and an integer multiple of dev->frames. The
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300714 * buffer_set_size will return an error if the required length would
715 * be too long.
716 */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200717 q = src_ceil_divide(cd->param.blk_out, (int)dev->frames) + 1;
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300718
719 /* Configure downstream buffer */
720 sink = list_first_item(&dev->bsink_list, struct comp_buffer,
721 source_list);
722 err = buffer_set_size(sink, q * dev->frames * dev->frame_bytes);
Liam Girdwood44509242017-09-07 15:34:42 +0100723 if (err < 0) {
724 trace_src_error("eSz");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700725 trace_error_value(sink->alloc_size);
726 trace_error_value(q * dev->frames * dev->frame_bytes);
Liam Girdwood44509242017-09-07 15:34:42 +0100727 return err;
728 }
729
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300730 /* Check that source buffer has sufficient size */
731 source = list_first_item(&dev->bsource_list, struct comp_buffer,
732 sink_list);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300733 if (source->size < cd->param.blk_in * dev->frame_bytes) {
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300734 trace_src_error("eSy");
735 return -EINVAL;
736 }
737
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300738 return 0;
739}
740
Liam Girdwood854b2e52017-09-02 23:14:02 +0100741static int src_ctrl_cmd(struct comp_dev *dev, struct sof_ipc_ctrl_data *cdata)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300742{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300743 trace_src_error("ec1");
744 return -EINVAL;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100745}
746
747/* used to pass standard and bespoke commands (with data) to component */
748static int src_cmd(struct comp_dev *dev, int cmd, void *data)
749{
750 struct sof_ipc_ctrl_data *cdata = data;
Ranjani Sridharan62004082017-09-06 22:01:40 +0100751 int ret = 0;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100752
Liam Girdwoodbe41b682017-09-21 16:48:18 +0100753 trace_src("cmd");
754
Pierre-Louis Bossartb513a452017-09-25 14:52:12 -0500755 if (cmd == COMP_CMD_SET_VALUE)
756 ret = src_ctrl_cmd(dev, cdata);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300757
Ranjani Sridharan62004082017-09-06 22:01:40 +0100758 return ret;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300759}
760
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700761static int src_trigger(struct comp_dev *dev, int cmd)
762{
763 trace_src("trg");
764
765 return comp_set_state(dev, cmd);
766}
767
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300768/* copy and process stream data from source to sink buffers */
769static int src_copy(struct comp_dev *dev)
770{
771 struct comp_data *cd = comp_get_drvdata(dev);
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500772 struct comp_buffer *source;
773 struct comp_buffer *sink;
774 int need_source;
775 int need_sink;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300776 size_t consumed = 0;
777 size_t produced = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300778
Liam Girdwoodab505272017-11-13 16:55:30 +0000779 trace_src("SRC");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300780
781 /* src component needs 1 source and 1 sink buffer */
782 source = list_first_item(&dev->bsource_list, struct comp_buffer,
783 sink_list);
784 sink = list_first_item(&dev->bsink_list, struct comp_buffer,
785 source_list);
786
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300787 /* Calculate needed amount of source buffer and sink buffer
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300788 * for one SRC run. The blk_in and blk are minimum condition to
789 * call copy. Copy can consume or produce a slightly larger block
790 * with the rates where block sizes are not constant. E.g. for
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600791 * 1 ms scheduling the blocks can be under or above 1 ms when the
792 * SRC interval block size constraint prevents exact 1 ms blocks.
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300793 */
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300794 need_source = cd->param.blk_in * dev->frame_bytes;
795 need_sink = cd->param.blk_out * dev->frame_bytes;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300796
Liam Girdwoodab505272017-11-13 16:55:30 +0000797 /* make sure source component buffer has enough data available and that
798 * the sink component buffer has enough free bytes for copy. Also
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200799 * check for XRUNs.
800 */
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200801 if (source->avail < need_source) {
Liam Girdwoodab505272017-11-13 16:55:30 +0000802 trace_src_error("xru");
803 return -EIO; /* xrun */
804 }
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200805 if (sink->free < need_sink) {
Liam Girdwoodab505272017-11-13 16:55:30 +0000806 trace_src_error("xro");
807 return -EIO; /* xrun */
808 }
809
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200810 cd->src_func(dev, source, sink, &consumed, &produced);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300811
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200812 tracev_value(consumed >> 3);
813 tracev_value(produced >> 3);
814
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200815 /* Calc new free and available if data was processed. These
816 * functions must not be called with 0 consumed/produced.
817 */
818 if (consumed > 0)
819 comp_update_buffer_consume(source, consumed);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300820
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000821 if (produced > 0) {
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200822 comp_update_buffer_produce(sink, produced);
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000823 return cd->param.blk_out;
824 }
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200825
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000826 /* produced no data */
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300827 return 0;
828}
829
830static int src_prepare(struct comp_dev *dev)
831{
Liam Girdwoodb083e582017-10-12 16:05:44 +0100832 trace_src("pre");
833
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700834 return comp_set_state(dev, COMP_TRIGGER_PREPARE);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300835}
836
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300837static int src_reset(struct comp_dev *dev)
838{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300839 struct comp_data *cd = comp_get_drvdata(dev);
840
841 trace_src("SRe");
842
843 cd->src_func = src_2s_s32_default;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300844 src_polyphase_reset(&cd->src);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300845
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700846 comp_set_state(dev, COMP_TRIGGER_RESET);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300847 return 0;
848}
849
850struct comp_driver comp_src = {
851 .type = SOF_COMP_SRC,
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300852 .ops = {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300853 .new = src_new,
854 .free = src_free,
855 .params = src_params,
856 .cmd = src_cmd,
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700857 .trigger = src_trigger,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300858 .copy = src_copy,
859 .prepare = src_prepare,
860 .reset = src_reset,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300861 },
862};
863
864void sys_comp_src_init(void)
865{
866 comp_register(&comp_src);
867}