Atlas - testffmpeg.c

Home / ext / SDL / test Lines: 1 | Size: 56800 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12/* Simple program: Display a video with a sprite bouncing around over it 13 * 14 * For a more complete video example, see ffplay.c in the ffmpeg sources. 15 */ 16 17#include <SDL3/SDL.h> 18#include <SDL3/SDL_main.h> 19#include <SDL3/SDL_test.h> 20 21#include <libavcodec/avcodec.h> 22#include <libavformat/avformat.h> 23#include <libavutil/avutil.h> 24#include <libavutil/mastering_display_metadata.h> 25#include <libavutil/pixdesc.h> 26#include <libswscale/swscale.h> 27 28#ifdef HAVE_EGL 29#include <SDL3/SDL_egl.h> 30#include <SDL3/SDL_opengl.h> 31#include <SDL3/SDL_opengles2.h> 32 33#include <libavutil/hwcontext_drm.h> 34 35#ifndef fourcc_code 36#define fourcc_code(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24)) 37#endif 38#ifndef DRM_FORMAT_R8 39#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') 40#endif 41#ifndef DRM_FORMAT_GR88 42#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') 43#endif 44#endif 45 46#define DRM_FORMAT_MOD_VENDOR_NONE 0 47#define DRM_FORMAT_RESERVED ((1ULL << 56) - 1) 48 49#define fourcc_mod_get_vendor(modifier) \ 50 (((modifier) >> 56) & 0xff) 51 52#define fourcc_mod_is_vendor(modifier, vendor) \ 53 (fourcc_mod_get_vendor(modifier) == DRM_FORMAT_MOD_VENDOR_##vendor) 54 55#define fourcc_mod_code(vendor, val) \ 56 ((((Uint64)DRM_FORMAT_MOD_VENDOR_##vendor) << 56) | ((val) & 0x00ffffffffffffffULL)) 57 58#define DRM_FORMAT_MOD_INVALID fourcc_mod_code(NONE, DRM_FORMAT_RESERVED) 59#define DRM_FORMAT_MOD_LINEAR fourcc_mod_code(NONE, 0) 60 61#ifdef SDL_PLATFORM_APPLE 62#include <CoreVideo/CoreVideo.h> 63#endif 64 65#ifdef SDL_PLATFORM_WIN32 66#define COBJMACROS 67#include <libavutil/hwcontext_d3d11va.h> 68#endif /* SDL_PLATFORM_WIN32 */ 69 70#include "testffmpeg_vulkan.h" 71 72#include "icon.h" 73 74static SDL_Texture *sprite; 75static SDL_FRect *positions; 76static SDL_FRect *velocities; 77static int sprite_w, sprite_h; 78static int num_sprites = 0; 79 80static SDL_Window *window; 81static SDL_Renderer *renderer; 82static SDL_AudioStream *audio; 83static SDL_Texture *video_texture; 84static Uint64 video_start; 85static bool software_only; 86static bool has_eglCreateImage; 87#ifdef HAVE_EGL 88static bool has_EGL_EXT_image_dma_buf_import; 89static bool has_EGL_EXT_image_dma_buf_import_modifiers; 90static PFNGLACTIVETEXTUREARBPROC glActiveTextureARBFunc; 91static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESFunc; 92#endif 93#ifdef SDL_PLATFORM_WIN32 94static ID3D11Device *d3d11_device; 95static ID3D11DeviceContext *d3d11_context; 96#endif 97static VulkanVideoContext *vulkan_context; 98struct SwsContextContainer 99{ 100 struct SwsContext *context; 101}; 102static const char *SWS_CONTEXT_CONTAINER_PROPERTY = "SWS_CONTEXT_CONTAINER"; 103static bool verbose; 104 105static bool CreateWindowAndRenderer(SDL_WindowFlags window_flags, const char *driver) 106{ 107 SDL_PropertiesID props; 108 bool useOpenGL = (driver && (SDL_strcmp(driver, "opengl") == 0 || SDL_strcmp(driver, "opengles2") == 0)); 109 bool useEGL = (driver && SDL_strcmp(driver, "opengles2") == 0); 110 bool useVulkan = (driver && SDL_strcmp(driver, "vulkan") == 0); 111 Uint32 flags = SDL_WINDOW_HIDDEN; 112 113 if (useOpenGL) { 114 if (useEGL) { 115 SDL_SetHint(SDL_HINT_VIDEO_FORCE_EGL, "1"); 116 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); 117 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); 118 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); 119 } else { 120 SDL_SetHint(SDL_HINT_VIDEO_FORCE_EGL, "0"); 121 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0); 122 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); 123 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 124 } 125 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); 126 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); 127 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); 128 129 flags |= SDL_WINDOW_OPENGL; 130 } 131 if (useVulkan) { 132 flags |= SDL_WINDOW_VULKAN; 133 } 134 135 /* The window will be resized to the video size when it's loaded, in OpenVideoStream() */ 136 window = SDL_CreateWindow("testffmpeg", 1920, 1080, flags); 137 if (!window) { 138 return false; 139 } 140 141 if (useVulkan) { 142 vulkan_context = CreateVulkanVideoContext(window); 143 if (!vulkan_context) { 144 SDL_DestroyWindow(window); 145 window = NULL; 146 return false; 147 } 148 } 149 150 props = SDL_CreateProperties(); 151 SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, driver); 152 SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); 153 if (useVulkan) { 154 SetupVulkanRenderProperties(vulkan_context, props); 155 } 156 if (SDL_GetBooleanProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_HDR_ENABLED_BOOLEAN, false)) { 157 /* Try to create an HDR capable renderer */ 158 SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB_LINEAR); 159 renderer = SDL_CreateRendererWithProperties(props); 160 } 161 if (!renderer) { 162 /* Try again with the sRGB colorspace */ 163 SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB); 164 renderer = SDL_CreateRendererWithProperties(props); 165 } 166 SDL_DestroyProperties(props); 167 if (!renderer) { 168 SDL_DestroyWindow(window); 169 window = NULL; 170 return false; 171 } 172 173 SDL_Log("Created renderer %s", SDL_GetRendererName(renderer)); 174 175#ifdef HAVE_EGL 176 if (useEGL) { 177 const char *egl_extensions = eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS); 178 if (!egl_extensions) { 179 return false; 180 } 181 182 char *extensions = SDL_strdup(egl_extensions); 183 if (!extensions) { 184 return false; 185 } 186 187 char *saveptr, *token; 188 token = SDL_strtok_r(extensions, " ", &saveptr); 189 if (!token) { 190 SDL_free(extensions); 191 return false; 192 } 193 do { 194 if (SDL_strcmp(token, "EGL_EXT_image_dma_buf_import") == 0) { 195 has_EGL_EXT_image_dma_buf_import = true; 196 } else if (SDL_strcmp(token, "EGL_EXT_image_dma_buf_import_modifiers") == 0) { 197 has_EGL_EXT_image_dma_buf_import_modifiers = true; 198 } 199 } while ((token = SDL_strtok_r(NULL, " ", &saveptr)) != NULL); 200 201 SDL_free(extensions); 202 203 if (SDL_GL_ExtensionSupported("GL_OES_EGL_image")) { 204 glEGLImageTargetTexture2DOESFunc = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES"); 205 } 206 207 glActiveTextureARBFunc = (PFNGLACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress("glActiveTextureARB"); 208 209 if (has_EGL_EXT_image_dma_buf_import && 210 glEGLImageTargetTexture2DOESFunc && 211 glActiveTextureARBFunc) { 212 has_eglCreateImage = true; 213 } 214 } 215#endif /* HAVE_EGL */ 216 217#ifdef SDL_PLATFORM_WIN32 218 d3d11_device = (ID3D11Device *)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_D3D11_DEVICE_POINTER, NULL); 219 if (d3d11_device) { 220 ID3D11Device_AddRef(d3d11_device); 221 ID3D11Device_GetImmediateContext(d3d11_device, &d3d11_context); 222 } 223#endif 224 225 return true; 226} 227 228static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h) 229{ 230 SDL_Texture *texture = NULL; 231 SDL_Surface *surface; 232 SDL_IOStream *src = SDL_IOFromConstMem(data, len); 233 if (src) { 234 surface = SDL_LoadPNG_IO(src, true); 235 if (surface) { 236 /* Treat white as transparent */ 237 SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255)); 238 239 texture = SDL_CreateTextureFromSurface(r, surface); 240 *w = surface->w; 241 *h = surface->h; 242 SDL_DestroySurface(surface); 243 } 244 } 245 return texture; 246} 247 248static void MoveSprite(void) 249{ 250 SDL_Rect viewport; 251 SDL_FRect *position, *velocity; 252 int i; 253 254 SDL_GetRenderViewport(renderer, &viewport); 255 256 for (i = 0; i < num_sprites; ++i) { 257 position = &positions[i]; 258 velocity = &velocities[i]; 259 position->x += velocity->x; 260 if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) { 261 velocity->x = -velocity->x; 262 position->x += velocity->x; 263 } 264 position->y += velocity->y; 265 if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) { 266 velocity->y = -velocity->y; 267 position->y += velocity->y; 268 } 269 } 270 271 /* Blit the sprite onto the screen */ 272 for (i = 0; i < num_sprites; ++i) { 273 position = &positions[i]; 274 275 /* Blit the sprite onto the screen */ 276 SDL_RenderTexture(renderer, sprite, NULL, position); 277 } 278} 279 280static SDL_PixelFormat GetTextureFormat(enum AVPixelFormat format) 281{ 282 switch (format) { 283 case AV_PIX_FMT_RGB8: 284 return SDL_PIXELFORMAT_RGB332; 285 case AV_PIX_FMT_RGB444: 286 return SDL_PIXELFORMAT_XRGB4444; 287 case AV_PIX_FMT_RGB555: 288 return SDL_PIXELFORMAT_XRGB1555; 289 case AV_PIX_FMT_BGR555: 290 return SDL_PIXELFORMAT_XBGR1555; 291 case AV_PIX_FMT_RGB565: 292 return SDL_PIXELFORMAT_RGB565; 293 case AV_PIX_FMT_BGR565: 294 return SDL_PIXELFORMAT_BGR565; 295 case AV_PIX_FMT_RGB24: 296 return SDL_PIXELFORMAT_RGB24; 297 case AV_PIX_FMT_BGR24: 298 return SDL_PIXELFORMAT_BGR24; 299 case AV_PIX_FMT_0RGB32: 300 return SDL_PIXELFORMAT_XRGB8888; 301 case AV_PIX_FMT_0BGR32: 302 return SDL_PIXELFORMAT_XBGR8888; 303 case AV_PIX_FMT_NE(RGB0, 0BGR): 304 return SDL_PIXELFORMAT_RGBX8888; 305 case AV_PIX_FMT_NE(BGR0, 0RGB): 306 return SDL_PIXELFORMAT_BGRX8888; 307 case AV_PIX_FMT_RGB32: 308 return SDL_PIXELFORMAT_ARGB8888; 309 case AV_PIX_FMT_RGB32_1: 310 return SDL_PIXELFORMAT_RGBA8888; 311 case AV_PIX_FMT_BGR32: 312 return SDL_PIXELFORMAT_ABGR8888; 313 case AV_PIX_FMT_BGR32_1: 314 return SDL_PIXELFORMAT_BGRA8888; 315 case AV_PIX_FMT_YUV420P: 316 return SDL_PIXELFORMAT_IYUV; 317 case AV_PIX_FMT_YUYV422: 318 return SDL_PIXELFORMAT_YUY2; 319 case AV_PIX_FMT_UYVY422: 320 return SDL_PIXELFORMAT_UYVY; 321 case AV_PIX_FMT_NV12: 322 return SDL_PIXELFORMAT_NV12; 323 case AV_PIX_FMT_NV21: 324 return SDL_PIXELFORMAT_NV21; 325 case AV_PIX_FMT_P010: 326 return SDL_PIXELFORMAT_P010; 327 default: 328 return SDL_PIXELFORMAT_UNKNOWN; 329 } 330} 331 332static bool SupportedPixelFormat(enum AVPixelFormat format) 333{ 334 if (!software_only) { 335 if (has_eglCreateImage && 336 (format == AV_PIX_FMT_VAAPI || format == AV_PIX_FMT_DRM_PRIME)) { 337 return true; 338 } 339#ifdef SDL_PLATFORM_APPLE 340 if (format == AV_PIX_FMT_VIDEOTOOLBOX) { 341 return true; 342 } 343#endif 344#ifdef SDL_PLATFORM_WIN32 345 if (d3d11_device && format == AV_PIX_FMT_D3D11) { 346 return true; 347 } 348#endif 349 if (vulkan_context && format == AV_PIX_FMT_VULKAN) { 350 return true; 351 } 352 } 353 354 if (GetTextureFormat(format) != SDL_PIXELFORMAT_UNKNOWN) { 355 return true; 356 } 357 return false; 358} 359 360static enum AVPixelFormat GetSupportedPixelFormat(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) 361{ 362 const enum AVPixelFormat *p; 363 364 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { 365 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); 366 367 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { 368 /* We support all memory formats using swscale */ 369 break; 370 } 371 372 if (SupportedPixelFormat(*p)) { 373 /* We support this format */ 374 break; 375 } 376 } 377 378 if (*p == AV_PIX_FMT_NONE) { 379 SDL_Log("Couldn't find a supported pixel format:"); 380 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { 381 SDL_Log(" %s", av_get_pix_fmt_name(*p)); 382 } 383 } 384 385 return *p; 386} 387 388static AVCodecContext *OpenVideoStream(AVFormatContext *ic, int stream, const AVCodec *codec) 389{ 390 AVStream *st = ic->streams[stream]; 391 AVCodecParameters *codecpar = st->codecpar; 392 AVCodecContext *context; 393 const AVCodecHWConfig *config; 394 int i; 395 int result; 396 397 SDL_Log("Video stream: %s %dx%d", avcodec_get_name(codec->id), codecpar->width, codecpar->height); 398 399 context = avcodec_alloc_context3(NULL); 400 if (!context) { 401 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_alloc_context3 failed"); 402 return NULL; 403 } 404 405 result = avcodec_parameters_to_context(context, ic->streams[stream]->codecpar); 406 if (result < 0) { 407 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_parameters_to_context failed: %s", av_err2str(result)); 408 avcodec_free_context(&context); 409 return NULL; 410 } 411 context->pkt_timebase = ic->streams[stream]->time_base; 412 413 /* Look for supported hardware accelerated configurations */ 414 i = 0; 415 while (!context->hw_device_ctx && 416 (config = avcodec_get_hw_config(codec, i++)) != NULL) { 417#if 0 418 SDL_Log("Found %s hardware acceleration with pixel format %s", av_hwdevice_get_type_name(config->device_type), av_get_pix_fmt_name(config->pix_fmt)); 419#endif 420 421 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) || 422 !SupportedPixelFormat(config->pix_fmt)) { 423 continue; 424 } 425 426#ifdef SDL_PLATFORM_WIN32 427 if (d3d11_device && config->device_type == AV_HWDEVICE_TYPE_D3D11VA) { 428 AVD3D11VADeviceContext *device_context; 429 430 context->hw_device_ctx = av_hwdevice_ctx_alloc(config->device_type); 431 432 device_context = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)context->hw_device_ctx->data)->hwctx; 433 device_context->device = d3d11_device; 434 ID3D11Device_AddRef(device_context->device); 435 device_context->device_context = d3d11_context; 436 ID3D11DeviceContext_AddRef(device_context->device_context); 437 438 result = av_hwdevice_ctx_init(context->hw_device_ctx); 439 if (result < 0) { 440 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create %s hardware device context: %s", av_hwdevice_get_type_name(config->device_type), av_err2str(result)); 441 } else { 442 SDL_Log("Using %s hardware acceleration with pixel format %s", av_hwdevice_get_type_name(config->device_type), av_get_pix_fmt_name(config->pix_fmt)); 443 } 444 } else 445#endif 446 if (vulkan_context && config->device_type == AV_HWDEVICE_TYPE_VULKAN) { 447 AVVulkanDeviceContext *device_context; 448 449 context->hw_device_ctx = av_hwdevice_ctx_alloc(config->device_type); 450 451 device_context = (AVVulkanDeviceContext *)((AVHWDeviceContext *)context->hw_device_ctx->data)->hwctx; 452 SetupVulkanDeviceContextData(vulkan_context, device_context); 453 454 result = av_hwdevice_ctx_init(context->hw_device_ctx); 455 if (result < 0) { 456 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create %s hardware device context: %s", av_hwdevice_get_type_name(config->device_type), av_err2str(result)); 457 } else { 458 SDL_Log("Using %s hardware acceleration with pixel format %s", av_hwdevice_get_type_name(config->device_type), av_get_pix_fmt_name(config->pix_fmt)); 459 } 460 } else { 461 result = av_hwdevice_ctx_create(&context->hw_device_ctx, config->device_type, NULL, NULL, 0); 462 if (result < 0) { 463 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create %s hardware device context: %s", av_hwdevice_get_type_name(config->device_type), av_err2str(result)); 464 } else { 465 SDL_Log("Using %s hardware acceleration with pixel format %s", av_hwdevice_get_type_name(config->device_type), av_get_pix_fmt_name(config->pix_fmt)); 466 } 467 } 468 } 469 470 /* Allow supported hardware accelerated pixel formats */ 471 context->get_format = GetSupportedPixelFormat; 472 473 if (codecpar->codec_id == AV_CODEC_ID_VVC) { 474 context->strict_std_compliance = -2; 475 476 /* Enable threaded decoding, VVC decode is slow */ 477 context->thread_count = SDL_GetNumLogicalCPUCores(); 478 context->thread_type = (FF_THREAD_FRAME | FF_THREAD_SLICE); 479 } 480 481 result = avcodec_open2(context, codec, NULL); 482 if (result < 0) { 483 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open codec %s: %s", avcodec_get_name(context->codec_id), av_err2str(result)); 484 avcodec_free_context(&context); 485 return NULL; 486 } 487 488 SDL_SetWindowSize(window, codecpar->width, codecpar->height); 489 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); 490 491 return context; 492} 493 494static SDL_Colorspace GetFrameColorspace(AVFrame *frame) 495{ 496 SDL_Colorspace colorspace = SDL_COLORSPACE_SRGB; 497 498 if (frame && frame->colorspace != AVCOL_SPC_RGB) { 499#ifdef DEBUG_COLORSPACE 500 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); 501#endif 502 colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR, 503 frame->color_range, 504 frame->color_primaries, 505 frame->color_trc, 506 frame->colorspace, 507 frame->chroma_location); 508 } 509 return colorspace; 510} 511 512static SDL_PropertiesID CreateVideoTextureProperties(AVFrame *frame, SDL_PixelFormat format, int access) 513{ 514 AVFrameSideData *pSideData; 515 SDL_PropertiesID props; 516 int width = frame->width; 517 int height = frame->height; 518 SDL_Colorspace colorspace = GetFrameColorspace(frame); 519 520 /* ITU-R BT.2408-6 recommends using an SDR white point of 203 nits, which is more likely for game content */ 521 static const float k_flSDRWhitePoint = 203.0f; 522 float flMaxLuminance = k_flSDRWhitePoint; 523 524 if (frame->hw_frames_ctx) { 525 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx->data); 526 527 width = frames->width; 528 height = frames->height; 529 if (format == SDL_PIXELFORMAT_UNKNOWN) { 530 format = GetTextureFormat(frames->sw_format); 531 } 532 } else { 533 if (format == SDL_PIXELFORMAT_UNKNOWN) { 534 format = GetTextureFormat(frame->format); 535 } 536 } 537 538 props = SDL_CreateProperties(); 539 SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, colorspace); 540 pSideData = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); 541 if (pSideData) { 542 AVMasteringDisplayMetadata *pMasteringDisplayMetadata = (AVMasteringDisplayMetadata *)pSideData->data; 543 flMaxLuminance = (float)pMasteringDisplayMetadata->max_luminance.num / pMasteringDisplayMetadata->max_luminance.den; 544 } else if (SDL_COLORSPACETRANSFER(colorspace) == SDL_TRANSFER_CHARACTERISTICS_PQ) { 545 /* The official definition is 10000, but PQ game content is often mastered for 400 or 1000 nits */ 546 flMaxLuminance = 1000.0f; 547 } 548 if (flMaxLuminance > k_flSDRWhitePoint) { 549 SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_SDR_WHITE_POINT_FLOAT, k_flSDRWhitePoint); 550 SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_HDR_HEADROOM_FLOAT, flMaxLuminance / k_flSDRWhitePoint); 551 } 552 SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, format); 553 SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, access); 554 SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, width); 555 SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, height); 556 557 return props; 558} 559 560static void SDLCALL FreeSwsContextContainer(void *userdata, void *value) 561{ 562 struct SwsContextContainer *sws_container = (struct SwsContextContainer *)value; 563 if (sws_container->context) { 564 sws_freeContext(sws_container->context); 565 } 566 SDL_free(sws_container); 567} 568 569static bool GetTextureForMemoryFrame(AVFrame *frame, SDL_Texture **texture) 570{ 571 int texture_width = 0, texture_height = 0; 572 SDL_PixelFormat texture_format = SDL_PIXELFORMAT_UNKNOWN; 573 SDL_PixelFormat frame_format = GetTextureFormat(frame->format); 574 575 if (*texture) { 576 SDL_PropertiesID props = SDL_GetTextureProperties(*texture); 577 texture_format = (SDL_PixelFormat)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_FORMAT_NUMBER, SDL_PIXELFORMAT_UNKNOWN); 578 texture_width = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0); 579 texture_height = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, 0); 580 } 581 if (!*texture || texture_width != frame->width || texture_height != frame->height || 582 (frame_format != SDL_PIXELFORMAT_UNKNOWN && texture_format != frame_format) || 583 (frame_format == SDL_PIXELFORMAT_UNKNOWN && texture_format != SDL_PIXELFORMAT_ARGB8888)) { 584 if (*texture) { 585 SDL_DestroyTexture(*texture); 586 } 587 588 SDL_PropertiesID props; 589 if (frame_format == SDL_PIXELFORMAT_UNKNOWN) { 590 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING); 591 } else { 592 props = CreateVideoTextureProperties(frame, frame_format, SDL_TEXTUREACCESS_STREAMING); 593 } 594 *texture = SDL_CreateTextureWithProperties(renderer, props); 595 SDL_DestroyProperties(props); 596 if (!*texture) { 597 return false; 598 } 599 600 if (frame_format == SDL_PIXELFORMAT_UNKNOWN || SDL_ISPIXELFORMAT_ALPHA(frame_format)) { 601 SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_BLEND); 602 } else { 603 SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_NONE); 604 } 605 SDL_SetTextureScaleMode(*texture, SDL_SCALEMODE_LINEAR); 606 } 607 608 switch (frame_format) { 609 case SDL_PIXELFORMAT_UNKNOWN: 610 { 611 SDL_PropertiesID props = SDL_GetTextureProperties(*texture); 612 struct SwsContextContainer *sws_container = (struct SwsContextContainer *)SDL_GetPointerProperty(props, SWS_CONTEXT_CONTAINER_PROPERTY, NULL); 613 if (!sws_container) { 614 sws_container = (struct SwsContextContainer *)SDL_calloc(1, sizeof(*sws_container)); 615 if (!sws_container) { 616 return false; 617 } 618 SDL_SetPointerPropertyWithCleanup(props, SWS_CONTEXT_CONTAINER_PROPERTY, sws_container, FreeSwsContextContainer, NULL); 619 } 620 sws_container->context = sws_getCachedContext(sws_container->context, frame->width, frame->height, frame->format, frame->width, frame->height, AV_PIX_FMT_BGRA, SWS_POINT, NULL, NULL, NULL); 621 if (sws_container->context) { 622 uint8_t *pixels[4]; 623 int pitch[4]; 624 if (SDL_LockTexture(*texture, NULL, (void **)&pixels[0], &pitch[0])) { 625 sws_scale(sws_container->context, (const uint8_t *const *)frame->data, frame->linesize, 0, frame->height, pixels, pitch); 626 SDL_UnlockTexture(*texture); 627 } 628 } else { 629 SDL_SetError("Can't initialize the conversion context"); 630 return false; 631 } 632 break; 633 } 634 case SDL_PIXELFORMAT_IYUV: 635 if (frame->linesize[0] > 0 && frame->linesize[1] > 0 && frame->linesize[2] > 0) { 636 SDL_UpdateYUVTexture(*texture, NULL, frame->data[0], frame->linesize[0], 637 frame->data[1], frame->linesize[1], 638 frame->data[2], frame->linesize[2]); 639 } else if (frame->linesize[0] < 0 && frame->linesize[1] < 0 && frame->linesize[2] < 0) { 640 SDL_UpdateYUVTexture(*texture, NULL, frame->data[0] + frame->linesize[0] * (frame->height - 1), -frame->linesize[0], 641 frame->data[1] + frame->linesize[1] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[1], 642 frame->data[2] + frame->linesize[2] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[2]); 643 } 644 break; 645 default: 646 if (frame->linesize[0] < 0) { 647 SDL_UpdateTexture(*texture, NULL, frame->data[0] + frame->linesize[0] * (frame->height - 1), -frame->linesize[0]); 648 } else { 649 SDL_UpdateTexture(*texture, NULL, frame->data[0], frame->linesize[0]); 650 } 651 break; 652 } 653 return true; 654} 655 656#ifdef HAVE_EGL 657 658static bool GetNV12TextureForDRMFrame(AVFrame *frame, SDL_Texture **texture) 659{ 660 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx ? frame->hw_frames_ctx->data : NULL); 661 const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0]; 662 int i, j, image_index; 663 EGLDisplay display = eglGetCurrentDisplay(); 664 SDL_PropertiesID props; 665 GLuint textures[2]; 666 667 if (*texture) { 668 /* Free the previous texture now that we're about to render a new one */ 669 SDL_DestroyTexture(*texture); 670 } else { 671 /* First time set up for NV12 textures */ 672 SDL_SetHint("SDL_RENDER_OPENGL_NV12_RG_SHADER", "1"); 673 } 674 675 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_NV12, SDL_TEXTUREACCESS_STATIC); 676 *texture = SDL_CreateTextureWithProperties(renderer, props); 677 SDL_DestroyProperties(props); 678 if (!*texture) { 679 return false; 680 } 681 SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_NONE); 682 SDL_SetTextureScaleMode(*texture, SDL_SCALEMODE_LINEAR); 683 684 props = SDL_GetTextureProperties(*texture); 685 textures[0] = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER, 0); 686 textures[1] = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_UV_NUMBER, 0); 687 if (!textures[0] || !textures[1]) { 688 SDL_SetError("Couldn't get NV12 OpenGL textures"); 689 return false; 690 } 691 692 /* import the frame into OpenGL */ 693 image_index = 0; 694 for (i = 0; i < desc->nb_layers; ++i) { 695 const AVDRMLayerDescriptor *layer = &desc->layers[i]; 696 for (j = 0; j < layer->nb_planes; ++j) { 697 const AVDRMPlaneDescriptor *plane = &layer->planes[j]; 698 const AVDRMObjectDescriptor *object = &desc->objects[plane->object_index]; 699 700 EGLAttrib attr[32]; 701 size_t k = 0; 702 703 attr[k++] = EGL_LINUX_DRM_FOURCC_EXT; 704 attr[k++] = layer->format; 705 706 attr[k++] = EGL_WIDTH; 707 attr[k++] = (frames ? frames->width : frame->width) / (image_index + 1); /* half size for chroma */ 708 709 attr[k++] = EGL_HEIGHT; 710 attr[k++] = (frames ? frames->height : frame->height) / (image_index + 1); 711 712 attr[k++] = EGL_DMA_BUF_PLANE0_FD_EXT; 713 attr[k++] = object->fd; 714 715 attr[k++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; 716 attr[k++] = plane->offset; 717 718 attr[k++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; 719 attr[k++] = plane->pitch; 720 721 if (has_EGL_EXT_image_dma_buf_import_modifiers) { 722 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; 723 attr[k++] = (object->format_modifier >> 0) & 0xFFFFFFFF; 724 725 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; 726 attr[k++] = (object->format_modifier >> 32) & 0xFFFFFFFF; 727 } 728 729 attr[k++] = EGL_NONE; 730 731 EGLImage image = eglCreateImage(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attr); 732 if (image == EGL_NO_IMAGE) { 733 SDL_Log("Couldn't create image: %d", glGetError()); 734 return false; 735 } 736 737 glActiveTextureARBFunc(GL_TEXTURE0_ARB + image_index); 738 glBindTexture(GL_TEXTURE_2D, textures[image_index]); 739 glEGLImageTargetTexture2DOESFunc(GL_TEXTURE_2D, image); 740 ++image_index; 741 } 742 } 743 744 return true; 745} 746 747static bool GetOESTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture) 748{ 749 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx ? frame->hw_frames_ctx->data : NULL); 750 const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0]; 751 int i, j, k, image_index; 752 EGLDisplay display = eglGetCurrentDisplay(); 753 SDL_PropertiesID props; 754 GLuint textureID; 755 EGLAttrib attr[64]; 756 SDL_Colorspace colorspace; 757 758 if (*texture) { 759 /* Free the previous texture now that we're about to render a new one */ 760 SDL_DestroyTexture(*texture); 761 } 762 763 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_EXTERNAL_OES, SDL_TEXTUREACCESS_STATIC); 764 *texture = SDL_CreateTextureWithProperties(renderer, props); 765 SDL_DestroyProperties(props); 766 if (!*texture) { 767 return false; 768 } 769 SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_NONE); 770 SDL_SetTextureScaleMode(*texture, SDL_SCALEMODE_LINEAR); 771 772 props = SDL_GetTextureProperties(*texture); 773 textureID = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER, 0); 774 if (!textureID) { 775 SDL_SetError("Couldn't get OpenGL texture"); 776 return false; 777 } 778 colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_COLORSPACE_NUMBER, SDL_COLORSPACE_UNKNOWN); 779 780 /* import the frame into OpenGL */ 781 k = 0; 782 attr[k++] = EGL_LINUX_DRM_FOURCC_EXT; 783 attr[k++] = desc->layers[0].format; 784 attr[k++] = EGL_WIDTH; 785 attr[k++] = frames ? frames->width : frame->width; 786 attr[k++] = EGL_HEIGHT; 787 attr[k++] = frames ? frames->height : frame->height; 788 image_index = 0; 789 for (i = 0; i < desc->nb_layers; ++i) { 790 const AVDRMLayerDescriptor *layer = &desc->layers[i]; 791 for (j = 0; j < layer->nb_planes; ++j) { 792 const AVDRMPlaneDescriptor *plane = &layer->planes[j]; 793 const AVDRMObjectDescriptor *object = &desc->objects[plane->object_index]; 794 795 switch (image_index) { 796 case 0: 797 attr[k++] = EGL_DMA_BUF_PLANE0_FD_EXT; 798 attr[k++] = object->fd; 799 attr[k++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; 800 attr[k++] = plane->offset; 801 attr[k++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; 802 attr[k++] = plane->pitch; 803 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 804 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; 805 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 806 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; 807 attr[k++] = (object->format_modifier >> 32); 808 } 809 break; 810 case 1: 811 attr[k++] = EGL_DMA_BUF_PLANE1_FD_EXT; 812 attr[k++] = object->fd; 813 attr[k++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT; 814 attr[k++] = plane->offset; 815 attr[k++] = EGL_DMA_BUF_PLANE1_PITCH_EXT; 816 attr[k++] = plane->pitch; 817 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 818 attr[k++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT; 819 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 820 attr[k++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT; 821 attr[k++] = (object->format_modifier >> 32); 822 } 823 break; 824 case 2: 825 attr[k++] = EGL_DMA_BUF_PLANE2_FD_EXT; 826 attr[k++] = object->fd; 827 attr[k++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT; 828 attr[k++] = plane->offset; 829 attr[k++] = EGL_DMA_BUF_PLANE2_PITCH_EXT; 830 attr[k++] = plane->pitch; 831 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 832 attr[k++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT; 833 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 834 attr[k++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT; 835 attr[k++] = (object->format_modifier >> 32); 836 } 837 break; 838 case 3: 839 attr[k++] = EGL_DMA_BUF_PLANE3_FD_EXT; 840 attr[k++] = object->fd; 841 attr[k++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT; 842 attr[k++] = plane->offset; 843 attr[k++] = EGL_DMA_BUF_PLANE3_PITCH_EXT; 844 attr[k++] = plane->pitch; 845 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 846 attr[k++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT; 847 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 848 attr[k++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT; 849 attr[k++] = (object->format_modifier >> 32); 850 } 851 break; 852 853 default: 854 break; 855 } 856 ++image_index; 857 } 858 } 859 860 switch (SDL_COLORSPACEPRIMARIES(colorspace)) { 861 case SDL_COLOR_PRIMARIES_BT601: 862 case SDL_COLOR_PRIMARIES_SMPTE240: 863 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 864 attr[k++] = EGL_ITU_REC601_EXT; 865 break; 866 case SDL_COLOR_PRIMARIES_BT709: 867 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 868 attr[k++] = EGL_ITU_REC709_EXT; 869 break; 870 case SDL_COLOR_PRIMARIES_BT2020: 871 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 872 attr[k++] = EGL_ITU_REC2020_EXT; 873 break; 874 default: 875 break; 876 } 877 878 switch (SDL_COLORSPACERANGE(colorspace)) { 879 case SDL_COLOR_RANGE_FULL: 880 attr[k++] = EGL_SAMPLE_RANGE_HINT_EXT; 881 attr[k++] = EGL_YUV_FULL_RANGE_EXT; 882 break; 883 case SDL_COLOR_RANGE_LIMITED: 884 default: 885 attr[k++] = EGL_SAMPLE_RANGE_HINT_EXT; 886 attr[k++] = EGL_YUV_NARROW_RANGE_EXT; 887 break; 888 } 889 890 switch (SDL_COLORSPACECHROMA(colorspace)) { 891 case SDL_CHROMA_LOCATION_LEFT: 892 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 893 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 894 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 895 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 896 break; 897 case SDL_CHROMA_LOCATION_CENTER: 898 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 899 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 900 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 901 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 902 break; 903 case SDL_CHROMA_LOCATION_TOPLEFT: 904 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 905 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 906 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 907 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 908 break; 909 default: 910 break; 911 } 912 913 SDL_assert(k < SDL_arraysize(attr)); 914 attr[k++] = EGL_NONE; 915 916 EGLImage image = eglCreateImage(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attr); 917 if (image == EGL_NO_IMAGE) { 918 SDL_Log("Couldn't create image: %d", glGetError()); 919 return false; 920 } 921 922 glActiveTextureARBFunc(GL_TEXTURE0_ARB); 923 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureID); 924 glEGLImageTargetTexture2DOESFunc(GL_TEXTURE_EXTERNAL_OES, image); 925 return true; 926} 927#endif /* HAVE_EGL */ 928 929static bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture) 930{ 931#ifdef HAVE_EGL 932 const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0]; 933 934 if (desc->nb_layers == 2 && 935 desc->layers[0].format == DRM_FORMAT_R8 && 936 desc->layers[1].format == DRM_FORMAT_GR88) { 937 return GetNV12TextureForDRMFrame(frame, texture); 938 } else { 939 return GetOESTextureForDRMFrame(frame, texture); 940 } 941#else 942 return false; 943#endif 944} 945 946static bool GetTextureForVAAPIFrame(AVFrame *frame, SDL_Texture **texture) 947{ 948 AVFrame *drm_frame; 949 bool result = false; 950 951 drm_frame = av_frame_alloc(); 952 if (drm_frame) { 953 drm_frame->format = AV_PIX_FMT_DRM_PRIME; 954 if (av_hwframe_map(drm_frame, frame, 0) == 0) { 955 result = GetTextureForDRMFrame(drm_frame, texture); 956 } else { 957 SDL_SetError("Couldn't map hardware frame"); 958 } 959 av_frame_free(&drm_frame); 960 } 961 return result; 962} 963 964static bool GetTextureForD3D11Frame(AVFrame *frame, SDL_Texture **texture) 965{ 966#ifdef SDL_PLATFORM_WIN32 967 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx->data); 968 int texture_width = 0, texture_height = 0; 969 ID3D11Texture2D *pTexture = (ID3D11Texture2D *)frame->data[0]; 970 UINT iSliceIndex = (UINT)(uintptr_t)frame->data[1]; 971 972 if (*texture) { 973 SDL_PropertiesID props = SDL_GetTextureProperties(*texture); 974 texture_width = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0); 975 texture_height = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, 0); 976 } 977 if (!*texture || texture_width != frames->width || texture_height != frames->height) { 978 if (*texture) { 979 SDL_DestroyTexture(*texture); 980 } 981 982 SDL_PropertiesID props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 983 *texture = SDL_CreateTextureWithProperties(renderer, props); 984 SDL_DestroyProperties(props); 985 if (!*texture) { 986 return false; 987 } 988 } 989 990 ID3D11Resource *dx11_resource = SDL_GetPointerProperty(SDL_GetTextureProperties(*texture), SDL_PROP_TEXTURE_D3D11_TEXTURE_POINTER, NULL); 991 if (!dx11_resource) { 992 SDL_SetError("Couldn't get texture ID3D11Resource interface"); 993 return false; 994 } 995 ID3D11DeviceContext_CopySubresourceRegion(d3d11_context, dx11_resource, 0, 0, 0, 0, (ID3D11Resource *)pTexture, iSliceIndex, NULL); 996 997 return true; 998#else 999 return false; 1000#endif 1001} 1002 1003static bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **texture) 1004{ 1005#ifdef SDL_PLATFORM_APPLE 1006 CVPixelBufferRef pPixelBuffer = (CVPixelBufferRef)frame->data[3]; 1007 SDL_PropertiesID props; 1008 1009 if (*texture) { 1010 /* Free the previous texture now that we're about to render a new one */ 1011 /* FIXME: We can actually keep a cache of textures that map to pixel buffers */ 1012 SDL_DestroyTexture(*texture); 1013 } 1014 1015 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 1016 SDL_SetPointerProperty(props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, pPixelBuffer); 1017 *texture = SDL_CreateTextureWithProperties(renderer, props); 1018 SDL_DestroyProperties(props); 1019 if (!*texture) { 1020 return false; 1021 } 1022 1023 return true; 1024#else 1025 return false; 1026#endif 1027} 1028 1029static bool GetTextureForVulkanFrame(AVFrame *frame, SDL_Texture **texture) 1030{ 1031 SDL_PropertiesID props; 1032 1033 if (*texture) { 1034 SDL_DestroyTexture(*texture); 1035 } 1036 1037 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 1038 *texture = CreateVulkanVideoTexture(vulkan_context, frame, renderer, props); 1039 SDL_DestroyProperties(props); 1040 if (!*texture) { 1041 return false; 1042 } 1043 return true; 1044} 1045 1046static bool GetTextureForFrame(AVFrame *frame, SDL_Texture **texture) 1047{ 1048 switch (frame->format) { 1049 case AV_PIX_FMT_VAAPI: 1050 return GetTextureForVAAPIFrame(frame, texture); 1051 case AV_PIX_FMT_DRM_PRIME: 1052 return GetTextureForDRMFrame(frame, texture); 1053 case AV_PIX_FMT_D3D11: 1054 return GetTextureForD3D11Frame(frame, texture); 1055 case AV_PIX_FMT_VIDEOTOOLBOX: 1056 return GetTextureForVideoToolboxFrame(frame, texture); 1057 case AV_PIX_FMT_VULKAN: 1058 return GetTextureForVulkanFrame(frame, texture); 1059 default: 1060 return GetTextureForMemoryFrame(frame, texture); 1061 } 1062} 1063 1064static int BeginFrameRendering(AVFrame *frame) 1065{ 1066 if (frame->format == AV_PIX_FMT_VULKAN) { 1067 return BeginVulkanFrameRendering(vulkan_context, frame, renderer); 1068 } 1069 return 0; 1070} 1071 1072static int FinishFrameRendering(AVFrame *frame) 1073{ 1074 if (frame->format == AV_PIX_FMT_VULKAN) { 1075 return FinishVulkanFrameRendering(vulkan_context, frame, renderer); 1076 } 1077 return 0; 1078} 1079 1080static void DisplayVideoTexture(AVFrame *frame) 1081{ 1082 /* Update the video texture */ 1083 if (!GetTextureForFrame(frame, &video_texture)) { 1084 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't get texture for frame: %s", SDL_GetError()); 1085 return; 1086 } 1087 1088 SDL_FRect src; 1089 src.x = 0.0f; 1090 src.y = 0.0f; 1091 src.w = (float)frame->width; 1092 src.h = (float)frame->height; 1093 if (frame->linesize[0] < 0) { 1094 SDL_RenderTextureRotated(renderer, video_texture, &src, NULL, 0.0, NULL, SDL_FLIP_VERTICAL); 1095 } else { 1096 SDL_RenderTexture(renderer, video_texture, &src, NULL); 1097 } 1098} 1099 1100static void DisplayVideoFrame(AVFrame *frame) 1101{ 1102 DisplayVideoTexture(frame); 1103} 1104 1105static void HandleVideoFrame(AVFrame *frame, double pts) 1106{ 1107 /* Quick and dirty PTS handling */ 1108 if (!video_start) { 1109 video_start = SDL_GetTicks(); 1110 } 1111 double now = (double)(SDL_GetTicks() - video_start) / 1000.0; 1112 if (now < pts) { 1113 SDL_DelayPrecise((Uint64)((pts - now) * SDL_NS_PER_SECOND)); 1114 } 1115 1116 if (BeginFrameRendering(frame) < 0) { 1117 return; 1118 } 1119 1120 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 1121 SDL_RenderClear(renderer); 1122 1123 DisplayVideoFrame(frame); 1124 1125 /* Render any bouncing balls */ 1126 MoveSprite(); 1127 1128 SDL_RenderPresent(renderer); 1129 1130 FinishFrameRendering(frame); 1131} 1132 1133static AVCodecContext *OpenAudioStream(AVFormatContext *ic, int stream, const AVCodec *codec) 1134{ 1135 AVStream *st = ic->streams[stream]; 1136 AVCodecParameters *codecpar = st->codecpar; 1137 AVCodecContext *context; 1138 int result; 1139 1140 SDL_Log("Audio stream: %s %d channels, %d Hz", avcodec_get_name(codec->id), codecpar->ch_layout.nb_channels, codecpar->sample_rate); 1141 1142 context = avcodec_alloc_context3(NULL); 1143 if (!context) { 1144 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_alloc_context3 failed"); 1145 return NULL; 1146 } 1147 1148 result = avcodec_parameters_to_context(context, ic->streams[stream]->codecpar); 1149 if (result < 0) { 1150 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_parameters_to_context failed: %s", av_err2str(result)); 1151 avcodec_free_context(&context); 1152 return NULL; 1153 } 1154 context->pkt_timebase = ic->streams[stream]->time_base; 1155 1156 result = avcodec_open2(context, codec, NULL); 1157 if (result < 0) { 1158 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open codec %s: %s", avcodec_get_name(context->codec_id), av_err2str(result)); 1159 avcodec_free_context(&context); 1160 return NULL; 1161 } 1162 1163 SDL_AudioSpec spec = { SDL_AUDIO_F32, codecpar->ch_layout.nb_channels, codecpar->sample_rate }; 1164 audio = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); 1165 if (audio) { 1166 SDL_ResumeAudioStreamDevice(audio); 1167 } else { 1168 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s", SDL_GetError()); 1169 } 1170 return context; 1171} 1172 1173static SDL_AudioFormat GetAudioFormat(int format) 1174{ 1175 switch (format) { 1176 case AV_SAMPLE_FMT_U8: 1177 case AV_SAMPLE_FMT_U8P: 1178 return SDL_AUDIO_U8; 1179 case AV_SAMPLE_FMT_S16: 1180 case AV_SAMPLE_FMT_S16P: 1181 return SDL_AUDIO_S16; 1182 case AV_SAMPLE_FMT_S32: 1183 case AV_SAMPLE_FMT_S32P: 1184 return SDL_AUDIO_S32; 1185 case AV_SAMPLE_FMT_FLT: 1186 case AV_SAMPLE_FMT_FLTP: 1187 return SDL_AUDIO_F32; 1188 default: 1189 /* Unsupported */ 1190 return SDL_AUDIO_UNKNOWN; 1191 } 1192} 1193 1194static bool IsPlanarAudioFormat(int format) 1195{ 1196 switch (format) { 1197 case AV_SAMPLE_FMT_U8P: 1198 case AV_SAMPLE_FMT_S16P: 1199 case AV_SAMPLE_FMT_S32P: 1200 case AV_SAMPLE_FMT_FLTP: 1201 case AV_SAMPLE_FMT_DBLP: 1202 case AV_SAMPLE_FMT_S64P: 1203 return true; 1204 default: 1205 return false; 1206 } 1207} 1208 1209static void InterleaveAudio(AVFrame *frame, const SDL_AudioSpec *spec) 1210{ 1211 int c, n; 1212 int samplesize = SDL_AUDIO_BYTESIZE(spec->format); 1213 int framesize = SDL_AUDIO_FRAMESIZE(*spec); 1214 Uint8 *data = (Uint8 *)SDL_malloc(frame->nb_samples * framesize); 1215 if (!data) { 1216 return; 1217 } 1218 1219 /* This could be optimized with SIMD and not allocating memory each time */ 1220 for (c = 0; c < spec->channels; ++c) { 1221 const Uint8 *src = frame->data[c]; 1222 Uint8 *dst = data + c * samplesize; 1223 for (n = frame->nb_samples; n--;) { 1224 SDL_memcpy(dst, src, samplesize); 1225 src += samplesize; 1226 dst += framesize; 1227 } 1228 } 1229 SDL_PutAudioStreamData(audio, data, frame->nb_samples * framesize); 1230 SDL_free(data); 1231} 1232 1233static void HandleAudioFrame(AVFrame *frame) 1234{ 1235 if (audio) { 1236 SDL_AudioSpec spec = { GetAudioFormat(frame->format), frame->ch_layout.nb_channels, frame->sample_rate }; 1237 SDL_SetAudioStreamFormat(audio, &spec, NULL); 1238 1239 if (frame->ch_layout.nb_channels > 1 && IsPlanarAudioFormat(frame->format)) { 1240 InterleaveAudio(frame, &spec); 1241 } else { 1242 SDL_PutAudioStreamData(audio, frame->data[0], frame->nb_samples * SDL_AUDIO_FRAMESIZE(spec)); 1243 } 1244 } 1245} 1246 1247static void av_log_callback(void *avcl, int level, const char *fmt, va_list vl) 1248{ 1249 const char *pszCategory = NULL; 1250 char *message; 1251 1252 switch (level) { 1253 case AV_LOG_PANIC: 1254 case AV_LOG_FATAL: 1255 pszCategory = "fatal error"; 1256 break; 1257 case AV_LOG_ERROR: 1258 pszCategory = "error"; 1259 break; 1260 case AV_LOG_WARNING: 1261 pszCategory = "warning"; 1262 break; 1263 case AV_LOG_INFO: 1264 pszCategory = "info"; 1265 break; 1266 case AV_LOG_VERBOSE: 1267 pszCategory = "verbose"; 1268 break; 1269 case AV_LOG_DEBUG: 1270 if (verbose) { 1271 pszCategory = "debug"; 1272 } 1273 break; 1274 } 1275 1276 if (!pszCategory) { 1277 // We don't care about this message 1278 return; 1279 } 1280 1281 SDL_vasprintf(&message, fmt, vl); 1282 SDL_Log("ffmpeg %s: %s", pszCategory, message); 1283 SDL_free(message); 1284} 1285 1286static void print_usage(SDLTest_CommonState *state, const char *argv0) 1287{ 1288 static const char *options[] = { "[--verbose]", "[--sprites N]", "[--audio-codec codec]", "[--video-codec codec]", "[--software]", "video_file", NULL }; 1289 SDLTest_CommonLogUsage(state, argv0, options); 1290} 1291 1292int main(int argc, char *argv[]) 1293{ 1294 const char *file = NULL; 1295 AVFormatContext *ic = NULL; 1296 int audio_stream = -1; 1297 int video_stream = -1; 1298 const char *audio_codec_name = NULL; 1299 const char *video_codec_name = NULL; 1300 const AVCodec *audio_codec = NULL; 1301 const AVCodec *video_codec = NULL; 1302 AVCodecContext *audio_context = NULL; 1303 AVCodecContext *video_context = NULL; 1304 AVPacket *pkt = NULL; 1305 AVFrame *frame = NULL; 1306 double first_pts = -1.0; 1307 int i; 1308 int result; 1309 int return_code = -1; 1310 SDL_WindowFlags window_flags; 1311 bool flushing = false; 1312 bool decoded = false; 1313 bool done = false; 1314 SDLTest_CommonState *state; 1315 1316 /* Initialize test framework */ 1317 state = SDLTest_CommonCreateState(argv, 0); 1318 1319 /* Enable standard application logging */ 1320 SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 1321 1322 /* Log ffmpeg messages */ 1323 av_log_set_callback(av_log_callback); 1324 1325 /* Parse commandline */ 1326 for (i = 1; i < argc;) { 1327 int consumed; 1328 1329 consumed = SDLTest_CommonArg(state, i); 1330 if (!consumed) { 1331 if (SDL_strcmp(argv[i], "--verbose") == 0) { 1332 verbose = true; 1333 consumed = 1; 1334 } else if (SDL_strcmp(argv[i], "--sprites") == 0 && argv[i + 1]) { 1335 num_sprites = SDL_atoi(argv[i + 1]); 1336 consumed = 2; 1337 } else if (SDL_strcmp(argv[i], "--audio-codec") == 0 && argv[i + 1]) { 1338 audio_codec_name = argv[i + 1]; 1339 consumed = 2; 1340 } else if (SDL_strcmp(argv[i], "--video-codec") == 0 && argv[i + 1]) { 1341 video_codec_name = argv[i + 1]; 1342 consumed = 2; 1343 } else if (SDL_strcmp(argv[i], "--software") == 0) { 1344 software_only = true; 1345 consumed = 1; 1346 } else if (!file) { 1347 /* We'll try to open this as a media file */ 1348 file = argv[i]; 1349 consumed = 1; 1350 } 1351 } 1352 if (consumed <= 0) { 1353 print_usage(state, argv[0]); 1354 return_code = 1; 1355 goto quit; 1356 } 1357 1358 i += consumed; 1359 } 1360 1361 if (!file) { 1362 print_usage(state, argv[0]); 1363 return_code = 1; 1364 goto quit; 1365 } 1366 1367 if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) { 1368 return_code = 2; 1369 goto quit; 1370 } 1371 1372 window_flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; 1373#ifdef SDL_PLATFORM_APPLE 1374 window_flags |= SDL_WINDOW_METAL; 1375#elif !defined(SDL_PLATFORM_WIN32) 1376 window_flags |= SDL_WINDOW_OPENGL; 1377#endif 1378 if (SDL_GetHint(SDL_HINT_RENDER_DRIVER) != NULL) { 1379 CreateWindowAndRenderer(window_flags, SDL_GetHint(SDL_HINT_RENDER_DRIVER)); 1380 } 1381#ifdef HAVE_EGL 1382 /* Try to create an EGL compatible window for DRM hardware frame support */ 1383 if (!window) { 1384 CreateWindowAndRenderer(window_flags, "opengles2"); 1385 } 1386#endif 1387#ifdef SDL_PLATFORM_APPLE 1388 if (!window) { 1389 CreateWindowAndRenderer(window_flags, "metal"); 1390 } 1391#endif 1392#ifdef SDL_PLATFORM_WIN32 1393 if (!window) { 1394 CreateWindowAndRenderer(window_flags, "direct3d11"); 1395 } 1396#endif 1397 if (!window) { 1398 if (!CreateWindowAndRenderer(window_flags, NULL)) { 1399 return_code = 2; 1400 goto quit; 1401 } 1402 } 1403 1404 if (!SDL_SetWindowTitle(window, file)) { 1405 SDL_Log("SDL_SetWindowTitle: %s", SDL_GetError()); 1406 } 1407 1408 /* Open the media file */ 1409 result = avformat_open_input(&ic, file, NULL, NULL); 1410 if (result < 0) { 1411 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open %s: %d", argv[1], result); 1412 return_code = 4; 1413 goto quit; 1414 } 1415 video_stream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, 0); 1416 if (video_stream >= 0) { 1417 if (video_codec_name) { 1418 video_codec = avcodec_find_decoder_by_name(video_codec_name); 1419 if (!video_codec) { 1420 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find codec '%s'", video_codec_name); 1421 return_code = 4; 1422 goto quit; 1423 } 1424 } 1425 video_context = OpenVideoStream(ic, video_stream, video_codec); 1426 if (!video_context) { 1427 return_code = 4; 1428 goto quit; 1429 } 1430 } 1431 audio_stream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, video_stream, &audio_codec, 0); 1432 if (audio_stream >= 0) { 1433 if (audio_codec_name) { 1434 audio_codec = avcodec_find_decoder_by_name(audio_codec_name); 1435 if (!audio_codec) { 1436 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find codec '%s'", audio_codec_name); 1437 return_code = 4; 1438 goto quit; 1439 } 1440 } 1441 audio_context = OpenAudioStream(ic, audio_stream, audio_codec); 1442 if (!audio_context) { 1443 return_code = 4; 1444 goto quit; 1445 } 1446 } 1447 pkt = av_packet_alloc(); 1448 if (!pkt) { 1449 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "av_packet_alloc failed"); 1450 return_code = 4; 1451 goto quit; 1452 } 1453 frame = av_frame_alloc(); 1454 if (!frame) { 1455 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "av_frame_alloc failed"); 1456 return_code = 4; 1457 goto quit; 1458 } 1459 1460 /* Create the sprite */ 1461 sprite = CreateTexture(renderer, icon_png, icon_png_len, &sprite_w, &sprite_h); 1462 1463 if (!sprite) { 1464 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture (%s)", SDL_GetError()); 1465 return_code = 3; 1466 goto quit; 1467 } 1468 1469 /* Allocate memory for the sprite info */ 1470 positions = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*positions)); 1471 velocities = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*velocities)); 1472 if (!positions || !velocities) { 1473 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!"); 1474 return_code = 3; 1475 goto quit; 1476 } 1477 1478 /* Position sprites and set their velocities */ 1479 SDL_Rect viewport; 1480 SDL_GetRenderViewport(renderer, &viewport); 1481 for (i = 0; i < num_sprites; ++i) { 1482 positions[i].x = (float)SDL_rand(viewport.w - sprite_w); 1483 positions[i].y = (float)SDL_rand(viewport.h - sprite_h); 1484 positions[i].w = (float)sprite_w; 1485 positions[i].h = (float)sprite_h; 1486 velocities[i].x = 0.0f; 1487 velocities[i].y = 0.0f; 1488 while (velocities[i].x == 0.f || velocities[i].y == 0.f) { 1489 velocities[i].x = (float)(SDL_rand(2 + 1) - 1); 1490 velocities[i].y = (float)(SDL_rand(2 + 1) - 1); 1491 } 1492 } 1493 1494 /* We're ready to go! */ 1495 SDL_ShowWindow(window); 1496 1497 /* Main render loop */ 1498 while (!done) { 1499 SDL_Event event; 1500 1501 /* Check for events */ 1502 while (SDL_PollEvent(&event)) { 1503 if (event.type == SDL_EVENT_QUIT || 1504 (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE)) { 1505 done = true; 1506 } 1507 } 1508 1509 if (!flushing) { 1510 result = av_read_frame(ic, pkt); 1511 if (result < 0) { 1512 SDL_Log("End of stream, finishing decode"); 1513 if (audio_context) { 1514 avcodec_flush_buffers(audio_context); 1515 } 1516 if (video_context) { 1517 avcodec_flush_buffers(video_context); 1518 } 1519 flushing = true; 1520 } else { 1521 if (pkt->stream_index == audio_stream) { 1522 result = avcodec_send_packet(audio_context, pkt); 1523 if (result < 0) { 1524 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_send_packet(audio_context) failed: %s", av_err2str(result)); 1525 } 1526 } else if (pkt->stream_index == video_stream) { 1527 result = avcodec_send_packet(video_context, pkt); 1528 if (result < 0) { 1529 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_send_packet(video_context) failed: %s", av_err2str(result)); 1530 } 1531 } 1532 av_packet_unref(pkt); 1533 } 1534 } 1535 1536 decoded = false; 1537 if (audio_context) { 1538 while (avcodec_receive_frame(audio_context, frame) >= 0) { 1539 HandleAudioFrame(frame); 1540 decoded = true; 1541 } 1542 if (flushing) { 1543 /* Let SDL know we're done sending audio */ 1544 SDL_FlushAudioStream(audio); 1545 } 1546 } 1547 if (video_context) { 1548 while (avcodec_receive_frame(video_context, frame) >= 0) { 1549 double pts = ((double)frame->pts * video_context->pkt_timebase.num) / video_context->pkt_timebase.den; 1550 if (first_pts < 0.0) { 1551 first_pts = pts; 1552 } 1553 pts -= first_pts; 1554 1555 HandleVideoFrame(frame, pts); 1556 decoded = true; 1557 } 1558 } else { 1559 /* Update video rendering */ 1560 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 1561 SDL_RenderClear(renderer); 1562 MoveSprite(); 1563 SDL_RenderPresent(renderer); 1564 } 1565 1566 if (flushing && !decoded) { 1567 if (SDL_GetAudioStreamQueued(audio) > 0) { 1568 /* Wait a little bit for the audio to finish */ 1569 SDL_Delay(10); 1570 } else { 1571 done = true; 1572 } 1573 } 1574 } 1575 return_code = 0; 1576quit: 1577#ifdef SDL_PLATFORM_WIN32 1578 if (d3d11_context) { 1579 ID3D11DeviceContext_Release(d3d11_context); 1580 d3d11_context = NULL; 1581 } 1582 if (d3d11_device) { 1583 ID3D11Device_Release(d3d11_device); 1584 d3d11_device = NULL; 1585 } 1586#endif 1587 SDL_free(positions); 1588 SDL_free(velocities); 1589 av_frame_free(&frame); 1590 av_packet_free(&pkt); 1591 avcodec_free_context(&audio_context); 1592 avcodec_free_context(&video_context); 1593 avformat_close_input(&ic); 1594 SDL_DestroyRenderer(renderer); 1595 if (vulkan_context) { 1596 DestroyVulkanVideoContext(vulkan_context); 1597 } 1598 SDL_DestroyWindow(window); 1599 SDL_Quit(); 1600 SDLTest_CommonDestroyState(state); 1601 return return_code; 1602} 1603
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.