v4l.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, frame_size;
  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->width <= 0 || ap->height <= 0) {
  76. av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
  77. return -1;
  78. }
  79. if (ap->time_base.den <= 0) {
  80. av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
  81. return -1;
  82. }
  83. s->time_base = ap->time_base;
  84. if((unsigned)ap->width > 32767 || (unsigned)ap->height > 32767) {
  85. av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
  86. ap->width, ap->height);
  87. return -1;
  88. }
  89. s->video_win.width = ap->width;
  90. s->video_win.height = ap->height;
  91. st = av_new_stream(s1, 0);
  92. if (!st)
  93. return AVERROR(ENOMEM);
  94. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  95. video_fd = open(s1->filename, O_RDWR);
  96. if (video_fd < 0) {
  97. av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
  98. goto fail;
  99. }
  100. if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
  101. av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
  102. goto fail;
  103. }
  104. if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
  105. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
  106. goto fail;
  107. }
  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. ioctl(video_fd, VIDIOCSWIN, s->video_win);
  163. s->frame_format = pict.palette;
  164. val = 1;
  165. ioctl(video_fd, VIDIOCCAPTURE, &val);
  166. s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
  167. s->use_mmap = 0;
  168. } else {
  169. s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
  170. if ((unsigned char*)-1 == s->video_buf) {
  171. s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
  172. if ((unsigned char*)-1 == s->video_buf) {
  173. av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  174. goto fail;
  175. }
  176. }
  177. s->gb_frame = 0;
  178. s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
  179. /* start to grab the first frame */
  180. s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
  181. s->gb_buf.height = s->video_win.height;
  182. s->gb_buf.width = s->video_win.width;
  183. s->gb_buf.format = pict.palette;
  184. if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
  185. if (errno != EAGAIN) {
  186. fail1:
  187. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
  188. } else {
  189. av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
  190. }
  191. goto fail;
  192. }
  193. for (j = 1; j < s->gb_buffers.frames; j++) {
  194. s->gb_buf.frame = j;
  195. ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
  196. }
  197. s->frame_format = s->gb_buf.format;
  198. s->use_mmap = 1;
  199. }
  200. for (j = 0; j < vformat_num; j++) {
  201. if (s->frame_format == video_formats[j].palette) {
  202. frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
  203. st->codec->pix_fmt = video_formats[j].pix_fmt;
  204. break;
  205. }
  206. }
  207. if (j >= vformat_num)
  208. goto fail;
  209. s->fd = video_fd;
  210. s->frame_size = frame_size;
  211. st->codec->codec_type = CODEC_TYPE_VIDEO;
  212. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  213. st->codec->width = s->video_win.width;
  214. st->codec->height = s->video_win.height;
  215. st->codec->time_base = s->time_base;
  216. st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
  217. return 0;
  218. fail:
  219. if (video_fd >= 0)
  220. close(video_fd);
  221. return AVERROR(EIO);
  222. }
  223. static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
  224. {
  225. uint8_t *ptr;
  226. while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
  227. (errno == EAGAIN || errno == EINTR));
  228. ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
  229. memcpy(buf, ptr, s->frame_size);
  230. /* Setup to capture the next frame */
  231. s->gb_buf.frame = s->gb_frame;
  232. if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
  233. if (errno == EAGAIN)
  234. av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
  235. else
  236. av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
  237. return AVERROR(EIO);
  238. }
  239. /* This is now the grabbing frame */
  240. s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
  241. return s->frame_size;
  242. }
  243. static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  244. {
  245. VideoData *s = s1->priv_data;
  246. int64_t curtime, delay;
  247. struct timespec ts;
  248. /* Calculate the time of the next frame */
  249. s->time_frame += INT64_C(1000000);
  250. /* wait based on the frame rate */
  251. for(;;) {
  252. curtime = av_gettime();
  253. delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
  254. if (delay <= 0) {
  255. if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
  256. /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
  257. s->time_frame += INT64_C(1000000);
  258. }
  259. break;
  260. }
  261. ts.tv_sec = delay / 1000000;
  262. ts.tv_nsec = (delay % 1000000) * 1000;
  263. nanosleep(&ts, NULL);
  264. }
  265. if (av_new_packet(pkt, s->frame_size) < 0)
  266. return AVERROR(EIO);
  267. pkt->pts = curtime;
  268. /* read one frame */
  269. if (s->use_mmap) {
  270. return v4l_mm_read_picture(s, pkt->data);
  271. } else {
  272. if (read(s->fd, pkt->data, pkt->size) != pkt->size)
  273. return AVERROR(EIO);
  274. return s->frame_size;
  275. }
  276. }
  277. static int grab_read_close(AVFormatContext *s1)
  278. {
  279. VideoData *s = s1->priv_data;
  280. if (s->use_mmap)
  281. munmap(s->video_buf, s->gb_buffers.size);
  282. /* mute audio. we must force it because the BTTV driver does not
  283. return its state correctly */
  284. s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
  285. ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
  286. close(s->fd);
  287. return 0;
  288. }
  289. AVInputFormat v4l_demuxer = {
  290. "video4linux",
  291. NULL_IF_CONFIG_SMALL("video grab"),
  292. sizeof(VideoData),
  293. NULL,
  294. grab_read_header,
  295. grab_read_packet,
  296. grab_read_close,
  297. .flags = AVFMT_NOFILE,
  298. };