diff options
Diffstat (limited to 'src/xine-engine')
-rw-r--r-- | src/xine-engine/audio_decoder.c | 7 | ||||
-rw-r--r-- | src/xine-engine/audio_out.h | 14 | ||||
-rw-r--r-- | src/xine-engine/metronom.c | 259 | ||||
-rw-r--r-- | src/xine-engine/metronom.h | 40 | ||||
-rw-r--r-- | src/xine-engine/video_decoder.c | 24 | ||||
-rw-r--r-- | src/xine-engine/xine.c | 20 |
6 files changed, 215 insertions, 149 deletions
diff --git a/src/xine-engine/audio_decoder.c b/src/xine-engine/audio_decoder.c index e7deee9ed..249f70777 100644 --- a/src/xine-engine/audio_decoder.c +++ b/src/xine-engine/audio_decoder.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: audio_decoder.c,v 1.21 2001/06/17 19:14:26 guenter Exp $ + * $Id: audio_decoder.c,v 1.22 2001/06/23 19:45:47 guenter Exp $ * * * functions that implement audio decoding @@ -72,10 +72,15 @@ void *audio_decoder_loop (void *this_gen) { this->audio_track_map[0] = 0; this->audio_track_map_entries = 0; + + this->metronom->audio_stream_start (this->metronom); break; case BUF_CONTROL_END: + + this->metronom->audio_stream_end (this->metronom); + if (this->cur_audio_decoder_plugin) { this->cur_audio_decoder_plugin->close (this->cur_audio_decoder_plugin); this->cur_audio_decoder_plugin = NULL; diff --git a/src/xine-engine/audio_out.h b/src/xine-engine/audio_out.h index 4c983bc0d..6b4e7a173 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.5 2001/06/18 10:49:31 guenter Exp $ + * $Id: audio_out.h,v 1.6 2001/06/23 19:45:47 guenter Exp $ */ #ifndef HAVE_AUDIO_OUT_H #define HAVE_AUDIO_OUT_H @@ -65,13 +65,17 @@ struct ao_functions_s { int (*open)(ao_functions_t *this, uint32_t bits, uint32_t rate, int mode); /* - * write audio data to output buffer - may block + * 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 */ - void (*write_audio_data)(ao_functions_t *this, - int16_t* audio_data, uint32_t num_samples, - uint32_t pts); + int (*write_audio_data)(ao_functions_t *this, + int16_t* audio_data, uint32_t num_samples, + uint32_t pts); /* * this is called when the decoder no longer uses the audio diff --git a/src/xine-engine/metronom.c b/src/xine-engine/metronom.c index 224348c3a..89a0e0567 100644 --- a/src/xine-engine/metronom.c +++ b/src/xine-engine/metronom.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: metronom.c,v 1.8 2001/06/04 17:13:36 guenter Exp $ + * $Id: metronom.c,v 1.9 2001/06/23 19:45:47 guenter Exp $ */ #ifdef HAVE_CONFIG_H @@ -38,35 +38,79 @@ #define MAX_PTS_TOLERANCE 5000 #define MAX_VIDEO_DELTA 1600 +#define MAX_AUDIO_DELTA 1600 #define AUDIO_SAMPLE_NUM 32768 -#define MAX_WRAP_TOLERANCE 90000 +#define WRAP_START_TIME 100000 +#define WRAP_TRESHOLD 30000 +#define MAX_NUM_WRAP_DIFF 100 -static void metronom_reset (metronom_t *this) { +static void metronom_video_stream_start (metronom_t *this) { pthread_mutex_lock (&this->lock); + printf ("video stream start...\n"); + this->pts_per_frame = 3000; this->video_vpts = 0; - this->audio_vpts = 0; this->video_pts_delta = 0; - this->audio_pts_delta = 0; this->last_video_pts = 0; this->num_video_vpts_guessed = 1; + + this->video_wrap_offset = 0; + this->wrap_diff_counter = 0; + + this->video_stream_running = 1; + this->video_stream_starting = 1; + + if (this->have_audio) { + while (!this->audio_stream_running) { + printf ("waiting for audio to start...\n"); + pthread_cond_wait (&this->audio_started, &this->lock); + } + } + pthread_cond_signal (&this->video_started); + + pthread_mutex_unlock (&this->lock); + + printf ("video stream start...done\n"); + + metronom_start_clock (this, 0); +} + +static void metronom_audio_stream_start (metronom_t *this) { + + pthread_mutex_lock (&this->lock); + + printf ("audio stream start...\n"); + + this->audio_vpts = 0; + + this->audio_pts_delta = 0; + this->num_audio_samples_guessed = 1; this->last_audio_pts = 0; - this->sync_pts = 0; - this->sync_vpts = 0; - - this->video_wrap_offset = 0; this->audio_wrap_offset = 0; + this->wrap_diff_counter = 0; + + this->audio_stream_running = 1; + this->audio_stream_starting = 1; - this->av_offset = 0; + while (!this->video_stream_running) { + printf ("waiting for video to start...\n"); + pthread_cond_wait (&this->video_started, &this->lock); + } + + pthread_cond_signal (&this->audio_started); pthread_mutex_unlock (&this->lock); + + printf ("audio stream start...done\n"); + + metronom_start_clock (this, 0); } static void metronom_set_video_rate (metronom_t *this, uint32_t pts_per_frame) { @@ -93,9 +137,9 @@ static void metronom_set_audio_rate (metronom_t *this, uint32_t pts_per_smpls) { } static uint32_t metronom_got_spu_packet (metronom_t *this, uint32_t pts) { - /* FIXME: Nasty hack */ + /* FIXME: not tested */ - return this->sync_pts; + return pts + this->video_wrap_offset; } static uint32_t metronom_got_video_frame (metronom_t *this, uint32_t pts) { @@ -104,81 +148,82 @@ static uint32_t metronom_got_video_frame (metronom_t *this, uint32_t pts) { pthread_mutex_lock (&this->lock); - /* pts = 0; */ - if (pts) { /* + * first video pts ? + */ + if (this->video_stream_starting) { + this->video_stream_starting = 0; + + this->video_wrap_offset = -1 * pts; + + printf ("metronom: first video pts => offset = %d\n", this->video_wrap_offset); + + } + + /* * did a wrap-around occur? */ - if ( (pts+this->video_wrap_offset+MAX_WRAP_TOLERANCE)<this->last_video_pts) { - this->video_wrap_offset = this->last_video_pts - pts + if ( ( (pts + WRAP_TRESHOLD) <this->last_video_pts) + && (pts<WRAP_START_TIME) ) { + + this->video_wrap_offset += this->last_video_pts - pts + this->num_video_vpts_guessed *(this->pts_per_frame + this->video_pts_delta); - + printf ("metronom: video pts wraparound detected, wrap_offset = %d\n", this->video_wrap_offset); + } + + /* + * audio and video wrap are not allowed to differ + * for too long + */ + + if ( !this->audio_stream_starting + && (this->video_wrap_offset != this->audio_wrap_offset)) { + this->wrap_diff_counter++; + if (this->wrap_diff_counter > MAX_NUM_WRAP_DIFF) { + + printf ("metronom: forcing video_wrap (%d) and audio wrap (%d)", + this->video_wrap_offset, this->audio_wrap_offset); + + if (this->video_wrap_offset > this->audio_wrap_offset) + this->audio_wrap_offset = this->video_wrap_offset; + else + this->video_wrap_offset = this->audio_wrap_offset; + + printf ("to %d\n", this->video_wrap_offset); + + this->wrap_diff_counter = 0; + } } - pts += this->video_wrap_offset; + vpts = pts + this->video_wrap_offset; /* * calc delta to compensate wrong framerates */ - if (this->last_video_vpts && (pts>this->last_video_pts)) { + if (this->last_video_pts && (pts>this->last_video_pts)) { int32_t vpts_diff; - uint32_t synced_vpts ; - int32_t diff; - diff = pts - this->last_video_pts; - synced_vpts = this->last_video_vpts + diff; - vpts_diff = synced_vpts - this->video_vpts; + vpts_diff = vpts - this->video_vpts; this->video_pts_delta += vpts_diff / (this->num_video_vpts_guessed); if (abs(this->video_pts_delta) >= MAX_VIDEO_DELTA) this->video_pts_delta = 0; - - this->num_video_vpts_guessed = 0; } - /* - * sync if necessary and possible - */ - - if (this->sync_vpts && (pts>this->sync_pts)) { - - int32_t vpts_diff; - uint32_t synced_vpts ; - int32_t diff; - - diff = pts - this->sync_pts; - synced_vpts = this->sync_vpts + diff; - vpts_diff = synced_vpts - this->video_vpts; - - xprintf (METRONOM | VERBOSE, "metronom: video calced vpts : %d <=> synced vpts : %d (diff: %d, delta: %d)\n", - this->video_vpts, synced_vpts, vpts_diff, this->video_pts_delta); - - if (abs(vpts_diff)>MAX_PTS_TOLERANCE) { - if ( synced_vpts>this->video_vpts ) { - this->video_vpts = synced_vpts; - } - } else { - xprintf (METRONOM | VERBOSE, "metronom: video tolerating diff\n"); - } - - } else - xprintf (METRONOM | VERBOSE, "metronom: video not synced on this one\n"); - - this->sync_pts = pts; - this->sync_vpts = this->video_vpts; - this->last_video_vpts = this->video_vpts; + this->num_video_vpts_guessed = 0; this->last_video_pts = pts; - } - - vpts = this->video_vpts; + this->video_vpts = vpts; + } else + vpts = this->video_vpts; + this->video_vpts += this->pts_per_frame + this->video_pts_delta; this->num_video_vpts_guessed++ ; @@ -203,57 +248,77 @@ static uint32_t metronom_got_audio_samples (metronom_t *this, uint32_t pts, uint int32_t diff; /* - * did a wrap-around occur? + * first audio pts ? */ + if (this->audio_stream_starting) { + this->audio_stream_starting = 0; + + this->audio_wrap_offset = -1 * pts; - if ((pts+this->audio_wrap_offset+MAX_WRAP_TOLERANCE)<this->last_audio_pts) { + printf ("metronom: first audio pts => offset = %d\n", this->audio_wrap_offset); + } - this->audio_wrap_offset = this->last_audio_pts - pts + /* + * did a wrap-around occur? + */ + if ( ( (pts + WRAP_TRESHOLD) < this->last_audio_pts ) + && (pts<WRAP_START_TIME) ) { + + this->audio_wrap_offset += this->last_audio_pts - pts + this->num_audio_samples_guessed *(this->audio_pts_delta + this->pts_per_smpls) / AUDIO_SAMPLE_NUM ; - + printf ("metronom: audio pts wraparound detected, wrap_offset = %d\n", this->audio_wrap_offset); - } - pts += this->audio_wrap_offset; + /* + * audio and video wrap are not allowed to differ + * for too long + */ - diff = pts - this->sync_pts; + if ( !this->video_stream_starting + && this->video_wrap_offset != this->audio_wrap_offset) { + this->wrap_diff_counter++; - if (this->sync_vpts && (pts>this->sync_pts)) { + if (this->wrap_diff_counter > MAX_NUM_WRAP_DIFF) { - int32_t vpts_diff; - uint32_t synced_vpts = this->sync_vpts + diff; + printf ("metronom: forcing video_wrap (%d) and audio wrap (%d)", + this->video_wrap_offset, this->audio_wrap_offset); - vpts_diff = synced_vpts - this->audio_vpts; + if (this->video_wrap_offset > this->audio_wrap_offset) + this->audio_wrap_offset = this->video_wrap_offset; + else + this->video_wrap_offset = this->audio_wrap_offset; - xprintf (METRONOM | VERBOSE, "metronom: audio calced vpts : %d <=> synced vpts : %d (diff: %d, delta: %d)\n", - this->audio_vpts, synced_vpts, vpts_diff, this->audio_pts_delta); - if (abs(vpts_diff)>5000) { + printf ("to %d\n", this->video_wrap_offset); - /* calc delta for wrong samplerates */ + this->wrap_diff_counter = 0; + } + } - this->audio_pts_delta += vpts_diff*AUDIO_SAMPLE_NUM / (this->num_audio_samples_guessed); - - if (abs(this->audio_pts_delta) >= 10000) - this->audio_pts_delta = 0; - - if (synced_vpts>this->audio_vpts) - this->audio_vpts = synced_vpts; - - } else - xprintf (METRONOM | VERBOSE, "metronom: audio tolerating diff\n"); + vpts = pts + this->audio_wrap_offset; + + /* + * calc delta to compensate wrong samplerates + */ + + if (this->last_audio_pts && (pts>this->last_audio_pts)) { + int32_t vpts_diff; - } else - xprintf (METRONOM | VERBOSE, "metronom: audio not synced on this one\n"); + vpts_diff = vpts - this->audio_vpts; + + this->audio_pts_delta += vpts_diff*AUDIO_SAMPLE_NUM / (this->num_audio_samples_guessed); + + if (abs(this->audio_pts_delta) >= MAX_AUDIO_DELTA) + this->audio_pts_delta = 0; + } - this->sync_pts = pts; - this->sync_vpts = this->audio_vpts; this->num_audio_samples_guessed = 0; this->last_audio_pts = pts; - } - - vpts = this->audio_vpts; + this->audio_vpts = vpts; + } else + vpts = this->audio_vpts; + this->audio_vpts += nsamples * (this->audio_pts_delta + this->pts_per_smpls) / AUDIO_SAMPLE_NUM; this->num_audio_samples_guessed += nsamples; @@ -360,11 +425,12 @@ static void metronom_adjust_clock(metronom_t *this, uint32_t desired_pts) pthread_mutex_unlock (&this->lock); } -metronom_t * metronom_init () { +metronom_t * metronom_init (int have_audio) { metronom_t *this = xmalloc (sizeof (metronom_t)); - this->reset = metronom_reset; + this->audio_stream_start= metronom_audio_stream_start; + this->video_stream_start= metronom_video_stream_start; this->set_video_rate = metronom_set_video_rate; this->get_video_rate = metronom_get_video_rate; this->set_audio_rate = metronom_set_audio_rate; @@ -380,8 +446,11 @@ metronom_t * metronom_init () { this->adjust_clock = metronom_adjust_clock; pthread_mutex_init (&this->lock, NULL); + pthread_cond_init (&this->video_started, NULL); + pthread_cond_init (&this->audio_started, NULL); - this->reset (this); + this->av_offset = 0; + this->have_audio = have_audio; return this; } diff --git a/src/xine-engine/metronom.h b/src/xine-engine/metronom.h index e051fc0da..5fae3eb9d 100644 --- a/src/xine-engine/metronom.h +++ b/src/xine-engine/metronom.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: metronom.h,v 1.3 2001/05/01 21:55:23 guenter Exp $ + * $Id: metronom.h,v 1.4 2001/06/23 19:45:47 guenter Exp $ * * metronom: general pts => virtual calculation/assoc * @@ -41,11 +41,18 @@ typedef struct metronom_s metronom_t ; struct metronom_s { /* - * clear all cached data, reset current vpts ... called if new input - * file is reached + * this is called to tell metronom to prepare for a new video stream */ - - void (*reset) (metronom_t *this); + + void (*video_stream_start) (metronom_t *this); + void (*video_stream_end) (metronom_t *this); + + /* + * this is called to tell metronom to prepare for a new audio stream + */ + + void (*audio_stream_start) (metronom_t *this); + void (*audio_stream_end) (metronom_t *this); /* * called by video output driver to inform metronom about current framerate @@ -118,7 +125,7 @@ struct metronom_s { /* * start metronom clock (no clock reset) */ - void (*start_clock) (metronom_t *this, uint32_t pts); + void (*start_clock) (metronom_t *this, int32_t pts); /* @@ -156,15 +163,11 @@ struct metronom_s { uint32_t video_vpts; uint32_t audio_vpts; - uint32_t sync_pts; - uint32_t sync_vpts; - - uint32_t video_wrap_offset; - uint32_t audio_wrap_offset; + int32_t video_wrap_offset; + int32_t audio_wrap_offset; + int wrap_diff_counter; - /* video delta for wrong framerates */ uint32_t last_video_pts; - uint32_t last_video_vpts; int num_video_vpts_guessed; int32_t video_pts_delta; @@ -178,8 +181,17 @@ struct metronom_s { int stopped ; pthread_mutex_t lock; + + int have_audio; + int video_stream_starting; + int video_stream_running; + int audio_stream_starting; + int audio_stream_running; + pthread_cond_t video_started; + pthread_cond_t audio_started; + }; -metronom_t *metronom_init (); +metronom_t *metronom_init (int have_audio); #endif diff --git a/src/xine-engine/video_decoder.c b/src/xine-engine/video_decoder.c index c5a6eebe5..caf40bbae 100644 --- a/src/xine-engine/video_decoder.c +++ b/src/xine-engine/video_decoder.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: video_decoder.c,v 1.25 2001/06/17 19:14:26 guenter Exp $ + * $Id: video_decoder.c,v 1.26 2001/06/23 19:45:47 guenter Exp $ * */ @@ -37,26 +37,14 @@ void *video_decoder_loop (void *this_gen) { while (running) { - /* printf ("video_decoder: getting buffer...\n"); */ + /* printf ("video_decoder: getting buffer...\n"); */ buf = this->video_fifo->get (this->video_fifo); if (buf->input_pos) this->cur_input_pos = buf->input_pos; - /* printf ("video_decoder: got buffer %d\n", buf->type); */ - - /* - * Call update status callback function if - * there is a video decoder initialized, like - * in mpeg1/2 playback. - */ - /* - if(this->cur_video_decoder_plugin != NULL) { - if(this->status == XINE_PLAY) - this->status_callback (this->status); - } - */ - + /* printf ("video_decoder: got buffer %d\n", buf->type); */ + switch (buf->type) { case BUF_CONTROL_START: @@ -69,6 +57,8 @@ void *video_decoder_loop (void *this_gen) { this->video_finished = 0; pthread_mutex_unlock (&this->xine_lock); + this->metronom->video_stream_start (this->metronom); + break; case BUF_VIDEO_MPEG: @@ -102,6 +92,8 @@ void *video_decoder_loop (void *this_gen) { case BUF_CONTROL_END: + this->metronom->video_stream_end (this->metronom); + if (this->cur_video_decoder_plugin) { this->cur_video_decoder_plugin->close (this->cur_video_decoder_plugin); this->cur_video_decoder_plugin = NULL; diff --git a/src/xine-engine/xine.c b/src/xine-engine/xine.c index eb08a1e7b..542958083 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.29 2001/06/21 17:34:24 guenter Exp $ + * $Id: xine.c,v 1.30 2001/06/23 19:45:47 guenter Exp $ * * top-level xine functions * @@ -93,9 +93,6 @@ void xine_stop (xine_t *this) { this->spu_fifo->clear(this->spu_fifo); - this->metronom->stop_clock (this->metronom); - this->metronom->reset(this->metronom); - pthread_mutex_unlock (&this->xine_lock); } @@ -219,12 +216,6 @@ static void xine_play_internal (xine_t *this, char *mrl, this->cur_demuxer_plugin->get_identifier()); /* - * metronom - */ - - this->metronom->reset(this->metronom); - - /* * start demuxer */ @@ -244,11 +235,6 @@ static void xine_play_internal (xine_t *this, char *mrl, this->status = XINE_PLAY; strncpy (this->cur_mrl, mrl, 1024); - /* - * start clock - */ - - this->metronom->start_clock (this->metronom, 0); } void xine_play (xine_t *this, char *MRL, int spos) { @@ -389,7 +375,7 @@ xine_t *xine_init (vo_driver_t *vo, * create a metronom */ - this->metronom = metronom_init (); + this->metronom = metronom_init (ao != NULL); /* * load input and demuxer plugins @@ -440,8 +426,6 @@ void xine_select_audio_channel (xine_t *this, int channel) { this->audio_channel = channel; - /* this->metronom->reset(this->metronom); */ - pthread_mutex_unlock (&this->xine_lock); } |