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.

738 lines
22KB

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