00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avdevice.h"
00023
00024 #if FF_API_V4L
00025
00026 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
00027 #include "config.h"
00028 #include "libavutil/rational.h"
00029 #include "libavutil/imgutils.h"
00030 #include "libavutil/log.h"
00031 #include "libavutil/opt.h"
00032 #include "libavcodec/dsputil.h"
00033 #include <unistd.h>
00034 #include <fcntl.h>
00035 #include <sys/ioctl.h>
00036 #include <sys/mman.h>
00037 #include <sys/time.h>
00038 #define _LINUX_TIME_H 1
00039 #include <linux/videodev.h>
00040 #include <time.h>
00041 #include <strings.h>
00042 #include "avdevice.h"
00043
00044 typedef struct {
00045 AVClass *class;
00046 int fd;
00047 int frame_format;
00048 int use_mmap;
00049 AVRational time_base;
00050 int64_t time_frame;
00051 int frame_size;
00052 struct video_capability video_cap;
00053 struct video_audio audio_saved;
00054 struct video_window video_win;
00055 uint8_t *video_buf;
00056 struct video_mbuf gb_buffers;
00057 struct video_mmap gb_buf;
00058 int gb_frame;
00059 int standard;
00060 } VideoData;
00061
00062 static const struct {
00063 int palette;
00064 int depth;
00065 enum PixelFormat pix_fmt;
00066 } video_formats [] = {
00067 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
00068 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
00069 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
00070 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
00071
00072 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
00073 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
00074 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
00075 };
00076
00077
00078 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00079 {
00080 VideoData *s = s1->priv_data;
00081 AVStream *st;
00082 int video_fd;
00083 int desired_palette, desired_depth;
00084 struct video_tuner tuner;
00085 struct video_audio audio;
00086 struct video_picture pict;
00087 int j;
00088 int vformat_num = FF_ARRAY_ELEMS(video_formats);
00089
00090 av_log(s1, AV_LOG_WARNING, "V4L input device is deprecated and will be removed in the next release.");
00091
00092 if (ap->time_base.den <= 0) {
00093 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
00094 return -1;
00095 }
00096 s->time_base = ap->time_base;
00097
00098 s->video_win.width = ap->width;
00099 s->video_win.height = ap->height;
00100
00101 st = av_new_stream(s1, 0);
00102 if (!st)
00103 return AVERROR(ENOMEM);
00104 av_set_pts_info(st, 64, 1, 1000000);
00105
00106 video_fd = open(s1->filename, O_RDWR);
00107 if (video_fd < 0) {
00108 av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
00109 goto fail;
00110 }
00111
00112 if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
00113 av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
00114 goto fail;
00115 }
00116
00117 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
00118 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
00119 goto fail;
00120 }
00121
00122
00123 if (s->video_win.width <= 0 || s->video_win.height <= 0) {
00124 if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
00125 av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
00126 goto fail;
00127 }
00128 }
00129
00130 if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
00131 return -1;
00132
00133 desired_palette = -1;
00134 desired_depth = -1;
00135 for (j = 0; j < vformat_num; j++) {
00136 if (ap->pix_fmt == video_formats[j].pix_fmt) {
00137 desired_palette = video_formats[j].palette;
00138 desired_depth = video_formats[j].depth;
00139 break;
00140 }
00141 }
00142
00143
00144 if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
00145 #if FF_API_FORMAT_PARAMETERS
00146 if (ap->standard) {
00147 if (!strcasecmp(ap->standard, "pal"))
00148 s->standard = VIDEO_MODE_PAL;
00149 else if (!strcasecmp(ap->standard, "secam"))
00150 s->standard = VIDEO_MODE_SECAM;
00151 else
00152 s->standard = VIDEO_MODE_NTSC;
00153 }
00154 #endif
00155 tuner.mode = s->standard;
00156 ioctl(video_fd, VIDIOCSTUNER, &tuner);
00157 }
00158
00159
00160 audio.audio = 0;
00161 ioctl(video_fd, VIDIOCGAUDIO, &audio);
00162 memcpy(&s->audio_saved, &audio, sizeof(audio));
00163 audio.flags &= ~VIDEO_AUDIO_MUTE;
00164 ioctl(video_fd, VIDIOCSAUDIO, &audio);
00165
00166 ioctl(video_fd, VIDIOCGPICT, &pict);
00167 #if 0
00168 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
00169 pict.colour,
00170 pict.hue,
00171 pict.brightness,
00172 pict.contrast,
00173 pict.whiteness);
00174 #endif
00175
00176 pict.palette = desired_palette;
00177 pict.depth= desired_depth;
00178 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
00179 for (j = 0; j < vformat_num; j++) {
00180 pict.palette = video_formats[j].palette;
00181 pict.depth = video_formats[j].depth;
00182 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
00183 break;
00184 }
00185 if (j >= vformat_num)
00186 goto fail1;
00187 }
00188
00189 if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
00190
00191 int val;
00192
00193 s->video_win.x = 0;
00194 s->video_win.y = 0;
00195 s->video_win.chromakey = -1;
00196 s->video_win.flags = 0;
00197
00198 if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
00199 av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
00200 goto fail;
00201 }
00202
00203 s->frame_format = pict.palette;
00204
00205 val = 1;
00206 if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
00207 av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
00208 goto fail;
00209 }
00210
00211 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
00212 s->use_mmap = 0;
00213 } else {
00214 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
00215 if ((unsigned char*)-1 == s->video_buf) {
00216 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
00217 if ((unsigned char*)-1 == s->video_buf) {
00218 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00219 goto fail;
00220 }
00221 }
00222 s->gb_frame = 0;
00223 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
00224
00225
00226 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
00227 s->gb_buf.height = s->video_win.height;
00228 s->gb_buf.width = s->video_win.width;
00229 s->gb_buf.format = pict.palette;
00230
00231 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
00232 if (errno != EAGAIN) {
00233 fail1:
00234 av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
00235 } else {
00236 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
00237 }
00238 goto fail;
00239 }
00240 for (j = 1; j < s->gb_buffers.frames; j++) {
00241 s->gb_buf.frame = j;
00242 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
00243 }
00244 s->frame_format = s->gb_buf.format;
00245 s->use_mmap = 1;
00246 }
00247
00248 for (j = 0; j < vformat_num; j++) {
00249 if (s->frame_format == video_formats[j].palette) {
00250 s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
00251 st->codec->pix_fmt = video_formats[j].pix_fmt;
00252 break;
00253 }
00254 }
00255
00256 if (j >= vformat_num)
00257 goto fail;
00258
00259 s->fd = video_fd;
00260
00261 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00262 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00263 st->codec->width = s->video_win.width;
00264 st->codec->height = s->video_win.height;
00265 st->codec->time_base = s->time_base;
00266 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
00267
00268 return 0;
00269 fail:
00270 if (video_fd >= 0)
00271 close(video_fd);
00272 return AVERROR(EIO);
00273 }
00274
00275 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
00276 {
00277 uint8_t *ptr;
00278
00279 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
00280 (errno == EAGAIN || errno == EINTR));
00281
00282 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
00283 memcpy(buf, ptr, s->frame_size);
00284
00285
00286 s->gb_buf.frame = s->gb_frame;
00287 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
00288 if (errno == EAGAIN)
00289 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
00290 else
00291 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
00292 return AVERROR(EIO);
00293 }
00294
00295
00296 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
00297
00298 return s->frame_size;
00299 }
00300
00301 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
00302 {
00303 VideoData *s = s1->priv_data;
00304 int64_t curtime, delay;
00305 struct timespec ts;
00306
00307
00308 s->time_frame += INT64_C(1000000);
00309
00310
00311 for(;;) {
00312 curtime = av_gettime();
00313 delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
00314 if (delay <= 0) {
00315 if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
00316
00317 s->time_frame += INT64_C(1000000);
00318 }
00319 break;
00320 }
00321 ts.tv_sec = delay / 1000000;
00322 ts.tv_nsec = (delay % 1000000) * 1000;
00323 nanosleep(&ts, NULL);
00324 }
00325
00326 if (av_new_packet(pkt, s->frame_size) < 0)
00327 return AVERROR(EIO);
00328
00329 pkt->pts = curtime;
00330
00331
00332 if (s->use_mmap) {
00333 return v4l_mm_read_picture(s, pkt->data);
00334 } else {
00335 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
00336 return AVERROR(EIO);
00337 return s->frame_size;
00338 }
00339 }
00340
00341 static int grab_read_close(AVFormatContext *s1)
00342 {
00343 VideoData *s = s1->priv_data;
00344
00345 if (s->use_mmap)
00346 munmap(s->video_buf, s->gb_buffers.size);
00347
00348
00349
00350 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
00351 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
00352
00353 close(s->fd);
00354 return 0;
00355 }
00356
00357 static const AVOption options[] = {
00358 { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00359 { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00360 { "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00361 { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00362 { NULL },
00363 };
00364
00365 static const AVClass v4l_class = {
00366 .class_name = "V4L indev",
00367 .item_name = av_default_item_name,
00368 .option = options,
00369 .version = LIBAVUTIL_VERSION_INT,
00370 };
00371
00372 AVInputFormat ff_v4l_demuxer = {
00373 "video4linux",
00374 NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
00375 sizeof(VideoData),
00376 NULL,
00377 grab_read_header,
00378 grab_read_packet,
00379 grab_read_close,
00380 .flags = AVFMT_NOFILE,
00381 .priv_class = &v4l_class,
00382 };
00383 #endif