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.

322 lines
8.0KB

  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <linux/videodev.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/mman.h>
  27. #include <errno.h>
  28. #include <sys/time.h>
  29. #include "avformat.h"
  30. typedef struct {
  31. int fd;
  32. int frame_format; /* see VIDEO_PALETTE_xxx */
  33. int use_mmap;
  34. int width, height;
  35. float rate;
  36. INT64 time_frame;
  37. } VideoData;
  38. const char *v4l_device = "/dev/video";
  39. /* XXX: move all that to the context */
  40. static struct video_capability video_cap;
  41. static UINT8 *video_buf;
  42. static struct video_mbuf gb_buffers;
  43. static struct video_mmap gb_buf;
  44. static struct video_audio audio;
  45. static int gb_frame = 0;
  46. static int v4l_init(URLContext *h)
  47. {
  48. VideoData *s = h->priv_data;
  49. int width, height;
  50. int ret;
  51. int video_fd, frame_size;
  52. width = s->width;
  53. height = s->height;
  54. video_fd = open(v4l_device, O_RDWR);
  55. if (video_fd < 0) {
  56. perror(v4l_device);
  57. return -EIO;
  58. }
  59. if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
  60. perror("VIDIOCGCAP");
  61. goto fail;
  62. }
  63. if (!(video_cap.type & VID_TYPE_CAPTURE)) {
  64. fprintf(stderr, "Fatal: grab device does not handle capture\n");
  65. goto fail;
  66. }
  67. /* unmute audio */
  68. ioctl(video_fd, VIDIOCGAUDIO, &audio);
  69. audio.flags &= ~VIDEO_AUDIO_MUTE;
  70. ioctl(video_fd, VIDIOCSAUDIO, &audio);
  71. ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
  72. if (ret < 0) {
  73. /* try to use read based access */
  74. struct video_window win;
  75. struct video_picture pict;
  76. int val;
  77. win.x = 0;
  78. win.y = 0;
  79. win.width = width;
  80. win.height = height;
  81. win.chromakey = -1;
  82. win.flags = 0;
  83. ioctl(video_fd, VIDIOCSWIN, &win);
  84. ioctl(video_fd, VIDIOCGPICT, &pict);
  85. #if 0
  86. printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
  87. pict.colour,
  88. pict.hue,
  89. pict.brightness,
  90. pict.contrast,
  91. pict.whiteness);
  92. #endif
  93. /* try to choose a suitable video format */
  94. pict.palette=VIDEO_PALETTE_YUV420P;
  95. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  96. if (ret < 0) {
  97. pict.palette=VIDEO_PALETTE_YUV422;
  98. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  99. if (ret < 0) {
  100. pict.palette=VIDEO_PALETTE_RGB24;
  101. ret = ioctl(video_fd, VIDIOCSPICT, &pict);
  102. if (ret < 0)
  103. goto fail1;
  104. }
  105. }
  106. s->frame_format = pict.palette;
  107. val = 1;
  108. ioctl(video_fd, VIDIOCCAPTURE, &val);
  109. s->time_frame = gettime();
  110. s->use_mmap = 0;
  111. } else {
  112. video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
  113. if ((unsigned char*)-1 == video_buf) {
  114. perror("mmap");
  115. goto fail;
  116. }
  117. gb_frame = 0;
  118. s->time_frame = gettime();
  119. /* start to grab the first frame */
  120. gb_buf.frame = 1 - gb_frame;
  121. gb_buf.height = height;
  122. gb_buf.width = width;
  123. gb_buf.format = VIDEO_PALETTE_YUV420P;
  124. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  125. if (ret < 0 && errno != EAGAIN) {
  126. /* try YUV422 */
  127. gb_buf.format = VIDEO_PALETTE_YUV422;
  128. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  129. if (ret < 0 && errno != EAGAIN) {
  130. /* try RGB24 */
  131. gb_buf.format = VIDEO_PALETTE_RGB24;
  132. ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
  133. }
  134. }
  135. if (ret < 0) {
  136. if (errno != EAGAIN) {
  137. fail1:
  138. fprintf(stderr, "Fatal: grab device does not support suitable format\n");
  139. } else {
  140. fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
  141. }
  142. goto fail;
  143. }
  144. s->frame_format = gb_buf.format;
  145. s->use_mmap = 1;
  146. }
  147. switch(s->frame_format) {
  148. case VIDEO_PALETTE_YUV420P:
  149. frame_size = (width * height * 3) / 2;
  150. break;
  151. case VIDEO_PALETTE_YUV422:
  152. frame_size = width * height * 2;
  153. break;
  154. case VIDEO_PALETTE_RGB24:
  155. frame_size = width * height * 3;
  156. break;
  157. default:
  158. goto fail;
  159. }
  160. s->fd = video_fd;
  161. h->packet_size = frame_size;
  162. return 0;
  163. fail:
  164. close(video_fd);
  165. return -EIO;
  166. }
  167. static int v4l_mm_read_picture(URLContext *h, UINT8 *buf)
  168. {
  169. VideoData *s = h->priv_data;
  170. UINT8 *ptr;
  171. gb_buf.frame = gb_frame;
  172. if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
  173. if (errno == EAGAIN)
  174. fprintf(stderr,"Cannot Sync\n");
  175. else
  176. perror("VIDIOCMCAPTURE");
  177. return -EIO;
  178. }
  179. gb_frame = 1 - gb_frame;
  180. while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
  181. (errno == EAGAIN || errno == EINTR));
  182. ptr = video_buf + gb_buffers.offsets[gb_frame];
  183. memcpy(buf, ptr, h->packet_size);
  184. return h->packet_size;
  185. }
  186. /* note: we support only one picture read at a time */
  187. static int video_read(URLContext *h, UINT8 *buf, int size)
  188. {
  189. VideoData *s = h->priv_data;
  190. INT64 curtime;
  191. if (size != h->packet_size)
  192. return -EINVAL;
  193. /* wait based on the frame rate */
  194. s->time_frame += (int)(1000000 / s->rate);
  195. do {
  196. curtime = gettime();
  197. } while (curtime < s->time_frame);
  198. /* read one frame */
  199. if (s->use_mmap) {
  200. return v4l_mm_read_picture(h, buf);
  201. } else {
  202. if (read(s->fd, buf, size) != size)
  203. return -EIO;
  204. return h->packet_size;
  205. }
  206. }
  207. static int video_get_format(URLContext *h, URLFormat *f)
  208. {
  209. VideoData *s = h->priv_data;
  210. f->width = s->width;
  211. f->height = s->height;
  212. f->frame_rate = (int)(s->rate * FRAME_RATE_BASE);
  213. strcpy(f->format_name, "rawvideo");
  214. switch(s->frame_format) {
  215. case VIDEO_PALETTE_YUV420P:
  216. f->pix_fmt = PIX_FMT_YUV420P;
  217. break;
  218. case VIDEO_PALETTE_YUV422:
  219. f->pix_fmt = PIX_FMT_YUV422;
  220. break;
  221. case VIDEO_PALETTE_RGB24:
  222. f->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
  223. break;
  224. default:
  225. abort();
  226. }
  227. return 0;
  228. }
  229. /* URI syntax: 'video:width,height,rate'
  230. */
  231. static int video_open(URLContext *h, const char *uri, int flags)
  232. {
  233. VideoData *s;
  234. const char *p;
  235. int width, height;
  236. int ret;
  237. float rate;
  238. /* extract parameters */
  239. p = uri;
  240. strstart(p, "video:", &p);
  241. width = strtol(p, (char **)&p, 0);
  242. if (width <= 0)
  243. return -EINVAL;
  244. if (*p == ',')
  245. p++;
  246. height = strtol(p, (char **)&p, 0);
  247. if (height <= 0)
  248. return -EINVAL;
  249. if (*p == ',')
  250. p++;
  251. rate = strtod(p, (char **)&p);
  252. if (rate <= 0)
  253. return -EINVAL;
  254. s = malloc(sizeof(VideoData));
  255. if (!s)
  256. return -ENOMEM;
  257. h->priv_data = s;
  258. h->is_streamed = 1;
  259. s->width = width;
  260. s->height = height;
  261. s->rate = rate;
  262. ret = v4l_init(h);
  263. if (ret)
  264. free(s);
  265. return ret;
  266. }
  267. static int video_close(URLContext *h)
  268. {
  269. VideoData *s = h->priv_data;
  270. close(s->fd);
  271. free(s);
  272. return 0;
  273. }
  274. URLProtocol video_protocol = {
  275. "video",
  276. video_open,
  277. video_read,
  278. NULL,
  279. NULL, /* seek */
  280. video_close,
  281. video_get_format,
  282. };