Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

477 lines
19 KiB

  1. //==========================================================================;
  2. // MSVidEncoder.cpp : Declaration of the CMSVidEncoder
  3. // copyright (c) Microsoft Corp. 1998-1999.
  4. /////////////////////////////////////////////////////////////////////////////
  5. #include "stdafx.h"
  6. #ifndef TUNING_MODEL_ONLY
  7. #include "msvidencoder.h"
  8. // HARD CODED pids for program stream video and audio
  9. const ULONG g_AudioID = 0xC0;
  10. const ULONG g_VideoID = 0xE0;
  11. DEFINE_EXTERN_OBJECT_ENTRY(CLSID_MSVidEncoder, CEncoder)
  12. // "Copied" From Demux Proppage
  13. static BYTE g_Mpeg2ProgramVideo [] = {
  14. 0x00, 0x00, 0x00, 0x00, // .hdr.rcSource.left = 0x00000000
  15. 0x00, 0x00, 0x00, 0x00, // .hdr.rcSource.top = 0x00000000
  16. 0xD0, 0x02, 0x00, 0x00, // .hdr.rcSource.right = 0x000002d0
  17. 0xE0, 0x01, 0x00, 0x00, // .hdr.rcSource.bottom = 0x000001e0
  18. 0x00, 0x00, 0x00, 0x00, // .hdr.rcTarget.left = 0x00000000
  19. 0x00, 0x00, 0x00, 0x00, // .hdr.rcTarget.top = 0x00000000
  20. 0x00, 0x00, 0x00, 0x00, // .hdr.rcTarget.right = 0x00000000
  21. 0x00, 0x00, 0x00, 0x00, // .hdr.rcTarget.bottom = 0x00000000
  22. 0x00, 0x09, 0x3D, 0x00, // .hdr.dwBitRate = 0x003d0900
  23. 0x00, 0x00, 0x00, 0x00, // .hdr.dwBitErrorRate = 0x00000000
  24. 0x63, 0x17, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, // .hdr.AvgTimePerFrame = 0x0000000000051763
  25. 0x00, 0x00, 0x00, 0x00, // .hdr.dwInterlaceFlags = 0x00000000
  26. 0x00, 0x00, 0x00, 0x00, // .hdr.dwCopyProtectFlags = 0x00000000
  27. 0x04, 0x00, 0x00, 0x00, // .hdr.dwPictAspectRatioX = 0x00000004
  28. 0x03, 0x00, 0x00, 0x00, // .hdr.dwPictAspectRatioY = 0x00000003
  29. 0x00, 0x00, 0x00, 0x00, // .hdr.dwReserved1 = 0x00000000
  30. 0x00, 0x00, 0x00, 0x00, // .hdr.dwReserved2 = 0x00000000
  31. 0x28, 0x00, 0x00, 0x00, // .hdr.bmiHeader.biSize = 0x00000028
  32. 0xD0, 0x02, 0x00, 0x00, // .hdr.bmiHeader.biWidth = 0x000002d0
  33. 0xE0, 0x01, 0x00, 0x00, // .hdr.bmiHeader.biHeight = 0x00000000
  34. 0x00, 0x00, // .hdr.bmiHeader.biPlanes = 0x0000
  35. 0x00, 0x00, // .hdr.bmiHeader.biBitCount = 0x0000
  36. 0x00, 0x00, 0x00, 0x00, // .hdr.bmiHeader.biCompression = 0x00000000
  37. 0x00, 0x00, 0x00, 0x00, // .hdr.bmiHeader.biSizeImage = 0x00000000
  38. 0xD0, 0x07, 0x00, 0x00, // .hdr.bmiHeader.biXPelsPerMeter = 0x000007d0
  39. 0x27, 0xCF, 0x00, 0x00, // .hdr.bmiHeader.biYPelsPerMeter = 0x0000cf27
  40. 0x00, 0x00, 0x00, 0x00, // .hdr.bmiHeader.biClrUsed = 0x00000000
  41. 0x00, 0x00, 0x00, 0x00, // .hdr.bmiHeader.biClrImportant = 0x00000000
  42. 0x98, 0xF4, 0x06, 0x00, // .dwStartTimeCode = 0x0006f498
  43. 0x56, 0x00, 0x00, 0x00, // .cbSequenceHeader = 0x00000056
  44. 0x02, 0x00, 0x00, 0x00, // .dwProfile = 0x00000002
  45. 0x02, 0x00, 0x00, 0x00, // .dwLevel = 0x00000002
  46. 0x00, 0x00, 0x00, 0x00, // .Flags = 0x00000000
  47. // .dwSequenceHeader [1]
  48. 0x00, 0x00, 0x01, 0xB3, 0x2D, 0x01, 0xE0, 0x24,
  49. 0x09, 0xC4, 0x23, 0x81, 0x10, 0x11, 0x11, 0x12,
  50. 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, 0x14, 0x14,
  51. 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x15,
  52. 0x15, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
  53. 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
  54. 0x18, 0x18, 0x18, 0x19, 0x18, 0x18, 0x18, 0x19,
  55. 0x1A, 0x1A, 0x1A, 0x1A, 0x19, 0x1B, 0x1B, 0x1B,
  56. 0x1B, 0x1B, 0x1C, 0x1C, 0x1C, 0x1C, 0x1E, 0x1E,
  57. 0x1E, 0x1F, 0x1F, 0x21, 0x00, 0x00, 0x01, 0xB5,
  58. 0x14, 0x82, 0x00, 0x01, 0x00, 0x00
  59. } ;
  60. // WaveFormatEx format block; generated with the following settings:
  61. //
  62. // fwHeadFlags = 0x1c;
  63. // wHeadEmphasis = 1;
  64. // fwHeadModeExt = 1;
  65. // fwHeadMode = 1;
  66. // dwHeadBitrate = 0x3e800;
  67. // fwHeadLayer = 0x2;
  68. // wfx.cbSize = 0x16;
  69. // wfx.wBitsPerSample = 0;
  70. // wfx.nBlockAlign = 0x300;
  71. // wfx.nAvgBytesPerSec = 0x7d00;
  72. // wfx.nSamplesPerSec = 0xbb80;
  73. // wfx.nChannels = 2;
  74. // wfx.wFormatTag = 0x50;
  75. // dwPTSLow = 0;
  76. // dwPTSHigh = 0;
  77. static BYTE g_MPEG1AudioFormat [] = {
  78. 0x50, 0x00, 0x02, 0x00, 0x80, 0xBB, 0x00, 0x00,
  79. 0x00, 0x7D, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  80. 0x16, 0x00, 0x02, 0x00, 0x00, 0xE8, 0x03, 0x00,
  81. 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x1C, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  83. } ;
  84. // End "Copied" From Demux Proppage
  85. STDMETHODIMP CEncoder::get_AudioEncoderInterface(/*[out, retval]*/ IUnknown **ppEncInt){
  86. if(!ppEncInt){
  87. return E_POINTER;
  88. }
  89. DSMediaType mediaAudio(MEDIATYPE_Audio);
  90. DSFilter p_filter = m_Filters[m_iEncoder];
  91. HRESULT hr = E_NOINTERFACE;
  92. // Starting at the ecoder filter find the pin/filter implimenting the endcoder api for audio
  93. // This should work because of two facts
  94. // 1. only one audio path or audio path coming into the encoder filter
  95. // 2. we only need to find one matching media type to identify the pin type
  96. do{
  97. DSFilter::iterator pins;
  98. // check the current filters pins for a audio media type
  99. for(pins = p_filter.begin(); pins != p_filter.end(); ++pins){
  100. for(DSPin::iterator mTypes = (*pins).begin(); mTypes != (*pins).end(); ++mTypes){
  101. if((*mTypes) == mediaAudio){
  102. // see if the current pin impliments the encoder api
  103. (*pins).QueryInterface(&m_qiAudEnc) ;
  104. if(m_qiAudEnc){
  105. hr = m_qiAudEnc.QueryInterface(ppEncInt);
  106. if(SUCCEEDED(hr)){
  107. return S_OK;
  108. }
  109. else{
  110. return E_UNEXPECTED;
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. // If we did not get to the end of the media types then we found a audio type and the pin did not inpliment the interface
  117. // time to track backwards
  118. if(mTypes != (*pins).end() && (*pins).GetDirection() == PINDIR_INPUT){
  119. // Following the audio path get the next filter backwards from current filter
  120. DSPin back = (*pins).GetConnection();
  121. if(back){
  122. p_filter = back.GetFilter();
  123. // Check to see if the new filter impliments the encoder api
  124. if(p_filter){
  125. p_filter.QueryInterface(&m_qiAudEnc);
  126. if(m_qiAudEnc){
  127. hr = m_qiAudEnc.QueryInterface(ppEncInt);
  128. if(SUCCEEDED(hr)){
  129. return S_OK;
  130. }
  131. else{
  132. return E_UNEXPECTED;
  133. }
  134. }
  135. }
  136. }
  137. break;
  138. }
  139. }
  140. if(pins == p_filter.end()){
  141. p_filter.Release();
  142. }
  143. } while(p_filter && FAILED(hr));
  144. return hr;
  145. }
  146. STDMETHODIMP CEncoder::get_VideoEncoderInterface(/*[out, retval]*/ IUnknown **ppEncInt){
  147. if(!ppEncInt){
  148. return E_POINTER;
  149. }
  150. DSMediaType mediaVideo(MEDIATYPE_Video);
  151. DSFilter p_filter = m_Filters[m_iEncoder];
  152. HRESULT hr = E_NOINTERFACE;
  153. if(!m_qiVidEnc){
  154. hr = p_filter.QueryInterface(&m_qiVidEnc);
  155. if(FAILED(hr)){
  156. m_qiVidEnc = static_cast<IUnknown*>(NULL);
  157. }
  158. }
  159. if(m_qiVidEnc){
  160. hr = m_qiVidEnc.QueryInterface(ppEncInt);
  161. if(SUCCEEDED(hr)){
  162. return S_OK;
  163. }
  164. else{
  165. return hr;
  166. }
  167. }
  168. // Starting at the ecoder filter find the pin/filter implimenting the endcoder api for video
  169. // This should work because of two facts
  170. // 1. only one video path or audio path coming into the encoder filter
  171. // 2. we only need to find one matching media type to identify the pin type
  172. do{
  173. DSFilter::iterator pins;
  174. // check the current filters pins for a video media type
  175. for(pins = p_filter.begin(); pins != p_filter.end(); ++pins){
  176. DSPin::iterator mTypes;
  177. for(mTypes = (*pins).begin(); mTypes != (*pins).end(); ++mTypes){
  178. if((*mTypes) == mediaVideo){
  179. // see if the current pin impliments the encoder api
  180. (*pins).QueryInterface(&m_qiVidEnc) ;
  181. if(m_qiVidEnc){
  182. hr = m_qiVidEnc.QueryInterface(ppEncInt);
  183. if(SUCCEEDED(hr)){
  184. return S_OK;
  185. }
  186. else{
  187. return hr;
  188. }
  189. }
  190. break;
  191. }
  192. }
  193. // If we did not get to the end of the media types then we found a video type and the pin did not inpliment the interface
  194. // time to track backwards
  195. if(mTypes != (*pins).end() && (*pins).GetDirection() == PINDIR_INPUT){
  196. // Following the video path get the next filter backwards from current filter
  197. DSPin back = (*pins).GetConnection();
  198. if(back){
  199. p_filter = back.GetFilter();
  200. // Check to see if the new filter impliments the encoder api
  201. if(p_filter){
  202. p_filter.QueryInterface(&m_qiVidEnc);
  203. if(m_qiVidEnc){
  204. hr = m_qiVidEnc.QueryInterface(ppEncInt);
  205. if(SUCCEEDED(hr)){
  206. return S_OK;
  207. }
  208. else{
  209. return E_UNEXPECTED;
  210. }
  211. }
  212. }
  213. }
  214. break;
  215. }
  216. }
  217. if(pins == p_filter.end()){
  218. p_filter.Release();
  219. }
  220. } while(p_filter && FAILED(hr));
  221. return hr;
  222. }
  223. HRESULT CEncoder::Unload(void) {
  224. IMSVidGraphSegmentImpl<CEncoder, MSVidSEG_XFORM, &GUID_NULL>::Unload();
  225. m_iEncoder = -1;
  226. m_qiVidEnc.Release();
  227. m_qiAudEnc.Release();
  228. return NOERROR;
  229. }
  230. // IMSVidGraphSegment
  231. STDMETHODIMP CEncoder::Build() {
  232. return NOERROR;
  233. }
  234. STDMETHODIMP CEncoder::PreRun() {
  235. return NOERROR;
  236. }
  237. STDMETHODIMP CEncoder::put_Container(IMSVidGraphSegmentContainer *pCtl){
  238. if (!m_fInit) {
  239. return CO_E_NOTINITIALIZED;
  240. }
  241. try {
  242. if (!pCtl) {
  243. return Unload();
  244. }
  245. if (m_pContainer) {
  246. if (!m_pContainer.IsEqualObject(VWSegmentContainer(pCtl))) {
  247. return Error(IDS_OBJ_ALREADY_INIT, __uuidof(IMSVidEncoder), CO_E_ALREADYINITIALIZED);
  248. } else {
  249. return NO_ERROR;
  250. }
  251. }
  252. // DON'T addref the container. we're guaranteed nested lifetimes
  253. // and an addref creates circular refcounts so we never unload.
  254. m_pContainer.p = pCtl;
  255. m_pGraph = m_pContainer.GetGraph();
  256. // Add some filters when there is an encoder api
  257. DSFilter pEncoder(m_pGraph.AddMoniker(m_pDev));
  258. if (!pEncoder) {
  259. return E_UNEXPECTED;
  260. }
  261. m_Filters.push_back(pEncoder);
  262. m_iEncoder = 0;
  263. TRACELM(TRACE_DETAIL, "CMSVidEncoder::put_Container() Encoder added");
  264. DSFilter::iterator fPin;
  265. DSMediaType mpeg2ProgramType(MEDIATYPE_Stream, MEDIASUBTYPE_MPEG2_PROGRAM);
  266. DSMediaType streamType(MEDIATYPE_Stream);
  267. for(fPin = pEncoder.begin(); fPin != pEncoder.end(); ++fPin) {
  268. DSPin curPin(*fPin);
  269. DSPin::iterator pMedia;
  270. // Find the Mpeg2 Progam Steam Pin if there is one
  271. for(pMedia = curPin.begin(); pMedia != curPin.end(); ++pMedia){
  272. if ((*pMedia) == streamType && curPin.GetDirection() == PINDIR_OUTPUT){
  273. break;
  274. }
  275. }
  276. if(pMedia == curPin.end()){
  277. continue;
  278. }
  279. else{
  280. if((*pMedia) == mpeg2ProgramType){
  281. // Found the program stream pin get a demux and set it up
  282. CComQIPtr<IMpeg2Demultiplexer> qiDeMux;
  283. qiDeMux.CoCreateInstance(CLSID_MPEG2Demultiplexer);
  284. if(!qiDeMux){
  285. ASSERT(FALSE);
  286. return E_UNEXPECTED;
  287. }
  288. DSFilter DeMux(qiDeMux);
  289. DSFilterList intermediates;
  290. CString csName(_T("MPEG-2 Demultiplexer"));
  291. HRESULT hr = m_pGraph.AddFilter(DeMux, csName);
  292. if (FAILED(hr)) {
  293. ASSERT(FALSE);
  294. return E_UNEXPECTED;
  295. }
  296. m_Filters.push_back(DeMux);
  297. m_iDemux = m_Filters.size() - 1;
  298. for(DSFilter::iterator dPin = DeMux.begin(); dPin != DeMux.end(); ++dPin){
  299. DSPin demuxIn(*dPin);
  300. if(demuxIn.GetDirection() == PINDIR_INPUT){
  301. hr = demuxIn.Connect(curPin);
  302. if (FAILED(hr)) {
  303. ASSERT(FALSE);
  304. return E_UNEXPECTED;
  305. }
  306. }
  307. }
  308. // Sprout the audio and video pins on the demxu
  309. DSPin dspAudio, dspVideo;
  310. DSMediaType mtVideo(MEDIATYPE_Video, MEDIASUBTYPE_MPEG2_VIDEO, FORMAT_MPEG2Video);
  311. mtVideo.p->bFixedSizeSamples = TRUE;
  312. mtVideo.p->cbFormat = sizeof(g_Mpeg2ProgramVideo);
  313. mtVideo.p->pbFormat = g_Mpeg2ProgramVideo;
  314. DSMediaType mtAudio(MEDIATYPE_Audio, MEDIASUBTYPE_MPEG1Payload, FORMAT_WaveFormatEx);
  315. mtAudio.p->bFixedSizeSamples = TRUE;
  316. mtAudio.p->cbFormat = sizeof(g_MPEG1AudioFormat);
  317. mtAudio.p->pbFormat = g_MPEG1AudioFormat;
  318. CComBSTR szAudio("Audio Pin");
  319. CComBSTR szVideo("Video Pin");
  320. hr = qiDeMux->CreateOutputPin(mtAudio, szAudio, &dspAudio);
  321. if (FAILED(hr)) {
  322. ASSERT(FALSE);
  323. return E_UNEXPECTED;
  324. }
  325. hr = qiDeMux->CreateOutputPin(mtVideo, szVideo, &dspVideo);
  326. if (FAILED(hr)) {
  327. ASSERT(FALSE);
  328. return E_UNEXPECTED;
  329. }
  330. // Map the pids correctly
  331. // TODO: use the encoder api to find the pids for audio and video
  332. CComQIPtr<IMPEG2StreamIdMap>qiMapper(dspVideo);
  333. hr = qiMapper->MapStreamId(g_VideoID, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0);
  334. if (FAILED(hr)) {
  335. ASSERT(FALSE);
  336. return E_UNEXPECTED;
  337. }
  338. qiMapper = dspAudio;
  339. hr = qiMapper->MapStreamId(g_AudioID, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0);
  340. if (FAILED(hr)) {
  341. ASSERT(FALSE);
  342. return E_UNEXPECTED;
  343. }
  344. // Clean up
  345. mtVideo.p->cbFormat = 0;
  346. mtVideo.p->pbFormat = 0;
  347. mtAudio.p->cbFormat = 0;
  348. mtAudio.p->pbFormat = 0;
  349. break;
  350. }
  351. #if 0 // code to support custom demux (e.g. asf/wmv demux by a third party)
  352. else{
  353. CRegKey c;
  354. TCHAR szCLSID[MAX_PATH + 1];
  355. szCLSID[0] = 0;
  356. CString keyname(_T("SOFTWARE\\Debug\\MSVidCtl"));
  357. DWORD rc = c.Open(HKEY_LOCAL_MACHINE, keyname, KEY_READ);
  358. if (rc == ERROR_SUCCESS) {
  359. DWORD len = sizeof(szCLSID);
  360. rc = c.QueryValue(szCLSID, _T("CustomDemuxCLSID"), &len);
  361. if (rc != ERROR_SUCCESS) {
  362. szCLSID[0] = 0;
  363. }
  364. }
  365. DSFilter DeMux;
  366. CComBSTR asfCLSID(szCLSID);
  367. GUID2 asfDemux(asfCLSID);
  368. DeMux.CoCreateInstance(asfDemux);
  369. if(!DeMux){
  370. ASSERT(FALSE);
  371. return E_UNEXPECTED;
  372. }
  373. DSFilterList intermediates;
  374. CString csName(_T("Custom Demultiplexer"));
  375. HRESULT hr = m_pGraph.AddFilter(DeMux, csName);
  376. if (FAILED(hr)) {
  377. ASSERT(FALSE);
  378. return E_UNEXPECTED;
  379. }
  380. m_Filters.push_back(DeMux);
  381. m_iDemux = m_Filters.size() - 1;
  382. for(DSFilter::iterator dPin = DeMux.begin(); dPin != DeMux.end(); ++dPin){
  383. DSPin demuxIn(*dPin);
  384. if(demuxIn.GetDirection() == PINDIR_INPUT){
  385. hr = demuxIn.Connect(curPin);
  386. if (FAILED(hr)) {
  387. ASSERT(FALSE);
  388. return E_UNEXPECTED;
  389. }
  390. }
  391. }
  392. }
  393. #endif
  394. }
  395. }
  396. // Don't fail if there is no program stream pin. could be elementry streams or non-mpeg content
  397. return NOERROR;
  398. } catch (ComException &e) {
  399. return e;
  400. } catch(...) {
  401. return E_UNEXPECTED;
  402. }
  403. return NOERROR;
  404. }
  405. // IMSVidDevice
  406. STDMETHODIMP CEncoder::get_Name(BSTR * Name){
  407. if (!m_fInit) {
  408. return CO_E_NOTINITIALIZED;
  409. }
  410. try {
  411. CComBSTR DefaultName("Encoder Segment");
  412. return GetName(((m_iEncoder > -1) ? (m_Filters[m_iEncoder]) : DSFilter()), m_pDev, DefaultName).CopyTo(Name);
  413. return NOERROR;
  414. } catch(...) {
  415. return E_POINTER;
  416. }
  417. }
  418. STDMETHODIMP CEncoder::InterfaceSupportsErrorInfo(REFIID riid){
  419. static const IID* arr[] =
  420. {
  421. &IID_IMSVidEncoder
  422. };
  423. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++){
  424. if (InlineIsEqualGUID(*arr[i],riid))
  425. return S_OK;
  426. }
  427. return S_FALSE;
  428. }
  429. #endif // TUNING_MODEL_ONLY