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.

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