• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavdevice/fbdev.c

Go to the documentation of this file.
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 #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 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         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 av_cold static int fbdev_read_header(AVFormatContext *avctx,
00099                                      AVFormatParameters *ap)
00100 {
00101     FBDevContext *fbdev = avctx->priv_data;
00102     AVStream *st = NULL;
00103     enum PixelFormat pix_fmt;
00104     int ret, flags = O_RDONLY;
00105 
00106     ret = av_parse_video_rate(&fbdev->framerate_q, fbdev->framerate);
00107     if (ret < 0) {
00108         av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", fbdev->framerate);
00109         return ret;
00110     }
00111 
00112     if (!(st = avformat_new_stream(avctx, NULL)))
00113         return AVERROR(ENOMEM);
00114     avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
00115 
00116     /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
00117     if (avctx->flags & AVFMT_FLAG_NONBLOCK)
00118         flags |= O_NONBLOCK;
00119 
00120     if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
00121         ret = AVERROR(errno);
00122         av_log(avctx, AV_LOG_ERROR,
00123                "Could not open framebuffer device '%s': %s\n",
00124                avctx->filename, strerror(ret));
00125         return ret;
00126     }
00127 
00128     if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
00129         ret = AVERROR(errno);
00130         av_log(avctx, AV_LOG_ERROR,
00131                "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
00132         goto fail;
00133     }
00134 
00135     if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
00136         ret = AVERROR(errno);
00137         av_log(avctx, AV_LOG_ERROR,
00138                "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
00139         goto fail;
00140     }
00141 
00142     pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
00143     if (pix_fmt == PIX_FMT_NONE) {
00144         ret = AVERROR(EINVAL);
00145         av_log(avctx, AV_LOG_ERROR,
00146                "Framebuffer pixel format not supported.\n");
00147         goto fail;
00148     }
00149 
00150     fbdev->width           = fbdev->varinfo.xres;
00151     fbdev->height          = fbdev->varinfo.yres;
00152     fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
00153     fbdev->frame_linesize  = fbdev->width * fbdev->bytes_per_pixel;
00154     fbdev->frame_size      = fbdev->frame_linesize * fbdev->height;
00155     fbdev->time_frame      = AV_NOPTS_VALUE;
00156     fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
00157     if (fbdev->data == MAP_FAILED) {
00158         ret = AVERROR(errno);
00159         av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
00160         goto fail;
00161     }
00162 
00163     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00164     st->codec->codec_id   = CODEC_ID_RAWVIDEO;
00165     st->codec->width      = fbdev->width;
00166     st->codec->height     = fbdev->height;
00167     st->codec->pix_fmt    = pix_fmt;
00168     st->codec->time_base  = (AVRational){fbdev->framerate_q.den, fbdev->framerate_q.num};
00169     st->codec->bit_rate   =
00170         fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
00171 
00172     av_log(avctx, AV_LOG_INFO,
00173            "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
00174            fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
00175            av_pix_fmt_descriptors[pix_fmt].name,
00176            fbdev->framerate_q.num, fbdev->framerate_q.den,
00177            st->codec->bit_rate);
00178     return 0;
00179 
00180 fail:
00181     close(fbdev->fd);
00182     return ret;
00183 }
00184 
00185 static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
00186 {
00187     FBDevContext *fbdev = avctx->priv_data;
00188     int64_t curtime, delay;
00189     struct timespec ts;
00190     int i, ret;
00191     uint8_t *pin, *pout;
00192 
00193     if (fbdev->time_frame == AV_NOPTS_VALUE)
00194         fbdev->time_frame = av_gettime();
00195 
00196     /* wait based on the frame rate */
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         if (avctx->flags & AVFMT_FLAG_NONBLOCK)
00204             return AVERROR(EAGAIN);
00205         ts.tv_sec  =  delay / 1000000;
00206         ts.tv_nsec = (delay % 1000000) * 1000;
00207         while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
00208     }
00209     /* compute the time of the next frame */
00210     fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
00211 
00212     if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
00213         return ret;
00214 
00215     /* refresh fbdev->varinfo, visible data position may change at each call */
00216     if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
00217         av_log(avctx, AV_LOG_WARNING,
00218                "Error refreshing variable info: %s\n", strerror(errno));
00219 
00220     pkt->pts = curtime;
00221 
00222     /* compute visible data offset */
00223     pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
00224                         fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
00225     pout = pkt->data;
00226 
00227     // TODO it'd be nice if the lines were aligned
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 av_cold static 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 };
Generated on Thu Jan 24 2013 17:08:54 for Libav by doxygen 1.7.1