v4l2.c 19 KB

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