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 #ifdef ENABLE_EXTENDED_COMPRESS_FORMAT 86 union snd_codec_options; 87 struct snd_compr_metadata; 88 #endif 89 /* 90 * compress_open: open a new compress stream 91 * returns the valid struct compress on success, NULL on failure 92 * If config does not specify a requested fragment size, on return 93 * it will be updated with the size and number of fragments that 94 * were configured 95 * 96 * @card: sound card number 97 * @device: device number 98 * @flags: device flags can be COMPRESS_OUT or COMPRESS_IN 99 * @config: stream config requested. Returns actual fragment config 100 */ 101 struct compress *compress_open(unsigned int card, unsigned int device, 102 unsigned int flags, struct compr_config *config); 103 104 /* 105 * compress_close: close the compress stream 106 * 107 * @compress: compress stream to be closed 108 */ 109 void compress_close(struct compress *compress); 110 111 /* 112 * compress_get_hpointer: get the hw timestamp 113 * return 0 on success, negative on error 114 * 115 * @compress: compress stream on which query is made 116 * @avail: buffer availble for write/read, in bytes 117 * @tstamp: hw time 118 */ 119 int compress_get_hpointer(struct compress *compress, 120 unsigned int *avail, struct timespec *tstamp); 121 122 123 /* 124 * compress_get_tstamp: get the raw hw timestamp 125 * return 0 on success, negative on error 126 * 127 * @compress: compress stream on which query is made 128 * @samples: number of decoded samples played 129 * @sampling_rate: sampling rate of decoded samples 130 */ 131 int compress_get_tstamp(struct compress *compress, 132 unsigned long *samples, unsigned int *sampling_rate); 133 134 /* 135 * compress_write: write data to the compress stream 136 * return bytes written on success, negative on error 137 * By default this is a blocking call and will not return 138 * until all bytes have been written or there was a 139 * write error. 140 * If non-blocking mode has been enabled with compress_nonblock(), 141 * this function will write all bytes that can be written without 142 * blocking and will then return the number of bytes successfully 143 * written. If the return value is not an error and is < size 144 * the caller can use compress_wait() to block until the driver 145 * is ready for more data. 146 * 147 * @compress: compress stream to be written to 148 * @buf: pointer to data 149 * @size: number of bytes to be written 150 */ 151 int compress_write(struct compress *compress, const void *buf, unsigned int size); 152 153 /* 154 * compress_read: read data from the compress stream 155 * return bytes read on success, negative on error 156 * By default this is a blocking call and will block until 157 * size bytes have been written or there was a read error. 158 * If non-blocking mode was enabled using compress_nonblock() 159 * the behaviour will change to read only as many bytes as 160 * are currently available (if no bytes are available it 161 * will return immediately). The caller can then use 162 * compress_wait() to block until more bytes are available. 163 * 164 * @compress: compress stream from where data is to be read 165 * @buf: pointer to data buffer 166 * @size: size of given buffer 167 */ 168 int compress_read(struct compress *compress, void *buf, unsigned int size); 169 170 /* 171 * compress_start: start the compress stream 172 * return 0 on success, negative on error 173 * 174 * @compress: compress stream to be started 175 */ 176 int compress_start(struct compress *compress); 177 178 /* 179 * compress_stop: stop the compress stream 180 * return 0 on success, negative on error 181 * 182 * @compress: compress stream to be stopped 183 */ 184 int compress_stop(struct compress *compress); 185 186 /* 187 * compress_pause: pause the compress stream 188 * return 0 on success, negative on error 189 * 190 * @compress: compress stream to be paused 191 */ 192 int compress_pause(struct compress *compress); 193 194 /* 195 * compress_resume: resume the compress stream 196 * return 0 on success, negative on error 197 * 198 * @compress: compress stream to be resumed 199 */ 200 int compress_resume(struct compress *compress); 201 202 /* 203 * compress_drain: drain the compress stream 204 * return 0 on success, negative on error 205 * 206 * @compress: compress stream to be drain 207 */ 208 int compress_drain(struct compress *compress); 209 210 /* 211 * compress_next_track: set the next track for stream 212 * 213 * return 0 on success, negative on error 214 * 215 * @compress: compress stream to be transistioned to next track 216 */ 217 int compress_next_track(struct compress *compress); 218 219 /* 220 * compress_partial_drain: drain will return after the last frame is decoded 221 * by DSP and will play the , All the data written into compressed 222 * ring buffer is decoded 223 * 224 * return 0 on success, negative on error 225 * 226 * @compress: compress stream to be drain 227 */ 228 int compress_partial_drain(struct compress *compress); 229 230 /* 231 * compress_set_gapless_metadata: set gapless metadata of a compress strem 232 * 233 * return 0 on success, negative on error 234 * 235 * @compress: compress stream for which metadata has to set 236 * @mdata: metadata encoder delay and padding 237 */ 238 239 int compress_set_gapless_metadata(struct compress *compress, 240 struct compr_gapless_mdata *mdata); 241 242 #ifdef ENABLE_EXTENDED_COMPRESS_FORMAT 243 /* 244 * compress_set_next_track_param: set params of next compress stream in gapless 245 * 246 * return 0 on success, negative on error 247 * 248 * @compress: compress stream for which codec options has to be set 249 * @codec_options: codec options of compress stream based on codec type 250 */ 251 252 int compress_set_next_track_param(struct compress *compress, 253 union snd_codec_options *codec_options); 254 #endif 255 256 /* 257 * is_codec_supported:check if the given codec is supported 258 * returns true when supported, false if not 259 * 260 * @card: sound card number 261 * @device: device number 262 * @flags: stream flags 263 * @codec: codec type and parameters to be checked 264 */ 265 bool is_codec_supported(unsigned int card, unsigned int device, 266 unsigned int flags, struct snd_codec *codec); 267 268 /* 269 * compress_set_max_poll_wait: set the maximum time tinycompress 270 * will wait for driver to signal a poll(). Interval is in 271 * milliseconds. 272 * Pass interval of -1 to disable timeout and make poll() wait 273 * until driver signals. 274 * If this function is not used the timeout defaults to 20 seconds. 275 */ 276 void compress_set_max_poll_wait(struct compress *compress, int milliseconds); 277 278 /* Enable or disable non-blocking mode for write and read */ 279 void compress_nonblock(struct compress *compress, int nonblock); 280 281 /* Wait for ring buffer to ready for next read or write */ 282 int compress_wait(struct compress *compress, int timeout_ms); 283 284 int is_compress_running(struct compress *compress); 285 286 int is_compress_ready(struct compress *compress); 287 288 /* Returns a human readable reason for the last error */ 289 const char *compress_get_error(struct compress *compress); 290 /* 291 * since the SNDRV_PCM_RATE_* is not availble anywhere in userspace 292 * and we have used these to define the sampling rate, we need to define 293 * then here 294 */ 295 #define SNDRV_PCM_RATE_5512 (1<<0) /* 5512Hz */ 296 #define SNDRV_PCM_RATE_8000 (1<<1) /* 8000Hz */ 297 #define SNDRV_PCM_RATE_11025 (1<<2) /* 11025Hz */ 298 #define SNDRV_PCM_RATE_16000 (1<<3) /* 16000Hz */ 299 #define SNDRV_PCM_RATE_22050 (1<<4) /* 22050Hz */ 300 #define SNDRV_PCM_RATE_32000 (1<<5) /* 32000Hz */ 301 #define SNDRV_PCM_RATE_44100 (1<<6) /* 44100Hz */ 302 #define SNDRV_PCM_RATE_48000 (1<<7) /* 48000Hz */ 303 #define SNDRV_PCM_RATE_64000 (1<<8) /* 64000Hz */ 304 #define SNDRV_PCM_RATE_88200 (1<<9) /* 88200Hz */ 305 #define SNDRV_PCM_RATE_96000 (1<<10) /* 96000Hz */ 306 #define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */ 307 #define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */ 308 309 /* utility functions */ 310 unsigned int compress_get_alsa_rate(unsigned int rate); 311 312 #ifdef ENABLE_EXTENDED_COMPRESS_FORMAT 313 /* set metadata */ 314 int compress_set_metadata(struct compress *compress, 315 struct snd_compr_metadata *mdata); 316 317 /* get metadata */ 318 int compress_get_metadata(struct compress *compress, 319 struct snd_compr_metadata *mdata); 320 #endif 321 322 #if defined(__cplusplus) 323 } 324 #endif 325 326 #endif 327