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.

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