1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// CodecExports.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Common/ComTry.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Windows/PropVariant.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../ICoder.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../Common/RegisterCodec.h"
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
13baa3858d3f5d128a5c8466b700098109edcad5f2repo syncextern unsigned int g_NumCodecs;
14baa3858d3f5d128a5c8466b700098109edcad5f2repo syncextern const CCodecInfo *g_Codecs[];
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const UInt16 kDecodeId = 0x2790;
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
18baa3858d3f5d128a5c8466b700098109edcad5f2repo syncDEFINE_GUID(CLSID_CCodec,
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync0x23170F69, 0x40C1, kDecodeId, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
21baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic inline HRESULT SetPropString(const char *s, unsigned int size, PROPVARIANT *value)
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if ((value->bstrVal = ::SysAllocStringByteLen(s, size)) != 0)
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    value->vt = VT_BSTR;
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
28baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic inline HRESULT SetPropGUID(const GUID &guid, PROPVARIANT *value)
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SetPropString((const char *)&guid, sizeof(GUID), value);
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
33baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic HRESULT SetClassID(CMethodId id, bool encode, PROPVARIANT *value)
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  GUID clsId = CLSID_CCodec;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (int i = 0; i < sizeof(id); i++, id >>= 8)
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    clsId.Data4[i] = (Byte)(id & 0xFF);
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (encode)
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    clsId.Data3++;
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SetPropGUID(clsId, value);
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
43baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic HRESULT FindCodecClassId(const GUID *clsID, UInt32 isCoder2, bool isFilter, bool &encode, int &index)
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  index = -1;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (clsID->Data1 != CLSID_CCodec.Data1 ||
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      clsID->Data2 != CLSID_CCodec.Data2 ||
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      (clsID->Data3 & ~1) != kDecodeId)
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_OK;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  encode = (clsID->Data3 != kDecodeId);
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 id = 0;
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (int j = 0; j < 8; j++)
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    id |= ((UInt64)clsID->Data4[j]) << (8 * j);
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (unsigned i = 0; i < g_NumCodecs; i++)
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const CCodecInfo &codec = *g_Codecs[i];
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (id != codec.Id || encode && !codec.CreateEncoder || !encode && !codec.CreateDecoder)
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter ||
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2)
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return E_NOINTERFACE;
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    index = i;
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_OK;
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
68baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSTDAPI CreateCoder2(bool encode, UInt32 index, const GUID *iid, void **outObject)
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  COM_TRY_BEGIN
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *outObject = 0;
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isCoder = (*iid == IID_ICompressCoder) != 0;
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isCoder2 = (*iid == IID_ICompressCoder2) != 0;
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isFilter = (*iid == IID_ICompressFilter) != 0;
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const CCodecInfo &codec = *g_Codecs[index];
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter ||
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2)
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return E_NOINTERFACE;
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (encode)
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!codec.CreateEncoder)
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return CLASS_E_CLASSNOTAVAILABLE;
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *outObject = codec.CreateEncoder();
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!codec.CreateDecoder)
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return CLASS_E_CLASSNOTAVAILABLE;
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *outObject = codec.CreateDecoder();
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (isCoder)
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ((ICompressCoder *)*outObject)->AddRef();
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else if (isCoder2)
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ((ICompressCoder2 *)*outObject)->AddRef();
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ((ICompressFilter *)*outObject)->AddRef();
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  COM_TRY_END
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
101baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSTDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject)
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *outObject = 0;
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isCoder = (*iid == IID_ICompressCoder) != 0;
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isCoder2 = (*iid == IID_ICompressCoder2) != 0;
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool isFilter = (*iid == IID_ICompressFilter) != 0;
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!isCoder && !isCoder2 && !isFilter)
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return E_NOINTERFACE;
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool encode;
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int codecIndex;
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT res = FindCodecClassId(clsid, isCoder2, isFilter, encode, codecIndex);
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != S_OK)
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return res;
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (codecIndex < 0)
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return CLASS_E_CLASSNOTAVAILABLE;
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return CreateCoder2(encode, codecIndex, iid, outObject);
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
119baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSTDAPI GetMethodProperty(UInt32 codecIndex, PROPID propID, PROPVARIANT *value)
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::VariantClear((VARIANTARG *)value);
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const CCodecInfo &codec = *g_Codecs[codecIndex];
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  switch(propID)
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    case NMethodPropID::kID:
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      value->uhVal.QuadPart = (UInt64)codec.Id;
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      value->vt = VT_UI8;
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    case NMethodPropID::kName:
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if ((value->bstrVal = ::SysAllocString(codec.Name)) != 0)
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        value->vt = VT_BSTR;
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    case NMethodPropID::kDecoder:
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (codec.CreateDecoder)
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return SetClassID(codec.Id, false, value);
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    case NMethodPropID::kEncoder:
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (codec.CreateEncoder)
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return SetClassID(codec.Id, true, value);
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    case NMethodPropID::kInStreams:
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (codec.NumInStreams != 1)
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        value->vt = VT_UI4;
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        value->ulVal = (ULONG)codec.NumInStreams;
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
156baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSTDAPI GetNumberOfMethods(UINT32 *numCodecs)
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *numCodecs = g_NumCodecs;
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
161