v4l2.c 37 KB

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