Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 1 | /* |
| 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 Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 36 | #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 Girdwood | 1764152 | 2017-06-09 17:27:02 +0100 | [diff] [blame] | 45 | #include <uapi/ipc.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 46 | |
| 47 | #include "src_config.h" |
| 48 | #include "src.h" |
| 49 | |
| 50 | #if SRC_SHORT |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 51 | #include <sof/audio/coefficients/src/src_tiny_int16_define.h> |
| 52 | #include <sof/audio/coefficients/src/src_tiny_int16_table.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 53 | #else |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 54 | #include <sof/audio/coefficients/src/src_std_int32_define.h> |
| 55 | #include <sof/audio/coefficients/src/src_std_int32_table.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 56 | #endif |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 57 | |
| 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 66 | /* 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 70 | /* src component private data */ |
| 71 | struct comp_data { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 72 | struct polyphase_src src; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 73 | struct src_param param; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 74 | int32_t *delay_lines; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 75 | uint32_t sink_rate; |
| 76 | uint32_t source_rate; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 77 | int32_t *sbuf_w_ptr; |
| 78 | int32_t *sbuf_r_ptr; |
| 79 | int sbuf_avail; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 80 | void (*src_func)(struct comp_dev *dev, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 81 | struct comp_buffer *source, |
| 82 | struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 83 | size_t *consumed, |
| 84 | size_t *produced); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 85 | void (*polyphase_func)(struct src_stage_prm *s); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 86 | }; |
| 87 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 88 | /* Calculate ceil() for integer division */ |
| 89 | int 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 */ |
| 101 | static 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 */ |
| 108 | static 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 */ |
| 114 | static 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 */ |
| 126 | int 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 | |
| 228 | static void src_state_reset(struct src_state *state) |
| 229 | { |
| 230 | state->fir_delay_size = 0; |
| 231 | state->out_delay_size = 0; |
| 232 | } |
| 233 | |
| 234 | static 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 | |
| 300 | void 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 | |
| 309 | int 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 Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 347 | /* Fallback function */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 348 | static void src_fallback(struct comp_dev *dev, struct comp_buffer *source, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 349 | struct comp_buffer *sink, size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 350 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 351 | *bytes_read = 0; |
| 352 | *bytes_written = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* Normal 2 stage SRC */ |
| 356 | static void src_2s_s32_default(struct comp_dev *dev, |
| 357 | struct comp_buffer *source, struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 358 | size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 359 | { |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 360 | struct src_stage_prm s1; |
| 361 | struct src_stage_prm s2; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 362 | int s1_blk_in; |
| 363 | int s1_blk_out; |
| 364 | int s2_blk_in; |
| 365 | int s2_blk_out; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 366 | struct comp_data *cd = comp_get_drvdata(dev); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 367 | 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 Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 370 | 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 Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 372 | int nch = dev->params.channels; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 373 | int sbuf_free = cd->param.sbuf_length - cd->sbuf_avail; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 374 | int n_read = 0; |
| 375 | int n_written = 0; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 376 | int n1 = 0; |
| 377 | int n2 = 0; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 378 | int avail_b = source->avail; |
| 379 | int free_b = sink->free; |
| 380 | int sz = sizeof(int32_t); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 381 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 382 | s1.x_end_addr = source->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 383 | s1.x_size = source->size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 384 | s1.y_addr = sbuf_addr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 385 | s1.y_end_addr = sbuf_end_addr; |
| 386 | s1.y_size = sbuf_size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 387 | 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 392 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 393 | s2.x_end_addr = sbuf_end_addr; |
| 394 | s2.x_size = sbuf_size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 395 | s2.y_addr = sink->addr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 396 | s2.y_end_addr = sink->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 397 | s2.y_size = sink->size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 398 | 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 403 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 404 | /* Test if 1st stage can be run with default block length to reach |
| 405 | * the period length or just under it. |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 406 | */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 407 | 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 410 | if (avail_b >= s1_blk_in * sz && sbuf_free >= s1_blk_out) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 411 | cd->polyphase_func(&s1); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 412 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 413 | cd->sbuf_w_ptr = s1.y_wptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 414 | cd->sbuf_avail += s1_blk_out; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 415 | n_read += s1_blk_in; |
| 416 | avail_b -= s1_blk_in * sz; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 417 | sbuf_free -= s1_blk_out; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 418 | n1 = s1.times; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 419 | } |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 420 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 421 | /* 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 425 | while (n1 < cd->param.stage1_times_max && |
| 426 | avail_b >= s1_blk_in * sz && |
| 427 | sbuf_free >= s1_blk_out) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 428 | 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 442 | if (cd->sbuf_avail >= s2_blk_in && free_b >= s2_blk_out * sz) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 443 | 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 Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 452 | /* 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 456 | while (n2 < cd->param.stage2_times_max && |
| 457 | cd->sbuf_avail >= s2_blk_in && |
| 458 | free_b >= s2_blk_out * sz) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 459 | cd->polyphase_func(&s2); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 460 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 461 | cd->sbuf_r_ptr = s2.x_rptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 462 | cd->sbuf_avail -= s2_blk_in; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 463 | free_b -= s2_blk_out * sz; |
| 464 | n_written += s2_blk_out; |
| 465 | n2 += s2.times; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 466 | } |
| 467 | *bytes_read = sizeof(int32_t) * n_read; |
| 468 | *bytes_written = sizeof(int32_t) * n_written; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* 1 stage SRC for simple conversions */ |
| 472 | static void src_1s_s32_default(struct comp_dev *dev, |
| 473 | struct comp_buffer *source, struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 474 | size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 475 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 476 | struct src_stage_prm s1; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 477 | struct comp_data *cd = comp_get_drvdata(dev); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 478 | int nch = dev->params.channels; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 479 | int n_read = 0; |
| 480 | int n_written = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 481 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 482 | s1.times = cd->param.stage1_times; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 483 | s1.x_rptr = (int32_t *)source->r_ptr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 484 | s1.x_end_addr = source->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 485 | s1.x_size = source->size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 486 | s1.y_wptr = (int32_t *)sink->w_ptr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 487 | s1.y_end_addr = sink->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 488 | s1.y_size = sink->size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 489 | s1.state = &cd->src.state1; |
| 490 | s1.stage = cd->src.stage1; |
| 491 | s1.nch = dev->params.channels; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 492 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 493 | cd->polyphase_func(&s1); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 494 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 495 | n_read += nch * cd->param.blk_in; |
| 496 | n_written += nch * cd->param.blk_out; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 497 | *bytes_read = n_read * sizeof(int32_t); |
| 498 | *bytes_written = n_written * sizeof(int32_t); |
| 499 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 500 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 501 | /* A fast copy function for same in and out rate */ |
| 502 | static 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 507 | int32_t *src = (int32_t *)source->r_ptr; |
| 508 | int32_t *snk = (int32_t *)sink->w_ptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 509 | 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 Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 519 | 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 Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 523 | 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 532 | } |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 533 | *bytes_read = frames * nch * sizeof(int32_t); |
| 534 | *bytes_written = frames * nch * sizeof(int32_t); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static struct comp_dev *src_new(struct sof_ipc_comp *comp) |
| 538 | { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 539 | struct comp_dev *dev; |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 540 | struct sof_ipc_comp_src *src; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 541 | struct sof_ipc_comp_src *ipc_src = (struct sof_ipc_comp_src *)comp; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 542 | struct comp_data *cd; |
| 543 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 544 | trace_src("new"); |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 545 | |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 546 | /* 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 Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 552 | dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, |
Liam Girdwood | 1764152 | 2017-06-09 17:27:02 +0100 | [diff] [blame] | 553 | COMP_SIZE(struct sof_ipc_comp_src)); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 554 | if (!dev) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 555 | return NULL; |
| 556 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 557 | src = (struct sof_ipc_comp_src *)&dev->comp; |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 558 | memcpy(src, ipc_src, sizeof(struct sof_ipc_comp_src)); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 559 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 560 | cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd)); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 561 | if (!cd) { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 562 | rfree(dev); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | comp_set_drvdata(dev, cd); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 567 | |
| 568 | cd->delay_lines = NULL; |
| 569 | cd->src_func = src_2s_s32_default; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 570 | cd->polyphase_func = src_polyphase_stage_cir; |
| 571 | src_polyphase_reset(&cd->src); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 572 | |
Liam Girdwood | be41b68 | 2017-09-21 16:48:18 +0100 | [diff] [blame] | 573 | dev->state = COMP_STATE_READY; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 574 | return dev; |
| 575 | } |
| 576 | |
| 577 | static void src_free(struct comp_dev *dev) |
| 578 | { |
| 579 | struct comp_data *cd = comp_get_drvdata(dev); |
| 580 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 581 | trace_src("fre"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 582 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 583 | /* Free dynamically reserved buffers for SRC algorithm */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 584 | if (!cd->delay_lines) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 585 | rfree(cd->delay_lines); |
| 586 | |
| 587 | rfree(cd); |
| 588 | rfree(dev); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* set component audio stream parameters */ |
Liam Girdwood | 2cfaebe | 2017-08-21 17:13:52 +0100 | [diff] [blame] | 592 | static int src_params(struct comp_dev *dev) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 593 | { |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 594 | 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 Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 598 | struct comp_buffer *sink; |
| 599 | struct comp_buffer *source; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 600 | size_t delay_lines_size; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 601 | uint32_t source_rate; |
| 602 | uint32_t sink_rate; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 603 | int32_t *buffer_start; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 604 | int n = 0; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 605 | int err; |
| 606 | int frames_is_for_source; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 607 | int q; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 608 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 609 | trace_src("par"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 610 | |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 611 | /* SRC supports S24_4LE and S32_LE formats */ |
| 612 | switch (config->frame_fmt) { |
| 613 | case SOF_IPC_FRAME_S24_4LE: |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 614 | cd->polyphase_func = src_polyphase_stage_cir_s24; |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 615 | break; |
| 616 | case SOF_IPC_FRAME_S32_LE: |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 617 | cd->polyphase_func = src_polyphase_stage_cir; |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 618 | break; |
| 619 | default: |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 620 | trace_src_error("sr0"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 621 | return -EINVAL; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 622 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 623 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 624 | /* Calculate source and sink rates, one rate will come from IPC new |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 625 | * and the other from params. |
| 626 | */ |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 627 | 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 Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 633 | frames_is_for_source = 0; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 634 | } 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 Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 640 | frames_is_for_source = 1; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 641 | } |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 642 | |
| 643 | /* Allocate needed memory for delay lines */ |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 644 | err = src_buffer_lengths(&cd->param, source_rate, sink_rate, |
Seppo Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 645 | params->channels, dev->frames, frames_is_for_source); |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 646 | if (err < 0) { |
| 647 | trace_src_error("sr1"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 648 | trace_error_value(source_rate); |
| 649 | trace_error_value(sink_rate); |
| 650 | trace_error_value(params->channels); |
| 651 | trace_error_value(dev->frames); |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 652 | return err; |
| 653 | } |
| 654 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 655 | delay_lines_size = sizeof(int32_t) * cd->param.total; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 656 | if (delay_lines_size == 0) { |
| 657 | trace_src_error("sr2"); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 661 | /* free any existing delay lines. TODO reuse if same size */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 662 | if (!cd->delay_lines) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 663 | rfree(cd->delay_lines); |
| 664 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 665 | cd->delay_lines = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, |
| 666 | delay_lines_size); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 667 | if (!cd->delay_lines) { |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 668 | trace_src_error("sr3"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 669 | trace_error_value(delay_lines_size); |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 670 | return -EINVAL; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 671 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 672 | |
| 673 | /* Clear all delay lines here */ |
| 674 | memset(cd->delay_lines, 0, delay_lines_size); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 675 | buffer_start = cd->delay_lines + cd->param.sbuf_length; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 676 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 677 | /* Initialize SRC for actual sample rate */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 678 | n = src_polyphase_init(&cd->src, &cd->param, buffer_start); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 679 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 680 | /* 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 685 | switch (n) { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 686 | case 0: |
| 687 | cd->src_func = src_copy_s32_default; /* 1:1 fast copy */ |
| 688 | break; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 689 | 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 Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 701 | cd->src_func = src_fallback; |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 702 | return -EINVAL; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 703 | } |
| 704 | |
Seppo Ingalsuo | 14d10a8 | 2017-09-22 16:08:47 +0300 | [diff] [blame] | 705 | /* Calculate period size based on config. First make sure that |
| 706 | * frame_bytes is set. |
| 707 | */ |
Seppo Ingalsuo | 28ef854 | 2017-08-25 18:29:04 +0300 | [diff] [blame] | 708 | dev->frame_bytes = |
| 709 | dev->params.sample_container_bytes * dev->params.channels; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 710 | |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 711 | /* The downstream buffer must be at least length of blk_out plus |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 712 | * a dev->frames and an integer multiple of dev->frames. The |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 713 | * buffer_set_size will return an error if the required length would |
| 714 | * be too long. |
| 715 | */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 716 | q = src_ceil_divide(cd->param.blk_out, (int)dev->frames) + 1; |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 717 | |
| 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 Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 722 | if (err < 0) { |
| 723 | trace_src_error("eSz"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 724 | trace_error_value(sink->alloc_size); |
| 725 | trace_error_value(q * dev->frames * dev->frame_bytes); |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 726 | return err; |
| 727 | } |
| 728 | |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 729 | /* Check that source buffer has sufficient size */ |
| 730 | source = list_first_item(&dev->bsource_list, struct comp_buffer, |
| 731 | sink_list); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 732 | if (source->size < cd->param.blk_in * dev->frame_bytes) { |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 733 | trace_src_error("eSy"); |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 740 | static int src_ctrl_cmd(struct comp_dev *dev, struct sof_ipc_ctrl_data *cdata) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 741 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 742 | trace_src_error("ec1"); |
| 743 | return -EINVAL; |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* used to pass standard and bespoke commands (with data) to component */ |
| 747 | static int src_cmd(struct comp_dev *dev, int cmd, void *data) |
| 748 | { |
| 749 | struct sof_ipc_ctrl_data *cdata = data; |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 750 | int ret = 0; |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 751 | |
Liam Girdwood | be41b68 | 2017-09-21 16:48:18 +0100 | [diff] [blame] | 752 | trace_src("cmd"); |
| 753 | |
Pierre-Louis Bossart | b513a45 | 2017-09-25 14:52:12 -0500 | [diff] [blame] | 754 | if (cmd == COMP_CMD_SET_VALUE) |
| 755 | ret = src_ctrl_cmd(dev, cdata); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 756 | |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 757 | return ret; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 758 | } |
| 759 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 760 | static 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 767 | /* copy and process stream data from source to sink buffers */ |
| 768 | static int src_copy(struct comp_dev *dev) |
| 769 | { |
| 770 | struct comp_data *cd = comp_get_drvdata(dev); |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 771 | struct comp_buffer *source; |
| 772 | struct comp_buffer *sink; |
| 773 | int need_source; |
| 774 | int need_sink; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 775 | size_t consumed = 0; |
| 776 | size_t produced = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 777 | |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 778 | trace_src("SRC"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 779 | |
| 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 Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 786 | /* Calculate needed amount of source buffer and sink buffer |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 787 | * 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 Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 790 | * 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 Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 792 | */ |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 793 | need_source = cd->param.blk_in * dev->frame_bytes; |
| 794 | need_sink = cd->param.blk_out * dev->frame_bytes; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 795 | |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 796 | /* make sure source component buffer has enough data available and that |
| 797 | * the sink component buffer has enough free bytes for copy. Also |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 798 | * check for XRUNs. |
| 799 | */ |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 800 | if (source->avail < need_source) { |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 801 | trace_src_error("xru"); |
| 802 | return -EIO; /* xrun */ |
| 803 | } |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 804 | if (sink->free < need_sink) { |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 805 | trace_src_error("xro"); |
| 806 | return -EIO; /* xrun */ |
| 807 | } |
| 808 | |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 809 | cd->src_func(dev, source, sink, &consumed, &produced); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 810 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 811 | tracev_value(consumed >> 3); |
| 812 | tracev_value(produced >> 3); |
| 813 | |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 814 | /* 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 Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 819 | |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 820 | if (produced > 0) { |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 821 | comp_update_buffer_produce(sink, produced); |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 822 | return cd->param.blk_out; |
| 823 | } |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 824 | |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 825 | /* produced no data */ |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static int src_prepare(struct comp_dev *dev) |
| 830 | { |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 831 | trace_src("pre"); |
| 832 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 833 | return comp_set_state(dev, COMP_TRIGGER_PREPARE); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 834 | } |
| 835 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 836 | static int src_reset(struct comp_dev *dev) |
| 837 | { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 838 | struct comp_data *cd = comp_get_drvdata(dev); |
| 839 | |
| 840 | trace_src("SRe"); |
| 841 | |
| 842 | cd->src_func = src_2s_s32_default; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 843 | src_polyphase_reset(&cd->src); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 844 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 845 | comp_set_state(dev, COMP_TRIGGER_RESET); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | struct comp_driver comp_src = { |
| 850 | .type = SOF_COMP_SRC, |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 851 | .ops = { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 852 | .new = src_new, |
| 853 | .free = src_free, |
| 854 | .params = src_params, |
| 855 | .cmd = src_cmd, |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 856 | .trigger = src_trigger, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 857 | .copy = src_copy, |
| 858 | .prepare = src_prepare, |
| 859 | .reset = src_reset, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 860 | }, |
| 861 | }; |
| 862 | |
| 863 | void sys_comp_src_init(void) |
| 864 | { |
| 865 | comp_register(&comp_src); |
| 866 | } |