v4l2.c 19 KB

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