v4l2.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 build 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 : %9s : %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: %9s : %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. #if FF_API_DESTRUCT_PACKET
  347. static void dummy_release_buffer(AVPacket *pkt)
  348. {
  349. av_assert0(0);
  350. }
  351. #endif
  352. static int enqueue_buffer(struct video_data *s, struct v4l2_buffer *buf)
  353. {
  354. int res = 0;
  355. if (v4l2_ioctl(s->fd, VIDIOC_QBUF, buf) < 0) {
  356. res = AVERROR(errno);
  357. av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", av_err2str(res));
  358. } else {
  359. avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
  360. }
  361. return res;
  362. }
  363. static void mmap_release_buffer(void *opaque, uint8_t *data)
  364. {
  365. struct v4l2_buffer buf = { 0 };
  366. struct buff_data *buf_descriptor = opaque;
  367. struct video_data *s = buf_descriptor->s;
  368. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  369. buf.memory = V4L2_MEMORY_MMAP;
  370. buf.index = buf_descriptor->index;
  371. av_free(buf_descriptor);
  372. enqueue_buffer(s, &buf);
  373. }
  374. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  375. static int64_t av_gettime_monotonic(void)
  376. {
  377. return av_gettime_relative();
  378. }
  379. #endif
  380. static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
  381. {
  382. struct video_data *s = ctx->priv_data;
  383. int64_t now;
  384. now = av_gettime();
  385. if (s->ts_mode == V4L_TS_ABS &&
  386. ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE) {
  387. av_log(ctx, AV_LOG_INFO, "Detected absolute timestamps\n");
  388. s->ts_mode = V4L_TS_CONVERT_READY;
  389. return 0;
  390. }
  391. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  392. if (ctx->streams[0]->avg_frame_rate.num) {
  393. now = av_gettime_monotonic();
  394. if (s->ts_mode == V4L_TS_MONO2ABS ||
  395. (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE)) {
  396. AVRational tb = {AV_TIME_BASE, 1};
  397. int64_t period = av_rescale_q(1, tb, ctx->streams[0]->avg_frame_rate);
  398. av_log(ctx, AV_LOG_INFO, "Detected monotonic timestamps, converting\n");
  399. /* microseconds instead of seconds, MHz instead of Hz */
  400. s->timefilter = ff_timefilter_new(1, period, 1.0E-6);
  401. if (!s->timefilter)
  402. return AVERROR(ENOMEM);
  403. s->ts_mode = V4L_TS_CONVERT_READY;
  404. return 0;
  405. }
  406. }
  407. #endif
  408. av_log(ctx, AV_LOG_ERROR, "Unknown timestamps\n");
  409. return AVERROR(EIO);
  410. }
  411. static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
  412. {
  413. struct video_data *s = ctx->priv_data;
  414. if (s->ts_mode) {
  415. int r = init_convert_timestamp(ctx, *ts);
  416. if (r < 0)
  417. return r;
  418. }
  419. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  420. if (s->timefilter) {
  421. int64_t nowa = av_gettime();
  422. int64_t nowm = av_gettime_monotonic();
  423. ff_timefilter_update(s->timefilter, nowa, nowm - s->last_time_m);
  424. s->last_time_m = nowm;
  425. *ts = ff_timefilter_eval(s->timefilter, *ts - nowm);
  426. }
  427. #endif
  428. return 0;
  429. }
  430. static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
  431. {
  432. struct video_data *s = ctx->priv_data;
  433. struct v4l2_buffer buf = {
  434. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  435. .memory = V4L2_MEMORY_MMAP
  436. };
  437. int res;
  438. /* FIXME: Some special treatment might be needed in case of loss of signal... */
  439. while ((res = v4l2_ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
  440. if (res < 0) {
  441. if (errno == EAGAIN) {
  442. pkt->size = 0;
  443. return AVERROR(EAGAIN);
  444. }
  445. res = AVERROR(errno);
  446. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
  447. av_err2str(res));
  448. return res;
  449. }
  450. if (buf.index >= s->buffers) {
  451. av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n");
  452. return AVERROR(EINVAL);
  453. }
  454. avpriv_atomic_int_add_and_fetch(&s->buffers_queued, -1);
  455. // always keep at least one buffer queued
  456. av_assert0(avpriv_atomic_int_get(&s->buffers_queued) >= 1);
  457. /* CPIA is a compressed format and we don't know the exact number of bytes
  458. * used by a frame, so set it here as the driver announces it.
  459. */
  460. if (ctx->video_codec_id == AV_CODEC_ID_CPIA)
  461. s->frame_size = buf.bytesused;
  462. if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
  463. av_log(ctx, AV_LOG_ERROR,
  464. "The v4l2 frame is %d bytes, but %d bytes are expected\n",
  465. buf.bytesused, s->frame_size);
  466. enqueue_buffer(s, &buf);
  467. return AVERROR_INVALIDDATA;
  468. }
  469. /* Image is at s->buff_start[buf.index] */
  470. if (avpriv_atomic_int_get(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
  471. /* when we start getting low on queued buffers, fall back on copying data */
  472. res = av_new_packet(pkt, buf.bytesused);
  473. if (res < 0) {
  474. av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
  475. enqueue_buffer(s, &buf);
  476. return res;
  477. }
  478. memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
  479. res = enqueue_buffer(s, &buf);
  480. if (res) {
  481. av_free_packet(pkt);
  482. return res;
  483. }
  484. } else {
  485. struct buff_data *buf_descriptor;
  486. pkt->data = s->buf_start[buf.index];
  487. pkt->size = buf.bytesused;
  488. #if FF_API_DESTRUCT_PACKET
  489. FF_DISABLE_DEPRECATION_WARNINGS
  490. pkt->destruct = dummy_release_buffer;
  491. FF_ENABLE_DEPRECATION_WARNINGS
  492. #endif
  493. buf_descriptor = av_malloc(sizeof(struct buff_data));
  494. if (!buf_descriptor) {
  495. /* Something went wrong... Since av_malloc() failed, we cannot even
  496. * allocate a buffer for memcpying into it
  497. */
  498. av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
  499. enqueue_buffer(s, &buf);
  500. return AVERROR(ENOMEM);
  501. }
  502. buf_descriptor->index = buf.index;
  503. buf_descriptor->s = s;
  504. pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer,
  505. buf_descriptor, 0);
  506. if (!pkt->buf) {
  507. av_log(ctx, AV_LOG_ERROR, "Failed to create a buffer\n");
  508. enqueue_buffer(s, &buf);
  509. av_freep(&buf_descriptor);
  510. return AVERROR(ENOMEM);
  511. }
  512. }
  513. pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
  514. convert_timestamp(ctx, &pkt->pts);
  515. return s->buf_len[buf.index];
  516. }
  517. static int mmap_start(AVFormatContext *ctx)
  518. {
  519. struct video_data *s = ctx->priv_data;
  520. enum v4l2_buf_type type;
  521. int i, res;
  522. for (i = 0; i < s->buffers; i++) {
  523. struct v4l2_buffer buf = {
  524. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  525. .index = i,
  526. .memory = V4L2_MEMORY_MMAP
  527. };
  528. if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) < 0) {
  529. res = AVERROR(errno);
  530. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
  531. av_err2str(res));
  532. return res;
  533. }
  534. }
  535. s->buffers_queued = s->buffers;
  536. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  537. if (v4l2_ioctl(s->fd, VIDIOC_STREAMON, &type) < 0) {
  538. res = AVERROR(errno);
  539. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
  540. av_err2str(res));
  541. return res;
  542. }
  543. return 0;
  544. }
  545. static void mmap_close(struct video_data *s)
  546. {
  547. enum v4l2_buf_type type;
  548. int i;
  549. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  550. /* We do not check for the result, because we could
  551. * not do anything about it anyway...
  552. */
  553. v4l2_ioctl(s->fd, VIDIOC_STREAMOFF, &type);
  554. for (i = 0; i < s->buffers; i++) {
  555. v4l2_munmap(s->buf_start[i], s->buf_len[i]);
  556. }
  557. av_freep(&s->buf_start);
  558. av_freep(&s->buf_len);
  559. }
  560. static int v4l2_set_parameters(AVFormatContext *ctx)
  561. {
  562. struct video_data *s = ctx->priv_data;
  563. struct v4l2_standard standard = { 0 };
  564. struct v4l2_streamparm streamparm = { 0 };
  565. struct v4l2_fract *tpf;
  566. AVRational framerate_q = { 0 };
  567. int i, ret;
  568. if (s->framerate &&
  569. (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
  570. av_log(ctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
  571. s->framerate);
  572. return ret;
  573. }
  574. if (s->standard) {
  575. if (s->std_id) {
  576. ret = 0;
  577. av_log(ctx, AV_LOG_DEBUG, "Setting standard: %s\n", s->standard);
  578. /* set tv standard */
  579. for (i = 0; ; i++) {
  580. standard.index = i;
  581. if (v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  582. ret = AVERROR(errno);
  583. break;
  584. }
  585. if (!av_strcasecmp(standard.name, s->standard))
  586. break;
  587. }
  588. if (ret < 0) {
  589. av_log(ctx, AV_LOG_ERROR, "Unknown or unsupported standard '%s'\n", s->standard);
  590. return ret;
  591. }
  592. if (v4l2_ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
  593. ret = AVERROR(errno);
  594. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_STD): %s\n", av_err2str(ret));
  595. return ret;
  596. }
  597. } else {
  598. av_log(ctx, AV_LOG_WARNING,
  599. "This device does not support any standard\n");
  600. }
  601. }
  602. /* get standard */
  603. if (v4l2_ioctl(s->fd, VIDIOC_G_STD, &s->std_id) == 0) {
  604. tpf = &standard.frameperiod;
  605. for (i = 0; ; i++) {
  606. standard.index = i;
  607. if (v4l2_ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
  608. ret = AVERROR(errno);
  609. if (ret == AVERROR(EINVAL)
  610. #ifdef ENODATA
  611. || ret == AVERROR(ENODATA)
  612. #endif
  613. ) {
  614. tpf = &streamparm.parm.capture.timeperframe;
  615. break;
  616. }
  617. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMSTD): %s\n", av_err2str(ret));
  618. return ret;
  619. }
  620. if (standard.id == s->std_id) {
  621. av_log(ctx, AV_LOG_DEBUG,
  622. "Current standard: %s, id: %"PRIx64", frameperiod: %d/%d\n",
  623. standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
  624. break;
  625. }
  626. }
  627. } else {
  628. tpf = &streamparm.parm.capture.timeperframe;
  629. }
  630. streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  631. if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
  632. ret = AVERROR(errno);
  633. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
  634. return ret;
  635. }
  636. if (framerate_q.num && framerate_q.den) {
  637. if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
  638. tpf = &streamparm.parm.capture.timeperframe;
  639. av_log(ctx, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
  640. framerate_q.den, framerate_q.num);
  641. tpf->numerator = framerate_q.den;
  642. tpf->denominator = framerate_q.num;
  643. if (v4l2_ioctl(s->fd, VIDIOC_S_PARM, &streamparm) < 0) {
  644. ret = AVERROR(errno);
  645. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_PARM): %s\n",
  646. av_err2str(ret));
  647. return ret;
  648. }
  649. if (framerate_q.num != tpf->denominator ||
  650. framerate_q.den != tpf->numerator) {
  651. av_log(ctx, AV_LOG_INFO,
  652. "The driver changed the time per frame from "
  653. "%d/%d to %d/%d\n",
  654. framerate_q.den, framerate_q.num,
  655. tpf->numerator, tpf->denominator);
  656. }
  657. } else {
  658. av_log(ctx, AV_LOG_WARNING,
  659. "The driver does not permit changing the time per frame\n");
  660. }
  661. }
  662. if (tpf->denominator > 0 && tpf->numerator > 0) {
  663. ctx->streams[0]->avg_frame_rate.num = tpf->denominator;
  664. ctx->streams[0]->avg_frame_rate.den = tpf->numerator;
  665. ctx->streams[0]->r_frame_rate = ctx->streams[0]->avg_frame_rate;
  666. } else
  667. av_log(ctx, AV_LOG_WARNING, "Time per frame unknown\n");
  668. return 0;
  669. }
  670. static int device_try_init(AVFormatContext *ctx,
  671. enum AVPixelFormat pix_fmt,
  672. int *width,
  673. int *height,
  674. uint32_t *desired_format,
  675. enum AVCodecID *codec_id)
  676. {
  677. int ret, i;
  678. *desired_format = ff_fmt_ff2v4l(pix_fmt, ctx->video_codec_id);
  679. if (*desired_format) {
  680. ret = device_init(ctx, width, height, *desired_format);
  681. if (ret < 0) {
  682. *desired_format = 0;
  683. if (ret != AVERROR(EINVAL))
  684. return ret;
  685. }
  686. }
  687. if (!*desired_format) {
  688. for (i = 0; ff_fmt_conversion_table[i].codec_id != AV_CODEC_ID_NONE; i++) {
  689. if (ctx->video_codec_id == AV_CODEC_ID_NONE ||
  690. ff_fmt_conversion_table[i].codec_id == ctx->video_codec_id) {
  691. av_log(ctx, AV_LOG_DEBUG, "Trying to set codec:%s pix_fmt:%s\n",
  692. avcodec_get_name(ff_fmt_conversion_table[i].codec_id),
  693. (char *)av_x_if_null(av_get_pix_fmt_name(ff_fmt_conversion_table[i].ff_fmt), "none"));
  694. *desired_format = ff_fmt_conversion_table[i].v4l2_fmt;
  695. ret = device_init(ctx, width, height, *desired_format);
  696. if (ret >= 0)
  697. break;
  698. else if (ret != AVERROR(EINVAL))
  699. return ret;
  700. *desired_format = 0;
  701. }
  702. }
  703. if (*desired_format == 0) {
  704. av_log(ctx, AV_LOG_ERROR, "Cannot find a proper format for "
  705. "codec '%s' (id %d), pixel format '%s' (id %d)\n",
  706. avcodec_get_name(ctx->video_codec_id), ctx->video_codec_id,
  707. (char *)av_x_if_null(av_get_pix_fmt_name(pix_fmt), "none"), pix_fmt);
  708. ret = AVERROR(EINVAL);
  709. }
  710. }
  711. *codec_id = ff_fmt_v4l2codec(*desired_format);
  712. av_assert0(*codec_id != AV_CODEC_ID_NONE);
  713. return ret;
  714. }
  715. static int v4l2_read_probe(AVProbeData *p)
  716. {
  717. if (av_strstart(p->filename, "/dev/video", NULL))
  718. return AVPROBE_SCORE_MAX - 1;
  719. return 0;
  720. }
  721. static int v4l2_read_header(AVFormatContext *ctx)
  722. {
  723. struct video_data *s = ctx->priv_data;
  724. AVStream *st;
  725. int res = 0;
  726. uint32_t desired_format;
  727. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  728. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  729. struct v4l2_input input = { 0 };
  730. st = avformat_new_stream(ctx, NULL);
  731. if (!st)
  732. return AVERROR(ENOMEM);
  733. #if CONFIG_LIBV4L2
  734. /* silence libv4l2 logging. if fopen() fails v4l2_log_file will be NULL
  735. and errors will get sent to stderr */
  736. if (s->use_libv4l2)
  737. v4l2_log_file = fopen("/dev/null", "w");
  738. #endif
  739. s->fd = device_open(ctx);
  740. if (s->fd < 0)
  741. return s->fd;
  742. if (s->channel != -1) {
  743. /* set video input */
  744. av_log(ctx, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
  745. if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
  746. res = AVERROR(errno);
  747. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
  748. goto fail;
  749. }
  750. } else {
  751. /* get current video input */
  752. if (v4l2_ioctl(s->fd, VIDIOC_G_INPUT, &s->channel) < 0) {
  753. res = AVERROR(errno);
  754. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
  755. goto fail;
  756. }
  757. }
  758. /* enum input */
  759. input.index = s->channel;
  760. if (v4l2_ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
  761. res = AVERROR(errno);
  762. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_ENUMINPUT): %s\n", av_err2str(res));
  763. goto fail;
  764. }
  765. s->std_id = input.std;
  766. av_log(ctx, AV_LOG_DEBUG, "Current input_channel: %d, input_name: %s, input_std: %"PRIx64"\n",
  767. s->channel, input.name, (uint64_t)input.std);
  768. if (s->list_format) {
  769. list_formats(ctx, s->list_format);
  770. res = AVERROR_EXIT;
  771. goto fail;
  772. }
  773. if (s->list_standard) {
  774. list_standards(ctx);
  775. res = AVERROR_EXIT;
  776. goto fail;
  777. }
  778. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  779. if (s->pixel_format) {
  780. AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
  781. if (codec)
  782. ctx->video_codec_id = codec->id;
  783. pix_fmt = av_get_pix_fmt(s->pixel_format);
  784. if (pix_fmt == AV_PIX_FMT_NONE && !codec) {
  785. av_log(ctx, AV_LOG_ERROR, "No such input format: %s.\n",
  786. s->pixel_format);
  787. res = AVERROR(EINVAL);
  788. goto fail;
  789. }
  790. }
  791. if (!s->width && !s->height) {
  792. struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
  793. av_log(ctx, AV_LOG_VERBOSE,
  794. "Querying the device for the current frame size\n");
  795. if (v4l2_ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  796. res = AVERROR(errno);
  797. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
  798. av_err2str(res));
  799. goto fail;
  800. }
  801. s->width = fmt.fmt.pix.width;
  802. s->height = fmt.fmt.pix.height;
  803. av_log(ctx, AV_LOG_VERBOSE,
  804. "Setting frame size to %dx%d\n", s->width, s->height);
  805. }
  806. res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &desired_format, &codec_id);
  807. if (res < 0)
  808. goto fail;
  809. /* If no pixel_format was specified, the codec_id was not known up
  810. * until now. Set video_codec_id in the context, as codec_id will
  811. * not be available outside this function
  812. */
  813. if (codec_id != AV_CODEC_ID_NONE && ctx->video_codec_id == AV_CODEC_ID_NONE)
  814. ctx->video_codec_id = codec_id;
  815. if ((res = av_image_check_size(s->width, s->height, 0, ctx)) < 0)
  816. goto fail;
  817. s->pixelformat = desired_format;
  818. if ((res = v4l2_set_parameters(ctx)) < 0)
  819. goto fail;
  820. st->codec->pix_fmt = ff_fmt_v4l2ff(desired_format, codec_id);
  821. s->frame_size =
  822. avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
  823. if ((res = mmap_init(ctx)) ||
  824. (res = mmap_start(ctx)) < 0)
  825. goto fail;
  826. s->top_field_first = first_field(s);
  827. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  828. st->codec->codec_id = codec_id;
  829. if (codec_id == AV_CODEC_ID_RAWVIDEO)
  830. st->codec->codec_tag =
  831. avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
  832. else if (codec_id == AV_CODEC_ID_H264) {
  833. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  834. }
  835. if (desired_format == V4L2_PIX_FMT_YVU420)
  836. st->codec->codec_tag = MKTAG('Y', 'V', '1', '2');
  837. else if (desired_format == V4L2_PIX_FMT_YVU410)
  838. st->codec->codec_tag = MKTAG('Y', 'V', 'U', '9');
  839. st->codec->width = s->width;
  840. st->codec->height = s->height;
  841. if (st->avg_frame_rate.den)
  842. st->codec->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
  843. return 0;
  844. fail:
  845. v4l2_close(s->fd);
  846. return res;
  847. }
  848. static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  849. {
  850. struct video_data *s = ctx->priv_data;
  851. AVFrame *frame = ctx->streams[0]->codec->coded_frame;
  852. int res;
  853. av_init_packet(pkt);
  854. if ((res = mmap_read_frame(ctx, pkt)) < 0) {
  855. return res;
  856. }
  857. if (frame && s->interlaced) {
  858. frame->interlaced_frame = 1;
  859. frame->top_field_first = s->top_field_first;
  860. }
  861. return pkt->size;
  862. }
  863. static int v4l2_read_close(AVFormatContext *ctx)
  864. {
  865. struct video_data *s = ctx->priv_data;
  866. if (avpriv_atomic_int_get(&s->buffers_queued) != s->buffers)
  867. av_log(ctx, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
  868. "close.\n");
  869. mmap_close(s);
  870. v4l2_close(s->fd);
  871. return 0;
  872. }
  873. static int v4l2_is_v4l_dev(const char *name)
  874. {
  875. return !strncmp(name, "video", 5) ||
  876. !strncmp(name, "radio", 5) ||
  877. !strncmp(name, "vbi", 3) ||
  878. !strncmp(name, "v4l-subdev", 10);
  879. }
  880. static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
  881. {
  882. struct video_data *s = ctx->priv_data;
  883. DIR *dir;
  884. struct dirent *entry;
  885. AVDeviceInfo *device = NULL;
  886. struct v4l2_capability cap;
  887. int ret = 0;
  888. if (!device_list)
  889. return AVERROR(EINVAL);
  890. dir = opendir("/dev");
  891. if (!dir) {
  892. ret = AVERROR(errno);
  893. av_log(ctx, AV_LOG_ERROR, "Couldn't open the directory: %s\n", av_err2str(ret));
  894. return ret;
  895. }
  896. while ((entry = readdir(dir))) {
  897. if (!v4l2_is_v4l_dev(entry->d_name))
  898. continue;
  899. snprintf(ctx->filename, sizeof(ctx->filename), "/dev/%s", entry->d_name);
  900. if ((s->fd = device_open(ctx)) < 0)
  901. continue;
  902. if (v4l2_ioctl(s->fd, VIDIOC_QUERYCAP, &cap) < 0) {
  903. ret = AVERROR(errno);
  904. av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", av_err2str(ret));
  905. goto fail;
  906. }
  907. device = av_mallocz(sizeof(AVDeviceInfo));
  908. if (!device) {
  909. ret = AVERROR(ENOMEM);
  910. goto fail;
  911. }
  912. device->device_name = av_strdup(ctx->filename);
  913. device->device_description = av_strdup(cap.card);
  914. if (!device->device_name || !device->device_description) {
  915. ret = AVERROR(ENOMEM);
  916. goto fail;
  917. }
  918. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  919. &device_list->nb_devices, device)) < 0)
  920. goto fail;
  921. v4l2_close(s->fd);
  922. s->fd = -1;
  923. continue;
  924. fail:
  925. if (device) {
  926. av_freep(&device->device_name);
  927. av_freep(&device->device_description);
  928. av_freep(&device);
  929. }
  930. if (s->fd >= 0)
  931. v4l2_close(s->fd);
  932. s->fd = -1;
  933. break;
  934. }
  935. closedir(dir);
  936. return ret;
  937. }
  938. #define OFFSET(x) offsetof(struct video_data, x)
  939. #define DEC AV_OPT_FLAG_DECODING_PARAM
  940. static const AVOption options[] = {
  941. { "standard", "set TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  942. { "channel", "set TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, DEC },
  943. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  944. { "pixel_format", "set preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  945. { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  946. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  947. { "list_formats", "list available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
  948. { "all", "show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  949. { "raw", "show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  950. { "compressed", "show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
  951. { "list_standards", "list supported standards and exit", OFFSET(list_standard), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC, "list_standards" },
  952. { "all", "show all supported standards", OFFSET(list_standard), AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, DEC, "list_standards" },
  953. { "timestamps", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  954. { "ts", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
  955. { "default", "use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, "timestamps" },
  956. { "abs", "use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, "timestamps" },
  957. { "mono2abs", "force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, "timestamps" },
  958. { "use_libv4l2", "use libv4l2 (v4l-utils) conversion functions", OFFSET(use_libv4l2), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  959. { NULL },
  960. };
  961. static const AVClass v4l2_class = {
  962. .class_name = "V4L2 indev",
  963. .item_name = av_default_item_name,
  964. .option = options,
  965. .version = LIBAVUTIL_VERSION_INT,
  966. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  967. };
  968. AVInputFormat ff_v4l2_demuxer = {
  969. .name = "video4linux2,v4l2",
  970. .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
  971. .priv_data_size = sizeof(struct video_data),
  972. .read_probe = v4l2_read_probe,
  973. .read_header = v4l2_read_header,
  974. .read_packet = v4l2_read_packet,
  975. .read_close = v4l2_read_close,
  976. .get_device_list = v4l2_get_device_list,
  977. .flags = AVFMT_NOFILE,
  978. .priv_class = &v4l2_class,
  979. };