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