* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "avformat.h"
-#include <linux/videodev.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
+#define _LINUX_TIME_H 1
+#include <linux/videodev.h>
#include <time.h>
typedef struct {
int use_mmap;
int width, height;
int frame_rate;
- INT64 time_frame;
+ int frame_rate_base;
+ int64_t time_frame;
int frame_size;
struct video_capability video_cap;
struct video_audio audio_saved;
- UINT8 *video_buf;
+ uint8_t *video_buf;
struct video_mbuf gb_buffers;
struct video_mmap gb_buf;
int gb_frame;
int aiw_enabled;
int deint;
int halfw;
- UINT8 *src_mem;
- UINT8 *lum_m4_mem;
+ uint8_t *src_mem;
+ uint8_t *lum_m4_mem;
} VideoData;
static int aiw_init(VideoData *s);
AVStream *st;
int width, height;
int video_fd, frame_size;
- int ret, frame_rate;
+ int ret, frame_rate, frame_rate_base;
int desired_palette;
+ struct video_tuner tuner;
struct video_audio audio;
const char *video_device;
+ int j;
- if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
+ if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0)
return -1;
width = ap->width;
height = ap->height;
- frame_rate = ap->frame_rate;
+ frame_rate = ap->time_base.den;
+ frame_rate_base = ap->time_base.num;
+ if((unsigned)width > 32767 || (unsigned)height > 32767)
+ return -1;
+
st = av_new_stream(s1, 0);
if (!st)
return -ENOMEM;
+ av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
s->width = width;
s->height = height;
- s->frame_rate = frame_rate;
+ s->frame_rate = frame_rate;
+ s->frame_rate_base = frame_rate_base;
video_device = ap->device;
if (!video_device)
}
if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
- fprintf(stderr, "Fatal: grab device does not handle capture\n");
+ av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
goto fail;
}
desired_palette = -1;
- if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
+ if (st->codec->pix_fmt == PIX_FMT_YUV420P) {
desired_palette = VIDEO_PALETTE_YUV420P;
- } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
+ } else if (st->codec->pix_fmt == PIX_FMT_YUV422) {
desired_palette = VIDEO_PALETTE_YUV422;
- } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
+ } else if (st->codec->pix_fmt == PIX_FMT_BGR24) {
desired_palette = VIDEO_PALETTE_RGB24;
}
+
+ /* set tv standard */
+ if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
+ if (!strcasecmp(ap->standard, "pal"))
+ tuner.mode = VIDEO_MODE_PAL;
+ else if (!strcasecmp(ap->standard, "secam"))
+ tuner.mode = VIDEO_MODE_SECAM;
+ else
+ tuner.mode = VIDEO_MODE_NTSC;
+ ioctl(video_fd, VIDIOCSTUNER, &tuner);
+ }
/* unmute audio */
audio.audio = 0;
val = 1;
ioctl(video_fd, VIDIOCCAPTURE, &val);
- s->time_frame = av_gettime();
+ s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
s->use_mmap = 0;
/* ATI All In Wonder automatic activation */
goto fail;
}
s->gb_frame = 0;
- s->time_frame = av_gettime();
+ s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
/* start to grab the first frame */
s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
if (ret < 0) {
if (errno != EAGAIN) {
fail1:
- fprintf(stderr, "Fatal: grab device does not support suitable format\n");
+ av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
} else {
- fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
+ av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
}
goto fail;
}
+ for (j = 1; j < s->gb_buffers.frames; j++) {
+ s->gb_buf.frame = j;
+ ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
+ }
s->frame_format = s->gb_buf.format;
s->use_mmap = 1;
}
switch(s->frame_format) {
case VIDEO_PALETTE_YUV420P:
frame_size = (width * height * 3) / 2;
- st->codec.pix_fmt = PIX_FMT_YUV420P;
+ st->codec->pix_fmt = PIX_FMT_YUV420P;
break;
case VIDEO_PALETTE_YUV422:
frame_size = width * height * 2;
- st->codec.pix_fmt = PIX_FMT_YUV422;
+ st->codec->pix_fmt = PIX_FMT_YUV422;
break;
case VIDEO_PALETTE_RGB24:
frame_size = width * height * 3;
- st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
+ st->codec->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
break;
default:
goto fail;
s->fd = video_fd;
s->frame_size = frame_size;
- st->codec.codec_type = CODEC_TYPE_VIDEO;
- st->codec.codec_id = CODEC_ID_RAWVIDEO;
- st->codec.width = width;
- st->codec.height = height;
- st->codec.frame_rate = frame_rate;
-
- av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
+ st->codec->codec_type = CODEC_TYPE_VIDEO;
+ st->codec->codec_id = CODEC_ID_RAWVIDEO;
+ st->codec->width = width;
+ st->codec->height = height;
+ st->codec->time_base.den = frame_rate;
+ st->codec->time_base.num = frame_rate_base;
+ st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
return 0;
fail:
if (video_fd >= 0)
close(video_fd);
av_free(st);
- return -EIO;
+ return AVERROR_IO;
}
-static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
+static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
{
- UINT8 *ptr;
+ uint8_t *ptr;
+
+ 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);
/* Setup to capture the next frame */
- s->gb_buf.frame = (s->gb_frame + 1) % s->gb_buffers.frames;
+ s->gb_buf.frame = s->gb_frame;
if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
if (errno == EAGAIN)
- fprintf(stderr,"Cannot Sync\n");
+ av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
else
perror("VIDIOCMCAPTURE");
- return -EIO;
+ return AVERROR_IO;
}
- 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);
-
/* This is now the grabbing frame */
- s->gb_frame = s->gb_buf.frame;
+ s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
return s->frame_size;
}
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
VideoData *s = s1->priv_data;
- INT64 curtime, delay;
+ int64_t curtime, delay;
struct timespec ts;
- INT64 per_frame = (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
/* Calculate the time of the next frame */
- s->time_frame += per_frame;
+ s->time_frame += int64_t_C(1000000);
/* wait based on the frame rate */
for(;;) {
curtime = av_gettime();
- delay = s->time_frame - curtime;
+ delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
if (delay <= 0) {
- if (delay < -per_frame) {
+ if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
/* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
- s->time_frame += per_frame;
+ s->time_frame += int64_t_C(1000000);
}
break;
}
}
if (av_new_packet(pkt, s->frame_size) < 0)
- return -EIO;
+ return AVERROR_IO;
pkt->pts = curtime & ((1LL << 48) - 1);
return v4l_mm_read_picture(s, pkt->data);
} else {
if (read(s->fd, pkt->data, pkt->size) != pkt->size)
- return -EIO;
+ return AVERROR_IO;
return s->frame_size;
}
}
if (height == s->video_cap.maxheight*2) s->deint=1;
if (width == s->video_cap.maxwidth/2) s->halfw=1;
} else {
- fprintf(stderr,"\nIncorrect Grab Size Supplied - Supported Sizes Are:\n");
- fprintf(stderr," %dx%d %dx%d %dx%d\n\n",
+ av_log(NULL, AV_LOG_ERROR, "\nIncorrect Grab Size Supplied - Supported Sizes Are:\n");
+ av_log(NULL, AV_LOG_ERROR, " %dx%d %dx%d %dx%d\n\n",
s->video_cap.maxwidth,s->video_cap.maxheight,
s->video_cap.maxwidth,s->video_cap.maxheight*2,
s->video_cap.maxwidth/2,s->video_cap.maxheight);
/* Read two fields separately. */
static int aiw_read_picture(VideoData *s, uint8_t *data)
{
- UINT8 *ptr, *lum, *cb, *cr;
+ uint8_t *ptr, *lum, *cb, *cr;
int h;
#ifndef HAVE_MMX
int sum;
#endif
- UINT8* src = s->src_mem;
- UINT8 *ptrend = &src[s->width*2];
+ uint8_t* src = s->src_mem;
+ uint8_t *ptrend = &src[s->width*2];
lum=data;
cb=&lum[s->width*s->height];
cr=&cb[(s->width*s->height)/4];
read(s->fd,src,s->width*4);
}
} else {
- UINT8 *lum_m1, *lum_m2, *lum_m3, *lum_m4;
+ uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4;
#ifdef HAVE_MMX
mmx_t rounder;
rounder.uw[0]=4;
movq_m2r(rounder,mm6);
pxor_r2r(mm7,mm7);
#else
- UINT8 *cm = cropTbl + MAX_NEG_CROP;
+ uint8_t *cm = cropTbl + MAX_NEG_CROP;
#endif
/* read two fields and deinterlace them */