ffmpeg / libavdevice / fbdev.c @ 47860766
History | View | Annotate | Download (8.46 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Stefano Sabatini
|
3 |
* Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com>
|
4 |
* Copyright (C) 2002 Gunnar Monell <gmo@linux.nu>
|
5 |
*
|
6 |
* This file is part of FFmpeg.
|
7 |
*
|
8 |
* FFmpeg is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU Lesser General Public
|
10 |
* License as published by the Free Software Foundation; either
|
11 |
* version 2.1 of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* FFmpeg is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16 |
* Lesser General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU Lesser General Public
|
19 |
* License along with FFmpeg; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
21 |
*/
|
22 |
|
23 |
/**
|
24 |
* @file
|
25 |
* Linux framebuffer input device,
|
26 |
* inspired by code from fbgrab.c by Gunnar Monell.
|
27 |
* See also http://linux-fbdev.sourceforge.net/.
|
28 |
*/
|
29 |
|
30 |
/* #define DEBUG */
|
31 |
|
32 |
#include <unistd.h> |
33 |
#include <fcntl.h> |
34 |
#include <sys/ioctl.h> |
35 |
#include <sys/time.h> |
36 |
#include <sys/mman.h> |
37 |
#include <time.h> |
38 |
#include <linux/fb.h> |
39 |
|
40 |
#include "libavutil/mem.h" |
41 |
#include "libavutil/pixdesc.h" |
42 |
#include "libavformat/avformat.h" |
43 |
|
44 |
struct rgb_pixfmt_map_entry {
|
45 |
int bits_per_pixel;
|
46 |
int red_offset, green_offset, blue_offset, alpha_offset;
|
47 |
enum PixelFormat pixfmt;
|
48 |
}; |
49 |
|
50 |
static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = { |
51 |
// bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
|
52 |
{ 32, 0, 8, 16, 24, PIX_FMT_RGBA }, |
53 |
{ 32, 16, 8, 0, 24, PIX_FMT_BGRA }, |
54 |
{ 32, 8, 16, 24, 0, PIX_FMT_ARGB }, |
55 |
{ 32, 3, 2, 8, 0, PIX_FMT_ABGR }, |
56 |
{ 24, 0, 8, 16, 0, PIX_FMT_RGB24 }, |
57 |
{ 24, 16, 8, 0, 0, PIX_FMT_BGR24 }, |
58 |
}; |
59 |
|
60 |
static enum PixelFormat |
61 |
get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
|
62 |
{ |
63 |
int i;
|
64 |
|
65 |
for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) { |
66 |
struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
|
67 |
if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
|
68 |
entry->red_offset == varinfo->red.offset && |
69 |
entry->green_offset == varinfo->green.offset && |
70 |
entry->blue_offset == varinfo->blue.offset) |
71 |
return entry->pixfmt;
|
72 |
} |
73 |
|
74 |
return PIX_FMT_NONE;
|
75 |
} |
76 |
|
77 |
typedef struct { |
78 |
int frame_size; ///< size in bytes of a grabbed frame |
79 |
AVRational time_base; ///< time base
|
80 |
int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
|
81 |
|
82 |
int fd; ///< framebuffer device file descriptor |
83 |
int width, heigth; ///< assumed frame resolution |
84 |
int frame_linesize; ///< linesize of the output frame, it is assumed to be constant |
85 |
int bytes_per_pixel;
|
86 |
|
87 |
struct fb_var_screeninfo varinfo; ///< variable info; |
88 |
struct fb_fix_screeninfo fixinfo; ///< fixed info; |
89 |
|
90 |
uint8_t *data; ///< framebuffer data
|
91 |
} FBDevContext; |
92 |
|
93 |
av_cold static int fbdev_read_header(AVFormatContext *avctx, |
94 |
AVFormatParameters *ap) |
95 |
{ |
96 |
FBDevContext *fbdev = avctx->priv_data; |
97 |
AVStream *st = NULL;
|
98 |
enum PixelFormat pix_fmt;
|
99 |
int ret, flags = O_RDONLY;
|
100 |
|
101 |
if (!(st = av_new_stream(avctx, 0))) |
102 |
return AVERROR(ENOMEM);
|
103 |
av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */ |
104 |
|
105 |
if (ap->time_base.den <= 0) { |
106 |
av_log(avctx, AV_LOG_ERROR, "Invalid time base %d/%d\n",
|
107 |
ap->time_base.num, ap->time_base.den); |
108 |
return AVERROR(EINVAL);
|
109 |
} |
110 |
|
111 |
/* NONBLOCK is ignored by the fbdev driver, only set for consistency */
|
112 |
if (avctx->flags & AVFMT_FLAG_NONBLOCK)
|
113 |
flags |= O_NONBLOCK; |
114 |
|
115 |
if ((fbdev->fd = open(avctx->filename, flags)) == -1) { |
116 |
ret = AVERROR(errno); |
117 |
av_log(avctx, AV_LOG_ERROR, |
118 |
"Could not open framebuffer device '%s': %s\n",
|
119 |
avctx->filename, strerror(ret)); |
120 |
return ret;
|
121 |
} |
122 |
|
123 |
if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { |
124 |
ret = AVERROR(errno); |
125 |
av_log(avctx, AV_LOG_ERROR, |
126 |
"FBIOGET_VSCREENINFO: %s\n", strerror(errno));
|
127 |
goto fail;
|
128 |
} |
129 |
|
130 |
if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) { |
131 |
ret = AVERROR(errno); |
132 |
av_log(avctx, AV_LOG_ERROR, |
133 |
"FBIOGET_FSCREENINFO: %s\n", strerror(errno));
|
134 |
goto fail;
|
135 |
} |
136 |
|
137 |
pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo); |
138 |
if (pix_fmt == PIX_FMT_NONE) {
|
139 |
ret = AVERROR(EINVAL); |
140 |
av_log(avctx, AV_LOG_ERROR, |
141 |
"Framebuffer pixel format not supported.\n");
|
142 |
goto fail;
|
143 |
} |
144 |
|
145 |
fbdev->width = fbdev->varinfo.xres; |
146 |
fbdev->heigth = fbdev->varinfo.yres; |
147 |
fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3; |
148 |
fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel; |
149 |
fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth; |
150 |
fbdev->time_base = ap->time_base; |
151 |
fbdev->time_frame = AV_NOPTS_VALUE; |
152 |
fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0); |
153 |
if (fbdev->data == MAP_FAILED) {
|
154 |
ret = AVERROR(errno); |
155 |
av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
|
156 |
goto fail;
|
157 |
} |
158 |
|
159 |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
160 |
st->codec->codec_id = CODEC_ID_RAWVIDEO; |
161 |
st->codec->width = fbdev->width; |
162 |
st->codec->height = fbdev->heigth; |
163 |
st->codec->pix_fmt = pix_fmt; |
164 |
st->codec->time_base = ap->time_base; |
165 |
st->codec->bit_rate = |
166 |
fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel / av_q2d(ap->time_base) * 8;
|
167 |
|
168 |
av_log(avctx, AV_LOG_INFO, |
169 |
"w:%d h:%d bpp:%d pixfmt:%s tb:%d/%d bit_rate:%d\n",
|
170 |
fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel, |
171 |
av_pix_fmt_descriptors[pix_fmt].name, |
172 |
ap->time_base.num, ap->time_base.den, |
173 |
st->codec->bit_rate); |
174 |
return 0; |
175 |
|
176 |
fail:
|
177 |
close(fbdev->fd); |
178 |
return ret;
|
179 |
} |
180 |
|
181 |
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) |
182 |
{ |
183 |
FBDevContext *fbdev = avctx->priv_data; |
184 |
int64_t curtime, delay; |
185 |
struct timespec ts;
|
186 |
int i, ret;
|
187 |
uint8_t *pin, *pout; |
188 |
|
189 |
if (fbdev->time_frame == AV_NOPTS_VALUE)
|
190 |
fbdev->time_frame = av_gettime(); |
191 |
|
192 |
/* wait based on the frame rate */
|
193 |
while (1) { |
194 |
curtime = av_gettime(); |
195 |
delay = fbdev->time_frame - curtime; |
196 |
av_dlog(avctx, |
197 |
"time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", |
198 |
fbdev->time_frame, curtime, delay); |
199 |
if (delay <= 0) { |
200 |
fbdev->time_frame += INT64_C(1000000) * av_q2d(fbdev->time_base);
|
201 |
break;
|
202 |
} |
203 |
if (avctx->flags & AVFMT_FLAG_NONBLOCK)
|
204 |
return AVERROR(EAGAIN);
|
205 |
ts.tv_sec = delay / 1000000;
|
206 |
ts.tv_nsec = (delay % 1000000) * 1000; |
207 |
while (nanosleep(&ts, &ts) < 0 && errno == EINTR); |
208 |
} |
209 |
|
210 |
if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0) |
211 |
return ret;
|
212 |
|
213 |
/* refresh fbdev->varinfo, visible data position may change at each call */
|
214 |
if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) |
215 |
av_log(avctx, AV_LOG_WARNING, |
216 |
"Error refreshing variable info: %s\n", strerror(errno));
|
217 |
|
218 |
pkt->pts = curtime; |
219 |
|
220 |
/* compute visible data offset */
|
221 |
pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset + |
222 |
fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; |
223 |
pout = pkt->data; |
224 |
|
225 |
for (i = 0; i < fbdev->heigth; i++) { |
226 |
memcpy(pout, pin, fbdev->frame_linesize); |
227 |
pin += fbdev->fixinfo.line_length; |
228 |
pout += fbdev->frame_linesize; |
229 |
} |
230 |
|
231 |
return fbdev->frame_size;
|
232 |
} |
233 |
|
234 |
av_cold static int fbdev_read_close(AVFormatContext *avctx) |
235 |
{ |
236 |
FBDevContext *fbdev = avctx->priv_data; |
237 |
|
238 |
munmap(fbdev->data, fbdev->frame_size); |
239 |
close(fbdev->fd); |
240 |
|
241 |
return 0; |
242 |
} |
243 |
|
244 |
AVInputFormat ff_fbdev_demuxer = { |
245 |
.name = "fbdev",
|
246 |
.long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
|
247 |
.priv_data_size = sizeof(FBDevContext),
|
248 |
.read_header = fbdev_read_header, |
249 |
.read_packet = fbdev_read_packet, |
250 |
.read_close = fbdev_read_close, |
251 |
.flags = AVFMT_NOFILE, |
252 |
}; |