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.

718 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 FF_API_FORMAT_PARAMETERS
  398. if (ap->standard) {
  399. av_freep(&s->standard);
  400. s->standard = av_strdup(ap->standard);
  401. }
  402. #endif
  403. if (s->standard) {
  404. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
  405. s->standard);
  406. /* set tv standard */
  407. memset (&standard, 0, sizeof (standard));
  408. for(i=0;;i++) {
  409. standard.index = i;
  410. if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  411. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  412. s->standard);
  413. return AVERROR(EIO);
  414. }
  415. if (!strcasecmp(standard.name, s->standard)) {
  416. break;
  417. }
  418. }
  419. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
  420. s->standard, (uint64_t)standard.id);
  421. if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  422. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  423. s->standard);
  424. return AVERROR(EIO);
  425. }
  426. }
  427. av_freep(&s->standard);
  428. if (ap->time_base.num && ap->time_base.den) {
  429. av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
  430. ap->time_base.num, ap->time_base.den);
  431. tpf->numerator = ap->time_base.num;
  432. tpf->denominator = ap->time_base.den;
  433. if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
  434. av_log(s1, AV_LOG_ERROR,
  435. "ioctl set time per frame(%d/%d) failed\n",
  436. ap->time_base.num, ap->time_base.den);
  437. return AVERROR(EIO);
  438. }
  439. if (ap->time_base.den != tpf->denominator ||
  440. ap->time_base.num != tpf->numerator) {
  441. av_log(s1, AV_LOG_INFO,
  442. "The driver changed the time per frame from %d/%d to %d/%d\n",
  443. ap->time_base.num, ap->time_base.den,
  444. tpf->numerator, tpf->denominator);
  445. }
  446. } else {
  447. /* if timebase value is not set in ap, read the timebase value from the driver */
  448. if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
  449. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno));
  450. return AVERROR(errno);
  451. }
  452. }
  453. ap->time_base.num = tpf->numerator;
  454. ap->time_base.den = tpf->denominator;
  455. return 0;
  456. }
  457. static uint32_t device_try_init(AVFormatContext *s1,
  458. const AVFormatParameters *ap,
  459. int *width,
  460. int *height,
  461. enum CodecID *codec_id)
  462. {
  463. uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
  464. if (desired_format == 0 ||
  465. device_init(s1, width, height, desired_format) < 0) {
  466. int i;
  467. desired_format = 0;
  468. for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  469. if (s1->video_codec_id == CODEC_ID_NONE ||
  470. fmt_conversion_table[i].codec_id == s1->video_codec_id) {
  471. desired_format = fmt_conversion_table[i].v4l2_fmt;
  472. if (device_init(s1, width, height, desired_format) >= 0) {
  473. break;
  474. }
  475. desired_format = 0;
  476. }
  477. }
  478. }
  479. if (desired_format != 0) {
  480. *codec_id = fmt_v4l2codec(desired_format);
  481. assert(*codec_id != CODEC_ID_NONE);
  482. }
  483. return desired_format;
  484. }
  485. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  486. {
  487. struct video_data *s = s1->priv_data;
  488. AVStream *st;
  489. int res;
  490. uint32_t desired_format, capabilities;
  491. enum CodecID codec_id;
  492. st = av_new_stream(s1, 0);
  493. if (!st) {
  494. return AVERROR(ENOMEM);
  495. }
  496. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  497. s->width = ap->width;
  498. s->height = ap->height;
  499. capabilities = 0;
  500. s->fd = device_open(s1, &capabilities);
  501. if (s->fd < 0) {
  502. return AVERROR(EIO);
  503. }
  504. av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
  505. if (!s->width && !s->height) {
  506. struct v4l2_format fmt;
  507. av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
  508. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  509. if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  510. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
  511. return AVERROR(errno);
  512. }
  513. s->width = fmt.fmt.pix.width;
  514. s->height = fmt.fmt.pix.height;
  515. av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
  516. }
  517. desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
  518. if (desired_format == 0) {
  519. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
  520. "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
  521. close(s->fd);
  522. return AVERROR(EIO);
  523. }
  524. if (av_image_check_size(s->width, s->height, 0, s1) < 0)
  525. return AVERROR(EINVAL);
  526. s->frame_format = desired_format;
  527. if (v4l2_set_parameters(s1, ap) < 0)
  528. return AVERROR(EIO);
  529. st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
  530. s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
  531. if (capabilities & V4L2_CAP_STREAMING) {
  532. s->io_method = io_mmap;
  533. res = mmap_init(s1);
  534. if (res == 0) {
  535. res = mmap_start(s1);
  536. }
  537. } else {
  538. s->io_method = io_read;
  539. res = read_init(s1);
  540. }
  541. if (res < 0) {
  542. close(s->fd);
  543. return AVERROR(EIO);
  544. }
  545. s->top_field_first = first_field(s->fd);
  546. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  547. st->codec->codec_id = codec_id;
  548. st->codec->width = s->width;
  549. st->codec->height = s->height;
  550. st->codec->time_base.den = ap->time_base.den;
  551. st->codec->time_base.num = ap->time_base.num;
  552. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  553. return 0;
  554. }
  555. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  556. {
  557. struct video_data *s = s1->priv_data;
  558. int res;
  559. if (s->io_method == io_mmap) {
  560. av_init_packet(pkt);
  561. res = mmap_read_frame(s1, pkt);
  562. } else if (s->io_method == io_read) {
  563. if (av_new_packet(pkt, s->frame_size) < 0)
  564. return AVERROR(EIO);
  565. res = read_frame(s1, pkt);
  566. } else {
  567. return AVERROR(EIO);
  568. }
  569. if (res < 0) {
  570. return res;
  571. }
  572. if (s1->streams[0]->codec->coded_frame) {
  573. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  574. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  575. }
  576. return pkt->size;
  577. }
  578. static int v4l2_read_close(AVFormatContext *s1)
  579. {
  580. struct video_data *s = s1->priv_data;
  581. if (s->io_method == io_mmap) {
  582. mmap_close(s);
  583. }
  584. close(s->fd);
  585. return 0;
  586. }
  587. static const AVOption options[] = {
  588. { "standard", "", offsetof(struct video_data, standard), FF_OPT_TYPE_STRING, {.str = "NTSC" }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  589. { NULL },
  590. };
  591. static const AVClass v4l2_class = {
  592. .class_name = "V4L2 indev",
  593. .item_name = av_default_item_name,
  594. .option = options,
  595. .version = LIBAVUTIL_VERSION_INT,
  596. };
  597. AVInputFormat ff_v4l2_demuxer = {
  598. "video4linux2",
  599. NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  600. sizeof(struct video_data),
  601. NULL,
  602. v4l2_read_header,
  603. v4l2_read_packet,
  604. v4l2_read_close,
  605. .flags = AVFMT_NOFILE,
  606. .priv_class = &v4l2_class,
  607. };