From 55b29e91a31f519e141fafa490a38a7db76f503e Mon Sep 17 00:00:00 2001 From: Vinod Koul Date: Thu, 15 Mar 2012 16:20:30 +0530 Subject: [PATCH] initial creation Add the initial files of tinycompress and utility player cplay Signed-off-by: Vinod Koul --- Android.mk | 22 ++ README | 31 ++- compress.c | 413 ++++++++++++++++++++++++++++ cplay.c | 359 ++++++++++++++++++++++++ include/sound/compress_offload.h | 81 ++++++ include/sound/compress_params.h | 230 ++++++++++++++++ include/tinycompress/tinycompress.h | 204 ++++++++++++++ include/tinycompress/tinymp3.h | 106 +++++++ include/tinycompress/version.h | 70 +++++ 9 files changed, 1515 insertions(+), 1 deletion(-) create mode 100644 Android.mk create mode 100644 compress.c create mode 100644 cplay.c create mode 100644 include/sound/compress_offload.h create mode 100644 include/sound/compress_params.h create mode 100644 include/tinycompress/tinycompress.h create mode 100644 include/tinycompress/tinymp3.h create mode 100644 include/tinycompress/version.h diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..b9c52e2 --- /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 +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/README b/README index 66a386b..73c19a6 100644 --- a/README +++ b/README @@ -1 +1,30 @@ -Just test... + 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 for library design +- Navjot Singh for writing the mp3 parser code + diff --git a/compress.c b/compress.c new file mode 100644 index 0000000..f4cf05a --- /dev/null +++ b/compress.c @@ -0,0 +1,413 @@ +/* + * BSD LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#define __force +#define __bitwise +#define __user +#include +#include "sound/compress_params.h" +#include "sound/compress_offload.h" +#include "tinycompress/tinycompress.h" + +#define COMPR_ERR_MAX 32 + +struct compress { + int fd; + unsigned int flags; + __u64 buffer_size; + char error[COMPR_ERR_MAX]; + struct compr_config *config; + int running:1; +}; + +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); + + if (errno) + snprintf(compress->error + sz, COMPR_ERR_MAX - sz, + ": %s", strerror(e)); + return e; +} + +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 bool _is_codec_supported(struct compress *compress, struct compr_config *config) +{ + struct snd_compr_caps caps; + bool codec = false; + unsigned int i; + + if (ioctl(compress->fd, SNDRV_COMPRESS_GET_CAPS, &caps)) { + oops(compress, errno, "cannot get device caps"); + return false; + } + + 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\n", + 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\n", + 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\n", + config->fragments, caps.min_fragments); + return false; + } + if (config->fragments > caps.max_fragments) { + oops(compress, -EINVAL, "requested fragments %d are above max supported %d\n", + 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; + char fn[256]; + int rc; + + compress = calloc(1, sizeof(struct compress)); + if (!compress || !config) + return &bad_compress; + + compress->config = config; + + snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); + + compress->flags = flags; + if (!((flags & COMPRESS_OUT) || (flags & COMPRESS_IN))) { + oops(compress, -EINVAL, "can't deduce device direction from given flags\n"); + goto input_fail; + } + if (flags & COMPRESS_OUT) { + /* this should be removed once we have capture tested */ + oops(compress, -EINVAL, "this version doesnt support capture\n"); + goto input_fail; + } + + compress->fd = open(fn, O_WRONLY); + if (compress->fd < 0) { + oops(compress, errno, "cannot open device '%s'", fn); + goto input_fail; + } +#if 0 + /* FIXME need to turn this On when DSP supports + * and treat in no support case + */ + if (_is_codec_supported(compress, config) == false) { + oops(compress, errno, "codec not supported\n"); + goto codec_fail; + } +#endif + fill_compress_params(config, ¶ms); + + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) { + oops(compress, errno, "cannot set device"); + goto codec_fail; + } + + return compress; + +codec_fail: + close(compress->fd); + compress->fd = -1; +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); +} + +int compress_get_hpointer(struct compress *compress, + unsigned int *avail, struct timespec *tstamp) +{ + struct snd_compr_avail kavail; + size_t 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"); + *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 * 1000000000) / kavail.tstamp.sampling_rate; + tstamp->tv_nsec = time; + return 0; +} + +int compress_write(struct compress *compress, char *buf, unsigned int size) +{ + struct snd_compr_avail avail; + struct pollfd fds; + int to_write, written, total = 0, ret; + + 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"); + + if (avail.avail == 0) { + /* nothing to write so wait for 10secs */ + ret = poll(&fds, 1, 1000000); + if (ret < 0) + return oops(compress, errno, "poll error"); + if (ret == 0) + return oops(compress, -EPIPE, "Poll timeout, Broken Pipe"); + if (fds.revents & POLLOUT) { + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); + if (avail.avail == 0) { + oops(compress, -EIO, "woken up even when avail is 0!!!"); + continue; + } + } + if (fds.revents & POLLERR) { + return oops(compress, -EIO, "poll returned error!"); + } + } + /* write avail bytes */ + if (size > avail.avail) + to_write = avail.avail; + else + to_write = size; + written = write(compress->fd, buf, to_write); + if (written < 0) + return oops(compress, errno, "write failed!\n"); + + size -= written; + buf += written; + total += written; + } + return total; +} + +int compress_read(struct compress *compress, void *buf, unsigned int size) +{ + return oops(compress, -ENOTTY, "Not supported yet in lib"); +} + +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\n"); + 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\n"); + 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\n"); + 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\n"); + return 0; +} + +bool is_codec_supported(unsigned int card, unsigned int device, + unsigned int flags, struct snd_codec *codec) +{ + struct snd_compr_params params; + unsigned int dev_flag; + bool ret; + int fd; + char fn[256]; + int rc; + + 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; +} + diff --git a/cplay.c b/cplay.c new file mode 100644 index 0000000..d2a968c --- /dev/null +++ b/cplay.c @@ -0,0 +1,359 @@ +/* + * BSD LICENSE + * + * tinyplay command line player for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#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 = 100*1024; + int c; + unsigned int card = 0, device = 0, frag = 4; + + + 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 i, 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; + switch (rate) { + case 5512: + codec.sample_rate = SNDRV_PCM_RATE_5512; + break; + case 8000: + codec.sample_rate = SNDRV_PCM_RATE_8000; + break; + case 11025: + codec.sample_rate = SNDRV_PCM_RATE_11025; + break; + case 16000: + codec.sample_rate = SNDRV_PCM_RATE_16000; + break; + case 220500: + codec.sample_rate = SNDRV_PCM_RATE_22050; + break; + case 32000: + codec.sample_rate = SNDRV_PCM_RATE_32000; + break; + case 44100: + codec.sample_rate = SNDRV_PCM_RATE_44100; + break; + case 48000: + codec.sample_rate = SNDRV_PCM_RATE_48000; + break; + case 64000: + codec.sample_rate = SNDRV_PCM_RATE_64000; + break; + case 88200: + codec.sample_rate = SNDRV_PCM_RATE_88200; + break; + case 96000: + codec.sample_rate = SNDRV_PCM_RATE_96000; + break; + case 176400: + codec.sample_rate = SNDRV_PCM_RATE_176400; + break; + case 192000: + codec.sample_rate = SNDRV_PCM_RATE_192000; + break; + default: + fprintf(stderr, "unknown sample rate %d\n", rate); + goto FILE_EXIT; + } + codec.bit_rate = bits; + codec.rate_control = 0; + codec.profile = 0; + codec.level = 0; + codec.ch_mode = 0; + codec.format = 0; + config.fragment_size = buffer_size/frag; + config.fragments = frag; + 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: Buufer 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 sucess\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/sound/compress_offload.h b/include/sound/compress_offload.h new file mode 100644 index 0000000..bbb09bc --- /dev/null +++ b/include/sound/compress_offload.h @@ -0,0 +1,81 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef __COMPRESS_OFFLOAD_H +#define __COMPRESS_OFFLOAD_H + +#include +#include +//#include + +#define SNDRV_COMPRESS_VERSION SNDRV_PROTOCOL_VERSION(0, 1, 0) + +struct snd_compressed_buffer { + __u32 fragment_size; + __u32 fragments; +}; + +struct snd_compr_params { + struct snd_compressed_buffer buffer; + struct snd_codec codec; + __u8 no_wake_mode; +}; + +struct snd_compr_tstamp { + __u32 byte_offset; + __u32 copied_total; + snd_pcm_uframes_t pcm_frames; + snd_pcm_uframes_t pcm_io_frames; + __u32 sampling_rate; +}; + +struct snd_compr_avail { + __u64 avail; + struct snd_compr_tstamp tstamp; +}; + +enum snd_compr_direction { + SND_COMPRESS_PLAYBACK = 0, + SND_COMPRESS_CAPTURE +}; + +struct snd_compr_caps { + __u32 num_codecs; + __u32 direction; + __u32 min_fragment_size; + __u32 max_fragment_size; + __u32 min_fragments; + __u32 max_fragments; + __u32 codecs[MAX_NUM_CODECS]; + __u32 reserved[11]; +}; + +struct snd_compr_codec_caps { + __u32 codec; + __u32 num_descriptors; + struct snd_codec_desc descriptor[MAX_NUM_CODEC_DESCRIPTORS]; +}; + +#define SNDRV_COMPRESS_IOCTL_VERSION _IOR('C', 0x00, int) +#define SNDRV_COMPRESS_GET_CAPS _IOWR('C', 0x10, struct snd_compr_caps) +#define SNDRV_COMPRESS_GET_CODEC_CAPS _IOWR('C', 0x11, struct snd_compr_codec_caps) +#define SNDRV_COMPRESS_SET_PARAMS _IOW('C', 0x12, struct snd_compr_params) +#define SNDRV_COMPRESS_GET_PARAMS _IOR('C', 0x13, struct snd_codec) +#define SNDRV_COMPRESS_TSTAMP _IOR('C', 0x20, struct snd_compr_tstamp) +#define SNDRV_COMPRESS_AVAIL _IOR('C', 0x21, struct snd_compr_avail) +#define SNDRV_COMPRESS_PAUSE _IO('C', 0x30) +#define SNDRV_COMPRESS_RESUME _IO('C', 0x31) +#define SNDRV_COMPRESS_START _IO('C', 0x32) +#define SNDRV_COMPRESS_STOP _IO('C', 0x33) +#define SNDRV_COMPRESS_DRAIN _IO('C', 0x34) + +#define SND_COMPR_TRIGGER_DRAIN 7 +#endif diff --git a/include/sound/compress_params.h b/include/sound/compress_params.h new file mode 100644 index 0000000..bc977c5 --- /dev/null +++ b/include/sound/compress_params.h @@ -0,0 +1,230 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef __SND_COMPRESS_PARAMS_H +#define __SND_COMPRESS_PARAMS_H + +#define MAX_NUM_CODECS 32 +#define MAX_NUM_CODEC_DESCRIPTORS 32 +#define MAX_NUM_BITRATES 32 + +#define SND_AUDIOCODEC_PCM ((__u32) 0x00000001) +#define SND_AUDIOCODEC_MP3 ((__u32) 0x00000002) +#define SND_AUDIOCODEC_AMR ((__u32) 0x00000003) +#define SND_AUDIOCODEC_AMRWB ((__u32) 0x00000004) +#define SND_AUDIOCODEC_AMRWBPLUS ((__u32) 0x00000005) +#define SND_AUDIOCODEC_AAC ((__u32) 0x00000006) +#define SND_AUDIOCODEC_WMA ((__u32) 0x00000007) +#define SND_AUDIOCODEC_REAL ((__u32) 0x00000008) +#define SND_AUDIOCODEC_VORBIS ((__u32) 0x00000009) +#define SND_AUDIOCODEC_FLAC ((__u32) 0x0000000A) +#define SND_AUDIOCODEC_IEC61937 ((__u32) 0x0000000B) +#define SND_AUDIOCODEC_G723_1 ((__u32) 0x0000000C) +#define SND_AUDIOCODEC_G729 ((__u32) 0x0000000D) + +#define SND_AUDIOPROFILE_PCM ((__u32) 0x00000001) + +#define SND_AUDIOCHANMODE_MP3_MONO ((__u32) 0x00000001) +#define SND_AUDIOCHANMODE_MP3_STEREO ((__u32) 0x00000002) +#define SND_AUDIOCHANMODE_MP3_JOINTSTEREO ((__u32) 0x00000004) +#define SND_AUDIOCHANMODE_MP3_DUAL ((__u32) 0x00000008) + +#define SND_AUDIOPROFILE_AMR ((__u32) 0x00000001) + +#define SND_AUDIOMODE_AMR_DTX_OFF ((__u32) 0x00000001) +#define SND_AUDIOMODE_AMR_VAD1 ((__u32) 0x00000002) +#define SND_AUDIOMODE_AMR_VAD2 ((__u32) 0x00000004) + +#define SND_AUDIOSTREAMFORMAT_UNDEFINED ((__u32) 0x00000000) +#define SND_AUDIOSTREAMFORMAT_CONFORMANCE ((__u32) 0x00000001) +#define SND_AUDIOSTREAMFORMAT_IF1 ((__u32) 0x00000002) +#define SND_AUDIOSTREAMFORMAT_IF2 ((__u32) 0x00000004) +#define SND_AUDIOSTREAMFORMAT_FSF ((__u32) 0x00000008) +#define SND_AUDIOSTREAMFORMAT_RTPPAYLOAD ((__u32) 0x00000010) +#define SND_AUDIOSTREAMFORMAT_ITU ((__u32) 0x00000020) + +#define SND_AUDIOPROFILE_AMRWB ((__u32) 0x00000001) + +#define SND_AUDIOMODE_AMRWB_DTX_OFF ((__u32) 0x00000001) +#define SND_AUDIOMODE_AMRWB_VAD1 ((__u32) 0x00000002) +#define SND_AUDIOMODE_AMRWB_VAD2 ((__u32) 0x00000004) + +#define SND_AUDIOPROFILE_AMRWBPLUS ((__u32) 0x00000001) + +#define SND_AUDIOPROFILE_AAC ((__u32) 0x00000001) + +#define SND_AUDIOMODE_AAC_MAIN ((__u32) 0x00000001) +#define SND_AUDIOMODE_AAC_LC ((__u32) 0x00000002) +#define SND_AUDIOMODE_AAC_SSR ((__u32) 0x00000004) +#define SND_AUDIOMODE_AAC_LTP ((__u32) 0x00000008) +#define SND_AUDIOMODE_AAC_HE ((__u32) 0x00000010) +#define SND_AUDIOMODE_AAC_SCALABLE ((__u32) 0x00000020) +#define SND_AUDIOMODE_AAC_ERLC ((__u32) 0x00000040) +#define SND_AUDIOMODE_AAC_LD ((__u32) 0x00000080) +#define SND_AUDIOMODE_AAC_HE_PS ((__u32) 0x00000100) +#define SND_AUDIOMODE_AAC_HE_MPS ((__u32) 0x00000200) + +#define SND_AUDIOSTREAMFORMAT_MP2ADTS ((__u32) 0x00000001) +#define SND_AUDIOSTREAMFORMAT_MP4ADTS ((__u32) 0x00000002) +#define SND_AUDIOSTREAMFORMAT_MP4LOAS ((__u32) 0x00000004) +#define SND_AUDIOSTREAMFORMAT_MP4LATM ((__u32) 0x00000008) +#define SND_AUDIOSTREAMFORMAT_ADIF ((__u32) 0x00000010) +#define SND_AUDIOSTREAMFORMAT_MP4FF ((__u32) 0x00000020) +#define SND_AUDIOSTREAMFORMAT_RAW ((__u32) 0x00000040) + +#define SND_AUDIOPROFILE_WMA7 ((__u32) 0x00000001) +#define SND_AUDIOPROFILE_WMA8 ((__u32) 0x00000002) +#define SND_AUDIOPROFILE_WMA9 ((__u32) 0x00000004) +#define SND_AUDIOPROFILE_WMA10 ((__u32) 0x00000008) + +#define SND_AUDIOMODE_WMA_LEVEL1 ((__u32) 0x00000001) +#define SND_AUDIOMODE_WMA_LEVEL2 ((__u32) 0x00000002) +#define SND_AUDIOMODE_WMA_LEVEL3 ((__u32) 0x00000004) +#define SND_AUDIOMODE_WMA_LEVEL4 ((__u32) 0x00000008) +#define SND_AUDIOMODE_WMAPRO_LEVELM0 ((__u32) 0x00000010) +#define SND_AUDIOMODE_WMAPRO_LEVELM1 ((__u32) 0x00000020) +#define SND_AUDIOMODE_WMAPRO_LEVELM2 ((__u32) 0x00000040) +#define SND_AUDIOMODE_WMAPRO_LEVELM3 ((__u32) 0x00000080) + +#define SND_AUDIOSTREAMFORMAT_WMA_ASF ((__u32) 0x00000001) + +#define SND_AUDIOSTREAMFORMAT_WMA_NOASF_HDR ((__u32) 0x00000002) + +#define SND_AUDIOPROFILE_REALAUDIO ((__u32) 0x00000001) + +#define SND_AUDIOMODE_REALAUDIO_G2 ((__u32) 0x00000001) +#define SND_AUDIOMODE_REALAUDIO_8 ((__u32) 0x00000002) +#define SND_AUDIOMODE_REALAUDIO_10 ((__u32) 0x00000004) +#define SND_AUDIOMODE_REALAUDIO_SURROUND ((__u32) 0x00000008) + +#define SND_AUDIOPROFILE_VORBIS ((__u32) 0x00000001) + +#define SND_AUDIOMODE_VORBIS ((__u32) 0x00000001) + +#define SND_AUDIOPROFILE_FLAC ((__u32) 0x00000001) + +#define SND_AUDIOMODE_FLAC_LEVEL0 ((__u32) 0x00000001) +#define SND_AUDIOMODE_FLAC_LEVEL1 ((__u32) 0x00000002) +#define SND_AUDIOMODE_FLAC_LEVEL2 ((__u32) 0x00000004) +#define SND_AUDIOMODE_FLAC_LEVEL3 ((__u32) 0x00000008) +#define SND_AUDIOMODE_FLAC_LEVEL4 ((__u32) 0x00000010) +#define SND_AUDIOMODE_FLAC_LEVEL5 ((__u32) 0x00000020) +#define SND_AUDIOMODE_FLAC_LEVEL6 ((__u32) 0x00000040) +#define SND_AUDIOMODE_FLAC_LEVEL7 ((__u32) 0x00000080) +#define SND_AUDIOMODE_FLAC_LEVEL8 ((__u32) 0x00000100) + +#define SND_AUDIOSTREAMFORMAT_FLAC ((__u32) 0x00000001) +#define SND_AUDIOSTREAMFORMAT_FLAC_OGG ((__u32) 0x00000002) + +#define SND_AUDIOPROFILE_IEC61937 ((__u32) 0x00000001) + +#define SND_AUDIOPROFILE_IEC61937_SPDIF ((__u32) 0x00000002) + +#define SND_AUDIOMODE_IEC_REF_STREAM_HEADER ((__u32) 0x00000000) +#define SND_AUDIOMODE_IEC_LPCM ((__u32) 0x00000001) +#define SND_AUDIOMODE_IEC_AC3 ((__u32) 0x00000002) +#define SND_AUDIOMODE_IEC_MPEG1 ((__u32) 0x00000004) +#define SND_AUDIOMODE_IEC_MP3 ((__u32) 0x00000008) +#define SND_AUDIOMODE_IEC_MPEG2 ((__u32) 0x00000010) +#define SND_AUDIOMODE_IEC_AACLC ((__u32) 0x00000020) +#define SND_AUDIOMODE_IEC_DTS ((__u32) 0x00000040) +#define SND_AUDIOMODE_IEC_ATRAC ((__u32) 0x00000080) +#define SND_AUDIOMODE_IEC_SACD ((__u32) 0x00000100) +#define SND_AUDIOMODE_IEC_EAC3 ((__u32) 0x00000200) +#define SND_AUDIOMODE_IEC_DTS_HD ((__u32) 0x00000400) +#define SND_AUDIOMODE_IEC_MLP ((__u32) 0x00000800) +#define SND_AUDIOMODE_IEC_DST ((__u32) 0x00001000) +#define SND_AUDIOMODE_IEC_WMAPRO ((__u32) 0x00002000) +#define SND_AUDIOMODE_IEC_REF_CXT ((__u32) 0x00004000) +#define SND_AUDIOMODE_IEC_HE_AAC ((__u32) 0x00008000) +#define SND_AUDIOMODE_IEC_HE_AAC2 ((__u32) 0x00010000) +#define SND_AUDIOMODE_IEC_MPEG_SURROUND ((__u32) 0x00020000) + +#define SND_AUDIOPROFILE_G723_1 ((__u32) 0x00000001) + +#define SND_AUDIOMODE_G723_1_ANNEX_A ((__u32) 0x00000001) +#define SND_AUDIOMODE_G723_1_ANNEX_B ((__u32) 0x00000002) +#define SND_AUDIOMODE_G723_1_ANNEX_C ((__u32) 0x00000004) + +#define SND_AUDIOPROFILE_G729 ((__u32) 0x00000001) + +#define SND_AUDIOMODE_G729_ANNEX_A ((__u32) 0x00000001) +#define SND_AUDIOMODE_G729_ANNEX_B ((__u32) 0x00000002) + +#define SND_RATECONTROLMODE_CONSTANTBITRATE ((__u32) 0x00000001) +#define SND_RATECONTROLMODE_VARIABLEBITRATE ((__u32) 0x00000002) + +struct snd_enc_wma { + __u32 super_block_align; +}; + +struct snd_enc_vorbis { + __s32 quality; + __u32 managed; + __u32 max_bit_rate; + __u32 min_bit_rate; + __u32 downmix; +}; + +struct snd_enc_real { + __u32 quant_bits; + __u32 start_region; + __u32 num_regions; +}; + +struct snd_enc_flac { + __u32 num; + __u32 gain; +}; + +struct snd_enc_generic { + __u32 bw; + __s32 reserved[15]; +}; + +union snd_codec_options { + struct snd_enc_wma wma; + struct snd_enc_vorbis vorbis; + struct snd_enc_real real; + struct snd_enc_flac flac; + struct snd_enc_generic generic; +}; + +struct snd_codec_desc { + __u32 max_ch; + __u32 sample_rates; + __u32 bit_rate[MAX_NUM_BITRATES]; + __u32 num_bitrates; + __u32 rate_control; + __u32 profiles; + __u32 modes; + __u32 formats; + __u32 min_buffer; + __u32 reserved[15]; +}; + +struct snd_codec { + __u32 id; + __u32 ch_in; + __u32 ch_out; + __u32 sample_rate; + __u32 bit_rate; + __u32 rate_control; + __u32 profile; + __u32 level; + __u32 ch_mode; + __u32 format; + __u32 align; + union snd_codec_options options; + __u32 reserved[3]; +}; + +#endif diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h new file mode 100644 index 0000000..13dea3e --- /dev/null +++ b/include/tinycompress/tinycompress.h @@ -0,0 +1,204 @@ +/* + * 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 + * + * @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; +}; + +#define COMPRESS_OUT 0x00000000 +#define COMPRESS_IN 0x10000000 + +struct compress; + +/* + * compress_open: open a new compress stream + * returns the valid struct compress on success, NULL on failure + * + * @card: sound card number + * @device: device number + * @flags: device flags can be COMPRESS_OUT or COMPRESS_IN + * @config: stream config requested + */ +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_write: write data to the compress stream + * return bytes written on success, negative on error + * this is a blocking call + * + * @compress: compress stream to be written to + * @buf: pointer to data + * @size: number of bytes to be written + */ +int compress_write(struct compress *compress, char *buf, unsigned int size); + +/* + * compress_read: read data from the compress stream + * return bytes read on success, negative on error + * + * @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); + +/* + * 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); + +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 */ + +#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 + * + * 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..89f0f4e --- /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 0 /* minor number of library version */ +#define TINYCOMPRESS_LIB_SUBMINOR 1 /* 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.0.1" + +#endif -- 2.47.3