Skip to content

Commit

Permalink
fftools/ffplay: add hwaccel decoding support
Browse files Browse the repository at this point in the history
Add vulkan renderer via libplacebo.

Simple usage:
$ ffplay -hwaccel vulkan foo.mp4

Use cuda to vulkan map:
$ ffplay -hwaccel cuda foo.mp4

Create vulkan instance by libplacebo, and enable debug:
$ ffplay -hwaccel vulkan \
	-vulkan_params create_by_placebo=1:debug=1 foo.mp4

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
  • Loading branch information
quink-black committed Nov 14, 2023
1 parent 889a022 commit a1a6a32
Show file tree
Hide file tree
Showing 6 changed files with 971 additions and 6 deletions.
2 changes: 1 addition & 1 deletion configure
Expand Up @@ -3904,7 +3904,7 @@ ffmpeg_select="aformat_filter anull_filter atrim_filter format_filter
ffmpeg_suggest="ole32 psapi shell32"
ffplay_deps="avcodec avformat avfilter swscale swresample sdl2"
ffplay_select="crop_filter transpose_filter hflip_filter vflip_filter rotate_filter"
ffplay_suggest="shell32"
ffplay_suggest="shell32 libplacebo vulkan"
ffprobe_deps="avcodec avformat"
ffprobe_suggest="shell32"

Expand Down
12 changes: 12 additions & 0 deletions doc/ffplay.texi
Expand Up @@ -196,6 +196,18 @@ will produce a thread pool with this many threads available for parallel
processing. The default is 0 which means that the thread count will be
determined by the number of available CPUs.

@item -enable_vulkan
Use vulkan renderer rather than SDL builtin renderer. Depends on libplacebo.

@item -vulkan_params

Vulkan configuration using a list of @var{key}=@var{value} pairs separated by
":".

@item -hwaccel
Use HW accelerated decoding. Enable this option will enable vulkan renderer
automatically.

@end table

@section While playing
Expand Down
2 changes: 2 additions & 0 deletions fftools/Makefile
Expand Up @@ -22,6 +22,8 @@ OBJS-ffmpeg += \
fftools/sync_queue.o \
fftools/thread_queue.o \

OBJS-ffplay += fftools/ffplay_renderer.o

define DOFFTOOL
OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes)
ifdef HAVE_GNU_WINDRES
Expand Down
96 changes: 91 additions & 5 deletions fftools/ffplay.c
Expand Up @@ -58,6 +58,7 @@
#include <SDL_thread.h>

#include "cmdutils.h"
#include "ffplay_renderer.h"
#include "opt_common.h"

const char program_name[] = "ffplay";
Expand Down Expand Up @@ -350,6 +351,9 @@ static char *afilters = NULL;
static int autorotate = 1;
static int find_stream_info = 1;
static int filter_nbthreads = 0;
static int enable_vulkan = 0;
static char *vulkan_params = NULL;
static const char *hwaccel = NULL;

/* current context */
static int is_full_screen;
Expand All @@ -362,6 +366,8 @@ static SDL_Renderer *renderer;
static SDL_RendererInfo renderer_info = {0};
static SDL_AudioDeviceID audio_dev;

static VkRenderer *vk_renderer;

