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.

719 lines
20KB

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