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.1KB

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