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.

542 lines
14KB

  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 fmt_map {
  58. enum PixelFormat ff_fmt;
  59. int32_t v4l2_fmt;
  60. };
  61. static struct fmt_map fmt_conversion_table[] = {
  62. {
  63. .ff_fmt = PIX_FMT_YUV420P,
  64. .v4l2_fmt = V4L2_PIX_FMT_YUV420,
  65. },
  66. {
  67. .ff_fmt = PIX_FMT_YUV422P,
  68. .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
  69. },
  70. {
  71. .ff_fmt = PIX_FMT_YUV422,
  72. .v4l2_fmt = V4L2_PIX_FMT_YUYV,
  73. },
  74. {
  75. .ff_fmt = PIX_FMT_UYVY422,
  76. .v4l2_fmt = V4L2_PIX_FMT_UYVY,
  77. },
  78. {
  79. .ff_fmt = PIX_FMT_YUV411P,
  80. .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
  81. },
  82. {
  83. .ff_fmt = PIX_FMT_YUV410P,
  84. .v4l2_fmt = V4L2_PIX_FMT_YUV410,
  85. },
  86. {
  87. .ff_fmt = PIX_FMT_BGR24,
  88. .v4l2_fmt = V4L2_PIX_FMT_BGR24,
  89. },
  90. {
  91. .ff_fmt = PIX_FMT_RGB24,
  92. .v4l2_fmt = V4L2_PIX_FMT_RGB24,
  93. },
  94. /*
  95. {
  96. .ff_fmt = PIX_FMT_RGBA32,
  97. .v4l2_fmt = V4L2_PIX_FMT_BGR32,
  98. },
  99. */
  100. {
  101. .ff_fmt = PIX_FMT_GRAY8,
  102. .v4l2_fmt = V4L2_PIX_FMT_GREY,
  103. },
  104. };
  105. static int device_open(const char *devname, uint32_t *capabilities)
  106. {
  107. struct v4l2_capability cap;
  108. int fd;
  109. int res;
  110. fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
  111. if (fd < 0) {
  112. av_log(NULL, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
  113. devname, strerror(errno));
  114. return -1;
  115. }
  116. res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
  117. // ENOIOCTLCMD definition only availble on __KERNEL__
  118. if (res < 0 && errno == 515)
  119. {
  120. av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
  121. close(fd);
  122. return -1;
  123. }
  124. if (res < 0) {
  125. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
  126. strerror(errno));
  127. close(fd);
  128. return -1;
  129. }
  130. if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
  131. av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
  132. close(fd);
  133. return -1;
  134. }
  135. *capabilities = cap.capabilities;
  136. return fd;
  137. }
  138. static int device_init(int fd, int *width, int *height, int pix_fmt)
  139. {
  140. struct v4l2_format fmt;
  141. int res;
  142. memset(&fmt, 0, sizeof(struct v4l2_format));
  143. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  144. fmt.fmt.pix.width = *width;
  145. fmt.fmt.pix.height = *height;
  146. fmt.fmt.pix.pixelformat = pix_fmt;
  147. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  148. res = ioctl(fd, VIDIOC_S_FMT, &fmt);
  149. if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
  150. av_log(NULL, 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);
  151. *width = fmt.fmt.pix.width;
  152. *height = fmt.fmt.pix.height;
  153. }
  154. return res;
  155. }
  156. static int first_field(int fd)
  157. {
  158. int res;
  159. v4l2_std_id std;
  160. res = ioctl(fd, VIDIOC_G_STD, &std);
  161. if (res < 0) {
  162. return 0;
  163. }
  164. if (std & V4L2_STD_NTSC) {
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
  170. {
  171. int i;
  172. for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
  173. if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
  174. return fmt_conversion_table[i].v4l2_fmt;
  175. }
  176. }
  177. return 0;
  178. }
  179. static enum PixelFormat fmt_v4l2ff(uint32_t 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].v4l2_fmt == pix_fmt) {
  184. return fmt_conversion_table[i].ff_fmt;
  185. }
  186. }
  187. return -1;
  188. }
  189. static int mmap_init(struct video_data *s)
  190. {
  191. struct v4l2_requestbuffers req;
  192. int i, res;
  193. memset(&req, 0, sizeof(struct v4l2_requestbuffers));
  194. req.count = desired_video_buffers;
  195. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  196. req.memory = V4L2_MEMORY_MMAP;
  197. res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
  198. if (res < 0) {
  199. if (errno == EINVAL) {
  200. av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
  201. } else {
  202. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
  203. }
  204. return -1;
  205. }
  206. if (req.count < 2) {
  207. av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
  208. return -1;
  209. }
  210. s->buffers = req.count;
  211. s->buf_start = av_malloc(sizeof(void *) * s->buffers);
  212. if (s->buf_start == NULL) {
  213. av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
  214. return -1;
  215. }
  216. s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
  217. if (s->buf_len == NULL) {
  218. av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
  219. av_free(s->buf_start);
  220. return -1;
  221. }
  222. for (i = 0; i < req.count; i++) {
  223. struct v4l2_buffer buf;
  224. memset(&buf, 0, sizeof(struct v4l2_buffer));
  225. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  226. buf.memory = V4L2_MEMORY_MMAP;
  227. buf.index = i;
  228. res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
  229. if (res < 0) {
  230. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
  231. return -1;
  232. }
  233. s->buf_len[i] = buf.length;
  234. if (s->buf_len[i] < s->frame_size) {
  235. av_log(NULL, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
  236. return -1;
  237. }
  238. s->buf_start[i] = mmap (NULL, buf.length,
  239. PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
  240. if (s->buf_start[i] == MAP_FAILED) {
  241. av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  242. return -1;
  243. }
  244. }
  245. return 0;
  246. }
  247. static int read_init(struct video_data *s)
  248. {
  249. return -1;
  250. }
  251. static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
  252. {
  253. struct v4l2_buffer buf;
  254. int res;
  255. memset(&buf, 0, sizeof(struct v4l2_buffer));
  256. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  257. buf.memory = V4L2_MEMORY_MMAP;
  258. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  259. while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
  260. ((errno == EAGAIN) || (errno == EINTR)));
  261. if (res < 0) {
  262. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
  263. return -1;
  264. }
  265. assert (buf.index < s->buffers);
  266. if (buf.bytesused != s->frame_size) {
  267. av_log(NULL, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
  268. return -1;
  269. }
  270. /* Image is at s->buff_start[buf.index] */
  271. memcpy(frame, s->buf_start[buf.index], buf.bytesused);
  272. *ts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  273. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  274. if (res < 0) {
  275. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
  276. return -1;
  277. }
  278. return s->buf_len[buf.index];
  279. }
  280. static int read_frame(struct video_data *s, void *frame, int64_t *ts)
  281. {
  282. return -1;
  283. }
  284. static int mmap_start(struct video_data *s)
  285. {
  286. enum v4l2_buf_type type;
  287. int i, res;
  288. for (i = 0; i < s->buffers; i++) {
  289. struct v4l2_buffer buf;
  290. memset(&buf, 0, sizeof(struct v4l2_buffer));
  291. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  292. buf.memory = V4L2_MEMORY_MMAP;
  293. buf.index = i;
  294. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  295. if (res < 0) {
  296. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
  297. return -1;
  298. }
  299. }
  300. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  301. res = ioctl (s->fd, VIDIOC_STREAMON, &type);
  302. if (res < 0) {
  303. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
  304. return -1;
  305. }
  306. return 0;
  307. }
  308. static void mmap_close(struct video_data *s)
  309. {
  310. enum v4l2_buf_type type;
  311. int i;
  312. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  313. /* We do not check for the result, because we could
  314. * not do anything about it anyway...
  315. */
  316. ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  317. for (i = 0; i < s->buffers; i++) {
  318. munmap(s->buf_start[i], s->buf_len[i]);
  319. }
  320. av_free(s->buf_start);
  321. av_free(s->buf_len);
  322. }
  323. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  324. {
  325. struct video_data *s = s1->priv_data;
  326. AVStream *st;
  327. int width, height;
  328. int res, frame_rate, frame_rate_base;
  329. uint32_t desired_format, capabilities;
  330. const char *video_device;
  331. if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  332. av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
  333. return -1;
  334. }
  335. width = ap->width;
  336. height = ap->height;
  337. frame_rate = ap->time_base.den;
  338. frame_rate_base = ap->time_base.num;
  339. if((unsigned)width > 32767 || (unsigned)height > 32767) {
  340. av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
  341. return -1;
  342. }
  343. st = av_new_stream(s1, 0);
  344. if (!st) {
  345. return -ENOMEM;
  346. }
  347. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  348. s->width = width;
  349. s->height = height;
  350. s->frame_rate = frame_rate;
  351. s->frame_rate_base = frame_rate_base;
  352. video_device = ap->device;
  353. if (!video_device) {
  354. video_device = "/dev/video";
  355. }
  356. capabilities = 0;
  357. s->fd = device_open(video_device, &capabilities);
  358. if (s->fd < 0) {
  359. av_free(st);
  360. return AVERROR_IO;
  361. }
  362. av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
  363. desired_format = fmt_ff2v4l(ap->pix_fmt);
  364. if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
  365. int i, done;
  366. done = 0; i = 0;
  367. while (!done) {
  368. desired_format = fmt_conversion_table[i].v4l2_fmt;
  369. if (device_init(s->fd, &width, &height, desired_format) < 0) {
  370. desired_format = 0;
  371. i++;
  372. } else {
  373. done = 1;
  374. }
  375. if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
  376. done = 1;
  377. }
  378. }
  379. }
  380. if (desired_format == 0) {
  381. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
  382. close(s->fd);
  383. av_free(st);
  384. return AVERROR_IO;
  385. }
  386. s->frame_format = desired_format;
  387. st->codec->pix_fmt = fmt_v4l2ff(desired_format);
  388. s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  389. if (capabilities & V4L2_CAP_STREAMING) {
  390. s->io_method = io_mmap;
  391. res = mmap_init(s);
  392. if (res == 0) {
  393. res = mmap_start(s);
  394. }
  395. } else {
  396. s->io_method = io_read;
  397. res = read_init(s);
  398. }
  399. if (res < 0) {
  400. close(s->fd);
  401. av_free(st);
  402. return AVERROR_IO;
  403. }
  404. s->top_field_first = first_field(s->fd);
  405. st->codec->codec_type = CODEC_TYPE_VIDEO;
  406. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  407. st->codec->width = width;
  408. st->codec->height = height;
  409. st->codec->time_base.den = frame_rate;
  410. st->codec->time_base.num = frame_rate_base;
  411. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  412. return 0;
  413. }
  414. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  415. {
  416. struct video_data *s = s1->priv_data;
  417. int res;
  418. if (av_new_packet(pkt, s->frame_size) < 0)
  419. return AVERROR_IO;
  420. if (s->io_method == io_mmap) {
  421. res = mmap_read_frame(s, pkt->data, &pkt->pts);
  422. } else if (s->io_method == io_read) {
  423. res = read_frame(s, pkt->data, &pkt->pts);
  424. } else {
  425. return AVERROR_IO;
  426. }
  427. if (res < 0) {
  428. return AVERROR_IO;
  429. }
  430. if (s1->streams[0]->codec->coded_frame) {
  431. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  432. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  433. }
  434. return s->frame_size;
  435. }
  436. static int v4l2_read_close(AVFormatContext *s1)
  437. {
  438. struct video_data *s = s1->priv_data;
  439. if (s->io_method == io_mmap) {
  440. mmap_close(s);
  441. }
  442. close(s->fd);
  443. return 0;
  444. }
  445. AVInputFormat v4l2_demuxer = {
  446. "video4linux2",
  447. "video grab",
  448. sizeof(struct video_data),
  449. NULL,
  450. v4l2_read_header,
  451. v4l2_read_packet,
  452. v4l2_read_close,
  453. .flags = AVFMT_NOFILE,
  454. };