FFmpeg
fbdev_enc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Lukasz Marek
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/mman.h>
25 #include <linux/fb.h>
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavformat/avformat.h"
30 #include "fbdev_common.h"
31 #include "avdevice.h"
32 
33 typedef struct {
34  AVClass *class; ///< class for private options
35  int xoffset; ///< x coordinate of top left corner
36  int yoffset; ///< y coordinate of top left corner
37  struct fb_var_screeninfo varinfo; ///< framebuffer variable info
38  struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
39  int fd; ///< framebuffer device file descriptor
40  uint8_t *data; ///< framebuffer data
41 } FBDevContext;
42 
44 {
45  FBDevContext *fbdev = h->priv_data;
47  int ret, flags = O_RDWR;
48  const char* device;
49 
50  if (h->nb_streams != 1 || h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
51  av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n");
52  return AVERROR(EINVAL);
53  }
54 
55  if (h->url[0])
56  device = h->url;
57  else
58  device = ff_fbdev_default_device();
59 
60  if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
61  ret = AVERROR(errno);
63  "Could not open framebuffer device '%s': %s\n",
64  device, av_err2str(ret));
65  return ret;
66  }
67 
68  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
69  ret = AVERROR(errno);
70  av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
71  goto fail;
72  }
73 
74  if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
75  ret = AVERROR(errno);
76  av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
77  goto fail;
78  }
79 
81  if (pix_fmt == AV_PIX_FMT_NONE) {
82  ret = AVERROR(EINVAL);
83  av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
84  goto fail;
85  }
86 
87  fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
88  if (fbdev->data == MAP_FAILED) {
89  ret = AVERROR(errno);
90  av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
91  goto fail;
92  }
93 
94  return 0;
95  fail:
96  close(fbdev->fd);
97  return ret;
98 }
99 
101 {
102  FBDevContext *fbdev = h->priv_data;
103  uint8_t *pin, *pout;
104  enum AVPixelFormat fb_pix_fmt;
105  int disp_height;
106  int bytes_to_copy;
107  AVCodecParameters *par = h->streams[0]->codecpar;
108  enum AVPixelFormat video_pix_fmt = par->format;
109  int video_width = par->width;
110  int video_height = par->height;
111  int bytes_per_pixel = ((par->bits_per_coded_sample + 7) >> 3);
112  int src_line_size = video_width * bytes_per_pixel;
113  int i;
114 
115  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
117  "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
118 
119  fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
120 
121  if (fb_pix_fmt != video_pix_fmt) {
122  av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
123  av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
124  return AVERROR(EINVAL);
125  }
126 
127  disp_height = FFMIN(fbdev->varinfo.yres, video_height);
128  bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
129 
130  pin = pkt->data;
131  pout = fbdev->data +
132  bytes_per_pixel * fbdev->varinfo.xoffset +
133  fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
134 
135  if (fbdev->xoffset) {
136  if (fbdev->xoffset < 0) {
137  if (-fbdev->xoffset >= video_width) //nothing to display
138  return 0;
139  bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
140  pin -= fbdev->xoffset * bytes_per_pixel;
141  } else {
142  int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
143  if (diff > 0) {
144  if (diff >= video_width) //nothing to display
145  return 0;
146  bytes_to_copy -= diff * bytes_per_pixel;
147  }
148  pout += bytes_per_pixel * fbdev->xoffset;
149  }
150  }
151 
152  if (fbdev->yoffset) {
153  if (fbdev->yoffset < 0) {
154  if (-fbdev->yoffset >= video_height) //nothing to display
155  return 0;
156  disp_height += fbdev->yoffset;
157  pin -= fbdev->yoffset * src_line_size;
158  } else {
159  int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
160  if (diff > 0) {
161  if (diff >= video_height) //nothing to display
162  return 0;
163  disp_height -= diff;
164  }
165  pout += fbdev->yoffset * fbdev->fixinfo.line_length;
166  }
167  }
168 
169  for (i = 0; i < disp_height; i++) {
170  memcpy(pout, pin, bytes_to_copy);
171  pout += fbdev->fixinfo.line_length;
172  pin += src_line_size;
173  }
174 
175  return 0;
176 }
177 
179 {
180  FBDevContext *fbdev = h->priv_data;
181  munmap(fbdev->data, fbdev->fixinfo.smem_len);
182  close(fbdev->fd);
183  return 0;
184 }
185 
187 {
188  return ff_fbdev_get_device_list(device_list);
189 }
190 
191 #define OFFSET(x) offsetof(FBDevContext, x)
192 #define ENC AV_OPT_FLAG_ENCODING_PARAM
193 static const AVOption options[] = {
194  { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
195  { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
196  { NULL }
197 };
198 
199 static const AVClass fbdev_class = {
200  .class_name = "fbdev outdev",
201  .item_name = av_default_item_name,
202  .option = options,
203  .version = LIBAVUTIL_VERSION_INT,
205 };
206 
208  .name = "fbdev",
209  .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
210  .priv_data_size = sizeof(FBDevContext),
211  .audio_codec = AV_CODEC_ID_NONE,
212  .video_codec = AV_CODEC_ID_RAWVIDEO,
216  .get_device_list = fbdev_get_device_list,
218  .priv_class = &fbdev_class,
219 };
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
AVOutputFormat::name
const char * name
Definition: avformat.h:504
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
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:478
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
ENC
#define ENC
Definition: fbdev_enc.c:192
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
data
const char data[16]
Definition: mxf.c:143
FBDevContext::xoffset
int xoffset
x coordinate of top left corner
Definition: fbdev_enc.c:35
fail
#define fail()
Definition: checkasm.h:127
FBDevContext::fixinfo
struct fb_fix_screeninfo fixinfo
fixed info;
Definition: fbdev_dec.c: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
pkt
AVPacket * pkt
Definition: movenc.c:59
FBDevContext::yoffset
int yoffset
y coordinate of top left corner
Definition: fbdev_enc.c:36
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
fbdev_write_packet
static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
Definition: fbdev_enc.c:100
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
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
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
fbdev_write_trailer
static av_cold int fbdev_write_trailer(AVFormatContext *h)
Definition: fbdev_enc.c:178
fbdev_get_device_list
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
Definition: fbdev_enc.c:186
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
Definition: log.h:40
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
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
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:464
OFFSET
#define OFFSET(x)
Definition: fbdev_enc.c:191
avdevice.h
fbdev_common.h
ff_fbdev_muxer
const AVOutputFormat ff_fbdev_muxer
Definition: fbdev_enc.c:207
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:727
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
fbdev_write_header
static av_cold int fbdev_write_header(AVFormatContext *h)
Definition: fbdev_enc.c:43
AVCodecParameters::height
int height
Definition: codec_par.h:127
FBDevContext
Definition: fbdev_dec.c:47
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ret
ret
Definition: filter_design.txt:187
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
avformat.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
FBDevContext::varinfo
struct fb_var_screeninfo varinfo
variable info;
Definition: fbdev_dec.c:58
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
AVCodecParameters::format
int format
Definition: codec_par.h:84
options
static const AVOption options[]
Definition: fbdev_enc.c:193
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
FBDevContext::data
uint8_t * data
framebuffer data
Definition: fbdev_dec.c:61
fbdev_class
static const AVClass fbdev_class
Definition: fbdev_enc.c:199
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