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.

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