Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2011 Stefano Sabatini 00003 * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com> 00004 * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu> 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00030 /* #define DEBUG */ 00031 00032 #include <unistd.h> 00033 #include <fcntl.h> 00034 #include <sys/ioctl.h> 00035 #include <sys/time.h> 00036 #include <sys/mman.h> 00037 #include <time.h> 00038 #include <linux/fb.h> 00039 00040 #include "libavutil/log.h" 00041 #include "libavutil/mem.h" 00042 #include "libavutil/opt.h" 00043 #include "libavutil/parseutils.h" 00044 #include "libavutil/pixdesc.h" 00045 #include "libavformat/avformat.h" 00046 00047 struct rgb_pixfmt_map_entry { 00048 int bits_per_pixel; 00049 int red_offset, green_offset, blue_offset, alpha_offset; 00050 enum PixelFormat pixfmt; 00051 }; 00052 00053 static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = { 00054 // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt 00055 { 32, 0, 8, 16, 24, PIX_FMT_RGBA }, 00056 { 32, 16, 8, 0, 24, PIX_FMT_BGRA }, 00057 { 32, 8, 16, 24, 0, PIX_FMT_ARGB }, 00058 { 32, 3, 2, 8, 0, PIX_FMT_ABGR }, 00059 { 24, 0, 8, 16, 0, PIX_FMT_RGB24 }, 00060 { 24, 16, 8, 0, 0, PIX_FMT_BGR24 }, 00061 }; 00062 00063 static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo) 00064 { 00065 int i; 00066 00067 for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) { 00068 struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i]; 00069 if (entry->bits_per_pixel == varinfo->bits_per_pixel && 00070 entry->red_offset == varinfo->red.offset && 00071 entry->green_offset == varinfo->green.offset && 00072 entry->blue_offset == varinfo->blue.offset) 00073 return entry->pixfmt; 00074 } 00075 00076 return PIX_FMT_NONE; 00077 } 00078 00079 typedef struct { 00080 AVClass *class; 00081 int frame_size; 00082 AVRational fps; 00083 char *framerate; 00084 int64_t time_frame; 00085 00086 int fd; 00087 int width, heigth; 00088 int frame_linesize; 00089 int bytes_per_pixel; 00090 00091 struct fb_var_screeninfo varinfo; 00092 struct fb_fix_screeninfo fixinfo; 00093 00094 uint8_t *data; 00095 } FBDevContext; 00096 00097 av_cold static int fbdev_read_header(AVFormatContext *avctx, 00098 AVFormatParameters *ap) 00099 { 00100 FBDevContext *fbdev = avctx->priv_data; 00101 AVStream *st = NULL; 00102 enum PixelFormat pix_fmt; 00103 int ret, flags = O_RDONLY; 00104 00105 ret = av_parse_video_rate(&fbdev->fps, fbdev->framerate); 00106 if (ret < 0) { 00107 av_log(avctx, AV_LOG_ERROR, "Couldn't parse framerate.\n"); 00108 return ret; 00109 } 00110 #if FF_API_FORMAT_PARAMETERS 00111 if (ap->time_base.num) 00112 fbdev->fps = (AVRational){ap->time_base.den, ap->time_base.num}; 00113 #endif 00114 00115 if (!(st = av_new_stream(avctx, 0))) 00116 return AVERROR(ENOMEM); 00117 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */ 00118 00119 /* NONBLOCK is ignored by the fbdev driver, only set for consistency */ 00120 if (avctx->flags & AVFMT_FLAG_NONBLOCK) 00121 flags |= O_NONBLOCK; 00122 00123 if ((fbdev->fd = open(avctx->filename, flags)) == -1) { 00124 ret = AVERROR(errno); 00125 av_log(avctx, AV_LOG_ERROR, 00126 "Could not open framebuffer device '%s': %s\n", 00127 avctx->filename, strerror(ret)); 00128 return ret; 00129 } 00130 00131 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { 00132 ret = AVERROR(errno); 00133 av_log(avctx, AV_LOG_ERROR, 00134 "FBIOGET_VSCREENINFO: %s\n", strerror(errno)); 00135 goto fail; 00136 } 00137 00138 if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) { 00139 ret = AVERROR(errno); 00140 av_log(avctx, AV_LOG_ERROR, 00141 "FBIOGET_FSCREENINFO: %s\n", strerror(errno)); 00142 goto fail; 00143 } 00144 00145 pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo); 00146 if (pix_fmt == PIX_FMT_NONE) { 00147 ret = AVERROR(EINVAL); 00148 av_log(avctx, AV_LOG_ERROR, 00149 "Framebuffer pixel format not supported.\n"); 00150 goto fail; 00151 } 00152 00153 fbdev->width = fbdev->varinfo.xres; 00154 fbdev->heigth = fbdev->varinfo.yres; 00155 fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3; 00156 fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel; 00157 fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth; 00158 fbdev->time_frame = AV_NOPTS_VALUE; 00159 fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0); 00160 if (fbdev->data == MAP_FAILED) { 00161 ret = AVERROR(errno); 00162 av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno)); 00163 goto fail; 00164 } 00165 00166 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00167 st->codec->codec_id = CODEC_ID_RAWVIDEO; 00168 st->codec->width = fbdev->width; 00169 st->codec->height = fbdev->heigth; 00170 st->codec->pix_fmt = pix_fmt; 00171 st->codec->time_base = (AVRational){fbdev->fps.den, fbdev->fps.num}; 00172 st->codec->bit_rate = 00173 fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel * av_q2d(fbdev->fps) * 8; 00174 00175 av_log(avctx, AV_LOG_INFO, 00176 "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n", 00177 fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel, 00178 av_pix_fmt_descriptors[pix_fmt].name, 00179 fbdev->fps.num, fbdev->fps.den, 00180 st->codec->bit_rate); 00181 return 0; 00182 00183 fail: 00184 close(fbdev->fd); 00185 return ret; 00186 } 00187 00188 static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) 00189 { 00190 FBDevContext *fbdev = avctx->priv_data; 00191 int64_t curtime, delay; 00192 struct timespec ts; 00193 int i, ret; 00194 uint8_t *pin, *pout; 00195 00196 if (fbdev->time_frame == AV_NOPTS_VALUE) 00197 fbdev->time_frame = av_gettime(); 00198 00199 /* wait based on the frame rate */ 00200 curtime = av_gettime(); 00201 delay = fbdev->time_frame - curtime; 00202 av_dlog(avctx, 00203 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", 00204 fbdev->time_frame, curtime, delay); 00205 if (delay > 0) { 00206 if (avctx->flags & AVFMT_FLAG_NONBLOCK) 00207 return AVERROR(EAGAIN); 00208 ts.tv_sec = delay / 1000000; 00209 ts.tv_nsec = (delay % 1000000) * 1000; 00210 while (nanosleep(&ts, &ts) < 0 && errno == EINTR); 00211 } 00212 /* compute the time of the next frame */ 00213 fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->fps); 00214 00215 if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0) 00216 return ret; 00217 00218 /* refresh fbdev->varinfo, visible data position may change at each call */ 00219 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) 00220 av_log(avctx, AV_LOG_WARNING, 00221 "Error refreshing variable info: %s\n", strerror(errno)); 00222 00223 pkt->pts = curtime; 00224 00225 /* compute visible data offset */ 00226 pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset + 00227 fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; 00228 pout = pkt->data; 00229 00230 // TODO it'd be nice if the lines were aligned 00231 for (i = 0; i < fbdev->heigth; i++) { 00232 memcpy(pout, pin, fbdev->frame_linesize); 00233 pin += fbdev->fixinfo.line_length; 00234 pout += fbdev->frame_linesize; 00235 } 00236 00237 return fbdev->frame_size; 00238 } 00239 00240 av_cold static int fbdev_read_close(AVFormatContext *avctx) 00241 { 00242 FBDevContext *fbdev = avctx->priv_data; 00243 00244 munmap(fbdev->data, fbdev->frame_size); 00245 close(fbdev->fd); 00246 00247 return 0; 00248 } 00249 00250 #define OFFSET(x) offsetof(FBDevContext, x) 00251 #define DEC AV_OPT_FLAG_DECODING_PARAM 00252 static const AVOption options[] = { 00253 { "framerate","", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC }, 00254 { NULL }, 00255 }; 00256 00257 static const AVClass fbdev_class = { 00258 .class_name = "fbdev indev", 00259 .item_name = av_default_item_name, 00260 .option = options, 00261 .version = LIBAVUTIL_VERSION_INT, 00262 }; 00263 00264 AVInputFormat ff_fbdev_demuxer = { 00265 .name = "fbdev", 00266 .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"), 00267 .priv_data_size = sizeof(FBDevContext), 00268 .read_header = fbdev_read_header, 00269 .read_packet = fbdev_read_packet, 00270 .read_close = fbdev_read_close, 00271 .flags = AVFMT_NOFILE, 00272 .priv_class = &fbdev_class, 00273 };