You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

647 lines
17KB

  1. /*
  2. * Video4Linux2 grab interface
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2006 Luca Abeni.
  5. *
  6. * Part of this file is based on the V4L2 video capture example
  7. * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
  8. *
  9. * Thanks to Michael Niedermayer for providing the mapping between
  10. * V4L2_PIX_FMT_* and PIX_FMT_*
  11. *
  12. *
  13. * This file is part of FFmpeg.
  14. *
  15. * FFmpeg is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. *
  20. * FFmpeg is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with FFmpeg; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #undef __STRICT_ANSI__ //workaround due to broken kernel headers
  30. #include "config.h"
  31. #include "libavformat/avformat.h"
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/mman.h>
  36. #include <sys/time.h>
  37. #include <asm/types.h>
  38. #include <linux/videodev2.h>
  39. #include <time.h>
  40. #include <strings.h>
  41. static const int desired_video_buffers = 256;
  42. enum io_method {
  43. io_read,
  44. io_mmap,
  45. io_userptr
  46. };
  47. struct video_data {
  48. int fd;
  49. int frame_format; /* V4L2_PIX_FMT_* */
  50. enum io_method io_method;
  51. int width, height;
  52. int frame_rate;
  53. int frame_rate_base;
  54. int frame_size;
  55. int top_field_first;
  56. int buffers;
  57. void **buf_start;
  58. unsigned int *buf_len;
  59. };
  60. struct buff_data {
  61. int index;
  62. int fd;
  63. };
  64. struct fmt_map {
  65. enum PixelFormat ff_fmt;
  66. int32_t v4l2_fmt;
  67. };
  68. static struct fmt_map fmt_conversion_table[] = {
  69. {
  70. .ff_fmt = PIX_FMT_YUV420P,
  71. .v4l2_fmt = V4L2_PIX_FMT_YUV420,
  72. },
  73. {
  74. .ff_fmt = PIX_FMT_YUV422P,
  75. .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
  76. },
  77. {
  78. .ff_fmt = PIX_FMT_YUYV422,
  79. .v4l2_fmt = V4L2_PIX_FMT_YUYV,
  80. },
  81. {
  82. .ff_fmt = PIX_FMT_UYVY422,
  83. .v4l2_fmt = V4L2_PIX_FMT_UYVY,
  84. },
  85. {
  86. .ff_fmt = PIX_FMT_YUV411P,
  87. .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
  88. },
  89. {
  90. .ff_fmt = PIX_FMT_YUV410P,
  91. .v4l2_fmt = V4L2_PIX_FMT_YUV410,
  92. },
  93. {
  94. .ff_fmt = PIX_FMT_BGR24,
  95. .v4l2_fmt = V4L2_PIX_FMT_BGR24,
  96. },
  97. {
  98. .ff_fmt = PIX_FMT_RGB24,
  99. .v4l2_fmt = V4L2_PIX_FMT_RGB24,
  100. },
  101. /*
  102. {
  103. .ff_fmt = PIX_FMT_RGB32,
  104. .v4l2_fmt = V4L2_PIX_FMT_BGR32,
  105. },
  106. */
  107. {
  108. .ff_fmt = PIX_FMT_GRAY8,
  109. .v4l2_fmt = V4L2_PIX_FMT_GREY,
  110. },
  111. };
  112. static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
  113. {
  114. struct v4l2_capability cap;
  115. int fd;
  116. int res;
  117. int flags = O_RDWR;
  118. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  119. flags |= O_NONBLOCK;
  120. }
  121. fd = open(ctx->filename, flags, 0);
  122. if (fd < 0) {
  123. av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
  124. ctx->filename, strerror(errno));
  125. return -1;
  126. }
  127. res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
  128. // ENOIOCTLCMD definition only availble on __KERNEL__
  129. if (res < 0 && errno == 515)
  130. {
  131. av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
  132. close(fd);
  133. return -1;
  134. }
  135. if (res < 0) {
  136. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
  137. strerror(errno));
  138. close(fd);
  139. return -1;
  140. }
  141. if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
  142. av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
  143. close(fd);
  144. return -1;
  145. }
  146. *capabilities = cap.capabilities;
  147. return fd;
  148. }
  149. static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
  150. {
  151. struct video_data *s = ctx->priv_data;
  152. int fd = s->fd;
  153. struct v4l2_format fmt;
  154. int res;
  155. memset(&fmt, 0, sizeof(struct v4l2_format));
  156. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  157. fmt.fmt.pix.width = *width;
  158. fmt.fmt.pix.height = *height;
  159. fmt.fmt.pix.pixelformat = pix_fmt;
  160. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  161. res = ioctl(fd, VIDIOC_S_FMT, &fmt);
  162. if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
  163. 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);
  164. *width = fmt.fmt.pix.width;
  165. *height = fmt.fmt.pix.height;
  166. }
  167. return res;
  168. }
  169. static int first_field(int fd)
  170. {
  171. int res;
  172. v4l2_std_id std;
  173. res = ioctl(fd, VIDIOC_G_STD, &std);
  174. if (res < 0) {
  175. return 0;
  176. }
  177. if (std & V4L2_STD_NTSC) {
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
  183. {
  184. int i;
  185. for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
  186. if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
  187. return fmt_conversion_table[i].v4l2_fmt;
  188. }
  189. }
  190. return 0;
  191. }
  192. static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
  193. {
  194. int i;
  195. for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
  196. if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
  197. return fmt_conversion_table[i].ff_fmt;
  198. }
  199. }
  200. return PIX_FMT_NONE;
  201. }
  202. static int mmap_init(AVFormatContext *ctx)
  203. {
  204. struct video_data *s = ctx->priv_data;
  205. struct v4l2_requestbuffers req;
  206. int i, res;
  207. memset(&req, 0, sizeof(struct v4l2_requestbuffers));
  208. req.count = desired_video_buffers;
  209. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  210. req.memory = V4L2_MEMORY_MMAP;
  211. res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
  212. if (res < 0) {
  213. if (errno == EINVAL) {
  214. av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
  215. } else {
  216. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
  217. }
  218. return -1;
  219. }
  220. if (req.count < 2) {
  221. av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
  222. return -1;
  223. }
  224. s->buffers = req.count;
  225. s->buf_start = av_malloc(sizeof(void *) * s->buffers);
  226. if (s->buf_start == NULL) {
  227. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
  228. return -1;
  229. }
  230. s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
  231. if (s->buf_len == NULL) {
  232. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
  233. av_free(s->buf_start);
  234. return -1;
  235. }
  236. for (i = 0; i < req.count; i++) {
  237. struct v4l2_buffer buf;
  238. memset(&buf, 0, sizeof(struct v4l2_buffer));
  239. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  240. buf.memory = V4L2_MEMORY_MMAP;
  241. buf.index = i;
  242. res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
  243. if (res < 0) {
  244. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
  245. return -1;
  246. }
  247. s->buf_len[i] = buf.length;
  248. if (s->buf_len[i] < s->frame_size) {
  249. av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
  250. return -1;
  251. }
  252. s->buf_start[i] = mmap (NULL, buf.length,
  253. PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
  254. if (s->buf_start[i] == MAP_FAILED) {
  255. av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  256. return -1;
  257. }
  258. }
  259. return 0;
  260. }
  261. static int read_init(AVFormatContext *ctx)
  262. {
  263. return -1;
  264. }
  265. static void mmap_release_buffer(AVPacket *pkt)
  266. {
  267. struct v4l2_buffer buf;
  268. int res, fd;
  269. struct buff_data *buf_descriptor = pkt->priv;
  270. memset(&buf, 0, sizeof(struct v4l2_buffer));
  271. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  272. buf.memory = V4L2_MEMORY_MMAP;
  273. buf.index = buf_descriptor->index;
  274. fd = buf_descriptor->fd;
  275. av_free(buf_descriptor);
  276. res = ioctl (fd, VIDIOC_QBUF, &buf);
  277. if (res < 0) {
  278. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
  279. }
  280. pkt->data = NULL;
  281. pkt->size = 0;
  282. }
  283. static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
  284. {
  285. struct video_data *s = ctx->priv_data;
  286. struct v4l2_buffer buf;
  287. struct buff_data *buf_descriptor;
  288. int res;
  289. memset(&buf, 0, sizeof(struct v4l2_buffer));
  290. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  291. buf.memory = V4L2_MEMORY_MMAP;
  292. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  293. while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  294. if (res < 0) {
  295. if (errno == EAGAIN) {
  296. pkt->size = 0;
  297. return AVERROR(EAGAIN);
  298. }
  299. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
  300. return -1;
  301. }
  302. assert (buf.index < s->buffers);
  303. if (buf.bytesused != s->frame_size) {
  304. av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
  305. return -1;
  306. }
  307. /* Image is at s->buff_start[buf.index] */
  308. pkt->data= s->buf_start[buf.index];
  309. pkt->size = buf.bytesused;
  310. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  311. pkt->destruct = mmap_release_buffer;
  312. buf_descriptor = av_malloc(sizeof(struct buff_data));
  313. if (buf_descriptor == NULL) {
  314. /* Something went wrong... Since av_malloc() failed, we cannot even
  315. * allocate a buffer for memcopying into it
  316. */
  317. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  318. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  319. return -1;
  320. }
  321. buf_descriptor->fd = s->fd;
  322. buf_descriptor->index = buf.index;
  323. pkt->priv = buf_descriptor;
  324. return s->buf_len[buf.index];
  325. }
  326. static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
  327. {
  328. return -1;
  329. }
  330. static int mmap_start(AVFormatContext *ctx)
  331. {
  332. struct video_data *s = ctx->priv_data;
  333. enum v4l2_buf_type type;
  334. int i, res;
  335. for (i = 0; i < s->buffers; i++) {
  336. struct v4l2_buffer buf;
  337. memset(&buf, 0, sizeof(struct v4l2_buffer));
  338. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  339. buf.memory = V4L2_MEMORY_MMAP;
  340. buf.index = i;
  341. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  342. if (res < 0) {
  343. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
  344. return -1;
  345. }
  346. }
  347. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  348. res = ioctl (s->fd, VIDIOC_STREAMON, &type);
  349. if (res < 0) {
  350. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
  351. return -1;
  352. }
  353. return 0;
  354. }
  355. static void mmap_close(struct video_data *s)
  356. {
  357. enum v4l2_buf_type type;
  358. int i;
  359. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  360. /* We do not check for the result, because we could
  361. * not do anything about it anyway...
  362. */
  363. ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  364. for (i = 0; i < s->buffers; i++) {
  365. munmap(s->buf_start[i], s->buf_len[i]);
  366. }
  367. av_free(s->buf_start);
  368. av_free(s->buf_len);
  369. }
  370. static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
  371. {
  372. struct video_data *s = s1->priv_data;
  373. struct v4l2_input input;
  374. struct v4l2_standard standard;
  375. int i;
  376. if(ap->channel>=0) {
  377. /* set tv video input */
  378. memset (&input, 0, sizeof (input));
  379. input.index = ap->channel;
  380. if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  381. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
  382. return AVERROR(EIO);
  383. }
  384. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
  385. ap->channel, input.name);
  386. if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
  387. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
  388. ap->channel);
  389. return AVERROR(EIO);
  390. }
  391. }
  392. if(ap->standard) {
  393. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
  394. ap->standard );
  395. /* set tv standard */
  396. memset (&standard, 0, sizeof (standard));
  397. for(i=0;;i++) {
  398. standard.index = i;
  399. if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  400. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  401. ap->standard);
  402. return AVERROR(EIO);
  403. }
  404. if(!strcasecmp(standard.name, ap->standard)) {
  405. break;
  406. }
  407. }
  408. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
  409. ap->standard, standard.id);
  410. if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  411. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  412. ap->standard);
  413. return AVERROR(EIO);
  414. }
  415. }
  416. return 0;
  417. }
  418. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  419. {
  420. struct video_data *s = s1->priv_data;
  421. AVStream *st;
  422. int width, height;
  423. int res, frame_rate, frame_rate_base;
  424. uint32_t desired_format, capabilities;
  425. if (ap->width <= 0 || ap->height <= 0) {
  426. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
  427. return -1;
  428. }
  429. if (ap->time_base.den <= 0) {
  430. av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
  431. return -1;
  432. }
  433. width = ap->width;
  434. height = ap->height;
  435. frame_rate = ap->time_base.den;
  436. frame_rate_base = ap->time_base.num;
  437. if((unsigned)width > 32767 || (unsigned)height > 32767) {
  438. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
  439. return -1;
  440. }
  441. st = av_new_stream(s1, 0);
  442. if (!st) {
  443. return AVERROR(ENOMEM);
  444. }
  445. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  446. s->width = width;
  447. s->height = height;
  448. s->frame_rate = frame_rate;
  449. s->frame_rate_base = frame_rate_base;
  450. capabilities = 0;
  451. s->fd = device_open(s1, &capabilities);
  452. if (s->fd < 0) {
  453. return AVERROR(EIO);
  454. }
  455. av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
  456. desired_format = fmt_ff2v4l(ap->pix_fmt);
  457. if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
  458. int i, done;
  459. done = 0; i = 0;
  460. while (!done) {
  461. desired_format = fmt_conversion_table[i].v4l2_fmt;
  462. if (device_init(s1, &width, &height, desired_format) < 0) {
  463. desired_format = 0;
  464. i++;
  465. } else {
  466. done = 1;
  467. }
  468. if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
  469. done = 1;
  470. }
  471. }
  472. }
  473. if (desired_format == 0) {
  474. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
  475. close(s->fd);
  476. return AVERROR(EIO);
  477. }
  478. s->frame_format = desired_format;
  479. if( v4l2_set_parameters( s1, ap ) < 0 )
  480. return AVERROR(EIO);
  481. st->codec->pix_fmt = fmt_v4l2ff(desired_format);
  482. s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  483. if (capabilities & V4L2_CAP_STREAMING) {
  484. s->io_method = io_mmap;
  485. res = mmap_init(s1);
  486. if (res == 0) {
  487. res = mmap_start(s1);
  488. }
  489. } else {
  490. s->io_method = io_read;
  491. res = read_init(s1);
  492. }
  493. if (res < 0) {
  494. close(s->fd);
  495. return AVERROR(EIO);
  496. }
  497. s->top_field_first = first_field(s->fd);
  498. st->codec->codec_type = CODEC_TYPE_VIDEO;
  499. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  500. st->codec->width = width;
  501. st->codec->height = height;
  502. st->codec->time_base.den = frame_rate;
  503. st->codec->time_base.num = frame_rate_base;
  504. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  505. return 0;
  506. }
  507. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  508. {
  509. struct video_data *s = s1->priv_data;
  510. int res;
  511. if (s->io_method == io_mmap) {
  512. av_init_packet(pkt);
  513. res = mmap_read_frame(s1, pkt);
  514. } else if (s->io_method == io_read) {
  515. if (av_new_packet(pkt, s->frame_size) < 0)
  516. return AVERROR(EIO);
  517. res = read_frame(s1, pkt);
  518. } else {
  519. return AVERROR(EIO);
  520. }
  521. if (res < 0) {
  522. return res;
  523. }
  524. if (s1->streams[0]->codec->coded_frame) {
  525. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  526. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  527. }
  528. return s->frame_size;
  529. }
  530. static int v4l2_read_close(AVFormatContext *s1)
  531. {
  532. struct video_data *s = s1->priv_data;
  533. if (s->io_method == io_mmap) {
  534. mmap_close(s);
  535. }
  536. close(s->fd);
  537. return 0;
  538. }
  539. AVInputFormat v4l2_demuxer = {
  540. "video4linux2",
  541. NULL_IF_CONFIG_SMALL("video grab"),
  542. sizeof(struct video_data),
  543. NULL,
  544. v4l2_read_header,
  545. v4l2_read_packet,
  546. v4l2_read_close,
  547. .flags = AVFMT_NOFILE,
  548. };