Libav 0.7.1
|
00001 /* 00002 * Linux video grab interface 00003 * Copyright (c) 2000,2001 Fabrice Bellard 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 "libavformat/avformat.h" 00033 #include "libavcodec/dsputil.h" 00034 #include <unistd.h> 00035 #include <fcntl.h> 00036 #include <sys/ioctl.h> 00037 #include <sys/mman.h> 00038 #include <sys/time.h> 00039 #define _LINUX_TIME_H 1 00040 #include <linux/videodev.h> 00041 #include <time.h> 00042 #include <strings.h> 00043 00044 typedef struct { 00045 AVClass *class; 00046 int fd; 00047 int frame_format; /* see VIDEO_PALETTE_xxx */ 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 /* NOTE: v4l uses BGR24, not RGB24 */ 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); /* 64 bits pts in us */ 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 /* no values set, autodetect them */ 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 /* set tv standard */ 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 /* unmute audio */ 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 av_dlog(s1, "v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n", 00168 pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness); 00169 /* try to choose a suitable video format */ 00170 pict.palette = desired_palette; 00171 pict.depth= desired_depth; 00172 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) { 00173 for (j = 0; j < vformat_num; j++) { 00174 pict.palette = video_formats[j].palette; 00175 pict.depth = video_formats[j].depth; 00176 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict)) 00177 break; 00178 } 00179 if (j >= vformat_num) 00180 goto fail1; 00181 } 00182 00183 if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) { 00184 /* try to use read based access */ 00185 int val; 00186 00187 s->video_win.x = 0; 00188 s->video_win.y = 0; 00189 s->video_win.chromakey = -1; 00190 s->video_win.flags = 0; 00191 00192 if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) { 00193 av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno)); 00194 goto fail; 00195 } 00196 00197 s->frame_format = pict.palette; 00198 00199 val = 1; 00200 if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) { 00201 av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno)); 00202 goto fail; 00203 } 00204 00205 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num; 00206 s->use_mmap = 0; 00207 } else { 00208 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0); 00209 if ((unsigned char*)-1 == s->video_buf) { 00210 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0); 00211 if ((unsigned char*)-1 == s->video_buf) { 00212 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno)); 00213 goto fail; 00214 } 00215 } 00216 s->gb_frame = 0; 00217 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num; 00218 00219 /* start to grab the first frame */ 00220 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames; 00221 s->gb_buf.height = s->video_win.height; 00222 s->gb_buf.width = s->video_win.width; 00223 s->gb_buf.format = pict.palette; 00224 00225 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) { 00226 if (errno != EAGAIN) { 00227 fail1: 00228 av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno)); 00229 } else { 00230 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n"); 00231 } 00232 goto fail; 00233 } 00234 for (j = 1; j < s->gb_buffers.frames; j++) { 00235 s->gb_buf.frame = j; 00236 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf); 00237 } 00238 s->frame_format = s->gb_buf.format; 00239 s->use_mmap = 1; 00240 } 00241 00242 for (j = 0; j < vformat_num; j++) { 00243 if (s->frame_format == video_formats[j].palette) { 00244 s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8; 00245 st->codec->pix_fmt = video_formats[j].pix_fmt; 00246 break; 00247 } 00248 } 00249 00250 if (j >= vformat_num) 00251 goto fail; 00252 00253 s->fd = video_fd; 00254 00255 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00256 st->codec->codec_id = CODEC_ID_RAWVIDEO; 00257 st->codec->width = s->video_win.width; 00258 st->codec->height = s->video_win.height; 00259 st->codec->time_base = s->time_base; 00260 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8; 00261 00262 return 0; 00263 fail: 00264 if (video_fd >= 0) 00265 close(video_fd); 00266 return AVERROR(EIO); 00267 } 00268 00269 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf) 00270 { 00271 uint8_t *ptr; 00272 00273 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 && 00274 (errno == EAGAIN || errno == EINTR)); 00275 00276 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame]; 00277 memcpy(buf, ptr, s->frame_size); 00278 00279 /* Setup to capture the next frame */ 00280 s->gb_buf.frame = s->gb_frame; 00281 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) { 00282 if (errno == EAGAIN) 00283 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n"); 00284 else 00285 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno)); 00286 return AVERROR(EIO); 00287 } 00288 00289 /* This is now the grabbing frame */ 00290 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames; 00291 00292 return s->frame_size; 00293 } 00294 00295 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt) 00296 { 00297 VideoData *s = s1->priv_data; 00298 int64_t curtime, delay; 00299 struct timespec ts; 00300 00301 /* Calculate the time of the next frame */ 00302 s->time_frame += INT64_C(1000000); 00303 00304 /* wait based on the frame rate */ 00305 for(;;) { 00306 curtime = av_gettime(); 00307 delay = s->time_frame * s->time_base.num / s->time_base.den - curtime; 00308 if (delay <= 0) { 00309 if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) { 00310 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */ 00311 s->time_frame += INT64_C(1000000); 00312 } 00313 break; 00314 } 00315 ts.tv_sec = delay / 1000000; 00316 ts.tv_nsec = (delay % 1000000) * 1000; 00317 nanosleep(&ts, NULL); 00318 } 00319 00320 if (av_new_packet(pkt, s->frame_size) < 0) 00321 return AVERROR(EIO); 00322 00323 pkt->pts = curtime; 00324 00325 /* read one frame */ 00326 if (s->use_mmap) { 00327 return v4l_mm_read_picture(s, pkt->data); 00328 } else { 00329 if (read(s->fd, pkt->data, pkt->size) != pkt->size) 00330 return AVERROR(EIO); 00331 return s->frame_size; 00332 } 00333 } 00334 00335 static int grab_read_close(AVFormatContext *s1) 00336 { 00337 VideoData *s = s1->priv_data; 00338 00339 if (s->use_mmap) 00340 munmap(s->video_buf, s->gb_buffers.size); 00341 00342 /* mute audio. we must force it because the BTTV driver does not 00343 return its state correctly */ 00344 s->audio_saved.flags |= VIDEO_AUDIO_MUTE; 00345 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved); 00346 00347 close(s->fd); 00348 return 0; 00349 } 00350 00351 static const AVOption options[] = { 00352 { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00353 { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00354 { "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00355 { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00356 { NULL }, 00357 }; 00358 00359 static const AVClass v4l_class = { 00360 .class_name = "V4L indev", 00361 .item_name = av_default_item_name, 00362 .option = options, 00363 .version = LIBAVUTIL_VERSION_INT, 00364 }; 00365 00366 AVInputFormat ff_v4l_demuxer = { 00367 "video4linux", 00368 NULL_IF_CONFIG_SMALL("Video4Linux device grab"), 00369 sizeof(VideoData), 00370 NULL, 00371 grab_read_header, 00372 grab_read_packet, 00373 grab_read_close, 00374 .flags = AVFMT_NOFILE, 00375 .priv_class = &v4l_class, 00376 }; 00377 #endif /* FF_API_V4L */