| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/file_open.h" | ||
| 27 | #include "libavutil/pixdesc.h" | ||
| 28 | #include "libavutil/log.h" | ||
| 29 | #include "libavutil/opt.h" | ||
| 30 | #include "libavformat/avformat.h" | ||
| 31 | #include "libavformat/mux.h" | ||
| 32 | #include "fbdev_common.h" | ||
| 33 | #include "avdevice.h" | ||
| 34 | |||
| 35 | typedef struct { | ||
| 36 | AVClass *class; ///< class for private options | ||
| 37 | int xoffset; ///< x coordinate of top left corner | ||
| 38 | int yoffset; ///< y coordinate of top left corner | ||
| 39 | struct fb_var_screeninfo varinfo; ///< framebuffer variable info | ||
| 40 | struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info | ||
| 41 | int fd; ///< framebuffer device file descriptor | ||
| 42 | uint8_t *data; ///< framebuffer data | ||
| 43 | } FBDevContext; | ||
| 44 | |||
| 45 | ✗ | static av_cold int fbdev_write_header(AVFormatContext *h) | |
| 46 | { | ||
| 47 | ✗ | FBDevContext *fbdev = h->priv_data; | |
| 48 | enum AVPixelFormat pix_fmt; | ||
| 49 | ✗ | int ret, flags = O_RDWR; | |
| 50 | const char* device; | ||
| 51 | |||
| 52 | ✗ | if (h->nb_streams != 1 || h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) { | |
| 53 | ✗ | av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n"); | |
| 54 | ✗ | return AVERROR(EINVAL); | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | if (h->url[0]) | |
| 58 | ✗ | device = h->url; | |
| 59 | else | ||
| 60 | ✗ | device = ff_fbdev_default_device(); | |
| 61 | |||
| 62 | ✗ | if ((fbdev->fd = avpriv_open(device, flags)) == -1) { | |
| 63 | ✗ | ret = AVERROR(errno); | |
| 64 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 65 | "Could not open framebuffer device '%s': %s\n", | ||
| 66 | ✗ | device, av_err2str(ret)); | |
| 67 | ✗ | return ret; | |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) { | |
| 71 | ✗ | ret = AVERROR(errno); | |
| 72 | ✗ | av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret)); | |
| 73 | ✗ | goto fail; | |
| 74 | } | ||
| 75 | |||
| 76 | ✗ | if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) { | |
| 77 | ✗ | ret = AVERROR(errno); | |
| 78 | ✗ | av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret)); | |
| 79 | ✗ | goto fail; | |
| 80 | } | ||
| 81 | |||
| 82 | ✗ | pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo); | |
| 83 | ✗ | if (pix_fmt == AV_PIX_FMT_NONE) { | |
| 84 | ✗ | ret = AVERROR(EINVAL); | |
| 85 | ✗ | av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n"); | |
| 86 | ✗ | goto fail; | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0); | |
| 90 | ✗ | if (fbdev->data == MAP_FAILED) { | |
| 91 | ✗ | ret = AVERROR(errno); | |
| 92 | ✗ | av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret)); | |
| 93 | ✗ | goto fail; | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | return 0; | |
| 97 | ✗ | fail: | |
| 98 | ✗ | close(fbdev->fd); | |
| 99 | ✗ | return ret; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt) | |
| 103 | { | ||
| 104 | ✗ | FBDevContext *fbdev = h->priv_data; | |
| 105 | const uint8_t *pin; | ||
| 106 | uint8_t *pout; | ||
| 107 | enum AVPixelFormat fb_pix_fmt; | ||
| 108 | int disp_height; | ||
| 109 | int bytes_to_copy; | ||
| 110 | ✗ | AVCodecParameters *par = h->streams[0]->codecpar; | |
| 111 | ✗ | enum AVPixelFormat video_pix_fmt = par->format; | |
| 112 | ✗ | int video_width = par->width; | |
| 113 | ✗ | int video_height = par->height; | |
| 114 | ✗ | int bytes_per_pixel = ((par->bits_per_coded_sample + 7) >> 3); | |
| 115 | ✗ | int src_line_size = video_width * bytes_per_pixel; | |
| 116 | int i; | ||
| 117 | |||
| 118 | ✗ | if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) | |
| 119 | ✗ | av_log(h, AV_LOG_WARNING, | |
| 120 | ✗ | "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno))); | |
| 121 | |||
| 122 | ✗ | fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo); | |
| 123 | |||
| 124 | ✗ | if (fb_pix_fmt != video_pix_fmt) { | |
| 125 | ✗ | av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n", | |
| 126 | av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt)); | ||
| 127 | ✗ | return AVERROR(EINVAL); | |
| 128 | } | ||
| 129 | |||
| 130 | ✗ | disp_height = FFMIN(fbdev->varinfo.yres, video_height); | |
| 131 | ✗ | bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel; | |
| 132 | |||
| 133 | ✗ | pin = pkt->data; | |
| 134 | ✗ | pout = fbdev->data + | |
| 135 | ✗ | bytes_per_pixel * fbdev->varinfo.xoffset + | |
| 136 | ✗ | fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; | |
| 137 | |||
| 138 | ✗ | if (fbdev->xoffset) { | |
| 139 | ✗ | if (fbdev->xoffset < 0) { | |
| 140 | ✗ | if (-fbdev->xoffset >= video_width) //nothing to display | |
| 141 | ✗ | return 0; | |
| 142 | ✗ | bytes_to_copy += fbdev->xoffset * bytes_per_pixel; | |
| 143 | ✗ | pin -= fbdev->xoffset * bytes_per_pixel; | |
| 144 | } else { | ||
| 145 | ✗ | int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres; | |
| 146 | ✗ | if (diff > 0) { | |
| 147 | ✗ | if (diff >= video_width) //nothing to display | |
| 148 | ✗ | return 0; | |
| 149 | ✗ | bytes_to_copy -= diff * bytes_per_pixel; | |
| 150 | } | ||
| 151 | ✗ | pout += bytes_per_pixel * fbdev->xoffset; | |
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | ✗ | if (fbdev->yoffset) { | |
| 156 | ✗ | if (fbdev->yoffset < 0) { | |
| 157 | ✗ | if (-fbdev->yoffset >= video_height) //nothing to display | |
| 158 | ✗ | return 0; | |
| 159 | ✗ | disp_height += fbdev->yoffset; | |
| 160 | ✗ | pin -= fbdev->yoffset * src_line_size; | |
| 161 | } else { | ||
| 162 | ✗ | int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres; | |
| 163 | ✗ | if (diff > 0) { | |
| 164 | ✗ | if (diff >= video_height) //nothing to display | |
| 165 | ✗ | return 0; | |
| 166 | ✗ | disp_height -= diff; | |
| 167 | } | ||
| 168 | ✗ | pout += fbdev->yoffset * fbdev->fixinfo.line_length; | |
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | ✗ | for (i = 0; i < disp_height; i++) { | |
| 173 | ✗ | memcpy(pout, pin, bytes_to_copy); | |
| 174 | ✗ | pout += fbdev->fixinfo.line_length; | |
| 175 | ✗ | pin += src_line_size; | |
| 176 | } | ||
| 177 | |||
| 178 | ✗ | return 0; | |
| 179 | } | ||
| 180 | |||
| 181 | ✗ | static av_cold int fbdev_write_trailer(AVFormatContext *h) | |
| 182 | { | ||
| 183 | ✗ | FBDevContext *fbdev = h->priv_data; | |
| 184 | ✗ | munmap(fbdev->data, fbdev->fixinfo.smem_len); | |
| 185 | ✗ | close(fbdev->fd); | |
| 186 | ✗ | return 0; | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list) | |
| 190 | { | ||
| 191 | ✗ | return ff_fbdev_get_device_list(device_list); | |
| 192 | } | ||
| 193 | |||
| 194 | #define OFFSET(x) offsetof(FBDevContext, x) | ||
| 195 | #define ENC AV_OPT_FLAG_ENCODING_PARAM | ||
| 196 | static const AVOption options[] = { | ||
| 197 | { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC }, | ||
| 198 | { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC }, | ||
| 199 | { NULL } | ||
| 200 | }; | ||
| 201 | |||
| 202 | static const AVClass fbdev_class = { | ||
| 203 | .class_name = "fbdev outdev", | ||
| 204 | .item_name = av_default_item_name, | ||
| 205 | .option = options, | ||
| 206 | .version = LIBAVUTIL_VERSION_INT, | ||
| 207 | .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT, | ||
| 208 | }; | ||
| 209 | |||
| 210 | const FFOutputFormat ff_fbdev_muxer = { | ||
| 211 | .p.name = "fbdev", | ||
| 212 | .p.long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"), | ||
| 213 | .priv_data_size = sizeof(FBDevContext), | ||
| 214 | .p.audio_codec = AV_CODEC_ID_NONE, | ||
| 215 | .p.video_codec = AV_CODEC_ID_RAWVIDEO, | ||
| 216 | .write_header = fbdev_write_header, | ||
| 217 | .write_packet = fbdev_write_packet, | ||
| 218 | .write_trailer = fbdev_write_trailer, | ||
| 219 | .get_device_list = fbdev_get_device_list, | ||
| 220 | .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, | ||
| 221 | .p.priv_class = &fbdev_class, | ||
| 222 | }; | ||
| 223 |