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 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/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 "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 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 while (1) {
00198 curtime = av_gettime();
00199 delay = fbdev->time_frame - curtime;
00200 av_dlog(avctx,
00201 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
00202 fbdev->time_frame, curtime, delay);
00203 if (delay <= 0) {
00204 fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
00205 break;
00206 }
00207 if (avctx->flags & AVFMT_FLAG_NONBLOCK)
00208 return AVERROR(EAGAIN);
00209 ts.tv_sec = delay / 1000000;
00210 ts.tv_nsec = (delay % 1000000) * 1000;
00211 while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
00212 }
00213 
00214 if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
00215 return ret;
00216 
00217 /* refresh fbdev->varinfo, visible data position may change at each call */
00218 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
00219 av_log(avctx, AV_LOG_WARNING,
00220 "Error refreshing variable info: %s\n", strerror(errno));
00221 
00222 pkt->pts = curtime;
00223 
00224 /* compute visible data offset */
00225 pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
00226 fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
00227 pout = pkt->data;
00228 
00229 for (i = 0; i < fbdev->height; i++) {
00230 memcpy(pout, pin, fbdev->frame_linesize);
00231 pin += fbdev->fixinfo.line_length;
00232 pout += fbdev->frame_linesize;
00233 }
00234 
00235 return fbdev->frame_size;
00236 }
00237 
00238 av_cold static int fbdev_read_close(AVFormatContext *avctx)
00239 {
00240 FBDevContext *fbdev = avctx->priv_data;
00241 
00242 munmap(fbdev->data, fbdev->frame_size);
00243 close(fbdev->fd);
00244 
00245 return 0;
00246 }
00247 
00248 #define OFFSET(x) offsetof(FBDevContext, x)
00249 #define DEC AV_OPT_FLAG_DECODING_PARAM
00250 static const AVOption options[] = {
00251 { "framerate","", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
00252 { NULL },
00253 };
00254 
00255 static const AVClass fbdev_class = {
00256 .class_name = "fbdev indev",
00257 .item_name = av_default_item_name,
00258 .option = options,
00259 .version = LIBAVUTIL_VERSION_INT,
00260 };
00261 
00262 AVInputFormat ff_fbdev_demuxer = {
00263 .name = "fbdev",
00264 .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
00265 .priv_data_size = sizeof(FBDevContext),
00266 .read_header = fbdev_read_header,
00267 .read_packet = fbdev_read_packet,
00268 .read_close = fbdev_read_close,
00269 .flags = AVFMT_NOFILE,
00270 .priv_class = &fbdev_class,
00271 };

Generated on Fri Oct 26 02:46:01 2012 for FFmpeg by doxygen 1.5.8

AltStyle によって変換されたページ (->オリジナル) /