Skip to content
Snippets Groups Projects
v4l.c 10.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
    /*
     * Linux video grab interface
    
     * Copyright (c) 2000,2001 Fabrice Bellard.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * This file is part of FFmpeg.
     *
     * FFmpeg 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.1 of the License, or (at your option) any later version.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * FFmpeg is distributed in the hope that it will be useful,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * You should have received a copy of the GNU Lesser General Public
    
     * License along with FFmpeg; if not, write to the Free Software
    
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     */
    
    #undef __STRICT_ANSI__ //workaround due to broken kernel headers
    
    #include "libavformat/avformat.h"
    #include "libavcodec/dsputil.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <sys/mman.h>
    #include <sys/time.h>
    
    #include <time.h>
    
    #include <strings.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    typedef struct {
        int fd;
        int frame_format; /* see VIDEO_PALETTE_xxx */
        int use_mmap;
    
        int64_t time_frame;
    
        struct video_capability video_cap;
        struct video_audio audio_saved;
    
        struct video_window video_win;
    
        uint8_t *video_buf;
    
        struct video_mbuf gb_buffers;
        struct video_mmap gb_buf;
        int gb_frame;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    } VideoData;
    
    
    static const struct {
    
        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 },
    
        {.palette = VIDEO_PALETTE_UYVY,    .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
        {.palette = VIDEO_PALETTE_YUYV,    .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
    
        /* NOTE: v4l uses BGR24, not RGB24 */
        {.palette = VIDEO_PALETTE_RGB24,   .depth = 24, .pix_fmt = PIX_FMT_BGR24   },
    
        {.palette = VIDEO_PALETTE_RGB565,  .depth = 16, .pix_fmt = PIX_FMT_BGR565  },
    
        {.palette = VIDEO_PALETTE_GREY,    .depth = 8,  .pix_fmt = PIX_FMT_GRAY8   },
    };
    
    
    
    static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        VideoData *s = s1->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int video_fd, frame_size;
    
    Luca Abeni's avatar
    Luca Abeni committed
        int desired_palette, desired_depth;
    
        struct video_tuner tuner;
    
        int vformat_num = FF_ARRAY_ELEMS(video_formats);
    
        if (ap->time_base.den <= 0) {
            av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
    
        if((unsigned)ap->width > 32767 || (unsigned)ap->height > 32767) {
    
            av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
    
        s->video_win.width = ap->width;
        s->video_win.height = ap->height;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        st = av_new_stream(s1, 0);
        if (!st)
    
    Luca Abeni's avatar
    Luca Abeni committed
        av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
    
        video_fd = open(s1->filename, O_RDWR);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (video_fd < 0) {
    
            av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* 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;
            }
        }
    
    
        if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
    
            av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            goto fail;
        }
    
    
        if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
    
            av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            goto fail;
        }
    
    
        desired_palette = -1;
    
    Luca Abeni's avatar
    Luca Abeni committed
        desired_depth = -1;
    
        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;
            }
    
    
        /* 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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* unmute audio */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        ioctl(video_fd, VIDIOCGAUDIO, &audio);
    
        memcpy(&s->audio_saved, &audio, sizeof(audio));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        audio.flags &= ~VIDEO_AUDIO_MUTE;
        ioctl(video_fd, VIDIOCSAUDIO, &audio);
    
    
        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 */
        pict.palette = desired_palette;
    
    Luca Abeni's avatar
    Luca Abeni committed
        pict.depth= desired_depth;
    
        if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
    
            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;
    
        if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* try to use read based access */
            int val;
    
    
            s->video_win.x = 0;
            s->video_win.y = 0;
            s->video_win.chromakey = -1;
            s->video_win.flags = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            ioctl(video_fd, VIDIOCSWIN, s->video_win);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
            s->frame_format = pict.palette;
    
            val = 1;
    
            if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
                av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
                goto fail;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            s->use_mmap = 0;
        } else {
    
            s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
    
            if ((unsigned char*)-1 == s->video_buf) {
    
                s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
    
                if ((unsigned char*)-1 == s->video_buf) {
    
                    av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* start to grab the first frame */
    
            s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
    
            s->gb_buf.height = s->video_win.height;
            s->gb_buf.width = s->video_win.width;
    
            if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (errno != EAGAIN) {
                fail1:
    
                    av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                } else {
    
                    av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
                goto fail;
            }
    
            for (j = 1; j < s->gb_buffers.frames; j++) {
              s->gb_buf.frame = j;
              ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            s->use_mmap = 1;
        }
    
    
        for (j = 0; j < vformat_num; j++) {
            if (s->frame_format == video_formats[j].palette) {
    
                frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
    
                st->codec->pix_fmt = video_formats[j].pix_fmt;
                break;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        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 = s->video_win.width;
        st->codec->height = s->video_win.height;
    
        st->codec->time_base = s->time_base;
    
        st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
     fail:
    
        if (video_fd >= 0)
            close(video_fd);
    
    static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        uint8_t *ptr;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        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;
    
        if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
    
            if (errno == EAGAIN)
    
                av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
    
                av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        /* This is now the grabbing frame */
    
        s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
    
    static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        VideoData *s = s1->priv_data;
    
        int64_t curtime, delay;
    
    
        /* Calculate the time of the next frame */
    
        s->time_frame += INT64_C(1000000);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* wait based on the frame rate */
    
            curtime = av_gettime();
    
            delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
    
            if (delay <= 0) {
    
                if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
    
                    /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
    
                    s->time_frame += INT64_C(1000000);
    
            ts.tv_sec = delay / 1000000;
            ts.tv_nsec = (delay % 1000000) * 1000;
            nanosleep(&ts, NULL);
        }
    
        if (av_new_packet(pkt, s->frame_size) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    Luca Abeni's avatar
    Luca Abeni committed
        pkt->pts = curtime;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* read one frame */
    
            return v4l_mm_read_picture(s, pkt->data);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            if (read(s->fd, pkt->data, pkt->size) != pkt->size)
    
    static int grab_read_close(AVFormatContext *s1)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        VideoData *s = s1->priv_data;
    
            munmap(s->video_buf, s->gb_buffers.size);
    
        /* 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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        close(s->fd);
        return 0;
    }
    
    
    AVInputFormat v4l_demuxer = {
    
        NULL_IF_CONFIG_SMALL("video grab"),
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        sizeof(VideoData),
    
        NULL,
        grab_read_header,
        grab_read_packet,
        grab_read_close,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };