FFmpeg  2.1.1
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/mem.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/time.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavformat/internal.h"
45 #include "avdevice.h"
46 #include "fbdev_common.h"
47 
48 typedef struct {
49  AVClass *class; ///< class for private options
50  int frame_size; ///< size in bytes of a grabbed frame
51  AVRational framerate_q; ///< framerate
52  int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
53 
54  int fd; ///< framebuffer device file descriptor
55  int width, height; ///< assumed frame resolution
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 
62  uint8_t *data; ///< framebuffer data
63 } FBDevContext;
64 
66 {
67  FBDevContext *fbdev = avctx->priv_data;
68  AVStream *st = NULL;
70  int ret, flags = O_RDONLY;
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 ((fbdev->fd = avpriv_open(avctx->filename, flags)) == -1) {
81  ret = AVERROR(errno);
82  av_log(avctx, AV_LOG_ERROR,
83  "Could not open framebuffer device '%s': %s\n",
84  avctx->filename, av_err2str(ret));
85  return ret;
86  }
87 
88  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
89  ret = AVERROR(errno);
90  av_log(avctx, AV_LOG_ERROR,
91  "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
92  goto fail;
93  }
94 
95  if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
96  ret = AVERROR(errno);
97  av_log(avctx, AV_LOG_ERROR,
98  "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
99  goto fail;
100  }
101 
102  pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
103  if (pix_fmt == AV_PIX_FMT_NONE) {
104  ret = AVERROR(EINVAL);
105  av_log(avctx, AV_LOG_ERROR,
106  "Framebuffer pixel format not supported.\n");
107  goto fail;
108  }
109 
110  fbdev->width = fbdev->varinfo.xres;
111  fbdev->height = fbdev->varinfo.yres;
112  fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
113  fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
114  fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
115  fbdev->time_frame = AV_NOPTS_VALUE;
116  fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
117  if (fbdev->data == MAP_FAILED) {
118  ret = AVERROR(errno);
119  av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
120  goto fail;
121  }
122 
125  st->codec->width = fbdev->width;
126  st->codec->height = fbdev->height;
127  st->codec->pix_fmt = pix_fmt;
128  st->codec->time_base = av_inv_q(fbdev->framerate_q);
129  st->codec->bit_rate =
130  fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
131 
132  av_log(avctx, AV_LOG_INFO,
133  "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
134  fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
135  av_get_pix_fmt_name(pix_fmt),
136  fbdev->framerate_q.num, fbdev->framerate_q.den,
137  st->codec->bit_rate);
138  return 0;
139 
140 fail:
141  close(fbdev->fd);
142  return ret;
143 }
144 
146 {
147  FBDevContext *fbdev = avctx->priv_data;
148  int64_t curtime, delay;
149  struct timespec ts;
150  int i, ret;
151  uint8_t *pin, *pout;
152 
153  if (fbdev->time_frame == AV_NOPTS_VALUE)
154  fbdev->time_frame = av_gettime();
155 
156  /* wait based on the frame rate */
157  while (1) {
158  curtime = av_gettime();
159  delay = fbdev->time_frame - curtime;
160  av_dlog(avctx,
161  "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
162  fbdev->time_frame, curtime, delay);
163  if (delay <= 0) {
164  fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
165  break;
166  }
167  if (avctx->flags & AVFMT_FLAG_NONBLOCK)
168  return AVERROR(EAGAIN);
169  ts.tv_sec = delay / 1000000;
170  ts.tv_nsec = (delay % 1000000) * 1000;
171  while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
172  }
173 
174  if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
175  return ret;
176 
177  /* refresh fbdev->varinfo, visible data position may change at each call */
178  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
179  av_log(avctx, AV_LOG_WARNING,
180  "Error refreshing variable info: %s\n", av_err2str(ret));
181 
182  pkt->pts = curtime;
183 
184  /* compute visible data offset */
185  pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
186  fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
187  pout = pkt->data;
188 
189  for (i = 0; i < fbdev->height; i++) {
190  memcpy(pout, pin, fbdev->frame_linesize);
191  pin += fbdev->fixinfo.line_length;
192  pout += fbdev->frame_linesize;
193  }
194 
195  return fbdev->frame_size;
196 }
197 
199 {
200  FBDevContext *fbdev = avctx->priv_data;
201 
202  munmap(fbdev->data, fbdev->fixinfo.smem_len);
203  close(fbdev->fd);
204 
205  return 0;
206 }
207 
208 #define OFFSET(x) offsetof(FBDevContext, x)
209 #define DEC AV_OPT_FLAG_DECODING_PARAM
210 static const AVOption options[] = {
211  { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
212  { NULL },
213 };
214 
215 static const AVClass fbdev_class = {
216  .class_name = "fbdev indev",
217  .item_name = av_default_item_name,
218  .option = options,
219  .version = LIBAVUTIL_VERSION_INT,
220 };
221 
223  .name = "fbdev",
224  .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
225  .priv_data_size = sizeof(FBDevContext),
229  .flags = AVFMT_NOFILE,
230  .priv_class = &fbdev_class,
231 };
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1048
int height
assumed frame resolution
Definition: fbdev_dec.c:55
#define DEC
Definition: fbdev_dec.c:209
AVOption.
Definition: opt.h:253
static const AVClass fbdev_class
Definition: fbdev_dec.c:215
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
void avpriv_set_pts_info(AVStream *s, 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:3922
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int num
numerator
Definition: rational.h:44
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:1860
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
Pixel format.
Definition: avcodec.h:4533
#define av_cold
Definition: avcodec.h:653
int frame_linesize
linesize of the output frame, it is assumed to be constant
Definition: fbdev_dec.c:56
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
#define OFFSET(x)
Definition: fbdev_dec.c:208
int frame_size
size in bytes of a grabbed frame
Definition: fbdev_dec.c:50
Format I/O context.
Definition: avformat.h:968
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
uint8_t
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:55
struct fb_var_screeninfo varinfo
variable info;
Definition: fbdev_dec.c:59
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:71
int64_t time_frame
time for the next frame to output (in 1/1000000 units)
Definition: fbdev_dec.c:52
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
enum AVPixelFormat pix_fmt
Definition: v4l.c:62
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:347
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
const OptionDef options[]
Definition: ffserver.c:4682
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
#define av_err2str(errnum)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
char filename[1024]
input or output filename
Definition: avformat.h:1018
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: fbdev_dec.c:145
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
static av_cold int fbdev_read_close(AVFormatContext *avctx)
Definition: fbdev_dec.c:198
struct fb_fix_screeninfo fixinfo
fixed info;
Definition: fbdev_dec.c:60
goto fail
Definition: avfilter.c:963
common internal API header
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int bit_rate
the average bitrate
Definition: avcodec.h:1204
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
AVRational framerate_q
framerate
Definition: fbdev_dec.c:51
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
AVInputFormat ff_fbdev_demuxer
Definition: fbdev_dec.c:222
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
Main libavdevice API header.
int bytes_per_pixel
Definition: fbdev_dec.c:57
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
Describe the class of an AVClass context structure.
Definition: log.h:50
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data
Definition: avcodec.h:1063
offset must point to AVRational
Definition: opt.h:233
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
static int flags
Definition: cpu.c:45
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
Definition: fbdev_common.c:43
int den
denominator
Definition: rational.h:45
static av_cold int fbdev_read_header(AVFormatContext *avctx)
Definition: fbdev_dec.c:65
#define AVERROR(e)
uint8_t * data
framebuffer data
Definition: fbdev_dec.c:62
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
int fd
framebuffer device file descriptor
Definition: fbdev_dec.c:54
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278