v4l2.c 38 KB

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