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.

676 lines
18KB

  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. #undef __STRICT_ANSI__ //workaround due to broken kernel headers
  30. #include "config.h"
  31. #include "libavformat/avformat.h"
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/mman.h>
  36. #include <sys/time.h>
  37. #if HAVE_SYS_VIDEOIO_H
  38. #include <sys/videoio.h>
  39. #else
  40. #include <asm/types.h>
  41. #include <linux/videodev2.h>
  42. #endif
  43. #include <time.h>
  44. #include <strings.h>
  45. static const int desired_video_buffers = 256;
  46. enum io_method {
  47. io_read,
  48. io_mmap,
  49. io_userptr
  50. };
  51. struct video_data {
  52. int fd;
  53. int frame_format; /* V4L2_PIX_FMT_* */
  54. enum io_method io_method;
  55. int width, height;
  56. int frame_size;
  57. int top_field_first;
  58. int buffers;
  59. void **buf_start;
  60. unsigned int *buf_len;
  61. };
  62. struct buff_data {
  63. int index;
  64. int fd;
  65. };
  66. struct fmt_map {
  67. enum PixelFormat ff_fmt;
  68. enum CodecID codec_id;
  69. uint32_t v4l2_fmt;
  70. };
  71. static struct fmt_map fmt_conversion_table[] = {
  72. {
  73. .ff_fmt = PIX_FMT_YUV420P,
  74. .codec_id = CODEC_ID_RAWVIDEO,
  75. .v4l2_fmt = V4L2_PIX_FMT_YUV420,
  76. },
  77. {
  78. .ff_fmt = PIX_FMT_YUV422P,
  79. .codec_id = CODEC_ID_RAWVIDEO,
  80. .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
  81. },
  82. {
  83. .ff_fmt = PIX_FMT_YUYV422,
  84. .codec_id = CODEC_ID_RAWVIDEO,
  85. .v4l2_fmt = V4L2_PIX_FMT_YUYV,
  86. },
  87. {
  88. .ff_fmt = PIX_FMT_UYVY422,
  89. .codec_id = CODEC_ID_RAWVIDEO,
  90. .v4l2_fmt = V4L2_PIX_FMT_UYVY,
  91. },
  92. {
  93. .ff_fmt = PIX_FMT_YUV411P,
  94. .codec_id = CODEC_ID_RAWVIDEO,
  95. .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
  96. },
  97. {
  98. .ff_fmt = PIX_FMT_YUV410P,
  99. .codec_id = CODEC_ID_RAWVIDEO,
  100. .v4l2_fmt = V4L2_PIX_FMT_YUV410,
  101. },
  102. {
  103. .ff_fmt = PIX_FMT_RGB555,
  104. .codec_id = CODEC_ID_RAWVIDEO,
  105. .v4l2_fmt = V4L2_PIX_FMT_RGB555,
  106. },
  107. {
  108. .ff_fmt = PIX_FMT_RGB565,
  109. .codec_id = CODEC_ID_RAWVIDEO,
  110. .v4l2_fmt = V4L2_PIX_FMT_RGB565,
  111. },
  112. {
  113. .ff_fmt = PIX_FMT_BGR24,
  114. .codec_id = CODEC_ID_RAWVIDEO,
  115. .v4l2_fmt = V4L2_PIX_FMT_BGR24,
  116. },
  117. {
  118. .ff_fmt = PIX_FMT_RGB24,
  119. .codec_id = CODEC_ID_RAWVIDEO,
  120. .v4l2_fmt = V4L2_PIX_FMT_RGB24,
  121. },
  122. {
  123. .ff_fmt = PIX_FMT_BGRA,
  124. .codec_id = CODEC_ID_RAWVIDEO,
  125. .v4l2_fmt = V4L2_PIX_FMT_BGR32,
  126. },
  127. {
  128. .ff_fmt = PIX_FMT_GRAY8,
  129. .codec_id = CODEC_ID_RAWVIDEO,
  130. .v4l2_fmt = V4L2_PIX_FMT_GREY,
  131. },
  132. {
  133. .ff_fmt = PIX_FMT_NONE,
  134. .codec_id = CODEC_ID_MJPEG,
  135. .v4l2_fmt = V4L2_PIX_FMT_MJPEG,
  136. },
  137. {
  138. .ff_fmt = PIX_FMT_NONE,
  139. .codec_id = CODEC_ID_MJPEG,
  140. .v4l2_fmt = V4L2_PIX_FMT_JPEG,
  141. },
  142. };
  143. static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
  144. {
  145. struct v4l2_capability cap;
  146. int fd;
  147. int res;
  148. int flags = O_RDWR;
  149. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  150. flags |= O_NONBLOCK;
  151. }
  152. fd = open(ctx->filename, flags, 0);
  153. if (fd < 0) {
  154. av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
  155. ctx->filename, strerror(errno));
  156. return -1;
  157. }
  158. res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
  159. // ENOIOCTLCMD definition only availble on __KERNEL__
  160. if (res < 0 && errno == 515)
  161. {
  162. av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
  163. close(fd);
  164. return -1;
  165. }
  166. if (res < 0) {
  167. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
  168. strerror(errno));
  169. close(fd);
  170. return -1;
  171. }
  172. if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
  173. av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
  174. close(fd);
  175. return -1;
  176. }
  177. *capabilities = cap.capabilities;
  178. return fd;
  179. }
  180. static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
  181. {
  182. struct video_data *s = ctx->priv_data;
  183. int fd = s->fd;
  184. struct v4l2_format fmt;
  185. int res;
  186. memset(&fmt, 0, sizeof(struct v4l2_format));
  187. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  188. fmt.fmt.pix.width = *width;
  189. fmt.fmt.pix.height = *height;
  190. fmt.fmt.pix.pixelformat = pix_fmt;
  191. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  192. res = ioctl(fd, VIDIOC_S_FMT, &fmt);
  193. if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
  194. 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);
  195. *width = fmt.fmt.pix.width;
  196. *height = fmt.fmt.pix.height;
  197. }
  198. if (pix_fmt != fmt.fmt.pix.pixelformat) {
  199. av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
  200. res = -1;
  201. }
  202. return res;
  203. }
  204. static int first_field(int fd)
  205. {
  206. int res;
  207. v4l2_std_id std;
  208. res = ioctl(fd, VIDIOC_G_STD, &std);
  209. if (res < 0) {
  210. return 0;
  211. }
  212. if (std & V4L2_STD_NTSC) {
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
  218. {
  219. int i;
  220. for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  221. if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
  222. return fmt_conversion_table[i].v4l2_fmt;
  223. }
  224. }
  225. return 0;
  226. }
  227. static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
  228. {
  229. int i;
  230. for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  231. if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
  232. return fmt_conversion_table[i].ff_fmt;
  233. }
  234. }
  235. return PIX_FMT_NONE;
  236. }
  237. static int mmap_init(AVFormatContext *ctx)
  238. {
  239. struct video_data *s = ctx->priv_data;
  240. struct v4l2_requestbuffers req;
  241. int i, res;
  242. memset(&req, 0, sizeof(struct v4l2_requestbuffers));
  243. req.count = desired_video_buffers;
  244. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  245. req.memory = V4L2_MEMORY_MMAP;
  246. res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
  247. if (res < 0) {
  248. if (errno == EINVAL) {
  249. av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
  250. } else {
  251. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
  252. }
  253. return -1;
  254. }
  255. if (req.count < 2) {
  256. av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
  257. return -1;
  258. }
  259. s->buffers = req.count;
  260. s->buf_start = av_malloc(sizeof(void *) * s->buffers);
  261. if (s->buf_start == NULL) {
  262. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
  263. return -1;
  264. }
  265. s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
  266. if (s->buf_len == NULL) {
  267. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
  268. av_free(s->buf_start);
  269. return -1;
  270. }
  271. for (i = 0; i < req.count; i++) {
  272. struct v4l2_buffer buf;
  273. memset(&buf, 0, sizeof(struct v4l2_buffer));
  274. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  275. buf.memory = V4L2_MEMORY_MMAP;
  276. buf.index = i;
  277. res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
  278. if (res < 0) {
  279. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
  280. return -1;
  281. }
  282. s->buf_len[i] = buf.length;
  283. if (s->buf_len[i] < s->frame_size) {
  284. av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
  285. return -1;
  286. }
  287. s->buf_start[i] = mmap (NULL, buf.length,
  288. PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
  289. if (s->buf_start[i] == MAP_FAILED) {
  290. av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  291. return -1;
  292. }
  293. }
  294. return 0;
  295. }
  296. static int read_init(AVFormatContext *ctx)
  297. {
  298. return -1;
  299. }
  300. static void mmap_release_buffer(AVPacket *pkt)
  301. {
  302. struct v4l2_buffer buf;
  303. int res, fd;
  304. struct buff_data *buf_descriptor = pkt->priv;
  305. if (pkt->data == NULL) {
  306. return;
  307. }
  308. memset(&buf, 0, sizeof(struct v4l2_buffer));
  309. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  310. buf.memory = V4L2_MEMORY_MMAP;
  311. buf.index = buf_descriptor->index;
  312. fd = buf_descriptor->fd;
  313. av_free(buf_descriptor);
  314. res = ioctl (fd, VIDIOC_QBUF, &buf);
  315. if (res < 0) {
  316. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
  317. }
  318. pkt->data = NULL;
  319. pkt->size = 0;
  320. }
  321. static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
  322. {
  323. struct video_data *s = ctx->priv_data;
  324. struct v4l2_buffer buf;
  325. struct buff_data *buf_descriptor;
  326. int res;
  327. memset(&buf, 0, sizeof(struct v4l2_buffer));
  328. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  329. buf.memory = V4L2_MEMORY_MMAP;
  330. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  331. while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  332. if (res < 0) {
  333. if (errno == EAGAIN) {
  334. pkt->size = 0;
  335. return AVERROR(EAGAIN);
  336. }
  337. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
  338. return -1;
  339. }
  340. assert (buf.index < s->buffers);
  341. if (buf.bytesused != s->frame_size) {
  342. av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
  343. return -1;
  344. }
  345. /* Image is at s->buff_start[buf.index] */
  346. pkt->data= s->buf_start[buf.index];
  347. pkt->size = buf.bytesused;
  348. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  349. pkt->destruct = mmap_release_buffer;
  350. buf_descriptor = av_malloc(sizeof(struct buff_data));
  351. if (buf_descriptor == NULL) {
  352. /* Something went wrong... Since av_malloc() failed, we cannot even
  353. * allocate a buffer for memcopying into it
  354. */
  355. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  356. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  357. return -1;
  358. }
  359. buf_descriptor->fd = s->fd;
  360. buf_descriptor->index = buf.index;
  361. pkt->priv = buf_descriptor;
  362. return s->buf_len[buf.index];
  363. }
  364. static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
  365. {
  366. return -1;
  367. }
  368. static int mmap_start(AVFormatContext *ctx)
  369. {
  370. struct video_data *s = ctx->priv_data;
  371. enum v4l2_buf_type type;
  372. int i, res;
  373. for (i = 0; i < s->buffers; i++) {
  374. struct v4l2_buffer buf;
  375. memset(&buf, 0, sizeof(struct v4l2_buffer));
  376. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  377. buf.memory = V4L2_MEMORY_MMAP;
  378. buf.index = i;
  379. res = ioctl (s->fd, VIDIOC_QBUF, &buf);
  380. if (res < 0) {
  381. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
  382. return -1;
  383. }
  384. }
  385. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  386. res = ioctl (s->fd, VIDIOC_STREAMON, &type);
  387. if (res < 0) {
  388. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. static void mmap_close(struct video_data *s)
  394. {
  395. enum v4l2_buf_type type;
  396. int i;
  397. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  398. /* We do not check for the result, because we could
  399. * not do anything about it anyway...
  400. */
  401. ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  402. for (i = 0; i < s->buffers; i++) {
  403. munmap(s->buf_start[i], s->buf_len[i]);
  404. }
  405. av_free(s->buf_start);
  406. av_free(s->buf_len);
  407. }
  408. static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
  409. {
  410. struct video_data *s = s1->priv_data;
  411. struct v4l2_input input;
  412. struct v4l2_standard standard;
  413. int i;
  414. if(ap->channel>=0) {
  415. /* set tv video input */
  416. memset (&input, 0, sizeof (input));
  417. input.index = ap->channel;
  418. if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  419. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
  420. return AVERROR(EIO);
  421. }
  422. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
  423. ap->channel, input.name);
  424. if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
  425. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
  426. ap->channel);
  427. return AVERROR(EIO);
  428. }
  429. }
  430. if(ap->standard) {
  431. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
  432. ap->standard );
  433. /* set tv standard */
  434. memset (&standard, 0, sizeof (standard));
  435. for(i=0;;i++) {
  436. standard.index = i;
  437. if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  438. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  439. ap->standard);
  440. return AVERROR(EIO);
  441. }
  442. if(!strcasecmp(standard.name, ap->standard)) {
  443. break;
  444. }
  445. }
  446. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
  447. ap->standard, (uint64_t)standard.id);
  448. if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  449. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  450. ap->standard);
  451. return AVERROR(EIO);
  452. }
  453. }
  454. return 0;
  455. }
  456. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  457. {
  458. struct video_data *s = s1->priv_data;
  459. AVStream *st;
  460. int width, height;
  461. int res;
  462. uint32_t desired_format, capabilities;
  463. if (ap->width <= 0 || ap->height <= 0) {
  464. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
  465. return -1;
  466. }
  467. width = ap->width;
  468. height = ap->height;
  469. if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
  470. return -1;
  471. st = av_new_stream(s1, 0);
  472. if (!st) {
  473. return AVERROR(ENOMEM);
  474. }
  475. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  476. s->width = width;
  477. s->height = height;
  478. capabilities = 0;
  479. s->fd = device_open(s1, &capabilities);
  480. if (s->fd < 0) {
  481. return AVERROR(EIO);
  482. }
  483. av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
  484. desired_format = fmt_ff2v4l(ap->pix_fmt);
  485. if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
  486. int i, done;
  487. done = 0; i = 0;
  488. while (!done) {
  489. desired_format = fmt_conversion_table[i].v4l2_fmt;
  490. if (device_init(s1, &width, &height, desired_format) < 0) {
  491. desired_format = 0;
  492. i++;
  493. } else {
  494. done = 1;
  495. }
  496. if (i == FF_ARRAY_ELEMS(fmt_conversion_table)) {
  497. done = 1;
  498. }
  499. }
  500. }
  501. if (desired_format == 0) {
  502. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
  503. close(s->fd);
  504. return AVERROR(EIO);
  505. }
  506. s->frame_format = desired_format;
  507. if( v4l2_set_parameters( s1, ap ) < 0 )
  508. return AVERROR(EIO);
  509. st->codec->pix_fmt = fmt_v4l2ff(desired_format);
  510. s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  511. if (capabilities & V4L2_CAP_STREAMING) {
  512. s->io_method = io_mmap;
  513. res = mmap_init(s1);
  514. if (res == 0) {
  515. res = mmap_start(s1);
  516. }
  517. } else {
  518. s->io_method = io_read;
  519. res = read_init(s1);
  520. }
  521. if (res < 0) {
  522. close(s->fd);
  523. return AVERROR(EIO);
  524. }
  525. s->top_field_first = first_field(s->fd);
  526. st->codec->codec_type = CODEC_TYPE_VIDEO;
  527. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  528. st->codec->width = width;
  529. st->codec->height = height;
  530. st->codec->time_base.den = ap->time_base.den;
  531. st->codec->time_base.num = ap->time_base.num;
  532. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  533. return 0;
  534. }
  535. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  536. {
  537. struct video_data *s = s1->priv_data;
  538. int res;
  539. if (s->io_method == io_mmap) {
  540. av_init_packet(pkt);
  541. res = mmap_read_frame(s1, pkt);
  542. } else if (s->io_method == io_read) {
  543. if (av_new_packet(pkt, s->frame_size) < 0)
  544. return AVERROR(EIO);
  545. res = read_frame(s1, pkt);
  546. } else {
  547. return AVERROR(EIO);
  548. }
  549. if (res < 0) {
  550. return res;
  551. }
  552. if (s1->streams[0]->codec->coded_frame) {
  553. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  554. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  555. }
  556. return pkt->size;
  557. }
  558. static int v4l2_read_close(AVFormatContext *s1)
  559. {
  560. struct video_data *s = s1->priv_data;
  561. if (s->io_method == io_mmap) {
  562. mmap_close(s);
  563. }
  564. close(s->fd);
  565. return 0;
  566. }
  567. AVInputFormat v4l2_demuxer = {
  568. "video4linux2",
  569. NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  570. sizeof(struct video_data),
  571. NULL,
  572. v4l2_read_header,
  573. v4l2_read_packet,
  574. v4l2_read_close,
  575. .flags = AVFMT_NOFILE,
  576. };