diff -ur --exclude-from dontdiff vanilla/main/configure main/configure --- vanilla/main/configure Sat Aug 10 11:29:23 2002 +++ main/configure Sat Aug 10 17:35:12 2002 @@ -1951,6 +1951,25 @@ fi echores "$_vsscanf" +echocheck "sys/sysinfo.h" +cat > $TMPC << EOF +#include +int main(void) { + struct sysinfo s_info; + sysinfo(&s_info); + return 0; +} +EOF +_sys_sysinfo=no +cc_check && _sys_sysinfo=yes +if test "$_sys_sysinfo" = yes ; then + _def_sys_sysinfo='#define HAVE_SYS_SYSINFO_H 1' + _inc_sysinfo='#include ' +else + _def_sys_sysinfo='#undef HAVE_SYS_SYSINFO_H' +fi +echores "$_sys_sysinfo" + ######### # VIDEO # @@ -4363,6 +4382,9 @@ /* Define this if your system has the header file for the OSS sound interface * in /usr/include */ $_def_soundcard + +/* Define this if your system has the sysinfo header */ +$_def_sys_sysinfo /* Define this if your system uses ftello() for off_t seeking */ Only in main: configure.log diff -ur --exclude-from dontdiff vanilla/main/libmpdemux/demuxer.c main/libmpdemux/demuxer.c --- vanilla/main/libmpdemux/demuxer.c Thu Aug 8 18:07:36 2002 +++ main/libmpdemux/demuxer.c Sat Aug 10 11:35:42 2002 @@ -155,6 +155,18 @@ extern void demux_close_avi(demuxer_t *demuxer); extern void demux_close_rawdv(demuxer_t* demuxer); +#ifdef USE_TV +#include "tv.h" +extern tvi_handle_t *tv_handler; +extern int tv_param_on; + +extern int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds, tvi_handle_t *tvh); +extern int demux_open_tv(demuxer_t *demuxer, tvi_handle_t *tvh); +#if defined(USE_TV) && defined(HAVE_TV_V4L) +extern void demux_close_tv(demuxer_t *demuxer, tvi_handle_t *tvh); +#endif +#endif + void free_demuxer(demuxer_t *demuxer){ int i; mp_msg(MSGT_DEMUXER,MSGL_V,"DEMUXER: freeing demuxer at %p \n",demuxer); @@ -177,6 +189,10 @@ demux_close_fli(demuxer); break; case DEMUXER_TYPE_NUV: demux_close_nuv(demuxer); break; +#if defined(USE_TV) && defined(HAVE_TV_V4L) + case DEMUXER_TYPE_TV: + demux_close_tv(demuxer, tv_handler); break; +#endif #ifdef HAVE_LIBDV095 case DEMUXER_TYPE_RAWDV: demux_close_rawdv(demuxer); break; @@ -267,15 +283,6 @@ int demux_nuv_fill_buffer(demuxer_t *demux); int demux_rtp_fill_buffer(demuxer_t *demux, demux_stream_t* ds); int demux_rawdv_fill_buffer(demuxer_t *demuxer); - -#ifdef USE_TV -#include "tv.h" -extern tvi_handle_t *tv_handler; -extern int tv_param_on; - -extern int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds, tvi_handle_t *tvh); -extern int demux_open_tv(demuxer_t *demuxer, tvi_handle_t *tvh); -#endif int demux_y4m_fill_buffer(demuxer_t *demux); int demux_audio_fill_buffer(demux_stream_t *ds); extern int demux_demuxers_fill_buffer(demuxer_t *demux,demux_stream_t *ds); diff -ur --exclude-from dontdiff vanilla/main/libmpdemux/tv.c main/libmpdemux/tv.c --- vanilla/main/libmpdemux/tv.c Thu May 23 16:53:11 2002 +++ main/libmpdemux/tv.c Sat Aug 10 11:35:42 2002 @@ -39,9 +39,7 @@ /* some default values */ int tv_param_audiorate = 44100; int tv_param_noaudio = 0; -#ifdef HAVE_TV_BSDBT848 int tv_param_immediate = 0; -#endif char *tv_param_freq = NULL; char *tv_param_channel = NULL; char *tv_param_norm = "pal"; @@ -272,14 +270,12 @@ printf("fps: %f, frametime: %f\n", sh_video->fps, sh_video->frametime); -#ifdef HAVE_TV_BSDBT848 /* If playback only mode, go to immediate mode, fail silently */ if(tv_param_immediate == 1) { funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0); tv_param_noaudio = 1; } -#endif /* set width */ funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w); @@ -370,6 +366,13 @@ return(funcs->start(tvh->priv)); } + +#if defined(USE_TV) && defined(HAVE_TV_V4L) +int demux_close_tv(demuxer_t *demuxer, tvi_handle_t *tvh) +{ + return(tvh->functions->uninit(tvh->priv)); +} +#endif /* ================== STREAM_TV ===================== */ tvi_handle_t *tvi_init_dummy(char *device); diff -ur --exclude-from dontdiff vanilla/main/libmpdemux/tv.h main/libmpdemux/tv.h --- vanilla/main/libmpdemux/tv.h Fri Apr 12 12:40:38 2002 +++ main/libmpdemux/tv.h Sat Aug 10 11:35:42 2002 @@ -72,9 +72,7 @@ #define TVI_CONTROL_IS_AUDIO 0x1 #define TVI_CONTROL_IS_VIDEO 0x2 #define TVI_CONTROL_IS_TUNER 0x3 -#ifdef HAVE_TV_BSDBT848 -#define TVI_CONTROL_IMMEDIATE 0x4 -#endif +#define TVI_CONTROL_IMMEDIATE 0x4 /* VIDEO controls */ #define TVI_CONTROL_VID_GET_FPS 0x101 diff -ur --exclude-from dontdiff vanilla/main/libmpdemux/tvi_v4l.c main/libmpdemux/tvi_v4l.c --- vanilla/main/libmpdemux/tvi_v4l.c Mon Jun 24 18:37:18 2002 +++ main/libmpdemux/tvi_v4l.c Sat Aug 10 17:36:39 2002 @@ -6,6 +6,9 @@ Some ideas are based on xawtv/libng's grab-v4l.c written by Gerd Knorr + Multithreading, a/v sync and native ALSA support by + Jindrich Makovicka + CODE IS UNDER DEVELOPMENT, NO FEATURE REQUESTS PLEASE! */ @@ -21,11 +24,19 @@ #include #include #include +#ifdef HAVE_ALSA9 +#include +#else #include +#endif #include #include #include #include +#include +#ifdef HAVE_SYS_SYSINFO +#include +#endif #include "mp_msg.h" #include "../libao2/afmt.h" @@ -43,6 +54,12 @@ #define MAX_AUDIO_CHANNELS 10 +#define AUD_BUF_SIZE 256 + +#define VID_BUF_SIZE_IMMEDIATE 2 + +#define AUD_SKEW_CNT 16 + typedef struct { /* general */ char *video_device; @@ -64,21 +81,61 @@ unsigned char *mmap; struct video_mmap *buf; int nbuf; - int queue; /* audio */ int audio_id; char *audio_device; struct video_audio audio[MAX_AUDIO_CHANNELS]; - int audio_fd; int audio_channels[MAX_AUDIO_CHANNELS]; int audio_format[MAX_AUDIO_CHANNELS]; int audio_samplesize[MAX_AUDIO_CHANNELS]; int audio_samplerate[MAX_AUDIO_CHANNELS]; int audio_blocksize; - /* other */ - double starttime; +#ifdef HAVE_ALSA9 + snd_pcm_t *handle; + snd_output_t *log; + int buffer_time, period_time, chunk_size; + size_t bits_per_sample, bits_per_frame; +#else + int audio_fd; +#endif + + /* buffering stuff */ + int immediate_mode; + + int audio_buffer_size; + unsigned char *audio_ringbuffer; + double audio_skew_buffer[AUD_SKEW_CNT]; + volatile int audio_empty; + volatile int audio_head; + volatile int audio_tail; + volatile int audio_cnt; + volatile double audio_skew; + volatile double audio_skew_factor; + volatile double audio_skew_measure_time; + volatile int audio_drop; + + int video_buffer_size; + unsigned char *video_ringbuffer; + double *video_timebuffer; + volatile int video_head; + volatile int video_tail; + volatile int video_cnt; + + volatile int shutdown; + + pthread_t audio_grabber_thread; + pthread_t video_grabber_thread; + pthread_mutex_t audio_starter; + pthread_mutex_t skew_mutex; + + double starttime; + double audio_secs_per_block; + double audio_skew_total; + long audio_recv_blocks_total; + long audio_sent_blocks_total; + } priv_t; #include "tvi_def.h" @@ -100,6 +157,9 @@ "unknown", "mono", "stereo", "language1", "language2", NULL }; +static void *audio_grabber(void *data); +static void *video_grabber(void *data); + static int palette2depth(int palette) { switch(palette) @@ -134,21 +194,19 @@ { switch(format) { - case IMGFMT_RGB15: + case IMGFMT_BGR15: return(VIDEO_PALETTE_RGB555); - case IMGFMT_RGB16: + case IMGFMT_BGR16: return(VIDEO_PALETTE_RGB565); - case IMGFMT_RGB24: + case IMGFMT_BGR24: return(VIDEO_PALETTE_RGB24); - case IMGFMT_RGB32: + case IMGFMT_BGR32: return(VIDEO_PALETTE_RGB32); case IMGFMT_YV12: case IMGFMT_I420: return(VIDEO_PALETTE_YUV420P); - case IMGFMT_UYVY: - return(VIDEO_PALETTE_YUV422); case IMGFMT_YUY2: - return(VIDEO_PALETTE_YUYV); + return(VIDEO_PALETTE_YUV422); } return(-1); } @@ -179,72 +237,19 @@ } /* set audio device name */ +#ifdef HAVE_ALSA9 + priv->audio_device = strdup("default"); +#else priv->audio_device = strdup("/dev/dsp"); +#endif return(h); } -static int init(priv_t *priv) +static void init_v4l_audio(priv_t *priv) { int i; - priv->video_fd = open(priv->video_device, O_RDWR); - mp_msg(MSGT_TV, MSGL_DBG2, "Video fd: %d, %x\n", priv->video_fd, - priv->video_device); - if (priv->video_fd == -1) - { - mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n", - priv->video_device, strerror(errno)); - goto err; - } - - priv->fps = 25; /* pal */ - - /* get capabilities (priv->capability is needed!) */ - if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1) - { - mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno)); - goto err; - } - - fcntl(priv->video_fd, F_SETFD, FD_CLOEXEC); - - mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.name); - mp_msg(MSGT_TV, MSGL_INFO, " Capabilites: "); - for (i = 0; device_cap2name[i] != NULL; i++) - if (priv->capability.type & (1 << i)) - mp_msg(MSGT_TV, MSGL_INFO, "%s ", device_cap2name[i]); - mp_msg(MSGT_TV, MSGL_INFO, "\n"); - mp_msg(MSGT_TV, MSGL_INFO, " Device type: %d\n", priv->capability.type); - mp_msg(MSGT_TV, MSGL_INFO, " Supported sizes: %dx%d => %dx%d\n", - priv->capability.minwidth, priv->capability.minheight, - priv->capability.maxwidth, priv->capability.maxheight); - priv->width = priv->capability.minwidth; - priv->height = priv->capability.minheight; - mp_msg(MSGT_TV, MSGL_INFO, " Inputs: %d\n", priv->capability.channels); - - priv->channels = (struct video_channel *)malloc(sizeof(struct video_channel)*priv->capability.channels); - if (!priv->channels) - goto malloc_failed; - memset(priv->channels, 0, sizeof(struct video_channel)*priv->capability.channels); - for (i = 0; i < priv->capability.channels; i++) - { - priv->channels[i].channel = i; - if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1) - { - mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno)); - break; - } - mp_msg(MSGT_TV, MSGL_INFO, " %d: %s: %s%s%s%s (tuner:%d, norm:%d)\n", i, - priv->channels[i].name, - (priv->channels[i].flags & VIDEO_VC_TUNER) ? "tuner " : "", - (priv->channels[i].flags & VIDEO_VC_AUDIO) ? "audio " : "", - (priv->channels[i].flags & VIDEO_TYPE_TV) ? "tv " : "", - (priv->channels[i].flags & VIDEO_TYPE_CAMERA) ? "camera " : "", - priv->channels[i].tuners, - priv->channels[i].norm); - } - /* audio chanlist */ if (priv->capability.audios) { @@ -254,7 +259,7 @@ { if (i >= MAX_AUDIO_CHANNELS) { - mp_msg(MSGT_TV, MSGL_ERR, "no space for more audio channels (incrase in source!) (%d > %d)\n", + mp_msg(MSGT_TV, MSGL_ERR, "no space for more audio channels (increase in source!) (%d > %d)\n", i, MAX_AUDIO_CHANNELS); i = priv->capability.audios; break; @@ -304,42 +309,101 @@ audio_out_format_name(priv->audio_format[i])); } } +} - if (!(priv->capability.type & VID_TYPE_CAPTURE)) - { - mp_msg(MSGT_TV, MSGL_ERR, "Only grabbing supported (for overlay use another program)\n"); - goto err; - } - - /* map grab buffer */ - if (ioctl(priv->video_fd, VIDIOCGMBUF, &priv->mbuf) == -1) - { - mp_msg(MSGT_TV, MSGL_ERR, "ioctl get mbuf failed: %s\n", strerror(errno)); - goto err; - } +#ifdef HAVE_ALSA9 +static int init_alsa_audio(priv_t *priv) +{ + snd_pcm_hw_params_t *params; + snd_pcm_sw_params_t *swparams; + size_t buffer_size; + int err; + size_t n; + unsigned int rate; + snd_pcm_uframes_t start_threshold, stop_threshold; + + snd_pcm_hw_params_alloca(¶ms); + snd_pcm_sw_params_alloca(&swparams); + + err = snd_pcm_hw_params_any(priv->handle, params); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "Broken configuration for this PCM: no configurations available\n"); + return 0; + } + err = snd_pcm_hw_params_set_access(priv->handle, params, + SND_PCM_ACCESS_RW_INTERLEAVED); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "Access type not available\n"); + return 0; + } + err = snd_pcm_hw_params_set_format(priv->handle, params, SND_PCM_FORMAT_S16_LE); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "Sample format non available\n"); + return 0; + } + err = snd_pcm_hw_params_set_channels(priv->handle, params, priv->audio_channels[priv->audio_id]); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "Channels count non available\n"); + return 0; + } + + err = snd_pcm_hw_params_set_rate_near(priv->handle, params, priv->audio_samplerate[priv->audio_id], 0); + assert(err >= 0); + rate = err; + + priv->buffer_time = 1000000; + priv->buffer_time = snd_pcm_hw_params_set_buffer_time_near(priv->handle, params, + priv->buffer_time, 0); + assert(priv->buffer_time >= 0); + priv->period_time = priv->buffer_time / 4; + priv->period_time = snd_pcm_hw_params_set_period_time_near(priv->handle, params, + priv->period_time, 0); + assert(priv->period_time >= 0); + err = snd_pcm_hw_params(priv->handle, params); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "Unable to install hw params:"); + snd_pcm_hw_params_dump(params, priv->log); + return 0; + } + priv->chunk_size = snd_pcm_hw_params_get_period_size(params, 0); + buffer_size = snd_pcm_hw_params_get_buffer_size(params); + if (priv->chunk_size == buffer_size) { + mp_msg(MSGT_TV, MSGL_ERR, "Can't use period equal to buffer size (%u == %lu)\n", priv->chunk_size, (long)buffer_size); + return 0; + } + snd_pcm_sw_params_current(priv->handle, swparams); + err = snd_pcm_sw_params_set_sleep_min(priv->handle, swparams,0); + assert(err >= 0); + err = snd_pcm_sw_params_set_avail_min(priv->handle, swparams, priv->chunk_size); + assert(err >= 0); + + err = snd_pcm_sw_params_set_start_threshold(priv->handle, swparams, 0); + assert(err >= 0); + err = snd_pcm_sw_params_set_stop_threshold(priv->handle, swparams, buffer_size); + assert(err >= 0); + + assert(err >= 0); + if (snd_pcm_sw_params(priv->handle, swparams) < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "unable to install sw params:\n"); + snd_pcm_sw_params_dump(swparams, priv->log); + return 0; + } + + snd_pcm_reset(priv->handle); + +// snd_pcm_dump(priv->handle, priv->log); + + priv->bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE); + priv->bits_per_frame = priv->bits_per_sample * priv->audio_channels[priv->audio_id]; + priv->audio_blocksize = priv->chunk_size * priv->bits_per_frame / 8; - mp_msg(MSGT_TV, MSGL_V, "mbuf: size=%d, frames=%d\n", - priv->mbuf.size, priv->mbuf.frames); - priv->mmap = mmap(0, priv->mbuf.size, PROT_READ|PROT_WRITE, - MAP_SHARED, priv->video_fd, 0); - if (priv->mmap == (unsigned char *)-1) - { - mp_msg(MSGT_TV, MSGL_ERR, "Unable to map memory for buffers: %s\n", strerror(errno)); - goto err; - } - mp_msg(MSGT_TV, MSGL_DBG2, "our buffer: %p\n", priv->mmap); + return 1; +} - /* num of buffers */ - priv->nbuf = priv->mbuf.frames; - - /* video buffers */ - priv->buf = (struct video_mmap *)malloc(priv->nbuf * sizeof(struct video_mmap)); - if (!priv->buf) - goto malloc_failed; - memset(priv->buf, 0, priv->nbuf * sizeof(struct video_mmap)); - - /* audio init */ -#if 1 +#else /* ! HAVE_ALSA9 */ + +static void init_oss_audio(priv_t *priv) +{ priv->audio_fd = open(priv->audio_device, O_RDONLY); if (priv->audio_fd < 0) { @@ -350,8 +414,6 @@ { int ioctl_param; - fcntl(priv->audio_fd, F_SETFL, O_NONBLOCK); - #if 0 ioctl_param = 0x7fff000d; /* 8k */ printf("ioctl dsp setfragment: %d\n", @@ -362,6 +424,7 @@ printf("ioctl dsp getfmt: %d\n", ioctl(priv->audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param)); + printf("Audio ID: %x\n", priv->audio_id); printf("Supported formats: %x\n", ioctl_param); if (!(ioctl_param & priv->audio_format[priv->audio_id])) printf("notsupported format\n"); @@ -417,9 +480,133 @@ ioctl(priv->audio_fd, SNDCTL_DSP_GETBLKSIZE, &priv->audio_blocksize)); printf("blocksize: %d\n", priv->audio_blocksize); } -#endif - return(1); +} +#endif /* ! HAVE_ALSA9 */ + +static int init(priv_t *priv) +{ + int i; + + priv->video_ringbuffer = NULL; + priv->video_timebuffer = NULL; + priv->audio_ringbuffer = NULL; +#ifdef HAVE_ALSA9 + priv->handle = NULL; + priv->log = NULL; +#endif + + priv->video_fd = open(priv->video_device, O_RDWR); + mp_msg(MSGT_TV, MSGL_DBG2, "Video fd: %d, %x\n", priv->video_fd, + priv->video_device); + if (priv->video_fd == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n", + priv->video_device, strerror(errno)); + goto err; + } + + priv->fps = 25; /* pal */ + + /* get capabilities (priv->capability is needed!) */ + if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno)); + goto err; + } + + fcntl(priv->video_fd, F_SETFD, FD_CLOEXEC); + + mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.name); + mp_msg(MSGT_TV, MSGL_INFO, " Capabilites: "); + for (i = 0; device_cap2name[i] != NULL; i++) + if (priv->capability.type & (1 << i)) + mp_msg(MSGT_TV, MSGL_INFO, "%s ", device_cap2name[i]); + mp_msg(MSGT_TV, MSGL_INFO, "\n"); + mp_msg(MSGT_TV, MSGL_INFO, " Device type: %d\n", priv->capability.type); + mp_msg(MSGT_TV, MSGL_INFO, " Supported sizes: %dx%d => %dx%d\n", + priv->capability.minwidth, priv->capability.minheight, + priv->capability.maxwidth, priv->capability.maxheight); + priv->width = priv->capability.minwidth; + priv->height = priv->capability.minheight; + mp_msg(MSGT_TV, MSGL_INFO, " Inputs: %d\n", priv->capability.channels); + + priv->channels = (struct video_channel *)malloc(sizeof(struct video_channel)*priv->capability.channels); + if (!priv->channels) + goto malloc_failed; + memset(priv->channels, 0, sizeof(struct video_channel)*priv->capability.channels); + for (i = 0; i < priv->capability.channels; i++) + { + priv->channels[i].channel = i; + if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno)); + break; + } + mp_msg(MSGT_TV, MSGL_INFO, " %d: %s: %s%s%s%s (tuner:%d, norm:%d)\n", i, + priv->channels[i].name, + (priv->channels[i].flags & VIDEO_VC_TUNER) ? "tuner " : "", + (priv->channels[i].flags & VIDEO_VC_AUDIO) ? "audio " : "", + (priv->channels[i].flags & VIDEO_TYPE_TV) ? "tv " : "", + (priv->channels[i].flags & VIDEO_TYPE_CAMERA) ? "camera " : "", + priv->channels[i].tuners, + priv->channels[i].norm); + } + + if (!(priv->capability.type & VID_TYPE_CAPTURE)) + { + mp_msg(MSGT_TV, MSGL_ERR, "Only grabbing supported (for overlay use another program)\n"); + goto err; + } + + /* map grab buffer */ + if (ioctl(priv->video_fd, VIDIOCGMBUF, &priv->mbuf) == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "ioctl get mbuf failed: %s\n", strerror(errno)); + goto err; + } + + mp_msg(MSGT_TV, MSGL_V, "mbuf: size=%d, frames=%d\n", + priv->mbuf.size, priv->mbuf.frames); + priv->mmap = mmap(0, priv->mbuf.size, PROT_READ|PROT_WRITE, + MAP_SHARED, priv->video_fd, 0); + if (priv->mmap == (unsigned char *)-1) + { + mp_msg(MSGT_TV, MSGL_ERR, "Unable to map memory for buffers: %s\n", strerror(errno)); + goto err; + } + mp_msg(MSGT_TV, MSGL_DBG2, "our buffer: %p\n", priv->mmap); + + /* num of buffers */ + priv->nbuf = priv->mbuf.frames; + + /* video buffers */ + priv->buf = (struct video_mmap *)malloc(priv->nbuf * sizeof(struct video_mmap)); + if (!priv->buf) + goto malloc_failed; + memset(priv->buf, 0, priv->nbuf * sizeof(struct video_mmap)); + + /* audio init */ + if (!tv_param_noaudio) { + int err; + + init_v4l_audio(priv); +#ifdef HAVE_ALSA9 + err = snd_pcm_open(&priv->handle, priv->audio_device, SND_PCM_STREAM_CAPTURE, 0); + if (err < 0) { + error("audio open error: %s", snd_strerror(err)); + return 0; + } + err = snd_output_stdio_attach(&priv->log, stderr, 0); + assert(err >= 0); + + if (!init_alsa_audio(priv)) return 0; +#else + init_oss_audio(priv); +#endif + } + + return(1); malloc_failed: if (priv->channels) @@ -434,20 +621,74 @@ static int uninit(priv_t *priv) { - close(priv->video_fd); + priv->shutdown = 1; + + mp_msg(MSGT_TV, MSGL_INFO, "Waiting for threads to finish... "); + if (!tv_param_noaudio) { + pthread_join(priv->audio_grabber_thread, NULL); + pthread_mutex_destroy(&priv->audio_starter); + pthread_mutex_destroy(&priv->skew_mutex); + } + pthread_join(priv->video_grabber_thread, NULL); + mp_msg(MSGT_TV, MSGL_INFO, "done\n"); priv->audio[priv->audio_id].volume = 0; priv->audio[priv->audio_id].flags |= VIDEO_AUDIO_MUTE; ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]); + + close(priv->video_fd); + +#ifdef HAVE_ALSA9 + if (priv->log) + snd_output_close(priv->log); + if (priv->handle) { + snd_pcm_close(priv->handle); + } +#else close(priv->audio_fd); +#endif + + if (priv->video_ringbuffer) + free(priv->video_ringbuffer); + if (priv->video_timebuffer) + free(priv->video_timebuffer); + if (!tv_param_noaudio) { + if (priv->audio_ringbuffer) + free(priv->audio_ringbuffer); + } return(1); } +static int get_capture_buffer_size(priv_t *priv) +{ + int bufsize, cnt; +#ifdef HAVE_SYS_SYSINFO + struct sysinfo si; + + sysinfo(&si); + if (si.totalram<2*1024*1024) { + bufsize = 1024*1024; + } else { + bufsize = si.totalram/2; + } +#else + bufsize = 16*1024*1024; +#endif + + cnt = bufsize/(priv->width*priv->bytesperline); + if (cnt < 2) cnt = 2; + + mp_msg(MSGT_TV, MSGL_INFO, "Allocating a ring buffer for %d frames, %d MB total size.\n", + cnt, cnt*priv->width*priv->bytesperline/(1024*1024)); + + return cnt; +} + static int start(priv_t *priv) { int i; - + int bytes_per_sample; if (ioctl(priv->video_fd, VIDIOCGPICT, &priv->picture) == -1) { @@ -457,11 +698,12 @@ priv->picture.palette = format2palette(priv->format); priv->picture.depth = palette2depth(priv->picture.palette); - priv->bytesperline = priv->width * priv->picture.depth / 8; -// if (IMGFMT_IS_BGR(priv->format) || IMGFMT_IS_RGB(priv->format)) -// priv->bytesperline = priv->width * priv->picture.depth / 8; -// if ((priv->format == IMGFMT_YV12) || (priv->format == IMGFMT_I420) || (priv->format == IMGFMT_IYUV)) -// priv->bytesperline = priv->width * 3 / 2; + + if (priv->format != IMGFMT_BGR15) { + priv->bytesperline = priv->width * priv->picture.depth / 8; + } else { + priv->bytesperline = priv->width * 2; + } printf("palette: %d, depth: %d, bytesperline: %d\n", priv->picture.palette, priv->picture.depth, priv->bytesperline); @@ -520,19 +762,72 @@ ioctl(priv->video_fd, VIDIOCSWIN, &win); } - /* start capture */ + // initialize video capture if (ioctl(priv->video_fd, VIDIOCCAPTURE, &one) == -1) { - mp_msg(MSGT_TV, MSGL_ERR, "ioctl capture failed: %s\n", strerror(errno)); + mp_msg(MSGT_TV, MSGL_ERR, "FATAL: ioctl ccapture failed: %s\n", strerror(errno)); return(0); } + #endif - { - struct timeval curtime; - gettimeofday(&curtime, NULL); - priv->starttime=curtime.tv_sec + curtime.tv_usec*.000001; + /* setup audio parameters */ + if (!tv_param_noaudio) { + priv->audio_buffer_size = AUD_BUF_SIZE; + priv->audio_ringbuffer = (unsigned char*)malloc(priv->audio_blocksize*priv->audio_buffer_size); + if (!priv->audio_ringbuffer) { + mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno)); + return 0; + } + +#ifdef HAVE_ALSA9 + bytes_per_sample = priv->bits_per_sample/8; +#else + bytes_per_sample = 2; +#endif + priv->audio_secs_per_block = (double)priv->audio_blocksize/(priv->audio_samplerate[priv->audio_id] + *priv->audio_channels[priv->audio_id] + *bytes_per_sample); + priv->audio_empty = 1; + priv->audio_head = 0; + priv->audio_tail = 0; + priv->audio_cnt = 0; + priv->audio_drop = 0; + priv->audio_skew = 0.0; + priv->audio_skew_total = 0.0; + priv->audio_recv_blocks_total = 0; + priv->audio_sent_blocks_total = 0; + } + + /* setup video parameters */ + if (priv->immediate_mode) { + priv->video_buffer_size = VID_BUF_SIZE_IMMEDIATE; + } else { + priv->video_buffer_size = get_capture_buffer_size(priv); + } + priv->video_ringbuffer = (unsigned char*)malloc(priv->bytesperline * priv->height * priv->video_buffer_size); + if (!priv->video_ringbuffer) { + mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno)); + return 0; + } + priv->video_timebuffer = (double*)malloc(sizeof(double) * priv->video_buffer_size); + if (!priv->video_timebuffer) { + mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate time buffer: %s\n", strerror(errno)); + return 0; + } + priv->video_head = 0; + priv->video_tail = 0; + priv->video_cnt = 0; + + /* launch capture threads */ + priv->shutdown = 0; + if (!tv_param_noaudio) { + pthread_mutex_init(&priv->audio_starter, NULL); + pthread_mutex_init(&priv->skew_mutex, NULL); + pthread_mutex_lock(&priv->audio_starter); + pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv); } + pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv); return(1); } @@ -576,6 +871,14 @@ } case TVI_CONTROL_VID_SET_FORMAT: priv->format = (int)*(void **)arg; + // !HACK! v4l uses BGR format instead of RGB + // and we have to correct this. Fortunately, + // tv.c reads later the format back so we + // can persuade it to use what we want. + if (IMGFMT_IS_RGB(priv->format)) { + priv->format &= ~IMGFMT_RGB_MASK; + priv->format |= IMGFMT_BGR; + } return(TVI_CONTROL_TRUE); case TVI_CONTROL_VID_GET_PLANES: (int)*(void **)arg = 1; /* FIXME, also not needed at this time */ @@ -754,14 +1057,22 @@ } case TVI_CONTROL_AUD_SET_SAMPLERATE: { +#ifdef HAVE_ALSA9 + priv->audio_samplerate[priv->audio_id] = (int)*(void **)arg; + if (!init_alsa_audio(priv)) return(TVI_CONTROL_FALSE); + priv->audio_samplesize[priv->audio_id] = + priv->audio_samplerate[priv->audio_id]/8/priv->fps* + priv->audio_channels[priv->audio_id]; + return(TVI_CONTROL_TRUE); +#else int tmp = priv->audio_samplerate[priv->audio_id] = (int)*(void **)arg; - if (ioctl(priv->audio_fd, SNDCTL_DSP_SPEED, &tmp) == -1) return(TVI_CONTROL_FALSE); priv->audio_samplesize[priv->audio_id] = priv->audio_samplerate[priv->audio_id]/8/priv->fps* priv->audio_channels[priv->audio_id]; return(TVI_CONTROL_TRUE); +#endif } /* ========== SPECIFIC controls =========== */ case TVI_CONTROL_SPC_GET_INPUT: @@ -821,50 +1132,183 @@ /* update local channel list */ control(priv, TVI_CONTROL_SPC_GET_INPUT, &req_chan); return(TVI_CONTROL_TRUE); + case TVI_CONTROL_IMMEDIATE: + priv->immediate_mode = 1; + return(TVI_CONTROL_TRUE); } } return(TVI_CONTROL_UNKNOWN); } -static double grab_video_frame(priv_t *priv, char *buffer, int len) +// copies a video frame +// for RGB (i.e. BGR in mplayer) flips the image upside down +// for YV12 swaps the 2nd and 3rd plane +static inline void copy_frame(priv_t *priv, unsigned char *dest, unsigned char *source) +{ + int i; + unsigned char *sptr; + + // YV12 uses VIDEO_PALETTE_YUV420P, but the planes are swapped + if (priv->format == IMGFMT_YV12) { + memcpy(dest, source, priv->width * priv->height); + memcpy(dest+priv->width * priv->height*5/4, source+priv->width * priv->height, priv->width * priv->height/4); + memcpy(dest+priv->width * priv->height, source+priv->width * priv->height*5/4, priv->width * priv->height/4); + return; + } + + switch (priv->picture.palette) { + case VIDEO_PALETTE_RGB24: + case VIDEO_PALETTE_RGB32: + case VIDEO_PALETTE_RGB555: + case VIDEO_PALETTE_RGB565: + sptr = source + (priv->height-1)*priv->bytesperline; + for (i = 0; i < priv->height; i++) { + memcpy(dest, sptr, priv->bytesperline); + dest += priv->bytesperline; + sptr -= priv->bytesperline; + } + break; + case VIDEO_PALETTE_UYVY: + case VIDEO_PALETTE_YUV420P: + default: + memcpy(dest, source, priv->bytesperline * priv->height); + } + +} + +// maximum skew change, in frames +#define MAX_SKEW_DELTA 0.6 +static void *video_grabber(void *data) { + priv_t *priv = (priv_t*)data; struct timeval curtime; - double timestamp; - int frame = priv->queue % priv->nbuf; - int nextframe = (priv->queue+1) % priv->nbuf; + double timestamp, skew, prev_skew, xskew, interval, prev_interval; + int frame, nextframe; + int fsize = priv->bytesperline * priv->height; + int i; + int first = 1; - mp_dbg(MSGT_TV, MSGL_DBG2, "grab_video_frame(priv=%p, buffer=%p, len=%d)\n", - priv, buffer, len); + int dropped = 0; + double dropped_time; + double drop_delta = 0.0; - mp_dbg(MSGT_TV, MSGL_DBG3, "buf: %p + frame: %d => %p\n", - priv->buf, nextframe, &priv->buf[nextframe]); - if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[nextframe]) == -1) - { - mp_msg(MSGT_TV, MSGL_ERR, "ioctl mcapture failed: %s\n", strerror(errno)); - return(0); + for (i = 0; i < priv->nbuf; i++) { + frame = i; + nextframe = (i+1)%priv->nbuf; + if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[nextframe]) == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "\nioctl mcapture failed: %s\n", strerror(errno)); + continue; + } + while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[frame].frame) < 0 && + (errno == EAGAIN || errno == EINTR)); + mp_dbg(MSGT_TV, MSGL_DBG3, "\npicture sync failed\n"); } - while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[frame].frame) < 0 && - (errno == EAGAIN || errno == EINTR)); - mp_dbg(MSGT_TV, MSGL_DBG3, "picture sync failed\n"); + prev_interval = 0.0; + prev_skew = priv->audio_skew; - priv->queue++; - - gettimeofday(&curtime, NULL); - timestamp=curtime.tv_sec + curtime.tv_usec*.000001; + for (;!priv->shutdown;) + { + for (i = 0; i < priv->nbuf && !priv->shutdown; i++) { + frame = i; + nextframe = (i+1)%priv->nbuf; + + if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[nextframe]) == -1) + { + mp_msg(MSGT_TV, MSGL_ERR, "\nioctl mcapture failed: %s\n", strerror(errno)); + continue; + } + + while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[frame].frame) < 0 && + (errno == EAGAIN || errno == EINTR)); + mp_dbg(MSGT_TV, MSGL_DBG3, "\npicture sync failed\n"); + + gettimeofday(&curtime, NULL); + if (first) { + // this was a first frame - let's launch the audio capture thread immediately + // before that, just initialize some variables + priv->starttime = curtime.tv_sec + curtime.tv_usec*.000001; + priv->audio_skew_measure_time = 0; + pthread_mutex_unlock(&priv->audio_starter); + first = 0; + } - mp_dbg(MSGT_TV, MSGL_DBG3, "mmap: %p + offset: %d => %p\n", - priv->mmap, priv->mbuf.offsets[frame], - priv->mmap+priv->mbuf.offsets[frame]); + interval = curtime.tv_sec + curtime.tv_usec*.000001 - priv->starttime; - /* XXX also directrendering would be nicer! */ - /* 3 times copying the same picture to other buffer :( */ + if (!priv->immediate_mode && (interval - prev_interval > 1.0/priv->fps*1.5)) { + mp_msg(MSGT_TV, MSGL_ERR, "\nvideo capture thread: frame delta = %lf s (probably dropped)\n", + interval - prev_interval); + } + + // interpolate the skew in time + xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor; + // correct extreme skew changes to avoid (especially) moving backwards in time + if (xskew - prev_skew > (interval - prev_interval)*MAX_SKEW_DELTA) { + skew = prev_skew + (interval - prev_interval)*MAX_SKEW_DELTA; + } else if (xskew - prev_skew < -(interval - prev_interval)*MAX_SKEW_DELTA) { + skew = prev_skew - (interval - prev_interval)*MAX_SKEW_DELTA; + } else { + skew = xskew; + } - /* copy the actual frame */ - memcpy(buffer, priv->mmap+priv->mbuf.offsets[frame], len); + prev_skew = skew; + prev_interval = interval; + +// fprintf(stderr, "\nv_interval = %lf, a_skew = %f, corr_skew = %f\n", interval/2, xskew, skew); +// fprintf(stderr, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt); + + if ((priv->video_tail+1) % priv->video_buffer_size == priv->video_head) { - return(timestamp-priv->starttime); + if (!priv->immediate_mode) { + mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n"); + } else { + if (!dropped) { + dropped = 1; + dropped_time = interval - skew; + } + } + } else { + if (priv->immediate_mode) { + if (dropped) { + drop_delta += interval - skew - dropped_time; + dropped = 0; + } + // compensate for audio skew + // negative skew => there are more audio samples, increase interval + // positive skew => less samples, shorten the interval + + // for TV, we pretend that dropped frames never existed + // without this, mplayer gets confused after pressing pause + priv->video_timebuffer[priv->video_tail] = interval - skew - drop_delta; + } else { + priv->video_timebuffer[priv->video_tail] = interval - skew; + } + + copy_frame(priv, priv->video_ringbuffer+(priv->video_tail)*fsize, priv->mmap+priv->mbuf.offsets[frame]); + priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size; + priv->video_cnt++; + } + + } + + } +} + +static double grab_video_frame(priv_t *priv, char *buffer, int len) +{ + double interval; + + while (priv->video_head == priv->video_tail) { + usleep(10000); + } + + interval = priv->video_timebuffer[priv->video_head]; + memcpy(buffer, priv->video_ringbuffer+priv->video_head*priv->bytesperline * priv->height, len); + priv->video_cnt--; + priv->video_head = (++priv->video_head)%priv->video_buffer_size; + return interval; } static int get_video_framesize(priv_t *priv) @@ -872,6 +1316,104 @@ return(priv->bytesperline * priv->height); } +static void *audio_grabber(void *data) +{ + priv_t *priv = (priv_t*)data; + struct timeval tv; + int ret; + int i, audio_skew_ptr = 0; + double tmp, current_time, current_skew, prev_skew = 0.0; + + pthread_mutex_lock(&priv->audio_starter); + +#ifdef HAVE_ALSA9 + snd_pcm_start(priv->handle); +#endif + + for (i = 0; i < AUD_SKEW_CNT; i++) + priv->audio_skew_buffer[i] = 0.0; + + for (;!priv->shutdown;) + { +#ifdef HAVE_ALSA9 + ret = snd_pcm_readi(priv->handle, priv->audio_ringbuffer+priv->audio_tail*priv->audio_blocksize, priv->chunk_size); + if (ret != priv->chunk_size) { + if (ret < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "\nerror reading audio: %s\n", snd_strerror(ret)); + } else { + mp_msg(MSGT_TV, MSGL_ERR, "\nnot enough audio samples!\n"); + } + continue; + } +#else + ret = read(priv->audio_fd, priv->audio_ringbuffer+priv->audio_tail*priv->audio_blocksize, priv->audio_blocksize); + if (ret != priv->audio_blocksize) { + if (ret < 0) { + mp_msg(MSGT_TV, MSGL_ERR, "\nerror reading audio: %s\n", strerror(errno)); + } else { + mp_msg(MSGT_TV, MSGL_ERR, "\nnot enough audio samples!\n"); + } + continue; + } +#endif + + gettimeofday(&tv, NULL); + + priv->audio_recv_blocks_total++; + current_time = tv.tv_sec + tv.tv_usec*.000001 - priv->starttime; + + // compute the moving sum of the skews + if (priv->audio_recv_blocks_total % 1024 == 0) { + // recompute the moving sum to avoid truncation errors + priv->audio_skew_buffer[audio_skew_ptr] = current_time + - priv->audio_secs_per_block*priv->audio_recv_blocks_total; + audio_skew_ptr = (audio_skew_ptr+1) % AUD_SKEW_CNT; + for (i = 0, tmp = 0.0; i < AUD_SKEW_CNT; i++) + tmp += priv->audio_skew_buffer[i]; + priv->audio_skew_total = tmp; + } else { + priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr]; + priv->audio_skew_buffer[audio_skew_ptr] = current_time + - priv->audio_secs_per_block*priv->audio_recv_blocks_total; + priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr]; + audio_skew_ptr = (audio_skew_ptr+1) % AUD_SKEW_CNT; + } + + pthread_mutex_lock(&priv->skew_mutex); + // linear interpolation - here we interpolate current skew value + // from the moving average, which we expect to be in the middle + // of the interval + if (priv->audio_recv_blocks_total > AUD_SKEW_CNT) { + priv->audio_skew = priv->audio_skew_total/AUD_SKEW_CNT; + priv->audio_skew += (priv->audio_skew*AUD_SKEW_CNT)/(2*priv->audio_recv_blocks_total-AUD_SKEW_CNT); + } else { +// priv->audio_skew = 2*priv->audio_skew_total/AUD_SKEW_CNT;; +// priv->audio_skew = (AUD_SKEW_CNT+priv->audio_recv_blocks_total)/AUD_SKEW_CNT*priv->audio_skew_total/priv->audio_recv_blocks_total; + // at the beginning, we return the raw values + priv->audio_skew = current_time - priv->audio_secs_per_block*priv->audio_recv_blocks_total; + } + // current skew factor (assuming linearity) + // used for further interpolation in video_grabber + // probably overkill but seems to be necessary for + // stress testing by dropping half of the audio frames ;) + // especially when using ALSA with large block sizes + // where audio_skew remains a long while behind + priv->audio_skew_factor = (priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time); + priv->audio_skew_measure_time = current_time; + prev_skew = priv->audio_skew; + pthread_mutex_unlock(&priv->skew_mutex); + + if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) { + mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n"); + priv->audio_drop++; + } else { + priv->audio_tail = (++priv->audio_tail) % priv->audio_buffer_size; + priv->audio_cnt++; + priv->audio_empty = 0; + } + } +} + static double grab_audio_frame(priv_t *priv, char *buffer, int len) { int in_len = 0; @@ -879,24 +1421,23 @@ mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", priv, buffer, len); - - while (--max_tries > 0) -// for (;;) - { - in_len = read(priv->audio_fd, buffer, len); -// printf("in_len: %d\n", in_len); -// fflush(NULL); - - if (in_len > 0) - break; - if (!((in_len == 0) || (in_len == -1 && (errno == EAGAIN || errno == EINTR)))) - { - in_len = 0; /* -EIO */ - break; - } - } - return 0; //(in_len); // FIXME! + // compensate for dropped audio frames + if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) { + priv->audio_drop--; + priv->audio_sent_blocks_total++; + memset(buffer, 0, len); + return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; + } + + while (priv->audio_head == priv->audio_tail) { + usleep(10000); + } + memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_blocksize, len); + priv->audio_head = (++priv->audio_head) % priv->audio_buffer_size; + priv->audio_cnt--; + priv->audio_sent_blocks_total++; + return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; } static int get_audio_framesize(priv_t *priv) diff -ur --exclude-from dontdiff vanilla/main/mencoder.c main/mencoder.c --- vanilla/main/mencoder.c Thu Aug 8 18:06:26 2002 +++ main/mencoder.c Sat Aug 10 11:35:42 2002 @@ -1187,6 +1187,8 @@ } // while(!at_eof) +if (demuxer) free_demuxer(demuxer); + #ifdef HAVE_MP3LAME // fixup CBR mp3 audio header: if(sh_audio && mux_a->codec==ACODEC_VBRMP3 && !lame_param_vbr){ diff -ur --exclude-from dontdiff vanilla/main/mplayer.c main/mplayer.c --- vanilla/main/mplayer.c Sat Aug 10 11:29:38 2002 +++ main/mplayer.c Sat Aug 10 11:35:42 2002 @@ -268,6 +268,8 @@ static stream_t* stream=NULL; +static demuxer_t *demuxer=NULL; + char* current_module=NULL; // for debugging int vo_gamma_brightness = 1000; @@ -291,6 +293,7 @@ #define INITED_STREAM 64 #define INITED_INPUT 128 #define INITED_VOBSUB 256 +#define INITED_DEMUXER 512 #define INITED_ALL 0xFFFF void uninit_player(unsigned int mask){ @@ -306,6 +309,12 @@ stream=NULL; } + if(mask&INITED_DEMUXER){ + current_module="uninit_demuxer"; + if(demuxer) free_demuxer(demuxer); + demuxer=NULL; + } + if(mask&INITED_VO){ inited_flags&=~INITED_VO; current_module="uninit_vo"; @@ -489,8 +498,6 @@ float sub_last_pts = -303; #endif -static demuxer_t *demuxer=NULL; - static demux_stream_t *d_audio=NULL; static demux_stream_t *d_video=NULL; static demux_stream_t *d_dvdsub=NULL; @@ -565,9 +572,7 @@ #endif -#ifdef HAVE_TV_BSDBT848 tv_param_immediate = 1; -#endif if ( argv[0] ) if(!strcmp(argv[0],"gmplayer") || @@ -1074,6 +1079,8 @@ demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id); if(!demuxer) goto goto_next_file; // exit_player(MSGTR_Exit_error); // ERROR + +inited_flags|=INITED_DEMUXER; current_module="demux_open2";