blob: cded8b83287e9e3290f2e83d19b7eea62ed11dff [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>
Liam Girdwood17641522017-06-09 17:27:02 +010045#include <uapi/ipc.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020046
47#include "src_config.h"
48#include "src.h"
49
50#if SRC_SHORT
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050051#include <sof/audio/coefficients/src/src_tiny_int16_define.h>
52#include <sof/audio/coefficients/src/src_tiny_int16_table.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020053#else
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050054#include <sof/audio/coefficients/src/src_std_int32_define.h>
55#include <sof/audio/coefficients/src/src_std_int32_table.h>
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020056#endif
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030057
58#ifdef MODULE_TEST
59#include <stdio.h>
60#endif
61
62#define trace_src(__e) trace_event(TRACE_CLASS_SRC, __e)
63#define tracev_src(__e) tracev_event(TRACE_CLASS_SRC, __e)
64#define trace_src_error(__e) trace_error(TRACE_CLASS_SRC, __e)
65
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020066/* The FIR maximum lengths are per channel so need to multiply them */
67#define MAX_FIR_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_FIR_DELAY_SIZE)
68#define MAX_OUT_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_OUT_DELAY_SIZE)
69
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030070/* src component private data */
71struct comp_data {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +030072 struct polyphase_src src;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030073 struct src_param param;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030074 int32_t *delay_lines;
Liam Girdwood3488cce2017-08-10 11:59:08 +010075 uint32_t sink_rate;
76 uint32_t source_rate;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030077 int32_t *sbuf_w_ptr;
78 int32_t *sbuf_r_ptr;
79 int sbuf_avail;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020080 void (*src_func)(struct comp_dev *dev,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030081 struct comp_buffer *source,
82 struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +030083 size_t *consumed,
84 size_t *produced);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020085 void (*polyphase_func)(struct src_stage_prm *s);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030086};
87
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +020088/* Calculate ceil() for integer division */
89int src_ceil_divide(int a, int b)
90{
91 int c;
92
93 c = a / b;
94 if (c * b < a)
95 c++;
96
97 return c;
98}
99
100/* Calculates the needed FIR delay line length */
101static int src_fir_delay_length(struct src_stage *s)
102{
103 return s->subfilter_length + (s->num_of_subfilters - 1) * s->idm
104 + s->blk_in;
105}
106
107/* Calculates the FIR output delay line length */
108static int src_out_delay_length(struct src_stage *s)
109{
110 return 1 + (s->num_of_subfilters - 1) * s->odm;
111}
112
113/* Returns index of a matching sample rate */
114static int src_find_fs(int fs_list[], int list_length, int fs)
115{
116 int i;
117
118 for (i = 0; i < list_length; i++) {
119 if (fs_list[i] == fs)
120 return i;
121 }
122 return -EINVAL;
123}
124
125/* Calculates buffers to allocate for a SRC mode */
126int src_buffer_lengths(struct src_param *a, int fs_in, int fs_out, int nch,
127 int frames, int frames_is_for_source)
128{
129 struct src_stage *stage1;
130 struct src_stage *stage2;
131 int q;
132 int den;
133 int num;
134 int frames2;
135
136 if (nch > PLATFORM_MAX_CHANNELS) {
137 trace_src_error("che");
138 tracev_value(nch);
139 return -EINVAL;
140 }
141
142 a->nch = nch;
143 a->idx_in = src_find_fs(src_in_fs, NUM_IN_FS, fs_in);
144 a->idx_out = src_find_fs(src_out_fs, NUM_OUT_FS, fs_out);
145
146 /* Check that both in and out rates are supported */
147 if (a->idx_in < 0 || a->idx_out < 0) {
148 trace_src_error("us1");
149 tracev_value(fs_in);
150 tracev_value(fs_out);
151 return -EINVAL;
152 }
153
154 stage1 = src_table1[a->idx_out][a->idx_in];
155 stage2 = src_table2[a->idx_out][a->idx_in];
156
157 /* Check from stage1 parameter for a deleted in/out rate combination.*/
158 if (stage1->filter_length < 1) {
159 trace_src_error("us2");
160 tracev_value(fs_in);
161 tracev_value(fs_out);
162 return -EINVAL;
163 }
164
165 a->fir_s1 = nch * src_fir_delay_length(stage1);
166 a->out_s1 = nch * src_out_delay_length(stage1);
167
168 /* Find out how many additional times the SRC can be executed
169 * while having block size less or equal to max_frames.
170 */
171 if (frames_is_for_source) {
172 /* Times that stage1 needs to run to input length of frames */
173 a->stage1_times_max = src_ceil_divide(frames, stage1->blk_in);
174 q = frames / stage1->blk_in;
175 a->stage1_times = MAX(q, 1);
176 a->blk_in = a->stage1_times * stage1->blk_in;
177
178 /* Times that stage2 needs to run */
179 den = stage2->blk_in * stage1->blk_in;
180 num = frames * stage2->blk_out * stage1->blk_out;
181 frames2 = src_ceil_divide(num, den);
182 a->stage2_times_max = src_ceil_divide(frames2, stage2->blk_out);
183 q = frames2 / stage2->blk_out;
184 a->stage2_times = MAX(q, 1);
185 a->blk_out = a->stage2_times * stage2->blk_out;
186 } else {
187 /* Times that stage2 needs to run to output length of frames */
188 a->stage2_times_max = src_ceil_divide(frames, stage2->blk_out);
189 q = frames / stage2->blk_out;
190 a->stage2_times = MAX(q, 1);
191 a->blk_out = a->stage2_times * stage2->blk_out;
192
193 /* Times that stage1 needs to run */
194 num = frames * stage2->blk_in * stage1->blk_in;
195 den = stage2->blk_out * stage1->blk_out;
196 frames2 = src_ceil_divide(num, den);
197 a->stage1_times_max = src_ceil_divide(frames2, stage1->blk_in);
198 q = frames2 / stage1->blk_in;
199 a->stage1_times = MAX(q, 1);
200 a->blk_in = a->stage1_times * stage1->blk_in;
201 }
202
203 if (stage2->filter_length == 1) {
204 a->fir_s2 = 0;
205 a->out_s2 = 0;
206 a->stage2_times = 0;
207 a->stage2_times_max = 0;
208 a->sbuf_length = 0;
209 } else {
210 a->fir_s2 = nch * src_fir_delay_length(stage2);
211 a->out_s2 = nch * src_out_delay_length(stage2);
212 /* 2x is an empirically tested length. Since the sink buffer
213 * capability to receive samples varies a shorter stage 2 output
214 * block will create a peak in internal buffer usage.
215 */
216
217 /* TODO 1: Equation for needed length */
218 a->sbuf_length = 2 * nch * stage1->blk_out
219 * a->stage1_times_max;
220 }
221
222 a->src_multich = a->fir_s1 + a->fir_s2 + a->out_s1 + a->out_s2;
223 a->total = a->sbuf_length + a->src_multich;
224
225 return 0;
226}
227
228static void src_state_reset(struct src_state *state)
229{
230 state->fir_delay_size = 0;
231 state->out_delay_size = 0;
232}
233
234static int init_stages(struct src_stage *stage1, struct src_stage *stage2,
235 struct polyphase_src *src, struct src_param *p,
236 int n, int32_t *delay_lines_start)
237{
238 /* Clear FIR state */
239 src_state_reset(&src->state1);
240 src_state_reset(&src->state2);
241
242 src->number_of_stages = n;
243 src->stage1 = stage1;
244 src->stage2 = stage2;
245 if (n == 1 && stage1->blk_out == 0)
246 return -EINVAL;
247
248 /* Optimized SRC requires subfilter length multiple of 4 */
249 if (stage1->filter_length > 1 && (stage1->subfilter_length & 0x3) > 0)
250 return -EINVAL;
251
252 if (stage2->filter_length > 1 && (stage2->subfilter_length & 0x3) > 0)
253 return -EINVAL;
254
255 /* Delay line sizes */
256 src->state1.fir_delay_size = p->fir_s1;
257 src->state1.out_delay_size = p->out_s1;
258 src->state1.fir_delay = delay_lines_start;
259 src->state1.out_delay =
260 src->state1.fir_delay + src->state1.fir_delay_size;
261 /* Initialize to last ensures that circular wrap cannot happen
262 * mid-frame. The size is multiple of channels count.
263 */
264 src->state1.fir_wp = &src->state1.fir_delay[p->fir_s1 - 1];
265 src->state1.out_rp = src->state1.out_delay;
266 if (n > 1) {
267 src->state2.fir_delay_size = p->fir_s2;
268 src->state2.out_delay_size = p->out_s2;
269 src->state2.fir_delay =
270 src->state1.out_delay + src->state1.out_delay_size;
271 src->state2.out_delay =
272 src->state2.fir_delay + src->state2.fir_delay_size;
273 /* Initialize to last ensures that circular wrap cannot happen
274 * mid-frame. The size is multiple of channels count.
275 */
276 src->state2.fir_wp = &src->state2.fir_delay[p->fir_s2 - 1];
277 src->state2.out_rp = src->state2.out_delay;
278 } else {
279 src->state2.fir_delay_size = 0;
280 src->state2.out_delay_size = 0;
281 src->state2.fir_delay = NULL;
282 src->state2.out_delay = NULL;
283 }
284
285 /* Check the sizes are less than MAX */
286 if (src->state1.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH ||
287 src->state1.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH ||
288 src->state2.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH ||
289 src->state2.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH) {
290 src->state1.fir_delay = NULL;
291 src->state1.out_delay = NULL;
292 src->state2.fir_delay = NULL;
293 src->state2.out_delay = NULL;
294 return -EINVAL;
295 }
296
297 return 0;
298}
299
300void src_polyphase_reset(struct polyphase_src *src)
301{
302 src->number_of_stages = 0;
303 src->stage1 = NULL;
304 src->stage2 = NULL;
305 src_state_reset(&src->state1);
306 src_state_reset(&src->state2);
307}
308
309int src_polyphase_init(struct polyphase_src *src, struct src_param *p,
310 int32_t *delay_lines_start)
311{
312 struct src_stage *stage1;
313 struct src_stage *stage2;
314 int n_stages;
315 int ret;
316
317 if (p->idx_in < 0 || p->idx_out < 0)
318 return -EINVAL;
319
320 /* Get setup for 2 stage conversion */
321 stage1 = src_table1[p->idx_out][p->idx_in];
322 stage2 = src_table2[p->idx_out][p->idx_in];
323 ret = init_stages(stage1, stage2, src, p, 2, delay_lines_start);
324 if (ret < 0)
325 return -EINVAL;
326
327 /* Get number of stages used for optimize opportunity. 2nd
328 * stage length is one if conversion needs only one stage.
329 * If input and output rate is the same return 0 to
330 * use a simple copy function instead of 1 stage FIR with one
331 * tap.
332 */
333 n_stages = (src->stage2->filter_length == 1) ? 1 : 2;
334 if (p->idx_in == p->idx_out)
335 n_stages = 0;
336
337 /* If filter length for first stage is zero this is a deleted
338 * mode from in/out matrix. Computing of such SRC mode needs
339 * to be prevented.
340 */
341 if (src->stage1->filter_length == 0)
342 return -EINVAL;
343
344 return n_stages;
345}
346
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300347/* Fallback function */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300348static void src_fallback(struct comp_dev *dev, struct comp_buffer *source,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300349 struct comp_buffer *sink, size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300350{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300351 *bytes_read = 0;
352 *bytes_written = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300353}
354
355/* Normal 2 stage SRC */
356static void src_2s_s32_default(struct comp_dev *dev,
357 struct comp_buffer *source, struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300358 size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300359{
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500360 struct src_stage_prm s1;
361 struct src_stage_prm s2;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300362 int s1_blk_in;
363 int s1_blk_out;
364 int s2_blk_in;
365 int s2_blk_out;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300366 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200367 int32_t *dest = (int32_t *)sink->w_ptr;
368 int32_t *src = (int32_t *)source->r_ptr;
369 int32_t *sbuf_addr = cd->delay_lines;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300370 int32_t *sbuf_end_addr = &cd->delay_lines[cd->param.sbuf_length];
371 int32_t sbuf_size = cd->param.sbuf_length * sizeof(int32_t);
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300372 int nch = dev->params.channels;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300373 int sbuf_free = cd->param.sbuf_length - cd->sbuf_avail;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300374 int n_read = 0;
375 int n_written = 0;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300376 int n1 = 0;
377 int n2 = 0;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300378 int avail_b = source->avail;
379 int free_b = sink->free;
380 int sz = sizeof(int32_t);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300381
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300382 s1.x_end_addr = source->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100383 s1.x_size = source->size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200384 s1.y_addr = sbuf_addr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300385 s1.y_end_addr = sbuf_end_addr;
386 s1.y_size = sbuf_size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300387 s1.state = &cd->src.state1;
388 s1.stage = cd->src.stage1;
389 s1.x_rptr = src;
390 s1.y_wptr = cd->sbuf_w_ptr;
391 s1.nch = nch;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300392
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300393 s2.x_end_addr = sbuf_end_addr;
394 s2.x_size = sbuf_size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200395 s2.y_addr = sink->addr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300396 s2.y_end_addr = sink->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100397 s2.y_size = sink->size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300398 s2.state = &cd->src.state2;
399 s2.stage = cd->src.stage2;
400 s2.x_rptr = cd->sbuf_r_ptr;
401 s2.y_wptr = dest;
402 s2.nch = nch;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300403
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300404 /* Test if 1st stage can be run with default block length to reach
405 * the period length or just under it.
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300406 */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300407 s1.times = cd->param.stage1_times;
408 s1_blk_in = s1.times * cd->src.stage1->blk_in * nch;
409 s1_blk_out = s1.times * cd->src.stage1->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200410 if (avail_b >= s1_blk_in * sz && sbuf_free >= s1_blk_out) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300411 cd->polyphase_func(&s1);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300412
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300413 cd->sbuf_w_ptr = s1.y_wptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300414 cd->sbuf_avail += s1_blk_out;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300415 n_read += s1_blk_in;
416 avail_b -= s1_blk_in * sz;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300417 sbuf_free -= s1_blk_out;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300418 n1 = s1.times;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300419 }
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300420
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300421 /* Run one block at time the remaining data for 1st stage. */
422 s1.times = 1;
423 s1_blk_in = cd->src.stage1->blk_in * nch;
424 s1_blk_out = cd->src.stage1->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200425 while (n1 < cd->param.stage1_times_max &&
426 avail_b >= s1_blk_in * sz &&
427 sbuf_free >= s1_blk_out) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300428 cd->polyphase_func(&s1);
429
430 cd->sbuf_w_ptr = s1.y_wptr;
431 cd->sbuf_avail += s1_blk_out;
432 n_read += s1_blk_in;
433 avail_b -= s1_blk_in * sz;
434 sbuf_free -= s1_blk_out;
435 n1 += s1.times;
436 }
437
438 /* Test if 2nd stage can be run with default block length. */
439 s2.times = cd->param.stage2_times;
440 s2_blk_in = s2.times * cd->src.stage2->blk_in * nch;
441 s2_blk_out = s2.times * cd->src.stage2->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200442 if (cd->sbuf_avail >= s2_blk_in && free_b >= s2_blk_out * sz) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300443 cd->polyphase_func(&s2);
444
445 cd->sbuf_r_ptr = s2.x_rptr;
446 cd->sbuf_avail -= s2_blk_in;
447 free_b -= s2_blk_out * sz;
448 n_written += s2_blk_out;
449 n2 = s2.times;
450 }
451
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300452 /* Run one block at time the remaining 2nd stage output */
453 s2.times = 1;
454 s2_blk_in = cd->src.stage2->blk_in * nch;
455 s2_blk_out = cd->src.stage2->blk_out * nch;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200456 while (n2 < cd->param.stage2_times_max &&
457 cd->sbuf_avail >= s2_blk_in &&
458 free_b >= s2_blk_out * sz) {
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300459 cd->polyphase_func(&s2);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300460
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300461 cd->sbuf_r_ptr = s2.x_rptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300462 cd->sbuf_avail -= s2_blk_in;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300463 free_b -= s2_blk_out * sz;
464 n_written += s2_blk_out;
465 n2 += s2.times;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300466 }
467 *bytes_read = sizeof(int32_t) * n_read;
468 *bytes_written = sizeof(int32_t) * n_written;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300469}
470
471/* 1 stage SRC for simple conversions */
472static void src_1s_s32_default(struct comp_dev *dev,
473 struct comp_buffer *source, struct comp_buffer *sink,
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300474 size_t *bytes_read, size_t *bytes_written)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300475{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300476 struct src_stage_prm s1;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300477 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300478 int nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300479 int n_read = 0;
480 int n_written = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300481
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300482 s1.times = cd->param.stage1_times;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200483 s1.x_rptr = (int32_t *)source->r_ptr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300484 s1.x_end_addr = source->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100485 s1.x_size = source->size;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200486 s1.y_wptr = (int32_t *)sink->w_ptr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300487 s1.y_end_addr = sink->end_addr;
Liam Girdwood44509242017-09-07 15:34:42 +0100488 s1.y_size = sink->size;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300489 s1.state = &cd->src.state1;
490 s1.stage = cd->src.stage1;
491 s1.nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300492
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300493 cd->polyphase_func(&s1);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300494
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300495 n_read += nch * cd->param.blk_in;
496 n_written += nch * cd->param.blk_out;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300497 *bytes_read = n_read * sizeof(int32_t);
498 *bytes_written = n_written * sizeof(int32_t);
499}
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300500
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300501/* A fast copy function for same in and out rate */
502static void src_copy_s32_default(struct comp_dev *dev,
503 struct comp_buffer *source, struct comp_buffer *sink,
504 size_t *bytes_read, size_t *bytes_written)
505{
506 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200507 int32_t *src = (int32_t *)source->r_ptr;
508 int32_t *snk = (int32_t *)sink->w_ptr;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300509 int nch = dev->params.channels;
510 int frames = cd->param.blk_in;
511 int n;
512 int n_wrap_src;
513 int n_wrap_snk;
514 int n_wrap_min;
515 int n_copy;
516
517 n = frames * nch;
518 while (n > 0) {
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200519 n_wrap_src = (int32_t *)source->end_addr - src;
520 n_wrap_snk = (int32_t *)sink->end_addr - snk;
521 n_wrap_min = (n_wrap_src < n_wrap_snk) ?
522 n_wrap_src : n_wrap_snk;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300523 n_copy = (n < n_wrap_min) ? n : n_wrap_min;
524 memcpy(snk, src, n_copy * sizeof(int32_t));
525
526 /* Update and check both source and destination for wrap */
527 n -= n_copy;
528 src += n_copy;
529 snk += n_copy;
530 src_circ_inc_wrap(&src, source->end_addr, source->size);
531 src_circ_inc_wrap(&snk, sink->end_addr, sink->size);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300532 }
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300533 *bytes_read = frames * nch * sizeof(int32_t);
534 *bytes_written = frames * nch * sizeof(int32_t);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300535}
536
537static struct comp_dev *src_new(struct sof_ipc_comp *comp)
538{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300539 struct comp_dev *dev;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100540 struct sof_ipc_comp_src *src;
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200541 struct sof_ipc_comp_src *ipc_src = (struct sof_ipc_comp_src *)comp;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300542 struct comp_data *cd;
543
Liam Girdwood3488cce2017-08-10 11:59:08 +0100544 trace_src("new");
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100545
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100546 /* validate init data - either SRC sink or source rate must be set */
547 if (ipc_src->source_rate == 0 && ipc_src->sink_rate == 0) {
548 trace_src_error("sn1");
549 return NULL;
550 }
551
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000552 dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM,
Liam Girdwood17641522017-06-09 17:27:02 +0100553 COMP_SIZE(struct sof_ipc_comp_src));
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200554 if (!dev)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300555 return NULL;
556
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200557 src = (struct sof_ipc_comp_src *)&dev->comp;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100558 memcpy(src, ipc_src, sizeof(struct sof_ipc_comp_src));
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300559
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000560 cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd));
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200561 if (!cd) {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300562 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300563 return NULL;
564 }
565
566 comp_set_drvdata(dev, cd);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300567
568 cd->delay_lines = NULL;
569 cd->src_func = src_2s_s32_default;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300570 cd->polyphase_func = src_polyphase_stage_cir;
571 src_polyphase_reset(&cd->src);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300572
Liam Girdwoodbe41b682017-09-21 16:48:18 +0100573 dev->state = COMP_STATE_READY;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300574 return dev;
575}
576
577static void src_free(struct comp_dev *dev)
578{
579 struct comp_data *cd = comp_get_drvdata(dev);
580
Liam Girdwood3488cce2017-08-10 11:59:08 +0100581 trace_src("fre");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300582
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300583 /* Free dynamically reserved buffers for SRC algorithm */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200584 if (!cd->delay_lines)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300585 rfree(cd->delay_lines);
586
587 rfree(cd);
588 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300589}
590
591/* set component audio stream parameters */
Liam Girdwood2cfaebe2017-08-21 17:13:52 +0100592static int src_params(struct comp_dev *dev)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300593{
Liam Girdwood3488cce2017-08-10 11:59:08 +0100594 struct sof_ipc_stream_params *params = &dev->params;
595 struct sof_ipc_comp_src *src = COMP_GET_IPC(dev, sof_ipc_comp_src);
596 struct sof_ipc_comp_config *config = COMP_GET_CONFIG(dev);
597 struct comp_data *cd = comp_get_drvdata(dev);
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500598 struct comp_buffer *sink;
599 struct comp_buffer *source;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300600 size_t delay_lines_size;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500601 uint32_t source_rate;
602 uint32_t sink_rate;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300603 int32_t *buffer_start;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500604 int n = 0;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500605 int err;
606 int frames_is_for_source;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500607 int q;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300608
Liam Girdwood3488cce2017-08-10 11:59:08 +0100609 trace_src("par");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300610
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300611 /* SRC supports S24_4LE and S32_LE formats */
612 switch (config->frame_fmt) {
613 case SOF_IPC_FRAME_S24_4LE:
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300614 cd->polyphase_func = src_polyphase_stage_cir_s24;
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300615 break;
616 case SOF_IPC_FRAME_S32_LE:
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300617 cd->polyphase_func = src_polyphase_stage_cir;
Seppo Ingalsuo887628a2017-09-14 16:00:05 +0300618 break;
619 default:
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100620 trace_src_error("sr0");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300621 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100622 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300623
Liam Girdwood3488cce2017-08-10 11:59:08 +0100624 /* Calculate source and sink rates, one rate will come from IPC new
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200625 * and the other from params.
626 */
Liam Girdwood3488cce2017-08-10 11:59:08 +0100627 if (src->source_rate == 0) {
628 /* params rate is source rate */
629 source_rate = params->rate;
630 sink_rate = src->sink_rate;
631 /* re-write our params with output rate for next component */
632 params->rate = sink_rate;
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300633 frames_is_for_source = 0;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100634 } else {
635 /* params rate is sink rate */
636 source_rate = src->source_rate;
637 sink_rate = params->rate;
638 /* re-write our params with output rate for next component */
639 params->rate = source_rate;
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300640 frames_is_for_source = 1;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100641 }
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300642
643 /* Allocate needed memory for delay lines */
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300644 err = src_buffer_lengths(&cd->param, source_rate, sink_rate,
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300645 params->channels, dev->frames, frames_is_for_source);
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100646 if (err < 0) {
647 trace_src_error("sr1");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700648 trace_error_value(source_rate);
649 trace_error_value(sink_rate);
650 trace_error_value(params->channels);
651 trace_error_value(dev->frames);
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100652 return err;
653 }
654
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300655 delay_lines_size = sizeof(int32_t) * cd->param.total;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100656 if (delay_lines_size == 0) {
657 trace_src_error("sr2");
658 return -EINVAL;
659 }
660
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600661 /* free any existing delay lines. TODO reuse if same size */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200662 if (!cd->delay_lines)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300663 rfree(cd->delay_lines);
664
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000665 cd->delay_lines = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM,
666 delay_lines_size);
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200667 if (!cd->delay_lines) {
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100668 trace_src_error("sr3");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700669 trace_error_value(delay_lines_size);
Liam Girdwood3488cce2017-08-10 11:59:08 +0100670 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100671 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300672
673 /* Clear all delay lines here */
674 memset(cd->delay_lines, 0, delay_lines_size);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300675 buffer_start = cd->delay_lines + cd->param.sbuf_length;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300676
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600677 /* Initialize SRC for actual sample rate */
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300678 n = src_polyphase_init(&cd->src, &cd->param, buffer_start);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300679
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300680 /* Reset stage buffer */
681 cd->sbuf_r_ptr = cd->delay_lines;
682 cd->sbuf_w_ptr = cd->delay_lines;
683 cd->sbuf_avail = 0;
684
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300685 switch (n) {
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300686 case 0:
687 cd->src_func = src_copy_s32_default; /* 1:1 fast copy */
688 break;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300689 case 1:
690 cd->src_func = src_1s_s32_default; /* Simpler 1 stage SRC */
691 break;
692 case 2:
693 cd->src_func = src_2s_s32_default; /* Default 2 stage SRC */
694 break;
695 default:
696 /* This is possibly due to missing coefficients for
697 * requested rates combination. Sink audio will be
698 * muted if copy() is run.
699 */
700 trace_src("SFa");
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300701 cd->src_func = src_fallback;
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300702 return -EINVAL;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300703 }
704
Seppo Ingalsuo14d10a82017-09-22 16:08:47 +0300705 /* Calculate period size based on config. First make sure that
706 * frame_bytes is set.
707 */
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300708 dev->frame_bytes =
709 dev->params.sample_container_bytes * dev->params.channels;
Liam Girdwood44509242017-09-07 15:34:42 +0100710
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300711 /* The downstream buffer must be at least length of blk_out plus
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300712 * a dev->frames and an integer multiple of dev->frames. The
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300713 * buffer_set_size will return an error if the required length would
714 * be too long.
715 */
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200716 q = src_ceil_divide(cd->param.blk_out, (int)dev->frames) + 1;
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300717
718 /* Configure downstream buffer */
719 sink = list_first_item(&dev->bsink_list, struct comp_buffer,
720 source_list);
721 err = buffer_set_size(sink, q * dev->frames * dev->frame_bytes);
Liam Girdwood44509242017-09-07 15:34:42 +0100722 if (err < 0) {
723 trace_src_error("eSz");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700724 trace_error_value(sink->alloc_size);
725 trace_error_value(q * dev->frames * dev->frame_bytes);
Liam Girdwood44509242017-09-07 15:34:42 +0100726 return err;
727 }
728
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300729 /* Check that source buffer has sufficient size */
730 source = list_first_item(&dev->bsource_list, struct comp_buffer,
731 sink_list);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300732 if (source->size < cd->param.blk_in * dev->frame_bytes) {
Seppo Ingalsuo54162292017-09-12 13:45:50 +0300733 trace_src_error("eSy");
734 return -EINVAL;
735 }
736
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300737 return 0;
738}
739
Liam Girdwood854b2e52017-09-02 23:14:02 +0100740static int src_ctrl_cmd(struct comp_dev *dev, struct sof_ipc_ctrl_data *cdata)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300741{
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300742 trace_src_error("ec1");
743 return -EINVAL;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100744}
745
746/* used to pass standard and bespoke commands (with data) to component */
747static int src_cmd(struct comp_dev *dev, int cmd, void *data)
748{
749 struct sof_ipc_ctrl_data *cdata = data;
Ranjani Sridharan62004082017-09-06 22:01:40 +0100750 int ret = 0;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100751
Liam Girdwoodbe41b682017-09-21 16:48:18 +0100752 trace_src("cmd");
753
Pierre-Louis Bossartb513a452017-09-25 14:52:12 -0500754 if (cmd == COMP_CMD_SET_VALUE)
755 ret = src_ctrl_cmd(dev, cdata);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300756
Ranjani Sridharan62004082017-09-06 22:01:40 +0100757 return ret;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300758}
759
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700760static int src_trigger(struct comp_dev *dev, int cmd)
761{
762 trace_src("trg");
763
764 return comp_set_state(dev, cmd);
765}
766
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300767/* copy and process stream data from source to sink buffers */
768static int src_copy(struct comp_dev *dev)
769{
770 struct comp_data *cd = comp_get_drvdata(dev);
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500771 struct comp_buffer *source;
772 struct comp_buffer *sink;
773 int need_source;
774 int need_sink;
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300775 size_t consumed = 0;
776 size_t produced = 0;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300777
Liam Girdwoodab505272017-11-13 16:55:30 +0000778 trace_src("SRC");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300779
780 /* src component needs 1 source and 1 sink buffer */
781 source = list_first_item(&dev->bsource_list, struct comp_buffer,
782 sink_list);
783 sink = list_first_item(&dev->bsink_list, struct comp_buffer,
784 source_list);
785
Seppo Ingalsuocb3a64d2017-09-06 15:16:12 +0300786 /* Calculate needed amount of source buffer and sink buffer
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300787 * for one SRC run. The blk_in and blk are minimum condition to
788 * call copy. Copy can consume or produce a slightly larger block
789 * with the rates where block sizes are not constant. E.g. for
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600790 * 1 ms scheduling the blocks can be under or above 1 ms when the
791 * SRC interval block size constraint prevents exact 1 ms blocks.
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300792 */
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300793 need_source = cd->param.blk_in * dev->frame_bytes;
794 need_sink = cd->param.blk_out * dev->frame_bytes;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300795
Liam Girdwoodab505272017-11-13 16:55:30 +0000796 /* make sure source component buffer has enough data available and that
797 * the sink component buffer has enough free bytes for copy. Also
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200798 * check for XRUNs.
799 */
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200800 if (source->avail < need_source) {
Liam Girdwoodab505272017-11-13 16:55:30 +0000801 trace_src_error("xru");
802 return -EIO; /* xrun */
803 }
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200804 if (sink->free < need_sink) {
Liam Girdwoodab505272017-11-13 16:55:30 +0000805 trace_src_error("xro");
806 return -EIO; /* xrun */
807 }
808
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200809 cd->src_func(dev, source, sink, &consumed, &produced);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300810
Seppo Ingalsuoe8afa162018-03-08 16:22:17 +0200811 tracev_value(consumed >> 3);
812 tracev_value(produced >> 3);
813
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200814 /* Calc new free and available if data was processed. These
815 * functions must not be called with 0 consumed/produced.
816 */
817 if (consumed > 0)
818 comp_update_buffer_consume(source, consumed);
Seppo Ingalsuo28fafc12017-10-05 18:57:37 +0300819
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000820 if (produced > 0) {
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200821 comp_update_buffer_produce(sink, produced);
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000822 return cd->param.blk_out;
823 }
Seppo Ingalsuo83b75602017-12-05 15:54:57 +0200824
Liam Girdwoode0f65a22017-12-08 20:34:29 +0000825 /* produced no data */
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300826 return 0;
827}
828
829static int src_prepare(struct comp_dev *dev)
830{
Liam Girdwoodb083e582017-10-12 16:05:44 +0100831 trace_src("pre");
832
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700833 return comp_set_state(dev, COMP_TRIGGER_PREPARE);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300834}
835
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300836static int src_reset(struct comp_dev *dev)
837{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300838 struct comp_data *cd = comp_get_drvdata(dev);
839
840 trace_src("SRe");
841
842 cd->src_func = src_2s_s32_default;
Seppo Ingalsuo4b007cb2017-10-20 18:45:30 +0300843 src_polyphase_reset(&cd->src);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300844
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700845 comp_set_state(dev, COMP_TRIGGER_RESET);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300846 return 0;
847}
848
849struct comp_driver comp_src = {
850 .type = SOF_COMP_SRC,
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300851 .ops = {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300852 .new = src_new,
853 .free = src_free,
854 .params = src_params,
855 .cmd = src_cmd,
Liam Girdwood7ec3a7c2018-03-28 18:05:37 -0700856 .trigger = src_trigger,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300857 .copy = src_copy,
858 .prepare = src_prepare,
859 .reset = src_reset,
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300860 },
861};
862
863void sys_comp_src_init(void)
864{
865 comp_register(&comp_src);
866}