v4l.c 11 KB

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