libavdevice/v4l.c
de6d9b64
 /*
  * Linux video grab interface
406792e7
  * Copyright (c) 2000,2001 Fabrice Bellard
de6d9b64
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
19720f15
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
de6d9b64
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
de6d9b64
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
de6d9b64
  *
19720f15
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64
  */
b0067549
 
3d96c13e
 #include "avdevice.h"
 
e9d4b8a5
 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
b0067549
 #include "config.h"
9580ba26
 #include "libavutil/rational.h"
7ffe76e5
 #include "libavutil/imgutils.h"
a861ffef
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
c3f9ebf7
 #include "libavformat/internal.h"
245976da
 #include "libavcodec/dsputil.h"
de6d9b64
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/time.h>
72a476c1
 #define _LINUX_TIME_H 1
 #include <linux/videodev.h>
608d0dee
 #include <time.h>
6b899e16
 #include "avdevice.h"
de6d9b64
 
 typedef struct {
a861ffef
     AVClass *class;
de6d9b64
     int fd;
     int frame_format; /* see VIDEO_PALETTE_xxx */
     int use_mmap;
9580ba26
     AVRational time_base;
0c1a9eda
     int64_t time_frame;
4972b26f
     int frame_size;
8949367e
     struct video_capability video_cap;
     struct video_audio audio_saved;
406c5808
     struct video_window video_win;
0c1a9eda
     uint8_t *video_buf;
8949367e
     struct video_mbuf gb_buffers;
     struct video_mmap gb_buf;
     int gb_frame;
a861ffef
     int standard;
de6d9b64
 } VideoData;
 
1411f55b
 static const struct {
c7c64e9e
     int palette;
     int depth;
ac627b3d
     enum AVPixelFormat pix_fmt;
c7c64e9e
 } video_formats [] = {
ac627b3d
     {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = AV_PIX_FMT_YUV420P },
     {.palette = VIDEO_PALETTE_YUV422,  .depth = 16, .pix_fmt = AV_PIX_FMT_YUYV422 },
     {.palette = VIDEO_PALETTE_UYVY,    .depth = 16, .pix_fmt = AV_PIX_FMT_UYVY422 },
     {.palette = VIDEO_PALETTE_YUYV,    .depth = 16, .pix_fmt = AV_PIX_FMT_YUYV422 },
c7c64e9e
     /* NOTE: v4l uses BGR24, not RGB24 */
ac627b3d
     {.palette = VIDEO_PALETTE_RGB24,   .depth = 24, .pix_fmt = AV_PIX_FMT_BGR24   },
     {.palette = VIDEO_PALETTE_RGB565,  .depth = 16, .pix_fmt = AV_PIX_FMT_BGR565  },
     {.palette = VIDEO_PALETTE_GREY,    .depth = 8,  .pix_fmt = AV_PIX_FMT_GRAY8   },
c7c64e9e
 };
 
 
4972b26f
 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
de6d9b64
 {
c9a65ca8
     VideoData *s = s1->priv_data;
4972b26f
     AVStream *st;
f233d348
     int video_fd;
76a77ff2
     int desired_palette, desired_depth;
e3ee3283
     struct video_tuner tuner;
8949367e
     struct video_audio audio;
c787ba85
     struct video_picture pict;
7aae3168
     int j;
37d3e066
     int vformat_num = FF_ARRAY_ELEMS(video_formats);
4972b26f
 
aaea1490
     av_log(s1, AV_LOG_WARNING, "V4L input device is deprecated and will be removed in the next release.");
 
e78d651f
     if (ap->time_base.den <= 0) {
         av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
4972b26f
         return -1;
11b9c0f4
     }
9580ba26
     s->time_base = ap->time_base;
115329f1
 
406c5808
     s->video_win.width = ap->width;
     s->video_win.height = ap->height;
115329f1
 
3b3bbdd3
     st = avformat_new_stream(s1, NULL);
c9a65ca8
     if (!st)
8fa36ae0
         return AVERROR(ENOMEM);
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
4972b26f
 
cc58300e
     video_fd = open(s1->filename, O_RDWR);
de6d9b64
     if (video_fd < 0) {
9f74582c
         av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
4972b26f
         goto fail;
de6d9b64
     }
115329f1
 
c40432d3
     if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
9f74582c
         av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
de6d9b64
         goto fail;
     }
 
