FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavformat/avformat.h"
31 #include "fbdev_common.h"
32 #include "avdevice.h"
33 
34 typedef struct {
35  AVClass *class; ///< class for private options
36  int xoffset; ///< x coordinate of top left corner
37  int yoffset; ///< y coordinate of top left corner
38  struct fb_var_screeninfo varinfo; ///< framebuffer variable info
39  struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
40  int fd; ///< framebuffer device file descriptor
41  uint8_t *data; ///< framebuffer data
42 } FBDevContext;
43 
45 {
46  FBDevContext *fbdev = h->priv_data;
48  int ret, flags = O_RDWR;
49  const char* device;
50 
51  if (h->nb_streams != 1 || h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
52  av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n");
53  return AVERROR(EINVAL);
54  }
55 
56  if (h->url[0])
57  device = h->url;
58  else
59  device = ff_fbdev_default_device();
60 
61  if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
62  ret = AVERROR(errno);
64  "Could not open framebuffer device '%s': %s\n",
65  device, av_err2str(ret));
66  return ret;
67  }
68 
69  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
70  ret = AVERROR(errno);
71  av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
72  goto fail;
73  }
74 
75  if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
76  ret = AVERROR(errno);
77  av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
78  goto fail;
79  }
80 
81  pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
82  if (pix_fmt == AV_PIX_FMT_NONE) {
83  ret = AVERROR(EINVAL);
84  av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
85  goto fail;
86  }
87 
88  fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
89  if (fbdev->data == MAP_FAILED) {
90  ret = AVERROR(errno);
91  av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
92  goto fail;
93  }
94 
95  return 0;
96  fail:
97  close(fbdev->fd);
98  return ret;
99 }
100 
102 {
103  FBDevContext *fbdev = h->priv_data;
104  uint8_t *pin, *pout;
105  enum AVPixelFormat fb_pix_fmt;
106  int disp_height;
107  int bytes_to_copy;
108  AVCodecParameters *par = h->streams[0]->codecpar;
109  enum AVPixelFormat video_pix_fmt = par->format;
110  int video_width = par->width;
111  int video_height = par->height;
112  int bytes_per_pixel = ((par->bits_per_coded_sample + 7) >> 3);
113  int src_line_size = video_width * bytes_per_pixel;
114  int i;
115 
116  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
118  "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
119 
120  fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
121 
122  if (fb_pix_fmt != video_pix_fmt) {
123  av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
124  av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
125  return AVERROR(EINVAL);
126  }
127 
128  disp_height = FFMIN(fbdev->varinfo.yres, video_height);
129  bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
130 
131  pin = pkt->data;
132  pout = fbdev->data +
133  bytes_per_pixel * fbdev->varinfo.xoffset +
134  fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
135 
136  if (fbdev->xoffset) {
137  if (fbdev->xoffset < 0) {
138  if (-fbdev->xoffset >= video_width) //nothing to display
139  return 0;
140  bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
141  pin -= fbdev->xoffset * bytes_per_pixel;
142  } else {
143  int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
144  if (diff > 0) {
145  if (diff >= video_width) //nothing to display
146  return 0;
147  bytes_to_copy -= diff * bytes_per_pixel;
148  }
149  pout += bytes_per_pixel * fbdev->xoffset;
150  }
151  }
152 
153  if (fbdev->yoffset) {
154  if (fbdev->yoffset < 0) {
155  if (-fbdev->yoffset >= video_height) //nothing to display
156  return 0;
157  disp_height += fbdev->yoffset;
158  pin -= fbdev->yoffset * src_line_size;
159  } else {
160  int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
161  if (diff > 0) {
162  if (diff >= video_height) //nothing to display
163  return 0;
164  disp_height -= diff;
165  }
166  pout += fbdev->yoffset * fbdev->fixinfo.line_length;
167  }
168  }
169 
170  for (i = 0; i < disp_height; i++) {
171  memcpy(pout, pin, bytes_to_copy);
172  pout += fbdev->fixinfo.line_length;
173  pin += src_line_size;
174  }
175 
176  return 0;
177 }
178 
180 {
181  FBDevContext *fbdev = h->priv_data;
182  munmap(fbdev->data, fbdev->fixinfo.smem_len);
183  close(fbdev->fd);
184  return 0;
185 }
186 
188 {
189  return ff_fbdev_get_device_list(device_list);
190 }
191 
192 #define OFFSET(x) offsetof(FBDevContext, x)
193 #define ENC AV_OPT_FLAG_ENCODING_PARAM
194 static const AVOption options[] = {
195  { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
196  { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
197  { NULL }
198 };
199 
200 static const AVClass fbdev_class = {
201  .class_name = "fbdev outdev",
202  .item_name = av_default_item_name,
203  .option = options,
204  .version = LIBAVUTIL_VERSION_INT,
206 };
207 
209  .name = "fbdev",
210  .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
211  .priv_data_size = sizeof(FBDevContext),
212  .audio_codec = AV_CODEC_ID_NONE,
213  .video_codec = AV_CODEC_ID_RAWVIDEO,
217  .get_device_list = fbdev_get_device_list,
219  .priv_class = &fbdev_class,
220 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:679
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static enum AVPixelFormat pix_fmt
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
Memory handling functions.
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
const char * ff_fbdev_default_device()
Definition: fbdev_common.c:64
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3876
Format I/O context.
Definition: avformat.h:1342
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:72
uint8_t
#define av_cold
Definition: attributes.h:82
int width
Video only.
Definition: avcodec.h:3950
AVOptions.
struct fb_var_screeninfo varinfo
variable info;
Definition: fbdev_dec.c:59
#define OFFSET(x)
Definition: fbdev_enc.c:192
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
uint8_t * data
Definition: avcodec.h:1430
static int flags
Definition: log.c:55
AVOutputFormat ff_fbdev_muxer
Definition: fbdev_enc.c:208
#define av_log(a,...)
Main libavdevice API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
char * url
input or output URL.
Definition: avformat.h:1438
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
#define fail()
Definition: checkasm.h:116
static const AVOption options[]
Definition: fbdev_enc.c:194
struct fb_fix_screeninfo fixinfo
fixed info;
Definition: fbdev_dec.c:60
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
#define FFMIN(a, b)
Definition: common.h:96
int yoffset
y coordinate of top left corner
Definition: fbdev_enc.c:37
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
static av_cold int fbdev_write_header(AVFormatContext *h)
Definition: fbdev_enc.c:44
const char * name
Definition: avformat.h:507
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
int xoffset
x coordinate of top left corner
Definition: fbdev_enc.c:36
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:469
static av_cold int fbdev_write_trailer(AVFormatContext *h)
Definition: fbdev_enc.c:179
#define ENC
Definition: fbdev_enc.c:193
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
Definition: fbdev_common.c:72
Describe the class of an AVClass context structure.
Definition: log.h:67
static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
Definition: fbdev_enc.c:187
static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
Definition: fbdev_enc.c:101
List of devices.
Definition: avdevice.h:460
Main libavformat public API header.
if(ret< 0)
Definition: vf_mcdeint.c:279
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
Definition: fbdev_common.c:48
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:472
static av_always_inline int diff(const uint32_t a, const uint32_t b)
void * priv_data
Format private data.
Definition: avformat.h:1370
static const AVClass fbdev_class
Definition: fbdev_enc.c:200
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3926
uint8_t * data
framebuffer data
Definition: fbdev_dec.c:62
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
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:2279
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1407
int fd
framebuffer device file descriptor
Definition: fbdev_dec.c:54