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.

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