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.

745 lines
23KB

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