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.

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