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.

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