8949367e
     if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
bb270c08
         av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
de6d9b64
         goto fail;
     }
4606ac8d
 
7274a480
     /* 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;
         }
     }
 
e16f217c
     if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
5d91e928
         return -1;
 
4606ac8d
     desired_palette = -1;
76a77ff2
     desired_depth = -1;
c7c64e9e
     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;
         }
115329f1
     }
e3ee3283
 
     /* set tv standard */
a861ffef
     if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
         tuner.mode = s->standard;
bb270c08
         ioctl(video_fd, VIDIOCSTUNER, &tuner);
e3ee3283
     }
115329f1
 
de6d9b64
     /* unmute audio */
8949367e
     audio.audio = 0;
de6d9b64
     ioctl(video_fd, VIDIOCGAUDIO, &audio);
8949367e
     memcpy(&s->audio_saved, &audio, sizeof(audio));
de6d9b64
     audio.flags &= ~VIDEO_AUDIO_MUTE;
     ioctl(video_fd, VIDIOCSAUDIO, &audio);
 
c787ba85
     ioctl(video_fd, VIDIOCGPICT, &pict);
aebb56e1
     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);
c787ba85
     /* try to choose a suitable video format */
     pict.palette = desired_palette;
76a77ff2
     pict.depth= desired_depth;
b9f382c8
     if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
c7c64e9e
         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;
c787ba85
         }
c7c64e9e
         if (j >= vformat_num)
             goto fail1;
c787ba85
     }
 
c40432d3
     if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
de6d9b64
         /* try to use read based access */
         int val;
 
406c5808
         s->video_win.x = 0;
         s->video_win.y = 0;
         s->video_win.chromakey = -1;
         s->video_win.flags = 0;
de6d9b64
 
faf67322
         if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
             av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
             goto fail;
         }
de6d9b64
 
         s->frame_format = pict.palette;
 
         val = 1;
fd867ffd
         if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
             av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
             goto fail;
         }
de6d9b64
 
9580ba26
         s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
de6d9b64
         s->use_mmap = 0;
     } else {
c40432d3
         s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
8949367e
         if ((unsigned char*)-1 == s->video_buf) {
c40432d3
             s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
d037d797
             if ((unsigned char*)-1 == s->video_buf) {
9f74582c
                 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
d037d797
                 goto fail;
             }
de6d9b64
         }
8949367e
         s->gb_frame = 0;
9580ba26
         s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
115329f1
 
de6d9b64
         /* start to grab the first frame */
8949367e
         s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
406c5808
         s->gb_buf.height = s->video_win.height;
         s->gb_buf.width = s->video_win.width;
c787ba85
         s->gb_buf.format = pict.palette;
 
b9f382c8
         if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
de6d9b64
             if (errno != EAGAIN) {
             fail1:
df77a6da
                 av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
de6d9b64
             } else {
c40432d3
                 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
de6d9b64
             }
             goto fail;
         }
bb270c08
         for (j = 1; j < s->gb_buffers.frames; j++) {
           s->gb_buf.frame = j;
           ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
         }
8949367e
         s->frame_format = s->gb_buf.format;
de6d9b64
         s->use_mmap = 1;
     }
 
c7c64e9e
     for (j = 0; j < vformat_num; j++) {
         if (s->frame_format == video_formats[j].palette) {
f233d348
             s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
c7c64e9e
             st->codec->pix_fmt = video_formats[j].pix_fmt;
             break;
         }
de6d9b64
     }
c7c64e9e
 
     if (j >= vformat_num)
         goto fail;
 
de6d9b64
     s->fd = video_fd;
115329f1
 
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
7a72695c
     st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
