00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00033 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
00034 #include "config.h"
00035 #include "libavformat/internal.h"
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038 #include <sys/ioctl.h>
00039 #include <sys/mman.h>
00040 #include <sys/time.h>
00041 #if HAVE_SYS_VIDEOIO_H
00042 #include <sys/videoio.h>
00043 #else
00044 #if HAVE_ASM_TYPES_H
00045 #include <asm/types.h>
00046 #endif
00047 #include <linux/videodev2.h>
00048 #endif
00049 #include "libavutil/avassert.h"
00050 #include "libavutil/imgutils.h"
00051 #include "libavutil/log.h"
00052 #include "libavutil/opt.h"
00053 #include "avdevice.h"
00054 #include "timefilter.h"
00055 #include "libavutil/parseutils.h"
00056 #include "libavutil/pixdesc.h"
00057 #include "libavutil/avstring.h"
00058
00059 #if CONFIG_LIBV4L2
00060 #include <libv4l2.h>
00061 #else
00062 #define v4l2_open open
00063 #define v4l2_close close
00064 #define v4l2_dup dup
00065 #define v4l2_ioctl ioctl
00066 #define v4l2_read read
00067 #define v4l2_mmap mmap
00068 #define v4l2_munmap munmap
00069 #endif
00070
00071 static const int desired_video_buffers = 256;
00072
00073 #define V4L_ALLFORMATS 3
00074 #define V4L_RAWFORMATS 1
00075 #define V4L_COMPFORMATS 2
00076
00080 #define V4L_TS_DEFAULT 0
00081
00085 #define V4L_TS_ABS 1
00086
00090 #define V4L_TS_MONO2ABS 2
00091
00097 #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
00098
00099 struct video_data {
00100 AVClass *class;
00101 int fd;
00102 int frame_format;
00103 int width, height;
00104 int frame_size;
00105 int interlaced;
00106 int top_field_first;
00107 int ts_mode;
00108 TimeFilter *timefilter;
00109 int64_t last_time_m;
00110
00111 int buffers;
00112 void **buf_start;
00113 unsigned int *buf_len;
00114 char *standard;
00115 int channel;
00116 char *pixel_format;
00117 int list_format;
00118 char *framerate;
00119 };
00120
00121 struct buff_data {
00122 int index;
00123 int fd;
00124 };
00125
00126 struct fmt_map {
00127 enum PixelFormat ff_fmt;
00128 enum AVCodecID codec_id;
00129 uint32_t v4l2_fmt;
00130 };
00131
00132 static struct fmt_map fmt_conversion_table[] = {
00133
00134 { PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
00135 { PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YVU420 },
00136 { PIX_FMT_YUV422P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
00137 { PIX_FMT_YUYV422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
00138 { PIX_FMT_UYVY422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
00139 { PIX_FMT_YUV411P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
00140 { PIX_FMT_YUV410P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
00141 { PIX_FMT_RGB555LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
00142 { PIX_FMT_RGB555BE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555X },
00143 { PIX_FMT_RGB565LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
00144 { PIX_FMT_RGB565BE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565X },
00145 { PIX_FMT_BGR24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
00146 { PIX_FMT_RGB24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
00147 { PIX_FMT_BGR0, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
00148 { PIX_FMT_0RGB, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB32 },
00149 { PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
00150 { PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
00151 { PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
00152 { PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
00153 #ifdef V4L2_PIX_FMT_CPIA1
00154 { PIX_FMT_NONE, AV_CODEC_ID_CPIA, V4L2_PIX_FMT_CPIA1 },
00155 #endif
00156 };
00157
00158 static int device_open(AVFormatContext *ctx)
00159 {
00160 struct v4l2_capability cap;
00161 int fd;
00162 int res, err;
00163 int flags = O_RDWR;
00164
00165 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
00166 flags |= O_NONBLOCK;
00167 }
00168
00169 fd = v4l2_open(ctx->filename, flags, 0);
00170 if (fd < 0) {
00171 err = errno;
00172
00173 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
00174 ctx->filename, strerror(err));
00175
00176 return AVERROR(err);
00177 }
00178
00179 res = v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap);
00180 if (res < 0) {
00181 err = errno;
00182 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
00183 strerror(err));
00184
00185 goto fail;
00186 }
00187
00188 av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
00189 fd, cap.capabilities);
00190
00191 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
00192 av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
00193 err = ENODEV;
00194
00195 goto fail;
00196 }
00197
00198 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
00199 av_log(ctx, AV_LOG_ERROR,
00200 "The device does not support the streaming I/O method.\n");
00201 err = ENOSYS;
00202
00203 goto fail;
00204 }
00205
00206 return fd;
00207
00208 fail:
00209 v4l2_close(fd);
00210 return AVERROR(err);
00211 }
00212
00213 static int device_init(AVFormatContext *ctx, int *width, int *height,
00214 uint32_t pix_fmt)
00215 {
00216 struct video_data *s = ctx->priv_data;
00217 int fd = s->fd;
00218 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
00219 struct v4l2_pix_format *pix = &fmt.fmt.pix;
00220
00221 int res;
00222
00223 pix->width = *width;
00224 pix->height = *height;
00225 pix->pixelformat = pix_fmt;
00226 pix->field = V4L2_FIELD_ANY;
00227
00228 res = v4l2_ioctl(fd, VIDIOC_S_FMT, &fmt);
00229
00230 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
00231 av_log(ctx, AV_LOG_INFO,
00232 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
00233 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
00234 *width = fmt.fmt.pix.width;
00235 *height = fmt.fmt.pix.height;
00236 }
00237
00238 if (pix_fmt != fmt.fmt.pix.pixelformat) {
00239 av_log(ctx, AV_LOG_DEBUG,
00240 "The V4L2 driver changed the pixel format "
00241 "from 0x%08X to 0x%08X\n",
00242 pix_fmt, fmt.fmt.pix.pixelformat);
00243 res = -1;
00244 }
00245
00246 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
00247 av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
00248 s->interlaced = 1;
00249 }
00250
00251 return res;
00252 }
00253
00254 static int first_field(int fd)
00255 {
00256 int res;
00257 v4l2_std_id std;
00258
00259 res = v4l2_ioctl(fd, VIDIOC_G_STD, &std);
00260 if (res < 0) {
00261 return 0;
00262 }
00263 if (std & V4L2_STD_NTSC) {
00264 return 0;
00265 }
00266
00267 return 1;
00268 }
00269
00270 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum AVCodecID codec_id)
00271 {
00272 int i;
00273
00274 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00275 if ((codec_id == AV_CODEC_ID_NONE ||
00276 fmt_conversion_table[i].codec_id == codec_id) &&
00277 (pix_fmt == PIX_FMT_NONE ||
00278 fmt_conversion_table[i].ff_fmt == pix_fmt)) {
00279 return fmt_conversion_table[i].v4l2_fmt;
00280 }
00281 }
00282
00283 return 0;
00284 }
00285
00286 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
00287 {
00288 int i;
00289
00290 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00291 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
00292 fmt_conversion_table[i].codec_id == codec_id) {
00293 return fmt_conversion_table[i].ff_fmt;
00294 }
00295 }
00296
00297 return PIX_FMT_NONE;
00298 }
00299
00300 static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
00301 {
00302 int i;
00303
00304 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00305 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
00306 return fmt_conversion_table[i].codec_id;
00307 }
00308 }
00309
00310 return AV_CODEC_ID_NONE;
00311 }
00312
00313 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
00314 static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
00315 {
00316 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
00317
00318 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
00319 switch (vfse.type) {
00320 case V4L2_FRMSIZE_TYPE_DISCRETE:
00321 av_log(ctx, AV_LOG_INFO, " %ux%u",
00322 vfse.discrete.width, vfse.discrete.height);
00323 break;
00324 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
00325 case V4L2_FRMSIZE_TYPE_STEPWISE:
00326 av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
00327 vfse.stepwise.min_width,
00328 vfse.stepwise.max_width,
00329 vfse.stepwise.step_width,
00330 vfse.stepwise.min_height,
00331 vfse.stepwise.max_height,
00332 vfse.stepwise.step_height);
00333 }
00334 vfse.index++;
00335 }
00336 }
00337 #endif
00338
00339 static void list_formats(AVFormatContext *ctx, int fd, int type)
00340 {
00341 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
00342
00343 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
00344 enum AVCodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
00345 enum PixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
00346
00347 vfd.index++;
00348
00349 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
00350 type & V4L_RAWFORMATS) {
00351 const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
00352 av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
00353 fmt_name ? fmt_name : "Unsupported",
00354 vfd.description);
00355 } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
00356 type & V4L_COMPFORMATS) {
00357 AVCodec *codec = avcodec_find_encoder(codec_id);
00358 av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
00359 codec ? codec->name : "Unsupported",
00360 vfd.description);
00361 } else {
00362 continue;
00363 }
00364
00365 #ifdef V4L2_FMT_FLAG_EMULATED
00366 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
00367 av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
00368 continue;
00369 }
00370 #endif
00371 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
00372 list_framesizes(ctx, fd, vfd.pixelformat);
00373 #endif
00374 av_log(ctx, AV_LOG_INFO, "\n");
00375 }
00376 }
00377
00378 static int mmap_init(AVFormatContext *ctx)
00379 {
00380 int i, res;
00381 struct video_data *s = ctx->priv_data;
00382 struct v4l2_requestbuffers req = {
00383 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
00384 .count = desired_video_buffers,
00385 .memory = V4L2_MEMORY_MMAP
00386 };
00387
00388 res = v4l2_ioctl(s->fd, VIDIOC_REQBUFS, &req);
00389 if (res < 0) {
00390 if (errno == EINVAL) {
00391 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
00392 } else {
00393 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
00394 }
00395 return AVERROR(errno);
00396 }
00397
00398 if (req.count < 2) {
00399 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
00400 return AVERROR(ENOMEM);
00401 }
00402 s->buffers = req.count;
00403 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
00404 if (s->buf_start == NULL) {
00405 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
00406 return AVERROR(ENOMEM);
00407 }
00408 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
00409 if (s->buf_len == NULL) {
00410 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
00411 av_free(s->buf_start);
00412 return AVERROR(ENOMEM);
00413 }
00414
00415 for (i = 0; i < req.count; i++) {
00416 struct v4l2_buffer buf = {
00417 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
00418 .index = i,
00419 .memory = V4L2_MEMORY_MMAP
00420 };
00421 res = v4l2_ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
00422 if (res < 0) {
00423 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
00424 return AVERROR(errno);
00425 }
00426
00427 s->buf_len[i] = buf.length;
00428 if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
00429 av_log(ctx, AV_LOG_ERROR,
00430 "Buffer len [%d] = %d != %d\n",
00431 i, s->buf_len[i], s->frame_size);
00432
00433 return -1;
00434 }
00435 s->buf_start[i] = v4l2_mmap(NULL, buf.length,
00436 PROT_READ | PROT_WRITE, MAP_SHARED,
00437 s->fd, buf.m.offset);
00438
00439 if (s->buf_start[i] == MAP_FAILED) {
00440 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00441 return AVERROR(errno);
00442 }
00443 }
00444
00445 return 0;
00446 }
00447
00448 static void mmap_release_buffer(AVPacket *pkt)
00449 {
00450 struct v4l2_buffer buf = { 0 };
00451 int res, fd;
00452 struct buff_data *buf_descriptor = pkt->priv;
00453
00454 if (pkt->data == NULL)
00455 return;
00456
00457 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00458 buf.memory = V4L2_MEMORY_MMAP;
00459 buf.index = buf_descriptor->index;
00460 fd = buf_descriptor->fd;
00461 av_free(buf_descriptor);
00462
00463 res = v4l2_ioctl(fd, VIDIOC_QBUF, &buf);
00464 if (res < 0)
00465 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
00466 strerror(errno));
00467
00468 pkt->data = NULL;
00469 pkt->size = 0;
00470 }
00471
00472 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
00473 static int64_t av_gettime_monotonic(void)
00474 {
00475 struct timespec tv;
00476
00477 clock_gettime(CLOCK_MONOTONIC, &tv);
00478 return (int64_t)tv.tv_sec * 1000000 + tv.tv_nsec / 1000;
00479 }
00480 #endif
00481
00482 static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
00483 {
00484 struct video_data *s = ctx->priv_data;
00485 int64_t now;
00486
00487 now = av_gettime();
00488 if (s->ts_mode == V4L_TS_ABS &&
00489 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE) {
00490 av_log(ctx, AV_LOG_INFO, "Detected absolute timestamps\n");
00491 s->ts_mode = V4L_TS_CONVERT_READY;
00492 return 0;
00493 }
00494 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
00495 now = av_gettime_monotonic();
00496 if (s->ts_mode == V4L_TS_MONO2ABS ||
00497 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE)) {
00498 int64_t period = av_rescale_q(1, ctx->streams[0]->codec->time_base,
00499 AV_TIME_BASE_Q);
00500 av_log(ctx, AV_LOG_INFO, "Detected monotonic timestamps, converting\n");
00501
00502 s->timefilter = ff_timefilter_new(1, period, 1.0E-6);
00503 s->ts_mode = V4L_TS_CONVERT_READY;
00504 return 0;
00505 }
00506 #endif
00507 av_log(ctx, AV_LOG_ERROR, "Unknown timestamps\n");
00508 return AVERROR(EIO);
00509 }
00510
00511 static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
00512 {
00513 struct video_data *s = ctx->priv_data;
00514
00515 if (s->ts_mode) {
00516 int r = init_convert_timestamp(ctx, *ts);
00517 if (r < 0)
00518 return r;
00519 }
00520 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
00521 if (s->timefilter) {
00522 int64_t nowa = av_gettime();
00523 int64_t nowm = av_gettime_monotonic();
00524 ff_timefilter_update(s->timefilter, nowa, nowm - s->last_time_m);
00525 s->last_time_m = nowm;
00526 *ts = ff_timefilter_eval(s->timefilter, *ts - nowm);
00527 }
00528 #endif
00529 return 0;
00530 }
00531
00532 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
00533 {
00534 struct video_data *s = ctx->priv_data;
00535 struct v4l2_buffer buf = {
00536 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
00537 .memory = V4L2_MEMORY_MMAP
00538 };
00539 struct buff_data *buf_descriptor;
00540 int res;
00541
00542
00543 while ((res = v4l2_ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
00544 if (res < 0) {
00545 if (errno == EAGAIN) {
00546 pkt->size = 0;
00547 return AVERROR(EAGAIN);
00548 }
00549 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
00550 strerror(errno));
00551
00552 return AVERROR(errno);
00553 }
00554 av_assert0(buf.index < s->buffers);
00555
00556
00557
00558
00559 if (ctx->video_codec_id == AV_CODEC_ID_CPIA)
00560 s->frame_size = buf.bytesused;
00561
00562 if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
00563 av_log(ctx, AV_LOG_ERROR,
00564 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
00565 buf.bytesused, s->frame_size);
00566
00567 return AVERROR_INVALIDDATA;
00568 }
00569
00570
00571 pkt->data= s->buf_start[buf.index];
00572 pkt->size = buf.bytesused;
00573 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
00574 res = convert_timestamp(ctx, &pkt->pts);
00575 if (res < 0)
00576 return res;
00577 pkt->destruct = mmap_release_buffer;
00578 buf_descriptor = av_malloc(sizeof(struct buff_data));
00579 if (buf_descriptor == NULL) {
00580
00581
00582
00583 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
00584 res = v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf);
00585
00586 return AVERROR(ENOMEM);
00587 }
00588 buf_descriptor->fd = s->fd;
00589 buf_descriptor->index = buf.index;
00590 pkt->priv = buf_descriptor;
00591
00592 return s->buf_len[buf.index];
00593 }
00594
00595 static int mmap_start(AVFormatContext *ctx)
00596 {
00597 struct video_data *s = ctx->priv_data;
00598 enum v4l2_buf_type type;
00599 int i, res;
00600
00601 for (i = 0; i < s->buffers; i++) {
00602 struct v4l2_buffer buf = {
00603 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
00604 .index = i,
00605 .memory = V4L2_MEMORY_MMAP
00606 };
00607
00608 res = v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf);
00609 if (res < 0) {
00610 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
00611 strerror(errno));
00612
00613 return AVERROR(errno);
00614 }
00615 }
00616
00617 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00618 res = v4l2_ioctl(s->fd, VIDIOC_STREAMON, &type);
00619 if (res < 0) {
00620 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
00621 strerror(errno));
00622
00623 return AVERROR(errno);
00624 }
00625
00626 return 0;
00627 }
00628
00629 static void mmap_close(struct video_data *s)
00630 {
00631 enum v4l2_buf_type type;
00632 int i;
00633
00634 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00635
00636
00637
00638 v4l2_ioctl(s->fd, VIDIOC_STREAMOFF, &type);
00639 for (i = 0; i < s->buffers; i++) {
00640 v4l2_munmap(s->buf_start[i], s->buf_len[i]);
00641 }
00642 av_free(s->buf_start);
00643 av_free(s->buf_len);
00644 }
00645
00646 static int v4l2_set_parameters(AVFormatContext *s1)
00647 {
00648 struct video_data *s = s1->priv_data;
00649 struct v4l2_input input = { 0 };
00650 struct v4l2_standard standard = { 0 };
00651 struct v4l2_streamparm streamparm = { 0 };
00652 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
00653 AVRational framerate_q = { 0 };
00654 int i, ret;
00655
00656 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00657
00658 if (s->framerate &&
00659 (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
00660 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
00661 s->framerate);
00662 return ret;
00663 }
00664
00665
00666 input.index = s->channel;
00667 if (v4l2_ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
00668 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
00669 return AVERROR(EIO);
00670 }
00671
00672 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
00673 s->channel, input.name);
00674 if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
00675 av_log(s1, AV_LOG_ERROR,
00676 "The V4L2 driver ioctl set input(%d) failed\n",
00677 s->channel);
00678 return AVERROR(EIO);
00679 }
00680
00681 if (s->standard) {
00682 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
00683 s->standard);
00684
00685 for(i=0;;i++) {
00686 standard.index = i;
00687 ret = v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard);
00688 if (ret < 0 || !av_strcasecmp(standard.name, s->standard))
00689 break;
00690 }
00691 if (ret < 0) {
00692 av_log(s1, AV_LOG_ERROR, "Unknown standard '%s'\n", s->standard);
00693 return ret;
00694 }
00695
00696 av_log(s1, AV_LOG_DEBUG,
00697 "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
00698 s->standard, (uint64_t)standard.id);
00699 if (v4l2_ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
00700 av_log(s1, AV_LOG_ERROR,
00701 "The V4L2 driver ioctl set standard(%s) failed\n",
00702 s->standard);
00703 return AVERROR(EIO);
00704 }
00705 }
00706
00707 if (framerate_q.num && framerate_q.den) {
00708 av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
00709 framerate_q.den, framerate_q.num);
00710 tpf->numerator = framerate_q.den;
00711 tpf->denominator = framerate_q.num;
00712
00713 if (v4l2_ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
00714 av_log(s1, AV_LOG_ERROR,
00715 "ioctl set time per frame(%d/%d) failed\n",
00716 framerate_q.den, framerate_q.num);
00717 return AVERROR(EIO);
00718 }
00719
00720 if (framerate_q.num != tpf->denominator ||
00721 framerate_q.den != tpf->numerator) {
00722 av_log(s1, AV_LOG_INFO,
00723 "The driver changed the time per frame from "
00724 "%d/%d to %d/%d\n",
00725 framerate_q.den, framerate_q.num,
00726 tpf->numerator, tpf->denominator);
00727 }
00728 } else {
00729 if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
00730 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
00731 strerror(errno));
00732 return AVERROR(errno);
00733 }
00734 }
00735 s1->streams[0]->codec->time_base.den = tpf->denominator;
00736 s1->streams[0]->codec->time_base.num = tpf->numerator;
00737
00738 return 0;
00739 }
00740
00741 static uint32_t device_try_init(AVFormatContext *s1,
00742 enum PixelFormat pix_fmt,
00743 int *width,
00744 int *height,
00745 enum AVCodecID *codec_id)
00746 {
00747 uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
00748
00749 if (desired_format == 0 ||
00750 device_init(s1, width, height, desired_format) < 0) {
00751 int i;
00752
00753 desired_format = 0;
00754 for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
00755 if (s1->video_codec_id == AV_CODEC_ID_NONE ||
00756 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
00757 desired_format = fmt_conversion_table[i].v4l2_fmt;
00758 if (device_init(s1, width, height, desired_format) >= 0) {
00759 break;
00760 }
00761 desired_format = 0;
00762 }
00763 }
00764 }
00765
00766 if (desired_format != 0) {
00767 *codec_id = fmt_v4l2codec(desired_format);
00768 av_assert0(*codec_id != AV_CODEC_ID_NONE);
00769 }
00770
00771 return desired_format;
00772 }
00773
00774 static int v4l2_read_header(AVFormatContext *s1)
00775 {
00776 struct video_data *s = s1->priv_data;
00777 AVStream *st;
00778 int res = 0;
00779 uint32_t desired_format;
00780 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
00781 enum PixelFormat pix_fmt = PIX_FMT_NONE;
00782
00783 st = avformat_new_stream(s1, NULL);
00784 if (!st) {
00785 res = AVERROR(ENOMEM);
00786 goto out;
00787 }
00788
00789 s->fd = device_open(s1);
00790 if (s->fd < 0) {
00791 res = s->fd;
00792 goto out;
00793 }
00794
00795 if (s->list_format) {
00796 list_formats(s1, s->fd, s->list_format);
00797 res = AVERROR_EXIT;
00798 goto out;
00799 }
00800
00801 avpriv_set_pts_info(st, 64, 1, 1000000);
00802
00803 if (s->pixel_format) {
00804 AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
00805
00806 if (codec)
00807 s1->video_codec_id = codec->id;
00808
00809 pix_fmt = av_get_pix_fmt(s->pixel_format);
00810
00811 if (pix_fmt == PIX_FMT_NONE && !codec) {
00812 av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
00813 s->pixel_format);
00814
00815 res = AVERROR(EINVAL);
00816 goto out;
00817 }
00818 }
00819
00820 if (!s->width && !s->height) {
00821 struct v4l2_format fmt;
00822
00823 av_log(s1, AV_LOG_VERBOSE,
00824 "Querying the device for the current frame size\n");
00825 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00826 if (v4l2_ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
00827 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
00828 strerror(errno));
00829 res = AVERROR(errno);
00830 goto out;
00831 }
00832
00833 s->width = fmt.fmt.pix.width;
00834 s->height = fmt.fmt.pix.height;
00835 av_log(s1, AV_LOG_VERBOSE,
00836 "Setting frame size to %dx%d\n", s->width, s->height);
00837 }
00838
00839 desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
00840 &codec_id);
00841
00842
00843
00844
00845
00846 if (codec_id != AV_CODEC_ID_NONE && s1->video_codec_id == AV_CODEC_ID_NONE)
00847 s1->video_codec_id = codec_id;
00848
00849 if (desired_format == 0) {
00850 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
00851 "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
00852 v4l2_close(s->fd);
00853
00854 res = AVERROR(EIO);
00855 goto out;
00856 }
00857 if ((res = av_image_check_size(s->width, s->height, 0, s1)) < 0)
00858 goto out;
00859
00860 s->frame_format = desired_format;
00861
00862 if ((res = v4l2_set_parameters(s1)) < 0)
00863 goto out;
00864
00865 st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
00866 s->frame_size =
00867 avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
00868
00869 if ((res = mmap_init(s1)) ||
00870 (res = mmap_start(s1)) < 0) {
00871 v4l2_close(s->fd);
00872 goto out;
00873 }
00874
00875 s->top_field_first = first_field(s->fd);
00876
00877 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00878 st->codec->codec_id = codec_id;
00879 if (codec_id == AV_CODEC_ID_RAWVIDEO)
00880 st->codec->codec_tag =
00881 avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
00882 if (desired_format == V4L2_PIX_FMT_YVU420)
00883 st->codec->codec_tag = MKTAG('Y', 'V', '1', '2');
00884 st->codec->width = s->width;
00885 st->codec->height = s->height;
00886 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
00887
00888 out:
00889 return res;
00890 }
00891
00892 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
00893 {
00894 struct video_data *s = s1->priv_data;
00895 AVFrame *frame = s1->streams[0]->codec->coded_frame;
00896 int res;
00897
00898 av_init_packet(pkt);
00899 if ((res = mmap_read_frame(s1, pkt)) < 0) {
00900 return res;
00901 }
00902
00903 if (frame && s->interlaced) {
00904 frame->interlaced_frame = 1;
00905 frame->top_field_first = s->top_field_first;
00906 }
00907
00908 return pkt->size;
00909 }
00910
00911 static int v4l2_read_close(AVFormatContext *s1)
00912 {
00913 struct video_data *s = s1->priv_data;
00914
00915 mmap_close(s);
00916
00917 v4l2_close(s->fd);
00918 return 0;
00919 }
00920
00921 #define OFFSET(x) offsetof(struct video_data, x)
00922 #define DEC AV_OPT_FLAG_DECODING_PARAM
00923
00924 static const AVOption options[] = {
00925 { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
00926 { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
00927 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
00928 { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00929 { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00930 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00931 { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
00932 { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
00933 { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
00934 { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
00935 { "timestamps", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
00936 { "default", "Use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, "timestamps" },
00937 { "abs", "Use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, "timestamps" },
00938 { "mono2abs", "Force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, "timestamps" },
00939 { "ts", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
00940 { NULL },
00941 };
00942
00943 static const AVClass v4l2_class = {
00944 .class_name = "V4L2 indev",
00945 .item_name = av_default_item_name,
00946 .option = options,
00947 .version = LIBAVUTIL_VERSION_INT,
00948 };
00949
00950 AVInputFormat ff_v4l2_demuxer = {
00951 .name = "video4linux2,v4l2",
00952 .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
00953 .priv_data_size = sizeof(struct video_data),
00954 .read_header = v4l2_read_header,
00955 .read_packet = v4l2_read_packet,
00956 .read_close = v4l2_read_close,
00957 .flags = AVFMT_NOFILE,
00958 .priv_class = &v4l2_class,
00959 };