]> git.sesse.net Git - ffmpeg/blob - libavdevice/fbdev_enc.c
Merge commit 'ed61f3ca8a0664a697782253b354055136c5d303'
[ffmpeg] / libavdevice / fbdev_enc.c
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
43 static av_cold int fbdev_write_header(AVFormatContext *h)
44 {
45     FBDevContext *fbdev = h->priv_data;
46     enum AVPixelFormat pix_fmt;
47     int ret, flags = O_RDWR;
48     const char* device;
49
50     if (h->nb_streams != 1 || h->streams[0]->codec->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->filename[0])
56         device = h->filename;
57     else
58         device = ff_fbdev_default_device();
59
60     if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
61         ret = AVERROR(errno);
62         av_log(h, AV_LOG_ERROR,
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
80     pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
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
100 static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
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     AVCodecContext *codec_ctx = h->streams[0]->codec;
108     enum AVPixelFormat video_pix_fmt = codec_ctx->pix_fmt;
109     int video_width = codec_ctx->width;
110     int video_height = codec_ctx->height;
111     int bytes_per_pixel = ((codec_ctx->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)
116         av_log(h, AV_LOG_WARNING,
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
178 static av_cold int fbdev_write_trailer(AVFormatContext *h)
179 {
180     FBDevContext *fbdev = h->priv_data;
181     munmap(fbdev->data, fbdev->fixinfo.smem_len);
182     close(fbdev->fd);
183     return 0;
184 }
185
186 #define OFFSET(x) offsetof(FBDevContext, x)
187 #define ENC AV_OPT_FLAG_ENCODING_PARAM
188 static const AVOption options[] = {
189     { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
190     { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
191     { NULL }
192 };
193
194 static const AVClass fbdev_class = {
195     .class_name = "fbdev outdev",
196     .item_name  = av_default_item_name,
197     .option     = options,
198     .version    = LIBAVUTIL_VERSION_INT,
199 };
200
201 AVOutputFormat ff_fbdev_muxer = {
202     .name           = "fbdev",
203     .long_name      = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
204     .priv_data_size = sizeof(FBDevContext),
205     .audio_codec    = AV_CODEC_ID_NONE,
206     .video_codec    = AV_CODEC_ID_RAWVIDEO,
207     .write_header   = fbdev_write_header,
208     .write_packet   = fbdev_write_packet,
209     .write_trailer  = fbdev_write_trailer,
210     .flags          = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
211     .priv_class     = &fbdev_class,
212 };