summaryrefslogtreecommitdiff
path: root/src/OpenGl/OpenGl_AVIWriter.cxx
blob: 6d76bff00b6438867da67c07ece26da84fa88e88 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// File:      OpenGl_AVIWriter.cxx
// Created:   15.04.07 13:44
// Author:    Alexey MORENOV & Alexander GRIGORIEV
// Copyright: Open CASCADE 2007

#include <OpenGl_AVIWriter.hxx>

#ifdef WNT

#ifdef _MSC_VER
// The AVI writer requires Video For Windows library 
#pragma comment(lib,"Vfw32.lib")
#endif

OpenGl_AVIWriter* OpenGl_AVIWriter::MyAVIWriterInstance = 0L;

OpenGl_AVIWriter * OpenGl_AVIWriter::GetInstance()
{
  return MyAVIWriterInstance;
}

//=======================================================================
//function : OpenGl_AVIWriter
//purpose  : 
//=======================================================================

Standard_EXPORT OpenGl_AVIWriter::OpenGl_AVIWriter
                 (const char *     theFileName, 
                  DWORD            dwCodec /* = mmioFOURCC('M','P','G','4') */,
                  Standard_Integer theFrameRate /* = 25 */)
  : myhHeap             (0L),
    myhWindow           (0L),
    myhAviDC            (0L),
    mylpBits            (0L),
    mylSample           (0L),
    mypAviFile          (0L),
    mypAviStream        (0L),
    mypAviCompressedStream(0L),
    myFileName          (0L),
    myIsAllowRecord     (Standard_False),
    myAppendFuncSelector(1)         //0=Dummy       1=FirstTime     2=Usual
{
  ::AVIFileInit();
  if (theFileName != 0L && theFileName[0] != '\0') {

    const size_t aLen = strlen(theFileName) + 1;
    myFileName = new char [aLen];
    memcpy(myFileName, theFileName, aLen);
    myErrMsg = (Standard_CString)"Method Succeeded";

    pAppendFrame[0]= &OpenGl_AVIWriter::AppendDummy;
    pAppendFrame[1]= &OpenGl_AVIWriter::AppendFrameFirstTime;
    pAppendFrame[2]= &OpenGl_AVIWriter::AppendFrameUsual;

    pAppendFrameBits[0]=&OpenGl_AVIWriter::AppendDummyBits;
    pAppendFrameBits[1]=&OpenGl_AVIWriter::AppendFrameBitsFirstTime;
    pAppendFrameBits[2]=&OpenGl_AVIWriter::AppendFrameBitsUsual;

    MyAVIWriterInstance = this;

    ZeroMemory(&myAviStreamInfo,sizeof(AVISTREAMINFO));
    myAviStreamInfo.fccType             = streamtypeVIDEO;
    myAviStreamInfo.fccHandler          = dwCodec;
    myAviStreamInfo.dwScale             = 1;
    myAviStreamInfo.dwRate              = theFrameRate; // Frames Per Second;
    myAviStreamInfo.dwQuality           = 100;/*//-1;  // Default Quality*/

    ZeroMemory(&myAviCompressOptions,sizeof(AVICOMPRESSOPTIONS));
    myAviCompressOptions.fccType        = streamtypeVIDEO;
    myAviCompressOptions.fccHandler     = myAviStreamInfo.fccHandler;
    myAviCompressOptions.dwFlags        =
      AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID|AVICOMPRESSF_DATARATE;
    myAviCompressOptions.dwKeyFrameEvery  = 1;
    myAviCompressOptions.dwBytesPerSecond = 125000;
    myAviCompressOptions.dwQuality      = 100;
  }
}

//=======================================================================
//function : ~OpenGl_AVIWriter
//purpose  : 
//=======================================================================

Standard_EXPORT OpenGl_AVIWriter::~OpenGl_AVIWriter(void)
{
  ReleaseMemory();
  AVIFileExit();
  if (myFileName)
    delete [] myFileName;
  MyAVIWriterInstance = 0L;
}

//=======================================================================
//function : StartRecording
//purpose  : 
//=======================================================================

void OpenGl_AVIWriter::StartRecording(const HANDLE hWin)
{
  if (hWin != NULL)
    myhWindow = hWin;
  myIsAllowRecord = Standard_True;
}

//=======================================================================
//function : StopRecording
//purpose  : 
//=======================================================================

void OpenGl_AVIWriter::StopRecording()
{
  myIsAllowRecord = Standard_False;
}

//=======================================================================
//function : ReleaseMemory
//purpose  : 
//=======================================================================

