[svn] / trunk / xvidextra / src / apps / miniconvert / recompress.cpp Repository:
ViewVC logotype

Annotation of /trunk/xvidextra/src/apps/miniconvert/recompress.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2045 - (view) (download)

1 : Irhall 2014 /*****************************************************************************
2 :     *
3 :     * Xvid MiniConvert
4 :     * - RecompressGraph class and WinMain() -
5 :     *
6 :     * Copyright(C) 2011 Xvid Solutions GmbH
7 :     *
8 :     * This program is free software ; you can redistribute it and/or modify
9 :     * it under the terms of the GNU General Public License as published by
10 :     * the Free Software Foundation ; either version 2 of the License, or
11 :     * (at your option) any later version.
12 :     *
13 :     * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 :     *
18 :     * You should have received a copy of the GNU General Public License
19 :     * along with this program ; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 :     * $Id$
23 :     *
24 :     ****************************************************************************/
25 :     /*
26 :     * Author(s): Ireneusz Hallmann
27 :     *
28 :     ****************************************************************************/
29 :    
30 :     #include "stdafx.h"
31 :    
32 :     #include "utils.h"
33 :     #include "filters.h"
34 :     #include "recompress.h"
35 :     #include "resource.h"
36 :     #include <stdio.h>
37 :     #include <vfw.h>
38 :     #include <wmsdk.h>
39 :    
40 :     //#define ALLOW_ONLY_AVI_INPUT
41 :    
42 :     #define APP_NAME TEXT("Xvid MiniConvert")
43 :    
44 : Isibaar 2019 #define Pass_FILE_A "xvid_2pass.stats"
45 :     #define Pass_FILE TEXT("xvid_2pass.stats")
46 : Irhall 2014
47 :     // Todo: Avoid global vars...
48 :     OPENFILENAME sOfn, sSfn;
49 :     TCHAR OpenFilePath[MAX_PATH *2] = TEXT("\0");
50 :     TCHAR SaveFilePath[MAX_PATH *2] = TEXT("\0");
51 :     HWND ghDlg;
52 :    
53 :     TCHAR *SrcFile, DstFile[MAX_PATH *2];
54 :     RecompressGraph *pRec;
55 :     HANDLE hConvertThread = 0;
56 :    
57 :     //////////////////////////////////////////////////////////////////////////////
58 :    
59 :     // Callback for FileCopyEx progress notification
60 :     DWORD CALLBACK
61 :     CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
62 :     LARGE_INTEGER TotalBytesTransferred,
63 :     LARGE_INTEGER StreamSize,
64 :     LARGE_INTEGER StreamBytesTransferred,
65 :     DWORD dwStreamNumber,
66 :     DWORD dwCallbackReason,
67 :     HANDLE hSourceFile,
68 :     HANDLE hDestinationFile,
69 :     LPVOID lpData )
70 :     {
71 :     RecompressGraph *pRecGraph = (RecompressGraph *) lpData;
72 :     double Progress = ((double)(TotalBytesTransferred.QuadPart) /
73 :     (double)(TotalFileSize.QuadPart) ) * 100.;
74 :    
75 :     if (pRecGraph->GetTotalSize() > 0) { // Have multiple input files?
76 :     Progress *= ((double)pRecGraph->GetCurSize() / (double) pRecGraph->GetTotalSize());
77 :     Progress += (100*(pRecGraph->GetElapsedSize()) / pRecGraph->GetTotalSize());
78 :     }
79 :    
80 :     // Update progress bar
81 :     PostMessage((HWND)pRecGraph->GetProgressWnd(), PBM_SETPOS, (WPARAM)Progress, 0);
82 :    
83 :     if (pRecGraph->IsBreakRequested())
84 :     return PROGRESS_STOP;
85 :     else
86 :     return PROGRESS_CONTINUE; // Continue the file copy
87 :     }
88 :    
89 :     // Helper function to determine size (in KB) of a given file
90 :     BOOL
91 :     DetermineFileSize(const TCHAR *fileName, DWORD *fileSizeOut)
92 :     {
93 :     BOOL ret;
94 :     WIN32_FILE_ATTRIBUTE_DATA fileInfo;
95 :    
96 :     if (fileName == NULL)
97 :     return FALSE;
98 :    
99 :     ret = GetFileAttributesEx(fileName, GetFileExInfoStandard, (void*)&fileInfo);
100 :    
101 :     if (!ret)
102 :     return FALSE;
103 :    
104 :     *fileSizeOut = (fileInfo.nFileSizeHigh<<22) |
105 :     (fileInfo.nFileSizeLow>>10); // Should work up to 4 TB file size...
106 :    
107 :     return TRUE;
108 :     }
109 :    
110 :     //////////////////////////////////////////////////////////////////////////////
111 :    
112 :     HRESULT
113 :     AddToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
114 :     {
115 :     IMoniker * pMoniker = NULL;
116 :     IRunningObjectTable *pROT = NULL;
117 :    
118 :     if (FAILED(GetRunningObjectTable(0, &pROT))) {
119 :     return E_FAIL;
120 :     }
121 :    
122 :     const size_t STRING_LENGTH = 256;
123 :     WCHAR wsz[STRING_LENGTH];
124 : Isibaar 2019 swprintf(wsz, STRING_LENGTH, L"FilterGraph %08x pid %08x",
125 : Irhall 2014 (DWORD_PTR)pUnkGraph, GetCurrentProcessId());
126 :    
127 : Isibaar 2019 HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
128 : Irhall 2014 if (SUCCEEDED(hr)) {
129 :     hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pUnkGraph,
130 :     pMoniker, pdwRegister);
131 :     pMoniker->Release();
132 :     }
133 :    
134 :     pROT->Release();
135 :     return hr;
136 :     }
137 :    
138 :    
139 :     void RemoveFromRot(DWORD pdwRegister)
140 :     {
141 :     IRunningObjectTable *pROT;
142 :    
143 :     if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
144 :     pROT->Revoke(pdwRegister);
145 :     pROT->Release();
146 :     }
147 :     }
148 :    
149 :     RecompressGraph::RecompressGraph()
150 :     {
151 :     m_szSourceFilePath = 0;
152 :     m_szDstFilePath = 0;
153 :    
154 :     m_UsedStreamsCnt = 0;
155 :     m_pGraph = 0;
156 :     m_pBuilder = 0;
157 :     m_pXvidCfgRec = 0;
158 :    
159 :     m_Bitrate = m_bFileCopy = 0;
160 :     m_Width = m_Height = 0;
161 :    
162 : Isibaar 2029 m_pVideoMeter = m_pSplitter = m_pSrcFilter = m_pXvidEncoder = 0;
163 : Irhall 2014 //m_pEmmsDummy = 0;
164 :     m_pMuxer = m_pFileWriter = m_pChgType = 0;
165 : Isibaar 2045 m_pIChgTypeNorm = 0;
166 : Irhall 2014 m_pXvidConfig = 0;
167 :     m_bBreakRequested = 0;
168 :    
169 : Isibaar 2029 m_pEvent = 0;
170 :     m_pControl = 0;
171 : Irhall 2014 m_elapsedSize = m_curSize = m_totalSize = 0;
172 :     }
173 :    
174 :     RecompressGraph::~RecompressGraph()
175 :     {
176 :     CleanUp();
177 :     }
178 :    
179 :     void
180 :     RecompressGraph::CleanUp()
181 :     {
182 :     m_UsedStreamsCnt = 0;
183 :    
184 :     if (m_szSourceFilePath)
185 :     free (m_szSourceFilePath);
186 :    
187 :     if (m_szDstFilePath)
188 :     free (m_szDstFilePath);
189 :    
190 :     if (m_pXvidCfgRec)
191 :     free(m_pXvidCfgRec);
192 :    
193 :     m_pXvidCfgRec = 0;
194 :     m_bBreakRequested = 0;
195 :    
196 :     m_szSourceFilePath = m_szDstFilePath = 0;
197 :     m_curSize = 0;
198 :    
199 :     if (m_pVideoMeter) {
200 :     m_pGraph->RemoveFilter(m_pVideoMeter);
201 :     m_pVideoMeter->Release();
202 :     m_pVideoMeter=0;
203 :     }
204 :    
205 :     if (m_pSrcFilter) {
206 :     m_pGraph->RemoveFilter(m_pSrcFilter);
207 :     m_pSrcFilter->Release();
208 :     m_pSrcFilter = 0;
209 :     }
210 :    
211 : Isibaar 2029 if (m_pSplitter) {
212 :     m_pGraph->RemoveFilter(m_pSplitter);
213 :     m_pSplitter->Release();
214 :     m_pSplitter = 0;
215 :     }
216 :    
217 : Irhall 2014 if (m_pXvidEncoder) {
218 :     m_pGraph->RemoveFilter(m_pXvidEncoder);
219 :     m_pXvidEncoder->Release();
220 :     m_pXvidEncoder=0;
221 :     }
222 :    
223 :     if (m_pXvidConfig) {
224 :     m_pXvidConfig->Release();
225 :     m_pXvidConfig = 0;
226 :     }
227 :    
228 : Isibaar 2045 if (m_pIChgTypeNorm) {
229 :     m_pIChgTypeNorm->Release();
230 :     m_pIChgTypeNorm = 0;
231 :     }
232 :    
233 : Irhall 2014 if (m_pChgType) {
234 :     m_pGraph->RemoveFilter(m_pChgType);
235 :     m_pChgType->Release();
236 :     m_pChgType=0;
237 :     }
238 :    
239 :     if (m_pMuxer) {
240 :     m_pGraph->RemoveFilter(m_pMuxer);
241 :     m_pMuxer->Release();
242 :     m_pMuxer = 0;
243 :     }
244 :    
245 :     if (m_pFileWriter) {
246 :     m_pGraph->RemoveFilter(m_pFileWriter);
247 :     m_pFileWriter->Release();
248 :     m_pFileWriter = 0;
249 :     }
250 :    
251 :     IEnumFilters *pIEnumFilters = 0;
252 :    
253 :     if (m_pGraph) {
254 :     m_pGraph->EnumFilters(&pIEnumFilters);
255 :     IBaseFilter *pFilter = 0;
256 :    
257 :     if (pIEnumFilters) {
258 :     while (pIEnumFilters->Next(1, &pFilter, 0) == S_OK) {
259 :     m_pGraph->RemoveFilter(pFilter);
260 :     pFilter->Release();
261 :     pIEnumFilters->Reset();
262 :     }
263 :     }
264 :     }
265 :    
266 :     if (pIEnumFilters)
267 :     pIEnumFilters->Release();
268 :    
269 :     while (m_vOtherFilters.size() > 0) {
270 :     IBaseFilter *pFlt = m_vOtherFilters.back();
271 :     m_vOtherFilters.pop_back();
272 :     m_pGraph->RemoveFilter(pFlt);
273 :     pFlt->Release();
274 :     }
275 :    
276 :     if (m_pGraph) {
277 :     m_pGraph->Release();
278 :     m_pGraph = 0;
279 :     }
280 :    
281 :     if (m_pBuilder) {
282 :     m_pBuilder->Release();
283 :     m_pBuilder = 0;
284 :     }
285 :     }
286 :    
287 :     HRESULT
288 :     RecompressGraph::CreateGraph(HWND in_ProgressWnd, int in_Pass)
289 :     {
290 :     m_FpsNom = m_FpsDen = 0;
291 :     m_ProgressWnd = in_ProgressWnd;
292 :    
293 :     m_bFileCopy = 1; // Initial guess: File copy strategy possible
294 :    
295 :     #ifdef ALLOW_ONLY_AVI_INPUT
296 :     if (fourcc_helper(m_szSourceFilePath, NULL, NULL, 1) != 0) return S_FALSE; // No AVI? -> fail
297 :     #endif
298 :    
299 :     DetermineFileSize(m_szSourceFilePath, &m_curSize);
300 :    
301 :     if (in_ProgressWnd && (in_Pass == 1) && (m_elapsedSize == 0))
302 :     PostMessage(in_ProgressWnd, PBM_SETPOS, 0, 0); // Reset progress bar
303 :    
304 :     HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
305 :     IID_ICaptureGraphBuilder2, (void **)&m_pBuilder);
306 :    
307 :     if (hr == S_OK) hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
308 :     IID_IGraphBuilder, (void **)&m_pGraph);
309 :    
310 :     if (hr == S_OK) hr = m_pBuilder->SetFiltergraph(m_pGraph);
311 : Isibaar 2029 m_bIsWMV = 0;
312 : Irhall 2014 if (hr == S_OK) hr = AddSourceFilter(m_szSourceFilePath);
313 :    
314 :     m_UsedStreamsCnt = 1;
315 :     m_vDuration = 0;
316 :     IMediaSeeking *pSeek = 0;
317 :     IPin *pOutVideoPin = 0;
318 :     CMediaType vMt;
319 :    
320 : Isibaar 2029 if (hr == S_OK && m_bIsWMV) {
321 : Irhall 2014 if (GetFilterPin(m_pSrcFilter, PINDIR_OUTPUT, 0, &pOutVideoPin,
322 : Isibaar 2029 MEDIATYPE_Video, GUID_NULL) == S_OK) {
323 : Irhall 2014 IEnumMediaTypes *pIEnumMediaTypes = 0;
324 :    
325 : Isibaar 2029 if (pOutVideoPin->EnumMediaTypes(&pIEnumMediaTypes) == S_OK) {
326 : Irhall 2014 AM_MEDIA_TYPE *ppMt;
327 :    
328 : Isibaar 2029 if (pIEnumMediaTypes->Next(1, &ppMt, 0) == S_OK) {
329 : Irhall 2014 vMt = *ppMt;
330 :     DeleteMediaType(ppMt);
331 :     }
332 :     pIEnumMediaTypes->Release();
333 :     }
334 :     pOutVideoPin->QueryInterface(IID_IMediaSeeking, (void **)&pSeek);
335 :     }
336 :     }
337 :    
338 :     IPin* pVMeterInVideoPin = 0;
339 :     if ((hr == S_OK)) {
340 : Isibaar 2029 //if (!m_bIsWMV && (hr == S_OK)) {
341 : Irhall 2014 CUnknown *pVidMeter = 0;
342 :    
343 :     if (hr == S_OK) pVidMeter = CProgressNotifyFilter::CreateInstance(0, &hr, 0);
344 :     if (hr == S_OK) hr = pVidMeter->NonDelegatingQueryInterface(IID_IBaseFilter, (void **)&m_pVideoMeter);
345 :     //hr = CoCreateInstance(CLSID_ProgressNotifyFilter, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&m_pVideoMeter);
346 : Isibaar 2019 if (hr == S_OK) hr = m_pGraph->AddFilter(m_pVideoMeter, L"Video meter");
347 : Irhall 2014 if (hr == S_OK) hr = GetFilterPin(m_pVideoMeter, PINDIR_INPUT, 0, &pVMeterInVideoPin, GUID_NULL, GUID_NULL);
348 :    
349 :     if (pOutVideoPin) {
350 :     hr = m_pGraph->Connect(pOutVideoPin, pVMeterInVideoPin);
351 :     pOutVideoPin->Release();
352 :     pOutVideoPin = 0;
353 :     }
354 :     else {
355 :     IPin *pVideoPin = 0;
356 :     hr = m_pBuilder->RenderStream(NULL, NULL, m_pSrcFilter, NULL, m_pVideoMeter);
357 :     if (hr == S_OK) hr = GetFilterPin(m_pVideoMeter, PINDIR_OUTPUT, 0, &pVideoPin, MEDIATYPE_Video, GUID_NULL);
358 :     if (hr == S_OK) pVideoPin->QueryInterface(IID_IMediaSeeking, (void **)&pSeek);
359 :     if (pVideoPin) pVideoPin->Release();
360 :     }
361 :     GetFilterPin(m_pVideoMeter, PINDIR_OUTPUT, 0, &pOutVideoPin, GUID_NULL, GUID_NULL);
362 :     }
363 :    
364 :     if (pSeek && (hr == S_OK)) {
365 :     LONGLONG Duration = 0;
366 :     hr = pSeek->GetDuration(&Duration);
367 :     if (hr == S_OK) m_vDuration = Duration;
368 :     }
369 :     if (pSeek) pSeek->Release();
370 :    
371 :     IBaseFilter *pCompressedVideoFilter = 0;
372 :     int m_bRecompress = 1;
373 :     GUID gSubtype;
374 :    
375 :     if (hr == S_OK) {
376 :     m_TotalFrames = 0;
377 :    
378 : Isibaar 2029 if (hr == S_OK && !m_bIsWMV) hr = pVMeterInVideoPin->ConnectionMediaType(&vMt);
379 : Irhall 2014
380 :     if (m_vDuration != 0) {
381 :     if (vMt.formattype != FORMAT_None) {
382 :     if (vMt.formattype == FORMAT_VideoInfo || vMt.formattype == FORMAT_MPEGVideo) {
383 :     m_AvgTimeForFrame = (DWORD) ((VIDEOINFOHEADER *)vMt.pbFormat)->AvgTimePerFrame;
384 : Isibaar 2029 if (m_bIsWMV) m_Bitrate = ((VIDEOINFOHEADER *)vMt.pbFormat)->dwBitRate;
385 : Irhall 2014 }
386 :     else if (vMt.formattype == FORMAT_VideoInfo2 || vMt.formattype == FORMAT_MPEG2Video) {
387 :     m_AvgTimeForFrame = (DWORD) (((VIDEOINFOHEADER2 *)vMt.pbFormat)->AvgTimePerFrame);
388 : Isibaar 2029 if (m_bIsWMV) m_Bitrate = ((VIDEOINFOHEADER2 *)vMt.pbFormat)->dwBitRate;
389 : Irhall 2014 }
390 :     else return VFW_E_TYPE_NOT_ACCEPTED;
391 :     }
392 :     m_TotalFrames = (DWORD) (m_AvgTimeForFrame ? (m_vDuration + (m_AvgTimeForFrame/2)) / m_AvgTimeForFrame : 0);
393 :     }
394 :    
395 :     gSubtype = vMt.subtype;
396 :     if (hr == S_OK) {
397 :     if (gSubtype == FOURCCMap('DIVX') || gSubtype == FOURCCMap('XVID')
398 :     || gSubtype == FOURCCMap('divx') || gSubtype == FOURCCMap('xvid')
399 :     || gSubtype == FOURCCMap('05XD') || gSubtype == FOURCCMap('V4PM')
400 :     || gSubtype == FOURCCMap('05xd') || gSubtype == FOURCCMap('v4pm')
401 :     || gSubtype == FOURCCMap('4PMR') || gSubtype == FOURCCMap('4pmr')
402 :     || gSubtype == FOURCCMap('4XDH') || gSubtype == FOURCCMap('4xdh')
403 :     || gSubtype == FOURCCMap('XVI3') || gSubtype == FOURCCMap('xvi3')
404 :     || gSubtype == FOURCCMap('0VI3') || gSubtype == FOURCCMap('0vi3')
405 :     || gSubtype == FOURCCMap('1VI3') || gSubtype == FOURCCMap('1vi3')
406 :     || gSubtype == FOURCCMap('2VI3') || gSubtype == FOURCCMap('2vi3')
407 :     || gSubtype == FOURCCMap('4PML') || gSubtype == FOURCCMap('4pml')
408 :     || gSubtype == FOURCCMap('4PMS') || gSubtype == FOURCCMap('4pms'))
409 :     {
410 : Isibaar 2029 pCompressedVideoFilter = m_pVideoMeter ? m_pVideoMeter : (m_pSplitter ? m_pSplitter : m_pSrcFilter);
411 : Irhall 2014 }
412 :     else {
413 :     m_bFileCopy = 0;
414 :    
415 :     if (hr == S_OK) hr = AddDirectXFilterByMoniker(CLSID_VideoCompressorCategory, "xvid", &m_pXvidEncoder, 1);
416 : Isibaar 2019 if (hr == S_OK) hr = m_pGraph->AddFilter(m_pXvidEncoder, L"Encoder");
417 : Irhall 2014
418 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, pOutVideoPin, m_pXvidEncoder, GUID_NULL, GUID_NULL, 0);
419 :     if (hr == S_OK) pCompressedVideoFilter = m_pXvidEncoder;
420 :    
421 :     m_ToAlloc = 0;
422 :     if (hr == S_OK) hr = m_pXvidEncoder->QueryInterface(IID_IAMVfwCompressDialogs, (void **)&m_pXvidConfig);
423 :    
424 :     if (hr == S_OK) m_ToAlloc = m_pXvidConfig->SendDriverMessage(ICM_GETSTATE, NULL, 0);
425 :     if (hr == S_OK) m_pXvidCfgRec = (CONFIG *)malloc(m_ToAlloc +10);
426 :     if (hr == S_OK) m_pXvidConfig->SendDriverMessage(ICM_GETSTATE, (LONG)m_pXvidCfgRec, 0);
427 :    
428 :     if (hr == S_OK) {
429 :     if (in_Pass == 1) {
430 :     m_pXvidCfgRec->mode = RC_MODE_2PASS1;
431 :     }
432 :     else {
433 :     m_pXvidCfgRec->mode = RC_MODE_2PASS2;
434 :    
435 :     if (m_Bitrate > 0) {
436 :     m_pXvidCfgRec->use_2pass_bitrate = 1;
437 :     m_pXvidCfgRec->bitrate = m_Bitrate/1000; // in kbps
438 :     }
439 :     else if (m_TotalFramesSize > 0) {
440 :     m_pXvidCfgRec->desired_size = (int) (m_TotalFramesSize/1024); // in KB
441 :     m_pXvidCfgRec->use_2pass_bitrate = 0;
442 :     }
443 :     else {
444 :     m_pXvidCfgRec->desired_size = (int) (.9f*m_curSize); // in KB
445 :     m_pXvidCfgRec->use_2pass_bitrate = 0;
446 :     }
447 :    
448 :     #if 1 // select a profile matching the input dimensions
449 :     if (m_Width == 0 || m_Height == 0) { // not detected
450 :     m_pXvidCfgRec->profile = 0x11;
451 :     strcpy(m_pXvidCfgRec->profile_name, "(unrestricted)");
452 :     }
453 :     else if (m_Width <= 352 && m_Height <= 288) {
454 :     m_pXvidCfgRec->profile = 0x0;
455 :     strcpy(m_pXvidCfgRec->profile_name, "Xvid Mobile");
456 :     }
457 :     else if (m_Width <= 720 && m_Height <= 576) {
458 :     m_pXvidCfgRec->profile = 0x1;
459 :     strcpy(m_pXvidCfgRec->profile_name, "Xvid Home");
460 :     }
461 :     else if (m_Width <= 1280 && m_Height <= 720) {
462 :     m_pXvidCfgRec->profile = 0x2;
463 :     strcpy(m_pXvidCfgRec->profile_name, "Xvid HD 720");
464 :     }
465 :     else if (m_Width <= 1920 && m_Height <= 1080) {
466 :     m_pXvidCfgRec->profile = 0x3;
467 :     strcpy(m_pXvidCfgRec->profile_name, "Xvid HD 1080");
468 :     }
469 :     else {
470 :     m_pXvidCfgRec->profile = 0x11;
471 :     strcpy(m_pXvidCfgRec->profile_name, "(unrestricted)");
472 :     }
473 :     #else
474 :     m_pXvidCfgRec->profile = 0xE;
475 :     strcpy(m_pXvidCfgRec->profile_name, "(unrestricted)");
476 :     #endif
477 :     }
478 :    
479 : Isibaar 2019 strcpy(m_pXvidCfgRec->stats, Pass_FILE_A);
480 : Irhall 2014
481 :     m_pXvidCfgRec->display_status = 0;
482 :    
483 :     if (hr == S_OK) m_pXvidConfig->SendDriverMessage(ICM_SETSTATE, (LONG)m_pXvidCfgRec, 0);
484 :     }
485 :     }
486 :     }
487 :     }
488 :    
489 :     CUnknown *pChgUnk = ChangeSubtypeT::CreateInstance(0, &hr);
490 :     if (hr == S_OK) hr = pChgUnk->NonDelegatingQueryInterface(IID_IBaseFilter, (void **)&m_pChgType);
491 : Isibaar 2019 if (hr == S_OK) hr = m_pGraph->AddFilter(m_pChgType, L"ChgToxvid");
492 : Irhall 2014 if (hr == S_OK) hr = ConnectFilters(m_pGraph, pCompressedVideoFilter, m_pChgType, GUID_NULL, GUID_NULL, 1);
493 :    
494 :     if (hr == S_OK) hr = AddFilterByCLSID((GUID *)&CLSID_AviDest, &m_pMuxer);
495 :    
496 :     #if 0
497 :     IConfigInterleaving *pIConfigInterleaving = 0;
498 :     if (hr == S_OK) hr = m_pMuxer->QueryInterface(IID_IConfigInterleaving, (void **)&pIConfigInterleaving);
499 :     REFERENCE_TIME InterleaveRate = 10000000LL, Preroll = 100000000LL;
500 :     if (hr == S_OK) hr = pIConfigInterleaving->put_Mode(INTERLEAVE_FULL);
501 :     if (hr == S_OK) hr = pIConfigInterleaving->put_Interleaving(&InterleaveRate, &Preroll);
502 :     if (pIConfigInterleaving) pIConfigInterleaving->Release();
503 :     #endif
504 :    
505 : Isibaar 2045 if (hr == S_OK) hr = m_pChgType->QueryInterface(IID_IRecProgressNotify, (void **)&m_pIChgTypeNorm);
506 :     if (hr == S_OK) hr = m_pIChgTypeNorm->SetTotalFrames(m_TotalFrames);
507 :     if (hr == S_OK) m_pIChgTypeNorm->SetPass(in_Pass);
508 :    
509 :     if (in_Pass == 2) {
510 :     LONGLONG FpsNom = m_MaxETime - m_MinETime, FpsDen = m_CountedFrames > 1 ? m_CountedFrames-1 : 1;
511 :     LONGLONG cAvgTimeForFrame = FpsNom/FpsDen;
512 :     //if (((ULONGLONG)m_AvgTimeForFrame*m_CountedFrames - FpsNom) > m_AvgTimeForFrame + m_CountedFrames) {
513 :     if (!m_AvgTimeForFrame) {
514 :     m_pIChgTypeNorm->SetForceTimeParams(m_MinSTime, FpsNom, FpsDen);
515 :     }
516 :     }
517 :    
518 : Irhall 2014 if (hr == S_OK) hr = ConnectFilters(m_pGraph, m_pChgType, m_pMuxer, MEDIATYPE_Video, GUID_NULL, 1);
519 :    
520 :     if (hr == S_OK) hr = AddFileWriter(m_szDstFilePath);
521 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, m_pMuxer, m_pFileWriter, GUID_NULL, GUID_NULL, 1);
522 :    
523 :     if ((hr == S_OK) && ((in_Pass ==2) || (!m_pXvidEncoder))) hr = AddAudioStreams(0);
524 :     else if (hr == S_OK) hr = AddAudioStreams(1);
525 :    
526 :     if (pVMeterInVideoPin) pVMeterInVideoPin->Release();
527 :     if (pOutVideoPin) pOutVideoPin->Release();
528 :     DeleteFile(m_szDstFilePath);
529 :     //AddToRot(m_pGraph, &dwReg);
530 :    
531 :     if (hr == S_OK && in_Pass != 2) { // Make progress bar visible
532 : Isibaar 2017 TCHAR buf[MAX_PATH+50], buf2[MAX_PATH];
533 : Isibaar 2019 if (_tcslen(m_szSourceFilePath) > 60) {
534 : Isibaar 2017 PathCompactPathEx(buf2, m_szSourceFilePath, 60, 0);
535 : Isibaar 2019 _stprintf(buf, TEXT("Converting %s"), buf2);
536 : Isibaar 2017 }
537 :     else
538 : Isibaar 2019 _stprintf(buf, TEXT("Converting %s..."), m_szSourceFilePath);
539 : Isibaar 2017
540 : Irhall 2014 ShowWindow(GetDlgItem(ghDlg, IDC_EDIT_SRC), SW_HIDE);
541 :     ShowWindow(GetDlgItem(ghDlg, IDC_BUTTON_SRC), SW_HIDE);
542 :     ShowWindow(GetDlgItem(ghDlg, IDC_EDIT_DST), SW_HIDE);
543 :     ShowWindow(GetDlgItem(ghDlg, IDC_BUTTON_DST), SW_HIDE);
544 :     ShowWindow(GetDlgItem(ghDlg, IDC_TARGET_LABEL), SW_HIDE);
545 :     ShowWindow(m_ProgressWnd, SW_SHOW);
546 :     SetDlgItemText(ghDlg, IDC_SOURCE_LABEL, buf);
547 :     }
548 :    
549 :     return hr;
550 :     }
551 :    
552 :     HRESULT
553 :     RecompressGraph::AddAudioStreams(int check_only)
554 :     {
555 :     IPin *pOutPin=0, *pInPin = 0;
556 :     PIN_INFO PinInfo = {NULL};
557 :     HRESULT hr = S_OK;
558 :     int bFraunhoferPro = 0;
559 :    
560 :     HKEY hKey;
561 :     DWORD size = MAX_PATH;
562 :     TCHAR kvalue[MAX_PATH];
563 :     RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Drivers32"), 0, KEY_READ, &hKey);
564 :     if (RegQueryValueEx(hKey, TEXT("msacm.l3acm"), 0, 0, (LPBYTE)kvalue, &size) != ERROR_SUCCESS)
565 :     bFraunhoferPro = 0;
566 :     else {
567 :     // C:\WINDOWS\System32\l3codecp.acm was not recognized
568 : Isibaar 2019 int sLen = _tcslen(kvalue);
569 :     if ((sLen >= 12) && (_tcscmp(&(kvalue[sLen - 12]), TEXT("l3codecp.acm")) == 0))
570 : Irhall 2014 bFraunhoferPro = 1;
571 :     }
572 :    
573 :     if (m_pVideoMeter) {
574 :     if (GetFilterPin(m_pVideoMeter, PINDIR_INPUT, 1, &pInPin,
575 :     MEDIATYPE_Video, GUID_NULL) == S_OK)
576 :     {
577 :     hr = pInPin->ConnectedTo(&pOutPin);
578 :     if (hr == S_OK) hr = pOutPin->QueryPinInfo(&PinInfo);
579 :     if (pOutPin) pOutPin->Release();
580 :     if (pInPin) pInPin->Release();
581 :     }
582 : Isibaar 2029 } else if (m_pSplitter) hr = m_pSplitter->QueryInterface(IID_IBaseFilter, (void **)&PinInfo.pFilter);
583 :     else hr = m_pSrcFilter->QueryInterface(IID_IBaseFilter, (void **)&PinInfo.pFilter);
584 : Irhall 2014
585 :     IPin *pAudioPin = 0;
586 :     if (check_only) {
587 :     if ((hr == S_OK) && (GetFilterPin(PinInfo.pFilter, PINDIR_OUTPUT, 0,
588 :     &pAudioPin, MEDIATYPE_Audio, GUID_NULL) == S_OK))
589 :     {
590 :     AM_MEDIA_TYPE *paomt = 0;
591 :     int bNeedsRecompression = 0;
592 :     IEnumMediaTypes *pAmtEnum = 0;
593 :     hr = pAudioPin->EnumMediaTypes(&pAmtEnum);
594 :    
595 :     if (hr == S_OK) {
596 :     hr = pAmtEnum->Reset();
597 :     if (hr == S_OK) hr = pAmtEnum->Next(1, &paomt, 0);
598 :     if (hr == S_OK && paomt->subtype.Data1 != 0x55 &&
599 :     (paomt->subtype.Data1 != 0x2000)) // don't recompress MP3/AC3
600 :     {
601 :     bNeedsRecompression = 1;
602 :     m_bFileCopy = 0; // file copy strategy cannot be used on this input
603 :     }
604 :     else if (((MPEGLAYER3WAVEFORMAT *)paomt->pbFormat)->fdwFlags & 4) { // VBR
605 :     bNeedsRecompression = 1;
606 :     }
607 :     if (pAmtEnum) pAmtEnum->Release();
608 :     pAmtEnum = 0;
609 :     if (paomt) DeleteMediaType(paomt);
610 :     if (hr == S_OK && bNeedsRecompression && !bFraunhoferPro) {
611 :     //MessageBox(0, "Error: Windows Media Player 11 needs to be installed to convert this Source File", APP_NAME, 0);
612 :     hr = VFW_E_CANNOT_CONNECT;
613 :     }
614 :     }
615 :     pAudioPin->Release();
616 :     }
617 :     }
618 :     else {
619 :     while ((hr == S_OK) && (GetFilterPin(PinInfo.pFilter, PINDIR_OUTPUT, 0,
620 :     &pAudioPin, MEDIATYPE_Audio, GUID_NULL) == S_OK))
621 :     {
622 :     AM_MEDIA_TYPE *paomt = 0;
623 :     int bNeedsRecompression = 0;
624 :     IEnumMediaTypes *pAmtEnum = 0;
625 :     hr = pAudioPin->EnumMediaTypes(&pAmtEnum);
626 :     if (hr == S_OK) {
627 :     hr = pAmtEnum->Reset();
628 :     if (hr == S_OK) hr = pAmtEnum->Next(1, &paomt, 0);
629 :     if ((hr == S_OK) && (paomt->subtype.Data1 != 0x55) &&
630 :     (paomt->subtype.Data1 != 0x2000)) // don't recompress MP3/AC3
631 :     {
632 :     bNeedsRecompression = 1;
633 :     m_bFileCopy = 0; // file copy strategy cannot be used on this input
634 :     }
635 : Isibaar 2029 else if ((paomt->subtype.Data1 == 0x55) &&
636 :     (((MPEGLAYER3WAVEFORMAT *)paomt->pbFormat)->fdwFlags != MPEGLAYER3_FLAG_PADDING_ON)){ // VBR
637 : Irhall 2014 bNeedsRecompression = 1;
638 :     }
639 :     if (pAmtEnum) pAmtEnum->Release();
640 :     pAmtEnum = 0;
641 :    
642 :     if (paomt) DeleteMediaType(paomt);
643 :     if (hr == S_OK && bNeedsRecompression) {
644 :    
645 :     IBaseFilter *pAudioCompressor = 0;
646 :     hr = AddDirectXFilterByMoniker(CLSID_AudioCompressorCategory, "MPEG Layer-3", &pAudioCompressor, 0);
647 :     if (hr == S_OK) hr = m_pGraph->AddFilter(pAudioCompressor, 0);
648 :    
649 : Isibaar 2029 IBaseFilter *pFFdA=0;
650 :     if (hr == S_OK) AddFilterByCLSID((GUID *)&CLSID_FFDshowAudioDecoder, &pFFdA);
651 :    
652 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, pAudioPin, pAudioCompressor, GUID_NULL, GUID_NULL, 0);
653 :    
654 :     RemoveIfUnconnectedInput(pFFdA);
655 :     if (pFFdA) pFFdA->Release();
656 : Irhall 2014
657 :     IPin *pMp3Pin = 0;
658 :     if (hr == S_OK) hr = GetFilterPin(pAudioCompressor, PINDIR_OUTPUT, 0, &pMp3Pin, MEDIATYPE_Audio, GUID_NULL);
659 :     IEnumMediaTypes *pMp3MtEnum = 0;
660 :    
661 :     if (hr == S_OK) hr = pMp3Pin->EnumMediaTypes(&pMp3MtEnum);
662 :     AM_MEDIA_TYPE *pMp3mt = 0;
663 :     int bConnected = 0;
664 :    
665 :     if (hr == S_OK) {
666 : Isibaar 2029 DWORD UsedDataRate = 0, MaxDataRate=0;
667 :     // Some files have mono track 8kHz, it's not possible to obtain 128kbps datarate
668 :     while (UsedDataRate != 16000 && (pMp3MtEnum->Next(1, &pMp3mt, 0) == S_OK)) {
669 : Irhall 2014 MPEGLAYER3WAVEFORMAT *pMp3cb = (MPEGLAYER3WAVEFORMAT *)pMp3mt->pbFormat;
670 :     if (
671 :     //pMp3cb->nSamplesPerSec == 44100 &&
672 : Isibaar 2029 pMp3cb->wfx.nAvgBytesPerSec == 16000) {
673 :     UsedDataRate = 16000;
674 :     }
675 :     if (MaxDataRate < pMp3cb->wfx.nAvgBytesPerSec)
676 :     MaxDataRate = pMp3cb->wfx.nAvgBytesPerSec;
677 :     DeleteMediaType(pMp3mt);
678 :     }
679 :     pMp3MtEnum->Reset();
680 :     if (UsedDataRate != 16000) UsedDataRate = MaxDataRate;
681 :    
682 :     while (!bConnected && (pMp3MtEnum->Next(1, &pMp3mt, 0) == S_OK)) {
683 :     MPEGLAYER3WAVEFORMAT *pMp3cb = (MPEGLAYER3WAVEFORMAT *)pMp3mt->pbFormat;
684 :     if (
685 :     //pMp3cb->nSamplesPerSec == 44100 &&
686 :     pMp3cb->wfx.nAvgBytesPerSec == UsedDataRate) {
687 :    
688 :     CUnknown *pMp3Normalizer = 0;
689 : Irhall 2014 IBaseFilter *pIMp3Normalizer = 0;
690 :     // Type 2 - only 16000 Bps = 128kbit audio
691 :     if (hr == S_OK) pMp3Normalizer = CProgressNotifyFilter::CreateInstance(0, &hr, 2);
692 :     if (hr == S_OK) hr = pMp3Normalizer->NonDelegatingQueryInterface(IID_IBaseFilter,
693 :     (void **)&pIMp3Normalizer);
694 :     if (hr == S_OK) hr = m_pGraph->AddFilter(pIMp3Normalizer, 0);
695 :    
696 : Isibaar 2045 IRecProgressNotify *pAudioRecNotify = 0;
697 :     pIMp3Normalizer->QueryInterface(IID_IRecProgressNotify, (void **)&pAudioRecNotify);
698 :     pAudioRecNotify->SetAudioBitrate(UsedDataRate);
699 :     pAudioRecNotify->Release();
700 :    
701 : Irhall 2014 IPin *pNormMp3Pin = 0;
702 :     if (hr == S_OK) hr = GetFilterPin(pIMp3Normalizer, PINDIR_INPUT, 0, &pNormMp3Pin, GUID_NULL, GUID_NULL);
703 :     if (hr == S_OK) hr = m_pGraph->ConnectDirect(pMp3Pin, pNormMp3Pin, pMp3mt);
704 :     if (pNormMp3Pin) pNormMp3Pin->Release();
705 :    
706 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, pIMp3Normalizer, m_pMuxer, GUID_NULL, GUID_NULL, 1);
707 :     if (pIMp3Normalizer) pIMp3Normalizer->Release();
708 :     if (hr == S_OK) bConnected = 1;
709 :     }
710 :     DeleteMediaType(pMp3mt);
711 :     }
712 :     }
713 :     if ((!bConnected || !bFraunhoferPro) && hr == S_OK) hr = VFW_E_CANNOT_CONNECT;
714 :     //if (hr == S_OK && !bFraunhoferPro) MessageBox(0, "Error: Windows Media Player 11 needs to be installed to convert this Source File", APP_NAME, 0);
715 :     if (pMp3Pin) pMp3Pin->Release();
716 :     if (pMp3MtEnum) pMp3MtEnum->Release();
717 :     if (pAudioCompressor) pAudioCompressor->Release();
718 :     }
719 :     else {
720 :     CUnknown *pMp3Normalizer = 0;
721 :     IBaseFilter *pIMp3Normalizer = 0;
722 :    
723 :     if (hr == S_OK) pMp3Normalizer = CProgressNotifyFilter::CreateInstance(0, &hr, 1);
724 :     if (hr == S_OK) hr = pMp3Normalizer->NonDelegatingQueryInterface(IID_IBaseFilter, (void **)&pIMp3Normalizer);
725 :     if (hr == S_OK) hr = m_pGraph->AddFilter(pIMp3Normalizer, 0);
726 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, pAudioPin, pIMp3Normalizer, MEDIATYPE_Audio, GUID_NULL, 0);
727 :     if (hr == S_OK) hr = ConnectFilters(m_pGraph, pIMp3Normalizer, m_pMuxer, MEDIATYPE_Audio, GUID_NULL, 0);
728 :     if (pIMp3Normalizer) pIMp3Normalizer->Release();
729 :     }
730 :     if (hr == S_OK && PinInfo.pFilter == m_pSrcFilter) m_UsedStreamsCnt++;
731 : Isibaar 2029 if (hr == S_OK && PinInfo.pFilter == m_pSplitter) m_UsedStreamsCnt++;
732 : Irhall 2014 }
733 :     pAudioPin->Release();
734 :     }
735 :     }
736 :    
737 :     if (PinInfo.pFilter) PinInfo.pFilter->Release();
738 :     return hr;
739 :     }
740 :    
741 :     HRESULT
742 :     RecompressGraph::AddFilterByCLSID(GUID *in_Filter, IBaseFilter **out_pFilter)
743 :     {
744 :     HRESULT hr = CoCreateInstance(*in_Filter, NULL, CLSCTX_INPROC_SERVER,
745 :     IID_IBaseFilter, (void **)out_pFilter);
746 :     if (hr == S_OK) hr = m_pGraph->AddFilter(*out_pFilter, NULL);
747 :    
748 :     return hr;
749 :     }
750 :    
751 :     HRESULT
752 :     RecompressGraph::SetDstFileName(LPCTSTR in_szFilePath)
753 :     {
754 :     if (m_pGraph)
755 :     return E_UNEXPECTED;
756 :    
757 :     if (m_szDstFilePath)
758 :     free (m_szDstFilePath);
759 :    
760 : Isibaar 2019 m_szDstFilePath = _tcsdup(in_szFilePath);
761 : Irhall 2014
762 :     return S_OK;
763 :     }
764 :    
765 :     HRESULT
766 :     RecompressGraph::AddFileWriter(LPCTSTR in_szFilePath)
767 :     {
768 :     if (!m_pGraph || m_pFileWriter)
769 :     return E_UNEXPECTED;
770 :    
771 : Isibaar 2019 int sLen = _tcslen(in_szFilePath);
772 :     OLECHAR *pwName = new OLECHAR [sLen+1];
773 :     #ifndef UNICODE
774 :     mbstowcs(pwName, in_szFilePath, sLen+1);
775 :     #else
776 :     wcsncpy(pwName, in_szFilePath, sLen+1);
777 :     #endif
778 :    
779 : Irhall 2014 HRESULT hr = AddFilterByCLSID((GUID *)&CLSID_FileWriter, &m_pFileWriter);
780 :     IFileSinkFilter* pDst=0;
781 :     if (hr == S_OK) hr = m_pFileWriter->QueryInterface(IID_IFileSinkFilter, (void **)(&pDst));
782 : Isibaar 2019 if (hr == S_OK) hr = pDst->SetFileName(pwName, &m_DstFileMediaType);
783 :     delete [] pwName;
784 : Irhall 2014 if (pDst)
785 :     pDst->Release();
786 :    
787 :     return hr;
788 :     }
789 :    
790 :     HRESULT
791 :     RecompressGraph::AddSourceFile(LPCTSTR in_szFilePath)
792 :     {
793 :     if (m_pGraph) return E_UNEXPECTED;
794 :    
795 :     if (m_szSourceFilePath)
796 :     free (m_szSourceFilePath);
797 :    
798 : Isibaar 2019 m_szSourceFilePath = _tcsdup(in_szFilePath);
799 : Irhall 2014
800 :     return S_OK;
801 :     }
802 :    
803 :     HRESULT
804 :     RecompressGraph::AddSourceFilter(LPCTSTR in_szFilePath)
805 :     {
806 : Isibaar 2045 HRESULT hr = S_OK;
807 : Irhall 2014 if (!m_pGraph || m_pSrcFilter) return E_UNEXPECTED;
808 :    
809 : Isibaar 2045 BYTE StartBytes[512];
810 :     DWORD cbReadHdr=0;
811 :     int ExpectedType=0;
812 :     HANDLE hFile = CreateFile(in_szFilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
813 :     if (hFile != INVALID_HANDLE_VALUE) {
814 :     if (ReadFile(hFile, StartBytes, sizeof(StartBytes), &cbReadHdr, 0) && cbReadHdr == sizeof(StartBytes)) {
815 :     if ((*(int *)StartBytes) == 'FFIR') ExpectedType = 1; // AVI
816 :     else if (((int *)StartBytes)[1] == 'pytf' || ((int *)StartBytes)[1] == 'tadm' || ((int *)StartBytes)[1] == 'voom') ExpectedType = 2; // MOV/MP4
817 :     else if (((int *)StartBytes)[0] == 0xA3DF451A) {
818 :     for (int iPos = 4; (ExpectedType == 0 && iPos +11 < sizeof(StartBytes)); iPos++) {
819 :     if ((*(int *)&(StartBytes[iPos]) == 0x6D888242) && (*(int *)&(StartBytes[iPos+3]) == 'rtam')
820 :     && (*(int *)&(StartBytes[iPos+7]) == 'akso')) {
821 :     ExpectedType = 3; // MKV
822 :     }
823 :     }
824 :     } else if (((int *)StartBytes)[0] == 0x75B22630) ExpectedType = 4; // WMV/ASF
825 :     } else hr = VFW_E_FILE_TOO_SHORT;
826 :     CloseHandle(hFile);
827 :     } else return VFW_E_NOT_FOUND;
828 :     if (hr != S_OK) return hr;
829 :    
830 : Isibaar 2019 int sLen = _tcslen(in_szFilePath);
831 :     OLECHAR *pwName = new OLECHAR [sLen+1];
832 :     #ifndef UNICODE
833 :     mbstowcs(pwName, in_szFilePath, sLen+1);
834 :     #else
835 :     wcsncpy(pwName, in_szFilePath, sLen+1);
836 :     #endif
837 : Irhall 2014
838 : Isibaar 2045 // preferred configuration:
839 :     // ASF/WMV : WM ASF Reader
840 :     // AVI: Async Reader -> Avi Splitter
841 :     // MOV/MP4: Async Reader -> Lav splitter (secondary Haali splitter) (Haali has problems with some movs and mp4 files)
842 :     // MKV: Async Reader -> Haali splitter (secondary Lav splitter)
843 :     // if configuration Reader->Splitter is not available for mov/mp4/mkv, then reader+splitter is tried.
844 :     // if everything fails, we let the GraphBuilder to choose filters by default.
845 : Isibaar 2019
846 : Isibaar 2029 IPin* pOutSrcPin=0, *pVideoPin=0;
847 : Isibaar 2045 if (ExpectedType == 4) m_bIsWMV = 1;
848 :     else {
849 :     hr = TrySourceFilter(pwName, (GUID *)&CLSID_AsyncReader);
850 :    
851 :     if (hr == S_OK) hr = GetFilterPin(m_pSrcFilter, PINDIR_OUTPUT, 0, &pOutSrcPin, GUID_NULL, GUID_NULL);
852 :     IEnumMediaTypes* pEnumSrcMediaTypes=0;
853 :     if (hr == S_OK) hr = pOutSrcPin->EnumMediaTypes(&pEnumSrcMediaTypes);
854 :     AM_MEDIA_TYPE *pMt =0;
855 :     if (hr == S_OK) hr = pEnumSrcMediaTypes->Next(1, &pMt, 0);
856 :     while (hr == S_OK && (pMt->majortype != MEDIATYPE_Stream || pMt->subtype == GUID_NULL)) {
857 :     DeleteMediaType(pMt);
858 :     pMt = 0;
859 :     hr = pEnumSrcMediaTypes->Next(1, &pMt, 0);
860 :     }
861 :     if (hr == S_OK) m_SrcFileMediaType = *pMt;
862 :     if (pMt) DeleteMediaType(pMt);
863 :     if (pEnumSrcMediaTypes) pEnumSrcMediaTypes->Release();
864 :     if (hr == S_OK) {
865 :     if (m_SrcFileMediaType.majortype == MEDIATYPE_Stream) {
866 :     if (m_SrcFileMediaType.subtype == MEDIASUBTYPE_Asf ||
867 :     m_SrcFileMediaType.subtype == MEDIASUBTYPE_ASF) {
868 :     m_bIsWMV = 1;
869 :     } else if (m_SrcFileMediaType.subtype == MEDIASUBTYPE_Avi)
870 :     hr = AddFilterByCLSID((GUID *)&CLSID_AviSplitter, &m_pSplitter);
871 :     else {
872 :     hr = AddFilterByCLSID((GUID *)((ExpectedType == 3) ? &CLSID_HaaliMediaSplitter_AR : &CLSID_LAVSplitter), &m_pSplitter);
873 : Isibaar 2029 if (hr == S_OK) {
874 :     hr = ConnectFilters(m_pGraph, pOutSrcPin, m_pSplitter);
875 :     if (hr == S_OK) hr = GetFilterPin(m_pSplitter, PINDIR_OUTPUT, 0, &pVideoPin, MEDIATYPE_Video, GUID_NULL);
876 : Isibaar 2045 if (m_pSplitter && !pVideoPin) {
877 : Isibaar 2029 m_pGraph->RemoveFilter(m_pSplitter);
878 :     m_pSplitter->Release();
879 :     m_pSplitter = 0;
880 :     }
881 :     if (pVideoPin) {pVideoPin->Release(); pVideoPin = 0;}
882 :     }
883 : Isibaar 2045 if (!m_pSplitter) {
884 :     hr = AddFilterByCLSID((GUID *)((ExpectedType != 3) ? &CLSID_HaaliMediaSplitter_AR : &CLSID_LAVSplitter), &m_pSplitter);
885 :     if (hr == S_OK) {
886 :     hr = ConnectFilters(m_pGraph, pOutSrcPin, m_pSplitter);
887 :     if (hr == S_OK) hr = GetFilterPin(m_pSplitter, PINDIR_OUTPUT, 0, &pVideoPin, MEDIATYPE_Video, GUID_NULL);
888 :     if (m_pSplitter && !pVideoPin) {
889 :     m_pGraph->RemoveFilter(m_pSplitter);
890 :     m_pSplitter->Release();
891 :     m_pSplitter = 0;
892 :     }
893 :     if (pVideoPin) {pVideoPin->Release(); pVideoPin = 0;}
894 :     }
895 :     }
896 : Isibaar 2029 }
897 : Isibaar 2045 } else hr = VFW_E_TYPE_NOT_ACCEPTED;
898 :     }
899 : Isibaar 2029 }
900 :    
901 :     if (m_bIsWMV || !m_pSplitter) {
902 :     if (m_pSrcFilter) {
903 :     m_pGraph->RemoveFilter(m_pSrcFilter);
904 :     m_pSrcFilter->Release();
905 :     }
906 :     m_pSrcFilter = 0;
907 :     if (m_bIsWMV) hr = TrySourceFilter(pwName, (GUID *)&CLSID_WMAsfReader );
908 :     else {
909 : Isibaar 2045 hr = TrySourceFilter(pwName, (GUID *)(ExpectedType == 3 ? &CLSID_HaaliMediaSplitter : &CLSID_LAVSource));
910 : Isibaar 2029 if (hr == S_OK) hr = GetFilterPin(m_pSrcFilter, PINDIR_OUTPUT, 0, &pVideoPin, MEDIATYPE_Video, GUID_NULL);
911 :     if (!m_pSrcFilter || !pVideoPin) {
912 :     if (!pVideoPin && m_pSrcFilter) {
913 :     m_pGraph->RemoveFilter(m_pSrcFilter);
914 :     m_pSrcFilter->Release();
915 :     m_pSrcFilter = 0;
916 :     }
917 : Isibaar 2045 if (!m_pSrcFilter) hr = TrySourceFilter(pwName, (GUID *)(ExpectedType != 3 ? &CLSID_HaaliMediaSplitter : &CLSID_LAVSource));
918 : Isibaar 2029 if (hr == S_OK) hr = GetFilterPin(m_pSrcFilter, PINDIR_OUTPUT, 0, &pVideoPin, MEDIATYPE_Video, GUID_NULL);
919 :     if (!pVideoPin && m_pSrcFilter) {
920 :     m_pGraph->RemoveFilter(m_pSrcFilter);
921 :     m_pSrcFilter->Release();
922 :     m_pSrcFilter = 0;
923 :     }
924 :     }
925 :     if (pVideoPin) {
926 :     pVideoPin->Release();
927 :     pVideoPin = 0;
928 :     }
929 :     }
930 :     if (!m_pSrcFilter) hr = m_pGraph->AddSourceFilter(pwName, 0, &m_pSrcFilter);
931 :     }
932 :     if (pOutSrcPin) pOutSrcPin->Release();
933 : Irhall 2014
934 :     return hr;
935 :     }
936 :    
937 :     HRESULT
938 : Isibaar 2029 RecompressGraph::TrySourceFilter(LPCOLESTR pwName, GUID *in_Clsid)
939 :     {
940 :     HRESULT hr = AddFilterByCLSID(in_Clsid, &m_pSrcFilter);
941 :     IFileSourceFilter* pSrc =0 ;
942 :     if (hr == S_OK) hr = m_pSrcFilter->QueryInterface(IID_IFileSourceFilter, (void **)(&pSrc));
943 :     if (hr == S_OK) hr = pSrc->Load(pwName, 0);//&m_SrcFileMediaType);
944 :     if (pSrc) pSrc->Release();
945 :     if (hr != S_OK && m_pSrcFilter) {
946 :     m_pGraph->RemoveFilter(m_pSrcFilter);
947 :     m_pSrcFilter->Release();
948 :     m_pSrcFilter = 0;
949 :     }
950 :     return hr;
951 :     }
952 :    
953 :     HRESULT
954 : Irhall 2014 RecompressGraph::SetProgress(int elapsedSize, int totalSize)
955 :     {
956 :    
957 :     m_elapsedSize = elapsedSize;
958 :     m_totalSize = totalSize;
959 :    
960 :     return S_OK;
961 :     }
962 :    
963 :     HRESULT
964 : Isibaar 2029 RecompressGraph::WaitForCompletion(long *out_Evt)
965 :     {
966 :     long Evt, P1, P2;
967 : Isibaar 2045 // TCHAR szErrorStr[150];
968 : Isibaar 2029 HRESULT hr = S_OK;
969 :     do {
970 :     hr = m_pEvent->GetEvent(&Evt, &P1, &P2, INFINITE);
971 :     if (Evt == EC_ERRORABORT) {
972 : Isibaar 2045 // _stprintf(szErrorStr, _TEXT("Error ocurred\nCode=%08X, %08X\nContinue?"), P1, P2);
973 :     // if (MessageBox(ghDlg, szErrorStr, 0, MB_YESNO) == IDYES) Evt = 0;
974 :     if (P1 == 0xC00D002F && m_bIsWMV)
975 :     Evt = 0;
976 : Isibaar 2029 }
977 :     } while (Evt != EC_COMPLETE && Evt != EC_STREAM_ERROR_STOPPED && Evt != EC_USERABORT && Evt != EC_ERRORABORT);
978 :     *out_Evt = Evt;
979 :     return S_OK;
980 :     }
981 :    
982 :     HRESULT
983 : Irhall 2014 RecompressGraph::Recompress()
984 :     {
985 :     if (!m_pGraph) return E_UNEXPECTED;
986 :    
987 : Isibaar 2029 long E = 0;
988 : Irhall 2014 HRESULT hr = S_OK;
989 :    
990 :     if (hr == S_OK && m_bFileCopy == 1 &&
991 :     fourcc_helper(m_szSourceFilePath, NULL, NULL, 1)==0) // check input is AVI !
992 :     {
993 :     if (!CopyFileEx(m_szSourceFilePath, m_szDstFilePath,
994 :     CopyProgressRoutine, this, FALSE, 0) || m_bBreakRequested) {
995 :     hr = E_FAIL;
996 :     DeleteFile(m_szDstFilePath);
997 :     }
998 :    
999 :     goto finish_recompress;
1000 :     }
1001 :    
1002 : Isibaar 2029 hr = m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pControl);
1003 :     if (hr == S_OK) hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void **)&m_pEvent);
1004 : Irhall 2014
1005 :     IRecProgressNotify *pBitrateProgressNotify=0;
1006 :     OAFilterState oas;
1007 :    
1008 :     if (hr == S_OK) hr = (m_pVideoMeter ? m_pVideoMeter : m_pChgType)->QueryInterface(IID_IRecProgressNotify,
1009 :     (void **)&pBitrateProgressNotify);
1010 :    
1011 :     //if (hr == S_OK) hr = (m_pVideoMeter ? m_pVideoMeter : (m_pEmmsDummy ? m_pEmmsDummy : m_pChgType))->QueryInterface(IID_IRecProgressNotify, (void **)&pBitrateProgressNotify);
1012 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetTotalFrames(m_TotalFrames);
1013 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetNotifyWnd(m_ProgressWnd);
1014 :    
1015 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetTotalSize(m_totalSize);
1016 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetElapsedSize(m_elapsedSize);
1017 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetCurSize(m_curSize);
1018 :    
1019 :     if (m_pXvidEncoder) {
1020 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetPass(0);
1021 :    
1022 :     m_pXvidConfig->Release();
1023 :     m_pXvidConfig = 0;
1024 :    
1025 :     if (hr == S_OK) {
1026 : Isibaar 2029 hr = m_pControl->Run();
1027 : Irhall 2014 if (hr == S_OK) {
1028 : Isibaar 2029 do hr = m_pControl->GetState(1000, &oas);
1029 : Irhall 2014 while ((oas != State_Running && hr == S_OK ) || hr == VFW_S_STATE_INTERMEDIATE || hr == S_FALSE);
1030 :     }
1031 :     }
1032 :    
1033 :     //long Evt, P1, P2;
1034 :     //if (hr == S_OK) do hr = pEvent->GetEvent(&Evt, &P1, &P2, INFINITE);
1035 :     //while (Evt != EC_COMPLETE && Evt != 3 && Evt != 2);
1036 : Isibaar 2029 //if (hr == S_OK) hr = m_pEvent->WaitForCompletion(INFINITE, &E);
1037 :     hr = WaitForCompletion(&E);
1038 : Irhall 2014
1039 : Isibaar 2029 if (m_pControl) {
1040 : Irhall 2014 HRESULT hrTmp;
1041 :     if (E == EC_USERABORT) m_pSrcFilter->Stop();
1042 : Isibaar 2029 hrTmp = m_pControl->Stop();
1043 :     do hrTmp = m_pControl->GetState(1000, &oas);
1044 : Irhall 2014 while ((oas != State_Stopped && hrTmp == S_OK ) ||
1045 :     hrTmp == VFW_S_STATE_INTERMEDIATE || hrTmp == S_FALSE);
1046 :     if (hr == S_OK) hr = hrTmp;
1047 :     }
1048 :     if (E == EC_ERRORABORT) hr = E_FAIL;
1049 :    
1050 :     // End of pass 1
1051 :     pBitrateProgressNotify->GetDimensions(m_Width, m_Height);
1052 :     pBitrateProgressNotify->GetBitrate(m_CountedFrames, m_TotalFramesSize);
1053 :     pBitrateProgressNotify->Release();
1054 :     pBitrateProgressNotify = 0;
1055 : Isibaar 2019 TCHAR *pSrcStr = _tcsdup(m_szSourceFilePath);
1056 :     TCHAR *pDstStr = _tcsdup(m_szDstFilePath);
1057 : Irhall 2014
1058 : Isibaar 2029 m_pControl->Release();
1059 :     m_pEvent->Release();
1060 :     m_pControl = 0;
1061 :     m_pEvent = 0;
1062 : Irhall 2014 int bBreakRequested = m_bBreakRequested;
1063 :     //if (hr == S_OK)
1064 : Isibaar 2045
1065 :     m_pIChgTypeNorm->GetMeasuredTimes(m_MinETime, m_MaxETime, m_MinSTime, m_MaxSTime);
1066 :    
1067 :     CleanUp();
1068 : Irhall 2014 m_szSourceFilePath = pSrcStr;
1069 :     m_szDstFilePath = pDstStr;
1070 :    
1071 :     if (bBreakRequested || hr != S_OK) {
1072 :     DeleteFile(m_szDstFilePath);
1073 : Isibaar 2019 DeleteFile(Pass_FILE);
1074 : Irhall 2014 hr = E_FAIL;
1075 :     goto finish_recompress;
1076 :     }
1077 :    
1078 :     if (hr == S_OK) hr = CreateGraph(m_ProgressWnd, 2);
1079 : Isibaar 2029 if (hr == S_OK) hr = m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pControl);
1080 :     if (hr == S_OK) hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void **)&m_pEvent);
1081 : Irhall 2014
1082 :     if (hr == S_OK) hr = (m_pVideoMeter ? m_pVideoMeter : m_pChgType)->QueryInterface(IID_IRecProgressNotify,
1083 :     (void **)&pBitrateProgressNotify);
1084 :     //if (hr == S_OK) hr = (m_pVideoMeter ? m_pVideoMeter : (m_pEmmsDummy ? m_pEmmsDummy : m_pChgType))->QueryInterface(IID_IRecProgressNotify, (void **)&pBitrateProgressNotify);
1085 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetTotalFrames(m_CountedFrames);
1086 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetNotifyWnd(m_ProgressWnd);
1087 :    
1088 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetPass(2);
1089 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetTotalSize(m_totalSize);
1090 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetElapsedSize(m_elapsedSize);
1091 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetCurSize(m_curSize);
1092 :     }
1093 :     else {
1094 :     if (hr == S_OK) hr = pBitrateProgressNotify->SetPass(1);
1095 :     }
1096 :    
1097 :     DeleteFile(m_szDstFilePath);
1098 :    
1099 :     if (hr == S_OK) {
1100 : Isibaar 2029 hr = m_pControl->Run();
1101 : Irhall 2014 if (hr == S_OK) {
1102 : Isibaar 2029 do hr = m_pControl->GetState(1000, &oas);
1103 : Irhall 2014 while ((oas != State_Running && hr == S_OK ) || hr == VFW_S_STATE_INTERMEDIATE || hr == S_FALSE);
1104 :     }
1105 :     }
1106 :    
1107 : Isibaar 2029 //#if 0
1108 :     // long Evt, P1, P2;
1109 :     //if (hr == S_OK) do hr = m_pEvent->GetEvent(&Evt, &P1, &P2, INFINITE);
1110 :     //while (Evt != EC_COMPLETE && (Evt != 3 || P1 == 0xC00D002F) && Evt != 2);
1111 : Irhall 2014
1112 : Isibaar 2029 //for (int iCnt=0; (iCnt < m_UsedStreamsCnt) && (hr == S_OK); iCnt++) {
1113 :     // if (hr == S_OK) do hr = pEvent->WaitForCompletion(INFINITE, &E);
1114 :     // while (hr == S_OK && E != 1);
1115 :     //}
1116 :     //#endif
1117 :     //if (hr == S_OK) hr = pEvent->WaitForCompletion(INFINITE, &E);
1118 :     // long Evt;
1119 :     if (hr == S_OK) hr = WaitForCompletion(&E);
1120 : Irhall 2014
1121 : Isibaar 2029 if (m_pControl) {
1122 : Irhall 2014 if (E == EC_USERABORT) m_pSrcFilter->Stop();
1123 : Isibaar 2029 HRESULT hrTmp = m_pControl->Stop();
1124 :     do hrTmp = m_pControl->GetState(1000, &oas);
1125 : Irhall 2014 while ((oas != State_Stopped && hrTmp == S_OK ) ||
1126 :     hrTmp == VFW_S_STATE_INTERMEDIATE || hrTmp == S_FALSE);
1127 :     if (hr == S_OK) hr = hrTmp;
1128 :     }
1129 :     if (E == EC_ERRORABORT) hr = E_FAIL;
1130 :    
1131 : Isibaar 2029 if (m_pControl) m_pControl->Release();
1132 :     if (m_pEvent) m_pEvent->Release();
1133 : Irhall 2014 if (pBitrateProgressNotify) pBitrateProgressNotify->Release();
1134 :    
1135 : Isibaar 2019 DeleteFile(Pass_FILE);
1136 : Irhall 2014
1137 :     finish_recompress:
1138 :    
1139 :     if (m_bFileCopy && (hr != E_FAIL))
1140 :     fourcc_helper(m_szDstFilePath, "XVID", "xvid", 0);
1141 :    
1142 :     if ((E == EC_COMPLETE || (m_bFileCopy && hr != E_FAIL)) &&
1143 :     ((int)(m_elapsedSize+m_curSize) >= m_totalSize))
1144 :     {
1145 :     MessageBox(ghDlg, TEXT("Conversion finished!"), APP_NAME, 0);
1146 :     }
1147 :    
1148 :     m_Width = m_Height = 0;
1149 :    
1150 :     if ((int)(m_elapsedSize+m_curSize) >= m_totalSize)
1151 :     {
1152 :     SetDlgItemText(ghDlg, IDC_SOURCE_LABEL, TEXT("Source file:"));
1153 :     SetDlgItemText(ghDlg, IDC_EDIT_SRC, TEXT(""));
1154 :     SetDlgItemText(ghDlg, IDC_EDIT_DST, TEXT(""));
1155 :     ShowWindow(m_ProgressWnd, SW_HIDE);
1156 :     ShowWindow(GetDlgItem(ghDlg, IDC_TARGET_LABEL), SW_SHOW);
1157 :     ShowWindow(GetDlgItem(ghDlg, IDC_EDIT_SRC), SW_SHOW);
1158 :     ShowWindow(GetDlgItem(ghDlg, IDC_BUTTON_SRC), SW_SHOW);
1159 :     ShowWindow(GetDlgItem(ghDlg, IDC_EDIT_DST), SW_SHOW);
1160 :     ShowWindow(GetDlgItem(ghDlg, IDC_BUTTON_DST), SW_SHOW);
1161 :     OpenFilePath[0] = TEXT('\0');
1162 :     OpenFilePath[1] = TEXT('\0');
1163 :     sOfn.lpstrFile[0] = TEXT('\0');
1164 :     sOfn.lpstrFile[1] = TEXT('\0');
1165 :     SaveFilePath[0] = TEXT('\0');
1166 :     }
1167 :    
1168 :     return hr;
1169 :     }
1170 :    
1171 :     void
1172 :     RecompressGraph::BreakConversion()
1173 :     {
1174 :     m_bBreakRequested = 1;
1175 :     if (m_pGraph) {
1176 :     IMediaEventSink *pSink = 0;
1177 :     m_pGraph->QueryInterface(IID_IMediaEventSink, (void **)&pSink);
1178 :    
1179 :     if (pSink) {
1180 :     pSink->Notify(EC_USERABORT, 0, 0);
1181 :     pSink->Release();
1182 :     }
1183 :     }
1184 :     }
1185 :    
1186 : Isibaar 2029 void
1187 :     RecompressGraph::RemoveIfUnconnectedInput(IBaseFilter *in_pFilter)
1188 :     {
1189 :     IPin *pPin;
1190 :     HRESULT hr = GetFilterPin(in_pFilter, PINDIR_INPUT, 0, &pPin, GUID_NULL, GUID_NULL);
1191 :     if (hr == S_OK && pPin) {
1192 :     pPin->Release();
1193 :     m_pGraph->RemoveFilter(in_pFilter);
1194 :     }
1195 :     }
1196 : Irhall 2014
1197 :     ////////////////////////////////////////////////////////////////////////////////
1198 :     // main.cpp
1199 :    
1200 :     DWORD WINAPI
1201 :     RecompressThreadProc(LPVOID xParam)
1202 :     {
1203 :     CoInitialize(0);
1204 :    
1205 :     int error = 0;
1206 :     int nbInputFiles = 1;
1207 :     int totalSize = 0;
1208 :     int elapsedSize = 0;
1209 :     TCHAR *NextFileName = SrcFile;
1210 :     TCHAR tempFile[2*MAX_PATH];
1211 :    
1212 : Isibaar 2019 if(SrcFile[_tcslen(SrcFile) + 1] != TEXT('\0')) // Multiple input files?
1213 : Irhall 2014 {
1214 :     nbInputFiles = 0;
1215 : Isibaar 2019 NextFileName = &NextFileName[_tcslen(NextFileName) + 1];
1216 : Irhall 2014
1217 :     while(NextFileName[0] != L'\0') // Count total filesize and number of source files
1218 :     {
1219 :     DWORD filesize;
1220 :     PathCombine(tempFile, SrcFile, NextFileName);
1221 :     DetermineFileSize(tempFile, &filesize);
1222 :     totalSize += filesize;
1223 :    
1224 : Isibaar 2019 NextFileName = &NextFileName[_tcslen(NextFileName) + 1];
1225 : Irhall 2014 nbInputFiles++;
1226 :     }
1227 :     }
1228 :    
1229 :     NextFileName = SrcFile;
1230 :     if (nbInputFiles > 1) {
1231 : Isibaar 2019 NextFileName = &NextFileName[_tcslen(NextFileName) + 1]; // Bypass dir and jump to first filename
1232 : Irhall 2014 }
1233 :    
1234 :     HRESULT hr = S_OK;
1235 :     int count = 0;
1236 :    
1237 :     while ((count < nbInputFiles) && (hr == S_OK)) {
1238 :     TCHAR szDstPath[2*MAX_PATH];
1239 :    
1240 :     count++;
1241 :    
1242 :     if (nbInputFiles == 1) {
1243 : Isibaar 2019 _tcscpy(tempFile, SrcFile); // Src
1244 : Irhall 2014
1245 :     PathStripPath(SrcFile);
1246 :    
1247 : Isibaar 2019 LPTSTR ext = PathFindExtension(SrcFile);
1248 : Irhall 2014 TCHAR sztmpPath[2*MAX_PATH];
1249 : Isibaar 2019 _tcsncpy(sztmpPath, SrcFile, (ext-SrcFile));
1250 : Irhall 2014 sztmpPath[ext-SrcFile] = TEXT('\0');
1251 : Isibaar 2019 _stprintf(sztmpPath, TEXT("%s_Xvid.avi"), sztmpPath);
1252 : Irhall 2014
1253 :     PathCombine(szDstPath, DstFile, sztmpPath); // Dst
1254 :     }
1255 :     else {
1256 :     PathCombine(tempFile, SrcFile, NextFileName); // Src
1257 :    
1258 : Isibaar 2019 LPTSTR ext = PathFindExtension(NextFileName);
1259 : Irhall 2014 TCHAR sztmpPath[2*MAX_PATH];
1260 : Isibaar 2019 _tcsncpy(sztmpPath, NextFileName, (ext-NextFileName));
1261 : Irhall 2014 sztmpPath[ext-NextFileName] = TEXT('\0');
1262 : Isibaar 2019 _stprintf(sztmpPath, TEXT("%s_Xvid.avi"), sztmpPath);
1263 : Irhall 2014
1264 :     PathCombine(szDstPath, DstFile, sztmpPath); // Dst
1265 :    
1266 : Isibaar 2019 NextFileName = &NextFileName[_tcslen(NextFileName) + 1];
1267 : Irhall 2014 }
1268 :    
1269 :     pRec->CleanUp();
1270 :    
1271 :     pRec->AddSourceFile(tempFile);
1272 :     pRec->SetDstFileName(szDstPath);
1273 :     pRec->SetProgress(elapsedSize, totalSize);
1274 :    
1275 :     hr = pRec->CreateGraph((HWND)xParam, 1);
1276 :    
1277 :     if (hr == S_OK) {
1278 :     hr = pRec->Recompress();
1279 :    
1280 :     if (hr != S_OK) {
1281 :     MessageBox((HWND)xParam, TEXT("Error: Conversion failure."), APP_NAME, MB_OK);
1282 :     goto finish_thread;
1283 :     }
1284 :     }
1285 :     else {
1286 :     if (nbInputFiles == 1)
1287 :     MessageBox((HWND)xParam, TEXT("Error: Source file not supported."), APP_NAME, MB_OK);
1288 :     else
1289 :     error = 1; // show message later
1290 :     }
1291 :    
1292 :     DWORD filesize;
1293 :     DetermineFileSize(tempFile, &filesize);
1294 :    
1295 :     elapsedSize += filesize;
1296 :     }
1297 :    
1298 :     finish_thread:
1299 :     pRec->CleanUp();
1300 :    
1301 :     if (error)
1302 :     MessageBox((HWND)xParam,
1303 :     TEXT("Error: One or more input files could not be successfully converted!"), APP_NAME, MB_OK);
1304 :    
1305 :     EnableWindow(GetDlgItem(ghDlg, IDC_BUTTON_START), 1);
1306 :    
1307 :     CoUninitialize();
1308 :     return 0;
1309 :     }
1310 :    
1311 :     //////////////////////////////////////////////////////////////////////////////
1312 :    
1313 :     // Callback for GetOpenFileName hook
1314 :     unsigned int CALLBACK
1315 :     DialogHook(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1316 :     {
1317 :     static HWND hwndParentDialog;
1318 :     LPOFNOTIFY lpofn;
1319 :     int cbLength;
1320 :     static LPTSTR lpsz;
1321 :     static int LastLen;
1322 :    
1323 :     switch (uMsg)
1324 :     {
1325 :     case WM_INITDIALOG:
1326 :     if(!SetProp(GetParent(hwnd), TEXT("OFN"), (void *) lParam))
1327 :     MessageBox(0, TEXT("SetProp() Failed"), APP_NAME, MB_OK);
1328 :     return 0;
1329 :    
1330 :     case WM_COMMAND:
1331 :     break;
1332 :    
1333 :     case WM_NOTIFY:
1334 :     lpofn = (LPOFNOTIFY) lParam;
1335 :    
1336 :     switch (lpofn->hdr.code)
1337 :     {
1338 :     case CDN_SELCHANGE:
1339 :     LPOPENFILENAME lpofn;
1340 :     cbLength = CommDlg_OpenSave_GetSpec(GetParent(hwnd), NULL, 0);
1341 :     cbLength += _MAX_PATH;
1342 :    
1343 :     lpofn = (LPOPENFILENAME) GetProp(GetParent(hwnd), TEXT("OFN"));
1344 :    
1345 :     if ((int)lpofn->nMaxFile < cbLength)
1346 :     {
1347 :     // Free any previously allocated buffer.
1348 :     if(lpsz) {
1349 :     HeapFree(GetProcessHeap(), 0, lpsz);
1350 :     }
1351 :     // Allocate a new buffer
1352 :     lpsz = (LPTSTR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbLength*sizeof(TCHAR));
1353 :     if (lpsz)
1354 :     {
1355 :     lpofn->lpstrFile = lpsz;
1356 :     lpofn->nMaxFile = cbLength;
1357 :     }
1358 :     }
1359 :     break;
1360 :     }
1361 :     return 0;
1362 :    
1363 :     case WM_DESTROY:
1364 :     RemoveProp(GetParent(hwnd), TEXT("OFN"));
1365 :     return 0;
1366 :     }
1367 :    
1368 :     return 0;
1369 :     }
1370 :    
1371 :     //////////////////////////////////////////////////////////////////////////////
1372 :    
1373 :     BOOL
1374 :     GetFolderSelection(HWND hWnd, LPTSTR szBuf, LPCTSTR szTitle)
1375 :     {
1376 :     LPITEMIDLIST pidl = NULL;
1377 :     BROWSEINFO bi = { 0 };
1378 :     BOOL bResult = FALSE;
1379 :    
1380 :     bi.hwndOwner = hWnd;
1381 :     bi.pszDisplayName = szBuf;
1382 :     bi.pidlRoot = NULL;
1383 :     bi.lpszTitle = szTitle;
1384 :     bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
1385 :    
1386 :     if ((pidl = SHBrowseForFolder(&bi)) != NULL)
1387 :     {
1388 :     bResult = SHGetPathFromIDList(pidl, szBuf);
1389 :     CoTaskMemFree(pidl);
1390 :     }
1391 :    
1392 :     return bResult;
1393 :     }
1394 :    
1395 :     //////////////////////////////////////////////////////////////////////////////
1396 :    
1397 :     LRESULT CALLBACK
1398 :     DIALOG_MAIN(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1399 :     {
1400 :     static HICON hIcon;
1401 :    
1402 :     switch (message)
1403 :     {
1404 :     case WM_INITDIALOG:
1405 :     HWND hwndOwner;
1406 :     RECT rc, rcDlg, rcOwner;
1407 :    
1408 :     if ((hwndOwner = GetParent(hDlg)) == NULL)
1409 :     {
1410 :     hwndOwner = GetDesktopWindow();
1411 :     }
1412 :    
1413 :     GetWindowRect(hwndOwner, &rcOwner);
1414 :     GetWindowRect(hDlg, &rcDlg);
1415 :     CopyRect(&rc, &rcOwner);
1416 :    
1417 :     OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1418 :     OffsetRect(&rc, -rc.left, -rc.top);
1419 :     OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1420 :    
1421 :     SetWindowPos(hDlg, HWND_TOP,
1422 :     rcOwner.left + (rc.right / 2),
1423 :     rcOwner.top + (rc.bottom / 2),
1424 :     0, 0, SWP_NOSIZE);
1425 :    
1426 :     hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 32, 32, 0);
1427 :     SendMessage(hDlg, WM_SETICON, ICON_BIG , (LPARAM)hIcon);
1428 :     ghDlg = hDlg;
1429 :     return TRUE;
1430 :    
1431 :     case WM_DESTROY:
1432 :     DestroyIcon(hIcon);
1433 :     PostQuitMessage(0);
1434 :     break;
1435 :    
1436 :     case WM_COMMAND:
1437 : Isibaar 2029 if (LOWORD(wParam) == IDCANCEL) {
1438 : Irhall 2014 EndDialog(hDlg, LOWORD(wParam));
1439 :     return TRUE;
1440 :     }
1441 :     else if (LOWORD(wParam) == IDC_BUTTON_SRC) {
1442 :     sOfn.hwndOwner = hDlg;
1443 :    
1444 :     GetDlgItemText(hDlg, IDC_EDIT_SRC, OpenFilePath, sizeof(OpenFilePath)/sizeof(OpenFilePath));
1445 :    
1446 : Isibaar 2019 if (_tcslen(sOfn.lpstrFile) == 0) {
1447 :     _stprintf(sOfn.lpstrFile, TEXT("%s"), OpenFilePath);
1448 : Irhall 2014 }
1449 :    
1450 :     if (GetOpenFileName(&sOfn)) {
1451 : Isibaar 2019 if(sOfn.lpstrFile[_tcslen(sOfn.lpstrFile) + 1] != TEXT('\0')) // Multiple input files?
1452 :     _stprintf(OpenFilePath, TEXT("%s (multiselect)"), sOfn.lpstrFile);
1453 : Irhall 2014 else
1454 : Isibaar 2019 _stprintf(OpenFilePath, TEXT("%s"), sOfn.lpstrFile);
1455 : Irhall 2014
1456 :     SetDlgItemText(hDlg, IDC_EDIT_SRC, OpenFilePath);
1457 :     }
1458 :     }
1459 :     else if (LOWORD(wParam) == IDC_BUTTON_DST) {
1460 :     sSfn.hwndOwner = hDlg;
1461 :     GetDlgItemText(hDlg, IDC_EDIT_DST, SaveFilePath, sizeof(SaveFilePath)/sizeof(SaveFilePath[0]));
1462 :     if (GetFolderSelection(hDlg, SaveFilePath, TEXT("Please select the output folder"))) {
1463 :     SetDlgItemText(hDlg, IDC_EDIT_DST, SaveFilePath);
1464 :     }
1465 :     }
1466 :     else if (LOWORD(wParam) == IDC_BUTTON_START) {
1467 :     GetDlgItemText(hDlg, IDC_EDIT_DST, SaveFilePath, sizeof(SaveFilePath)/sizeof(SaveFilePath[0]));
1468 :     GetDlgItemText(hDlg, IDC_EDIT_SRC, OpenFilePath, sizeof(OpenFilePath)/sizeof(OpenFilePath[0]));
1469 :    
1470 : Isibaar 2019 if ((_tcslen(OpenFilePath) > 0 || _tcslen(sOfn.lpstrFile) > 0) && _tcslen(SaveFilePath) > 0) {
1471 : Irhall 2014 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_START), 0);
1472 : Isibaar 2019 if (_tcslen(sOfn.lpstrFile) > 0)
1473 : Irhall 2014 SrcFile = sOfn.lpstrFile;
1474 :     else {
1475 : Isibaar 2019 OpenFilePath[_tcslen(OpenFilePath)+1] = TEXT('\0');
1476 : Irhall 2014 SrcFile = OpenFilePath;
1477 :     }
1478 :    
1479 : Isibaar 2019 _tcscpy(DstFile, SaveFilePath);
1480 : Irhall 2014 hConvertThread = CreateThread(0, 0, RecompressThreadProc, GetDlgItem(hDlg, IDC_PROGRESSBAR), 0, 0);
1481 :     }
1482 :     else
1483 :     MessageBox(ghDlg, TEXT("Error: You need to select a source file and output directory first!"), APP_NAME, 0);
1484 :     }
1485 :     break;
1486 :     }
1487 :     return FALSE;
1488 :     }
1489 :    
1490 :    
1491 :     int APIENTRY
1492 :     _tWinMain(HINSTANCE hInstance,
1493 :     HINSTANCE hPrevInstance,
1494 :     LPTSTR lpCmdLine,
1495 :     int nCmdShow)
1496 :     {
1497 :     CoInitialize(0);
1498 :    
1499 :     static TCHAR buf[2*MAX_PATH+1];
1500 :    
1501 :     sOfn.lStructSize = sizeof (OPENFILENAME);
1502 :     sOfn.hInstance = hInstance;
1503 :     #ifdef ALLOW_ONLY_AVI_INPUT
1504 :     sOfn.lpstrFilter = TEXT("Media files (*.avi;*.divx)\0*.avi;*.divx\0All files\0*.*\0");
1505 :     #else
1506 :     sOfn.lpstrFilter = TEXT("Media files (*.avi;*.divx;*.mp4;*.mov;*.wmv;*.asf;*.mkv)\0*.avi;*.mp4;*.mov;*.wmv;*.asf;*.divx;*.mkv\0All files\0*.*\0");
1507 :     #endif
1508 :     sOfn.lpstrCustomFilter = 0;
1509 :     sOfn.nMaxCustFilter = 0;
1510 :     sOfn.nFilterIndex = 1;
1511 :     sOfn.lpstrFileTitle = 0;
1512 :     sOfn.nMaxFileTitle = 0;
1513 :     sOfn.lpstrInitialDir = 0;
1514 :     sOfn.lpstrTitle = TEXT("Select source file");
1515 :     sOfn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1516 :     sOfn.nFileOffset = 0;
1517 :     sOfn.nFileExtension = 0;
1518 :     sOfn.lpstrDefExt = TEXT(".avi");
1519 :     sOfn.lCustData = (LPARAM)(&sOfn);
1520 :     sOfn.lpfnHook = DialogHook;
1521 :     sOfn.lpTemplateName = 0;
1522 :     sOfn.pvReserved = 0;
1523 :     sOfn.dwReserved = 0;
1524 :     sOfn.FlagsEx = 0;
1525 :     sOfn.lpstrFile = buf;
1526 :     sOfn.nMaxFile = 2*MAX_PATH;
1527 :     sOfn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_ENABLEHOOK;
1528 :    
1529 :    
1530 :     sSfn.lStructSize = sizeof (OPENFILENAME);
1531 :     sSfn.hInstance = hInstance;
1532 :     sSfn.lpstrFilter = TEXT("Avi files (*.avi)\0*.avi\0");
1533 :     sSfn.lpstrCustomFilter = 0;
1534 :     sSfn.nMaxCustFilter = 0;
1535 :     sSfn.nFilterIndex = 1;
1536 :     sSfn.lpstrFileTitle = 0;
1537 :     sSfn.nMaxFileTitle = 0;
1538 :     sSfn.lpstrInitialDir = 0;
1539 :     sSfn.lpstrTitle = TEXT("Select target file");
1540 :     sSfn.nFileOffset = 0;
1541 :     sSfn.nFileExtension = 0;
1542 :     sSfn.lpstrDefExt = TEXT(".avi");
1543 :     sSfn.lpTemplateName = 0;
1544 :     sSfn.pvReserved = 0;
1545 :     sSfn.dwReserved = 0;
1546 :     sSfn.FlagsEx = 0;
1547 :    
1548 :     pRec = new RecompressGraph();
1549 :     DialogBox(hInstance, (LPCTSTR)IDD_DIALOG_MAIN, NULL, (DLGPROC)DIALOG_MAIN);
1550 :     pRec->BreakConversion();
1551 :     if (hConvertThread != NULL) {
1552 :     WaitForSingleObject(hConvertThread, INFINITE);
1553 :     CloseHandle(hConvertThread);
1554 :     }
1555 :    
1556 :     delete pRec;
1557 :     CoUninitialize();
1558 :    
1559 :     return 0;
1560 :     }
1561 :    

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4