v4l2.c 38 KB

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