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