static const struct TextureFormatEntry {
enum AVPixelFormat format;
int texture_fmt;
Expand Down Expand Up @@ -954,6 +960,11 @@ static void video_image_display(VideoState *is)
SDL_Rect rect;

vp = frame_queue_peek_last(&is->pictq);
if (vk_renderer) {
vk_renderer_display(vk_renderer, vp->frame);
return;
}

if (is->subtitle_st) {
if (frame_queue_nb_remaining(&is->subpq) > 0) {
sp = frame_queue_peek(&is->subpq);
Expand Down Expand Up @@ -1289,6 +1300,8 @@ static void do_exit(VideoState *is)
}
if (renderer)
SDL_DestroyRenderer(renderer);
if (vk_renderer)
vk_renderer_destroy(vk_renderer);
if (window)
SDL_DestroyWindow(window);
uninit_opts();
Expand Down Expand Up @@ -2546,6 +2559,37 @@ static int audio_open(void *opaque, AVChannelLayout *wanted_channel_layout, int
return spec.size;
}

static int create_hwaccel(AVBufferRef **device_ctx)
{
enum AVHWDeviceType type;
int ret;
AVBufferRef *vk_dev;

*device_ctx = NULL;

if (!hwaccel)
return 0;

type = av_hwdevice_find_type_by_name(hwaccel);
if (type == AV_HWDEVICE_TYPE_NONE)
return AVERROR(ENOTSUP);

ret = vk_renderer_get_hw_dev(vk_renderer, &vk_dev);
if (ret < 0)
return ret;

ret = av_hwdevice_ctx_create_derived(device_ctx, type, vk_dev, 0);
if (!ret)
return 0;

if (ret != AVERROR(ENOSYS))
return ret;

av_log(NULL, AV_LOG_WARNING, "Derive %s from vulkan not supported.\n", hwaccel);
ret = av_hwdevice_ctx_create(device_ctx, type, NULL, NULL, 0);
return ret;
}

/* open a given stream. Return 0 if OK */
static int stream_component_open(VideoState *is, int stream_index)
{
Expand Down Expand Up @@ -2613,6 +2657,12 @@ static int stream_component_open(VideoState *is, int stream_index)

av_dict_set(&opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);

if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
ret = create_hwaccel(&avctx->hw_device_ctx);
if (ret < 0)
goto fail;
}

if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
goto fail;
}
Expand Down Expand Up @@ -3447,6 +3497,8 @@ static void event_loop(VideoState *cur_stream)
SDL_DestroyTexture(cur_stream->vis_texture);
cur_stream->vis_texture = NULL;
}
if (vk_renderer)
vk_renderer_resize(vk_renderer, screen_width, screen_height);
case SDL_WINDOWEVENT_EXPOSED:
cur_stream->force_refresh = 1;
}
Expand Down Expand Up @@ -3611,6 +3663,9 @@ static const OptionDef options[] = {
{ "find_stream_info", OPT_BOOL | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
"read and decode the streams to fill missing information with heuristics" },
{ "filter_threads", HAS_ARG | OPT_INT | OPT_EXPERT, { &filter_nbthreads }, "number of filter threads per graph" },
{ "enable_vulkan", OPT_BOOL, { &enable_vulkan }, "enable vulkan renderer" },
{ "vulkan_params", HAS_ARG | OPT_STRING | OPT_EXPERT, { &vulkan_params }, "vulkan configuration using a list of key=value pairs separated by ':'" },
{ "hwaccel", HAS_ARG | OPT_STRING | OPT_EXPERT, { &hwaccel }, "use HW accelerated decoding" },
{ NULL, },
};

Expand Down Expand Up @@ -3725,9 +3780,40 @@ int main(int argc, char **argv)
#ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
#endif
if (hwaccel && !enable_vulkan) {
av_log(NULL, AV_LOG_INFO, "Enable vulkan renderer to support hwaccel %s\n", hwaccel);
enable_vulkan = 1;
}
if (enable_vulkan) {
vk_renderer = vk_get_renderer();
if (vk_renderer) {
#if SDL_VERSION_ATLEAST(2, 0, 6)
flags |= SDL_WINDOW_VULKAN;
#endif
} else {
av_log(NULL, AV_LOG_WARNING, "Doesn't support vulkan renderer, fallback to SDL renderer\n");
enable_vulkan = 0;
}
}
window = SDL_CreateWindow(program_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, default_width, default_height, flags);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
if (window) {
if (!window) {
av_log(NULL, AV_LOG_FATAL, "Failed to create window: %s", SDL_GetError());
do_exit(NULL);
}

if (vk_renderer) {
AVDictionary *dict = NULL;

if (vulkan_params)
av_dict_parse_string(&dict, vulkan_params, "=", ":", 0);
ret = vk_renderer_create(vk_renderer, window, dict);
av_dict_free(&dict);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL, "Failed to create vulkan renderer, %s\n", av_err2str(ret));
do_exit(NULL);
}
} else {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
av_log(NULL, AV_LOG_WARNING, "Failed to initialize a hardware accelerated renderer: %s\n", SDL_GetError());
Expand All @@ -3737,10 +3823,10 @@ int main(int argc, char **argv)
if (!SDL_GetRendererInfo(renderer, &renderer_info))
av_log(NULL, AV_LOG_VERBOSE, "Initialized %s renderer.\n", renderer_info.name);
}
}
if (!window || !renderer || !renderer_info.num_texture_formats) {
av_log(NULL, AV_LOG_FATAL, "Failed to create window or renderer: %s", SDL_GetError());
do_exit(NULL);
if (!renderer || !renderer_info.num_texture_formats) {
av_log(NULL, AV_LOG_FATAL, "Failed to create window or renderer: %s", SDL_GetError());
do_exit(NULL);
}
}
}

Expand Down

0 comments on commit a1a6a32

Please sign in to comment.