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