1 // CodecExports.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../../C/CpuArch.h"
6 
7 #include "../../Common/ComTry.h"
8 #include "../../Common/MyCom.h"
9 
10 #include "../../Windows/PropVariant.h"
11 
12 #include "../ICoder.h"
13 
14 #include "../Common/RegisterCodec.h"
15 
16 extern unsigned int g_NumCodecs;
17 extern const CCodecInfo *g_Codecs[];
18 
19 extern unsigned int g_NumHashers;
20 extern const CHasherInfo *g_Hashers[];
21 
22 static const UInt16 kDecodeId = 0x2790;
23 static const UInt16 kEncodeId = 0x2791;
24 static const UInt16 kHasherId = 0x2792;
25 
26 DEFINE_GUID(CLSID_CCodec,
27 0x23170F69, 0x40C1, kDecodeId, 0, 0, 0, 0, 0, 0, 0, 0);
28 
SetPropString(const char * s,unsigned int size,PROPVARIANT * value)29 static inline HRESULT SetPropString(const char *s, unsigned int size, PROPVARIANT *value)
30 {
31   if ((value->bstrVal = ::SysAllocStringByteLen(s, size)) != 0)
32     value->vt = VT_BSTR;
33   return S_OK;
34 }
35 
SetPropGUID(const GUID & guid,PROPVARIANT * value)36 static inline HRESULT SetPropGUID(const GUID &guid, PROPVARIANT *value)
37 {
38   return SetPropString((const char *)&guid, sizeof(GUID), value);
39 }
40 
SetClassID(CMethodId id,UInt16 typeId,PROPVARIANT * value)41 static HRESULT SetClassID(CMethodId id, UInt16 typeId, PROPVARIANT *value)
42 {
43   GUID clsId;
44   clsId.Data1 = CLSID_CCodec.Data1;
45   clsId.Data2 = CLSID_CCodec.Data2;
46   clsId.Data3 = typeId;
47   SetUi64(clsId.Data4, id);
48   return SetPropGUID(clsId, value);
49 }
50 
FindCodecClassId(const GUID * clsID,UInt32 isCoder2,bool isFilter,bool & encode,int & index)51 static HRESULT FindCodecClassId(const GUID *clsID, UInt32 isCoder2, bool isFilter, bool &encode, int &index)
52 {
53   index = -1;
54   if (clsID->Data1 != CLSID_CCodec.Data1 ||
55       clsID->Data2 != CLSID_CCodec.Data2)
56     return S_OK;
57   encode = true;
58   if (clsID->Data3 == kDecodeId)
59     encode = false;
60   else if (clsID->Data3 != kEncodeId)
61     return S_OK;
62   UInt64 id = GetUi64(clsID->Data4);
63   for (unsigned i = 0; i < g_NumCodecs; i++)
64   {
65     const CCodecInfo &codec = *g_Codecs[i];
66     if (id != codec.Id || encode && !codec.CreateEncoder || !encode && !codec.CreateDecoder)
67       continue;
68     if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter ||
69         codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2)
70       return E_NOINTERFACE;
71     index = i;
72     return S_OK;
73   }
74   return S_OK;
75 }
76 
CreateCoder2(bool encode,int index,const GUID * iid,void ** outObject)77 STDAPI CreateCoder2(bool encode, int index, const GUID *iid, void **outObject)
78 {
79   COM_TRY_BEGIN
80   *outObject = 0;
81   bool isCoder = (*iid == IID_ICompressCoder) != 0;
82   bool isCoder2 = (*iid == IID_ICompressCoder2) != 0;
83   bool isFilter = (*iid == IID_ICompressFilter) != 0;
84   const CCodecInfo &codec = *g_Codecs[index];
85   if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter ||
86       codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2)
87     return E_NOINTERFACE;
88   if (encode)
89   {
90     if (!codec.CreateEncoder)
91       return CLASS_E_CLASSNOTAVAILABLE;
92     *outObject = codec.CreateEncoder();
93   }
94   else
95   {
96     if (!codec.CreateDecoder)
97       return CLASS_E_CLASSNOTAVAILABLE;
98     *outObject = codec.CreateDecoder();
99   }
100   if (*outObject)
101   {
102     if (isCoder)
103       ((ICompressCoder *)*outObject)->AddRef();
104     else if (isCoder2)
105       ((ICompressCoder2 *)*outObject)->AddRef();
106     else
107       ((ICompressFilter *)*outObject)->AddRef();
108   }
109   return S_OK;
110   COM_TRY_END
111 }
112 
CreateCoder(const GUID * clsid,const GUID * iid,void ** outObject)113 STDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject)
114 {
115   COM_TRY_BEGIN
116   *outObject = 0;
117   bool isCoder = (*iid == IID_ICompressCoder) != 0;
118   bool isCoder2 = (*iid == IID_ICompressCoder2) != 0;
119   bool isFilter = (*iid == IID_ICompressFilter) != 0;
120   if (!isCoder && !isCoder2 && !isFilter)
121     return E_NOINTERFACE;
122   bool encode;
123   int codecIndex;
124   HRESULT res = FindCodecClassId(clsid, isCoder2, isFilter, encode, codecIndex);
125   if (res != S_OK)
126     return res;
127   if (codecIndex < 0)
128     return CLASS_E_CLASSNOTAVAILABLE;
129 
130   const CCodecInfo &codec = *g_Codecs[codecIndex];
131   if (encode)
132     *outObject = codec.CreateEncoder();
133   else
134     *outObject = codec.CreateDecoder();
135   if (*outObject)
136   {
137     if (isCoder)
138       ((ICompressCoder *)*outObject)->AddRef();
139     else if (isCoder2)
140       ((ICompressCoder2 *)*outObject)->AddRef();
141     else
142       ((ICompressFilter *)*outObject)->AddRef();
143   }
144   return S_OK;
145   COM_TRY_END
146 }
147 
GetMethodProperty(UInt32 codecIndex,PROPID propID,PROPVARIANT * value)148 STDAPI GetMethodProperty(UInt32 codecIndex, PROPID propID, PROPVARIANT *value)
149 {
150   ::VariantClear((VARIANTARG *)value);
151   const CCodecInfo &codec = *g_Codecs[codecIndex];
152   switch (propID)
153   {
154     case NMethodPropID::kID:
155       value->uhVal.QuadPart = (UInt64)codec.Id;
156       value->vt = VT_UI8;
157       break;
158     case NMethodPropID::kName:
159       if ((value->bstrVal = ::SysAllocString(codec.Name)) != 0)
160         value->vt = VT_BSTR;
161       break;
162     case NMethodPropID::kDecoder:
163       if (codec.CreateDecoder)
164         return SetClassID(codec.Id, kDecodeId, value);
165       break;
166     case NMethodPropID::kEncoder:
167       if (codec.CreateEncoder)
168         return SetClassID(codec.Id, kEncodeId, value);
169       break;
170     case NMethodPropID::kInStreams:
171       if (codec.NumInStreams != 1)
172       {
173         value->vt = VT_UI4;
174         value->ulVal = (ULONG)codec.NumInStreams;
175       }
176       break;
177   }
178   return S_OK;
179 }
180 
GetNumberOfMethods(UINT32 * numCodecs)181 STDAPI GetNumberOfMethods(UINT32 *numCodecs)
182 {
183   *numCodecs = g_NumCodecs;
184   return S_OK;
185 }
186 
187 
FindHasherClassId(const GUID * clsID)188 static int FindHasherClassId(const GUID *clsID)
189 {
190   if (clsID->Data1 != CLSID_CCodec.Data1 ||
191       clsID->Data2 != CLSID_CCodec.Data2 ||
192       clsID->Data3 != kHasherId)
193     return -1;
194   UInt64 id = GetUi64(clsID->Data4);
195   for (unsigned i = 0; i < g_NumCodecs; i++)
196     if (id == g_Hashers[i]->Id)
197       return i;
198   return -1;
199 }
200 
CreateHasher2(UInt32 index,IHasher ** hasher)201 static HRESULT CreateHasher2(UInt32 index, IHasher **hasher)
202 {
203   COM_TRY_BEGIN
204   *hasher = g_Hashers[index]->CreateHasher();
205   if (*hasher)
206     (*hasher)->AddRef();
207   return S_OK;
208   COM_TRY_END
209 }
210 
CreateHasher(const GUID * clsid,IHasher ** outObject)211 STDAPI CreateHasher(const GUID *clsid, IHasher **outObject)
212 {
213   COM_TRY_BEGIN
214   *outObject = 0;
215   int index = FindHasherClassId(clsid);
216   if (index < 0)
217     return CLASS_E_CLASSNOTAVAILABLE;
218   return CreateHasher2(index, outObject);
219   COM_TRY_END
220 }
221 
GetHasherProp(UInt32 codecIndex,PROPID propID,PROPVARIANT * value)222 STDAPI GetHasherProp(UInt32 codecIndex, PROPID propID, PROPVARIANT *value)
223 {
224   ::VariantClear((VARIANTARG *)value);
225   const CHasherInfo &codec = *g_Hashers[codecIndex];
226   switch (propID)
227   {
228     case NMethodPropID::kID:
229       value->uhVal.QuadPart = (UInt64)codec.Id;
230       value->vt = VT_UI8;
231       break;
232     case NMethodPropID::kName:
233       if ((value->bstrVal = ::SysAllocString(codec.Name)) != 0)
234         value->vt = VT_BSTR;
235       break;
236     case NMethodPropID::kEncoder:
237       if (codec.CreateHasher)
238         return SetClassID(codec.Id, kHasherId, value);
239       break;
240     case NMethodPropID::kDigestSize:
241       value->ulVal = (ULONG)codec.DigestSize;
242       value->vt = VT_UI4;
243       break;
244   }
245   return S_OK;
246 }
247 
248 class CHashers:
249   public IHashers,
250   public CMyUnknownImp
251 {
252 public:
253   MY_UNKNOWN_IMP1(IHashers)
254 
255   STDMETHOD_(UInt32, GetNumHashers)();
256   STDMETHOD(GetHasherProp)(UInt32 index, PROPID propID, PROPVARIANT *value);
257   STDMETHOD(CreateHasher)(UInt32 index, IHasher **hasher);
258 };
259 
GetHashers(IHashers ** hashers)260 STDAPI GetHashers(IHashers **hashers)
261 {
262   COM_TRY_BEGIN
263   *hashers = new CHashers;
264   if (*hashers)
265     (*hashers)->AddRef();
266   return S_OK;
267   COM_TRY_END
268 }
269 
STDMETHODIMP_(UInt32)270 STDMETHODIMP_(UInt32) CHashers::GetNumHashers()
271 {
272   return g_NumHashers;
273 }
274 
GetHasherProp(UInt32 index,PROPID propID,PROPVARIANT * value)275 STDMETHODIMP CHashers::GetHasherProp(UInt32 index, PROPID propID, PROPVARIANT *value)
276 {
277   return ::GetHasherProp(index, propID, value);
278 }
279 
CreateHasher(UInt32 index,IHasher ** hasher)280 STDMETHODIMP CHashers::CreateHasher(UInt32 index, IHasher **hasher)
281 {
282   return ::CreateHasher2(index, hasher);
283 }
284