+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_video_mf.c - Windows Media Foundation Hardware / Inbox Video Decoder Backend |
| 7 | */ |
| 8 | |
| 9 | #include "avx_video_codec.h" |
| 10 | #include "avx_internal.h" |
| 11 | |
| 12 | #if defined(AVX_OS_WINDOWS) |
| 13 | |
| 14 | #define COBJMACROS |
| 15 | #define INITGUID |
| 16 | #include <windows.h> |
| 17 | #include <initguid.h> |
| 18 | #include <objbase.h> |
| 19 | #include <mfapi.h> |
| 20 | #include <mfidl.h> |
| 21 | #include <mftransform.h> |
| 22 | #include <mferror.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | typedef HRESULT (WINAPI *pfn_CoInitializeEx)(LPVOID, DWORD); |
| 27 | typedef void (WINAPI *pfn_CoTaskMemFree)(LPVOID); |
| 28 | typedef HRESULT (WINAPI *pfn_MFStartup)(ULONG, DWORD); |
| 29 | typedef HRESULT (WINAPI *pfn_MFShutdown)(void); |
| 30 | typedef HRESULT (WINAPI *pfn_MFTEnumEx)(GUID, UINT32, const MFT_REGISTER_TYPE_INFO *, |
| 31 | const MFT_REGISTER_TYPE_INFO *, IMFActivate ***, UINT32 *); |
| 32 | typedef HRESULT (WINAPI *pfn_MFCreateMediaType)(IMFMediaType **); |
| 33 | typedef HRESULT (WINAPI *pfn_MFCreateSample)(IMFSample **); |
| 34 | typedef HRESULT (WINAPI *pfn_MFCreateMemoryBuffer)(DWORD, IMFMediaBuffer **); |
| 35 | |
| 36 | static avx_mutex_t s_mf_lock = AVX_MUTEX_INITIALIZER; |
| 37 | static bool s_mf_loaded = false; |
| 38 | static bool s_mf_failed = false; |
| 39 | static HMODULE s_ole32_dll = NULL; |
| 40 | static HMODULE s_mfplat_dll = NULL; |
| 41 | |
| 42 | static pfn_CoInitializeEx p_CoInitializeEx = NULL; |
| 43 | static pfn_CoTaskMemFree p_CoTaskMemFree = NULL; |
| 44 | static pfn_MFStartup p_MFStartup = NULL; |
| 45 | static pfn_MFShutdown p_MFShutdown = NULL; |
| 46 | static pfn_MFTEnumEx p_MFTEnumEx = NULL; |
| 47 | static pfn_MFCreateMediaType p_MFCreateMediaType = NULL; |
| 48 | static pfn_MFCreateSample p_MFCreateSample = NULL; |
| 49 | static pfn_MFCreateMemoryBuffer p_MFCreateMemoryBuffer = NULL; |
| 50 | |
| 51 | /* Dynamically binds Windows COM and Media Foundation runtime DLLs (ole32.dll, mfplat.dll). */ |
| 52 | static bool mf_load_runtime(void) { |
| 53 | avx_mutex_lock(&s_mf_lock); |
| 54 | if (s_mf_loaded) { |
| 55 | avx_mutex_unlock(&s_mf_lock); |
| 56 | return true; |
| 57 | } |
| 58 | if (s_mf_failed) { |
| 59 | avx_mutex_unlock(&s_mf_lock); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | s_ole32_dll = LoadLibraryW(L"ole32.dll"); |
| 64 | s_mfplat_dll = LoadLibraryW(L"mfplat.dll"); |
| 65 | if (!s_ole32_dll || !s_mfplat_dll) { |
| 66 | s_mf_failed = true; |
| 67 | avx_mutex_unlock(&s_mf_lock); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | p_CoInitializeEx = (pfn_CoInitializeEx)(void *)GetProcAddress(s_ole32_dll, "CoInitializeEx"); |
| 72 | p_CoTaskMemFree = (pfn_CoTaskMemFree)(void *)GetProcAddress(s_ole32_dll, "CoTaskMemFree"); |
| 73 | p_MFStartup = (pfn_MFStartup)(void *)GetProcAddress(s_mfplat_dll, "MFStartup"); |
| 74 | p_MFShutdown = (pfn_MFShutdown)(void *)GetProcAddress(s_mfplat_dll, "MFShutdown"); |
| 75 | p_MFTEnumEx = (pfn_MFTEnumEx)(void *)GetProcAddress(s_mfplat_dll, "MFTEnumEx"); |
| 76 | p_MFCreateMediaType = (pfn_MFCreateMediaType)(void *)GetProcAddress(s_mfplat_dll, "MFCreateMediaType"); |
| 77 | p_MFCreateSample = (pfn_MFCreateSample)(void *)GetProcAddress(s_mfplat_dll, "MFCreateSample"); |
| 78 | p_MFCreateMemoryBuffer = (pfn_MFCreateMemoryBuffer)(void *)GetProcAddress(s_mfplat_dll, "MFCreateMemoryBuffer"); |
| 79 | |
| 80 | if (!p_CoInitializeEx || !p_CoTaskMemFree || !p_MFStartup || !p_MFTEnumEx || |
| 81 | !p_MFCreateMediaType || !p_MFCreateSample || !p_MFCreateMemoryBuffer) { |
| 82 | s_mf_failed = true; |
| 83 | avx_mutex_unlock(&s_mf_lock); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | HRESULT hr = p_CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 88 | if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) { |
| 89 | s_mf_failed = true; |
| 90 | avx_mutex_unlock(&s_mf_lock); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (FAILED(p_MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET))) { |
| 95 | s_mf_failed = true; |
| 96 | avx_mutex_unlock(&s_mf_lock); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | s_mf_loaded = true; |
| 101 | avx_mutex_unlock(&s_mf_lock); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | typedef struct { |
| 106 | IMFTransform *mft; |
| 107 | IMFMediaEventGenerator *evgen; |
| 108 | bool is_async; |
| 109 | bool provider_allocs; |
| 110 | uint32_t src_fourcc; |
| 111 | uint32_t width; |
| 112 | uint32_t height; |
| 113 | uint32_t stride; |
| 114 | GUID out_subtype; |
| 115 | DWORD out_buf_size; |
| 116 | int pending_inputs; |
| 117 | uint8_t *frame_buf; |
| 118 | size_t frame_buf_size; |
| 119 | } avx_mf_dec_ctx_t; |
| 120 | |
| 121 | static bool mf_fourcc_to_guid(uint32_t fourcc, GUID *out_guid) { |
| 122 | if (fourcc == OAVX_FOURCC_VIDEO_TYPE_H264 || fourcc == 0x34363268) { |
| 123 | *out_guid = MFVideoFormat_H264; |
| 124 | return true; |
| 125 | } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC || fourcc == 0x43564568) { |
| 126 | *out_guid = MFVideoFormat_HEVC; |
| 127 | return true; |
| 128 | } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 129 | *out_guid = MFVideoFormat_AV1; |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | static int mf_select_output_type(avx_mf_dec_ctx_t *ctx) { |
| 136 | for (DWORD idx = 0; ; ++idx) { |
| 137 | IMFMediaType *mt = NULL; |
| 138 | HRESULT hr = IMFTransform_GetOutputAvailableType(ctx->mft, 0, idx, &mt); |
| 139 | if (hr == MF_E_NO_MORE_TYPES || FAILED(hr) || !mt) break; |
| 140 | |
| 141 | GUID sub = {0}; |
| 142 | if (SUCCEEDED(IMFMediaType_GetGUID(mt, &MF_MT_SUBTYPE, &sub))) { |
| 143 | if (IsEqualGUID(&sub, &MFVideoFormat_NV12) || IsEqualGUID(&sub, &MFVideoFormat_I420)) { |
| 144 | if (SUCCEEDED(IMFTransform_SetOutputType(ctx->mft, 0, mt, 0))) { |
| 145 | ctx->out_subtype = sub; |
| 146 | UINT64 fsize = 0; |
| 147 | if (SUCCEEDED(IMFMediaType_GetUINT64(mt, &MF_MT_FRAME_SIZE, &fsize))) { |
| 148 | UINT32 w = (UINT32)(fsize >> 32); |
| 149 | UINT32 h = (UINT32)(fsize & 0xFFFFFFFFU); |
| 150 | if (w > 0 && h > 0) { |
| 151 | ctx->width = w; |
| 152 | ctx->height = h; |
| 153 | } |
| 154 | } |
| 155 | UINT32 def_stride_u32 = 0; |
| 156 | if (SUCCEEDED(IMFMediaType_GetUINT32(mt, &MF_MT_DEFAULT_STRIDE, &def_stride_u32))) { |
| 157 | INT32 s_signed = (INT32)def_stride_u32; |
| 158 | UINT32 abs_s = (s_signed < 0) ? (UINT32)(-s_signed) : (UINT32)s_signed; |
| 159 | ctx->stride = (abs_s >= ctx->width) ? abs_s : ctx->width; |
| 160 | } else { |
| 161 | ctx->stride = ctx->width; |
| 162 | } |
| 163 | |
| 164 | MFT_OUTPUT_STREAM_INFO osi; |
| 165 | memset(&osi, 0, sizeof(osi)); |
| 166 | if (SUCCEEDED(IMFTransform_GetOutputStreamInfo(ctx->mft, 0, &osi))) { |
| 167 | ctx->out_buf_size = osi.cbSize; |
| 168 | ctx->provider_allocs = ((osi.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | |
| 169 | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)) != 0); |
| 170 | } |
| 171 | IMFMediaType_Release(mt); |
| 172 | return 0; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | IMFMediaType_Release(mt); |
| 177 | } |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | static bool mf_dec_supports_fourcc(uint32_t src_fourcc) { |
| 182 | GUID in_sub = {0}; |
| 183 | if (!mf_fourcc_to_guid(src_fourcc, &in_sub) || !mf_load_runtime()) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | MFT_REGISTER_TYPE_INFO in_info = { MFMediaType_Video, in_sub }; |
| 188 | IMFActivate **acts = NULL; |
| 189 | UINT32 count = 0; |
| 190 | UINT32 flags = MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SYNCMFT | |
| 191 | MFT_ENUM_FLAG_ASYNCMFT | MFT_ENUM_FLAG_SORTANDFILTER; |
| 192 | HRESULT hr = p_MFTEnumEx(MFT_CATEGORY_VIDEO_DECODER, flags, &in_info, NULL, &acts, &count); |
| 193 | if (SUCCEEDED(hr) && acts) { |
| 194 | for (UINT32 i = 0; i < count; ++i) { |
| 195 | if (acts[i]) IMFActivate_Release(acts[i]); |
| 196 | } |
| 197 | p_CoTaskMemFree(acts); |
| 198 | return count > 0; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | static void mf_dec_destroy(void *backend_ctx) { |
| 204 | avx_mf_dec_ctx_t *ctx = (avx_mf_dec_ctx_t *)backend_ctx; |
| 205 | if (!ctx) return; |
| 206 | |
| 207 | if (ctx->mft) { |
| 208 | IMFTransform_ProcessMessage(ctx->mft, MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0); |
| 209 | IMFTransform_ProcessMessage(ctx->mft, MFT_MESSAGE_COMMAND_FLUSH, 0); |
| 210 | if (ctx->evgen) { |
| 211 | IMFMediaEventGenerator_Release(ctx->evgen); |
| 212 | ctx->evgen = NULL; |
| 213 | } |
| 214 | IMFTransform_Release(ctx->mft); |
| 215 | ctx->mft = NULL; |
| 216 | } |
| 217 | free(ctx->frame_buf); |
| 218 | free(ctx); |
| 219 | } |
| 220 | |
| 221 | /* Enumerates and activates Media Foundation Transforms (MFTEnumEx), preferring hardware decoders. */ |
| 222 | static int mf_try_activate_mft(avx_mf_dec_ctx_t *ctx, const GUID *in_sub, UINT32 flags) { |
| 223 | MFT_REGISTER_TYPE_INFO in_info = { MFMediaType_Video, *in_sub }; |
| 224 | IMFActivate **acts = NULL; |
| 225 | UINT32 count = 0; |
| 226 | if (FAILED(p_MFTEnumEx(MFT_CATEGORY_VIDEO_DECODER, flags, &in_info, NULL, &acts, &count)) || !acts) { |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | int rc = -1; |
| 231 | for (UINT32 i = 0; i < count; ++i) { |
| 232 | IMFTransform *mft = NULL; |
| 233 | if (!acts[i] || FAILED(IMFActivate_ActivateObject(acts[i], &IID_IMFTransform, (void **)&mft)) || !mft) { |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | IMFAttributes *attrs = NULL; |
| 238 | bool is_async = false; |
| 239 | IMFMediaEventGenerator *evgen = NULL; |
| 240 | if (SUCCEEDED(IMFTransform_GetAttributes(mft, &attrs)) && attrs) { |
| 241 | UINT32 async_flag = 0; |
| 242 | if (SUCCEEDED(IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &async_flag)) && async_flag) { |
| 243 | is_async = true; |
| 244 | IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE); |
| 245 | } |
| 246 | IMFAttributes_SetUINT32(attrs, &MF_LOW_LATENCY, TRUE); |
| 247 | IMFAttributes_Release(attrs); |
| 248 | } |
| 249 | |
| 250 | if (is_async) { |
| 251 | if (FAILED(IMFTransform_QueryInterface(mft, &IID_IMFMediaEventGenerator, (void **)&evgen)) || !evgen) { |
| 252 | IMFTransform_Release(mft); |
| 253 | continue; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | IMFMediaType *in_mt = NULL; |
| 258 | if (FAILED(p_MFCreateMediaType(&in_mt)) || !in_mt) { |
| 259 | if (evgen) IMFMediaEventGenerator_Release(evgen); |
| 260 | IMFTransform_Release(mft); |
| 261 | continue; |
| 262 | } |
| 263 | IMFMediaType_SetGUID(in_mt, &MF_MT_MAJOR_TYPE, &MFMediaType_Video); |
| 264 | IMFMediaType_SetGUID(in_mt, &MF_MT_SUBTYPE, in_sub); |
| 265 | IMFMediaType_SetUINT32(in_mt, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive); |
| 266 | UINT64 fsize = ((UINT64)ctx->width << 32) | (UINT64)ctx->height; |
| 267 | IMFMediaType_SetUINT64(in_mt, &MF_MT_FRAME_SIZE, fsize); |
| 268 | |
| 269 | HRESULT hr = IMFTransform_SetInputType(mft, 0, in_mt, 0); |
| 270 | IMFMediaType_Release(in_mt); |
| 271 | if (FAILED(hr)) { |
| 272 | if (evgen) IMFMediaEventGenerator_Release(evgen); |
| 273 | IMFTransform_Release(mft); |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | ctx->mft = mft; |
| 278 | ctx->evgen = evgen; |
| 279 | ctx->is_async = is_async; |
| 280 | if (mf_select_output_type(ctx) != 0) { |
| 281 | ctx->mft = NULL; |
| 282 | ctx->evgen = NULL; |
| 283 | ctx->is_async = false; |
| 284 | if (evgen) IMFMediaEventGenerator_Release(evgen); |
| 285 | IMFTransform_Release(mft); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | IMFTransform_ProcessMessage(mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0); |
| 290 | IMFTransform_ProcessMessage(mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); |
| 291 | rc = 0; |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | for (UINT32 i = 0; i < count; ++i) { |
| 296 | if (acts[i]) IMFActivate_Release(acts[i]); |
| 297 | } |
| 298 | p_CoTaskMemFree(acts); |
| 299 | return rc; |
| 300 | } |
| 301 | |
| 302 | static int mf_dec_create(const avx_video_dec_cfg_t *cfg, void **backend_ctx) { |
| 303 | if (!cfg || !backend_ctx) return -1; |
| 304 | |
| 305 | GUID in_sub = {0}; |
| 306 | if (!mf_fourcc_to_guid(cfg->fourcc, &in_sub) || !mf_load_runtime()) { |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | p_CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 311 | |
| 312 | avx_mf_dec_ctx_t *ctx = (avx_mf_dec_ctx_t *)calloc(1, sizeof(*ctx)); |
| 313 | if (!ctx) return -1; |
| 314 | ctx->src_fourcc = cfg->fourcc; |
| 315 | ctx->width = cfg->width ? cfg->width : 1920; |
| 316 | ctx->height = cfg->height ? cfg->height : 1080; |
| 317 | |
| 318 | if (mf_try_activate_mft(ctx, &in_sub, |
| 319 | MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_ASYNCMFT | MFT_ENUM_FLAG_SORTANDFILTER) != 0 && |
| 320 | mf_try_activate_mft(ctx, &in_sub, |
| 321 | MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_SORTANDFILTER) != 0) { |
| 322 | mf_dec_destroy(ctx); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | *backend_ctx = ctx; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int mf_drain_async_events(avx_mf_dec_ctx_t *ctx, bool *have_output) { |
| 331 | if (!ctx->is_async || !ctx->evgen) return 0; |
| 332 | |
| 333 | for (int iter = 0; iter < 16; ++iter) { |
| 334 | IMFMediaEvent *ev = NULL; |
| 335 | HRESULT hr = IMFMediaEventGenerator_GetEvent(ctx->evgen, MF_EVENT_FLAG_NO_WAIT, &ev); |
| 336 | if (hr == MF_E_NO_EVENTS_AVAILABLE || FAILED(hr) || !ev) break; |
| 337 | |
| 338 | MediaEventType met = MEUnknown; |
| 339 | IMFMediaEvent_GetType(ev, &met); |
| 340 | IMFMediaEvent_Release(ev); |
| 341 | |
| 342 | if (met == METransformNeedInput) { |
| 343 | ctx->pending_inputs++; |
| 344 | } else if (met == METransformHaveOutput) { |
| 345 | *have_output = true; |
| 346 | } |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /* Copies decoded IMFSample picture buffer to destination frame, adjusting for stride alignment. */ |
| 352 | static int mf_copy_sample_to_frame(avx_mf_dec_ctx_t *ctx, IMFSample *sample, oavx_video_frame_t *out_frame) { |
| 353 | IMFMediaBuffer *buf = NULL; |
| 354 | if (FAILED(IMFSample_ConvertToContiguousBuffer(sample, &buf)) || !buf) { |
| 355 | return AVX_VIDEO_CODEC_ERROR; |
| 356 | } |
| 357 | |
| 358 | BYTE *src_ptr = NULL; |
| 359 | DWORD src_len = 0; |
| 360 | LONG pitch = (LONG)(ctx->stride ? ctx->stride : ctx->width); |
| 361 | IMF2DBuffer *buf2d = NULL; |
| 362 | bool locked_2d = false; |
| 363 | |
| 364 | if (SUCCEEDED(IMFMediaBuffer_QueryInterface(buf, &IID_IMF2DBuffer, (void **)&buf2d)) && buf2d) { |
| 365 | if (SUCCEEDED(IMF2DBuffer_Lock2D(buf2d, &src_ptr, &pitch))) { |
| 366 | locked_2d = true; |
| 367 | } else { |
| 368 | IMF2DBuffer_Release(buf2d); |
| 369 | buf2d = NULL; |
| 370 | } |
| 371 | } |
| 372 | if (!locked_2d && FAILED(IMFMediaBuffer_Lock(buf, &src_ptr, NULL, &src_len))) { |
| 373 | IMFMediaBuffer_Release(buf); |
| 374 | return AVX_VIDEO_CODEC_ERROR; |
| 375 | } |
| 376 | if (pitch < 0) pitch = -pitch; |
| 377 | |
| 378 | uint32_t w = ctx->width; |
| 379 | uint32_t h = ctx->height; |
| 380 | uint64_t need_u64 = (uint64_t)w * (uint64_t)h * 2ULL; |
| 381 | size_t s = (size_t)pitch; |
| 382 | if (w == 0 || h == 0 || w > AVX_MAX_VIDEO_WIDTH || h > AVX_MAX_VIDEO_HEIGHT || |
| 383 | need_u64 > AVX_MAX_DECOMPRESSED_FRAME_SIZE || s < (size_t)w) { |
| 384 | if (locked_2d) { IMF2DBuffer_Unlock2D(buf2d); IMF2DBuffer_Release(buf2d); } |
| 385 | else { IMFMediaBuffer_Unlock(buf); } |
| 386 | IMFMediaBuffer_Release(buf); |
| 387 | return AVX_VIDEO_CODEC_ERROR; |
| 388 | } |
| 389 | |
| 390 | bool is_nv12 = IsEqualGUID(&ctx->out_subtype, &MFVideoFormat_NV12); |
| 391 | uint32_t cw = (w + 1U) / 2U; |
| 392 | uint32_t ch = (h + 1U) / 2U; |
| 393 | size_t need = is_nv12 |
| 394 | ? ((size_t)w * (size_t)h + (size_t)w * (size_t)ch) |
| 395 | : ((size_t)w * (size_t)h + 2ULL * (size_t)cw * (size_t)ch); |
| 396 | |
| 397 | if (!locked_2d) { |
| 398 | size_t min_src = is_nv12 |
| 399 | ? ((size_t)h * s + (size_t)ch * s) |
| 400 | : ((size_t)h * s + 2ULL * (size_t)ch * (s / 2U)); |
| 401 | if ((size_t)src_len < min_src) { |
| 402 | IMFMediaBuffer_Unlock(buf); |
| 403 | IMFMediaBuffer_Release(buf); |
| 404 | return AVX_VIDEO_CODEC_ERROR; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (ctx->frame_buf_size < need) { |
| 409 | uint8_t *nb = (uint8_t *)realloc(ctx->frame_buf, need); |
| 410 | if (!nb) { |
| 411 | if (locked_2d) { IMF2DBuffer_Unlock2D(buf2d); IMF2DBuffer_Release(buf2d); } |
| 412 | else { IMFMediaBuffer_Unlock(buf); } |
| 413 | IMFMediaBuffer_Release(buf); |
| 414 | return AVX_VIDEO_CODEC_ERROR; |
| 415 | } |
| 416 | ctx->frame_buf = nb; |
| 417 | ctx->frame_buf_size = need; |
| 418 | } |
| 419 | |
| 420 | for (uint32_t y = 0; y < h; ++y) { |
| 421 | memcpy(ctx->frame_buf + (size_t)y * w, src_ptr + (size_t)y * s, w); |
| 422 | } |
| 423 | |
| 424 | if (is_nv12) { |
| 425 | const uint8_t *src_uv = src_ptr + (size_t)h * s; |
| 426 | uint8_t *dst_uv = ctx->frame_buf + (size_t)w * h; |
| 427 | for (uint32_t y = 0; y < ch; ++y) { |
| 428 | memcpy(dst_uv + (size_t)y * w, src_uv + (size_t)y * s, w); |
| 429 | } |
| 430 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_NV12; |
| 431 | out_frame->planes[0] = ctx->frame_buf; |
| 432 | out_frame->strides[0] = w; |
| 433 | out_frame->planes[1] = dst_uv; |
| 434 | out_frame->strides[1] = w; |
| 435 | out_frame->planes[2] = NULL; |
| 436 | out_frame->strides[2] = 0; |
| 437 | } else { |
| 438 | size_t cs = s / 2; |
| 439 | const uint8_t *src_u = src_ptr + (size_t)h * s; |
| 440 | const uint8_t *src_v = src_u + (size_t)ch * cs; |
| 441 | uint8_t *dst_u = ctx->frame_buf + (size_t)w * h; |
| 442 | uint8_t *dst_v = dst_u + (size_t)cw * ch; |
| 443 | for (uint32_t y = 0; y < ch; ++y) memcpy(dst_u + (size_t)y * cw, src_u + (size_t)y * cs, cw); |
| 444 | for (uint32_t y = 0; y < ch; ++y) memcpy(dst_v + (size_t)y * cw, src_v + (size_t)y * cs, cw); |
| 445 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_I420; |
| 446 | out_frame->planes[0] = ctx->frame_buf; |
| 447 | out_frame->strides[0] = w; |
| 448 | out_frame->planes[1] = dst_u; |
| 449 | out_frame->strides[1] = cw; |
| 450 | out_frame->planes[2] = dst_v; |
| 451 | out_frame->strides[2] = cw; |
| 452 | } |
| 453 | out_frame->width = w; |
| 454 | out_frame->height = h; |
| 455 | out_frame->planes[3] = NULL; |
| 456 | out_frame->strides[3] = 0; |
| 457 | |
| 458 | if (locked_2d) { IMF2DBuffer_Unlock2D(buf2d); IMF2DBuffer_Release(buf2d); } |
| 459 | else { IMFMediaBuffer_Unlock(buf); } |
| 460 | IMFMediaBuffer_Release(buf); |
| 461 | return AVX_VIDEO_CODEC_OK; |
| 462 | } |
| 463 | |
| 464 | /* Submits compressed packet to MFT via ProcessInput and drains decoded pictures via ProcessOutput. */ |
| 465 | static int mf_dec_decode(void *backend_ctx, |
| 466 | const uint8_t *pkt_data, size_t pkt_size, |
| 467 | oavx_video_frame_t *out_frame) |
| 468 | { |
| 469 | avx_mf_dec_ctx_t *ctx = (avx_mf_dec_ctx_t *)backend_ctx; |
| 470 | if (!ctx || !ctx->mft || !pkt_data || pkt_size == 0 || |
| 471 | pkt_size > (size_t)0xFFFFFFFFU || !out_frame) { |
| 472 | return AVX_VIDEO_CODEC_ERROR; |
| 473 | } |
| 474 | |
| 475 | p_CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 476 | |
| 477 | IMFSample *in_sample = NULL; |
| 478 | IMFMediaBuffer *in_buf = NULL; |
| 479 | if (FAILED(p_MFCreateSample(&in_sample)) || !in_sample || |
| 480 | FAILED(p_MFCreateMemoryBuffer((DWORD)pkt_size, &in_buf)) || !in_buf) { |
| 481 | if (in_sample) IMFSample_Release(in_sample); |
| 482 | if (in_buf) IMFMediaBuffer_Release(in_buf); |
| 483 | return AVX_VIDEO_CODEC_ERROR; |
| 484 | } |
| 485 | |
| 486 | BYTE *dst = NULL; |
| 487 | if (FAILED(IMFMediaBuffer_Lock(in_buf, &dst, NULL, NULL))) { |
| 488 | IMFMediaBuffer_Release(in_buf); |
| 489 | IMFSample_Release(in_sample); |
| 490 | return AVX_VIDEO_CODEC_ERROR; |
| 491 | } |
| 492 | memcpy(dst, pkt_data, pkt_size); |
| 493 | IMFMediaBuffer_Unlock(in_buf); |
| 494 | IMFMediaBuffer_SetCurrentLength(in_buf, (DWORD)pkt_size); |
| 495 | IMFSample_AddBuffer(in_sample, in_buf); |
| 496 | IMFMediaBuffer_Release(in_buf); |
| 497 | |
| 498 | bool have_output = false; |
| 499 | if (ctx->is_async) { |
| 500 | mf_drain_async_events(ctx, &have_output); |
| 501 | } |
| 502 | |
| 503 | HRESULT hr_in = IMFTransform_ProcessInput(ctx->mft, 0, in_sample, 0); |
| 504 | bool retry_input = (hr_in == MF_E_NOTACCEPTING); |
| 505 | if (FAILED(hr_in) && !retry_input) { |
| 506 | IMFSample_Release(in_sample); |
| 507 | return AVX_VIDEO_CODEC_ERROR; |
| 508 | } |
| 509 | if (!retry_input) { |
| 510 | IMFSample_Release(in_sample); |
| 511 | in_sample = NULL; |
| 512 | if (ctx->pending_inputs > 0) ctx->pending_inputs--; |
| 513 | } |
| 514 | |
| 515 | for (int attempt = 0; attempt < 12; ++attempt) { |
| 516 | if (ctx->is_async) { |
| 517 | mf_drain_async_events(ctx, &have_output); |
| 518 | if (retry_input && ctx->pending_inputs > 0) { |
| 519 | HRESULT hr_retry = IMFTransform_ProcessInput(ctx->mft, 0, in_sample, 0); |
| 520 | if (SUCCEEDED(hr_retry)) { |
| 521 | IMFSample_Release(in_sample); |
| 522 | in_sample = NULL; |
| 523 | retry_input = false; |
| 524 | ctx->pending_inputs--; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | MFT_OUTPUT_DATA_BUFFER out_data; |
| 530 | memset(&out_data, 0, sizeof(out_data)); |
| 531 | IMFSample *caller_sample = NULL; |
| 532 | if (!ctx->provider_allocs) { |
| 533 | DWORD alloc_sz = ctx->out_buf_size ? ctx->out_buf_size : (ctx->width * ctx->height * 2U); |
| 534 | IMFMediaBuffer *ob = NULL; |
| 535 | if (SUCCEEDED(p_MFCreateSample(&caller_sample)) && |
| 536 | SUCCEEDED(p_MFCreateMemoryBuffer(alloc_sz, &ob))) { |
| 537 | IMFSample_AddBuffer(caller_sample, ob); |
| 538 | IMFMediaBuffer_Release(ob); |
| 539 | out_data.pSample = caller_sample; |
| 540 | } else { |
| 541 | if (caller_sample) IMFSample_Release(caller_sample); |
| 542 | if (ob) IMFMediaBuffer_Release(ob); |
| 543 | if (in_sample) IMFSample_Release(in_sample); |
| 544 | return AVX_VIDEO_CODEC_ERROR; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | DWORD status = 0; |
| 549 | HRESULT hr_out = IMFTransform_ProcessOutput(ctx->mft, 0, 1, &out_data, &status); |
| 550 | if (out_data.pEvents) IMFCollection_Release(out_data.pEvents); |
| 551 | |
| 552 | if (hr_out == MF_E_TRANSFORM_STREAM_CHANGE) { |
| 553 | if (caller_sample) IMFSample_Release(caller_sample); |
| 554 | if (mf_select_output_type(ctx) != 0) { |
| 555 | if (in_sample) IMFSample_Release(in_sample); |
| 556 | return AVX_VIDEO_CODEC_ERROR; |
| 557 | } |
| 558 | continue; |
| 559 | } |
| 560 | if (hr_out == MF_E_TRANSFORM_NEED_MORE_INPUT) { |
| 561 | if (caller_sample) IMFSample_Release(caller_sample); |
| 562 | if (retry_input) { |
| 563 | HRESULT hr2 = IMFTransform_ProcessInput(ctx->mft, 0, in_sample, 0); |
| 564 | if (SUCCEEDED(hr2)) { |
| 565 | IMFSample_Release(in_sample); |
| 566 | in_sample = NULL; |
| 567 | retry_input = false; |
| 568 | continue; |
| 569 | } |
| 570 | if (ctx->is_async && hr2 == MF_E_NOTACCEPTING) { |
| 571 | Sleep(1); |
| 572 | continue; |
| 573 | } |
| 574 | IMFSample_Release(in_sample); |
| 575 | return AVX_VIDEO_CODEC_ERROR; |
| 576 | } |
| 577 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 578 | } |
| 579 | if (FAILED(hr_out) || !out_data.pSample) { |
| 580 | if (caller_sample) IMFSample_Release(caller_sample); |
| 581 | if (ctx->is_async && retry_input) { |
| 582 | Sleep(1); |
| 583 | continue; |
| 584 | } |
| 585 | if (in_sample) IMFSample_Release(in_sample); |
| 586 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 587 | } |
| 588 | |
| 589 | int rc = mf_copy_sample_to_frame(ctx, out_data.pSample, out_frame); |
| 590 | IMFSample_Release(out_data.pSample); |
| 591 | if (retry_input && in_sample) { |
| 592 | IMFTransform_ProcessInput(ctx->mft, 0, in_sample, 0); |
| 593 | IMFSample_Release(in_sample); |
| 594 | in_sample = NULL; |
| 595 | } |
| 596 | return rc; |
| 597 | } |
| 598 | |
| 599 | if (in_sample) IMFSample_Release(in_sample); |
| 600 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 601 | } |
| 602 | |
| 603 | static void mf_dec_reset(void *backend_ctx) { |
| 604 | avx_mf_dec_ctx_t *ctx = (avx_mf_dec_ctx_t *)backend_ctx; |
| 605 | if (!ctx || !ctx->mft) return; |
| 606 | IMFTransform_ProcessMessage(ctx->mft, MFT_MESSAGE_COMMAND_FLUSH, 0); |
| 607 | IMFTransform_ProcessMessage(ctx->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); |
| 608 | } |
| 609 | |
| 610 | static const avx_video_decoder_ops_t s_mf_decoder_ops = { |
| 611 | .name = "mediafoundation", |
| 612 | .priority = 90, |
| 613 | .is_hardware = true, |
| 614 | .supports_fourcc = mf_dec_supports_fourcc, |
| 615 | .create = mf_dec_create, |
| 616 | .decode = mf_dec_decode, |
| 617 | .reset = mf_dec_reset, |
| 618 | .destroy = mf_dec_destroy |
| 619 | }; |
| 620 | |
| 621 | const avx_video_decoder_ops_t *avx_video_mf_get_ops(void) { |
| 622 | return &s_mf_decoder_ops; |
| 623 | } |
| 624 | |
| 625 | #else |
| 626 | |
| 627 | const avx_video_decoder_ops_t *avx_video_mf_get_ops(void) { |
| 628 | return NULL; |
| 629 | } |
| 630 | |
| 631 | #endif /* AVX_OS_WINDOWS */ |