v4l2.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html)
  27. *
  28. * Thanks to Michael Niedermayer for providing the mapping between
  29. * V4L2_PIX_FMT_* and AV_PIX_FMT_*
  30. */
  31. #include <stdatomic.h>
  32. #include "v4l2-common.h"
  33. #include <dirent.h>
  34. #if CONFIG_LIBV4L2
  35. #include <libv4l2.h>
  36. #endif
  37. static const int desired_video_buffers = 256;
  38. #define V4L_ALLFORMATS 3
  39. #define V4L_RAWFORMATS 1
  40. #define V4L_COMPFORMATS 2
  41. /**
  42. * Return timestamps to the user exactly as returned by the kernel
  43. */
  44. #define V4L_TS_DEFAULT 0
  45. /**
  46. * Autodetect the kind of timestamps returned by the kernel and convert to
  47. * absolute (wall clock) timestamps.
  48. */
  49. #define V4L_TS_ABS 1
  50. /**
  51. * Assume kernel timestamps are from the monotonic clock and convert to
  52. * absolute timestamps.
  53. */
  54. #define V4L_TS_MONO2ABS 2
  55. /**
  56. * Once the kind of timestamps returned by the kernel have been detected,
  57. * the value of the timefilter (NULL or not) determines whether a conversion
  58. * takes place.
  59. */
  60. #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
  61. struct video_data {
  62. AVClass *class;
  63. int fd;
  64. int pixelformat; /* V4L2_PIX_FMT_* */
  65. int width, height;
  66. int frame_size;
  67. int interlaced;
  68. int top_field_first;
  69. int ts_mode;
  70. TimeFilter *timefilter;
  71. int64_t last_time_m;
  72. int buffers;
  73. atomic_int buffers_queued;
  74. void **buf_start;
  75. unsigned int *buf_len;
  76. char *standard;
  77. v4l2_std_id std_id;
  78. int channel;
  79. char *pixel_format; /**< Set by a private option. */
  80. int list_format; /**< Set by a private option. */
  81. int list_standard; /**< Set by a private option. */
  82. char *framerate; /**< Set by a private option. */
  83. int use_libv4l2;
  84. int (*open_f)(const char *file, int oflag, ...);
  85. int (*close_f)(int fd);
  86. int (*dup_f)(int fd);
  87. int (*ioctl_f)(int fd, unsigned long int request, ...);
  88. ssize_t (*read_f)(int fd, void *buffer, size_t n);
  89. void *(*mmap_f)(void *start, size_t length, int prot, int flags, int fd, int64_t offset);
  90. int (*munmap_f)(void *_start, size_t length);
  91. };
  92. struct buff_data {
  93. struct video_data *s;
  94. int index;
  95. };
  96. static int device_open(AVFormatContext *ctx)
  97. {
  98. struct video_data *s = ctx->priv_data;
  99. struct v4l2_capability cap;
  100. int fd;
  101. int err;
  102. int flags = O_RDWR;
  103. #define SET_WRAPPERS(prefix) do { \
  104. s->open_f = prefix ## open; \
  105. s->close_f = prefix ## close; \
  106. s->dup_f = prefix ## dup; \
  107. s->ioctl_f = prefix ## ioctl; \
  108. s->read_f = prefix ## read; \
  109. s->mmap_f = prefix ## mmap; \
  110. s->munmap_f = prefix ## munmap; \
  111. } while (0)
  112. if (s->use_libv4l2) {
  113. #if CONFIG_LIBV4L2
  114. SET_WRAPPERS(v4l2_);
  115. #else
  116. av_log(ctx, AV_LOG_ERROR, "libavdevice is not built with libv4l2 support.\n");
  117. return AVERROR(EINVAL);
  118. #endif
  119. } else {
  120. SET_WRAPPERS();
  121. }
  122. #define v4l2_open s->open_f
  123. #define v4l2_close s->close_f
  124. #define v4l2_dup s->dup_f
  125. #define v4l2_ioctl s->ioctl_f
  126. #define v4l2_read s->read_f
  127. #define v4l2_mmap s->mmap_f
  128. #define v4l2_munmap s->munmap_f
  129. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  130. flags |= O_NONBLOCK;
  131. }
  132. fd = v4l2_open(ctx->filename, flags, 0);
  133. if (fd < 0) {
  134. err = AVERROR(errno);
  135. av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s: %s\n",
  136. ctx->filename, av_err2str(err));
  137. return err;
  138. }
  139. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
  140. err = AVERROR(errno);
  141. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
  142. av_err2str(err));
  143. goto fail;
  144. }
  145. av_log(ctx, AV_LOG_VERBOSE, "fd:%d capabilities:%x\n",
  146. fd, cap.capabilities);
  147. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  148. av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
  149. err = AVERROR(ENODEV);
  150. goto fail;
  151. }
  152. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  153. av_log(ctx, AV_LOG_ERROR,
  154. "The device does not support the streaming I/O method.\n");
  155. err = AVERROR(ENOSYS);
  156. goto fail;
  157. }
  158. return fd;
  159. fail:
  160. v4l2_close(fd);
  161. return err;
  162. }
  163. static int device_init(AVFormatContext *ctx, int *width, int *height,
  164. uint32_t pixelformat)
  165. {
  166. struct video_data *s = ctx->priv_data;
  167. struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
  168. int res = 0;
  169. fmt.fmt.pix.width = *width;
  170. fmt.fmt.pix.height = *height;
  171. fmt.fmt.pix.pixelformat = pixelformat;
  172. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  173. /* Some drivers will fail and return EINVAL when the pixelformat
  174. is not supported (even if type field is valid and supported) */
  175. if (v4l2_ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0)
  176. res = AVERROR(errno);
  177. if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
  178. av_log(ctx, AV_LOG_INFO,
  179. "The V4L2 driver changed the video from %dx%d to %dx%d\n",
  180. *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
  181. *width = fmt.fmt.pix.width;
  182. *height = fmt.fmt.pix.height;
  183. }
  184. if (pixelformat != fmt.fmt.pix.pixelformat) {
  185. av_log(ctx, AV_LOG_DEBUG,
  186. "The V4L2 driver changed the pixel format "
  187. "from 0x%08X to 0x%08X\n",
  188. pixelformat, fmt.fmt.pix.pixelformat);
  189. res = AVERROR(EINVAL);
  190. }
  191. if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
  192. av_log(ctx, AV_LOG_DEBUG,
  193. "The V4L2 driver is using the interlaced mode\n");
  194. s->interlaced = 1;
  195. }
  196. return res;
  197. }
  198. static int first_field(const struct video_data *s)
  199. {
  200. int res;
  201. v4l2_std_id std;
  202. res = v4l2_ioctl(s->fd, VIDIOC_G_STD, &std);
  203. if (res < 0)
  204. return 0;
  205. if (std & V4L2_STD_NTSC)
  206. return 0;
  207. return 1;
  208. }
  209. #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
  210. static void list_framesizes(AVFormatContext *ctx, uint32_t pixelformat)
  211. {
  212. const struct video_data *s = ctx->priv_data;
  213. struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
  214. while(!v4l2_ioctl(s->fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
  215. switch (vfse.type) {
  216. case V4L2_FRMSIZE_TYPE_DISCRETE:
  217. av_log(ctx, AV_LOG_INFO, " %ux%u",
  218. vfse.discrete.width, vfse.discrete.height);
  219. break;
  220. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  221. case V4L2_FRMSIZE_TYPE_STEPWISE:
  222. av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
  223. vfse.stepwise.min_width,
  224. vfse.stepwise.max_width,
  225. vfse.stepwise.step_width,
  226. vfse.stepwise.min_height,
  227. vfse.stepwise.max_height,
  228. vfse.stepwise.step_height);
  229. }
  230. vfse.index++;
  231. }
  232. }
  233. #endif
  234. static void list_formats(AVFormatContext *ctx, int type)
  235. {
  236. const struct video_data *s = ctx->priv_data;
  237. struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
  238. while(!v4l2_ioctl(s->fd, VIDIOC_ENUM_FMT, &vfd)) {
  239. enum AVCodecID codec_id = ff_fmt_v4l2codec(vfd.pixelformat);
  240. enum AVPixelFormat pix_fmt = ff_fmt_v4l2ff(vfd.pixelformat, codec_id);
  241. vfd.index++;
  242. if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
  243. type & V4L_RAWFORMATS) {
  244. const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
  245. av_log(ctx, AV_LOG_INFO, "Raw : %11s : %20s :",
  246. fmt_name ? fmt_name : "Unsupported",
  247. vfd.description);
  248. } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
  249. type & V4L_COMPFORMATS) {
  250. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  251. av_log(ctx, AV_LOG_INFO, "Compressed: %11s : %20s :",
  252. desc ? desc->name : "Unsupported",
  253. vfd.description);
  254. } else {
  255. continue;
  256. }
  257. #ifdef V4L2_FMT_FLAG_EMULATED
  258. if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
  259. av_log(ctx, AV_LOG_INFO, " Emulated :");
  260. #endif
  261. #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
  262. list_framesizes(ctx, vfd.pixelformat);
  263. #endif
  264. av_log(ctx, AV_LOG_INFO, "\n");
  265. }
  266. }
  267. static void list_standards(AVFormatContext *ctx)
  268. {
  269. int ret;
  270. struct video_data *s = ctx->priv_data;
  271. struct v4l2_standard standard;
  272. if (s->std_id == 0)
  273. return;
  274. for (standard.index = 0; ; standard.index++) {
  275. if (v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  276. ret = AVERROR(errno);
  277. if (ret == AVERROR(EINVAL)) {
  278. break;
  279. } else {
  280. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMSTD): %s\n", av_err2str(ret));
  281. return;
  282. }
  283. }
  284. av_log(ctx, AV_LOG_INFO, "%2d, %16"PRIx64", %s\n",
  285. standard.index, (uint64_t)standard.id, standard.name);
  286. }
  287. }
  288. static int mmap_init(AVFormatContext *ctx)
  289. {
  290. int i, res;
  291. struct video_data *s = ctx->priv_data;
  292. struct v4l2_requestbuffers req = {
  293. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  294. .count = desired_video_buffers,
  295. .memory = V4L2_MEMORY_MMAP
  296. };
  297. if (v4l2_ioctl(s->fd, VIDIOC_REQBUFS, &req) < 0) {
  298. res = AVERROR(errno);
  299. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS): %s\n", av_err2str(res));
  300. return res;
  301. }
  302. if (req.count < 2) {
  303. av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
  304. return AVERROR(ENOMEM);
  305. }
  306. s->buffers = req.count;
  307. s->buf_start = av_malloc_array(s->buffers, sizeof(void *));
  308. if (!s->buf_start) {
  309. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
  310. return AVERROR(ENOMEM);
  311. }
  312. s->buf_len = av_malloc_array(s->buffers, sizeof(unsigned int));
  313. if (!s->buf_len) {
  314. av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
  315. av_freep(&s->buf_start);
  316. return AVERROR(ENOMEM);
  317. }
  318. for (i = 0; i < req.count; i++) {
  319. struct v4l2_buffer buf = {
  320. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  321. .index = i,
  322. .memory = V4L2_MEMORY_MMAP
  323. };
  324. if (v4l2_ioctl(s->fd, VIDIOC_QUERYBUF, &buf) < 0) {
  325. res = AVERROR(errno);
  326. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF): %s\n", av_err2str(res));
  327. return res;
  328. }
  329. s->buf_len[i] = buf.length;
  330. if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
  331. av_log(ctx, AV_LOG_ERROR,
  332. "buf_len[%d] = %d < expected frame size %d\n",
  333. i, s->buf_len[i], s->frame_size);
  334. return AVERROR(ENOMEM);
  335. }
  336. s->buf_start[i] = v4l2_mmap(NULL, buf.length,
  337. PROT_READ | PROT_WRITE, MAP_SHARED,
  338. s->fd, buf.m.offset);
  339. if (s->buf_start[i] == MAP_FAILED) {
  340. res = AVERROR(errno);
  341. av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", av_err2str(res));
  342. return res;
  343. }
  344. }
  345. return 0;
  346. }
  347. static int enqueue_buffer(struct video_data *s, struct v4l2_buffer *buf)
  348. {
  349. int res = 0;
  350. if (v4l2_ioctl(s->fd, VIDIOC_QBUF, buf) < 0) {
  351. res = AVERROR(errno);
  352. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", av_err2str(res));
  353. } else {
  354. atomic_fetch_add(&s->buffers_queued, 1);
  355. }
  356. return res;
  357. }
  358. static void mmap_release_buffer(void *opaque, uint8_t *data)
  359. {
  360. struct v4l2_buffer buf = { 0 };
  361. struct buff_data *buf_descriptor = opaque;
  362. struct video_data *s = buf_descriptor->s;
  363. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  364. buf.memory = V4L2_MEMORY_MMAP;
  365. buf.index = buf_descriptor->index;
  366. av_free(buf_descriptor);
  367. enqueue_buffer(s, &buf);
  368. }
  369. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  370. static int64_t av_gettime_monotonic(void)
  371. {
  372. return av_gettime_relative();
  373. }
  374. #endif
  375. static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
  376. {
  377. struct video_data *s = ctx->priv_data;
  378. int64_t now;
  379. now = av_gettime();
  380. if (s->ts_mode == V4L_TS_ABS &&
  381. ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE) {
  382. av_log(ctx, AV_LOG_INFO, "Detected absolute timestamps\n");
  383. s->ts_mode = V4L_TS_CONVERT_READY;
  384. return 0;
  385. }
  386. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  387. if (ctx->streams[0]->avg_frame_rate.num) {
  388. now = av_gettime_monotonic();
  389. if (s->ts_mode == V4L_TS_MONO2ABS ||
  390. (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE)) {
  391. AVRational tb = {AV_TIME_BASE, 1};
  392. int64_t period = av_rescale_q(1, tb, ctx->streams[0]->avg_frame_rate);
  393. av_log(ctx, AV_LOG_INFO, "Detected monotonic timestamps, converting\n");
  394. /* microseconds instead of seconds, MHz instead of Hz */
  395. s->timefilter = ff_timefilter_new(1, period, 1.0E-6);
  396. if (!s->timefilter)
  397. return AVERROR(ENOMEM);
  398. s->ts_mode = V4L_TS_CONVERT_READY;
  399. return 0;
  400. }
  401. }
  402. #endif
  403. av_log(ctx, AV_LOG_ERROR, "Unknown timestamps\n");
  404. return AVERROR(EIO);
  405. }
  406. static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
  407. {
  408. struct video_data *s = ctx->priv_data;
  409. if (s->ts_mode) {
  410. int r = init_convert_timestamp(ctx, *ts);
  411. if (r < 0)
  412. return r;
  413. }
  414. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  415. if (s->timefilter) {
  416. int64_t nowa = av_gettime();
  417. int64_t nowm = av_gettime_monotonic();
  418. ff_timefilter_update(s->timefilter, nowa, nowm - s->last_time_m);
  419. s->last_time_m = nowm;
  420. *ts = ff_timefilter_eval(s->timefilter, *ts - nowm);
  421. }
  422. #endif
  423. return 0;
  424. }
  425. static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
  426. {
  427. struct video_data *s = ctx->priv_data;
  428. struct v4l2_buffer buf = {
  429. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  430. .memory = V4L2_MEMORY_MMAP
  431. };
  432. int res;
  433. pkt->size = 0;
  434. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  435. while ((res = v4l2_ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  436. if (res < 0) {
  437. if (errno == EAGAIN)
  438. return AVERROR(EAGAIN);
  439. res = AVERROR(errno);
  440. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
  441. av_err2str(res));
  442. return res;
  443. }
  444. if (buf.index >= s->buffers) {
  445. av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n");
  446. return AVERROR(EINVAL);
  447. }
  448. atomic_fetch_add(&s->buffers_queued, -1);
  449. // always keep at least one buffer queued
  450. av_assert0(atomic_load(&s->buffers_queued) >= 1);
  451. #ifdef V4L2_BUF_FLAG_ERROR
  452. if (buf.flags & V4L2_BUF_FLAG_ERROR) {
  453. av_log(ctx, AV_LOG_WARNING,
  454. "Dequeued v4l2 buffer contains corrupted data (%d bytes).\n",
  455. buf.bytesused);
  456. buf.bytesused = 0;
  457. } else
  458. #endif
  459. {
  460. /* CPIA is a compressed format and we don't know the exact number of bytes
  461. * used by a frame, so set it here as the driver announces it. */
  462. if (ctx->video_codec_id == AV_CODEC_ID_CPIA)
  463. s->frame_size = buf.bytesused;
  464. if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
  465. av_log(ctx, AV_LOG_ERROR,
  466. "Dequeued v4l2 buffer contains %d bytes, but %d were expected. Flags: 0x%08X.\n",
  467. buf.bytesused, s->frame_size, buf.flags);
  468. enqueue_buffer(s, &buf);
  469. return AVERROR_INVALIDDATA;
  470. }
  471. }
  472. /* Image is at s->buff_start[buf.index] */
  473. if (atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
  474. /* when we start getting low on queued buffers, fall back on copying data */
  475. res = av_new_packet(pkt, buf.bytesused);
  476. if (res < 0) {
  477. av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
  478. enqueue_buffer(s, &buf);
  479. return res;
  480. }
  481. memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
  482. res = enqueue_buffer(s, &buf);
  483. if (res) {
  484. av_packet_unref(pkt);
  485. return res;
  486. }
  487. } else {
  488. struct buff_data *buf_descriptor;
  489. pkt->data = s->buf_start[buf.index];
  490. pkt->size = buf.bytesused;
  491. buf_descriptor = av_malloc(sizeof(struct buff_data));
  492. if (!buf_descriptor) {
  493. /* Something went wrong... Since av_malloc() failed, we cannot even
  494. * allocate a buffer for memcpying into it
  495. */
  496. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  497. enqueue_buffer(s, &buf);
  498. return AVERROR(ENOMEM);
  499. }
  500. buf_descriptor->index = buf.index;
  501. buf_descriptor->s = s;
  502. pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer,
  503. buf_descriptor, 0);
  504. if (!pkt->buf) {
  505. av_log(ctx, AV_LOG_ERROR, "Failed to create a buffer\n");
  506. enqueue_buffer(s, &buf);
  507. av_freep(&buf_descriptor);
  508. return AVERROR(ENOMEM);
  509. }
  510. }
  511. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  512. convert_timestamp(ctx, &pkt->pts);
  513. return pkt->size;
  514. }
  515. static int mmap_start(AVFormatContext *ctx)
  516. {
  517. struct video_data *s = ctx->priv_data;
  518. enum v4l2_buf_type type;
  519. int i, res;
  520. for (i = 0; i < s->buffers; i++) {
  521. struct v4l2_buffer buf = {
  522. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  523. .index = i,
  524. .memory = V4L2_MEMORY_MMAP
  525. };
  526. if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) < 0) {
  527. res = AVERROR(errno);
  528. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
  529. av_err2str(res));
  530. return res;
  531. }
  532. }
  533. atomic_store(&s->buffers_queued, s->buffers);
  534. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  535. if (v4l2_ioctl(s->fd, VIDIOC_STREAMON, &type) < 0) {
  536. res = AVERROR(errno);
  537. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
  538. av_err2str(res));
  539. return res;
  540. }
  541. return 0;
  542. }
  543. static void mmap_close(struct video_data *s)
  544. {
  545. enum v4l2_buf_type type;
  546. int i;
  547. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  548. /* We do not check for the result, because we could
  549. * not do anything about it anyway...
  550. */
  551. v4l2_ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  552. for (i = 0; i < s->buffers; i++) {
  553. v4l2_munmap(s->buf_start[i], s->buf_len[i]);
  554. }
  555. av_freep(&s->buf_start);
  556. av_freep(&s->buf_len);
  557. }
  558. static int v4l2_set_parameters(AVFormatContext *ctx)
  559. {
  560. struct video_data *s = ctx->priv_data;
  561. struct v4l2_standard standard = { 0 };
  562. struct v4l2_streamparm streamparm = { 0 };
  563. struct v4l2_fract *tpf;
  564. AVRational framerate_q = { 0 };
  565. int i, ret;
  566. if (s->framerate &&
  567. (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
  568. av_log(ctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
  569. s->framerate);
  570. return ret;
  571. }
  572. if (s->standard) {
  573. if (s->std_id) {
  574. ret = 0;
  575. av_log(ctx, AV_LOG_DEBUG, "Setting standard: %s\n", s->standard);
  576. /* set tv standard */
  577. for (i = 0; ; i++) {
  578. standard.index = i;
  579. if (v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  580. ret = AVERROR(errno);
  581. break;
  582. }
  583. if (!av_strcasecmp(standard.name, s->standard))
  584. break;
  585. }
  586. if (ret < 0) {
  587. av_log(ctx, AV_LOG_ERROR, "Unknown or unsupported standard '%s'\n", s->standard);
  588. return ret;
  589. }
  590. if (v4l2_ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  591. ret = AVERROR(errno);
  592. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_STD): %s\n", av_err2str(ret));
  593. return ret;
  594. }
  595. } else {
  596. av_log(ctx, AV_LOG_WARNING,
  597. "This device does not support any standard\n");
  598. }
  599. }
  600. /* get standard */
  601. if (v4l2_ioctl(s->fd, VIDIOC_G_STD, &s->std_id) == 0) {
  602. tpf = &standard.frameperiod;
  603. for (i = 0; ; i++) {
  604. standard.index = i;
  605. if (v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  606. ret = AVERROR(errno);
  607. if (ret == AVERROR(EINVAL)
  608. #ifdef ENODATA
  609. || ret == AVERROR(ENODATA)
  610. #endif
  611. ) {
  612. tpf = &streamparm.parm.capture.timeperframe;
  613. break;
  614. }
  615. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMSTD): %s\n", av_err2str(ret));
  616. return ret;
  617. }
  618. if (standard.id == s->std_id) {
  619. av_log(ctx, AV_LOG_DEBUG,
  620. "Current standard: %s, id: %"PRIx64", frameperiod: %d/%d\n",
  621. standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
  622. break;
  623. }
  624. }
  625. } else {
  626. tpf = &streamparm.parm.capture.timeperframe;
  627. }
  628. streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  629. if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
  630. ret = AVERROR(errno);
  631. av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
  632. } else if (framerate_q.num && framerate_q.den) {
  633. if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
  634. tpf = &streamparm.parm.capture.timeperframe;
  635. av_log(ctx, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
  636. framerate_q.den, framerate_q.num);
  637. tpf->numerator = framerate_q.den;
  638. tpf->denominator = framerate_q.num;
  639. if (v4l2_ioctl(s->fd, VIDIOC_S_PARM, &streamparm) < 0) {
  640. ret = AVERROR(errno);
  641. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_PARM): %s\n",
  642. av_err2str(ret));
  643. return ret;
  644. }
  645. if (framerate_q.num != tpf->denominator ||
  646. framerate_q.den != tpf->numerator) {
  647. av_log(ctx, AV_LOG_INFO,
  648. "The driver changed the time per frame from "
  649. "%d/%d to %d/%d\n",
  650. framerate_q.den, framerate_q.num,
  651. tpf->numerator, tpf->denominator);
  652. }
  653. } else {
  654. av_log(ctx, AV_LOG_WARNING,
  655. "The driver does not permit changing the time per frame\n");
  656. }
  657. }
  658. if (tpf->denominator > 0 && tpf->numerator > 0) {
  659. ctx->streams[0]->avg_frame_rate.num = tpf->denominator;
  660. ctx->streams[0]->avg_frame_rate.den = tpf->numerator;
  661. ctx->streams[0]->r_frame_rate = ctx->streams[0]->avg_frame_rate;
  662. } else
  663. av_log(ctx, AV_LOG_WARNING, "Time per frame unknown\n");
  664. return 0;
  665. }
  666. static int device_try_init(AVFormatContext *ctx,
  667. enum AVPixelFormat pix_fmt,
  668. int *width,
  669. int *height,
  670. uint32_t *desired_format,
  671. enum AVCodecID *codec_id)
  672. {
  673. int ret, i;
  674. *desired_format = ff_fmt_ff2v4l(pix_fmt, ctx->video_codec_id);
  675. if (*desired_format) {
  676. ret = device_init(ctx, width, height, *desired_format);
  677. if (ret < 0) {
  678. *desired_format = 0;
  679. if (ret != AVERROR(EINVAL))
  680. return ret;
  681. }
  682. }
  683. if (!*desired_format) {
  684. for (i = 0; ff_fmt_conversion_table[i].codec_id != AV_CODEC_ID_NONE; i++) {
  685. if (ctx->video_codec_id == AV_CODEC_ID_NONE ||
  686. ff_fmt_conversion_table[i].codec_id == ctx->video_codec_id) {
  687. av_log(ctx, AV_LOG_DEBUG, "Trying to set codec:%s pix_fmt:%s\n",
  688. avcodec_get_name(ff_fmt_conversion_table[i].codec_id),
  689. (char *)av_x_if_null(av_get_pix_fmt_name(ff_fmt_conversion_table[i].ff_fmt), "none"));
  690. *desired_format = ff_fmt_conversion_table[i].v4l2_fmt;
  691. ret = device_init(ctx, width, height, *desired_format);
  692. if (ret >= 0)
  693. break;
  694. else if (ret != AVERROR(EINVAL))
  695. return ret;
  696. *desired_format = 0;
  697. }
  698. }
  699. if (*desired_format == 0) {
  700. av_log(ctx, AV_LOG_ERROR, "Cannot find a proper format for "
  701. "codec '%s' (id %d), pixel format '%s' (id %d)\n",
  702. avcodec_get_name(ctx->video_codec_id), ctx->video_codec_id,
  703. (char *)av_x_if_null(av_get_pix_fmt_name(pix_fmt), "none"), pix_fmt);
  704. ret = AVERROR(EINVAL);
  705. }
  706. }
  707. *codec_id = ff_fmt_v4l2codec(*desired_format);
  708. av_assert0(*codec_id != AV_CODEC_ID_NONE);
  709. return ret;
  710. }
  711. static int v4l2_read_probe(AVProbeData *p)
  712. {
  713. if (av_strstart(p->filename, "/dev/video", NULL))
  714. return AVPROBE_SCORE_MAX - 1;
  715. return 0;
  716. }
  717. static int v4l2_read_header(AVFormatContext *ctx)
  718. {
  719. struct video_data *s = ctx->priv_data;
  720. AVStream *st;
  721. int res = 0;
  722. uint32_t desired_format;
  723. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  724. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  725. struct v4l2_input input = { 0 };
  726. st = avformat_new_stream(ctx, NULL);
  727. if (!st)
  728. return AVERROR(ENOMEM);
  729. #if CONFIG_LIBV4L2
  730. /* silence libv4l2 logging. if fopen() fails v4l2_log_file will be NULL
  731. and errors will get sent to stderr */
  732. if (s->use_libv4l2)
  733. v4l2_log_file = fopen("/dev/null", "w");
  734. #endif
  735. s->fd = device_open(ctx);
  736. if (s->fd < 0)
  737. return s->fd;
  738. if (s->channel != -1) {
  739. /* set video input */
  740. av_log(ctx, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
  741. if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
  742. res = AVERROR(errno);
  743. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
  744. goto fail;
  745. }
  746. } else {
  747. /* get current video input */
  748. if (v4l2_ioctl(s->fd, VIDIOC_G_INPUT, &s->channel) < 0) {
  749. res = AVERROR(errno);
  750. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
  751. goto fail;
  752. }
  753. }
  754. /* enum input */
  755. input.index = s->channel;
  756. if (v4l2_ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  757. res = AVERROR(errno);
  758. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMINPUT): %s\n", av_err2str(res));
  759. goto fail;
  760. }
  761. s->std_id = input.std;
  762. av_log(ctx, AV_LOG_DEBUG, "Current input_channel: %d, input_name: %s, input_std: %"PRIx64"\n",
  763. s->channel, input.name, (uint64_t)input.std);
  764. if (s->list_format) {
  765. list_formats(ctx, s->list_format);
  766. res = AVERROR_EXIT;
  767. goto fail;
  768. }
  769. if (s->list_standard) {
  770. list_standards(ctx);
  771. res = AVERROR_EXIT;
  772. goto fail;
  773. }
  774. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  775. if (s->pixel_format) {
  776. AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
  777. if (codec)
  778. ctx->video_codec_id = codec->id;
  779. pix_fmt = av_get_pix_fmt(s->pixel_format);
  780. if (pix_fmt == AV_PIX_FMT_NONE && !codec) {
  781. av_log(ctx, AV_LOG_ERROR, "No such input format: %s.\n",
  782. s->pixel_format);
  783. res = AVERROR(EINVAL);
  784. goto fail;
  785. }
  786. }
  787. if (!s->width && !s->height) {
  788. struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
  789. av_log(ctx, AV_LOG_VERBOSE,
  790. "Querying the device for the current frame size\n");
  791. if (v4l2_ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  792. res = AVERROR(errno);
  793. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
  794. av_err2str(res));
  795. goto fail;
  796. }
  797. s->width = fmt.fmt.pix.width;
  798. s->height = fmt.fmt.pix.height;
  799. av_log(ctx, AV_LOG_VERBOSE,
  800. "Setting frame size to %dx%d\n", s->width, s->height);
  801. }
  802. res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &desired_format, &codec_id);
  803. if (res < 0)
  804. goto fail;
  805. /* If no pixel_format was specified, the codec_id was not known up
  806. * until now. Set video_codec_id in the context, as codec_id will
  807. * not be available outside this function
  808. */
  809. if (codec_id != AV_CODEC_ID_NONE && ctx->video_codec_id == AV_CODEC_ID_NONE)
  810. ctx->video_codec_id = codec_id;
  811. if ((res = av_image_check_size(s->width, s->height, 0, ctx)) < 0)
  812. goto fail;
  813. s->pixelformat = desired_format;
  814. if ((res = v4l2_set_parameters(ctx)) < 0)
  815. goto fail;
  816. st->codecpar->format = ff_fmt_v4l2ff(desired_format, codec_id);
  817. if (st->codecpar->format != AV_PIX_FMT_NONE)
  818. s->frame_size = av_image_get_buffer_size(st->codecpar->format,
  819. s->width, s->height, 1);
  820. if ((res = mmap_init(ctx)) ||
  821. (res = mmap_start(ctx)) < 0)
  822. goto fail;
  823. s->top_field_first = first_field(s);
  824. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  825. st->codecpar->codec_id = codec_id;
  826. if (codec_id == AV_CODEC_ID_RAWVIDEO)
  827. st->codecpar->codec_tag =
  828. avcodec_pix_fmt_to_codec_tag(st->codecpar->format);
  829. else if (codec_id == AV_CODEC_ID_H264) {
  830. st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
  831. }
  832. if (desired_format == V4L2_PIX_FMT_YVU420)
  833. st->codecpar->codec_tag = MKTAG('Y', 'V', '1', '2');
  834. else if (desired_format == V4L2_PIX_FMT_YVU410)
  835. st->codecpar->codec_tag = MKTAG('Y', 'V', 'U', '9');
  836. st->codecpar->width = s->width;
  837. st->codecpar->height = s->height;
  838. if (st->avg_frame_rate.den)
  839. st->codecpar->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
  840. return 0;
  841. fail:
  842. v4l2_close(s->fd);
  843. return res;
  844. }
  845. static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  846. {
  847. #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
  848. FF_DISABLE_DEPRECATION_WARNINGS
  849. struct video_data *s = ctx->priv_data;
  850. AVFrame *frame = ctx->streams[0]->codec->coded_frame;
  851. FF_ENABLE_DEPRECATION_WARNINGS
  852. #endif
  853. int res;
  854. if ((res = mmap_read_frame(ctx, pkt)) < 0) {
  855. return res;
  856. }
  857. #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
  858. FF_DISABLE_DEPRECATION_WARNINGS
  859. if (frame && s->interlaced) {
  860. frame->interlaced_frame = 1;
  861. frame->top_field_first = s->top_field_first;
  862. }
  863. FF_ENABLE_DEPRECATION_WARNINGS
  864. #endif
  865. return pkt->size;
  866. }
  867. static int v4l2_read_close(AVFormatContext *ctx)
  868. {
  869. struct video_data *s = ctx->priv_data;
  870. if (atomic_load(&s->buffers_queued) != s->buffers)
  871. av_log(ctx, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
  872. "close.\n");
  873. mmap_close(s);
  874. v4l2_close(s->fd);
  875. return 0;
  876. }
  877. static int v4l2_is_v4l_dev(const char *name)
  878. {
  879. return !strncmp(name, "video", 5) ||
  880. !strncmp(name, "radio", 5) ||
  881. !strncmp(name, "vbi", 3) ||
  882. !strncmp(name, "v4l-subdev", 10);
  883. }
  884. static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
  885. {
  886. struct video_data *s = ctx->priv_data;
  887. DIR *dir;
  888. struct dirent *entry;
  889. AVDeviceInfo *device = NULL;
  890. struct v4l2_capability cap;
  891. int ret = 0;
  892. if (!device_list)
  893. return AVERROR(EINVAL);
  894. dir = opendir("/dev");
  895. if (!dir) {
  896. ret = AVERROR(errno);
  897. av_log(ctx, AV_LOG_ERROR, "Couldn't open the directory: %s\n", av_err2str(ret));
  898. return ret;
  899. }
  900. while ((entry = readdir(dir))) {
  901. if (!v4l2_is_v4l_dev(entry->d_name))
  902. continue;
  903. snprintf(ctx->filename, sizeof(ctx->filename), "/dev/%s", entry->d_name);
  904. if ((s->fd = device_open(ctx)) < 0)
  905. continue;
  906. if (v4l2_ioctl(s->fd, VIDIOC_QUERYCAP, &cap) < 0) {
  907. ret = AVERROR(errno);
  908. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", av_err2str(ret));
  909. goto fail;
  910. }
  911. device = av_mallocz(sizeof(AVDeviceInfo));
  912. if (!device) {
  913. ret = AVERROR(ENOMEM);
  914. goto fail;
  915. }
  916. device->device_name = av_strdup(ctx->filename);
  917. device->device_description = av_strdup(cap.card);
  918. if (!device->device_name || !device->device_description) {
  919. ret = AVERROR(ENOMEM);
  920. goto fail;
  921. }
  922. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  923. &device_list->nb_devices, device)) < 0)
  924. goto fail;
  925. v4l2_close(s->fd);
  926. s->fd = -1;
  927. continue;
  928. fail:
  929. if (device) {
  930. av_freep(&device->device_name);
  931. av_freep(&device->device_description);
  932. av_freep(&device);
  933. }
  934. if (s->fd >= 0)
  935. v4l2_close(s->fd);
  936. s->fd = -1;
  937. break;
  938. }
  939. closedir(dir);
  940. return ret;
  941. }
  942. #define OFFSET(x) offsetof(struct video_data, x)
  943. #define DEC AV_OPT_FLAG_DECODING_PARAM
  944. static const AVOption options[] = {
  945. { "standard", "set TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  946. { "channel", "set TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, DEC },
  947. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  948. { "pixel_format", "set preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  949. { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  950. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  951. { "list_formats", "list available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
  952. { "all", "show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  953. { "raw", "show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  954. { "compressed", "show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  955. { "list_standards", "list supported standards and exit", OFFSET(list_standard), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC, "list_standards" },
  956. { "all", "show all supported standards", OFFSET(list_standard), AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, DEC, "list_standards" },
  957. { "timestamps", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  958. { "ts", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  959. { "default", "use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, "timestamps" },
  960. { "abs", "use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, "timestamps" },
  961. { "mono2abs", "force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, "timestamps" },
  962. { "use_libv4l2", "use libv4l2 (v4l-utils) conversion functions", OFFSET(use_libv4l2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  963. { NULL },
  964. };
  965. static const AVClass v4l2_class = {
  966. .class_name = "V4L2 indev",
  967. .item_name = av_default_item_name,
  968. .option = options,
  969. .version = LIBAVUTIL_VERSION_INT,
  970. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  971. };
  972. AVInputFormat ff_v4l2_demuxer = {
  973. .name = "video4linux2,v4l2",
  974. .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  975. .priv_data_size = sizeof(struct video_data),
  976. .read_probe = v4l2_read_probe,
  977. .read_header = v4l2_read_header,
  978. .read_packet = v4l2_read_packet,
  979. .read_close = v4l2_read_close,
  980. .get_device_list = v4l2_get_device_list,
  981. .flags = AVFMT_NOFILE,
  982. .priv_class = &v4l2_class,
  983. };