yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
2.5 KiB128 linesraw
1#include "stdafx.h"
2#include "mfStartup.h"
3#include <atlbase.h>
4#include <mfapi.h>
5#pragma comment(lib, "Mfplat.lib")
6
7namespace
8{
9	struct sCoInitStatus
10	{
11		// Possible state:
12		// -1 is the initial state, coInitialize never called
13		// S_OK - CoInitializeEx succeeded, in this state the counter tracks the count of coInitialize() for the current thread
14		// S_FALSE - CoInitializeEx failed with RPC_E_CHANGED_MODE, or did nothing because already initialized for the current thread
15		// Error status - CoInitializeEx failed for some other reason
16		HRESULT code = -1;
17		uint32_t counter = 0;
18	};
19	thread_local sCoInitStatus coInitStatus;
20
21	static HRESULT coInitialize()
22	{
23		sCoInitStatus& cis = coInitStatus;
24		HRESULT hr = cis.code;
25		if( SUCCEEDED( hr ) )
26		{
27			if( S_OK == hr )
28				cis.counter++;
29			return S_FALSE;
30		}
31
32		if( hr == HRESULT( -1 ) )
33		{
34			hr = CoInitializeEx( nullptr, COINIT_MULTITHREADED );
35			if( S_OK == hr )
36			{
37				cis.counter = 1;
38				return cis.code = S_OK;
39			}
40			if( S_FALSE == hr || RPC_E_CHANGED_MODE == hr )
41			{
42				return cis.code = S_FALSE;
43			}
44			cis.code = hr;
45			return hr;
46		}
47		
48		return hr;
49	}
50
51	static void coUninitialize()
52	{
53		sCoInitStatus& cis = coInitStatus;
54		if( cis.code == S_OK )
55		{
56			assert( cis.counter > 0 );
57			cis.counter--;
58			if( 0 == cis.counter )
59				CoUninitialize();
60		}
61	}
62
63	static CComAutoCriticalSection s_lock;
64#define LOCK() CComCritSecLock<CComAutoCriticalSection> lock{ s_lock }
65	static uint32_t mfStartupCounter = 0;
66
67	constexpr uint8_t FlagCOM = 1;
68	constexpr uint8_t FlagMF = 0x10;
69}
70
71using namespace Whisper;
72
73MfStartupRaii::~MfStartupRaii()
74{
75	if( 0 != ( successFlags & FlagMF ) )
76	{
77		LOCK();
78		assert( mfStartupCounter > 0 );
79		mfStartupCounter--;
80		if( mfStartupCounter > 0 )
81			return;
82		MFShutdown();
83		successFlags &= ~FlagMF;
84	}
85	
86	if( 0 != ( successFlags & FlagCOM ) )
87	{
88		coUninitialize();
89		successFlags &= ~FlagCOM;
90	}
91}
92
93HRESULT MfStartupRaii::startup()
94{
95	if( 0 != ( successFlags & FlagMF ) )
96		return HRESULT_FROM_WIN32( ERROR_ALREADY_INITIALIZED );
97
98	HRESULT hr = coInitialize();
99	CHECK( hr );
100	if( hr == S_OK )
101		successFlags |= FlagCOM;
102
103	LOCK();
104
105	if( 0 == mfStartupCounter )
106	{
107		HRESULT hr = MFStartup( MF_VERSION, MFSTARTUP_LITE );
108		if( SUCCEEDED( hr ) )
109		{
110			mfStartupCounter = 1;
111			successFlags |= FlagMF;
112			return S_OK;
113		}
114
115		if( 0 != ( successFlags & FlagCOM ) )
116		{
117			coUninitialize();
118			successFlags &= ~FlagCOM;
119		}
120		return hr;
121	}
122	else
123	{
124		mfStartupCounter++;
125		successFlags |= FlagMF;
126		return S_FALSE;
127	}
128}