• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavdevice/v4l2.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000,2001 Fabrice Bellard
00003  * Copyright (c) 2006 Luca Abeni
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00033 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
00034 #include "config.h"
00035 #include <unistd.h>
00036 #include <fcntl.h>
00037 #include <sys/ioctl.h>
00038 #include <sys/mman.h>
00039 #include <sys/time.h>
00040 #if HAVE_SYS_VIDEOIO_H
00041 #include <sys/videoio.h>
00042 #else
00043 #include <asm/types.h>
00044 #include <linux/videodev2.h>
00045 #endif
00046 #include <time.h>
00047 #include <strings.h>
00048 #include "libavutil/imgutils.h"
00049 #include "libavutil/log.h"
00050 #include "libavutil/opt.h"
00051 #include "avdevice.h"
00052 #include "libavutil/parseutils.h"
00053 #include "libavutil/pixdesc.h"
00054 
00055 static const int desired_video_buffers = 256;
00056 
00057 enum io_method {
00058     io_read,
00059     io_mmap,
00060     io_userptr
00061 };
00062 
00063 struct video_data {
00064     AVClass *class;
00065     int fd;
00066     int frame_format; /* V4L2_PIX_FMT_* */
00067     enum io_method io_method;
00068     int width, height;
00069     int frame_size;
00070     int top_field_first;
00071 
00072     int buffers;
00073     void **buf_start;
00074     unsigned int *buf_len;
00075     char *standard;
00076     int channel;
00077     char *video_size; 
00078     char *pixel_format; 
00079     char *framerate;    
00080 };
00081 
00082 struct buff_data {
00083     int index;
00084     int fd;
00085 };
00086 
00087 struct fmt_map {
00088     enum PixelFormat ff_fmt;
00089     enum CodecID codec_id;
00090     uint32_t v4l2_fmt;
00091 };
00092 
00093 static struct fmt_map fmt_conversion_table[] = {
00094     //ff_fmt           codec_id           v4l2_fmt
00095     { PIX_FMT_YUV420P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420  },
00096     { PIX_FMT_YUV422P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
00097     { PIX_FMT_YUYV422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV    },
00098     { PIX_FMT_UYVY422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY    },
00099     { PIX_FMT_YUV411P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
00100     { PIX_FMT_YUV410P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410  },
00101     { PIX_FMT_RGB555,  CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555  },
00102     { PIX_FMT_RGB565,  CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565  },
00103     { PIX_FMT_BGR24,   CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24   },
00104     { PIX_FMT_RGB24,   CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24   },
00105     { PIX_FMT_BGRA,    CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32   },
00106     { PIX_FMT_GRAY8,   CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY    },
00107     { PIX_FMT_NV12,    CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12    },
00108     { PIX_FMT_NONE,    CODEC_ID_MJPEG,    V4L2_PIX_FMT_MJPEG   },
00109     { PIX_FMT_NONE,    CODEC_ID_MJPEG,    V4L2_PIX_FMT_JPEG    },
00110 };
00111 
00112 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
00113 {
00114     struct v4l2_capability cap;
00115     int fd;
00116     int res, err;
00117     int flags = O_RDWR;
00118 
00119     if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
00120         flags |= O_NONBLOCK;
00121     }
00122     fd = open(ctx->filename, flags, 0);
00123     if (fd < 0) {
00124         av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
00125                  ctx->filename, strerror(errno));
00126         return AVERROR(errno);
00127     }
00128 
00129     res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
00130     // ENOIOCTLCMD definition only availble on __KERNEL__
00131     if (res < 0 && ((err = errno) == 515)) {
00132         av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
00133         close(fd);
00134 
00135         return AVERROR(515);
00136     }
00137     if (res < 0) {
00138         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
00139                  strerror(errno));
00140         close(fd);
00141         return AVERROR(err);
00142     }
00143     if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
00144         av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
00145         close(fd);
00146         return AVERROR(ENODEV);
00147     }
00148     *capabilities = cap.capabilities;
00149 
00150     return fd;
00151 }
00152 
00153 static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
00154 {
00155     struct video_data *s = ctx->priv_data;
00156     int fd = s->fd;
00157     struct v4l2_format fmt = {0};
00158     int res;
00159 
00160     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00161     fmt.fmt.pix.width = *width;
00162     fmt.fmt.pix.height = *height;
00163     fmt.fmt.pix.pixelformat = pix_fmt;
00164     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
00165     res = ioctl(fd, VIDIOC_S_FMT, &fmt);
00166     if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
00167         av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
00168         *width = fmt.fmt.pix.width;
00169         *height = fmt.fmt.pix.height;
00170     }
00171 
00172     if (pix_fmt != fmt.fmt.pix.pixelformat) {
00173         av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
00174         res = -1;
00175     }
00176 
00177     return res;
00178 }
00179 
00180 static int first_field(int fd)
00181 {
00182     int res;
00183     v4l2_std_id std;
00184 
00185     res = ioctl(fd, VIDIOC_G_STD, &std);
00186     if (res < 0) {
00187         return 0;
00188     }
00189     if (std & V4L2_STD_NTSC) {
00190         return 0;
00191     }
00192 
00193     return 1;
00194 }
00195 
00196 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
00197 {
00198     int i;
00199 
00200     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00201         if ((codec_id == CODEC_ID_NONE ||
00202              fmt_conversion_table[i].codec_id == codec_id) &&
00203             (pix_fmt == PIX_FMT_NONE ||
00204              fmt_conversion_table[i].ff_fmt == pix_fmt)) {
00205             return fmt_conversion_table[i].v4l2_fmt;
00206         }
00207     }
00208 
00209     return 0;
00210 }
00211 
00212 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
00213 {
00214     int i;
00215 
00216     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00217         if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
00218             fmt_conversion_table[i].codec_id == codec_id) {
00219             return fmt_conversion_table[i].ff_fmt;
00220         }
00221     }
00222 
00223     return PIX_FMT_NONE;
00224 }
00225 
00226 static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
00227 {
00228     int i;
00229 
00230     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00231         if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
00232             return fmt_conversion_table[i].codec_id;
00233         }
00234     }
00235 
00236     return CODEC_ID_NONE;
00237 }
00238 
00239 static int mmap_init(AVFormatContext *ctx)
00240 {
00241     struct video_data *s = ctx->priv_data;
00242     struct v4l2_requestbuffers req = {0};
00243     int i, res;
00244 
00245     req.count = desired_video_buffers;
00246     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00247     req.memory = V4L2_MEMORY_MMAP;
00248     res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
00249     if (res < 0) {
00250         if (errno == EINVAL) {
00251             av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
00252         } else {
00253             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
00254         }
00255         return AVERROR(errno);
00256     }
00257 
00258     if (req.count < 2) {
00259         av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
00260         return AVERROR(ENOMEM);
00261     }
00262     s->buffers = req.count;
00263     s->buf_start = av_malloc(sizeof(void *) * s->buffers);
00264     if (s->buf_start == NULL) {
00265         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
00266         return AVERROR(ENOMEM);
00267     }
00268     s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
00269     if (s->buf_len == NULL) {
00270         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
00271         av_free(s->buf_start);
00272         return AVERROR(ENOMEM);
00273     }
00274 
00275     for (i = 0; i < req.count; i++) {
00276         struct v4l2_buffer buf = {0};
00277 
00278         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00279         buf.memory = V4L2_MEMORY_MMAP;
00280         buf.index = i;
00281         res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
00282         if (res < 0) {
00283             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
00284             return AVERROR(errno);
00285         }
00286 
00287         s->buf_len[i] = buf.length;
00288         if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
00289             av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
00290 
00291             return -1;
00292         }
00293         s->buf_start[i] = mmap (NULL, buf.length,
00294                         PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
00295         if (s->buf_start[i] == MAP_FAILED) {
00296             av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00297             return AVERROR(errno);
00298         }
00299     }
00300 
00301     return 0;
00302 }
00303 
00304 static int read_init(AVFormatContext *ctx)
00305 {
00306     return -1;
00307 }
00308 
00309 static void mmap_release_buffer(AVPacket *pkt)
00310 {
00311     struct v4l2_buffer buf = {0};
00312     int res, fd;
00313     struct buff_data *buf_descriptor = pkt->priv;
00314 
00315     if (pkt->data == NULL) {
00316          return;
00317     }
00318 
00319     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00320     buf.memory = V4L2_MEMORY_MMAP;
00321     buf.index = buf_descriptor->index;
00322     fd = buf_descriptor->fd;
00323     av_free(buf_descriptor);
00324 
00325     res = ioctl(fd, VIDIOC_QBUF, &buf);
00326     if (res < 0) {
00327         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
00328     }
00329     pkt->data = NULL;
00330     pkt->size = 0;
00331 }
00332 
00333 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
00334 {
00335     struct video_data *s = ctx->priv_data;
00336     struct v4l2_buffer buf = {0};
00337     struct buff_data *buf_descriptor;
00338     int res;
00339 
00340     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00341     buf.memory = V4L2_MEMORY_MMAP;
00342 
00343     /* FIXME: Some special treatment might be needed in case of loss of signal... */
00344     while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
00345     if (res < 0) {
00346         if (errno == EAGAIN) {
00347             pkt->size = 0;
00348             return AVERROR(EAGAIN);
00349         }
00350         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
00351 
00352         return AVERROR(errno);
00353     }
00354     assert(buf.index < s->buffers);
00355     if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
00356         av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
00357         return AVERROR_INVALIDDATA;
00358     }
00359 
00360     /* Image is at s->buff_start[buf.index] */
00361     pkt->data= s->buf_start[buf.index];
00362     pkt->size = buf.bytesused;
00363     pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
00364     pkt->destruct = mmap_release_buffer;
00365     buf_descriptor = av_malloc(sizeof(struct buff_data));
00366     if (buf_descriptor == NULL) {
00367         /* Something went wrong... Since av_malloc() failed, we cannot even
00368          * allocate a buffer for memcopying into it
00369          */
00370         av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
00371         res = ioctl(s->fd, VIDIOC_QBUF, &buf);
00372 
00373         return AVERROR(ENOMEM);
00374     }
00375     buf_descriptor->fd = s->fd;
00376     buf_descriptor->index = buf.index;
00377     pkt->priv = buf_descriptor;
00378 
00379     return s->buf_len[buf.index];
00380 }
00381 
00382 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
00383 {
00384     return -1;
00385 }
00386 
00387 static int mmap_start(AVFormatContext *ctx)
00388 {
00389     struct video_data *s = ctx->priv_data;
00390     enum v4l2_buf_type type;
00391     int i, res;
00392 
00393     for (i = 0; i < s->buffers; i++) {
00394         struct v4l2_buffer buf = {0};
00395 
00396         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00397         buf.memory = V4L2_MEMORY_MMAP;
00398         buf.index  = i;
00399 
00400         res = ioctl(s->fd, VIDIOC_QBUF, &buf);
00401         if (res < 0) {
00402             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
00403             return AVERROR(errno);
00404         }
00405     }
00406 
00407     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00408     res = ioctl(s->fd, VIDIOC_STREAMON, &type);
00409     if (res < 0) {
00410         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
00411         return AVERROR(errno);
00412     }
00413 
00414     return 0;
00415 }
00416 
00417 static void mmap_close(struct video_data *s)
00418 {
00419     enum v4l2_buf_type type;
00420     int i;
00421 
00422     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00423     /* We do not check for the result, because we could
00424      * not do anything about it anyway...
00425      */
00426     ioctl(s->fd, VIDIOC_STREAMOFF, &type);
00427     for (i = 0; i < s->buffers; i++) {
00428         munmap(s->buf_start[i], s->buf_len[i]);
00429     }
00430     av_free(s->buf_start);
00431     av_free(s->buf_len);
00432 }
00433 
00434 static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
00435 {
00436     struct video_data *s = s1->priv_data;
00437     struct v4l2_input input = {0};
00438     struct v4l2_standard standard = {0};
00439     struct v4l2_streamparm streamparm = {0};
00440     struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
00441     int i, ret;
00442     AVRational fps={0};
00443 
00444     streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00445 
00446     if (s->framerate && (ret = av_parse_video_rate(&fps, s->framerate)) < 0) {
00447         av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n");
00448         return ret;
00449     }
00450 #if FF_API_FORMAT_PARAMETERS
00451     if (ap->channel > 0)
00452         s->channel = ap->channel;
00453     if (ap->time_base.num)
00454         fps = (AVRational){ap->time_base.den, ap->time_base.num};
00455 #endif
00456 
00457     /* set tv video input */
00458     input.index = s->channel;
00459     if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
00460         av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
00461         return AVERROR(EIO);
00462     }
00463 
00464     av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
00465             s->channel, input.name);
00466     if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
00467         av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
00468                 s->channel);
00469         return AVERROR(EIO);
00470     }
00471 
00472 #if FF_API_FORMAT_PARAMETERS
00473     if (ap->standard) {
00474         av_freep(&s->standard);
00475         s->standard = av_strdup(ap->standard);
00476     }
00477 #endif
00478 
00479     if (s->standard) {
00480         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
00481                s->standard);
00482         /* set tv standard */
00483         for (i = 0;; i++) {
00484             standard.index = i;
00485             ret = ioctl(s->fd, VIDIOC_ENUMSTD, &standard);
00486             if (ret < 0 || !strcasecmp(standard.name, s->standard))
00487                 break;
00488         }
00489         if (ret < 0) {
00490             av_log(s1, AV_LOG_ERROR, "Unknown standard '%s'\n", s->standard);
00491             return ret;
00492         }
00493 
00494         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
00495                s->standard, (uint64_t)standard.id);
00496         if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
00497             av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
00498                    s->standard);
00499             return AVERROR(EIO);
00500         }
00501     }
00502 
00503     if (fps.num && fps.den) {
00504         av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
00505                fps.den, fps.num);
00506         tpf->numerator   = fps.den;
00507         tpf->denominator = fps.num;
00508         if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
00509             av_log(s1, AV_LOG_ERROR,
00510                    "ioctl set time per frame(%d/%d) failed\n",
00511                    fps.den, fps.num);
00512             return AVERROR(EIO);
00513         }
00514 
00515         if (fps.num != tpf->denominator ||
00516             fps.den != tpf->numerator) {
00517             av_log(s1, AV_LOG_INFO,
00518                    "The driver changed the time per frame from %d/%d to %d/%d\n",
00519                    fps.den, fps.num,
00520                    tpf->numerator, tpf->denominator);
00521         }
00522     } else {
00523         /* if timebase value is not set, read the timebase value from the driver */
00524         if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
00525             av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno));
00526             return AVERROR(errno);
00527         }
00528     }
00529     s1->streams[0]->codec->time_base.den = tpf->denominator;
00530     s1->streams[0]->codec->time_base.num = tpf->numerator;
00531 
00532     return 0;
00533 }
00534 
00535 static uint32_t device_try_init(AVFormatContext *s1,
00536                                 enum PixelFormat pix_fmt,
00537                                 int *width,
00538                                 int *height,
00539                                 enum CodecID *codec_id)
00540 {
00541     uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
00542 
00543     if (desired_format == 0 ||
00544         device_init(s1, width, height, desired_format) < 0) {
00545         int i;
00546 
00547         desired_format = 0;
00548         for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00549             if (s1->video_codec_id == CODEC_ID_NONE ||
00550                 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
00551                 desired_format = fmt_conversion_table[i].v4l2_fmt;
00552                 if (device_init(s1, width, height, desired_format) >= 0) {
00553                     break;
00554                 }
00555                 desired_format = 0;
00556             }
00557         }
00558     }
00559     if (desired_format != 0) {
00560         *codec_id = fmt_v4l2codec(desired_format);
00561         assert(*codec_id != CODEC_ID_NONE);
00562     }
00563 
00564     return desired_format;
00565 }
00566 
00567 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00568 {
00569     struct video_data *s = s1->priv_data;
00570     AVStream *st;
00571     int res = 0;
00572     uint32_t desired_format, capabilities;
00573     enum CodecID codec_id;
00574     enum PixelFormat pix_fmt = PIX_FMT_NONE;
00575 
00576     st = av_new_stream(s1, 0);
00577     if (!st) {
00578         res = AVERROR(ENOMEM);
00579         goto out;
00580     }
00581     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
00582 
00583     if (s->video_size && (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
00584         av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
00585         goto out;
00586     }
00587     if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
00588         av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
00589         res = AVERROR(EINVAL);
00590         goto out;
00591     }
00592 #if FF_API_FORMAT_PARAMETERS
00593     if (ap->width > 0)
00594         s->width  = ap->width;
00595     if (ap->height > 0)
00596         s->height = ap->height;
00597     if (ap->pix_fmt)
00598         pix_fmt = ap->pix_fmt;
00599 #endif
00600 
00601     capabilities = 0;
00602     s->fd = device_open(s1, &capabilities);
00603     if (s->fd < 0) {
00604         res = AVERROR(EIO);
00605         goto out;
00606     }
00607     av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
00608 
00609     if (!s->width && !s->height) {
00610         struct v4l2_format fmt;
00611 
00612         av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
00613         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00614         if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
00615             av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
00616             res = AVERROR(errno);
00617             goto out;
00618         }
00619         s->width  = fmt.fmt.pix.width;
00620         s->height = fmt.fmt.pix.height;
00621         av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
00622     }
00623 
00624     desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id);
00625     if (desired_format == 0) {
00626         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
00627                "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
00628         close(s->fd);
00629 
00630         res = AVERROR(EIO);
00631         goto out;
00632     }
00633     if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
00634         goto out;
00635     s->frame_format = desired_format;
00636 
00637     if ((res = v4l2_set_parameters(s1, ap) < 0))
00638         goto out;
00639 
00640     st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
00641     s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
00642     if (capabilities & V4L2_CAP_STREAMING) {
00643         s->io_method = io_mmap;
00644         res = mmap_init(s1);
00645         if (res == 0) {
00646             res = mmap_start(s1);
00647         }
00648     } else {
00649         s->io_method = io_read;
00650         res = read_init(s1);
00651     }
00652     if (res < 0) {
00653         close(s->fd);
00654         res = AVERROR(EIO);
00655         goto out;
00656     }
00657     s->top_field_first = first_field(s->fd);
00658 
00659     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00660     st->codec->codec_id = codec_id;
00661     st->codec->width = s->width;
00662     st->codec->height = s->height;
00663     st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
00664 
00665 out:
00666     return res;
00667 }
00668 
00669 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
00670 {
00671     struct video_data *s = s1->priv_data;
00672     int res;
00673 
00674     if (s->io_method == io_mmap) {
00675         av_init_packet(pkt);
00676         res = mmap_read_frame(s1, pkt);
00677     } else if (s->io_method == io_read) {
00678         if (av_new_packet(pkt, s->frame_size) < 0)
00679             return AVERROR(EIO);
00680 
00681         res = read_frame(s1, pkt);
00682     } else {
00683         return AVERROR(EIO);
00684     }
00685     if (res < 0) {
00686         return res;
00687     }
00688 
00689     if (s1->streams[0]->codec->coded_frame) {
00690         s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
00691         s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
00692     }
00693 
00694     return pkt->size;
00695 }
00696 
00697 static int v4l2_read_close(AVFormatContext *s1)
00698 {
00699     struct video_data *s = s1->priv_data;
00700 
00701     if (s->io_method == io_mmap) {
00702         mmap_close(s);
00703     }
00704 
00705     close(s->fd);
00706     return 0;
00707 }
00708 
00709 #define OFFSET(x) offsetof(struct video_data, x)
00710 #define DEC AV_OPT_FLAG_DECODING_PARAM
00711 
00712 static const AVOption options[] = {
00713     { "standard", "", OFFSET(standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
00714     { "channel",  "", OFFSET(channel),  FF_OPT_TYPE_INT,    {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00715     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00716     { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00717     { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00718     { NULL },
00719 };
00720 
00721 static const AVClass v4l2_class = {
00722     .class_name = "V4L2 indev",
00723     .item_name  = av_default_item_name,
00724     .option     = options,
00725     .version    = LIBAVUTIL_VERSION_INT,
00726 };
00727 
00728 AVInputFormat ff_v4l2_demuxer = {
00729     "video4linux2",
00730     NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
00731     sizeof(struct video_data),
00732     NULL,
00733     v4l2_read_header,
00734     v4l2_read_packet,
00735     v4l2_read_close,
00736     .flags = AVFMT_NOFILE,
00737     .priv_class = &v4l2_class,
00738 };

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1