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.

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