Browse code

framebuffer device demuxer

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Stefano Sabatini authored on 2011/03/08 02:54:52
Showing 7 changed files
... ...
@@ -80,6 +80,7 @@ version <next>:
80 80
 - Bitmap Brothers JV playback system
81 81
 - Apple HTTP Live Streaming protocol handler
82 82
 - sndio support for playback and record
83
+- Linux framebuffer input device added
83 84
 
84 85
 
85 86
 version 0.6:
... ...
@@ -1430,6 +1430,7 @@ alsa_indev_deps="alsa_asoundlib_h snd_pcm_htimestamp"
1430 1430
 alsa_outdev_deps="alsa_asoundlib_h"
1431 1431
 bktr_indev_deps_any="dev_bktr_ioctl_bt848_h machine_ioctl_bt848_h dev_video_bktr_ioctl_bt848_h dev_ic_bt8xx_h"
1432 1432
 dv1394_indev_deps="dv1394 dv_demuxer"
1433
+fbdev_indev_deps="linux_fb_h"
1433 1434
 jack_indev_deps="jack_jack_h"
1434 1435
 libdc1394_indev_deps="libdc1394"
1435 1436
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
... ...
@@ -2897,6 +2898,7 @@ fi
2897 2897
 
2898 2898
 texi2html -version > /dev/null 2>&1 && enable texi2html || disable texi2html
2899 2899
 
2900
+check_header linux/fb.h
2900 2901
 check_header linux/videodev.h
2901 2902
 check_header linux/videodev2.h
2902 2903
 check_header sys/videoio.h
... ...
@@ -59,6 +59,31 @@ BSD video input device.
59 59
 
60 60
 Linux DV 1394 input device.
61 61
 