void OpenGl_AVIWriter::ReleaseMemory()
{
  myAppendFuncSelector=0;      //Point to DummyFunction

  if(myhAviDC)
  {
    DeleteDC(myhAviDC);
    myhAviDC=NULL;
  }
  if(mypAviCompressedStream)
  {
    AVIStreamRelease(mypAviCompressedStream);
    mypAviCompressedStream=NULL;
  }
  if(mypAviStream)
  {
    AVIStreamRelease(mypAviStream);
    mypAviStream=NULL;
  }
  if(mypAviFile)
  {
    AVIFileRelease(mypAviFile);
    mypAviFile=NULL;
  }
  if(mylpBits)
  {
    HeapFree(myhHeap,HEAP_NO_SERIALIZE,mylpBits);
    mylpBits=NULL;
  }
  if(myhHeap)
  {
    HeapDestroy(myhHeap);
    myhHeap=NULL;
  }
}

//=======================================================================
//function : SetErrorMessage
//purpose  : 
//=======================================================================

void OpenGl_AVIWriter::SetErrorMessage(const char * theErrorMessage)
{
  myErrMsg = (Standard_CString)theErrorMessage;
}

//=======================================================================
//function : InitMovieCreation
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::InitMovieCreation (int nFrameWidth,
                                             int nFrameHeight,
                                             int nBitsPerPixel)
{
  int  nMaxWidth=GetSystemMetrics(SM_CXSCREEN),
       nMaxHeight=GetSystemMetrics(SM_CYSCREEN);

  myhAviDC = CreateCompatibleDC(NULL);
  if(myhAviDC==NULL)
  {
    SetErrorMessage("Unable to Create Compatible DC");
    return E_FAIL;
  }

  if (nFrameWidth > nMaxWidth)
    nMaxWidth= nFrameWidth;
  if (nFrameHeight > nMaxHeight)
    nMaxHeight = nFrameHeight;

  myhHeap=HeapCreate(HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4, 0);
  if(myhHeap==NULL)
  {
    SetErrorMessage("Unable to Create Heap");
    return E_FAIL;
  }

  mylpBits=HeapAlloc(myhHeap, HEAP_ZERO_MEMORY|HEAP_NO_SERIALIZE,
                     nMaxWidth*nMaxHeight*4);
  if(mylpBits==NULL)
  {
    SetErrorMessage("Unable to Allocate Memory on Heap");
    return E_FAIL;
  }

  HRESULT hr;
  hr = ::AVIFileOpen(&mypAviFile, myFileName, OF_CREATE|OF_WRITE, NULL);
  if (!hr == AVIERR_OK)
  {
    SetErrorMessage("Unable to Create the Movie File");
    return E_FAIL;
  }
  /*
  if(FAILED(::AVIFileOpen(&mypAviFile, myszFileName, OF_CREATE|OF_WRITE, NULL)))
  {
    SetErrorMessage("Unable to Create the Movie File");
    return E_FAIL;
  }
  */

  myAviStreamInfo.dwSuggestedBufferSize = nMaxWidth * nMaxHeight * 4;
  SetRect(&myAviStreamInfo.rcFrame, 0, 0, nFrameWidth, nFrameHeight);
  strncpy(myAviStreamInfo.szName, "Video Stream", 64);

  if(FAILED(AVIFileCreateStream(mypAviFile,&mypAviStream,&myAviStreamInfo)))
  {
    SetErrorMessage("Unable to Create Video Stream in the Movie File");
    return E_FAIL;
  }

  if(FAILED(AVIMakeCompressedStream(&mypAviCompressedStream,
                                    mypAviStream,
                                    &myAviCompressOptions,
                                    NULL)))
  {
    // One reason this error might occur is if you are using a Codec that is not
    // available on your system. Check the mmioFOURCC() code you are using and
    // make sure you have that codec installed properly on your machine.
    SetErrorMessage("Unable to Create Compressed Stream: "
                    "Check your CODEC options");
    return E_FAIL;
  }

  BITMAPINFO bmpInfo;
  ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  bmpInfo.bmiHeader.biPlanes            = 1;
  bmpInfo.bmiHeader.biWidth             = nFrameWidth;
  bmpInfo.bmiHeader.biHeight            = nFrameHeight;
  bmpInfo.bmiHeader.biCompression       = BI_RGB;
  bmpInfo.bmiHeader.biBitCount          = (WORD)nBitsPerPixel;
  bmpInfo.bmiHeader.biSize              = sizeof(BITMAPINFOHEADER);
  bmpInfo.bmiHeader.biSizeImage         = (bmpInfo.bmiHeader.biWidth *
                                           bmpInfo.bmiHeader.biHeight*
                                           bmpInfo.bmiHeader.biBitCount)/8;

  if(FAILED(AVIStreamSetFormat(mypAviCompressedStream,
                               0,
                               (LPVOID)&bmpInfo,
                               bmpInfo.bmiHeader.biSize)))
  {
    // One reason this error might occur is if your bitmap does not meet
    // the Codec requirements.
    // For example, 
    //   your bitmap is 32bpp while the Codec supports only 16 or 24 bpp; Or
    //   your bitmap is 274 * 258 size, while the Codec supports only sizes
    //   that are powers of 2; etc...
    // Possible solution to avoid this is: make your bitmap suit the codec
    // requirements, or Choose a codec that is suitable for your bitmap.
    SetErrorMessage("Unable to Set Video Stream Format");
    return E_FAIL;
  }

  return S_OK;  // Everything went Fine
}

