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 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 
22 #include "avdevice.h"
23 
24 #if FF_API_V4L
25 
26 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
27 #include "config.h"
28 #include "libavutil/rational.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/log.h"
31 #include "libavutil/opt.h"
32 #include "libavformat/avformat.h"
33 #include "libavformat/internal.h"
34 #include "libavcodec/dsputil.h"
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/time.h>
40 #define _LINUX_TIME_H 1
41 #include <linux/videodev.h>
42 #include <time.h>
43 
44 typedef struct {
45  AVClass *class;
46  int fd;
47  int frame_format; /* see VIDEO_PALETTE_xxx */
48  int use_mmap;
50  int64_t time_frame;
52  struct video_capability video_cap;
53  struct video_audio audio_saved;
54  struct video_window video_win;
55  uint8_t *video_buf;
56  struct video_mbuf gb_buffers;
57  struct video_mmap gb_buf;
58  int gb_frame;
59  int standard;
60 } VideoData;
61 
62 static const struct {
63  int palette;
64  int depth;
66 } video_formats [] = {
67  {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
68  {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
69  {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
70  {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
71  /* NOTE: v4l uses BGR24, not RGB24 */
72  {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
73  {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
74  {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
75 };
76 
77 
79 {
80  VideoData *s = s1->priv_data;
81  AVStream *st;
82  int video_fd;
83  int desired_palette, desired_depth;
84  struct video_tuner tuner;
85  struct video_audio audio;
86  struct video_picture pict;
87  int j;
88  int vformat_num = FF_ARRAY_ELEMS(video_formats);
89 
90  av_log(s1, AV_LOG_WARNING, "V4L input device is deprecated and will be removed in the next release.");
91 
92  if (ap->time_base.den <= 0) {
93  av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
94  return -1;
95  }
96  s->time_base = ap->time_base;
97 
98  s->video_win.width = ap->width;
99  s->video_win.height = ap->height;
100 
101  st = avformat_new_stream(s1, NULL);
102  if (!st)
103  return AVERROR(ENOMEM);
104  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
105 
106  video_fd = open(s1->filename, O_RDWR);
107  if (video_fd < 0) {
108  av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
109  goto fail;
110  }
111 
112  if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
113  av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
114  goto fail;
115  }
116 
117  if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
118  av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
119  goto fail;
120  }
121 
122  /* no values set, autodetect them */
123  if (s->video_win.width <= 0 || s->video_win.height <= 0) {
124  if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
125  av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
126  goto fail;
127  }
128  }
129 
130  if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
131  return -1;
132 
133  desired_palette = -1;
134  desired_depth = -1;
135  for (j = 0; j < vformat_num; j++) {
136  if (ap->pix_fmt == video_formats[j].pix_fmt) {
137  desired_palette = video_formats[j].palette;
138  desired_depth = video_formats[j].depth;
139  break;
140  }
141  }
142 
143  /* set tv standard */
144  if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
145  tuner.mode = s->standard;
146  ioctl(video_fd, VIDIOCSTUNER, &tuner);
147  }
148 
149  /* unmute audio */
150  audio.audio = 0;
151  ioctl(video_fd, VIDIOCGAUDIO, &audio);
152  memcpy(&s->audio_saved, &audio, sizeof(audio));
153  audio.flags &= ~VIDEO_AUDIO_MUTE;
154  ioctl(video_fd, VIDIOCSAUDIO, &audio);
155 
156  ioctl(video_fd, VIDIOCGPICT, &pict);
157  av_dlog(s1, "v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
158  pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness);
159  /* try to choose a suitable video format */
160  pict.palette = desired_palette;
161  pict.depth= desired_depth;
162  if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
163  for (j = 0; j < vformat_num; j++) {
164  pict.palette = video_formats[j].palette;
165  pict.depth = video_formats[j].depth;
166  if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
167  break;
168  }
169  if (j >= vformat_num)
170  goto fail1;
171  }
172 
173  if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
174  /* try to use read based access */
175  int val;
176 
177  s->video_win.x = 0;
178  s->video_win.y = 0;
179  s->video_win.chromakey = -1;
180  s->video_win.flags = 0;
181 
182  if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
183  av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
184  goto fail;
185  }
186 
187  s->frame_format = pict.palette;
188 
189  val = 1;
190  if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
191  av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
192  goto fail;
193  }
194 
195  s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
196  s->use_mmap = 0;
197  } else {
198  s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
199  if ((unsigned char*)-1 == s->video_buf) {
200  s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
201  if ((unsigned char*)-1 == s->video_buf) {
202  av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
203  goto fail;
204  }
205  }
206  s->gb_frame = 0;
207  s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
208 
209  /* start to grab the first frame */
210  s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
211  s->gb_buf.height = s->video_win.height;
212  s->gb_buf.width = s->video_win.width;
213  s->gb_buf.format = pict.palette;
214 
215  if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
216  if (errno != EAGAIN) {
217  fail1:
218  av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
219  } else {
220  av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
221  }
222  goto fail;
223  }
224  for (j = 1; j < s->gb_buffers.frames; j++) {
225  s->gb_buf.frame = j;
226  ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
227  }
228  s->frame_format = s->gb_buf.format;
229  s->use_mmap = 1;
230  }
231 
232  for (j = 0; j < vformat_num; j++) {
233  if (s->frame_format == video_formats[j].palette) {
234  s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
235  st->codec->pix_fmt = video_formats[j].pix_fmt;
236  break;
237  }
238  }
239 
240  if (j >= vformat_num)
241  goto fail;
242 
243  s->fd = video_fd;
244 
247  st->codec->width = s->video_win.width;
248  st->codec->height = s->video_win.height;
249  st->codec->time_base = s->time_base;
250  st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
251 
252  return 0;
253  fail:
254  if (video_fd >= 0)
255  close(video_fd);
256  return AVERROR(EIO);
257 }
258 
259 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
260 {
261  uint8_t *ptr;
262 
263  while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
264  (errno == EAGAIN || errno == EINTR));
265 
266  ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
267  memcpy(buf, ptr, s->frame_size);
268 
269  /* Setup to capture the next frame */
270  s->gb_buf.frame = s->gb_frame;
271  if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
272  if (errno == EAGAIN)
273  av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
274  else
275  av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
276  return AVERROR(EIO);
277  }
278 
279  /* This is now the grabbing frame */
280  s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
281 
282  return s->frame_size;
283 }
284 
286 {
287  VideoData *s = s1->priv_data;
288  int64_t curtime, delay;
289  struct timespec ts;
290 
291  /* Calculate the time of the next frame */
292  s->time_frame += INT64_C(1000000);
293 
294  /* wait based on the frame rate */
295  for(;;) {
296  curtime = av_gettime();
297  delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
298  if (delay <= 0) {
299  if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
300  /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
301  s->time_frame += INT64_C(1000000);
302  }
303  break;
304  }
305  ts.tv_sec = delay / 1000000;
306  ts.tv_nsec = (delay % 1000000) * 1000;
307  nanosleep(&ts, NULL);
308  }
309 
310  if (av_new_packet(pkt, s->frame_size) < 0)
311  return AVERROR(EIO);
312 
313  pkt->pts = curtime;
314 
315  /* read one frame */
316  if (s->use_mmap) {
317  return v4l_mm_read_picture(s, pkt->data);
318  } else {
319  if (read(s->fd, pkt->data, pkt->size) != pkt->size)
320  return AVERROR(EIO);
321  return s->frame_size;
322  }
323 }
324 
326 {
327  VideoData *s = s1->priv_data;
328 
329  if (s->use_mmap)
330  munmap(s->video_buf, s->gb_buffers.size);
331 
332  /* mute audio. we must force it because the BTTV driver does not
333  return its state correctly */
334  s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
335  ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
336 
337  close(s->fd);
338  return 0;
339 }
340 
341 static const AVOption options[] = {
342  { "standard", "", offsetof(VideoData, standard), AV_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
343  { "PAL", "", 0, AV_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
344  { "SECAM", "", 0, AV_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
345  { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
346  { NULL },
347 };
348 
349 static const AVClass v4l_class = {
350  .class_name = "V4L indev",
351  .item_name = av_default_item_name,
352  .option = options,
353  .version = LIBAVUTIL_VERSION_INT,
354 };
355 
357  .name = "video4linux",
358  .long_name = NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
359  .priv_data_size = sizeof(VideoData),
363  .flags = AVFMT_NOFILE,
364  .priv_class = &v4l_class,
365 };
366 #endif /* FF_API_V4L */
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
AVOption.
Definition: opt.h:244
misc image utilities
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
struct video_capability video_cap
Definition: v4l.c:52
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
enum PixelFormat pix_fmt
Definition: v4l.c:65
int num
numerator
Definition: rational.h:44
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:275
int size
Definition: avcodec.h:909
AVOptions.
int use_mmap
Definition: v4l.c:48
#define FF_ARRAY_ELEMS(a)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
Format I/O context.
Definition: avformat.h:863
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
struct video_window video_win
Definition: v4l.c:54
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:140
AVRational time_base
Definition: v4l.c:49
Main libavdevice API header.
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l.c:285
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
Definition: bktr.c:52
int fd
Definition: v4l.c:46
int depth
Definition: v4l.c:64
AVCodecContext * codec
codec context
Definition: avformat.h:623
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:213
static const AVClass v4l_class
Definition: v4l.c:349
int bit_rate
the average bitrate
Definition: avcodec.h:1340
char filename[1024]
input or output filename
Definition: avformat.h:910
int width
picture width / height.
Definition: avcodec.h:1408
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
Definition: v4l.c:259
#define PIX_FMT_BGR565
Definition: pixfmt.h:180
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
Definition: log.h:158
LIBAVUTIL_VERSION_INT
Definition: eval.c:50
struct video_mmap gb_buf
Definition: v4l.c:57
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
av_default_item_name
Definition: dnxhdenc.c:43
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:65
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
int standard
Definition: bktr.c:58
Describe the class of an AVClass context structure.
Definition: log.h:33
rational number numerator/denominator
Definition: rational.h:43
uint8_t * video_buf
Definition: v4l.c:55
#define s1
Definition: regdef.h:38
static const AVOption options[]
Definition: v4l.c:341
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:67
int frame_format
Definition: v4l.c:47
int palette
Definition: v4l.c:63
int frame_size
Definition: v4l.c:51
PixelFormat
Pixel format.
Definition: pixfmt.h:62
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:368
rational numbers
static int grab_read_close(AVFormatContext *s1)
Definition: v4l.c:325
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
int gb_frame
Definition: v4l.c:58
int den
denominator
Definition: rational.h:45
DSP utils.
struct video_mbuf gb_buffers
Definition: v4l.c:56
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
Definition: v4l.c:78
AVInputFormat ff_v4l_demuxer
Definition: v4l.c:356
void * priv_data
Format private data.
Definition: avformat.h:883
int64_t time_frame
Definition: v4l.c:50
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
Y , 8bpp.
Definition: pixfmt.h:72
struct video_audio audio_saved
Definition: v4l.c:53
static const struct @61 video_formats[]
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
enum CodecID codec_id
Definition: avcodec.h:1575
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: utils.c:3531