Line |
Branch |
Exec |
Source |
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 http://linux-fbdev.sourceforge.net/ |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
#include <unistd.h> |
31 |
|
|
#include <fcntl.h> |
32 |
|
|
#include <sys/ioctl.h> |
33 |
|
|
#include <sys/mman.h> |
34 |
|
|
#include <time.h> |
35 |
|
|
#include <linux/fb.h> |
36 |
|
|
|
37 |
|
|
#include "libavutil/file_open.h" |
38 |
|
|
#include "libavutil/internal.h" |
39 |
|
|
#include "libavutil/log.h" |
40 |
|
|
#include "libavutil/opt.h" |
41 |
|
|
#include "libavutil/time.h" |
42 |
|
|
#include "libavutil/parseutils.h" |
43 |
|
|
#include "libavutil/pixdesc.h" |
44 |
|
|
#include "libavformat/demux.h" |
45 |
|
|
#include "libavformat/internal.h" |
46 |
|
|
#include "avdevice.h" |
47 |
|
|
#include "fbdev_common.h" |
48 |
|
|
|
49 |
|
|
typedef struct FBDevContext { |
50 |
|
|
AVClass *class; ///< class for private options |
51 |
|
|
int frame_size; ///< size in bytes of a grabbed frame |
52 |
|
|
AVRational framerate_q; ///< framerate |
53 |
|
|
int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units) |
54 |
|
|
|
55 |
|
|
int fd; ///< framebuffer device file descriptor |
56 |
|
|
int width, height; ///< assumed frame resolution |
57 |
|
|
int frame_linesize; ///< linesize of the output frame, it is assumed to be constant |
58 |
|
|
int bytes_per_pixel; |
59 |
|
|
|
60 |
|
|
struct fb_var_screeninfo varinfo; ///< variable info; |
61 |
|
|
struct fb_fix_screeninfo fixinfo; ///< fixed info; |
62 |
|
|
|
63 |
|
|
uint8_t *data; ///< framebuffer data |
64 |
|
|
} FBDevContext; |
65 |
|
|
|
66 |
|
✗ |
static av_cold int fbdev_read_header(AVFormatContext *avctx) |
67 |
|
|
{ |
68 |
|
✗ |
FBDevContext *fbdev = avctx->priv_data; |
69 |
|
✗ |
AVStream *st = NULL; |
70 |
|
|
enum AVPixelFormat pix_fmt; |
71 |
|
✗ |
int ret, flags = O_RDONLY; |
72 |
|
|
const char* device; |
73 |
|
|
|
74 |
|
✗ |
if (!(st = avformat_new_stream(avctx, NULL))) |
75 |
|
✗ |
return AVERROR(ENOMEM); |
76 |
|
✗ |
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */ |
77 |
|
|
|
78 |
|
|
/* NONBLOCK is ignored by the fbdev driver, only set for consistency */ |
79 |
|
✗ |
if (avctx->flags & AVFMT_FLAG_NONBLOCK) |
80 |
|
✗ |
flags |= O_NONBLOCK; |
81 |
|
|
|
82 |
|
✗ |
if (avctx->url[0]) |
83 |
|
✗ |
device = avctx->url; |
84 |
|
|
else |
85 |
|
✗ |
device = ff_fbdev_default_device(); |
86 |
|
|
|
87 |
|
✗ |
if ((fbdev->fd = avpriv_open(device, flags)) == -1) { |
88 |
|
✗ |
ret = AVERROR(errno); |
89 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, |
90 |
|
|
"Could not open framebuffer device '%s': %s\n", |
91 |
|
✗ |
device, av_err2str(ret)); |
92 |
|
✗ |
return ret; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
✗ |
if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { |
96 |
|
✗ |
ret = AVERROR(errno); |
97 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, |
98 |
|
✗ |
"FBIOGET_VSCREENINFO: %s\n", av_err2str(ret)); |
99 |
|
✗ |
goto fail; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
✗ |
if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) { |
103 |
|
✗ |
ret = AVERROR(errno); |
104 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, |
105 |
|
✗ |
"FBIOGET_FSCREENINFO: %s\n", av_err2str(ret)); |
106 |
|
✗ |
goto fail; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
✗ |
pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo); |
110 |
|
✗ |
if (pix_fmt == AV_PIX_FMT_NONE) { |
111 |
|
✗ |
ret = AVERROR(EINVAL); |
112 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, |
113 |
|
|
"Framebuffer pixel format not supported.\n"); |
114 |
|
✗ |
goto fail; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
fbdev->width = fbdev->varinfo.xres; |
118 |
|
✗ |
fbdev->height = fbdev->varinfo.yres; |
119 |
|
✗ |
fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3; |
120 |
|
✗ |
fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel; |
121 |
|
✗ |
fbdev->frame_size = fbdev->frame_linesize * fbdev->height; |
122 |
|
✗ |
fbdev->time_frame = AV_NOPTS_VALUE; |
123 |
|
✗ |
fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0); |
124 |
|
✗ |
if (fbdev->data == MAP_FAILED) { |
125 |
|
✗ |
ret = AVERROR(errno); |
126 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret)); |
127 |
|
✗ |
goto fail; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
✗ |
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
131 |
|
✗ |
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; |
132 |
|
✗ |
st->codecpar->width = fbdev->width; |
133 |
|
✗ |
st->codecpar->height = fbdev->height; |
134 |
|
✗ |
st->codecpar->format = pix_fmt; |
135 |
|
✗ |
st->avg_frame_rate = fbdev->framerate_q; |
136 |
|
✗ |
st->codecpar->bit_rate = |
137 |
|
✗ |
fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8; |
138 |
|
|
|
139 |
|
✗ |
av_log(avctx, AV_LOG_INFO, |
140 |
|
|
"w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%"PRId64"\n", |
141 |
|
|
fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel, |
142 |
|
|
av_get_pix_fmt_name(pix_fmt), |
143 |
|
|
fbdev->framerate_q.num, fbdev->framerate_q.den, |
144 |
|
✗ |
st->codecpar->bit_rate); |
145 |
|
✗ |
return 0; |
146 |
|
|
|
147 |
|
✗ |
fail: |
148 |
|
✗ |
close(fbdev->fd); |
149 |
|
✗ |
return ret; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
✗ |
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) |
153 |
|
|
{ |
154 |
|
✗ |
FBDevContext *fbdev = avctx->priv_data; |
155 |
|
|
int64_t curtime, delay; |
156 |
|
|
struct timespec ts; |
157 |
|
|
int i, ret; |
158 |
|
|
uint8_t *pin, *pout; |
159 |
|
|
|
160 |
|
✗ |
if (fbdev->time_frame == AV_NOPTS_VALUE) |
161 |
|
✗ |
fbdev->time_frame = av_gettime_relative(); |
162 |
|
|
|
163 |
|
|
/* wait based on the frame rate */ |
164 |
|
|
while (1) { |
165 |
|
✗ |
curtime = av_gettime_relative(); |
166 |
|
✗ |
delay = fbdev->time_frame - curtime; |
167 |
|
✗ |
av_log(avctx, AV_LOG_TRACE, |
168 |
|
|
"time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", |
169 |
|
|
fbdev->time_frame, curtime, delay); |
170 |
|
✗ |
if (delay <= 0) { |
171 |
|
✗ |
fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q); |
172 |
|
✗ |
break; |
173 |
|
|
} |
174 |
|
✗ |
if (avctx->flags & AVFMT_FLAG_NONBLOCK) |
175 |
|
✗ |
return AVERROR(EAGAIN); |
176 |
|
✗ |
ts.tv_sec = delay / 1000000; |
177 |
|
✗ |
ts.tv_nsec = (delay % 1000000) * 1000; |
178 |
|
✗ |
while (nanosleep(&ts, &ts) < 0 && errno == EINTR); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
✗ |
if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0) |
182 |
|
✗ |
return ret; |
183 |
|
|
|
184 |
|
|
/* refresh fbdev->varinfo, visible data position may change at each call */ |
185 |
|
✗ |
if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { |
186 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, |
187 |
|
✗ |
"Error refreshing variable info: %s\n", av_err2str(AVERROR(errno))); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
✗ |
pkt->pts = av_gettime(); |
191 |
|
|
|
192 |
|
|
/* compute visible data offset */ |
193 |
|
✗ |
pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset + |
194 |
|
✗ |
fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; |
195 |
|
✗ |
pout = pkt->data; |
196 |
|
|
|
197 |
|
✗ |
for (i = 0; i < fbdev->height; i++) { |
198 |
|
✗ |
memcpy(pout, pin, fbdev->frame_linesize); |
199 |
|
✗ |
pin += fbdev->fixinfo.line_length; |
200 |
|
✗ |
pout += fbdev->frame_linesize; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
✗ |
return fbdev->frame_size; |
204 |
|
|
} |
205 |
|
|
|
206 |
|
✗ |
static av_cold int fbdev_read_close(AVFormatContext *avctx) |
207 |
|
|
{ |
208 |
|
✗ |
FBDevContext *fbdev = avctx->priv_data; |
209 |
|
|
|
210 |
|
✗ |
munmap(fbdev->data, fbdev->fixinfo.smem_len); |
211 |
|
✗ |
close(fbdev->fd); |
212 |
|
|
|
213 |
|
✗ |
return 0; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
✗ |
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list) |
217 |
|
|
{ |
218 |
|
✗ |
return ff_fbdev_get_device_list(device_list); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
#define OFFSET(x) offsetof(FBDevContext, x) |
222 |
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM |
223 |
|
|
static const AVOption options[] = { |
224 |
|
|
{ "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, |
225 |
|
|
{ NULL }, |
226 |
|
|
}; |
227 |
|
|
|
228 |
|
|
static const AVClass fbdev_class = { |
229 |
|
|
.class_name = "fbdev indev", |
230 |
|
|
.item_name = av_default_item_name, |
231 |
|
|
.option = options, |
232 |
|
|
.version = LIBAVUTIL_VERSION_INT, |
233 |
|
|
.category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT, |
234 |
|
|
}; |
235 |
|
|
|
236 |
|
|
const FFInputFormat ff_fbdev_demuxer = { |
237 |
|
|
.p.name = "fbdev", |
238 |
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"), |
239 |
|
|
.p.flags = AVFMT_NOFILE, |
240 |
|
|
.p.priv_class = &fbdev_class, |
241 |
|
|
.priv_data_size = sizeof(FBDevContext), |
242 |
|
|
.read_header = fbdev_read_header, |
243 |
|
|
.read_packet = fbdev_read_packet, |
244 |
|
|
.read_close = fbdev_read_close, |
245 |
|
|
.get_device_list = fbdev_get_device_list, |
246 |
|
|
}; |
247 |
|
|
|