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.

729 lines
21KB

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