1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2017 Intel Corporation. All rights reserved.
4  *
5  * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6  */
7 
8 #ifndef __SOF_AUDIO_SRC_SRC_H__
9 #define __SOF_AUDIO_SRC_SRC_H__
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 struct src_param {
15 	int fir_s1;
16 	int fir_s2;
17 	int out_s1;
18 	int out_s2;
19 	int sbuf_length;
20 	int src_multich;
21 	int total;
22 	int blk_in;
23 	int blk_out;
24 	int stage1_times;
25 	int stage2_times;
26 	int idx_in;
27 	int idx_out;
28 	int nch;
29 };
30 
31 struct src_stage {
32 	const int idm;
33 	const int odm;
34 	const int num_of_subfilters;
35 	const int subfilter_length;
36 	const int filter_length;
37 	const int blk_in;
38 	const int blk_out;
39 	const int halfband;
40 	const int shift;
41 	const void *coefs; /* Can be int16_t or int32_t depending on config */
42 };
43 
44 struct src_state {
45 	int fir_delay_size;	/* samples */
46 	int out_delay_size;	/* samples */
47 	int32_t *fir_delay;
48 	int32_t *out_delay;
49 	int32_t *fir_wp;
50 	int32_t *out_rp;
51 };
52 
53 struct polyphase_src {
54 	int number_of_stages;
55 	struct src_stage *stage1;
56 	struct src_stage *stage2;
57 	struct src_state state1;
58 	struct src_state state2;
59 };
60 
61 struct src_stage_prm {
62 	int nch;
63 	int times;
64 	void *x_rptr;
65 	void *x_end_addr;
66 	size_t x_size;
67 	void *y_wptr;
68 	void *y_addr;
69 	void *y_end_addr;
70 	size_t y_size;
71 	int shift;
72 	struct src_state *state;
73 	struct src_stage *stage;
74 };
75 
src_inc_wrap(int32_t ** ptr,int32_t * end,size_t size)76 static inline void src_inc_wrap(int32_t **ptr, int32_t *end, size_t size)
77 {
78 	if (*ptr >= end)
79 		*ptr = (int32_t *)((uint8_t *)*ptr - size);
80 }
81 
src_dec_wrap(int32_t ** ptr,int32_t * addr,size_t size)82 static inline void src_dec_wrap(int32_t **ptr, int32_t *addr, size_t size)
83 {
84 	if (*ptr < addr)
85 		*ptr = (int32_t *)((uint8_t *)*ptr + size);
86 }
87 
88 #if CONFIG_FORMAT_S16LE
src_inc_wrap_s16(int16_t ** ptr,int16_t * end,size_t size)89 static inline void src_inc_wrap_s16(int16_t **ptr, int16_t *end, size_t size)
90 {
91 	if (*ptr >= end)
92 		*ptr = (int16_t *)((uint8_t *)*ptr - size);
93 }
94 
src_dec_wrap_s16(int16_t ** ptr,int16_t * addr,size_t size)95 static inline void src_dec_wrap_s16(int16_t **ptr, int16_t *addr, size_t size)
96 {
97 	if (*ptr < addr)
98 		*ptr = (int16_t *)((uint8_t *)*ptr + size);
99 }
100 #endif /* CONFIG_FORMAT_S16LE */
101 
102 void src_polyphase_reset(struct polyphase_src *src);
103 
104 int src_polyphase_init(struct polyphase_src *src, struct src_param *p,
105 		       int32_t *delay_lines_start);
106 
107 int src_polyphase(struct polyphase_src *src, int32_t x[], int32_t y[],
108 		  int n_in);
109 
110 #if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE
111 void src_polyphase_stage_cir(struct src_stage_prm *s);
112 #endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */
113 
114 #if CONFIG_FORMAT_S16LE
115 void src_polyphase_stage_cir_s16(struct src_stage_prm *s);
116 #endif /* CONFIG_FORMAT_S16LE */
117 
118 int src_buffer_lengths(struct src_param *a, int fs_in, int fs_out, int nch,
119 		       int source_frames);
120 
121 int32_t src_input_rates(void);
122 
123 int32_t src_output_rates(void);
124 
125 #endif /* __SOF_AUDIO_SRC_SRC_H__ */
126