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.

742 lines
22KB

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