summaryrefslogtreecommitdiffstats
path: root/libmpdemux/tvi_v4l.c
diff options
context:
space:
mode:
Diffstat (limited to 'libmpdemux/tvi_v4l.c')
-rw-r--r--libmpdemux/tvi_v4l.c908
1 files changed, 671 insertions, 237 deletions
diff --git a/libmpdemux/tvi_v4l.c b/libmpdemux/tvi_v4l.c
index 67a1f70346..7bbf7409dc 100644
--- a/libmpdemux/tvi_v4l.c
+++ b/libmpdemux/tvi_v4l.c
@@ -6,6 +6,9 @@
Some ideas are based on xawtv/libng's grab-v4l.c written by
Gerd Knorr <kraxel@bytesex.org>
+ Multithreading, a/v sync and native ALSA support by
+ Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
+
CODE IS UNDER DEVELOPMENT, NO FEATURE REQUESTS PLEASE!
*/
@@ -21,11 +24,14 @@
#include <sys/types.h>
#include <sys/time.h>
#include <linux/videodev.h>
-#include <linux/soundcard.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
+#include <pthread.h>
+#ifdef HAVE_SYS_SYSINFO_H
+#include <sys/sysinfo.h>
+#endif
#include "mp_msg.h"
#include "../libao2/afmt.h"
@@ -34,6 +40,8 @@
#include "tv.h"
+#include "audio_in.h"
+
static tvi_info_t info = {
"Video 4 Linux input",
"v4l",
@@ -41,12 +49,22 @@ static tvi_info_t info = {
"under development"
};
+#define PAL_WIDTH 768
+#define PAL_HEIGHT 576
+#define PAL_FPS 25
+
+#define NTSC_WIDTH 640
+#define NTSC_HEIGHT 480
+#define NTSC_FPS 30
+
#define MAX_AUDIO_CHANNELS 10
+#define VID_BUF_SIZE_IMMEDIATE 2
+
typedef struct {
/* general */
char *video_device;
- int video_fd;
+ int video_fd;
struct video_capability capability;
struct video_channel *channels;
int act_channel;
@@ -61,24 +79,54 @@ typedef struct {
int fps;
struct video_mbuf mbuf;
- unsigned char *mmap;
+ unsigned char *mmap;
struct video_mmap *buf;
int nbuf;
- int queue;
/* audio */
- int audio_id;
char *audio_device;
+ audio_in_t audio_in;
+
+ int audio_id;
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;
+ /* buffering stuff */
+ int immediate_mode;
+
+ int audio_buffer_size;
+ int aud_skew_cnt;
+ unsigned char *audio_ringbuffer;
+ double *audio_skew_buffer;
+ 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 first;
+ 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 +148,9 @@ static const char *audio_mode2name[] = {
"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,28 +185,47 @@ static int format2palette(int format)
{
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);
}
+// sets and sanitizes audio buffer/block sizes
+static void setup_audio_buffer_sizes(priv_t *priv)
+{
+ int bytes_per_sample = priv->audio_in.bytes_per_sample;
+
+ // make the audio buffer at least 5 seconds long
+ priv->audio_buffer_size = 1 + 5*priv->audio_in.samplerate
+ *priv->audio_in.channels
+ *bytes_per_sample/priv->audio_in.blocksize;
+ if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256;
+
+ // make the skew buffer at least 1 second long
+ priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate
+ *priv->audio_in.channels
+ *bytes_per_sample/priv->audio_in.blocksize;
+ if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16;
+
+ mp_msg(MSGT_TV, MSGL_V, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n",
+ priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt);
+}
+
static int one = 1, zero = 0;
-tvi_handle_t *tvi_init_v4l(char *device)
+tvi_handle_t *tvi_init_v4l(char *device, char *adevice)
{
tvi_handle_t *h;
priv_t *priv;
@@ -172,22 +242,114 @@ tvi_handle_t *tvi_init_v4l(char *device)
else
priv->video_device = strdup(device);
+ /* set video device name */
+ if (!adevice)
+ priv->audio_device = NULL;
+ else {
+ priv->audio_device = strdup(adevice);
+ }
+
/* allocation failed */
if (!priv->video_device) {
free_handle(h);
return(NULL);
}
- /* set audio device name */
- priv->audio_device = strdup("/dev/dsp");
-
return(h);
}
+/* retrieves info about audio channels from the BTTV */
+static void init_v4l_audio(priv_t *priv)
+{
+ int i;
+
+ if (!priv->capability.audios) return;
+
+ /* audio chanlist */
+
+ mp_msg(MSGT_TV, MSGL_INFO, " Audio devices: %d\n", priv->capability.audios);
+
+ for (i = 0; i < priv->capability.audios; i++)
+ {
+ if (i >= MAX_AUDIO_CHANNELS)
+ {
+ 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;
+ }
+
+ priv->audio[i].audio = i;
+ if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1)
+ {
+ mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno));
+ break;
+ }
+
+ /* mute all channels */
+ priv->audio[i].volume = 0;
+ priv->audio[i].flags |= VIDEO_AUDIO_MUTE;
+ if (tv_param_mono) {
+ priv->audio[i].mode = VIDEO_SOUND_MONO;
+ ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[i]);
+ } else {
+ /* try to set stereo */
+ priv->audio[i].mode = VIDEO_SOUND_STEREO;
+ ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[i]);
+ }
+
+ priv->audio[i].audio = i;
+ if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1)
+ {
+ mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno));
+ break;
+ }
+
+ switch(priv->audio[i].mode)
+ {
+ case VIDEO_SOUND_MONO:
+ case VIDEO_SOUND_LANG1:
+ case VIDEO_SOUND_LANG2:
+ priv->audio_channels[i] = 1;
+ break;
+ case VIDEO_SOUND_STEREO:
+ priv->audio_channels[i] = 2;
+ break;
+ }
+
+ /* display stuff */
+ mp_msg(MSGT_TV, MSGL_V, " %d: %s: ", priv->audio[i].audio,
+ priv->audio[i].name);
+ if (priv->audio[i].flags & VIDEO_AUDIO_MUTABLE) {
+ mp_msg(MSGT_TV, MSGL_V, "muted=%s ",
+ (priv->audio[i].flags & VIDEO_AUDIO_MUTE) ? "yes" : "no");
+ }
+ mp_msg(MSGT_TV, MSGL_V, "volume=%d bass=%d treble=%d balance=%d mode=%s\n",
+ priv->audio[i].volume, priv->audio[i].bass, priv->audio[i].treble,
+ priv->audio[i].balance, audio_mode2name[priv->audio[i].mode]);
+ mp_msg(MSGT_TV, MSGL_V, " channels: %d\n", priv->audio_channels[i]);
+ }
+}
+
static int init(priv_t *priv)
{
int i;
+ if (tv_param_immediate == 1)
+ tv_param_noaudio = 1;
+
+ if (!tv_param_noaudio) {
+ }
+
+ if (priv->audio_device) {
+ audio_in_set_device(&priv->audio_in, priv->audio_device);
+ }
+
+ priv->video_ringbuffer = NULL;
+ priv->video_timebuffer = NULL;
+ priv->audio_ringbuffer = NULL;
+ priv->audio_skew_buffer = NULL;
+
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);
@@ -198,7 +360,7 @@ static int init(priv_t *priv)
goto err;
}
- priv->fps = 25; /* pal */
+ priv->fps = PAL_FPS; /* pal */
/* get capabilities (priv->capability is needed!) */
if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1)
@@ -207,6 +369,25 @@ static int init(priv_t *priv)
goto err;
}
+ if (ioctl(priv->video_fd, VIDIOCGTUNER, &priv->tuner) == -1)
+ {
+ mp_msg(MSGT_TV, MSGL_ERR, "ioctl get tuner failed: %s\n", strerror(errno));
+ goto err;
+ }
+
+ switch (priv->tuner.mode) {
+ case VIDEO_MODE_PAL:
+ case VIDEO_MODE_SECAM:
+ priv->fps = PAL_FPS;
+ break;
+ case VIDEO_MODE_NTSC:
+ priv->fps = NTSC_FPS;
+ break;
+ default:
+ mp_msg(MSGT_TV, MSGL_ERR, "get tuner returned an unknown mode: %d\n", priv->tuner.mode);
+ goto err;
+ }
+
fcntl(priv->video_fd, F_SETFD, FD_CLOEXEC);
mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.name);
@@ -245,66 +426,6 @@ static int init(priv_t *priv)
priv->channels[i].norm);
}
- /* audio chanlist */
- if (priv->capability.audios)
- {
- mp_msg(MSGT_TV, MSGL_INFO, " Audio devices: %d\n", priv->capability.audios);
-
- for (i = 0; i < priv->capability.audios; i++)
- {
- if (i >= MAX_AUDIO_CHANNELS)
- {
- mp_msg(MSGT_TV, MSGL_ERR, "no space for more audio channels (incrase in source!) (%d > %d)\n",
- i, MAX_AUDIO_CHANNELS);
- i = priv->capability.audios;
- break;
- }
-
- priv->audio[i].audio = i;
- if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1)
- {
- mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno));
- break;
- }
-
- if (priv->audio[i].volume <= 0)
- priv->audio[i].volume = 100;
- priv->audio[i].flags &= ~VIDEO_AUDIO_MUTE;
- ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[i]);
-
- switch(priv->audio[i].mode)
- {
- case VIDEO_SOUND_MONO:
- case VIDEO_SOUND_LANG1:
- case VIDEO_SOUND_LANG2:
- priv->audio_channels[i] = 1;
- break;
- case VIDEO_SOUND_STEREO:
- priv->audio_channels[i] = 2;
- break;
- }
-
- priv->audio_format[i] = AFMT_S16_LE;
- priv->audio_samplerate[i] = 44100;
- priv->audio_samplesize[i] =
- priv->audio_samplerate[i]/8/priv->fps*
- priv->audio_channels[i];
-
- /* display stuff */
- mp_msg(MSGT_TV, MSGL_V, " %d: %s: ", priv->audio[i].audio,
- priv->audio[i].name);
- if (priv->audio[i].flags & VIDEO_AUDIO_MUTABLE)
- mp_msg(MSGT_TV, MSGL_V, "muted=%s ",
- (priv->audio[i].flags & VIDEO_AUDIO_MUTE) ? "yes" : "no");
- mp_msg(MSGT_TV, MSGL_V, "volume=%d bass=%d treble=%d balance=%d mode=%s\n",
- priv->audio[i].volume, priv->audio[i].bass, priv->audio[i].treble,
- priv->audio[i].balance, audio_mode2name[priv->audio[i].mode]);
- mp_msg(MSGT_TV, MSGL_V, " channels: %d, samplerate: %d, samplesize: %d, format: %s\n",
- priv->audio_channels[i], priv->audio_samplerate[i], priv->audio_samplesize[i],
- 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");
@@ -338,88 +459,35 @@ static int init(priv_t *priv)
goto malloc_failed;
memset(priv->buf, 0, priv->nbuf * sizeof(struct video_mmap));
- /* audio init */
-#if 1
- priv->audio_fd = open(priv->audio_device, O_RDONLY);
- if (priv->audio_fd < 0)
- {
- mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n",
- priv->audio_device, strerror(errno));
- }
- else
- {
- int ioctl_param;
-
- fcntl(priv->audio_fd, F_SETFL, O_NONBLOCK);
+ /* init v4l audio even when we don't capture */
+ init_v4l_audio(priv);
-#if 0
- ioctl_param = 0x7fff000d; /* 8k */
- printf("ioctl dsp setfragment: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SETFRAGMENT, &ioctl_param));
-#endif
+ if (!priv->capability.audios) tv_param_noaudio = 1;
- ioctl_param = 0 ;
- printf("ioctl dsp getfmt: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param));
+ /* audio init */
+ if (!tv_param_noaudio) {
+ int err;
- printf("Supported formats: %x\n", ioctl_param);
- if (!(ioctl_param & priv->audio_format[priv->audio_id]))
- printf("notsupported format\n");
-
- ioctl_param = priv->audio_format[priv->audio_id];
- printf("ioctl dsp setfmt: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param));
-
-// ioctl(priv->audio_fd, SNDCTL_DSP_GETISPACE, &ioctl_param);
-// printf("getispace: %d\n", ioctl_param);
-
- if (priv->audio_channels[priv->audio_id] > 2)
- {
- ioctl_param = priv->audio_channels[priv->audio_id];
- printf("ioctl dsp channels: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param));
- }
+#ifdef HAVE_ALSA9
+ if (tv_param_alsa)
+ audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
else
- {
-// if (priv->audio_channels[priv->audio_id] == 2)
-// ioctl_param = 1;
-// else
-// ioctl_param = 0;
-
- ioctl_param = (priv->audio_channels[priv->audio_id] == 2);
- printf("ioctl dsp stereo: %d (req: %d)\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_STEREO, &ioctl_param),
- ioctl_param);
- }
-
- ioctl_param = priv->audio_samplerate[priv->audio_id];
- printf("ioctl dsp speed: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SPEED, &ioctl_param));
-
-#if 0
- ioctl_param = 0;
- ioctl_param = ~PCM_ENABLE_INPUT;
- printf("ioctl dsp trigger: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param));
- ioctl_param = PCM_ENABLE_INPUT;
- printf("ioctl dsp trigger: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param));
+ audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
+#else
+ audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
#endif
- printf("ioctl dsp trigger: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_GETTRIGGER, &ioctl_param));
- printf("trigger: %x\n", ioctl_param);
- ioctl_param = PCM_ENABLE_INPUT;
- printf("ioctl dsp trigger: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param));
-
- printf("ioctl dsp getblocksize: %d\n",
- ioctl(priv->audio_fd, SNDCTL_DSP_GETBLKSIZE, &priv->audio_blocksize));
- printf("blocksize: %d\n", priv->audio_blocksize);
+ if (tv_param_audio_id < priv->capability.audios)
+ priv->audio_id = tv_param_audio_id;
+ else
+ priv->audio_id = 0;
+ audio_in_set_samplerate(&priv->audio_in, 44100);
+ audio_in_set_channels(&priv->audio_in, priv->audio_channels[priv->audio_id]);
+ if (audio_in_setup(&priv->audio_in) < 0) return 0;
+ setup_audio_buffer_sizes(priv);
}
-#endif
- return(1);
+ return(1);
malloc_failed:
if (priv->channels)
@@ -434,20 +502,68 @@ err:
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->audio_fd);
+
+ close(priv->video_fd);
+
+ audio_in_uninit(&priv->audio_in);
+
+ 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);
+ if (priv->audio_skew_buffer)
+ free(priv->audio_skew_buffer);
+ }
return(1);
}
+static int get_capture_buffer_size(priv_t *priv)
+{
+ int bufsize, cnt;
+#ifdef HAVE_SYS_SYSINFO_H
+ 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,14 +573,12 @@ static int start(priv_t *priv)
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;
- printf("palette: %d, depth: %d, bytesperline: %d\n",
- priv->picture.palette, priv->picture.depth, priv->bytesperline);
+ if (priv->format != IMGFMT_BGR15) {
+ priv->bytesperline = priv->width * priv->picture.depth / 8;
+ } else {
+ priv->bytesperline = priv->width * 2;
+ }
mp_msg(MSGT_TV, MSGL_INFO, "Picture values:\n");
mp_msg(MSGT_TV, MSGL_INFO, " Depth: %d, Palette: %d (Format: %s)\n", priv->picture.depth,
@@ -520,26 +634,94 @@ static int start(priv_t *priv)
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) {
+ setup_audio_buffer_sizes(priv);
+ bytes_per_sample = priv->audio_in.bytes_per_sample;
+ priv->audio_skew_buffer = (double*)malloc(sizeof(double)*priv->aud_skew_cnt);
+ if (!priv->audio_skew_buffer) {
+ mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno));
+ return 0;
+ }
+
+ priv->audio_ringbuffer = (unsigned char*)malloc(priv->audio_in.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;
+ }
+
+ priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate
+ *priv->audio_in.channels
+ *bytes_per_sample);
+ 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);
+ }
+
+ if (!tv_param_noaudio) {
+ if (priv->video_buffer_size < 3.0*priv->fps*priv->audio_secs_per_block) {
+ mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n"
+ "You will probably experience heavy framedrops.\n");
+ }
+ }
+
+ 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;
+ priv->first = 1;
+
+ /* enable audio */
+ if (priv->audio[priv->audio_id].volume <= 0)
+ priv->audio[priv->audio_id].volume = 100;
+ priv->audio[priv->audio_id].flags &= ~VIDEO_AUDIO_MUTE;
+ ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]);
+
+ /* 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);
+ }
+ /* we'll launch the video capture later, when a first request for a frame arrives */
return(1);
}
static int control(priv_t *priv, int cmd, void *arg)
{
- mp_msg(MSGT_TV, MSGL_DBG2, "debug: control(priv=%p, cmd=%d, arg=%p)\n",
+ mp_msg(MSGT_TV, MSGL_DBG2, "\ndebug: control(priv=%p, cmd=%d, arg=%p)\n",
priv, cmd, arg);
switch(cmd)
{
@@ -576,6 +758,14 @@ static int control(priv_t *priv, int cmd, void *arg)
}
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 */
@@ -682,6 +872,7 @@ static int control(priv_t *priv, int cmd, void *arg)
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set freq failed: %s\n", strerror(errno));
return(TVI_CONTROL_FALSE);
}
+ usleep(100000); // wait to supress noise during switching
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_TUN_GET_TUNER:
@@ -700,7 +891,7 @@ static int control(priv_t *priv, int cmd, void *arg)
{
if (ioctl(priv->video_fd, VIDIOCSTUNER, &priv->tuner) == -1)
{
- mp_msg(MSGT_TV, MSGL_ERR, "ioctl get tuner failed: %s\n", strerror(errno));
+ mp_msg(MSGT_TV, MSGL_ERR, "ioctl set tuner failed: %s\n", strerror(errno));
return(TVI_CONTROL_FALSE);
}
return(TVI_CONTROL_TRUE);
@@ -708,20 +899,57 @@ static int control(priv_t *priv, int cmd, void *arg)
case TVI_CONTROL_TUN_SET_NORM:
{
int req_mode = (int)*(void **)arg;
-
- if ((!(priv->tuner.flags & VIDEO_TUNER_NORM)) ||
- ((req_mode == VIDEO_MODE_PAL) && !(priv->tuner.flags & VIDEO_TUNER_PAL)) ||
- ((req_mode == VIDEO_MODE_NTSC) && !(priv->tuner.flags & VIDEO_TUNER_NTSC)) ||
- ((req_mode == VIDEO_MODE_SECAM) && !(priv->tuner.flags & VIDEO_TUNER_SECAM)))
+
+ if ((req_mode != TV_NORM_PAL) && (req_mode != TV_NORM_NTSC) && (req_mode != TV_NORM_SECAM)) {
+ mp_msg(MSGT_TV, MSGL_ERR, "Unknown norm!\n");
+ return(TVI_CONTROL_FALSE);
+ }
+
+ if (((req_mode == TV_NORM_PAL) && !(priv->tuner.flags & VIDEO_TUNER_PAL)) ||
+ ((req_mode == TV_NORM_NTSC) && !(priv->tuner.flags & VIDEO_TUNER_NTSC)) ||
+ ((req_mode == TV_NORM_SECAM) && !(priv->tuner.flags & VIDEO_TUNER_SECAM)))
{
mp_msg(MSGT_TV, MSGL_ERR, "Tuner isn't capable to set norm!\n");
return(TVI_CONTROL_FALSE);
}
- priv->tuner.mode = req_mode;
+ switch(req_mode) {
+ case TV_NORM_PAL:
+ priv->tuner.mode = VIDEO_MODE_PAL;
+ break;
+ case TV_NORM_NTSC:
+ priv->tuner.mode = VIDEO_MODE_NTSC;
+ break;
+ case TV_NORM_SECAM:
+ priv->tuner.mode = VIDEO_MODE_SECAM;
+ break;
+ }
- if (control(priv, TVI_CONTROL_TUN_SET_TUNER, &priv->tuner) != TVI_CONTROL_TRUE)
+ if (control(priv, TVI_CONTROL_TUN_SET_TUNER, &priv->tuner) != TVI_CONTROL_TRUE) {
return(TVI_CONTROL_FALSE);
+ }
+
+ if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1) {
+ mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno));
+ return(TVI_CONTROL_FALSE);
+ }
+
+ if(req_mode == TV_NORM_PAL || req_mode == TV_NORM_SECAM) {
+ priv->fps = PAL_FPS;
+ }
+
+ if(req_mode == TV_NORM_NTSC) {
+ priv->fps = NTSC_FPS;
+ }
+
+ if(priv->height > priv->capability.maxheight) {
+ priv->height = priv->capability.maxheight;
+ }
+
+ if(priv->width > priv->capability.maxwidth) {
+ priv->width = priv->capability.maxwidth;
+ }
+
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_TUN_GET_NORM:
@@ -734,33 +962,28 @@ static int control(priv_t *priv, int cmd, void *arg)
/* ========== AUDIO controls =========== */
case TVI_CONTROL_AUD_GET_FORMAT:
{
- (int)*(void **)arg = priv->audio_format[priv->audio_id];
+ (int)*(void **)arg = AFMT_S16_LE;
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_AUD_GET_CHANNELS:
{
- (int)*(void **)arg = priv->audio_channels[priv->audio_id];
+ (int)*(void **)arg = priv->audio_in.channels;
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_AUD_GET_SAMPLERATE:
{
- (int)*(void **)arg = priv->audio_samplerate[priv->audio_id];
+ (int)*(void **)arg = priv->audio_in.samplerate;
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_AUD_GET_SAMPLESIZE:
{
- (int)*(void **)arg = priv->audio_samplesize[priv->audio_id]/8;
+ (int)*(void **)arg = priv->audio_in.bytes_per_sample;
return(TVI_CONTROL_TRUE);
}
case TVI_CONTROL_AUD_SET_SAMPLERATE:
{
- 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];
+ if (audio_in_set_samplerate(&priv->audio_in, (int)*(void **)arg) < 0) return TVI_CONTROL_FALSE;
+ setup_audio_buffer_sizes(priv);
return(TVI_CONTROL_TRUE);
}
/* ========== SPECIFIC controls =========== */
@@ -821,50 +1044,189 @@ static int control(priv_t *priv, int cmd, void *arg)
/* 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)
+ /* start the capture process */
+ if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[0]) == -1)
{
- mp_msg(MSGT_TV, MSGL_ERR, "ioctl mcapture failed: %s\n", strerror(errno));
- return(0);
+ mp_msg(MSGT_TV, MSGL_ERR, "\nioctl mcapture failed: %s\n", strerror(errno));
}
+ while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[1].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;
+ }
+
+ interval = curtime.tv_sec + curtime.tv_usec*.000001 - priv->starttime;
+
+ if (!priv->immediate_mode && (
+ ((interval - prev_interval < 1.0/priv->fps*0.85) && interval > 0.0001)
+ || (interval - prev_interval > 1.0/priv->fps*1.15) ) ) {
+ mp_msg(MSGT_TV, MSGL_V, "\nvideo capture thread: frame delta ~ %.1lf fps\n",
+ 1.0/(interval - prev_interval));
+ }
- mp_dbg(MSGT_TV, MSGL_DBG3, "mmap: %p + offset: %d => %p\n",
- priv->mmap, priv->mbuf.offsets[frame],
- priv->mmap+priv->mbuf.offsets[frame]);
+ // interpolate the skew in time
+ pthread_mutex_lock(&priv->skew_mutex);
+ xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor;
+ pthread_mutex_unlock(&priv->skew_mutex);
+ // 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;
+ }
- /* XXX also directrendering would be nicer! */
- /* 3 times copying the same picture to other buffer :( */
+ mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %lf, v_interval = %lf, a_skew = %f, corr_skew = %f\n",
+ 1.0/(interval - prev_interval), interval/2, xskew, skew);
+ mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt);
- /* copy the actual frame */
- memcpy(buffer, priv->mmap+priv->mbuf.offsets[frame], len);
+ prev_skew = skew;
+ prev_interval = interval;
+
+ if ((priv->video_tail+1) % priv->video_buffer_size == priv->video_head) {
+
+ 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;