summaryrefslogtreecommitdiff
path: root/src/libxineadec
diff options
context:
space:
mode:
Diffstat (limited to 'src/libxineadec')
-rw-r--r--src/libxineadec/Makefile.am41
-rw-r--r--src/libxineadec/xine_lpcm_decoder.c288
-rw-r--r--src/libxineadec/xine_speex_decoder.c436
-rw-r--r--src/libxineadec/xine_vorbis_decoder.c354
4 files changed, 1106 insertions, 13 deletions
diff --git a/src/libxineadec/Makefile.am b/src/libxineadec/Makefile.am
index 7f34b4bfd..b2ffcf078 100644
--- a/src/libxineadec/Makefile.am
+++ b/src/libxineadec/Makefile.am
@@ -2,26 +2,41 @@ include $(top_srcdir)/misc/Makefile.common
EXTRA_DIST = fooaudio.c
-libdir = $(XINE_PLUGINDIR)
-
SUBDIRS = gsm610 nosefart
-lib_LTLIBRARIES = \
+AM_LDFLAGS = $(xineplug_ldflags)
+
+if HAVE_VORBIS
+vorbis_module = xineplug_decode_vorbis.la
+endif
+
+if HAVE_SPEEX
+speex_module = xineplug_decode_speex.la
+endif
+
+xineplug_LTLIBRARIES = \
xineplug_decode_gsm610.la \
- xineplug_decode_nsf.la
+ xineplug_decode_nsf.la \
+ xineplug_decode_lpcm.la \
+ $(vorbis_module) \
+ $(speex_module)
xineplug_decode_gsm610_la_SOURCES = gsm610.c
xineplug_decode_gsm610_la_CFLAGS = $(VISIBILITY_FLAG)
-xineplug_decode_gsm610_la_LDFLAGS = -avoid-version -module
-xineplug_decode_gsm610_la_LIBADD = \
- $(XINE_LIB) \
- $(top_builddir)/src/libxineadec/gsm610/libgsm610.la
+xineplug_decode_gsm610_la_LIBADD = $(XINE_LIB) gsm610/libgsm610.la
xineplug_decode_nsf_la_SOURCES = nsf.c
xineplug_decode_nsf_la_CFLAGS = $(VISIBILITY_FLAG) -DNSF_PLAYER -fno-strict-aliasing
-xineplug_decode_nsf_la_LDFLAGS = -avoid-version -module
-xineplug_decode_nsf_la_LIBADD = -lm \
- $(XINE_LIB) \
- $(top_builddir)/src/libxineadec/nosefart/libnosefart.la
+xineplug_decode_nsf_la_LIBADD = $(XINE_LIB) -lm nosefart/libnosefart.la
+
+xineplug_decode_lpcm_la_SOURCES = xine_lpcm_decoder.c
+xineplug_decode_lpcm_la_CFLAGS = $(VISIBILITY_FLAG)
+xineplug_decode_lpcm_la_LIBADD = $(XINE_LIB)
+
+xineplug_decode_vorbis_la_SOURCES = xine_vorbis_decoder.c
+xineplug_decode_vorbis_la_LIBADD = $(XINE_LIB) $(VORBIS_LIBS) $(OGG_LIBS)
+xineplug_decode_vorbis_la_CFLAGS = $(VISIBILITY_FLAG) $(VORBIS_CFLAGS)
-# noinst_HEADERS =
+xineplug_decode_speex_la_SOURCES = xine_speex_decoder.c
+xineplug_decode_speex_la_LIBADD = $(XINE_LIB) $(SPEEX_LIBS)
+xineplug_decode_speex_la_CFLAGS = $(VISIBILITY_FLAGS) $(SPEEX_CFLAGS)
diff --git a/src/libxineadec/xine_lpcm_decoder.c b/src/libxineadec/xine_lpcm_decoder.c
new file mode 100644
index 000000000..43bea4cbf
--- /dev/null
+++ b/src/libxineadec/xine_lpcm_decoder.c
@@ -0,0 +1,288 @@
+/*
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * $Id: xine_decoder.c,v 1.62 2007/03/17 20:59:36 dgp85 Exp $
+ *
+ * 31-8-2001 Added LPCM rate sensing.
+ * (c) 2001 James Courtier-Dutton James@superbug.demon.co.uk
+ *
+ */
+#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_internal.h"
+#include "audio_out.h"
+#include "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 char *get_identifier (audio_decoder_class_t *this) {
+ return "Linear PCM";
+}
+
+static char *get_description (audio_decoder_class_t *this) {
+ return "Linear PCM audio decoder plugin";
+}
+
+static void dispose_class (audio_decoder_class_t *this) {
+ free (this);
+}
+
+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.get_identifier = get_identifier;
+ this->decoder_class.get_description = get_description;
+ this->decoder_class.dispose = dispose_class;
+
+ return this;
+}
+
+static 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, 15, "pcm", XINE_VERSION_CODE, &dec_info_audio, init_plugin },
+ { PLUGIN_NONE, 0, "", 0, NULL, NULL }
+};
diff --git a/src/libxineadec/xine_speex_decoder.c b/src/libxineadec/xine_speex_decoder.c
new file mode 100644
index 000000000..b729dc3bb
--- /dev/null
+++ b/src/libxineadec/xine_speex_decoder.c
@@ -0,0 +1,436 @@
+/*
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * $Id: xine_decoder.c,v 1.22 2007/01/19 01:48:05 dgp85 Exp $
+ *
+ * (ogg/)speex audio decoder plugin (libspeex wrapper) for xine
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#define LOG_MODULE "speex_decoder"
+#define LOG_VERBOSE
+/*
+#define LOG
+*/
+#define LOG_BUFFERS 0
+
+#include "xine_internal.h"
+#include "audio_out.h"
+#include "buffer.h"
+
+#include <ogg/ogg.h>
+
+#include <speex/speex.h>
+#include <speex/speex_header.h>
+#include <speex/speex_callbacks.h>
+#include <speex/speex_stereo.h>
+
+#define MAX_FRAME_SIZE 2000
+
+typedef struct {
+ audio_decoder_class_t decoder_class;
+} speex_class_t;
+
+typedef struct speex_decoder_s {
+ audio_decoder_t audio_decoder;
+
+ int64_t pts;
+
+ int output_sampling_rate;
+ int output_open;
+ int output_mode;
+
+ /* speex stuff */
+ void *st;
+ int frame_size;
+ int rate;
+ int nframes;
+ int channels;
+ SpeexBits bits;
+ SpeexStereoState stereo;
+ int expect_metadata;
+
+ float output[MAX_FRAME_SIZE];
+
+ int header_count;
+
+ xine_stream_t *stream;
+
+} speex_decoder_t;
+
+
+static void speex_reset (audio_decoder_t *this_gen) {
+
+ speex_decoder_t *this = (speex_decoder_t *) this_gen;
+
+ speex_bits_init (&this->bits);
+}
+
+static void speex_discontinuity (audio_decoder_t *this_gen) {
+
+ speex_decoder_t *this = (speex_decoder_t *) this_gen;
+
+ this->pts=0;
+}
+
+/* Known speex comment keys from ogg123 sources*/
+static struct {
+ char *key; /* includes the '=' for programming convenience */
+ int xine_metainfo_index;
+} speex_comment_keys[] = {
+ {"ARTIST=", XINE_META_INFO_ARTIST},
+ {"ALBUM=", XINE_META_INFO_ALBUM},
+ {"TITLE=", XINE_META_INFO_TITLE},
+ {"GENRE=", XINE_META_INFO_GENRE},
+ {"DESCRIPTION=", XINE_META_INFO_COMMENT},
+ {"DATE=", XINE_META_INFO_YEAR},
+ {NULL, 0}
+};
+
+#define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
+ ((buf[base+2]<<16)&0xff0000)| \
+ ((buf[base+1]<<8)&0xff00)| \
+ (buf[base]&0xff))
+
+static
+void read_metadata (speex_decoder_t *this, char * comments, int length)
+{
+ char * c = comments;
+ int len, i, nb_fields;
+ char * end;
+
+ _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "speex");
+
+ if (length < 8) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
+ return;
+ }
+
+ end = c+length;
+ len = readint (c, 0);
+ c += 4;
+
+ if (c+len > end) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
+ return;
+ }
+
+#ifdef LOG
+ /* Encoder */
+ printf ("libspeex: ");
+ fwrite (c, 1, len, stdout);
+ printf ("\n");
+#endif
+
+ c += len;
+
+ if (c+4 > end) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
+ return;
+ }
+
+ nb_fields = readint (c, 0);
+ c += 4;
+
+ for (i = 0; i < nb_fields; i++) {
+ if (c+4 > end) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
+ return;
+ }
+
+ len = readint (c, 0);
+ c += 4;
+ if (c+len > end) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
+ return;
+ }
+
+#ifdef LOG
+ printf ("libspeex: ");
+ fwrite (c, 1, len, stdout);
+ printf ("\n");
+#endif
+
+ for (i = 0; speex_comment_keys[i].key != NULL; i++) {
+
+ if ( !strncasecmp (speex_comment_keys[i].key, c,
+ strlen(speex_comment_keys[i].key)) ) {
+ int keylen = strlen(speex_comment_keys[i].key);
+ char meta_info[(len - keylen) + 1];
+
+ lprintf ("known metadata %d %d\n",
+ i, speex_comment_keys[i].xine_metainfo_index);
+
+ snprintf(meta_info, (len - keylen), "%s", c + keylen);
+ _x_meta_info_set_utf8(this->stream, speex_comment_keys[i].xine_metainfo_index, meta_info);
+ }
+ }
+
+ c += len;
+ }
+}
+
+static void speex_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
+
+ speex_decoder_t *this = (speex_decoder_t *) this_gen;
+
+ llprintf (LOG_BUFFERS, "decode buf=%8p content=%8p flags=%08x\n",
+ buf, buf->content, buf->decoder_flags);
+
+ if ( (buf->decoder_flags & BUF_FLAG_HEADER) &&
+ !(buf->decoder_flags & BUF_FLAG_STDHEADER) ) {
+ lprintf ("preview buffer, %d headers to go\n", this->header_count);
+
+ if (this->header_count) {
+
+ if (!this->st) {
+ SpeexMode * spx_mode;
+ SpeexHeader * spx_header;
+ int modeID;
+ int bitrate;
+
+ speex_bits_init (&this->bits);
+
+ spx_header = speex_packet_to_header (buf->content, buf->size);
+
+ if (!spx_header) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: could not read Speex header\n");
+ return;
+ }
+
+ modeID = spx_header->mode;
+ spx_mode = (SpeexMode *) speex_mode_list[modeID];
+
+ if (spx_mode->bitstream_version != spx_header->mode_bitstream_version) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: incompatible Speex mode bitstream version\n");
+ return;
+ }
+
+ this->st = speex_decoder_init (spx_mode);
+ if (!this->st) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: decoder initialization failed\n");
+ return;
+ }
+
+ this->rate = spx_header->rate;
+ speex_decoder_ctl (this->st, SPEEX_SET_SAMPLING_RATE, &this->rate);
+ _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE,
+ this->rate);
+
+ this->channels = spx_header->nb_channels;
+ if (this->channels == 2) {
+ SpeexCallback callback;
+
+ callback.callback_id = SPEEX_INBAND_STEREO;
+ callback.func = speex_std_stereo_request_handler;
+ callback.data = &this->stereo;
+ speex_decoder_ctl (this->st, SPEEX_SET_HANDLER, &callback);
+ }
+
+ this->nframes = spx_header->frames_per_packet;
+ if (!this->nframes) this->nframes = 1;
+
+ speex_decoder_ctl (this->st, SPEEX_GET_FRAME_SIZE, &this->frame_size);
+
+ speex_decoder_ctl (this->st, SPEEX_GET_BITRATE, &bitrate);
+ if (bitrate <= 1) bitrate = 16000; /* assume 16 kbit */
+ _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, bitrate);
+
+ this->header_count += spx_header->extra_headers;
+ this->expect_metadata = 1;
+
+ free (spx_header);
+ } else if (this->expect_metadata) {
+ read_metadata (this, buf->content, buf->size);
+ }
+
+ this->header_count--;
+
+ if (!this->header_count) {
+ int mode = _x_ao_channels2mode(this->channels);
+
+ if (!this->output_open) {
+ this->output_open =
+ this->stream->audio_out->open(this->stream->audio_out,
+ this->stream,
+ 16,
+ this->rate,
+ mode);
+ lprintf ("this->output_open after attempt is %d\n", this->output_open);
+ }
+ }
+ }
+
+ } else if (this->output_open) {
+ int i, j;
+
+ audio_buffer_t *audio_buffer;
+
+ audio_buffer =
+ this->stream->audio_out->get_buffer (this->stream->audio_out);
+
+ speex_bits_read_from (&this->bits, buf->content, buf->size);
+
+ for (j = 0; j < this->nframes; j++) {
+ int ret;
+ int bitrate;
+ ogg_int16_t * ptr = audio_buffer->mem;
+
+ ret = speex_decode (this->st, &this->bits, this->output);
+
+ if (ret==-1)
+ break;
+ if (ret==-2) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: Decoding error, corrupted stream?\n");
+ break;
+ }
+ if (speex_bits_remaining(&this->bits)<0) {
+ xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: Decoding overflow, corrupted stream?\n");
+ break;
+ }
+
+ if (this->channels == 2) {
+ speex_decode_stereo (this->output, this->frame_size, &this->stereo);
+ }
+
+ speex_decoder_ctl (this->st, SPEEX_GET_BITRATE, &bitrate);
+ if (bitrate <= 1) bitrate = 16000; /* assume 16 kbit */
+ _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, bitrate);
+
+ /*PCM saturation (just in case)*/
+ for (i=0; i < this->frame_size * this->channels; i++)
+ {
+ if (this->output[i]>32000.0)
+ this->output[i]=32000.0;
+ else if (this->output[i]<-32000.0)
+ this->output[i]=-32000.0;
+ }
+
+ /*Convert to short and play */
+ for (i=0; i< this->frame_size * this->channels; i++) {
+ *ptr++ = (ogg_int16_t)this->output[i];
+ }
+
+ audio_buffer->vpts = this->pts;
+ this->pts=0;
+ audio_buffer->num_frames = this->frame_size;
+
+ this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream);
+
+ buf->pts=0;
+
+ }
+ }
+ else {
+ llprintf (LOG_BUFFERS, "output not open\n");
+ }
+}
+
+static void speex_dispose (audio_decoder_t *this_gen) {
+
+ speex_decoder_t *this = (speex_decoder_t *) this_gen;
+
+ if (this->st) {
+ speex_decoder_destroy (this->st);
+ }
+ speex_bits_destroy (&this->bits);
+
+ if (this->output_open)
+ this->stream->audio_out->close (this->stream->audio_out, this->stream);
+
+ free (this_gen);
+}
+
+static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen,
+ xine_stream_t *stream) {
+
+ speex_decoder_t *this ;
+ static SpeexStereoState init_stereo = SPEEX_STEREO_STATE_INIT;
+
+ this = (speex_decoder_t *) xine_xmalloc (sizeof (speex_decoder_t));
+
+ this->audio_decoder.decode_data = speex_decode_data;
+ this->audio_decoder.reset = speex_reset;
+ this->audio_decoder.discontinuity = speex_discontinuity;
+ this->audio_decoder.dispose = speex_dispose;
+ this->stream = stream;
+
+ this->output_open = 0;
+ this->header_count = 1;
+ this->expect_metadata = 0;
+
+ this->st = NULL;
+
+ this->channels = 1;
+
+ memcpy (&this->stereo, &init_stereo, sizeof (SpeexStereoState));
+
+ return (audio_decoder_t *) this;
+}
+
+/*
+ * speex plugin class
+ */
+
+static char *get_identifier (audio_decoder_class_t *this) {
+ return "speex";
+}
+
+static char *get_description (audio_decoder_class_t *this) {
+ return "Speex audio decoder plugin";
+}
+
+static void dispose_class (audio_decoder_class_t *this) {
+ free (this);
+}
+
+static void *init_plugin (xine_t *xine, void *data) {
+
+ speex_class_t *this;
+
+ this = (speex_class_t *) xine_xmalloc (sizeof (speex_class_t));
+
+ this->decoder_class.open_plugin = open_plugin;
+ this->decoder_class.get_identifier = get_identifier;
+ this->decoder_class.get_description = get_description;
+ this->decoder_class.dispose = dispose_class;
+
+ return this;
+}
+
+static uint32_t audio_types[] = {
+ BUF_AUDIO_SPEEX, 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, 15, "speex", XINE_VERSION_CODE, &dec_info_audio, init_plugin },
+ { PLUGIN_NONE, 0, "", 0, NULL, NULL }
+};
diff --git a/src/libxineadec/xine_vorbis_decoder.c b/src/libxineadec/xine_vorbis_decoder.c
new file mode 100644
index 000000000..ef8575949
--- /dev/null
+++ b/src/libxineadec/xine_vorbis_decoder.c
@@ -0,0 +1,354 @@
+/*
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * $Id: xine_decoder.c,v 1.48 2006/12/04 13:59:38 dgp85 Exp $
+ *
+ * (ogg/)vorbis audio decoder plugin (libvorbis wrapper) for xine
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#define LOG_MODULE "vorbis_decoder"
+#define LOG_VERBOSE
+/*
+#define LOG
+*/
+
+#include "xine_internal.h"
+#include "audio_out.h"
+#include "buffer.h"
+
+#include <ogg/ogg.h>
+#include <vorbis/codec.h>
+
+#define MAX_NUM_SAMPLES 4096
+
+typedef struct {
+ audio_decoder_class_t decoder_class;
+} vorbis_class_t;
+
+typedef struct vorbis_decoder_s {
+ audio_decoder_t audio_decoder;
+
+ int64_t pts;
+
+ int output_sampling_rate;
+ int output_open;
+ int output_mode;
+
+ ogg_packet op; /* we must use this struct to sent data to libvorbis */
+
+ /* vorbis stuff */
+ vorbis_info vi; /* stores static vorbis bitstream settings */
+ vorbis_comment vc;
+ vorbis_dsp_state vd; /* central working state for packet->PCM decoder */
+ vorbis_block vb; /* local working state for packet->PCM decoder */
+
+ int16_t convbuffer[MAX_NUM_SAMPLES];
+ int convsize;
+
+ int header_count;
+
+ xine_stream_t *stream;
+
+} vorbis_decoder_t;
+
+
+static void vorbis_reset (audio_decoder_t *this_gen) {
+
+ vorbis_decoder_t *this = (vorbis_decoder_t *) this_gen;
+
+ if( !this->header_count )
+ vorbis_block_init(&this->vd,&this->vb);
+}
+
+static void vorbis_discontinuity (audio_decoder_t *this_gen) {
+
+ vorbis_decoder_t *this = (vorbis_decoder_t *) this_gen;
+
+ this->pts=0;
+}
+
+/* Known vorbis comment keys from ogg123 sources*/
+static struct {
+ char *key; /* includes the '=' for programming convenience */
+ int xine_metainfo_index;
+} vorbis_comment_keys[] = {
+ {"ARTIST=", XINE_META_INFO_ARTIST},
+ {"ALBUM=", XINE_META_INFO_ALBUM},
+ {"TITLE=", XINE_META_INFO_TITLE},
+ {"GENRE=", XINE_META_INFO_GENRE},
+ {"DESCRIPTION=", XINE_META_INFO_COMMENT},
+ {"COMMENT=", XINE_META_INFO_COMMENT},
+ {"DATE=", XINE_META_INFO_YEAR},
+ {"TRACKNUMBER=", XINE_META_INFO_TRACK_NUMBER},
+ {NULL, 0}
+};
+
+static void get_metadata (vorbis_decoder_t *this) {
+
+ char **ptr=this->vc.user_comments;
+ while(*ptr){
+
+ char *comment = *ptr;
+ int i;
+
+ lprintf("%s\n", comment);
+
+ for (i = 0; vorbis_comment_keys[i].key != NULL; i++) {
+
+ if ( !strncasecmp (vorbis_comment_keys[i].key, comment,
+ strlen(vorbis_comment_keys[i].key)) ) {
+
+ lprintf ("known metadata %d %d\n",
+ i, vorbis_comment_keys[i].xine_metainfo_index);
+
+ _x_meta_info_set_utf8(this->stream, vorbis_comment_keys[i].xine_metainfo_index,
+ comment + strlen(vorbis_comment_keys[i].key));
+
+ }
+ }
+ ++ptr;
+ }
+
+ _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "vorbis");
+}
+
+static void vorbis_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
+
+ vorbis_decoder_t *this = (vorbis_decoder_t *) this_gen;
+
+ memset( &this->op, 0, sizeof(this->op) );
+ this->op.packet = buf->content;
+ this->op.bytes = buf->size;
+
+ if ( (buf->decoder_flags & BUF_FLAG_HEADER) &&
+ !(buf->decoder_flags & BUF_FLAG_STDHEADER) ) {
+ lprintf ("%d headers to go\n", this->header_count);
+
+ if (this->header_count) {
+ int res = 0;
+
+ if (this->header_count == 3)
+ this->op.b_o_s = 1;
+
+
+ if( (res = vorbis_synthesis_headerin(&this->vi,&this->vc,&this->op)) < 0 ){
+ /* error case; not a vorbis header */
+ xine_log(this->stream->xine, XINE_LOG_MSG, "libvorbis: this bitstream does not contain vorbis audio data. Following first 64 bytes (return: %d).\n", res);
+ xine_hexdump(this->op.packet, this->op.bytes < 64 ? this->op.bytes : 64);
+ return;
+ }
+
+ this->header_count--;
+
+ if (!this->header_count) {
+
+ int mode = AO_CAP_MODE_MONO;
+
+ get_metadata (this);
+
+ mode = _x_ao_channels2mode(this->vi.channels);
+
+ this->convsize=MAX_NUM_SAMPLES/this->vi.channels;
+
+ if (!this->output_open) {
+ this->output_open = this->stream->audio_out->open(this->stream->audio_out,
+ this->stream,
+ 16,
+ this->vi.rate,
+ mode) ;
+
+ _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE,
+ this->vi.bitrate_nominal);
+
+ }
+
+ /* OK, got and parsed all three headers. Initialize the Vorbis
+ * packet->PCM decoder. */
+ lprintf("all three headers parsed. initializing decoder.\n");
+ /* initialize central decode state */
+ vorbis_synthesis_init(&this->vd,&this->vi);
+ /* initialize local state for most of the decode so multiple
+ * block decodes can proceed in parallel. We could init
+ * multiple vorbis_block structures for vd here */
+ vorbis_block_init(&this->vd,&this->vb);
+ }
+ }
+
+ } else if (this->output_open) {
+
+ float **pcm;
+ int samples;
+
+ if(vorbis_synthesis(&this->vb,&this->op)==0)
+ vorbis_synthesis_blockin(&this->vd,&this->vb);
+
+ if (buf->pts!=0)
+ this->pts=buf->pts;
+
+ while ((samples=vorbis_synthesis_pcmout(&this->vd,&pcm))>0){
+
+ /* **pcm is a multichannel float vector. In stereo, for
+ * example, pcm[0][...] is left, and pcm[1][...] is right.
+ * samples is the size of each channel. Convert the float
+ * values (-1.<=range<=1.) to whatever PCM format and write
+ * it out
+ */
+
+ int i,j;
+ int clipflag=0;
+ int bout=(samples<this->convsize?samples:this->convsize);
+ audio_buffer_t *audio_buffer;
+
+ audio_buffer = this->stream->audio_out->get_buffer (this->stream->audio_out);
+
+ /* convert floats to 16 bit signed ints (host order) and
+ interleave */
+ for(i=0;i<this->vi.channels;i++){
+ ogg_int16_t *ptr=audio_buffer->mem+i;
+ float *mono=pcm[i];
+ for(j=0;j<bout;j++){
+ int val=mono[j]*32767.f;
+ /* might as well guard against clipping */
+ if(val>32767){
+ val=32767;
+ clipflag=1;
+ }
+ if(val<-32768){
+ val=-32768;
+ clipflag=1;
+ }
+ *ptr=val;
+ ptr+=this->vi.channels;
+ }
+ }
+
+ audio_buffer->vpts = this->pts;
+ this->pts=0;
+ audio_buffer->num_frames = bout;
+
+ this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream);
+
+ buf->pts=0;
+
+ /* tell libvorbis how many samples we actually consumed */
+ vorbis_synthesis_read(&this->vd,bout);
+ }
+ }
+ lprintf("output not open\n");
+}
+
+static void vorbis_dispose (audio_decoder_t *this_gen) {
+
+ vorbis_decoder_t *this = (vorbis_decoder_t *) this_gen;
+
+ if( !this->header_count ) {
+ lprintf("deinitializing decoder\n");
+
+ vorbis_block_clear(&this->vb);
+ vorbis_dsp_clear(&this->vd);
+ }
+
+ vorbis_comment_clear(&this->vc);
+
+ vorbis_info_clear(&this->vi); /* must be called last */
+
+ if (this->output_open)
+ this->stream->audio_out->close (this->stream->audio_out, this->stream);
+
+ lprintf("libvorbis instance destroyed\n");
+
+ free (this_gen);
+}
+
+static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen,
+ xine_stream_t *stream) {
+
+ vorbis_decoder_t *this ;
+
+ this = (vorbis_decoder_t *) xine_xmalloc (sizeof (vorbis_decoder_t));
+
+ this->audio_decoder.decode_data = vorbis_decode_data;
+ this->audio_decoder.reset = vorbis_reset;
+ this->audio_decoder.discontinuity = vorbis_discontinuity;
+ this->audio_decoder.dispose = vorbis_dispose;
+ this->stream = stream;
+
+ this->output_open = 0;
+ this->header_count = 3;
+ this->convsize = 0;
+
+ vorbis_info_init(&this->vi);
+ vorbis_comment_init(&this->vc);
+
+ lprintf("libvorbis decoder instance created\n");
+
+ return (audio_decoder_t *) this;
+}
+
+/*
+ * vorbis plugin class
+ */
+
+static char *get_identifier (audio_decoder_class_t *this) {
+ return "vorbis";
+}
+
+static char *get_description (audio_decoder_class_t *this) {
+ return "vorbis audio decoder plugin";
+}
+
+static void dispose_class (audio_decoder_class_t *this) {
+ free (this);
+}
+
+static void *init_plugin (xine_t *xine, void *data) {
+
+ vorbis_class_t *this;
+
+ this = (vorbis_class_t *) xine_xmalloc (sizeof (vorbis_class_t));
+
+ this->decoder_class.open_plugin = open_plugin;
+ this->decoder_class.get_identifier = get_identifier;
+ this->decoder_class.get_description = get_description;
+ this->decoder_class.dispose = dispose_class;
+
+ return this;
+}
+
+static uint32_t audio_types[] = {
+ BUF_AUDIO_VORBIS, 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, 15, "vorbis", XINE_VERSION_CODE, &dec_info_audio, init_plugin },
+ { PLUGIN_NONE, 0, "", 0, NULL, NULL }
+};