Skip to content

Commit 7ec44d3

Browse files
committed
testffmpeg: added support for VA-API and DRM frames when using Vulkan rendering
1 parent ac9ef17 commit 7ec44d3

3 files changed

Lines changed: 294 additions & 39 deletions

File tree

test/testffmpeg.c

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static SDL_Window *window;
8181
static SDL_Renderer *renderer;
8282
static SDL_AudioStream *audio;
8383
static SDL_Texture *video_texture;
84+
static AVCodecContext *audio_context;
85+
static AVCodecContext *video_context;
8486
static Uint64 video_start;
8587
static bool nodelay;
8688
static bool software_only;
@@ -347,7 +349,8 @@ static bool SupportedPixelFormat(enum AVPixelFormat format)
347349
return true;
348350
}
349351
#endif
350-
if (vulkan_context && format == AV_PIX_FMT_VULKAN) {
352+
if (vulkan_context &&
353+
(format == AV_PIX_FMT_VULKAN || format == AV_PIX_FMT_VAAPI || format == AV_PIX_FMT_DRM_PRIME)) {
351354
return true;
352355
}
353356
}
@@ -497,15 +500,28 @@ static SDL_Colorspace GetFrameColorspace(AVFrame *frame)
497500
SDL_Colorspace colorspace = SDL_COLORSPACE_SRGB;
498501

499502
if (frame && frame->colorspace != AVCOL_SPC_RGB) {
503+
if (frame->colorspace != AVCOL_SPC_UNSPECIFIED ||
504+
video_context->colorspace == AVCOL_SPC_UNSPECIFIED) {
500505
#ifdef DEBUG_COLORSPACE
501-
SDL_Log("Frame colorspace: range: %d, primaries: %d, trc: %d, colorspace: %d, chroma_location: %d", frame->color_range, frame->color_primaries, frame->color_trc, frame->colorspace, frame->chroma_location);
506+
SDL_Log("Frame colorspace: range: %d, primaries: %d, trc: %d, colorspace: %d, chroma_location: %d", frame->color_range, frame->color_primaries, frame->color_trc, frame->colorspace, frame->chroma_location);
502507
#endif
503-
colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
504-
frame->color_range,
505-
frame->color_primaries,
506-
frame->color_trc,
507-
frame->colorspace,
508-
frame->chroma_location);
508+
colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
509+
frame->color_range,
510+
frame->color_primaries,
511+
frame->color_trc,
512+
frame->colorspace,
513+
frame->chroma_location);
514+
} else if (video_context->colorspace != AVCOL_SPC_UNSPECIFIED) {
515+
#ifdef DEBUG_COLORSPACE
516+
SDL_Log("Video colorspace: range: %d, primaries: %d, trc: %d, colorspace: %d, chroma_location: %d", video_context->color_range, video_context->color_primaries, video_context->color_trc, video_context->colorspace, video_context->chroma_sample_location);
517+
#endif
518+
colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
519+
video_context->color_range,
520+
video_context->color_primaries,
521+
video_context->color_trc,
522+
video_context->colorspace,
523+
video_context->chroma_sample_location);
524+
}
509525
}
510526
return colorspace;
511527
}
@@ -654,9 +670,26 @@ static bool GetTextureForMemoryFrame(AVFrame *frame, SDL_Texture **texture)
654670
return true;
655671
}
656672

673+
static bool GetTextureForVulkanFrame(AVFrame *frame, SDL_Texture **texture)
674+
{
675+
SDL_PropertiesID props;
676+
677+
if (*texture) {
678+
SDL_DestroyTexture(*texture);
679+
}
680+
681+
props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC);
682+
*texture = CreateVulkanVideoTexture(vulkan_context, frame, renderer, props);
683+
SDL_DestroyProperties(props);
684+
if (!*texture) {
685+
return false;
686+
}
687+
return true;
688+
}
689+
657690
#ifdef HAVE_EGL
658691

659-
static bool GetNV12TextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
692+
static bool GetEGLNV12TextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
660693
{
661694
AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx ? frame->hw_frames_ctx->data : NULL);
662695
const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0];
@@ -746,7 +779,7 @@ static bool GetNV12TextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
746779
return true;
747780
}
748781

749-
static bool GetOESTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
782+
static bool GetEGLOESTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
750783
{
751784
AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx ? frame->hw_frames_ctx->data : NULL);
752785
const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0];
@@ -931,19 +964,24 @@ static bool GetOESTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
931964

932965
static bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
933966
{
967+
if (vulkan_context) {
968+
return GetTextureForVulkanFrame(frame, texture);
969+
} else {
934970
#ifdef HAVE_EGL
935-
const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0];
971+
const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0];
936972

937-
if (desc->nb_layers == 2 &&
938-
desc->layers[0].format == DRM_FORMAT_R8 &&
939-
desc->layers[1].format == DRM_FORMAT_GR88) {
940-
return GetNV12TextureForDRMFrame(frame, texture);
941-
} else {
942-
return GetOESTextureForDRMFrame(frame, texture);
943-
}
973+
if (desc->nb_layers == 2 &&
974+
desc->layers[0].format == DRM_FORMAT_R8 &&
975+
desc->layers[1].format == DRM_FORMAT_GR88) {
976+
return GetEGLNV12TextureForDRMFrame(frame, texture);
977+
} else {
978+
return GetEGLOESTextureForDRMFrame(frame, texture);
979+
}
944980
#else
945-
return false;
981+
SDL_Log("Creating EGL textures is not supported");
982+
return false;
946983
#endif
984+
}
947985
}
948986

949987
static bool GetTextureForVAAPIFrame(AVFrame *frame, SDL_Texture **texture)
@@ -1029,23 +1067,6 @@ static bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **texture
10291067
#endif
10301068
}
10311069

1032-
static bool GetTextureForVulkanFrame(AVFrame *frame, SDL_Texture **texture)
1033-
{
1034-
SDL_PropertiesID props;
1035-
1036-
if (*texture) {
1037-
SDL_DestroyTexture(*texture);
1038-
}
1039-
1040-
props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC);
1041-
*texture = CreateVulkanVideoTexture(vulkan_context, frame, renderer, props);
1042-
SDL_DestroyProperties(props);
1043-
if (!*texture) {
1044-
return false;
1045-
}
1046-
return true;
1047-
}
1048-
10491070
static bool GetTextureForFrame(AVFrame *frame, SDL_Texture **texture)
10501071
{
10511072
switch (frame->format) {
@@ -1305,8 +1326,6 @@ int main(int argc, char *argv[])
13051326
const char *video_codec_name = NULL;
13061327
const AVCodec *audio_codec = NULL;
13071328
const AVCodec *video_codec = NULL;
1308-
AVCodecContext *audio_context = NULL;
1309-
AVCodecContext *video_context = NULL;
13101329
AVPacket *pkt = NULL;
13111330
AVFrame *frame = NULL;
13121331
double first_pts = -1.0;

0 commit comments

Comments
 (0)