2001-07-22 16:18:56 +02:00
|
|
|
/*
|
|
|
|
* Linux video grab interface
|
2002-05-26 00:34:32 +02:00
|
|
|
* Copyright (c) 2000,2001 Fabrice Bellard.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-22 16:18:56 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 00:34:32 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2001-08-13 23:37:10 +02:00
|
|
|
#include "avformat.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
#include <linux/videodev.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
2002-02-18 11:20:03 +01:00
|
|
|
#include <time.h>
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
int frame_format; /* see VIDEO_PALETTE_xxx */
|
|
|
|
int use_mmap;
|
|
|
|
int width, height;
|
2001-09-25 01:27:06 +02:00
|
|
|
int frame_rate;
|
2001-07-22 16:18:56 +02:00
|
|
|
INT64 time_frame;
|
2001-09-25 01:27:06 +02:00
|
|
|
int frame_size;
|
2001-07-22 16:18:56 +02:00
|
|
|
} VideoData;
|
|
|
|
|
|
|
|
const char *v4l_device = "/dev/video";
|
|
|
|
|
|
|
|
/* XXX: move all that to the context */
|
|
|
|
|
|
|
|
static struct video_capability video_cap;
|
|
|
|
static UINT8 *video_buf;
|
|
|
|
static struct video_mbuf gb_buffers;
|
|
|
|
static struct video_mmap gb_buf;
|
2001-09-16 23:49:48 +02:00
|
|
|
static struct video_audio audio, audio_saved;
|
2001-07-22 16:18:56 +02:00
|
|
|
static int gb_frame = 0;
|
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-05-20 18:31:13 +02:00
|
|
|
VideoData *s = s1->priv_data;
|
2001-09-25 01:27:06 +02:00
|
|
|
AVStream *st;
|
2001-07-22 16:18:56 +02:00
|
|
|
int width, height;
|
|
|
|
int video_fd, frame_size;
|
2001-09-25 01:27:06 +02:00
|
|
|
int ret, frame_rate;
|
2002-04-08 22:58:56 +02:00
|
|
|
int desired_palette;
|
2001-09-25 01:27:06 +02:00
|
|
|
|
|
|
|
if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
|
|
|
|
return -1;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
width = ap->width;
|
|
|
|
height = ap->height;
|
|
|
|
frame_rate = ap->frame_rate;
|
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
st = av_new_stream(s1, 0);
|
|
|
|
if (!st)
|
2001-09-25 01:27:06 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
s->width = width;
|
|
|
|
s->height = height;
|
|
|
|
s->frame_rate = frame_rate;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
video_fd = open(v4l_device, O_RDWR);
|
|
|
|
if (video_fd < 0) {
|
|
|
|
perror(v4l_device);
|
2001-09-25 01:27:06 +02:00
|
|
|
goto fail;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
|
|
|
|
perror("VIDIOCGCAP");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(video_cap.type & VID_TYPE_CAPTURE)) {
|
|
|
|
fprintf(stderr, "Fatal: grab device does not handle capture\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-04-08 22:58:56 +02:00
|
|
|
|
|
|
|
desired_palette = -1;
|
|
|
|
if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
|
|
|
|
desired_palette = VIDEO_PALETTE_YUV420P;
|
|
|
|
} else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
|
|
|
|
desired_palette = VIDEO_PALETTE_YUV422;
|
|
|
|
} else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
|
|
|
|
desired_palette = VIDEO_PALETTE_RGB24;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* unmute audio */
|
|
|
|
ioctl(video_fd, VIDIOCGAUDIO, &audio);
|
2001-09-16 23:49:48 +02:00
|
|
|
memcpy(&audio_saved, &audio, sizeof(audio));
|
2001-07-22 16:18:56 +02:00
|
|
|
audio.flags &= ~VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(video_fd, VIDIOCSAUDIO, &audio);
|
|
|
|
|
|
|
|
ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
|
|
|
|
if (ret < 0) {
|
|
|
|
/* try to use read based access */
|
|
|
|
struct video_window win;
|
|
|
|
struct video_picture pict;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
win.x = 0;
|
|
|
|
win.y = 0;
|
|
|
|
win.width = width;
|
|
|
|
win.height = height;
|
|
|
|
win.chromakey = -1;
|
|
|
|
win.flags = 0;
|
|
|
|
|
|
|
|
ioctl(video_fd, VIDIOCSWIN, &win);
|
|
|
|
|
|
|
|
ioctl(video_fd, VIDIOCGPICT, &pict);
|
|
|
|
#if 0
|
|
|
|
printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
|
|
|
|
pict.colour,
|
|
|
|
pict.hue,
|
|
|
|
pict.brightness,
|
|
|
|
pict.contrast,
|
|
|
|
pict.whiteness);
|
|
|
|
#endif
|
|
|
|
/* try to choose a suitable video format */
|
2002-04-08 22:58:56 +02:00
|
|
|
pict.palette = desired_palette;
|
|
|
|
if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
|
|
|
|
pict.palette=VIDEO_PALETTE_YUV420P;
|
2001-07-22 16:18:56 +02:00
|
|
|
ret = ioctl(video_fd, VIDIOCSPICT, &pict);
|
|
|
|
if (ret < 0) {
|
2002-04-08 22:58:56 +02:00
|
|
|
pict.palette=VIDEO_PALETTE_YUV422;
|
2001-07-22 16:18:56 +02:00
|
|
|
ret = ioctl(video_fd, VIDIOCSPICT, &pict);
|
2002-04-08 22:58:56 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
pict.palette=VIDEO_PALETTE_RGB24;
|
|
|
|
ret = ioctl(video_fd, VIDIOCSPICT, &pict);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail1;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->frame_format = pict.palette;
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
ioctl(video_fd, VIDIOCCAPTURE, &val);
|
|
|
|
|
2002-07-25 18:05:25 +02:00
|
|
|
s->time_frame = av_gettime();
|
2001-07-22 16:18:56 +02:00
|
|
|
s->use_mmap = 0;
|
|
|
|
} else {
|
|
|
|
video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
|
|
|
|
if ((unsigned char*)-1 == video_buf) {
|
|
|
|
perror("mmap");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
gb_frame = 0;
|
2002-07-25 18:05:25 +02:00
|
|
|
s->time_frame = av_gettime();
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* start to grab the first frame */
|
2002-04-08 22:58:56 +02:00
|
|
|
gb_buf.frame = gb_frame % gb_buffers.frames;
|
2001-07-22 16:18:56 +02:00
|
|
|
gb_buf.height = height;
|
|
|
|
gb_buf.width = width;
|
2002-04-08 22:58:56 +02:00
|
|
|
gb_buf.format = desired_palette;
|
|
|
|
|
|
|
|
if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf)) < 0) {
|
|
|
|
gb_buf.format = VIDEO_PALETTE_YUV420P;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
|
2002-04-08 22:58:56 +02:00
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
|
|
|
/* try YUV422 */
|
|
|
|
gb_buf.format = VIDEO_PALETTE_YUV422;
|
|
|
|
|
|
|
|
ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
|
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
|
|
|
/* try RGB24 */
|
|
|
|
gb_buf.format = VIDEO_PALETTE_RGB24;
|
|
|
|
ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
|
|
|
|
}
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
fail1:
|
|
|
|
fprintf(stderr, "Fatal: grab device does not support suitable format\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->frame_format = gb_buf.format;
|
|
|
|
s->use_mmap = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(s->frame_format) {
|
|
|
|
case VIDEO_PALETTE_YUV420P:
|
|
|
|
frame_size = (width * height * 3) / 2;
|
2001-09-25 01:27:06 +02:00
|
|
|
st->codec.pix_fmt = PIX_FMT_YUV420P;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case VIDEO_PALETTE_YUV422:
|
|
|
|
frame_size = width * height * 2;
|
2001-09-25 01:27:06 +02:00
|
|
|
st->codec.pix_fmt = PIX_FMT_YUV422;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case VIDEO_PALETTE_RGB24:
|
|
|
|
frame_size = width * height * 3;
|
2001-09-25 01:27:06 +02:00
|
|
|
st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->fd = video_fd;
|
2001-09-25 01:27:06 +02:00
|
|
|
s->frame_size = frame_size;
|
|
|
|
|
2002-05-09 03:18:42 +02:00
|
|
|
st->codec.codec_type = CODEC_TYPE_VIDEO;
|
2001-09-25 01:27:06 +02:00
|
|
|
st->codec.codec_id = CODEC_ID_RAWVIDEO;
|
|
|
|
st->codec.width = width;
|
|
|
|
st->codec.height = height;
|
|
|
|
st->codec.frame_rate = frame_rate;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
fail:
|
2001-09-25 01:27:06 +02:00
|
|
|
if (video_fd >= 0)
|
|
|
|
close(video_fd);
|
2002-05-19 01:11:09 +02:00
|
|
|
av_free(st);
|
2001-07-22 16:18:56 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
2002-05-02 06:21:22 +02:00
|
|
|
struct timeval tv_s;
|
|
|
|
//struct timeval tv_e;
|
|
|
|
//int delay;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-04-08 22:58:56 +02:00
|
|
|
/* Setup to capture the next frame */
|
|
|
|
gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
|
2001-07-22 16:18:56 +02:00
|
|
|
if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
|
2002-05-09 03:18:42 +02:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
fprintf(stderr,"Cannot Sync\n");
|
|
|
|
else
|
2001-07-22 16:18:56 +02:00
|
|
|
perror("VIDIOCMCAPTURE");
|
2002-05-09 03:18:42 +02:00
|
|
|
return -EIO;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-04-08 22:58:56 +02:00
|
|
|
|
|
|
|
gettimeofday(&tv_s, 0);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
|
|
|
|
(errno == EAGAIN || errno == EINTR));
|
|
|
|
|
|
|
|
ptr = video_buf + gb_buffers.offsets[gb_frame];
|
2001-09-25 01:27:06 +02:00
|
|
|
memcpy(buf, ptr, s->frame_size);
|
2002-04-08 22:58:56 +02:00
|
|
|
|
|
|
|
/* This is now the grabbing frame */
|
|
|
|
gb_frame = gb_buf.frame;
|
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
return s->frame_size;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2001-09-25 01:27:06 +02:00
|
|
|
VideoData *s = s1->priv_data;
|
|
|
|
INT64 curtime, delay;
|
|
|
|
struct timespec ts;
|
2002-04-08 22:58:56 +02:00
|
|
|
int first;
|
|
|
|
INT64 per_frame = (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
|
|
|
|
int dropped = 0;
|
|
|
|
|
|
|
|
/* Calculate the time of the next frame */
|
|
|
|
s->time_frame += per_frame;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* wait based on the frame rate */
|
2002-04-08 22:58:56 +02:00
|
|
|
for(first = 1;; first = 0) {
|
2002-07-25 18:05:25 +02:00
|
|
|
curtime = av_gettime();
|
2001-09-25 01:27:06 +02:00
|
|
|
delay = s->time_frame - curtime;
|
2002-04-08 22:58:56 +02:00
|
|
|
if (delay <= 0) {
|
|
|
|
if (delay < -per_frame) {
|
|
|
|
/* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
|
|
|
|
dropped = 1;
|
|
|
|
s->time_frame += per_frame;
|
|
|
|
}
|
2001-09-25 01:27:06 +02:00
|
|
|
break;
|
2002-04-08 22:58:56 +02:00
|
|
|
}
|
2001-09-25 01:27:06 +02:00
|
|
|
ts.tv_sec = delay / 1000000;
|
|
|
|
ts.tv_nsec = (delay % 1000000) * 1000;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, s->frame_size) < 0)
|
|
|
|
return -EIO;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-04-08 22:58:56 +02:00
|
|
|
if (dropped)
|
|
|
|
pkt->flags |= PKT_FLAG_DROPPED_FRAME;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* read one frame */
|
|
|
|
if (s->use_mmap) {
|
2001-09-25 01:27:06 +02:00
|
|
|
return v4l_mm_read_picture(s, pkt->data);
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2001-09-25 01:27:06 +02:00
|
|
|
if (read(s->fd, pkt->data, pkt->size) != pkt->size)
|
2001-07-22 16:18:56 +02:00
|
|
|
return -EIO;
|
2001-09-25 01:27:06 +02:00
|
|
|
return s->frame_size;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-25 01:27:06 +02:00
|
|
|
static int grab_read_close(AVFormatContext *s1)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2001-09-25 01:27:06 +02:00
|
|
|
VideoData *s = s1->priv_data;
|
2001-09-16 23:49:48 +02:00
|
|
|
/* restore audio settings */
|
|
|
|
ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
close(s->fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
AVInputFormat video_grab_device_format = {
|
2001-09-25 01:27:06 +02:00
|
|
|
"video_grab_device",
|
|
|
|
"video grab",
|
2002-05-20 18:31:13 +02:00
|
|
|
sizeof(VideoData),
|
2001-09-25 01:27:06 +02:00
|
|
|
NULL,
|
|
|
|
grab_read_header,
|
|
|
|
grab_read_packet,
|
|
|
|
grab_read_close,
|
2002-05-20 18:31:13 +02:00
|
|
|
flags: AVFMT_NOFILE,
|
2001-07-22 16:18:56 +02:00
|
|
|
};
|
2002-05-20 18:31:13 +02:00
|
|
|
|
|
|
|
int video_grab_init(void)
|
|
|
|
{
|
|
|
|
av_register_input_format(&video_grab_device_format);
|
|
|
|
return 0;
|
|
|
|
}
|