Merge pull request #400 from vladmikhalin/basic-av-player

Basic AvPlayer implementation
This commit is contained in:
georgemoralis 2024-08-16 11:17:56 +03:00 committed by GitHub
commit 3197ad336e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 2659 additions and 147 deletions

3
.gitmodules vendored
View File

@ -61,3 +61,6 @@
[submodule "externals/date"] [submodule "externals/date"]
path = externals/date path = externals/date
url = https://github.com/HowardHinnant/date.git url = https://github.com/HowardHinnant/date.git
[submodule "externals/ffmpeg-core"]
path = externals/ffmpeg-core
url = https://github.com/shadps4-emu/ext-ffmpeg-core

View File

@ -193,6 +193,16 @@ set(SYSTEM_LIBS src/core/libraries/system/commondialog.cpp
src/core/libraries/disc_map/disc_map.cpp src/core/libraries/disc_map/disc_map.cpp
src/core/libraries/disc_map/disc_map.h src/core/libraries/disc_map/disc_map.h
src/core/libraries/disc_map/disc_map_codes.h src/core/libraries/disc_map/disc_map_codes.h
src/core/libraries/avplayer/avplayer_common.cpp
src/core/libraries/avplayer/avplayer_common.h
src/core/libraries/avplayer/avplayer_file_streamer.cpp
src/core/libraries/avplayer/avplayer_file_streamer.h
src/core/libraries/avplayer/avplayer_impl.cpp
src/core/libraries/avplayer/avplayer_impl.h
src/core/libraries/avplayer/avplayer_source.cpp
src/core/libraries/avplayer/avplayer_source.h
src/core/libraries/avplayer/avplayer_state.cpp
src/core/libraries/avplayer/avplayer_state.h
src/core/libraries/avplayer/avplayer.cpp src/core/libraries/avplayer/avplayer.cpp
src/core/libraries/avplayer/avplayer.h src/core/libraries/avplayer/avplayer.h
) )
@ -597,7 +607,7 @@ endif()
create_target_directory_groups(shadps4) create_target_directory_groups(shadps4)
target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API) target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API ffmpeg)
target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::SPIRV glslang::glslang SDL3::SDL3) target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::SPIRV glslang::glslang SDL3::SDL3)
if (APPLE) if (APPLE)

View File

@ -47,6 +47,11 @@ else()
endif() endif()
endif() endif()
if (NOT TARGET ffmpeg)
set(ARCHITECTURE "x86_64")
add_subdirectory(ffmpeg-core)
endif()
# Zlib-Ng # Zlib-Ng
if (NOT TARGET zlib-ng::zlib) if (NOT TARGET zlib-ng::zlib)
set(ZLIB_ENABLE_TESTS OFF) set(ZLIB_ENABLE_TESTS OFF)

1
externals/ffmpeg-core vendored Submodule

@ -0,0 +1 @@
Subproject commit e30b7d7fe228bfb3f6e41ce1040b44a15eb7d5e0

View File

