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