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>
35 #include <linux/fb.h>
36
47
49 AVClass *
class;
///< class for private options
52 int64_t
time_frame;
///< time for the next frame to output (in 1/1000000 units)
53
54 int fd;
///< framebuffer device file descriptor
56 int frame_linesize;
///< linesize of the output frame, it is assumed to be constant
58
59 struct fb_var_screeninfo
varinfo;
///< variable info;
60 struct fb_fix_screeninfo
fixinfo;
///< fixed info;
61
64
66 {
70 int ret,
flags = O_RDONLY;
71 const char* device;
72
76
77 /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
79 flags |= O_NONBLOCK;
80
83 else
85
89 "Could not open framebuffer device '%s': %s\n",
91 return ret;
92 }
93
94 if (ioctl(fbdev->
fd, FBIOGET_VSCREENINFO, &fbdev->
varinfo) < 0) {
99 }
100
101 if (ioctl(fbdev->
fd, FBIOGET_FSCREENINFO, &fbdev->
fixinfo) < 0) {
104 "FBIOGET_FSCREENINFO: %s\n",
av_err2str(ret));
106 }
107
112 "Framebuffer pixel format not supported.\n");
114 }
115
122 fbdev->
data = mmap(
NULL, fbdev->
fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->
fd, 0);
123 if (fbdev->
data == MAP_FAILED) {
127 }
128
137
139 "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%"PRId64"\n",
144 return 0;
145
148 return ret;
149 }
150
152 {
154 int64_t curtime, delay;
155 struct timespec ts;
156 int i, ret;
158
161
162 /* wait based on the frame rate */
163 while (1) {
167 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
169 if (delay <= 0) {
171 break;
172 }
175 ts.tv_sec = delay / 1000000;
176 ts.tv_nsec = (delay % 1000000) * 1000;
177 while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
178 }
179
181 return ret;
182
183 /* refresh fbdev->varinfo, visible data position may change at each call */
184 if (ioctl(fbdev->
fd, FBIOGET_VSCREENINFO, &fbdev->
varinfo) < 0) {
187 }
188
190
191 /* compute visible data offset */
195
196 for (i = 0; i < fbdev->
height; i++) {
198 pin += fbdev->
fixinfo.line_length;
200 }
201
203 }
204
206 {
208
211
212 return 0;
213 }
214
216 {
218 }
219
220 #define OFFSET(x) offsetof(FBDevContext, x)
221 #define DEC AV_OPT_FLAG_DECODING_PARAM
225 };
226
233 };
234
244 .priv_class = &fbdev_class,
245 };
int height
assumed frame resolution
static enum AVPixelFormat pix_fmt
static const AVClass fbdev_class
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
Memory handling functions.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
const char * av_default_item_name(void *ptr)
Return the context name.
const char * ff_fbdev_default_device()
int frame_linesize
linesize of the output frame, it is assumed to be constant
int frame_size
size in bytes of a grabbed frame
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
struct fb_var_screeninfo varinfo
variable info;
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
int64_t time_frame
time for the next frame to output (in 1/1000000 units)
int flags
Flags modifying the (de)muxer behaviour.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static av_cold int read_close(AVFormatContext *ctx)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Main libavdevice API header.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * url
input or output URL.
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
enum AVMediaType codec_type
General type of the encoded data.
AVRational avg_frame_rate
Average framerate.
static av_cold int fbdev_read_close(AVFormatContext *avctx)
struct fb_fix_screeninfo fixinfo
fixed info;
common internal API header
static const AVOption options[]
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
AVRational framerate_q
framerate
static int read_header(FFV1Context *f)
int64_t av_gettime(void)
Get the current time in microseconds.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define AV_LOG_INFO
Standard information.
AVInputFormat ff_fbdev_demuxer
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
offset must point to AVRational
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
static av_cold int fbdev_read_header(AVFormatContext *avctx)
void * priv_data
Format private data.
uint8_t * data
framebuffer data
AVCodecParameters * codecpar
Codec parameters associated with this stream.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
AVPixelFormat
Pixel format.
This structure stores compressed data.
int fd
framebuffer device file descriptor
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.