v4l2.c 37 KB

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