406c5808
     st->codec->width = s->video_win.width;
     st->codec->height = s->video_win.height;
9580ba26
     st->codec->time_base = s->time_base;
f233d348
     st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
45dd5c69
 
de6d9b64
     return 0;
  fail:
4972b26f
     if (video_fd >= 0)
         close(video_fd);
6f3e0b21
     return AVERROR(EIO);
de6d9b64
 }
 
0c1a9eda
 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
de6d9b64
 {
0c1a9eda
     uint8_t *ptr;
de6d9b64
 
7aae3168
     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);
 
4606ac8d
     /* Setup to capture the next frame */
7aae3168
     s->gb_buf.frame = s->gb_frame;
8949367e
     if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
28c66901
         if (errno == EAGAIN)
bc874dae
             av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
28c66901
         else
9f74582c
             av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
6f3e0b21
         return AVERROR(EIO);
de6d9b64
     }
4606ac8d
 
     /* This is now the grabbing frame */
7aae3168
     s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
4606ac8d
 
4972b26f
     return s->frame_size;
de6d9b64
 }
 
4972b26f
 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64
 {
4972b26f
     VideoData *s = s1->priv_data;
0c1a9eda
     int64_t curtime, delay;
4972b26f
     struct timespec ts;
4606ac8d
 
     /* Calculate the time of the next frame */
8da9266c
     s->time_frame += INT64_C(1000000);
de6d9b64
 
     /* wait based on the frame rate */
45dd5c69
     for(;;) {
a11bf0bd
         curtime = av_gettime();
9580ba26
         delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
4606ac8d
         if (delay <= 0) {
9580ba26
             if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
4606ac8d
                 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
8da9266c
                 s->time_frame += INT64_C(1000000);
4606ac8d
             }
4972b26f
             break;
115329f1
         }
4972b26f
         ts.tv_sec = delay / 1000000;
         ts.tv_nsec = (delay % 1000000) * 1000;
         nanosleep(&ts, NULL);
     }
 
     if (av_new_packet(pkt, s->frame_size) < 0)
6f3e0b21
         return AVERROR(EIO);
de6d9b64
 
0a7b514f
     pkt->pts = curtime;
45dd5c69
 
de6d9b64
     /* read one frame */
3ab64e46
     if (s->use_mmap) {
4972b26f
         return v4l_mm_read_picture(s, pkt->data);
de6d9b64
     } else {
4972b26f
         if (read(s->fd, pkt->data, pkt->size) != pkt->size)
6f3e0b21
             return AVERROR(EIO);
4972b26f
         return s->frame_size;
de6d9b64
     }
 }
 
4972b26f
 static int grab_read_close(AVFormatContext *s1)
de6d9b64
 {
4972b26f
     VideoData *s = s1->priv_data;
e61efa24
 
     if (s->use_mmap)
8949367e
         munmap(s->video_buf, s->gb_buffers.size);
e61efa24
 
8949367e
     /* 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);
5a56c87c
 
de6d9b64
     close(s->fd);
     return 0;
 }
 
a861ffef
 static const AVOption options[] = {
d46c1c72
     { "standard", "", offsetof(VideoData, standard), AV_OPT_TYPE_INT, {.i64 = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
d5f65e9d
     { "PAL",   "", 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_MODE_PAL},   0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
     { "SECAM", "", 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
     { "NTSC",  "", 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_MODE_NTSC},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
a861ffef
     { NULL },
 };
 
 static const AVClass v4l_class = {
     .class_name = "V4L indev",
     .item_name  = av_default_item_name,
     .option     = options,
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
66355be3
 AVInputFormat ff_v4l_demuxer = {
6921272b
     .name           = "video4linux,v4l",
30b4ee79
     .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
     .priv_data_size = sizeof(VideoData),
     .read_header    = grab_read_header,
     .read_packet    = grab_read_packet,
     .read_close     = grab_read_close,
     .flags          = AVFMT_NOFILE,
     .priv_class     = &v4l_class,
de6d9b64
 };