blob: c11ef3fd0d9caddeebcfc496a3fdf3cc3f796883 [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>
36#include <reef/reef.h>
37#include <reef/lock.h>
38#include <reef/list.h>
39#include <reef/stream.h>
40#include <reef/alloc.h>
41#include <reef/work.h>
42#include <reef/clock.h>
43#include <reef/audio/component.h>
44#include <reef/audio/pipeline.h>
Liam Girdwood17641522017-06-09 17:27:02 +010045#include <uapi/ipc.h>
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030046#include "src_core.h"
47
48#ifdef MODULE_TEST
49#include <stdio.h>
50#endif
51
52#define trace_src(__e) trace_event(TRACE_CLASS_SRC, __e)
53#define tracev_src(__e) tracev_event(TRACE_CLASS_SRC, __e)
54#define trace_src_error(__e) trace_error(TRACE_CLASS_SRC, __e)
55
56/* src component private data */
57struct comp_data {
58 struct polyphase_src src[PLATFORM_MAX_CHANNELS];
59 int32_t *delay_lines;
60 int scratch_length;
Liam Girdwood3488cce2017-08-10 11:59:08 +010061 uint32_t sink_rate;
62 uint32_t source_rate;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +030063 void (*src_func)(struct comp_dev *dev,
64 struct comp_buffer *source,
65 struct comp_buffer *sink,
66 uint32_t source_frames,
67 uint32_t sink_frames);
68};
69
70/* Common mute function for 2s and 1s SRC. This preserves the same
71 * buffer consume and produce pattern as normal operation.
72 */
73static void src_muted_s32(struct comp_buffer *source, struct comp_buffer *sink,
74 int blk_in, int blk_out, int nch, int source_frames)
75{
76
77 int i;
78 int32_t *src = (int32_t *) source->r_ptr;
79 int32_t *dest = (int32_t *) sink->w_ptr;
80 int32_t *end = (int32_t *) sink->end_addr;
81 int n_read = 0;
82 int n_max;
83 int n;
84 int n_written = 0;
85
86 for (i = 0; i < source_frames - blk_in + 1; i += blk_in) {
87 n_max = end - dest;
88 n = nch*blk_out;
89 if (n < n_max) {
90 bzero(dest, n * sizeof(int32_t));
91 dest += n;
92 } else {
93 /* Also case n_max == n is done here */
94 bzero(dest, n_max * sizeof(int32_t));
95 dest = (int32_t *) sink->addr;
96 bzero(dest, (n - n_max) * sizeof(int32_t));
97 dest += n - n_max;
98 }
99 n_read += nch*blk_in;
100 n_written += nch*blk_out;
101 }
102 source->r_ptr = src + n_read;
103 sink->w_ptr = dest;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300104}
105
106/* Fallback function to just output muted samples and advance
107 * pointers. Note that a buffer that is not having integer number of
108 * frames in a period will drift since there is no similar blk in/out
109 * check as for SRC.
110 */
111static void fallback_s32(struct comp_dev *dev,
112 struct comp_buffer *source,
113 struct comp_buffer *sink,
114 uint32_t source_frames,
115 uint32_t sink_frames)
116{
117
118 struct comp_data *cd = comp_get_drvdata(dev);
Liam Girdwood3488cce2017-08-10 11:59:08 +0100119 int nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300120 int blk_in = cd->src[0].blk_in;
121 int blk_out = cd->src[0].blk_out;
122
123 src_muted_s32(source, sink, blk_in, blk_out, nch, source_frames);
124
125}
126
127/* Normal 2 stage SRC */
128static void src_2s_s32_default(struct comp_dev *dev,
129 struct comp_buffer *source, struct comp_buffer *sink,
130 uint32_t source_frames, uint32_t sink_frames)
131{
132 int i, j;
133 struct polyphase_src *s;
134 struct comp_data *cd = comp_get_drvdata(dev);
135 int blk_in = cd->src[0].blk_in;
136 int blk_out = cd->src[0].blk_out;
137 int n_times1 = cd->src[0].stage1_times;
138 int n_times2 = cd->src[0].stage2_times;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100139 int nch = dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300140 int32_t *dest = (int32_t *) sink->w_ptr;
141 int32_t *src = (int32_t *) source->r_ptr;
142 struct src_stage_prm s1, s2;
143 int n_read = 0;
144 int n_written = 0;
145
146 if (cd->src[0].mute) {
Liam Girdwood3488cce2017-08-10 11:59:08 +0100147 src_muted_s32(source, sink, blk_in, blk_out, nch, source_frames);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300148 return;
149 }
150
151 s1.times = n_times1;
152 s1.x_end_addr = source->end_addr;
153 s1.x_size = source->alloc_size;
154 s1.x_inc = nch;
155 s1.y_end_addr = &cd->delay_lines[cd->scratch_length];
156 s1.y_size = STAGE_BUF_SIZE * sizeof(int32_t);
157 s1.y_inc = 1;
158
159 s2.times = n_times2;
160 s2.x_end_addr = &cd->delay_lines[cd->scratch_length];
161 s2.x_size = STAGE_BUF_SIZE * sizeof(int32_t);
162 s2.x_inc = 1;
163 s2.y_end_addr = sink->end_addr;
164 s2.y_size = sink->alloc_size;
165 s2.y_inc = nch;
166
Seppo Ingalsuo6f27ab62017-06-12 11:31:15 +0300167 s1.x_rptr = src + nch - 1;
168 s2.y_wptr = dest + nch - 1;
169
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300170 for (j = 0; j < nch; j++) {
171 s = &cd->src[j]; /* Point to src[] for this channel */
172 s1.x_rptr = src++;
173 s2.y_wptr = dest++;
174 s1.state = &s->state1;
175 s1.stage = s->stage1;
176 s2.state = &s->state2;
177 s2.stage = s->stage2;
178
179 for (i = 0; i < source_frames - blk_in + 1; i += blk_in) {
180 /* Reset output to buffer start, read interleaved */
181 s1.y_wptr = cd->delay_lines;
182 src_polyphase_stage_cir(&s1);
183 s2.x_rptr = cd->delay_lines;
184 src_polyphase_stage_cir(&s2);
185
186 n_read += blk_in;
187 n_written += blk_out;
188 }
189 }
190 source->r_ptr = s1.x_rptr - nch + 1;
191 sink->w_ptr = s2.y_wptr - nch + 1;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300192}
193
194/* 1 stage SRC for simple conversions */
195static void src_1s_s32_default(struct comp_dev *dev,
196 struct comp_buffer *source, struct comp_buffer *sink,
197 uint32_t source_frames, uint32_t sink_frames)
198{
199 int i, j;
200 //int32_t *xp, *yp;
201 struct polyphase_src *s;
202
203 struct comp_data *cd = comp_get_drvdata(dev);
204 int blk_in = cd->src[0].blk_in;
205 int blk_out = cd->src[0].blk_out;
206 int n_times = cd->src[0].stage1_times;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100207 int nch = dev->params.channels;
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300208 int32_t *dest = (int32_t *) sink->w_ptr;
209 int32_t *src = (int32_t *) source->r_ptr;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300210 int n_read = 0;
211 int n_written = 0;
212 struct src_stage_prm s1;
213
214 if (cd->src[0].mute) {
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300215 src_muted_s32(source, sink, blk_in, blk_out, nch,
216 source_frames);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300217 return;
218 }
219
220 s1.times = n_times;
221 s1.x_end_addr = source->end_addr;
222 s1.x_size = source->alloc_size;
223 s1.x_inc = nch;
224 s1.y_end_addr = sink->end_addr;
225 s1.y_size = sink->alloc_size;
226 s1.y_inc = nch;
Seppo Ingalsuo6f27ab62017-06-12 11:31:15 +0300227 s1.x_rptr = src + nch - 1;
228 s1.y_wptr = dest + nch - 1;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300229
230 for (j = 0; j < nch; j++) {
231 s = &cd->src[j]; /* Point to src for this channel */
232 s1.x_rptr = src++;
233 s1.y_wptr = dest++;
234 s1.state = &s->state1;
235 s1.stage = s->stage1;
236
237 for (i = 0; i + blk_in - 1 < source_frames; i += blk_in) {
238 src_polyphase_stage_cir(&s1);
239
240 n_read += blk_in;
241 n_written += blk_out;
242 }
243
244 }
245 source->r_ptr = s1.x_rptr - nch + 1;
246 sink->w_ptr = s1.y_wptr - nch + 1;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300247}
248
249static struct comp_dev *src_new(struct sof_ipc_comp *comp)
250{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300251 struct comp_dev *dev;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100252 struct sof_ipc_comp_src *src;
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300253 struct sof_ipc_comp_src *ipc_src = (struct sof_ipc_comp_src *) comp;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300254 struct comp_data *cd;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100255 int i;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300256
Liam Girdwood3488cce2017-08-10 11:59:08 +0100257 trace_src("new");
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100258
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100259 /* validate init data - either SRC sink or source rate must be set */
260 if (ipc_src->source_rate == 0 && ipc_src->sink_rate == 0) {
261 trace_src_error("sn1");
262 return NULL;
263 }
264
Liam Girdwood3488cce2017-08-10 11:59:08 +0100265 dev = rzalloc(RZONE_RUNTIME, RFLAGS_NONE,
Liam Girdwood17641522017-06-09 17:27:02 +0100266 COMP_SIZE(struct sof_ipc_comp_src));
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300267 if (dev == NULL)
268 return NULL;
269
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300270 src = (struct sof_ipc_comp_src *) &dev->comp;
Liam Girdwood960cf8e2017-06-12 11:38:07 +0100271 memcpy(src, ipc_src, sizeof(struct sof_ipc_comp_src));
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300272
Liam Girdwood3488cce2017-08-10 11:59:08 +0100273 cd = rzalloc(RZONE_RUNTIME, RFLAGS_NONE, sizeof(*cd));
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300274 if (cd == NULL) {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300275 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300276 return NULL;
277 }
278
279 comp_set_drvdata(dev, cd);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300280
281 cd->delay_lines = NULL;
282 cd->src_func = src_2s_s32_default;
283 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
284 src_polyphase_reset(&cd->src[i]);
285
286 return dev;
287}
288
289static void src_free(struct comp_dev *dev)
290{
291 struct comp_data *cd = comp_get_drvdata(dev);
292
Liam Girdwood3488cce2017-08-10 11:59:08 +0100293 trace_src("fre");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300294
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300295 /* Free dynamically reserved buffers for SRC algorithm */
296 if (cd->delay_lines != NULL)
297 rfree(cd->delay_lines);
298
299 rfree(cd);
300 rfree(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300301}
302
303/* set component audio stream parameters */
Liam Girdwood2cfaebe2017-08-21 17:13:52 +0100304static int src_params(struct comp_dev *dev)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300305{
Liam Girdwood3488cce2017-08-10 11:59:08 +0100306 struct sof_ipc_stream_params *params = &dev->params;
307 struct sof_ipc_comp_src *src = COMP_GET_IPC(dev, sof_ipc_comp_src);
308 struct sof_ipc_comp_config *config = COMP_GET_CONFIG(dev);
309 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300310 struct src_alloc need;
311 size_t delay_lines_size;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100312 uint32_t source_rate, sink_rate;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300313 int32_t *buffer_start;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100314 int n = 0, i, err;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300315
Liam Girdwood3488cce2017-08-10 11:59:08 +0100316 trace_src("par");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300317
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300318 /* SRC supports only S32_LE PCM format */
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100319 if (config->frame_fmt != SOF_IPC_FRAME_S32_LE) {
320 trace_src_error("sr0");
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300321 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100322 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300323
Liam Girdwood3488cce2017-08-10 11:59:08 +0100324 /* Calculate source and sink rates, one rate will come from IPC new
325 * and the other from params. */
326 if (src->source_rate == 0) {
327 /* params rate is source rate */
328 source_rate = params->rate;
329 sink_rate = src->sink_rate;
330 /* re-write our params with output rate for next component */
331 params->rate = sink_rate;
332 } else {
333 /* params rate is sink rate */
334 source_rate = src->source_rate;
335 sink_rate = params->rate;
336 /* re-write our params with output rate for next component */
337 params->rate = source_rate;
338 }
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300339
340 /* Allocate needed memory for delay lines */
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100341 err = src_buffer_lengths(&need, source_rate, sink_rate, params->channels);
342 if (err < 0) {
343 trace_src_error("sr1");
344 trace_value(source_rate);
345 trace_value(sink_rate);
346 trace_value(params->channels);
347 return err;
348 }
349
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300350 delay_lines_size = sizeof(int32_t) * need.total;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100351 if (delay_lines_size == 0) {
352 trace_src_error("sr2");
353 return -EINVAL;
354 }
355
356 /* free any existing dalay lines. TODO reuse if same size */
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300357 if (cd->delay_lines != NULL)
358 rfree(cd->delay_lines);
359
Liam Girdwood22ce8062017-09-03 22:04:26 +0100360 cd->delay_lines = rballoc(RZONE_RUNTIME, RFLAGS_NONE, delay_lines_size);
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100361 if (cd->delay_lines == NULL) {
362 trace_src_error("sr3");
363 trace_value(delay_lines_size);
Liam Girdwood3488cce2017-08-10 11:59:08 +0100364 return -EINVAL;
Liam Girdwoodb12aa0d2017-09-03 22:00:58 +0100365 }
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300366
367 /* Clear all delay lines here */
368 memset(cd->delay_lines, 0, delay_lines_size);
369 cd->scratch_length = need.scratch;
370 buffer_start = cd->delay_lines + need.scratch;
371
372 /* Initize SRC for actual sample rate */
Liam Girdwood3488cce2017-08-10 11:59:08 +0100373 for (i = 0; i < params->channels; i++) {
374 n = src_polyphase_init(&cd->src[i], source_rate,
375 sink_rate, buffer_start);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300376 buffer_start += need.single_src;
377 }
378
379 switch (n) {
380 case 1:
381 cd->src_func = src_1s_s32_default; /* Simpler 1 stage SRC */
382 break;
383 case 2:
384 cd->src_func = src_2s_s32_default; /* Default 2 stage SRC */
385 break;
386 default:
387 /* This is possibly due to missing coefficients for
388 * requested rates combination. Sink audio will be
389 * muted if copy() is run.
390 */
391 trace_src("SFa");
392 cd->src_func = fallback_s32;
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300393 return -EINVAL;
Liam Girdwood3488cce2017-08-10 11:59:08 +0100394 break;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300395 }
396
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300397 /* Need to compute this */
398 dev->frame_bytes =
399 dev->params.sample_container_bytes * dev->params.channels;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300400
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300401 return 0;
402}
403
Liam Girdwood854b2e52017-09-02 23:14:02 +0100404static int src_ctrl_cmd(struct comp_dev *dev, struct sof_ipc_ctrl_data *cdata)
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300405{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300406 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300407 int i;
408
Liam Girdwood854b2e52017-09-02 23:14:02 +0100409 switch (cdata->cmd) {
410 case SOF_CTRL_CMD_MUTE:
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300411 trace_src("SMu");
412 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
413 src_polyphase_mute(&cd->src[i]);
414
415 break;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100416 case SOF_CTRL_CMD_UNMUTE:
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300417 trace_src("SUm");
418 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
419 src_polyphase_unmute(&cd->src[i]);
420
421 break;
Liam Girdwood854b2e52017-09-02 23:14:02 +0100422 default:
423 trace_src_error("ec1");
424 return -EINVAL;
425 }
426
427 return 0;
428}
429
430/* used to pass standard and bespoke commands (with data) to component */
431static int src_cmd(struct comp_dev *dev, int cmd, void *data)
432{
433 struct sof_ipc_ctrl_data *cdata = data;
434
435 trace_src("SCm");
436
437 switch (cmd) {
438 case COMP_CMD_SET_VALUE:
439 return src_ctrl_cmd(dev, cdata);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300440 case COMP_CMD_START:
441 trace_src("SSt");
442 dev->state = COMP_STATE_RUNNING;
443 break;
444 case COMP_CMD_STOP:
445 trace_src("SSp");
446 if (dev->state == COMP_STATE_RUNNING ||
447 dev->state == COMP_STATE_DRAINING ||
448 dev->state == COMP_STATE_PAUSED) {
449 comp_buffer_reset(dev);
450 dev->state = COMP_STATE_SETUP;
451 }
452 break;
453 case COMP_CMD_PAUSE:
454 trace_src("SPe");
455 /* only support pausing for running */
456 if (dev->state == COMP_STATE_RUNNING)
457 dev->state = COMP_STATE_PAUSED;
458
459 break;
460 case COMP_CMD_RELEASE:
461 trace_src("SRl");
462 dev->state = COMP_STATE_RUNNING;
463 break;
464 default:
465 trace_src("SDf");
466 break;
467 }
468
469 return 0;
470}
471
472/* copy and process stream data from source to sink buffers */
473static int src_copy(struct comp_dev *dev)
474{
475 struct comp_data *cd = comp_get_drvdata(dev);
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300476 struct comp_buffer *source, *sink;
477 int need_source, need_sink, blk_in, blk_out, frames_source, frames_sink;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300478
479 trace_comp("SRC");
480
481 /* src component needs 1 source and 1 sink buffer */
482 source = list_first_item(&dev->bsource_list, struct comp_buffer,
483 sink_list);
484 sink = list_first_item(&dev->bsink_list, struct comp_buffer,
485 source_list);
486
487 /* Check that source has enough frames available and sink enough
488 * frames free.
489 */
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300490 blk_in = src_polyphase_get_blk_in(&cd->src[0]);
491 blk_out = src_polyphase_get_blk_out(&cd->src[0]);
492 frames_source = dev->frames * blk_in / blk_out;
493 frames_sink = frames_source * blk_out / blk_in;
494 need_source = frames_source * dev->frame_bytes;
495 need_sink = frames_sink * dev->frame_bytes;
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300496
497 /* Run as many times as buffers allow */
498 while ((source->avail >= need_source) && (sink->free >= need_sink)) {
499 /* Run src */
500 cd->src_func(dev, source, sink, frames_source, frames_sink);
501
Seppo Ingalsuo28ef8542017-08-25 18:29:04 +0300502 /* calc new free and available */
503 comp_update_buffer_consume(source, 0);
504 comp_update_buffer_produce(sink, 0);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300505 }
506
507 return 0;
508}
509
510static int src_prepare(struct comp_dev *dev)
511{
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300512 dev->state = COMP_STATE_PREPARE;
513 return 0;
514}
515
516static int src_preload(struct comp_dev *dev)
517{
Liam Girdwood3488cce2017-08-10 11:59:08 +0100518 return src_copy(dev);
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300519}
520
521static int src_reset(struct comp_dev *dev)
522{
523 int i;
524 struct comp_data *cd = comp_get_drvdata(dev);
525
526 trace_src("SRe");
527
528 cd->src_func = src_2s_s32_default;
529 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
530 src_polyphase_reset(&cd->src[i]);
531
532 dev->state = COMP_STATE_INIT;
533 return 0;
534}
535
536struct comp_driver comp_src = {
537 .type = SOF_COMP_SRC,
Seppo Ingalsuo4a006912017-06-28 19:25:51 +0300538 .ops = {
Seppo Ingalsuo6a274832017-06-07 14:17:55 +0300539 .new = src_new,
540 .free = src_free,
541 .params = src_params,
542 .cmd = src_cmd,
543 .copy = src_copy,
544 .prepare = src_prepare,
545 .reset = src_reset,
546 .preload = src_preload,
547 },
548};
549
550void sys_comp_src_init(void)
551{
552 comp_register(&comp_src);
553}