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.

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