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.

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