2001-07-22 16:18:56 +02:00
|
|
|
/*
|
|
|
|
* Linux video grab interface
|
2009-01-19 16:46:40 +01:00
|
|
|
* Copyright (c) 2000,2001 Fabrice Bellard
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2006-10-07 17:30:46 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2002-05-26 00:34:32 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2008-03-20 20:07:24 +01:00
|
|
|
|
2008-08-19 23:20:39 +02:00
|
|
|
#undef __STRICT_ANSI__ //workaround due to broken kernel headers
|
2008-03-20 20:07:24 +01:00
|
|
|
#include "config.h"
|
2008-12-18 00:43:56 +01:00
|
|
|
#include "libavutil/rational.h"
|
2011-02-07 14:37:08 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2011-05-23 21:21:59 +02:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/opt.h"
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavcodec/dsputil.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
2003-09-09 21:14:05 +02:00
|
|
|
#define _LINUX_TIME_H 1
|
|
|
|
#include <linux/videodev.h>
|
2002-02-18 11:20:03 +01:00
|
|
|
#include <time.h>
|
2008-08-13 19:22:53 +02:00
|
|
|
#include <strings.h>
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2011-05-23 21:21:59 +02:00
|
|
|
AVClass *class;
|
2001-07-22 16:18:56 +02:00
|
|
|
int fd;
|
|
|
|
int frame_format; /* see VIDEO_PALETTE_xxx */
|
|
|
|
int use_mmap;
|
2008-12-18 00:43:56 +01:00
|
|
|
AVRational time_base;
|
2003-02-11 17:35:48 +01:00
|
|
|
int64_t time_frame;
|
2001-09-25 01:27:06 +02:00
|
|
|
int frame_size;
|
2002-11-19 15:57:42 +01:00
|
|
|
struct video_capability video_cap;
|
|
|
|
struct video_audio audio_saved;
|
2008-12-18 00:51:19 +01:00
|
|
|
struct video_window video_win;
|
2003-02-11 17:35:48 +01:00
|
|
|
uint8_t *video_buf;
|
2002-11-19 15:57:42 +01:00
|
|
|
struct video_mbuf gb_buffers;
|
|
|
|
struct video_mmap gb_buf;
|
|
|
|
int gb_frame;
|
2011-05-23 21:21:59 +02:00
|
|
|
int standard;
|
2001-07-22 16:18:56 +02:00
|
|
|
} VideoData;
|
|
|
|
|
2008-01-07 13:42:02 +01:00
|
|
|
static const struct {
|
2007-06-04 15:25:29 +02:00
|
|
|
int palette;
|
|
|
|
int depth;
|
|
|
|
enum PixelFormat pix_fmt;
|
|
|
|
} video_formats [] = {
|
|
|
|
{.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
|
|
|
|
{.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
|
2007-06-04 15:28:02 +02:00
|
|
|
{.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
|
|
|
|
{.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
|
2007-06-04 15:25:29 +02:00
|
|
|
/* NOTE: v4l uses BGR24, not RGB24 */
|
|
|
|
{.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
|
2007-06-04 15:28:02 +02:00
|
|
|
{.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
|
2007-06-04 15:25:29 +02:00
|
|
|
{.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
2008-12-26 10:31:21 +01:00
|
|
|
int video_fd;
|
2006-06-28 14:18:22 +02:00
|
|
|
int desired_palette, desired_depth;
|
2003-06-25 12:21:45 +02:00
|
|
|
struct video_tuner tuner;
|
2002-11-19 15:57:42 +01:00
|
|
|
struct video_audio audio;
|
2006-05-04 14:23:12 +02:00
|
|
|
struct video_picture pict;
|
2004-04-25 15:30:21 +02:00
|
|
|
int j;
|
2008-10-21 23:40:24 +02:00
|
|
|
int vformat_num = FF_ARRAY_ELEMS(video_formats);
|
2001-09-25 01:27:06 +02:00
|
|
|
|
2008-08-06 20:20:04 +02:00
|
|
|
if (ap->time_base.den <= 0) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
|
2001-09-25 01:27:06 +02:00
|
|
|
return -1;
|
2006-03-13 10:47:37 +01:00
|
|
|
}
|
2008-12-18 00:43:56 +01:00
|
|
|
s->time_base = ap->time_base;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2008-12-18 00:51:19 +01:00
|
|
|
s->video_win.width = ap->width;
|
|
|
|
s->video_win.height = ap->height;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
st = av_new_stream(s1, 0);
|
|
|
|
if (!st)
|
2007-02-13 19:26:14 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2006-02-01 12:31:33 +01:00
|
|
|
av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
2001-09-25 01:27:06 +02:00
|
|
|
|
2007-02-14 18:01:32 +01:00
|
|
|
video_fd = open(s1->filename, O_RDWR);
|
2001-07-22 16:18:56 +02:00
|
|
|
if (video_fd < 0) {
|
2007-11-19 08:54:04 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
|
2001-09-25 01:27:06 +02:00
|
|
|
goto fail;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2008-12-26 10:07:51 +01:00
|
|
|
if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
|
2007-11-19 08:54:04 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
|
2001-07-22 16:18:56 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2002-11-19 15:57:42 +01:00
|
|
|
if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
|
2005-12-22 02:10:11 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
|
2001-07-22 16:18:56 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2002-04-08 22:58:56 +02:00
|
|
|
|
2008-12-26 11:12:57 +01:00
|
|
|
/* no values set, autodetect them */
|
|
|
|
if (s->video_win.width <= 0 || s->video_win.height <= 0) {
|
|
|
|
if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 21:15:29 +02:00
|
|
|
if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
|
2008-12-28 15:41:19 +01:00
|
|
|
return -1;
|
|
|
|
|
2002-04-08 22:58:56 +02:00
|
|
|
desired_palette = -1;
|
2006-06-28 14:18:22 +02:00
|
|
|
desired_depth = -1;
|
2007-06-04 15:25:29 +02:00
|
|
|
for (j = 0; j < vformat_num; j++) {
|
|
|
|
if (ap->pix_fmt == video_formats[j].pix_fmt) {
|
|
|
|
desired_palette = video_formats[j].palette;
|
|
|
|
desired_depth = video_formats[j].depth;
|
|
|
|
break;
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
}
|
2003-06-25 12:21:45 +02:00
|
|
|
|
|
|
|
/* set tv standard */
|
2011-05-23 21:21:59 +02:00
|
|
|
if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
|
|
|
|
if (ap->standard) {
|
|
|
|
if (!strcasecmp(ap->standard, "pal"))
|
|
|
|
s->standard = VIDEO_MODE_PAL;
|
|
|
|
else if (!strcasecmp(ap->standard, "secam"))
|
|
|
|
s->standard = VIDEO_MODE_SECAM;
|
|
|
|
else
|
|
|
|
s->standard = VIDEO_MODE_NTSC;
|
|
|
|
}
|
|
|
|
tuner.mode = s->standard;
|
2005-12-22 02:10:11 +01:00
|
|
|
ioctl(video_fd, VIDIOCSTUNER, &tuner);
|
2003-06-25 12:21:45 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* unmute audio */
|
2002-11-19 15:57:42 +01:00
|
|
|
audio.audio = 0;
|
2001-07-22 16:18:56 +02:00
|
|
|
ioctl(video_fd, VIDIOCGAUDIO, &audio);
|
2002-11-19 15:57:42 +01:00
|
|
|
memcpy(&s->audio_saved, &audio, sizeof(audio));
|
2001-07-22 16:18:56 +02:00
|
|
|
audio.flags &= ~VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(video_fd, VIDIOCSAUDIO, &audio);
|
|
|
|
|
2006-05-04 14:23:12 +02:00
|
|
|
ioctl(video_fd, VIDIOCGPICT, &pict);
|
2011-04-29 17:27:01 +02:00
|
|
|
av_dlog(s1, "v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
|
|
|
|
pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness);
|
2006-05-04 14:23:12 +02:00
|
|
|
/* try to choose a suitable video format */
|
|
|
|
pict.palette = desired_palette;
|
2006-06-28 14:18:22 +02:00
|
|
|
pict.depth= desired_depth;
|
2008-12-07 23:52:32 +01:00
|
|
|
if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
|
2007-06-04 15:25:29 +02:00
|
|
|
for (j = 0; j < vformat_num; j++) {
|
|
|
|
pict.palette = video_formats[j].palette;
|
|
|
|
pict.depth = video_formats[j].depth;
|
|
|
|
if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
|
|
|
|
break;
|
2006-05-04 14:23:12 +02:00
|
|
|
}
|
2007-06-04 15:25:29 +02:00
|
|
|
if (j >= vformat_num)
|
|
|
|
goto fail1;
|
2006-05-04 14:23:12 +02:00
|
|
|
}
|
|
|
|
|
2008-12-26 10:07:51 +01:00
|
|
|
if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
|
2001-07-22 16:18:56 +02:00
|
|
|
/* try to use read based access */
|
|
|
|
int val;
|
|
|
|
|
2008-12-18 00:51:19 +01:00
|
|
|
s->video_win.x = 0;
|
|
|
|
s->video_win.y = 0;
|
|
|
|
s->video_win.chromakey = -1;
|
|
|
|
s->video_win.flags = 0;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2008-12-27 12:40:07 +01:00
|
|
|
if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
s->frame_format = pict.palette;
|
|
|
|
|
|
|
|
val = 1;
|
2008-12-26 10:12:12 +01:00
|
|
|
if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2008-12-18 00:43:56 +01:00
|
|
|
s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
|
2001-07-22 16:18:56 +02:00
|
|
|
s->use_mmap = 0;
|
|
|
|
} else {
|
2008-12-26 10:07:51 +01:00
|
|
|
s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
|
2002-11-19 15:57:42 +01:00
|
|
|
if ((unsigned char*)-1 == s->video_buf) {
|
2008-12-26 10:07:51 +01:00
|
|
|
s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
|
2006-12-28 01:32:06 +01:00
|
|
|
if ((unsigned char*)-1 == s->video_buf) {
|
2007-11-19 08:54:04 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
|
2006-12-28 01:32:06 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-11-19 15:57:42 +01:00
|
|
|
s->gb_frame = 0;
|
2008-12-18 00:43:56 +01:00
|
|
|
s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* start to grab the first frame */
|
2002-11-19 15:57:42 +01:00
|
|
|
s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
|
2008-12-18 00:51:19 +01:00
|
|
|
s->gb_buf.height = s->video_win.height;
|
|
|
|
s->gb_buf.width = s->video_win.width;
|
2006-05-04 14:23:12 +02:00
|
|
|
s->gb_buf.format = pict.palette;
|
|
|
|
|
2008-12-07 23:52:32 +01:00
|
|
|
if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
|
2001-07-22 16:18:56 +02:00
|
|
|
if (errno != EAGAIN) {
|
|
|
|
fail1:
|
2008-12-27 12:44:56 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2008-12-26 10:07:51 +01:00
|
|
|
av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-12-22 02:10:11 +01:00
|
|
|
for (j = 1; j < s->gb_buffers.frames; j++) {
|
|
|
|
s->gb_buf.frame = j;
|
|
|
|
ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
|
|
|
|
}
|
2002-11-19 15:57:42 +01:00
|
|
|
s->frame_format = s->gb_buf.format;
|
2001-07-22 16:18:56 +02:00
|
|
|
s->use_mmap = 1;
|
|
|
|
}
|
|
|
|
|
2007-06-04 15:25:29 +02:00
|
|
|
for (j = 0; j < vformat_num; j++) {
|
|
|
|
if (s->frame_format == video_formats[j].palette) {
|
2008-12-26 10:31:21 +01:00
|
|
|
s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
|
2007-06-04 15:25:29 +02:00
|
|
|
st->codec->pix_fmt = video_formats[j].pix_fmt;
|
|
|
|
break;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2007-06-04 15:25:29 +02:00
|
|
|
|
|
|
|
if (j >= vformat_num)
|
|
|
|
goto fail;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->fd = video_fd;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->codec_id = CODEC_ID_RAWVIDEO;
|
2008-12-18 00:51:19 +01:00
|
|
|
st->codec->width = s->video_win.width;
|
|
|
|
st->codec->height = s->video_win.height;
|
2008-12-18 00:43:56 +01:00
|
|
|
st->codec->time_base = s->time_base;
|
2008-12-26 10:31:21 +01:00
|
|
|
st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
|
2002-11-19 16:52:29 +01:00
|
|
|
|
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);
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2003-02-11 17:35:48 +01:00
|
|
|
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-02-11 17:35:48 +01:00
|
|
|
uint8_t *ptr;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2004-04-25 15:30:21 +02:00
|
|
|
while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
|
|
|
|
(errno == EAGAIN || errno == EINTR));
|
|
|
|
|
|
|
|
ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
|
|
|
|
memcpy(buf, ptr, s->frame_size);
|
|
|
|
|
2002-04-08 22:58:56 +02:00
|
|
|
/* Setup to capture the next frame */
|
2004-04-25 15:30:21 +02:00
|
|
|
s->gb_buf.frame = s->gb_frame;
|
2002-11-19 15:57:42 +01:00
|
|
|
if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
|
2002-05-09 03:18:42 +02:00
|
|
|
if (errno == EAGAIN)
|
2004-03-03 16:41:21 +01:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
|
2002-05-09 03:18:42 +02:00
|
|
|
else
|
2007-11-19 08:54:04 +01:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-04-08 22:58:56 +02:00
|
|
|
|
|
|
|
/* This is now the grabbing frame */
|
2004-04-25 15:30:21 +02:00
|
|
|
s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
|
2002-04-08 22:58:56 +02:00
|
|
|
|
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;
|
2003-02-11 17:35:48 +01:00
|
|
|
int64_t curtime, delay;
|
2001-09-25 01:27:06 +02:00
|
|
|
struct timespec ts;
|
2002-04-08 22:58:56 +02:00
|
|
|
|
|
|
|
/* Calculate the time of the next frame */
|
2006-12-07 00:46:11 +01:00
|
|
|
s->time_frame += INT64_C(1000000);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* wait based on the frame rate */
|
2002-11-19 16:52:29 +01:00
|
|
|
for(;;) {
|
2002-07-25 18:05:25 +02:00
|
|
|
curtime = av_gettime();
|
2008-12-18 00:43:56 +01:00
|
|
|
delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
|
2002-04-08 22:58:56 +02:00
|
|
|
if (delay <= 0) {
|
2008-12-18 00:43:56 +01:00
|
|
|
if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
|
2002-04-08 22:58:56 +02:00
|
|
|
/* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
|
2006-12-07 00:46:11 +01:00
|
|
|
s->time_frame += INT64_C(1000000);
|
2002-04-08 22:58:56 +02:00
|
|
|
}
|
2001-09-25 01:27:06 +02:00
|
|
|
break;
|
2005-12-17 19:14:38 +01: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)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2006-02-01 12:31:33 +01:00
|
|
|
pkt->pts = curtime;
|
2002-11-19 16:52:29 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* read one frame */
|
2007-12-28 04:30:23 +01:00
|
|
|
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)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(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;
|
2002-09-01 10:54:22 +02:00
|
|
|
|
|
|
|
if (s->use_mmap)
|
2002-11-19 15:57:42 +01:00
|
|
|
munmap(s->video_buf, s->gb_buffers.size);
|
2002-09-01 10:54:22 +02:00
|
|
|
|
2002-11-19 15:57:42 +01:00
|
|
|
/* mute audio. we must force it because the BTTV driver does not
|
|
|
|
return its state correctly */
|
|
|
|
s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
|
2001-09-16 23:49:48 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
close(s->fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-23 21:21:59 +02:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
|
|
|
|
{ "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
|
|
|
|
{ "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
|
|
|
|
{ "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass v4l_class = {
|
|
|
|
.class_name = "V4L indev",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
AVInputFormat ff_v4l_demuxer = {
|
2003-01-22 23:40:52 +01:00
|
|
|
"video4linux",
|
2008-12-26 12:23:50 +01:00
|
|
|
NULL_IF_CONFIG_SMALL("Video4Linux device 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-10-07 01:06:06 +02:00
|
|
|
.flags = AVFMT_NOFILE,
|
2011-05-23 21:21:59 +02:00
|
|
|
.priv_class = &v4l_class,
|
2001-07-22 16:18:56 +02:00
|
|
|
};
|