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 
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 
49  if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
50  av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n");
51  return AVERROR(EINVAL);
52  }
53 
54  if ((fbdev->fd = avpriv_open(h->filename, flags)) == -1) {
55  ret = AVERROR(errno);
57  "Could not open framebuffer device '%s': %s\n",
58  h->filename, av_err2str(ret));
59  return ret;
60  }
61 
62  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
63  ret = AVERROR(errno);
64  av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
65  goto fail;
66  }
67 
68  if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
69  ret = AVERROR(errno);
70  av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
71  goto fail;
72  }
73 
74  pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
75  if (pix_fmt == AV_PIX_FMT_NONE) {
76  ret = AVERROR(EINVAL);
77  av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
78  goto fail;
79  }
80 
81  fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
82  if (fbdev->data == MAP_FAILED) {
83  ret = AVERROR(errno);
84  av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
85  goto fail;
86  }
87 
88  return 0;
89  fail:
90  close(fbdev->fd);
91  return ret;
92 }
93 
95 {
96  FBDevContext *fbdev = h->priv_data;
97  uint8_t *pin, *pout;
98  enum AVPixelFormat fb_pix_fmt;
99  int disp_height;
100  int bytes_to_copy;
101  AVCodecContext *codec_ctx = h->streams[0]->codec;
102  enum AVPixelFormat video_pix_fmt = codec_ctx->pix_fmt;
103  int video_width = codec_ctx->width;
104  int video_height = codec_ctx->height;
105  int bytes_per_pixel = ((codec_ctx->bits_per_coded_sample + 7) >> 3);
106  int src_line_size = video_width * bytes_per_pixel;
107  int i;
108 
109  if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
111  "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
112 
113  fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
114 
115  if (fb_pix_fmt != video_pix_fmt) {
116  av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
117  av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
118  return AVERROR(EINVAL);
119  }
120 
121  disp_height = FFMIN(fbdev->varinfo.yres, video_height);
122  bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
123 
124  pin = pkt->data;
125  pout = fbdev->data +
126  bytes_per_pixel * fbdev->varinfo.xoffset +
127  fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
128 
129  if (fbdev->xoffset) {
130  if (fbdev->xoffset < 0) {
131  if (-fbdev->xoffset >= video_width) //nothing to display
132  return 0;
133  bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
134  pin -= fbdev->xoffset * bytes_per_pixel;
135  } else {
136  int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
137  if (diff > 0) {
138  if (diff >= video_width) //nothing to display
139  return 0;
140  bytes_to_copy -= diff * bytes_per_pixel;
141  }
142  pout += bytes_per_pixel * fbdev->xoffset;
143  }
144  }
145 
146  if (fbdev->yoffset) {
147  if (fbdev->yoffset < 0) {
148  if (-fbdev->yoffset >= video_height) //nothing to display
149  return 0;
150  disp_height += fbdev->yoffset;
151  pin -= fbdev->yoffset * src_line_size;
152  } else {
153  int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
154  if (diff > 0) {
155  if (diff >= video_height) //nothing to display
156  return 0;
157  disp_height -= diff;
158  }
159  pout += fbdev->yoffset * fbdev->fixinfo.line_length;
160  }
161  }
162 
163  for (i = 0; i < disp_height; i++) {
164  memcpy(pout, pin, bytes_to_copy);
165  pout += fbdev->fixinfo.line_length;
166  pin += src_line_size;
167  }
168 
169  return 0;
170 }
171 
173 {
174  FBDevContext *fbdev = h->priv_data;
175  munmap(fbdev->data, fbdev->fixinfo.smem_len);
176  close(fbdev->fd);
177  return 0;
178 }
179 
180 #define OFFSET(x) offsetof(FBDevContext, x)
181 #define ENC AV_OPT_FLAG_ENCODING_PARAM
182 static const AVOption options[] = {
183  { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
184  { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
185  { NULL }
186 };
187 
188 static const AVClass fbdev_class = {
189  .class_name = "fbdev outdev",
190  .item_name = av_default_item_name,
191  .option = options,
192  .version = LIBAVUTIL_VERSION_INT,
193 };
194 
196  .name = "fbdev",
197  .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
198  .priv_data_size = sizeof(FBDevContext),
199  .audio_codec = AV_CODEC_ID_NONE,
200  .video_codec = AV_CODEC_ID_RAWVIDEO,
205  .priv_class = &fbdev_class,
206 };