@ -1,19 +1,23 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include "sdl_audio.h"
#include "common/assert.h"
#include "core/libraries/error_codes.h"
#include <SDL3/SDL_audio.h> #include <SDL3/SDL_audio.h>
#include <SDL3/SDL_init.h> #include <SDL3/SDL_init.h>
#include <SDL3/SDL_timer.h> #include <SDL3/SDL_timer.h>
#include "common/assert.h"
#include "core/libraries/error_codes.h" #include <mutex> // std::unique_lock
#include "sdl_audio.h"
namespace Audio { namespace Audio {
int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq, int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
Libraries::AudioOut::OrbisAudioOutParamFormat format) { Libraries::AudioOut::OrbisAudioOutParamFormat format) {
using Libraries::AudioOut::OrbisAudioOutParamFormat; using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::scoped_lock lock{m_mutex}; std::unique_lock lock{m_mutex};
for (int id = 0; id < portsOut.size(); id++) { for (int id = 0; id < portsOut.size(); id++) {
auto& port = portsOut[id]; auto& port = portsOut[id];
if (!port.isOpen) { if (!port.isOpen) {
@ -88,7 +92,7 @@ int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
} }
s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) { s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
std::scoped_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
if (!port.isOpen) { if (!port.isOpen) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
@ -100,7 +104,7 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
int result = SDL_PutAudioStreamData(port.stream, ptr, int result = SDL_PutAudioStreamData(port.stream, ptr,
port.samples_num * port.sample_size * port.channels_num); port.samples_num * port.sample_size * port.channels_num);
// TODO find a correct value 8192 is estimated // TODO find a correct value 8192 is estimated
while (SDL_GetAudioStreamAvailable(port.stream) > 8192) { while (SDL_GetAudioStreamAvailable(port.stream) > 65536) {
SDL_Delay(0); SDL_Delay(0);
} }
@ -109,7 +113,7 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) { bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
using Libraries::AudioOut::OrbisAudioOutParamFormat; using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::scoped_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
if (!port.isOpen) { if (!port.isOpen) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
@ -147,7 +151,7 @@ bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
} }
bool SDLAudio::AudioOutGetStatus(s32 handle, int* type, int* channels_num) { bool SDLAudio::AudioOutGetStatus(s32 handle, int* type, int* channels_num) {
std::scoped_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
*type = port.type; *type = port.type;
*channels_num = port.channels_num; *channels_num = port.channels_num;

View File

@ -3,7 +3,7 @@
#pragma once #pragma once
#include <mutex> #include <shared_mutex>
#include <SDL3/SDL_audio.h> #include <SDL3/SDL_audio.h>
#include "core/libraries/audio/audioout.h" #include "core/libraries/audio/audioout.h"
@ -32,7 +32,7 @@ private:
int volume[8] = {}; int volume[8] = {};
SDL_AudioStream* stream = nullptr; SDL_AudioStream* stream = nullptr;
}; };
std::mutex m_mutex; std::shared_mutex m_mutex;
std::array<PortOut, 22> portsOut; // main up to 8 ports , BGM 1 port , voice up to 4 ports , std::array<PortOut, 22> portsOut; // main up to 8 ports , BGM 1 port , voice up to 4 ports ,
// personal up to 4 ports , padspk up to 5 ports , aux 1 port // personal up to 4 ports , padspk up to 5 ports , aux 1 port
}; };

View File

@ -106,12 +106,12 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) {
SUB(Lib, DiscMap) \ SUB(Lib, DiscMap) \
SUB(Lib, Png) \ SUB(Lib, Png) \
SUB(Lib, PlayGo) \ SUB(Lib, PlayGo) \
SUB(Lib, Random) \
SUB(Lib, Usbd) \ SUB(Lib, Usbd) \
SUB(Lib, Ajm) \ SUB(Lib, Ajm) \
SUB(Lib, ErrorDialog) \ SUB(Lib, ErrorDialog) \
SUB(Lib, ImeDialog) \ SUB(Lib, ImeDialog) \
SUB(Lib, AvPlayer) \ SUB(Lib, AvPlayer) \
SUB(Lib, Random) \
CLS(Frontend) \ CLS(Frontend) \
CLS(Render) \ CLS(Render) \
SUB(Render, Vulkan) \ SUB(Render, Vulkan) \

View File

@ -25,9 +25,9 @@ void MntPoints::UnmountAll() {
m_mnt_pairs.clear(); m_mnt_pairs.clear();
} }
std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) { std::filesystem::path MntPoints::GetHostPath(std::string_view guest_directory) {
// Evil games like Turok2 pass double slashes e.g /app0//game.kpf // Evil games like Turok2 pass double slashes e.g /app0//game.kpf
auto corrected_path = guest_directory; std::string corrected_path(guest_directory);
size_t pos = corrected_path.find("//"); size_t pos = corrected_path.find("//");
while (pos != std::string::npos) { while (pos != std::string::npos) {
corrected_path.replace(pos, 2, "/"); corrected_path.replace(pos, 2, "/");

View File

@ -31,7 +31,7 @@ public:
void Unmount(const std::filesystem::path& host_folder, const std::string& guest_folder); void Unmount(const std::filesystem::path& host_folder, const std::string& guest_folder);
void UnmountAll(); void UnmountAll();
std::filesystem::path GetHostPath(const std::string& guest_directory); std::filesystem::path GetHostPath(std::string_view guest_directory);
const MntPair* GetMount(const std::string& guest_path) { const MntPair* GetMount(const std::string& guest_path) {
const auto it = std::ranges::find_if( const auto it = std::ranges::find_if(

View File

@ -175,7 +175,6 @@ int PS4_SYSV_ABI sceAudioOutGetLastOutputTime() {
} }
int PS4_SYSV_ABI sceAudioOutGetPortState(s32 handle, OrbisAudioOutPortState* state) { int PS4_SYSV_ABI sceAudioOutGetPortState(s32 handle, OrbisAudioOutPortState* state) {
int type = 0; int type = 0;
int channels_num = 0; int channels_num = 0;
@ -235,11 +234,11 @@ int PS4_SYSV_ABI sceAudioOutGetSystemState() {
} }
int PS4_SYSV_ABI sceAudioOutInit() { int PS4_SYSV_ABI sceAudioOutInit() {
LOG_TRACE(Lib_AudioOut, "called");
if (audio != nullptr) { if (audio != nullptr) {
return ORBIS_AUDIO_OUT_ERROR_ALREADY_INIT; return ORBIS_AUDIO_OUT_ERROR_ALREADY_INIT;
} }
audio = std::make_unique<Audio::SDLAudio>(); audio = std::make_unique<Audio::SDLAudio>();
LOG_INFO(Lib_AudioOut, "called");
return ORBIS_OK; return ORBIS_OK;
} }

View File

@ -1,21 +1,34 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Generated By moduleGenerator
#include "avplayer.h" #include "avplayer.h"
#include "avplayer_impl.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/libraries/error_codes.h" #include "core/libraries/error_codes.h"
#include "core/libraries/kernel/thread_management.h"
#include "core/libraries/libs.h" #include "core/libraries/libs.h"
namespace Libraries::AvPlayer { namespace Libraries::AvPlayer {
int PS4_SYSV_ABI sceAvPlayerAddSource() { using namespace Kernel;
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK; s32 PS4_SYSV_ABI sceAvPlayerAddSource(SceAvPlayerHandle handle, const char* filename) {
LOG_TRACE(Lib_AvPlayer, "filename = {}", filename);
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->AddSource(filename);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
} }
int PS4_SYSV_ABI sceAvPlayerAddSourceEx() { s32 PS4_SYSV_ABI sceAvPlayerAddSourceEx(SceAvPlayerHandle handle, SceAvPlayerUriType uriType,
SceAvPlayerSourceDetails* sourceDetails) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK; return ORBIS_OK;
} }
@ -24,122 +37,244 @@ int PS4_SYSV_ABI sceAvPlayerChangeStream() {
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceAvPlayerClose() { s32 PS4_SYSV_ABI sceAvPlayerClose(SceAvPlayerHandle handle) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
delete handle;
return ORBIS_OK;
}
u64 PS4_SYSV_ABI sceAvPlayerCurrentTime(SceAvPlayerHandle handle) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->CurrentTime();
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
s32 PS4_SYSV_ABI sceAvPlayerDisableStream(SceAvPlayerHandle handle, u32 stream_id) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceAvPlayerEnableStream(SceAvPlayerHandle handle, u32 stream_id) {
LOG_TRACE(Lib_AvPlayer, "stream_id = {}", stream_id);
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->EnableStream(stream_id);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
bool PS4_SYSV_ABI sceAvPlayerGetAudioData(SceAvPlayerHandle handle, SceAvPlayerFrameInfo* p_info) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr || p_info == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->GetAudioData(*p_info);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
s32 PS4_SYSV_ABI sceAvPlayerGetStreamInfo(SceAvPlayerHandle handle, u32 stream_id,
SceAvPlayerStreamInfo* p_info) {
LOG_TRACE(Lib_AvPlayer, "stream_id = {}", stream_id);
if (handle == nullptr || p_info == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->GetStreamInfo(stream_id, *p_info);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
bool PS4_SYSV_ABI sceAvPlayerGetVideoData(SceAvPlayerHandle handle,
SceAvPlayerFrameInfo* video_info) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr || video_info == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->GetVideoData(*video_info);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
bool PS4_SYSV_ABI sceAvPlayerGetVideoDataEx(SceAvPlayerHandle handle,
SceAvPlayerFrameInfoEx* video_info) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr || video_info == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->GetVideoData(*video_info);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
SceAvPlayerHandle PS4_SYSV_ABI sceAvPlayerInit(SceAvPlayerInitData* data) {
LOG_TRACE(Lib_AvPlayer, "called");
if (data == nullptr) {
return nullptr;
}
if (data->memory_replacement.allocate == nullptr ||
data->memory_replacement.allocate_texture == nullptr ||
data->memory_replacement.deallocate == nullptr ||
data->memory_replacement.deallocate_texture == nullptr) {
LOG_ERROR(Lib_AvPlayer, "All allocators are required for AVPlayer Initialisation.");
return nullptr;
}
return new AvPlayer(*data);
}
s32 PS4_SYSV_ABI sceAvPlayerInitEx(const SceAvPlayerInitDataEx* p_data,
SceAvPlayerHandle* p_player) {
LOG_TRACE(Lib_AvPlayer, "called");
if (p_data == nullptr || p_player == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
if (p_data->memory_replacement.allocate == nullptr ||
p_data->memory_replacement.allocate_texture == nullptr ||
p_data->memory_replacement.deallocate == nullptr ||
p_data->memory_replacement.deallocate_texture == nullptr) {
LOG_ERROR(Lib_AvPlayer, "All allocators are required for AVPlayer Initialisation.");
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
SceAvPlayerInitData data = {};
data.memory_replacement = p_data->memory_replacement;
data.file_replacement = p_data->file_replacement;
data.event_replacement = p_data->event_replacement;
data.default_language = p_data->default_language;
data.num_output_video_framebuffers = p_data->num_output_video_framebuffers;
data.auto_start = p_data->auto_start;
*p_player = new AvPlayer(data);
return ORBIS_OK;
}
bool PS4_SYSV_ABI sceAvPlayerIsActive(SceAvPlayerHandle handle) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr) {
LOG_TRACE(Lib_AvPlayer, "returning ORBIS_AVPLAYER_ERROR_INVALID_PARAMS");
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->IsActive();
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
s32 PS4_SYSV_ABI sceAvPlayerJumpToTime(SceAvPlayerHandle handle, uint64_t time) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called, time (msec) = {}", time);
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceAvPlayerPause(SceAvPlayerHandle handle) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceAvPlayerPostInit(SceAvPlayerHandle handle, SceAvPlayerPostInitData* data) {
LOG_TRACE(Lib_AvPlayer, "called");
if (handle == nullptr || data == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->PostInit(*data);
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
}
s32 PS4_SYSV_ABI sceAvPlayerPrintf(const char* format, ...) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceAvPlayerCurrentTime() { s32 PS4_SYSV_ABI sceAvPlayerResume(SceAvPlayerHandle handle) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceAvPlayerSetAvSyncMode(SceAvPlayerHandle handle,
SceAvPlayerAvSyncMode sync_mode) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceAvPlayerSetLogCallback(SceAvPlayerLogCallback log_cb, void* user_data) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceAvPlayerDisableStream() { s32 PS4_SYSV_ABI sceAvPlayerSetLooping(SceAvPlayerHandle handle, bool loop_flag) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_TRACE(Lib_AvPlayer, "called, looping = {}", loop_flag);
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
if (!handle->SetLooping(loop_flag)) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceAvPlayerEnableStream() { s32 PS4_SYSV_ABI sceAvPlayerSetTrickSpeed(SceAvPlayerHandle handle, s32 trick_speed) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceAvPlayerGetAudioData() { s32 PS4_SYSV_ABI sceAvPlayerStart(SceAvPlayerHandle handle) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_TRACE(Lib_AvPlayer, "called");
return ORBIS_OK; if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->Start();
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
} }
int PS4_SYSV_ABI sceAvPlayerGetStreamInfo() { s32 PS4_SYSV_ABI sceAvPlayerStop(SceAvPlayerHandle handle) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_TRACE(Lib_AvPlayer, "called");
return ORBIS_OK; if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->Stop();
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
} }
int PS4_SYSV_ABI sceAvPlayerGetVideoData() { s32 PS4_SYSV_ABI sceAvPlayerStreamCount(SceAvPlayerHandle handle) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_TRACE(Lib_AvPlayer, "called");
return ORBIS_OK; if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
const auto res = handle->GetStreamCount();
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
return res;
} }
int PS4_SYSV_ABI sceAvPlayerGetVideoDataEx() { s32 PS4_SYSV_ABI sceAvPlayerVprintf(const char* format, va_list args) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerInit() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerInitEx() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerIsActive() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerJumpToTime() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerPause() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerPostInit() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerPrintf() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerResume() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerSetAvSyncMode() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerSetLogCallback() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerSetLooping() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerSetTrickSpeed() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerStart() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerStop() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerStreamCount() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}
int PS4_SYSV_ABI sceAvPlayerVprintf() {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called"); LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK; return ORBIS_OK;
} }

View File

@ -5,39 +5,288 @@
#include "common/types.h" #include "common/types.h"
#include <stdarg.h> // va_list
#include <stddef.h> // size_t
namespace Core::Loader { namespace Core::Loader {
class SymbolsResolver; class SymbolsResolver;
} }
namespace Libraries::AvPlayer { namespace Libraries::AvPlayer {
int PS4_SYSV_ABI sceAvPlayerAddSource(); class AvPlayer;
int PS4_SYSV_ABI sceAvPlayerAddSourceEx();
int PS4_SYSV_ABI sceAvPlayerChangeStream(); using SceAvPlayerHandle = AvPlayer*;
int PS4_SYSV_ABI sceAvPlayerClose();
int PS4_SYSV_ABI sceAvPlayerCurrentTime(); enum SceAvPlayerUriType { SCE_AVPLAYER_URI_TYPE_SOURCE = 0 };
int PS4_SYSV_ABI sceAvPlayerDisableStream();
int PS4_SYSV_ABI sceAvPlayerEnableStream(); struct SceAvPlayerUri {
int PS4_SYSV_ABI sceAvPlayerGetAudioData(); const char* name;
int PS4_SYSV_ABI sceAvPlayerGetStreamInfo(); u32 length;
int PS4_SYSV_ABI sceAvPlayerGetVideoData(); };
int PS4_SYSV_ABI sceAvPlayerGetVideoDataEx();
int PS4_SYSV_ABI sceAvPlayerInit(); enum SceAvPlayerSourceType {
int PS4_SYSV_ABI sceAvPlayerInitEx(); SCE_AVPLAYER_SOURCE_TYPE_UNKNOWN = 0,
int PS4_SYSV_ABI sceAvPlayerIsActive(); SCE_AVPLAYER_SOURCE_TYPE_FILE_MP4 = 1,
int PS4_SYSV_ABI sceAvPlayerJumpToTime(); SCE_AVPLAYER_SOURCE_TYPE_HLS = 8
int PS4_SYSV_ABI sceAvPlayerPause(); };
int PS4_SYSV_ABI sceAvPlayerPostInit();
int PS4_SYSV_ABI sceAvPlayerPrintf(); struct SceAvPlayerSourceDetails {
int PS4_SYSV_ABI sceAvPlayerResume(); SceAvPlayerUri uri;
int PS4_SYSV_ABI sceAvPlayerSetAvSyncMode(); u8 reserved1[64];
int PS4_SYSV_ABI sceAvPlayerSetLogCallback(); SceAvPlayerSourceType source_type;
int PS4_SYSV_ABI sceAvPlayerSetLooping(); u8 reserved2[44];
int PS4_SYSV_ABI sceAvPlayerSetTrickSpeed(); };
int PS4_SYSV_ABI sceAvPlayerStart();
int PS4_SYSV_ABI sceAvPlayerStop(); struct SceAvPlayerAudio {
int PS4_SYSV_ABI sceAvPlayerStreamCount(); u16 channel_count;
int PS4_SYSV_ABI sceAvPlayerVprintf(); u8 reserved1[2];
u32 sample_rate;
u32 size;
u8 language_code[4];
};
struct SceAvPlayerVideo {
u32 width;
u32 height;
f32 aspect_ratio;
u8 language_code[4];
};
struct SceAvPlayerTextPosition {
u16 top;
u16 left;
u16 bottom;
u16 right;
};
struct SceAvPlayerTimedText {
u8 language_code[4];
u16 text_size;
u16 font_size;
SceAvPlayerTextPosition position;
};
union SceAvPlayerStreamDetails {
u8 reserved[16];
SceAvPlayerAudio audio;
SceAvPlayerVideo video;
SceAvPlayerTimedText subs;
};
struct SceAvPlayerFrameInfo {
u8* pData;
u8 reserved[4];
u64 timestamp;
SceAvPlayerStreamDetails details;
};
struct SceAvPlayerStreamInfo {
u32 type;
u8 reserved[4];
SceAvPlayerStreamDetails details;
u64 duration;
u64 start_time;
};
struct SceAvPlayerAudioEx {
u16 channel_count;
u8 reserved[2];
u32 sample_rate;
u32 size;
u8 language_code[4];
u8 reserved1[64];
};
struct SceAvPlayerVideoEx {
u32 width;
u32 height;
f32 aspect_ratio;
u8 language_code[4];
u32 framerate;
u32 crop_left_offset;
u32 crop_right_offset;
u32 crop_top_offset;
u32 crop_bottom_offset;
u32 pitch;
u8 luma_bit_depth;
u8 chroma_bit_depth;
bool video_full_range_flag;
u8 reserved1[37];
};
struct SceAvPlayerTimedTextEx {
u8 language_code[4];
u8 reserved[12];
u8 reserved1[64];
};
union SceAvPlayerStreamDetailsEx {
SceAvPlayerAudioEx audio;
SceAvPlayerVideoEx video;
SceAvPlayerTimedTextEx subs;
u8 reserved1[80];
};
struct SceAvPlayerFrameInfoEx {
void* pData;
u8 reserved[4];
u64 timestamp;
SceAvPlayerStreamDetailsEx details;
};
typedef void* PS4_SYSV_ABI (*SceAvPlayerAllocate)(void* p, u32 align, u32 size);
typedef void PS4_SYSV_ABI (*SceAvPlayerDeallocate)(void* p, void* mem);
typedef void* PS4_SYSV_ABI (*SceAvPlayerAllocateTexture)(void* p, u32 align, u32 size);
typedef void PS4_SYSV_ABI (*SceAvPlayerDeallocateTexture)(void* p, void* mem);
struct SceAvPlayerMemAllocator {
void* object_ptr;
SceAvPlayerAllocate allocate;
SceAvPlayerDeallocate deallocate;
SceAvPlayerAllocateTexture allocate_texture;
SceAvPlayerDeallocateTexture deallocate_texture;
};
typedef s32 PS4_SYSV_ABI (*SceAvPlayerOpenFile)(void* p, const char* name);
typedef s32 PS4_SYSV_ABI (*SceAvPlayerCloseFile)(void* p);
typedef s32 PS4_SYSV_ABI (*SceAvPlayerReadOffsetFile)(void* p, u8* buf, u64 pos, u32 len);
typedef u64 PS4_SYSV_ABI (*SceAvPlayerSizeFile)(void* p);
struct SceAvPlayerFileReplacement {
void* object_ptr;
SceAvPlayerOpenFile open;
SceAvPlayerCloseFile close;
SceAvPlayerReadOffsetFile readOffset;
SceAvPlayerSizeFile size;
};
typedef void PS4_SYSV_ABI (*SceAvPlayerEventCallback)(void* p, s32 event, s32 src_id, void* data);
struct SceAvPlayerEventReplacement {
void* object_ptr;
SceAvPlayerEventCallback event_callback;
};
enum SceAvPlayerDebuglevels {
SCE_AVPLAYER_DBG_NONE,
SCE_AVPLAYER_DBG_INFO,
SCE_AVPLAYER_DBG_WARNINGS,
SCE_AVPLAYER_DBG_ALL
};
struct SceAvPlayerInitData {
SceAvPlayerMemAllocator memory_replacement;
SceAvPlayerFileReplacement file_replacement;
SceAvPlayerEventReplacement event_replacement;
SceAvPlayerDebuglevels debug_level;
u32 base_priority;
s32 num_output_video_framebuffers;
bool auto_start;
u8 reserved[3];
const char* default_language;
};
struct SceAvPlayerInitDataEx {
size_t this_size;
SceAvPlayerMemAllocator memory_replacement;
SceAvPlayerFileReplacement file_replacement;
SceAvPlayerEventReplacement event_replacement;
const char* default_language;
SceAvPlayerDebuglevels debug_level;
u32 audio_decoder_priority;
u32 audio_decoder_affinity;
u32 video_decoder_priority;
u32 video_decoder_affinity;
u32 demuxer_priority;
u32 demuxer_affinity;
u32 controller_priority;
u32 controller_affinity;
u32 http_streaming_priority;
u32 http_streaming_affinity;
u32 file_streaming_priority;
u32 file_streaming_affinity;
s32 num_output_video_framebuffers;
bool auto_start;
u8 reserved[3];
};
enum SceAvPlayerStreamType {
SCE_AVPLAYER_VIDEO,
SCE_AVPLAYER_AUDIO,
SCE_AVPLAYER_TIMEDTEXT,
SCE_AVPLAYER_UNKNOWN
};
enum SceAvPlayerVideoDecoderType {
SCE_AVPLAYER_VIDEO_DECODER_TYPE_DEFAULT = 0,
SCE_AVPLAYER_VIDEO_DECODER_TYPE_RESERVED1,
SCE_AVPLAYER_VIDEO_DECODER_TYPE_SOFTWARE,
SCE_AVPLAYER_VIDEO_DECODER_TYPE_SOFTWARE2
};
enum SceAvPlayerAudioDecoderType {
SCE_AVPLAYER_AUDIO_DECODER_TYPE_DEFAULT = 0,
SCE_AVPLAYER_AUDIO_DECODER_TYPE_RESERVED1,
SCE_AVPLAYER_AUDIO_DECODER_TYPE_RESERVED2
};
struct SceAvPlayerDecoderInit {
union {
SceAvPlayerVideoDecoderType video_type;
SceAvPlayerAudioDecoderType audio_type;
u8 reserved[4];
} decoderType;
union {
struct {
s32 cpu_affinity_mask;
s32 cpu_thread_priority;
u8 decode_pipeline_depth;
u8 compute_pipe_id;
u8 compute_queue_id;
u8 enable_interlaced;
u8 reserved[16];
} avcSw2;
struct {
u8 audio_channel_order;
u8 reserved[27];
} aac;
u8 reserved[28];
} decoderParams;
};
struct SceAvPlayerHTTPCtx {
u32 http_context_id;
u32 ssl_context_id;
};
struct SceAvPlayerPostInitData {
u32 demux_video_buffer_size;
SceAvPlayerDecoderInit video_decoder_init;
SceAvPlayerDecoderInit audio_decoder_init;
SceAvPlayerHTTPCtx http_context;
u8 reserved[56];
};
enum SceAvPlayerAvSyncMode {
SCE_AVPLAYER_AV_SYNC_MODE_DEFAULT = 0,
SCE_AVPLAYER_AV_SYNC_MODE_NONE
};
typedef int PS4_SYSV_ABI (*SceAvPlayerLogCallback)(void* p, const char* format, va_list args);
enum SceAvPlayerEvents {
SCE_AVPLAYER_STATE_STOP = 0x01,
SCE_AVPLAYER_STATE_READY = 0x02,
SCE_AVPLAYER_STATE_PLAY = 0x03,
SCE_AVPLAYER_STATE_PAUSE = 0x04,
SCE_AVPLAYER_STATE_BUFFERING = 0x05,
SCE_AVPLAYER_TIMED_TEXT_DELIVERY = 0x10,
SCE_AVPLAYER_WARNING_ID = 0x20,
SCE_AVPLAYER_ENCRYPTION = 0x30,
SCE_AVPLAYER_DRM_ERROR = 0x40
};
void RegisterlibSceAvPlayer(Core::Loader::SymbolsResolver* sym); void RegisterlibSceAvPlayer(Core::Loader::SymbolsResolver* sym);
} // namespace Libraries::AvPlayer
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "avplayer.h"
#include "avplayer_common.h"
#include <algorithm> // std::equal
#include <cctype> // std::tolower
#include <string_view> // std::string_view
namespace Libraries::AvPlayer {
using namespace Kernel;
static bool ichar_equals(char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
}
static bool iequals(std::string_view l, std::string_view r) {
return std::ranges::equal(l, r, ichar_equals);
}
SceAvPlayerSourceType GetSourceType(std::string_view path) {
if (path.empty()) {
return SCE_AVPLAYER_SOURCE_TYPE_UNKNOWN;
}
std::string_view name = path;
if (path.find("://") != std::string_view::npos) {
// This path is a URI. Strip HTTP parameters from it.
// schema://server.domain/path/file.ext/and/beyond?param=value#paragraph ->
// -> schema://server.domain/path/to/file.ext/and/beyond
name = path.substr(0, path.find_first_of("?#"));
if (name.empty()) {
return SCE_AVPLAYER_SOURCE_TYPE_UNKNOWN;
}
}
// schema://server.domain/path/to/file.ext/and/beyond -> .ext/and/beyond
auto ext = name.substr(name.rfind('.'));
if (ext.empty()) {
return SCE_AVPLAYER_SOURCE_TYPE_UNKNOWN;
}
// .ext/and/beyond -> .ext
ext = ext.substr(0, ext.find('/'));
if (iequals(ext, ".mp4") || iequals(ext, ".m4v") || iequals(ext, ".m3d") ||
iequals(ext, ".m4a") || iequals(ext, ".mov")) {
return SCE_AVPLAYER_SOURCE_TYPE_FILE_MP4;
}
if (iequals(ext, ".m3u8")) {
return SCE_AVPLAYER_SOURCE_TYPE_HLS;
}
return SCE_AVPLAYER_SOURCE_TYPE_UNKNOWN;
}
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "common/assert.h"
#include "common/logging/log.h"
#include "core/libraries/kernel/thread_management.h"
#include <optional>
#include <utility>
#include <queue>
#define AVPLAYER_IS_ERROR(x) ((x) < 0)
namespace Libraries::AvPlayer {
enum class AvState {
Initial,
AddingSource,
Ready,
Play,
Stop,
EndOfFile,
Pause,
C0x08,
Jump,
TrickMode,
C0x0B,
Buffering,
Starting,
Error,
};
enum class AvEventType {
ChangeFlowState = 21,
WarningId = 22,
RevertState = 30,
AddSource = 40,
Error = 255,
};
union AvPlayerEventData {
u32 num_frames; // 20
AvState state; // AvEventType::ChangeFlowState
s32 error; // AvEventType::WarningId
u32 attempt; // AvEventType::AddSource
};
struct AvPlayerEvent {
AvEventType event;
AvPlayerEventData payload;
};
template <class T>
class AvPlayerQueue {
public:
size_t Size() {
return m_queue.size();
}
void Push(T&& value) {
std::lock_guard guard(m_mutex);
m_queue.emplace(std::forward<T>(value));
}
std::optional<T> Pop() {
if (Size() == 0) {
return std::nullopt;
}
std::lock_guard guard(m_mutex);
auto result = std::move(m_queue.front());
m_queue.pop();
return result;
}
void Clear() {
std::lock_guard guard(m_mutex);
m_queue = {};
}
private:
std::mutex m_mutex{};
std::queue<T> m_queue{};
};
SceAvPlayerSourceType GetSourceType(std::string_view path);
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "common/types.h"
struct AVIOContext;
namespace Libraries::AvPlayer {
class IDataStreamer {
public:
virtual ~IDataStreamer() = default;
virtual AVIOContext* GetContext() = 0;
};
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,86 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "avplayer_file_streamer.h"
#include "avplayer_common.h"
#include <magic_enum.hpp>
extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
}
#include <algorithm> // std::max, std::min
#define AVPLAYER_AVIO_BUFFER_SIZE 4096
namespace Libraries::AvPlayer {
AvPlayerFileStreamer::AvPlayerFileStreamer(const SceAvPlayerFileReplacement& file_replacement,
std::string_view path)
: m_file_replacement(file_replacement) {
const auto ptr = m_file_replacement.object_ptr;
m_fd = m_file_replacement.open(ptr, path.data());
ASSERT(m_fd >= 0);
m_file_size = m_file_replacement.size(ptr);
// avio_buffer is deallocated in `avio_context_free`
const auto avio_buffer = reinterpret_cast<u8*>(av_malloc(AVPLAYER_AVIO_BUFFER_SIZE));
m_avio_context =
avio_alloc_context(avio_buffer, AVPLAYER_AVIO_BUFFER_SIZE, 0, this,
&AvPlayerFileStreamer::ReadPacket, nullptr, &AvPlayerFileStreamer::Seek);
}
AvPlayerFileStreamer::~AvPlayerFileStreamer() {
if (m_avio_context != nullptr) {
avio_context_free(&m_avio_context);
}
if (m_file_replacement.close != nullptr && m_fd >= 0) {
const auto close = m_file_replacement.close;
const auto ptr = m_file_replacement.object_ptr;
close(ptr);
}
}
s32 AvPlayerFileStreamer::ReadPacket(void* opaque, u8* buffer, s32 size) {
const auto self = reinterpret_cast<AvPlayerFileStreamer*>(opaque);
if (self->m_position >= self->m_file_size) {
return AVERROR_EOF;
}
if (self->m_position + size > self->m_file_size) {
size = self->m_file_size - self->m_position;
}
const auto read_offset = self->m_file_replacement.readOffset;
const auto ptr = self->m_file_replacement.object_ptr;
const auto bytes_read = read_offset(ptr, buffer, self->m_position, size);
if (bytes_read == 0 && size != 0) {
return AVERROR_EOF;
}
self->m_position += bytes_read;
return bytes_read;
}
s64 AvPlayerFileStreamer::Seek(void* opaque, s64 offset, int whence) {
const auto self = reinterpret_cast<AvPlayerFileStreamer*>(opaque);
if (whence & AVSEEK_SIZE) {
return self->m_file_size;
}
if (whence == SEEK_CUR) {
self->m_position =
std::min(u64(std::max(s64(0), s64(self->m_position) + offset)), self->m_file_size);
return self->m_position;
} else if (whence == SEEK_SET) {
self->m_position = std::min(u64(std::max(s64(0), offset)), self->m_file_size);
return self->m_position;
} else if (whence == SEEK_END) {
self->m_position =
std::min(u64(std::max(s64(0), s64(self->m_file_size) + offset)), self->m_file_size);
return self->m_position;
}
return -1;
}
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "avplayer_data_streamer.h"
#include <string_view>
#include <vector>
struct AVIOContext;
namespace Libraries::AvPlayer {
class AvPlayerFileStreamer : public IDataStreamer {
public:
AvPlayerFileStreamer(const SceAvPlayerFileReplacement& file_replacement, std::string_view path);
~AvPlayerFileStreamer();
AVIOContext* GetContext() override {
return m_avio_context;
}
private:
static s32 ReadPacket(void* opaque, u8* buffer, s32 size);
static s64 Seek(void* opaque, s64 buffer, int whence);
SceAvPlayerFileReplacement m_file_replacement;
int m_fd = -1;
u64 m_position{};
u64 m_file_size{};
AVIOContext* m_avio_context{};
};
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,200 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "avplayer_common.h"
#include "avplayer_file_streamer.h"
#include "avplayer_impl.h"
#include "common/logging/log.h"
#include "core/libraries/error_codes.h"
#include "core/libraries/kernel/libkernel.h"
using namespace Libraries::Kernel;
namespace Libraries::AvPlayer {
void* PS4_SYSV_ABI AvPlayer::Allocate(void* handle, u32 alignment, u32 size) {
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
const auto allocate = self->m_init_data_original.memory_replacement.allocate;
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
return allocate(ptr, alignment, size);
}
void PS4_SYSV_ABI AvPlayer::Deallocate(void* handle, void* memory) {
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
const auto deallocate = self->m_init_data_original.memory_replacement.deallocate;
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
return deallocate(ptr, memory);
}
void* PS4_SYSV_ABI AvPlayer::AllocateTexture(void* handle, u32 alignment, u32 size) {
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
const auto allocate = self->m_init_data_original.memory_replacement.allocate_texture;
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
return allocate(ptr, alignment, size);
}
void PS4_SYSV_ABI AvPlayer::DeallocateTexture(void* handle, void* memory) {
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
const auto deallocate = self->m_init_data_original.memory_replacement.deallocate_texture;
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
return deallocate(ptr, memory);
}
int PS4_SYSV_ABI AvPlayer::OpenFile(void* handle, const char* filename) {
auto const self = reinterpret_cast<AvPlayer*>(handle);
std::lock_guard guard(self->m_file_io_mutex);
const auto open = self->m_init_data_original.file_replacement.open;
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
return open(ptr, filename);
}
int PS4_SYSV_ABI AvPlayer::CloseFile(void* handle) {
auto const self = reinterpret_cast<AvPlayer*>(handle);
std::lock_guard guard(self->m_file_io_mutex);
const auto close = self->m_init_data_original.file_replacement.close;
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
return close(ptr);
}
int PS4_SYSV_ABI AvPlayer::ReadOffsetFile(void* handle, u8* buffer, u64 position, u32 length) {
auto const self = reinterpret_cast<AvPlayer*>(handle);
std::lock_guard guard(self->m_file_io_mutex);
const auto read_offset = self->m_init_data_original.file_replacement.readOffset;
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
return read_offset(ptr, buffer, position, length);
}
u64 PS4_SYSV_ABI AvPlayer::SizeFile(void* handle) {
auto const self = reinterpret_cast<AvPlayer*>(handle);
std::lock_guard guard(self->m_file_io_mutex);
const auto size = self->m_init_data_original.file_replacement.size;
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
return size(ptr);
}
SceAvPlayerInitData AvPlayer::StubInitData(const SceAvPlayerInitData& data) {
SceAvPlayerInitData result = data;
result.memory_replacement.object_ptr = this;
result.memory_replacement.allocate = &AvPlayer::Allocate;
result.memory_replacement.deallocate = &AvPlayer::Deallocate;
result.memory_replacement.allocate_texture = &AvPlayer::AllocateTexture;
result.memory_replacement.deallocate_texture = &AvPlayer::DeallocateTexture;
if (data.file_replacement.open == nullptr || data.file_replacement.close == nullptr ||
data.file_replacement.readOffset == nullptr || data.file_replacement.size == nullptr) {
result.file_replacement = {};
} else {
result.file_replacement.object_ptr = this;
result.file_replacement.open = &AvPlayer::OpenFile;
result.file_replacement.close = &AvPlayer::CloseFile;
result.file_replacement.readOffset = &AvPlayer::ReadOffsetFile;
result.file_replacement.size = &AvPlayer::SizeFile;
}
return result;
}
AvPlayer::AvPlayer(const SceAvPlayerInitData& data)
: m_init_data(StubInitData(data)), m_init_data_original(data),
m_state(std::make_unique<AvPlayerState>(m_init_data)) {}
s32 AvPlayer::PostInit(const SceAvPlayerPostInitData& data) {
m_post_init_data = data;
return ORBIS_OK;
}
s32 AvPlayer::AddSource(std::string_view path) {
if (path.empty()) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
if (AVPLAYER_IS_ERROR(m_state->AddSource(path, GetSourceType(path)))) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK;
}
s32 AvPlayer::GetStreamCount() {
if (m_state == nullptr) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
const auto res = m_state->GetStreamCount();
if (AVPLAYER_IS_ERROR(res)) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return res;
}
s32 AvPlayer::GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info) {
if (AVPLAYER_IS_ERROR(m_state->GetStreamInfo(stream_index, info))) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK;
}
s32 AvPlayer::EnableStream(u32 stream_index) {
if (m_state == nullptr) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
if (!m_state->EnableStream(stream_index)) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK;
}
s32 AvPlayer::Start() {
return m_state->Start();
}
bool AvPlayer::GetVideoData(SceAvPlayerFrameInfo& video_info) {
if (m_state == nullptr) {
return false;
}
return m_state->GetVideoData(video_info);
}
bool AvPlayer::GetVideoData(SceAvPlayerFrameInfoEx& video_info) {
if (m_state == nullptr) {
return false;
}
return m_state->GetVideoData(video_info);
}
bool AvPlayer::GetAudioData(SceAvPlayerFrameInfo& audio_info) {
if (m_state == nullptr) {
return false;
}
return m_state->GetAudioData(audio_info);
}
bool AvPlayer::IsActive() {
if (m_state == nullptr) {
return false;
}
return m_state->IsActive();
}
u64 AvPlayer::CurrentTime() {
if (m_state == nullptr) {
return 0;
}
return m_state->CurrentTime();
}
s32 AvPlayer::Stop() {
if (m_state == nullptr || !m_state->Stop()) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK;
}
bool AvPlayer::SetLooping(bool is_looping) {
if (m_state == nullptr) {
return false;
}
return m_state->SetLooping(is_looping);
}
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "avplayer_data_streamer.h"
#include "avplayer_state.h"
#include "core/libraries/kernel/thread_management.h"
#include <mutex>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#include <memory>
#include <vector>
namespace Libraries::AvPlayer {
class AvPlayer {
public:
AvPlayer(const SceAvPlayerInitData& data);
s32 PostInit(const SceAvPlayerPostInitData& data);
s32 AddSource(std::string_view filename);
s32 GetStreamCount();
s32 GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info);
s32 EnableStream(u32 stream_index);
s32 Start();
bool GetAudioData(SceAvPlayerFrameInfo& audio_info);
bool GetVideoData(SceAvPlayerFrameInfo& video_info);
bool GetVideoData(SceAvPlayerFrameInfoEx& video_info);
bool IsActive();
u64 CurrentTime();
s32 Stop();
bool SetLooping(bool is_looping);
private:
using ScePthreadMutex = Kernel::ScePthreadMutex;
// Memory Replacement
static void* PS4_SYSV_ABI Allocate(void* handle, u32 alignment, u32 size);
static void PS4_SYSV_ABI Deallocate(void* handle, void* memory);
static void* PS4_SYSV_ABI AllocateTexture(void* handle, u32 alignment, u32 size);
static void PS4_SYSV_ABI DeallocateTexture(void* handle, void* memory);
// File Replacement
static int PS4_SYSV_ABI OpenFile(void* handle, const char* filename);
static int PS4_SYSV_ABI CloseFile(void* handle);
static int PS4_SYSV_ABI ReadOffsetFile(void* handle, u8* buffer, u64 position, u32 length);
static u64 PS4_SYSV_ABI SizeFile(void* handle);
SceAvPlayerInitData StubInitData(const SceAvPlayerInitData& data);
SceAvPlayerInitData m_init_data{};
SceAvPlayerInitData m_init_data_original{};
SceAvPlayerPostInitData m_post_init_data{};
std::mutex m_file_io_mutex{};
std::atomic_bool m_has_source{};
std::unique_ptr<AvPlayerState> m_state{};
};
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,730 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "avplayer_source.h"
#include "avplayer_file_streamer.h"
#include "common/singleton.h"
#include "core/file_sys/fs.h"
#include "core/libraries/kernel/time_management.h"
#include <magic_enum.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
}
namespace Libraries::AvPlayer {
using namespace Kernel;
AvPlayerSource::AvPlayerSource(AvPlayerStateCallback& state, std::string_view path,
const SceAvPlayerInitData& init_data,
SceAvPlayerSourceType source_type)
: m_state(state), m_memory_replacement(init_data.memory_replacement),
m_num_output_video_framebuffers(
std::min(std::max(2, init_data.num_output_video_framebuffers), 16)) {
AVFormatContext* context = avformat_alloc_context();
if (init_data.file_replacement.open != nullptr) {
m_up_data_streamer =
std::make_unique<AvPlayerFileStreamer>(init_data.file_replacement, path);
context->pb = m_up_data_streamer->GetContext();
ASSERT(!AVPLAYER_IS_ERROR(avformat_open_input(&context, nullptr, nullptr, nullptr)));
} else {
const auto mnt = Common::Singleton<Core::FileSys::MntPoints>::Instance();
const auto filepath = mnt->GetHostPath(path);
ASSERT(!AVPLAYER_IS_ERROR(
avformat_open_input(&context, filepath.string().c_str(), nullptr, nullptr)));
}
m_avformat_context = AVFormatContextPtr(context, &ReleaseAVFormatContext);
}
AvPlayerSource::~AvPlayerSource() {
Stop();
}
bool AvPlayerSource::FindStreamInfo() {
if (m_avformat_context == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not find stream info. NULL context.");
return false;
}
if (m_avformat_context->nb_streams > 0) {
return true;
}
return avformat_find_stream_info(m_avformat_context.get(), nullptr) == 0;
}
s32 AvPlayerSource::GetStreamCount() {
if (m_avformat_context == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not get stream count. NULL context.");
return -1;
}
LOG_INFO(Lib_AvPlayer, "Stream Count: {}", m_avformat_context->nb_streams);
return m_avformat_context->nb_streams;
}
static s32 CodecTypeToStreamType(AVMediaType codec_type) {
switch (codec_type) {
case AVMediaType::AVMEDIA_TYPE_VIDEO:
return SCE_AVPLAYER_VIDEO;
case AVMediaType::AVMEDIA_TYPE_AUDIO:
return SCE_AVPLAYER_AUDIO;
case AVMediaType::AVMEDIA_TYPE_SUBTITLE:
return SCE_AVPLAYER_TIMEDTEXT;
default:
LOG_ERROR(Lib_AvPlayer, "Unexpected AVMediaType {}", magic_enum::enum_name(codec_type));
return -1;
}
}
static f32 AVRationalToF32(const AVRational& rational) {
return f32(rational.num) / rational.den;
}
s32 AvPlayerSource::GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info) {
info = {};
if (m_avformat_context == nullptr || stream_index >= m_avformat_context->nb_streams) {
LOG_ERROR(Lib_AvPlayer, "Could not get stream {} info.", stream_index);
return -1;
}
const auto p_stream = m_avformat_context->streams[stream_index];
if (p_stream == nullptr || p_stream->codecpar == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not get stream {} info. NULL stream.", stream_index);
return -1;
}
info.type = CodecTypeToStreamType(p_stream->codecpar->codec_type);
info.start_time = p_stream->start_time;
info.duration = p_stream->duration;
const auto p_lang_node = av_dict_get(p_stream->metadata, "language", nullptr, 0);
if (p_lang_node != nullptr) {
LOG_INFO(Lib_AvPlayer, "Stream {} language = {}", stream_index, p_lang_node->value);
} else {
LOG_WARNING(Lib_AvPlayer, "Stream {} language is unknown", stream_index);
}
switch (info.type) {
case SCE_AVPLAYER_VIDEO:
LOG_INFO(Lib_AvPlayer, "Stream {} is a video stream.", stream_index);
info.details.video.aspect_ratio =
f32(p_stream->codecpar->width) / p_stream->codecpar->height;
info.details.video.width = p_stream->codecpar->width;
info.details.video.height = p_stream->codecpar->height;
if (p_lang_node != nullptr) {
std::memcpy(info.details.video.language_code, p_lang_node->value,
std::min(strlen(p_lang_node->value), size_t(3)));
}
break;
case SCE_AVPLAYER_AUDIO:
LOG_INFO(Lib_AvPlayer, "Stream {} is an audio stream.", stream_index);
info.details.audio.channel_count = p_stream->codecpar->ch_layout.nb_channels;
info.details.audio.sample_rate = p_stream->codecpar->sample_rate;
info.details.audio.size = 0; // sceAvPlayerGetStreamInfo() is expected to set this to 0
if (p_lang_node != nullptr) {
std::memcpy(info.details.audio.language_code, p_lang_node->value,
std::min(strlen(p_lang_node->value), size_t(3)));
}
break;
case SCE_AVPLAYER_TIMEDTEXT:
LOG_WARNING(Lib_AvPlayer, "Stream {} is a timedtext stream.", stream_index);
info.details.subs.font_size = 12;
info.details.subs.text_size = 12;
if (p_lang_node != nullptr) {
std::memcpy(info.details.subs.language_code, p_lang_node->value,
std::min(strlen(p_lang_node->value), size_t(3)));
}
break;
default:
LOG_ERROR(Lib_AvPlayer, "Stream {} type is unknown: {}.", stream_index, info.type);
return -1;
}
return 0;
}
bool AvPlayerSource::EnableStream(u32 stream_index) {
if (m_avformat_context == nullptr || stream_index >= m_avformat_context->nb_streams) {
return false;
}
const auto stream = m_avformat_context->streams[stream_index];
const auto decoder = avcodec_find_decoder(stream->codecpar->codec_id);
if (decoder == nullptr) {
return false;
}
switch (stream->codecpar->codec_type) {
case AVMediaType::AVMEDIA_TYPE_VIDEO: {
m_video_stream_index = stream_index;
m_video_codec_context =
AVCodecContextPtr(avcodec_alloc_context3(decoder), &ReleaseAVCodecContext);
if (avcodec_parameters_to_context(m_video_codec_context.get(), stream->codecpar) < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not copy stream {} avcodec parameters to context.",
stream_index);
return false;
}
if (avcodec_open2(m_video_codec_context.get(), decoder, nullptr) < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not open avcodec for video stream {}.", stream_index);
return false;
}
const auto width = m_video_codec_context->width;
const auto size = (width * m_video_codec_context->height * 3) / 2;
for (u64 index = 0; index < m_num_output_video_framebuffers; ++index) {
m_video_buffers.Push(FrameBuffer(m_memory_replacement, 0x100, size));
}
LOG_INFO(Lib_AvPlayer, "Video stream {} enabled", stream_index);
break;
}
case AVMediaType::AVMEDIA_TYPE_AUDIO: {
m_audio_stream_index = stream_index;
m_audio_codec_context =
AVCodecContextPtr(avcodec_alloc_context3(decoder), &ReleaseAVCodecContext);
if (avcodec_parameters_to_context(m_audio_codec_context.get(), stream->codecpar) < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not copy stream {} avcodec parameters to context.",
stream_index);
return false;
}
if (avcodec_open2(m_audio_codec_context.get(), decoder, nullptr) < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not open avcodec for audio stream {}.", stream_index);
return false;
}
const auto num_channels = m_audio_codec_context->ch_layout.nb_channels;
const auto align = num_channels * sizeof(u16);
const auto size = num_channels * sizeof(u16) * 1024;
for (u64 index = 0; index < 4; ++index) {
m_audio_buffers.Push(FrameBuffer(m_memory_replacement, 0x100, size));
}
LOG_INFO(Lib_AvPlayer, "Audio stream {} enabled", stream_index);
break;
}
default:
LOG_WARNING(Lib_AvPlayer, "Unknown stream type {} for stream {}",
magic_enum::enum_name(stream->codecpar->codec_type), stream_index);
break;
}
return true;
}
void AvPlayerSource::SetLooping(bool is_looping) {
m_is_looping = is_looping;
}
std::optional<bool> AvPlayerSource::HasFrames(u32 num_frames) {
return m_video_packets.Size() > num_frames || m_is_eof;
}
s32 AvPlayerSource::Start() {
std::unique_lock lock(m_state_mutex);
if (m_audio_codec_context == nullptr && m_video_codec_context == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not start playback. NULL context.");
return -1;
}
m_demuxer_thread = std::jthread([this](std::stop_token stop) { this->DemuxerThread(stop); });
m_video_decoder_thread =
std::jthread([this](std::stop_token stop) { this->VideoDecoderThread(stop); });
m_audio_decoder_thread =
std::jthread([this](std::stop_token stop) { this->AudioDecoderThread(stop); });
m_start_time = std::chrono::high_resolution_clock::now();
return 0;
}
bool AvPlayerSource::Stop() {
std::unique_lock lock(m_state_mutex);
if (!HasRunningThreads()) {
LOG_WARNING(Lib_AvPlayer, "Could not stop playback: already stopped.");
return false;
}
m_video_decoder_thread.request_stop();
m_audio_decoder_thread.request_stop();
m_demuxer_thread.request_stop();
if (m_demuxer_thread.joinable()) {
m_demuxer_thread.join();
}
if (m_video_decoder_thread.joinable()) {
m_video_decoder_thread.join();
}
if (m_audio_decoder_thread.joinable()) {
m_audio_decoder_thread.join();
}
if (m_current_audio_frame.has_value()) {
m_audio_buffers.Push(std::move(m_current_audio_frame.value()));
m_current_audio_frame.reset();
}
if (m_current_video_frame.has_value()) {
m_video_buffers.Push(std::move(m_current_video_frame.value()));
m_current_video_frame.reset();
}
m_stop_cv.Notify();
m_audio_packets.Clear();
m_video_packets.Clear();
m_audio_frames.Clear();
m_video_frames.Clear();
return true;
}
bool AvPlayerSource::GetVideoData(SceAvPlayerFrameInfo& video_info) {
if (!IsActive()) {
return false;
}
SceAvPlayerFrameInfoEx info{};
if (!GetVideoData(info)) {
return false;
}
video_info = {};
video_info.timestamp = u64(info.timestamp);
video_info.pData = reinterpret_cast<u8*>(info.pData);
video_info.details.video.aspect_ratio = info.details.video.aspect_ratio;
video_info.details.video.width = info.details.video.width;
video_info.details.video.height = info.details.video.height;
return true;
}
static void CopyNV12Data(u8* dst, const AVFrame& src) {
std::memcpy(dst, src.data[0], src.width * src.height);
std::memcpy(dst + src.width * src.height, src.data[1], (src.width * src.height) / 2);
}
bool AvPlayerSource::GetVideoData(SceAvPlayerFrameInfoEx& video_info) {
if (!IsActive()) {
return false;
}
m_video_frames_cv.Wait([this] { return m_video_frames.Size() != 0 || m_is_eof; });
auto frame = m_video_frames.Pop();
if (!frame.has_value()) {
LOG_WARNING(Lib_AvPlayer, "Could get video frame. EOF reached.");
return false;
}
{
using namespace std::chrono;
auto elapsed_time =
duration_cast<milliseconds>(high_resolution_clock::now() - m_start_time).count();
if (elapsed_time < frame->info.timestamp) {
if (m_stop_cv.WaitFor(milliseconds(frame->info.timestamp - elapsed_time),
[&] { return elapsed_time >= frame->info.timestamp; })) {
return false;
}
}
}
// return the buffer to the queue
if (m_current_video_frame.has_value()) {
m_video_buffers.Push(std::move(m_current_video_frame.value()));
m_video_buffers_cv.Notify();
}
m_current_video_frame = std::move(frame->buffer);
video_info = frame->info;
return true;
}
bool AvPlayerSource::GetAudioData(SceAvPlayerFrameInfo& audio_info) {
if (!IsActive()) {
return false;
}
m_audio_frames_cv.Wait([this] { return m_audio_frames.Size() != 0 || m_is_eof; });
auto frame = m_audio_frames.Pop();
if (!frame.has_value()) {
LOG_WARNING(Lib_AvPlayer, "Could get audio frame. EOF reached.");
return false;
}
{
using namespace std::chrono;
auto elapsed_time =
duration_cast<milliseconds>(high_resolution_clock::now() - m_start_time).count();
if (elapsed_time < frame->info.timestamp) {
if (m_stop_cv.WaitFor(milliseconds(frame->info.timestamp - elapsed_time),
[&] { return elapsed_time >= frame->info.timestamp; })) {
return false;
}
}
}
// return the buffer to the queue
if (m_current_audio_frame.has_value()) {
m_audio_buffers.Push(std::move(m_current_audio_frame.value()));
m_audio_buffers_cv.Notify();
}
m_current_audio_frame = std::move(frame->buffer);
audio_info = {};
audio_info.timestamp = frame->info.timestamp;
audio_info.pData = reinterpret_cast<u8*>(frame->info.pData);
audio_info.details.audio.size = frame->info.details.audio.size;
audio_info.details.audio.channel_count = frame->info.details.audio.channel_count;
return true;
}
u64 AvPlayerSource::CurrentTime() {
using namespace std::chrono;
return duration_cast<milliseconds>(high_resolution_clock::now() - m_start_time).count();
}
bool AvPlayerSource::IsActive() {
return !m_is_eof || m_audio_packets.Size() != 0 || m_video_packets.Size() != 0 ||
m_video_frames.Size() != 0 || m_audio_frames.Size() != 0;
}
void AvPlayerSource::ReleaseAVPacket(AVPacket* packet) {
if (packet != nullptr) {
av_packet_free(&packet);
}
}
void AvPlayerSource::ReleaseAVFrame(AVFrame* frame) {
if (frame != nullptr) {
av_frame_free(&frame);
}
}
void AvPlayerSource::ReleaseAVCodecContext(AVCodecContext* context) {
if (context != nullptr) {
avcodec_free_context(&context);
}
}
void AvPlayerSource::ReleaseSWRContext(SwrContext* context) {
if (context != nullptr) {
swr_free(&context);
}
}
void AvPlayerSource::ReleaseSWSContext(SwsContext* context) {
if (context != nullptr) {
sws_freeContext(context);
}
}
void AvPlayerSource::ReleaseAVFormatContext(AVFormatContext* context) {
if (context != nullptr) {
avformat_close_input(&context);
}
}
void AvPlayerSource::DemuxerThread(std::stop_token stop) {
using namespace std::chrono;
if (!m_audio_stream_index.has_value() && !m_video_stream_index.has_value()) {
LOG_WARNING(Lib_AvPlayer, "Could not start DEMUXER thread. No streams enabled.");
return;
}
LOG_INFO(Lib_AvPlayer, "Demuxer Thread started");
while (!stop.stop_requested()) {
if (m_video_packets.Size() > 30 && m_audio_packets.Size() > 8) {
std::this_thread::sleep_for(milliseconds(5));
continue;
}
AVPacketPtr up_packet(av_packet_alloc(), &ReleaseAVPacket);
const auto res = av_read_frame(m_avformat_context.get(), up_packet.get());
if (res < 0) {
if (res == AVERROR_EOF) {
if (m_is_looping) {
LOG_INFO(Lib_AvPlayer, "EOF reached in demuxer. Looping the source...");
avio_seek(m_avformat_context->pb, 0, SEEK_SET);
if (m_video_stream_index.has_value()) {
const auto index = m_video_stream_index.value();
const auto stream = m_avformat_context->streams[index];
avformat_seek_file(m_avformat_context.get(), index, 0, 0, stream->duration,
0);
}
if (m_audio_stream_index.has_value()) {
const auto index = m_audio_stream_index.value();
const auto stream = m_avformat_context->streams[index];
avformat_seek_file(m_avformat_context.get(), index, 0, 0, stream->duration,
0);
}
continue;
} else {
LOG_INFO(Lib_AvPlayer, "EOF reached in demuxer. Exiting.");
break;
}
} else {
LOG_ERROR(Lib_AvPlayer, "Could not read AV frame: error = {}", res);
m_state.OnError();
return;
}
break;
}
if (up_packet->stream_index == m_video_stream_index) {
m_video_packets.Push(std::move(up_packet));
m_video_packets_cv.Notify();
} else if (up_packet->stream_index == m_audio_stream_index) {
m_audio_packets.Push(std::move(up_packet));
m_audio_packets_cv.Notify();
}
}
m_is_eof = true;
m_video_packets_cv.Notify();
m_audio_packets_cv.Notify();
m_video_frames_cv.Notify();
m_audio_frames_cv.Notify();
if (m_video_decoder_thread.joinable()) {
m_video_decoder_thread.join();
}
if (m_audio_decoder_thread.joinable()) {
m_audio_decoder_thread.join();
}
m_state.OnEOF();
LOG_INFO(Lib_AvPlayer, "Demuxer Thread exited normaly");
}
AvPlayerSource::AVFramePtr AvPlayerSource::ConvertVideoFrame(const AVFrame& frame) {
auto nv12_frame = AVFramePtr{av_frame_alloc(), &ReleaseAVFrame};
nv12_frame->pts = frame.pts;
nv12_frame->pkt_dts = frame.pkt_dts < 0 ? 0 : frame.pkt_dts;
nv12_frame->format = AV_PIX_FMT_NV12;
nv12_frame->width = frame.width;
nv12_frame->height = frame.height;
nv12_frame->sample_aspect_ratio = frame.sample_aspect_ratio;
av_frame_get_buffer(nv12_frame.get(), 0);
if (m_sws_context == nullptr) {
m_sws_context =
SWSContextPtr(sws_getContext(frame.width, frame.height, AVPixelFormat(frame.format),
frame.width, frame.height, AV_PIX_FMT_NV12,
SWS_FAST_BILINEAR, nullptr, nullptr, nullptr),
&ReleaseSWSContext);
}
const auto res = sws_scale(m_sws_context.get(), frame.data, frame.linesize, 0, frame.height,
nv12_frame->data, nv12_frame->linesize);
if (res < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not convert to NV12: {}", av_err2str(res));
return AVFramePtr{nullptr, &ReleaseAVFrame};
}
return nv12_frame;
}
Frame AvPlayerSource::PrepareVideoFrame(FrameBuffer buffer, const AVFrame& frame) {
ASSERT(frame.format == AV_PIX_FMT_NV12);
auto p_buffer = buffer.GetBuffer();
CopyNV12Data(p_buffer, frame);
const auto pkt_dts = u64(frame.pkt_dts) * 1000;
const auto stream = m_avformat_context->streams[m_video_stream_index.value()];
const auto time_base = stream->time_base;
const auto den = time_base.den;
const auto num = time_base.num;
const auto timestamp = (num != 0 && den > 1) ? (pkt_dts * num) / den : pkt_dts;
return Frame{
.buffer = std::move(buffer),
.info =
{
.pData = p_buffer,
.timestamp = timestamp,
.details =
{
.video =
{
.width = u32(frame.width),
.height = u32(frame.height),
.aspect_ratio = AVRationalToF32(frame.sample_aspect_ratio),
.pitch = u32(frame.linesize[0]),
.luma_bit_depth = 8,
.chroma_bit_depth = 8,
},
},
},
};
}
void AvPlayerSource::VideoDecoderThread(std::stop_token stop) {
using namespace std::chrono;
LOG_INFO(Lib_AvPlayer, "Video Decoder Thread started");
while ((!m_is_eof || m_video_packets.Size() != 0) && !stop.stop_requested()) {
if (!m_video_packets_cv.Wait(stop,
[this] { return m_video_packets.Size() != 0 || m_is_eof; })) {
continue;
}
const auto packet = m_video_packets.Pop();
if (!packet.has_value()) {
continue;
}
auto res = avcodec_send_packet(m_video_codec_context.get(), packet->get());
if (res < 0 && res != AVERROR(EAGAIN)) {
m_state.OnError();
LOG_ERROR(Lib_AvPlayer, "Could not send packet to the video codec. Error = {}",
av_err2str(res));
return;
}
while (res >= 0) {
if (!m_video_buffers_cv.Wait(stop, [this] { return m_video_buffers.Size() != 0; })) {
break;
}
if (m_video_buffers.Size() == 0) {
continue;
}
auto up_frame = AVFramePtr(av_frame_alloc(), &ReleaseAVFrame);
res = avcodec_receive_frame(m_video_codec_context.get(), up_frame.get());
if (res < 0) {
if (res == AVERROR_EOF) {
LOG_INFO(Lib_AvPlayer, "EOF reached in video decoder");
return;
} else if (res != AVERROR(EAGAIN)) {
LOG_ERROR(Lib_AvPlayer,
"Could not receive frame from the video codec. Error = {}",
av_err2str(res));
m_state.OnError();
return;
}
} else {
auto buffer = m_video_buffers.Pop();
if (!buffer.has_value()) {
// Video buffers queue was cleared. This means that player was stopped.
break;
}
if (up_frame->format != AV_PIX_FMT_NV12) {
const auto nv12_frame = ConvertVideoFrame(*up_frame);
m_video_frames.Push(PrepareVideoFrame(std::move(buffer.value()), *nv12_frame));
} else {
m_video_frames.Push(PrepareVideoFrame(std::move(buffer.value()), *up_frame));
}
m_video_frames_cv.Notify();
}
}
}
LOG_INFO(Lib_AvPlayer, "Video Decoder Thread exited normaly");
}
AvPlayerSource::AVFramePtr AvPlayerSource::ConvertAudioFrame(const AVFrame& frame) {
auto pcm16_frame = AVFramePtr{av_frame_alloc(), &ReleaseAVFrame};
pcm16_frame->pts = frame.pts;
pcm16_frame->pkt_dts = frame.pkt_dts < 0 ? 0 : frame.pkt_dts;
pcm16_frame->format = AV_SAMPLE_FMT_S16;
pcm16_frame->ch_layout = frame.ch_layout;
pcm16_frame->sample_rate = frame.sample_rate;
if (m_swr_context == nullptr) {
SwrContext* swr_context = nullptr;
AVChannelLayout in_ch_layout = frame.ch_layout;
AVChannelLayout out_ch_layout = frame.ch_layout;
swr_alloc_set_opts2(&swr_context, &out_ch_layout, AV_SAMPLE_FMT_S16, frame.sample_rate,
&in_ch_layout, AVSampleFormat(frame.format), frame.sample_rate, 0,
nullptr);
m_swr_context = SWRContextPtr(swr_context, &ReleaseSWRContext);
swr_init(m_swr_context.get());
}
const auto res = swr_convert_frame(m_swr_context.get(), pcm16_frame.get(), &frame);
if (res < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not convert to NV12: {}", av_err2str(res));
return AVFramePtr{nullptr, &ReleaseAVFrame};
}
return pcm16_frame;
}
Frame AvPlayerSource::PrepareAudioFrame(FrameBuffer buffer, const AVFrame& frame) {
ASSERT(frame.format == AV_SAMPLE_FMT_S16);
ASSERT(frame.nb_samples <= 1024);
auto p_buffer = buffer.GetBuffer();
const auto size = frame.ch_layout.nb_channels * frame.nb_samples * sizeof(u16);
std::memcpy(p_buffer, frame.data[0], size);
const auto pkt_dts = u64(frame.pkt_dts) * 1000;
const auto stream = m_avformat_context->streams[m_audio_stream_index.value()];
const auto time_base = stream->time_base;
const auto den = time_base.den;
const auto num = time_base.num;
const auto timestamp = (num != 0 && den > 1) ? (pkt_dts * num) / den : pkt_dts;
return Frame{
.buffer = std::move(buffer),
.info =
{
.pData = p_buffer,
.timestamp = timestamp,
.details =
{
.audio =
{
.channel_count = u16(frame.ch_layout.nb_channels),
.size = u32(size),
},
},
},
};
}
void AvPlayerSource::AudioDecoderThread(std::stop_token stop) {
using namespace std::chrono;
LOG_INFO(Lib_AvPlayer, "Audio Decoder Thread started");
while ((!m_is_eof || m_audio_packets.Size() != 0) && !stop.stop_requested()) {
if (!m_audio_packets_cv.Wait(stop,
[this] { return m_audio_packets.Size() != 0 || m_is_eof; })) {
continue;
}
const auto packet = m_audio_packets.Pop();
if (!packet.has_value()) {
continue;
}
auto res = avcodec_send_packet(m_audio_codec_context.get(), packet->get());
if (res < 0 && res != AVERROR(EAGAIN)) {
m_state.OnError();
LOG_ERROR(Lib_AvPlayer, "Could not send packet to the audio codec. Error = {}",
av_err2str(res));
return;
}
while (res >= 0) {
if (!m_audio_buffers_cv.Wait(stop, [this] { return m_audio_buffers.Size() != 0; })) {
break;
}
if (m_audio_buffers.Size() == 0) {
continue;
}
auto up_frame = AVFramePtr(av_frame_alloc(), &ReleaseAVFrame);
res = avcodec_receive_frame(m_audio_codec_context.get(), up_frame.get());
if (res < 0) {
if (res == AVERROR_EOF) {
LOG_INFO(Lib_AvPlayer, "EOF reached in audio decoder");
return;
} else if (res != AVERROR(EAGAIN)) {
m_state.OnError();
LOG_ERROR(Lib_AvPlayer,
"Could not receive frame from the audio codec. Error = {}",
av_err2str(res));
return;
}
} else {
auto buffer = m_audio_buffers.Pop();
if (!buffer.has_value()) {
// Audio buffers queue was cleared. This means that player was stopped.
break;
}
if (up_frame->format != AV_SAMPLE_FMT_S16) {
const auto pcm16_frame = ConvertAudioFrame(*up_frame);
m_audio_frames.Push(PrepareAudioFrame(std::move(buffer.value()), *pcm16_frame));
} else {
m_audio_frames.Push(PrepareAudioFrame(std::move(buffer.value()), *up_frame));
}
m_audio_frames_cv.Notify();
}
}
}
LOG_INFO(Lib_AvPlayer, "Audio Decoder Thread exited normaly");
}
bool AvPlayerSource::HasRunningThreads() const {
return m_demuxer_thread.joinable() || m_video_decoder_thread.joinable() ||
m_audio_decoder_thread.joinable();
}
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,219 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "avplayer_common.h"
#include "avplayer_data_streamer.h"
#include "common/polyfill_thread.h"
#include "common/types.h"
#include "core/libraries/kernel/thread_management.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <optional>
#include <string>
struct AVCodecContext;
struct AVFormatContext;
struct AVFrame;
struct AVIOContext;
struct AVPacket;
struct SwrContext;
struct SwsContext;
namespace Libraries::AvPlayer {
class AvPlayerStateCallback {
public:
virtual ~AvPlayerStateCallback() = default;
virtual void OnWarning(u32 id) = 0;
virtual void OnError() = 0;
virtual void OnEOF() = 0;
};
class FrameBuffer {
public:
FrameBuffer(const SceAvPlayerMemAllocator& memory_replacement, u32 align, u32 size) noexcept
: m_memory_replacement(memory_replacement),
m_data(Allocate(memory_replacement, align, size)) {
ASSERT_MSG(m_data, "Could not allocated frame buffer.");
}
~FrameBuffer() {
if (m_data != nullptr) {
Deallocate(m_memory_replacement, m_data);
m_data = {};
}
}
FrameBuffer(const FrameBuffer&) noexcept = delete;
FrameBuffer& operator=(const FrameBuffer&) noexcept = delete;
FrameBuffer(FrameBuffer&& r) noexcept
: m_memory_replacement(r.m_memory_replacement), m_data(r.m_data) {
r.m_data = nullptr;
};
FrameBuffer& operator=(FrameBuffer&& r) noexcept {
std::swap(m_data, r.m_data);
return *this;
}
u8* GetBuffer() const noexcept {
return m_data;
}
private:
static u8* Allocate(const SceAvPlayerMemAllocator& memory_replacement, u32 align, u32 size) {
return reinterpret_cast<u8*>(
memory_replacement.allocate(memory_replacement.object_ptr, align, size));
}
static void Deallocate(const SceAvPlayerMemAllocator& memory_replacement, void* ptr) {
memory_replacement.deallocate(memory_replacement.object_ptr, ptr);
}
const SceAvPlayerMemAllocator& m_memory_replacement;
u8* m_data = nullptr;
};
struct Frame {
FrameBuffer buffer;
SceAvPlayerFrameInfoEx info;
};
class EventCV {
public:
template <class Pred>
void Wait(Pred pred) {
std::unique_lock lock(m_mutex);
m_cv.wait(lock, std::move(pred));
}
template <class Pred>
bool Wait(std::stop_token stop, Pred pred) {
std::unique_lock lock(m_mutex);
return m_cv.wait(lock, std::move(stop), std::move(pred));
}
template <class Pred, class Rep, class Period>
bool WaitFor(std::chrono::duration<Rep, Period> timeout, Pred pred) {
std::unique_lock lock(m_mutex);
return m_cv.wait_for(lock, timeout, std::move(pred));
}
void Notify() {
std::unique_lock lock(m_mutex);
m_cv.notify_all();
}
private:
std::mutex m_mutex{};
std::condition_variable_any m_cv{};
};
class AvPlayerSource {
public:
AvPlayerSource(AvPlayerStateCallback& state, std::string_view path,
const SceAvPlayerInitData& init_data, SceAvPlayerSourceType source_type);
~AvPlayerSource();
bool FindStreamInfo();
s32 GetStreamCount();
s32 GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info);
bool EnableStream(u32 stream_index);
void SetLooping(bool is_looping);
std::optional<bool> HasFrames(u32 num_frames);
s32 Start();
bool Stop();
bool GetAudioData(SceAvPlayerFrameInfo& audio_info);
bool GetVideoData(SceAvPlayerFrameInfo& video_info);
bool GetVideoData(SceAvPlayerFrameInfoEx& video_info);
u64 CurrentTime();
bool IsActive();
private:
using ScePthread = Kernel::ScePthread;
static void ReleaseAVPacket(AVPacket* packet);
static void ReleaseAVFrame(AVFrame* frame);
static void ReleaseAVCodecContext(AVCodecContext* context);
static void ReleaseSWRContext(SwrContext* context);
static void ReleaseSWSContext(SwsContext* context);
static void ReleaseAVFormatContext(AVFormatContext* context);
using AVPacketPtr = std::unique_ptr<AVPacket, decltype(&ReleaseAVPacket)>;
using AVFramePtr = std::unique_ptr<AVFrame, decltype(&ReleaseAVFrame)>;
using AVCodecContextPtr = std::unique_ptr<AVCodecContext, decltype(&ReleaseAVCodecContext)>;
using SWRContextPtr = std::unique_ptr<SwrContext, decltype(&ReleaseSWRContext)>;
using SWSContextPtr = std::unique_ptr<SwsContext, decltype(&ReleaseSWSContext)>;
using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(&ReleaseAVFormatContext)>;
void DemuxerThread(std::stop_token stop);
void VideoDecoderThread(std::stop_token stop);
void AudioDecoderThread(std::stop_token stop);
bool HasRunningThreads() const;
AVFramePtr ConvertAudioFrame(const AVFrame& frame);
AVFramePtr ConvertVideoFrame(const AVFrame& frame);
Frame PrepareAudioFrame(FrameBuffer buffer, const AVFrame& frame);
Frame PrepareVideoFrame(FrameBuffer buffer, const AVFrame& frame);
AvPlayerStateCallback& m_state;
SceAvPlayerMemAllocator m_memory_replacement{};
u32 m_num_output_video_framebuffers{};
std::atomic_bool m_is_looping = false;
std::atomic_bool m_is_eof = false;
std::unique_ptr<IDataStreamer> m_up_data_streamer;
AvPlayerQueue<FrameBuffer> m_audio_buffers;
AvPlayerQueue<FrameBuffer> m_video_buffers;
AvPlayerQueue<AVPacketPtr> m_audio_packets;
AvPlayerQueue<AVPacketPtr> m_video_packets;
AvPlayerQueue<Frame> m_audio_frames;
AvPlayerQueue<Frame> m_video_frames;
std::optional<FrameBuffer> m_current_video_frame;
std::optional<FrameBuffer> m_current_audio_frame;
std::optional<s32> m_video_stream_index{};
std::optional<s32> m_audio_stream_index{};
EventCV m_audio_packets_cv{};
EventCV m_audio_frames_cv{};
EventCV m_audio_buffers_cv{};
EventCV m_video_packets_cv{};
EventCV m_video_frames_cv{};
EventCV m_video_buffers_cv{};
EventCV m_stop_cv{};
std::mutex m_state_mutex{};
std::jthread m_demuxer_thread{};
std::jthread m_video_decoder_thread{};
std::jthread m_audio_decoder_thread{};
AVFormatContextPtr m_avformat_context{nullptr, &ReleaseAVFormatContext};
AVCodecContextPtr m_video_codec_context{nullptr, &ReleaseAVCodecContext};
AVCodecContextPtr m_audio_codec_context{nullptr, &ReleaseAVCodecContext};
SWRContextPtr m_swr_context{nullptr, &ReleaseSWRContext};
SWSContextPtr m_sws_context{nullptr, &ReleaseSWSContext};
std::chrono::high_resolution_clock::time_point m_start_time{};
};
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,493 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "avplayer_file_streamer.h"
#include "avplayer_source.h"
#include "avplayer_state.h"
#include "core/libraries/error_codes.h"
#include "core/libraries/kernel/time_management.h"
#include <magic_enum.hpp>
namespace Libraries::AvPlayer {
using namespace Kernel;
void PS4_SYSV_ABI AvPlayerState::AutoPlayEventCallback(void* opaque, s32 event_id, s32 source_id,
void* event_data) {
auto const self = reinterpret_cast<AvPlayerState*>(opaque);
if (event_id == SCE_AVPLAYER_STATE_READY) {
s32 video_stream_index = -1;
s32 audio_stream_index = -1;
s32 timedtext_stream_index = -1;
const s32 stream_count = self->GetStreamCount();
if (AVPLAYER_IS_ERROR(stream_count)) {
return;
}
if (stream_count == 0) {
self->Stop();
return;
}
for (u32 stream_index = 0; stream_index < stream_count; ++stream_index) {
SceAvPlayerStreamInfo info{};
self->GetStreamInfo(stream_index, info);
const std::string_view default_language(
reinterpret_cast<char*>(self->m_default_language));
switch (info.type) {
case SCE_AVPLAYER_VIDEO:
if (video_stream_index == -1) {
video_stream_index = stream_index;
}
if (!default_language.empty() &&
default_language == reinterpret_cast<char*>(info.details.video.language_code)) {
video_stream_index = stream_index;
}
break;
case SCE_AVPLAYER_AUDIO:
if (audio_stream_index == -1) {
audio_stream_index = stream_index;
}
if (!default_language.empty() &&
default_language == reinterpret_cast<char*>(info.details.video.language_code)) {
audio_stream_index = stream_index;
}
break;
case SCE_AVPLAYER_TIMEDTEXT:
if (default_language.empty()) {
timedtext_stream_index = stream_index;
break;
}
if (default_language == reinterpret_cast<char*>(info.details.video.language_code)) {
timedtext_stream_index = stream_index;
}
break;
}
}
if (video_stream_index != -1) {
self->EnableStream(video_stream_index);
}
if (audio_stream_index != -1) {
self->EnableStream(audio_stream_index);
}
if (timedtext_stream_index != -1) {
self->EnableStream(timedtext_stream_index);
}
self->Start();
return;
}
// Pass other events to the game
const auto callback = self->m_event_replacement.event_callback;
const auto ptr = self->m_event_replacement.object_ptr;
if (callback != nullptr) {
callback(ptr, event_id, 0, event_data);
}
}
// Called inside GAME thread
AvPlayerState::AvPlayerState(const SceAvPlayerInitData& init_data)
: m_init_data(init_data), m_event_replacement(init_data.event_replacement) {
if (m_event_replacement.event_callback == nullptr || init_data.auto_start) {
m_auto_start = true;
m_init_data.event_replacement.event_callback = &AvPlayerState::AutoPlayEventCallback;
m_init_data.event_replacement.object_ptr = this;
}
if (init_data.default_language != nullptr) {
std::memcpy(m_default_language, init_data.default_language, sizeof(m_default_language));
}
SetState(AvState::Initial);
StartControllerThread();
}
AvPlayerState::~AvPlayerState() {
{
std::unique_lock lock(m_source_mutex);
m_up_source.reset();
}
if (m_controller_thread.joinable()) {
m_controller_thread.request_stop();
m_controller_thread.join();
}
m_event_queue.Clear();
}
// Called inside GAME thread
s32 AvPlayerState::AddSource(std::string_view path, SceAvPlayerSourceType source_type) {
if (path.empty()) {
LOG_ERROR(Lib_AvPlayer, "File path is empty.");
return -1;
}
{
std::unique_lock lock(m_source_mutex);
if (m_up_source != nullptr) {
LOG_ERROR(Lib_AvPlayer, "Only one source is supported.");
return -1;
}
m_up_source = std::make_unique<AvPlayerSource>(*this, path, m_init_data, source_type);
}
AddSourceEvent();
return 0;
}
// Called inside GAME thread
s32 AvPlayerState::GetStreamCount() {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not get stream count. No source.");
return -1;
}
return m_up_source->GetStreamCount();
}
// Called inside GAME thread
s32 AvPlayerState::GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not get stream {} info. No source.", stream_index);
return -1;
}
return m_up_source->GetStreamInfo(stream_index, info);
}
// Called inside GAME thread
s32 AvPlayerState::Start() {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr || m_up_source->Start() < 0) {
LOG_ERROR(Lib_AvPlayer, "Could not start playback.");
return -1;
}
SetState(AvState::Play);
OnPlaybackStateChanged(AvState::Play);
return 0;
}
void AvPlayerState::AvControllerThread(std::stop_token stop) {
using std::chrono::milliseconds;
while (!stop.stop_requested()) {
if (m_event_queue.Size() != 0) {
ProcessEvent();
continue;
}
std::this_thread::sleep_for(milliseconds(5));
UpdateBufferingState();
}
}
// Called inside GAME thread
void AvPlayerState::AddSourceEvent() {
SetState(AvState::AddingSource);
m_event_queue.Push(AvPlayerEvent{
.event = AvEventType::AddSource,
});
}
void AvPlayerState::WarningEvent(s32 id) {
m_event_queue.Push(AvPlayerEvent{
.event = AvEventType::WarningId,
.payload =
{
.error = id,
},
});
}
// Called inside GAME thread
void AvPlayerState::StartControllerThread() {
m_controller_thread =
std::jthread([this](std::stop_token stop) { this->AvControllerThread(stop); });
}
// Called inside GAME thread
bool AvPlayerState::EnableStream(u32 stream_index) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
return false;
}
return m_up_source->EnableStream(stream_index);
}
// Called inside GAME thread
bool AvPlayerState::Stop() {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr || m_current_state == AvState::Stop) {
return false;
}
if (!SetState(AvState::Stop)) {
return false;
}
OnPlaybackStateChanged(AvState::Stop);
return m_up_source->Stop();
}
bool AvPlayerState::GetVideoData(SceAvPlayerFrameInfo& video_info) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
return false;
}
return m_up_source->GetVideoData(video_info);
}
bool AvPlayerState::GetVideoData(SceAvPlayerFrameInfoEx& video_info) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
return false;
}
return m_up_source->GetVideoData(video_info);
}
bool AvPlayerState::GetAudioData(SceAvPlayerFrameInfo& audio_info) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
return false;
}
return m_up_source->GetAudioData(audio_info);
}
bool AvPlayerState::IsActive() {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
return false;
}
return m_current_state != AvState::Stop && m_current_state != AvState::Error &&
m_current_state != AvState::EndOfFile && m_up_source->IsActive();
}
u64 AvPlayerState::CurrentTime() {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not get current time. No source.");
return 0;
}
return m_up_source->CurrentTime();
}
bool AvPlayerState::SetLooping(bool is_looping) {
std::shared_lock lock(m_source_mutex);
if (m_up_source == nullptr) {
LOG_ERROR(Lib_AvPlayer, "Could not set loop flag. No source.");
return false;
}
m_up_source->SetLooping(is_looping);
return true;
}
// May be called from different threads
void AvPlayerState::OnWarning(u32 id) {
// Forward to CONTROLLER thread
WarningEvent(id);
}
void AvPlayerState::OnError() {
SetState(AvState::Error);
OnPlaybackStateChanged(AvState::Error);
}
void AvPlayerState::OnEOF() {
SetState(AvState::EndOfFile);
}
// Called inside CONTROLLER thread
void AvPlayerState::OnPlaybackStateChanged(AvState state) {
switch (state) {
case AvState::Ready: {
EmitEvent(SCE_AVPLAYER_STATE_READY);
break;
}
case AvState::Play: {
EmitEvent(SCE_AVPLAYER_STATE_PLAY);
break;
}
case AvState::Stop: {
EmitEvent(SCE_AVPLAYER_STATE_STOP);
break;
}
case AvState::Pause: {
EmitEvent(SCE_AVPLAYER_STATE_PAUSE);
break;
}
case AvState::Buffering: {
EmitEvent(SCE_AVPLAYER_STATE_BUFFERING);
break;
}
default:
break;
}
}
// Called inside CONTROLLER and GAME threads
bool AvPlayerState::SetState(AvState state) {
std::lock_guard guard(m_state_machine_mutex);
if (!IsStateTransitionValid(state)) {
LOG_ERROR(Lib_AvPlayer, "Invalid state transition: {} -> {}",
magic_enum::enum_name(m_current_state.load()), magic_enum::enum_name(state));
return false;
}
m_previous_state.store(m_current_state);
m_current_state.store(state);
return true;
}
// Called inside CONTROLLER thread
std::optional<bool> AvPlayerState::OnBufferingCheckEvent(u32 num_frames) {
std::shared_lock lock(m_source_mutex);
if (!m_up_source) {
return std::nullopt;
}
return m_up_source->HasFrames(num_frames);
}
// Called inside CONTROLLER thread
void AvPlayerState::EmitEvent(SceAvPlayerEvents event_id, void* event_data) {
LOG_INFO(Lib_AvPlayer, "Sending event to the game: id = {}", magic_enum::enum_name(event_id));
const auto callback = m_init_data.event_replacement.event_callback;
if (callback) {
const auto ptr = m_init_data.event_replacement.object_ptr;
callback(ptr, event_id, 0, event_data);
}
}
// Called inside CONTROLLER thread
void AvPlayerState::ProcessEvent() {
if (m_current_state == AvState::Jump) {
return;
}
std::lock_guard guard(m_event_handler_mutex);
auto event = m_event_queue.Pop();
if (!event.has_value()) {
return;
}
switch (event->event) {
case AvEventType::WarningId: {
OnWarning(event->payload.error);
break;
}
case AvEventType::RevertState: {
SetState(m_previous_state.load());
break;
}
case AvEventType::AddSource: {
std::shared_lock lock(m_source_mutex);
if (m_up_source->FindStreamInfo()) {
SetState(AvState::Ready);
OnPlaybackStateChanged(AvState::Ready);
} else {
OnWarning(ORBIS_AVPLAYER_ERROR_NOT_SUPPORTED);
SetState(AvState::Error);
}
break;
}
case AvEventType::Error: {
OnWarning(event->payload.error);
SetState(AvState::Error);
break;
}
default:
break;
}
}
// Called inside CONTROLLER thread
void AvPlayerState::UpdateBufferingState() {
if (m_current_state == AvState::Buffering) {
const auto has_frames = OnBufferingCheckEvent(10);
if (!has_frames.has_value()) {
return;
}
if (has_frames.value()) {
const auto state =
m_previous_state >= AvState::C0x0B ? m_previous_state.load() : AvState::Play;
SetState(state);
OnPlaybackStateChanged(state);
}
} else if (m_current_state == AvState::Play) {
const auto has_frames = OnBufferingCheckEvent(0);
if (!has_frames.has_value()) {
return;
}
if (!has_frames.value()) {
SetState(AvState::Buffering);
OnPlaybackStateChanged(AvState::Buffering);
}
}
}
bool AvPlayerState::IsStateTransitionValid(AvState state) {
switch (state) {
case AvState::Play: {
switch (m_current_state.load()) {
case AvState::Stop:
case AvState::EndOfFile:
// case AvState::C0x08:
case AvState::Error:
return false;
default:
return true;
}
}
case AvState::Pause: {
switch (m_current_state.load()) {
case AvState::Stop:
case AvState::EndOfFile:
// case AvState::C0x08:
case AvState::Starting:
case AvState::Error:
return false;
default:
return true;
}
}
case AvState::Jump: {
switch (m_current_state.load()) {
case AvState::Stop:
case AvState::EndOfFile:
// case AvState::C0x08:
case AvState::TrickMode:
case AvState::Starting:
case AvState::Error:
return false;
default:
return true;
}
}
case AvState::TrickMode: {
switch (m_current_state.load()) {
case AvState::Stop:
case AvState::EndOfFile:
// case AvState::C0x08:
case AvState::Jump:
case AvState::Starting:
case AvState::Error:
return false;
default:
return true;
}
}
case AvState::Buffering: {
switch (m_current_state.load()) {
case AvState::Stop:
case AvState::EndOfFile:
case AvState::Pause:
// case AvState::C0x08:
case AvState::Starting:
case AvState::Error:
return false;
default:
return true;
}
}
default:
return true;
}
}
} // namespace Libraries::AvPlayer

View File

@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "avplayer.h"
#include "avplayer_data_streamer.h"
#include "avplayer_source.h"
#include "common/polyfill_thread.h"
#include "core/libraries/kernel/thread_management.h"
#include <memory>
#include <mutex>
#include <shared_mutex>
namespace Libraries::AvPlayer {
class Stream;
class AvDecoder;
class AvPlayerState : public AvPlayerStateCallback {
public:
AvPlayerState(const SceAvPlayerInitData& init_data);
~AvPlayerState();
s32 AddSource(std::string_view filename, SceAvPlayerSourceType source_type);
s32 GetStreamCount();
s32 GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info);
bool EnableStream(u32 stream_index);
s32 Start();
bool Stop();
bool GetAudioData(SceAvPlayerFrameInfo& audio_info);
bool GetVideoData(SceAvPlayerFrameInfo& video_info);
bool GetVideoData(SceAvPlayerFrameInfoEx& video_info);
bool IsActive();
u64 CurrentTime();
bool SetLooping(bool is_looping);
private:
using ScePthreadMutex = Kernel::ScePthreadMutex;
using ScePthread = Kernel::ScePthread;
// Event Replacement
static void PS4_SYSV_ABI AutoPlayEventCallback(void* handle, s32 event_id, s32 source_id,
void* event_data);
void OnWarning(u32 id) override;
void OnError() override;
void OnEOF() override;
void OnPlaybackStateChanged(AvState state);
std::optional<bool> OnBufferingCheckEvent(u32 num_frames);
void EmitEvent(SceAvPlayerEvents event_id, void* event_data = nullptr);
bool SetState(AvState state);
void AvControllerThread(std::stop_token stop);
void AddSourceEvent();
void WarningEvent(s32 id);
void StartControllerThread();
void ProcessEvent();
void UpdateBufferingState();
bool IsStateTransitionValid(AvState state);
std::unique_ptr<AvPlayerSource> m_up_source;
SceAvPlayerInitData m_init_data{};
SceAvPlayerEventReplacement m_event_replacement{};
bool m_auto_start{};
u8 m_default_language[4]{};
std::atomic<AvState> m_current_state;
std::atomic<AvState> m_previous_state;
u32 m_thread_priority;
u32 m_thread_affinity;
std::atomic_uint32_t m_some_event_result{};
std::shared_mutex m_source_mutex{};
std::mutex m_state_machine_mutex{};
std::mutex m_event_handler_mutex{};
std::jthread m_controller_thread{};
AvPlayerQueue<AvPlayerEvent> m_event_queue{};
};
} // namespace Libraries::AvPlayer

View File

@ -457,5 +457,18 @@ constexpr int ORBIS_NP_TROPHY_ERROR_HANDLE_EXCEEDS_MAX = 0x80551624;
constexpr int ORBIS_NP_TROPHY_ERROR_CONTEXT_ALREADY_EXISTS = 0x80551613; constexpr int ORBIS_NP_TROPHY_ERROR_CONTEXT_ALREADY_EXISTS = 0x80551613;
constexpr int ORBIS_NP_TROPHY_ERROR_CONTEXT_EXCEEDS_MAX = 0x80551622; constexpr int ORBIS_NP_TROPHY_ERROR_CONTEXT_EXCEEDS_MAX = 0x80551622;
// AvPlayer library
constexpr int ORBIS_AVPLAYER_ERROR_INVALID_PARAMS = 0x806A0001;
constexpr int ORBIS_AVPLAYER_ERROR_OPERATION_FAILED = 0x806A0002;
constexpr int ORBIS_AVPLAYER_ERROR_NO_MEMORY = 0x806A0003;
constexpr int ORBIS_AVPLAYER_ERROR_NOT_SUPPORTED = 0x806A0004;
constexpr int ORBIS_AVPLAYER_ERROR_WAR_FILE_NONINTERLEAVED = 0x806A00A0;
constexpr int ORBIS_AVPLAYER_ERROR_WAR_LOOPING_BACK = 0x806A00A1;
constexpr int ORBIS_AVPLAYER_ERROR_WAR_JUMP_COMPLETE = 0x806A00A3;
constexpr int ORBIS_AVPLAYER_ERROR_INFO_MARLIN_ENCRY = 0x806A00B0;
constexpr int ORBIS_AVPLAYER_ERROR_INFO_PLAYREADY_ENCRY = 0x806A00B4;
constexpr int ORBIS_AVPLAYER_ERROR_INFO_AES_ENCRY = 0x806A00B5;
constexpr int ORBIS_AVPLAYER_ERROR_INFO_OTHER_ENCRY = 0x806A00BF;
// AppContent library // AppContent library
constexpr int ORBIS_APP_CONTENT_ERROR_PARAMETER = 0x80D90002; constexpr int ORBIS_APP_CONTENT_ERROR_PARAMETER = 0x80D90002;

View File

@ -792,8 +792,8 @@ int PS4_SYSV_ABI sceSaveDataTransferringMount() {
} }
s32 PS4_SYSV_ABI sceSaveDataUmount(const OrbisSaveDataMountPoint* mountPoint) { s32 PS4_SYSV_ABI sceSaveDataUmount(const OrbisSaveDataMountPoint* mountPoint) {
LOG_INFO(Lib_SaveData, "mountPoint = {}", std::string(mountPoint->data)); LOG_INFO(Lib_SaveData, "mountPoint = {}", mountPoint->data);
if (std::string(mountPoint->data).empty()) { if (std::string_view(mountPoint->data).empty()) {
return ORBIS_SAVE_DATA_ERROR_NOT_MOUNTED; return ORBIS_SAVE_DATA_ERROR_NOT_MOUNTED;
} }
const auto& mount_dir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / const auto& mount_dir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) /