Atlas - testffmpeg.c

Home / ext / SDL / test Lines: 1 | Size: 56882 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2026 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 eglDestroyImage(display, image); 741 ++image_index; 742 } 743 } 744 745 return true; 746} 747 748static bool GetOESTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture) 749{ 750 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx ? frame->hw_frames_ctx->data : NULL); 751 const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0]; 752 int i, j, k, image_index; 753 EGLDisplay display = eglGetCurrentDisplay(); 754 SDL_PropertiesID props; 755 GLuint textureID; 756 EGLAttrib attr[64]; 757 SDL_Colorspace colorspace; 758 759 if (*texture) { 760 /* Free the previous texture now that we're about to render a new one */ 761 SDL_DestroyTexture(*texture); 762 } 763 764 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_EXTERNAL_OES, SDL_TEXTUREACCESS_STATIC); 765 *texture = SDL_CreateTextureWithProperties(renderer, props); 766 SDL_DestroyProperties(props); 767 if (!*texture) { 768 return false; 769 } 770 SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_NONE); 771 SDL_SetTextureScaleMode(*texture, SDL_SCALEMODE_LINEAR); 772 773 props = SDL_GetTextureProperties(*texture); 774 textureID = (GLuint)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER, 0); 775 if (!textureID) { 776 SDL_SetError("Couldn't get OpenGL texture"); 777 return false; 778 } 779 colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_COLORSPACE_NUMBER, SDL_COLORSPACE_UNKNOWN); 780 781 /* import the frame into OpenGL */ 782 k = 0; 783 attr[k++] = EGL_LINUX_DRM_FOURCC_EXT; 784 attr[k++] = desc->layers[0].format; 785 attr[k++] = EGL_WIDTH; 786 attr[k++] = frames ? frames->width : frame->width; 787 attr[k++] = EGL_HEIGHT; 788 attr[k++] = frames ? frames->height : frame->height; 789 image_index = 0; 790 for (i = 0; i < desc->nb_layers; ++i) { 791 const AVDRMLayerDescriptor *layer = &desc->layers[i]; 792 for (j = 0; j < layer->nb_planes; ++j) { 793 const AVDRMPlaneDescriptor *plane = &layer->planes[j]; 794 const AVDRMObjectDescriptor *object = &desc->objects[plane->object_index]; 795 796 switch (image_index) { 797 case 0: 798 attr[k++] = EGL_DMA_BUF_PLANE0_FD_EXT; 799 attr[k++] = object->fd; 800 attr[k++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; 801 attr[k++] = plane->offset; 802 attr[k++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; 803 attr[k++] = plane->pitch; 804 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 805 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; 806 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 807 attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; 808 attr[k++] = (object->format_modifier >> 32); 809 } 810 break; 811 case 1: 812 attr[k++] = EGL_DMA_BUF_PLANE1_FD_EXT; 813 attr[k++] = object->fd; 814 attr[k++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT; 815 attr[k++] = plane->offset; 816 attr[k++] = EGL_DMA_BUF_PLANE1_PITCH_EXT; 817 attr[k++] = plane->pitch; 818 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 819 attr[k++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT; 820 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 821 attr[k++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT; 822 attr[k++] = (object->format_modifier >> 32); 823 } 824 break; 825 case 2: 826 attr[k++] = EGL_DMA_BUF_PLANE2_FD_EXT; 827 attr[k++] = object->fd; 828 attr[k++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT; 829 attr[k++] = plane->offset; 830 attr[k++] = EGL_DMA_BUF_PLANE2_PITCH_EXT; 831 attr[k++] = plane->pitch; 832 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 833 attr[k++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT; 834 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 835 attr[k++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT; 836 attr[k++] = (object->format_modifier >> 32); 837 } 838 break; 839 case 3: 840 attr[k++] = EGL_DMA_BUF_PLANE3_FD_EXT; 841 attr[k++] = object->fd; 842 attr[k++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT; 843 attr[k++] = plane->offset; 844 attr[k++] = EGL_DMA_BUF_PLANE3_PITCH_EXT; 845 attr[k++] = plane->pitch; 846 if (has_EGL_EXT_image_dma_buf_import_modifiers && object->format_modifier != DRM_FORMAT_MOD_INVALID) { 847 attr[k++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT; 848 attr[k++] = (object->format_modifier & 0xFFFFFFFF); 849 attr[k++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT; 850 attr[k++] = (object->format_modifier >> 32); 851 } 852 break; 853 854 default: 855 break; 856 } 857 ++image_index; 858 } 859 } 860 861 switch (SDL_COLORSPACEPRIMARIES(colorspace)) { 862 case SDL_COLOR_PRIMARIES_BT601: 863 case SDL_COLOR_PRIMARIES_SMPTE240: 864 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 865 attr[k++] = EGL_ITU_REC601_EXT; 866 break; 867 case SDL_COLOR_PRIMARIES_BT709: 868 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 869 attr[k++] = EGL_ITU_REC709_EXT; 870 break; 871 case SDL_COLOR_PRIMARIES_BT2020: 872 attr[k++] = EGL_YUV_COLOR_SPACE_HINT_EXT; 873 attr[k++] = EGL_ITU_REC2020_EXT; 874 break; 875 default: 876 break; 877 } 878 879 switch (SDL_COLORSPACERANGE(colorspace)) { 880 case SDL_COLOR_RANGE_FULL: 881 attr[k++] = EGL_SAMPLE_RANGE_HINT_EXT; 882 attr[k++] = EGL_YUV_FULL_RANGE_EXT; 883 break; 884 case SDL_COLOR_RANGE_LIMITED: 885 default: 886 attr[k++] = EGL_SAMPLE_RANGE_HINT_EXT; 887 attr[k++] = EGL_YUV_NARROW_RANGE_EXT; 888 break; 889 } 890 891 switch (SDL_COLORSPACECHROMA(colorspace)) { 892 case SDL_CHROMA_LOCATION_LEFT: 893 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 894 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 895 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 896 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 897 break; 898 case SDL_CHROMA_LOCATION_CENTER: 899 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 900 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 901 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 902 attr[k++] = EGL_YUV_CHROMA_SITING_0_5_EXT; 903 break; 904 case SDL_CHROMA_LOCATION_TOPLEFT: 905 attr[k++] = EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT; 906 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 907 attr[k++] = EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT; 908 attr[k++] = EGL_YUV_CHROMA_SITING_0_EXT; 909 break; 910 default: 911 break; 912 } 913 914 SDL_assert(k < SDL_arraysize(attr)); 915 attr[k++] = EGL_NONE; 916 917 EGLImage image = eglCreateImage(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attr); 918 if (image == EGL_NO_IMAGE) { 919 SDL_Log("Couldn't create image: %d", glGetError()); 920 return false; 921 } 922 923 glActiveTextureARBFunc(GL_TEXTURE0_ARB); 924 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureID); 925 glEGLImageTargetTexture2DOESFunc(GL_TEXTURE_EXTERNAL_OES, image); 926 eglDestroyImage(display, image); 927 return true; 928} 929#endif /* HAVE_EGL */ 930 931static bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture) 932{ 933#ifdef HAVE_EGL 934 const AVDRMFrameDescriptor *desc = (const AVDRMFrameDescriptor *)frame->data[0]; 935 936 if (desc->nb_layers == 2 && 937 desc->layers[0].format == DRM_FORMAT_R8 && 938 desc->layers[1].format == DRM_FORMAT_GR88) { 939 return GetNV12TextureForDRMFrame(frame, texture); 940 } else { 941 return GetOESTextureForDRMFrame(frame, texture); 942 } 943#else 944 return false; 945#endif 946} 947 948static bool GetTextureForVAAPIFrame(AVFrame *frame, SDL_Texture **texture) 949{ 950 AVFrame *drm_frame; 951 bool result = false; 952 953 drm_frame = av_frame_alloc(); 954 if (drm_frame) { 955 drm_frame->format = AV_PIX_FMT_DRM_PRIME; 956 if (av_hwframe_map(drm_frame, frame, 0) == 0) { 957 result = GetTextureForDRMFrame(drm_frame, texture); 958 } else { 959 SDL_SetError("Couldn't map hardware frame"); 960 } 961 av_frame_free(&drm_frame); 962 } 963 return result; 964} 965 966static bool GetTextureForD3D11Frame(AVFrame *frame, SDL_Texture **texture) 967{ 968#ifdef SDL_PLATFORM_WIN32 969 AVHWFramesContext *frames = (AVHWFramesContext *)(frame->hw_frames_ctx->data); 970 int texture_width = 0, texture_height = 0; 971 ID3D11Texture2D *pTexture = (ID3D11Texture2D *)frame->data[0]; 972 UINT iSliceIndex = (UINT)(uintptr_t)frame->data[1]; 973 974 if (*texture) { 975 SDL_PropertiesID props = SDL_GetTextureProperties(*texture); 976 texture_width = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0); 977 texture_height = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, 0); 978 } 979 if (!*texture || texture_width != frames->width || texture_height != frames->height) { 980 if (*texture) { 981 SDL_DestroyTexture(*texture); 982 } 983 984 SDL_PropertiesID props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 985 *texture = SDL_CreateTextureWithProperties(renderer, props); 986 SDL_DestroyProperties(props); 987 if (!*texture) { 988 return false; 989 } 990 } 991 992 ID3D11Resource *dx11_resource = SDL_GetPointerProperty(SDL_GetTextureProperties(*texture), SDL_PROP_TEXTURE_D3D11_TEXTURE_POINTER, NULL); 993 if (!dx11_resource) { 994 SDL_SetError("Couldn't get texture ID3D11Resource interface"); 995 return false; 996 } 997 ID3D11DeviceContext_CopySubresourceRegion(d3d11_context, dx11_resource, 0, 0, 0, 0, (ID3D11Resource *)pTexture, iSliceIndex, NULL); 998 999 return true; 1000#else 1001 return false; 1002#endif 1003} 1004 1005static bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **texture) 1006{ 1007#ifdef SDL_PLATFORM_APPLE 1008 CVPixelBufferRef pPixelBuffer = (CVPixelBufferRef)frame->data[3]; 1009 SDL_PropertiesID props; 1010 1011 if (*texture) { 1012 /* Free the previous texture now that we're about to render a new one */ 1013 /* FIXME: We can actually keep a cache of textures that map to pixel buffers */ 1014 SDL_DestroyTexture(*texture); 1015 } 1016 1017 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 1018 SDL_SetPointerProperty(props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, pPixelBuffer); 1019 *texture = SDL_CreateTextureWithProperties(renderer, props); 1020 SDL_DestroyProperties(props); 1021 if (!*texture) { 1022 return false; 1023 } 1024 1025 return true; 1026#else 1027 return false; 1028#endif 1029} 1030 1031static bool GetTextureForVulkanFrame(AVFrame *frame, SDL_Texture **texture) 1032{ 1033 SDL_PropertiesID props; 1034 1035 if (*texture) { 1036 SDL_DestroyTexture(*texture); 1037 } 1038 1039 props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC); 1040 *texture = CreateVulkanVideoTexture(vulkan_context, frame, renderer, props); 1041 SDL_DestroyProperties(props); 1042 if (!*texture) { 1043 return false; 1044 } 1045 return true; 1046} 1047 1048static bool GetTextureForFrame(AVFrame *frame, SDL_Texture **texture) 1049{ 1050 switch (frame->format) { 1051 case AV_PIX_FMT_VAAPI: 1052 return GetTextureForVAAPIFrame(frame, texture); 1053 case AV_PIX_FMT_DRM_PRIME: 1054 return GetTextureForDRMFrame(frame, texture); 1055 case AV_PIX_FMT_D3D11: 1056 return GetTextureForD3D11Frame(frame, texture); 1057 case AV_PIX_FMT_VIDEOTOOLBOX: 1058 return GetTextureForVideoToolboxFrame(frame, texture); 1059 case AV_PIX_FMT_VULKAN: 1060 return GetTextureForVulkanFrame(frame, texture); 1061 default: 1062 return GetTextureForMemoryFrame(frame, texture); 1063 } 1064} 1065 1066static int BeginFrameRendering(AVFrame *frame) 1067{ 1068 if (frame->format == AV_PIX_FMT_VULKAN) { 1069 return BeginVulkanFrameRendering(vulkan_context, frame, renderer); 1070 } 1071 return 0; 1072} 1073 1074static int FinishFrameRendering(AVFrame *frame) 1075{ 1076 if (frame->format == AV_PIX_FMT_VULKAN) { 1077 return FinishVulkanFrameRendering(vulkan_context, frame, renderer); 1078 } 1079 return 0; 1080} 1081 1082static void DisplayVideoTexture(AVFrame *frame) 1083{ 1084 /* Update the video texture */ 1085 if (!GetTextureForFrame(frame, &video_texture)) { 1086 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't get texture for frame: %s", SDL_GetError()); 1087 return; 1088 } 1089 1090 SDL_FRect src; 1091 src.x = 0.0f; 1092 src.y = 0.0f; 1093 src.w = (float)frame->width; 1094 src.h = (float)frame->height; 1095 if (frame->linesize[0] < 0) { 1096 SDL_RenderTextureRotated(renderer, video_texture, &src, NULL, 0.0, NULL, SDL_FLIP_VERTICAL); 1097 } else { 1098 SDL_RenderTexture(renderer, video_texture, &src, NULL); 1099 } 1100} 1101 1102static void DisplayVideoFrame(AVFrame *frame) 1103{ 1104 DisplayVideoTexture(frame); 1105} 1106 1107static void HandleVideoFrame(AVFrame *frame, double pts) 1108{ 1109 /* Quick and dirty PTS handling */ 1110 if (!video_start) { 1111 video_start = SDL_GetTicks(); 1112 } 1113 double now = (double)(SDL_GetTicks() - video_start) / 1000.0; 1114 if (now < pts) { 1115 SDL_DelayPrecise((Uint64)((pts - now) * SDL_NS_PER_SECOND)); 1116 } 1117 1118 if (BeginFrameRendering(frame) < 0) { 1119 return; 1120 } 1121 1122 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 1123 SDL_RenderClear(renderer); 1124 1125 DisplayVideoFrame(frame); 1126 1127 /* Render any bouncing balls */ 1128 MoveSprite(); 1129 1130 SDL_RenderPresent(renderer); 1131 1132 FinishFrameRendering(frame); 1133} 1134 1135static AVCodecContext *OpenAudioStream(AVFormatContext *ic, int stream, const AVCodec *codec) 1136{ 1137 AVStream *st = ic->streams[stream]; 1138 AVCodecParameters *codecpar = st->codecpar; 1139 AVCodecContext *context; 1140 int result; 1141 1142 SDL_Log("Audio stream: %s %d channels, %d Hz", avcodec_get_name(codec->id), codecpar->ch_layout.nb_channels, codecpar->sample_rate); 1143 1144 context = avcodec_alloc_context3(NULL); 1145 if (!context) { 1146 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_alloc_context3 failed"); 1147 return NULL; 1148 } 1149 1150 result = avcodec_parameters_to_context(context, ic->streams[stream]->codecpar); 1151 if (result < 0) { 1152 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_parameters_to_context failed: %s", av_err2str(result)); 1153 avcodec_free_context(&context); 1154 return NULL; 1155 } 1156 context->pkt_timebase = ic->streams[stream]->time_base; 1157 1158 result = avcodec_open2(context, codec, NULL); 1159 if (result < 0) { 1160 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open codec %s: %s", avcodec_get_name(context->codec_id), av_err2str(result)); 1161 avcodec_free_context(&context); 1162 return NULL; 1163 } 1164 1165 SDL_AudioSpec spec = { SDL_AUDIO_F32, codecpar->ch_layout.nb_channels, codecpar->sample_rate }; 1166 audio = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); 1167 if (audio) { 1168 SDL_ResumeAudioStreamDevice(audio); 1169 } else { 1170 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s", SDL_GetError()); 1171 } 1172 return context; 1173} 1174 1175static SDL_AudioFormat GetAudioFormat(int format) 1176{ 1177 switch (format) { 1178 case AV_SAMPLE_FMT_U8: 1179 case AV_SAMPLE_FMT_U8P: 1180 return SDL_AUDIO_U8; 1181 case AV_SAMPLE_FMT_S16: 1182 case AV_SAMPLE_FMT_S16P: 1183 return SDL_AUDIO_S16; 1184 case AV_SAMPLE_FMT_S32: 1185 case AV_SAMPLE_FMT_S32P: 1186 return SDL_AUDIO_S32; 1187 case AV_SAMPLE_FMT_FLT: 1188 case AV_SAMPLE_FMT_FLTP: 1189 return SDL_AUDIO_F32; 1190 default: 1191 /* Unsupported */ 1192 return SDL_AUDIO_UNKNOWN; 1193 } 1194} 1195 1196static bool IsPlanarAudioFormat(int format) 1197{ 1198 switch (format) { 1199 case AV_SAMPLE_FMT_U8P: 1200 case AV_SAMPLE_FMT_S16P: 1201 case AV_SAMPLE_FMT_S32P: 1202 case AV_SAMPLE_FMT_FLTP: 1203 case AV_SAMPLE_FMT_DBLP: 1204 case AV_SAMPLE_FMT_S64P: 1205 return true; 1206 default: 1207 return false; 1208 } 1209} 1210 1211static void InterleaveAudio(AVFrame *frame, const SDL_AudioSpec *spec) 1212{ 1213 int c, n; 1214 int samplesize = SDL_AUDIO_BYTESIZE(spec->format); 1215 int framesize = SDL_AUDIO_FRAMESIZE(*spec); 1216 Uint8 *data = (Uint8 *)SDL_malloc(frame->nb_samples * framesize); 1217 if (!data) { 1218 return; 1219 } 1220 1221 /* This could be optimized with SIMD and not allocating memory each time */ 1222 for (c = 0; c < spec->channels; ++c) { 1223 const Uint8 *src = frame->data[c]; 1224 Uint8 *dst = data + c * samplesize; 1225 for (n = frame->nb_samples; n--;) { 1226 SDL_memcpy(dst, src, samplesize); 1227 src += samplesize; 1228 dst += framesize; 1229 } 1230 } 1231 SDL_PutAudioStreamData(audio, data, frame->nb_samples * framesize); 1232 SDL_free(data); 1233} 1234 1235static void HandleAudioFrame(AVFrame *frame) 1236{ 1237 if (audio) { 1238 SDL_AudioSpec spec = { GetAudioFormat(frame->format), frame->ch_layout.nb_channels, frame->sample_rate }; 1239 SDL_SetAudioStreamFormat(audio, &spec, NULL); 1240 1241 if (frame->ch_layout.nb_channels > 1 && IsPlanarAudioFormat(frame->format)) { 1242 InterleaveAudio(frame, &spec); 1243 } else { 1244 SDL_PutAudioStreamData(audio, frame->data[0], frame->nb_samples * SDL_AUDIO_FRAMESIZE(spec)); 1245 } 1246 } 1247} 1248 1249static void av_log_callback(void *avcl, int level, const char *fmt, va_list vl) 1250{ 1251 const char *pszCategory = NULL; 1252 char *message; 1253 1254 switch (level) { 1255 case AV_LOG_PANIC: 1256 case AV_LOG_FATAL: 1257 pszCategory = "fatal error"; 1258 break; 1259 case AV_LOG_ERROR: 1260 pszCategory = "error"; 1261 break; 1262 case AV_LOG_WARNING: 1263 pszCategory = "warning"; 1264 break; 1265 case AV_LOG_INFO: 1266 pszCategory = "info"; 1267 break; 1268 case AV_LOG_VERBOSE: 1269 pszCategory = "verbose"; 1270 break; 1271 case AV_LOG_DEBUG: 1272 if (verbose) { 1273 pszCategory = "debug"; 1274 } 1275 break; 1276 } 1277 1278 if (!pszCategory) { 1279 // We don't care about this message 1280 return; 1281 } 1282 1283 SDL_vasprintf(&message, fmt, vl); 1284 SDL_Log("ffmpeg %s: %s", pszCategory, message); 1285 SDL_free(message); 1286} 1287 1288static void print_usage(SDLTest_CommonState *state, const char *argv0) 1289{ 1290 static const char *options[] = { "[--verbose]", "[--sprites N]", "[--audio-codec codec]", "[--video-codec codec]", "[--software]", "video_file", NULL }; 1291 SDLTest_CommonLogUsage(state, argv0, options); 1292} 1293 1294int main(int argc, char *argv[]) 1295{ 1296 const char *file = NULL; 1297 AVFormatContext *ic = NULL; 1298 int audio_stream = -1; 1299 int video_stream = -1; 1300 const char *audio_codec_name = NULL; 1301 const char *video_codec_name = NULL; 1302 const AVCodec *audio_codec = NULL; 1303 const AVCodec *video_codec = NULL; 1304 AVCodecContext *audio_context = NULL; 1305 AVCodecContext *video_context = NULL; 1306 AVPacket *pkt = NULL; 1307 AVFrame *frame = NULL; 1308 double first_pts = -1.0; 1309 int i; 1310 int result; 1311 int return_code = -1; 1312 SDL_WindowFlags window_flags; 1313 bool flushing = false; 1314 bool decoded = false; 1315 bool done = false; 1316 SDLTest_CommonState *state; 1317 1318 /* Initialize test framework */ 1319 state = SDLTest_CommonCreateState(argv, 0); 1320 1321 /* Enable standard application logging */ 1322 SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 1323 1324 /* Log ffmpeg messages */ 1325 av_log_set_callback(av_log_callback); 1326 1327 /* Parse commandline */ 1328 for (i = 1; i < argc;) { 1329 int consumed; 1330 1331 consumed = SDLTest_CommonArg(state, i); 1332 if (!consumed) { 1333 if (SDL_strcmp(argv[i], "--verbose") == 0) { 1334 verbose = true; 1335 consumed = 1; 1336 } else if (SDL_strcmp(argv[i], "--sprites") == 0 && argv[i + 1]) { 1337 num_sprites = SDL_atoi(argv[i + 1]); 1338 consumed = 2; 1339 } else if (SDL_strcmp(argv[i], "--audio-codec") == 0 && argv[i + 1]) { 1340 audio_codec_name = argv[i + 1]; 1341 consumed = 2; 1342 } else if (SDL_strcmp(argv[i], "--video-codec") == 0 && argv[i + 1]) { 1343 video_codec_name = argv[i + 1]; 1344 consumed = 2; 1345 } else if (SDL_strcmp(argv[i], "--software") == 0) { 1346 software_only = true; 1347 consumed = 1; 1348 } else if (!file) { 1349 /* We'll try to open this as a media file */ 1350 file = argv[i]; 1351 consumed = 1; 1352 } 1353 } 1354 if (consumed <= 0) { 1355 print_usage(state, argv[0]); 1356 return_code = 1; 1357 goto quit; 1358 } 1359 1360 i += consumed; 1361 } 1362 1363 if (!file) { 1364 print_usage(state, argv[0]); 1365 return_code = 1; 1366 goto quit; 1367 } 1368 1369 if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) { 1370 return_code = 2; 1371 goto quit; 1372 } 1373 1374 window_flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; 1375#ifdef SDL_PLATFORM_APPLE 1376 window_flags |= SDL_WINDOW_METAL; 1377#elif !defined(SDL_PLATFORM_WIN32) 1378 window_flags |= SDL_WINDOW_OPENGL; 1379#endif 1380 if (SDL_GetHint(SDL_HINT_RENDER_DRIVER) != NULL) { 1381 CreateWindowAndRenderer(window_flags, SDL_GetHint(SDL_HINT_RENDER_DRIVER)); 1382 } 1383#ifdef HAVE_EGL 1384 /* Try to create an EGL compatible window for DRM hardware frame support */ 1385 if (!window) { 1386 CreateWindowAndRenderer(window_flags, "opengles2"); 1387 } 1388#endif 1389#ifdef SDL_PLATFORM_APPLE 1390 if (!window) { 1391 CreateWindowAndRenderer(window_flags, "metal"); 1392 } 1393#endif 1394#ifdef SDL_PLATFORM_WIN32 1395 if (!window) { 1396 CreateWindowAndRenderer(window_flags, "direct3d11"); 1397 } 1398#endif 1399 if (!window) { 1400 if (!CreateWindowAndRenderer(window_flags, NULL)) { 1401 return_code = 2; 1402 goto quit; 1403 } 1404 } 1405 1406 if (!SDL_SetWindowTitle(window, file)) { 1407 SDL_Log("SDL_SetWindowTitle: %s", SDL_GetError()); 1408 } 1409 1410 /* Open the media file */ 1411 result = avformat_open_input(&ic, file, NULL, NULL); 1412 if (result < 0) { 1413 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open %s: %d", argv[1], result); 1414 return_code = 4; 1415 goto quit; 1416 } 1417 video_stream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, 0); 1418 if (video_stream >= 0) { 1419 if (video_codec_name) { 1420 video_codec = avcodec_find_decoder_by_name(video_codec_name); 1421 if (!video_codec) { 1422 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find codec '%s'", video_codec_name); 1423 return_code = 4; 1424 goto quit; 1425 } 1426 } 1427 video_context = OpenVideoStream(ic, video_stream, video_codec); 1428 if (!video_context) { 1429 return_code = 4; 1430 goto quit; 1431 } 1432 } 1433 audio_stream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, video_stream, &audio_codec, 0); 1434 if (audio_stream >= 0) { 1435 if (audio_codec_name) { 1436 audio_codec = avcodec_find_decoder_by_name(audio_codec_name); 1437 if (!audio_codec) { 1438 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find codec '%s'", audio_codec_name); 1439 return_code = 4; 1440 goto quit; 1441 } 1442 } 1443 audio_context = OpenAudioStream(ic, audio_stream, audio_codec); 1444 if (!audio_context) { 1445 return_code = 4; 1446 goto quit; 1447 } 1448 } 1449 pkt = av_packet_alloc(); 1450 if (!pkt) { 1451 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "av_packet_alloc failed"); 1452 return_code = 4; 1453 goto quit; 1454 } 1455 frame = av_frame_alloc(); 1456 if (!frame) { 1457 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "av_frame_alloc failed"); 1458 return_code = 4; 1459 goto quit; 1460 } 1461 1462 /* Create the sprite */ 1463 sprite = CreateTexture(renderer, icon_png, icon_png_len, &sprite_w, &sprite_h); 1464 1465 if (!sprite) { 1466 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture (%s)", SDL_GetError()); 1467 return_code = 3; 1468 goto quit; 1469 } 1470 1471 /* Allocate memory for the sprite info */ 1472 positions = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*positions)); 1473 velocities = (SDL_FRect *)SDL_malloc(num_sprites * sizeof(*velocities)); 1474 if (!positions || !velocities) { 1475 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!"); 1476 return_code = 3; 1477 goto quit; 1478 } 1479 1480 /* Position sprites and set their velocities */ 1481 SDL_Rect viewport; 1482 SDL_GetRenderViewport(renderer, &viewport); 1483 for (i = 0; i < num_sprites; ++i) { 1484 positions[i].x = (float)SDL_rand(viewport.w - sprite_w); 1485 positions[i].y = (float)SDL_rand(viewport.h - sprite_h); 1486 positions[i].w = (float)sprite_w; 1487 positions[i].h = (float)sprite_h; 1488 velocities[i].x = 0.0f; 1489 velocities[i].y = 0.0f; 1490 while (velocities[i].x == 0.f || velocities[i].y == 0.f) { 1491 velocities[i].x = (float)(SDL_rand(2 + 1) - 1); 1492 velocities[i].y = (float)(SDL_rand(2 + 1) - 1); 1493 } 1494 } 1495 1496 /* We're ready to go! */ 1497 SDL_ShowWindow(window); 1498 1499 /* Main render loop */ 1500 while (!done) { 1501 SDL_Event event; 1502 1503 /* Check for events */ 1504 while (SDL_PollEvent(&event)) { 1505 if (event.type == SDL_EVENT_QUIT || 1506 (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE)) { 1507 done = true; 1508 } 1509 } 1510 1511 if (!flushing) { 1512 result = av_read_frame(ic, pkt); 1513 if (result < 0) { 1514 SDL_Log("End of stream, finishing decode"); 1515 if (audio_context) { 1516 avcodec_flush_buffers(audio_context); 1517 } 1518 if (video_context) { 1519 avcodec_flush_buffers(video_context); 1520 } 1521 flushing = true; 1522 } else { 1523 if (pkt->stream_index == audio_stream) { 1524 result = avcodec_send_packet(audio_context, pkt); 1525 if (result < 0) { 1526 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_send_packet(audio_context) failed: %s", av_err2str(result)); 1527 } 1528 } else if (pkt->stream_index == video_stream) { 1529 result = avcodec_send_packet(video_context, pkt); 1530 if (result < 0) { 1531 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "avcodec_send_packet(video_context) failed: %s", av_err2str(result)); 1532 } 1533 } 1534 av_packet_unref(pkt); 1535 } 1536 } 1537 1538 decoded = false; 1539 if (audio_context) { 1540 while (avcodec_receive_frame(audio_context, frame) >= 0) { 1541 HandleAudioFrame(frame); 1542 decoded = true; 1543 } 1544 if (flushing) { 1545 /* Let SDL know we're done sending audio */ 1546 SDL_FlushAudioStream(audio); 1547 } 1548 } 1549 if (video_context) { 1550 while (avcodec_receive_frame(video_context, frame) >= 0) { 1551 double pts = ((double)frame->pts * video_context->pkt_timebase.num) / video_context->pkt_timebase.den; 1552 if (first_pts < 0.0) { 1553 first_pts = pts; 1554 } 1555 pts -= first_pts; 1556 1557 HandleVideoFrame(frame, pts); 1558 decoded = true; 1559 } 1560 } else { 1561 /* Update video rendering */ 1562 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 1563 SDL_RenderClear(renderer); 1564 MoveSprite(); 1565 SDL_RenderPresent(renderer); 1566 } 1567 1568 if (flushing && !decoded) { 1569 if (SDL_GetAudioStreamQueued(audio) > 0) { 1570 /* Wait a little bit for the audio to finish */ 1571 SDL_Delay(10); 1572 } else { 1573 done = true; 1574 } 1575 } 1576 } 1577 return_code = 0; 1578quit: 1579#ifdef SDL_PLATFORM_WIN32 1580 if (d3d11_context) { 1581 ID3D11DeviceContext_Release(d3d11_context); 1582 d3d11_context = NULL; 1583 } 1584 if (d3d11_device) { 1585 ID3D11Device_Release(d3d11_device); 1586 d3d11_device = NULL; 1587 } 1588#endif 1589 SDL_free(positions); 1590 SDL_free(velocities); 1591 av_frame_free(&frame); 1592 av_packet_free(&pkt); 1593 avcodec_free_context(&audio_context); 1594 avcodec_free_context(&video_context); 1595 avformat_close_input(&ic); 1596 SDL_DestroyRenderer(renderer); 1597 if (vulkan_context) { 1598 DestroyVulkanVideoContext(vulkan_context); 1599 } 1600 SDL_DestroyWindow(window); 1601 SDL_Quit(); 1602 SDLTest_CommonDestroyState(state); 1603 return return_code; 1604} 1605
[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.