diff options
author | James Courtier-Dutton <jcdutton@users.sourceforge.net> | 2001-08-21 19:39:50 +0000 |
---|---|---|
committer | James Courtier-Dutton <jcdutton@users.sourceforge.net> | 2001-08-21 19:39:50 +0000 |
commit | 790d5baee128cb40800fee392e74c01afefe4756 (patch) | |
tree | 4aa716c83a891ca3876968ede7f74c7482bfa7f0 /src/xine-engine | |
parent | f92de1992b6c30602b6785c0fb84fe3be6730c54 (diff) | |
download | xine-lib-790d5baee128cb40800fee392e74c01afefe4756.tar.gz xine-lib-790d5baee128cb40800fee392e74c01afefe4756.tar.bz2 |
First checkin of new audio_out architecture.
Audio sync and AC3 formatting now done in xine-lib/src/xine-engine/audio_out.c
Audio out now done in xine-lib/src/audio_out/*
Currently, only xine-lib/src/audio_oss_out.c is active.
Re-support of others will follow as other people implement them.
ao_functions split into ao_instance and ao_driver
and function in a similar fashion as video_out.
All decoder plugins now call ao_instance instead of ao_functions.
ao_instance is used in xine-lib/src/xine-engine/audio_out.c
ao_driver is used in xine-lib/src/audio_out/*
See also changes in xine-ui files
CVS patchset: 459
CVS date: 2001/08/21 19:39:50
Diffstat (limited to 'src/xine-engine')
-rw-r--r-- | src/xine-engine/Makefile.am | 4 | ||||
-rw-r--r-- | src/xine-engine/audio_out.c | 336 | ||||
-rw-r--r-- | src/xine-engine/audio_out.h | 113 | ||||
-rw-r--r-- | src/xine-engine/load_plugins.c | 8 | ||||
-rw-r--r-- | src/xine-engine/xine.c | 9 | ||||
-rw-r--r-- | src/xine-engine/xine_internal.h | 10 |
6 files changed, 442 insertions, 38 deletions
diff --git a/src/xine-engine/Makefile.am b/src/xine-engine/Makefile.am index c71bc26fe..0448aba9d 100644 --- a/src/xine-engine/Makefile.am +++ b/src/xine-engine/Makefile.am @@ -10,7 +10,7 @@ lib_LTLIBRARIES = libxine.la libxine_la_SOURCES = xine.c metronom.c configfile.c buffer.c monitor.c \ utils.c load_plugins.c video_decoder.c \ - audio_decoder.c video_out.c events.c + audio_decoder.c video_out.c audio_out.c resample.c events.c libxine_la_LIBADD = cpu_accel.lo \ $(THREAD_LIBS) \ $(DYNAMIC_LD_LIBS) \ @@ -20,7 +20,7 @@ libxine_la_LDFLAGS = -version-info 5:0:5 include_HEADERS = buffer.h metronom.h configfile.h \ monitor.h cpu_accel.h attributes.h utils.h \ - audio_out.h video_out.h xine_internal.h spu_decoder.h \ + audio_out.h resample.h video_out.h xine_internal.h spu_decoder.h \ events.h noinst_HEADERS = bswap.h diff --git a/src/xine-engine/audio_out.c b/src/xine-engine/audio_out.c new file mode 100644 index 000000000..19273fdfd --- /dev/null +++ b/src/xine-engine/audio_out.c @@ -0,0 +1,336 @@ +/* + * Copyright (C) 2000, 2001 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 self program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * $Id: audio_out.c,v 1.3 2001/08/21 19:39:50 jcdutton Exp $ + * + * 20-8-2001 First implementation of Audio sync and Audio driver separation. + * Copyright (C) 2001 James Courtier-Dutton James@superbug.demon.co.uk + * + * General Programming Guidelines: - + * New concept of an "audio_frame". + * An audio_frame consists of all the samples required to fill every audio channel to a full amount of bits. + * So, it does not mater how many bits per sample, or how many audio channels are being used, the number of audio_frames is the same. + * E.g. 16 bit stereo is 4 bytes, but one frame. + * 16 bit 5.1 surround is 12 bytes, but one frame. + * The purpose of this is to make the audio_sync code a lot more readable, rather than having to multiply by the amount of channels all the time + * when dealing with audio_bytes instead of audio_frames. + * + * The number of samples passed to/from the audio driver is also sent in units of audio_frames. + */ + +/* required for swab() */ +#define _XOPEN_SOURCE 500 +/* required for FNDELAY decl */ +#define _BSD_SOURCE 1 + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <fcntl.h> +#include <math.h> +#include <unistd.h> +#if defined(__OpenBSD__) +#include <soundcard.h> +#elif defined(__FreeBSD__) +#include <machine/soundcard.h> +#else +#if defined(__linux__) +#include <linux/config.h> /* Check for DEVFS */ +#endif +#include <sys/soundcard.h> +#endif +#include <sys/ioctl.h> +#include <inttypes.h> + +#include "xine_internal.h" +#include "monitor.h" +#include "audio_out.h" +#include "resample.h" +#include "metronom.h" +#include "utils.h" + +#ifndef AFMT_S16_NE +# if defined(sparc) || defined(__sparc__) || defined(PPC) +/* Big endian machines */ +# define AFMT_S16_NE AFMT_S16_BE +# else +# define AFMT_S16_NE AFMT_S16_LE +# endif +#endif + +#ifndef AFMT_AC3 +# define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */ +#endif + +#define AO_OUT_OSS_IFACE_VERSION 1 + +#define AUDIO_NUM_FRAGMENTS 15 +#define AUDIO_FRAGMENT_SIZE 8192 + +/* bufsize must be a multiple of 3 and 5 for 5.0 and 5.1 channel playback! */ +#define ZERO_BUF_SIZE 15360 + +#define GAP_TOLERANCE 5000 +#define MAX_GAP 90000 + +#ifdef CONFIG_DEVFS_FS +#define DSP_TEMPLATE "/dev/sound/dsp%d" +#else +#define DSP_TEMPLATE "/dev/dsp%d" +#endif + +/* + * open the audio device for writing to + */ +static int ao_open(ao_instance_t *self, + uint32_t bits, uint32_t rate, int mode) +{ + int result; + if(result=self->driver->open(self->driver,bits,rate,mode)<0) { + printf("open failed!\n"); + return -1; + }; +// self->frame_rate_factor = (double) self->output_frame_rate / (double) self->input_frame_rate; + self->mode = mode; + self->input_frame_rate = rate; + self->frames_in_buffer = 0; + self->audio_started = 0; + self->last_audio_vpts = 0; + + self->output_frame_rate=rate; + self->num_channels = self->driver->num_channels(self->driver); + + self->frame_rate_factor = (double) 1 / (double) 1; + self->audio_step = (uint32_t) 90000 * (uint32_t) 32768 + / self->input_frame_rate; + self->frames_per_kpts = self->output_frame_rate * self->num_channels * 2 * 1024 / 90000; + xprintf (VERBOSE|AUDIO, "audio_out : audio_step %d pts per 32768 frames\n", self->audio_step); + + self->metronom->set_audio_rate(self->metronom, self->audio_step); + + + return 1; +} + +static void ao_fill_gap (ao_instance_t *self, uint32_t pts_len) { + + int num_bytes ; + xprintf (VERBOSE|AUDIO, "audio_out : fill_gap\n"); + + if (pts_len > MAX_GAP) + pts_len = MAX_GAP; + num_bytes = pts_len * self->frames_per_kpts / 1024; + num_bytes = (num_bytes / (2*self->num_channels)) * (2*self->num_channels); + + if(self->mode == AO_CAP_MODE_AC3) return; /* FIXME */ + + printf ("audio_out: inserting %d 0-bytes to fill a gap of %d pts\n",num_bytes, pts_len); + + self->frames_in_buffer += num_bytes; + + while (num_bytes > 0) { + if (num_bytes > ZERO_BUF_SIZE) { + self->driver->write(self->driver, self->zero_space, ZERO_BUF_SIZE); + num_bytes -= ZERO_BUF_SIZE; + } else { + self->driver->write(self->driver, self->zero_space, num_bytes); + num_bytes = 0; + } + } +} + + + +static int ao_write(ao_instance_t *self, + int16_t* output_frames, uint32_t num_frames, + uint32_t pts_) +{ + uint32_t vpts, buffer_vpts; + int32_t gap; + int bDropPackage; + int pos; + + if (self->driver<0) + return 1; + + vpts = self->metronom->got_audio_samples (self->metronom, pts_, num_frames); + + xprintf (VERBOSE|AUDIO, "audio_out: got %d frames, vpts=%d pts=%d\n", + num_frames, vpts,pts_); + + if (vpts<self->last_audio_vpts) { + /* reject self */ + xprintf (VERBOSE|AUDIO, "audio_out: rejected frame vpts=%d, last_audio_vpts=%d\n", vpts,self->last_audio_vpts) + + return 1; + } + + self->last_audio_vpts = vpts; + + bDropPackage = 0; + + if ( self->audio_has_realtime || !self->audio_started ) { + + /* + * where, in the timeline is the "end" of the audio buffer at the moment? + */ + + buffer_vpts = self->metronom->get_current_time (self->metronom); + + if (self->audio_started) { + pos = self->driver->delay(self->driver); + } else + pos = 0; + if ( (self->mode==AO_CAP_MODE_AC3) && (pos>10) ) pos-=10; /* External AC3 decoder delay correction */ + + if (pos>self->frames_in_buffer) /* buffer ran dry */ + self->frames_in_buffer = pos; + + buffer_vpts += (self->frames_in_buffer - pos) * 1024 / self->frames_per_kpts; + + /* + * calculate gap: + */ + + gap = vpts - buffer_vpts; + xprintf (VERBOSE|AUDIO, "audio_out: buff=%d pos=%d buf_vpts=%d gap=%d\n", + self->frames_in_buffer, pos,buffer_vpts,gap); + + if (gap>GAP_TOLERANCE) { + ao_fill_gap (self, gap); + + /* keep xine responsive */ + + if (gap>MAX_GAP) + return 0; + + } else if (gap<-GAP_TOLERANCE) { + bDropPackage = 1; + xprintf (VERBOSE|AUDIO, "audio_out: audio package (vpts = %d %d)" + "dropped\n", vpts, gap); + } + + } /* has realtime */ + + /* + * resample and output frames + */ + if(self->mode == AO_CAP_MODE_AC3) bDropPackage=0; + + if (!bDropPackage) { + int num_output_frames = num_frames * (self->output_frame_rate) / self->input_frame_rate; + + if ((!self->do_resample) && (self->mode != AO_CAP_MODE_AC3)) { + xprintf (VERBOSE|AUDIO, "audio_out: writing without resampling\n"); + self->driver->write(self->driver, output_frames, + num_output_frames ); + } else switch (self->mode) { + case AO_CAP_MODE_MONO: + audio_out_resample_mono (output_frames, num_frames, + self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + break; + case AO_CAP_MODE_STEREO: + audio_out_resample_stereo (output_frames, num_frames, + self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + break; + case AO_CAP_MODE_4CHANNEL: + audio_out_resample_4channel (output_frames, num_frames, + self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + break; + case AO_CAP_MODE_5CHANNEL: + audio_out_resample_5channel (output_frames, num_frames, + self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + break; + case AO_CAP_MODE_5_1CHANNEL: + audio_out_resample_6channel (output_frames, num_frames, + self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + break; + case AO_CAP_MODE_AC3: + num_output_frames = (num_frames+8)/4; + self->frame_buffer[0] = 0xf872; //spdif syncword + self->frame_buffer[1] = 0x4e1f; // ............. + self->frame_buffer[2] = 0x0001; // AC3 data + self->frame_buffer[3] = num_frames * 8; + self->frame_buffer[4] = 0x0b77; // AC3 syncwork already in output_frames + + // ac3 seems to be swabbed data + swab(output_frames,self->frame_buffer+4, num_frames ); + self->driver->write(self->driver, self->zero_space, 2); /* Prevents crackle at start. */ + self->driver->write(self->driver, self->frame_buffer, num_output_frames); + self->driver->write(self->driver, self->zero_space, 1534-num_output_frames); + num_output_frames=num_output_frames; + break; + } + + xprintf (AUDIO|VERBOSE, "audio_out :audio package written\n"); + + /* + * step values + */ + + self->frames_in_buffer += num_output_frames ; + self->audio_started = 1; + } + + return 1; + +} + + +static void ao_close(ao_instance_t *self) +{ + self->driver->close(self->driver); +} + +static uint32_t ao_get_capabilities (ao_instance_t *self) { + uint32_t result; + result=self->driver->get_capabilities(self->driver); + return result; +} + +ao_instance_t *ao_new_instance (ao_driver_t *driver, metronom_t *metronom) { + + ao_instance_t *self; + + self = xmalloc (sizeof (ao_instance_t)) ; + self->driver = driver; + self->metronom = metronom; + + self->open = ao_open; + self->write = ao_write; + self->close = ao_close; + self->get_capabilities = ao_get_capabilities; + self->audio_loop_running = 0; + self->frame_buffer = malloc (40000); + memset (self->frame_buffer, 0, 40000); + self->zero_space = malloc (ZERO_BUF_SIZE); + memset (self->zero_space, 0, ZERO_BUF_SIZE); + return self; +} + diff --git a/src/xine-engine/audio_out.h b/src/xine-engine/audio_out.h index c3e1fffbc..27b4c2f2d 100644 --- a/src/xine-engine/audio_out.h +++ b/src/xine-engine/audio_out.h @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * - * $Id: audio_out.h,v 1.8 2001/07/20 22:37:56 guenter Exp $ + * $Id: audio_out.h,v 1.9 2001/08/21 19:39:50 jcdutton Exp $ */ #ifndef HAVE_AUDIO_OUT_H #define HAVE_AUDIO_OUT_H @@ -37,13 +37,13 @@ extern "C" { #define AUDIO_OUT_IFACE_VERSION 1 /* - * ao_functions_s contains the functions every audio output + * ao_driver_s contains the driver every audio output * driver plugin has to implement. */ -typedef struct ao_functions_s ao_functions_t; +typedef struct ao_driver_s ao_driver_t; -struct ao_functions_s { +struct ao_driver_s { /* * @@ -52,12 +52,7 @@ struct ao_functions_s { * * See AO_CAP_* bellow. */ - uint32_t (*get_capabilities) (ao_functions_t *this); - - /* - * connect this driver to the xine engine - */ - void (*connect) (ao_functions_t *this, metronom_t *metronom); + uint32_t (*get_capabilities) (ao_driver_t *this); /* * open the driver and make it ready to receive audio data @@ -65,8 +60,21 @@ struct ao_functions_s { * * return value: <=0 : failure, 1 : ok */ + int (*open)(ao_driver_t *this, uint32_t bits, uint32_t rate, int mode); + + /* return the number of audio channels + */ + int (*num_channels)(ao_driver_t *self_gen); + + /* return the number of bytes per frame. + * A frame is equivalent to one sample being output on every audio channel. + */ + int (*bytes_per_frame)(ao_driver_t *self_gen); - int (*open)(ao_functions_t *this, uint32_t bits, uint32_t rate, int mode); + /* return the delay is frames measured by + * looking at pending samples in the audio output device + */ + int (*delay)(ao_driver_t *self_gen); /* * write audio data to output buffer @@ -76,24 +84,20 @@ struct ao_functions_s { * 0 => audio samples were not yet processed, * call write_audio_data with the _same_ samples again */ - - int (*write_audio_data)(ao_functions_t *this, - int16_t* audio_data, uint32_t num_samples, - uint32_t pts); + int (*write)(ao_driver_t *this, + int16_t* audio_data, uint32_t num_samples); /* * this is called when the decoder no longer uses the audio * output driver - the driver should get ready to get opened() again */ - - void (*close)(ao_functions_t *this); + void (*close)(ao_driver_t *this); /* * shut down this audio output driver plugin and * free all resources allocated */ - - void (*exit) (ao_functions_t *this); + void (*exit) (ao_driver_t *this); /* * Get, Set a property of audio driver. @@ -103,19 +107,82 @@ struct ao_functions_s { * * See AC_PROP_* bellow for available properties. */ - int (*get_property) (ao_functions_t *this, int property); + int (*get_property) (ao_driver_t *this, int property); - int (*set_property) (ao_functions_t *this, int property, int value); + int (*set_property) (ao_driver_t *this, int property, int value); }; +/* + * ao_instance_s contains the instance every audio decoder talks to + */ +typedef struct ao_instance_s ao_instance_t; + +struct ao_instance_s { + uint32_t (*get_capabilities) (ao_instance_t *this); /* for constants see below */ + + /* open display driver for video output */ + int (*open) (ao_instance_t *this, + uint32_t bits, uint32_t rate, int mode); + + /* + * write audio data to output buffer + * audio driver must sync sample playback with metronom + * return value: + * 1 => audio samples were processed ok + * 0 => audio samples were not yet processed, + * call write_audio_data with the _same_ samples again + */ + + int (*write)(ao_driver_t *this, + int16_t* audio_data, uint32_t num_frames, + uint32_t pts); + + /* audio driver is no longer used by decoder => close */ + void (*close) (ao_instance_t *self); + + /* called on xine exit */ + void (*exit) (ao_instance_t *this); + + /* private stuff */ + + ao_driver_t *driver; + metronom_t *metronom; + + int audio_loop_running; + pthread_t audio_thread; + int audio_step; /* pts per 32 768 samples (sample = #bytes/2) */ + int32_t frames_per_kpts; /* bytes per 1024/90000 sec */ + int32_t output_frame_rate, input_frame_rate; + double frame_rate_factor; + uint32_t num_channels; + uint32_t frames_in_buffer; /* a frame is equivalent to one sample in each channel. */ + int audio_started; + int audio_has_realtime; /* OSS driver supports real-time */ + uint32_t last_audio_vpts; + int resample_conf; + int do_resample; + int mode; + uint16_t *frame_buffer; + int16_t *zero_space; + int pts_per_half_frame; + int pts_per_frame; + + int num_frames_delivered; + int num_frames_skipped; + int num_frames_discarded; +}; +/* This initiates the audio_out sync routines + * found in ./src/xine-engine/audio_out.c + */ +ao_instance_t *ao_new_instance (ao_driver_t *driver, metronom_t *metronom) ; /* * to build a dynamic audio output plugin, - * you have to implement these functions: + * you have to implement these driver: * * - * ao_functions_t *init_audio_out_plugin (config_values_t *config) + * ao_driver_t *init_audio_out_plugin (config_values_t *config) * * init this plugin, check if device is available * diff --git a/src/xine-engine/load_plugins.c b/src/xine-engine/load_plugins.c index 619717545..56936d1a1 100644 --- a/src/xine-engine/load_plugins.c +++ b/src/xine-engine/load_plugins.c @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * - * $Id: load_plugins.c,v 1.37 2001/08/14 01:38:17 guenter Exp $ + * $Id: load_plugins.c,v 1.38 2001/08/21 19:39:50 jcdutton Exp $ * * * Load input/demux/audio_out/video_out/codec plugins @@ -713,11 +713,11 @@ char **xine_list_audio_output_plugins(void) { return plugin_ids; } -ao_functions_t *xine_load_audio_output_plugin(config_values_t *config, +ao_driver_t *xine_load_audio_output_plugin(config_values_t *config, char *id) { DIR *dir; - ao_functions_t *aod = NULL; + ao_driver_t *aod = NULL; dir = opendir (XINE_PLUGINDIR); @@ -757,7 +757,7 @@ ao_functions_t *xine_load_audio_output_plugin(config_values_t *config, if((initplug = dlsym(plugin, "init_audio_out_plugin")) != NULL) { - aod = (ao_functions_t *) initplug(config); + aod = (ao_driver_t *) initplug(config); if (aod) printf("load_plugins: audio output plugin %s successfully" diff --git a/src/xine-engine/xine.c b/src/xine-engine/xine.c index 83d1e4ebe..17fd9385b 100644 --- a/src/xine-engine/xine.c +++ b/src/xine-engine/xine.c @@ -17,7 +17,7 @@ * 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.c,v 1.44 2001/08/20 20:49:51 f1rmb Exp $ + * $Id: xine.c,v 1.45 2001/08/21 19:39:50 jcdutton Exp $ * * top-level xine functions * @@ -452,7 +452,7 @@ static void event_handler(xine_t *xine, event_t *event, void *data) { } xine_t *xine_init (vo_driver_t *vo, - ao_functions_t *ao, + ao_driver_t *ao, config_values_t *config, gui_stream_end_cb_t stream_end_cb, gui_get_next_mrl_cb_t get_next_mrl_cb, @@ -515,8 +515,9 @@ xine_t *xine_init (vo_driver_t *vo, video_decoder_init (this); if(ao) { - this->audio_out = ao; - this->audio_out->connect (this->audio_out, this->metronom); + this->audio_out = ao_new_instance (ao, this->metronom); +// this->audio_out = ao; +// this->audio_out->connect (this->audio_out, this->metronom); } audio_decoder_init (this); printf("xine_init returning\n"); diff --git a/src/xine-engine/xine_internal.h b/src/xine-engine/xine_internal.h index 13fc9803b..8c91426e6 100644 --- a/src/xine-engine/xine_internal.h +++ b/src/xine-engine/xine_internal.h @@ -17,7 +17,7 @@ * 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_internal.h,v 1.38 2001/08/17 16:15:37 f1rmb Exp $ + * $Id: xine_internal.h,v 1.39 2001/08/21 19:39:50 jcdutton Exp $ * */ @@ -93,7 +93,7 @@ struct audio_decoder_s { int (*can_handle) (audio_decoder_t *this, int buf_type); - void (*init) (audio_decoder_t *this, ao_functions_t *audio_out); + void (*init) (audio_decoder_t *this, ao_instance_t *audio_out); void (*decode_data) (audio_decoder_t *this, buf_element_t *buf); @@ -167,7 +167,7 @@ struct xine_s { video_decoder_t *cur_video_decoder_plugin; int video_finished; - ao_functions_t *audio_out; + ao_instance_t *audio_out; fifo_buffer_t *audio_fifo; pthread_t audio_thread; audio_decoder_t *audio_decoder_plugins[DECODER_PLUGIN_MAX]; @@ -202,7 +202,7 @@ config_values_t *config_file_init (char *filename); */ xine_t *xine_init (vo_driver_t *vo, - ao_functions_t *ao, + ao_driver_t *ao, config_values_t *config, gui_stream_end_cb_t stream_end_cb, gui_get_next_mrl_cb_t get_next_mrl_cb, @@ -447,7 +447,7 @@ char **xine_list_audio_output_plugins (); * load a specific audio output plugin */ -ao_functions_t *xine_load_audio_output_plugin(config_values_t *config, char *id); +ao_driver_t *xine_load_audio_output_plugin(config_values_t *config, char *id); /** * @defgroup eventgroup Sending events |