v4l2.c 22 KB

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