v4l.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Linux video grab interface
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  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. #include "avdevice.h"
  22. #if FF_API_V4L
  23. #undef __STRICT_ANSI__ //workaround due to broken kernel headers
  24. #include "config.h"
  25. #include "libavutil/rational.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/opt.h"
  29. #include "libavcodec/dsputil.h"
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/mman.h>
  34. #include <sys/time.h>
  35. #define _LINUX_TIME_H 1
  36. #include <linux/videodev.h>
  37. #include <time.h>
  38. #include <strings.h>
  39. #include "avdevice.h"
  40. typedef struct {
  41. AVClass *class;
  42. int fd;
  43. int frame_format; /* see VIDEO_PALETTE_xxx */
  44. int use_mmap;
  45. AVRational time_base;
  46. int64_t time_frame;
  47. int frame_size;
  48. struct video_capability video_cap;
  49. struct video_audio audio_saved;
  50. struct video_window video_win;
  51. uint8_t *video_buf;
  52. struct video_mbuf gb_buffers;
  53. struct video_mmap gb_buf;
  54. int gb_frame;
  55. int standard;
  56. } VideoData;
  57. static const struct {
  58. int palette;
  59. int depth;
  60. enum PixelFormat pix_fmt;
  61. } video_formats [] = {
  62. {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
  63. {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
  64. {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
  65. {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
  66. /* NOTE: v4l uses BGR24, not RGB24 */
  67. {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
  68. {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
  69. {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
  70. };
  71. static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  72. {
  73. VideoData *s = s1->priv_data;
  74. AVStream *st;
  75. int video_fd;
  76. int desired_palette, desired_depth;
  77. struct video_tuner tuner;
  78. struct video_audio audio;
  79. struct video_picture pict;
  80. int j;
  81. int vformat_num = FF_ARRAY_ELEMS(video_formats);
  82. av_log(s1, AV_LOG_WARNING, "V4L input device is deprecated and will be removed in the next release.");
  83. if (ap->time_base.den <= 0) {
  84. av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
  85. return -1;
  86. }
  87. s->time_base = ap->time_base;
  88. s->video_win.width = ap->width;
  89. s->video_win.height = ap->height;
  90. st = av_new_stream(s1, 0);
  91. if (!st)
  92. return AVERROR(ENOMEM);
  93. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  94. video_fd = open(s1->filename, O_RDWR);
  95. if (video_fd < 0) {
  96. av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
  97. goto fail;
  98. }
  99. if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
  100. av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
  101. goto fail;
  102. }
  103. if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
  104. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
  105. goto fail;
  106. }
  107. /* no values set, autodetect them */
  108. if (s->video_win.width <= 0 || s->video_win.height <= 0) {
  109. if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
  110. av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
  111. goto fail;
  112. }
  113. }
  114. if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
  115. return -1;
  116. desired_palette = -1;
  117. desired_depth = -1;
  118. for (j = 0; j < vformat_num; j++) {
  119. if (ap->pix_fmt == video_formats[j].pix_fmt) {
  120. desired_palette = video_formats[j].palette;
  121. desired_depth = video_formats[j].depth;
  122. break;
  123. }
  124. }
  125. /* set tv standard */
  126. if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
  127. #if FF_API_FORMAT_PARAMETERS
  128. if (ap->standard) {
  129. if (!strcasecmp(ap->standard, "pal"))
  130. s->standard = VIDEO_MODE_PAL;
  131. else if (!strcasecmp(ap->standard, "secam"))
  132. s->standard = VIDEO_MODE_SECAM;
  133. else
  134. s->standard = VIDEO_MODE_NTSC;
  135. }
  136. #endif
  137. tuner.mode = s->standard;
  138. ioctl(video_fd, VIDIOCSTUNER, &tuner);
  139. }
  140. /* unmute audio */
  141. audio.audio = 0;
  142. ioctl(video_fd, VIDIOCGAUDIO, &audio);
  143. memcpy(&s->audio_saved, &audio, sizeof(audio));
  144. audio.flags &= ~VIDEO_AUDIO_MUTE;
  145. ioctl(video_fd, VIDIOCSAUDIO, &audio);
  146. ioctl(video_fd, VIDIOCGPICT, &pict);
  147. #if 0
  148. printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
  149. pict.colour,
  150. pict.hue,
  151. pict.brightness,
  152. pict.contrast,
  153. pict.whiteness);
  154. #endif
  155. /* try to choose a suitable video format */
  156. pict.palette = desired_palette;
  157. pict.depth= desired_depth;
  158. if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
  159. for (j = 0; j < vformat_num; j++) {
  160. pict.palette = video_formats[j].palette;
  161. pict.depth = video_formats[j].depth;
  162. if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
  163. break;
  164. }
  165. if (j >= vformat_num)
  166. goto fail1;
  167. }
  168. if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
  169. /* try to use read based access */
  170. int val;
  171. s->video_win.x = 0;
  172. s->video_win.y = 0;
  173. s->video_win.chromakey = -1;
  174. s->video_win.flags = 0;
  175. if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
  176. av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
  177. goto fail;
  178. }
  179. s->frame_format = pict.palette;
  180. val = 1;
  181. if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
  182. av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
  183. goto fail;
  184. }
  185. s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
  186. s->use_mmap = 0;
  187. } else {
  188. s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
  189. if ((unsigned char*)-1 == s->video_buf) {
  190. s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
  191. if ((unsigned char*)-1 == s->video_buf) {
  192. av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  193. goto fail;
  194. }
  195. }
  196. s->gb_frame = 0;
  197. s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
  198. /* start to grab the first frame */
  199. s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
  200. s->gb_buf.height = s->video_win.height;
  201. s->gb_buf.width = s->video_win.width;
  202. s->gb_buf.format = pict.palette;
  203. if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
  204. if (errno != EAGAIN) {
  205. fail1:
  206. av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
  207. } else {
  208. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
  209. }
  210. goto fail;
  211. }
  212. for (j = 1; j < s->gb_buffers.frames; j++) {
  213. s->gb_buf.frame = j;
  214. ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
  215. }
  216. s->frame_format = s->gb_buf.format;
  217. s->use_mmap = 1;
  218. }
  219. for (j = 0; j < vformat_num; j++) {
  220. if (s->frame_format == video_formats[j].palette) {
  221. s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
  222. st->codec->pix_fmt = video_formats[j].pix_fmt;
  223. break;
  224. }
  225. }
  226. if (j >= vformat_num)
  227. goto fail;
  228. s->fd = video_fd;
  229. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  230. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  231. st->codec->width = s->video_win.width;
  232. st->codec->height = s->video_win.height;
  233. st->codec->time_base = s->time_base;
  234. st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
  235. return 0;
  236. fail:
  237. if (video_fd >= 0)
  238. close(video_fd);
  239. return AVERROR(EIO);
  240. }
  241. static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
  242. {
  243. uint8_t *ptr;
  244. while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
  245. (errno == EAGAIN || errno == EINTR));
  246. ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
  247. memcpy(buf, ptr, s->frame_size);
  248. /* Setup to capture the next frame */
  249. s->gb_buf.frame = s->gb_frame;
  250. if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
  251. if (errno == EAGAIN)
  252. av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
  253. else
  254. av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
  255. return AVERROR(EIO);
  256. }
  257. /* This is now the grabbing frame */
  258. s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
  259. return s->frame_size;
  260. }
  261. static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  262. {
  263. VideoData *s = s1->priv_data;
  264. int64_t curtime, delay;
  265. struct timespec ts;
  266. /* Calculate the time of the next frame */
  267. s->time_frame += INT64_C(1000000);
  268. /* wait based on the frame rate */
  269. for(;;) {
  270. curtime = av_gettime();
  271. delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
  272. if (delay <= 0) {
  273. if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
  274. /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
  275. s->time_frame += INT64_C(1000000);
  276. }
  277. break;
  278. }
  279. ts.tv_sec = delay / 1000000;
  280. ts.tv_nsec = (delay % 1000000) * 1000;
  281. nanosleep(&ts, NULL);
  282. }
  283. if (av_new_packet(pkt, s->frame_size) < 0)
  284. return AVERROR(EIO);
  285. pkt->pts = curtime;
  286. /* read one frame */
  287. if (s->use_mmap) {
  288. return v4l_mm_read_picture(s, pkt->data);
  289. } else {
  290. if (read(s->fd, pkt->data, pkt->size) != pkt->size)
  291. return AVERROR(EIO);
  292. return s->frame_size;
  293. }
  294. }
  295. static int grab_read_close(AVFormatContext *s1)
  296. {
  297. VideoData *s = s1->priv_data;
  298. if (s->use_mmap)
  299. munmap(s->video_buf, s->gb_buffers.size);
  300. /* mute audio. we must force it because the BTTV driver does not
  301. return its state correctly */
  302. s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
  303. ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
  304. close(s->fd);
  305. return 0;
  306. }
  307. static const AVOption options[] = {
  308. { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  309. { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  310. { "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  311. { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  312. { NULL },
  313. };
  314. static const AVClass v4l_class = {
  315. .class_name = "V4L indev",
  316. .item_name = av_default_item_name,
  317. .option = options,
  318. .version = LIBAVUTIL_VERSION_INT,
  319. };
  320. AVInputFormat ff_v4l_demuxer = {
  321. "video4linux",
  322. NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
  323. sizeof(VideoData),
  324. NULL,
  325. grab_read_header,
  326. grab_read_packet,
  327. grab_read_close,
  328. .flags = AVFMT_NOFILE,
  329. .priv_class = &v4l_class,
  330. };
  331. #endif /* FF_API_V4L */