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