You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
7.9KB

  1. /*
  2. * Linux video grab interface
  3. * Copyright (c) 2000,2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include <linux/videodev.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. typedef struct {
  28. int fd;
  29. int frame_format; /* see VIDEO_PALETTE_xxx */
  30. int use_mmap;
  31. int width, height;
  32. int frame_rate;
  33. INT64 time_frame;
  34. int frame_size;
  35. } VideoData;
  36. const char *v4l_device = "/dev/video";
  37. /* XXX: move all that to the context */
  38. static struct video_capability video_cap;
  39. static UINT8 *video_buf;
  40. static struct video_mbuf gb_buffers;
  41. static struct video_mmap gb_buf;
  42. static struct video_audio audio, audio_saved;
  43. static int gb_frame = 0;
  44. static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  45. {
  46. VideoData *s;
  47. AVStream *st;
  48. int width, height;
  49. int video_fd, frame_size;
  50. int ret, frame_rate;
  51. if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
  52. return -1;
  53. width = ap->width;
  54. height = ap->height;
  55. frame_rate = ap->frame_rate;
  56. s = av_mallocz(sizeof(VideoData));
  57. if (!s)
  58. return -ENOMEM;
  59. st = av_mallocz(sizeof(AVStream));
  60. if (!st) {
  61. free(s);
  62. return -ENOMEM;
  63. }
  64. s1->priv_data = s;
  65. s1->nb_streams = 1;
  66. s1->streams[0] = st;
  67. s->width = width;
  68. s->height = height;
  69. s->frame_rate = frame_rate;
  70. video_fd = open(v4l_device, O_RDWR);
  71. if (video_fd < 0) {
  72. perror(v4l_device);
  73. goto fail;
  74. }
  75. if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
  76. perror("VIDIOCGCAP");
  77. goto fail;
  78. }
  79. if (!(video_cap.type & VID_TYPE_CAPTURE)) {
  80. fprintf(stderr, "Fatal: grab device does not handle capture\n");
  81. goto fail;
  82. }
  83. /* unmute audio */
  84. ioctl(video_fd, VIDIOCGAUDIO, &audio);
  85. memcpy(&audio_saved, &audio, sizeof(audio));
  86. audio.flags &= ~VIDEO_AUDIO_MUTE;
  87. ioctl(video_fd, VIDIOCSAUDIO, &audio);
  88. ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
  89. if (ret < 0) {
  90. /* try to use read based access */
  91. struct video_window win;
  92. struct video_picture pict;
  93. int val;
  94. win.x = 0;
  95. win.y = 0;
  96. win.width = width;
  97. win.height = height;
  98. win.chromakey = -1;
  99. win.flags = 0;
  100. ioctl(video_fd, VIDIOCSWIN, &win);
  101. ioctl(video_fd, VIDIOCGPICT, &pict);
  102. #if 0
  103. printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
  104. pict.colour,
  105. pict.hue,
  106. pict.brightness,
  107. pict.contrast,
  108. pict.whiteness);
  109. #endif
  110. /* try to choose a suitable video format */
  111. pict.palette=VIDEO_PALETTE_YUV420P;
  112. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  113. if (ret < 0) {
  114. pict.palette=VIDEO_PALETTE_YUV422;
  115. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  116. if (ret < 0) {
  117. pict.palette=VIDEO_PALETTE_RGB24;
  118. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  119. if (ret < 0)
  120. goto fail1;
  121. }
  122. }
  123. s->frame_format = pict.palette;
  124. val = 1;
  125. ioctl(video_fd, VIDIOCCAPTURE, &val);
  126. s->time_frame = gettime();
  127. s->use_mmap = 0;
  128. } else {
  129. video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
  130. if ((unsigned char*)-1 == video_buf) {
  131. perror("mmap");
  132. goto fail;
  133. }
  134. gb_frame = 0;
  135. s->time_frame = gettime();
  136. /* start to grab the first frame */
  137. gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
  138. gb_buf.height = height;
  139. gb_buf.width = width;
  140. gb_buf.format = VIDEO_PALETTE_YUV420P;
  141. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  142. if (ret < 0 && errno != EAGAIN) {
  143. /* try YUV422 */
  144. gb_buf.format = VIDEO_PALETTE_YUV422;
  145. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  146. if (ret < 0 && errno != EAGAIN) {
  147. /* try RGB24 */
  148. gb_buf.format = VIDEO_PALETTE_RGB24;
  149. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  150. }
  151. }
  152. if (ret < 0) {
  153. if (errno != EAGAIN) {
  154. fail1:
  155. fprintf(stderr, "Fatal: grab device does not support suitable format\n");
  156. } else {
  157. fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
  158. }
  159. goto fail;
  160. }
  161. s->frame_format = gb_buf.format;
  162. s->use_mmap = 1;
  163. }
  164. switch(s->frame_format) {
  165. case VIDEO_PALETTE_YUV420P:
  166. frame_size = (width * height * 3) / 2;
  167. st->codec.pix_fmt = PIX_FMT_YUV420P;
  168. break;
  169. case VIDEO_PALETTE_YUV422:
  170. frame_size = width * height * 2;
  171. st->codec.pix_fmt = PIX_FMT_YUV422;
  172. break;
  173. case VIDEO_PALETTE_RGB24:
  174. frame_size = width * height * 3;
  175. st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
  176. break;
  177. default:
  178. goto fail;
  179. }
  180. s->fd = video_fd;
  181. s->frame_size = frame_size;
  182. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  183. st->codec.width = width;
  184. st->codec.height = height;
  185. st->codec.frame_rate = frame_rate;
  186. return 0;
  187. fail:
  188. if (video_fd >= 0)
  189. close(video_fd);
  190. free(st);
  191. free(s);
  192. return -EIO;
  193. }
  194. static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
  195. {
  196. UINT8 *ptr;
  197. gb_buf.frame = gb_frame;
  198. if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
  199. if (errno == EAGAIN)
  200. fprintf(stderr,"Cannot Sync\n");
  201. else
  202. perror("VIDIOCMCAPTURE");
  203. return -EIO;
  204. }
  205. gb_frame = (gb_frame + 1) % gb_buffers.frames;
  206. while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
  207. (errno == EAGAIN || errno == EINTR));
  208. ptr = video_buf + gb_buffers.offsets[gb_frame];
  209. memcpy(buf, ptr, s->frame_size);
  210. return s->frame_size;
  211. }
  212. static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  213. {
  214. VideoData *s = s1->priv_data;
  215. INT64 curtime, delay;
  216. struct timespec ts;
  217. /* wait based on the frame rate */
  218. s->time_frame += (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
  219. for(;;) {
  220. curtime = gettime();
  221. delay = s->time_frame - curtime;
  222. if (delay <= 0)
  223. break;
  224. ts.tv_sec = delay / 1000000;
  225. ts.tv_nsec = (delay % 1000000) * 1000;
  226. nanosleep(&ts, NULL);
  227. }
  228. if (av_new_packet(pkt, s->frame_size) < 0)
  229. return -EIO;
  230. /* read one frame */
  231. if (s->use_mmap) {
  232. return v4l_mm_read_picture(s, pkt->data);
  233. } else {
  234. if (read(s->fd, pkt->data, pkt->size) != pkt->size)
  235. return -EIO;
  236. return s->frame_size;
  237. }
  238. }
  239. static int grab_read_close(AVFormatContext *s1)
  240. {
  241. VideoData *s = s1->priv_data;
  242. /* restore audio settings */
  243. ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
  244. close(s->fd);
  245. free(s);
  246. return 0;
  247. }
  248. AVFormat video_grab_device_format = {
  249. "video_grab_device",
  250. "video grab",
  251. "",
  252. "",
  253. CODEC_ID_NONE,
  254. CODEC_ID_NONE,
  255. NULL,
  256. NULL,
  257. NULL,
  258. grab_read_header,
  259. grab_read_packet,
  260. grab_read_close,
  261. NULL,
  262. AVFMT_NOFILE,
  263. };