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.

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