v4l.c 11 KB

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