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