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.

537 lines
13KB

  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. assert(buf.bytesused == s->frame_size);
  267. /* Image is at s->buff_start[buf.index] */
  268. memcpy(frame, s->buf_start[buf.index], buf.bytesused);
  269. *ts = buf.timestamp.tv_sec * int64_t_C(1000000) + buf.timestamp.tv_usec;
  270. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  271. if (res < 0) {
  272. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
  273. return -1;
  274. }
  275. return s->buf_len[buf.index];
  276. }
  277. static int read_frame(struct video_data *s, void *frame, int64_t *ts)
  278. {
  279. return -1;
  280. }
  281. static int mmap_start(struct video_data *s)
  282. {
  283. enum v4l2_buf_type type;
  284. int i, res;
  285. for (i = 0; i < s->buffers; i++) {
  286. struct v4l2_buffer buf;
  287. memset(&buf, 0, sizeof(struct v4l2_buffer));
  288. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  289. buf.memory = V4L2_MEMORY_MMAP;
  290. buf.index = i;
  291. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  292. if (res < 0) {
  293. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
  294. return -1;
  295. }
  296. }
  297. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  298. res = ioctl (s->fd, VIDIOC_STREAMON, &type);
  299. if (res < 0) {
  300. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
  301. return -1;
  302. }
  303. return 0;
  304. }
  305. static void mmap_close(struct video_data *s)
  306. {
  307. enum v4l2_buf_type type;
  308. int i;
  309. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  310. /* We do not check for the result, because we could
  311. * not do anything about it anyway...
  312. */
  313. ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  314. for (i = 0; i < s->buffers; i++) {
  315. munmap(s->buf_start[i], s->buf_len[i]);
  316. }
  317. av_free(s->buf_start);
  318. av_free(s->buf_len);
  319. }
  320. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  321. {
  322. struct video_data *s = s1->priv_data;
  323. AVStream *st;
  324. int width, height;
  325. int res, frame_rate, frame_rate_base;
  326. uint32_t desired_format, capabilities;
  327. const char *video_device;
  328. if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  329. av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
  330. return -1;
  331. }
  332. width = ap->width;
  333. height = ap->height;
  334. frame_rate = ap->time_base.den;
  335. frame_rate_base = ap->time_base.num;
  336. if((unsigned)width > 32767 || (unsigned)height > 32767) {
  337. av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
  338. return -1;
  339. }
  340. st = av_new_stream(s1, 0);
  341. if (!st) {
  342. return -ENOMEM;
  343. }
  344. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  345. s->width = width;
  346. s->height = height;
  347. s->frame_rate = frame_rate;
  348. s->frame_rate_base = frame_rate_base;
  349. video_device = ap->device;
  350. if (!video_device) {
  351. video_device = "/dev/video";
  352. }
  353. capabilities = 0;
  354. s->fd = device_open(video_device, &capabilities);
  355. if (s->fd < 0) {
  356. av_free(st);
  357. return AVERROR_IO;
  358. }
  359. av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
  360. desired_format = fmt_ff2v4l(ap->pix_fmt);
  361. if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
  362. int i, done;
  363. done = 0; i = 0;
  364. while (!done) {
  365. desired_format = fmt_conversion_table[i].v4l2_fmt;
  366. if (device_init(s->fd, &width, &height, desired_format) < 0) {
  367. desired_format = 0;
  368. i++;
  369. } else {
  370. done = 1;
  371. }
  372. if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
  373. done = 1;
  374. }
  375. }
  376. }
  377. if (desired_format == 0) {
  378. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
  379. close(s->fd);
  380. av_free(st);
  381. return AVERROR_IO;
  382. }
  383. s->frame_format = desired_format;
  384. st->codec->pix_fmt = fmt_v4l2ff(desired_format);
  385. s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  386. if (capabilities & V4L2_CAP_STREAMING) {
  387. s->io_method = io_mmap;
  388. res = mmap_init(s);
  389. if (res == 0) {
  390. res = mmap_start(s);
  391. }
  392. } else {
  393. s->io_method = io_read;
  394. res = read_init(s);
  395. }
  396. if (res < 0) {
  397. close(s->fd);
  398. av_free(st);
  399. return AVERROR_IO;
  400. }
  401. s->top_field_first = first_field(s->fd);
  402. st->codec->codec_type = CODEC_TYPE_VIDEO;
  403. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  404. st->codec->width = width;
  405. st->codec->height = height;
  406. st->codec->time_base.den = frame_rate;
  407. st->codec->time_base.num = frame_rate_base;
  408. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  409. return 0;
  410. }
  411. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  412. {
  413. struct video_data *s = s1->priv_data;
  414. int res;
  415. if (av_new_packet(pkt, s->frame_size) < 0)
  416. return AVERROR_IO;
  417. if (s->io_method == io_mmap) {
  418. res = mmap_read_frame(s, pkt->data, &pkt->pts);
  419. } else if (s->io_method == io_read) {
  420. res = read_frame(s, pkt->data, &pkt->pts);
  421. } else {
  422. return AVERROR_IO;
  423. }
  424. if (res < 0) {
  425. return AVERROR_IO;
  426. }
  427. if (s1->streams[0]->codec->coded_frame) {
  428. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  429. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  430. }
  431. return s->frame_size;
  432. }
  433. static int v4l2_read_close(AVFormatContext *s1)
  434. {
  435. struct video_data *s = s1->priv_data;
  436. if (s->io_method == io_mmap) {
  437. mmap_close(s);
  438. }
  439. close(s->fd);
  440. return 0;
  441. }
  442. AVInputFormat v4l2_demuxer = {
  443. "video4linux2",
  444. "video grab",
  445. sizeof(struct video_data),
  446. NULL,
  447. v4l2_read_header,
  448. v4l2_read_packet,
  449. v4l2_read_close,
  450. .flags = AVFMT_NOFILE,
  451. };