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.

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