1 /*
2  * BSD LICENSE
3  *
4  * Copyright (c) 2011-2012, Intel Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * Neither the name of Intel Corporation nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * LGPL LICENSE
32  *
33  * tinycompress library for compress audio offload in alsa
34  * Copyright (c) 2011-2012, Intel Corporation.
35  *
36  *
37  * This program is free software; you can redistribute it and/or modify it
38  * under the terms and conditions of the GNU Lesser General Public License,
39  * version 2.1, as published by the Free Software Foundation.
40  *
41  * This program is distributed in the hope it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
44  * License for more details.
45  *
46  * You should have received a copy of the GNU Lesser General Public License
47  * along with this program; if not, write to
48  * the Free Software Foundation, Inc.,
49  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
50  */
51 
52 
53 #ifndef __TINYCOMPRESS_H
54 #define __TINYCOMPRESS_H
55 
56 #if defined(__cplusplus)
57 extern "C" {
58 #endif
59 /*
60  * struct compr_config: config structure, needs to be filled by app
61  * If fragment_size or fragments are zero, this means "don't care"
62  * and tinycompress will choose values that the driver supports
63  *
64  * @fragment_size: size of fragment requested, in bytes
65  * @fragments: number of fragments
66  * @codec: codec type and parameters requested
67  */
68 struct compr_config {
69 	__u32 fragment_size;
70 	__u32 fragments;
71 	struct snd_codec *codec;
72 };
73 
74 struct compr_gapless_mdata {
75 	__u32 encoder_delay;
76 	__u32 encoder_padding;
77 };
78 
79 #define COMPRESS_OUT        0x20000000
80 #define COMPRESS_IN         0x10000000
81 
82 struct compress;
83 struct snd_compr_tstamp;
84 
85 /*
86  * compress_open: open a new compress stream
87  * returns the valid struct compress on success, NULL on failure
88  * If config does not specify a requested fragment size, on return
89  * it will be updated with the size and number of fragments that
90  * were configured
91  *
92  * @card: sound card number
93  * @device: device number
94  * @flags: device flags can be COMPRESS_OUT or COMPRESS_IN
95  * @config: stream config requested. Returns actual fragment config
96  */
97 struct compress *compress_open(unsigned int card, unsigned int device,
98 		unsigned int flags, struct compr_config *config);
99 
100 /*
101  * compress_close: close the compress stream
102  *
103  * @compress: compress stream to be closed
104  */
105 void compress_close(struct compress *compress);
106 
107 /*
108  * compress_get_hpointer: get the hw timestamp
109  * return 0 on success, negative on error
110  *
111  * @compress: compress stream on which query is made
112  * @avail: buffer availble for write/read, in bytes
113  * @tstamp: hw time
114  */
115 int compress_get_hpointer(struct compress *compress,
116 		unsigned int *avail, struct timespec *tstamp);
117 
118 
119 /*
120  * compress_get_tstamp: get the raw hw timestamp
121  * return 0 on success, negative on error
122  *
123  * @compress: compress stream on which query is made
124  * @samples: number of decoded samples played
125  * @sampling_rate: sampling rate of decoded samples
126  */
127 int compress_get_tstamp(struct compress *compress,
128 		unsigned long *samples, unsigned int *sampling_rate);
129 
130 /*
131  * compress_write: write data to the compress stream
132  * return bytes written on success, negative on error
133  * By default this is a blocking call and will not return
134  * until all bytes have been written or there was a
135  * write error.
136  * If non-blocking mode has been enabled with compress_nonblock(),
137  * this function will write all bytes that can be written without
138  * blocking and will then return the number of bytes successfully
139  * written. If the return value is not an error and is < size
140  * the caller can use compress_wait() to block until the driver
141  * is ready for more data.
142  *
143  * @compress: compress stream to be written to
144  * @buf: pointer to data
145  * @size: number of bytes to be written
146  */
147 int compress_write(struct compress *compress, const void *buf, unsigned int size);
148 
149 /*
150  * compress_read: read data from the compress stream
151  * return bytes read on success, negative on error
152  * By default this is a blocking call and will block until
153  * size bytes have been written or there was a read error.
154  * If non-blocking mode was enabled using compress_nonblock()
155  * the behaviour will change to read only as many bytes as
156  * are currently available (if no bytes are available it
157  * will return immediately). The caller can then use
158  * compress_wait() to block until more bytes are available.
159  *
160  * @compress: compress stream from where data is to be read
161  * @buf: pointer to data buffer
162  * @size: size of given buffer
163  */
164 int compress_read(struct compress *compress, void *buf, unsigned int size);
165 
166 /*
167  * compress_start: start the compress stream
168  * return 0 on success, negative on error
169  *
170  * @compress: compress stream to be started
171  */
172 int compress_start(struct compress *compress);
173 
174 /*
175  * compress_stop: stop the compress stream
176  * return 0 on success, negative on error
177  *
178  * @compress: compress stream to be stopped
179  */
180 int compress_stop(struct compress *compress);
181 
182 /*
183  * compress_pause: pause the compress stream
184  * return 0 on success, negative on error
185  *
186  * @compress: compress stream to be paused
187  */
188 int compress_pause(struct compress *compress);
189 
190 /*
191  * compress_resume: resume the compress stream
192  * return 0 on success, negative on error
193  *
194  * @compress: compress stream to be resumed
195  */
196 int compress_resume(struct compress *compress);
197 
198 /*
199  * compress_drain: drain the compress stream
200  * return 0 on success, negative on error
201  *
202  * @compress: compress stream to be drain
203  */
204 int compress_drain(struct compress *compress);
205 
206 /*
207  * compress_next_track: set the next track for stream
208  *
209  * return 0 on success, negative on error
210  *
211  * @compress: compress stream to be transistioned to next track
212  */
213 int compress_next_track(struct compress *compress);
214 
215 /*
216  * compress_partial_drain: drain will return after the last frame is decoded
217  * by DSP and will play the , All the data written into compressed
218  * ring buffer is decoded
219  *
220  * return 0 on success, negative on error
221  *
222  * @compress: compress stream to be drain
223  */
224 int compress_partial_drain(struct compress *compress);
225 
226 /*
227  * compress_set_gapless_metadata: set gapless metadata of a compress strem
228  *
229  * return 0 on success, negative on error
230  *
231  * @compress: compress stream for which metadata has to set
232  * @mdata: metadata encoder delay and  padding
233  */
234 
235 int compress_set_gapless_metadata(struct compress *compress,
236 			struct compr_gapless_mdata *mdata);
237 
238 /*
239  * is_codec_supported:check if the given codec is supported
240  * returns true when supported, false if not
241  *
242  * @card: sound card number
243  * @device: device number
244  * @flags: stream flags
245  * @codec: codec type and parameters to be checked
246  */
247 bool is_codec_supported(unsigned int card, unsigned int device,
248 	       unsigned int flags, struct snd_codec *codec);
249 
250 /*
251  * compress_set_max_poll_wait: set the maximum time tinycompress
252  * will wait for driver to signal a poll(). Interval is in
253  * milliseconds.
254  * Pass interval of -1 to disable timeout and make poll() wait
255  * until driver signals.
256  * If this function is not used the timeout defaults to 20 seconds.
257  */
258 void compress_set_max_poll_wait(struct compress *compress, int milliseconds);
259 
260 /* Enable or disable non-blocking mode for write and read */
261 void compress_nonblock(struct compress *compress, int nonblock);
262 
263 /* Wait for ring buffer to ready for next read or write */
264 int compress_wait(struct compress *compress, int timeout_ms);
265 
266 int is_compress_running(struct compress *compress);
267 
268 int is_compress_ready(struct compress *compress);
269 
270 /* Returns a human readable reason for the last error */
271 const char *compress_get_error(struct compress *compress);
272 /*
273  * since the SNDRV_PCM_RATE_* is not availble anywhere in userspace
274  * and we have used these to define the sampling rate, we need to define
275  * then here
276  */
277 #define SNDRV_PCM_RATE_5512		(1<<0)		/* 5512Hz */
278 #define SNDRV_PCM_RATE_8000		(1<<1)		/* 8000Hz */
279 #define SNDRV_PCM_RATE_11025		(1<<2)		/* 11025Hz */
280 #define SNDRV_PCM_RATE_16000		(1<<3)		/* 16000Hz */
281 #define SNDRV_PCM_RATE_22050		(1<<4)		/* 22050Hz */
282 #define SNDRV_PCM_RATE_32000		(1<<5)		/* 32000Hz */
283 #define SNDRV_PCM_RATE_44100		(1<<6)		/* 44100Hz */
284 #define SNDRV_PCM_RATE_48000		(1<<7)		/* 48000Hz */
285 #define SNDRV_PCM_RATE_64000		(1<<8)		/* 64000Hz */
286 #define SNDRV_PCM_RATE_88200		(1<<9)		/* 88200Hz */
287 #define SNDRV_PCM_RATE_96000		(1<<10)		/* 96000Hz */
288 #define SNDRV_PCM_RATE_176400		(1<<11)		/* 176400Hz */
289 #define SNDRV_PCM_RATE_192000		(1<<12)		/* 192000Hz */
290 
291 /* utility functions */
292 unsigned int compress_get_alsa_rate(unsigned int rate);
293 
294 
295 #if defined(__cplusplus)
296 }
297 #endif
298 
299 #endif
300