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> |
Seppo Ingalsuo | 92dc65f | 2018-05-04 18:01:16 +0300 | [diff] [blame] | 45 | #include <sof/math/numbers.h> |
Liam Girdwood | 1764152 | 2017-06-09 17:27:02 +0100 | [diff] [blame] | 46 | #include <uapi/ipc.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 47 | |
| 48 | #include "src_config.h" |
| 49 | #include "src.h" |
| 50 | |
| 51 | #if SRC_SHORT |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 52 | #include <sof/audio/coefficients/src/src_tiny_int16_define.h> |
| 53 | #include <sof/audio/coefficients/src/src_tiny_int16_table.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 54 | #else |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 55 | #include <sof/audio/coefficients/src/src_std_int32_define.h> |
| 56 | #include <sof/audio/coefficients/src/src_std_int32_table.h> |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 57 | #endif |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 58 | |
| 59 | #ifdef MODULE_TEST |
| 60 | #include <stdio.h> |
| 61 | #endif |
| 62 | |
| 63 | #define trace_src(__e) trace_event(TRACE_CLASS_SRC, __e) |
| 64 | #define tracev_src(__e) tracev_event(TRACE_CLASS_SRC, __e) |
| 65 | #define trace_src_error(__e) trace_error(TRACE_CLASS_SRC, __e) |
| 66 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 67 | /* The FIR maximum lengths are per channel so need to multiply them */ |
| 68 | #define MAX_FIR_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_FIR_DELAY_SIZE) |
| 69 | #define MAX_OUT_DELAY_SIZE_XNCH (PLATFORM_MAX_CHANNELS * MAX_OUT_DELAY_SIZE) |
| 70 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 71 | /* src component private data */ |
| 72 | struct comp_data { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 73 | struct polyphase_src src; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 74 | struct src_param param; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 75 | int32_t *delay_lines; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 76 | uint32_t sink_rate; |
| 77 | uint32_t source_rate; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 78 | int32_t *sbuf_w_ptr; |
| 79 | int32_t *sbuf_r_ptr; |
| 80 | int sbuf_avail; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 81 | void (*src_func)(struct comp_dev *dev, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 82 | struct comp_buffer *source, |
| 83 | struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 84 | size_t *consumed, |
| 85 | size_t *produced); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 86 | void (*polyphase_func)(struct src_stage_prm *s); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 87 | }; |
| 88 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 89 | /* Calculate ceil() for integer division */ |
| 90 | int src_ceil_divide(int a, int b) |
| 91 | { |
| 92 | int c; |
| 93 | |
| 94 | c = a / b; |
| 95 | if (c * b < a) |
| 96 | c++; |
| 97 | |
| 98 | return c; |
| 99 | } |
| 100 | |
| 101 | /* Calculates the needed FIR delay line length */ |
| 102 | static int src_fir_delay_length(struct src_stage *s) |
| 103 | { |
| 104 | return s->subfilter_length + (s->num_of_subfilters - 1) * s->idm |
| 105 | + s->blk_in; |
| 106 | } |
| 107 | |
| 108 | /* Calculates the FIR output delay line length */ |
| 109 | static int src_out_delay_length(struct src_stage *s) |
| 110 | { |
| 111 | return 1 + (s->num_of_subfilters - 1) * s->odm; |
| 112 | } |
| 113 | |
| 114 | /* Returns index of a matching sample rate */ |
| 115 | static int src_find_fs(int fs_list[], int list_length, int fs) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | for (i = 0; i < list_length; i++) { |
| 120 | if (fs_list[i] == fs) |
| 121 | return i; |
| 122 | } |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | |
| 126 | /* Calculates buffers to allocate for a SRC mode */ |
| 127 | int src_buffer_lengths(struct src_param *a, int fs_in, int fs_out, int nch, |
| 128 | int frames, int frames_is_for_source) |
| 129 | { |
| 130 | struct src_stage *stage1; |
| 131 | struct src_stage *stage2; |
| 132 | int q; |
| 133 | int den; |
| 134 | int num; |
| 135 | int frames2; |
| 136 | |
| 137 | if (nch > PLATFORM_MAX_CHANNELS) { |
| 138 | trace_src_error("che"); |
| 139 | tracev_value(nch); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | a->nch = nch; |
| 144 | a->idx_in = src_find_fs(src_in_fs, NUM_IN_FS, fs_in); |
| 145 | a->idx_out = src_find_fs(src_out_fs, NUM_OUT_FS, fs_out); |
| 146 | |
| 147 | /* Check that both in and out rates are supported */ |
| 148 | if (a->idx_in < 0 || a->idx_out < 0) { |
| 149 | trace_src_error("us1"); |
| 150 | tracev_value(fs_in); |
| 151 | tracev_value(fs_out); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | stage1 = src_table1[a->idx_out][a->idx_in]; |
| 156 | stage2 = src_table2[a->idx_out][a->idx_in]; |
| 157 | |
| 158 | /* Check from stage1 parameter for a deleted in/out rate combination.*/ |
| 159 | if (stage1->filter_length < 1) { |
| 160 | trace_src_error("us2"); |
| 161 | tracev_value(fs_in); |
| 162 | tracev_value(fs_out); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | a->fir_s1 = nch * src_fir_delay_length(stage1); |
| 167 | a->out_s1 = nch * src_out_delay_length(stage1); |
| 168 | |
| 169 | /* Find out how many additional times the SRC can be executed |
| 170 | * while having block size less or equal to max_frames. |
| 171 | */ |
| 172 | if (frames_is_for_source) { |
| 173 | /* Times that stage1 needs to run to input length of frames */ |
| 174 | a->stage1_times_max = src_ceil_divide(frames, stage1->blk_in); |
| 175 | q = frames / stage1->blk_in; |
| 176 | a->stage1_times = MAX(q, 1); |
| 177 | a->blk_in = a->stage1_times * stage1->blk_in; |
| 178 | |
| 179 | /* Times that stage2 needs to run */ |
| 180 | den = stage2->blk_in * stage1->blk_in; |
| 181 | num = frames * stage2->blk_out * stage1->blk_out; |
| 182 | frames2 = src_ceil_divide(num, den); |
| 183 | a->stage2_times_max = src_ceil_divide(frames2, stage2->blk_out); |
| 184 | q = frames2 / stage2->blk_out; |
| 185 | a->stage2_times = MAX(q, 1); |
| 186 | a->blk_out = a->stage2_times * stage2->blk_out; |
| 187 | } else { |
| 188 | /* Times that stage2 needs to run to output length of frames */ |
| 189 | a->stage2_times_max = src_ceil_divide(frames, stage2->blk_out); |
| 190 | q = frames / stage2->blk_out; |
| 191 | a->stage2_times = MAX(q, 1); |
| 192 | a->blk_out = a->stage2_times * stage2->blk_out; |
| 193 | |
| 194 | /* Times that stage1 needs to run */ |
| 195 | num = frames * stage2->blk_in * stage1->blk_in; |
| 196 | den = stage2->blk_out * stage1->blk_out; |
| 197 | frames2 = src_ceil_divide(num, den); |
| 198 | a->stage1_times_max = src_ceil_divide(frames2, stage1->blk_in); |
| 199 | q = frames2 / stage1->blk_in; |
| 200 | a->stage1_times = MAX(q, 1); |
| 201 | a->blk_in = a->stage1_times * stage1->blk_in; |
| 202 | } |
| 203 | |
| 204 | if (stage2->filter_length == 1) { |
| 205 | a->fir_s2 = 0; |
| 206 | a->out_s2 = 0; |
| 207 | a->stage2_times = 0; |
| 208 | a->stage2_times_max = 0; |
| 209 | a->sbuf_length = 0; |
| 210 | } else { |
| 211 | a->fir_s2 = nch * src_fir_delay_length(stage2); |
| 212 | a->out_s2 = nch * src_out_delay_length(stage2); |
| 213 | /* 2x is an empirically tested length. Since the sink buffer |
| 214 | * capability to receive samples varies a shorter stage 2 output |
| 215 | * block will create a peak in internal buffer usage. |
| 216 | */ |
| 217 | |
| 218 | /* TODO 1: Equation for needed length */ |
| 219 | a->sbuf_length = 2 * nch * stage1->blk_out |
| 220 | * a->stage1_times_max; |
| 221 | } |
| 222 | |
| 223 | a->src_multich = a->fir_s1 + a->fir_s2 + a->out_s1 + a->out_s2; |
| 224 | a->total = a->sbuf_length + a->src_multich; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static void src_state_reset(struct src_state *state) |
| 230 | { |
| 231 | state->fir_delay_size = 0; |
| 232 | state->out_delay_size = 0; |
| 233 | } |
| 234 | |
| 235 | static int init_stages(struct src_stage *stage1, struct src_stage *stage2, |
| 236 | struct polyphase_src *src, struct src_param *p, |
| 237 | int n, int32_t *delay_lines_start) |
| 238 | { |
| 239 | /* Clear FIR state */ |
| 240 | src_state_reset(&src->state1); |
| 241 | src_state_reset(&src->state2); |
| 242 | |
| 243 | src->number_of_stages = n; |
| 244 | src->stage1 = stage1; |
| 245 | src->stage2 = stage2; |
| 246 | if (n == 1 && stage1->blk_out == 0) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | /* Optimized SRC requires subfilter length multiple of 4 */ |
| 250 | if (stage1->filter_length > 1 && (stage1->subfilter_length & 0x3) > 0) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | if (stage2->filter_length > 1 && (stage2->subfilter_length & 0x3) > 0) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | /* Delay line sizes */ |
| 257 | src->state1.fir_delay_size = p->fir_s1; |
| 258 | src->state1.out_delay_size = p->out_s1; |
| 259 | src->state1.fir_delay = delay_lines_start; |
| 260 | src->state1.out_delay = |
| 261 | src->state1.fir_delay + src->state1.fir_delay_size; |
| 262 | /* Initialize to last ensures that circular wrap cannot happen |
| 263 | * mid-frame. The size is multiple of channels count. |
| 264 | */ |
| 265 | src->state1.fir_wp = &src->state1.fir_delay[p->fir_s1 - 1]; |
| 266 | src->state1.out_rp = src->state1.out_delay; |
| 267 | if (n > 1) { |
| 268 | src->state2.fir_delay_size = p->fir_s2; |
| 269 | src->state2.out_delay_size = p->out_s2; |
| 270 | src->state2.fir_delay = |
| 271 | src->state1.out_delay + src->state1.out_delay_size; |
| 272 | src->state2.out_delay = |
| 273 | src->state2.fir_delay + src->state2.fir_delay_size; |
| 274 | /* Initialize to last ensures that circular wrap cannot happen |
| 275 | * mid-frame. The size is multiple of channels count. |
| 276 | */ |
| 277 | src->state2.fir_wp = &src->state2.fir_delay[p->fir_s2 - 1]; |
| 278 | src->state2.out_rp = src->state2.out_delay; |
| 279 | } else { |
| 280 | src->state2.fir_delay_size = 0; |
| 281 | src->state2.out_delay_size = 0; |
| 282 | src->state2.fir_delay = NULL; |
| 283 | src->state2.out_delay = NULL; |
| 284 | } |
| 285 | |
| 286 | /* Check the sizes are less than MAX */ |
| 287 | if (src->state1.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH || |
| 288 | src->state1.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH || |
| 289 | src->state2.fir_delay_size > MAX_FIR_DELAY_SIZE_XNCH || |
| 290 | src->state2.out_delay_size > MAX_OUT_DELAY_SIZE_XNCH) { |
| 291 | src->state1.fir_delay = NULL; |
| 292 | src->state1.out_delay = NULL; |
| 293 | src->state2.fir_delay = NULL; |
| 294 | src->state2.out_delay = NULL; |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | void src_polyphase_reset(struct polyphase_src *src) |
| 302 | { |
| 303 | src->number_of_stages = 0; |
| 304 | src->stage1 = NULL; |
| 305 | src->stage2 = NULL; |
| 306 | src_state_reset(&src->state1); |
| 307 | src_state_reset(&src->state2); |
| 308 | } |
| 309 | |
| 310 | int src_polyphase_init(struct polyphase_src *src, struct src_param *p, |
| 311 | int32_t *delay_lines_start) |
| 312 | { |
| 313 | struct src_stage *stage1; |
| 314 | struct src_stage *stage2; |
| 315 | int n_stages; |
| 316 | int ret; |
| 317 | |
| 318 | if (p->idx_in < 0 || p->idx_out < 0) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | /* Get setup for 2 stage conversion */ |
| 322 | stage1 = src_table1[p->idx_out][p->idx_in]; |
| 323 | stage2 = src_table2[p->idx_out][p->idx_in]; |
| 324 | ret = init_stages(stage1, stage2, src, p, 2, delay_lines_start); |
| 325 | if (ret < 0) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | /* Get number of stages used for optimize opportunity. 2nd |
| 329 | * stage length is one if conversion needs only one stage. |
| 330 | * If input and output rate is the same return 0 to |
| 331 | * use a simple copy function instead of 1 stage FIR with one |
| 332 | * tap. |
| 333 | */ |
| 334 | n_stages = (src->stage2->filter_length == 1) ? 1 : 2; |
| 335 | if (p->idx_in == p->idx_out) |
| 336 | n_stages = 0; |
| 337 | |
| 338 | /* If filter length for first stage is zero this is a deleted |
| 339 | * mode from in/out matrix. Computing of such SRC mode needs |
| 340 | * to be prevented. |
| 341 | */ |
| 342 | if (src->stage1->filter_length == 0) |
| 343 | return -EINVAL; |
| 344 | |
| 345 | return n_stages; |
| 346 | } |
| 347 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 348 | /* Fallback function */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 349 | static void src_fallback(struct comp_dev *dev, struct comp_buffer *source, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 350 | struct comp_buffer *sink, size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 351 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 352 | *bytes_read = 0; |
| 353 | *bytes_written = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Normal 2 stage SRC */ |
| 357 | static void src_2s_s32_default(struct comp_dev *dev, |
| 358 | struct comp_buffer *source, struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 359 | size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 360 | { |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 361 | struct src_stage_prm s1; |
| 362 | struct src_stage_prm s2; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 363 | int s1_blk_in; |
| 364 | int s1_blk_out; |
| 365 | int s2_blk_in; |
| 366 | int s2_blk_out; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 367 | struct comp_data *cd = comp_get_drvdata(dev); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 368 | int32_t *dest = (int32_t *)sink->w_ptr; |
| 369 | int32_t *src = (int32_t *)source->r_ptr; |
| 370 | int32_t *sbuf_addr = cd->delay_lines; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 371 | int32_t *sbuf_end_addr = &cd->delay_lines[cd->param.sbuf_length]; |
| 372 | int32_t sbuf_size = cd->param.sbuf_length * sizeof(int32_t); |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 373 | int nch = dev->params.channels; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 374 | int sbuf_free = cd->param.sbuf_length - cd->sbuf_avail; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 375 | int n_read = 0; |
| 376 | int n_written = 0; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 377 | int n1 = 0; |
| 378 | int n2 = 0; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 379 | int avail_b = source->avail; |
| 380 | int free_b = sink->free; |
| 381 | int sz = sizeof(int32_t); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 382 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 383 | s1.x_end_addr = source->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 384 | s1.x_size = source->size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 385 | s1.y_addr = sbuf_addr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 386 | s1.y_end_addr = sbuf_end_addr; |
| 387 | s1.y_size = sbuf_size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 388 | s1.state = &cd->src.state1; |
| 389 | s1.stage = cd->src.stage1; |
| 390 | s1.x_rptr = src; |
| 391 | s1.y_wptr = cd->sbuf_w_ptr; |
| 392 | s1.nch = nch; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 393 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 394 | s2.x_end_addr = sbuf_end_addr; |
| 395 | s2.x_size = sbuf_size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 396 | s2.y_addr = sink->addr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 397 | s2.y_end_addr = sink->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 398 | s2.y_size = sink->size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 399 | s2.state = &cd->src.state2; |
| 400 | s2.stage = cd->src.stage2; |
| 401 | s2.x_rptr = cd->sbuf_r_ptr; |
| 402 | s2.y_wptr = dest; |
| 403 | s2.nch = nch; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 404 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 405 | /* Test if 1st stage can be run with default block length to reach |
| 406 | * the period length or just under it. |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 407 | */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 408 | s1.times = cd->param.stage1_times; |
| 409 | s1_blk_in = s1.times * cd->src.stage1->blk_in * nch; |
| 410 | s1_blk_out = s1.times * cd->src.stage1->blk_out * nch; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 411 | if (avail_b >= s1_blk_in * sz && sbuf_free >= s1_blk_out) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 412 | cd->polyphase_func(&s1); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 413 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 414 | cd->sbuf_w_ptr = s1.y_wptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 415 | cd->sbuf_avail += s1_blk_out; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 416 | n_read += s1_blk_in; |
| 417 | avail_b -= s1_blk_in * sz; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 418 | sbuf_free -= s1_blk_out; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 419 | n1 = s1.times; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 420 | } |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 421 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 422 | /* Run one block at time the remaining data for 1st stage. */ |
| 423 | s1.times = 1; |
| 424 | s1_blk_in = cd->src.stage1->blk_in * nch; |
| 425 | s1_blk_out = cd->src.stage1->blk_out * nch; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 426 | while (n1 < cd->param.stage1_times_max && |
| 427 | avail_b >= s1_blk_in * sz && |
| 428 | sbuf_free >= s1_blk_out) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 429 | cd->polyphase_func(&s1); |
| 430 | |
| 431 | cd->sbuf_w_ptr = s1.y_wptr; |
| 432 | cd->sbuf_avail += s1_blk_out; |
| 433 | n_read += s1_blk_in; |
| 434 | avail_b -= s1_blk_in * sz; |
| 435 | sbuf_free -= s1_blk_out; |
| 436 | n1 += s1.times; |
| 437 | } |
| 438 | |
| 439 | /* Test if 2nd stage can be run with default block length. */ |
| 440 | s2.times = cd->param.stage2_times; |
| 441 | s2_blk_in = s2.times * cd->src.stage2->blk_in * nch; |
| 442 | s2_blk_out = s2.times * cd->src.stage2->blk_out * nch; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 443 | 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] | 444 | cd->polyphase_func(&s2); |
| 445 | |
| 446 | cd->sbuf_r_ptr = s2.x_rptr; |
| 447 | cd->sbuf_avail -= s2_blk_in; |
| 448 | free_b -= s2_blk_out * sz; |
| 449 | n_written += s2_blk_out; |
| 450 | n2 = s2.times; |
| 451 | } |
| 452 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 453 | /* Run one block at time the remaining 2nd stage output */ |
| 454 | s2.times = 1; |
| 455 | s2_blk_in = cd->src.stage2->blk_in * nch; |
| 456 | s2_blk_out = cd->src.stage2->blk_out * nch; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 457 | while (n2 < cd->param.stage2_times_max && |
| 458 | cd->sbuf_avail >= s2_blk_in && |
| 459 | free_b >= s2_blk_out * sz) { |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 460 | cd->polyphase_func(&s2); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 461 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 462 | cd->sbuf_r_ptr = s2.x_rptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 463 | cd->sbuf_avail -= s2_blk_in; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 464 | free_b -= s2_blk_out * sz; |
| 465 | n_written += s2_blk_out; |
| 466 | n2 += s2.times; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 467 | } |
| 468 | *bytes_read = sizeof(int32_t) * n_read; |
| 469 | *bytes_written = sizeof(int32_t) * n_written; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* 1 stage SRC for simple conversions */ |
| 473 | static void src_1s_s32_default(struct comp_dev *dev, |
| 474 | struct comp_buffer *source, struct comp_buffer *sink, |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 475 | size_t *bytes_read, size_t *bytes_written) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 476 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 477 | struct src_stage_prm s1; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 478 | struct comp_data *cd = comp_get_drvdata(dev); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 479 | int nch = dev->params.channels; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 480 | int n_read = 0; |
| 481 | int n_written = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 482 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 483 | s1.times = cd->param.stage1_times; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 484 | s1.x_rptr = (int32_t *)source->r_ptr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 485 | s1.x_end_addr = source->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 486 | s1.x_size = source->size; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 487 | s1.y_wptr = (int32_t *)sink->w_ptr; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 488 | s1.y_end_addr = sink->end_addr; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 489 | s1.y_size = sink->size; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 490 | s1.state = &cd->src.state1; |
| 491 | s1.stage = cd->src.stage1; |
| 492 | s1.nch = dev->params.channels; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 493 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 494 | cd->polyphase_func(&s1); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 495 | |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 496 | n_read += nch * cd->param.blk_in; |
| 497 | n_written += nch * cd->param.blk_out; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 498 | *bytes_read = n_read * sizeof(int32_t); |
| 499 | *bytes_written = n_written * sizeof(int32_t); |
| 500 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 501 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 502 | /* A fast copy function for same in and out rate */ |
| 503 | static void src_copy_s32_default(struct comp_dev *dev, |
| 504 | struct comp_buffer *source, struct comp_buffer *sink, |
| 505 | size_t *bytes_read, size_t *bytes_written) |
| 506 | { |
| 507 | struct comp_data *cd = comp_get_drvdata(dev); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 508 | int32_t *src = (int32_t *)source->r_ptr; |
| 509 | int32_t *snk = (int32_t *)sink->w_ptr; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 510 | int nch = dev->params.channels; |
| 511 | int frames = cd->param.blk_in; |
| 512 | int n; |
| 513 | int n_wrap_src; |
| 514 | int n_wrap_snk; |
| 515 | int n_wrap_min; |
| 516 | int n_copy; |
| 517 | |
| 518 | n = frames * nch; |
| 519 | while (n > 0) { |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 520 | n_wrap_src = (int32_t *)source->end_addr - src; |
| 521 | n_wrap_snk = (int32_t *)sink->end_addr - snk; |
| 522 | n_wrap_min = (n_wrap_src < n_wrap_snk) ? |
| 523 | n_wrap_src : n_wrap_snk; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 524 | n_copy = (n < n_wrap_min) ? n : n_wrap_min; |
| 525 | memcpy(snk, src, n_copy * sizeof(int32_t)); |
| 526 | |
| 527 | /* Update and check both source and destination for wrap */ |
| 528 | n -= n_copy; |
| 529 | src += n_copy; |
| 530 | snk += n_copy; |
| 531 | src_circ_inc_wrap(&src, source->end_addr, source->size); |
| 532 | src_circ_inc_wrap(&snk, sink->end_addr, sink->size); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 533 | } |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 534 | *bytes_read = frames * nch * sizeof(int32_t); |
| 535 | *bytes_written = frames * nch * sizeof(int32_t); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static struct comp_dev *src_new(struct sof_ipc_comp *comp) |
| 539 | { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 540 | struct comp_dev *dev; |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 541 | struct sof_ipc_comp_src *src; |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 542 | 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] | 543 | struct comp_data *cd; |
| 544 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 545 | trace_src("new"); |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 546 | |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 547 | /* validate init data - either SRC sink or source rate must be set */ |
| 548 | if (ipc_src->source_rate == 0 && ipc_src->sink_rate == 0) { |
| 549 | trace_src_error("sn1"); |
| 550 | return NULL; |
| 551 | } |
| 552 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 553 | dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, |
Liam Girdwood | 1764152 | 2017-06-09 17:27:02 +0100 | [diff] [blame] | 554 | COMP_SIZE(struct sof_ipc_comp_src)); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 555 | if (!dev) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 556 | return NULL; |
| 557 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 558 | src = (struct sof_ipc_comp_src *)&dev->comp; |
Liam Girdwood | 960cf8e | 2017-06-12 11:38:07 +0100 | [diff] [blame] | 559 | memcpy(src, ipc_src, sizeof(struct sof_ipc_comp_src)); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 560 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 561 | cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd)); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 562 | if (!cd) { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 563 | rfree(dev); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | comp_set_drvdata(dev, cd); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 568 | |
| 569 | cd->delay_lines = NULL; |
| 570 | cd->src_func = src_2s_s32_default; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 571 | cd->polyphase_func = src_polyphase_stage_cir; |
| 572 | src_polyphase_reset(&cd->src); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 573 | |
Liam Girdwood | be41b68 | 2017-09-21 16:48:18 +0100 | [diff] [blame] | 574 | dev->state = COMP_STATE_READY; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 575 | return dev; |
| 576 | } |
| 577 | |
| 578 | static void src_free(struct comp_dev *dev) |
| 579 | { |
| 580 | struct comp_data *cd = comp_get_drvdata(dev); |
| 581 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 582 | trace_src("fre"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 583 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 584 | /* Free dynamically reserved buffers for SRC algorithm */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 585 | if (!cd->delay_lines) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 586 | rfree(cd->delay_lines); |
| 587 | |
| 588 | rfree(cd); |
| 589 | rfree(dev); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* set component audio stream parameters */ |
Liam Girdwood | 2cfaebe | 2017-08-21 17:13:52 +0100 | [diff] [blame] | 593 | static int src_params(struct comp_dev *dev) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 594 | { |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 595 | struct sof_ipc_stream_params *params = &dev->params; |
| 596 | struct sof_ipc_comp_src *src = COMP_GET_IPC(dev, sof_ipc_comp_src); |
| 597 | struct sof_ipc_comp_config *config = COMP_GET_CONFIG(dev); |
| 598 | struct comp_data *cd = comp_get_drvdata(dev); |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 599 | struct comp_buffer *sink; |
| 600 | struct comp_buffer *source; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 601 | size_t delay_lines_size; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 602 | uint32_t source_rate; |
| 603 | uint32_t sink_rate; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 604 | int32_t *buffer_start; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 605 | int n = 0; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 606 | int err; |
| 607 | int frames_is_for_source; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 608 | int q; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 609 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 610 | trace_src("par"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 611 | |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 612 | /* SRC supports S24_4LE and S32_LE formats */ |
| 613 | switch (config->frame_fmt) { |
| 614 | case SOF_IPC_FRAME_S24_4LE: |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 615 | cd->polyphase_func = src_polyphase_stage_cir_s24; |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 616 | break; |
| 617 | case SOF_IPC_FRAME_S32_LE: |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 618 | cd->polyphase_func = src_polyphase_stage_cir; |
Seppo Ingalsuo | 887628a | 2017-09-14 16:00:05 +0300 | [diff] [blame] | 619 | break; |
| 620 | default: |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 621 | trace_src_error("sr0"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 622 | return -EINVAL; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 623 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 624 | |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 625 | /* Calculate source and sink rates, one rate will come from IPC new |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 626 | * and the other from params. |
| 627 | */ |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 628 | if (src->source_rate == 0) { |
| 629 | /* params rate is source rate */ |
| 630 | source_rate = params->rate; |
| 631 | sink_rate = src->sink_rate; |
| 632 | /* re-write our params with output rate for next component */ |
| 633 | params->rate = sink_rate; |
Seppo Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 634 | frames_is_for_source = 0; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 635 | } else { |
| 636 | /* params rate is sink rate */ |
| 637 | source_rate = src->source_rate; |
| 638 | sink_rate = params->rate; |
| 639 | /* re-write our params with output rate for next component */ |
| 640 | params->rate = source_rate; |
Seppo Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 641 | frames_is_for_source = 1; |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 642 | } |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 643 | |
| 644 | /* Allocate needed memory for delay lines */ |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 645 | err = src_buffer_lengths(&cd->param, source_rate, sink_rate, |
Seppo Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 646 | params->channels, dev->frames, frames_is_for_source); |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 647 | if (err < 0) { |
| 648 | trace_src_error("sr1"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 649 | trace_error_value(source_rate); |
| 650 | trace_error_value(sink_rate); |
| 651 | trace_error_value(params->channels); |
| 652 | trace_error_value(dev->frames); |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 653 | return err; |
| 654 | } |
| 655 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 656 | delay_lines_size = sizeof(int32_t) * cd->param.total; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 657 | if (delay_lines_size == 0) { |
| 658 | trace_src_error("sr2"); |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 662 | /* free any existing delay lines. TODO reuse if same size */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 663 | if (!cd->delay_lines) |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 664 | rfree(cd->delay_lines); |
| 665 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 666 | cd->delay_lines = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, |
| 667 | delay_lines_size); |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 668 | if (!cd->delay_lines) { |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 669 | trace_src_error("sr3"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 670 | trace_error_value(delay_lines_size); |
Liam Girdwood | 3488cce | 2017-08-10 11:59:08 +0100 | [diff] [blame] | 671 | return -EINVAL; |
Liam Girdwood | b12aa0d | 2017-09-03 22:00:58 +0100 | [diff] [blame] | 672 | } |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 673 | |
| 674 | /* Clear all delay lines here */ |
| 675 | memset(cd->delay_lines, 0, delay_lines_size); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 676 | buffer_start = cd->delay_lines + cd->param.sbuf_length; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 677 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 678 | /* Initialize SRC for actual sample rate */ |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 679 | n = src_polyphase_init(&cd->src, &cd->param, buffer_start); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 680 | |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 681 | /* Reset stage buffer */ |
| 682 | cd->sbuf_r_ptr = cd->delay_lines; |
| 683 | cd->sbuf_w_ptr = cd->delay_lines; |
| 684 | cd->sbuf_avail = 0; |
| 685 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 686 | switch (n) { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 687 | case 0: |
| 688 | cd->src_func = src_copy_s32_default; /* 1:1 fast copy */ |
| 689 | break; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 690 | case 1: |
| 691 | cd->src_func = src_1s_s32_default; /* Simpler 1 stage SRC */ |
| 692 | break; |
| 693 | case 2: |
| 694 | cd->src_func = src_2s_s32_default; /* Default 2 stage SRC */ |
| 695 | break; |
| 696 | default: |
| 697 | /* This is possibly due to missing coefficients for |
| 698 | * requested rates combination. Sink audio will be |
| 699 | * muted if copy() is run. |
| 700 | */ |
| 701 | trace_src("SFa"); |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 702 | cd->src_func = src_fallback; |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 703 | return -EINVAL; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 704 | } |
| 705 | |
Seppo Ingalsuo | 14d10a8 | 2017-09-22 16:08:47 +0300 | [diff] [blame] | 706 | /* Calculate period size based on config. First make sure that |
| 707 | * frame_bytes is set. |
| 708 | */ |
Seppo Ingalsuo | 28ef854 | 2017-08-25 18:29:04 +0300 | [diff] [blame] | 709 | dev->frame_bytes = |
| 710 | dev->params.sample_container_bytes * dev->params.channels; |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 711 | |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 712 | /* The downstream buffer must be at least length of blk_out plus |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 713 | * a dev->frames and an integer multiple of dev->frames. The |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 714 | * buffer_set_size will return an error if the required length would |
| 715 | * be too long. |
| 716 | */ |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 717 | q = src_ceil_divide(cd->param.blk_out, (int)dev->frames) + 1; |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 718 | |
| 719 | /* Configure downstream buffer */ |
| 720 | sink = list_first_item(&dev->bsink_list, struct comp_buffer, |
| 721 | source_list); |
| 722 | err = buffer_set_size(sink, q * dev->frames * dev->frame_bytes); |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 723 | if (err < 0) { |
| 724 | trace_src_error("eSz"); |
Ranjani Sridharan | 210989d | 2018-03-25 17:34:04 -0700 | [diff] [blame] | 725 | trace_error_value(sink->alloc_size); |
| 726 | trace_error_value(q * dev->frames * dev->frame_bytes); |
Liam Girdwood | 4450924 | 2017-09-07 15:34:42 +0100 | [diff] [blame] | 727 | return err; |
| 728 | } |
| 729 | |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 730 | /* Check that source buffer has sufficient size */ |
| 731 | source = list_first_item(&dev->bsource_list, struct comp_buffer, |
| 732 | sink_list); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 733 | if (source->size < cd->param.blk_in * dev->frame_bytes) { |
Seppo Ingalsuo | 5416229 | 2017-09-12 13:45:50 +0300 | [diff] [blame] | 734 | trace_src_error("eSy"); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 741 | 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] | 742 | { |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 743 | trace_src_error("ec1"); |
| 744 | return -EINVAL; |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* used to pass standard and bespoke commands (with data) to component */ |
| 748 | static int src_cmd(struct comp_dev *dev, int cmd, void *data) |
| 749 | { |
| 750 | struct sof_ipc_ctrl_data *cdata = data; |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 751 | int ret = 0; |
Liam Girdwood | 854b2e5 | 2017-09-02 23:14:02 +0100 | [diff] [blame] | 752 | |
Liam Girdwood | be41b68 | 2017-09-21 16:48:18 +0100 | [diff] [blame] | 753 | trace_src("cmd"); |
| 754 | |
Pierre-Louis Bossart | b513a45 | 2017-09-25 14:52:12 -0500 | [diff] [blame] | 755 | if (cmd == COMP_CMD_SET_VALUE) |
| 756 | ret = src_ctrl_cmd(dev, cdata); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 757 | |
Ranjani Sridharan | 6200408 | 2017-09-06 22:01:40 +0100 | [diff] [blame] | 758 | return ret; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 759 | } |
| 760 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 761 | static int src_trigger(struct comp_dev *dev, int cmd) |
| 762 | { |
| 763 | trace_src("trg"); |
| 764 | |
| 765 | return comp_set_state(dev, cmd); |
| 766 | } |
| 767 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 768 | /* copy and process stream data from source to sink buffers */ |
| 769 | static int src_copy(struct comp_dev *dev) |
| 770 | { |
| 771 | struct comp_data *cd = comp_get_drvdata(dev); |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 772 | struct comp_buffer *source; |
| 773 | struct comp_buffer *sink; |
| 774 | int need_source; |
| 775 | int need_sink; |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 776 | size_t consumed = 0; |
| 777 | size_t produced = 0; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 778 | |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 779 | trace_src("SRC"); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 780 | |
| 781 | /* src component needs 1 source and 1 sink buffer */ |
| 782 | source = list_first_item(&dev->bsource_list, struct comp_buffer, |
| 783 | sink_list); |
| 784 | sink = list_first_item(&dev->bsink_list, struct comp_buffer, |
| 785 | source_list); |
| 786 | |
Seppo Ingalsuo | cb3a64d | 2017-09-06 15:16:12 +0300 | [diff] [blame] | 787 | /* Calculate needed amount of source buffer and sink buffer |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 788 | * for one SRC run. The blk_in and blk are minimum condition to |
| 789 | * call copy. Copy can consume or produce a slightly larger block |
| 790 | * with the rates where block sizes are not constant. E.g. for |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 791 | * 1 ms scheduling the blocks can be under or above 1 ms when the |
| 792 | * SRC interval block size constraint prevents exact 1 ms blocks. |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 793 | */ |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 794 | need_source = cd->param.blk_in * dev->frame_bytes; |
| 795 | need_sink = cd->param.blk_out * dev->frame_bytes; |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 796 | |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 797 | /* make sure source component buffer has enough data available and that |
| 798 | * the sink component buffer has enough free bytes for copy. Also |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 799 | * check for XRUNs. |
| 800 | */ |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 801 | if (source->avail < need_source) { |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 802 | trace_src_error("xru"); |
| 803 | return -EIO; /* xrun */ |
| 804 | } |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 805 | if (sink->free < need_sink) { |
Liam Girdwood | ab50527 | 2017-11-13 16:55:30 +0000 | [diff] [blame] | 806 | trace_src_error("xro"); |
| 807 | return -EIO; /* xrun */ |
| 808 | } |
| 809 | |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 810 | cd->src_func(dev, source, sink, &consumed, &produced); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 811 | |
Seppo Ingalsuo | e8afa16 | 2018-03-08 16:22:17 +0200 | [diff] [blame] | 812 | tracev_value(consumed >> 3); |
| 813 | tracev_value(produced >> 3); |
| 814 | |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 815 | /* Calc new free and available if data was processed. These |
| 816 | * functions must not be called with 0 consumed/produced. |
| 817 | */ |
| 818 | if (consumed > 0) |
| 819 | comp_update_buffer_consume(source, consumed); |
Seppo Ingalsuo | 28fafc1 | 2017-10-05 18:57:37 +0300 | [diff] [blame] | 820 | |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 821 | if (produced > 0) { |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 822 | comp_update_buffer_produce(sink, produced); |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 823 | return cd->param.blk_out; |
| 824 | } |
Seppo Ingalsuo | 83b7560 | 2017-12-05 15:54:57 +0200 | [diff] [blame] | 825 | |
Liam Girdwood | e0f65a2 | 2017-12-08 20:34:29 +0000 | [diff] [blame] | 826 | /* produced no data */ |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static int src_prepare(struct comp_dev *dev) |
| 831 | { |
Liam Girdwood | b083e58 | 2017-10-12 16:05:44 +0100 | [diff] [blame] | 832 | trace_src("pre"); |
| 833 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 834 | return comp_set_state(dev, COMP_TRIGGER_PREPARE); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 835 | } |
| 836 | |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 837 | static int src_reset(struct comp_dev *dev) |
| 838 | { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 839 | struct comp_data *cd = comp_get_drvdata(dev); |
| 840 | |
| 841 | trace_src("SRe"); |
| 842 | |
| 843 | cd->src_func = src_2s_s32_default; |
Seppo Ingalsuo | 4b007cb | 2017-10-20 18:45:30 +0300 | [diff] [blame] | 844 | src_polyphase_reset(&cd->src); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 845 | |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 846 | comp_set_state(dev, COMP_TRIGGER_RESET); |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | struct comp_driver comp_src = { |
| 851 | .type = SOF_COMP_SRC, |
Seppo Ingalsuo | 4a00691 | 2017-06-28 19:25:51 +0300 | [diff] [blame] | 852 | .ops = { |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 853 | .new = src_new, |
| 854 | .free = src_free, |
| 855 | .params = src_params, |
| 856 | .cmd = src_cmd, |
Liam Girdwood | 7ec3a7c | 2018-03-28 18:05:37 -0700 | [diff] [blame] | 857 | .trigger = src_trigger, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 858 | .copy = src_copy, |
| 859 | .prepare = src_prepare, |
| 860 | .reset = src_reset, |
Seppo Ingalsuo | 6a27483 | 2017-06-07 14:17:55 +0300 | [diff] [blame] | 861 | }, |
| 862 | }; |
| 863 | |
| 864 | void sys_comp_src_init(void) |
| 865 | { |
| 866 | comp_register(&comp_src); |
| 867 | } |