//=======================================================================
//function : AppendFrameFirstTime
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendFrameFirstTime(HBITMAP  hBitmap)
{
  BITMAP Bitmap;
  GetObject(hBitmap, sizeof(BITMAP), &Bitmap);

  if(SUCCEEDED(InitMovieCreation( Bitmap.bmWidth, 
                                  Bitmap.bmHeight, 
                                  Bitmap.bmBitsPixel)))
  {
    myAppendFuncSelector = 2;      //Point to the UsualAppend Function
    return AppendFrameUsual(hBitmap);
  }

  ReleaseMemory();
  return E_FAIL;
}

//=======================================================================
//function : AppendFrameUsual
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendFrameUsual(HBITMAP hBitmap)
{
  BITMAPINFO    bmpInfo;

  bmpInfo.bmiHeader.biBitCount=0;
  bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);

  GetDIBits(myhAviDC,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);

  bmpInfo.bmiHeader.biCompression=BI_RGB;

  GetDIBits(myhAviDC,
            hBitmap,
            0,
            bmpInfo.bmiHeader.biHeight,
            mylpBits,
            &bmpInfo,
            DIB_RGB_COLORS);

  if(FAILED(AVIStreamWrite(mypAviCompressedStream,
                           mylSample++,
                           1,
                           mylpBits,
                           bmpInfo.bmiHeader.biSizeImage,
                           0,
                           NULL,
                           NULL)))
  {
    SetErrorMessage("Unable to Write Video Stream to the output Movie File");
    ReleaseMemory();
    return E_FAIL;
  }

  return S_OK;
}

//=======================================================================
//function : AppendDummy
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendDummy(HBITMAP)
{
  return E_FAIL;
}

//=======================================================================
//function : AppendNewFrame
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendNewFrame(HBITMAP hBitmap)
{
  return (this->*pAppendFrame[myAppendFuncSelector])((HBITMAP)hBitmap);
}

//=======================================================================
//function : AppendNewFrame
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendNewFrame(int nWidth,
                                         int nHeight,
                                         LPVOID pBits,
                                         int nBitsPerPixel)
{
  return (this->*pAppendFrameBits[myAppendFuncSelector])(nWidth,
                                                          nHeight,
                                                          pBits,
                                                          nBitsPerPixel);
}

//=======================================================================
//function : AppendFrameFirstTime
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendFrameBitsFirstTime(int nWidth,
                                                   int nHeight,
                                                   LPVOID pBits,
                                                   int nBitsPerPixel)
{
  if(SUCCEEDED(InitMovieCreation(nWidth, nHeight, nBitsPerPixel)))
  {
    myAppendFuncSelector=2;    //Point to the UsualAppend Function
    return AppendFrameBitsUsual(nWidth, nHeight, pBits, nBitsPerPixel);
  }
  ReleaseMemory();

  return E_FAIL;
}

//=======================================================================
//function : AppendFrameUsual
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendFrameBitsUsual(int nWidth,
                                               int nHeight,
                                               LPVOID pBits,
                                               int nBitsPerPixel)
{
  DWORD dwSize=nWidth*nHeight*nBitsPerPixel/8;

  if(FAILED(AVIStreamWrite(mypAviCompressedStream,
                           mylSample++,
                           1,
                           pBits,
                           dwSize,
                           0,
                           NULL,
                           NULL)))
  {
    SetErrorMessage("Unable to Write Video Stream to the output Movie File");
    ReleaseMemory();
    return E_FAIL;
  }

  return S_OK;
}

//=======================================================================
//function : AppendDummy
//purpose  : 
//=======================================================================

HRESULT OpenGl_AVIWriter::AppendDummyBits(int nWidth,
                                          int nHeight,
                                          LPVOID pBits,
                                          int nBitsPerPixel)
{
  return E_FAIL;
}

//=======================================================================
//function : AviWriter
//purpose  : 
//=======================================================================

void OpenGl_AVIWriter_AVIWriter(void * pp,
                                int  nWidth,
                                int  nHeight,
                                int  nBitsPerPixel)
{
  if (OpenGl_AVIWriter::GetInstance() != 0L)
    if (OpenGl_AVIWriter::GetInstance()->IsRecording())
    {

      OpenGl_AVIWriter::GetInstance()->AppendNewFrame(nWidth,
                                                      nHeight,
                                                      pp,
                                                      nBitsPerPixel);
    }
}

//=======================================================================
//function : AllowWriting
//purpose  : 
//=======================================================================

Standard_Boolean OpenGl_AVIWriter_AllowWriting(void * hWin)
{
  Standard_Boolean aResult(Standard_False);
  const OpenGl_AVIWriter * anInst = OpenGl_AVIWriter::GetInstance();
  if (anInst != 0L) {
    if (hWin == NULL || anInst->HWindow() == hWin)
      aResult = static_cast<Standard_Boolean> (anInst->IsRecording());
  }
  return aResult;
}

#endif //WNT