From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 0249449498 for ; Tue, 11 Jun 2024 13:03:27 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F78668D820; Tue, 11 Jun 2024 16:03:25 +0300 (EEST) Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA59668D61A for ; Tue, 11 Jun 2024 16:03:18 +0300 (EEST) Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-6c702226b0aso4861358a12.1 for ; Tue, 11 Jun 2024 06:03:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718110996; x=1718715796; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3wWplwdxNsCrXHWHQzKhwwicvVkwtnuRABvuafEF7a8=; b=bW4OIk1wmA1WYbn8tFe8Zn1Dd4/uoXwE6uPok4In/p7UhDn2cunm2/q96bi6LdPzNv 9iU0DEj4TPvCuceqXHB+Y4udRRn3/RvX7IhmxD2y/8YCcYIGjRVib2Jk8QkY9pywoL5x lgGD6VegUxpEI5MEk6qi2FNQKouEVHtpJI89Vrdcf2zsJKRfsyhX0wquJJiFF4lWUiNv GBjK3KSqvhLsvhWlaeHUdbCx01ixBtU3e6rvVmPFjhfL+/tyCioHgqVHGZ31uv2OUV8I ysP6Es4dzHbhZWMn/cZpTa7wo9PJbTpkZnFKc7PSXhX/JXrkeW13JfXgzT0k5ceulND7 /vyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718110996; x=1718715796; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3wWplwdxNsCrXHWHQzKhwwicvVkwtnuRABvuafEF7a8=; b=eIDpoMaffflYpZpM77m398QRR7D2JDycP3w3DBZmFUZTCGE4KCv9wTXEI+gCUhg2n9 L9wyrSUesVML9phTNvHIIgHi2SJmK/FU0ZwmKtb5RZfhE+klhj+Ue022Q+elTZWhVuhY 9XLQWNc3YKfTkpeuDjn9/qnwg62CajHJIJtcLUgSGwPFcJbP+QocUa09X0aXeji+4UG1 p9gbSTGzSOSjSzlAenh+pqH5efpkscDBFdfrB0Fw+19JbbzcJi5ZLAEqEZl7UeH2dZy7 ItSZfOpiYjQBNQ8kYxTerYtIUgNniEv6iaWguVbNXF0+a7SO2AljNLBTval0o7u1xPtf zRUw== X-Gm-Message-State: AOJu0YyQe9+Zm+LAKG2s/sEORnirsHT7l1atVQX8O4xhCFeB8a+px/xd FnhNFd95ifbjtnum+i509ZWS8hzgm6jsPTrszkAMAgu8dWqFG6UyDaF6hg== X-Google-Smtp-Source: AGHT+IFrYSrqV9tVsFujb2fVpm8ok99MqHPlSYZZgFK4sByG0B8IhFk8RF8JaiaRmFxtglV9+5ijAg== X-Received: by 2002:a17:902:f54b:b0:1f7:2046:a8c5 with SMTP id d9443c01a7336-1f728792543mr33163015ad.1.1718110995753; Tue, 11 Jun 2024 06:03:15 -0700 (PDT) Received: from PC.mioffice.cn ([43.224.245.231]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6bd7ed6bdsm101888955ad.248.2024.06.11.06.03.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 06:03:15 -0700 (PDT) From: Shiqi Zhu To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jun 2024 21:03:10 +0800 Message-Id: <20240611130310.1131755-1-hiccupzhu@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add fbsink for video display. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Shiqi Zhu Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This patch wants to extend the functionality of sink-filter. Signed-off-by: Shiqi Zhu --- 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 +#include +#include +#include +#include + +#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 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".