diff options
author | Baligh Uddin <baligh@google.com> | 2013-11-01 16:02:16 -0700 |
---|---|---|
committer | Baligh Uddin <baligh@google.com> | 2013-11-01 16:02:16 -0700 |
commit | 56d8daedc9f26ed2c3e6b4328364654621a4fbbb (patch) | |
tree | 6f64996e4d7484896e61651c8965ec5f52382644 | |
parent | 615b6252c82050b40ad4591b602a1363ea06545f (diff) | |
parent | a85e245a09c028d36cbf04f233be10bc583691f5 (diff) | |
download | android_external_tinycompress-56d8daedc9f26ed2c3e6b4328364654621a4fbbb.tar.gz android_external_tinycompress-56d8daedc9f26ed2c3e6b4328364654621a4fbbb.tar.bz2 android_external_tinycompress-56d8daedc9f26ed2c3e6b4328364654621a4fbbb.zip |
Merge remote-tracking branch 'origin/kitkat-dev'
-rw-r--r-- | Android.mk | 22 | ||||
-rw-r--r-- | MODULE_LICENSE_BSD | 0 | ||||
-rw-r--r-- | NOTICE | 262 | ||||
-rw-r--r-- | README | 30 | ||||
-rw-r--r-- | compress.c | 630 | ||||
-rw-r--r-- | cplay.c | 330 | ||||
-rw-r--r-- | include/tinycompress/tinycompress.h | 293 | ||||
-rw-r--r-- | include/tinycompress/tinymp3.h | 106 | ||||
-rw-r--r-- | include/tinycompress/version.h | 70 | ||||
-rw-r--r-- | utils.c | 96 |
10 files changed, 1839 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..104dfef --- /dev/null +++ b/Android.mk @@ -0,0 +1,22 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include +LOCAL_SRC_FILES:= compress.c utils.c +LOCAL_MODULE := libtinycompress +LOCAL_SHARED_LIBRARIES:= libcutils libutils +LOCAL_MODULE_TAGS := optional +LOCAL_PRELINK_MODULE := false + +include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include +LOCAL_SRC_FILES:= cplay.c +LOCAL_MODULE := cplay +LOCAL_SHARED_LIBRARIES:= libcutils libutils libtinycompress +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) + diff --git a/MODULE_LICENSE_BSD b/MODULE_LICENSE_BSD new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/MODULE_LICENSE_BSD @@ -0,0 +1,262 @@ +BSD LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul <vinod.koul@linux.intel.com> + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. +Neither the name of Intel Corporation nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +LGPL LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +tinyplay command line player for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul <vinod.koul@linux.intel.com> + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. +Neither the name of Intel Corporation nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +LGPL LICENSE + +tinyplay command line player for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +mp3 header and prasing +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul <vinod.koul@linux.intel.com> + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. +Neither the name of Intel Corporation nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +LGPL LICENSE + +mp3 header and parsing +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. +Neither the name of Intel Corporation nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +LGPL LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +tinycompress utility functions +Copyright (c) 2011-2013, Intel Corporation +All rights reserved. + +Author: Vinod Koul <vinod.koul@intel.com> + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. +Neither the name of Intel Corporation nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +LGPL LICENSE + +tinycompress utility functions +Copyright (c) 2011-2013, Intel Corporation + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. @@ -0,0 +1,30 @@ + README for tinycompress + ======================= + vinod.koul@linux.intel.com + ========================== + +1. WHAT + tinycompress is a userspace library for anyone who wants to use the ALSA +compressed APIs introduced in Linux 3.3 +This library provides the APIs to open a ALSA compressed device and read/write +compressed data like MP3 etc to it. + + This also includes a utility command line player (cplay) which demonstrates +the usage of this API. Currently this contains support for playing the mp3 format + +2. WHERE + The library can found in alsa-project.org +Git: git clone git://git.alsa-project.org/tinycompress.git +Http: http://git.alsa-project.org/?p=tinycompress.git + +3. PATCHES + Please send any enhancements/fixes to alsa developer mailing list at: +alsa-devel@alsa-project.org. + +4. LICENSE + tinycompress is provided under LGPL and BSD dual license + +5. CREDITS +- Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> for library design +- Navjot Singh <navjot.singh@intel.com> for writing the mp3 parser code + diff --git a/compress.c b/compress.c new file mode 100644 index 0000000..5cd0966 --- /dev/null +++ b/compress.c @@ -0,0 +1,630 @@ +/* + * BSD LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul <vinod.koul@linux.intel.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <stdarg.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <poll.h> +#include <stdbool.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <limits.h> + +#include <linux/types.h> +#include <linux/ioctl.h> +#define __force +#define __bitwise +#define __user +#include <sound/asound.h> +#include "sound/compress_params.h" +#include "sound/compress_offload.h" +#include "tinycompress/tinycompress.h" + +#define COMPR_ERR_MAX 128 + +/* Default maximum time we will wait in a poll() - 20 seconds */ +#define DEFAULT_MAX_POLL_WAIT_MS 20000 + +struct compress { + int fd; + unsigned int flags; + char error[COMPR_ERR_MAX]; + struct compr_config *config; + int running; + int max_poll_wait_ms; + int nonblocking; + unsigned int gapless_metadata; + unsigned int next_track; +}; + +static int oops(struct compress *compress, int e, const char *fmt, ...) +{ + va_list ap; + int sz; + + va_start(ap, fmt); + vsnprintf(compress->error, COMPR_ERR_MAX, fmt, ap); + va_end(ap); + sz = strlen(compress->error); + + snprintf(compress->error + sz, COMPR_ERR_MAX - sz, + ": %s", strerror(e)); + errno = e; + + return -1; +} + +const char *compress_get_error(struct compress *compress) +{ + return compress->error; +} +static struct compress bad_compress = { + .fd = -1, +}; + +int is_compress_running(struct compress *compress) +{ + return ((compress->fd > 0) && compress->running) ? 1 : 0; +} + +int is_compress_ready(struct compress *compress) +{ + return (compress->fd > 0) ? 1 : 0; +} + +static int get_compress_version(struct compress *compress) +{ + int version = 0; + + if (ioctl(compress->fd, SNDRV_COMPRESS_IOCTL_VERSION, &version)) { + oops(compress, errno, "cant read version"); + return -1; + } + return version; +} + +static bool _is_codec_supported(struct compress *compress, struct compr_config *config, + const struct snd_compr_caps *caps) +{ + bool codec = false; + unsigned int i; + + for (i = 0; i < caps->num_codecs; i++) { + if (caps->codecs[i] == config->codec->id) { + /* found the codec */ + codec = true; + break; + } + } + if (codec == false) { + oops(compress, ENXIO, "this codec is not supported"); + return false; + } + + if (config->fragment_size < caps->min_fragment_size) { + oops(compress, EINVAL, "requested fragment size %d is below min supported %d", + config->fragment_size, caps->min_fragment_size); + return false; + } + if (config->fragment_size > caps->max_fragment_size) { + oops(compress, EINVAL, "requested fragment size %d is above max supported %d", + config->fragment_size, caps->max_fragment_size); + return false; + } + if (config->fragments < caps->min_fragments) { + oops(compress, EINVAL, "requested fragments %d are below min supported %d", + config->fragments, caps->min_fragments); + return false; + } + if (config->fragments > caps->max_fragments) { + oops(compress, EINVAL, "requested fragments %d are above max supported %d", + config->fragments, caps->max_fragments); + return false; + } + + /* TODO: match the codec properties */ + return true; +} + +static bool _is_codec_type_supported(int fd, struct snd_codec *codec) +{ + struct snd_compr_caps caps; + bool found = false; + unsigned int i; + + if (ioctl(fd, SNDRV_COMPRESS_GET_CAPS, &caps)) { + oops(&bad_compress, errno, "cannot get device caps"); + return false; + } + + for (i = 0; i < caps.num_codecs; i++) { + if (caps.codecs[i] == codec->id) { + /* found the codec */ + found = true; + break; + } + } + /* TODO: match the codec properties */ + return found; +} + +static inline void +fill_compress_params(struct compr_config *config, struct snd_compr_params *params) +{ + params->buffer.fragment_size = config->fragment_size; + params->buffer.fragments = config->fragments; + memcpy(¶ms->codec, config->codec, sizeof(params->codec)); +} + +struct compress *compress_open(unsigned int card, unsigned int device, + unsigned int flags, struct compr_config *config) +{ + struct compress *compress; + struct snd_compr_params params; + struct snd_compr_caps caps; + char fn[256]; + + if (!config) { + oops(&bad_compress, EINVAL, "passed bad config"); + return &bad_compress; + } + + compress = calloc(1, sizeof(struct compress)); + if (!compress) { + oops(&bad_compress, errno, "cannot allocate compress object"); + return &bad_compress; + } + + compress->next_track = 0; + compress->gapless_metadata = 0; + compress->config = calloc(1, sizeof(*config)); + if (!compress->config) + goto input_fail; + + snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); + + compress->max_poll_wait_ms = DEFAULT_MAX_POLL_WAIT_MS; + + compress->flags = flags; + if (!((flags & COMPRESS_OUT) || (flags & COMPRESS_IN))) { + oops(&bad_compress, EINVAL, "can't deduce device direction from given flags"); + goto config_fail; + } + + if (flags & COMPRESS_OUT) { + compress->fd = open(fn, O_RDONLY); + } else { + compress->fd = open(fn, O_WRONLY); + } + if (compress->fd < 0) { + oops(&bad_compress, errno, "cannot open device '%s'", fn); + goto config_fail; + } + + if (ioctl(compress->fd, SNDRV_COMPRESS_GET_CAPS, &caps)) { + oops(compress, errno, "cannot get device caps"); + goto codec_fail; + } + + /* If caller passed "don't care" fill in default values */ + if ((config->fragment_size == 0) || (config->fragments == 0)) { + config->fragment_size = caps.min_fragment_size; + config->fragments = caps.max_fragments; + } + +#if 0 + /* FIXME need to turn this On when DSP supports + * and treat in no support case + */ + if (_is_codec_supported(compress, config, &caps) == false) { + oops(compress, errno, "codec not supported\n"); + goto codec_fail; + } +#endif + + memcpy(compress->config, config, sizeof(*compress->config)); + fill_compress_params(config, ¶ms); + + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) { + oops(&bad_compress, errno, "cannot set device"); + goto codec_fail; + } + + return compress; + +codec_fail: + close(compress->fd); + compress->fd = -1; +config_fail: + free(compress->config); +input_fail: + free(compress); + return &bad_compress; +} + +void compress_close(struct compress *compress) +{ + if (compress == &bad_compress) + return; + + if (compress->fd >= 0) + close(compress->fd); + compress->running = 0; + compress->fd = -1; + free(compress->config); + free(compress); +} + +int compress_get_hpointer(struct compress *compress, + unsigned int *avail, struct timespec *tstamp) +{ + struct snd_compr_avail kavail; + __u64 time; + + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail)) + return oops(compress, errno, "cannot get avail"); + if (0 == kavail.tstamp.sampling_rate) + return oops(compress, ENODATA, "sample rate unknown"); + *avail = (unsigned int)kavail.avail; + time = kavail.tstamp.pcm_io_frames / kavail.tstamp.sampling_rate; + tstamp->tv_sec = time; + time = kavail.tstamp.pcm_io_frames % kavail.tstamp.sampling_rate; + tstamp->tv_nsec = time * 1000000000 / kavail.tstamp.sampling_rate; + return 0; +} + +int compress_get_tstamp(struct compress *compress, + unsigned long *samples, unsigned int *sampling_rate) +{ + struct snd_compr_tstamp ktstamp; + + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + + if (ioctl(compress->fd, SNDRV_COMPRESS_TSTAMP, &ktstamp)) + return oops(compress, errno, "cannot get tstamp"); + + *samples = ktstamp.pcm_io_frames; + *sampling_rate = ktstamp.sampling_rate; + return 0; +} + +int compress_write(struct compress *compress, const void *buf, unsigned int size) +{ + struct snd_compr_avail avail; + struct pollfd fds; + int to_write = 0; /* zero indicates we haven't written yet */ + int written, total = 0, ret; + const char* cbuf = buf; + const unsigned int frag_size = compress->config->fragment_size; + + if (!(compress->flags & COMPRESS_IN)) + return oops(compress, EINVAL, "Invalid flag set"); + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + fds.fd = compress->fd; + fds.events = POLLOUT; + + /*TODO: treat auto start here first */ + while (size) { + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); + + /* We can write if we have at least one fragment available + * or there is enough space for all remaining data + */ + if ((avail.avail < frag_size) && (avail.avail < size)) { + + if (compress->nonblocking) + return total; + + ret = poll(&fds, 1, compress->max_poll_wait_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } + /* A pause will cause -EBADFD or zero. + * This is not an error, just stop writing */ + if ((ret == 0) || (ret == -EBADFD)) + break; + if (ret < 0) + return oops(compress, errno, "poll error"); + if (fds.revents & POLLOUT) { + continue; + } + } + /* write avail bytes */ + if (size > avail.avail) + to_write = avail.avail; + else + to_write = size; + written = write(compress->fd, cbuf, to_write); + /* If play was paused the write returns -EBADFD */ + if (written == -EBADFD) + break; + if (written < 0) + return oops(compress, errno, "write failed!"); + + size -= written; + cbuf += written; + total += written; + } + return total; +} + +int compress_read(struct compress *compress, void *buf, unsigned int size) +{ + struct snd_compr_avail avail; + struct pollfd fds; + int to_read = 0; + int num_read, total = 0, ret; + char* cbuf = buf; + const unsigned int frag_size = compress->config->fragment_size; + + if (!(compress->flags & COMPRESS_OUT)) + return oops(compress, EINVAL, "Invalid flag set"); + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + fds.fd = compress->fd; + fds.events = POLLIN; + + while (size) { + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); + + if ( (avail.avail < frag_size) && (avail.avail < size) ) { + /* Less than one fragment available and not at the + * end of the read, so poll + */ + if (compress->nonblocking) + return total; + + ret = poll(&fds, 1, compress->max_poll_wait_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } + /* A pause will cause -EBADFD or zero. + * This is not an error, just stop reading */ + if ((ret == 0) || (ret == -EBADFD)) + break; + if (ret < 0) + return oops(compress, errno, "poll error"); + if (fds.revents & POLLIN) { + continue; + } + } + /* read avail bytes */ + if (size > avail.avail) + to_read = avail.avail; + else + to_read = size; + num_read = read(compress->fd, cbuf, to_read); + /* If play was paused the read returns -EBADFD */ + if (num_read == -EBADFD) + break; + if (num_read < 0) + return oops(compress, errno, "read failed!"); + + size -= num_read; + cbuf += num_read; + total += num_read; + } + + return total; +} + +int compress_start(struct compress *compress) +{ + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_START)) + return oops(compress, errno, "cannot start the stream"); + compress->running = 1; + return 0; + +} + +int compress_stop(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_STOP)) + return oops(compress, errno, "cannot stop the stream"); + return 0; +} + +int compress_pause(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_PAUSE)) + return oops(compress, errno, "cannot pause the stream"); + return 0; +} + +int compress_resume(struct compress *compress) +{ + if (ioctl(compress->fd, SNDRV_COMPRESS_RESUME)) + return oops(compress, errno, "cannot resume the stream"); + return 0; +} + +int compress_drain(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_DRAIN)) + return oops(compress, errno, "cannot drain the stream"); + return 0; +} + +int compress_partial_drain(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, ENODEV, "device not ready"); + + if (!compress->next_track) + return oops(compress, EPERM, "next track not signalled"); + if (ioctl(compress->fd, SNDRV_COMPRESS_PARTIAL_DRAIN)) + return oops(compress, errno, "cannot drain the stream\n"); + compress->next_track = 0; + return 0; +} + +int compress_next_track(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, ENODEV, "device not ready"); + + if (!compress->gapless_metadata) + return oops(compress, EPERM, "metadata not set"); + if (ioctl(compress->fd, SNDRV_COMPRESS_NEXT_TRACK)) + return oops(compress, errno, "cannot set next track\n"); + compress->next_track = 1; + compress->gapless_metadata = 0; + return 0; +} + +int compress_set_gapless_metadata(struct compress *compress, + struct compr_gapless_mdata *mdata) +{ + struct snd_compr_metadata metadata; + int version; + + if (!is_compress_ready(compress)) + return oops(compress, ENODEV, "device not ready"); + + version = get_compress_version(compress); + if (version <= 0) + return -1; + + if (version < SNDRV_PROTOCOL_VERSION(0, 1, 1)) + return oops(compress, ENXIO, "gapless apis not supported in kernel"); + + metadata.key = SNDRV_COMPRESS_ENCODER_PADDING; + metadata.value[0] = mdata->encoder_padding; + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata)) + return oops(compress, errno, "can't set metadata for stream\n"); + + metadata.key = SNDRV_COMPRESS_ENCODER_DELAY; + metadata.value[0] = mdata->encoder_delay; + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata)) + return oops(compress, errno, "can't set metadata for stream\n"); + compress->gapless_metadata = 1; + return 0; +} + +bool is_codec_supported(unsigned int card, unsigned int device, + unsigned int flags, struct snd_codec *codec) +{ + unsigned int dev_flag; + bool ret; + int fd; + char fn[256]; + + snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); + + if (flags & COMPRESS_OUT) + dev_flag = O_RDONLY; + else + dev_flag = O_WRONLY; + + fd = open(fn, dev_flag); + if (fd < 0) + return oops(&bad_compress, errno, "cannot open device '%s'", fn); + + ret = _is_codec_type_supported(fd, codec); + + close(fd); + return ret; +} + +void compress_set_max_poll_wait(struct compress *compress, int milliseconds) +{ + compress->max_poll_wait_ms = milliseconds; +} + +void compress_nonblock(struct compress *compress, int nonblock) +{ + compress->nonblocking = !!nonblock; +} + +int compress_wait(struct compress *compress, int timeout_ms) +{ + struct pollfd fds; + int ret; + + fds.fd = compress->fd; + fds.events = POLLOUT | POLLIN; + + ret = poll(&fds, 1, timeout_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } + /* A pause will cause -EBADFD or zero. */ + if ((ret < 0) && (ret != -EBADFD)) + return oops(compress, errno, "poll error"); + if (fds.revents & (POLLOUT | POLLIN)) { + return 0; + } + return ret; +} + @@ -0,0 +1,330 @@ +/* + * BSD LICENSE + * + * tinyplay command line player for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul <vinod.koul@linux.intel.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * tinyplay command line player for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdint.h> +#include <linux/types.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <signal.h> +#include <stdbool.h> +#include <getopt.h> +#include <sys/time.h> +#define __force +#define __bitwise +#define __user +#include "sound/compress_params.h" +#include "tinycompress/tinycompress.h" +#include "tinycompress/tinymp3.h" + +static int verbose; + +static void usage(void) +{ + fprintf(stderr, "usage: cplay [OPTIONS] filename\n" + "-c\tcard number\n" + "-d\tdevice node\n" + "-b\tbuffer size\n" + "-f\tfragments\n\n" + "-v\tverbose mode\n" + "-h\tPrints this help list\n\n" + "Example:\n" + "\tcplay -c 1 -d 2 test.mp3\n" + "\tcplay -f 5 test.mp3\n"); + + exit(EXIT_FAILURE); +} + +void play_samples(char *name, unsigned int card, unsigned int device, + unsigned long buffer_size, unsigned int frag); + +struct mp3_header { + uint16_t sync; + uint8_t format1; + uint8_t format2; +}; + +int parse_mp3_header(struct mp3_header *header, unsigned int *num_channels, + unsigned int *sample_rate, unsigned int *bit_rate) +{ + int ver_idx, mp3_version, layer, bit_rate_idx, sample_rate_idx, channel_idx; + + /* check sync bits */ + if ((header->sync & MP3_SYNC) != MP3_SYNC) { + fprintf(stderr, "Error: Can't find sync word\n"); + return -1; + } + ver_idx = (header->sync >> 11) & 0x03; + mp3_version = ver_idx == 0 ? MPEG25 : ((ver_idx & 0x1) ? MPEG1 : MPEG2); + layer = 4 - ((header->sync >> 9) & 0x03); + bit_rate_idx = ((header->format1 >> 4) & 0x0f); + sample_rate_idx = ((header->format1 >> 2) & 0x03); + channel_idx = ((header->format2 >> 6) & 0x03); + + if (sample_rate_idx == 3 || layer == 4 || bit_rate_idx == 15) { + fprintf(stderr, "Error: Can't find valid header\n"); + return -1; + } + *num_channels = (channel_idx == MONO ? 1 : 2); + *sample_rate = mp3_sample_rates[mp3_version][sample_rate_idx]; + *bit_rate = (mp3_bit_rates[mp3_version][layer - 1][bit_rate_idx]) * 1000; + if (verbose) + printf("%s: exit\n", __func__); + return 0; +} + +int check_codec_format_supported(unsigned int card, unsigned int device, struct snd_codec *codec) +{ + if (is_codec_supported(card, device, COMPRESS_IN, codec) == false) { + fprintf(stderr, "Error: This codec or format is not supported by DSP\n"); + return -1; + } + return 0; +} + +static int print_time(struct compress *compress) +{ + unsigned int avail; + struct timespec tstamp; + + if (compress_get_hpointer(compress, &avail, &tstamp) != 0) { + fprintf(stderr, "Error querying timestamp\n"); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + return -1; + } else + fprintf(stderr, "DSP played %jd.%jd\n", (intmax_t)tstamp.tv_sec, (intmax_t)tstamp.tv_nsec*1000); + return 0; +} + +int main(int argc, char **argv) +{ + char *file; + unsigned long buffer_size = 0; + int c; + unsigned int card = 0, device = 0, frag = 0; + + + if (argc < 2) + usage(); + + verbose = 0; + while ((c = getopt(argc, argv, "hvb:f:c:d:")) != -1) { + switch (c) { + case 'h': + usage(); + break; + case 'b': + buffer_size = strtol(optarg, NULL, 0); + break; + case 'f': + frag = strtol(optarg, NULL, 10); + break; + case 'c': + card = strtol(optarg, NULL, 10); + break; + case 'd': + device = strtol(optarg, NULL, 10); + break; + case 'v': + verbose = 1; + break; + default: + exit(EXIT_FAILURE); + } + } + if (optind >= argc) + usage(); + + file = argv[optind]; + + play_samples(file, card, device, buffer_size, frag); + + fprintf(stderr, "Finish Playing.... Close Normally\n"); + exit(EXIT_SUCCESS); +} + +void play_samples(char *name, unsigned int card, unsigned int device, + unsigned long buffer_size, unsigned int frag) +{ + struct compr_config config; + struct snd_codec codec; + struct compress *compress; + struct mp3_header header; + FILE *file; + char *buffer; + int size, num_read, wrote; + unsigned int channels, rate, bits; + + if (verbose) + printf("%s: entry\n", __func__); + file = fopen(name, "rb"); + if (!file) { + fprintf(stderr, "Unable to open file '%s'\n", name); + exit(EXIT_FAILURE); + } + + fread(&header, sizeof(header), 1, file); + + if (parse_mp3_header(&header, &channels, &rate, &bits) == -1) { + fclose(file); + exit(EXIT_FAILURE); + } + + codec.id = SND_AUDIOCODEC_MP3; + codec.ch_in = channels; + codec.ch_out = channels; + codec.sample_rate = compress_get_alsa_rate(rate); + if (!codec.sample_rate) { + fprintf(stderr, "invalid sample rate %d\n", rate); + fclose(file); + exit(EXIT_FAILURE); + } + codec.bit_rate = bits; + codec.rate_control = 0; + codec.profile = 0; + codec.level = 0; + codec.ch_mode = 0; + codec.format = 0; + if ((buffer_size != 0) && (frag != 0)) { + config.fragment_size = buffer_size/frag; + config.fragments = frag; + } else { + /* use driver defaults */ + config.fragment_size = 0; + config.fragments = 0; + } + config.codec = &codec; + + compress = compress_open(card, device, COMPRESS_IN, &config); + if (!compress || !is_compress_ready(compress)) { + fprintf(stderr, "Unable to open Compress device %d:%d\n", + card, device); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto FILE_EXIT; + }; + if (verbose) + printf("%s: Opened compress device\n", __func__); + size = config.fragment_size; + buffer = malloc(size * config.fragments); + if (!buffer) { + fprintf(stderr, "Unable to allocate %d bytes\n", size); + goto COMP_EXIT; + } + + /* we will write frag fragment_size and then start */ + num_read = fread(buffer, 1, size * config.fragments, file); + if (num_read > 0) { + if (verbose) + printf("%s: Doing first buffer write of %d\n", __func__, num_read); + wrote = compress_write(compress, buffer, num_read); + if (wrote < 0) { + fprintf(stderr, "Error %d playing sample\n", wrote); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto BUF_EXIT; + } + if (wrote != num_read) { + /* TODO: Buufer pointer needs to be set here */ + fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); + } + } + printf("Playing file %s On Card %u device %u, with buffer of %lu bytes\n", + name, card, device, buffer_size); + printf("Format %u Channels %u, %u Hz, Bit Rate %d\n", + SND_AUDIOCODEC_MP3, channels, rate, bits); + + compress_start(compress); + if (verbose) + printf("%s: You should hear audio NOW!!!\n", __func__); + + do { + num_read = fread(buffer, 1, size, file); + if (num_read > 0) { + wrote = compress_write(compress, buffer, num_read); + if (wrote < 0) { + fprintf(stderr, "Error playing sample\n"); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto BUF_EXIT; + } + if (wrote != num_read) { + /* TODO: Buffer pointer needs to be set here */ + fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); + } + if (verbose) { + print_time(compress); + printf("%s: wrote %d\n", __func__, wrote); + } + } + } while (num_read > 0); + + if (verbose) + printf("%s: exit success\n", __func__); + /* issue drain if it supports */ + compress_drain(compress); + free(buffer); + fclose(file); + compress_close(compress); + return; +BUF_EXIT: + free(buffer); +COMP_EXIT: + compress_close(compress); +FILE_EXIT: + fclose(file); + if (verbose) + printf("%s: exit failure\n", __func__); + exit(EXIT_FAILURE); +} + diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h new file mode 100644 index 0000000..40de69a --- /dev/null +++ b/include/tinycompress/tinycompress.h @@ -0,0 +1,293 @@ +/* + * BSD LICENSE + * + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __TINYCOMPRESS_H +#define __TINYCOMPRESS_H + +#if defined(__cplusplus) +extern "C" { +#endif +/* + * struct compr_config: config structure, needs to be filled by app + * If fragment_size or fragments are zero, this means "don't care" + * and tinycompress will choose values that the driver supports + * + * @fragment_size: size of fragment requested, in bytes + * @fragments: number of fragments + * @codec: codec type and parameters requested + */ +struct compr_config { + __u32 fragment_size; + __u32 fragments; + struct snd_codec *codec; +}; + +struct compr_gapless_mdata { + __u32 encoder_delay; + __u32 encoder_padding; +}; + +#define COMPRESS_OUT 0x20000000 +#define COMPRESS_IN 0x10000000 + +struct compress; +struct snd_compr_tstamp; + +/* + * compress_open: open a new compress stream + * returns the valid struct compress on success, NULL on failure + * If config does not specify a requested fragment size, on return + * it will be updated with the size and number of fragments that + * were configured + * + * @card: sound card number + * @device: device number + * @flags: device flags can be COMPRESS_OUT or COMPRESS_IN + * @config: stream config requested. Returns actual fragment config + */ +struct compress *compress_open(unsigned int card, unsigned int device, + unsigned int flags, struct compr_config *config); + +/* + * compress_close: close the compress stream + * + * @compress: compress stream to be closed + */ +void compress_close(struct compress *compress); + +/* + * compress_get_hpointer: get the hw timestamp + * return 0 on success, negative on error + * + * @compress: compress stream on which query is made + * @avail: buffer availble for write/read, in bytes + * @tstamp: hw time + */ +int compress_get_hpointer(struct compress *compress, + unsigned int *avail, struct timespec *tstamp); + + +/* + * compress_get_tstamp: get the raw hw timestamp + * return 0 on success, negative on error + * + * @compress: compress stream on which query is made + * @samples: number of decoded samples played + * @sampling_rate: sampling rate of decoded samples + */ +int compress_get_tstamp(struct compress *compress, + unsigned long *samples, unsigned int *sampling_rate); + +/* + * compress_write: write data to the compress stream + * return bytes written on success, negative on error + * By default this is a blocking call and will not return + * until all bytes have been written or there was a + * write error. + * If non-blocking mode has been enabled with compress_nonblock(), + * this function will write all bytes that can be written without + * blocking and will then return the number of bytes successfully + * written. If the return value is not an error and is < size + * the caller can use compress_wait() to block until the driver + * is ready for more data. + * + * @compress: compress stream to be written to + * @buf: pointer to data + * @size: number of bytes to be written + */ +int compress_write(struct compress *compress, const void *buf, unsigned int size); + +/* + * compress_read: read data from the compress stream + * return bytes read on success, negative on error + * By default this is a blocking call and will block until + * size bytes have been written or there was a read error. + * If non-blocking mode was enabled using compress_nonblock() + * the behaviour will change to read only as many bytes as + * are currently available (if no bytes are available it + * will return immediately). The caller can then use + * compress_wait() to block until more bytes are available. + * + * @compress: compress stream from where data is to be read + * @buf: pointer to data buffer + * @size: size of given buffer + */ +int compress_read(struct compress *compress, void *buf, unsigned int size); + +/* + * compress_start: start the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be started + */ +int compress_start(struct compress *compress); + +/* + * compress_stop: stop the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be stopped + */ +int compress_stop(struct compress *compress); + +/* + * compress_pause: pause the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be paused + */ +int compress_pause(struct compress *compress); + +/* + * compress_resume: resume the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be resumed + */ +int compress_resume(struct compress *compress); + +/* + * compress_drain: drain the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be drain + */ +int compress_drain(struct compress *compress); + +/* + * compress_next_track: set the next track for stream + * + * return 0 on success, negative on error + * + * @compress: compress stream to be transistioned to next track + */ +int compress_next_track(struct compress *compress); + +/* + * compress_partial_drain: drain will return after the last frame is decoded + * by DSP and will play the , All the data written into compressed + * ring buffer is decoded + * + * return 0 on success, negative on error + * + * @compress: compress stream to be drain + */ +int compress_partial_drain(struct compress *compress); + +/* + * compress_set_gapless_metadata: set gapless metadata of a compress strem + * + * return 0 on success, negative on error + * + * @compress: compress stream for which metadata has to set + * @mdata: metadata encoder delay and padding + */ + +int compress_set_gapless_metadata(struct compress *compress, + struct compr_gapless_mdata *mdata); + +/* + * is_codec_supported:check if the given codec is supported + * returns true when supported, false if not + * + * @card: sound card number + * @device: device number + * @flags: stream flags + * @codec: codec type and parameters to be checked + */ +bool is_codec_supported(unsigned int card, unsigned int device, + unsigned int flags, struct snd_codec *codec); + +/* + * compress_set_max_poll_wait: set the maximum time tinycompress + * will wait for driver to signal a poll(). Interval is in + * milliseconds. + * Pass interval of -1 to disable timeout and make poll() wait + * until driver signals. + * If this function is not used the timeout defaults to 20 seconds. + */ +void compress_set_max_poll_wait(struct compress *compress, int milliseconds); + +/* Enable or disable non-blocking mode for write and read */ +void compress_nonblock(struct compress *compress, int nonblock); + +/* Wait for ring buffer to ready for next read or write */ +int compress_wait(struct compress *compress, int timeout_ms); + +int is_compress_running(struct compress *compress); + +int is_compress_ready(struct compress *compress); + +/* Returns a human readable reason for the last error */ +const char *compress_get_error(struct compress *compress); +/* + * since the SNDRV_PCM_RATE_* is not availble anywhere in userspace + * and we have used these to define the sampling rate, we need to define + * then here + */ +#define SNDRV_PCM_RATE_5512 (1<<0) /* 5512Hz */ +#define SNDRV_PCM_RATE_8000 (1<<1) /* 8000Hz */ +#define SNDRV_PCM_RATE_11025 (1<<2) /* 11025Hz */ +#define SNDRV_PCM_RATE_16000 (1<<3) /* 16000Hz */ +#define SNDRV_PCM_RATE_22050 (1<<4) /* 22050Hz */ +#define SNDRV_PCM_RATE_32000 (1<<5) /* 32000Hz */ +#define SNDRV_PCM_RATE_44100 (1<<6) /* 44100Hz */ +#define SNDRV_PCM_RATE_48000 (1<<7) /* 48000Hz */ +#define SNDRV_PCM_RATE_64000 (1<<8) /* 64000Hz */ +#define SNDRV_PCM_RATE_88200 (1<<9) /* 88200Hz */ +#define SNDRV_PCM_RATE_96000 (1<<10) /* 96000Hz */ +#define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */ +#define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */ + +/* utility functions */ +unsigned int compress_get_alsa_rate(unsigned int rate); +#endif diff --git a/include/tinycompress/tinymp3.h b/include/tinycompress/tinymp3.h new file mode 100644 index 0000000..b7b45e3 --- /dev/null +++ b/include/tinycompress/tinymp3.h @@ -0,0 +1,106 @@ +/* + * BSD LICENSE + * + * mp3 header and prasing + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul <vinod.koul@linux.intel.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * mp3 header and parsing + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __TINYMP3_H +#define __TINYMP3_H + +#if defined(__cplusplus) +extern "C" { +#endif + + +#define MP3_SYNC 0xe0ff + +const int mp3_sample_rates[3][3] = { + {44100, 48000, 32000}, /* MPEG-1 */ + {22050, 24000, 16000}, /* MPEG-2 */ + {11025, 12000, 8000}, /* MPEG-2.5 */ +}; + +const int mp3_bit_rates[3][3][15] = { + { + /* MPEG-1 */ + { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448}, /* Layer 1 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384}, /* Layer 2 */ + { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}, /* Layer 3 */ + }, + { + /* MPEG-2 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* Layer 1 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 2 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 3 */ + }, + { + /* MPEG-2.5 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* Layer 1 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 2 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 3 */ + }, +}; + +enum mpeg_version { + MPEG1 = 0, + MPEG2 = 1, + MPEG25 = 2 +}; + +enum mp3_stereo_mode { + STEREO = 0x00, + JOINT = 0x01, + DUAL = 0x02, + MONO = 0x03 +}; + +#endif diff --git a/include/tinycompress/version.h b/include/tinycompress/version.h new file mode 100644 index 0000000..000f6a2 --- /dev/null +++ b/include/tinycompress/version.h @@ -0,0 +1,70 @@ +/* + * BSD LICENSE + * + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __VERSION_H +#define __VERSION_H + + +#define TINYCOMPRESS_LIB_MAJOR 0 /* major number of library version */ +#define TINYCOMPRESS_LIB_MINOR 2 /* minor number of library version */ +#define TINYCOMPRESS_LIB_SUBMINOR 0 /* subminor number of library version */ + +/** library version */ +#define TINYCOMPRESS_LIB_VERSION \ + ((TINYCOMPRESS_LIB_MAJOR<<16)|\ + (TINYCOMPRESS_LIB_MINOR<<8)|\ + TINYCOMPRESS_LIB_SUBMINOR) + +/** library version (string) */ +#define TINYCOMPRESS_LIB_VERSION_STR "0.2.0" + +#endif @@ -0,0 +1,96 @@ +/* + * BSD LICENSE + * + * tinycompress utility functions + * Copyright (c) 2011-2013, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul <vinod.koul@intel.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * LGPL LICENSE + * + * tinycompress utility functions + * Copyright (c) 2011-2013, Intel Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <linux/types.h> +#include <sys/time.h> +#define __force +#define __bitwise +#define __user +#include "tinycompress/tinycompress.h" + + +unsigned int compress_get_alsa_rate(unsigned int rate) +{ + switch (rate) { + case 5512: + return SNDRV_PCM_RATE_5512; + case 8000: + return SNDRV_PCM_RATE_8000; + case 11025: + return SNDRV_PCM_RATE_11025; + case 16000: + return SNDRV_PCM_RATE_16000; + case 22050: + return SNDRV_PCM_RATE_22050; + case 32000: + return SNDRV_PCM_RATE_32000; + case 44100: + return SNDRV_PCM_RATE_44100; + case 48000: + return SNDRV_PCM_RATE_48000; + case 64000: + return SNDRV_PCM_RATE_64000; + case 88200: + return SNDRV_PCM_RATE_88200; + case 96000: + return SNDRV_PCM_RATE_96000; + case 176400: + return SNDRV_PCM_RATE_176400; + case 192000: + return SNDRV_PCM_RATE_192000; + default: + return 0; + } +} |