v4l2.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
  631. } else if (framerate_q.num && framerate_q.den) {
  632. if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
  633. tpf = &streamparm.parm.capture.timeperframe;
  634. av_log(ctx, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
  635. framerate_q.den, framerate_q.num);
  636. tpf->numerator = framerate_q.den;
  637. tpf->denominator = framerate_q.num;
  638. if (v4l2_ioctl(s->fd, VIDIOC_S_PARM, &streamparm) < 0) {
  639. ret = AVERROR(errno);
  640. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_PARM): %s\n",
  641. av_err2str(ret));
  642. return ret;
  643. }
  644. if (framerate_q.num != tpf->denominator ||
  645. framerate_q.den != tpf->numerator) {
  646. av_log(ctx, AV_LOG_INFO,
  647. "The driver changed the time per frame from "
  648. "%d/%d to %d/%d\n",
  649. framerate_q.den, framerate_q.num,
  650. tpf->numerator, tpf->denominator);
  651. }
  652. } else {
  653. av_log(ctx, AV_LOG_WARNING,
  654. "The driver does not permit changing the time per frame\n");
  655. }
  656. }
  657. if (tpf->denominator > 0 && tpf->numerator > 0) {
  658. ctx->streams[0]->avg_frame_rate.num = tpf->denominator;
  659. ctx->streams[0]->avg_frame_rate.den = tpf->numerator;
  660. ctx->streams[0]->r_frame_rate = ctx->streams[0]->avg_frame_rate;
  661. } else
  662. av_log(ctx, AV_LOG_WARNING, "Time per frame unknown\n");
  663. return 0;
  664. }
  665. static int device_try_init(AVFormatContext *ctx,
  666. enum AVPixelFormat pix_fmt,
  667. int *width,
  668. int *height,
  669. uint32_t *desired_format,
  670. enum AVCodecID *codec_id)
  671. {
  672. int ret, i;
  673. *desired_format = ff_fmt_ff2v4l(pix_fmt, ctx->video_codec_id);
  674. if (*desired_format) {
  675. ret = device_init(ctx, width, height, *desired_format);
  676. if (ret < 0) {
  677. *desired_format = 0;
  678. if (ret != AVERROR(EINVAL))
  679. return ret;
  680. }
  681. }
  682. if (!*desired_format) {
  683. for (i = 0; ff_fmt_conversion_table[i].codec_id != AV_CODEC_ID_NONE; i++) {
  684. if (ctx->video_codec_id == AV_CODEC_ID_NONE ||
  685. ff_fmt_conversion_table[i].codec_id == ctx->video_codec_id) {
  686. av_log(ctx, AV_LOG_DEBUG, "Trying to set codec:%s pix_fmt:%s\n",
  687. avcodec_get_name(ff_fmt_conversion_table[i].codec_id),
  688. (char *)av_x_if_null(av_get_pix_fmt_name(ff_fmt_conversion_table[i].ff_fmt), "none"));
  689. *desired_format = ff_fmt_conversion_table[i].v4l2_fmt;
  690. ret = device_init(ctx, width, height, *desired_format);
  691. if (ret >= 0)
  692. break;
  693. else if (ret != AVERROR(EINVAL))
  694. return ret;
  695. *desired_format = 0;
  696. }
  697. }
  698. if (*desired_format == 0) {
  699. av_log(ctx, AV_LOG_ERROR, "Cannot find a proper format for "
  700. "codec '%s' (id %d), pixel format '%s' (id %d)\n",
  701. avcodec_get_name(ctx->video_codec_id), ctx->video_codec_id,
  702. (char *)av_x_if_null(av_get_pix_fmt_name(pix_fmt), "none"), pix_fmt);
  703. ret = AVERROR(EINVAL);
  704. }
  705. }
  706. *codec_id = ff_fmt_v4l2codec(*desired_format);
  707. av_assert0(*codec_id != AV_CODEC_ID_NONE);
  708. return ret;
  709. }
  710. static int v4l2_read_probe(AVProbeData *p)
  711. {
  712. if (av_strstart(p->filename, "/dev/video", NULL))
  713. return AVPROBE_SCORE_MAX - 1;
  714. return 0;
  715. }
  716. static int v4l2_read_header(AVFormatContext *ctx)
  717. {
  718. struct video_data *s = ctx->priv_data;
  719. AVStream *st;
  720. int res = 0;
  721. uint32_t desired_format;
  722. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  723. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  724. struct v4l2_input input = { 0 };
  725. st = avformat_new_stream(ctx, NULL);
  726. if (!st)
  727. return AVERROR(ENOMEM);
  728. #if CONFIG_LIBV4L2
  729. /* silence libv4l2 logging. if fopen() fails v4l2_log_file will be NULL
  730. and errors will get sent to stderr */
  731. if (s->use_libv4l2)
  732. v4l2_log_file = fopen("/dev/null", "w");
  733. #endif
  734. s->fd = device_open(ctx);
  735. if (s->fd < 0)
  736. return s->fd;
  737. if (s->channel != -1) {
  738. /* set video input */
  739. av_log(ctx, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
  740. if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
  741. res = AVERROR(errno);
  742. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
  743. goto fail;
  744. }
  745. } else {
  746. /* get current video input */
  747. if (v4l2_ioctl(s->fd, VIDIOC_G_INPUT, &s->channel) < 0) {
  748. res = AVERROR(errno);
  749. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
  750. goto fail;
  751. }
  752. }
  753. /* enum input */
  754. input.index = s->channel;
  755. if (v4l2_ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  756. res = AVERROR(errno);
  757. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMINPUT): %s\n", av_err2str(res));
  758. goto fail;
  759. }
  760. s->std_id = input.std;
  761. av_log(ctx, AV_LOG_DEBUG, "Current input_channel: %d, input_name: %s, input_std: %"PRIx64"\n",
  762. s->channel, input.name, (uint64_t)input.std);
  763. if (s->list_format) {
  764. list_formats(ctx, s->list_format);
  765. res = AVERROR_EXIT;
  766. goto fail;
  767. }
  768. if (s->list_standard) {
  769. list_standards(ctx);
  770. res = AVERROR_EXIT;
  771. goto fail;
  772. }
  773. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  774. if (s->pixel_format) {
  775. AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
  776. if (codec)
  777. ctx->video_codec_id = codec->id;
  778. pix_fmt = av_get_pix_fmt(s->pixel_format);
  779. if (pix_fmt == AV_PIX_FMT_NONE && !codec) {
  780. av_log(ctx, AV_LOG_ERROR, "No such input format: %s.\n",
  781. s->pixel_format);
  782. res = AVERROR(EINVAL);
  783. goto fail;
  784. }
  785. }
  786. if (!s->width && !s->height) {
  787. struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
  788. av_log(ctx, AV_LOG_VERBOSE,
  789. "Querying the device for the current frame size\n");
  790. if (v4l2_ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  791. res = AVERROR(errno);
  792. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
  793. av_err2str(res));
  794. goto fail;
  795. }
  796. s->width = fmt.fmt.pix.width;
  797. s->height = fmt.fmt.pix.height;
  798. av_log(ctx, AV_LOG_VERBOSE,
  799. "Setting frame size to %dx%d\n", s->width, s->height);
  800. }
  801. res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &desired_format, &codec_id);
  802. if (res < 0)
  803. goto fail;
  804. /* If no pixel_format was specified, the codec_id was not known up
  805. * until now. Set video_codec_id in the context, as codec_id will
  806. * not be available outside this function
  807. */
  808. if (codec_id != AV_CODEC_ID_NONE && ctx->video_codec_id == AV_CODEC_ID_NONE)
  809. ctx->video_codec_id = codec_id;
  810. if ((res = av_image_check_size(s->width, s->height, 0, ctx)) < 0)
  811. goto fail;
  812. s->pixelformat = desired_format;
  813. if ((res = v4l2_set_parameters(ctx)) < 0)
  814. goto fail;
  815. st->codecpar->format = ff_fmt_v4l2ff(desired_format, codec_id);
  816. s->frame_size = av_image_get_buffer_size(st->codecpar->format,
  817. s->width, s->height, 1);
  818. if ((res = mmap_init(ctx)) ||
  819. (res = mmap_start(ctx)) < 0)
  820. goto fail;
  821. s->top_field_first = first_field(s);
  822. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  823. st->codecpar->codec_id = codec_id;
  824. if (codec_id == AV_CODEC_ID_RAWVIDEO)
  825. st->codecpar->codec_tag =
  826. avcodec_pix_fmt_to_codec_tag(st->codecpar->format);
  827. else if (codec_id == AV_CODEC_ID_H264) {
  828. st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
  829. }
  830. if (desired_format == V4L2_PIX_FMT_YVU420)
  831. st->codecpar->codec_tag = MKTAG('Y', 'V', '1', '2');
  832. else if (desired_format == V4L2_PIX_FMT_YVU410)
  833. st->codecpar->codec_tag = MKTAG('Y', 'V', 'U', '9');
  834. st->codecpar->width = s->width;
  835. st->codecpar->height = s->height;
  836. if (st->avg_frame_rate.den)
  837. st->codecpar->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
  838. return 0;
  839. fail:
  840. v4l2_close(s->fd);
  841. return res;
  842. }
  843. static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  844. {
  845. #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
  846. FF_DISABLE_DEPRECATION_WARNINGS
  847. struct video_data *s = ctx->priv_data;
  848. AVFrame *frame = ctx->streams[0]->codec->coded_frame;
  849. FF_ENABLE_DEPRECATION_WARNINGS
  850. #endif
  851. int res;
  852. av_init_packet(pkt);
  853. if ((res = mmap_read_frame(ctx, pkt)) < 0) {
  854. return res;
  855. }
  856. #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
  857. FF_DISABLE_DEPRECATION_WARNINGS
  858. if (frame && s->interlaced) {
  859. frame->interlaced_frame = 1;
  860. frame->top_field_first = s->top_field_first;
  861. }
  862. FF_ENABLE_DEPRECATION_WARNINGS
  863. #endif
  864. return pkt->size;
  865. }
  866. static int v4l2_read_close(AVFormatContext *ctx)
  867. {
  868. struct video_data *s = ctx->priv_data;
  869. if (avpriv_atomic_int_get(&s->buffers_queued) != s->buffers)
  870. av_log(ctx, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
  871. "close.\n");
  872. mmap_close(s);
  873. v4l2_close(s->fd);
  874. return 0;
  875. }
  876. static int v4l2_is_v4l_dev(const char *name)
  877. {
  878. return !strncmp(name, "video", 5) ||
  879. !strncmp(name, "radio", 5) ||
  880. !strncmp(name, "vbi", 3) ||
  881. !strncmp(name, "v4l-subdev", 10);
  882. }
  883. static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
  884. {
  885. struct video_data *s = ctx->priv_data;
  886. DIR *dir;
  887. struct dirent *entry;
  888. AVDeviceInfo *device = NULL;
  889. struct v4l2_capability cap;
  890. int ret = 0;
  891. if (!device_list)
  892. return AVERROR(EINVAL);
  893. dir = opendir("/dev");
  894. if (!dir) {
  895. ret = AVERROR(errno);
  896. av_log(ctx, AV_LOG_ERROR, "Couldn't open the directory: %s\n", av_err2str(ret));
  897. return ret;
  898. }
  899. while ((entry = readdir(dir))) {
  900. if (!v4l2_is_v4l_dev(entry->d_name))
  901. continue;
  902. snprintf(ctx->filename, sizeof(ctx->filename), "/dev/%s", entry->d_name);
  903. if ((s->fd = device_open(ctx)) < 0)
  904. continue;
  905. if (v4l2_ioctl(s->fd, VIDIOC_QUERYCAP, &cap) < 0) {
  906. ret = AVERROR(errno);
  907. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", av_err2str(ret));
  908. goto fail;
  909. }
  910. device = av_mallocz(sizeof(AVDeviceInfo));
  911. if (!device) {
  912. ret = AVERROR(ENOMEM);
  913. goto fail;
  914. }
  915. device->device_name = av_strdup(ctx->filename);
  916. device->device_description = av_strdup(cap.card);
  917. if (!device->device_name || !device->device_description) {
  918. ret = AVERROR(ENOMEM);
  919. goto fail;
  920. }
  921. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  922. &device_list->nb_devices, device)) < 0)
  923. goto fail;
  924. v4l2_close(s->fd);
  925. s->fd = -1;
  926. continue;
  927. fail:
  928. if (device) {
  929. av_freep(&device->device_name);
  930. av_freep(&device->device_description);
  931. av_freep(&device);
  932. }
  933. if (s->fd >= 0)
  934. v4l2_close(s->fd);
  935. s->fd = -1;
  936. break;
  937. }
  938. closedir(dir);
  939. return ret;
  940. }
  941. #define OFFSET(x) offsetof(struct video_data, x)
  942. #define DEC AV_OPT_FLAG_DECODING_PARAM
  943. static const AVOption options[] = {
  944. { "standard", "set TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  945. { "channel", "set TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, DEC },
  946. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  947. { "pixel_format", "set preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  948. { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  949. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  950. { "list_formats", "list available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
  951. { "all", "show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  952. { "raw", "show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  953. { "compressed", "show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  954. { "list_standards", "list supported standards and exit", OFFSET(list_standard), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC, "list_standards" },
  955. { "all", "show all supported standards", OFFSET(list_standard), AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, DEC, "list_standards" },
  956. { "timestamps", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  957. { "ts", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  958. { "default", "use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, "timestamps" },
  959. { "abs", "use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, "timestamps" },
  960. { "mono2abs", "force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, "timestamps" },
  961. { "use_libv4l2", "use libv4l2 (v4l-utils) conversion functions", OFFSET(use_libv4l2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  962. { NULL },
  963. };
  964. static const AVClass v4l2_class = {
  965. .class_name = "V4L2 indev",
  966. .item_name = av_default_item_name,
  967. .option = options,
  968. .version = LIBAVUTIL_VERSION_INT,
  969. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  970. };
  971. AVInputFormat ff_v4l2_demuxer = {
  972. .name = "video4linux2,v4l2",
  973. .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  974. .priv_data_size = sizeof(struct video_data),
  975. .read_probe = v4l2_read_probe,
  976. .read_header = v4l2_read_header,
  977. .read_packet = v4l2_read_packet,
  978. .read_close = v4l2_read_close,
  979. .get_device_list = v4l2_get_device_list,
  980. .flags = AVFMT_NOFILE,
  981. .priv_class = &v4l2_class,
  982. };