forked from markessien/DirectEncode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncodeErdLib.cpp
412 lines (315 loc) · 12.4 KB
/
EncodeErdLib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/** This file is part of The Standard Mpeg Encoder.
The Standard Mpeg Encoder is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
The Standard Mpeg Encoder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Standard Mpeg Encoder.
If not, see <http://www.gnu.org/licenses/>.
**/
#include "stdafx.h"
#include "encodeerdlib.h"
// #include <ios>
//#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
// int CEncodeErdLib::m_nFileHandle = 0;
CEncodeErdLib::CEncodeErdLib(void) : m_hEncodeDLL(NULL),
m_pLicense(NULL),
m_pCallback(NULL),
m_nFileHandle(0),
m_nContext(0)
{
}
CEncodeErdLib::~CEncodeErdLib(void)
{
mediaFilePassThruCmd_MT(m_nContext, 0, 0, 1001);
_ASSERT(m_pLicense == NULL);
mediaFileUninitialize_MT(m_nContext);
}
HRESULT CEncodeErdLib::EncodeAudioSample(IMediaSample *pSample)
{
// Pass an audio sample to the encoding dll
if (pSample == NULL || mediaFileWriteAudio == NULL || m_bWritingAudio == false)
return E_POINTER;
BYTE* ptr = NULL;
HRESULT hr = pSample->GetPointer(&ptr);
if (FAILED(hr) || ptr == NULL)
return E_POINTER;
long nLen = pSample->GetActualDataLength();
LONGLONG llStart = 0, llEnd = 0;
pSample->GetTime(&llStart, &llEnd);
hr = (HRESULT)mediaFileWriteAudio_MT(m_nContext, ptr, nLen, llStart, llEnd);
return hr;
}
HRESULT CEncodeErdLib::EndOfStream()
{
if (mediaFileEndOfStream_MT == NULL)
return E_FAIL;
HRESULT hr = (HRESULT)mediaFileEndOfStream_MT(m_nContext);
return hr;
}
HRESULT CEncodeErdLib::EncodeVideoSample(IMediaSample *pSample)
{
// Flip a video sample, then pass to the encoding dll
if (pSample == NULL || mediaFileWriteVideoRGB24_MT == NULL || m_pBuffer == NULL || m_bWritingVideo == false)
return E_POINTER;
BYTE* ptr = NULL;
HRESULT hr = pSample->GetPointer(&ptr);
if (FAILED(hr) || ptr == NULL)
{
_ASSERT(FALSE);
return E_POINTER;
}
long nLen = pSample->GetActualDataLength();
if (FAILED(Flip(ptr, nLen)))
return S_FALSE;
LONGLONG llStart = 0, llEnd = 0;
pSample->GetTime(&llStart, &llEnd);
#ifdef _DEBUG
double dSecs = llStart / UNITS;
double dEnd = llEnd / UNITS;
#endif
hr = (HRESULT)mediaFileWriteVideoRGB24_MT(m_nContext, m_pBuffer, nLen, llStart, llEnd);
return hr;
}
HRESULT CEncodeErdLib::StartConversion()
{
if (m_pEncodeProfile == NULL)
{
_ASSERT(m_pEncodeProfile);
return E_POINTER;
}
long nSourceWidth = m_SourceSettings.nSourceWidth;
long nSourceHeight = m_SourceSettings.nSourceHeight;
EncodeParameters& encsettings = m_pEncodeProfile->GetEncodeParameters();
memcpy(&encsettings.wfexAudioSrc, &m_SourceSettings.wfexSrc, sizeof(WAVEFORMATEX));
encsettings.bInputHasAudio = m_SourceSettings.bHasAudio;
if (encsettings.audioOutputFormat == AUDIOFORMAT_NONE)
encsettings.bWriteAudioStream = false;
else
encsettings.bWriteAudioStream = m_SourceSettings.bHasAudio;
if (encsettings.videoOutputFormat == VIDEOFORMAT_NONE)
encsettings.bWriteVideoStream = false;
else
encsettings.bWriteVideoStream = m_SourceSettings.bHasVideo;
encsettings.bUseVideoBitrates = true;
encsettings.bUseAudioBitrates = true;
encsettings.bAutoSelectBitrate = false;
m_bWritingAudio = encsettings.bWriteAudioStream;
m_bWritingVideo = encsettings.bWriteVideoStream;
// encsettings.bWriteSystemsFile = true;
encsettings.bInputHasVideo = m_SourceSettings.bHasVideo;
encsettings.nInputX = nSourceWidth;
encsettings.nInputY = nSourceHeight;
if (encsettings.dInputFPS == 0.0f)
encsettings.dInputFPS = m_SourceSettings.dSourceFPS;
_ASSERT(mediaFilePassThruCmd_MT);
if (mediaFilePassThruCmd_MT == NULL)
{
return E_POINTER;
}
USES_CONVERSION;
strcpy_s(encsettings.szOutputFile, 1022, W2A(m_szOutputFile));
if (m_pLicense == NULL)
{
m_pLicense = new CLicense();
mediaFilePassThruCmd_MT(m_nContext, 0, (unsigned char*)m_pLicense->GenerateLicense(), 100000);
delete m_pLicense;
m_pLicense = NULL;
}
mediaFileSetBufferCallback_MT(m_nContext, (unsigned char*)this, WriteToFile);
mediaFileSetEncodeParameters_MT(m_nContext, &encsettings);
HRESULT hr = (HRESULT)mediaFileCreate_MT(m_nContext);
if (FAILED(hr))
return hr;
long nBufferWidth = max(encsettings.nOutputX, nSourceWidth);
long nBufferHeight = max(encsettings.nOutputY, nSourceHeight);
m_pBuffer = new BYTE[nBufferWidth*nBufferHeight*4];
memset(m_pBuffer, 0, nBufferWidth*nBufferHeight*4);
return S_OK;
}
HRESULT CEncodeErdLib::StopConversion()
{
// TODO : This call takes a long time, why block other thread?
if (mediaFileClose_MT)
mediaFileClose_MT(m_nContext);
if (m_pBuffer)
delete [] m_pBuffer;
m_pBuffer = NULL;
if (m_hEncodeDLL != NULL)
{
// FreeLibrary(m_hEncodeDLL);
// m_hEncodeDLL = NULL;
}
return S_OK;
}
long CEncodeErdLib::GetWrittenBytes()
{
if (mediaFilePassThruCmd_MT)
{
return (long)mediaFilePassThruCmd_MT(m_nContext, "fs", 0, 0);
}
return 0;
}
signed __int64 __cdecl CEncodeErdLib::WriteToFile(unsigned char* pUserData, unsigned char* pBuf, int nBufLen, int flags, signed __int64 newPos, int nID)
{
CEncodeErdLib* pThis = (CEncodeErdLib*)pUserData;
// sorry for this code, had to do some testing
switch (flags)
{
case 0: // open
#if defined(TEST_MANUAL_WRITE)
m_nFileHandle = _open("c:\\test_grab.mpg", _O_WRONLY | _O_CREAT);
#endif
break;
case 1: // write
#if defined(TEST_MANUAL_WRITE)
_write(m_nFileHandle, pBuf, nBufLen);
#else
pThis->m_pCallback->Deliver(pBuf, nBufLen, nID);
#endif
break;
case 2: // close
#if defined(TEST_MANUAL_WRITE)
_close(m_nFileHandle);
#endif
break;
case 3: // seek
#if defined(TEST_MANUAL_WRITE)
return _lseek(m_nFileHandle, newPos, SEEK_SET);
#else
{
// if connected to a memory based filter, this will fail
// -> streaming would not be possible with mp4 files
// same with sample grabbing
return pThis->m_pCallback->SeekOutput(newPos, nID);
}
#endif
}
TraceVerbose("WriteToFile exit\n");
return 0;
}
bool CEncodeErdLib::SetVideoEncoder(char* pstrPath)
{
if (m_hEncodeDLL != NULL)
{
// this is a work around to make sure that the encoder
// dll is reloaded. This resets all the parameters and
// makes multiple mpeg-4 conversions work
FreeLibrary(m_hEncodeDLL);
m_hEncodeDLL = NULL;
}
// load the libraries
/*
HINSTANCE hTmp = LoadLibrary("avutilerd-49.dll");
hTmp = LoadLibrary("avformaterd-52.dll");
hTmp = LoadLibrary("avcodecerd-51.dll");
hTmp = LoadLibrary("libfaac.dll");
*/
if (strchr(pstrPath, '\\') == NULL)
{
// in this case, we received a filename, and
// not a pathname. We load the dll that is in
// the module path first, before we use the standard
// search path
// _ASSERT(FALSE); // verify this!
TCHAR szProgPath[1024] = {0};
GetModuleFileName(NULL, szProgPath, 1023);
// MessageBox(0, szProgPath, 0, 0);
TCHAR* p = PathFindFileName(szProgPath);
if (p && p != szProgPath)
p[0] = 0;
_tcscat_s(szProgPath, 1024, pstrPath);
// MessageBox(0, szProgPath, 0, 0);
m_hEncodeDLL = LoadLibrary(szProgPath);
if (m_hEncodeDLL == NULL)
{
#ifdef _DEBUG
// m_hEncodeDLL = LoadLibrary(pstrPath);
m_hEncodeDLL = LoadLibrary("C:\\Mark\\Development\\DirectEncode\\Libraries\\erdmpglib\\erdmpg-6.dll");
// m_hEncodeDLL = LoadLibrary("C:\\Documents an d Settings\\Mark\\Desktop\\erdmpglib\\Debug\\erdmpg-6.debug.dll");
#endif
}
}
if (m_hEncodeDLL == NULL)
{
_ASSERT(FALSE);
m_hEncodeDLL = LoadLibrary(pstrPath);
if (m_hEncodeDLL == NULL)
{
char sz[1024];
strcpy_s(sz, 1024, "Encode library erdmpg-6.dll is not available. Please copy it to your program or system path.");
#ifndef VS6
sprintf(sz, "Encode library erdmpg-6.dll is not available. Please copy it to your program or system path. Error Number=%lu", GetLastError());
#endif
MessageBox(0, sz, "LoadLibrary Error", 0);
_ASSERT(m_hEncodeDLL);
return false;
}
}
mediaFileCreate = (MediaFileCreate) GetProcAddress(m_hEncodeDLL, "MediaFileCreate");
ASSERT(mediaFileCreate);
mediaFileInitialize = (MediaFileInitialize) GetProcAddress(m_hEncodeDLL, "MediaFileInitialize");
ASSERT(mediaFileInitialize);
mediaFileUninitialize = (MediaFileUninitialize) GetProcAddress(m_hEncodeDLL, "MediaFileUninitialize");
ASSERT(mediaFileUninitialize);
mediaFileClose = (MediaFileClose) GetProcAddress(m_hEncodeDLL, "MediaFileClose");
ASSERT(mediaFileClose);
mediaFileSetEncodeParameters = (MediaFileSetEncodeParameters) GetProcAddress(m_hEncodeDLL, "MediaFileSetEncodeParameters");
ASSERT(mediaFileSetEncodeParameters);
mediaFileWriteVideoBGR24 = (MediaFileWriteVideoBGR24) GetProcAddress(m_hEncodeDLL, "MediaFileWriteVideoBGR24");
ASSERT(mediaFileWriteVideoBGR24);
mediaFileWriteVideoRGB24 = (MediaFileWriteVideoRGB24) GetProcAddress(m_hEncodeDLL, "MediaFileWriteVideoRGB24");
ASSERT(mediaFileWriteVideoRGB24);
mediaFilePassThruCmd = (MediaFilePassThruCmd) GetProcAddress(m_hEncodeDLL, "MediaFilePassThruCmd");
ASSERT(mediaFilePassThruCmd);
mediaFilePassThruCmd(0, 0, 0);
mediaFileWriteVideoYUV420 = (MediaFileWriteVideoYUV420) GetProcAddress(m_hEncodeDLL, "MediaFileWriteVideoYUV420");
ASSERT(mediaFileWriteVideoYUV420);
mediaFileWriteAudio = (MediaFileWriteAudio) GetProcAddress(m_hEncodeDLL, "MediaFileWriteAudio");
ASSERT(mediaFileWriteAudio);
mediaFileSetBufferCallback = (MediaFileSetBufferCallback) GetProcAddress(m_hEncodeDLL, "MediaFileSetBufferCallback");
ASSERT(mediaFileSetBufferCallback);
mediaFileEndOfStream = (MediaFileEndOfStream) GetProcAddress(m_hEncodeDLL, "MediaFileEndOfStream");
ASSERT(mediaFileEndOfStream);
// Load new multithreaded functions
mediaFileCreate_MT = (MediaFileCreate_MT) GetProcAddress(m_hEncodeDLL, "MediaFileCreate_MT");
ASSERT(mediaFileCreate_MT);
if (mediaFileCreate_MT == NULL)
MessageBox(0, _T("This version of the encoder requires a newer erdmpg-6.dll"), 0, 0);
mediaFileClose_MT = (MediaFileClose_MT) GetProcAddress(m_hEncodeDLL, "MediaFileClose_MT");
ASSERT(mediaFileClose_MT);
mediaFileSetEncodeParameters_MT = (MediaFileSetEncodeParameters_MT) GetProcAddress(m_hEncodeDLL, "MediaFileSetEncodeParameters_MT");
ASSERT(mediaFileSetEncodeParameters_MT);
mediaFileWriteVideoRGB24_MT = (MediaFileWriteVideoRGB24_MT) GetProcAddress(m_hEncodeDLL, "MediaFileWriteVideoRGB24_MT");
ASSERT(mediaFileWriteVideoRGB24_MT);
mediaFileWriteAudio_MT = (MediaFileWriteAudio_MT) GetProcAddress(m_hEncodeDLL, "MediaFileWriteAudio_MT");
ASSERT(mediaFileWriteAudio_MT);
mediaFilePassThruCmd_MT = (MediaFilePassThruCmd_MT) GetProcAddress(m_hEncodeDLL, "MediaFilePassThruCmd_MT");
ASSERT(mediaFilePassThruCmd_MT);
mediaFileWriteAudio_MT = (MediaFileWriteAudio_MT) GetProcAddress(m_hEncodeDLL, "MediaFileWriteAudio_MT");
ASSERT(mediaFileWriteAudio_MT);
mediaFileSetBufferCallback_MT = (MediaFileSetBufferCallback_MT) GetProcAddress(m_hEncodeDLL, "MediaFileSetBufferCallback_MT");
ASSERT(mediaFileSetBufferCallback_MT);
mediaFileWriteAudio_MT = (MediaFileWriteAudio_MT) GetProcAddress(m_hEncodeDLL, "MediaFileWriteAudio_MT");
ASSERT(mediaFileWriteAudio_MT);
mediaFileInitialize_MT = (MediaFileInitialize_MT) GetProcAddress(m_hEncodeDLL, "MediaFileInitialize_MT");
ASSERT(mediaFileInitialize_MT);
mediaFileUninitialize_MT = (MediaFileUninitialize_MT) GetProcAddress(m_hEncodeDLL, "MediaFileUninitialize_MT");
ASSERT(mediaFileUninitialize_MT);
mediaFileEndOfStream_MT = (MediaFileEndOfStream_MT) GetProcAddress(m_hEncodeDLL, "MediaFileEndOfStream_MT");
ASSERT(mediaFileEndOfStream_MT);
mediaFileSetParameter_MT = (MediaFileSetParameter_MT) GetProcAddress(m_hEncodeDLL, "MediaFileSetParameter_MT");
ASSERT(mediaFileSetParameter_MT);
mediaFileSetFormat_MT = (MediaFileSetFormat_MT) GetProcAddress(m_hEncodeDLL, "MediaFileSetFormat_MT");
ASSERT(mediaFileSetFormat_MT);
// if (mediaFileUninitialize_MT)
// mediaFileUninitialize_MT();
m_nContext = mediaFileInitialize_MT();
return true;
}