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.

355 lines
11KB

  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. #include "avformat.h"
  22. #include "dsputil.h"
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/mman.h>
  27. #include <sys/time.h>
  28. #define _LINUX_TIME_H 1
  29. #include <linux/videodev.h>
  30. #include <time.h>
  31. typedef struct {
  32. int fd;
  33. int frame_format; /* see VIDEO_PALETTE_xxx */
  34. int use_mmap;
  35. int width, height;
  36. int frame_rate;
  37. int frame_rate_base;
  38. int64_t time_frame;
  39. int frame_size;
  40. struct video_capability video_cap;
  41. struct video_audio audio_saved;
  42. uint8_t *video_buf;
  43. struct video_mbuf gb_buffers;
  44. struct video_mmap gb_buf;
  45. int gb_frame;
  46. } VideoData;
  47. static const struct {
  48. int palette;
  49. int depth;
  50. enum PixelFormat pix_fmt;
  51. } video_formats [] = {
  52. {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
  53. {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
  54. {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
  55. {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
  56. /* NOTE: v4l uses BGR24, not RGB24 */
  57. {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
  58. {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
  59. {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
  60. };
  61. static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  62. {
  63. VideoData *s = s1->priv_data;
  64. AVStream *st;
  65. int width, height;
  66. int video_fd, frame_size;
  67. int ret, frame_rate, frame_rate_base;
  68. int desired_palette, desired_depth;
  69. struct video_tuner tuner;
  70. struct video_audio audio;
  71. struct video_picture pict;
  72. int j;
  73. int vformat_num = sizeof(video_formats) / sizeof(video_formats[0]);
  74. if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  75. av_log(s1, AV_LOG_ERROR, "Bad capture size (%dx%d) or wrong time base (%d)\n",
  76. ap->width, ap->height, ap->time_base.den);
  77. return -1;
  78. }
  79. width = ap->width;
  80. height = ap->height;
  81. frame_rate = ap->time_base.den;
  82. frame_rate_base = ap->time_base.num;
  83. if((unsigned)width > 32767 || (unsigned)height > 32767) {
  84. av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
  85. width, height);
  86. return -1;
  87. }
  88. st = av_new_stream(s1, 0);
  89. if (!st)
  90. return AVERROR(ENOMEM);
  91. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  92. s->width = width;
  93. s->height = height;
  94. s->frame_rate = frame_rate;
  95. s->frame_rate_base = frame_rate_base;
  96. video_fd = open(s1->filename, O_RDWR);
  97. if (video_fd < 0) {
  98. av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
  99. goto fail;
  100. }
  101. if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
  102. av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
  103. goto fail;
  104. }
  105. if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
  106. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
  107. goto fail;
  108. }
  109. desired_palette = -1;
  110. desired_depth = -1;
  111. for (j = 0; j < vformat_num; j++) {
  112. if (ap->pix_fmt == video_formats[j].pix_fmt) {
  113. desired_palette = video_formats[j].palette;
  114. desired_depth = video_formats[j].depth;
  115. break;
  116. }
  117. }
  118. /* set tv standard */
  119. if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
  120. if (!strcasecmp(ap->standard, "pal"))
  121. tuner.mode = VIDEO_MODE_PAL;
  122. else if (!strcasecmp(ap->standard, "secam"))
  123. tuner.mode = VIDEO_MODE_SECAM;
  124. else
  125. tuner.mode = VIDEO_MODE_NTSC;
  126. ioctl(video_fd, VIDIOCSTUNER, &tuner);
  127. }
  128. /* unmute audio */
  129. audio.audio = 0;
  130. ioctl(video_fd, VIDIOCGAUDIO, &audio);
  131. memcpy(&s->audio_saved, &audio, sizeof(audio));
  132. audio.flags &= ~VIDEO_AUDIO_MUTE;
  133. ioctl(video_fd, VIDIOCSAUDIO, &audio);
  134. ioctl(video_fd, VIDIOCGPICT, &pict);
  135. #if 0
  136. printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
  137. pict.colour,
  138. pict.hue,
  139. pict.brightness,
  140. pict.contrast,
  141. pict.whiteness);
  142. #endif
  143. /* try to choose a suitable video format */
  144. pict.palette = desired_palette;
  145. pict.depth= desired_depth;
  146. if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
  147. for (j = 0; j < vformat_num; j++) {
  148. pict.palette = video_formats[j].palette;
  149. pict.depth = video_formats[j].depth;
  150. if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
  151. break;
  152. }
  153. if (j >= vformat_num)
  154. goto fail1;
  155. }
  156. ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
  157. if (ret < 0) {
  158. /* try to use read based access */
  159. struct video_window win;
  160. int val;
  161. win.x = 0;
  162. win.y = 0;
  163. win.width = width;
  164. win.height = height;
  165. win.chromakey = -1;
  166. win.flags = 0;
  167. ioctl(video_fd, VIDIOCSWIN, &win);
  168. s->frame_format = pict.palette;
  169. val = 1;
  170. ioctl(video_fd, VIDIOCCAPTURE, &val);
  171. s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
  172. s->use_mmap = 0;
  173. } else {
  174. s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
  175. if ((unsigned char*)-1 == s->video_buf) {
  176. s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
  177. if ((unsigned char*)-1 == s->video_buf) {
  178. av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
  179. goto fail;
  180. }
  181. }
  182. s->gb_frame = 0;
  183. s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
  184. /* start to grab the first frame */
  185. s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
  186. s->gb_buf.height = height;
  187. s->gb_buf.width = width;
  188. s->gb_buf.format = pict.palette;
  189. ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
  190. if (ret < 0) {
  191. if (errno != EAGAIN) {
  192. fail1:
  193. av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
  194. } else {
  195. av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
  196. }
  197. goto fail;
  198. }
  199. for (j = 1; j < s->gb_buffers.frames; j++) {
  200. s->gb_buf.frame = j;
  201. ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
  202. }
  203. s->frame_format = s->gb_buf.format;
  204. s->use_mmap = 1;
  205. }
  206. for (j = 0; j < vformat_num; j++) {
  207. if (s->frame_format == video_formats[j].palette) {
  208. frame_size = width * height * video_formats[j].depth / 8;
  209. st->codec->pix_fmt = video_formats[j].pix_fmt;
  210. break;
  211. }
  212. }
  213. if (j >= vformat_num)
  214. goto fail;
  215. s->fd = video_fd;
  216. s->frame_size = frame_size;
  217. st->codec->codec_type = CODEC_TYPE_VIDEO;
  218. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  219. st->codec->width = width;
  220. st->codec->height = height;
  221. st->codec->time_base.den = frame_rate;
  222. st->codec->time_base.num = frame_rate_base;
  223. st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
  224. return 0;
  225. fail:
  226. if (video_fd >= 0)
  227. close(video_fd);
  228. av_free(st);
  229. return AVERROR(EIO);
  230. }
  231. static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
  232. {
  233. uint8_t *ptr;
  234. while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
  235. (errno == EAGAIN || errno == EINTR));
  236. ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
  237. memcpy(buf, ptr, s->frame_size);
  238. /* Setup to capture the next frame */
  239. s->gb_buf.frame = s->gb_frame;
  240. if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
  241. if (errno == EAGAIN)
  242. av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
  243. else
  244. av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
  245. return AVERROR(EIO);
  246. }
  247. /* This is now the grabbing frame */
  248. s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
  249. return s->frame_size;
  250. }
  251. static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  252. {
  253. VideoData *s = s1->priv_data;
  254. int64_t curtime, delay;
  255. struct timespec ts;
  256. /* Calculate the time of the next frame */
  257. s->time_frame += INT64_C(1000000);
  258. /* wait based on the frame rate */
  259. for(;;) {
  260. curtime = av_gettime();
  261. delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
  262. if (delay <= 0) {
  263. if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
  264. /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
  265. s->time_frame += INT64_C(1000000);
  266. }
  267. break;
  268. }
  269. ts.tv_sec = delay / 1000000;
  270. ts.tv_nsec = (delay % 1000000) * 1000;
  271. nanosleep(&ts, NULL);
  272. }
  273. if (av_new_packet(pkt, s->frame_size) < 0)
  274. return AVERROR(EIO);
  275. pkt->pts = curtime;
  276. /* read one frame */
  277. if (s->use_mmap) {
  278. return v4l_mm_read_picture(s, pkt->data);
  279. } else {
  280. if (read(s->fd, pkt->data, pkt->size) != pkt->size)
  281. return AVERROR(EIO);
  282. return s->frame_size;
  283. }
  284. }
  285. static int grab_read_close(AVFormatContext *s1)
  286. {
  287. VideoData *s = s1->priv_data;
  288. if (s->use_mmap)
  289. munmap(s->video_buf, s->gb_buffers.size);
  290. /* mute audio. we must force it because the BTTV driver does not
  291. return its state correctly */
  292. s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
  293. ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
  294. close(s->fd);
  295. return 0;
  296. }
  297. AVInputFormat v4l_demuxer = {
  298. "video4linux",
  299. "video grab",
  300. sizeof(VideoData),
  301. NULL,
  302. grab_read_header,
  303. grab_read_packet,
  304. grab_read_close,
  305. .flags = AVFMT_NOFILE,
  306. };