00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
00031 #include "config.h"
00032 #include "libavformat/avformat.h"
00033 #include <unistd.h>
00034 #include <fcntl.h>
00035 #include <sys/ioctl.h>
00036 #include <sys/mman.h>
00037 #include <sys/time.h>
00038 #if HAVE_SYS_VIDEOIO_H
00039 #include <sys/videoio.h>
00040 #else
00041 #include <asm/types.h>
00042 #include <linux/videodev2.h>
00043 #endif
00044 #include <time.h>
00045 #include <strings.h>
00046
00047 static const int desired_video_buffers = 256;
00048
00049 enum io_method {
00050 io_read,
00051 io_mmap,
00052 io_userptr
00053 };
00054
00055 struct video_data {
00056 int fd;
00057 int frame_format;
00058 enum io_method io_method;
00059 int width, height;
00060 int frame_size;
00061 int top_field_first;
00062
00063 int buffers;
00064 void **buf_start;
00065 unsigned int *buf_len;
00066 };
00067
00068 struct buff_data {
00069 int index;
00070 int fd;
00071 };
00072
00073 struct fmt_map {
00074 enum PixelFormat ff_fmt;
00075 int32_t v4l2_fmt;
00076 };
00077
00078 static struct fmt_map fmt_conversion_table[] = {
00079 {
00080 .ff_fmt = PIX_FMT_YUV420P,
00081 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
00082 },
00083 {
00084 .ff_fmt = PIX_FMT_YUV422P,
00085 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
00086 },
00087 {
00088 .ff_fmt = PIX_FMT_YUYV422,
00089 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
00090 },
00091 {
00092 .ff_fmt = PIX_FMT_UYVY422,
00093 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
00094 },
00095 {
00096 .ff_fmt = PIX_FMT_YUV411P,
00097 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
00098 },
00099 {
00100 .ff_fmt = PIX_FMT_YUV410P,
00101 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
00102 },
00103 {
00104 .ff_fmt = PIX_FMT_RGB555,
00105 .v4l2_fmt = V4L2_PIX_FMT_RGB555,
00106 },
00107 {
00108 .ff_fmt = PIX_FMT_RGB565,
00109 .v4l2_fmt = V4L2_PIX_FMT_RGB565,
00110 },
00111 {
00112 .ff_fmt = PIX_FMT_BGR24,
00113 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
00114 },
00115 {
00116 .ff_fmt = PIX_FMT_RGB24,
00117 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
00118 },
00119 {
00120 .ff_fmt = PIX_FMT_BGRA,
00121 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
00122 },
00123 {
00124 .ff_fmt = PIX_FMT_GRAY8,
00125 .v4l2_fmt = V4L2_PIX_FMT_GREY,
00126 },
00127 };
00128
00129 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
00130 {
00131 struct v4l2_capability cap;
00132 int fd;
00133 int res;
00134 int flags = O_RDWR;
00135
00136 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
00137 flags |= O_NONBLOCK;
00138 }
00139 fd = open(ctx->filename, flags, 0);
00140 if (fd < 0) {
00141 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
00142 ctx->filename, strerror(errno));
00143
00144 return -1;
00145 }
00146
00147 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
00148
00149 if (res < 0 && errno == 515)
00150 {
00151 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
00152 close(fd);
00153
00154 return -1;
00155 }
00156 if (res < 0) {
00157 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
00158 strerror(errno));
00159 close(fd);
00160
00161 return -1;
00162 }
00163 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
00164 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
00165 close(fd);
00166
00167 return -1;
00168 }
00169 *capabilities = cap.capabilities;
00170
00171 return fd;
00172 }
00173
00174 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
00175 {
00176 struct video_data *s = ctx->priv_data;
00177 int fd = s->fd;
00178 struct v4l2_format fmt;
00179 int res;
00180
00181 memset(&fmt, 0, sizeof(struct v4l2_format));
00182 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00183 fmt.fmt.pix.width = *width;
00184 fmt.fmt.pix.height = *height;
00185 fmt.fmt.pix.pixelformat = pix_fmt;
00186 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
00187 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
00188 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
00189 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);
00190 *width = fmt.fmt.pix.width;
00191 *height = fmt.fmt.pix.height;
00192 }
00193
00194 if (pix_fmt != fmt.fmt.pix.pixelformat) {
00195 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);
00196 res = -1;
00197 }
00198
00199 return res;
00200 }
00201
00202 static int first_field(int fd)
00203 {
00204 int res;
00205 v4l2_std_id std;
00206
00207 res = ioctl(fd, VIDIOC_G_STD, &std);
00208 if (res < 0) {
00209 return 0;
00210 }
00211 if (std & V4L2_STD_NTSC) {
00212 return 0;
00213 }
00214
00215 return 1;
00216 }
00217
00218 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
00219 {
00220 int i;
00221
00222 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00223 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
00224 return fmt_conversion_table[i].v4l2_fmt;
00225 }
00226 }
00227
00228 return 0;
00229 }
00230
00231 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
00232 {
00233 int i;
00234
00235 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00236 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
00237 return fmt_conversion_table[i].ff_fmt;
00238 }
00239 }
00240
00241 return PIX_FMT_NONE;
00242 }
00243
00244 static int mmap_init(AVFormatContext *ctx)
00245 {
00246 struct video_data *s = ctx->priv_data;
00247 struct v4l2_requestbuffers req;
00248 int i, res;
00249
00250 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
00251 req.count = desired_video_buffers;
00252 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00253 req.memory = V4L2_MEMORY_MMAP;
00254 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
00255 if (res < 0) {
00256 if (errno == EINVAL) {
00257 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
00258 } else {
00259 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
00260 }
00261
00262 return -1;
00263 }
00264
00265 if (req.count < 2) {
00266 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
00267
00268 return -1;
00269 }
00270 s->buffers = req.count;
00271 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
00272 if (s->buf_start == NULL) {
00273 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
00274
00275 return -1;
00276 }
00277 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
00278 if (s->buf_len == NULL) {
00279 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
00280 av_free(s->buf_start);
00281
00282 return -1;
00283 }
00284
00285 for (i = 0; i < req.count; i++) {
00286 struct v4l2_buffer buf;
00287
00288 memset(&buf, 0, sizeof(struct v4l2_buffer));
00289 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00290 buf.memory = V4L2_MEMORY_MMAP;
00291 buf.index = i;
00292 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
00293 if (res < 0) {
00294 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
00295
00296 return -1;
00297 }
00298
00299 s->buf_len[i] = buf.length;
00300 if (s->buf_len[i] < s->frame_size) {
00301 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
00302
00303 return -1;
00304 }
00305 s->buf_start[i] = mmap (NULL, buf.length,
00306 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
00307 if (s->buf_start[i] == MAP_FAILED) {
00308 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00309
00310 return -1;
00311 }
00312 }
00313
00314 return 0;
00315 }
00316
00317 static int read_init(AVFormatContext *ctx)
00318 {
00319 return -1;
00320 }
00321
00322 static void mmap_release_buffer(AVPacket *pkt)
00323 {
00324 struct v4l2_buffer buf;
00325 int res, fd;
00326 struct buff_data *buf_descriptor = pkt->priv;
00327
00328 if (pkt->data == NULL) {
00329 return;
00330 }
00331
00332 memset(&buf, 0, sizeof(struct v4l2_buffer));
00333 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00334 buf.memory = V4L2_MEMORY_MMAP;
00335 buf.index = buf_descriptor->index;
00336 fd = buf_descriptor->fd;
00337 av_free(buf_descriptor);
00338
00339 res = ioctl (fd, VIDIOC_QBUF, &buf);
00340 if (res < 0) {
00341 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
00342 }
00343 pkt->data = NULL;
00344 pkt->size = 0;
00345 }
00346
00347 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
00348 {
00349 struct video_data *s = ctx->priv_data;
00350 struct v4l2_buffer buf;
00351 struct buff_data *buf_descriptor;
00352 int res;
00353
00354 memset(&buf, 0, sizeof(struct v4l2_buffer));
00355 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00356 buf.memory = V4L2_MEMORY_MMAP;
00357
00358
00359 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
00360 if (res < 0) {
00361 if (errno == EAGAIN) {
00362 pkt->size = 0;
00363
00364 return AVERROR(EAGAIN);
00365 }
00366 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
00367
00368 return -1;
00369 }
00370 assert (buf.index < s->buffers);
00371 if (buf.bytesused != s->frame_size) {
00372 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
00373
00374 return -1;
00375 }
00376
00377
00378 pkt->data= s->buf_start[buf.index];
00379 pkt->size = buf.bytesused;
00380 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
00381 pkt->destruct = mmap_release_buffer;
00382 buf_descriptor = av_malloc(sizeof(struct buff_data));
00383 if (buf_descriptor == NULL) {
00384
00385
00386
00387 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
00388 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
00389
00390 return -1;
00391 }
00392 buf_descriptor->fd = s->fd;
00393 buf_descriptor->index = buf.index;
00394 pkt->priv = buf_descriptor;
00395
00396 return s->buf_len[buf.index];
00397 }
00398
00399 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
00400 {
00401 return -1;
00402 }
00403
00404 static int mmap_start(AVFormatContext *ctx)
00405 {
00406 struct video_data *s = ctx->priv_data;
00407 enum v4l2_buf_type type;
00408 int i, res;
00409
00410 for (i = 0; i < s->buffers; i++) {
00411 struct v4l2_buffer buf;
00412
00413 memset(&buf, 0, sizeof(struct v4l2_buffer));
00414 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00415 buf.memory = V4L2_MEMORY_MMAP;
00416 buf.index = i;
00417
00418 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
00419 if (res < 0) {
00420 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
00421
00422 return -1;
00423 }
00424 }
00425
00426 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00427 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
00428 if (res < 0) {
00429 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
00430
00431 return -1;
00432 }
00433
00434 return 0;
00435 }
00436
00437 static void mmap_close(struct video_data *s)
00438 {
00439 enum v4l2_buf_type type;
00440 int i;
00441
00442 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00443
00444
00445
00446 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
00447 for (i = 0; i < s->buffers; i++) {
00448 munmap(s->buf_start[i], s->buf_len[i]);
00449 }
00450 av_free(s->buf_start);
00451 av_free(s->buf_len);
00452 }
00453
00454 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
00455 {
00456 struct video_data *s = s1->priv_data;
00457 struct v4l2_input input;
00458 struct v4l2_standard standard;
00459 int i;
00460
00461 if(ap->channel>=0) {
00462
00463 memset (&input, 0, sizeof (input));
00464 input.index = ap->channel;
00465 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
00466 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
00467 return AVERROR(EIO);
00468 }
00469
00470 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
00471 ap->channel, input.name);
00472 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
00473 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
00474 ap->channel);
00475 return AVERROR(EIO);
00476 }
00477 }
00478
00479 if(ap->standard) {
00480 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
00481 ap->standard );
00482
00483 memset (&standard, 0, sizeof (standard));
00484 for(i=0;;i++) {
00485 standard.index = i;
00486 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
00487 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
00488 ap->standard);
00489 return AVERROR(EIO);
00490 }
00491
00492 if(!strcasecmp(standard.name, ap->standard)) {
00493 break;
00494 }
00495 }
00496
00497 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
00498 ap->standard, (uint64_t)standard.id);
00499 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
00500 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
00501 ap->standard);
00502 return AVERROR(EIO);
00503 }
00504 }
00505
00506 return 0;
00507 }
00508
00509 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00510 {
00511 struct video_data *s = s1->priv_data;
00512 AVStream *st;
00513 int width, height;
00514 int res;
00515 uint32_t desired_format, capabilities;
00516
00517 if (ap->width <= 0 || ap->height <= 0) {
00518 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
00519 return -1;
00520 }
00521
00522 width = ap->width;
00523 height = ap->height;
00524
00525 if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
00526 return -1;
00527
00528 st = av_new_stream(s1, 0);
00529 if (!st) {
00530 return AVERROR(ENOMEM);
00531 }
00532 av_set_pts_info(st, 64, 1, 1000000);
00533
00534 s->width = width;
00535 s->height = height;
00536
00537 capabilities = 0;
00538 s->fd = device_open(s1, &capabilities);
00539 if (s->fd < 0) {
00540 return AVERROR(EIO);
00541 }
00542 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
00543
00544 desired_format = fmt_ff2v4l(ap->pix_fmt);
00545 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
00546 int i, done;
00547
00548 done = 0; i = 0;
00549 while (!done) {
00550 desired_format = fmt_conversion_table[i].v4l2_fmt;
00551 if (device_init(s1, &width, &height, desired_format) < 0) {
00552 desired_format = 0;
00553 i++;
00554 } else {
00555 done = 1;
00556 }
00557 if (i == FF_ARRAY_ELEMS(fmt_conversion_table)) {
00558 done = 1;
00559 }
00560 }
00561 }
00562 if (desired_format == 0) {
00563 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
00564 close(s->fd);
00565
00566 return AVERROR(EIO);
00567 }
00568 s->frame_format = desired_format;
00569
00570 if( v4l2_set_parameters( s1, ap ) < 0 )
00571 return AVERROR(EIO);
00572
00573 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
00574 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00575 if (capabilities & V4L2_CAP_STREAMING) {
00576 s->io_method = io_mmap;
00577 res = mmap_init(s1);
00578 if (res == 0) {
00579 res = mmap_start(s1);
00580 }
00581 } else {
00582 s->io_method = io_read;
00583 res = read_init(s1);
00584 }
00585 if (res < 0) {
00586 close(s->fd);
00587
00588 return AVERROR(EIO);
00589 }
00590 s->top_field_first = first_field(s->fd);
00591
00592 st->codec->codec_type = CODEC_TYPE_VIDEO;
00593 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00594 st->codec->width = width;
00595 st->codec->height = height;
00596 st->codec->time_base.den = ap->time_base.den;
00597 st->codec->time_base.num = ap->time_base.num;
00598 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
00599
00600 return 0;
00601 }
00602
00603 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
00604 {
00605 struct video_data *s = s1->priv_data;
00606 int res;
00607
00608 if (s->io_method == io_mmap) {
00609 av_init_packet(pkt);
00610 res = mmap_read_frame(s1, pkt);
00611 } else if (s->io_method == io_read) {
00612 if (av_new_packet(pkt, s->frame_size) < 0)
00613 return AVERROR(EIO);
00614
00615 res = read_frame(s1, pkt);
00616 } else {
00617 return AVERROR(EIO);
00618 }
00619 if (res < 0) {
00620 return res;
00621 }
00622
00623 if (s1->streams[0]->codec->coded_frame) {
00624 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
00625 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
00626 }
00627
00628 return s->frame_size;
00629 }
00630
00631 static int v4l2_read_close(AVFormatContext *s1)
00632 {
00633 struct video_data *s = s1->priv_data;
00634
00635 if (s->io_method == io_mmap) {
00636 mmap_close(s);
00637 }
00638
00639 close(s->fd);
00640 return 0;
00641 }
00642
00643 AVInputFormat v4l2_demuxer = {
00644 "video4linux2",
00645 NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
00646 sizeof(struct video_data),
00647 NULL,
00648 v4l2_read_header,
00649 v4l2_read_packet,
00650 v4l2_read_close,
00651 .flags = AVFMT_NOFILE,
00652 };