FFmpeg
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/internal.h"
38 #include "libavutil/log.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/time.h"
41 #include "libavutil/parseutils.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavformat/internal.h"
44 #include "avdevice.h"
45 #include "fbdev_common.h"
46 
47 typedef struct FBDevContext {
48  AVClass *class; ///< class for private options
49  int frame_size; ///< size in bytes of a grabbed frame
50  AVRational framerate_q; ///< framerate
51  int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
52 
53  int fd; ///< framebuffer device file descriptor
54  int width, height; ///< assumed frame resolution
55  int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
57 
58  struct fb_var_screeninfo varinfo; ///< variable info;
59  struct fb_fix_screeninfo fixinfo; ///< fixed info;
60 
61  uint8_t *data; ///< framebuffer data
62 } FBDevContext;
63 
65 {
66  FBDevContext *fbdev = avctx->priv_data;
67  AVStream *st = NULL;
69  int ret, flags = O_RDONLY;
70  const char* device;
71 
72  if (!(st = avformat_new_stream(avctx, NULL)))
73  return AVERROR(ENOMEM);
74  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
75 
76  /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
77  if (avctx->flags & AVFMT_FLAG_NONBLOCK)
78  flags |= O_NONBLOCK;
79 
80  if (avctx->url[0])
81  device = avctx->url;
82  else
83  device = ff_fbdev_default_device();
84 
85  if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
86  ret = AVERROR(errno);
87  av_log(avctx, AV_LOG_ERROR,
88  "Could not open framebuffer device '%s': %s\n",
89  device, av_err2str(ret));
90  return ret;
91  }
92 
93  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
94  ret = AVERROR(errno);
95  av_log(avctx, AV_LOG_ERROR,
96  "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
97  goto fail;
98  }
99 
100  if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
101  ret = AVERROR(errno);
102  av_log(avctx, AV_LOG_ERROR,
103  "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
104  goto fail;
105  }
106 
108  if (pix_fmt == AV_PIX_FMT_NONE) {
109  ret = AVERROR(EINVAL);
110  av_log(avctx, AV_LOG_ERROR,
111  "Framebuffer pixel format not supported.\n");
112  goto fail;
113  }
114 
115  fbdev->width = fbdev->varinfo.xres;
116  fbdev->height = fbdev->varinfo.yres;
117  fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
118  fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
119  fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
120  fbdev->time_frame = AV_NOPTS_VALUE;
121  fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
122  if (fbdev->data == MAP_FAILED) {
123  ret = AVERROR(errno);
124  av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
125  goto fail;
126  }
127 
130  st->codecpar->width = fbdev->width;
131  st->codecpar->height = fbdev->height;
132  st->codecpar->format = pix_fmt;
133  st->avg_frame_rate = fbdev->framerate_q;
134  st->codecpar->bit_rate =
135  fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
136 
137  av_log(avctx, AV_LOG_INFO,
138  "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%"PRId64"\n",
139  fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
141  fbdev->framerate_q.num, fbdev->framerate_q.den,
142  st->codecpar->bit_rate);
143  return 0;
144 
145 fail:
146  close(fbdev->fd);
147  return ret;
148 }
149 
151 {
152  FBDevContext *fbdev = avctx->priv_data;
153  int64_t curtime, delay;
154  struct timespec ts;
155  int i, ret;
156  uint8_t *pin, *pout;
157 
158  if (fbdev->time_frame == AV_NOPTS_VALUE)
159  fbdev->time_frame = av_gettime_relative();
160 
161  /* wait based on the frame rate */
162  while (1) {
163  curtime = av_gettime_relative();
164  delay = fbdev->time_frame - curtime;
165  av_log(avctx, AV_LOG_TRACE,
166  "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
167  fbdev->time_frame, curtime, delay);
168  if (delay <= 0) {
169  fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
170  break;
171  }
172  if (avctx->flags & AVFMT_FLAG_NONBLOCK)
173  return AVERROR(EAGAIN);
174  ts.tv_sec = delay / 1000000;
175  ts.tv_nsec = (delay % 1000000) * 1000;
176  while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
177  }
178 
179  if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
180  return ret;
181 
182  /* refresh fbdev->varinfo, visible data position may change at each call */
183  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
184  av_log(avctx, AV_LOG_WARNING,
185  "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
186  }
187 
188  pkt->pts = av_gettime();
189 
190  /* compute visible data offset */
191  pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
192  fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
193  pout = pkt->data;
194 
195  for (i = 0; i < fbdev->height; i++) {
196  memcpy(pout, pin, fbdev->frame_linesize);
197  pin += fbdev->fixinfo.line_length;
198  pout += fbdev->frame_linesize;
199  }
200 
201  return fbdev->frame_size;
202 }
203 
205 {
206  FBDevContext *fbdev = avctx->priv_data;
207 
208  munmap(fbdev->data, fbdev->fixinfo.smem_len);
209  close(fbdev->fd);
210 
211  return 0;
212 }
213 
215 {
216  return ff_fbdev_get_device_list(device_list);
217 }
218 
219 #define OFFSET(x) offsetof(FBDevContext, x)
220 #define DEC AV_OPT_FLAG_DECODING_PARAM
221 static const AVOption options[] = {
222  { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
223  { NULL },
224 };
225 
226 static const AVClass fbdev_class = {
227  .class_name = "fbdev indev",
228  .item_name = av_default_item_name,
229  .option = options,
230  .version = LIBAVUTIL_VERSION_INT,
232 };
233 
235  .name = "fbdev",
236  .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
237  .priv_data_size = sizeof(FBDevContext),
241  .get_device_list = fbdev_get_device_list,
242  .flags = AVFMT_NOFILE,
243  .priv_class = &fbdev_class,
244 };
av_gettime_relative
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
ff_fbdev_default_device
const char * ff_fbdev_default_device()
Definition: fbdev_common.c:64
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
FBDevContext::bytes_per_pixel
int bytes_per_pixel
Definition: fbdev_dec.c:56
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:237
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1015
fbdev_class
static const AVClass fbdev_class
Definition: fbdev_dec.c:226
fail
#define fail()
Definition: checkasm.h:127
FBDevContext::fixinfo
struct fb_fix_screeninfo fixinfo
fixed info;
Definition: fbdev_dec.c:59
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:141
AVRational::num
int num
Numerator.
Definition: rational.h:59
ff_get_pixfmt_from_fb_varinfo
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
Definition: fbdev_common.c:48
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
av_cold
#define av_cold
Definition: attributes.h:90
ff_fbdev_get_device_list
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
Definition: fbdev_common.c:72
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1318
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
fbdev_get_device_list
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
Definition: fbdev_dec.c:214
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
FBDevContext::fd
int fd
framebuffer device file descriptor
Definition: fbdev_dec.c:53
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
FBDevContext::frame_size
int frame_size
size in bytes of a grabbed frame
Definition: fbdev_dec.c:49
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
fbdev_read_close
static av_cold int fbdev_read_close(AVFormatContext *avctx)
Definition: fbdev_dec.c:204
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
parseutils.h
FBDevContext::width
int width
Definition: fbdev_dec.c:54
time.h
AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition: log.h:41
FBDevContext::time_frame
int64_t time_frame
time for the next frame to output (in 1/1000000 units)
Definition: fbdev_dec.c:51
ff_fbdev_demuxer
const AVInputFormat ff_fbdev_demuxer
Definition: fbdev_dec.c:234
OFFSET
#define OFFSET(x)
Definition: fbdev_dec.c:219
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
DEC
#define DEC
Definition: fbdev_dec.c:220
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1283
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:464
avdevice.h
fbdev_common.h
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
FBDevContext
Definition: fbdev_dec.c:47
fbdev_read_packet
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: fbdev_dec.c:150
AVFMT_FLAG_NONBLOCK
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1321
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:935
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AVDeviceInfoList
List of devices.
Definition: avdevice.h:467
FBDevContext::frame_linesize
int frame_linesize
linesize of the output frame, it is assumed to be constant
Definition: fbdev_dec.c:55
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avpriv_set_pts_info
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: utils.c:1196
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
FBDevContext::varinfo
struct fb_var_screeninfo varinfo
variable info;
Definition: fbdev_dec.c:58
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
fbdev_read_header
static av_cold int fbdev_read_header(AVFormatContext *avctx)
Definition: fbdev_dec.c:64
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FBDevContext::data
uint8_t * data
framebuffer data
Definition: fbdev_dec.c:61
options
static const AVOption options[]
Definition: fbdev_dec.c:221
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1228
FBDevContext::height
int height
assumed frame resolution
Definition: fbdev_dec.c:54
FBDevContext::framerate_q
AVRational framerate_q
framerate
Definition: fbdev_dec.c:50
av_get_pix_fmt_name
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:2580