v4l2.c 17 KB

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