FFmpeg
Loading...
Searching...
No Matches
fbdev_dec.c
Go to the documentation of this file.
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"
43#include "libavutil/pixdesc.h"
44#include "libavformat/demux.h"
46#include "avdevice.h"
47#include "fbdev_common.h"
48
49typedef 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
59
60 struct fb_var_screeninfo varinfo; ///< variable info;
61 struct fb_fix_screeninfo fixinfo; ///< fixed info;
62
63 uint8_t *data; ///< framebuffer data
65
67{
68 FBDevContext *fbdev = avctx->priv_data;
69 AVStream *st = NULL;
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
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
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,
143 fbdev->framerate_q.num, fbdev->framerate_q.den,
144 st->codecpar->bit_rate);
145 return 0;
146
147fail:
148 close(fbdev->fd);
149 return ret;
150}
151
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)
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
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
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
223static const AVOption options[] = {
224 { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
225 { NULL },
226};
227
228static const AVClass fbdev_class = {
229 .class_name = "fbdev indev",
230 .item_name = av_default_item_name,
231 .option = options,
232 .version = LIBAVUTIL_VERSION_INT,
234};
235
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),
245 .get_device_list = fbdev_get_device_list,
246};
const FFInputFormat ff_fbdev_demuxer
Definition fbdev_dec.c:236
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Main libavdevice API header.
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition avformat.h:1487
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
const char * ff_fbdev_default_device(void)
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
static const AVClass fbdev_class
Definition fbdev_dec.c:228
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition fbdev_dec.c:152
static av_cold int fbdev_read_close(AVFormatContext *avctx)
Definition fbdev_dec.c:206
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
Definition fbdev_dec.c:216
static av_cold int fbdev_read_header(AVFormatContext *avctx)
Definition fbdev_dec.c:66
#define OFFSET(x)
Definition fbdev_dec.c:221
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define fail
Definition test.h:479
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition file_open.c:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define DEC
Definition librsvgdec.c:149
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition log.h:42
AVOptions.
misc parsing utilities
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.
Definition pixdesc.c:3380
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
List of devices.
Definition avdevice.h:343
Format I/O context.
Definition avformat.h:1333
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1484
char * url
input or output URL.
Definition avformat.h:1449
void * priv_data
Format private data.
Definition avformat.h:1361
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
int fd
framebuffer device file descriptor
Definition fbdev_dec.c:55
int frame_linesize
linesize of the output frame, it is assumed to be constant
Definition fbdev_dec.c:57
int frame_size
size in bytes of a grabbed frame
Definition fbdev_dec.c:51
int bytes_per_pixel
Definition fbdev_dec.c:58
struct fb_fix_screeninfo fixinfo
fixed info;
Definition fbdev_dec.c:61
AVRational framerate_q
framerate
Definition fbdev_dec.c:52
struct fb_var_screeninfo varinfo
variable info;
Definition fbdev_dec.c:60
int height
assumed frame resolution
Definition fbdev_dec.c:56
uint8_t * data
framebuffer data
Definition fbdev_dec.c:63
int64_t time_frame
time for the next frame to output (in 1/1000000 units)
Definition fbdev_dec.c:53
#define av_log(a,...)
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40