From bbdb54dccbb1c71a91fa243b0db11539957168a2 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 9 Sep 2026 14:10:45 +0900 Subject: [PATCH] =?UTF-8?q?Media=20Foundation=20=E3=81=AB=E3=82=88?= =?UTF-8?q?=E3=82=8B=E5=BE=A9=E5=8F=B7=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyEdit.vcxproj | 2 + ProxyEdit.vcxproj.filters | 6 + src/MediaDecoder.cpp | 270 ++++++++++++++++++++++++++++++++++++++ src/MediaDecoder.h | 54 ++++++++ 4 files changed, 332 insertions(+) create mode 100644 src/MediaDecoder.cpp create mode 100644 src/MediaDecoder.h diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 1d03df5..804988b 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -96,6 +96,7 @@ + @@ -106,6 +107,7 @@ + diff --git a/ProxyEdit.vcxproj.filters b/ProxyEdit.vcxproj.filters index a0e07ec..3b1faac 100644 --- a/ProxyEdit.vcxproj.filters +++ b/ProxyEdit.vcxproj.filters @@ -31,6 +31,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -54,6 +57,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/MediaDecoder.cpp b/src/MediaDecoder.cpp new file mode 100644 index 0000000..6433f34 --- /dev/null +++ b/src/MediaDecoder.cpp @@ -0,0 +1,270 @@ +#include "MediaDecoder.h" + +#include +#include +#include +#include +#include + +#include + +namespace pe { + +namespace { + +std::atomic g_started{0}; +const unsigned kFourccNv12 = 0x3231564E; +const unsigned kFourccYuy2 = 0x32595559; + +void CopyNv12(const BYTE* source, int pitch, int source_width, int source_height, + unsigned char* destination, int stride, int rows) { + const BYTE* luma = source; + const BYTE* chroma = source + (ptrdiff_t)pitch * source_height; + int usable = source_width; + if (usable * 2 > stride) usable = stride / 2; + for (int y = 0; y < rows; y++) { + const BYTE* luma_row = luma + (ptrdiff_t)y * pitch; + const BYTE* chroma_row = chroma + (ptrdiff_t)(y / 2) * pitch; + unsigned char* target = destination + (size_t)y * stride; + for (int x = 0; x + 1 < usable; x += 2) { + target[x * 2 + 0] = luma_row[x]; + target[x * 2 + 1] = chroma_row[x]; + target[x * 2 + 2] = luma_row[x + 1]; + target[x * 2 + 3] = chroma_row[x + 1]; + } + } +} + +void CopyP010(const BYTE* source, int pitch, int source_width, int source_height, + unsigned char* destination, int stride, int rows) { + const BYTE* luma = source; + const BYTE* chroma = source + (ptrdiff_t)pitch * source_height; + int usable = source_width; + if (usable * 2 > stride) usable = stride / 2; + for (int y = 0; y < rows; y++) { + const unsigned short* luma_row = (const unsigned short*)(luma + (ptrdiff_t)y * pitch); + const unsigned short* chroma_row = (const unsigned short*)(chroma + (ptrdiff_t)(y / 2) * pitch); + unsigned char* target = destination + (size_t)y * stride; + for (int x = 0; x + 1 < usable; x += 2) { + target[x * 2 + 0] = (unsigned char)(luma_row[x] >> 8); + target[x * 2 + 1] = (unsigned char)(chroma_row[x] >> 8); + target[x * 2 + 2] = (unsigned char)(luma_row[x + 1] >> 8); + target[x * 2 + 3] = (unsigned char)(chroma_row[x + 1] >> 8); + } + } +} + +void CopyYuy2(const BYTE* source, int pitch, int source_width, unsigned char* destination, int stride, + int rows) { + int bytes = source_width * 2 < stride ? source_width * 2 : stride; + for (int y = 0; y < rows; y++) { + memcpy(destination + (size_t)y * stride, source + (ptrdiff_t)y * pitch, (size_t)bytes); + } +} + +} + +bool StartMediaFoundation() { + if (g_started.load() > 0) return true; + if (FAILED(MFStartup(MF_VERSION, MFSTARTUP_LITE))) return false; + g_started.store(1); + return true; +} + +void StopMediaFoundation() { + if (g_started.exchange(0) > 0) MFShutdown(); +} + +MediaDecoder::~MediaDecoder() { + Close(); +} + +bool MediaDecoder::Open(const std::wstring& path, bool hardware) { + Close(); + if (!StartMediaFoundation()) return false; + std::lock_guard lock(mutex_); + if (hardware) { + UINT flags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT | D3D11_CREATE_DEVICE_BGRA_SUPPORT; + if (SUCCEEDED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, nullptr, 0, + D3D11_SDK_VERSION, &device_, nullptr, nullptr)) && + device_) { + ID3D10Multithread* threading = nullptr; + if (SUCCEEDED(device_->QueryInterface(IID_PPV_ARGS(&threading)))) { + threading->SetMultithreadProtected(TRUE); + threading->Release(); + } + if (FAILED(MFCreateDXGIDeviceManager(&token_, &manager_)) || + FAILED(manager_->ResetDevice(device_, token_))) { + if (manager_) { + manager_->Release(); + manager_ = nullptr; + } + } + } + } + IMFAttributes* attributes = nullptr; + if (FAILED(MFCreateAttributes(&attributes, 4))) return false; + attributes->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, TRUE); + attributes->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, hardware ? TRUE : FALSE); + if (manager_) attributes->SetUnknown(MF_SOURCE_READER_D3D_MANAGER, manager_); + HRESULT result = MFCreateSourceReaderFromURL(path.c_str(), attributes, &reader_); + attributes->Release(); + if (FAILED(result) || !reader_) return false; + + reader_->SetStreamSelection(MF_SOURCE_READER_ALL_STREAMS, FALSE); + reader_->SetStreamSelection(MF_SOURCE_READER_FIRST_VIDEO_STREAM, TRUE); + + IMFMediaType* native = nullptr; + if (SUCCEEDED(reader_->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &native)) && native) { + GUID subtype{}; + if (SUCCEEDED(native->GetGUID(MF_MT_SUBTYPE, &subtype))) { + ten_bit_ = subtype.Data1 == 0x30313050 || subtype.Data1 == 0x30313259; + } + native->Release(); + } + + IMFMediaType* wanted = nullptr; + if (FAILED(MFCreateMediaType(&wanted))) return false; + wanted->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video); + wanted->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12); + result = reader_->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, wanted); + wanted->Release(); + if (FAILED(result)) return false; + + IMFMediaType* current = nullptr; + if (SUCCEEDED(reader_->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, ¤t)) && current) { + UINT32 width = 0, height = 0, numerator = 0, denominator = 0; + MFGetAttributeSize(current, MF_MT_FRAME_SIZE, &width, &height); + MFGetAttributeRatio(current, MF_MT_FRAME_RATE, &numerator, &denominator); + GUID subtype{}; + current->GetGUID(MF_MT_SUBTYPE, &subtype); + fourcc_ = subtype.Data1; + width_ = (int)width; + height_ = (int)height; + rate_ = (int)numerator; + scale_ = (int)denominator; + current->Release(); + } + if (width_ <= 0 || height_ <= 0 || rate_ <= 0 || scale_ <= 0) return false; + + PROPVARIANT duration; + PropVariantInit(&duration); + if (SUCCEEDED(reader_->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, + &duration)) && + duration.vt == VT_UI8) { + double seconds = (double)duration.uhVal.QuadPart / 10000000.0; + frame_count_ = (long long)(seconds * (double)rate_ / (double)scale_ + 0.5); + } + PropVariantClear(&duration); + if (frame_count_ <= 0) frame_count_ = 1; + next_index_ = -1; + return true; +} + +void MediaDecoder::Close() { + std::lock_guard lock(mutex_); + if (reader_) { + reader_->Release(); + reader_ = nullptr; + } + if (manager_) { + manager_->Release(); + manager_ = nullptr; + } + if (device_) { + device_->Release(); + device_ = nullptr; + } + next_index_ = -1; +} + +bool MediaDecoder::IsOpen() const { + return reader_ != nullptr; +} + +long long MediaDecoder::FrameToTime(long long index) const { + if (rate_ <= 0) return 0; + return (long long)((double)index * (double)scale_ / (double)rate_ * 10000000.0 + 0.5); +} + +bool MediaDecoder::Seek(long long index) { + PROPVARIANT position; + PropVariantInit(&position); + position.vt = VT_I8; + position.hVal.QuadPart = FrameToTime(index); + HRESULT result = reader_->SetCurrentPosition(GUID_NULL, position); + PropVariantClear(&position); + next_index_ = index; + return SUCCEEDED(result); +} + +bool MediaDecoder::ReadFrameYuy2(long long index, unsigned char* destination, int stride, int height) { + std::lock_guard lock(mutex_); + if (!reader_) return false; + if (index != next_index_ && !Seek(index)) return false; + const long long wanted = FrameToTime(index); + const long long step = FrameToTime(1); + for (int guard = 0; guard < 900; guard++) { + DWORD stream = 0, flags = 0; + LONGLONG stamp = 0; + IMFSample* sample = nullptr; + if (FAILED(reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &stream, &flags, &stamp, + &sample))) { + return false; + } + if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { + if (sample) sample->Release(); + return false; + } + if (!sample) continue; + if (stamp + step <= wanted - step / 2) { + sample->Release(); + continue; + } + IMFMediaBuffer* buffer = nullptr; + bool done = false; + if (SUCCEEDED(sample->ConvertToContiguousBuffer(&buffer)) && buffer) { + BYTE* scan = nullptr; + LONG pitch = 0; + DWORD length = 0; + IMF2DBuffer* planar = nullptr; + bool locked = false; + if (SUCCEEDED(buffer->QueryInterface(IID_PPV_ARGS(&planar)))) { + if (SUCCEEDED(planar->Lock2D(&scan, &pitch))) locked = true; + } + if (!locked) { + DWORD maximum = 0; + if (SUCCEEDED(buffer->Lock(&scan, &maximum, &length))) { + pitch = (fourcc_ == kFourccYuy2) ? width_ * 2 : width_; + } else { + scan = nullptr; + } + } + if (length == 0) buffer->GetCurrentLength(&length); + if (scan) { + int rows = height_ < height ? height_ : height; + if (fourcc_ == kFourccYuy2) { + CopyYuy2(scan, pitch, width_, destination, stride, rows); + } else { + long long expected = (long long)(pitch < 0 ? -pitch : pitch) * height_ * 3 / 2; + if (expected > 0 && (long long)length >= expected * 7 / 4) { + CopyP010(scan, pitch, width_, height_, destination, stride, rows); + } else { + CopyNv12(scan, pitch, width_, height_, destination, stride, rows); + } + } + done = true; + if (locked) planar->Unlock2D(); + else buffer->Unlock(); + } + if (planar) planar->Release(); + buffer->Release(); + } + sample->Release(); + if (done) next_index_ = index + 1; + return done; + } + return false; +} + +} diff --git a/src/MediaDecoder.h b/src/MediaDecoder.h new file mode 100644 index 0000000..ffa6827 --- /dev/null +++ b/src/MediaDecoder.h @@ -0,0 +1,54 @@ +#pragma once + +#include + +#include +#include +#include +#include + +struct IMFSourceReader; +struct IMFDXGIDeviceManager; +struct ID3D11Device; + +namespace pe { + +bool StartMediaFoundation(); +void StopMediaFoundation(); + +class MediaDecoder { +public: + ~MediaDecoder(); + bool Open(const std::wstring& path, bool hardware); + void Close(); + bool IsOpen() const; + + int Width() const { return width_; } + int Height() const { return height_; } + int Rate() const { return rate_; } + int Scale() const { return scale_; } + long long FrameCount() const { return frame_count_; } + bool TenBit() const { return ten_bit_; } + + bool ReadFrameYuy2(long long index, unsigned char* destination, int stride, int height); + +private: + bool Seek(long long index); + long long FrameToTime(long long index) const; + + IMFSourceReader* reader_ = nullptr; + IMFDXGIDeviceManager* manager_ = nullptr; + ID3D11Device* device_ = nullptr; + unsigned token_ = 0; + int width_ = 0; + int height_ = 0; + int rate_ = 0; + int scale_ = 0; + unsigned fourcc_ = 0; + long long frame_count_ = 0; + long long next_index_ = -1; + bool ten_bit_ = false; + std::mutex mutex_; +}; + +}