diff options
Diffstat (limited to 'src/audio_dec')
-rw-r--r-- | src/audio_dec/Makefile.am | 85 | ||||
-rw-r--r-- | src/audio_dec/fooaudio.c | 336 | ||||
-rw-r--r-- | src/audio_dec/gsm610.c | 281 | ||||
-rw-r--r-- | src/audio_dec/xine_a52_decoder.c | 866 | ||||
-rw-r--r-- | src/audio_dec/xine_dts_decoder.c | 578 | ||||
-rw-r--r-- | src/audio_dec/xine_faad_decoder.c | 477 | ||||
-rw-r--r-- | src/audio_dec/xine_lpcm_decoder.c | 278 | ||||
-rw-r--r-- | src/audio_dec/xine_mad_decoder.c | 365 | ||||
-rw-r--r-- | src/audio_dec/xine_musepack_decoder.c | 462 |
9 files changed, 3728 insertions, 0 deletions
diff --git a/src/audio_dec/Makefile.am b/src/audio_dec/Makefile.am new file mode 100644 index 000000000..a85497bbf --- /dev/null +++ b/src/audio_dec/Makefile.am @@ -0,0 +1,85 @@ +include $(top_srcdir)/misc/Makefile.common + +AM_CFLAGS = $(DEFAULT_OCFLAGS) $(VISIBILITY_FLAG) +AM_LDFLAGS = $(xineplug_ldflags) + +EXTRA_DIST = fooaudio.c + +if ENABLE_MUSEPACK +musepack_module = xineplug_decode_mpc.la +endif + +if ENABLE_DTS +dts_module = xineplug_decode_dts.la +endif + +if ENABLE_MAD +mad_module = xineplug_decode_mad.la +endif + +if ENABLE_A52DEC +a52_module = xineplug_decode_a52.la +endif + +if ENABLE_FAAD +faad_module = xineplug_decode_faad.la +endif + +$(top_builddir)/contrib/a52dec/liba52.la: + $(MAKE) -C $(top_builddir)/contrib/a52dec + +$(top_builddir)/contrib/libfaad/libfaad.la: + $(MAKE) -C $(top_builddir)/contrib/libfaad + +$(top_builddir)/contrib/libmad/libmad.la: + $(MAKE) -C $(top_builddir)/contrib/libmad + +$(top_builddir)/contrib/libmpcdec/libmpcdec.la: + $(MAKE) -C $(top_builddir)/contrib/libmpcdec + +$(top_builddir)/contrib/libdca/libdca.la: + $(MAKE) -C $(top_builddir)/contrib/libdca + +$(top_builddir)/contrib/gsm610/libgsm610.la: + $(MAKE) -C $(top_builddir)/contrib/gsm610 + +xineplug_LTLIBRARIES = \ + xineplug_decode_gsm610.la \ + xineplug_decode_lpcm.la \ + $(musepack_module) \ + $(dts_module) \ + $(mad_module) \ + $(a52_module) \ + $(faad_module) + +xineplug_decode_gsm610_la_SOURCES = gsm610.c +xineplug_decode_gsm610_la_LIBADD = $(XINE_LIB) $(top_builddir)/contrib/gsm610/libgsm610.la +xineplug_decode_gsm610_la_CPPFLAGS = -I$(top_srcdir)/contrib/gsm610 + +xineplug_decode_lpcm_la_SOURCES = xine_lpcm_decoder.c +xineplug_decode_lpcm_la_LIBADD = $(XINE_LIB) + +xineplug_decode_mpc_la_SOURCES = xine_musepack_decoder.c +xineplug_decode_mpc_la_DEPENDENCIES = $(MPCDEC_DEPS) +xineplug_decode_mpc_la_LIBADD = $(XINE_LIB) $(LTLIBINTL) $(MPCDEC_LIBS) +xineplug_decode_mpc_la_CFLAGS = $(AM_CFLAGS) $(MPCDEC_CFLAGS) + +xineplug_decode_dts_la_SOURCES = xine_dts_decoder.c +xineplug_decode_dts_la_DEPENDENCIES = $(LIBDTS_DEPS) +xineplug_decode_dts_la_LIBADD = $(XINE_LIB) $(LTLIBINTL) $(LIBDTS_LIBS) +xineplug_decode_dts_la_CFLAGS = $(AM_CFLAGS) $(LIBDTS_CFLAGS) + +xineplug_decode_mad_la_SOURCES = xine_mad_decoder.c +xineplug_decode_mad_la_DEPENDENCIES = $(LIBMAD_DEPS) +xineplug_decode_mad_la_LIBADD = $(XINE_LIB) $(LIBMAD_LIBS) +xineplug_decode_mad_la_CFLAGS = $(AM_CFLAGS) $(LIBMAD_CFLAGS) + +xineplug_decode_a52_la_SOURCES = xine_a52_decoder.c +xineplug_decode_a52_la_DEPENDENCIES = $(A52DEC_DEPS) +xineplug_decode_a52_la_LIBADD = $(XINE_LIB) $(LTLIBINTL) $(A52DEC_LIBS) -lm +xineplug_decode_a52_la_CFLAGS = $(AM_CFLAGS) $(A52DEC_CFLAGS) + +xineplug_decode_faad_la_SOURCES = xine_faad_decoder.c +xineplug_decode_faad_la_DEPENDENCIES = $(FAAD_DEPS) +xineplug_decode_faad_la_LIBADD = $(XINE_LIB) $(LTLIBINTL) $(FAAD_LIBS) -lm +xineplug_decode_faad_la_CFLAGS = $(FAAD_CFLAGS) diff --git a/src/audio_dec/fooaudio.c b/src/audio_dec/fooaudio.c new file mode 100644 index 000000000..34a3f2d48 --- /dev/null +++ b/src/audio_dec/fooaudio.c @@ -0,0 +1,336 @@ +/* + * Copyright (C) 2000-2001 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + * + * fooaudio.c: This is a reference audio decoder for the xine multimedia + * player. It really works too! It will output a continuous sine wave in + * place of the data it should actually send. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> +#include <xine/xineutils.h> +#include "bswap.h" + +/* math.h required for fooaudio sine wave generation */ +#include <math.h> + +#define AUDIOBUFSIZE 128*1024 + +typedef struct { + audio_decoder_class_t decoder_class; +} fooaudio_class_t; + +typedef struct fooaudio_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + int sample_rate; /* audio sample rate */ + int bits_per_sample; /* bits/sample, usually 8 or 16 */ + int channels; /* 1 or 2, usually */ + + int output_open; /* flag to indicate audio is ready */ + + unsigned char *buf; /* data accumulation buffer */ + int bufsize; /* maximum size of buf */ + int size; /* size of accumulated data in buf */ + + /* fooaudio-specific variables */ + int64_t last_pts; + unsigned int iteration; + +} fooaudio_decoder_t; + +/************************************************************************** + * fooaudio specific decode functions + *************************************************************************/ + +/************************************************************************** + * xine audio plugin functions + *************************************************************************/ + +static void fooaudio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + fooaudio_decoder_t *this = (fooaudio_decoder_t *) this_gen; + audio_buffer_t *audio_buffer; + int i; + int64_t samples_to_generate; + int samples_to_send; + + if (buf->decoder_flags & BUF_FLAG_STDHEADER) { + + /* When the engine sends a BUF_FLAG_HEADER flag, it is time to initialize + * the decoder. The buffer element type has 4 decoder_info fields, + * 0..3. Field 1 is the sample rate. Field 2 is the bits/sample. Field + * 3 is the number of channels. */ + this->sample_rate = buf->decoder_info[1]; + this->bits_per_sample = buf->decoder_info[2]; + this->channels = buf->decoder_info[3]; + + /* initialize the data accumulation buffer */ + this->buf = xine_xmalloc(AUDIOBUFSIZE); + this->bufsize = AUDIOBUFSIZE; + this->size = 0; + + /* take this opportunity to initialize stream/meta information */ + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "fooaudio"); + + /* perform any other required initialization */ + this->last_pts = -1; + this->iteration = 0; + + return; + } + + /* if the audio output is not open yet, open the audio output */ +#warning: Audio output is hardcoded to mono 16-bit PCM + if (!this->output_open) { + this->output_open = (this->stream->audio_out->open) ( + this->stream->audio_out, + this->stream, +/* this->bits_per_sample, */ + 16, + this->sample_rate, +/* _x_ao_channels2mode(this->channels));*/ + AO_CAP_MODE_MONO); + } + + /* if the audio still isn't open, do not go any further with the decode */ + if (!this->output_open) + return; + + /* accumulate the data passed through the buffer element type; increase + * the accumulator buffer size as necessary */ + if( this->size + buf->size > this->bufsize ) { + this->bufsize = this->size + 2 * buf->size; + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "fooaudio: increasing source buffer to %d to avoid overflow.\n", this->bufsize); + this->buf = realloc( this->buf, this->bufsize ); + } + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + /* When a buffer element type has the BUF_FLAG_FRAME_END flag set, it is + * time to decode the data in the buffer. */ + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + + /* This is where the real meat of the audio decoder is implemented. + * The general strategy is to decode the data in the accumulation buffer + * into raw PCM data and then dispatch the PCM to the engine in smaller + * buffers. What follows in the inside of this scope is the meat of + * this particular audio decoder. */ + + /* Operation of the fooaudio decoder: + * This decoder generates a continuous sine pattern based on the pts + * values sent by the xine engine. Two pts values are needed to know + * how long to make the audio. Thus, If this is the first frame or + * a seek has occurred (indicated by this->last_pts = -1), + * log the pts but do not create any audio. + * + * When a valid pts delta is generated, create n audio samples, where + * n is given as: + * + * n pts delta + * ----------- = --------- => n = (pts delta * sample rate) / 90000 + * sample rate 90000 + * + */ + + if (this->last_pts != -1) { + + /* no real reason to set this variable to 0 first; I just wanted the + * novelty of using all 4 basic arithmetic ops in a row (+ - * /) */ + samples_to_generate = 0; + samples_to_generate += buf->pts; + samples_to_generate -= this->last_pts; + samples_to_generate *= this->sample_rate; + samples_to_generate /= 90000; + + /* save the pts now since it will likely be trashed later */ + this->last_pts = buf->pts; + + while (samples_to_generate) { + + /* get an audio buffer */ + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + if (audio_buffer->mem_size == 0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "fooaudio: Help! Allocated audio buffer with nothing in it!\n"); + return; + } + + /* samples_to_generate is a sample count; mem_size is a byte count */ + if (samples_to_generate > audio_buffer->mem_size / 2) + samples_to_send = audio_buffer->mem_size / 2; + else + samples_to_send = samples_to_generate; + samples_to_generate -= samples_to_send; + +#define WAVE_HZ 300 + /* fill up the samples in the buffer */ + for (i = 0; i < samples_to_send; i++) + audio_buffer->mem[i] = + (short)(sin(2 * M_PI * this->iteration++ / WAVE_HZ) * 32767); + + /* final prep for audio buffer dispatch */ + audio_buffer->num_frames = samples_to_send; + audio_buffer->vpts = buf->pts; + buf->pts = 0; /* only first buffer gets the real pts */ + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + + } + } else { + /* log the pts for the next time */ + this->last_pts = buf->pts; + } + + /* reset data accumulation buffer */ + this->size = 0; + } +} + +/* This function resets the state of the audio decoder. This usually + * entails resetting the data accumulation buffer. */ +static void fooaudio_reset (audio_decoder_t *this_gen) { + + fooaudio_decoder_t *this = (fooaudio_decoder_t *) this_gen; + + this->size = 0; + + /* this is specific to fooaudio */ + this->last_pts = -1; +} + +/* This function resets the last pts value of the audio decoder. */ +static void fooaudio_discontinuity (audio_decoder_t *this_gen) { + + fooaudio_decoder_t *this = (fooaudio_decoder_t *) this_gen; + + /* this is specific to fooaudio */ + this->last_pts = -1; +} + +/* This function closes the audio output and frees the private audio decoder + * structure. */ +static void fooaudio_dispose (audio_decoder_t *this_gen) { + + fooaudio_decoder_t *this = (fooaudio_decoder_t *) this_gen; + + /* close the audio output */ + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + /* free anything that was allocated during operation */ + free(this->buf); + free(this); +} + +/* This function allocates, initializes, and returns a private audio + * decoder structure. */ +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + fooaudio_decoder_t *this ; + + this = (fooaudio_decoder_t *) xine_xmalloc (sizeof (fooaudio_decoder_t)); + + /* connect the member functions */ + this->audio_decoder.decode_data = fooaudio_decode_data; + this->audio_decoder.reset = fooaudio_reset; + this->audio_decoder.discontinuity = fooaudio_discontinuity; + this->audio_decoder.dispose = fooaudio_dispose; + + /* connect the stream */ + this->stream = stream; + + /* audio output is not open at the start */ + this->output_open = 0; + + /* initialize the basic audio parameters */ + this->channels = 0; + this->sample_rate = 0; + this->bits_per_sample = 0; + + /* initialize the data accumulation buffer */ + this->buf = NULL; + this->bufsize = 0; + this->size = 0; + + /* return the newly-initialized audio decoder */ + return &this->audio_decoder; +} + +/* This function frees the audio decoder class and any other memory that was + * allocated. */ +static void dispose_class (audio_decoder_class_t *this_gen) { + + fooaudio_class_t *this = (fooaudio_class_t *)this_gen; + + free (this); +} + +/* This function allocates a private audio decoder class and initializes + * the class's member functions. */ +static void *init_plugin (xine_t *xine, void *data) { + + fooaudio_class_t *this ; + + this = (fooaudio_class_t *) xine_malloc (sizeof (fooaudio_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "fooaudio"; + this->decoder_class.description = N_("fooaudio: reference xine audio decoder plugin"); + this->decoder_class.dispose = dispose_class; + + return this; +} + +/* This is a list of all of the internal xine audio buffer types that + * this decoder is able to handle. Check src/xine-engine/buffer.h for a + * list of valid buffer types (and add a new one if the one you need does + * not exist). Terminate the list with a 0. */ +static const uint32_t audio_types[] = { + /* BUF_AUDIO_FOO, */ + 0 +}; + +/* This data structure combines the list of supported xine buffer types and + * the priority that the plugin should be given with respect to other + * plugins that handle the same buffer type. A plugin with priority (n+1) + * will be used instead of a plugin with priority (n). */ +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 5 /* priority */ +}; + +/* The plugin catalog entry. This is the only information that this plugin + * will export to the public. */ +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* { type, API version, "name", version, special_info, init_function }, */ + { PLUGIN_AUDIO_DECODER, 16, "fooaudio", XINE_VERSION_CODE, &dec_info_audio, &init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; + diff --git a/src/audio_dec/gsm610.c b/src/audio_dec/gsm610.c new file mode 100644 index 000000000..a0226638a --- /dev/null +++ b/src/audio_dec/gsm610.c @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2000-2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + * + * GSM 6.10 Audio Decoder + * This decoder is based on the GSM 6.10 codec library found at: + * http://kbs.cs.tu-berlin.de/~jutta/toast.html + * Additionally, here is an article regarding the software that appeared + * in Dr. Dobbs Journal: + * http://www.ddj.com/documents/s=1012/ddj9412b/9412b.htm + * + * This is the notice that comes with the software: + * -------------------------------------------------------------------- + * Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann, + * Technische Universitaet Berlin + * + * Any use of this software is permitted provided that this notice is not + * removed and that neither the authors nor the Technische Universitaet Berlin + * are deemed to have made any representations as to the suitability of this + * software for any purpose nor are held responsible for any defects of + * this software. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. + * + * As a matter of courtesy, the authors request to be informed about uses + * this software has found, about bugs in this software, and about any + * improvements that may be of general interest. + * + * Berlin, 28.11.1994 + * Jutta Degener + * Carsten Bormann + * -------------------------------------------------------------------- + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdlib.h> +#include <string.h> + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> +#include <xine/xineutils.h> +#include "bswap.h" + +#include "private.h" +#include "gsm.h" + +#define AUDIOBUFSIZE 128*1024 + +#define GSM610_SAMPLE_SIZE 16 +#define GSM610_BLOCK_SIZE 160 + +typedef struct { + audio_decoder_class_t decoder_class; +} gsm610_class_t; + +typedef struct gsm610_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + unsigned int buf_type; + int output_open; + int sample_rate; + + unsigned char *buf; + int bufsize; + int size; + + gsm gsm_state; + +} gsm610_decoder_t; + +/************************************************************************** + * xine audio plugin functions + *************************************************************************/ + +static void gsm610_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + gsm610_decoder_t *this = (gsm610_decoder_t *) this_gen; + audio_buffer_t *audio_buffer; + int in_ptr; + + if (buf->decoder_flags & BUF_FLAG_STDHEADER) { + this->sample_rate = buf->decoder_info[1]; + + this->buf = xine_xmalloc(AUDIOBUFSIZE); + this->bufsize = AUDIOBUFSIZE; + this->size = 0; + + /* stream/meta info */ + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "GSM 6.10"); + + return; + } + + if (!this->output_open) { + + this->gsm_state = gsm_create(); + this->buf_type = buf->type; + + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, + this->stream, GSM610_SAMPLE_SIZE, this->sample_rate, AO_CAP_MODE_MONO); + } + + /* if the audio still isn't open, bail */ + if (!this->output_open) + return; + + if( this->size + buf->size > this->bufsize ) { + this->bufsize = this->size + 2 * buf->size; + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "gsm610: increasing source buffer to %d to avoid overflow.\n", this->bufsize); + this->buf = realloc( this->buf, this->bufsize ); + } + + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { /* time to decode a frame */ + int16_t decode_buffer[GSM610_BLOCK_SIZE]; + + /* handle the Microsoft variant of GSM data */ + if (this->buf_type == BUF_AUDIO_MSGSM) { + + this->gsm_state->wav_fmt = 1; + + /* the data should line up on a 65-byte boundary */ + if ((buf->size % 65) != 0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "gsm610: received MS GSM block that does not line up\n"); + this->size = 0; + return; + } + + in_ptr = 0; + while (this->size) { + gsm_decode(this->gsm_state, &this->buf[in_ptr], decode_buffer); + if ((in_ptr % 65) == 0) { + in_ptr += 33; + this->size -= 33; + } else { + in_ptr += 32; + this->size -= 32; + } + + /* dispatch the decoded audio; assume that the audio buffer will + * always contain at least 160 samples */ + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + + xine_fast_memcpy(audio_buffer->mem, decode_buffer, + GSM610_BLOCK_SIZE * 2); + audio_buffer->num_frames = GSM610_BLOCK_SIZE; + + audio_buffer->vpts = buf->pts; + buf->pts = 0; /* only first buffer gets the real pts */ + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + } + } else { + + /* handle the other variant, which consists of 33-byte blocks */ + this->gsm_state->wav_fmt = 0; + + /* the data should line up on a 33-byte boundary */ + if ((buf->size % 33) != 0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "gsm610: received GSM block that does not line up\n"); + this->size = 0; + return; + } + + in_ptr = 0; + while (this->size) { + gsm_decode(this->gsm_state, &this->buf[in_ptr], decode_buffer); + in_ptr += 33; + this->size -= 33; + + /* dispatch the decoded audio; assume that the audio buffer will + * always contain at least 160 samples */ + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + + xine_fast_memcpy(audio_buffer->mem, decode_buffer, + GSM610_BLOCK_SIZE * 2); + audio_buffer->num_frames = GSM610_BLOCK_SIZE; + + audio_buffer->vpts = buf->pts; + buf->pts = 0; /* only first buffer gets the real pts */ + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + } + } + } +} + +static void gsm610_reset (audio_decoder_t *this_gen) { +} + +static void gsm610_discontinuity (audio_decoder_t *this_gen) { +} + +static void gsm610_dispose (audio_decoder_t *this_gen) { + + gsm610_decoder_t *this = (gsm610_decoder_t *) this_gen; + + if (this->gsm_state) + gsm_destroy(this->gsm_state); + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + if (this->buf) + free(this->buf); + + free (this_gen); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + gsm610_decoder_t *this ; + + this = (gsm610_decoder_t *) xine_xmalloc (sizeof (gsm610_decoder_t)); + + this->audio_decoder.decode_data = gsm610_decode_data; + this->audio_decoder.reset = gsm610_reset; + this->audio_decoder.discontinuity = gsm610_discontinuity; + this->audio_decoder.dispose = gsm610_dispose; + + this->output_open = 0; + this->sample_rate = 0; + this->stream = stream; + this->buf = NULL; + this->size = 0; + + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + + gsm610_class_t *this ; + + this = (gsm610_class_t *) xine_xmalloc (sizeof (gsm610_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "GSM 6.10"; + this->decoder_class.description = N_("GSM 6.10 audio decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_MSGSM, + BUF_AUDIO_GSM610, + 0 +}; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 9 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 16, "gsm610", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_a52_decoder.c b/src/audio_dec/xine_a52_decoder.c new file mode 100644 index 000000000..4928c38d0 --- /dev/null +++ b/src/audio_dec/xine_a52_decoder.c @@ -0,0 +1,866 @@ +/* + * Copyright (C) 2000-2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + * + * stuff needed to turn liba52 into a xine decoder plugin + */ + +#ifndef __sun +/* required for swab() */ +#define _XOPEN_SOURCE 500 +#endif +/* avoid compiler warnings */ +#define _BSD_SOURCE 1 + +#include <config.h> + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <assert.h> + +#define LOG_MODULE "a52_decoder" +#define LOG_VERBOSE +/* +#define LOG +#define LOG_PTS +*/ + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> + +#ifdef HAVE_A52DEC_A52_H +# include <a52dec/a52.h> +#else +# include "a52.h" +#endif + +#ifdef HAVE_A52DEC_A52_INTERNAL_H +# include <a52dec/a52_internal.h> +#else +# include "a52_internal.h" +#endif + +#include <xine/buffer.h> +#include <xine/xineutils.h> + +#include "../../contrib/a52dec/crc.c" + +#undef DEBUG_A52 +#ifdef DEBUG_A52 +int a52file; +#endif + +typedef struct { + audio_decoder_class_t decoder_class; + config_values_t *config; + + float a52_level; + int disable_dynrng_compress; + int enable_surround_downmix; + +} a52dec_class_t; + +typedef struct a52dec_decoder_s { + audio_decoder_t audio_decoder; + + a52dec_class_t *class; + xine_stream_t *stream; + int64_t pts; + int64_t pts_list[5]; + int32_t pts_list_position; + + uint8_t frame_buffer[3840]; + uint8_t *frame_ptr; + int sync_state; + int frame_length, frame_todo; + uint16_t syncword; + + a52_state_t *a52_state; + int a52_flags; + int a52_bit_rate; + int a52_sample_rate; + int have_lfe; + + int a52_flags_map[11]; + int ao_flags_map[11]; + + int audio_caps; + int bypass_mode; + int output_sampling_rate; + int output_open; + int output_mode; + +} a52dec_decoder_t; + +struct frmsize_s +{ + uint16_t bit_rate; + uint16_t frm_size[3]; +}; + +static const struct frmsize_s frmsizecod_tbl[64] = +{ + { 32 ,{64 ,69 ,96 } }, + { 32 ,{64 ,70 ,96 } }, + { 40 ,{80 ,87 ,120 } }, + { 40 ,{80 ,88 ,120 } }, + { 48 ,{96 ,104 ,144 } }, + { 48 ,{96 ,105 ,144 } }, + { 56 ,{112 ,121 ,168 } }, + { 56 ,{112 ,122 ,168 } }, + { 64 ,{128 ,139 ,192 } }, + { 64 ,{128 ,140 ,192 } }, + { 80 ,{160 ,174 ,240 } }, + { 80 ,{160 ,175 ,240 } }, + { 96 ,{192 ,208 ,288 } }, + { 96 ,{192 ,209 ,288 } }, + { 112 ,{224 ,243 ,336 } }, + { 112 ,{224 ,244 ,336 } }, + { 128 ,{256 ,278 ,384 } }, + { 128 ,{256 ,279 ,384 } }, + { 160 ,{320 ,348 ,480 } }, + { 160 ,{320 ,349 ,480 } }, + { 192 ,{384 ,417 ,576 } }, + { 192 ,{384 ,418 ,576 } }, + { 224 ,{448 ,487 ,672 } }, + { 224 ,{448 ,488 ,672 } }, + { 256 ,{512 ,557 ,768 } }, + { 256 ,{512 ,558 ,768 } }, + { 320 ,{640 ,696 ,960 } }, + { 320 ,{640 ,697 ,960 } }, + { 384 ,{768 ,835 ,1152 } }, + { 384 ,{768 ,836 ,1152 } }, + { 448 ,{896 ,975 ,1344 } }, + { 448 ,{896 ,976 ,1344 } }, + { 512 ,{1024 ,1114 ,1536 } }, + { 512 ,{1024 ,1115 ,1536 } }, + { 576 ,{1152 ,1253 ,1728 } }, + { 576 ,{1152 ,1254 ,1728 } }, + { 640 ,{1280 ,1393 ,1920 } }, + { 640 ,{1280 ,1394 ,1920 } } +}; + +/* config callbacks */ +static void a52_level_change_cb(void *this_gen, xine_cfg_entry_t *entry); +static void dynrng_compress_change_cb(void *this_gen, xine_cfg_entry_t *entry); +static void surround_downmix_change_cb(void *this_gen, xine_cfg_entry_t *entry); + + +static void a52dec_reset (audio_decoder_t *this_gen) { + + a52dec_decoder_t *this = (a52dec_decoder_t *) this_gen; + + this->syncword = 0; + this->sync_state = 0; + this->pts = 0; + this->pts_list[0] = 0; + this->pts_list_position = 0; +} + +static void a52dec_discontinuity (audio_decoder_t *this_gen) { + + a52dec_decoder_t *this = (a52dec_decoder_t *) this_gen; + + this->pts = 0; + this->pts_list[0] = 0; + this->pts_list_position = 0; +} + +static inline int16_t blah (int32_t i) { + + if (i > 0x43c07fff) + return 32767; + else if (i < 0x43bf8000) + return -32768; + else + return i - 0x43c00000; +} + +static inline void float_to_int (float * _f, int16_t * s16, int num_channels) { + int i; + int32_t * f = (int32_t *) _f; /* XXX assumes IEEE float format */ + + for (i = 0; i < 256; i++) { + s16[num_channels*i] = blah (f[i]); + } +} + +static inline void mute_channel (int16_t * s16, int num_channels) { + int i; + + for (i = 0; i < 256; i++) { + s16[num_channels*i] = 0; + } +} + +static void a52dec_decode_frame (a52dec_decoder_t *this, int64_t pts, int preview_mode) { + + int output_mode = AO_CAP_MODE_STEREO; + + /* + * do we want to decode this frame in software? + */ +#ifdef LOG_PTS + printf("a52dec:decode_frame:pts=%lld\n",pts); +#endif + if (!this->bypass_mode) { + + int a52_output_flags, i; + sample_t level = this->class->a52_level; + audio_buffer_t *buf; + int16_t *int_samples; + sample_t *samples = a52_samples(this->a52_state); + + /* + * oki, decode this frame in software + */ + + /* determine output mode */ + + a52_output_flags = this->a52_flags_map[this->a52_flags & A52_CHANNEL_MASK]; + + if (a52_frame (this->a52_state, + this->frame_buffer, + &a52_output_flags, + &level, 384)) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "liba52: a52_frame error\n"); + return; + } + + if (this->class->disable_dynrng_compress) + a52_dynrng (this->a52_state, NULL, NULL); + + this->have_lfe = a52_output_flags & A52_LFE; + if (this->have_lfe) + if (this->audio_caps & AO_CAP_MODE_5_1CHANNEL) { + output_mode = AO_CAP_MODE_5_1CHANNEL; + } else if (this->audio_caps & AO_CAP_MODE_4_1CHANNEL) { + output_mode = AO_CAP_MODE_4_1CHANNEL; + } else { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "liba52: WHAT DO I DO!!!\n"); + output_mode = this->ao_flags_map[a52_output_flags]; + } + else + output_mode = this->ao_flags_map[a52_output_flags]; + /* + * (re-)open output device + */ + + if (!this->output_open + || (this->a52_sample_rate != this->output_sampling_rate) + || (output_mode != this->output_mode)) { + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, + this->stream, 16, + this->a52_sample_rate, + output_mode) ; + this->output_sampling_rate = this->a52_sample_rate; + this->output_mode = output_mode; + } + + + if (!this->output_open || preview_mode) + return; + + + /* + * decode a52 and convert/interleave samples + */ + + buf = this->stream->audio_out->get_buffer (this->stream->audio_out); + int_samples = buf->mem; + buf->num_frames = 256*6; + + for (i = 0; i < 6; i++) { + if (a52_block (this->a52_state)) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "liba52: a52_block error on audio channel %d\n", i); +#if 0 + for(n=0;n<2000;n++) { + printf("%02x ",this->frame_buffer[n]); + if ((n % 32) == 0) printf("\n"); + } + printf("\n"); +#endif + buf->num_frames = 0; + break; + } + + switch (output_mode) { + case AO_CAP_MODE_MONO: + float_to_int (&samples[0], int_samples+(i*256), 1); + break; + case AO_CAP_MODE_STEREO: + float_to_int (&samples[0*256], int_samples+(i*256*2), 2); + float_to_int (&samples[1*256], int_samples+(i*256*2)+1, 2); + break; + case AO_CAP_MODE_4CHANNEL: + float_to_int (&samples[0*256], int_samples+(i*256*4), 4); /* L */ + float_to_int (&samples[1*256], int_samples+(i*256*4)+1, 4); /* R */ + float_to_int (&samples[2*256], int_samples+(i*256*4)+2, 4); /* RL */ + float_to_int (&samples[3*256], int_samples+(i*256*4)+3, 4); /* RR */ + break; + case AO_CAP_MODE_4_1CHANNEL: + float_to_int (&samples[0*256], int_samples+(i*256*6)+5, 6); /* LFE */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+3, 6); /* RR */ + mute_channel ( int_samples+(i*256*6)+4, 6); /* C */ + break; + case AO_CAP_MODE_5CHANNEL: + float_to_int (&samples[0*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+4, 6); /* C */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+3, 6); /* RR */ + mute_channel ( int_samples+(i*256*6)+5, 6); /* LFE */ + break; + case AO_CAP_MODE_5_1CHANNEL: + float_to_int (&samples[0*256], int_samples+(i*256*6)+5, 6); /* lfe */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+4, 6); /* C */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[5*256], int_samples+(i*256*6)+3, 6); /* RR */ + break; + default: + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "liba52: help - unsupported mode %08x\n", output_mode); + } + } + + lprintf ("%d frames output\n", buf->num_frames); + + /* output decoded samples */ + + buf->vpts = pts; + + this->stream->audio_out->put_buffer (this->stream->audio_out, buf, this->stream); + + } else { + + /* + * loop through a52 data + */ + + if (!this->output_open) { + + int sample_rate, bit_rate, flags; + + a52_syncinfo (this->frame_buffer, &flags, &sample_rate, &bit_rate); + + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, + this->stream, 16, + sample_rate, + AO_CAP_MODE_A52) ; + this->output_mode = AO_CAP_MODE_A52; + } + + if (this->output_open && !preview_mode) { + /* SPDIF Passthrough + * Build SPDIF Header and encaps the A52 audio data in it. + */ + uint32_t syncword, crc1, fscod,frmsizecod,bsid,bsmod,frame_size; + uint8_t *data_out,*data_in; + audio_buffer_t *buf = this->stream->audio_out->get_buffer (this->stream->audio_out); + data_in=(uint8_t *) this->frame_buffer; + data_out=(uint8_t *) buf->mem; + syncword = data_in[0] | (data_in[1] << 8); + crc1 = data_in[2] | (data_in[3] << 8); + fscod = (data_in[4] >> 6) & 0x3; + frmsizecod = data_in[4] & 0x3f; + bsid = (data_in[5] >> 3) & 0x1f; + bsmod = data_in[5] & 0x7; /* bsmod, stream = 0 */ + frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] ; + + data_out[0] = 0x72; data_out[1] = 0xf8; /* spdif syncword */ + data_out[2] = 0x1f; data_out[3] = 0x4e; /* .............. */ + data_out[4] = 0x01; /* AC3 data */ + data_out[5] = bsmod; /* bsmod, stream = 0 */ + data_out[6] = (frame_size << 4) & 0xff; /* frame_size * 16 */ + data_out[7] = ((frame_size ) >> 4) & 0xff; + swab(data_in, &data_out[8], frame_size * 2 ); + + buf->num_frames = 1536; + buf->vpts = pts; + + this->stream->audio_out->put_buffer (this->stream->audio_out, buf, this->stream); + + } + } +} + +static void a52dec_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + a52dec_decoder_t *this = (a52dec_decoder_t *) this_gen; + uint8_t *current = buf->content; + uint8_t *sync_start=current + 1; + uint8_t *end = buf->content + buf->size; + uint8_t byte; + int32_t n; + uint16_t crc16; + uint16_t crc16_result; + + lprintf ("decode data %d bytes of type %08x, pts=%"PRId64"\n", + buf->size, buf->type, buf->pts); + lprintf ("decode data decoder_info=%d, %d\n",buf->decoder_info[1],buf->decoder_info[2]); + + if (buf->decoder_flags & BUF_FLAG_HEADER) + return; + + /* swap byte pairs if this is RealAudio DNET data */ + if (buf->type == BUF_AUDIO_DNET) { + + lprintf ("byte-swapping dnet\n"); + + while (current != end) { + byte = *current++; + *(current - 1) = *current; + *current++ = byte; + } + + /* reset */ + current = buf->content; + end = buf->content + buf->size; + } + + /* A52 packs come from the DVD in blocks of about 2048 bytes. + * Only 1 PTS values can be assigned to each block. + * An A52 frame is about 1700 bytes long. + * So, a single A52 packs can contain 2 A52 frames (or the beginning of an A52 frame at least). + * If we have a PTS value, which A52 frame does it apply to? The A52 pack tells us that. + * So, the info about which A52 frame the PTS applies to is contained in decoder_info sent from the demuxer. + * + * The PTS value from the A52 pack (DVD sector) can only be applied at the start of an A52 frame. + * We call the start of an A52 frame a frame header. + * So, if a A52 pack has 2 "Number of frame headers" is means that the A52 pack contains 2 A52 frame headers. + * The "First access unit" then tells us which A52 frame the PTS value applies to. + * + * Take the following example: - + * PACK1: PTS = 10. Contains the entire A52 frame1, followed by the beginning of the frame2. PTS applies to frame1. + * PACK2: PTS = 1000, Contains the rest of frame2, and the whole of frame3. and the start of frame4. PTS applies to frame4. + * PACK3: PTS = 0 (none), Contains the rest of frame4. + * + * Output should be: - + * frame1, PTS=10 + * frame2, PTS=0 + * frame3, PTS=0 + * frame4, PTS=1000 + * + * So, we have to keep track of PTS values from previous A52 packs here, otherwise they get put on the wrong frame. + */ + + + /* FIXME: the code here does not match the explanation above */ + if (buf->pts) { + int32_t info; + info = buf->decoder_info[1]; + this->pts = buf->pts; + this->pts_list[this->pts_list_position]=buf->pts; + this->pts_list_position++; + if( this->pts_list_position > 3 ) + this->pts_list_position = 3; + if (info == 2) { + this->pts_list[this->pts_list_position]=0; + this->pts_list_position++; + if( this->pts_list_position > 3 ) + this->pts_list_position = 3; + } + } +#if 0 + for(n=0;n < buf->size;n++) { + if ((n % 32) == 0) printf("\n"); + printf("%x ", current[n]); + } + printf("\n"); +#endif + + lprintf ("processing...state %d\n", this->sync_state); + + while (current < end) { + switch (this->sync_state) { + case 0: /* Looking for sync header */ + this->syncword = (this->syncword << 8) | *current++; + if (this->syncword == 0x0b77) { + + this->frame_buffer[0] = 0x0b; + this->frame_buffer[1] = 0x77; + + this->sync_state = 1; + this->frame_ptr = this->frame_buffer+2; + } + break; + + case 1: /* Looking for enough bytes for sync_info. */ + sync_start = current - 1; + *this->frame_ptr++ = *current++; + if ((this->frame_ptr - this->frame_buffer) > 16) { + int a52_flags_old = this->a52_flags; + int a52_sample_rate_old = this->a52_sample_rate; + int a52_bit_rate_old = this->a52_bit_rate; + + this->frame_length = a52_syncinfo (this->frame_buffer, + &this->a52_flags, + &this->a52_sample_rate, + &this->a52_bit_rate); + + if (this->frame_length < 80) { /* Invalid a52 frame_length */ + this->syncword = 0; + current = sync_start; + this->sync_state = 0; + break; + } + + lprintf("Frame length = %d\n",this->frame_length); + + this->frame_todo = this->frame_length - 17; + this->sync_state = 2; + if (!_x_meta_info_get(this->stream, XINE_META_INFO_AUDIOCODEC) || + a52_flags_old != this->a52_flags || + a52_sample_rate_old != this->a52_sample_rate || + a52_bit_rate_old != this->a52_bit_rate) { + + switch (this->a52_flags & A52_CHANNEL_MASK) { + case A52_3F2R: + if (this->a52_flags & A52_LFE) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 5.1"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 5.0"); + break; + case A52_3F1R: + case A52_2F2R: + if (this->a52_flags & A52_LFE) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 4.1"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 4.0"); + break; + case A52_2F1R: + case A52_3F: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 3.0"); + break; + case A52_STEREO: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 2.0 (stereo)"); + break; + case A52_DOLBY: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 2.0 (dolby)"); + break; + case A52_MONO: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52 1.0"); + break; + default: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "A/52"); + break; + } + + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, this->a52_bit_rate); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE, this->a52_sample_rate); + } + } + break; + + case 2: /* Filling frame_buffer with sync_info bytes */ + *this->frame_ptr++ = *current++; + this->frame_todo--; + if (this->frame_todo < 1) { + this->sync_state = 3; + } else break; + + case 3: /* Ready for decode */ + crc16 = (uint16_t) ((this->frame_buffer[2] << 8) | this->frame_buffer[3]) ; + crc16_result = crc16_block(&this->frame_buffer[2], this->frame_length - 2) ; /* frame_length */ + if (crc16_result != 0) { /* CRC16 failed */ + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "liba52:a52 frame failed crc16 checksum.\n"); + current = sync_start; + this->pts = 0; + this->syncword = 0; + this->sync_state = 0; + break; + } +#if 0 + a52dec_decode_frame (this, this->pts_list[0], buf->decoder_flags & BUF_FLAG_PREVIEW); +#else + a52dec_decode_frame (this, this->pts, buf->decoder_flags & BUF_FLAG_PREVIEW); +#endif + for(n=0;n<4;n++) { + this->pts_list[n] = this->pts_list[n+1]; + } + this->pts_list_position--; + if( this->pts_list_position < 0 ) + this->pts_list_position = 0; +#if 0 + printf("liba52: pts_list = %lld, %lld, %lld\n", + this->pts_list[0], + this->pts_list[1], + this->pts_list[2]); +#endif + case 4: /* Clear up ready for next frame */ + this->pts = 0; + this->syncword = 0; + this->sync_state = 0; + break; + default: /* No come here */ + break; + } + } + +#ifdef DEBUG_A52 + write (a52file, this->frame_buffer, this->frame_length); +#endif +} + +static void a52dec_dispose (audio_decoder_t *this_gen) { + + a52dec_decoder_t *this = (a52dec_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + this->output_open = 0; + + a52_free(this->a52_state); + this->a52_state = NULL; + +#ifdef DEBUG_A52 + close (a52file); +#endif + free (this_gen); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + a52dec_decoder_t *this ; + + lprintf ("open_plugin called\n"); + + this = (a52dec_decoder_t *) xine_xmalloc (sizeof (a52dec_decoder_t)); + + this->audio_decoder.decode_data = a52dec_decode_data; + this->audio_decoder.reset = a52dec_reset; + this->audio_decoder.discontinuity = a52dec_discontinuity; + this->audio_decoder.dispose = a52dec_dispose; + this->stream = stream; + this->class = (a52dec_class_t *) class_gen; + + /* int i; */ + + this->audio_caps = stream->audio_out->get_capabilities(stream->audio_out); + this->syncword = 0; + this->sync_state = 0; + this->output_open = 0; + this->pts = 0; + this->pts_list[0] = 0; + this->pts_list_position = 0; + + if( !this->a52_state ) { + this->a52_state = +#ifdef HAVE_A52DEC_A52_H /* External liba52 */ + /* When using external liba52, enable _all_ capabilities, even + if that might break stuff if they add some new capability + that depends on CPU's caps. + At the moment the only capability is DJBFFT, which is tested + only if djbfft is being used at compile time. + + The actual question would be: why don't they check for + capabilities themselves? + */ +#warning "Enabling all external liba52 capabilities." + a52_init (0xFFFFFFFF) +#else + a52_init (xine_mm_accel()) +#endif + ; + } + + /* + * find out if this driver supports a52 output + * or, if not, how many channels we've got + */ + + if (this->audio_caps & AO_CAP_MODE_A52) + this->bypass_mode = 1; + else { + this->bypass_mode = 0; + + this->a52_flags_map[A52_MONO] = A52_MONO; + this->a52_flags_map[A52_STEREO] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_3F] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_2F1R] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_3F1R] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_2F2R] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_3F2R] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + this->a52_flags_map[A52_DOLBY] = ((this->class->enable_surround_downmix ? A52_DOLBY : A52_STEREO)); + + this->ao_flags_map[A52_MONO] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_STEREO] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_3F] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_2F1R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_3F1R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[A52_DOLBY] = AO_CAP_MODE_STEREO; + + /* find best mode */ + if (this->audio_caps & AO_CAP_MODE_5_1CHANNEL) { + + this->a52_flags_map[A52_2F2R] = A52_2F2R; + this->a52_flags_map[A52_3F2R] = A52_3F2R | A52_LFE; + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_5CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_5CHANNEL) { + + this->a52_flags_map[A52_2F2R] = A52_2F2R; + this->a52_flags_map[A52_3F2R] = A52_3F2R; + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_5CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_4_1CHANNEL) { + + this->a52_flags_map[A52_2F2R] = A52_2F2R; + this->a52_flags_map[A52_3F2R] = A52_2F2R | A52_LFE; + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_4CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_4CHANNEL) { + + this->a52_flags_map[A52_2F2R] = A52_2F2R; + this->a52_flags_map[A52_3F2R] = A52_2F2R; + + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_4CHANNEL; + + /* else if (this->audio_caps & AO_CAP_MODE_STEREO) + defaults are ok */ + } else if (!(this->audio_caps & AO_CAP_MODE_STEREO)) { + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, _("HELP! a mono-only audio driver?!\n")); + + this->a52_flags_map[A52_MONO] = A52_MONO; + this->a52_flags_map[A52_STEREO] = A52_MONO; + this->a52_flags_map[A52_3F] = A52_MONO; + this->a52_flags_map[A52_2F1R] = A52_MONO; + this->a52_flags_map[A52_3F1R] = A52_MONO; + this->a52_flags_map[A52_2F2R] = A52_MONO; + this->a52_flags_map[A52_3F2R] = A52_MONO; + this->a52_flags_map[A52_DOLBY] = A52_MONO; + + this->ao_flags_map[A52_MONO] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_STEREO] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_3F] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_2F1R] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_3F1R] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_2F2R] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_3F2R] = AO_CAP_MODE_MONO; + this->ao_flags_map[A52_DOLBY] = AO_CAP_MODE_MONO; + } + } + + /* + for (i = 0; i<8; i++) + this->a52_flags_map[i] |= A52_ADJUST_LEVEL; + */ +#ifdef DEBUG_A52 + a52file = open ("test.a52", O_CREAT | O_WRONLY | O_TRUNC, 0644); +#endif + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + + a52dec_class_t *this; + config_values_t *cfg; + + this = (a52dec_class_t *) xine_xmalloc (sizeof (a52dec_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "a/52dec"; + this->decoder_class.description = N_("liba52 based a52 audio decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + cfg = this->config = xine->config; + + this->a52_level = (float) cfg->register_range (cfg, "audio.a52.level", 100, + 0, 200, + _("A/52 volume"), + _("With A/52 audio, you can modify the volume " + "at the decoder level. This has the advantage " + "of the audio being already decoded for the " + "specified volume, so later operations like " + "channel downmixing will work on an audio stream " + "of the given volume."), + 10, a52_level_change_cb, this) / 100.0; + this->disable_dynrng_compress = !cfg->register_bool (cfg, "audio.a52.dynamic_range", 0, + _("use A/52 dynamic range compression"), + _("Dynamic range compression limits the dynamic " + "range of the audio. This means making the loud " + "sounds softer, and the soft sounds louder, so you can " + "more easily listen to the audio in a noisy " + "environment without disturbing anyone."), + 0, dynrng_compress_change_cb, this); + this->enable_surround_downmix = cfg->register_bool (cfg, "audio.a52.surround_downmix", 0, + _("downmix audio to 2 channel surround stereo"), + _("When you want to listen to multichannel surround " + "sound, but you have only two speakers or a " + "surround decoder or amplifier which does some " + "sort of matrix surround decoding like prologic, " + "you should enable this option so that the " + "additional channels are mixed into the stereo " + "signal."), + 0, surround_downmix_change_cb, this); + lprintf ("init_plugin called\n"); + return this; +} + +static void a52_level_change_cb(void *this_gen, xine_cfg_entry_t *entry) +{ + ((a52dec_class_t *)this_gen)->a52_level = entry->num_value / 100.0; +} + +static void dynrng_compress_change_cb(void *this_gen, xine_cfg_entry_t *entry) +{ + ((a52dec_class_t *)this_gen)->disable_dynrng_compress = !entry->num_value; +} + +static void surround_downmix_change_cb(void *this_gen, xine_cfg_entry_t *entry) +{ + ((a52dec_class_t *)this_gen)->enable_surround_downmix = entry->num_value; +} + + +static const uint32_t audio_types[] = { + BUF_AUDIO_A52, + BUF_AUDIO_DNET, + 0 + }; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 5 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER | PLUGIN_MUST_PRELOAD, 16, "a/52", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_dts_decoder.c b/src/audio_dec/xine_dts_decoder.c new file mode 100644 index 000000000..2b8dabd10 --- /dev/null +++ b/src/audio_dec/xine_dts_decoder.c @@ -0,0 +1,578 @@ +/* + * Copyright (C) 2000-2007 the xine project + * + * This file is part of xine, a unix 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + */ + +/** + * @file + * @brief DTS decoder for xine + * + * @author Joachim Koenig (2001-09-04) + * @author James Courtier-Dutton (2001-12-09) + */ + +#ifndef __sun +/* required for swab() */ +#define _XOPEN_SOURCE 500 +#endif +/* avoid compiler warnings */ +#define _BSD_SOURCE 1 + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <assert.h> + +#define LOG_MODULE "libdts" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include <xine/xine_internal.h> +#include <xine/xineutils.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> + +#include "bswap.h" + +#include <dts.h> + +#define MAX_AC5_FRAME 4096 + +typedef struct { + audio_decoder_class_t decoder_class; +} dts_class_t; + +typedef struct { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + audio_decoder_class_t *class; + + dts_state_t *dts_state; + int64_t pts; + + int audio_caps; + int sync_state; + int ac5_length, ac5_pcm_length, frame_todo; + uint32_t syncdword; + uint8_t frame_buffer[MAX_AC5_FRAME]; + uint8_t *frame_ptr; + + int output_open; + + int bypass_mode; + int dts_flags; + int dts_sample_rate; + int dts_bit_rate; + int dts_flags_map[11]; /* Convert from stream dts_flags to the dts_flags we want from the dts downmixer */ + int ao_flags_map[11]; /* Convert from the xine AO_CAP's to dts_flags. */ + int have_lfe; + + +} dts_decoder_t; + +static void dts_reset (audio_decoder_t *const this_gen) { +} + +static void dts_discontinuity (audio_decoder_t *const this_gen) { +} + +/** + * @brief Convert a array of floating point samples into 16-bit signed integer samples + * @param f Floating point samples array (origin) + * @param s16 16-bit signed integer samples array (destination) + * @param num_channels Number of channels present in the stream + * + * @todo This same work is being done in many decoders to adapt the output of + * the decoder to what the audio output can actually use, this should be + * done by the audio_output loop, not by the decoders. + * @note This is subtly different from the function with the same name in xine_musepack_decoder.c + */ +static inline void float_to_int (const float *const _f, int16_t *const s16, const int num_channels) { + const int endidx = 256 * num_channels; + int i, j; + + for (i = 0, j = 0; j < endidx; i++, j += num_channels) { + const float f = _f[i] * 32767; + if (f > INT16_MAX) + s16[j] = INT16_MAX; + else if (f < INT16_MIN) + s16[j] = INT16_MIN; + else + s16[j] = f; + /* printf("samples[%d] = %f, %d\n", i, _f[i], s16[num_channels*i]); */ + } +} + +static inline void mute_channel (int16_t *const s16, const int num_channels) { + const int endidx = 256 * num_channels; + int i; + + for (i = 0; i < endidx; i += num_channels) + s16[i] = 0; +} + +static void dts_decode_frame (dts_decoder_t *this, const int64_t pts, const int preview_mode) { + + audio_buffer_t *audio_buffer; + uint32_t ac5_spdif_type=0; + int output_mode = AO_CAP_MODE_STEREO; + uint8_t *data_out; + uint8_t *const data_in = this->frame_buffer; + + lprintf("decode_frame\n"); + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + audio_buffer->vpts = pts; + + if(this->bypass_mode) { + /* SPDIF digital output */ + if (!this->output_open) { + this->output_open = ((this->stream->audio_out->open) (this->stream->audio_out, this->stream, + 16, this->dts_sample_rate, + AO_CAP_MODE_AC5)); + } + + if (!this->output_open) + return; + + data_out=(uint8_t *) audio_buffer->mem; + if (this->ac5_length > 8191) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "libdts: ac5_length too long\n"); + this->ac5_pcm_length = 0; + } + + switch (this->ac5_pcm_length) { + case 512: + ac5_spdif_type = 0x0b; /* DTS-1 (512-sample bursts) */ + break; + case 1024: + ac5_spdif_type = 0x0c; /* DTS-1 (1024-sample bursts) */ + break; + case 2048: + ac5_spdif_type = 0x0d; /* DTS-1 (2048-sample bursts) */ + break; + default: + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "libdts: DTS %i-sample bursts not supported\n", this->ac5_pcm_length); + return; + } + +#ifdef LOG_DEBUG + { + int i; + printf("libdts: DTS frame type=%d\n",data_in[4] >> 7); + printf("libdts: DTS deficit frame count=%d\n",(data_in[4] & 0x7f) >> 2); + printf("libdts: DTS AC5 PCM samples=%d\n",ac5_pcm_samples); + printf("libdts: DTS AC5 length=%d\n",this->ac5_length); + printf("libdts: DTS AC5 bitrate=%d\n",((data_in[8] & 0x03) << 4) | (data_in[8] >> 4)); + printf("libdts: DTS AC5 spdif type=%d\n", ac5_spdif_type); + + printf("libdts: "); + for(i=2000;i<2048;i++) { + printf("%02x ",data_in[i]); + } + printf("\n"); + } +#endif + + lprintf("length=%d pts=%"PRId64"\n",this->ac5_pcm_length,audio_buffer->vpts); + + audio_buffer->num_frames = this->ac5_pcm_length; + + data_out[0] = 0x72; data_out[1] = 0xf8; /* spdif syncword */ + data_out[2] = 0x1f; data_out[3] = 0x4e; /* .............. */ + data_out[4] = ac5_spdif_type; /* DTS data */ + data_out[5] = 0; /* Unknown */ + data_out[6] = (this->ac5_length << 3) & 0xff; /* ac5_length * 8 */ + data_out[7] = ((this->ac5_length ) >> 5) & 0xff; + + if( this->ac5_pcm_length ) { + if( this->ac5_pcm_length % 2) { + swab(data_in, &data_out[8], this->ac5_length ); + } else { + swab(data_in, &data_out[8], this->ac5_length + 1); + } + } + } else { + /* Software decode */ + int i, dts_output_flags; + int16_t *const int_samples = audio_buffer->mem; + int number_of_dts_blocks; + + level_t level = 1.0; + sample_t *samples; + + dts_output_flags = this->dts_flags_map[this->dts_flags & DTS_CHANNEL_MASK]; + + if(dts_frame(this->dts_state, data_in, &dts_output_flags, &level, 0)) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libdts: dts_frame error\n"); + return; + } + + this->have_lfe = dts_output_flags & DTS_LFE; + if (this->have_lfe) + if (this->audio_caps & AO_CAP_MODE_5_1CHANNEL) { + output_mode = AO_CAP_MODE_5_1CHANNEL; + } else if (this->audio_caps & AO_CAP_MODE_4_1CHANNEL) { + output_mode = AO_CAP_MODE_4_1CHANNEL; + } else { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "libdts: WHAT DO I DO!!!\n"); + output_mode = this->ao_flags_map[dts_output_flags & DTS_CHANNEL_MASK]; + } + else + output_mode = this->ao_flags_map[dts_output_flags & DTS_CHANNEL_MASK]; + + if (!this->output_open) { + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, this->stream, + 16, this->dts_sample_rate, + output_mode); + } + + if (!this->output_open) + return; + number_of_dts_blocks = dts_blocks_num (this->dts_state); + audio_buffer->num_frames = 256*number_of_dts_blocks; + for(i = 0; i < number_of_dts_blocks; i++) { + if(dts_block(this->dts_state)) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "libdts: dts_block error on audio channel %d\n", i); + audio_buffer->num_frames = 0; + break; + } + + samples = dts_samples(this->dts_state); + switch (output_mode) { + case AO_CAP_MODE_MONO: + float_to_int (&samples[0], int_samples+(i*256), 1); + break; + case AO_CAP_MODE_STEREO: + /* Tested, working. */ + float_to_int (&samples[0*256], int_samples+(i*256*2), 2); /* L */ + float_to_int (&samples[1*256], int_samples+(i*256*2)+1, 2); /* R */ + break; + case AO_CAP_MODE_4CHANNEL: + /* Tested, working */ + float_to_int (&samples[0*256], int_samples+(i*256*4), 4); /* L */ + float_to_int (&samples[1*256], int_samples+(i*256*4)+1, 4); /* R */ + float_to_int (&samples[2*256], int_samples+(i*256*4)+2, 4); /* RL */ + float_to_int (&samples[3*256], int_samples+(i*256*4)+3, 4); /* RR */ + break; + case AO_CAP_MODE_4_1CHANNEL: + /* Tested, working */ + float_to_int (&samples[0*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+3, 6); /* RR */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+5, 6); /* LFE */ + mute_channel ( int_samples+(i*256*6)+4, 6); /* C */ + break; + case AO_CAP_MODE_5CHANNEL: + /* Tested, working */ + float_to_int (&samples[0*256], int_samples+(i*256*6)+4, 6); /* C */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+3, 6); /* RR */ + mute_channel ( int_samples+(i*256*6)+5, 6); /* LFE */ + break; + case AO_CAP_MODE_5_1CHANNEL: + float_to_int (&samples[0*256], int_samples+(i*256*6)+4, 6); /* C */ + float_to_int (&samples[1*256], int_samples+(i*256*6)+0, 6); /* L */ + float_to_int (&samples[2*256], int_samples+(i*256*6)+1, 6); /* R */ + float_to_int (&samples[3*256], int_samples+(i*256*6)+2, 6); /* RL */ + float_to_int (&samples[4*256], int_samples+(i*256*6)+3, 6); /* RR */ + float_to_int (&samples[5*256], int_samples+(i*256*6)+5, 6); /* LFE */ /* Not working yet */ + break; + default: + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libdts: help - unsupported mode %08x\n", output_mode); + } + } + } + + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + + +} + +static void dts_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + dts_decoder_t *const this = (dts_decoder_t *) this_gen; + uint8_t *current = (uint8_t *)buf->content; + uint8_t *sync_start=current + 1; + uint8_t *const end = buf->content + buf->size; + + lprintf("decode_data\n"); + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + if (buf->decoder_flags & BUF_FLAG_STDHEADER) + return; + + lprintf ("processing...state %d\n", this->sync_state); + + while (current < end) { + switch (this->sync_state) { + case 0: /* Looking for sync header */ + this->syncdword = (this->syncdword << 8) | *current++; +/* + if ((this->syncdword == 0xff1f00e8) || + (this->syncdword == 0x1fffe800) || + (this->syncdword == 0xfe7f0180) || + (this->syncdword == 0x7ffe8001) ) { +*/ + + if ((this->syncdword == 0x7ffe8001) || (this->syncdword == 0xff1f00e8)) { + const uint32_t be_syncdword = be2me_32(this->syncdword); + + lprintf ("sync found: syncdword=0x%x\n", this->syncdword); + + memcpy(this->frame_buffer, &be_syncdword, sizeof(be_syncdword)); + + this->sync_state = 1; + this->frame_ptr = this->frame_buffer+4; + this->pts = buf->pts; + } + break; + + case 1: /* Looking for enough bytes for sync_info. */ + sync_start = current - 1; + *this->frame_ptr++ = *current++; + if ((this->frame_ptr - this->frame_buffer) > 19) { + const int old_dts_flags = this->dts_flags; + const int old_dts_sample_rate = this->dts_sample_rate; + const int old_dts_bit_rate = this->dts_bit_rate; + + this->ac5_length = dts_syncinfo (this->dts_state, this->frame_buffer, + &this->dts_flags, + &this->dts_sample_rate, + &this->dts_bit_rate, &(this->ac5_pcm_length)); + lprintf("ac5_length=%d\n",this->ac5_length); + lprintf("dts_sample_rate=%d\n",this->dts_sample_rate); + + if ( (this->ac5_length < 80) || (this->ac5_length > MAX_AC5_FRAME) ) { /* Invalid dts ac5_pcm_length */ + this->syncdword = 0; + current = sync_start; + this->sync_state = 0; + break; + } + + lprintf("Frame length = %d\n",this->ac5_pcm_length); + + this->frame_todo = this->ac5_length - 20; + this->sync_state = 2; + if (!_x_meta_info_get(this->stream, XINE_META_INFO_AUDIOCODEC) || + old_dts_flags != this->dts_flags || + old_dts_sample_rate != this->dts_sample_rate || + old_dts_bit_rate != this->dts_bit_rate) { + + switch (this->dts_flags & DTS_CHANNEL_MASK) { + case DTS_3F2R: + if (this->dts_flags & DTS_LFE) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 5.1"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 5.0"); + break; + case DTS_3F1R: + case DTS_2F2R: + if (this->dts_flags & DTS_LFE) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 4.1"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 4.0"); + break; + case DTS_2F1R: + case DTS_3F: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 3.0"); + break; + case DTS_STEREO: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 2.0 (stereo)"); + break; + case DTS_MONO: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS 1.0"); + break; + default: + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DTS"); + break; + } + + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, this->dts_bit_rate); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE, this->dts_sample_rate); + } + } + break; + + case 2: /* Filling frame_buffer with sync_info bytes */ + *this->frame_ptr++ = *current++; + this->frame_todo--; + if (this->frame_todo < 1) { + this->sync_state = 3; + } else break; + + case 3: /* Ready for decode */ +#if 0 + dtsdec_decode_frame (this, this->pts_list[0], buf->decoder_flags & BUF_FLAG_PREVIEW); +#else + dts_decode_frame (this, this->pts, buf->decoder_flags & BUF_FLAG_PREVIEW); +#endif + case 4: /* Clear up ready for next frame */ + this->pts = 0; + this->syncdword = 0; + this->sync_state = 0; + break; + default: /* No come here */ + break; + } + } +} + +static void dts_dispose (audio_decoder_t *this_gen) { + dts_decoder_t *const this = (dts_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + free (this); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + dts_decoder_t *this ; + + lprintf("open_plugin\n"); + + this = (dts_decoder_t *) xine_xmalloc (sizeof (dts_decoder_t)); + + this->audio_decoder.decode_data = dts_decode_data; + this->audio_decoder.reset = dts_reset; + this->audio_decoder.discontinuity = dts_discontinuity; + this->audio_decoder.dispose = dts_dispose; + + this->dts_state = dts_init(0); + this->audio_caps = stream->audio_out->get_capabilities(stream->audio_out); + if(this->audio_caps & AO_CAP_MODE_AC5) + this->bypass_mode = 1; + else { + this->bypass_mode = 0; + /* FIXME: Leave "DOLBY pro logic" downmix out for now. */ + this->dts_flags_map[DTS_MONO] = DTS_MONO; + this->dts_flags_map[DTS_STEREO] = DTS_STEREO; + this->dts_flags_map[DTS_3F] = DTS_STEREO; + this->dts_flags_map[DTS_2F1R] = DTS_STEREO; + this->dts_flags_map[DTS_3F1R] = DTS_STEREO; + this->dts_flags_map[DTS_2F2R] = DTS_STEREO; + this->dts_flags_map[DTS_3F2R] = DTS_STEREO; + + this->ao_flags_map[DTS_MONO] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_STEREO] = AO_CAP_MODE_STEREO; + this->ao_flags_map[DTS_3F] = AO_CAP_MODE_STEREO; + this->ao_flags_map[DTS_2F1R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[DTS_3F1R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_STEREO; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_STEREO; + + /* find best mode */ + if (this->audio_caps & AO_CAP_MODE_5_1CHANNEL) { + + this->dts_flags_map[DTS_2F2R] = DTS_2F2R; + this->dts_flags_map[DTS_3F2R] = DTS_3F2R | DTS_LFE; + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_5CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_5CHANNEL) { + + this->dts_flags_map[DTS_2F2R] = DTS_2F2R; + this->dts_flags_map[DTS_3F2R] = DTS_3F2R; + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_5CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_4_1CHANNEL) { + + this->dts_flags_map[DTS_2F2R] = DTS_2F2R; + this->dts_flags_map[DTS_3F2R] = DTS_2F2R | DTS_LFE; + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_4CHANNEL; + + } else if (this->audio_caps & AO_CAP_MODE_4CHANNEL) { + + this->dts_flags_map[DTS_2F2R] = DTS_2F2R; + this->dts_flags_map[DTS_3F2R] = DTS_2F2R; + + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_4CHANNEL; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_4CHANNEL; + + /* else if (this->audio_caps & AO_CAP_MODE_STEREO) + defaults are ok */ + } else if (!(this->audio_caps & AO_CAP_MODE_STEREO)) { + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, _("HELP! a mono-only audio driver?!\n")); + + this->dts_flags_map[DTS_MONO] = DTS_MONO; + this->dts_flags_map[DTS_STEREO] = DTS_MONO; + this->dts_flags_map[DTS_3F] = DTS_MONO; + this->dts_flags_map[DTS_2F1R] = DTS_MONO; + this->dts_flags_map[DTS_3F1R] = DTS_MONO; + this->dts_flags_map[DTS_2F2R] = DTS_MONO; + this->dts_flags_map[DTS_3F2R] = DTS_MONO; + + this->ao_flags_map[DTS_MONO] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_STEREO] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_3F] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_2F1R] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_3F1R] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_2F2R] = AO_CAP_MODE_MONO; + this->ao_flags_map[DTS_3F2R] = AO_CAP_MODE_MONO; + } + } + this->stream = stream; + this->class = class_gen; + this->output_open = 0; + + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + dts_class_t *this ; + + lprintf("init_plugin\n"); + + this = (dts_class_t *) xine_xmalloc (sizeof (dts_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "DTS"; + this->decoder_class.description = N_("DTS passthru audio format decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_DTS, 0 + }; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 1 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 16, "dts", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_faad_decoder.c b/src/audio_dec/xine_faad_decoder.c new file mode 100644 index 000000000..0c7c6dd01 --- /dev/null +++ b/src/audio_dec/xine_faad_decoder.c @@ -0,0 +1,477 @@ +/* + * Copyright (C) 2000-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + */ + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#define LOG_MODULE "libfaad" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> +#include <xine/xineutils.h> +#ifdef HAVE_NEAACDEC_H +#include <neaacdec.h> +#else +#include "common.h" +#include "structs.h" +#include "decoder.h" +#include "syntax.h" +#endif + +#define FAAD_MIN_STREAMSIZE 768 /* 6144 bits/channel */ + +typedef struct { + audio_decoder_class_t decoder_class; +} faad_class_t; + +typedef struct faad_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + /* faad2 stuff */ + NeAACDecHandle faac_dec; + NeAACDecConfigurationPtr faac_cfg; + NeAACDecFrameInfo faac_finfo; + int faac_failed; + + int raw_mode; + + unsigned char *buf; + int size; + int rec_audio_src_size; + int max_audio_src_size; + int pts; + + unsigned char *dec_config; + int dec_config_size; + + unsigned long rate; + int bits_per_sample; + unsigned char num_channels; + int sbr; + + int output_open; + + unsigned long total_time; + unsigned long total_data; +} faad_decoder_t; + + +static void faad_reset (audio_decoder_t *this_gen) { + + faad_decoder_t *this = (faad_decoder_t *) this_gen; + this->size = 0; +} + +static void faad_meta_info_set ( faad_decoder_t *this ) { + switch (this->num_channels) { + case 1: + if (this->faac_finfo.sbr == SBR_UPSAMPLED) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "HE-AAC 1.0 (libfaad)"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "AAC 1.0 (libfaad)"); + break; + case 2: + /* check if this is downmixed 5.1 */ + if (!this->faac_cfg || !this->faac_cfg->downMatrix) { + if (this->faac_finfo.sbr == SBR_UPSAMPLED) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "HE-AAC 2.0 (libfaad)"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "AAC 2.0 (libfaad)"); + break; + } + case 6: + if (this->faac_finfo.sbr == SBR_UPSAMPLED) + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "HE-AAC 5.1 (libfaad)"); + else + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "AAC 5.1 (libfaad)"); + break; + } +} + +static int faad_open_dec( faad_decoder_t *this ) { + int used; + + this->faac_dec = NeAACDecOpen(); + if( !this->faac_dec ) { + xprintf( this->stream->xine, XINE_VERBOSITY_LOG, + _("libfaad: libfaad NeAACDecOpen() failed.\n")); + this->faac_failed++; + } else { + if( this->dec_config ) { + used = NeAACDecInit2(this->faac_dec, this->dec_config, this->dec_config_size, + &this->rate, &this->num_channels); + + if( used < 0 ) { + xprintf( this->stream->xine, XINE_VERBOSITY_LOG, + _("libfaad: libfaad NeAACDecInit2 failed.\n")); + this->faac_failed++; + } else + lprintf( "NeAACDecInit2 returned rate=%"PRId32" channels=%d\n", + this->rate, this->num_channels ); + } else { + used = NeAACDecInit(this->faac_dec, this->buf, this->size, + &this->rate, &this->num_channels); + + if( used < 0 ) { + xprintf ( this->stream->xine, XINE_VERBOSITY_LOG, + _("libfaad: libfaad NeAACDecInit failed.\n")); + this->faac_failed++; + } else { + lprintf( "NeAACDecInit() returned rate=%"PRId32" channels=%d (used=%d)\n", + this->rate, this->num_channels, used); + + this->size -= used; + memmove( this->buf, &this->buf[used], this->size ); + } + } + } + + if( !this->bits_per_sample ) + this->bits_per_sample = 16; + + if( this->faac_failed ) { + if( this->faac_dec ) { + NeAACDecClose( this->faac_dec ); + this->faac_dec = NULL; + } + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + } else { + faad_meta_info_set(this); + } + + return this->faac_failed; +} + +static int faad_open_output( faad_decoder_t *this ) { + int ao_cap_mode; + + this->rec_audio_src_size = this->num_channels * FAAD_MIN_STREAMSIZE; + + switch( this->num_channels ) { + case 1: + ao_cap_mode=AO_CAP_MODE_MONO; + break; + case 6: + if(this->stream->audio_out->get_capabilities(this->stream->audio_out) & + AO_CAP_MODE_5_1CHANNEL) { + ao_cap_mode = AO_CAP_MODE_5_1CHANNEL; + break; + } else { + this->faac_cfg = NeAACDecGetCurrentConfiguration(this->faac_dec); + this->faac_cfg->downMatrix = 1; + NeAACDecSetConfiguration(this->faac_dec, this->faac_cfg); + this->num_channels = 2; + } + case 2: + ao_cap_mode=AO_CAP_MODE_STEREO; + break; + default: + return 0; + } + + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, + this->stream, + this->bits_per_sample, + this->rate, + ao_cap_mode) ; + return this->output_open; +} + +static void faad_decode_audio ( faad_decoder_t *this, int end_frame ) { + int used, decoded, outsize; + uint8_t *sample_buffer; + uint8_t *inbuf; + audio_buffer_t *audio_buffer; + int sample_size = this->size; + + if( !this->faac_dec ) + return; + + inbuf = this->buf; + while( (!this->raw_mode && end_frame && this->size >= 10) || + (this->raw_mode && this->size >= this->rec_audio_src_size) ) { + + sample_buffer = NeAACDecDecode(this->faac_dec, + &this->faac_finfo, inbuf, sample_size); + + if( !sample_buffer ) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "libfaad: %s\n", NeAACDecGetErrorMessage(this->faac_finfo.error)); + used = 1; + } else { + used = this->faac_finfo.bytesconsumed; + + /* raw AAC parameters might only be known after decoding the first frame */ + if( !this->dec_config && + (this->num_channels != this->faac_finfo.channels || + this->rate != this->faac_finfo.samplerate) ) { + + this->num_channels = this->faac_finfo.channels; + this->rate = this->faac_finfo.samplerate; + + lprintf("NeAACDecDecode() returned rate=%"PRId32" channels=%d used=%d\n", + this->rate, this->num_channels, used); + + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + faad_open_output( this ); + + faad_meta_info_set( this ); + } + + /* faad doesn't tell us about sbr until after the first frame */ + if (this->sbr != this->faac_finfo.sbr) { + this->sbr = this->faac_finfo.sbr; + faad_meta_info_set( this ); + } + + /* estimate bitrate */ + this->total_time += (1000*this->faac_finfo.samples/(this->rate*this->num_channels)); + this->total_data += 8*used; + + if ((this->total_time > LONG_MAX) || (this->total_data > LONG_MAX)) { + this->total_time >>= 2; + this->total_data >>= 2; + } + + if (this->total_time) + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, + 1000*(this->total_data/this->total_time)); + + decoded = this->faac_finfo.samples * 2; /* 1 sample = 2 bytes */ + + lprintf("decoded %d/%d output %ld\n", + used, this->size, this->faac_finfo.samples ); + + while( decoded ) { + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + + if( decoded < audio_buffer->mem_size ) + outsize = decoded; + else + outsize = audio_buffer->mem_size; + + xine_fast_memcpy( audio_buffer->mem, sample_buffer, outsize ); + + audio_buffer->num_frames = outsize / (this->num_channels*2); + audio_buffer->vpts = this->pts; + + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + + this->pts = 0; + decoded -= outsize; + sample_buffer += outsize; + } + } + + if(used >= this->size){ + this->size = 0; + } else { + this->size -= used; + inbuf += used; + } + + if( !this->raw_mode ) + this->size = 0; + } + + if( this->size ) + memmove( this->buf, inbuf, this->size); + +} + +static void faad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + faad_decoder_t *this = (faad_decoder_t *) this_gen; + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + /* store config information from ESDS mp4/qt atom */ + if( !this->faac_dec && (buf->decoder_flags & BUF_FLAG_SPECIAL) && + buf->decoder_info[1] == BUF_SPECIAL_DECODER_CONFIG ) { + + this->dec_config = xine_xmalloc(buf->decoder_info[2]); + this->dec_config_size = buf->decoder_info[2]; + memcpy(this->dec_config, buf->decoder_info_ptr[2], buf->decoder_info[2]); + + if( faad_open_dec(this) ) + return; + + this->raw_mode = 0; + } + + /* get audio parameters from file header + (may be overwritten by libfaad returned parameters) */ + if (buf->decoder_flags & BUF_FLAG_STDHEADER) { + this->rate=buf->decoder_info[1]; + this->bits_per_sample=buf->decoder_info[2] ; + this->num_channels=buf->decoder_info[3] ; + + if( buf->size > sizeof(xine_waveformatex) ) { + xine_waveformatex *wavex = (xine_waveformatex *) buf->content; + + if( wavex->cbSize > 0 ) { + this->dec_config = xine_xmalloc(wavex->cbSize); + this->dec_config_size = wavex->cbSize; + memcpy(this->dec_config, buf->content + sizeof(xine_waveformatex), + wavex->cbSize); + + if( faad_open_dec(this) ) + return; + + this->raw_mode = 0; + } + } + } else { + + lprintf ("decoding %d data bytes...\n", buf->size); + + if( (int)buf->size <= 0 || this->faac_failed ) + return; + + if( !this->size ) + this->pts = buf->pts; + + if( this->size + buf->size > this->max_audio_src_size ) { + this->max_audio_src_size = this->size + 2 * buf->size; + this->buf = realloc( this->buf, this->max_audio_src_size ); + } + + memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + if( !this->faac_dec && faad_open_dec(this) ) + return; + + /* open audio device as needed */ + if (!this->output_open) { + faad_open_output( this ); + } + + faad_decode_audio(this, buf->decoder_flags & BUF_FLAG_FRAME_END ); + } +} + +static void faad_discontinuity (audio_decoder_t *this_gen) { +} + +static void faad_dispose (audio_decoder_t *this_gen) { + + faad_decoder_t *this = (faad_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + if( this->buf ) + free(this->buf); + this->buf = NULL; + this->size = 0; + this->max_audio_src_size = 0; + + if( this->dec_config ) + free(this->dec_config); + this->dec_config = NULL; + this->dec_config_size = 0; + + if( this->faac_dec ) + NeAACDecClose(this->faac_dec); + this->faac_dec = NULL; + this->faac_failed = 0; + + free (this); +} + + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + faad_decoder_t *this ; + + this = (faad_decoder_t *) xine_xmalloc (sizeof (faad_decoder_t)); + + this->audio_decoder.decode_data = faad_decode_data; + this->audio_decoder.reset = faad_reset; + this->audio_decoder.discontinuity = faad_discontinuity; + this->audio_decoder.dispose = faad_dispose; + + this->stream = stream; + this->output_open = 0; + this->raw_mode = 1; + this->faac_dec = NULL; + this->faac_failed = 0; + this->buf = NULL; + this->size = 0; + this->max_audio_src_size = 0; + this->dec_config = NULL; + this->dec_config_size = 0; + this->total_time = 0; + this->total_data = 0; + + this->rate = 0; + + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + + faad_class_t *this ; + + this = (faad_class_t *) xine_xmalloc (sizeof (faad_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "FAAD"; + this->decoder_class.description = N_("Freeware Advanced Audio Decoder"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_AAC, 0 + }; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 1 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 16, "faad", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_lpcm_decoder.c b/src/audio_dec/xine_lpcm_decoder.c new file mode 100644 index 000000000..e84b112f4 --- /dev/null +++ b/src/audio_dec/xine_lpcm_decoder.c @@ -0,0 +1,278 @@ +/* + * Copyright (C) 2000-2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + */ + +/** + * @file + * @author James Courtier-Dutton <james@superbug.demon.co.uk> + * + * @date 2001-08-31 Added LPCM rate sensing + */ + +#ifndef __sun +#define _XOPEN_SOURCE 500 +#endif +/* avoid compiler warnings */ +#define _BSD_SOURCE 1 + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <netinet/in.h> /* ntohs */ + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> + +#ifdef WIN32 +#include <winsock.h> +/*#include <Winsock2.h>*/ /* htons */ +#endif + +typedef struct { + audio_decoder_class_t decoder_class; +} lpcm_class_t; + +typedef struct lpcm_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + uint32_t rate; + uint32_t bits_per_sample; + uint32_t number_of_channels; + uint32_t ao_cap_mode; + + int output_open; + int cpu_be; /**< TRUE, if we're a Big endian CPU */ +} lpcm_decoder_t; + +static void lpcm_reset (audio_decoder_t *this_gen) { + + /* lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen; */ + +} + +static void lpcm_discontinuity (audio_decoder_t *this_gen) { +} + +static void lpcm_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen; + int16_t *sample_buffer=(int16_t *)buf->content; + int stream_be; + audio_buffer_t *audio_buffer; + int format_changed = 0; + + /* Drop preview data */ + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + /* get config byte from mpeg2 stream */ + if ( (buf->decoder_flags & BUF_FLAG_SPECIAL) && + buf->decoder_info[1] == BUF_SPECIAL_LPCM_CONFIG ) { + unsigned int bits_per_sample = 16; + unsigned int sample_rate = 0; + unsigned int num_channels; + + num_channels = (buf->decoder_info[2] & 0x7) + 1; + switch ((buf->decoder_info[2]>>4) & 3) { + case 0: sample_rate = 48000; break; + case 1: sample_rate = 96000; break; + case 2: sample_rate = 44100; break; + case 3: sample_rate = 32000; break; + } + switch ((buf->decoder_info[2]>>6) & 3) { + case 0: bits_per_sample = 16; break; + case 1: bits_per_sample = 20; break; + case 2: bits_per_sample = 24; break; + } + + if( this->bits_per_sample != bits_per_sample || + this->number_of_channels != num_channels || + this->rate != sample_rate || + !this->output_open ) { + this->bits_per_sample = bits_per_sample; + this->number_of_channels = num_channels; + this->rate = sample_rate; + format_changed++; + } + } + + if( buf->decoder_flags & BUF_FLAG_STDHEADER ) { + this->rate=buf->decoder_info[1]; + this->bits_per_sample=buf->decoder_info[2] ; + this->number_of_channels=buf->decoder_info[3] ; + format_changed++; + } + + /* + * (re-)open output device + */ + if ( format_changed ) { + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + this->ao_cap_mode=_x_ao_channels2mode(this->number_of_channels); + + /* force 24-bit samples into 16 bits for now */ + if (this->bits_per_sample == 24) + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, this->stream, + 16, + this->rate, + this->ao_cap_mode) ; + else + this->output_open = (this->stream->audio_out->open) (this->stream->audio_out, this->stream, + this->bits_per_sample, + this->rate, + this->ao_cap_mode) ; + + /* stream/meta info */ + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "Linear PCM"); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, + this->bits_per_sample * this->rate * this->number_of_channels); + } + + if (!this->output_open || (buf->decoder_flags & BUF_FLAG_HEADER) ) + return; + + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + + /* Swap LPCM samples into native byte order, if necessary */ + buf->type &= 0xffff0000; + stream_be = ( buf->type == BUF_AUDIO_LPCM_BE ); + + if( this->bits_per_sample == 16 ){ + if (stream_be != this->cpu_be) + swab (sample_buffer, audio_buffer->mem, buf->size); + else + memcpy (audio_buffer->mem, sample_buffer, buf->size); + } + else if( this->bits_per_sample == 20 ) { + uint8_t *s = (uint8_t *)sample_buffer; + uint8_t *d = (uint8_t *)audio_buffer->mem; + int n = buf->size; + + if (stream_be != this->cpu_be) { + while( n >= 0 ) { + swab( s, d, 8 ); + s += 10; + d += 8; + n -= 10; + } + } else { + while( n >= 0 ) { + memcpy( d, s, 8 ); + s += 10; + d += 8; + n -= 10; + } + } + } else if( this->bits_per_sample == 24 ) { + uint8_t *s = (uint8_t *)sample_buffer; + uint8_t *d = (uint8_t *)audio_buffer->mem; + int n = buf->size; + + while (n >= 0) { + if ( stream_be ) { + *d++ = s[0]; + *d++ = s[1]; + } else { + *d++ = s[1]; + *d++ = s[2]; + } + + s += 3; + n -= 3; + } + } else { + memcpy (audio_buffer->mem, sample_buffer, buf->size); + } + + audio_buffer->vpts = buf->pts; + audio_buffer->num_frames = (((buf->size*8)/this->number_of_channels)/this->bits_per_sample); + + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + +} + +static void lpcm_dispose (audio_decoder_t *this_gen) { + lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + free (this_gen); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + lpcm_decoder_t *this ; + + this = (lpcm_decoder_t *) xine_xmalloc (sizeof (lpcm_decoder_t)); + + this->audio_decoder.decode_data = lpcm_decode_data; + this->audio_decoder.reset = lpcm_reset; + this->audio_decoder.discontinuity = lpcm_discontinuity; + this->audio_decoder.dispose = lpcm_dispose; + + this->output_open = 0; + this->rate = 0; + this->bits_per_sample=0; + this->number_of_channels=0; + this->ao_cap_mode=0; + this->stream = stream; + + this->cpu_be = ( htons(1) == 1 ); + + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + + lpcm_class_t *this ; + + this = (lpcm_class_t *) xine_xmalloc (sizeof (lpcm_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "Linear PCM"; + this->decoder_class.description = N_("Linear PCM audio decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_LPCM_BE, BUF_AUDIO_LPCM_LE, 0 +}; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 1 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 16, "pcm", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_mad_decoder.c b/src/audio_dec/xine_mad_decoder.c new file mode 100644 index 000000000..de7590f4e --- /dev/null +++ b/src/audio_dec/xine_mad_decoder.c @@ -0,0 +1,365 @@ +/* + * Copyright (C) 2000-2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + * + * stuff needed to turn libmad into a xine decoder plugin + */ + +#include <stdlib.h> +#include <string.h> +#include <config.h> + +#ifdef HAVE_MAD_H +#include <mad.h> +#endif + +#define LOG_MODULE "mad_decoder" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> +#include <xine/xineutils.h> + +#ifdef HAVE_MAD_H +# include <mad.h> +#else +# include "frame.h" +# include "synth.h" +#endif + +#define INPUT_BUF_SIZE 16384 + +typedef struct { + audio_decoder_class_t decoder_class; +} mad_class_t; + +typedef struct mad_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *xstream; + + int64_t pts; + + struct mad_synth synth; + struct mad_stream stream; + struct mad_frame frame; + + int output_sampling_rate; + int output_open; + int output_mode; + + uint8_t buffer[INPUT_BUF_SIZE]; + int bytes_in_buffer; + int preview_mode; + +} mad_decoder_t; + +static void mad_reset (audio_decoder_t *this_gen) { + + mad_decoder_t *this = (mad_decoder_t *) this_gen; + + mad_synth_finish (&this->synth); + mad_frame_finish (&this->frame); + mad_stream_finish(&this->stream); + + this->pts = 0; + this->bytes_in_buffer = 0; + this->preview_mode = 0; + + mad_synth_init (&this->synth); + mad_stream_init (&this->stream); + this->stream.options = MAD_OPTION_IGNORECRC; + mad_frame_init (&this->frame); +} + + +static void mad_discontinuity (audio_decoder_t *this_gen) { + + mad_decoder_t *this = (mad_decoder_t *) this_gen; + + this->pts = 0; +} + +/* utility to scale and round samples to 16 bits */ + +static inline +signed int scale(mad_fixed_t sample) +{ + /* round */ + sample += (1L << (MAD_F_FRACBITS - 16)); + + /* clip */ + if (sample >= MAD_F_ONE) + sample = MAD_F_ONE - 1; + else if (sample < -MAD_F_ONE) + sample = -MAD_F_ONE; + + /* quantize */ + return sample >> (MAD_F_FRACBITS + 1 - 16); +} + +/* +static int head_check(mad_decoder_t *this) { + + if( (this->header & 0xffe00000) != 0xffe00000) + return 0; + if(!((this->header>>17)&3)) + return 0; + if( ((this->header>>12)&0xf) == 0xf) + return 0; + if( ((this->header>>10)&0x3) == 0x3 ) + return 0; + return 1; +} +*/ + +static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + mad_decoder_t *this = (mad_decoder_t *) this_gen; + + lprintf ("decode data, decoder_flags: %d\n", buf->decoder_flags); + + if (buf->size>(INPUT_BUF_SIZE-this->bytes_in_buffer)) { + xprintf (this->xstream->xine, XINE_VERBOSITY_DEBUG, + "libmad: ALERT input buffer too small (%d bytes, %d avail)!\n", + buf->size, INPUT_BUF_SIZE-this->bytes_in_buffer); + buf->size = INPUT_BUF_SIZE-this->bytes_in_buffer; + } + + if ((buf->decoder_flags & BUF_FLAG_HEADER) == 0) { + + /* reset decoder on leaving preview mode */ + if ((buf->decoder_flags & BUF_FLAG_PREVIEW) == 0) { + if (this->preview_mode) { + mad_reset (this_gen); + } + } else { + this->preview_mode = 1; + } + + xine_fast_memcpy (&this->buffer[this->bytes_in_buffer], + buf->content, buf->size); + this->bytes_in_buffer += buf->size; + + /* + printf ("libmad: decode data - doing it\n"); + */ + + mad_stream_buffer (&this->stream, this->buffer, + this->bytes_in_buffer); + + while (1) { + + if (mad_frame_decode (&this->frame, &this->stream) != 0) { + + if (this->stream.next_frame) { + int num_bytes = + this->buffer + this->bytes_in_buffer - this->stream.next_frame; + + /* printf("libmad: MAD_ERROR_BUFLEN\n"); */ + + memmove(this->buffer, this->stream.next_frame, num_bytes); + this->bytes_in_buffer = num_bytes; + } + + switch (this->stream.error) { + + case MAD_ERROR_BUFLEN: + return; + + default: + mad_stream_buffer (&this->stream, this->buffer, + this->bytes_in_buffer); + } + + } else { + int mode = (this->frame.header.mode == MAD_MODE_SINGLE_CHANNEL) ? AO_CAP_MODE_MONO : AO_CAP_MODE_STEREO; + + if (!this->output_open + || (this->output_sampling_rate != this->frame.header.samplerate) + || (this->output_mode != mode)) { + + lprintf ("audio sample rate %d mode %08x\n", this->frame.header.samplerate, mode); + + /* the mpeg audio demuxer can set audio bitrate */ + if (! _x_stream_info_get(this->xstream, XINE_STREAM_INFO_AUDIO_BITRATE)) { + _x_stream_info_set(this->xstream, XINE_STREAM_INFO_AUDIO_BITRATE, + this->frame.header.bitrate); + } + + /* the mpeg audio demuxer can set this meta info */ + if (! _x_meta_info_get(this->xstream, XINE_META_INFO_AUDIOCODEC)) { + switch (this->frame.header.layer) { + case MAD_LAYER_I: + _x_meta_info_set_utf8(this->xstream, XINE_META_INFO_AUDIOCODEC, + "MPEG audio layer 1 (lib: MAD)"); + break; + case MAD_LAYER_II: + _x_meta_info_set_utf8(this->xstream, XINE_META_INFO_AUDIOCODEC, + "MPEG audio layer 2 (lib: MAD)"); + break; + case MAD_LAYER_III: + _x_meta_info_set_utf8(this->xstream, XINE_META_INFO_AUDIOCODEC, + "MPEG audio layer 3 (lib: MAD)"); + break; + default: + _x_meta_info_set_utf8(this->xstream, XINE_META_INFO_AUDIOCODEC, + "MPEG audio (lib: MAD)"); + } + } + + if (this->output_open) { + this->xstream->audio_out->close (this->xstream->audio_out, this->xstream); + this->output_open = 0; + } + if (!this->output_open) { + this->output_open = (this->xstream->audio_out->open) (this->xstream->audio_out, + this->xstream, 16, + this->frame.header.samplerate, + mode) ; + } + if (!this->output_open) { + return; + } + this->output_sampling_rate = this->frame.header.samplerate; + this->output_mode = mode; + } + + mad_synth_frame (&this->synth, &this->frame); + + if ( (buf->decoder_flags & BUF_FLAG_PREVIEW) == 0 ) { + + unsigned int nchannels, nsamples; + mad_fixed_t const *left_ch, *right_ch; + struct mad_pcm *pcm = &this->synth.pcm; + audio_buffer_t *audio_buffer; + uint16_t *output; + + audio_buffer = this->xstream->audio_out->get_buffer (this->xstream->audio_out); + output = audio_buffer->mem; + + nchannels = pcm->channels; + nsamples = pcm->length; + left_ch = pcm->samples[0]; + right_ch = pcm->samples[1]; + + while (nsamples--) { + /* output sample(s) in 16-bit signed little-endian PCM */ + + *output++ = scale(*left_ch++); + + if (nchannels == 2) + *output++ = scale(*right_ch++); + + } + + audio_buffer->num_frames = pcm->length; + audio_buffer->vpts = buf->pts; + + this->xstream->audio_out->put_buffer (this->xstream->audio_out, audio_buffer, this->xstream); + + buf->pts = 0; + + } + + lprintf ("decode worked\n"); + } + } + + } +} + +static void mad_dispose (audio_decoder_t *this_gen) { + + mad_decoder_t *this = (mad_decoder_t *) this_gen; + + mad_synth_finish (&this->synth); + mad_frame_finish (&this->frame); + mad_stream_finish(&this->stream); + + if (this->output_open) { + this->xstream->audio_out->close (this->xstream->audio_out, this->xstream); + this->output_open = 0; + } + + free (this_gen); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + mad_decoder_t *this ; + + this = (mad_decoder_t *) xine_xmalloc (sizeof (mad_decoder_t)); + + this->audio_decoder.decode_data = mad_decode_data; + this->audio_decoder.reset = mad_reset; + this->audio_decoder.discontinuity = mad_discontinuity; + this->audio_decoder.dispose = mad_dispose; + + this->output_open = 0; + this->bytes_in_buffer = 0; + this->preview_mode = 0; + + this->xstream = stream; + + mad_synth_init (&this->synth); + mad_stream_init (&this->stream); + mad_frame_init (&this->frame); + + this->stream.options = MAD_OPTION_IGNORECRC; + + lprintf ("init\n"); + + return &this->audio_decoder; +} + +/* + * mad plugin class + */ +static void *init_plugin (xine_t *xine, void *data) { + + mad_class_t *this; + + this = (mad_class_t *) xine_xmalloc (sizeof (mad_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "mad"; + this->decoder_class.description = N_("libmad based mpeg audio layer 1/2/3 decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_MPEG, 0 +}; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 7 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 16, "mad", XINE_VERSION_CODE, &dec_info_audio, init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/audio_dec/xine_musepack_decoder.c b/src/audio_dec/xine_musepack_decoder.c new file mode 100644 index 000000000..c556c5b9a --- /dev/null +++ b/src/audio_dec/xine_musepack_decoder.c @@ -0,0 +1,462 @@ +/* + * Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA + */ + +/** + * @file + * @brief xine interface to libmusepack/libmpcdec + * @author James Stembridge <jstembridge@gmail.com> + * + * @todo Add support for 32-bit float samples. + * @todo Add support for seeking. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#define LOG_MODULE "mpc_decoder" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include <xine/xine_internal.h> +#include <xine/audio_out.h> +#include <xine/buffer.h> +#include <xine/xineutils.h> + +#include <mpcdec/mpcdec.h> + +#define MPC_DECODER_MEMSIZE 65536 +#define MPC_DECODER_MEMSIZE2 (MPC_DECODER_MEMSIZE/2) + +#define INIT_BUFSIZE (MPC_DECODER_MEMSIZE*2) + +typedef struct { + audio_decoder_class_t decoder_class; +} mpc_class_t; + +typedef struct mpc_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + int sample_rate; /* audio sample rate */ + int bits_per_sample; /* bits/sample, usually 8 or 16 */ + int channels; /* 1 or 2, usually */ + + int output_open; /* flag to indicate audio is ready */ + + unsigned char *buf; /* data accumulation buffer */ + unsigned int buf_max; /* maximum size of buf */ + unsigned int read; /* size of accum. data already read */ + unsigned int size; /* size of accumulated data in buf */ + + mpc_reader reader; + mpc_streaminfo streaminfo; + mpc_decoder decoder; + + int decoder_ok; + unsigned int current_frame; + + int32_t file_size; + +} mpc_decoder_t; + + +/************************************************************************** + * musepack specific functions + *************************************************************************/ + +/* Reads size bytes of data into buffer at ptr. */ +static int32_t mpc_reader_read(void *const data, void *const ptr, int size) { + mpc_decoder_t *const this = (mpc_decoder_t *) data; + + lprintf("mpc_reader_read: size=%d\n", size); + + /* Don't try to read more data than we have */ + if (size > (this->size - this->read)) + size = this->size - this->read; + + /* Copy the data */ + xine_fast_memcpy(ptr, &this->buf[this->read], size); + + /* Update our position in the data buffer */ + this->read += size; + + return size; +} + +/* Seeks to byte position offset. */ +static mpc_bool_t mpc_reader_seek(void *const data, const int32_t offset) { + mpc_decoder_t *const this = (mpc_decoder_t *) data; + + lprintf("mpc_reader_seek: offset=%d\n", offset); + + /* seek is only called when reading the header so we can assume + * that the buffer starts at the start of the file */ + this->read = offset; + + return TRUE; +} + +/* Returns the current byte offset in the stream. */ +static int32_t mpc_reader_tell(void *const data) { + lprintf("mpc_reader_tell\n"); + + /* Tell isn't used so just return 0 */ + return 0; +} + +/* Returns the total length of the source stream, in bytes. */ +static int32_t mpc_reader_get_size(void *const data) { + mpc_decoder_t *const this = (mpc_decoder_t *) data; + + lprintf("mpc_reader_get_size\n"); + + return this->file_size; +} + +/* True if the stream is a seekable stream. */ +static mpc_bool_t mpc_reader_canseek(void *data) { + lprintf("mpc_reader_canseek\n"); + + return TRUE; +} + +/** + * @brief Convert a array of floating point samples into 16-bit signed integer samples + * @param f Floating point samples array (origin) + * @param s16 16-bit signed integer samples array (destination) + * @param samples Number of samples to convert + * + * @todo This same work is being done in many decoders to adapt the output of + * the decoder to what the audio output can actually use, this should be + * done by the audio_output loop, not by the decoders. + */ +static inline void float_to_int(const float *const _f, int16_t *const s16, const int samples) { + int i; + for (i = 0; i < samples; i++) { + const float f = _f[i] * 32767; + if (f > INT16_MAX) + s16[i] = INT16_MAX; + else if (f < INT16_MIN) + s16[i] = INT16_MIN; + else + s16[i] = f; + /* printf("samples[%d] = %f, %d\n", i, _f[i], s16[num_channels*i]); */ + } +} + +/* Decode a musepack frame */ +static int mpc_decode_frame (mpc_decoder_t *this) { + float buffer[MPC_DECODER_BUFFER_LENGTH]; + uint32_t frames; + + lprintf("mpd_decode_frame\n"); + + frames = mpc_decoder_decode(&this->decoder, buffer, 0, 0); + + if (frames > 0) { + audio_buffer_t *audio_buffer; + int16_t *int_samples; + + lprintf("got %d samples\n", frames); + + /* Get audio buffer */ + audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out); + audio_buffer->vpts = 0; + audio_buffer->num_frames = frames; + + /* Convert samples */ + int_samples = (int16_t *) audio_buffer->mem; + float_to_int(buffer, int_samples, frames*this->channels); + + /* Output converted samples */ + this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream); + } + + return frames; +} + +/************************************************************************** + * xine audio plugin functions + *************************************************************************/ + +static void mpc_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + mpc_decoder_t *this = (mpc_decoder_t *) this_gen; + int err; + + lprintf("mpc_decode_data\n"); + + if (!_x_stream_info_get(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED)) + return; + + /* We don't handle special buffers */ + if (buf->decoder_flags & BUF_FLAG_SPECIAL) + return; + + /* Read header */ + if (buf->decoder_flags & BUF_FLAG_HEADER) { + + lprintf("header\n"); + + /* File size is in decoder_info[0] */ + this->file_size = buf->decoder_info[0]; + + /* Initialise the data accumulation buffer */ + this->buf = xine_xmalloc(INIT_BUFSIZE); + this->buf_max = INIT_BUFSIZE; + this->read = 0; + this->size = 0; + + /* Initialise the reader */ + this->reader.read = mpc_reader_read; + this->reader.seek = mpc_reader_seek; + this->reader.tell = mpc_reader_tell; + this->reader.get_size = mpc_reader_get_size; + this->reader.canseek = mpc_reader_canseek; + this->reader.data = this; + + /* Copy header to buffer */ + xine_fast_memcpy(this->buf, buf->content, buf->size); + this->size = buf->size; + + /* Initialise and read stream info */ + mpc_streaminfo_init(&this->streaminfo); + + if ((err = mpc_streaminfo_read(&this->streaminfo, &this->reader))) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("libmusepack: mpc_streaminfo_read failed: %d\n"), err); + + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + + this->sample_rate = this->streaminfo.sample_freq; + this->channels = this->streaminfo.channels; + this->bits_per_sample = 16; + + /* After the header the demuxer starts sending data from an offset + * of 28 bytes */ + this->size = 28; + + /* We need to keep track of the current frame so we now when we've + * reached the end of the stream */ + this->current_frame = 0; + + /* Setup the decoder */ + mpc_decoder_setup(&this->decoder, &this->reader); + this->decoder_ok = 0; + + /* Take this opportunity to initialize stream/meta information */ + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, + "Musepack (libmusepack)"); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, + (int) this->streaminfo.average_bitrate); + + return; + } + + lprintf("data: %u size=%u read=%u\n", buf->size, this->size, this->read); + + /* if the audio output is not open yet, open the audio output */ + 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, + _x_ao_channels2mode(this->channels)); + } + + /* if the audio still isn't open, do not go any further with the decode */ + if (!this->output_open) + return; + + /* If we run out of space in our internal buffer we discard what's + * already been read */ + if (((this->size + buf->size) > this->buf_max) && this->read) { + lprintf("discarding read data\n"); + this->size -= this->read; + memmove(this->buf, &this->buf[this->read], this->size); + this->read = 0; + } + + /* If there still isn't space we have to increase the size of the + * internal buffer */ + if ((this->size + buf->size) > this->buf_max) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "libmusepack: increasing internal buffer size\n"); + this->buf_max += 2*buf->size; + this->buf = realloc(this->buf, this->buf_max); + } + + /* Copy data */ + xine_fast_memcpy(&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + /* Time to decode */ + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + /* Increment frame count */ + if (this->current_frame++ == this->streaminfo.frames) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("libmusepack: data after last frame ignored\n")); + return; + } + + if (!this->decoder_ok) { + /* We require MPC_DECODER_MEMSIZE bytes to initialise the decoder */ + if ((this->size - this->read) >= MPC_DECODER_MEMSIZE) { + lprintf("initialise"); + + if (!mpc_decoder_initialize(&this->decoder, &this->streaminfo)) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("libmusepack: mpc_decoder_initialise failed\n")); + + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + + this->decoder_ok = 1; + } else { + /* Not enough data yet */ + return; + } + } + + /* mpc_decoder_decode may cause a read of MPC_DECODER_MEMSIZE/2 bytes so + * make sure we have enough data available */ + if ((this->size - this->read) >= MPC_DECODER_MEMSIZE2) { + lprintf("decoding\n"); + + if ((err = mpc_decode_frame(this)) < 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("libmusepack: mpc_decoder_decode failed: %d\n"), err); + + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + } + + /* If we are at the end of the stream we decode the remaining frames as we + * know we'll have enough data */ + if (this->current_frame == this->streaminfo.frames) { + lprintf("flushing buffers\n"); + + do { + if ((err = mpc_decode_frame(this)) < 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("libmusepack: mpc_decoder_decode failed: %d\n"), err); + } + } while (err > 0); + + lprintf("buffers flushed\n"); + } + } +} + +static void mpc_reset (audio_decoder_t *this_gen) { + mpc_decoder_t *this = (mpc_decoder_t *) this_gen; + + this->size = 0; + this->read = 0; +} + +static void mpc_discontinuity (audio_decoder_t *this_gen) { + /* mpc_decoder_t *this = (mpc_decoder_t *) this_gen; */ +} + +static void mpc_dispose (audio_decoder_t *this_gen) { + + mpc_decoder_t *this = (mpc_decoder_t *) this_gen; + + /* close the audio output */ + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + + /* free anything that was allocated during operation */ + free(this->buf); + + free(this); +} + +static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + mpc_decoder_t *this ; + + this = (mpc_decoder_t *) xine_xmalloc (sizeof (mpc_decoder_t)); + + /* connect the member functions */ + this->audio_decoder.decode_data = mpc_decode_data; + this->audio_decoder.reset = mpc_reset; + this->audio_decoder.discontinuity = mpc_discontinuity; + this->audio_decoder.dispose = mpc_dispose; + + /* connect the stream */ + this->stream = stream; + + /* audio output is not open at the start */ + this->output_open = 0; + + /* no buffer yet */ + this->buf = NULL; + + /* initialize the basic audio parameters */ + this->channels = 0; + this->sample_rate = 0; + this->bits_per_sample = 0; + + /* return the newly-initialized audio decoder */ + return &this->audio_decoder; +} + +static void *init_plugin (xine_t *xine, void *data) { + + mpc_class_t *this ; + + this = (mpc_class_t *) xine_xmalloc (sizeof (mpc_class_t)); + + this->decoder_class.open_plugin = open_plugin; + this->decoder_class.identifier = "mpc"; + this->decoder_class.description = N_("mpc: musepack audio decoder plugin"); + this->decoder_class.dispose = default_audio_decoder_class_dispose; + + return this; +} + +static const uint32_t audio_types[] = { + BUF_AUDIO_MPC, + 0 +}; + +static const decoder_info_t dec_info_audio = { + audio_types, /* supported types */ + 5 /* priority */ +}; + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* { type, API version, "name", version, special_info, init_function }, */ + { PLUGIN_AUDIO_DECODER, 16, "mpc", XINE_VERSION_CODE, &dec_info_audio, &init_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; + |