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.

657 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. return res;
  178. }
  179. static int first_field(int fd)
  180. {
  181. int res;
  182. v4l2_std_id std;
  183. res = ioctl(fd, VIDIOC_G_STD, &std);
  184. if (res < 0) {
  185. return 0;
  186. }
  187. if (std & V4L2_STD_NTSC) {
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. static uint32_t fmt_ff2v4l(enum PixelFormat 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].ff_fmt == pix_fmt) {
  197. return fmt_conversion_table[i].v4l2_fmt;
  198. }
  199. }
  200. return 0;
  201. }
  202. static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
  203. {
  204. int i;
  205. for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
  206. if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
  207. return fmt_conversion_table[i].ff_fmt;
  208. }
  209. }
  210. return PIX_FMT_NONE;
  211. }
  212. static int mmap_init(AVFormatContext *ctx)
  213. {
  214. struct video_data *s = ctx->priv_data;
  215. struct v4l2_requestbuffers req;
  216. int i, res;
  217. memset(&req, 0, sizeof(struct v4l2_requestbuffers));
  218. req.count = desired_video_buffers;
  219. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  220. req.memory = V4L2_MEMORY_MMAP;
  221. res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
  222. if (res < 0) {
  223. if (errno == EINVAL) {
  224. av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
  225. } else {
  226. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
  227. }
  228. return -1;
  229. }
  230. if (req.count < 2) {
  231. av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
  232. return -1;
  233. }
  234. s->buffers = req.count;
  235. s->buf_start = av_malloc(sizeof(void *) * s->buffers);
  236. if (s->buf_start == NULL) {
  237. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
  238. return -1;
  239. }
  240. s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
  241. if (s->buf_len == NULL) {
  242. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
  243. av_free(s->buf_start);
  244. return -1;
  245. }
  246. for (i = 0; i < req.count; i++) {
  247. struct v4l2_buffer buf;
  248. memset(&buf, 0, sizeof(struct v4l2_buffer));
  249. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  250. buf.memory = V4L2_MEMORY_MMAP;
  251. buf.index = i;
  252. res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
  253. if (res < 0) {
  254. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
  255. return -1;
  256. }
  257. s->buf_len[i] = buf.length;
  258. if (s->buf_len[i] < s->frame_size) {
  259. av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
  260. return -1;
  261. }
  262. s->buf_start[i] = mmap (NULL, buf.length,
  263. PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
  264. if (s->buf_start[i] == MAP_FAILED) {
  265. av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  266. return -1;
  267. }
  268. }
  269. return 0;
  270. }
  271. static int read_init(AVFormatContext *ctx)
  272. {
  273. return -1;
  274. }
  275. static void mmap_release_buffer(AVPacket *pkt)
  276. {
  277. struct v4l2_buffer buf;
  278. int res, fd;
  279. struct buff_data *buf_descriptor = pkt->priv;
  280. memset(&buf, 0, sizeof(struct v4l2_buffer));
  281. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  282. buf.memory = V4L2_MEMORY_MMAP;
  283. buf.index = buf_descriptor->index;
  284. fd = buf_descriptor->fd;
  285. av_free(buf_descriptor);
  286. res = ioctl (fd, VIDIOC_QBUF, &buf);
  287. if (res < 0) {
  288. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
  289. }
  290. pkt->data = NULL;
  291. pkt->size = 0;
  292. }
  293. static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
  294. {
  295. struct video_data *s = ctx->priv_data;
  296. struct v4l2_buffer buf;
  297. struct buff_data *buf_descriptor;
  298. int res;
  299. memset(&buf, 0, sizeof(struct v4l2_buffer));
  300. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  301. buf.memory = V4L2_MEMORY_MMAP;
  302. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  303. while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  304. if (res < 0) {
  305. if (errno == EAGAIN) {
  306. pkt->size = 0;
  307. return AVERROR(EAGAIN);
  308. }
  309. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
  310. return -1;
  311. }
  312. assert (buf.index < s->buffers);
  313. if (buf.bytesused != s->frame_size) {
  314. av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
  315. return -1;
  316. }
  317. /* Image is at s->buff_start[buf.index] */
  318. pkt->data= s->buf_start[buf.index];
  319. pkt->size = buf.bytesused;
  320. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  321. pkt->destruct = mmap_release_buffer;
  322. buf_descriptor = av_malloc(sizeof(struct buff_data));
  323. if (buf_descriptor == NULL) {
  324. /* Something went wrong... Since av_malloc() failed, we cannot even
  325. * allocate a buffer for memcopying into it
  326. */
  327. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  328. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  329. return -1;
  330. }
  331. buf_descriptor->fd = s->fd;
  332. buf_descriptor->index = buf.index;
  333. pkt->priv = buf_descriptor;
  334. return s->buf_len[buf.index];
  335. }
  336. static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
  337. {
  338. return -1;
  339. }
  340. static int mmap_start(AVFormatContext *ctx)
  341. {
  342. struct video_data *s = ctx->priv_data;
  343. enum v4l2_buf_type type;
  344. int i, res;
  345. for (i = 0; i < s->buffers; i++) {
  346. struct v4l2_buffer buf;
  347. memset(&buf, 0, sizeof(struct v4l2_buffer));
  348. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  349. buf.memory = V4L2_MEMORY_MMAP;
  350. buf.index = i;
  351. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  352. if (res < 0) {
  353. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
  354. return -1;
  355. }
  356. }
  357. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  358. res = ioctl (s->fd, VIDIOC_STREAMON, &type);
  359. if (res < 0) {
  360. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. static void mmap_close(struct video_data *s)
  366. {
  367. enum v4l2_buf_type type;
  368. int i;
  369. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  370. /* We do not check for the result, because we could
  371. * not do anything about it anyway...
  372. */
  373. ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  374. for (i = 0; i < s->buffers; i++) {
  375. munmap(s->buf_start[i], s->buf_len[i]);
  376. }
  377. av_free(s->buf_start);
  378. av_free(s->buf_len);
  379. }
  380. static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
  381. {
  382. struct video_data *s = s1->priv_data;
  383. struct v4l2_input input;
  384. struct v4l2_standard standard;
  385. int i;
  386. if(ap->channel>=0) {
  387. /* set tv video input */
  388. memset (&input, 0, sizeof (input));
  389. input.index = ap->channel;
  390. if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  391. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
  392. return AVERROR(EIO);
  393. }
  394. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
  395. ap->channel, input.name);
  396. if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
  397. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
  398. ap->channel);
  399. return AVERROR(EIO);
  400. }
  401. }
  402. if(ap->standard) {
  403. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
  404. ap->standard );
  405. /* set tv standard */
  406. memset (&standard, 0, sizeof (standard));
  407. for(i=0;;i++) {
  408. standard.index = i;
  409. if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  410. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  411. ap->standard);
  412. return AVERROR(EIO);
  413. }
  414. if(!strcasecmp(standard.name, ap->standard)) {
  415. break;
  416. }
  417. }
  418. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
  419. ap->standard, standard.id);
  420. if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  421. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  422. ap->standard);
  423. return AVERROR(EIO);
  424. }
  425. }
  426. return 0;
  427. }
  428. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  429. {
  430. struct video_data *s = s1->priv_data;
  431. AVStream *st;
  432. int width, height;
  433. int res, frame_rate, frame_rate_base;
  434. uint32_t desired_format, capabilities;
  435. if (ap->width <= 0 || ap->height <= 0) {
  436. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
  437. return -1;
  438. }
  439. if (ap->time_base.den <= 0) {
  440. av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
  441. return -1;
  442. }
  443. width = ap->width;
  444. height = ap->height;
  445. frame_rate = ap->time_base.den;
  446. frame_rate_base = ap->time_base.num;
  447. if((unsigned)width > 32767 || (unsigned)height > 32767) {
  448. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
  449. return -1;
  450. }
  451. st = av_new_stream(s1, 0);
  452. if (!st) {
  453. return AVERROR(ENOMEM);
  454. }
  455. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  456. s->width = width;
  457. s->height = height;
  458. s->frame_rate = frame_rate;
  459. s->frame_rate_base = frame_rate_base;
  460. capabilities = 0;
  461. s->fd = device_open(s1, &capabilities);
  462. if (s->fd < 0) {
  463. return AVERROR(EIO);
  464. }
  465. av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
  466. desired_format = fmt_ff2v4l(ap->pix_fmt);
  467. if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
  468. int i, done;
  469. done = 0; i = 0;
  470. while (!done) {
  471. desired_format = fmt_conversion_table[i].v4l2_fmt;
  472. if (device_init(s1, &width, &height, desired_format) < 0) {
  473. desired_format = 0;
  474. i++;
  475. } else {
  476. done = 1;
  477. }
  478. if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
  479. done = 1;
  480. }
  481. }
  482. }
  483. if (desired_format == 0) {
  484. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
  485. close(s->fd);
  486. return AVERROR(EIO);
  487. }
  488. s->frame_format = desired_format;
  489. if( v4l2_set_parameters( s1, ap ) < 0 )
  490. return AVERROR(EIO);
  491. st->codec->pix_fmt = fmt_v4l2ff(desired_format);
  492. s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  493. if (capabilities & V4L2_CAP_STREAMING) {
  494. s->io_method = io_mmap;
  495. res = mmap_init(s1);
  496. if (res == 0) {
  497. res = mmap_start(s1);
  498. }
  499. } else {
  500. s->io_method = io_read;
  501. res = read_init(s1);
  502. }
  503. if (res < 0) {
  504. close(s->fd);
  505. return AVERROR(EIO);
  506. }
  507. s->top_field_first = first_field(s->fd);
  508. st->codec->codec_type = CODEC_TYPE_VIDEO;
  509. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  510. st->codec->width = width;
  511. st->codec->height = height;
  512. st->codec->time_base.den = frame_rate;
  513. st->codec->time_base.num = frame_rate_base;
  514. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  515. return 0;
  516. }
  517. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  518. {
  519. struct video_data *s = s1->priv_data;
  520. int res;
  521. if (s->io_method == io_mmap) {
  522. av_init_packet(pkt);
  523. res = mmap_read_frame(s1, pkt);
  524. } else if (s->io_method == io_read) {
  525. if (av_new_packet(pkt, s->frame_size) < 0)
  526. return AVERROR(EIO);
  527. res = read_frame(s1, pkt);
  528. } else {
  529. return AVERROR(EIO);
  530. }
  531. if (res < 0) {
  532. return res;
  533. }
  534. if (s1->streams[0]->codec->coded_frame) {
  535. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  536. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  537. }
  538. return s->frame_size;
  539. }
  540. static int v4l2_read_close(AVFormatContext *s1)
  541. {
  542. struct video_data *s = s1->priv_data;
  543. if (s->io_method == io_mmap) {
  544. mmap_close(s);
  545. }
  546. close(s->fd);
  547. return 0;
  548. }
  549. AVInputFormat v4l2_demuxer = {
  550. "video4linux2",
  551. NULL_IF_CONFIG_SMALL("video grab"),
  552. sizeof(struct video_data),
  553. NULL,
  554. v4l2_read_header,
  555. v4l2_read_packet,
  556. v4l2_read_close,
  557. .flags = AVFMT_NOFILE,
  558. };