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.

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