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