[FFmpeg-devel] [PATCH] avfilter: add fbsink for video display.
Shiqi Zhu
hiccupzhu at gmail.com
Tue Jun 11 16:03:10 EEST 2024
This patch wants to extend the functionality of sink-filter.
Signed-off-by: Shiqi Zhu <hiccupzhu at gmail.com>
---
configure | 1 +
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vsink_fbsink.c | 319 +++++++++++++++++++++++++++++++++++++
4 files changed, 322 insertions(+)
create mode 100644 libavfilter/vsink_fbsink.c
diff --git a/configure b/configure
index 6c5b8aab9a..986bf9b069 100755
--- a/configure
+++ b/configure
@@ -3977,6 +3977,7 @@ xstack_qsv_filter_deps="libmfx"
xstack_qsv_filter_select="qsvvpp"
pad_vaapi_filter_deps="vaapi_1"
drawbox_vaapi_filter_deps="vaapi_1"
+fbsink_filter_deps="linux_fb_h"
# examples
avio_http_serve_files_deps="avformat avutil fork"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 5992fd161f..7b9167ab8c 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -616,6 +616,7 @@ OBJS-$(CONFIG_YUVTESTSRC_FILTER) += vsrc_testsrc.o
OBJS-$(CONFIG_ZONEPLATE_FILTER) += vsrc_testsrc.o
OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o
+OBJS-$(CONFIG_FBSINK_FILTER) += vsink_fbsink.o
# multimedia filters
OBJS-$(CONFIG_A3DSCOPE_FILTER) += avf_a3dscope.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index c532682fc2..9d565f2376 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -579,6 +579,7 @@ extern const AVFilter ff_vsrc_yuvtestsrc;
extern const AVFilter ff_vsrc_zoneplate;
extern const AVFilter ff_vsink_nullsink;
+extern const AVFilter ff_vsink_fbsink;
/* multimedia filters */
extern const AVFilter ff_avf_a3dscope;
diff --git a/libavfilter/vsink_fbsink.c b/libavfilter/vsink_fbsink.c
new file mode 100644
index 0000000000..97d94def46
--- /dev/null
+++ b/libavfilter/vsink_fbsink.c
@@ -0,0 +1,319 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avfilter.h"
+#include "internal.h"
+#include "libavutil/internal.h"
+#include "libavutil/opt.h"
+#include "libavfilter/filters.h"
+#include "libavfilter/formats.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/common.h"
+#include "libavutil/file_open.h"
+#include "libavutil/mem.h"
+#include "libavutil/pixdesc.h"
+#include <linux/fb.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#define SAFE_CLOSE(fd) do { if (fd >= 0) { close(fd); fd = -1; } } while (0)
+
+typedef struct FBSinkContext {
+ const AVClass *class;
+ int xoffset; ///< x coordinate of top left corner
+ int yoffset; ///< y coordinate of top left corner
+ struct fb_var_screeninfo varinfo; ///< framebuffer variable info
+ struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
+ int fd; ///< framebuffer device file descriptor
+ uint8_t *data; ///< framebuffer data
+ const char *devname; ///< framebuffer device
+ int width, height;
+ int format;
+ AVRational sample_aspect_ratio;
+ AVRational frame_rate;
+ int bits_per_coded_sample;
+} FBSinkContext;
+
+struct rgb_pixfmt_map_entry {
+ int bits_per_pixel;
+ int red_offset, green_offset, blue_offset, alpha_offset;
+ enum AVPixelFormat pixfmt;
+};
+
+static const char *ff_fbdev_default_device(void)
+{
+ const char *dev = getenv("FRAMEBUFFER");
+ if (!dev)
+ dev = "/dev/fb0";
+ return dev;
+}
+
+static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
+ // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
+ { 32, 0, 8, 16, 24, AV_PIX_FMT_RGBA },
+ { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
+ { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
+ { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
+ { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
+ { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
+ { 16, 11, 5, 0, 0, AV_PIX_FMT_RGB565 },
+};
+
+static enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
+{
+ int i;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
+ const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
+ if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
+ entry->red_offset == varinfo->red.offset &&
+ entry->green_offset == varinfo->green.offset &&
+ entry->blue_offset == varinfo->blue.offset)
+ return entry->pixfmt;
+ }
+
+ return AV_PIX_FMT_NONE;
+}
+
+static int fbsink_init(AVFilterContext *ctx)
+{
+ FBSinkContext *fbdev = ctx->priv;
+ const char *device;
+ int ret;
+
+ if (fbdev->devname)
+ device = fbdev->devname;
+ else
+ device = ff_fbdev_default_device();
+
+ if ((fbdev->fd = avpriv_open(device, O_RDWR)) == -1) {
+ ret = AVERROR(errno);
+ av_log(ctx, AV_LOG_ERROR,
+ "Could not open framebuffer device '%s': %s\n",
+ device, av_err2str(ret));
+ return ret;
+ }
+
+ if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
+ ret = AVERROR(errno);
+ av_log(ctx, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
+ goto fail;
+ }
+
+ if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
+ ret = AVERROR(errno);
+ av_log(ctx, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
+ goto fail;
+ }
+
+ fbdev->format = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
+ if (fbdev->format == AV_PIX_FMT_NONE) {
+ ret = AVERROR(EINVAL);
+ av_log(ctx, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
+ goto fail;
+ }
+
+ fbdev->bits_per_coded_sample = av_get_bits_per_pixel(av_pix_fmt_desc_get(fbdev->format));
+
+ fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
+ if (fbdev->data == MAP_FAILED) {
+ ret = AVERROR(errno);
+ av_log(ctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
+ goto fail;
+ }
+
+ return 0;
+fail:
+ close(fbdev->fd);
+ return 0;
+}
+
+static void fbsink_uninit(AVFilterContext *ctx)
+{
+ FBSinkContext *fbdev = ctx->priv;
+ if (fbdev->data != MAP_FAILED && fbdev->data != NULL) {
+ munmap(fbdev->data, fbdev->fixinfo.smem_len);
+ fbdev->data = MAP_FAILED;
+ }
+ SAFE_CLOSE(fbdev->fd);
+}
+
+static int fbsink_activate(AVFilterContext *ctx)
+{
+ AVFilterLink *inlink = ctx->inputs[0];
+ FBSinkContext *fbdev = ctx->priv;
+ const uint8_t *pin;
+ uint8_t *pout;
+ int disp_height;
+ int bytes_to_copy;
+ int64_t pts;
+ AVFrame *frame;
+ int bytes_per_pixel = fbdev->bits_per_coded_sample >> 3;
+ int video_width = fbdev->width;
+ int video_height = fbdev->height;
+ int src_line_size;
+ int i, ret;
+
+ if (!ff_inlink_check_available_frame(inlink)) {
+ ff_inlink_acknowledge_status(inlink, &ret, &pts);
+ if (ret >= 0)
+ ff_inlink_request_frame(inlink);
+
+ return ret;
+ }
+
+ ret = ff_inlink_consume_frame(inlink, &frame);
+ if (ret < 0)
+ return ret;
+
+ src_line_size = frame->linesize[0];
+ disp_height = FFMIN(fbdev->varinfo.yres, video_height);
+ bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
+
+ pin = frame->data[0];
+ pout = fbdev->data +
+ bytes_per_pixel * fbdev->varinfo.xoffset +
+ fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
+
+ if (fbdev->xoffset) {
+ if (fbdev->xoffset < 0) {
+ if (-fbdev->xoffset >= video_width) // nothing to display
+ return 0;
+ bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
+ pin -= fbdev->xoffset * bytes_per_pixel;
+ } else {
+ int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
+ if (diff > 0) {
+ if (diff >= video_width) // nothing to display
+ return 0;
+ bytes_to_copy -= diff * bytes_per_pixel;
+ }
+ pout += bytes_per_pixel * fbdev->xoffset;
+ }
+ }
+
+ if (fbdev->yoffset) {
+ if (fbdev->yoffset < 0) {
+ if (-fbdev->yoffset >= video_height) // nothing to display
+ return 0;
+ disp_height += fbdev->yoffset;
+ pin -= fbdev->yoffset * src_line_size;
+ } else {
+ int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
+ if (diff > 0) {
+ if (diff >= video_height) // nothing to display
+ return 0;
+ disp_height -= diff;
+ }
+ pout += fbdev->yoffset * fbdev->fixinfo.line_length;
+ }
+ }
+
+ for (i = 0; i < disp_height; i++) {
+ memcpy(pout, pin, bytes_to_copy);
+ pout += fbdev->fixinfo.line_length;
+ pin += src_line_size;
+ }
+
+ if (fbdev->frame_rate.den > 0 && fbdev->frame_rate.num > 0)
+ usleep(av_rescale(AV_TIME_BASE, fbdev->frame_rate.den, fbdev->frame_rate.num));
+
+ return ff_filter_frame(ctx->outputs[0], frame);
+}
+
+static int fbsink_query_formats(AVFilterContext *ctx)
+{
+ FBSinkContext *fbdev = ctx->priv;
+ AVFilterFormats *formats = NULL;
+ int ret;
+
+ ret = ff_add_format(&formats, fbdev->format);
+ if (ret < 0)
+ return ret;
+
+ ret = ff_set_common_formats(ctx, formats);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int fbsink_input_props(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ FBSinkContext *sink = ctx->priv;
+
+ switch (inlink->type) {
+ case AVMEDIA_TYPE_VIDEO:
+ sink->width = inlink->w;
+ sink->height = inlink->h;
+ sink->sample_aspect_ratio = inlink->sample_aspect_ratio;
+ sink->frame_rate = inlink->frame_rate;
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+#define OFFSET(x) offsetof(FBSinkContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption fbsink_options[] = {
+ { "devname", "fb device name", OFFSET(devname), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, .flags = FLAGS },
+ { NULL },
+};
+
+static const AVClass fbsink_class = {
+ .class_name = "fbsink_class",
+ .item_name = av_default_item_name,
+ .option = fbsink_options,
+ .version = LIBAVUTIL_VERSION_INT,
+ .category = AV_CLASS_CATEGORY_FILTER,
+};
+
+static const AVFilterPad avfilter_vsink_fbsink_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = fbsink_input_props,
+ },
+};
+
+static const AVFilterPad avfilter_vsink_fbsink_outputs[] = {
+ {
+ .name = NULL,
+ .type = AVMEDIA_TYPE_VIDEO,
+ },
+};
+
+const AVFilter ff_vsink_fbsink = {
+ .name = "fbsink",
+ .description = NULL_IF_CONFIG_SMALL("Frame buffer video display."),
+ .priv_class = &fbsink_class,
+ .priv_size = sizeof(FBSinkContext),
+ .init = fbsink_init,
+ .uninit = fbsink_uninit,
+ .activate = fbsink_activate,
+ FILTER_QUERY_FUNC(fbsink_query_formats),
+ FILTER_INPUTS(avfilter_vsink_fbsink_inputs),
+ FILTER_OUTPUTS(avfilter_vsink_fbsink_outputs),
+};
--
2.34.1
More information about the ffmpeg-devel
mailing list