v4l2.c 20 KB

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