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.

716 lines
21KB

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