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.

369 lines
12KB

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