v4l2.c 38 KB

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