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 FFmpeg. 00007 * 00008 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/mman.h> 00036 #include <time.h> 00037 #include <linux/fb.h> 00038 00039 #include "libavutil/log.h" 00040 #include "libavutil/mem.h" 00041 #include "libavutil/opt.h" 00042 #include "libavutil/time.h" 00043 #include "libavutil/parseutils.h" 00044 #include "libavutil/pixdesc.h" 00045 #include "avdevice.h" 00046 #include "libavformat/internal.h" 00047 00048 struct rgb_pixfmt_map_entry { 00049 int bits_per_pixel; 00050 int red_offset, green_offset, blue_offset, alpha_offset; 00051 enum PixelFormat pixfmt; 00052 }; 00053 00054 static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = { 00055 // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt 00056 { 32, 0, 8, 16, 24, PIX_FMT_RGBA }, 00057 { 32, 16, 8, 0, 24, PIX_FMT_BGRA }, 00058 { 32, 8, 16, 24, 0, PIX_FMT_ARGB }, 00059 { 32, 3, 2, 8, 0, PIX_FMT_ABGR }, 00060 { 24, 0, 8, 16, 0, PIX_FMT_RGB24 }, 00061 { 24, 16, 8, 0, 0, PIX_FMT_BGR24 }, 00062 }; 00063 00064 static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo) 00065 { 00066 int i; 00067 00068 for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) { 00069 const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i]; 00070 if (entry->bits_per_pixel == varinfo->bits_per_pixel && 00071 entry->red_offset == varinfo->red.offset && 00072 entry->green_offset == varinfo->green.offset && 00073 entry->blue_offset == varinfo->blue.offset) 00074 return entry->pixfmt; 00075 } 00076 00077 return PIX_FMT_NONE; 00078 } 00079 00080 typedef struct { 00081 AVClass *class; 00082 int frame_size; 00083 AVRational framerate_q; 00084 char *framerate; 00085 int64_t time_frame; 00086 00087 int fd; 00088 int width, height; 00089 int frame_linesize; 00090 int bytes_per_pixel; 00091 00092 struct fb_var_screeninfo varinfo; 00093 struct fb_fix_screeninfo fixinfo; 00094 00095 uint8_t *data; 00096 } FBDevContext; 00097 00098 static av_cold int fbdev_read_header(AVFormatContext *avctx) 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->framerate_q, fbdev->framerate); 00106 if (ret < 0) { 00107 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", fbdev->framerate); 00108 return ret; 00109 } 00110 00111 if (!(st = avformat_new_stream(avctx, NULL))) 00112 return AVERROR(ENOMEM); 00113 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */ 00114 00115 /* NONBLOCK is ignored by the fbdev driver, only set for consistency */ 00116 if (avctx->flags & AVFMT_FLAG_NONBLOCK) 00117 flags |= O_NONBLOCK; 00118 00119 if ((fbdev->fd = open(avctx->filename, flags)) == -1) { 00120 ret = AVERROR(errno); 00121 av_log(avctx, AV_LOG_ERROR, 00122 "Could not open framebuffer device '%s': %s\n", 00123 avctx->filename, strerror(ret)); 00124 return ret; 00125 } 00126 00127 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { 00128 ret = AVERROR(errno); 00129 av_log(avctx, AV_LOG_ERROR, 00130 "FBIOGET_VSCREENINFO: %s\n", strerror(errno)); 00131 goto fail; 00132 } 00133 00134 if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) { 00135 ret = AVERROR(errno); 00136 av_log(avctx, AV_LOG_ERROR, 00137 "FBIOGET_FSCREENINFO: %s\n", strerror(errno)); 00138 goto fail; 00139 } 00140 00141 pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo); 00142 if (pix_fmt == PIX_FMT_NONE) { 00143 ret = AVERROR(EINVAL); 00144 av_log(avctx, AV_LOG_ERROR, 00145 "Framebuffer pixel format not supported.\n"); 00146 goto fail; 00147 } 00148 00149 fbdev->width = fbdev->varinfo.xres; 00150 fbdev->height = fbdev->varinfo.yres; 00151 fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3; 00152 fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel; 00153 fbdev->frame_size = fbdev->frame_linesize * fbdev->height; 00154 fbdev->time_frame = AV_NOPTS_VALUE; 00155 fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0); 00156 if (fbdev->data == MAP_FAILED) { 00157 ret = AVERROR(errno); 00158 av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno)); 00159 goto fail; 00160 } 00161 00162 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00163 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO; 00164 st->codec->width = fbdev->width; 00165 st->codec->height = fbdev->height; 00166 st->codec->pix_fmt = pix_fmt; 00167 st->codec->time_base = av_inv_q(fbdev->framerate_q); 00168 st->codec->bit_rate = 00169 fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8; 00170 00171 av_log(avctx, AV_LOG_INFO, 00172 "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n", 00173 fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel, 00174 av_pix_fmt_descriptors[pix_fmt].name, 00175 fbdev->framerate_q.num, fbdev->framerate_q.den, 00176 st->codec->bit_rate); 00177 return 0; 00178 00179 fail: 00180 close(fbdev->fd); 00181 return ret; 00182 } 00183 00184 static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) 00185 { 00186 FBDevContext *fbdev = avctx->priv_data; 00187 int64_t curtime, delay; 00188 struct timespec ts; 00189 int i, ret; 00190 uint8_t *pin, *pout; 00191 00192 if (fbdev->time_frame == AV_NOPTS_VALUE) 00193 fbdev->time_frame = av_gettime(); 00194 00195 /* wait based on the frame rate */ 00196 while (1) { 00197 curtime = av_gettime(); 00198 delay = fbdev->time_frame - curtime; 00199 av_dlog(avctx, 00200 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", 00201 fbdev->time_frame, curtime, delay); 00202 if (delay <= 0) { 00203 fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q); 00204 break; 00205 } 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 00213 if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0) 00214 return ret; 00215 00216 /* refresh fbdev->varinfo, visible data position may change at each call */ 00217 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) 00218 av_log(avctx, AV_LOG_WARNING, 00219 "Error refreshing variable info: %s\n", strerror(errno)); 00220 00221 pkt->pts = curtime; 00222 00223 /* compute visible data offset */ 00224 pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset + 00225 fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; 00226 pout = pkt->data; 00227 00228 for (i = 0; i < fbdev->height; i++) { 00229 memcpy(pout, pin, fbdev->frame_linesize); 00230 pin += fbdev->fixinfo.line_length; 00231 pout += fbdev->frame_linesize; 00232 } 00233 00234 return fbdev->frame_size; 00235 } 00236 00237 static av_cold int fbdev_read_close(AVFormatContext *avctx) 00238 { 00239 FBDevContext *fbdev = avctx->priv_data; 00240 00241 munmap(fbdev->data, fbdev->frame_size); 00242 close(fbdev->fd); 00243 00244 return 0; 00245 } 00246 00247 #define OFFSET(x) offsetof(FBDevContext, x) 00248 #define DEC AV_OPT_FLAG_DECODING_PARAM 00249 static const AVOption options[] = { 00250 { "framerate","", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC }, 00251 { NULL }, 00252 }; 00253 00254 static const AVClass fbdev_class = { 00255 .class_name = "fbdev indev", 00256 .item_name = av_default_item_name, 00257 .option = options, 00258 .version = LIBAVUTIL_VERSION_INT, 00259 }; 00260 00261 AVInputFormat ff_fbdev_demuxer = { 00262 .name = "fbdev", 00263 .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"), 00264 .priv_data_size = sizeof(FBDevContext), 00265 .read_header = fbdev_read_header, 00266 .read_packet = fbdev_read_packet, 00267 .read_close = fbdev_read_close, 00268 .flags = AVFMT_NOFILE, 00269 .priv_class = &fbdev_class, 00270 };