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