v4l2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 = {0};
  142. int res;
  143. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  144. fmt.fmt.pix.width = *width;
  145. fmt.fmt.pix.height = *height;
  146. fmt.fmt.pix.pixelformat = pix_fmt;
  147. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  148. res = ioctl(fd, VIDIOC_S_FMT, &fmt);
  149. if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
  150. 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);
  151. *width = fmt.fmt.pix.width;
  152. *height = fmt.fmt.pix.height;
  153. }
  154. if (pix_fmt != fmt.fmt.pix.pixelformat) {
  155. 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);
  156. res = -1;
  157. }
  158. return res;
  159. }
  160. static int first_field(int fd)
  161. {
  162. int res;
  163. v4l2_std_id std;
  164. res = ioctl(fd, VIDIOC_G_STD, &std);
  165. if (res < 0) {
  166. return 0;
  167. }
  168. if (std & V4L2_STD_NTSC) {
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
  174. {
  175. int i;
  176. for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  177. if ((codec_id == CODEC_ID_NONE ||
  178. fmt_conversion_table[i].codec_id == codec_id) &&
  179. (pix_fmt == PIX_FMT_NONE ||
  180. fmt_conversion_table[i].ff_fmt == pix_fmt)) {
  181. return fmt_conversion_table[i].v4l2_fmt;
  182. }
  183. }
  184. return 0;
  185. }
  186. static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
  187. {
  188. int i;
  189. for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  190. if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
  191. fmt_conversion_table[i].codec_id == codec_id) {
  192. return fmt_conversion_table[i].ff_fmt;
  193. }
  194. }
  195. return PIX_FMT_NONE;
  196. }
  197. static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
  198. {
  199. int i;
  200. for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  201. if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
  202. return fmt_conversion_table[i].codec_id;
  203. }
  204. }
  205. return CODEC_ID_NONE;
  206. }
  207. static int mmap_init(AVFormatContext *ctx)
  208. {
  209. struct video_data *s = ctx->priv_data;
  210. struct v4l2_requestbuffers req = {0};
  211. int i, res;
  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 = {0};
  242. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  243. buf.memory = V4L2_MEMORY_MMAP;
  244. buf.index = i;
  245. res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
  246. if (res < 0) {
  247. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
  248. return AVERROR(errno);
  249. }
  250. s->buf_len[i] = buf.length;
  251. if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
  252. av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
  253. return -1;
  254. }
  255. s->buf_start[i] = mmap (NULL, buf.length,
  256. PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
  257. if (s->buf_start[i] == MAP_FAILED) {
  258. av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  259. return AVERROR(errno);
  260. }
  261. }
  262. return 0;
  263. }
  264. static int read_init(AVFormatContext *ctx)
  265. {
  266. return -1;
  267. }
  268. static void mmap_release_buffer(AVPacket *pkt)
  269. {
  270. struct v4l2_buffer buf = {0};
  271. int res, fd;
  272. struct buff_data *buf_descriptor = pkt->priv;
  273. if (pkt->data == NULL) {
  274. return;
  275. }
  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 = {0};
  292. struct buff_data *buf_descriptor;
  293. int res;
  294. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  295. buf.memory = V4L2_MEMORY_MMAP;
  296. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  297. while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  298. if (res < 0) {
  299. if (errno == EAGAIN) {
  300. pkt->size = 0;
  301. return AVERROR(EAGAIN);
  302. }
  303. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
  304. return AVERROR(errno);
  305. }
  306. assert(buf.index < s->buffers);
  307. if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
  308. av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
  309. return AVERROR_INVALIDDATA;
  310. }
  311. /* Image is at s->buff_start[buf.index] */
  312. pkt->data= s->buf_start[buf.index];
  313. pkt->size = buf.bytesused;
  314. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  315. pkt->destruct = mmap_release_buffer;
  316. buf_descriptor = av_malloc(sizeof(struct buff_data));
  317. if (buf_descriptor == NULL) {
  318. /* Something went wrong... Since av_malloc() failed, we cannot even
  319. * allocate a buffer for memcopying into it
  320. */
  321. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  322. res = ioctl(s->fd, VIDIOC_QBUF, &buf);
  323. return AVERROR(ENOMEM);
  324. }
  325. buf_descriptor->fd = s->fd;
  326. buf_descriptor->index = buf.index;
  327. pkt->priv = buf_descriptor;
  328. return s->buf_len[buf.index];
  329. }
  330. static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
  331. {
  332. return -1;
  333. }
  334. static int mmap_start(AVFormatContext *ctx)
  335. {
  336. struct video_data *s = ctx->priv_data;
  337. enum v4l2_buf_type type;
  338. int i, res;
  339. for (i = 0; i < s->buffers; i++) {
  340. struct v4l2_buffer buf = {0};
  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 = {0};
  377. struct v4l2_standard standard = {0};
  378. struct v4l2_streamparm streamparm = {0};
  379. struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
  380. int i, ret;
  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. input.index = s->channel;
  388. if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  389. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
  390. return AVERROR(EIO);
  391. }
  392. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
  393. s->channel, input.name);
  394. if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
  395. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
  396. s->channel);
  397. return AVERROR(EIO);
  398. }
  399. #if FF_API_FORMAT_PARAMETERS
  400. if (ap->standard) {
  401. av_freep(&s->standard);
  402. s->standard = av_strdup(ap->standard);
  403. }
  404. #endif
  405. if (s->standard) {
  406. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
  407. s->standard);
  408. /* set tv standard */
  409. for (i = 0;; i++) {
  410. standard.index = i;
  411. ret = ioctl(s->fd, VIDIOC_ENUMSTD, &standard);
  412. if (ret < 0 || !strcasecmp(standard.name, s->standard))
  413. break;
  414. }
  415. if (ret < 0) {
  416. av_log(s1, AV_LOG_ERROR, "Unknown standard '%s'\n", s->standard);
  417. return ret;
  418. }
  419. av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
  420. s->standard, (uint64_t)standard.id);
  421. if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  422. av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
  423. s->standard);
  424. return AVERROR(EIO);
  425. }
  426. }
  427. if (ap->time_base.num && ap->time_base.den) {
  428. av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
  429. ap->time_base.num, ap->time_base.den);
  430. tpf->numerator = ap->time_base.num;
  431. tpf->denominator = ap->time_base.den;
  432. if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
  433. av_log(s1, AV_LOG_ERROR,
  434. "ioctl set time per frame(%d/%d) failed\n",
  435. ap->time_base.num, ap->time_base.den);
  436. return AVERROR(EIO);
  437. }
  438. if (ap->time_base.den != tpf->denominator ||
  439. ap->time_base.num != tpf->numerator) {
  440. av_log(s1, AV_LOG_INFO,
  441. "The driver changed the time per frame from %d/%d to %d/%d\n",
  442. ap->time_base.num, ap->time_base.den,
  443. tpf->numerator, tpf->denominator);
  444. }
  445. } else {
  446. /* if timebase value is not set in ap, read the timebase value from the driver */
  447. if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
  448. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno));
  449. return AVERROR(errno);
  450. }
  451. }
  452. ap->time_base.num = tpf->numerator;
  453. ap->time_base.den = tpf->denominator;
  454. return 0;
  455. }
  456. static uint32_t device_try_init(AVFormatContext *s1,
  457. enum PixelFormat pix_fmt,
  458. int *width,
  459. int *height,
  460. enum CodecID *codec_id)
  461. {
  462. uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
  463. if (desired_format == 0 ||
  464. device_init(s1, width, height, desired_format) < 0) {
  465. int i;
  466. desired_format = 0;
  467. for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
  468. if (s1->video_codec_id == CODEC_ID_NONE ||
  469. fmt_conversion_table[i].codec_id == s1->video_codec_id) {
  470. desired_format = fmt_conversion_table[i].v4l2_fmt;
  471. if (device_init(s1, width, height, desired_format) >= 0) {
  472. break;
  473. }
  474. desired_format = 0;
  475. }
  476. }
  477. }
  478. if (desired_format != 0) {
  479. *codec_id = fmt_v4l2codec(desired_format);
  480. assert(*codec_id != CODEC_ID_NONE);
  481. }
  482. return desired_format;
  483. }
  484. static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  485. {
  486. struct video_data *s = s1->priv_data;
  487. AVStream *st;
  488. int res = 0;
  489. uint32_t desired_format, capabilities;
  490. enum CodecID codec_id;
  491. enum PixelFormat pix_fmt = PIX_FMT_NONE;
  492. st = av_new_stream(s1, 0);
  493. if (!st) {
  494. res = AVERROR(ENOMEM);
  495. goto out;
  496. }
  497. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  498. if (s->video_size && (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
  499. av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
  500. goto out;
  501. }
  502. if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
  503. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  504. res = AVERROR(EINVAL);
  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. if (ap->pix_fmt)
  513. pix_fmt = ap->pix_fmt;
  514. #endif
  515. capabilities = 0;
  516. s->fd = device_open(s1, &capabilities);
  517. if (s->fd < 0) {
  518. res = AVERROR(EIO);
  519. goto out;
  520. }
  521. av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
  522. if (!s->width && !s->height) {
  523. struct v4l2_format fmt;
  524. av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
  525. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  526. if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  527. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
  528. res = AVERROR(errno);
  529. goto out;
  530. }
  531. s->width = fmt.fmt.pix.width;
  532. s->height = fmt.fmt.pix.height;
  533. av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
  534. }
  535. desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id);
  536. if (desired_format == 0) {
  537. av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
  538. "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
  539. close(s->fd);
  540. res = AVERROR(EIO);
  541. goto out;
  542. }
  543. if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
  544. goto out;
  545. s->frame_format = desired_format;
  546. if ((res = v4l2_set_parameters(s1, ap) < 0))
  547. goto out;
  548. st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
  549. s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
  550. if (capabilities & V4L2_CAP_STREAMING) {
  551. s->io_method = io_mmap;
  552. res = mmap_init(s1);
  553. if (res == 0) {
  554. res = mmap_start(s1);
  555. }
  556. } else {
  557. s->io_method = io_read;
  558. res = read_init(s1);
  559. }
  560. if (res < 0) {
  561. close(s->fd);
  562. res = AVERROR(EIO);
  563. goto out;
  564. }
  565. s->top_field_first = first_field(s->fd);
  566. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  567. st->codec->codec_id = codec_id;
  568. st->codec->width = s->width;
  569. st->codec->height = s->height;
  570. st->codec->time_base.den = ap->time_base.den;
  571. st->codec->time_base.num = ap->time_base.num;
  572. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  573. out:
  574. av_freep(&s->video_size);
  575. av_freep(&s->pixel_format);
  576. av_freep(&s->standard);
  577. return res;
  578. }
  579. static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
  580. {
  581. struct video_data *s = s1->priv_data;
  582. int res;
  583. if (s->io_method == io_mmap) {
  584. av_init_packet(pkt);
  585. res = mmap_read_frame(s1, pkt);
  586. } else if (s->io_method == io_read) {
  587. if (av_new_packet(pkt, s->frame_size) < 0)
  588. return AVERROR(EIO);
  589. res = read_frame(s1, pkt);
  590. } else {
  591. return AVERROR(EIO);
  592. }
  593. if (res < 0) {
  594. return res;
  595. }
  596. if (s1->streams[0]->codec->coded_frame) {
  597. s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
  598. s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
  599. }
  600. return pkt->size;
  601. }
  602. static int v4l2_read_close(AVFormatContext *s1)
  603. {
  604. struct video_data *s = s1->priv_data;
  605. if (s->io_method == io_mmap) {
  606. mmap_close(s);
  607. }
  608. close(s->fd);
  609. return 0;
  610. }
  611. #define OFFSET(x) offsetof(struct video_data, x)
  612. #define DEC AV_OPT_FLAG_DECODING_PARAM
  613. static const AVOption options[] = {
  614. { "standard", "", OFFSET(standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  615. { "channel", "", OFFSET(channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  616. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  617. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  618. { NULL },
  619. };
  620. static const AVClass v4l2_class = {
  621. .class_name = "V4L2 indev",
  622. .item_name = av_default_item_name,
  623. .option = options,
  624. .version = LIBAVUTIL_VERSION_INT,
  625. };
  626. AVInputFormat ff_v4l2_demuxer = {
  627. "video4linux2",
  628. NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  629. sizeof(struct video_data),
  630. NULL,
  631. v4l2_read_header,
  632. v4l2_read_packet,
  633. v4l2_read_close,
  634. .flags = AVFMT_NOFILE,
  635. .priv_class = &v4l2_class,
  636. };