62
+@section fbdev
63
+
64
+Linux framebuffer input device.
65
+
66
+The Linux framebuffer is a graphic hardware-independent abstraction
67
+layer to show graphics on a computer monitor, typically on the
68
+console. It is accessed through a file device node, usually
69
+@file{/dev/fb0}.
70
+
71
+For more detailed information read the file
72
+Documentation/fb/framebuffer.txt included in the Linux source tree.
73
+
74
+To record from the framebuffer device @file{/dev/fb0} with
75
+@file{ffmpeg}:
76
+@example
77
+ffmpeg -f fbdev -r 10 -i /dev/fb0 out.avi
78
+@end example
79
+
80
+You can take a single screenshot image with the command:
81
+@example
82
+ffmpeg -f fbdev -vframes 1 -r 1 -i /dev/fb0 screenshot.jpeg
83
+@end example
84
+
85
+See also @url{http://linux-fbdev.sourceforge.net/}, and fbset(1).
86
+
62 87
 @section jack
63 88
 
64 89
 JACK input device.
... ...
@@ -14,6 +14,7 @@ OBJS-$(CONFIG_ALSA_OUTDEV)               += alsa-audio-common.o \
14 14
                                             alsa-audio-enc.o
15 15
 OBJS-$(CONFIG_BKTR_INDEV)                += bktr.o
16 16
 OBJS-$(CONFIG_DV1394_INDEV)              += dv1394.o
17
+OBJS-$(CONFIG_FBDEV_INDEV)               += fbdev.o
17 18
 OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o
18 19
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
19 20
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
... ...
@@ -42,6 +42,7 @@ void avdevice_register_all(void)
42 42
     REGISTER_INOUTDEV (ALSA, alsa);
43 43
     REGISTER_INDEV    (BKTR, bktr);
44 44
     REGISTER_INDEV    (DV1394, dv1394);
45
+    REGISTER_INDEV    (FBDEV, fbdev);
45 46
     REGISTER_INDEV    (JACK, jack);
46 47
     REGISTER_INOUTDEV (OSS, oss);
47 48
     REGISTER_INOUTDEV (SNDIO, sndio);
... ...
@@ -22,7 +22,7 @@
22 22
 #include "libavutil/avutil.h"
23 23
 
24 24
 #define LIBAVDEVICE_VERSION_MAJOR 52
25
-#define LIBAVDEVICE_VERSION_MINOR  3
25
+#define LIBAVDEVICE_VERSION_MINOR  4
26 26
 #define LIBAVDEVICE_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,250 @@
0
+/*
1
+ * Copyright (c) 2011 Stefano Sabatini
2
+ * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com>
3
+ * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu>
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * Libav is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file
24
+ * Linux framebuffer input device,
25
+ * inspired by code from fbgrab.c by Gunnar Monell.
26
+ * See also http://linux-fbdev.sourceforge.net/.
27
+ */
28
+
29
+/* #define DEBUG */
30
+
31
+#include <unistd.h>
32
+#include <fcntl.h>
33
+#include <sys/ioctl.h>
34
+#include <sys/time.h>
35
+#include <sys/mman.h>
36
+#include <time.h>
37
+#include <linux/fb.h>
38
+
39
+#include "libavutil/mem.h"
40
+#include "libavutil/pixdesc.h"
41
+#include "libavformat/avformat.h"
42
+
43
+struct rgb_pixfmt_map_entry {
44
+    int bits_per_pixel;
45
+    int red_offset, green_offset, blue_offset, alpha_offset;
46
+    enum PixelFormat pixfmt;
47
+};
48
+
49
+static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
50
+    // bpp, red_offset,  green_offset, blue_offset, alpha_offset, pixfmt
51
+    {  32,       0,           8,          16,           24,   PIX_FMT_RGBA  },
52
+    {  32,      16,           8,           0,           24,   PIX_FMT_BGRA  },
53
+    {  32,       8,          16,          24,            0,   PIX_FMT_ARGB  },
54
+    {  32,       3,           2,           8,            0,   PIX_FMT_ABGR  },
55
+    {  24,       0,           8,          16,            0,   PIX_FMT_RGB24 },
56
+    {  24,      16,           8,           0,            0,   PIX_FMT_BGR24 },
57
+};
58
+
59
+static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
60
+{
61
+    int i;
62
+
63
+    for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
64
+        struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
65
+        if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
66
+            entry->red_offset     == varinfo->red.offset     &&
67
+            entry->green_offset   == varinfo->green.offset   &&
68
+            entry->blue_offset    == varinfo->blue.offset)
69
+            return entry->pixfmt;
70
+    }
71
+
72
+    return PIX_FMT_NONE;
73
+}
74
+
75
+typedef struct {
76
+    int frame_size;          ///< size in bytes of a grabbed frame
77
+    AVRational time_base;    ///< time base
78
+    int64_t time_frame;      ///< time for the next frame to output (in 1/1000000 units)
79
+
80
+    int fd;                  ///< framebuffer device file descriptor
81
+    int width, heigth;       ///< assumed frame resolution
82
+    int frame_linesize;      ///< linesize of the output frame, it is assumed to be constant
83
+    int bytes_per_pixel;
84
+
85
+    struct fb_var_screeninfo varinfo; ///< variable info;
86
+    struct fb_fix_screeninfo fixinfo; ///< fixed    info;
87
+
88
+    uint8_t *data;           ///< framebuffer data
89
+} FBDevContext;
90
+
91
+av_cold static int fbdev_read_header(AVFormatContext *avctx,
92
+                                     AVFormatParameters *ap)
93
+{
94
+    FBDevContext *fbdev = avctx->priv_data;
95
+    AVStream *st = NULL;
96
+    enum PixelFormat pix_fmt;
97
+    int ret, flags = O_RDONLY;
98
+
99
+    if (!(st = av_new_stream(avctx, 0)))
100
+        return AVERROR(ENOMEM);
101
+    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
102
+
103
+    if (ap->time_base.den <= 0) {
104
+        av_log(avctx, AV_LOG_ERROR, "Invalid time base %d/%d\n",
105
+               ap->time_base.num, ap->time_base.den);
106
+        return AVERROR(EINVAL);
107
+    }
108
+
109
+    /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
110
+    if (avctx->flags & AVFMT_FLAG_NONBLOCK)
111
+        flags |= O_NONBLOCK;
112
+
113
+    if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
114
+        ret = AVERROR(errno);
115
+        av_log(avctx, AV_LOG_ERROR,
116
+               "Could not open framebuffer device '%s': %s\n",
117
+               avctx->filename, strerror(ret));
118
+        return ret;
119
+    }
120
+
121
+    if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
122
+        ret = AVERROR(errno);
123
+        av_log(avctx, AV_LOG_ERROR,
124
+               "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
125
+        goto fail;
126
+    }
127
+
128
+    if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
129
+        ret = AVERROR(errno);
130
+        av_log(avctx, AV_LOG_ERROR,
131
+               "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
132
+        goto fail;
133
+    }
134
+
135
+    pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
136
+    if (pix_fmt == PIX_FMT_NONE) {
137
+        ret = AVERROR(EINVAL);
138
+        av_log(avctx, AV_LOG_ERROR,
139
+               "Framebuffer pixel format not supported.\n");
140
+        goto fail;
141
+    }
142
+
143
+    fbdev->width           = fbdev->varinfo.xres;
144
+    fbdev->heigth          = fbdev->varinfo.yres;
145
+    fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
146
+    fbdev->frame_linesize  = fbdev->width * fbdev->bytes_per_pixel;
147
+    fbdev->frame_size      = fbdev->frame_linesize * fbdev->heigth;
148
+    fbdev->time_base       = ap->time_base;
149
+    fbdev->time_frame      = AV_NOPTS_VALUE;
150
+    fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
151
+    if (fbdev->data == MAP_FAILED) {
152
+        ret = AVERROR(errno);
153
+        av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
154
+        goto fail;
155
+    }
156
+
157
+    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
158
+    st->codec->codec_id   = CODEC_ID_RAWVIDEO;
159
+    st->codec->width      = fbdev->width;
160
+    st->codec->height     = fbdev->heigth;
161
+    st->codec->pix_fmt    = pix_fmt;
162
+    st->codec->time_base  = ap->time_base;
163
+    st->codec->bit_rate   =
164
+        fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel / av_q2d(ap->time_base) * 8;
165
+
166
+    av_log(avctx, AV_LOG_INFO,
167
+           "w:%d h:%d bpp:%d pixfmt:%s tb:%d/%d bit_rate:%d\n",
168
+           fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel,
169
+           av_pix_fmt_descriptors[pix_fmt].name,
170
+           ap->time_base.num, ap->time_base.den,
171
+           st->codec->bit_rate);
172
+    return 0;
173
+
174
+fail:
175
+    close(fbdev->fd);
176
+    return ret;
177
+}
178
+
179
+static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
180
+{
181
+    FBDevContext *fbdev = avctx->priv_data;
182
+    int64_t curtime, delay;
183
+    struct timespec ts;
184
+    int i, ret;
185
+    uint8_t *pin, *pout;
186
+
187
+    if (fbdev->time_frame == AV_NOPTS_VALUE)
188
+        fbdev->time_frame = av_gettime();
189
+
190
+    /* wait based on the frame rate */
191
+    curtime = av_gettime();
192
+    delay = fbdev->time_frame - curtime;
193
+    av_dlog(avctx,
194
+            "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
195
+            fbdev->time_frame, curtime, delay);
196
+    if (delay > 0) {
197
+        if (avctx->flags & AVFMT_FLAG_NONBLOCK)
198
+            return AVERROR(EAGAIN);
199
+        ts.tv_sec  =  delay / 1000000;
200
+        ts.tv_nsec = (delay % 1000000) * 1000;
201
+        while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
202
+    }
203
+    /* compute the time of the next frame */
204
+    fbdev->time_frame += INT64_C(1000000) * av_q2d(fbdev->time_base);
205
+
206
+    if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
207
+        return ret;
208
+
209
+    /* refresh fbdev->varinfo, visible data position may change at each call */
210
+    if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
211
+        av_log(avctx, AV_LOG_WARNING,
212
+               "Error refreshing variable info: %s\n", strerror(errno));
213
+
214
+    pkt->pts = curtime;
215
+
216
+    /* compute visible data offset */
217
+    pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
218
+                        fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
219
+    pout = pkt->data;
220
+
221
+    // TODO it'd be nice if the lines were aligned
222
+    for (i = 0; i < fbdev->heigth; i++) {
223
+        memcpy(pout, pin, fbdev->frame_linesize);
224
+        pin  += fbdev->fixinfo.line_length;
225
+        pout += fbdev->frame_linesize;
226
+    }
227
+
228
+    return fbdev->frame_size;
229
+}
230
+
231
+av_cold static int fbdev_read_close(AVFormatContext *avctx)
232
+{
233
+    FBDevContext *fbdev = avctx->priv_data;
234
+
235
+    munmap(fbdev->data, fbdev->frame_size);
236
+    close(fbdev->fd);
237
+
238
+    return 0;
239
+}
240
+
241
+AVInputFormat ff_fbdev_demuxer = {
242
+    .name           = "fbdev",
243
+    .long_name      = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
244
+    .priv_data_size = sizeof(FBDevContext),
245
+    .read_header    = fbdev_read_header,
246
+    .read_packet    = fbdev_read_packet,
247
+    .read_close     = fbdev_read_close,
248
+    .flags          = AVFMT_NOFILE,
249
+};