diff options
Diffstat (limited to 'src/combined/decoder_wavpack.c')
-rw-r--r-- | src/combined/decoder_wavpack.c | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/src/combined/decoder_wavpack.c b/src/combined/decoder_wavpack.c new file mode 100644 index 000000000..38a1b2bbe --- /dev/null +++ b/src/combined/decoder_wavpack.c @@ -0,0 +1,358 @@ +/* + * Copyright (C) 2007 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * xine interface to libwavpack by Diego Pettenò <flameeyes@gentoo.org> + * + * $Id: decoder_wavpack.c,v 1.1 2007/01/24 04:57:27 dgp85 Exp $ + */ + +#define LOG_MODULE "decode_wavpack" +#define LOG_VERBOSE + +#include "xine_internal.h" + +#include <wavpack/wavpack.h> +#include "combined_wavpack.h" + +typedef struct { + audio_decoder_class_t decoder_class; +} wavpack_class_t; + +typedef struct { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + int output_open; + + int sample_rate; + int bits_per_sample; + int channels; + + uint8_t *buf; + size_t buf_size; + size_t buf_pos; +} wavpack_decoder_t; + +#ifndef __unused +# ifdef SUPPORT_ATTRIBUTE_UNUSED +# define __unused __attribute__((unused)) +# else +# define __unused +# endif +#endif + +/* Wrapper functions for Wavpack */ +static int32_t xine_buffer_read_bytes(void *const this_gen, void *const data, + int32_t bcount) { + wavpack_decoder_t *const this = (wavpack_decoder_t*)this_gen; + + if ( bcount <= 0 ) + return 0; + + if ( bcount > (this->buf_size - this->buf_pos) ) + bcount = (this->buf_size - this->buf_pos); + + xine_fast_memcpy(data, this->buf + this->buf_pos, bcount); + this->buf_pos += bcount; + + return bcount; +} + +static uint32_t xine_buffer_get_pos(void *const this_gen) { + wavpack_decoder_t *const this = (wavpack_decoder_t*)this_gen; + return this->buf_pos; +} + +static int xine_buffer_set_pos_rel(void *const this_gen, const int32_t delta, + const int mode) { + wavpack_decoder_t *const this = (wavpack_decoder_t*)this_gen; + + switch(mode) { + case SEEK_SET: + if ( delta < 0 || delta > this->buf_size ) + return -1; + + this->buf_pos = delta; + return 0; + case SEEK_CUR: + if ( (this->buf_pos+delta) < 0 || (this->buf_pos+delta) > this->buf_size ) + return -1; + + this->buf_pos += delta; + return 0; + case SEEK_END: + if ( delta < 0 || delta > this->buf_size ) + return -1; + + this->buf_pos = this->buf_size - delta; + + return 0; + } +} + +static int xine_buffer_set_pos_abs(void *const this_gen, const uint32_t pos) { + return xine_buffer_set_pos_rel(this_gen, pos, SEEK_SET); +} + +static int xine_buffer_push_back_byte(void *const this_gen, const int c) { + if ( ! xine_buffer_set_pos_rel(this_gen, -1, SEEK_CUR) ) + return EOF; + return c; +} + +static uint32_t xine_buffer_get_length(void *const this_gen) { + wavpack_decoder_t *const this = (wavpack_decoder_t*)this_gen; + return this->buf_size; +} + +static int xine_buffer_can_seek(void *const this_gen) { + wavpack_decoder_t *const this = (wavpack_decoder_t*)this_gen; + return 1; +} + +static int32_t xine_buffer_write_bytes(__unused void *const id, + __unused void *const data, + __unused const int32_t bcount) { + lprintf("xine_buffer_write_bytes: acces is read-only.\n"); + return 0; +} + +/* Wavpack plugin functions */ +static void wavpack_reset (audio_decoder_t *const this_gen) +{ + wavpack_decoder_t *const this = (wavpack_decoder_t *) this_gen; + + this->buf_pos = 0; +} + +static void wavpack_discontinuity (audio_decoder_t *const this_gen) +{ + wavpack_decoder_t *this = (wavpack_decoder_t *) this_gen; + + lprintf("Discontinuity!\n"); +} + +static void wavpack_decode_data (audio_decoder_t *const this_gen, buf_element_t *const buf) +{ + wavpack_decoder_t *const this = (wavpack_decoder_t *) this_gen; + int ret = 1; + + /* We are getting the stream header, open up the audio + * device, and collect information about the stream + */ + if (buf->decoder_flags & BUF_FLAG_STDHEADER) + { + int mode = AO_CAP_MODE_MONO; + + this->sample_rate = buf->decoder_info[1]; + this->bits_per_sample = buf->decoder_info[2]; + this->channels = buf->decoder_info[3]; + + mode = _x_ao_channels2mode(this->channels); + + _x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, + "WavPack"); + + if (!this->output_open) + { + this->output_open = this->stream->audio_out->open ( + this->stream->audio_out, + this->stream, + this->bits_per_sample, + this->sample_rate, + mode); + } + this->buf_pos = 0; + } else if (this->output_open) { + /* This isn't a header frame and we have opened the output device */ + + /* First time, we prepend the header, so that Wavpack library can open it. + * As FFmpeg requires the last 12 bytes of the header for its stream, create + * the rest of the header when the frame ended (so we have its size). + */ + if ( ! this->buf ) + this->buf = xine_xmalloc(this->buf_size = sizeof(wvheader_t)-12); + if ( this->buf_pos == 0 ) + this->buf_pos = sizeof(wvheader_t)-12; + + /* What we have buffered so far, and what is coming in + * is larger than our buffer + */ + if (this->buf_pos + buf->size > this->buf_size) + { + this->buf_size += 2 * buf->size; + this->buf = realloc (this->buf, this->buf_size); + lprintf("reallocating buffer to %zd\n", this->buf_size); + } + + xine_fast_memcpy (&this->buf[this->buf_pos], buf->content, buf->size); + this->buf_pos += buf->size; + + if ( buf->decoder_flags & BUF_FLAG_FRAME_END ) { + static WavpackStreamReader wavpack_buffer_reader = { + .read_bytes = xine_buffer_read_bytes, + .get_pos = xine_buffer_get_pos, + .set_pos_abs = xine_buffer_set_pos_abs, + .set_pos_rel = xine_buffer_set_pos_rel, + .push_back_byte = xine_buffer_push_back_byte, + .get_length = xine_buffer_get_length, + .can_seek = xine_buffer_can_seek, + .write_bytes = xine_buffer_write_bytes + }; + + WavpackContext *ctx = NULL; + char error[256]; /* Current version of wavpack (4.31) does not write more than this */ + int32_t samples_left; uint32_t samples_total; + wvheader_t *header = (wvheader_t*)this->buf; + + header->idcode = wvpk_signature; +#ifdef WORDS_BIGENDIAN + header->block_size = bswap_32(this->buf_size-sizeof(wvheader_t)); + header->wv_version = bswap_16(0x0406); +#else + header->block_size = this->buf_pos-8; + header->wv_version = 0x0406; +#endif + header->track = 0; header->index = 0; + header->file_samples = (uint32_t)-1; + header->samples_index = 0; + + this->buf_pos = 0; + + if ( header->samples_count == 0 ) return; + + ctx = WavpackOpenFileInputEx(&wavpack_buffer_reader, this, NULL, error, OPEN_STREAMING, 0); + if ( ! ctx ) { + lprintf("unable to open the stream: %s\n", error); + this->buf_pos = 0; + return; + } + + samples_left = samples_total = header->samples_count; + while ( samples_left > 0 ) { + uint32_t buf_samples, decoded_count; + audio_buffer_t *audio_buffer = this->stream->audio_out->get_buffer(this->stream->audio_out); + int32_t *decoded; + int i; + + buf_samples = audio_buffer->mem_size / (this->channels * (this->bits_per_sample/8)); + if ( buf_samples > samples_left ) buf_samples = samples_left; + if ( buf_samples > 4096 ) buf_samples = 4096; + + decoded = alloca(buf_samples * this->channels * sizeof(int32_t)); + + decoded_count = WavpackUnpackSamples(ctx, decoded, buf_samples); + if ( decoded_count == 0 && *error ) { + fprintf(stderr, "Error during decode: %s\n", error); + break; + } + + if ( decoded_count == 0 ) { + lprintf("Finished decoding, but still %"PRId64" samples left?\n", samples_left); + break; + } + + lprintf("Decoded %d samples\n", buf_samples); + + samples_left -= decoded_count; + + audio_buffer->num_frames = decoded_count; + audio_buffer->vpts = 0; /* TODO: Fix the pts calculation */ + // audio_buffer->vpts = (buf->pts * (samples_total-samples_left)) / samples_total; + lprintf("Audio buffer with pts %"PRId64"\n", audio_buffer->vpts); + + switch(this->bits_per_sample) { + case 8: { + int8_t *data8 = (int8_t*)audio_buffer->mem; + for(i = 0; i < decoded_count*this->channels; i++) + data8[i] = decoded[i]; + } + break; + case 16: { + int16_t *data16 = (int16_t*)audio_buffer->mem; + for(i = 0; i < decoded_count*this->channels; i++) + data16[i] = decoded[i]; + } + } + + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + } + + WavpackCloseFile(ctx); + this->buf_pos = 0; + } + } else + return; +} + +static void wavpack_dispose (audio_decoder_t *this_gen) { + wavpack_decoder_t *this = (wavpack_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + free(this->buf); + + free (this_gen); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + wavpack_decoder_t * const this = (wavpack_decoder_t *) xine_xmalloc (sizeof (wavpack_decoder_t)); + + this->audio_decoder.decode_data = wavpack_decode_data; + this->audio_decoder.reset = wavpack_reset; + this->audio_decoder.discontinuity = wavpack_discontinuity; + this->audio_decoder.dispose = wavpack_dispose; + this->stream = stream; + + this->buf = NULL; + this->buf_size = 0; + + return (audio_decoder_t *) this; +} + +/* + * wavpack plugin class + */ + +static char *get_identifier (audio_decoder_class_t *this) { + return "wavpackdec"; +} + +static char *get_description (audio_decoder_class_t *this) { + return "wavpack audio decoder plugin"; +} + +static void dispose_class (audio_decoder_class_t *this) { + free (this); +} + +void *decoder_wavpack_init_plugin (xine_t *xine, void *data) { + wavpack_class_t *this; + + this = (wavpack_class_t *) xine_xmalloc (sizeof (wavpack_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.get_identifier = get_identifier; + this->decoder_class.get_description = get_description; + this->decoder_class.dispose = dispose_class; + + return this; +} |