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.

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